-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathworldswitcher.simba
More file actions
569 lines (486 loc) · 13.3 KB
/
worldswitcher.simba
File metadata and controls
569 lines (486 loc) · 13.3 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
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
(*
# WorldSwitcher
WorldSwitcher is resposible for changing worlds with the logged in world
switcher in the {ref}`Logout` gametab.
```{figure} ../../images/worldswitcher.png
```
*)
{$DEFINE WL_WORLDSWITCHER_INCLUDED}
{$INCLUDE_ONCE WaspLib/osrs.simba}
type
(*
## ERSWorldSwitcherButton enum
```pascal
ERSWorldSwitcherButton = enum(CONFIGURE, CLOSE, LOGOUT);
```
Enum representing the world switcher buttons.
*)
ERSWorldSwitcherButton = enum(CONFIGURE, CLOSE, LOGOUT);
(*
## TRSWorld type
Helper record for {ref}`TRSworldSwitcher`.
*)
TRSWorld = record
Number: Integer;
Bounds: TBox;
end;
(*
## TRSWorldArray type
Helper type for {ref}`TRSworldSwitcher`.
*)
TRSWorldArray = array of TRSWorld;
procedure ShowOnTarget(worlds: TRSWorldArray); overload;
var
img: TImage;
i: Integer;
begin
img := Target.GetImage();
img.DrawColor := $FFFFFF;
for i := 0 to High(worlds) do
begin
WriteLn worlds[i].Number;
img.DrawBox(worlds[i].Bounds);
end;
img.Show();
end;
type
(*
## TRSWorldSwitcher type
Main record that interacts with the {ref}`WorldSwitcher`.
*)
TRSWorldSwitcher = record
Scroll: TRSScrollBar;
Cooldown: TCountDown;
CurrentWorldBounds, SortArrow: TBox;
Buttons: array [ERSWorldSwitcherButton] of TRSButton;
const SILVER: TColor = $E0E0E0;
const YELLOW: TColor = $00F0F0;
end;
(*
## TRSWorldSwitcher.Setup
```pascal
procedure TRSWorldSwitcher.Setup();
```
Interal method used to setup the {ref}`TRSWorldSwitcher` cooldown.
This is automatically called for you on the {ref}`WorldSwitcher variable`.
*)
procedure TRSWorldSwitcher.Setup();
var
t: Int64;
begin
t := Time();
Self.Cooldown.FLength := 8000;
Self.Cooldown.FStartTime := t-8000;
Self.Cooldown.FFinishTime := t;
Self.Cooldown.FPauseTime := 0;
end;
(*
## TRSWorldSwitcher.SetupGameTab
```pascal
procedure TRSWorldSwitcher.SetupGameTab();
```
Interal method used to setup the {ref}`TRSWorldSwitcher` coordinates.
This is automatically called for you on the {ref}`WorldSwitcher variable`.
*)
procedure TRSWorldSwitcher.SetupGameTab();
begin
with GameTab.Bounds do
begin
Self.CurrentWorldBounds := [X2-52, Y1+4, X2-25, Y1+17];
Self.Buttons[ERSWorldSwitcherButton.CONFIGURE] := new TRSButton([X1, Y1+4, X1+15, Y1+19]);
Self.Buttons[ERSWorldSwitcherButton.CLOSE] := new TRSButton([X2-17, Y1+2, X2+3, Y1+22]);
Self.Buttons[ERSWorldSwitcherButton.LOGOUT] := new TRSButton([X2-19, Y2-30, X2+1, Y2-1]);
Self.SortArrow := [X1+18, Y1+25, X1+34, Y1+35];
Self.Scroll.Area := [X1-3, Y1+36, X2-13, Y2-33];
end;
Self.Scroll.Setup();
end;
function TRSWorldSwitcher._IsOpen(): Boolean;
begin
Result := Target.HasColor([$0A0B0E, 5, EColorSpace.HSV, [0.368, 0.761, 1.873]], 90, Self.Buttons[ERSWorldSwitcherButton.CLOSE].Bounds);
end;
(*
## WorldSwitcher.IsOpen
```pascal
function TRSWorldSwitcher.IsOpen(): Boolean;
```
Returns true if the world switcher is open.
Example:
```pascal
WriteLn WorldSwitcher.IsOpen();
```
*)
function TRSWorldSwitcher.IsOpen(): Boolean;
begin
Result := GameTabs.IsOpen(ERSGameTab.LOGOUT) and Self._IsOpen();
end;
(*
## WorldSwitcher.IsLoading
```pascal
function TRSWorldSwitcher.IsLoading(): Boolean;
```
Returns true if the world switcher is loading.
The world switcher takes a few milliseconds to load.
Example:
```pascal
WriteLn WorldSwitcher.IsLoading();
```
*)
function TRSWorldSwitcher.IsLoading(): Boolean;
begin
Result := not Target.HasColor(RSFonts.ORANGE, 0, 50, Self.CurrentWorldBounds);
end;
(*
## WorldSwitcher.WaitLoading
```pascal
function TRSWorldSwitcher.WaitLoading(time: Integer = 600; interval: Integer = -1): Boolean;
```
Waits for the world switcher to finish loading.
For more information on loading read {ref}`WorldSwitcher.IsLoading`.
Example:
```pascal
WriteLn WorldSwitcher.WaitLoading();
```
*)
function TRSWorldSwitcher.WaitLoading(time: Integer = 600; interval: Integer = -1): Boolean;
begin
if interval < 0 then interval := RandomMode(100, 50, 1500);
Result := SleepUntil(not Self.IsLoading(), interval, time);
end;
(*
## WorldSwitcher.Open
```pascal
function TRSWorldSwitcher.Open(waitLoad: Boolean = True): Boolean;
```
Attempts to open the world switcher.
This method is overriden after {ref}`Logout`, for more information check
{ref}`WorldSwitcher.Open override`.
Example:
```pascal
WriteLn WorldSwitcher.Open();
```
*)
function TRSWorldSwitcher.Open(waitLoad: Boolean = True): Boolean;
begin
//overriden in TRSLogout.
Result := SleepUntil(not Self._IsOpen(), RandomMode(100, 50, 1500), TICK);
if Result and waitLoad then
Self.WaitLoading(4*TICK);
end;
(*
## Logout.CloseWorldSwitcher
```pascal
function TRSLogout.CloseWorldSwitcher(): Boolean;
```
Closes the world switcher.
Example:
```pascal
Logout.CloseWorldSwitcher();
```
*)
function TRSWorldSwitcher.Close(): Boolean;
begin
if not Self.IsOpen() then Exit(True);
Self.Buttons[ERSWorldSwitcherButton.CLOSE].Click();
Result := SleepUntil(not Self._IsOpen(), RandomMode(100, 50, 1500), 600);
end;
(*
## WorldSwitcher.Logout
```pascal
function TRSWorldSwitcher.Logout(attempts: UInt32 = 5; time: UInt32 = 20000): Boolean;
```
Clicks the logout button. Returns true if we succesfully logout of the game.
Example:
```pascal
WriteLn WorldSwitcher.Logout();
```
*)
function TRSWorldSwitcher.Logout(attempts: UInt32 = 5; time: UInt32 = 20000): Boolean;
var
i: Integer;
interval: UInt32;
begin
if not Self.IsOpen() then Exit;
interval := time div attempts;
if Keyboard.LastKey = EKeyCode.RETURN then
Keyboard.RandomKey(); //unlock enter if using remote input
for i := 1 to attempts do
begin
Self.Buttons[ERSWorldSwitcherButton.LOGOUT].Click();
if SleepUntil(not RSClient.IsLoggedIn(), 500, interval + Random(-2000, 2000)) then
Exit(True);
end;
end;
(*
## WorldSwitcher.GetWorlds
```pascal
function TRSWorldSwitcher.GetWorlds(): TRSWorldArray;
```
Returns the currently visible {ref}`TRSWorldArray`.
Example:
```pascal
ShowOnTarget(WorldSwitcher.GetWorlds());
```
*)
function TRSWorldSwitcher.GetWorlds(): TRSWorldArray;
var
tpa: TPointArray;
atpa: T2DPointArray;
weights: TIntegerArray;
world: TRSWorld;
begin
tpa := Target.FindColor(Self.SILVER, 0, Self.Scroll.Area) +
Target.FindColor(Self.YELLOW, 0, Self.Scroll.Area);
atpa := tpa.Cluster(5, 1.5);
for tpa in atpa do
with tpa.Bounds() do
begin
if Height <> 10 then Continue;
world.Number := OCR.RecognizeNumber([X1-3, Y1-2, X2+3, Y2+2], RSFonts.PLAIN_12, [Self.SILVER, Self.YELLOW], 0);
if world.Number = -1 then Continue;
world.Bounds := [Self.Scroll.Area.X1, Y1-2, Self.Scroll.Area.X2, Y2+3];
weights += Y1;
Result += World;
end;
Result.Sort(weights, True);
end;
(*
## WorldSwitcher.FindWorld
```pascal
function TRSWorldSwitcher.FindWorld(number: Integer; out world: TRSWorld): Boolean;
```
Returns true if the specified world `number` is found.
The world data is returned through the `world` variable.
Example:
```pascal
if WorldSwitcher.FindWorld(311, world) then
ShowOnTarget([world]);
```
*)
function TRSWorldSwitcher.FindWorld(number: Integer; out world: TRSWorld): Boolean;
var
tpa: TPointArray;
atpa: T2DPointArray;
begin
tpa := Target.FindColor(Self.SILVER, 0, Self.Scroll.Area) +
Target.FindColor(Self.YELLOW, 0, Self.Scroll.Area);
atpa := tpa.Cluster(5, 1.5);
for tpa in atpa do
with tpa.Bounds() do
begin
if Height <> 10 then Continue;
world.Number := OCR.RecognizeNumber([X1-3, Y1-2, X2+3, Y2+2], RSFonts.PLAIN_12, [Self.SILVER, Self.YELLOW], 0);
if world.Number <> number then Continue;
world.Bounds := [Self.Scroll.Area.X1, Y1-2, Self.Scroll.Area.X2, Y2+3];
Exit(True);
end;
end;
(*
## WorldSwitcher.IsSorted
```pascal
function TRSWorldSwitcher.IsSorted(): Boolean;
```
Checks if the worlds in the world switcher are sorted.
Example:
```pascal
WriteLn WorldSwitcher.IsSorted();
```
*)
function TRSWorldSwitcher.IsSorted(): Boolean;
var
arrow : TPointArray;
begin
arrow := Target.FindColor(
ColorTolerance($0A5DA3, 0.781, EColorSpace.HSV, [2.366, 0.472, 0.164]),
Self.SortArrow
);
Result := Self.SortArrow.Center = arrow.Mean();
end;
(*
## WorldSwitcher.Sort
```pascal
function TRSWorldSwitcher.Sort(): Boolean;
```
Sorts the worlds from lower to higher numbers.
Example:
```pascal
WriteLn WorldSwitcher.Sort();
```
*)
function TRSWorldSwitcher.Sort(): Boolean;
begin
if not Self.IsOpen() then Exit;
if Self.IsSorted() then Exit(True);
Mouse.Click(Self.SortArrow, EMouseButton.LEFT);
Result := SleepUntil(Self.IsSorted(), 50, 600);
end;
function TRSWorldSwitcher.WaitSwitch(world: Integer; failCooldown: Boolean = True): Boolean; forward;
function TRSWorldSwitcher._Hop(next: Integer): Boolean;
var
timeout: TCountDown;
available: TRSWorldArray;
world: TRSWorld;
begin
if not Self.IsSorted() and not Self.Sort() then
Exit;
timeout.Start(20 * ONE_SECOND);
repeat
Sleep(20,40);
available := Self.GetWorlds();
if available = [] then Exit;
for world in available do
if world.Number = next then
begin
Mouse.Click(world.Bounds, EMouseButton.LEFT);
if Self.WaitSwitch(next) then
Exit(True);
end;
Self.Scroll.Scroll(Biometrics.RandomModeInteger(2,1,3), next > available[0].Number);
until not InRange(Self.Scroll.GetLevel(), 1, 99) or timeout.IsFinished;
end;
(*
## WorldSwitcher.World property
```pascal
property TRSWorldSwitcher.World: Integer;
property TRSWorldSwitcher.World(next: Integer): Boolean;
```
Returns or sets the current world we are on.
Setting a new world will only happen if `TRSWorldSwitcher.Cooldown` is finished,
which is a cooldown that starts everytime you change worlds.
Example:
```pascal
WriteLn WorldSwitcher.World;
WorldSwitcher.World := 311;
WriteLn WorldSwitcher.World;
```
*)
property TRSWorldSwitcher.World: Integer;
begin
Result := OCR.RecognizeNumber(Self.CurrentWorldBounds, RSFonts.BOLD, [RSFonts.ORANGE], 0);
end;
property TRSWorldSwitcher.World(next: Integer): Boolean;
var
current: Integer;
begin
if not Self.Cooldown.IsFinished or not Self.Open() then
Exit;
current := Self.World;
if next = current then
Exit(True);
Result := Self._Hop(next);
end;
(*
## WorldSwitcher.Next
```pascal
function TRSWorldSwitcher.Next(): Boolean;
```
Hops to the world that follows our current world according to the contents of
`Profiles[ProfileIndex].Worlds`.
Example:
```pascal
WriteLn WorldSwitcher.World;
WorldSwitcher.Next();
WriteLn WorldSwitcher.World;
```
*)
function TRSWorldSwitcher.Next(): Boolean;
var
current, next: Integer;
begin
if not Self.Cooldown.IsFinished or not Self.Open() then
Exit;
if Profiles[ProfileIndex].Worlds = [] then
raise GetDebugLn('WorldSwitcher', 'Current profile has no worlds saved.');
current := Self.World;
if Profiles[ProfileIndex].Worlds = [current] then
Exit(True);
next := Profiles[ProfileIndex].Worlds.IndexOf(current);
if Inc(next) > High(Profiles[ProfileIndex].Worlds) then
next := 0;
Result := Self._Hop(Profiles[ProfileIndex].Worlds[next]);
end;
(*
## WorldSwitcher.Previous
```pascal
function TRSWorldSwitcher.Previous(): Boolean;
```
Hops to the world that comes before our current world according to the contents
of `Profiles[ProfileIndex].Worlds`.
Example:
```pascal
WriteLn WorldSwitcher.World;
WorldSwitcher.Previous();
WriteLn WorldSwitcher.World;
```
*)
function TRSWorldSwitcher.Previous(): Boolean;
var
current, prev: Integer;
begin
if not Self.Cooldown.IsFinished or not Self.Open() then
Exit;
if Profiles[ProfileIndex].Worlds = [] then
raise GetDebugLn('WorldSwitcher', 'Current profile has no worlds saved.');
current := Self.World;
if Profiles[ProfileIndex].Worlds = [current] then
Exit(True);
prev := Profiles[ProfileIndex].Worlds.IndexOf(current);
if Dec(prev) < 0 then
prev := High(Profiles[ProfileIndex].Worlds);
Result := Self._Hop(Profiles[ProfileIndex].Worlds[prev]);
end;
(*
## WorldSwitcher.Random
```pascal
function TRSWorldSwitcher.Random(): Boolean;
```
Hops to a random world that's in `Profiles[ProfileIndex].Worlds` and is not our
current world.
Example:
```pascal
WriteLn WorldSwitcher.World;
WorldSwitcher.Random();
WriteLn WorldSwitcher.World;
```
*)
function TRSWorldSwitcher.Random(): Boolean;
var
current, rnd: Integer;
begin
if not Self.Cooldown.IsFinished or not Self.Open() then
Exit;
if Profiles[ProfileIndex].Worlds = [] then
raise GetDebugLn('WorldSwitcher', 'Current profile has no worlds saved.');
current := Self.World;
if Profiles[ProfileIndex].Worlds = [current] then
Exit(True);
repeat
rnd := Profiles[ProfileIndex].Worlds.Random();
until rnd <> current;
Result := Self._Hop(rnd);
end;
(*
## WorldSwitcher.WaitSwitch
```pascal
function TRSWorldSwitcher.WaitSwitch(world: Integer; failCooldown: Boolean = True): Boolean;
```
Exits false if "Please wait" not found, presumably due to combat.
Returns true if we find "Please wait" and finish world hopping successfully to
the specified `world`
*)
function TRSWorldSwitcher.WaitSwitch(world: Integer; failCooldown: Boolean = True): Boolean;
begin
if not SleepUntil(MainScreen.IsServerMessage('Please wait'), 200, 3*TICK) then
begin
if failCooldown then
Self.Cooldown.Restart(0, 7000);
Exit;
end;
Result := SleepUntil(Self.World = world, 200, 10000);
end;
var
(*
## WorldSwitcher variable
Global {ref}`TRSWorldSwitcher` variable.
*)
WorldSwitcher: TRSWorldSwitcher;