-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
456 lines (337 loc) · 8.04 KB
/
functions.js
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
/*
Main Variables
More Inside the Code
*/
// The Minimal XP a Monster must give to be target
var MIN_XP_OF_MONSTER = 800
// The Maximal Level your Items are beeing upgraded
var MAX_ITEM_LEVEL = 6
// The Json Information to show -- Development
var SHOW_INFORMATION = character
// The Number of Milliseconds for the Main Loop
var LOOP_SPEED = 250
/*
Begin of Functions
*/
/* Main Functions */
// This Function is called on each Loop
function Main() {
// Don't do anything while Moving
if (character.moving) { return; }
// Get target from Server
var target = get_targeted_monster();
// Act
OneLoop(target);
return;
}
// Action And Order the Character does each round
function OneLoop(target) {
DrinkPotion()
Loot()
Attack(target)
Move(target)
CheckTarget(target)
Update()
Refill()
return;
}
/* Support Functions */
// Min HP to drink Potion
var minhp_potion = 500
// Min MP to drink Potion
var minmp_potion = 500
// Drink a HP or MP Potion
function DrinkPotion() {
if (hpLow()) {
parent.use('hp')
}
if (mpLow()) {
parent.use('mp');
}
return;
}
// Defines When to drink Hp
function hpLow() {
if(character.hp < minhp_potion) {
return true;
}
return false;
}
// Defines when to drink Mp
function mpLow() {
if (character.mp < minmp_potion) {
return true;
}
return false;
}
/* Loot Functions */
// Loot the chests
function Loot() {
loot();
}
/* Attack Functions */
// Attack the Target
function Attack(target) {
if (!target) return;
if (!in_attack_range(target)) return;
if ( target.hp && !target.dead ) {
attack(target)
}
return;
}
/* Movement Functions */
// Move in X Direction
var movementspeed_x = 1
// Move in Y Direction
var movementspeed_y = 2
// Move towards Target If possible
function Move(target) {
if (!target) {
if (attack_target_off!=null){
// Go to Friend
MoveTo(attack_target_off)
return;
}
// Just Move Around
MoveAround()
return;
}
if(!in_attack_range(target)) {
// Get Closer to it
MoveTowards(target)
return;
}
return;
}
// Move to Specific Player
function MoveTo(player) {
set_message("Go to " + player.name)
move(
character.real_x+(player.real_x-character.real_x-1),
character.real_y+(player.real_y-character.real_y-1)
);
return;
}
// Move to a Monster
function MoveTowards(monster) {
game_log("Move half the Distance to monster")
// Half the Distance? Should be configurable...
move(
character.real_x+(target.real_x-character.real_x)/2,
character.real_y+(target.real_y-character.real_y)/2
);
return;
}
// Move without target
function MoveAround() {
set_message("No Monsters, bored");
// Move according to speed
move(
character.real_x + movementspeed_x,
character.real_y + movementspeed_y
);
return;
}
/* Aiming Functions */
// Name of the player to get target from
var attack_target_off = null
// Max Atk for Monster
var monster_maxatk = 200
// Min XP for Monster
var monster_minxp = MIN_XP_OF_MONSTER
// Name of the Monster
var monster_name = null
// Get Monster which is 'free'
var monster_notarget = true
// Get New Target if needed
function CheckTarget(target) {
if (!target) {
target = GetTargetOff(attack_target_off)
if (!target) {
// Get New Monster Target
target = GetNearestMonster();
}
if (target) {
game_log("Set Target to " + target.mtype)
change_target(target)
return;
}
}
return;
}
// Get someelses Target
function GetTargetOff(playername) {
// Check
if (!playername) return null;
// Check out the Player
player = get_player(playername);
// Check out his target
target = get_target_of(player);
return target;
}
// Get Nearest Monster
function GetNearestMonster() {
target=get_nearest_monster({
min_xp:monster_minxp,
max_att:monster_maxatk,
target:monster_name,
no_target:monster_notarget
});
return target;
}
/* Update Functions */
// The Max Level to wich Items should be updated
var MaxItemLevel = MAX_ITEM_LEVEL
// Minimal Money you want to keep
var MoneySwitch = 100000
function Update() {
// Don't Update if no Money
if (character.gold <= MoneySwitch) {
set_message("Updates Paused")
return
}
// Loop through Equipped Items
for (slot in character.slots) {
// Get the Item
item = character.slots[slot]
// Get Name & Level
var name = item["name"]
var level = item["level"]
// Excluding it for the moment -- Needs a different Scroll
if (name == "hpamulet") {
continue;
}
// Buy the Item if needed
BuyItem(name)
// Update the Item if possible
UpdateItem(name)
// Equip the Item if needed
EquipItem(name, level)
}
}
// Updates the Item
function UpdateItem(ItemName) {
// Get Item
item = FindItem(ItemName)
if (!item) {
game_log("Error: Item not found")
return
}
// Max Level?
if(item["level"] >= MaxItemLevel) {
return
}
// Get the Scroll
scroll = FindItem(ScrollName)
// No Scrolls - No Updates
if (!scroll) {
return
}
// Finally -- Do it!
upgrade(ItemIndex(ItemName), ItemIndex(ScrollName))
return;
}
// Get the Item according to ItemName
function FindItem(ItemName) {
// Finds First Item of the Name
for (i=0; i<character.items.length; i++) {
// Check out the Item
item = character.items[i]
// Does it fit?
if (item && item["name"] == ItemName) {
return item;
}
}
return null;
}
// Get the Index of the Item -- Desperately looking for a better way
function ItemIndex(ItemName) {
// Finds First Index of the Name
for (i=0; i<character.items.length; i++) {
// Check out the Item
item = character.items[i]
// Does it fit?
if (item && item["name"] == ItemName) {
return i;
}
}
return null;
}
// Buy the specified Item
function BuyItem(ItemName) {
// Check out Item
item = FindItem(ItemName)
// Only buy if you don't have it yet
if (!item) {
game_log("Buying + '" + ItemName + "'")
// Buy only One
buy(ItemName, 1)
return;
}
return;
}
// Equip the Item if its better than SlotLevel
function EquipItem(ItemName, SlotLevel) {
// Check out the item
item = FindItem(ItemName)
// Does it exists?
if (!item) return;
// Is it better?
if (item.level > SlotLevel) {
game_log("Equipping " + item["name"])
equip(ItemIndex(ItemName))
return;
}
return;
}
/* Refill Functions */
// Name of the HP Potion
var HpName = "hpot0"
// Amount of Potions to buy
var HpAmount = 100
// When to buy Potion
var HpSafety = 100
// The Name of the Scroll to buy
var ScrollName = "scroll0"
// The Amount of Scrolls to buy
var ScrollAmount = 10
// When to buy Scroll
var ScrollSafety = 10
// Refill your Stock
function Refill() {
// Buy HP Potions
RefillHealthPotions()
// Buy Scrolls
RefillScrolls()
}
// Buy HP Potions according to Parameters
function RefillHealthPotions () {
potion = FindItem(HpName)
if (!potion || potion["q"] <= HpSafety) {
buy(HpName, HpAmount)
}
return;
}
// Buy Scrolls
function RefillScrolls() {
scroll = FindItem(ScrollName)
if (!scroll || scroll["q"] <= ScrollSafety) {
buy(ScrollName, ScrollAmount)
}
return
}
/* development Functions */
// Show a Json of the Spefied Paramter
function Show(item) {
if (item) {
show_json(item);
}
return;
}
/*
End of Functions
*/
/* Starter Function */
setInterval( Main, LOOP_SPEED );
/* Show it Once */
Show(SHOW_INFORMATION)