Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: convert NPC updating to message system #125

Merged
merged 7 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/main/java/org/runejs/client/Projectile.java
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,5 @@ public void trackTarget(int loopCycle, int arg1, int targetY, int k, int targetX
if(!moving)
speedVectorZ = -speedVectorScalar * Math.tan((double) startSlope * 0.02454369);
heightOffset = (-currentHeight + (double) k - speedVectorZ * cyclesRemaining) * 2.0 / (cyclesRemaining * cyclesRemaining);
if(arg1 != 0)
Npc.parseTrackedNpcs();
}
}
135 changes: 20 additions & 115 deletions src/main/java/org/runejs/client/media/renderable/actor/Npc.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.runejs.client.cache.def.SpotAnimDefinition;
import org.runejs.client.media.renderable.Model;
import org.runejs.client.net.IncomingPackets;
import org.runejs.client.net.PacketBuffer;
import org.runejs.client.scene.Scene;
import org.runejs.client.scene.SceneCluster;

Expand All @@ -17,22 +18,22 @@ public class Npc extends Actor {
public ActorDefinition actorDefinition;


public static void parseNpcUpdateMasks() {
public static void parseNpcUpdateMasks(PacketBuffer buffer) {
for(int i = 0; i < actorUpdatingIndex; i++) {
int npcIndex = Player.actorUpdatingIndices[i];
Npc npc = Player.npcs[npcIndex];
int mask = IncomingPackets.incomingPacketBuffer.getUnsignedByte();
int mask = buffer.getUnsignedByte();
if((0x1 & mask) != 0) {
int i_3_ = IncomingPackets.incomingPacketBuffer.getUnsignedByte();
int i_4_ = IncomingPackets.incomingPacketBuffer.getUnsignedByte();
int i_3_ = buffer.getUnsignedByte();
int i_4_ = buffer.getUnsignedByte();
npc.method785(i_4_, MovedStatics.pulseCycle, i_3_);
npc.anInt3139 = MovedStatics.pulseCycle + 300;
npc.remainingHitpoints = IncomingPackets.incomingPacketBuffer.getUnsignedByte();
npc.maximumHitpoints = IncomingPackets.incomingPacketBuffer.getUnsignedByte();
npc.remainingHitpoints = buffer.getUnsignedByte();
npc.maximumHitpoints = buffer.getUnsignedByte();
}
if((0x20 & mask) != 0) {
npc.graphicId = IncomingPackets.incomingPacketBuffer.getUnsignedShortLE();
int i_5_ = IncomingPackets.incomingPacketBuffer.getIntBE();
npc.graphicId = buffer.getUnsignedShortLE();
int i_5_ = buffer.getIntBE();
npc.anInt3129 = 0;
npc.graphicDelay = MovedStatics.pulseCycle + (0xffff & i_5_);
npc.graphicHeight = i_5_ >> 16;
Expand All @@ -43,24 +44,24 @@ public static void parseNpcUpdateMasks() {
npc.graphicId = -1;
}
if((mask & 0x4) != 0) {
npc.facingActorIndex = IncomingPackets.incomingPacketBuffer.getUnsignedShortBE();
npc.facingActorIndex = buffer.getUnsignedShortBE();
if(npc.facingActorIndex == 65535)
npc.facingActorIndex = -1;
}
if((0x2 & mask) != 0) {
int i_6_ = IncomingPackets.incomingPacketBuffer.getUnsignedByte();
int i_7_ = IncomingPackets.incomingPacketBuffer.getUnsignedByte();
int i_6_ = buffer.getUnsignedByte();
int i_7_ = buffer.getUnsignedByte();
npc.method785(i_7_, MovedStatics.pulseCycle, i_6_);
npc.anInt3139 = MovedStatics.pulseCycle + 300;
npc.remainingHitpoints = IncomingPackets.incomingPacketBuffer.getUnsignedByte();
npc.maximumHitpoints = IncomingPackets.incomingPacketBuffer.getUnsignedByte();
npc.remainingHitpoints = buffer.getUnsignedByte();
npc.maximumHitpoints = buffer.getUnsignedByte();
}
if((0x40 & mask) != 0) {
npc.forcedChatMessage = IncomingPackets.incomingPacketBuffer.getString();
npc.forcedChatMessage = buffer.getString();
npc.anInt3078 = 100;
}
if((mask & 0x80) != 0) {
npc.actorDefinition = ActorDefinition.getDefinition(IncomingPackets.incomingPacketBuffer.getUnsignedShortBE());
npc.actorDefinition = ActorDefinition.getDefinition(buffer.getUnsignedShortBE());
npc.anInt3083 = npc.actorDefinition.rotateRightAnimation;
npc.anInt3113 = npc.actorDefinition.degreesToTurn;
npc.turnRightAnimationId = npc.actorDefinition.rotate90RightAnimation;
Expand All @@ -72,14 +73,14 @@ public static void parseNpcUpdateMasks() {
npc.turnAroundAnimationId = npc.actorDefinition.rotate180Animation;
}
if((mask & 0x8) != 0) {
npc.facePositionX = IncomingPackets.incomingPacketBuffer.getUnsignedShortBE();
npc.facePositionY = IncomingPackets.incomingPacketBuffer.getUnsignedShortLE();
npc.facePositionX = buffer.getUnsignedShortBE();
npc.facePositionY = buffer.getUnsignedShortLE();
}
if((0x10 & mask) != 0) {
int animationId = IncomingPackets.incomingPacketBuffer.getUnsignedShortBE();
int animationId = buffer.getUnsignedShortBE();
if(animationId == 65535)
animationId = -1;
int animationDelay = IncomingPackets.incomingPacketBuffer.getUnsignedByte();
int animationDelay = buffer.getUnsignedByte();
if(animationId == npc.playingAnimation && animationId != -1) {
int i_10_ = ProducingGraphicsBuffer_Sub1.getAnimationSequence(animationId).replyMode;
if(i_10_ == 1) {
Expand All @@ -102,102 +103,6 @@ public static void parseNpcUpdateMasks() {
}
}

public static void parseTrackedNpcs() {
IncomingPackets.incomingPacketBuffer.initBitAccess();
int trackedNpcCount = IncomingPackets.incomingPacketBuffer.getBits(8);
if(Player.npcCount > trackedNpcCount) {
for(int i = trackedNpcCount; i < Player.npcCount; i++)
Player.deregisterActorIndices[Class17.deregisterActorCount++] = Player.npcIds[i];
}
if(Player.npcCount < trackedNpcCount)
throw new RuntimeException("gnpov1");
Player.npcCount = 0;
for(int i = 0; i < trackedNpcCount; i++) {
int trackedNpcIndex = Player.npcIds[i];
Npc npc = Player.npcs[trackedNpcIndex];
int updateRequired = IncomingPackets.incomingPacketBuffer.getBits(1);
if(updateRequired == 0) {
Player.npcIds[Player.npcCount++] = trackedNpcIndex;
npc.anInt3134 = MovedStatics.pulseCycle;
} else {
int movementType = IncomingPackets.incomingPacketBuffer.getBits(2);
if(movementType == 0) { // No movement
Player.npcIds[Player.npcCount++] = trackedNpcIndex;
npc.anInt3134 = MovedStatics.pulseCycle;
Player.actorUpdatingIndices[actorUpdatingIndex++] = trackedNpcIndex;
} else if(movementType == 1) { // Walking
Player.npcIds[Player.npcCount++] = trackedNpcIndex;
npc.anInt3134 = MovedStatics.pulseCycle;
int walkDirection = IncomingPackets.incomingPacketBuffer.getBits(3);
npc.move(walkDirection, false);
int runUpdateBlock = IncomingPackets.incomingPacketBuffer.getBits(1);
if(runUpdateBlock == 1)
Player.actorUpdatingIndices[actorUpdatingIndex++] = trackedNpcIndex;
} else if(movementType == 2) { // Running
Player.npcIds[Player.npcCount++] = trackedNpcIndex;
npc.anInt3134 = MovedStatics.pulseCycle;
int walkDirection = IncomingPackets.incomingPacketBuffer.getBits(3);
npc.move(walkDirection, true);
int runDirection = IncomingPackets.incomingPacketBuffer.getBits(3);
npc.move(runDirection, true);
int runUpdateBlock = IncomingPackets.incomingPacketBuffer.getBits(1);
if(runUpdateBlock == 1)
Player.actorUpdatingIndices[actorUpdatingIndex++] = trackedNpcIndex;
} else if(movementType == 3) // Yeet
Player.deregisterActorIndices[Class17.deregisterActorCount++] = trackedNpcIndex;
}
}
}

public static void registerNewNpcs() {
while(IncomingPackets.incomingPacketBuffer.getRemainingBits(IncomingPackets.incomingPacketSize) >= 27) {
int i = IncomingPackets.incomingPacketBuffer.getBits(15);
if(i == 32767) {
break;
}

boolean initializing = false;
if(Player.npcs[i] == null) {
Player.npcs[i] = new Npc();
initializing = true;
}

Npc npc = Player.npcs[i];
Player.npcIds[Player.npcCount++] = i;
npc.anInt3134 = MovedStatics.pulseCycle;
int initialFaceDirection = Projectile.directions[IncomingPackets.incomingPacketBuffer.getBits(3)];
if(initializing) {
npc.initialFaceDirection = initialFaceDirection;
}

int offsetX = IncomingPackets.incomingPacketBuffer.getBits(5);
if(offsetX > 15)
offsetX -= 32;
int offsetY = IncomingPackets.incomingPacketBuffer.getBits(5);
int runUpdateBlock = IncomingPackets.incomingPacketBuffer.getBits(1);
if(offsetY > 15)
offsetY -= 32;
if(runUpdateBlock == 1)
Player.actorUpdatingIndices[actorUpdatingIndex++] = i;
int discardWalkingQueue = IncomingPackets.incomingPacketBuffer.getBits(1);

npc.actorDefinition = ActorDefinition.getDefinition(IncomingPackets.incomingPacketBuffer.getBits(13));
npc.turnLeftAnimationId = npc.actorDefinition.rotate90LeftAnimation;
npc.idleAnimation = npc.actorDefinition.stanceAnimation;
npc.anInt3083 = npc.actorDefinition.rotateRightAnimation;
npc.walkAnimationId = npc.actorDefinition.walkAnimation;
npc.anInt3096 = npc.actorDefinition.boundaryDimension;
npc.turnAroundAnimationId = npc.actorDefinition.rotate180Animation;
npc.standTurnAnimationId = npc.actorDefinition.rotateLeftAnimation;
npc.anInt3113 = npc.actorDefinition.degreesToTurn;
if(npc.anInt3113 == 0)
npc.anInt3118 = 0;
npc.turnRightAnimationId = npc.actorDefinition.rotate90RightAnimation;
npc.method787(Player.localPlayer.pathX[0] + offsetY, -7717, discardWalkingQueue == 1, Player.localPlayer.pathY[0] + offsetX);
}
IncomingPackets.incomingPacketBuffer.finishBitAccess();
}

public static int method400(int arg0) {
if(arg0 != -1)
SceneCluster.packetBuffer = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.runejs.client.message.inbound.console.*;
import org.runejs.client.message.inbound.misc.*;
import org.runejs.client.message.inbound.region.*;
import org.runejs.client.message.inbound.updating.UpdatePlayersInboundMessage;
import org.runejs.client.message.inbound.updating.*;
import org.runejs.client.message.inbound.widget.*;
import org.runejs.client.message.inbound.widget.container.*;
import org.runejs.client.message.inbound.widget.input.*;
Expand Down Expand Up @@ -66,7 +66,10 @@ public RS435HandlerRegistry() {
register(UpdateCarryWeightMessage.class, new UpdateCarryWeightMessageHandler());
register(UpdateRunEnergyMessage.class, new UpdateRunEnergyMessageHandler());
register(UpdateSkillMessage.class, new UpdateSkillMessageHandler());

// player/npc updating
register(UpdatePlayersInboundMessage.class, new UpdatePlayersMessageHandler());
register(UpdateNPCsInboundMessage.class, new UpdateNPCsMessageHandler());

// widget
register(SetWidgetTextInboundMessage.class, new SetWidgetTextMessageHandler());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package org.runejs.client.message.handler.rs435;

import org.runejs.client.Class17;
import org.runejs.client.MovedStatics;
import org.runejs.client.Projectile;
import org.runejs.client.cache.def.ActorDefinition;
import org.runejs.client.media.renderable.actor.Actor;
import org.runejs.client.media.renderable.actor.Npc;
import org.runejs.client.media.renderable.actor.Player;
import org.runejs.client.message.handler.MessageHandler;
import org.runejs.client.message.inbound.updating.UpdateNPCsInboundMessage;
import org.runejs.client.message.inbound.updating.movement.ActorGroupMovementUpdate;
import org.runejs.client.message.inbound.updating.movement.MovementUpdate;
import org.runejs.client.message.inbound.updating.registration.ActorGroupRegistrationUpdate;
import org.runejs.client.message.inbound.updating.registration.NPCRegistration;

public class UpdateNPCsMessageHandler implements MessageHandler<UpdateNPCsInboundMessage> {
@Override
public void handle(UpdateNPCsInboundMessage message) {
// initialisation steps
Class17.deregisterActorCount = 0;
Actor.actorUpdatingIndex = 0;

handleTrackedMovement(message.trackedNPCMovement);
handleRegisterNewNPCs(message.newNPCs);

Npc.parseNpcUpdateMasks(message.appearanceUpdates);

for(int i = 0; i < Class17.deregisterActorCount; i++) {
int trackedNpcIndex = Player.deregisterActorIndices[i];
if(MovedStatics.pulseCycle != Player.npcs[trackedNpcIndex].anInt3134) {
Player.npcs[trackedNpcIndex].actorDefinition = null;
Player.npcs[trackedNpcIndex] = null;
}
}

// TODO check there are no bytes left (buffer.currentPosition != buffer.size) or should this happen in decoder?

for(int i = 0; Player.npcCount > i; i++) {
if(Player.npcs[Player.npcIds[i]] == null)
throw new RuntimeException("gnp2 pos:" + i + " size:" + Player.npcCount);
}
}

/**
* Handles the movement of tracked NPCs
* @param update The update containing the movement information
*/
private void handleTrackedMovement(ActorGroupMovementUpdate update) {
int trackedNpcCount = update.actorCount;
if(Player.npcCount > trackedNpcCount) {
for(int i = trackedNpcCount; i < Player.npcCount; i++)
Player.deregisterActorIndices[Class17.deregisterActorCount++] = Player.npcIds[i];
}

if(Player.npcCount < trackedNpcCount) {
System.out.println("gppov1 [npc] " + Player.npcCount + " " + trackedNpcCount);
throw new RuntimeException("gppov1");
}

Player.npcCount = 0;

for(int i = 0; trackedNpcCount > i; i++) {
int trackedNpcIndex = Player.npcIds[i];
Npc npc = Player.npcs[trackedNpcIndex];

ActorGroupMovementUpdate.ActorMovementUpdate npcUpdate = update.movementUpdates[i];

// no update required
if (npcUpdate == null) {
Player.npcIds[Player.npcCount++] = trackedNpcIndex;
npc.anInt3134 = MovedStatics.pulseCycle;
Player.actorUpdatingIndices[Npc.actorUpdatingIndex++] = trackedNpcIndex;
continue;
}

// deregister npc if necessary
if (npcUpdate.shouldDeregister) {
Player.deregisterActorIndices[Class17.deregisterActorCount++] = trackedNpcIndex;
continue;
}

Player.npcIds[Player.npcCount++] = trackedNpcIndex;
npc.anInt3134 = MovedStatics.pulseCycle;

// if no movement then we can skip the rest
if (npcUpdate.movementUpdate == null) {
Player.actorUpdatingIndices[Npc.actorUpdatingIndex++] = trackedNpcIndex;
continue;
}

// handle the player movement
MovementUpdate movementUpdate = npcUpdate.movementUpdate;

if (movementUpdate.runDirection != null) {
npc.move(movementUpdate.walkDirection, true);
npc.move(movementUpdate.runDirection, true);
} else {
npc.move(movementUpdate.walkDirection, false);
}

if (movementUpdate.runUpdateBlock) {
Player.actorUpdatingIndices[Npc.actorUpdatingIndex++] = trackedNpcIndex;
}
}
}

/**
* Handles the registration of new NPCs
* @param update The update containing the registration information
*/
private void handleRegisterNewNPCs(ActorGroupRegistrationUpdate<NPCRegistration> update) {
for (NPCRegistration playerUpdate : update.players) {
int i = playerUpdate.index;

// likely isn't needed as it's handled in the decoder
if(i == 32767)
break;

// rehydrate the player from existing appearance data
boolean initializing = false;
if(Player.npcs[i] == null) {
Player.npcs[i] = new Npc();
initializing = true;
}

// begin registration proper
Npc npc = Player.npcs[i];
Player.npcIds[Player.npcCount++] = i;
npc.anInt3134 = MovedStatics.pulseCycle;

int initialFaceDirection = playerUpdate.faceDirection;
int faceDirection = Projectile.directions[initialFaceDirection];
if(initializing)
npc.initialFaceDirection = faceDirection;

int offsetX = playerUpdate.offsetX;
int offsetY = playerUpdate.offsetY;
if(offsetX > 15)
offsetX -= 32;
if(offsetY > 15)
offsetY -= 32;

if (playerUpdate.updateRequired) {
Player.actorUpdatingIndices[Npc.actorUpdatingIndex++] = i;
}

npc.actorDefinition = ActorDefinition.getDefinition(playerUpdate.definitionId);
npc.turnLeftAnimationId = npc.actorDefinition.rotate90LeftAnimation;
npc.idleAnimation = npc.actorDefinition.stanceAnimation;
npc.anInt3083 = npc.actorDefinition.rotateRightAnimation;
npc.walkAnimationId = npc.actorDefinition.walkAnimation;
npc.anInt3096 = npc.actorDefinition.boundaryDimension;
npc.turnAroundAnimationId = npc.actorDefinition.rotate180Animation;
npc.standTurnAnimationId = npc.actorDefinition.rotateLeftAnimation;
npc.anInt3113 = npc.actorDefinition.degreesToTurn;
if(npc.anInt3113 == 0)
npc.anInt3118 = 0;
npc.turnRightAnimationId = npc.actorDefinition.rotate90RightAnimation;
npc.method787(Player.localPlayer.pathX[0] + offsetY, -7717, playerUpdate.updateRequired, Player.localPlayer.pathY[0] + offsetX);
}
}
}
Loading