RuneHive-Game
Loading...
Searching...
No Matches
com.runehive.net.packet.out.SendNpcUpdate Class Reference

The packet that's responsible for updating npcs. More...

Inheritance diagram for com.runehive.net.packet.out.SendNpcUpdate:
Collaboration diagram for com.runehive.net.packet.out.SendNpcUpdate:

Public Member Functions

boolean encode (Player player)
 SendNpcUpdate ()
Public Member Functions inherited from com.runehive.net.packet.OutgoingPacket
void execute (Player player)
 OutgoingPacket (int opcode, int capacity)
 OutgoingPacket (int opcode, PacketType type)
 OutgoingPacket (int opcode, PacketType type, int size)

Static Private Member Functions

static void addNewNpc (PacketBuilder packet, Player player, Npc npc)
static void appendAnimationMask (Npc npc, PacketBuilder maskBuf)
static void appendFaceCoordinateMask (Npc npc, PacketBuilder maskBuf)
static void appendFaceEntityMask (Npc npc, PacketBuilder maskBuf)
static void appendFirstHitMask (final Npc npc, final PacketBuilder updateBlock)
static void appendForceChatMask (Npc npc, PacketBuilder maskBuf)
static void appendGfxMask (Npc npc, PacketBuilder maskBuf)
static void appendSecondHitMask (final Npc npc, final PacketBuilder updateBlock)
static void appendTransformationMask (Npc npc, PacketBuilder maskBuf)
static void updateMovement (PacketBuilder packet, Npc npc)
static void updateNpc (PacketBuilder maskBuf, Npc npc)

Additional Inherited Members

Protected Attributes inherited from com.runehive.net.packet.OutgoingPacket
final PacketBuilder builder

Detailed Description

The packet that's responsible for updating npcs.

Author
nshusa

Definition at line 33 of file SendNpcUpdate.java.

Constructor & Destructor Documentation

◆ SendNpcUpdate()

com.runehive.net.packet.out.SendNpcUpdate.SendNpcUpdate ( )

Definition at line 35 of file SendNpcUpdate.java.

35 {
36 super(65, PacketType.VAR_SHORT);
37 }

References com.runehive.net.packet.PacketType.VAR_SHORT.

Member Function Documentation

◆ addNewNpc()

void com.runehive.net.packet.out.SendNpcUpdate.addNewNpc ( PacketBuilder packet,
Player player,
Npc npc )
staticprivate

Definition at line 103 of file SendNpcUpdate.java.

103 {
104 packet.writeBits(16, npc.getIndex()); // 16 bits needed since server supports 32000 NPCs
105 packet.writeBits(5, npc.getPosition().getY() - player.getPosition().getY());
106 packet.writeBits(5, npc.getPosition().getX() - player.getPosition().getX());
107 packet.writeBit(false); // discard walking queue
108 packet.writeBits(Config.NPC_BITS, npc.id);
109 packet.writeBit(npc.isUpdateRequired());
110 }

References com.runehive.game.world.entity.Entity.getIndex(), com.runehive.game.world.entity.Entity.getPosition(), com.runehive.game.world.position.Position.getX(), com.runehive.game.world.position.Position.getY(), com.runehive.game.world.entity.mob.Mob.id, com.runehive.game.world.entity.mob.Mob.isUpdateRequired(), and com.runehive.Config.NPC_BITS.

Referenced by encode().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ appendAnimationMask()

void com.runehive.net.packet.out.SendNpcUpdate.appendAnimationMask ( Npc npc,
PacketBuilder maskBuf )
staticprivate

Definition at line 195 of file SendNpcUpdate.java.

195 {
196 Animation anim = npc.getAnimation().orElse(Animation.RESET);
197 maskBuf.writeShort(anim.getId(), ByteOrder.LE)
198 .writeByte(anim.getDelay());
199 }

References com.runehive.game.world.entity.mob.Mob.getAnimation(), com.runehive.game.Animation.getDelay(), com.runehive.game.Animation.getId(), com.runehive.net.codec.ByteOrder.LE, com.runehive.game.Animation.RESET, com.runehive.net.packet.PacketBuilder.writeByte(), and com.runehive.net.packet.PacketBuilder.writeShort().

Referenced by updateNpc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ appendFaceCoordinateMask()

void com.runehive.net.packet.out.SendNpcUpdate.appendFaceCoordinateMask ( Npc npc,
PacketBuilder maskBuf )
staticprivate

Definition at line 225 of file SendNpcUpdate.java.

225 {
226 Position loc = npc.facePosition;
227 if (loc == null) {
228 Position currentPos = npc.getPosition();
229 Direction currentDir = npc.movement.lastDirection;
230 maskBuf.writeShort(((currentPos.getX() + currentDir.getDirectionX()) << 1) + 1, ByteOrder.LE)
231 .writeShort(((currentPos.getY() + currentDir.getDirectionY()) << 1) + 1, ByteOrder.LE);
232 } else {
233 maskBuf.writeShort((loc.getX() << 1) + 1, ByteOrder.LE)
234 .writeShort((loc.getY() << 1) + 1, ByteOrder.LE);
235 }
236 }

References com.runehive.game.world.entity.mob.Mob.facePosition, com.runehive.game.world.entity.mob.Direction.getDirectionX(), com.runehive.game.world.entity.mob.Direction.getDirectionY(), com.runehive.game.world.entity.Entity.getPosition(), com.runehive.game.world.position.Position.getX(), com.runehive.game.world.position.Position.getY(), com.runehive.game.world.entity.mob.movement.Movement.lastDirection, com.runehive.net.codec.ByteOrder.LE, com.runehive.game.world.entity.mob.Mob.movement, and com.runehive.net.packet.PacketBuilder.writeShort().

Referenced by updateNpc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ appendFaceEntityMask()

void com.runehive.net.packet.out.SendNpcUpdate.appendFaceEntityMask ( Npc npc,
PacketBuilder maskBuf )
staticprivate

Definition at line 207 of file SendNpcUpdate.java.

207 {
208 Mob mob = npc.interactingWith;
209 int index = 65535;
210 if (mob != null) {
211 index = mob.getIndex();
212 if (mob.isPlayer()) {
213 index += 32768;
214 }
215 maskBuf.writeShort(index);
216 } else {
217 maskBuf.writeShort(index);
218 }
219 }
val index

References com.runehive.game.world.entity.Entity.getIndex(), com.runehive.game.world.entity.mob.Mob.interactingWith, com.runehive.game.world.entity.mob.Mob.isPlayer(), and com.runehive.net.packet.PacketBuilder.writeShort().

Referenced by updateNpc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ appendFirstHitMask()

void com.runehive.net.packet.out.SendNpcUpdate.appendFirstHitMask ( final Npc npc,
final PacketBuilder updateBlock )
staticprivate

Definition at line 242 of file SendNpcUpdate.java.

242 {
243 Hit hit = npc.firstHit;
244
245 boolean multipleHits = hit.getMultipleHits() != null;
246 updateBlock.writeByte(multipleHits ? 1 : 0);
247
248 int max = npc.getMaximumHealth() >= 500 ? 200 : 100;
249 int health = npc.getCurrentHealth() * max / npc.getMaximumHealth();
250 if (health > max) health = max;
251
252 if(multipleHits) {
253 System.out.println("total 1 = " + hit.getMultipleHits().length);
254 updateBlock.writeByte(hit.getMultipleHits().length);
255
256 for(int index = 0; index < hit.getMultipleHits().length; index++) {
257 Hit currentHit = hit.getMultipleHits()[index];
258
259 int id = currentHit.getHitsplat().getId();
260
261 if (currentHit.getHitsplat() == Hitsplat.NORMAL && currentHit.getDamage() > 0) {
262 id++;
263 }
264
265 updateBlock.writeByte(currentHit.getDamage());
266 updateBlock.writeByte(id);
267 updateBlock.writeByte(currentHit.getHitIcon().getId());
268 }
269
270 } else {
271
272 int id = hit.getHitsplat().getId();
273
274 if (hit.getHitsplat() == Hitsplat.NORMAL && hit.getDamage() > 0) {
275 id++;
276 }
277
278 updateBlock.writeByte(hit.getDamage());
279 updateBlock.writeByte(id);
280 updateBlock.writeByte(hit.getHitIcon().getId());
281 }
282
283 updateBlock.writeByte(health);
284 updateBlock.writeByte(max);
285
286 /*int id = hit.getHitsplat().getId();
287 int max = npc.getMaximumHealth() >= 500 ? 200 : 100;
288 int health = max * npc.getCurrentHealth() / npc.getMaximumHealth();
289 if (health > max) health = max;
290
291 if (hit.getHitsplat() == Hitsplat.NORMAL && hit.getDamage() > 0) {
292 id++;
293 }
294
295 updateBlock.writeByte(hit.getDamage());
296 updateBlock.writeByte(id, ByteModification.ADD);
297 updateBlock.writeByte(hit.getHitIcon().getId());
298 updateBlock.writeByte(health);
299 updateBlock.writeByte(max, ByteModification.NEG);*/
300 }

References com.runehive.game.world.entity.mob.Mob.firstHit, com.runehive.game.world.entity.mob.Mob.getCurrentHealth(), com.runehive.game.world.entity.combat.hit.Hit.getDamage(), com.runehive.game.world.entity.combat.hit.Hit.getHitIcon(), com.runehive.game.world.entity.combat.hit.Hit.getHitsplat(), com.runehive.game.world.entity.combat.hit.HitIcon.getId(), com.runehive.game.world.entity.combat.hit.Hitsplat.getId(), com.runehive.game.world.entity.mob.Mob.getMaximumHealth(), com.runehive.game.world.entity.combat.hit.Hit.getMultipleHits(), com.runehive.game.world.entity.combat.hit.Hitsplat.NORMAL, and com.runehive.net.packet.PacketBuilder.writeByte().

Referenced by updateNpc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ appendForceChatMask()

void com.runehive.net.packet.out.SendNpcUpdate.appendForceChatMask ( Npc npc,
PacketBuilder maskBuf )
staticprivate

Definition at line 221 of file SendNpcUpdate.java.

221 {
222 maskBuf.writeString(npc.forceChat);
223 }

References com.runehive.game.world.entity.mob.Mob.forceChat, and com.runehive.net.packet.PacketBuilder.writeString().

Referenced by updateNpc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ appendGfxMask()

void com.runehive.net.packet.out.SendNpcUpdate.appendGfxMask ( Npc npc,
PacketBuilder maskBuf )
staticprivate

Definition at line 201 of file SendNpcUpdate.java.

201 {
202 Graphic gfx = npc.getGraphic().orElse(Graphic.RESET);
203 maskBuf.writeShort(gfx.getId())
204 .writeInt(gfx.getDelay() | gfx.getHeight());
205 }

References com.runehive.game.Graphic.getDelay(), com.runehive.game.world.entity.mob.Mob.getGraphic(), com.runehive.game.Graphic.getHeight(), com.runehive.game.Graphic.getId(), com.runehive.game.Graphic.RESET, com.runehive.net.packet.PacketBuilder.writeInt(), and com.runehive.net.packet.PacketBuilder.writeShort().

Referenced by updateNpc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ appendSecondHitMask()

void com.runehive.net.packet.out.SendNpcUpdate.appendSecondHitMask ( final Npc npc,
final PacketBuilder updateBlock )
staticprivate

Definition at line 302 of file SendNpcUpdate.java.

302 {
303 Hit hit = npc.secondHit;
304
305 Hit[] multipleHits = hit.getMultipleHits();
306 boolean isMultipleHits = multipleHits != null;
307 Hit[] multipleHitsArray = isMultipleHits ? Arrays.copyOf(multipleHits, multipleHits.length) : null;
308 updateBlock.writeByte(isMultipleHits ? 1 : 0);
309
310 int max = npc.getMaximumHealth() >= 500 ? 200 : 100;
311 int health = npc.getCurrentHealth() * max / npc.getMaximumHealth();
312 if (health > max) health = max;
313
314 if(isMultipleHits) {
315 updateBlock.writeByte(multipleHitsArray.length);
316 for (Hit currentHit : multipleHitsArray) {
317 int id = currentHit.getHitsplat().getId();
318
319 if (currentHit.getHitsplat() == Hitsplat.NORMAL
320 && currentHit.getDamage() > 0) {
321 id++;
322 }
323
324 updateBlock.writeByte(currentHit.getDamage());
325 updateBlock.writeByte(id);
326 updateBlock.writeByte(currentHit.getHitIcon().getId());
327 }
328 } else {
329
330 int id = hit.getHitsplat().getId();
331
332 if (hit.getHitsplat() == Hitsplat.NORMAL && hit.getDamage() > 0) {
333 id++;
334 }
335
336 updateBlock.writeByte(hit.getDamage());
337 updateBlock.writeByte(id);
338 updateBlock.writeByte(hit.getHitIcon().getId());
339 }
340
341 updateBlock.writeByte(health);
342 updateBlock.writeByte(max);
343 /*int id = hit.getHitsplat().getId();
344 int max = npc.getMaximumHealth() >= 500 ? 200 : 100;
345 int health = npc.getCurrentHealth() * max / npc.getMaximumHealth();
346 if (health > max) health = max;
347
348 if (hit.getHitsplat() == Hitsplat.NORMAL && hit.getDamage() > 0) {
349 id++;
350 }
351
352 updateBlock.writeByte(hit.getDamage());
353 updateBlock.writeByte(id, ByteModification.SUB);
354 updateBlock.writeByte(hit.getHitIcon().getId());
355 updateBlock.writeByte(health);
356 updateBlock.writeByte(max, ByteModification.NEG);*/
357 }

References com.runehive.game.world.entity.mob.Mob.getCurrentHealth(), com.runehive.game.world.entity.combat.hit.Hit.getDamage(), com.runehive.game.world.entity.combat.hit.Hit.getHitIcon(), com.runehive.game.world.entity.combat.hit.Hit.getHitsplat(), com.runehive.game.world.entity.combat.hit.HitIcon.getId(), com.runehive.game.world.entity.combat.hit.Hitsplat.getId(), com.runehive.game.world.entity.mob.Mob.getMaximumHealth(), com.runehive.game.world.entity.combat.hit.Hit.getMultipleHits(), com.runehive.game.world.entity.combat.hit.Hitsplat.NORMAL, com.runehive.game.world.entity.mob.Mob.secondHit, and com.runehive.net.packet.PacketBuilder.writeByte().

Referenced by updateNpc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ appendTransformationMask()

void com.runehive.net.packet.out.SendNpcUpdate.appendTransformationMask ( Npc npc,
PacketBuilder maskBuf )
staticprivate

Definition at line 238 of file SendNpcUpdate.java.

238 {
239 maskBuf.writeShort(npc.id, ByteModification.ADD, ByteOrder.LE);
240 }

References com.runehive.net.codec.ByteModification.ADD, com.runehive.game.world.entity.mob.Mob.id, com.runehive.net.codec.ByteOrder.LE, and com.runehive.net.packet.PacketBuilder.writeShort().

Referenced by updateNpc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ encode()

boolean com.runehive.net.packet.out.SendNpcUpdate.encode ( Player player)

Reimplemented from com.runehive.net.packet.OutgoingPacket.

Definition at line 40 of file SendNpcUpdate.java.

40 {
41 PacketBuilder maskBuf = PacketBuilder.alloc();
42 try {
43 builder.initializeAccess(AccessType.BIT);
44 builder.writeBits(8, player.viewport.getNpcsInViewport().size());
45
46 for (Iterator<Npc> itr = player.viewport.getNpcsInViewport().iterator(); itr.hasNext(); ) {
47
48 Npc npc = itr.next();
49
50 if (player.viewport.shouldRemove(npc)) {
51 if (npc.atomicPlayerCount.decrementAndGet() < 0) {
52 npc.atomicPlayerCount.set(0);
53 }
54 itr.remove();
55 builder.writeBits(1, 1);
56 builder.writeBits(2, 3);
57 } else {
58 updateMovement(builder, npc);
59
60 if (npc.isUpdateRequired()) {
61 updateNpc(maskBuf, npc);
62 }
63 }
64
65 }
66
67 int npcsAdded = 0;
68
69 for (Npc localNpc : World.getRegions().getLocalNpcs(player)) {
70
71 if (player.viewport.getNpcsInViewport().size() >= Viewport.CAPACITY || npcsAdded == Viewport.ADD_THRESHOLD) {
72 break;
73 }
74
75 if (player.viewport.add(localNpc)) {
76 npcsAdded++;
77 addNewNpc(builder, player, localNpc);
78 updateNpc(maskBuf, localNpc);
79
80 if (localNpc.atomicPlayerCount.incrementAndGet() < 0) {
81 localNpc.atomicPlayerCount.set(0);
82 }
83 }
84
85 }
86
87 if (maskBuf.content().readableBytes() > 0) {
88 builder.writeBits(16, 65535);
89 builder.initializeAccess(AccessType.BYTE);
90 builder.writeBytes(maskBuf.content());
91 } else {
92 builder.initializeAccess(AccessType.BYTE);
93 }
94
95 } catch (Exception ex) {
96 ex.printStackTrace();
97 } finally {
98 maskBuf.release();
99 }
100 return true;
101 }

References com.runehive.game.world.entity.mob.Viewport.add(), com.runehive.game.world.entity.mob.Viewport.ADD_THRESHOLD, addNewNpc(), com.runehive.net.packet.PacketBuilder.alloc(), com.runehive.game.world.entity.mob.npc.Npc.atomicPlayerCount, com.runehive.net.codec.AccessType.BIT, com.runehive.net.packet.OutgoingPacket.builder, com.runehive.net.codec.AccessType.BYTE, com.runehive.game.world.entity.mob.Viewport.CAPACITY, com.runehive.game.world.region.RegionManager.getLocalNpcs(), com.runehive.game.world.entity.mob.Viewport.getNpcsInViewport(), com.runehive.game.world.World.getRegions(), com.runehive.game.world.entity.mob.Mob.isUpdateRequired(), com.runehive.game.world.entity.mob.Viewport.shouldRemove(), updateMovement(), updateNpc(), and com.runehive.game.world.entity.mob.player.Player.viewport.

Here is the call graph for this function:

◆ updateMovement()

void com.runehive.net.packet.out.SendNpcUpdate.updateMovement ( PacketBuilder packet,
Npc npc )
staticprivate

Definition at line 112 of file SendNpcUpdate.java.

112 {
113 final boolean updateRequired = npc.isUpdateRequired();
114
115 final Movement movement = npc.movement;
116 final int runDirection = movement.getRunningDirection();
117 final int walkDirection = movement.getWalkingDirection();
118
119 if (runDirection != -1) {
120 packet.writeBit(true)
121 .writeBits(2, 2)
122 .writeBits(3, walkDirection)
123 .writeBits(3, runDirection)
124 .writeBit(updateRequired);
125 } else if (walkDirection != -1) {
126 packet.writeBit(true)
127 .writeBits(2, 1)
128 .writeBits(3, walkDirection)
129 .writeBit(updateRequired);
130 } else {
131 packet.writeBit(updateRequired);
132 if (updateRequired) {
133 packet.writeBits(2, 0);
134 }
135 }
136 }

References com.runehive.game.world.entity.mob.movement.Movement.getRunningDirection(), com.runehive.game.world.entity.mob.movement.Movement.getWalkingDirection(), com.runehive.game.world.entity.mob.Mob.isUpdateRequired(), and com.runehive.game.world.entity.mob.Mob.movement.

Referenced by encode().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ updateNpc()

void com.runehive.net.packet.out.SendNpcUpdate.updateNpc ( PacketBuilder maskBuf,
Npc npc )
staticprivate

Definition at line 138 of file SendNpcUpdate.java.

138 {
139 if (!npc.isUpdateRequired()) {
140 return;
141 }
142
143 int mask = 0;
144
145 final EnumSet<UpdateFlag> updateFlags = npc.updateFlags;
146 for (UpdateFlag flag : UpdateFlag.npcOrder) {
147 if (updateFlags.contains(flag) && flag.canApply(npc)) {
148 mask |= flag.npcMask;
149 }
150 }
151
152 maskBuf.writeByte(mask);
153
154 // TODO replace the checks.
155 /* for (UpdateFlag flag : UpdateFlag.npcOrder) {
156 if (UpdateFlag.containsNpc(mask, flag)) {
157
158 }
159 }*/
160
161 if (UpdateFlag.containsNpc(mask, UpdateFlag.ANIMATION)) {
162 appendAnimationMask(npc, maskBuf);
163 }
164
165 if (UpdateFlag.containsNpc(mask, UpdateFlag.GRAPHICS)) {
166 appendGfxMask(npc, maskBuf);
167 }
168
169 if (UpdateFlag.containsNpc(mask, UpdateFlag.INTERACT)) {
170 appendFaceEntityMask(npc, maskBuf);
171 }
172
173 if (UpdateFlag.containsNpc(mask, UpdateFlag.FORCED_CHAT)) {
174 appendForceChatMask(npc, maskBuf);
175 }
176
177 if (UpdateFlag.containsNpc(mask, UpdateFlag.FIRST_HIT)) {
178 appendFirstHitMask(npc, maskBuf);
179 }
180
181 if (UpdateFlag.containsNpc(mask, UpdateFlag.SECOND_HIT)) {
182 appendSecondHitMask(npc, maskBuf);
183 }
184
185 if (UpdateFlag.containsNpc(mask, UpdateFlag.TRANSFORM)) {
186 appendTransformationMask(npc, maskBuf);
187 }
188
189 if (UpdateFlag.containsNpc(mask, UpdateFlag.FACE_COORDINATE)) {
190 appendFaceCoordinateMask(npc, maskBuf);
191 }
192
193 }

References com.runehive.game.world.entity.mob.UpdateFlag.ANIMATION, appendAnimationMask(), appendFaceCoordinateMask(), appendFaceEntityMask(), appendFirstHitMask(), appendForceChatMask(), appendGfxMask(), appendSecondHitMask(), appendTransformationMask(), com.runehive.game.world.entity.mob.UpdateFlag.canApply(), com.runehive.game.world.entity.mob.UpdateFlag.containsNpc(), com.runehive.game.world.entity.mob.UpdateFlag.FACE_COORDINATE, com.runehive.game.world.entity.mob.UpdateFlag.FIRST_HIT, com.runehive.game.world.entity.mob.UpdateFlag.FORCED_CHAT, com.runehive.game.world.entity.mob.UpdateFlag.GRAPHICS, com.runehive.game.world.entity.mob.UpdateFlag.INTERACT, com.runehive.game.world.entity.mob.Mob.isUpdateRequired(), com.runehive.game.world.entity.mob.UpdateFlag.npcMask, com.runehive.game.world.entity.mob.UpdateFlag.npcOrder, com.runehive.game.world.entity.mob.UpdateFlag.SECOND_HIT, com.runehive.game.world.entity.mob.UpdateFlag.TRANSFORM, com.runehive.game.world.entity.mob.Mob.updateFlags, and com.runehive.net.packet.PacketBuilder.writeByte().

Referenced by encode().

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file: