RuneHive-Game
Loading...
Searching...
No Matches
CombatTarget.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.combat;
2
3import com.runehive.game.world.entity.mob.Mob;
4import com.runehive.game.world.entity.mob.npc.Npc;
5import com.runehive.game.world.entity.mob.player.Player;
6import com.runehive.game.world.pathfinding.path.SimplePathChecker;
7import com.runehive.game.world.position.Area;
8import com.runehive.game.world.position.Position;
9import com.runehive.game.world.region.Region;
10import com.runehive.util.Utility;
11
12import java.util.concurrent.TimeUnit;
13
14public class CombatTarget {
15
16 /** The aggression timeout in minutes. */
17 private static final int AGGRESSION_TIMEOUT = 20;
18 private final Mob mob;
19 private Mob target;
20 private int distance;
21
23 this.mob = mob;
24 this.distance = Integer.MAX_VALUE;
25 }
26
27 /** Checks the aggression for this mob if a target is set. */
29 /* No target */
30 if (target == null)
31 return;
32
33 /* The target is unreachable */
34 if (distance == Integer.MAX_VALUE)
35 return;
36
37 /* The mob is too far from spawn */
39 if (mob.getCombat().isUnderAttack()) {
40 Mob trgt = target;
41 distance = Integer.MAX_VALUE;
42 mob.getCombat().reset();
43 if (mob.isNpc() && mob.getNpc().boundaries.length > 0) {
44 Position pos = Utility.randomElement(mob.getNpc().boundaries);
45 mob.interact(trgt);
46 mob.walkExactlyTo(pos, () -> {
47 mob.resetFace();
49 });
50 }
51 } else {
52 mob.getCombat().reset();
53 mob.resetFace();
55 }
56 return;
57 }
58
59 int dist = Utility.getDistance(target, mob);
60 int aggressionRadius = mob.width() + 3;
61
63 aggressionRadius = Region.SIZE;
64 }
65
66 /* The mob is too far from target */
67 if (dist > aggressionRadius)
68 return;
69
70 /* The mob is already in combat with the target */
71 if (mob.getCombat().isAttacking(target))
72 return;
73
74 if (!mob.getCombat().attack(target)) {
75 mob.getCombat().reset();
76 }
77 }
78
79 /**
80 * Compares the given mob with the current target. If the give mob is closer
81 * than the current target, the target will be set to the given mob.
82 *
83 * @param other the mob to compare to the target
84 */
85
86 void compare(Mob other, int level, Position spawn) {
87 int dist = Utility.getDistance(mob, other);
88
89 /* Already targeting this mob */
90 if (isTarget(other))
91 return;
92
93 /* The npc is too far from target */
94 if (dist > Region.VIEW_DISTANCE)
95 return;
96
97 if (spawn != null && !(mob instanceof Player && mob.activity != null) && !Area.inFightCaves(mob) && !Area.inGodwars(mob) && Utility.getDistance(other, spawn) > Region.VIEW_DISTANCE)
98 return;
99
100 if (other.skills.getCombatLevel() > level * 2 && !Area.inWilderness(mob))
101 return;
102
103 /* Found a closer target */
105 target = other;
106 distance = dist;
107 }
108 }
109
110 public static void checkAggression(Player player) {
111 if (!player.isVisible())
112 return;
113
114 if (player.viewport.getNpcsInViewport().isEmpty())
115 return;
116
117 if (player.getCombat().inCombat() && !Area.inMulti(player))
118 return;
119
120 for (Npc npc : player.viewport.getNpcsInViewport()) {
121 if (npc == null || !npc.isValid())
122 continue;
123
124 if (!npc.definition.isAttackable() || !npc.definition.isAggressive())
125 continue;
126
127 if (npc.isDead() || !npc.isVisible() || npc.forceWalking)
128 continue;
129
130 if (npc.locking.locked())
131 continue;
132
133 if (player.aggressionTimer.elapsed(AGGRESSION_TIMEOUT, TimeUnit.MINUTES)
134 && !Area.inGodwars(npc)
135 && player.activity == null) {
136 if (npc.getCombat().isAttacking(player) && !player.getCombat().isAttacking(npc))
137 npc.getCombat().reset();
138 continue;
139 }
140
141 /* Godwars check */
142 //TODO CLEANER WAY SORRY MICHAEL LOL
143 if (Area.inGodwars(npc) && !Area.inGodwarsChambers(npc)) {
144 if (npc.npcAssistant.isArmadyl()) {
145 boolean found = false;
146 for (Npc npc1 : npc.getRegion().getNpcs(npc.getHeight())) {
147 if (Utility.getDistance(npc, npc1.getPosition()) < 5 && !npc1.npcAssistant.isArmadyl()) {
148 found = true;
149 npc.getCombat().attack(npc1);
150 break;
151 }
152 }
153
154 if (found) {
155 continue;
156 }
157
158 if (player.equipment.hasArmadyl()) {
159 continue;
160 }
161 } else if (npc.npcAssistant.isBandos()) {
162 boolean found = false;
163 for (Npc npc1 : npc.getRegion().getNpcs(npc.getHeight())) {
164 if (Utility.getDistance(npc, npc1.getPosition()) < 5 && !npc1.npcAssistant.isBandos()) {
165 found = true;
166 npc.getCombat().attack(npc1);
167 break;
168 }
169 }
170
171 if (found) {
172 continue;
173 }
174
175 if (player.equipment.hasBandos()) {
176 continue;
177 }
178 } else if (npc.npcAssistant.isSaradomin()) {
179 boolean found = false;
180 for (Npc npc1 : npc.getRegion().getNpcs(npc.getHeight())) {
181 if (Utility.getDistance(npc, npc1.getPosition()) < 5 && !npc1.npcAssistant.isSaradomin()) {
182 found = true;
183 npc.getCombat().attack(npc1);
184 break;
185 }
186 }
187
188 if (found) {
189 continue;
190 }
191
192 if (player.equipment.hasSaradomin()) {
193 continue;
194 }
195 } else if (npc.npcAssistant.isZamorak()) {
196 boolean found = false;
197 for (Npc npc1 : npc.getRegion().getNpcs(npc.getHeight())) {
198 if (Utility.getDistance(npc, npc1.getPosition()) < 5 && !npc1.npcAssistant.isZamorak()) {
199 found = true;
200 npc.getCombat().attack(npc1);
201 break;
202 }
203 }
204
205 if (found) {
206 continue;
207 }
208
209 if (player.equipment.hasZamorak()) {
210 continue;
211 }
212 }
213 }
214
215 npc.getCombat().compare(player, npc.definition.getCombatLevel(), npc.spawnPosition);
216 }
217 }
218
219 void resetTarget() {
220 target = null;
221 distance = Integer.MAX_VALUE;
222 }
223
224 public void setTarget(Mob target) {
225 this.target = target;
227 }
228
229 boolean isTarget(Mob mob) {
230 return mob.equals(target);
231 }
232
233 public Mob getTarget() {
234 return target;
235 }
236}
void checkAggression(Position spawn)
Checks the aggression for this mob if a target is set.
void compare(Mob other, int level, Position spawn)
Compares the given mob with the current target.
static final int AGGRESSION_TIMEOUT
The aggression timeout in minutes.
Handles the mob class.
Definition Mob.java:66
List< Npc > getNpcsInViewport()
The collection of npcs in this viewport.
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
Combat< Player > getCombat()
The combat of the mob.
Definition Player.java:759
int getCombatLevel()
Gets the mob's combat level.
Represents a PathFinder which is meant to be used to check projectiles passage in a straight line.
static boolean checkProjectile(Interactable source, Interactable target)
Handles checking if mobs are in a certain area.
Definition Area.java:13
static boolean inMulti(Entity entity)
Definition Area.java:263
static boolean inGodwarsChambers(Mob mob)
Definition Area.java:382
static boolean inFightCaves(Interactable entity)
Definition Area.java:129
static boolean inWilderness(Position position)
Definition Area.java:272
static boolean inGodwars(Interactable entity)
Definition Area.java:133
Represents a single tile on the game world.
Definition Position.java:14
Represents a single region.
Definition Region.java:21
Collection< Npc > getNpcs(int height)
Definition Region.java:110
boolean elapsed(long time, TimeUnit unit)
Handles miscellaneous methods.
Definition Utility.java:27
static int getDistance(Interactable source, Position target)
Definition Utility.java:363
static< T > T randomElement(Collection< T > collection)
Picks a random element out of any array type.
Definition Utility.java:248