Skip to content

Commit 33cf648

Browse files
committed
feat(Minimap): CompassRadians and CompassDegrees setters
- also fixed a small bug in the door handler, that would cause it to loop forever - added new Mouse.Drag methods -
1 parent b7dc951 commit 33cf648

File tree

8 files changed

+206
-99
lines changed

8 files changed

+206
-99
lines changed

osrs/antiban/antibantasks.simba

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,13 @@ end;
8383
procedure TAntiban.SmallCameraRotation();
8484
begin
8585
WriteLn GetDebugLn('Antiban', 'Small camera rotation');
86-
Minimap.SetCompassAngle(Minimap.CompassDegrees + Random(-15,15));
86+
Minimap.CompassRadians := Minimap.CompassRadians + GaussRand(PI/12, PI/4) * Random(-1,1);
8787
end;
8888

8989
procedure TAntiban.LargeCameraRotation();
9090
begin
9191
WriteLn GetDebugLn('Antiban', 'Large camera rotation');
92-
93-
case RandomBoolean(0.5) of
94-
True: Minimap.SetCompassAngle(Minimap.CompassDegrees - GaussRand(30, 360));
95-
False: Minimap.SetCompassAngle(Minimap.CompassDegrees + GaussRand(30, 360));
96-
end;
92+
Minimap.CompassRadians := Minimap.CompassRadians + GaussRand(PI/6, TAU) * Random(-1,1);
9793
end;
9894

9995

osrs/interfaces/minimap.simba

Lines changed: 139 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ Main record used to interact with the {ref}`Minimap`.
4848
Polygon: TPolygon;
4949

5050
Compass: record
51-
PreviousRadians: Single;
5251
Circle: TCircle;
52+
Accuracy: Double;
5353
end;
5454

5555
Mask: TPointArray;
@@ -224,22 +224,36 @@ begin
224224
end;
225225

226226

227+
(*
228+
## Minimap.Compass
229+
The minimap compass refers to the small compass on the top left corner of the
230+
minimap:
231+
```{figure} ../../images/compass.png
232+
```
233+
The following properties are used to read and set the compass angle as both
234+
radians and degrees as needed.
235+
*)
227236

228237
(*
229-
## Minimap.CompassRadians
238+
### Minimap.CompassRadians
230239
```pascal
231240
property TRSMinimap.CompassRadians: Single;
241+
property TRSMinimap.CompassRadians(radians: Single): Boolean;
242+
property TRSMinimap.CompassRadians(minRadians, maxRadians: Single): Boolean;
232243
```
233-
Returns the minimap compass angle as radians.
244+
Returns or sets the minimap compass angle as radians.
245+
246+
The accuracy can be controlled through the `Minimap.Compass.Accuracy` variable
247+
and it's value should be in radians. It's default is aproximately
248+
5º (`0.03*PI`).
234249

235250
Credits: [slacky](https://slacky.one/)
236251

237252
Example:
238253
```pascal
239254
WriteLn Minimap.CompassRadians;
240-
```
241-
If you are not sure what the compass refers to, it refers to this:
242-
```{figure} ../../images/compass.png
255+
WriteLn Minimap.CompassRadians := PI;
256+
WriteLn Minimap.CompassRadians;
243257
```
244258
*)
245259
property TRSMinimap.CompassRadians: Single;
@@ -288,108 +302,103 @@ begin
288302
Result := Result + TAU;
289303
end;
290304

291-
(*
292-
## Minimap.CompassDegrees
293-
```pascal
294-
property TRSMinimap.CompassDegrees: Single;
295-
```
296-
Returns the minimap compass angle as radians.
297-
298-
Credits: [slacky](https://slacky.one/)
299-
300-
Example:
301-
```pascal
302-
WriteLn Minimap.CompassDegrees;
303-
```
304-
If you are not sure what the compass refers to, it refers to this:
305-
```{figure} ../../images/compass.png
306-
```
307-
*)
308-
property TRSMinimap.CompassDegrees: Single;
309-
begin
310-
Result := RadToDeg(Self.CompassRadians);
311-
end;
312-
313-
314-
(*
315-
## Minimap.SetCompassAngle
316-
```pascal
317-
function TRSMinimap.SetCompassAngleEx(degrees, accuracy: Single): Boolean;
318-
function TRSMinimap.SetCompassAngle(degrees: Single): Boolean;
319-
function TRSMinimap.SetCompassAngle(minDegrees, maxDegrees: Single; accuracy: Single = 5): Boolean; overload;
320-
```
321-
Sets the current compass angle.
322-
If you specify a minimum and a maximum angle a gaussian distribution will be used.
323-
324-
Example:
325-
```pascal
326-
Minimap.SetCompassAngle(180);
327-
```
328-
*)
329-
function TRSMinimap.SetCompassAngleEx(degrees, accuracy: Single): Boolean;
305+
property TRSMinimap.CompassRadians(radians: Double): Boolean;
330306
const
331-
DEG_PER_PIXEL = 2.83;
307+
RAD_PER_PIXEL: Double = 162.2004206;
332308
var
333309
available: TBox;
334-
remaining: Integer;
335-
left2right: Boolean;
310+
remain, oneDeg: Double;
311+
clockwise: Boolean;
336312
destination: TPoint;
337313
pixels: Integer;
338314
timeout: UInt64;
339315
begin
340316
available := RSClient.Bounds.Expand(-10);
341-
degrees := DegNormalize(degrees);
317+
radians := RadNormalize(radians);
342318
timeout := Time() + RandomMean(6000, 8000);
319+
oneDeg := DegToRad(1);
343320

344321
repeat
345-
remaining := Round(DeltaAngle(Self.CompassDegrees, degrees));
346-
if Abs(remaining) <= accuracy then Exit(True);
322+
remain := DeltaAngle(Self.CompassRadians, radians, TAU);
323+
if Abs(remain) <= Self.Compass.Accuracy then
324+
Exit(True);
347325

348-
pixels := Round(Abs(DeltaAngle(Self.CompassDegrees, degrees)) * DEG_PER_PIXEL);
349-
left2right := InRange(remaining, 1, 180) or (remaining < -180);
326+
pixels := Round(Abs(remain) * RAD_PER_PIXEL);
327+
clockwise := InRange(remain, oneDeg, PI) or (remain < -PI);
350328

351-
if left2right then
329+
if clockwise then
352330
begin
353331
if (not available.Contains(Target.MouseXY)) or ((Target.MouseY + 200 > available.Y2) or (Target.MouseX + pixels > available.X2)) then
354-
Target.MouseMove([available.X1, available.Y1, available.X1 + 200, available.Y1 + 200]);
332+
Mouse.Move([available.X1, available.Y1, available.X1 + 200, available.Y1 + 200]);
355333

356334
destination.X := Target.MouseX + pixels;
357335
end
358336
else
359337
begin
360338
if (not available.Contains(Target.MouseXY)) or ((Target.MouseY + 200 > available.Y2) or (Target.MouseX - pixels < available.X1)) then
361-
Target.MouseMove([available.X2 - 200, available.Y1, available.X2, available.Y1 + 200]);
339+
Mouse.Move([available.X2 - 200, available.Y1, available.X2, available.Y1 + 200]);
362340

363-
destination.X := Target.MouseX - Pixels;
341+
destination.X := Target.MouseX - pixels;
364342
end;
365343

366344
destination.Y := Random(Target.MouseY + 100, available.Y2);
367345

368-
Target.MouseDown(EMouseButton.MIDDLE);
369-
Target.MouseMove(destination);
370-
Target.MouseUp(EMouseButton.MIDDLE);
371-
372-
Sleep(100, 1000, ERandomDir.LEFT);
346+
Mouse.Drag(destination, EMouseButton.MIDDLE);
347+
Biometrics.Sleep(100, 1000, ERandomDir.LEFT);
373348
until Time() > timeout;
374349

375-
WriteLn GetDebugLn('Minimap', 'SetCompassAngle timed out.', ELogLevel.WARN);
350+
WriteLn GetDebugLn('Minimap', 'Setting `CompassRadians` timed out.', ELogLevel.WARN);
351+
376352
if not RSClient.IsLoggedIn() then
377353
Login.DoLogin()
378354
else
379355
WriteLn GetDebugLn('Minimap', 'Make sure the setting "Middle mouse button controls the camera" is enabled in the game settings.', ELogLevel.WARN);
380356
end;
381357

382-
function TRSMinimap.SetCompassAngle(degrees: Single): Boolean;
358+
property TRSMinimap.CompassRadians(minRadians, maxRadians: Single): Boolean;
359+
var
360+
radians: Single;
383361
begin
384-
Result := Self.SetCompassAngleEx(degrees, 5);
362+
radians := RandomMean(RadNormalize(minRadians), RadNormalize(maxRadians));
363+
Result := Self.CompassRadians := radians;
385364
end;
386365

387-
function TRSMinimap.SetCompassAngle(minDegrees, maxDegrees: Single; accuracy: Single = 5): Boolean; overload;
388-
var
389-
degrees: Single;
366+
367+
(*
368+
### Minimap.CompassDegrees
369+
```pascal
370+
property TRSMinimap.CompassDegrees: Single;
371+
property TRSMinimap.CompassDegrees(degrees: Single): Boolean;
372+
property TRSMinimap.CompassDegrees(minDegrees, maxDegrees: Single): Boolean;
373+
```
374+
Returns or sets the minimap compass angle as degrees.
375+
376+
The accuracy can be controlled through the `Minimap.Compass.Accuracy` variable
377+
and it's value should be in radians. It's default is aproximately
378+
5º (`0.03*PI`).
379+
380+
Example:
381+
```pascal
382+
WriteLn Minimap.CompassDegrees;
383+
WriteLn Minimap.CompassDegrees := 180;
384+
WriteLn Minimap.CompassDegrees;
385+
```
386+
If you are not sure what the compass refers to, check the image at the end of
387+
{ref}`Minimap.CompassRadians`.
388+
*)
389+
property TRSMinimap.CompassDegrees: Single;
390+
begin
391+
Result := RadToDeg(Self.CompassRadians);
392+
end;
393+
394+
property TRSMinimap.CompassDegrees(degrees: Single): Boolean;
395+
begin
396+
Result := Self.CompassRadians := DegToRad(degrees);
397+
end;
398+
399+
property TRSMinimap.CompassDegrees(minDegrees, maxDegrees: Single): Boolean;
390400
begin
391-
degrees := RandomMean(DegNormalize(minDegrees), DegNormalize(maxDegrees));
392-
Result := Self.SetCompassAngleEx(degrees, accuracy);
401+
Result := Self.CompassRadians[DegToRad(minDegrees), DegToRad(maxDegrees)];
393402
end;
394403

395404

@@ -639,6 +648,66 @@ begin
639648
end;
640649

641650

651+
(*
652+
## Minimap.CountColor
653+
```pascal
654+
function TRSMinimap.CountColor(color: TColor; tolerance: Single = 0): Integer;
655+
function TRSMinimap.CountColor(color: TColorTolerance): Integer; overload;
656+
```
657+
Counts the specified `color` on the {ref}`Minimap`.
658+
659+
Example:
660+
```pascal
661+
WriteLn Minimap.CountColor($FFFFFF);
662+
```
663+
*)
664+
function TRSMinimap.CountColor(color: TColor; tolerance: Single = 0): Integer;
665+
var
666+
tpa: TPointArray;
667+
begin
668+
tpa := Target.FindColor(color, tolerance, Self.Bounds);
669+
Result := tpa.ExtractPolygon(Self.Polygon).Length;
670+
end;
671+
672+
function TRSMinimap.CountColor(color: TColorTolerance): Integer; overload;
673+
var
674+
tpa: TPointArray;
675+
begin
676+
tpa := Target.FindColor(color, Self.Bounds);
677+
Result := tpa.ExtractPolygon(Self.Polygon).Length;
678+
end;
679+
680+
681+
(*
682+
## Minimap.ColorPercent
683+
```pascal
684+
function TRSMinimap.ColorPercent(color: TColor; tolerance: Single = 0): Single;
685+
function TRSMinimap.ColorPercent(color: TColorTolerance): Single; overload;
686+
```
687+
Counts the percentage of the specified `color` on the {ref}`Minimap`.
688+
689+
Example:
690+
```pascal
691+
WriteLn Minimap.ColorPercent($FFFFFF);
692+
```
693+
*)
694+
function TRSMinimap.ColorPercent(color: TColor; tolerance: Single = 0): Single;
695+
var
696+
count: Integer;
697+
begin
698+
count := Self.CountColor(color, tolerance);
699+
Result := count / Self.Polygon.Area;
700+
end;
701+
702+
function TRSMinimap.ColorPercent(color: TColorTolerance): Single; overload;
703+
var
704+
count: Integer;
705+
begin
706+
count := Self.CountColor(color);
707+
Result := count / Self.Polygon.Area;
708+
end;
709+
710+
642711
(*
643712
## Minimap.Normalize
644713
```pascal

osrs/interfaces/mm2ms.simba

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,11 +447,10 @@ begin
447447
Break;
448448
end;
449449

450-
if not Result then Exit;
450+
if not Result then
451+
Exit;
451452

452-
Self.SetCompassAngle(
453-
Self.Center.AngleBetween(tpa[i]) - Self.Center.AngleBetween(pt) + randomness
454-
);
453+
Self.CompassDegrees := Self.Center.AngleBetween(tpa[i]) - Self.Center.AngleBetween(pt) + randomness;
455454
end;
456455

457456
(*

osrs/interfaces/setup.simba

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ begin
152152
MainScreen.UptextOCR.Tolerance := 25;
153153
MainScreen.UptextOCR.ShadowTolerance := 65;
154154
MainScreen.UptextOCR.Whitelist := ['a'..'z', 'A'..'Z', '0'..'9', '-', '>', '(', ')', '/'];
155+
Minimap.Compass.Accuracy := 0.03*PI;
155156
Options.ZoomLevel := -1;
156157
MM2MS.Projector.RSZoom := -1;
157158
RSMouseZoom.ZoomLevel := -1;

osrs/position/map/entities.simba

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,6 @@ var
323323
attempt, i: Integer;
324324
atpa: T2DPointArray;
325325
coordinates, tpa: TPointArray;
326-
angle: Double;
327326
begin
328327
Result := Self._UpTextCheck(shouldExit);
329328
if shouldExit then Exit;
@@ -349,10 +348,10 @@ begin
349348

350349
if MainScreen.IsUpText(Self.UpText) then Exit(True);
351350

352-
if attempt <> (attempts - 1) then Continue;
351+
if attempt <> (attempts - 1) then
352+
Continue;
353353

354-
angle := Minimap.CompassDegrees;
355-
Minimap.SetCompassAngle(angle-50, angle+50);
354+
Minimap.CompassRadians := Minimap.CompassRadians + PI/3.6 * Random(-1,1);
356355
end;
357356
end;
358357

@@ -423,7 +422,7 @@ begin
423422
Continue;
424423
end;
425424

426-
Minimap.SetCompassAngle(Minimap.CompassDegrees, 50);
425+
Minimap.CompassRadians := Minimap.CompassRadians + PI/3.6 * Random(-1,1);
427426
end;
428427
end;
429428

osrs/position/map/objects.simba

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,6 @@ var
542542
attempt, i: Integer;
543543
atpa: T2DPointArray;
544544
tpa: TPointArray;
545-
angle: Double;
546545
coordinates: TPointArray;
547546
begin
548547
if ChooseOption.IsOpen() then
@@ -583,10 +582,10 @@ begin
583582

584583
if MainScreen.IsUpText(Self.UpText + action) then Exit(True);
585584

586-
if attempt <> (attempts - 1) then Continue;
585+
if attempt <> (attempts - 1) then
586+
Continue;
587587

588-
angle := Minimap.CompassDegrees;
589-
Minimap.SetCompassAngle(angle-50, angle+50);
588+
Minimap.CompassRadians := Minimap.CompassRadians + PI/3.6 * Random(-1,1);
590589
end;
591590
end;
592591

@@ -713,7 +712,7 @@ begin
713712
Continue;
714713
end;
715714

716-
Minimap.SetCompassAngle(Minimap.CompassDegrees, 50);
715+
Minimap.CompassRadians := Minimap.CompassRadians + PI/3.6 * Random(-1,1);
717716
end;
718717
end;
719718

0 commit comments

Comments
 (0)