RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
CombatTarget.java
1package com.osroyale.game.world.entity.combat;
2
3import com.osroyale.game.world.entity.mob.Mob;
4import com.osroyale.game.world.entity.mob.npc.Npc;
5import com.osroyale.game.world.entity.mob.player.Player;
6import com.osroyale.game.world.pathfinding.path.SimplePathChecker;
7import com.osroyale.game.world.position.Area;
8import com.osroyale.game.world.position.Position;
9import com.osroyale.game.world.region.Region;
10import com.osroyale.util.Utility;
11
12import java.util.concurrent.TimeUnit;
13
47
48public class CombatTarget {
49
51 private static final int AGGRESSION_TIMEOUT = 20;
52 private final Mob mob;
53 private Mob target;
54 private int distance;
55
56 CombatTarget(Mob mob) {
57 this.mob = mob;
58 this.distance = Integer.MAX_VALUE;
59 }
60
62 void checkAggression(Position spawn) {
63 /* No target */
64 if (target == null)
65 return;
66
67 /* The target is unreachable */
68 if (distance == Integer.MAX_VALUE)
69 return;
70
71 /* The mob is too far from spawn */
72 if (Utility.getDistance(mob, spawn) > Region.VIEW_DISTANCE && !Area.inFightCaves(mob) && !Area.inGodwarsChambers(mob)) {
73 if (mob.getCombat().isUnderAttack()) {
74 Mob trgt = target;
75 distance = Integer.MAX_VALUE;
76 mob.getCombat().reset();
77 if (mob.isNpc() && mob.getNpc().boundaries.length > 0) {
78 Position pos = Utility.randomElement(mob.getNpc().boundaries);
79 mob.interact(trgt);
80 mob.walkExactlyTo(pos, () -> {
81 mob.resetFace();
82 resetTarget();
83 });
84 }
85 } else {
86 mob.getCombat().reset();
87 mob.resetFace();
88 resetTarget();
89 }
90 return;
91 }
92
93 int dist = Utility.getDistance(target, mob);
94 int aggressionRadius = mob.width() + 3;
95
96 if (Area.inGodwarsChambers(mob)) {
97 aggressionRadius = Region.SIZE;
98 }
99
100 /* The mob is too far from target */
101 if (dist > aggressionRadius)
102 return;
103
104 /* The mob is already in combat with the target */
105 if (mob.getCombat().isAttacking(target))
106 return;
107
108 if (!mob.getCombat().attack(target)) {
109 mob.getCombat().reset();
110 }
111 }
112
119
120 void compare(Mob other, int level, Position spawn) {
121 int dist = Utility.getDistance(mob, other);
122
123 /* Already targeting this mob */
124 if (isTarget(other))
125 return;
126
127 /* The npc is too far from target */
128 if (dist > Region.VIEW_DISTANCE)
129 return;
130
131 if (spawn != null && !(mob instanceof Player && mob.activity != null) && !Area.inFightCaves(mob) && !Area.inGodwars(mob) && Utility.getDistance(other, spawn) > Region.VIEW_DISTANCE)
132 return;
133
134 if (other.skills.getCombatLevel() > level * 2 && !Area.inWilderness(mob))
135 return;
136
137 /* Found a closer target */
138 if (dist < distance && (SimplePathChecker.checkProjectile(mob, other) || SimplePathChecker.checkProjectile(other, mob))) {
139 target = other;
140 distance = dist;
141 }
142 }
143
144 public static void checkAggression(Player player) {
145 if (!player.isVisible())
146 return;
147
148 if (player.viewport.getNpcsInViewport().isEmpty())
149 return;
150
151 if (player.getCombat().inCombat() && !Area.inMulti(player))
152 return;
153
154 for (Npc npc : player.viewport.getNpcsInViewport()) {
155 if (npc == null || !npc.isValid())
156 continue;
157
158 if (!npc.definition.isAttackable() || !npc.definition.isAggressive())
159 continue;
160
161 if (npc.isDead() || !npc.isVisible() || npc.forceWalking)
162 continue;
163
164 if (npc.locking.locked())
165 continue;
166
167 if (player.aggressionTimer.elapsed(AGGRESSION_TIMEOUT, TimeUnit.MINUTES)
168 && !Area.inGodwars(npc)
169 && player.activity == null) {
170 if (npc.getCombat().isAttacking(player) && !player.getCombat().isAttacking(npc))
171 npc.getCombat().reset();
172 continue;
173 }
174
175 /* Godwars check */
176 //TODO CLEANER WAY SORRY MICHAEL LOL
177 if (Area.inGodwars(npc) && !Area.inGodwarsChambers(npc)) {
178 if (npc.npcAssistant.isArmadyl()) {
179 boolean found = false;
180 for (Npc npc1 : npc.getRegion().getNpcs(npc.getHeight())) {
181 if (Utility.getDistance(npc, npc1.getPosition()) < 5 && !npc1.npcAssistant.isArmadyl()) {
182 found = true;
183 npc.getCombat().attack(npc1);
184 break;
185 }
186 }
187
188 if (found) {
189 continue;
190 }
191
192 if (player.equipment.hasArmadyl()) {
193 continue;
194 }
195 } else if (npc.npcAssistant.isBandos()) {
196 boolean found = false;
197 for (Npc npc1 : npc.getRegion().getNpcs(npc.getHeight())) {
198 if (Utility.getDistance(npc, npc1.getPosition()) < 5 && !npc1.npcAssistant.isBandos()) {
199 found = true;
200 npc.getCombat().attack(npc1);
201 break;
202 }
203 }
204
205 if (found) {
206 continue;
207 }
208
209 if (player.equipment.hasBandos()) {
210 continue;
211 }
212 } else if (npc.npcAssistant.isSaradomin()) {
213 boolean found = false;
214 for (Npc npc1 : npc.getRegion().getNpcs(npc.getHeight())) {
215 if (Utility.getDistance(npc, npc1.getPosition()) < 5 && !npc1.npcAssistant.isSaradomin()) {
216 found = true;
217 npc.getCombat().attack(npc1);
218 break;
219 }
220 }
221
222 if (found) {
223 continue;
224 }
225
226 if (player.equipment.hasSaradomin()) {
227 continue;
228 }
229 } else if (npc.npcAssistant.isZamorak()) {
230 boolean found = false;
231 for (Npc npc1 : npc.getRegion().getNpcs(npc.getHeight())) {
232 if (Utility.getDistance(npc, npc1.getPosition()) < 5 && !npc1.npcAssistant.isZamorak()) {
233 found = true;
234 npc.getCombat().attack(npc1);
235 break;
236 }
237 }
238
239 if (found) {
240 continue;
241 }
242
243 if (player.equipment.hasZamorak()) {
244 continue;
245 }
246 }
247 }
248
249 npc.getCombat().compare(player, npc.definition.getCombatLevel(), npc.spawnPosition);
250 }
251 }
252
253 void resetTarget() {
254 target = null;
255 distance = Integer.MAX_VALUE;
256 }
257
258 public void setTarget(Mob target) {
259 this.target = target;
260 distance = Utility.getDistance(mob, target);
261 }
262
263 boolean isTarget(Mob mob) {
264 return mob.equals(target);
265 }
266
267 public Mob getTarget() {
268 return target;
269 }
270}
abstract Combat<? extends Mob > getCombat()
Collection< Npc > getNpcs(int height)
Definition Region.java:147
static< T > T randomElement(Collection< T > collection)
Definition Utility.java:285