-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainloop.scar
567 lines (495 loc) · 21.2 KB
/
Mainloop.scar
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
{==============================================================================\
| MSI Group Scripting Include |
| Mainloop.scar |
|==============================================================================|
| For all the script Artificial Inteligence |
| |
| * function MSI_BestTool: Integer; * by Coh3n |
| * function MSI_WorstTool: Integer; * by Coh3n |
| * function MSI_SetupPlayer: Boolean; * by Coh3n |
| * function MSI_GetObjectList(): TIntegerArray; * by Coh3n |
| * function MSI_GetItemList(): TIntegerArray; * by Coh3n |
| * function MSI_GetExpList; TExtendedArray; * by Coh3n |
| * function MSI_WalkToLoc: Boolean; * by The MSI Team |
| * function MSI_WalkToBank: Boolean; * by The MSI Team |
| * function MSI_DoSkill: Boolean; * by The MSI Team |
| * procedure MSI_CountItems: Boolean; * by Coh3n |
| * function MSI_BankMaterials: Boolean; * by The MSI Team |
| * procedure MSI_HandleBreaking; * by Coh3n |
| * function MSI_TimedOut: Boolean; * by Coh3n |
| * procedure MSI_UnsetPlayer(); * by RM and Coh3n |
| * function MSI_MainLoop: Boolean; * by Coh3n |
\_____________________________________________________________________________}
{******************************************************************************}
{ function MSI_BestTool: Integer; }
{ By: Coh3n }
{ Date: 10 June 2010 }
{ Description: Determines the best tool for the job at hand. }
{******************************************************************************}
function MSI_BestTool: Integer;
var
currSkill, currLevel: Integer;
begin
if (not LoggedIn) then
Exit;
{ Get the current level of the chosen skill, and then look for the required
tool, starting with the best one for that player's skill level. }
currSkill := PlayerSkills[CurrentPlayer][CurrentSkill];
currLevel := GetSkillLevel(CurrSkill);
MSI_Debug('MSI_BestTool', 'CurrentSkill level: ' + IntToStr(currLevel), DEBUG_FUNCTIONS);
case PlayerSkills[CurrentPlayer][CurrentSkill] of
SKILL_WOODCUTTING:
case (currLevel) of
REQ_RUNE..REQ_MAX_LEVEL: Result := HATCHET_RUNE;
REQ_ADAMANT..REQ_RUNE: Result := HATCHET_ADAMANT;
REQ_MITHRIL..REQ_ADAMANT: Result := HATCHET_MITHRIL;
REQ_STEEL..REQ_MITHRIL: Result := HATCHET_STEEL;
else
Result := HATCHET_IRON;
end;
end;
end;
{******************************************************************************}
{ function MSI_WorstTool: Integer; }
{ By: Coh3n }
{ Date: 10 June 2010 }
{ Description: Determines the worst tool for the job at hand. }
{******************************************************************************}
function MSI_WorstTool: Integer;
begin
if (not LoggedIn) then
Exit;
case (PlayerSkills[CurrentPlayer][CurrentSkill]) of
SKILL_WOODCUTTING:
Result := HATCHET_BRONZE;
end;
end;
{******************************************************************************}
{ function MSI_SetupPlayer: Boolean; }
{ By: Coh3n }
{ Date: 10 June 2010 }
{ Description: Sets up the player for autoing. }
{******************************************************************************}
function MSI_SetupPlayer: Boolean;
var
bestTool, worstTool, x, y, i, j , k: Integer;
bankCheck: Boolean;
gameTabs: TIntegerArray;
begin
if (not LoggedIn) then
LoginPlayer;
Inc(Stats_CustomVars[STATS_LOGINS]);
MSI_SendStats;
SetAngle(True);
SetRun(True);
{ Starts by looking for the best tool for that player's skill level, and
moves down from there. }
bestTool := MSI_BestTool;
worstTool := MSI_WorstTool;
gameTabs := [tab_Equip, tab_Inv];
for j := 0 to High(gameTabs) do
for i := bestTool downto worstTool do
begin
Result := MSI_FindTool(i, gameTabs[j], x, y);
if (Result) then
Exit;
end;
{ If the required tool isn't found, then will open the bank and attempt to
withdraw the proper tool. }
if (not Result) then
begin
if (not Players[CurrentPlayer].Booleans[P_BANK]) then
begin
MSI_Debug('MSI_SetupPlayer', 'Failed to find required tool', DEBUG_FUNCTIONS);
Exit;
end;
case (Players[CurrentPlayer].Integers[P_LOC]) of
LOC_VE_BANK:
bankCheck := MSI_OpenBank(OBJ_BANK_VE, WALK_WC_VE_BANK_4, Players[CurrentPlayer].Pin);
else
MSI_Debug('MSI_SetupPlayer', 'Invalid Player.Integers[P_LOC]', DEBUG_FUNCTIONS);
end;
if bankCheck then
for k := bestTool downto 0 do
begin
Result := MSI_Withdraw([k], [1]);
if (Result) then
Break
else begin
CloseBank;
MSI_Debug('MSI_PlayerSetup', 'Couldn''t Withdraw Tool.', DEBUG_FUNCTIONS);
end;
end
else
MSI_Debug('MSI_SetupPlayer', 'Unable to Open Bank.', DEBUG_FUNCTIONS);
end;
end;
{******************************************************************************}
{ function MSI_GetObjectList: TIntegerArray; }
{ By: Coh3n }
{ Date: 10 June 2010 }
{ Description: Converts the variant array to an integer array }
{******************************************************************************}
function MSI_GetObjectList(whichPlayer, whichSkill: Integer): TIntegerArray;
var
i: Integer;
tempStr: String;
objects: TVariantArray;
begin
objects := PlayerObjects[whichPlayer][whichSkill];
SetLength(Result, Length(objects));
try
for i := 0 to High(objects) do
begin
tempStr := IntToStr(objects[i]);
Result[i] := StrToInt(tempStr);
end;
except
MSI_Debug('MSI_GetObjectList', 'Invalid object type in DeclarePlayers!', DEBUG_FUNCTIONS);
Exit;
end;
end;
{******************************************************************************}
{ function MSI_GetItemList: TIntegerArray; }
{ By: Coh3n }
{ Date: 10 June 2010 }
{ Description: Gets the materials required depending on the object autoed }
{******************************************************************************}
function MSI_GetItemList(whichPlayer, whichSkill: Integer): TIntegerArray;
var
i: Integer;
objects: TIntegerArray;
begin
objects := MSI_GetObjectList(whichPlayer, whichSkill);
SetLength(Result, Length(objects));
for i := 0 to High(objects) do
case objects[i] of { Result[I] := Objects[I] + 13}
OBJ_TREE_NORMAL: Result[i] := LOGS_NORMAL;
OBJ_TREE_OAK: Result[i] := LOGS_OAK;
OBJ_TREE_WILLOW: Result[i] := LOGS_WILLOW;
OBJ_TREE_MAPLE: Result[i] := LOGS_MAPLE;
OBJ_TREE_YEW: Result[i] := LOGS_YEW;
else
MSI_Debug('MSI_GetItemList', 'Invalid object in DeclarePlayers!', DEBUG_FUNCTIONS);
end;
end;
{******************************************************************************}
{ function MSI_GetExpList: TIntegerArray; }
{ By: Coh3n }
{ Date: 10 June 2010 }
{ Description: Gets the experience gained by obtaining items in the item list }
{******************************************************************************}
function MSI_GetExpList: TExtendedArray;
var
i: Integer;
objects: TIntegerArray;
msObjs: TMSObjectArray;
begin
objects := MSI_GetObjectList(CurrentPlayer, CurrentSkill);
SetLength(Result, Length(objects));
msObjs := MSI_GetObjects(objects);
for i := 0 to High(objects) do
Result[i] := msObjs[i].Exp;
end;
{******************************************************************************}
{ function MSI_WalkToLoc: Boolean; }
{ By: The MSI Team }
{ Date: 10 June 2010 }
{ Description: Walks to the proper location depending on skill/location }
{******************************************************************************}
function MSI_WalkToLoc: Boolean;
begin
if (not LoggedIn) then
Exit;
case PlayerSkills[CurrentPlayer][CurrentSkill] of
SKILL_WOODCUTTING:
case Players[CurrentPlayer].Integers[P_LOC] of
LOC_VE_BANK:
Result := MSI_PerformWalk(LOC_VE_BANK, LOC_VE_TREES);
LOC_VE_TREES:
Result := True;
else
MSI_Debug('MSI_WalkToLoc', 'Invalid location for woodcutting skill', DEBUG_FUNCTIONS);
end;
else
MSI_Debug('MSI_WalkToLoc', 'Invalid player skill!', DEBUG_FUNCTIONS);
end;
end;
{******************************************************************************}
{ function MSI_WalkToBank: Boolean; }
{ By: The MSI Team }
{ Date: 10 June 2010 }
{ Description: Walks to the bank depending on skill/location }
{******************************************************************************}
function MSI_WalkToBank: Boolean;
begin
if (not InvFull) or (not LoggedIn) then
Exit;
case PlayerSkills[CurrentPlayer][CurrentSkill] of
SKILL_WOODCUTTING:
case Players[CurrentPlayer].Integers[P_LOC] of
LOC_VE_TREES:
Result := MSI_PerformWalk(LOC_VE_TREES, LOC_VE_BANK);
LOC_VE_BANK:
Result := True;
else
MSI_Debug('MSI_WalkToBank', 'Invalid location for woodcutting skill', DEBUG_FUNCTIONS);
end;
else
MSI_Debug('MSI_WalkToBank', 'Invalid player skill!', DEBUG_FUNCTIONS);
end;
end;
{******************************************************************************}
{ function MSI_DoSkill: Boolean; }
{ By: The MSI Team }
{ Date: 10 June 2010 }
{ Description: Performs the specified skill }
{******************************************************************************}
function MSI_DoSkill: Boolean;
var
objectTypes, itemTypes: TIntegerArray;
begin
if (not LoggedIn) then
Exit;
objectTypes := MSI_GetObjectList(CurrentPlayer, CurrentSkill);
itemTypes := MSI_GetItemList(CurrentPlayer, CurrentSkill);
case PlayerSkills[CurrentPlayer][CurrentSkill] of
SKILL_WOODCUTTING:
if (Players[CurrentPlayer].Booleans[P_BANK]) then
Result := MSI_ChopInvFull(objectTypes)
else
Result := MSI_PowerChop(objectTypes, itemTypes);
else
MSI_Debug('MSI_WalkToBank', 'Invalid player skill!', DEBUG_FUNCTIONS);
end;
end;
{******************************************************************************}
{ procedure MSI_CountItems; }
{ By: Coh3n }
{ Date: 10 June 2010 }
{ Description: Counts the materials and calculates the experience gained }
{ from items in the inventory }
{******************************************************************************}
procedure MSI_CountItems;
var
itemCount, expCount, i: Integer;
items: TIntegerArray;
exps: TExtendedArray;
begin
if (not LoggedIn) then
Exit;
items := MSI_GetItemList(CurrentPlayer, CurrentSkill);
exps := MSI_GetExpList;
for i := 0 to High(items) do
begin
MSI_GetItem(items[i]);
itemCount := CountItems('dtm', MSI_Item[items[i]].DTM, []);
expCount := Round(exps[i] * itemCount);
MSI_AddToProggy(i, itemCount, ADD_ITEMS); //Adds amount of item
MSI_AddToProggy(-1, expCount, ADD_EXP); //Adds exp to skill
MSI_Debug('MSI_CountItems', 'Found ' + IntToStr(itemCount) + ' of item '
+ MSI_Item[items[i]].Name, DEBUG_FUNCTIONS);
end;
end;
{******************************************************************************}
{ function MSI_BankMaterials: Boolean; }
{ By: The MSI Team }
{ Date: 10 June 2010 }
{ Description: Banks the specified materials }
{******************************************************************************}
function MSI_BankMaterials: Boolean;
begin
if (not LoggedIn) then
Exit;
case PlayerSkills[CurrentPlayer][CurrentSkill] of
SKILL_WOODCUTTING:
case Players[CurrentPlayer].Integers[P_LOC] of
LOC_VE_BANK:
begin
MSI_CountItems;
if (not MSI_OpenBank(OBJ_BANK_VE, WALK_WC_VE_BANK_4, Players[CurrentPlayer].Pin)) then
begin
ProgressReportArray[CurrentPlayer].FalseReason := 'Failed to open bank: OBJ_BANK_VE';
Exit;
end;
Result := MSI_Deposit(MSI_GetItemList(CurrentPlayer, CurrentSkill), []);
if (not Result) then
ProgressReportArray[CurrentPlayer].FalseReason := 'Failed to deposit items'
else begin
MSI_AddToProggy(-1, -1, ADD_LOADS);
Inc(Stats_CustomVars[STATS_BANKS]);
MSI_SendStats;
end;
end;
else
MSI_Debug('MSI_BankMaterials', 'Invalid location for woodcutting skill.', DEBUG_FUNCTIONS);
end;
else
MSI_Debug('MSI_BankMaterials', 'Invalid player skill!', DEBUG_FUNCTIONS);
end;
end;
{******************************************************************************}
{ procedure MSI_HandleBreaking; }
{ By: Coh3n }
{ Date: 10 June 2010 }
{ Description: Simple procedure for breaking }
{******************************************************************************}
procedure MSI_HandleBreaking;
begin
if (not LoggedIn) then
Exit;
if (BankScreen) then
CloseBank;
if (HowManyPlayers = 1) then
begin
MSI_Debug('MSI_HandleBreaking', 'Taking a short break', DEBUG_PLAYER_ACTIONS);
ExitToLobby;
Wait((BreakTime * 60000) + RandomRange(-300000, 300000));
MSI_Debug('MSI_HandleBreaking', 'Break over, continuing to play', DEBUG_PLAYER_ACTIONS);
if (LobbyScreen) then
MouseBox(80, 450, 560, 470, 1);
end else
begin
MSI_Debug('MSI_HandleBreaking', 'Switching players', DEBUG_PLAYER_ACTIONS);
NextPlayer(True);
end;
Inc(Stats_CustomVars[STATS_LOGINS]);
MSI_SendStats;
end;
{******************************************************************************}
{ function MSI_TimedOut: Boolean; }
{ By: Coh3n }
{ Date: 10 June 2010 }
{ Description: Handles connection losses }
{******************************************************************************}
function MSI_TimedOut: Boolean;
var
t: Integer;
begin
Result := False;
t := GetSystemTime + 10000;
while (GetSystemTime < t) do
if (LobbyScreen) or (not LoggedIn) then
begin
MSI_Debug('MSI_TimedOut', 'Suspicion of connection loss', DEBUG_FUNCTIONS);
if (LobbyScreen) then
MouseBox(80, 450, 560, 470, 1)
else
LoginPlayer;
Result := True;
Break;
end;
end;
{******************************************************************************}
{ procedure MSI_UnsetPlayer(func, reason: String; setReason: Boolean); }
{ By: RM and Coh3n }
{ Date: 10 June 2010 }
{ Description: Sets the player to false if something fails }
{******************************************************************************}
procedure MSI_UnsetPlayer(func, reason: String; setReason: Boolean);
begin
MSI_Debug(func, reason, DEBUG_PLAYER_ACTIONS);
Players[CurrentPlayer].Active := False;
if (setReason) then
ProgressReportArray[CurrentPlayer].FalseReason := reason;
MSI_ProgressReport;
if (not AllPlayersInactive) then //To avoid infinite loop in NextPlayer
begin
NextPlayer(False);
Inc(Stats_CustomVars[STATS_LOGINS]);
MSI_SendStats;
end;
end;
{******************************************************************************}
{ procedure MSI_MainLoop; }
{ By: Coh3n }
{ Date: 10 June 2010 }
{ Description: The MainLoop - performs all actions of the script }
{******************************************************************************}
procedure MSI_MainLoop;
var
breakAfterLoads, time: Integer;
begin
scriptStart := TheDate(DATE_DAY) + ' at ' + TheTime;
Stats_ScriptID := '47';
Stats_UserID := statsID;
Stats_Password := statsPass;
//To gain access
if (saveDebug) then
begin
MSI_Debug('MSI_MainLoop', 'Created file', DEBUG_FUNCTIONS);
MSI_ProgressReport;
end;
repeat
//Setup player
if (not MSI_SetupPlayer) then
begin
if (MSI_TimedOut) then
Continue;
MSI_UnsetPlayer('MSI_MainLoop', 'Failed to setup player', True);
Continue;
end;
repeat
MarkTime(time);
if (Players[CurrentPlayer].Booleans[P_BANK]) then
//Walk to location if not power skilling
if (not MSI_WalkToLoc) then
begin
if (MSI_TimedOut) then
Break;
MSI_UnsetPlayer('MSI_MainLoop', 'Failed to walk to location', False);
Break;
end;
//Perform the skill
if (not MSI_DoSkill) then
begin
if (MSI_TimedOut) then
Break;
MSI_UnsetPlayer('MSI_MainLoop', 'Failed to perform skill: ' + IntToStr(CurrentSkill), True);
Break;
end;
if (Players[CurrentPlayer].Booleans[P_BANK]) then
begin
//Walk to the bank
if (not MSI_WalkToBank) then
begin
if (MSI_TimedOut) then
Break;
MSI_UnsetPlayer('MSI_MainLoop', 'Failed to walk to bank', False);
Break;
end;
//Bank the materials
if (not MSI_BankMaterials) then
begin
if (MSI_TimedOut) then
Break;
MSI_UnsetPlayer('MSI_MainLoop', 'Failed to bank materials', False);
Break;
end;
end;
//Set the progress report times
IncEx(ProgressReportArray[CurrentPlayer].PlayerTime, TimeFromMark(time));
IncEx(ProgressReportArray[CurrentPlayer].SkillInfo[CurrentSkill].TotalTime, TimeFromMark(time));
MSI_ProgressReport;
Inc(breakAfterLoads);
if (breakAfterLoads >= (BreakAfter + RandomRange(-2, 2))) then
begin
MSI_HandleBreaking;
breakAfterLoads := 0;
end;
until(not Players[CurrentPlayer].Active);
until(AllPlayersInactive);
if (AllPlayersInactive) then
begin
MSI_Debug('MSI_MainLoop', 'All players inactive, terminating script', DEBUG_PLAYER_ACTIONS);
Logout; //to be sure the last player is logged out
MSI_ProgressReport;
FreeSRL;
TerminateScript;
end;
end;
procedure ScriptTerminate;
begin
Writeln('Thank-you for using MSI Phoenix scripts!');
MSI_FreeItems;
end;