RuneHive-Game
Loading...
Searching...
No Matches
Waypoint.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.mob.movement.waypoint;
2
3import com.runehive.game.task.Task;
4import com.runehive.game.world.Interactable;
5import com.runehive.game.world.World;
6import com.runehive.game.world.entity.mob.Mob;
7import com.runehive.game.world.entity.mob.data.PacketType;
8import com.runehive.game.world.entity.mob.npc.Npc;
9import com.runehive.game.world.entity.mob.player.Player;
10import com.runehive.game.world.object.GameObject;
11import com.runehive.game.world.object.GameObjectDefinition;
12import com.runehive.game.world.pathfinding.TraversalMap;
13import com.runehive.game.world.position.Position;
14import com.runehive.game.world.region.Region;
15import com.runehive.net.packet.out.SendMessage;
16import com.runehive.util.Utility;
17
18import java.util.Objects;
19
20public abstract class Waypoint extends Task {
21 protected final Mob mob;
24
26 super(true, 0);
27 this.mob = mob;
28 this.target = target;
29 }
30
31 protected abstract void onDestination();
32
33 protected int getRadius() {
34 return 1;
35 }
36
37 protected boolean withinDistance() {
38 if (target instanceof final GameObject object) {
39 Position position = mob.getPosition();
40 int x = position.getX();
41 int y = position.getY();
42
43 GameObjectDefinition definition = object.getDefinition();
44 int walkingFlag = definition.getWalkingFlag();
45 int sizeX = definition.getWidth();
46 int sizeY = definition.getLength();
47 int rotation = object.getDirection().getId();
48 if (rotation % 2 != 0) {
49 sizeX = definition.getLength();
50 sizeY = definition.getWidth();
51 }
52 if (rotation != 0) {
53 walkingFlag = (walkingFlag << rotation & 0xF) + (walkingFlag >> 4 - rotation);
54 }
55 if (sizeX != 0 && sizeY != 0) {
56 Position targetPosition = target.getPosition();
57 int destX = targetPosition.getX();
58 int destY = targetPosition.getY();
59 int height = targetPosition.getHeight();
60
61 int flag = TraversalMap.getFlags(destX, destY, height);
62
63 int cornerX = destX + sizeX - 1;
64 int cornerY = destY + sizeY - 1;
65
66 if (destX <= x && cornerX >= x && y >= destY && y <= cornerY) {
67 return true;
68 }
69 if (x == destX - 1 && destY <= y && y <= cornerY && (0x8 & flag) == 0 && (0x8 & walkingFlag) == 0) {
70 return true;
71 }
72 if (x == cornerX + 1 && destY <= y && cornerY >= y && (flag & 0x80) == 0 && (0x2 & walkingFlag) == 0) {
73 return true;
74 }
75 if (y == destY - 1 && destX <= x && cornerX >= x && (0x2 & flag) == 0 && (0x4 & walkingFlag) == 0) {
76 return true;
77 }
78 if (y == cornerY + 1 && destX <= x && cornerX >= x && (flag & 0x20) == 0 && (0x1 & walkingFlag) == 0) {
79 return true;
80 }
81 }
82 }
83 return Utility.getDistance(mob, target) <= getRadius() && !mob.movement.needsPlacement();
84 }
85
86 @Override
87 protected void onSchedule() {
88 if (mob.locking.locked(PacketType.MOVEMENT)) {
89 return;
90 }
91
92 if (target instanceof Mob) {
93 mob.interact((Mob) target);
94 }
95
96 if (!withinDistance()) {
97 findRoute();
98 }
99 }
100
101 @Override
102 public void execute() {
103 if (target instanceof Mob && Utility.inside(mob, target)) {
104 if (!mob.locking.locked(PacketType.MOVEMENT)
105 && !mob.movement.needsPlacement()) {
106 Mob targetMob = (Mob) target;
107 if (targetMob.hasPriorityIndex(mob)
108 || (!targetMob.isFixingInside() && !targetMob.movement.needsPlacement())) {
110 }
111 }
112 return;
113 } else {
114 mob.setFixingInside(false);
115 }
116
117 if (withinDistance()) {
119 return;
120 }
121
122 if (target instanceof GameObject gameObject && mob.isPlayer()) {
123 if (gameObject.distance() > 1 && withinDistance(mob.getPosition().getX(), mob.getPosition().getY(), target.getPosition().getX(), target.getPosition().getY(), target.width(), gameObject.length(), gameObject.distance())) {
125 return;
126 }
127 }
128
129 if (target.getPosition().equals(lastPosition)) {
130 return;
131 }
132
133 if (mob.locking.locked(PacketType.MOVEMENT)) {
134 return;
135 }
136
137 lastPosition = target.getPosition();
138 findRoute();
139 }
140
141 private void findRoute() {
142 if (target instanceof Player && mob.equals(((Player) target).pet)) {
143 int distance = Utility.getDistance(mob, target);
144 if (distance > Region.VIEW_DISTANCE) {
145 Npc pet = mob.getNpc();
146 pet.move(target.getPosition());
147 pet.instance = ((Player) target).instance;
148 World.schedule(1, () -> {
149 pet.interact((Player) target);
150 pet.follow((Player) target);
151 });
152 }
153 }
154
155// if (this instanceof CombatWaypoint) {
156// System.out.println(mob.getPosition());
157// System.out.println(target.getPosition());
158// System.out.println(Utility.getDelta(mob, target));
159// System.out.println();
160// }
161
162 boolean smart = mob.isPlayer() || (mob.isNpc() && !(this instanceof CombatWaypoint));
163
164 if (smart && mob.movement.dijkstraPath(target)) {
165 return;
166 }
167
168 if (mob.movement.simplePath(target)) {
169 return;
170 }
171
172 if (mob.isPlayer())
173 mob.getPlayer().send(new SendMessage("I can't reach that!"));
174
175 /* No path can be found, lets get out of here!!!! */
176 cancel();
177 }
178
179 @Override
180 protected void onCancel(boolean logout) {
181 mob.resetFace();
182
183 if (target instanceof Mob other) {
184 other.attributes.remove("mob-following");
185 }
186 }
187
188 @Override
189 public boolean equals(Object obj) {
190 if (obj == this) return true;
191 if (obj instanceof Waypoint other) {
192 return Objects.equals(mob, other.mob)
193 && Objects.equals(target, other.target);
194 }
195 return false;
196 }
197
198 @Override
199 public String toString() {
200 return getClass().getSimpleName() + "{" +
201 "target=" + target +
202 '}';
203 }
204
206 return target;
207 }
208
209 public void onChange() {
210 execute();
211// mob.movement.processNextMovement();
212 }
213
214 boolean withinDistance(int x, int y, int x2, int y2, int width, int height, int distance) {
215 if (x > x2) {
216 x2 += Math.min(width, x - x2) - 1;
217 }
218 if (y > y2) {
219 y2 += Math.min(height, y - y2) - 1;
220 }
221 return Math.abs(x - x2) <= distance && Math.abs(y - y2) <= distance;
222 }
223}
synchronized final void cancel()
Cancels all subsequent executions.
Definition Task.java:113
Task(boolean instant, int delay)
Creates a new Task.
Definition Task.java:41
Represents the game world.
Definition World.java:46
static void schedule(Task task)
Submits a new event.
Definition World.java:247
Handles the mob class.
Definition Mob.java:66
void interact(Mob mob)
Sets the mob interacting with another mob.
Definition Mob.java:278
void move(Position position)
Moves the mob to a set position.
Definition Mob.java:340
boolean hasPriorityIndex(Mob other)
Definition Mob.java:763
boolean withinDistance(int x, int y, int x2, int y2, int width, int height, int distance)
void onSchedule()
A function executed on registration.
Definition Waypoint.java:87
void onCancel(boolean logout)
A function executed on cancellation.
void execute()
A function representing the unit of work that will be carried out.
Represents a non-player character in the in-game world.
Definition Npc.java:29
This class represents a character controlled by a player.
Definition Player.java:125
Contains traversal data for a set of regions.
static int getFlags(int x, int y, int height)
Represents a single tile on the game world.
Definition Position.java:14
int getHeight()
Gets the height coordinate, or height.
Definition Position.java:51
int getY()
Gets the absolute y coordinate.
Definition Position.java:46
int getX()
Gets the absolute x coordinate.
Definition Position.java:41
Represents a single region.
Definition Region.java:21
The OutgoingPacket that sends a message to a Players chatbox in the client.
Handles miscellaneous methods.
Definition Utility.java:27
static boolean inside(Interactable source, Interactable target)
Definition Utility.java:783
static int getDistance(Interactable source, Position target)
Definition Utility.java:363
static void fixInsidePosition(Mob source, Interactable target)
Definition Utility.java:567
An object implementing Interactable has uses.