-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAA_RedSpidersEggs.java
More file actions
496 lines (387 loc) · 13.9 KB
/
AA_RedSpidersEggs.java
File metadata and controls
496 lines (387 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import java.util.HashMap;
import java.util.Map;
/**
* Collects red spiders eggs in Edgeville Dungeon.
* Banks at Edgeville Bank. Walks back to Edgeville on death.
* <p>
* Required:
* Start script at Edgeville Bank or at Deadly Red Spiders.
* Optional: Sleeping bag, equipment, weapon.
* <p>
* Optional Parameter:
* <controlled|attack|strength|defense> (default strength)
* <p>
*
* @Author Chomp
*/
public class AA_RedSpidersEggs extends AA_Script {
private static final Coordinate COORD_LUMBRIDGE = new Coordinate(120, 648);
private static final Coordinate COORD_EDGEVILLE = new Coordinate(213, 456);
private static final Coordinate COORD_WILD_GATE = new Coordinate(198, 3248);
private static final int[] NPC_IDS_BLOCKING = new int[]{23, 40, 46};
private static final int COORD_Y_DUNGEON = 3000;
private final Map<RedSpidersEggs, Long> spawnMap = new HashMap<>();
private Coordinate nextEggSpawn;
private PathWalker pathWalker;
private long startTime;
private int playerX;
private int playerY;
private int initialEggsCount;
private int eggsCollected;
private int eggsBanked;
private int deathCount;
private boolean died;
private boolean banking;
public AA_RedSpidersEggs(final Extension extension) {
super(extension);
}
@Override
public void init(final String parameters) {
if (!parameters.isEmpty()) combatStyle = CombatStyle.valueOf(parameters.toUpperCase());
setCombatStyle(combatStyle.getIndex());
initialEggsCount = getInventoryCount(RedSpidersEggs.ITEM_ID);
banking = getInventoryCount() == MAX_INV_SIZE;
startTime = System.currentTimeMillis();
}
@Override
public int main() {
if (died) {
if (isDead() || (pathWalker != null && pathWalker.walkPath())) {
return 0;
}
pathWalker = null;
died = false;
}
playerX = getX();
playerY = getY();
if (bot.getCombatStyle() != combatStyle.getIndex()) setCombatStyle(combatStyle.getIndex());
return banking ? bank() : collectEggs();
}
@Override
public void onDeath() {
System.out.printf("[%s] Oh dear, you are dead.%n", this);
if (pathWalker == null) {
pathWalker = new PathWalker(bot);
pathWalker.init(null);
}
final PathWalker.Path path = pathWalker.calcPath(COORD_LUMBRIDGE.getX(),
COORD_LUMBRIDGE.getY(),
COORD_EDGEVILLE.getX(),
COORD_EDGEVILLE.getY());
if (path != null) {
pathWalker.setPath(path);
deathCount++;
died = true;
} else {
exit("Failed to calculate path from Lumbridge to Edgeville.");
}
}
private int bank() {
if (Area.BANK.contains(playerX, playerY)) {
if (!isBanking()) {
return openBank();
}
if (hasInventoryItem(RedSpidersEggs.ITEM_ID)) {
deposit(RedSpidersEggs.ITEM_ID, MAX_INV_SIZE);
return SLEEP_ONE_TICK;
}
if (!hasInventoryItem(ITEM_ID_SLEEPING_BAG) && hasBankItem(ITEM_ID_SLEEPING_BAG)) {
withdraw(ITEM_ID_SLEEPING_BAG, 1);
return SLEEP_TWO_SECONDS;
}
eggsBanked = bankCount(RedSpidersEggs.ITEM_ID);
banking = false;
return 0;
}
if (Area.LADDER_ROOM.contains(playerX, playerY)) {
if (isWallObjectClosed(Object.LADDER_DOOR)) {
atWallObject(Object.LADDER_DOOR.coordinate.getX(), Object.LADDER_DOOR.coordinate.getY());
return SLEEP_ONE_SECOND;
}
walkTo(Object.LADDER_DOOR.coordinate.getX(), Object.LADDER_DOOR.coordinate.getY() - 1);
return SLEEP_ONE_TICK;
}
if (!isInDungeon()) {
if (isWithinReach(Object.BANK_DOORS)) {
if (isObjectClosed(Object.BANK_DOORS)) {
atObject(Object.BANK_DOORS.coordinate.getX(), Object.BANK_DOORS.coordinate.getY());
return SLEEP_ONE_SECOND;
}
walkTo(Object.BANK_DOORS.coordinate.getX(), Object.BANK_DOORS.coordinate.getY() + 1);
return SLEEP_ONE_TICK;
}
walkTo(Object.BANK_DOORS.coordinate.getX(), Object.BANK_DOORS.coordinate.getY());
return SLEEP_ONE_TICK;
}
if (isInWilderness()) {
if (playerY < COORD_WILD_GATE.getY()) {
walkTo(COORD_WILD_GATE.getX(), COORD_WILD_GATE.getY());
return SLEEP_ONE_TICK;
}
if (isWithinReach(Object.WILDERNESS_GATE)) {
atObject(Object.WILDERNESS_GATE.coordinate.getX(), Object.WILDERNESS_GATE.coordinate.getY());
return SLEEP_ONE_SECOND;
}
walkTo(Object.WILDERNESS_GATE.coordinate.getX() + 1, Object.WILDERNESS_GATE.coordinate.getY() - 1);
return SLEEP_ONE_TICK;
}
if (!inCombat()) {
final int blockingNpcIndex = getBlockingNpcIndex(playerX, playerY);
if (blockingNpcIndex != -1) {
attackNpc(blockingNpcIndex);
return SLEEP_ONE_TICK;
}
}
if (playerX < Object.DUNGEON_GATE.coordinate.getX()) {
if (isWithinReach(Object.DUNGEON_GATE)) {
if (isObjectClosed(Object.DUNGEON_GATE)) {
atObject(Object.DUNGEON_GATE.coordinate.getX(), Object.DUNGEON_GATE.coordinate.getY());
return SLEEP_ONE_SECOND;
}
walkTo(Object.DUNGEON_GATE.coordinate.getX(), Object.DUNGEON_GATE.coordinate.getY());
return SLEEP_ONE_TICK;
}
walkTo(Object.DUNGEON_GATE.coordinate.getX() - 1, Object.DUNGEON_GATE.coordinate.getY());
return SLEEP_ONE_TICK;
}
if (isWithinReach(Object.LADDER_UP)) {
atObject(Object.LADDER_UP.coordinate.getX(), Object.LADDER_UP.coordinate.getY());
return SLEEP_ONE_SECOND;
}
walkTo(Object.LADDER_UP.coordinate.getX(), Object.LADDER_UP.coordinate.getY() - 1);
return SLEEP_ONE_TICK;
}
private int collectEggs() {
if (Area.RED_SPIDERS.contains(playerX, playerY)) {
if (getInventoryCount() == MAX_INV_SIZE) {
eggsCollected += getInventoryCount(RedSpidersEggs.ITEM_ID);
banking = true;
return 0;
}
final RedSpidersEggs redSpidersEggs = getNearestRedSpidersEggs();
if (redSpidersEggs != null) {
if (inCombat()) {
walkTo(redSpidersEggs.coordinate.getX(), redSpidersEggs.coordinate.getY());
return SLEEP_ONE_TICK;
}
pickupItem(RedSpidersEggs.ITEM_ID, redSpidersEggs.coordinate.getX(), redSpidersEggs.coordinate.getY());
return SLEEP_ONE_TICK;
}
if (nextEggSpawn != null &&
(playerX != nextEggSpawn.getX() || playerY != nextEggSpawn.getY())) {
walkTo(nextEggSpawn.getX(), nextEggSpawn.getY());
return SLEEP_ONE_TICK;
}
return 0;
}
if (isInDungeon()) {
if (isInWilderness()) {
walkTo(Area.RED_SPIDERS.lowerBoundingCoordinate.getX(), Area.RED_SPIDERS.upperBoundingCoordinate.getY());
return SLEEP_ONE_TICK;
}
if (!inCombat()) {
final int blockingNpcIndex = getBlockingNpcIndex(playerX, playerY);
if (blockingNpcIndex != -1) {
attackNpc(blockingNpcIndex);
return SLEEP_ONE_TICK;
}
}
if (playerX < Object.DUNGEON_GATE.coordinate.getX()) {
if (getFatigue() != 0 && hasInventoryItem(ITEM_ID_SLEEPING_BAG)) {
return sleep();
}
if (isWithinReach(Object.WILDERNESS_GATE)) {
atObject(Object.WILDERNESS_GATE.coordinate.getX(), Object.WILDERNESS_GATE.coordinate.getY());
return SLEEP_ONE_SECOND;
}
walkTo(Object.WILDERNESS_GATE.coordinate.getX(), Object.WILDERNESS_GATE.coordinate.getY());
return SLEEP_ONE_TICK;
}
if (isWithinReach(Object.DUNGEON_GATE)) {
if (isObjectClosed(Object.DUNGEON_GATE)) {
atObject(Object.DUNGEON_GATE.coordinate.getX(), Object.DUNGEON_GATE.coordinate.getY());
return SLEEP_ONE_SECOND;
}
walkTo(Object.DUNGEON_GATE.coordinate.getX() - 1, Object.DUNGEON_GATE.coordinate.getY());
return SLEEP_ONE_TICK;
}
walkTo(Object.DUNGEON_GATE.coordinate.getX(), Object.DUNGEON_GATE.coordinate.getY());
return SLEEP_ONE_TICK;
}
if (Area.LADDER_ROOM.contains(playerX, playerY)) {
atObject(Object.LADDER_DOWN.coordinate.getX(), Object.LADDER_DOWN.coordinate.getY());
return SLEEP_ONE_SECOND;
}
if (Area.BANK.contains(playerX, playerY)) {
if (isObjectClosed(Object.BANK_DOORS)) {
atObject(Object.BANK_DOORS.coordinate.getX(), Object.BANK_DOORS.coordinate.getY());
return SLEEP_ONE_SECOND;
}
walkTo(Object.LADDER_DOOR.coordinate.getX(), Object.LADDER_DOOR.coordinate.getY() - 1);
return SLEEP_ONE_TICK;
}
if (isWithinReach(Object.LADDER_DOOR)) {
if (isWallObjectClosed(Object.LADDER_DOOR)) {
atWallObject(Object.LADDER_DOOR.coordinate.getX(), Object.LADDER_DOOR.coordinate.getY());
return SLEEP_ONE_SECOND;
}
walkTo(Object.LADDER_DOOR.coordinate.getX(), Object.LADDER_DOOR.coordinate.getY());
return SLEEP_ONE_TICK;
}
walkTo(Object.LADDER_DOOR.coordinate.getX(), Object.LADDER_DOOR.coordinate.getY() - 1);
return SLEEP_ONE_TICK;
}
private boolean isWallObjectClosed(final Object wallObject) {
return getWallObjectIdFromCoords(wallObject.coordinate.getX(), wallObject.coordinate.getY()) == wallObject.id;
}
private boolean isInDungeon() {
return playerY >= COORD_Y_DUNGEON;
}
private boolean isWithinReach(final Object object) {
return distanceTo(object.coordinate.getX(), object.coordinate.getY()) <= 1;
}
private boolean isObjectClosed(final Object object) {
return getObjectIdFromCoords(object.coordinate.getX(), object.coordinate.getY()) == object.id;
}
private boolean isInWilderness() {
return playerY < Object.WILDERNESS_GATE.coordinate.getY();
}
private int getBlockingNpcIndex(final int playerX, final int playerY) {
final int direction = bot.getMobDirection(bot.getPlayer());
for (int index = 0; index < bot.getNpcCount(); index++) {
if (!inArray(NPC_IDS_BLOCKING, getNpcId(index))) {
continue;
}
final int npcX = getNpcX(index);
final int npcY = getNpcY(index);
if (npcY == playerY) {
if (npcX == playerX - 1 && (direction == DIR_EAST || direction == DIR_NORTHEAST)) {
return index;
}
if (npcX == playerX + 1 && (direction == DIR_WEST || direction == DIR_SOUTHWEST)) {
return index;
}
}
if (npcX == playerX) {
if (npcY == playerY - 1 &&
(direction == DIR_NORTH ||
direction == DIR_NORTHWEST ||
direction == DIR_EAST ||
direction == DIR_NORTHEAST)) {
return index;
}
if (npcY == playerY + 1 && direction >= DIR_WEST && direction <= DIR_SOUTHEAST) {
return index;
}
}
}
return -1;
}
private RedSpidersEggs getNearestRedSpidersEggs() {
RedSpidersEggs redSpidersEggs = null;
int minimumDistance = Integer.MAX_VALUE;
for (final RedSpidersEggs spawn : RedSpidersEggs.SPAWNS) {
if (!isItemAt(RedSpidersEggs.ITEM_ID, spawn.coordinate.getX(), spawn.coordinate.getY())) {
continue;
}
final int distance = distanceTo(spawn.coordinate.getX(), spawn.coordinate.getY());
if (distance >= minimumDistance) {
continue;
}
minimumDistance = distance;
redSpidersEggs = spawn;
}
return redSpidersEggs;
}
@Override
public void paint() {
int y = PAINT_OFFSET_Y;
drawString("@yel@Red Spiders Eggs", PAINT_OFFSET_X, y, 1, 0);
drawString(String.format("@yel@Runtime: @whi@%s", toDuration(startTime)),
PAINT_OFFSET_X, y += PAINT_OFFSET_Y_INCREMENT, 1, 0);
final int count = Math.max(0, eggsCollected + getInventoryCount(RedSpidersEggs.ITEM_ID) - initialEggsCount);
drawString(String.format("@yel@Collected: @whi@%s @cya@(@whi@%s eggs@cya@/@whi@hr@cya@)",
DECIMAL_FORMAT.format(count), toUnitsPerHour(count, startTime)),
PAINT_OFFSET_X, y += PAINT_OFFSET_Y_INCREMENT * 2, 1, 0);
if (eggsBanked > 0) {
drawString(String.format("@gre@Banked: @whi@%s", DECIMAL_FORMAT.format(eggsBanked)),
PAINT_OFFSET_X, y += PAINT_OFFSET_Y_INCREMENT, 1, 0);
}
if (deathCount > 0) {
drawString("", PAINT_OFFSET_X, y += PAINT_OFFSET_Y_INCREMENT, 1, 0);
drawString(String.format("@red@Deaths: @whi@%d", deathCount),
PAINT_OFFSET_X, y + PAINT_OFFSET_Y_INCREMENT, 1, 0);
}
}
@Override
public void onGroundItemDespawned(final int groundItemIndex) {
if (bot.getGroundItemId(groundItemIndex) != RedSpidersEggs.ITEM_ID) {
return;
}
final int x = bot.getGroundItemLocalX(groundItemIndex) + bot.getAreaX();
final int y = bot.getGroundItemLocalY(groundItemIndex) + bot.getAreaY();
final RedSpidersEggs redSpidersEggs = RedSpidersEggs.fromCoord(x, y);
if (redSpidersEggs == null) {
return;
}
spawnMap.put(redSpidersEggs, System.currentTimeMillis());
nextEggSpawn = spawnMap.entrySet().stream().min(Map.Entry.comparingByValue()).get().getKey().coordinate;
}
private enum RedSpidersEggs implements Comparable<RedSpidersEggs> {
NORTH(new Coordinate(204, 3232)),
EAST(new Coordinate(201, 3234)),
SOUTH(new Coordinate(208, 3240)),
WEST(new Coordinate(209, 3236));
private static final RedSpidersEggs[] SPAWNS = RedSpidersEggs.values();
private static final int ITEM_ID = 219;
private final Coordinate coordinate;
RedSpidersEggs(final Coordinate coordinate) {
this.coordinate = coordinate;
}
private static RedSpidersEggs fromCoord(final int x, final int y) {
for (final RedSpidersEggs eggs : SPAWNS) {
if (x == eggs.coordinate.getX() && y == eggs.coordinate.getY()) {
return eggs;
}
}
return null;
}
}
private enum Area implements RSArea {
RED_SPIDERS(new Coordinate(197, 3225), new Coordinate(210, 3243)),
BANK(new Coordinate(212, 448), new Coordinate(220, 453)),
LADDER_ROOM(new Coordinate(214, 465), new Coordinate(218, 469));
private final Coordinate lowerBoundingCoordinate;
private final Coordinate upperBoundingCoordinate;
Area(final Coordinate lowerBoundingCoordinate, final Coordinate upperBoundingCoordinate) {
this.lowerBoundingCoordinate = lowerBoundingCoordinate;
this.upperBoundingCoordinate = upperBoundingCoordinate;
}
public Coordinate getLowerBoundingCoordinate() {
return lowerBoundingCoordinate;
}
public Coordinate getUpperBoundingCoordinate() {
return upperBoundingCoordinate;
}
}
private enum Object implements RSObject {
DUNGEON_GATE(57, new Coordinate(211, 3272)),
WILDERNESS_GATE(305, new Coordinate(196, 3266)),
BANK_DOORS(64, new Coordinate(217, 447)),
LADDER_DOOR(2, new Coordinate(218, 465)),
LADDER_UP(5, new Coordinate(215, 3300)),
LADDER_DOWN(6, new Coordinate(215, 468));
private final int id;
private final Coordinate coordinate;
Object(final int id, final Coordinate coordinate) {
this.id = id;
this.coordinate = coordinate;
}
public int getId() {
return id;
}
public Coordinate getCoordinate() {
return coordinate;
}
}
}