-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAA_HelemosShopBuyer.java
More file actions
355 lines (283 loc) · 10.4 KB
/
AA_HelemosShopBuyer.java
File metadata and controls
355 lines (283 loc) · 10.4 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
/**
* Buys dragon axes from Helemos at the Hero's Guild.
* Script parameter: # of dragon axes to buy
* <p>
* Start script at Falador west bank or at the Hero's Guild.
* Have at least 200,000 coins in bank and/or inventory.
* <p>
*
* @author Pomch
*/
public class AA_HelemosShopBuyer extends AA_Script {
private static final Coordinate COORD_LOAD_GATE_NORTH = new Coordinate(326, 544);
private static final Coordinate COORD_LOAD_GATE_SOUTH = new Coordinate(346, 479);
private static final Coordinate COORD_LOAD_FALADOR = new Coordinate(324, 512);
private static final int ITEM_ID_COINS = 10;
private static final int ITEM_ID_DRAGON_AXE = 594;
private static final int NPC_ID_HELEMOS = 269;
private static final int COST_DRAGON_AXE = 200000;
private static final int MAX_DIST = 18;
private Coordinate prevCoord;
private long startTime;
private long timeout;
private int axesBought;
private int coinsSpent;
private int limit;
private int remaining;
private boolean banking;
private boolean idle;
private boolean initialized;
public AA_HelemosShopBuyer(final Extension bot) {
super(bot);
}
@Override
public void init(final String parameters) {
if (parameters.isEmpty()) {
throw new IllegalStateException("Empty script parameter. Enter the number of dragon axes to buy.");
}
try {
limit = Integer.parseInt(parameters);
if (limit <= 0) throw new IllegalStateException("Script parameter must be a positive number.");
remaining = limit - axesBought;
} catch (final NumberFormatException e) {
throw new IllegalStateException("Enter the number of axes to buy as a script parameter.", e);
}
final int coinCount = getInventoryItemIdCount(ITEM_ID_COINS);
banking = coinCount < COST_DRAGON_AXE || (isInventoryFull() && coinCount > COST_DRAGON_AXE);
startTime = System.currentTimeMillis();
}
@Override
public int main() {
return !initialized ? initialize() : idle ? idle() : banking ? bank() : buy();
}
@Override
public void onServerMessage(final String message) {
if (message.endsWith("enough coins")) {
banking = true;
} else if (message.endsWith("coins")) {
axesBought++;
coinsSpent = axesBought * COST_DRAGON_AXE;
remaining = limit - axesBought;
if (axesBought >= limit) banking = true;
} else if (message.startsWith("Helemos") || message.endsWith("here?")) {
timeout = System.currentTimeMillis() + TIMEOUT_THREE_SECONDS;
} else if (message.endsWith("area")) {
idle = true;
prevCoord = new Coordinate(getPlayerX(), getPlayerY());
} else if (message.startsWith("You will now get receipts")) {
initialized = true;
} else {
super.onServerMessage(message);
}
}
@Override
public void paint() {
int y = PAINT_OFFSET_Y;
bot.drawString("@red@Helemos Shop Buyer", PAINT_OFFSET_X, y, 1, 0);
bot.drawString(String.format("@yel@Runtime: @whi@%s", toDuration(startTime)),
PAINT_OFFSET_X, y += PAINT_OFFSET_Y_INCREMENT, 1, 0);
bot.drawString(String.format("@yel@Spent: @whi@%s gp", DECIMAL_FORMAT.format(coinsSpent)),
PAINT_OFFSET_X, y += PAINT_OFFSET_Y_INCREMENT * 2, 1, 0);
bot.drawString(String.format("@yel@Bought: @whi@%s @cya@(@whi@%s axes@cya@/@whi@hr@cya@)",
DECIMAL_FORMAT.format(axesBought), toUnitsPerHour(axesBought, startTime)),
PAINT_OFFSET_X, y += PAINT_OFFSET_Y_INCREMENT, 1, 0);
bot.drawString(String.format("@yel@Remaining: @whi@%d axes", remaining),
PAINT_OFFSET_X, y += PAINT_OFFSET_Y_INCREMENT * 2, 1, 0);
bot.drawString(String.format("@yel@Time remaining: @whi@%s",
toTimeToCompletion(axesBought, remaining, startTime)),
PAINT_OFFSET_X, y + PAINT_OFFSET_Y_INCREMENT, 1, 0);
}
private int initialize() {
if (System.currentTimeMillis() <= timeout) return 0;
setTypeLine("::togglereceipts");
while (!next()) ;
timeout = System.currentTimeMillis() + TIMEOUT_TWO_SECONDS;
return 0;
}
private int idle() {
if (getPlayerX() != prevCoord.getX() || getPlayerY() != prevCoord.getY()) {
idle = false;
return 0;
}
final Coordinate walkableCoord = getWalkableCoordinate();
if (walkableCoord == null) {
System.err.println("Error handling idle movement. Could not find a tile to walk to.");
idle = false;
return 0;
}
walkTo(walkableCoord.getX(), walkableCoord.getY());
return SLEEP_ONE_TICK;
}
private int bank() {
if (isBankOpen()) {
for (int idx = 0; idx < getInventoryItemCount(); idx++) {
final int itemId = getInventoryItemId(idx);
if (itemId != ITEM_ID_COINS) {
deposit(itemId, getInventoryItemIdCount(itemId));
return SLEEP_ONE_TICK;
}
}
if (axesBought >= limit) {
return exit(String.format("Finished buying %d/%d dragon axes.",
axesBought, limit));
}
final int coinCount = getInventoryItemIdCount(ITEM_ID_COINS);
int reqCoins = Math.min(MAX_INVENTORY_SIZE * COST_DRAGON_AXE, (limit - axesBought) * COST_DRAGON_AXE);
if (coinCount > reqCoins) {
if (System.currentTimeMillis() <= timeout) return 0;
deposit(ITEM_ID_COINS, coinCount - reqCoins);
timeout = System.currentTimeMillis() + TIMEOUT_TWO_SECONDS;
return 0;
}
if (coinCount < reqCoins) {
if (System.currentTimeMillis() <= timeout) return 0;
final int bankedCoins = (getBankItemIdCount(ITEM_ID_COINS) / COST_DRAGON_AXE) *
COST_DRAGON_AXE;
if (bankedCoins < COST_DRAGON_AXE) {
return exit(String.format("Out of coins. Bought %d/%d dragon axes.",
axesBought, limit));
}
reqCoins = Math.min(reqCoins, bankedCoins);
if (coinCount < reqCoins) {
withdraw(ITEM_ID_COINS, reqCoins - coinCount);
timeout = System.currentTimeMillis() + TIMEOUT_TWO_SECONDS;
return 0;
}
}
banking = false;
closeBank();
return 0;
}
if (ScriptArea.BANK.contains(getPlayerX(), getPlayerY())) return openBank();
if (ScriptArea.GUILD_ENTRANCE.contains(getPlayerX(), getPlayerY())) {
if (!isInventoryFull()) {
final int[] axe = getItemById(ITEM_ID_DRAGON_AXE);
if (axe[0] != -1 && ScriptArea.GUILD_ENTRANCE.contains(axe[1], axe[2])) {
pickupItem(ITEM_ID_DRAGON_AXE, axe[1], axe[2]);
return SLEEP_ONE_TICK;
}
}
final Coordinate guildDoor = ScriptObject.GUILD_DOOR.getCoordinate();
atWallObject(guildDoor.getX(), guildDoor.getY());
return SLEEP_ONE_SECOND;
}
if (getPlayerX() <= ScriptObject.MEMBERS_GATE.getCoordinate().getX()) {
if (getPlayerY() < COORD_LOAD_FALADOR.getY()) {
walkTo(COORD_LOAD_FALADOR.getX(), COORD_LOAD_FALADOR.getY());
return SLEEP_ONE_TICK;
}
final Coordinate bankDoors = ScriptObject.BANK_DOORS.getCoordinate();
if (distanceTo(bankDoors.getX(), bankDoors.getY()) > MAX_DIST) {
walkTo(bankDoors.getX(), bankDoors.getY());
return SLEEP_ONE_TICK;
}
if (getObjectId(bankDoors.getX(), bankDoors.getY()) == ScriptObject.BANK_DOORS.getId()) {
atObject(bankDoors.getX(), bankDoors.getY());
return SLEEP_ONE_SECOND;
}
walkTo(bankDoors.getX() + 1, bankDoors.getY());
return SLEEP_ONE_TICK;
}
if (getPlayerY() >= COORD_LOAD_GATE_SOUTH.getY()) {
if (System.currentTimeMillis() <= timeout) return 0;
final Coordinate membersGate = ScriptObject.MEMBERS_GATE.getCoordinate();
atObject(membersGate.getX(), membersGate.getY());
timeout = System.currentTimeMillis() + TIMEOUT_FIVE_SECONDS;
return 0;
}
walkTo(COORD_LOAD_GATE_SOUTH.getX(), COORD_LOAD_GATE_SOUTH.getY());
return SLEEP_ONE_TICK;
}
private int buy() {
if (isShopOpen()) {
final int coinCount = getInventoryItemIdCount(ITEM_ID_COINS);
if (coinCount < COST_DRAGON_AXE) {
banking = true;
return 0;
}
if (getShopItemCount(0) == 0) return 0;
if (isInventoryFull()) {
if (coinCount > COST_DRAGON_AXE) {
banking = true;
return 0;
}
final int axeIdx = getInventoryItemIndex(ITEM_ID_DRAGON_AXE);
if (axeIdx == -1) {
banking = true;
return 0;
}
dropItem(axeIdx);
return SLEEP_ONE_SECOND;
}
buyShopItem(0, 1);
return SLEEP_TWO_SECONDS;
}
if (ScriptArea.GUILD_ENTRANCE.contains(getPlayerX(), getPlayerY())) {
final Object helemos = getNearestNpcNotTalking(NPC_ID_HELEMOS);
if (helemos == null) return 0;
return openShop(NPC_ID_HELEMOS);
}
if (ScriptArea.BANK.contains(getPlayerX(), getPlayerY())) {
final Coordinate bankDoors = ScriptObject.BANK_DOORS.getCoordinate();
if (getObjectId(bankDoors.getX(), bankDoors.getY()) == ScriptObject.BANK_DOORS.getId()) {
atObject(bankDoors.getX(), bankDoors.getY());
return SLEEP_ONE_SECOND;
}
}
if (getPlayerX() > ScriptObject.MEMBERS_GATE.getCoordinate().getX()) {
final Coordinate guildDoor = ScriptObject.GUILD_DOOR.getCoordinate();
if (distanceTo(guildDoor.getX(), guildDoor.getY()) <= 1) {
atWallObject(guildDoor.getX(), guildDoor.getY());
return SLEEP_ONE_SECOND;
}
walkTo(guildDoor.getX(), guildDoor.getY());
return SLEEP_ONE_TICK;
}
if (getPlayerY() <= COORD_LOAD_GATE_NORTH.getY()) {
final Coordinate membersGate = ScriptObject.MEMBERS_GATE.getCoordinate();
if (distanceTo(membersGate.getX(), membersGate.getY()) <= 1) {
if (System.currentTimeMillis() <= timeout) return 0;
atObject(membersGate.getX(), membersGate.getY());
timeout = System.currentTimeMillis() + TIMEOUT_TWO_SECONDS;
return 0;
}
walkTo(membersGate.getX(), membersGate.getY());
return SLEEP_ONE_TICK;
}
walkTo(COORD_LOAD_GATE_NORTH.getX(), COORD_LOAD_GATE_NORTH.getY());
return SLEEP_ONE_TICK;
}
private enum ScriptArea implements RSArea {
GUILD_ENTRANCE(new Coordinate(368, 434), new Coordinate(377, 440)),
BANK(new Coordinate(328, 549), new Coordinate(334, 557));
private final Coordinate lowerBoundingCoordinate;
private final Coordinate upperBoundingCoordinate;
ScriptArea(Coordinate lowerBoundingCoordinate, Coordinate upperBoundingCoordinate) {
this.lowerBoundingCoordinate = lowerBoundingCoordinate;
this.upperBoundingCoordinate = upperBoundingCoordinate;
}
public Coordinate getLowerBoundingCoordinate() {
return this.lowerBoundingCoordinate;
}
public Coordinate getUpperBoundingCoordinate() {
return this.upperBoundingCoordinate;
}
}
private enum ScriptObject implements RSObject {
GUILD_DOOR(74, new Coordinate(372, 441)),
MEMBERS_GATE(137, new Coordinate(341, 487)),
BANK_DOORS(64, new Coordinate(327, 552));
private final int id;
private final Coordinate coordinate;
ScriptObject(int id, Coordinate coordinate) {
this.id = id;
this.coordinate = coordinate;
}
public int getId() {
return this.id;
}
public Coordinate getCoordinate() {
return this.coordinate;
}
}
}