Skip to content

Commit

Permalink
Merge pull request #98 from Jameskmonger/packet-impl-object-actions
Browse files Browse the repository at this point in the history
refactor: convert object interaction packets to new system
  • Loading branch information
Promises authored Feb 28, 2023
2 parents 62cf8cc + 9e20465 commit 5b3785f
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ public static void loadTerrainBlock(CollisionMap[] collisions, int regionX_maybe
}
}

public static boolean method596(int arg0, int arg1, byte arg2, int arg3) {
public static boolean method596(int arg0, int arg1, byte junk, int arg3) {
int i = 0x7fff & arg1 >> 14;
if(arg2 > -9)
method596(-4, -74, (byte) -44, -62);
int i_14_ = Npc.currentScene.getArrangement(Player.worldLevel, arg0, arg3, arg1);
if(i_14_ == -1)
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import org.runejs.client.media.renderable.actor.Pathfinding;
import org.runejs.client.media.renderable.actor.Player;
import org.runejs.client.media.renderable.actor.PlayerAppearance;
import org.runejs.client.message.outbound.interactions.ObjectInteractionOutboundMessage;
import org.runejs.client.net.ISAAC;
import org.runejs.client.net.OutgoingPackets;
import org.runejs.client.net.PacketBuffer;
import org.runejs.client.node.CachedNode;
import org.runejs.client.scene.InteractiveObject;
Expand Down Expand Up @@ -584,10 +586,19 @@ public static void processMenuActions(int arg1) {
}
if(action == ActionRowType.INTERACT_WITH_OBJECT_OPTION_4.getId()) {
AnimationSequence.method596(i, npcIdx, (byte) -79, i_10_);
SceneCluster.packetBuffer.putPacket(229);
SceneCluster.packetBuffer.putShortLE(MovedStatics.baseX + i);
SceneCluster.packetBuffer.putShortLE((0x1ffffd20 & npcIdx) >> 14);
SceneCluster.packetBuffer.putShortLE(Class26.baseY + i_10_);

int objectX = MovedStatics.baseX + i;
int objectId = (0x1ffffd20 & npcIdx) >> 14;
int objectY = Class26.baseY + i_10_;

OutgoingPackets.sendMessage(
new ObjectInteractionOutboundMessage(
4,
objectId,
objectX,
objectY
)
);
}
if(action == ActionRowType.CAST_MAGIC_ON_PLAYER.getId()) {
Player class40_sub5_sub17_sub4_sub1 = Player.trackedPlayers[npcIdx];
Expand Down Expand Up @@ -835,10 +846,19 @@ public static void processMenuActions(int arg1) {
}
if(action == ActionRowType.INTERACT_WITH_OBJECT_OPTION_5.getId()) {
AnimationSequence.method596(i, npcIdx, (byte) -11, i_10_);
SceneCluster.packetBuffer.putPacket(62);
SceneCluster.packetBuffer.putShortBE((0x1fffd05d & npcIdx) >> 14);
SceneCluster.packetBuffer.putShortLE(i_10_ + Class26.baseY);
SceneCluster.packetBuffer.putShortLE(i + MovedStatics.baseX);

int objectId = (0x1ffffd20 & npcIdx) >> 14;
int objectY = i_10_ + Class26.baseY;
int objectX = i + MovedStatics.baseX;

OutgoingPackets.sendMessage(
new ObjectInteractionOutboundMessage(
5,
objectId,
objectX,
objectY
)
);
}
if(action == ActionRowType.INTERACT_WITH_NPC_OPTION_2.getId()) {
Npc class40_sub5_sub17_sub4_sub2 = Player.npcs[npcIdx];
Expand Down Expand Up @@ -869,10 +889,19 @@ public static void processMenuActions(int arg1) {
}
if(action == ActionRowType.INTERACT_WITH_OBJECT_OPTION_2.getId()) {
AnimationSequence.method596(i, npcIdx, (byte) -77, i_10_);
SceneCluster.packetBuffer.putPacket(164);
SceneCluster.packetBuffer.putShortLE(MovedStatics.baseX + i);
SceneCluster.packetBuffer.putShortLE(Class26.baseY + i_10_);
SceneCluster.packetBuffer.putShortLE(npcIdx >> 14 & 0x7fff);

int objectX = i + MovedStatics.baseX;
int objectY = i_10_ + Class26.baseY;
int objectId = npcIdx >> 14 & 0x7fff;

OutgoingPackets.sendMessage(
new ObjectInteractionOutboundMessage(
2,
objectId,
objectX,
objectY
)
);
}
if(action == ActionRowType.USE_ITEM_ON_OBJECT.getId() && AnimationSequence.method596(i, npcIdx, (byte) -104, i_10_)) {
SceneCluster.packetBuffer.putPacket(24);
Expand Down Expand Up @@ -1080,10 +1109,19 @@ public static void processMenuActions(int arg1) {
}
if(action == ActionRowType.INTERACT_WITH_OBJECT_OPTION_3.getId()) {
AnimationSequence.method596(i, npcIdx, (byte) -104, i_10_);
SceneCluster.packetBuffer.putPacket(183);
SceneCluster.packetBuffer.putShortBE(i_10_ + Class26.baseY);
SceneCluster.packetBuffer.putShortBE(npcIdx >> 14 & 0x7fff);
SceneCluster.packetBuffer.putShortBE(i + MovedStatics.baseX);

int objectY = i_10_ + Class26.baseY;
int objectId = npcIdx >> 14 & 0x7fff;
int objectX = i + MovedStatics.baseX;

OutgoingPackets.sendMessage(
new ObjectInteractionOutboundMessage(
3,
objectId,
objectX,
objectY
)
);
}
if(action == 50) {
Class33.method406(i, npcIdx, i_10_);
Expand Down Expand Up @@ -1241,10 +1279,18 @@ public static void processMenuActions(int arg1) {
}
if(action == ActionRowType.INTERACT_WITH_OBJECT_OPTION_1.getId()) {
AnimationSequence.method596(i, npcIdx, (byte) -47, i_10_);
SceneCluster.packetBuffer.putPacket(30);
SceneCluster.packetBuffer.putShortBE(0x7fff & npcIdx >> 14);
SceneCluster.packetBuffer.putShortBE(Class26.baseY + i_10_);
SceneCluster.packetBuffer.putShortLE(i + MovedStatics.baseX);
int objectId = 0x7fff & npcIdx >> 14;
int objectX = i + MovedStatics.baseX;
int objectY = i_10_ + Class26.baseY;

OutgoingPackets.sendMessage(
new ObjectInteractionOutboundMessage(
1,
objectId,
objectX,
objectY
)
);
}
if(action == ActionRowType.INTERACT_WITH_NPC_OPTION_5.getId()) {
Npc class40_sub5_sub17_sub4_sub2 = Player.npcs[npcIdx];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.runejs.client.message.outbound.interactions;

import org.runejs.client.message.OutboundMessage;

/**
* Represents a message sent to the server when a player interacts with an object.
*
* e.g. clicking on a tree to chop it down
*/
public class ObjectInteractionOutboundMessage implements OutboundMessage {
/**
* Which option on the object was clicked
*
* i.e. 1 = first option, 2 = second option, etc.
*/
public final int option;

/**
* The id of the object
*/
public final int objectId;

/**
* The x coordinate of the object
*/
public final int x;

/**
* The y coordinate of the object
*/
public final int y;

public ObjectInteractionOutboundMessage(int option, int objectId, int x, int y) {
this.option = option;
this.objectId = objectId;
this.x = x;
this.y = y;
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/runejs/client/net/OutgoingPackets.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public static void init(ISAAC outCipher) {

/**
* Opens a fixed-size packet with the specified opcode.
*
* @param size The size of the packet in bytes, not including the opcode.
*/
public static PacketBuffer openFixedSizePacket(int size, int opcode) {
// data + 1 byte for opcode
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.runejs.client.net.codec.runejs435;

import org.runejs.client.message.outbound.interactions.ObjectInteractionOutboundMessage;
import org.runejs.client.net.PacketType;
import org.runejs.client.net.codec.MessagePacketCodec;
import org.runejs.client.net.codec.runejs435.decoder.audio.PlayQuickSongMessageDecoder;
import org.runejs.client.net.codec.runejs435.decoder.audio.PlaySongMessageDecoder;
import org.runejs.client.net.codec.runejs435.decoder.audio.PlaySoundMessageDecoder;
import org.runejs.client.net.codec.runejs435.encoder.*;
import org.runejs.client.net.codec.runejs435.decoder.*;

/**
* A {@link MessagePacketCodec} for the RuneJS customised 435 protocol.
Expand All @@ -18,6 +18,7 @@ public RuneJS435PacketCodec() {
}

private void registerEncoders() {
register(ObjectInteractionOutboundMessage.class, new ObjectInteractionMessageEncoder());
}

private void registerDecoders() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.runejs.client.net.codec.runejs435.encoder.interactions;

import org.runejs.client.message.outbound.interactions.ObjectInteractionOutboundMessage;
import org.runejs.client.net.OutgoingPackets;
import org.runejs.client.net.PacketBuffer;
import org.runejs.client.net.codec.MessageEncoder;

public class ObjectInteractionMessageEncoder implements MessageEncoder<ObjectInteractionOutboundMessage> {

@Override
public PacketBuffer encode(ObjectInteractionOutboundMessage message) {
switch (message.option) {
case 1:
return encodeOption1Interaction(message);
case 2:
return encodeOption2Interaction(message);
case 3:
return encodeOption3Interaction(message);
case 4:
return encodeOption4Interaction(message);
case 5:
return encodeOption5Interaction(message);
default:
throw new RuntimeException("Invalid option: " + message.option);
}
}

private PacketBuffer encodeOption1Interaction(ObjectInteractionOutboundMessage message) {
PacketBuffer buffer = OutgoingPackets.openFixedSizePacket(6, 30);

buffer.putShortBE(message.objectId);
buffer.putShortBE(message.y);
buffer.putShortLE(message.x);

return buffer;
}

private PacketBuffer encodeOption2Interaction(ObjectInteractionOutboundMessage message) {
PacketBuffer buffer = OutgoingPackets.openFixedSizePacket(6, 164);

buffer.putShortLE(message.x);
buffer.putShortLE(message.y);
buffer.putShortLE(message.objectId);

return buffer;
}

private PacketBuffer encodeOption3Interaction(ObjectInteractionOutboundMessage message) {
PacketBuffer buffer = OutgoingPackets.openFixedSizePacket(6, 183);

buffer.putShortBE(message.y);
buffer.putShortBE(message.objectId);
buffer.putShortBE(message.x);

return buffer;
}

private PacketBuffer encodeOption4Interaction(ObjectInteractionOutboundMessage message) {
PacketBuffer buffer = OutgoingPackets.openFixedSizePacket(6, 229);

buffer.putShortLE(message.x);
buffer.putShortLE(message.objectId);
buffer.putShortLE(message.y);

return buffer;
}

private PacketBuffer encodeOption5Interaction(ObjectInteractionOutboundMessage message) {
PacketBuffer buffer = OutgoingPackets.openFixedSizePacket(6, 62);

buffer.putShortBE(message.objectId);
buffer.putShortLE(message.y);
buffer.putShortLE(message.x);

return buffer;
}

}

0 comments on commit 5b3785f

Please sign in to comment.