Skip to content

Commit

Permalink
cannon: use varps for cannon location and state
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam- committed Apr 28, 2024
1 parent 3f8280d commit f8cdb21
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 119 deletions.
2 changes: 2 additions & 0 deletions runelite-api/src/main/java/net/runelite/api/VarPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
*/
public final class VarPlayer
{
public static final int CANNON_STATE = 2;
public static final int CANNON_AMMO = 3;
public static final int CANNON_COORD = 4;
public static final int ATTACK_STYLE = 43;
public static final int QUEST_POINTS = 101;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,12 @@ public WorldArea toWorldArea()
{
return new WorldArea(this, 1, 1);
}

/**
* Create a WorldPoint from a packed Jagex coordinate
*/
public static WorldPoint fromCoord(int c)
{
return new WorldPoint((c >>> 14) & 0x3FFF, c & 0x3FFF, (c >>> 28) & 0x3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,19 @@
import javax.inject.Inject;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.AnimationID;
import net.runelite.api.ChatMessageType;
import net.runelite.api.Client;
import net.runelite.api.GameObject;
import net.runelite.api.GameState;
import net.runelite.api.InventoryID;
import net.runelite.api.Item;
import net.runelite.api.ItemID;
import net.runelite.api.MenuAction;
import net.runelite.api.ObjectID;
import net.runelite.api.Player;
import net.runelite.api.VarPlayer;
import net.runelite.api.coords.LocalPoint;
import net.runelite.api.coords.WorldArea;
import net.runelite.api.coords.WorldPoint;
import net.runelite.api.events.ChatMessage;
import net.runelite.api.events.GameObjectSpawned;
import net.runelite.api.events.GameStateChanged;
import net.runelite.api.events.ItemContainerChanged;
import net.runelite.api.events.MenuOptionClicked;
import net.runelite.api.events.VarbitChanged;
import net.runelite.api.widgets.ComponentID;
import net.runelite.api.widgets.Widget;
import net.runelite.client.Notifier;
import net.runelite.client.callback.ClientThread;
import net.runelite.client.config.ConfigManager;
Expand All @@ -77,8 +67,6 @@ public class CannonPlugin extends Plugin

private CannonCounter counter;
private boolean cannonBallNotificationSent;
private WorldPoint clickedCannonLocation;
private boolean firstCannonLoad;

@Getter
private int cballsLeft;
Expand All @@ -92,9 +80,6 @@ public class CannonPlugin extends Plugin
@Getter
private int cannonWorld = -1;

@Getter
private GameObject cannon;

@Getter
private List<WorldPoint> spotPoints = new ArrayList<>();

Expand Down Expand Up @@ -247,65 +232,40 @@ public void onGameStateChanged(GameStateChanged gameStateChanged)
}

@Subscribe
public void onGameObjectSpawned(GameObjectSpawned event)
public void onVarbitChanged(VarbitChanged varbitChanged)
{
GameObject gameObject = event.getGameObject();

Player localPlayer = client.getLocalPlayer();
if ((gameObject.getId() == ObjectID.CANNON_BASE || gameObject.getId() == ObjectID.CANNON_BASE_43029) && !cannonPlaced)
if (varbitChanged.getVarpId() == VarPlayer.CANNON_AMMO)
{
if (localPlayer.getWorldLocation().distanceTo(gameObject.getWorldLocation()) <= 2
&& localPlayer.getAnimation() == AnimationID.BURYING_BONES)
int old = cballsLeft;
cballsLeft = varbitChanged.getValue();

if (cballsLeft > old)
{
cannonPosition = buildCannonWorldArea(gameObject.getWorldLocation());
cannonWorld = client.getWorld();
cannon = gameObject;
cannonBallNotificationSent = false;
}
}
}

@Subscribe
public void onMenuOptionClicked(MenuOptionClicked event)
{
if (cannonPosition != null || (event.getId() != ObjectID.DWARF_MULTICANNON && event.getId() != ObjectID.DWARF_MULTICANNON_43027))
{
return;
}

// Check if cannonballs are being used on the cannon
if (event.getMenuAction() == MenuAction.WIDGET_TARGET_ON_GAME_OBJECT
&& client.getSelectedWidget() != null
&& client.getSelectedWidget().getId() == ComponentID.INVENTORY_CONTAINER)
{
final Widget selected = client.getSelectedWidget();
final int itemId = selected.getItemId();
if (itemId != ItemID.CANNONBALL && itemId != ItemID.GRANITE_CANNONBALL)
if (!cannonBallNotificationSent && cballsLeft > 0 && config.lowWarningThreshold() >= cballsLeft)
{
return;
notifier.notify(config.showCannonNotifications(), String.format("Your cannon has %d cannon balls remaining!", cballsLeft));
cannonBallNotificationSent = true;
}
}
// Check for the Fire option being selected on the cannon.
else if (event.getMenuAction() != MenuAction.GAME_OBJECT_FIRST_OPTION)
else if (varbitChanged.getVarpId() == VarPlayer.CANNON_COORD)
{
return;
WorldPoint c = WorldPoint.fromCoord(varbitChanged.getValue());
cannonPosition = buildCannonWorldArea(c);
}

// Store the click location as a WorldPoint to avoid issues with scene loads
clickedCannonLocation = WorldPoint.fromScene(client, event.getParam0(), event.getParam1(), client.getPlane());
log.debug("Updated cannon location: {}", clickedCannonLocation);
}

@Subscribe
public void onVarbitChanged(VarbitChanged varbitChanged)
{
if (varbitChanged.getVarpId() == VarPlayer.CANNON_AMMO)
else if (varbitChanged.getVarpId() == VarPlayer.CANNON_STATE)
{
cballsLeft = varbitChanged.getValue();
cannonPlaced = varbitChanged.getValue() == 4;

if (!cannonBallNotificationSent && cballsLeft > 0 && config.lowWarningThreshold() >= cballsLeft)
if (cannonPlaced)
{
notifier.notify(config.showCannonNotifications(), String.format("Your cannon has %d cannon balls remaining!", cballsLeft));
cannonBallNotificationSent = true;
addCounter();
}
else
{
removeCounter();
}
}
}
Expand All @@ -320,58 +280,12 @@ public void onChatMessage(ChatMessage event)

if (event.getMessage().equals("You add the furnace."))
{
cannonPlaced = true;
addCounter();
firstCannonLoad = true;
}
else if (event.getMessage().contains("You pick up the cannon")
|| event.getMessage().contains("Your cannon has decayed. Speak to Nulodion to get a new one!")
|| event.getMessage().contains("Your cannon has been destroyed!"))
{
cannonPlaced = false;
removeCounter();
cannonPosition = null;
}
else if (event.getMessage().startsWith("You load the cannon with"))
{
// Set the cannon's position and object if the player's animation was interrupted during setup
if (cannonPosition == null && clickedCannonLocation != null)
{
// There is a window of 1 tick where the player can add the furnace, click on another cannon, and then
// the initial cannon load message arrives. This can cause the client to confuse the other cannon with
// the player's, so ignore that first message when deciding the cannon's location.
if (firstCannonLoad)
{
firstCannonLoad = false;
}
else
{
LocalPoint lp = LocalPoint.fromWorld(client, clickedCannonLocation);
if (lp != null)
{
GameObject[] objects = client.getScene().getTiles()[client.getPlane()][lp.getSceneX()][lp.getSceneY()].getGameObjects();
if (objects.length > 0 && client.getLocalPlayer().getWorldLocation().distanceTo(objects[0].getWorldLocation()) <= 2)
{
cannonPlaced = true;
cannonWorld = client.getWorld();
cannon = objects[0];
cannonPosition = buildCannonWorldArea(cannon.getWorldLocation());
}
}
}
clickedCannonLocation = null;
}

cannonBallNotificationSent = false;
cannonWorld = client.getWorld();
}
else if (event.getMessage().contains("Your cannon is out of ammo!"))
{
notifier.notify(config.showCannonNotifications(), "Your cannon is out of ammo!");
}
else if (event.getMessage().equals("This isn't your cannon!") || event.getMessage().equals("This is not your cannon."))
{
clickedCannonLocation = null;
}
}

Color getStateColor()
Expand Down Expand Up @@ -414,6 +328,6 @@ private void removeCounter()

private static WorldArea buildCannonWorldArea(WorldPoint worldPoint)
{
return new WorldArea(worldPoint.getX() - 1, worldPoint.getY() - 1, 3, 3, worldPoint.getPlane());
return new WorldArea(worldPoint.getX(), worldPoint.getY(), 3, 3, worldPoint.getPlane());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public void onScriptPreFired(ScriptPreFired scriptPreFired)
case DEPLETED_VEIN_26667: // Depleted motherlode vein
case DEPLETED_VEIN_26668: // Depleted motherlode vein
{
WorldPoint worldPoint = new WorldPoint((locCoord >>> 14) & 0x3FFF, locCoord & 0x3FFF, (locCoord >>> 28) & 0x3);
WorldPoint worldPoint = WorldPoint.fromCoord(locCoord);
Rock rock = Rock.ORE_VEIN;
RockRespawn rockRespawn = new RockRespawn(rock, worldPoint, Instant.now(), ticks * Constants.GAME_TICK_LENGTH, rock.getZOffset());
respawns.add(rockRespawn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ public void onScriptPreFired(ScriptPreFired scriptPreFired)
case NullObjectID.NULL_34637:
case NullObjectID.NULL_34639:
{
WorldPoint worldPoint = new WorldPoint((locCoord >>> 14) & 0x3FFF, locCoord & 0x3FFF, (locCoord >>> 28) & 0x3);
WorldPoint worldPoint = WorldPoint.fromCoord(locCoord);
GameObject gameObject = findObject(worldPoint);
if (gameObject == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ public void before()
@Test
public void testAmmoCountOnPlace()
{
ChatMessage chatMessage = new ChatMessage();
chatMessage.setType(ChatMessageType.SPAM);
chatMessage.setMessage("You add the furnace.");
VarbitChanged varbitChanged = new VarbitChanged();
varbitChanged.setVarpId(VarPlayer.CANNON_STATE);
varbitChanged.setValue(4);

plugin.onChatMessage(chatMessage);
plugin.onVarbitChanged(varbitChanged);
assertTrue(plugin.isCannonPlaced());

plugin.onVarbitChanged(cannonAmmoChanged);
Expand All @@ -126,11 +126,11 @@ public void testCannonInfoBox()
{
when(config.showInfobox()).thenReturn(true);

ChatMessage chatMessage = new ChatMessage();
chatMessage.setType(ChatMessageType.SPAM);
chatMessage.setMessage("You add the furnace.");
VarbitChanged varbitChanged = new VarbitChanged();
varbitChanged.setVarpId(VarPlayer.CANNON_STATE);
varbitChanged.setValue(4);

plugin.onChatMessage(chatMessage);
plugin.onVarbitChanged(varbitChanged);
assertTrue(plugin.isCannonPlaced());

assertEquals(0, plugin.getCballsLeft());
Expand Down

0 comments on commit f8cdb21

Please sign in to comment.