-
Notifications
You must be signed in to change notification settings - Fork 9
/
Walking.simba
502 lines (433 loc) · 9.6 KB
/
Walking.simba
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
{$include_once Internal/Reflection.simba}
{$include_once Constants.simba}
{$include_once Timing.simba}
{$include_once GameTab.simba}
{$include_once Login.simba}
{$include_once Menu.simba}
{*
R_TileToMM
~~~~~~~~~~
Converts the input tile to a point on the minimap.
Example:
Mouse.Move(R_TileToMM(R_GetMe.Tile));
*}
Function R_TileToMM(Tile: RSTile): TPoint;
begin
Result := Tile.ToLocal.TileToMM;
end;
{*
R_TileToMS
~~~~~~~~~~
Converts the input tile to a point on the main-screen.
Example:
Mouse.Move(R_TileToMS(R_GetMe.Tile));
*}
Function R_TileToMS(Tile: RSTile): TPoint;
begin
Result := Tile.ToLocal.TileToMS;
end;
{*
R_MMToMS
~~~~~~~~~~
Converts the input point to a mainscreen point.
Example:
Mouse.Move(R_MMToMS(R_TileToMM(R_GetMe.Tile)));
*}
Function R_MMToMS(P: TPoint): TPoint;
begin
Result := TPoint(RSTile(P).MMToTile.TileToMS);
end;
{*
R_MMToTile
~~~~~~~~~~
Converts the input point to a RSTile.
Example:
Mouse.Move(R_MMToTile(R_TileToMM(R_GetMe.Tile)));
*}
Function R_MMToTile(P: TPoint): RSTile;
begin
Result := RSTile(P).MMToTile;
end;
{*
R_TileOnMM
~~~~~~~~~~
Checks if the input tile is on the Mini-Map.
Example:
WriteLn(R_TileOnMM(Some_RSObject.Tile));
*}
Function R_TileOnMM(Tile: RSTile): Boolean;
var
W, H, MMCX, MMCY: Int32;
Temp: TPointArray;
begin
GetClientDimensions(W, H);
if RSClient.IsResizable then
begin
MMCX := (W - 82); //From RSTile.
MMCY := 84;
end else
begin
MMCX := (W - 122);
MMCY := 84;
end;
SetLength(Temp, 1);
Temp[0] := Tile.ToLocal.TileToMM;
FilterPointsPie(Temp, 0.0, 360.0, 0.0, 73.0 {75.0}, MMCX, MMCY);
Result := Length(Temp) > 0;
end;
{*
R_TileHeight
~~~~~~~~~~~~
Returns the height of a given tile.
Example:
if (R_TileHeight > 13) then
...
*}
Function R_TileHeight(Tile: RSTile): Int32;
begin
Result := Tile.GetHeight;
end;
{*
R_TileBounds
~~~~~~~~~~~~
Returns the mainscreen bounds of a given tile.
Example:
TileBounds := R_TileBounds(R_GetMe.Tile);
*}
Function R_TileBounds(Tile: RSTile): TRectangle;
var
Rect: T2DRectangle;
begin
Rect := Tile.Project;
Result.Top := Rect.NW;
Result.Right := Rect.NE;
Result.Btm := Rect.SE;
Result.Left := Rect.SW;
end;
{*
R_GetMidTile
~~~~~~~~~~~~
Returns the middle tile between the two tiles.
Example:
MidTile := R_GetMidTile([2543, 3254], [2513, 3267]);
*}
Function R_GetMidTile(Tile1, Tile2: RSTile): RSTile;
begin
Result := Point(Round((Tile1.X + Tile2.X)/2), Round((Tile1.Y+Tile2.Y)/2));
end;
Function R_WaitUntilDistanceFromFlag(Distance: Int32): Boolean;
var
Me: RSPlayer;
Dest: RSTile;
T: Timer;
begin
Me := RSPlayer.Me;
Dest := Me.Destination;
if (Dest.X = -1) or (Dest.Y = -1) then
Exit(False);
T.Start;
while (Me.Tile.DistanceTo(Dest) > Distance) do
begin
Wait(250, 500);
if T.ElapsedTime > 60000 then
begin
Me.Free;
Exit(False);
end;
end;
Me.Free;
Result := True;
end;
Function R_WalkToTileMM(Tile: RSTile): Boolean;
begin
Result := False;
if not R_TileOnMM(Tile) then
Exit;
Mouse.Click(R_TileToMM(Tile), MOUSE_LEFT);
Result := True;
end;
Function R_WalkToTileMS(Tile: RSTile): Boolean;
var
P: TPoint;
T: Timer;
begin
if not R_IsLoggedIn then
Exit;
P := R_TileToMS(Tile);
if (P.X = -1) or (P.Y = -1) then
Exit;
Mouse.Move(P, 2, True);
if R_WaitUpText(['Walk here'], True, 50) then
begin
Mouse.Click(MOUSE_LEFT);
end else
begin
Mouse.Click(MOUSE_RIGHT);
Wait(400, 500);
if not R_ChooseOptions(['Walk here'], True) then
begin
R_ChooseOptions(['Cancel'], True)
end;
end;
Wait(120, 180);
T.Start;
R_WaitUntilDistanceFromFlag(5);
Result := T.ElapsedTime < 60000;
end;
(*
R_WindPath
~~~~~~~~~~
.. code-block:: pascal
function R_WindPath(xs, ys, xe, ye, gravity, wind, minWait, maxWait, maxStep, targetArea: extended): TPointArray;
Generates a blind path of points!
.. note::
by JuKKa
*)
Function R_WindPath(xs, ys, xe, ye, gravity, wind, maxStep, targetArea: extended): TPointArray;
var
veloX, veloY, windX, windY, veloMag, dist, randomDist, lastDist, step: Extended;
lastX, lastY: Int32;
sqrt2, sqrt3, sqrt5: Extended;
begin
sqrt2:= sqrt(2);
sqrt3:= sqrt(3);
sqrt5:= sqrt(5);
while hypot(xs - xe, ys - ye) > 1 do
begin
dist:= hypot(xs - xe, ys - ye);
wind:= min(wind, dist);
if dist >= targetArea then
begin
windX:= windX / sqrt3 + (random(round(wind) * 2 + 1) - wind) / sqrt5;
windY:= windY / sqrt3 + (random(round(wind) * 2 + 1) - wind) / sqrt5;
end else
begin
windX:= windX / sqrt2;
windY:= windY / sqrt2;
if (maxStep < 3) then
begin
maxStep:= random(3) + 3.0;
end else
begin
maxStep:= maxStep / sqrt5;
end;
end;
veloX:= veloX + windX;
veloY:= veloY + windY;
veloX:= veloX + gravity * (xe - xs) / dist;
veloY:= veloY + gravity * (ye - ys) / dist;
if hypot(veloX, veloY) > maxStep then
begin
randomDist:= maxStep / 2.0 + random(0, round(maxStep) / 2);
veloMag:= sqrt(veloX * veloX + veloY * veloY);
veloX:= (veloX / veloMag) * randomDist;
veloY:= (veloY / veloMag) * randomDist;
end;
lastX:= Round(xs);
lastY:= Round(ys);
xs:= xs + veloX;
ys:= ys + veloY;
SetLength(Result, Length(Result) + 1);
Result[High(Result)] := Point(Round(xs), Round(ys));
step:= hypot(xs - lastX, ys - lastY);
lastdist:= dist;
end;
end;
Function R_BlindWalkMSEx(Tile: RSTile; Distance: Integer): Boolean;
var
I, Tries: Int32;
CurrentTile: RSTile;
CTRLPoints: TPointArray;
Me: RSPlayer;
begin
Tries := 0;
Me := RSPlayer.Me;
Repeat
CurrentTile := Me.Tile;
CtrlPoints := R_WindPath(CurrentTile.X, CurrentTile.Y, Tile.X, Tile.Y, 5.0, 2.5, 1.5, 1.0);
Inc(Tries);
if Length(CtrlPoints) = 0 then
begin
Me.Free;
Exit(True);
end;
if (Tries > 20)then
begin
Me.Free;
Exit;
end;
For I:= High(CtrlPoints) Downto 0 do
begin
if R_WalkToTileMS(CtrlPoints[I]) then
begin
Result := Me.Tile.DistanceTo(Tile) <= Distance;
Break;
end;
end;
Until(Result);
end;
Function R_BlindWalkMS(Tile: TPoint): Boolean;
begin
Result := R_BlindWalkMsEx(Tile, 2);
end;
Function R_WalkPathMMCommon(Path: Array of RSTile; Reverse: Boolean): Boolean;
type
TTileArray = Array of RSTile;
var
I, H, Fails, lP: Int32;
P, MM, MMF: TPoint;
T, K: Timer;
Timeout: Int32;
Me: RSPlayer;
begin
Me := RSPlayer.Me;
Timeout := 10000 + Random(2000);
T.Start;
Fails := 0;
lP := 0;
if Reverse then
begin
Path := TTileArray(TPointArray(Path).Reversed());
end;
H := High(Path);
While (not Result) and (T.ElapsedTime < Timeout) and (Fails < 5) do
begin
if (not R_IsLoggedIn) then
begin
Me.Free;
Exit;
end;
P := Me.Tile;
For I := H Downto 0 do
begin
if ((I - lP) > 3) then
Continue
else
lP := I;
MM := R_TileToMM(Path[I]);
if MM = MMF then
begin
Inc(Fails);
end;
if R_TileOnMM(Path[I]) then
begin
R_WalkToTileMM(Path[I]);
K.Start;
Repeat
if not Me.IsMoving then
begin
break;
end;
Until((K.ElapsedTime > 2000) or R_WaitUntilDistanceFromFlag(30));
MMF := MM;
if (I = H) then
begin
R_WaitUntilDistanceFromFlag(5);
end else
begin
R_WaitUntilDistanceFromFlag(25);
end;
T.Reset;
T.Start;
Timeout := 7000 + Random(1000);
Break;
end;
end;
Result := (I = H);
end;
Me.Free;
end;
Function R_WalkPathMMEx(Path: Array of RSTile; Reverse: Boolean): Boolean;
begin
if (not R_IsLoggedIn) then
begin
Exit;
end;
Result := R_WalkPathMMCommon(Path, Reverse);
R_WaitUntilDistanceFromFlag(5);
end;
Function R_WalkPathMM(Path: Array of RSTile): Boolean;
begin
Result := R_WalkPathMMEx(Path, False);
end;
Function R_WalkPathEx(Path: Array of RSTile; Reverse: Boolean; DistToMsWalk: Int32): Boolean;
type
TTileArray = Array of RSTile;
begin
if (not R_IsLoggedIn) then
begin
Exit;
end;
Result := R_WalkPathMMCommon(Path, Reverse);
if DistToMsWalk > 0 then
begin
if Reverse then
begin
Result := R_BlindWalkMS(Path[0]);
end else
begin
Result := R_BlindWalkMS(Path[High(Path)]);
end;
end
end;
Function R_WalkPath(Path: Array of RSTile): Boolean;
begin
Result := R_WalkPathEx(Path, False, 0);
end;
Function R_BlindWalkEx(Tile: TPoint; FinishMs: Boolean; DistToMsWalk: integer): Boolean;
type
TTileArray = Array of RSTile;
var
Tries, Fails: Integer;
M: TPoint;
CtrlPoints: Array of RSTile;
Me: RSPlayer;
begin
Me := RSPlayer.Me;
Repeat
Inc(Fails);
if Fails > 1 then
begin
Me.Free;
Exit;
end;
if not R_IsLoggedIn then
begin
Me.Free;
Exit;
end;
M := Me.Tile;
if M.X = - 1 then
Continue;
if Tile.X = - 1 then
Continue;
if Me.Tile.DistanceTo(Tile) <= 2.0 then
begin
Me.Free;
Exit;
end;
if Length(CtrlPoints) = 0 then
begin
//CtrlPoints := TPABetweenPoints(M, Tile, 5 + Random(2), 2);
CtrlPoints := TTileArray(R_WindPath(M.X, M.Y, Tile.X, Tile.Y, 5.0, 2.5, 1.5, 1.0));
end;
Inc(Tries);
if Tries > 20 then
begin
Me.Free;
Exit;
end;
if FinishMs then
begin
Result := R_WalkPathEx(CtrlPoints, False, DistToMsWalk);
end else
begin
R_WalkPath(CtrlPoints);
end;
Until(Result);
Me.Free;
end;
Function R_BlindWalk(Tile: RSTile): Boolean;
begin
Result := R_BlindWalkEx(Tile, False, 0);
end;