-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathWalker.simba
659 lines (554 loc) · 17.4 KB
/
Walker.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
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
{==============================================================================]
Author: Jarl K. Holta
Project: RSWalker
Project URL: https://github.com/WarPie/RSWalker
License: GNU GPL (http://www.gnu.org/licenses/gpl.html)
[==============================================================================}
{$IFDEF HINTS}{$DEFINE !HAD_HINTS}{$H-}{$ENDIF}
{$IFDEF WIN32}
{$loadlib ../Includes/RSWalker/libWalker.dll}
{$ENDIF}
{$IFNDECL MatchTemplate}
{$loadlib ../Includes/RSWalker/matchTempl}
{$ENDIF}
{$IFDECL TMEMSCAN}
{$include_once MemScan.pas}
{$ENDIF}
{$include_once Utilities.pas}
{$include_once Base.pas}
type
PRSWalker = ^TRSWalker;
TOnWalkEvent = procedure(Sender: PRSWalker; MyPos: TPoint);
TOnMoveEvent = procedure(Sender: PRSWalker);
TRSWalker = record
//internals
Finder: TRSPosFinder;
WorldMap, WorldSample: T2DIntArray;
LocalMap: T2DIntArray;
FGlobal: TPoint; // memscan related variable
FBaseOffset: TPoint; // start of map slices
FPath: TPointArray; // current path, set by calling WalkPath
FPathIdx: Int32; // current index in current path.
//commonly adjusted variables
MinimapShift: Int32; // the minimum pixelshift for isMoving-tests
SkipClose: Int32; // how near the flag before it can click next point
MinRunEnergy: Int32; // minimum run energy before it can toggle run
KeepMouseActive: Boolean; // unused
ScreenWalk: Boolean; // walk using mainscreen rather than minimap
MemScanEnabled: Boolean; // disable MemScan even tho it may be loaded and all
Graph: TWebGraph; // wab walking graph, you can load your own graph.
//hooks
onWalkEvent: TOnWalkEvent;
onMoveEvent: TOnMoveEvent;
//core switch, can be toggled off to stop walking mid walk [toggles on automatically by walking with RSW]
Enabled: Boolean;
end;
var
RSW_SEARCH_PATHS: TStringArray := [
'',
IncludePath,
IncludePath+'maps/',
IncludePath+'RSWalker/maps/',
IncludePath+'RSWalker/maps/slices/'
];
{implementation}
procedure TRSWalker.SetDefaultValues();
begin
onWalkEvent := nil;
onMoveEvent := nil;
SkipClose := 30;
MinRunEnergy := 20;
Graph := RSW_Graph;
MinimapShift := 220;
MemScanEnabled := {$IFDECL TMEMSCAN}True{$ELSE}False{$ENDIF};
Enabled := True;
end;
function TRSWalker.TryGetClientID(): UInt32; static;
begin
Result := 0;
{$IFDECL TMEMSCAN}
{$IFDEF SMART}
Result := SMART.PID;
{$ELSE}
Result := GetWindowProcessID( Client.GetIOManager().GetKeyMouseTarget().GetHandle() );
{$ENDIF}
{$ENDIF}
end;
function TRSWalker.FindMapPath(path:String): String;
var
testPath:String;
begin
for testPath in RSW_SEARCH_PATHS do
if FileExists(testPath+path) then
Exit(testPath);
RaiseException(erException, 'TRSWalker.Init -> Map `'+result+'` not found');
end;
procedure TRSWalker.Init(World: String; PID:Int32=0; AScanRatio:Int32=6);
var
BMP: PtrInt;
begin
with self do
begin
if (PID = 0) then PID := self.TryGetClientID();
Finder.Init(PID);
Finder.ScanRatio := AScanRatio;
Finder.TMFormula := TM_CCOEFF_NORMED;
{$IFDECL ClearFFTCache}ClearFFTCache;{$ENDIF}
self.SetDefaultValues();
if world <> '' then
begin
BMP := LoadBitmap(FindMapPath(world)+world);
WorldMap := BitmapToMatrix(BMP);
FreeBitmap(BMP);
WorldSample := WorldMap.DownscaleImage(Finder.ScanRatio);
end else
self.Enabled := False;
end;
end;
procedure TRSWalker.InitEx(Name: String; Slices: TPointArray; PID:Int32=0; AScanRatio:Int32=6);
var
BMP: TMufasaBitmap;
Folder: String;
begin
with self do
begin
if (PID = 0) then PID := self.TryGetClientID();
Finder.Init(PID);
Finder.ScanRatio := AScanRatio;
Finder.TMFormula := TM_CCOEFF_NORMED;
{$IFDECL ClearFFTCache}ClearFFTCache;{$ENDIF};
Self.SetDefaultValues();
Folder := FindMapPath(Format(SLICE_FMT, ['', Name, 0,0]));
BMP := RSWUtils.AssembleSlices(Folder, Name, Slices, Self.FBaseOffset);
WorldMap := BMP.ToMatrix();
BMP.Free();
WorldSample := WorldMap.DownscaleImage(Finder.ScanRatio);
Self.Graph.BlockOutside(Slices);
end;
end;
procedure TRSWalker.Free();
begin
Finder.Free();
FBaseOffset := [0,0];
SetLength(WorldMap, 0);
SetLength(WorldSample, 0);
SetLength(LocalMap, 0);
end;
function TRSWalker.SetFormula(Formula: ETMFormula): TPoint;
begin
Self.Finder.TMFormula := Formula;
end;
{---| IF MEMSCAN |---]
Changes the state of MemScan (use, or not use)
Returns True if it's enabled.
}
function TRSWalker.ToggleMemScan(): Boolean;
begin
memScanEnabled := not memScanEnabled;
Result := memScanEnabled and (Finder.Process > 0);
end;
{---| IF MEMSCAN |---]
Locates the current minimap in the world map
}
function TRSWalker.FindChunk(pt:TPoint; area:Int32=100): TPoint;
var
loc,locSample: T2DIntArray;
B: TBox;
shift: TPoint;
begin
B := [pt.x-area, pt.y-area, pt.x+area, pt.y+area];
B := [max(52,B.x1),max(52,B.y1),min(459,B.x2),min(459,B.y2)];
loc := self.LocalMap.Crop(b);
locSample := loc.DownscaleImage(Finder.ScanRatio);
shift := Point(pt.x-B.x1, pt.y-B.y1);
Result := MatchTemplate(WorldSample, locSample, Finder.TMFormula).ArgMax();
Result := finder.FindImageNear(Result * Finder.ScanRatio, WorldMap, loc, 20);
Result.x := (Result.x + shift.x) - pt.x;
Result.y := (Result.y + shift.y) - pt.y;
end;
{---| IF MEMSCAN |---]
Updates the position of the current minimap-chuck
This function is only executed when the minimap loads a new chunk
}
procedure TRSWalker.UpdateGlobalStart(localPt:TPoint);
var sim:Double;
begin
if Length(self.LocalMap) = Length(Finder.LocalMap) then
sim := Finder.LocalMap.CompareImageAt(self.LocalMap, [0,0], 1);
if sim < 1 then
begin
self.LocalMap := Finder.LocalMap;
self.FGlobal := self.FindChunk(localPt);
end;
end;
{---| IF MEMSCAN |---]
Returns the current position of our character when using memscan
- For normal usage use TRSWalker.GetMyPos
}
function TRSWalker._GetPos(): TPoint;
var
localPt:TPoint;
begin
localPt := Finder.GetLocalPos();
self.UpdateGlobalStart(localPt);
Result.x := localPt.x + self.FGlobal.x;
Result.y := localPt.y + self.FGlobal.y;
end;
{
Returns the current position of our character when NOT using memscan
- For normal usage use TRSWalker.GetMyPos
}
function TRSWalker._GetPosCustom(): TPoint;
var
mmsample, minimap: T2DIntArray;
xcorr: T2DRealArray;
best: TPoint;
begin
minimap := RSWUtils.GetMinimap(True, False);
mmsample:= minimap.DownscaleImage(Finder.ScanRatio);
xcorr := MatchTemplate(WorldSample, mmsample, Finder.TMFormula);
best := xcorr.ArgMax();
Result := Finder.FindImageNear(best * Finder.ScanRatio, WorldMap, Minimap, 20);
Result.X += Length(minimap[0]) div 2;
Result.Y += Length(minimap ) div 2;
Finder.Similarity := xcorr[best.Y, best.X];
end;
(*
Returns our characters current position.
@note: If `minSimiliarty` is set it will return (-1,-1) when it's less similar than what's expected.
*)
function TRSWalker.GetMyPos(minSimiliarty: Double = -$FFFFFFF): TPoint;
begin
if Length(self.WorldMap) = 0 then
RaiseException('TRSWalker.GetMyPos -> RSWalker is not set up for this task!');
if (Finder.Process > 0) and (self.MemScanEnabled) then
Result := self._GetPos()
else
Result := self._GetPosCustom();
Result += Self.FBaseOffset;
if (self.Finder.Similarity < minSimiliarty) then
Exit(Point(-1,-1));
end;
(*
Returns the tile position on the mainscreen as a rectangle
*)
function TRSWalker.GetTileMSEx(Me, Loc: TPoint; Height:Double=0; Offx,Offy:Double=0): TRectangle;
var
angle: Double;
begin
Loc := Point(MM2MS.MMCX, MM2MS.MMCY) + (Loc - Me);
angle := Minimap.GetCompassAngle(False);
Loc := Loc.Rotate(angle, Point(MM2MS.MMCX, MM2MS.MMCY));
Result := Minimap.VecToMSRect(Vec3(Loc.x - offx, Loc.y - offy, Height), angle);
end;
(*
Same as the above, only you dont have to pass "MyPos" to it.
*)
function TRSWalker.GetTileMS(Loc: TPoint; Height:Double=0; Offx,Offy:Double=0): TRectangle;
begin
Result := GetTileMSEx(Self.GetMyPos(), Loc, Height, Offx, Offy);
end;
(*
Takes the position we are walking to in both it's forms:
- mmPt (click position on minimap)
- worldPt (goal position in the world map)
Two hooks are called here:
- onMoveEvent(self)
*)
procedure TRSWalker.WalkToPos(mmPos:TPoint; Rnd:Int32=0; doRand:Boolean=False);
var
i,st: Int64;
t: TCountDown;
flag: TPoint;
B: TBox;
skipDist: Double;
begin
mmPos := RotatePoint(mmPos, Minimap.GetCompassAngle(False), Minimap.Center.X, Minimap.Center.Y);
skipDist := srl.SkewedRand(skipClose, 10, MM_RAD);
if (doRand) then
while (Rnd > 1) and (Hypot(mmPos.x-Minimap.Center.X, mmPos.y-Minimap.Center.Y) >= MM_RAD - Rnd*Sqrt(2)) do
Dec(Rnd);
if not Self.ScreenWalk then
mouse.Click(mmPos, rnd, mouse_left)
else begin
B := Minimap.PointToMsBox(mmPos);
B.LimitTo(Mainscreen.GetBounds); //fucking self-alterting procedures (I did not make these!)
mouse.Click(B, mouse_left);
end;
Wait(300,500);
t.Init(25000);
repeat
if (i mod 2 = 0) and (Minimap.GetRunEnergy >= self.MinRunEnergy) then
Minimap.ToggleRun(True);
if Minimap.FindFlag(flag) then
begin
if (flag.DistanceTo(Minimap.Center) < skipDist) then
Break;
end else if (Self.GetMyPos().DistanceTo(FPath[FPathIdx]) < skipDist) then
Break;
st := GetTickCount() + Random(60);
if Assigned(@self.onMoveEvent) then
self.onMoveEvent(@self);
Wait( Max(1, st-GetTickCount()) );
until (not self.Enabled) or (not Minimap.IsPlayerMoving(Self.MinimapShift)) or (t.IsFinished());
end;
(*
Simple method to do 1 step on the minimap.
*)
function TRSWalker.WalkStep(WorldPos:TPoint; Rnd:Int32=0; WaitFlag:Boolean=True): Boolean; overload;
var
mmPos, myPos: TPoint;
B: TBox;
t: TCountDown;
begin
myPos := self.GetMyPos();
mmPos.x := (WorldPos.x - myPos.x) + minimap.Center.X;
mmPos.y := (WorldPos.y - myPos.y) + minimap.Center.Y;
if (Hypot(myPos.x-WorldPos.x, myPos.y-WorldPos.y) > MM_RAD) then
Exit(False);
mmPos := RotatePoint(mmPos, Minimap.GetCompassAngle(False), Minimap.Center.X, Minimap.Center.Y);
if not Self.ScreenWalk then
mouse.Click(mmPos, rnd, mouse_left)
else begin
B := Minimap.PointToMsBox(mmPos);
B.LimitTo(Mainscreen.GetBounds); //fucking self-alterting procedures (I did not make these!)
mouse.Click(B, mouse_left);
end;
if WaitFlag then
begin
t.Init(25000);
repeat
if Minimap.GetRunEnergy >= self.minRunEnergy then
Minimap.ToggleRun(True);
Wait(Random(75));
until (not Minimap.isFlagPresent()) or t.IsFinished();
end;
end;
(*
Waits until your player is finished moving, while calling move event.
*)
procedure TRSWalker.WaitPlayerMoving(TimeOut: Int32 = 30000);
var
T: UInt64;
begin
T := GetTickCount() + TimeOut;
while (T > GetTickCount()) and (Self.Enabled) do
begin
if Assigned(@Self.OnMoveEvent) then
Self.OnMoveEvent(@Self);
if (not Minimap.isFlagPresent()) and (not Minimap.isPlayerMoving(Self.MinimapShift)) then
Break;
Wait(Random(50, 100));
end;
end;
(*
Walks a path.. returns true if we successfully walked the path, otherwise false.
One hook is used here:
- onWalkEvent(self, myPos);
*)
function TRSWalker.WalkPath(Path: TPointArray; ClickRnd:Int32=4; FinalCorrection:Boolean=False): Boolean;
var
i,j,h,prev,tmp,attempts: Int32;
nearest,distance: Double;
mmPos, myPos: TPoint;
t: TCountDown;
function RandomizeRunEnergy(e:Int32):Int32;
begin
if e > 100 then Exit();
Result := Random(max(5,e-10), e+10);
end;
begin
if Length(worldMap) = 0 then
RaiseException('TRSWalker.WalkPath -> RSWalker is not set up for this task!');
self.Enabled := True;
tmp := self.MinRunEnergy;
// get start point in path
myPos := Self.GetMyPos();
nearest := 100000;
for j:=0 to High(path) do
begin
distance := Hypot(path[j].x-myPos.x, path[j].y-myPos.y);
if (distance < nearest) then
begin
nearest := distance;
i := j;
end;
end;
// generate a dense path from starting point
FPath := Copy(path, i, Length(path)-i);
FPath := RSWUtils.BuildPath([myPos]+FPath, 1,6);
// do the walk
t.Init(Random(23000, 27000));
FPathIdx := 0;
H := High(FPath);
while (FPathIdx < H) and (not t.IsFinished()) do
begin
self.minRunEnergy := RandomizeRunEnergy(self.minRunEnergy);
myPos := Self.GetMyPos();
if not Self.ScreenWalk then
begin
while (FPathIdx < H) and (Hypot(myPos.x-FPath[FPathIdx+1].x, myPos.y-FPath[FPathIdx+1].y) < MM_RAD) do
Inc(FPathIdx);
end else
begin
while (FPathIdx < H) and Mainscreen.FBounds.Contains(Minimap.PointToMs(FPath[FPathIdx+1] - myPos + minimap.Center)) do
Inc(FPathIdx);
end;
if prev = FPathIdx then
begin
Inc(attempts);
if attempts > 7 then
begin
self.MinRunEnergy := tmp;
Exit(False);
end;
end else
attempts := 0;
mmPos := FPath[FPathIdx] - myPos + minimap.Center;
if Assigned(@self.onWalkEvent) then
begin
self.onWalkEvent(@self, myPos);
if not(self.Enabled) then
begin
self.MinRunEnergy := tmp;
Exit(True);
end;
end;
self.WalkToPos(mmPos, ClickRnd, FPathIdx<>h);
if not(self.Enabled) then
begin
self.MinRunEnergy := tmp;
Exit(True);
end;
prev := FPathIdx;
t.Init(Random(20000, 25000));
end;
self.WaitPlayerMoving();
myPos := Self.GetMyPos();
Result := Hypot(myPos.x-FPath[h].x, myPos.y-FPath[h].y) < 16;
if Result and FinalCorrection then
begin
distance := Hypot(myPos.x-FPath[h].x, myPos.y-FPath[h].y);
if (distance < MM_RAD) and (distance > 6) then
begin
mmPos.x := (FPath[h].x - myPos.x) + Minimap.Center.X;
mmPos.y := (FPath[h].y - myPos.y) + Minimap.Center.Y;
self.WalkToPos(mmPos, 1, True);
self.WaitPlayerMoving();
end;
end;
self.minRunEnergy := tmp;
end;
(*
Walks the path, if it fails it retries `Retries` times. Returns true if we
successfully walked the path, otherwise false
*)
function TRSWalker.WalkPath2(Path: TPointArray; Retries: Int32=3): Boolean;
begin
for 1 to Retries do
if Self.WalkPath(Path) then
Exit(True)
else
Wait(600,900);
end;
(*
Uses the currenly loaded graph (by default a graph that matches world.png) to walk to desitnation
*)
function TRSWalker.WebWalk(Destination: TPoint; ClickRnd:Int32=4; PathRnd:Double=0; FinalCorrection:Boolean=False): Boolean;
var
path: TPointArray;
begin
if (PathRnd > 1) or (PathRnd < 0) then
RaiseException(erException, 'TRSWalker.WebWalk -> PathRnd should be in the range `0..1` where `1` is max');
path := Self.Graph.PathBetween(Self.GetMyPos, Destination, PathRnd);
Result := Self.WalkPath(path, ClickRnd, FinalCorrection);
end;
function TRSWalker.WebWalk(Destination: String; ClickRnd:Int32=4; PathRnd:Double=0; FinalCorrection:Boolean=False): Boolean; overload;
var
dstNode: Int32;
begin
dstNode := Self.Graph.FindNode(Destination);
if dstNode = -1 then
RaiseException(erException, 'TRSWalker.WebWalk -> No node named `'+Destination+'` exists');
Result := Self.WebWalk(Self.Graph.Nodes[dstNode], ClickRnd, PathRnd, FinalCorrection);
end;
(*
Debug your current location, how accurately it's found, and the time it used to find it
*)
procedure TRSWalker.DebugPos();
var
mx,my,x1,y1,x2,y2: Int32;
mat: T2DIntArray;
BMP, BMP1: TMufasaBitmap;
TPA: TPointArray;
glob: TPoint;
timeUsed: Double;
begin
timeUsed := PerformanceTimer;
glob := Self.GetMyPos();
glob -= Self.FBaseOffset;
timeUsed := PerformanceTimer - timeUsed;
x1 := Max(0, glob.x-200);
y1 := Max(0, glob.y-200);
x2 := Min(x1+400, High(Self.WorldMap[0]));
y2 := Min(y1+400, High(Self.WorldMap));
mat := Self.WorldMap.Crop(Box(x1,y1,x2,y2));
mx := glob.x - x1;
my := glob.y - y1;
BMP1.Init(client.GetMBitmaps);
BMP1.DrawMatrix(mat);
BMP1.LineTo(Point(0,my), Point(x2-x1,my), $00FF00);
BMP1.LineTo(Point(mx,0), Point(mx,y2-y1), $00FF00);
BMP1.DrawBox(Box(mx-2,my-2,mx+2,my+2), False, $FFFFFF);
BMP.Init(client.GetMBitmaps);
BMP.SetSize(560,420);
BMP.DrawClear($403027);
BMP1.DrawTransparent(10,10, BMP);
BMP1.Free();
glob += Self.FBaseOffset;
{$IFDEF WINDOWS}
BMP.DrawSystemText(
'('+ToStr(glob.x)+', '+ToStr(glob.y)+')',
'Courier New', 13,
Point(422,16),
True, $FFFFFF);
{$ELSE}
BMP.DrawClippedText(
'('+ToStr(glob.x)+', '+ToStr(glob.y)+')',
Point(422,16),
'SmallChars07',
True, $FFFFFF);
{$ENDIF}
mat := RSWUtils.GetMinimap(True,False);
BMP1.Init(client.GetMBitmaps);
BMP1.DrawMatrix(mat);
BMP1.DrawTransparent(426,40, BMP);
BMP1.Free();
{$IFDEF WINDOWS}
BMP.DrawSystemText(
'Acc '+ToStr(Round(Finder.Similarity,3)),
'Courier New', 11,
Point(422,370),
True, $FFFFFF);
BMP.DrawSystemText(
'Used '+ToStr(Round(timeUsed))+' ms',
'Courier New', 11,
Point(422,396),
True, $FFFFFF);
{$ELSE}
BMP.DrawClippedText(
'Acc '+ToStr(Round(Finder.Similarity,3)),
Point(422,370),
'SmallChars07',
True, $FFFFFF);
BMP.DrawClippedText(
'Used '+ToStr(Round(Finder.Similarity,3))+' ms',
Point(422,396),
'SmallChars07',
True, $FFFFFF);
{$ENDIF}
DisplayDebugImgWindow(BMP.GetWidth(), BMP.GetHeight());
DrawBitmapDebugImg(BMP.GetIndex());
BMP.Free();
end;
{$IFDEF !HAD_HINTS}{$H+}{$ENDIF}