imthenewguy

TRandomMousePoint

Jan 9th, 2021 (edited)
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.80 KB | None | 0 0
  1. program new;
  2.  
  3. type TRandomMousePoint = record
  4.   MidPoint:Tpoint;
  5.   MaxRadDist, BaseDist:Integer;
  6.   RefPoints, RefPoints_Distance:TPointArray;
  7.   Bounds:Tbox;
  8. end;
  9.  
  10. var
  11.   RandomPoint:TRandomMousePoint;
  12.  
  13. function TRandomMousePoint.RandPosition(Mu, Sigma, min, max:Double): Integer;
  14. var
  15.   t, g:Double;
  16. begin
  17.   t := 2 * PI * Random();
  18.   g := mu + (sigma * Sqrt(-2 * Ln(Random()))) * Cos(t);
  19.   if (g<min) then
  20.     g := min;
  21.   if (g > max) then
  22.     g := max;
  23.   Result := Round(g);
  24. end;
  25.  
  26. function TRandomMousePoint.CalculatePoint(Pnt:TPoint; MaxRadDist:Integer; BaseDist:Integer=4):TPoint;
  27. begin
  28.   result.x := RandPosition(Pnt.x, BaseDist, Pnt.x-MaxRadDist, Pnt.x+MaxRadDist);
  29.   result.y := RandPosition(Pnt.y, BaseDist, Pnt.y-MaxRadDist, Pnt.y+MaxRadDist);
  30. end;
  31.  
  32. function TRandomMousePoint.GetPoint:TPoint;
  33. var
  34.   Option:integer;
  35. begin
  36.   Option := randomrange(1, 10000);
  37.   repeat
  38.     case Option of
  39.       1..2000     : Result := Self.CalculatePoint(Self.RefPoints[0], Self.MaxRadDist, Self.BaseDist);
  40.       2001..5000  : Result := Self.CalculatePoint(Self.RefPoints[1], Self.MaxRadDist, Self.BaseDist);
  41.       5001..10000 : Result := Self.CalculatePoint(Self.MidPoint, Self.MaxRadDist, Self.BaseDist);
  42.     end;
  43.     if (Self.Bounds.x1 < 1) then
  44.       break;
  45.   until(PointInBox(Result, Self.Bounds));
  46. end;
  47.  
  48. procedure TRandomMousePoint.ChangeMidPoint(p:Tpoint);
  49. var
  50.   i:integer;
  51. begin
  52.   if not (Length(Self.RefPoints) = 2) then exit;
  53.   Self.MidPoint := p;
  54.   Self.RefPoints[0].x := Self.MidPoint.x - Self.RefPoints_Distance[0].x;
  55.   Self.RefPoints[0].y := Self.MidPoint.y - Self.RefPoints_Distance[0].y;
  56.   Self.RefPoints[1].x := Self.MidPoint.x - Self.RefPoints_Distance[1].x;
  57.   Self.RefPoints[1].y := Self.MidPoint.y - Self.RefPoints_Distance[1].y;
  58. end;
  59.  
  60. procedure TRandomMousePoint.Setup(MidPoint:TPoint; MaxRadDist, RefPointsMaxDistFromMid:integer; BaseDist:integer=12; Bounds:Tbox=[0,0,0,0]; DebugRefPoints:boolean=false);
  61. var
  62.   i:integer;
  63. begin
  64.   writeln(length(Self.RefPoints));
  65.   Self.MidPoint := MidPoint;
  66.   Self.MaxRadDist := MaxRadDist;
  67.   Self.Bounds := Bounds;
  68.   Self.BaseDist := BaseDist;
  69.   if (length(Self.RefPoints) < 1) then
  70.   begin
  71.     for i := 0 to 1 do
  72.     begin
  73.       Self.RefPoints.Append(Self.CalculatePoint(MidPoint, RefPointsMaxDistFromMid, RefPointsMaxDistFromMid));
  74.       SetArrayLength(Self.RefPoints_Distance, length(Self.RefPoints_Distance) + 1);
  75.       Self.RefPoints_Distance[i].x := Self.MidPoint.x - Self.RefPoints[i].x;
  76.       Self.RefPoints_Distance[i].y := Self.MidPoint.y - Self.RefPoints[i].y;
  77.     end;
  78.   end;
  79.   if DebugRefPoints then
  80.     writeln('Reference points: ', Self.RefPoints);
  81. end;
  82.  
  83. function TRandomMousePoint.PointInBox(b:tbox; Dynamic_RefPoints:boolean=true):tpoint;
  84. var
  85.   d:integer;
  86. begin
  87.   if Dynamic_RefPoints then
  88.     if (length(Self.RefPoints) = 2) then
  89.     begin
  90.       Self.ChangeMidPoint(b.mid);
  91.       Self.Bounds := b;
  92.     end;
  93.   if not (length(Self.RefPoints) = 2) then
  94.   begin
  95.     d := Distance(b.Mid.x, b.Mid.y, b.x2, b.y2);
  96.     Self.Setup(b.Mid, d, round(d/2), round(d/4), b, false);
  97.   end;
  98.   result := Self.GetPoint;
  99. end;
  100.  
  101. procedure Example_One(Runs:integer);
  102. var
  103.   b:tbox;
  104.   Count:integer;
  105.   p:tpoint;
  106.  
  107. begin
  108.   // Example use
  109.   b := [100, 100, 200, 200];
  110.   repeat
  111.     p := RandomPoint.PointInBox(b);
  112.     MoveMouse(p.x, p.y);
  113.     wait(1);
  114.     ClickMouse(p.x, p.y, MOUSE_LEFT);
  115.     inc(count);
  116.   until(count > Runs);
  117. end;
  118.  
  119. procedure Example_two(Runs:integer);
  120. var
  121.   b:tbox;
  122.   Count:integer;
  123.   p:tpoint;
  124. begin
  125.   b := [100, 100, 200, 200];
  126.   p := b.Mid;
  127.   RandomPoint.Setup(p, 42, 20, 12, b, true);
  128.   repeat
  129.     p := RandomPoint.GetPoint;
  130.     MoveMouse(p.x, p.y);
  131.     wait(1);
  132.     ClickMouse(p.x, p.y, MOUSE_LEFT);
  133.     inc(count);
  134.   until(count > runs);
  135. end;
  136.  
  137. begin
  138.   Example_One(500);
  139.   Example_two(500);
  140.  
  141. end.
Add Comment
Please, Sign In to add comment