/
mouse.simba
595 lines (492 loc) · 13.9 KB
/
mouse.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
(*
Moving the mouse
~~~~~~~~~~~~~~~~
Move the mouse to a x, y coord.
- Mouse.Move(100, 100);
Move the mouse to a point.
- Mouse.Move(Point(100, 100));
Move the mouse to a random (gauss) point in a box.
- Mouse.Move(IntToBox(0, 0, 100, 100));
Move the mouse to a random (gauss) point in a circle.
- Mouse.Move(Point(100, 100), 50);
Clicking the mouse
~~~~~~~~~~~~~~~~~~
Vaild click types: mouse_Left, mouse_Right, mouse_Middle.
Click the mouse at the current position.
- Mouse.Click(mouse_Left);
Move and click the mouse to a x, y coord.
- Mouse.Click(100, 100, mouse_Left);
Move and click the mouse to a point.
- Mouse.Click(Point(100, 100), mouse_Right);
Move and click the mouse to a random (gauss) point in a box.
- Mouse.Click(IntToBox(0, 0, 100, 100), mouse_LEft);
Move and click the mouse to a random (gauss) point in a circle.
- Mouse.Click(Point(100, 100), 50, mouse_Right);
*)
type
TMouseMode = (mouse_Human, mouse_Brake, mouse_Accurate);
TMouseChangeDestEvent = procedure(Sender: TObject; var xe, ye: Extended; var exit_method: Boolean);
TMouse = record
{$IFNDEF CODEINSIGHT}
FSpeed: Integer;
FMode: TMouseMode;
FGravity, FWind: array [0..2] of Integer;
FOnMouseMove: TMouseMoveEvent;
FOnMouseEvent: TMouseEvent;
FOnMouseChangeDestEvent: TMouseChangeDestEvent;
{$ENDIF}
end;
procedure TMouse.__Setup();
begin
FSpeed := Random(21, 23);
FMode := mouse_Human;
FOnMouseMove := nil;
FOnMouseEvent := nil;
FOnMouseChangeDestEvenT := nil;
FGravity[Ord(Mouse_Human)] := 7;
FGravity[Ord(Mouse_Brake)] := 7;
FGravity[Ord(Mouse_Accurate)] := 14;
FWind[Ord(mouse_Human)] := 4;
FWind[Ord(mouse_Brake)] := 4;
FWind[Ord(mouse_Accurate)] := 3;
end;
procedure TMouse.ValidateButton(const Button: Integer);
begin
if (not Button = mouse_Left) or (not Button = mouse_Right) or (not Button = mouse_Middle) then
RaiseException(erCustomError, 'TMouse.ValidateButton: Mouse button isn''t vaild, use (mouse_Left, mouse_Right, mouse_Middle');
end;
procedure TMouse.SetPosition(const Target: TPoint);
begin
if (@FOnMouseMove <> nil) then
FOnMouseMove(@Self, [], Target.X, Target.Y);
MoveMouse(Target.X, Target.Y);
end;
function TMouse.GetPosition(): TPoint;
begin
GetMousePos(Result.X, Result.Y);
end;
function TMouse.GetMode(): TMouseMode;
begin
Result := FMode;
end;
procedure TMouse.SetMode(const NewMode: TMouseMode);
begin
FMode := NewMode;
end;
function TMouse.GetSpeed(): Integer;
begin
Result := Self.FSpeed;
end;
procedure TMouse.SetSpeed(const NewSpeed: Integer);
begin
Self.FSpeed := NewSpeed;
end;
function TMouse.GetGravity(const Mode: TMouseMode): Integer;
begin
Result := Self.FGravity[Ord(Mode)];
end;
procedure TMouse.SetGravity(const Mode: TMouseMode; const NewGravity: Integer);
begin
Self.FGravity[Ord(Mode)] := NewGravity;
end;
function TMouse.GetWind(const Mode: TMouseMode): Integer;
begin
Result := Self.FWind[Ord(Mode)];
end;
procedure TMouse.SetWind(const Mode: TMouseMode; const NewWind: Integer);
begin
Self.FWind[Ord(Mode)] := NewWind;
end;
function TMouse.GetOnMouseMove(): TMouseMoveEvent;
begin
Result := @Self.FOnMouseMove;
end;
procedure TMouse.SetOnMouseMove(const Event: TMouseMoveEvent);
begin
Self.FOnMouseMove := @Event;
end;
function TMouse.GetOnMouseEvent(): TMouseEvent;
begin
Result := @Self.FOnMouseEvent;
end;
procedure TMouse.SetOnMouseEvent(const Event: TMouseEvent);
begin
Self.FOnMouseEvent := @Event;
end;
function TMouse.GetOnChangeDestEvent(): TMouseChangeDestEvent;
begin
Result := @Self.FOnMouseChangeDestEvent;
end;
procedure TMouse.SetOnChangeDestEvent(const Event: TMouseChangeDestEvent);
begin
Self.FOnMouseChangeDestEvent := @Event;
end;
procedure TMouse.Hold(const Button: Integer);
var
p: TPoint;
begin
Self.ValidateButton(Button);
p := Self.GetPosition();
HoldMouse(p.x, p.y, Button);
end;
procedure TMouse.Release(const Button: Integer);
var
p: TPoint;
begin
Self.ValidateButton(Button);
p := Self.GetPosition();
ReleaseMouse(p.x, p.y, Button);
end;
{$IFNDEF CODEINSIGHT}
procedure TMouse._MoveBrake(xs, ys, xe, ye, gravity, wind, targetArea: extended; double: boolean);
const
sqrt2 := sqrt(2);
sqrt3 := sqrt(3);
sqrt5 := sqrt(5);
var
veloX,veloY,windX,windY,veloMag,dist,randomDist,lastDist,D: extended;
lastX,lastY,tempSpeed,W,TDist, xsR, ysR: Integer;
t: UInt64;
PDist,maxStep,dModA,dModB,nModA,nModB: extended;
Event, doExit: Boolean;
begin
tempSpeed := Self.getSpeed();
Event := Assigned(@FOnMouseChangeDestEvent);
TDist := Distance(Round(xs), Round(ys), Round(xe), Round(ye));
if (TDist < 1) then
TDist := 1;
dModA := 0.88;
dModB := 0.95;
if (TDist > 220) then
begin
nModA := 0.08;
nModB := 0.04;
end else if (TDist <= 220) then
begin
nModA := 0.20;
nModB := 0.10;
end;
t := GetTickCount64() + Random(9700, 10000);
repeat
if (GetTickCount64() > t) then
RaiseException(erCustomError, GetGlobalName(@Self) + ' timed out');
if (Event) then
begin
FOnMouseChangeDestEvent(@Self, xe, ye, doExit);
if (doExit) then
Exit;
end;
dist := hypot(xs - xe, ys - ye);
wind := minE(wind, dist);
if (dist < 1) then
dist := 1;
PDist := (dist/TDist);
if (PDist < 0.01) then
PDist := 0.01;
if double then
begin
if (PDist <= dModA) then
begin
D := (round((round(dist)*0.3))/5);
if (D < 20) then
D := 20;
end else if (PDist > dModA) then
begin
if (PDist < dModB) then
D := randomRange(5, 8)
else if (PDist >= dModB) then
D := randomRange(3, 4);
end;
end;
if (PDist >= nModA) then
begin
D := (round((round(dist)*0.3))/5);
if (D < 20) then
D := 20;
end else if (PDist < nModA) then
begin
if (PDist >= nModB) then
D := randomRange(5, 8)
else if (PDist < nModB) then
D := randomRange(3, 4);
end;
if (D <= round(dist)) then
maxStep := D
else
maxStep := round(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;
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(round(maxStep) div 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;
xsR := Round(xs);
ysR := Round(ys);
if (lastX <> xsR) or (lastY <> ysR) then
Self.setPosition([xsR, ysR]);
W := (random((round(100/tempSpeed)))*6);
if (W < 4) then
W := 4;
if (double) then
if (PDist > dModA) then
W := round(W*2.5)
else
W := round(W*1.2);
Wait(W);
lastdist := dist;
until (hypot(xs - xe, ys - ye) < 1);
if (Round(xe) <> Round(xs)) or (Round(ye) <> Round(ys)) then
Self.setPosition([Round(xe), Round(ye)]);
Self.setSpeed(tempSpeed);
end;
{$ENDIF}
{$IFNDEF CODEINSIGHT}
procedure TMouse._MoveHuman(xs, ys, xe, ye, gravity, wind, targetArea: extended);
const
sqrt2 := sqrt(2);
sqrt3 := sqrt(3);
sqrt5 := sqrt(5);
var
veloX,veloY,windX,windY,veloMag,dist,randomDist,lastDist,D: extended;
lastX,lastY, tempSpeed,W,TDist, xsR, ysR: integer;
t: UInt64;
maxStep,rCnc: extended;
Event, doExit: Boolean;
begin
tempSpeed := Self.getSpeed();
Event := Assigned(@FOnMouseChangeDestEvent);
TDist := distance(Round(xs), Round(ys), Round(xe), Round(ye));
t := GetTickCount64() + Random(9700, 10000);
repeat
if (GetTickCount64() > t) then
RaiseException(erCustomError, GetGlobalName(@Self) + ' timed out');
if (Event) then
begin
FOnMouseChangeDestEvent(@Self, xe, ye, doExit);
if (doExit) then
Exit;
end;
dist := hypot(xs - xe, ys - ye);
wind := minE(wind, dist);
if (dist < 1) then
dist := 1;
D := (round((round(TDist)*0.3))/7);
if (D > 25) then
D := 25;
if (D < 5) then
D := 5;
rCnc := random(6);
if (rCnc = 1) then
D := randomRange(2,3);
if (D <= round(dist)) then
maxStep := D
else
maxStep := round(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;
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(round(maxStep) div 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;
xsR := Round(xs);
ysR := Round(ys);
if (lastX <> xsR) or (lastY <> ysR) then
Self.setPosition([xsR, ysR]);
W := (random((round(100/tempSpeed)))*6);
if (W < 4) then
W := 4;
W := round(W * 0.7);
Wait(W);
lastdist := dist;
until (hypot(xs - xe, ys - ye) < 1);
if (Round(xe) <> Round(xs)) or (Round(ye) <> Round(ys)) then
Self.setPosition([Round(xe), Round(ye)]);
Self.setSpeed(tempSpeed);
end;
{$ENDIF}
procedure TMouse.Move(const Target: TPoint);
var
RandSpeed: Extended;
p: TPoint;
begin
RandSpeed := (Random(Self.getSpeed()) / 2.0 + Self.getSpeed()) / 10.0;
p := Self.getPosition();
case (Self.getMode()) of
mouse_Human:
Self._MoveHuman(p.X, p.Y, Target.X, Target.Y, FGravity[Ord(mouse_Human)], FWind[Ord(mouse_Human)], 10.0 * randSpeed);
mouse_Brake:
Self._MoveBrake(p.X, p.Y, Target.X, Target.Y, FGravity[Ord(mouse_Brake)], FWind[Ord(mouse_Brake)], 10.0 * randSpeed, False);
mouse_Accurate:
Self._MoveBrake(p.X, p.Y, Target.X, Target.Y, FGravity[Ord(mouse_Accurate)], FWind[Ord(mouse_Accurate)], 10.0 * randSpeed, False);
end;
end;
procedure TMouse.Move(const X, Y: Integer); overload;
begin
Self.Move(Point(X, Y));
end;
procedure TMouse.Move(const Box: TBox; ForcedMove:Boolean=False; Gauss:Boolean=True); overload;
begin
if not(ForcedMove) and PointInBox(Self.GetPosition(),Box) then
Exit();
if (Gauss) then
Self.Move(srl.RandomPoint(Box))
else
Self.Move(Point(Random(Box.X1, Box.X2), Random(Box.Y1, Box.Y2)));
end;
procedure TMouse.Move(const Middle: TPoint; const Radius: Integer); overload;
begin
Self.Move(srl.RandomPoint(Middle, Radius));
end;
procedure TMouse.Click(const Button: Integer);
var
p: TPoint;
begin
Self.ValidateButton(Button);
p := Self.getPosition();
if (Assigned(@FOnMouseEvent)) then
Self.FOnMouseEvent(@Self, Abs(Abs(not Button) - 2), [], p.X, p.Y);
Self.Hold(Button);
Wait(Random(60, 155));
Self.Release(Button);
end;
procedure TMouse.Click(const Target: TPoint; const Button: Integer); overload;
var
p: TPoint;
begin
Self.ValidateButton(Button);
p := Self.getPosition();
if (p.X <> Target.X) or (p.Y <> Target.Y) then
begin
Self.Move(Target.X, Target.Y);
Wait(16 + Random(70));
end;
Self.Click(Button);
end;
procedure TMouse.Click(const X, Y: Integer; const Button: Integer); overload;
begin
Self.ValidateButton(Button);
Self.Click(Point(X, Y), Button);
end;
procedure TMouse.Click(const Box: TBox; const Button: Integer; ForcedMove:Boolean=False); overload;
begin
if not(ForcedMove) and PointInBox(Self.GetPosition(),Box) then
begin
Self.Click(Button);
Exit();
end;
Self.Click(srl.RandomPoint(Box), Button);
end;
procedure TMouse.Click(const Middle: TPoint; const Radius, Button: Integer); overload;
begin
Self.Click(srl.RandomPoint(Middle, Radius), Button);
end;
procedure TMouse.Miss(const Target: TPoint; const Correct: Boolean = False; const WaitAfterMiss: Integer = 0);
var
randSpeed: Extended;
x2,y2,tempSpeed,dist,mp, a: integer;
p: TPoint;
begin
a := Self.getSpeed();
dist := Distance(p.X, p.Y, Target.X, Target.Y);
mp := round(dist / 150);
if (mp < 0) then
mp := 1;
randSpeed := (Random(a) / 2.0 + a) / 10.0;
Self.Move(Random(Target.x-(a*mp), Target.x+(a*mp)), Random(Target.y-(a*mp), Target.y+(a*mp)));
if (Correct) then
begin
Wait(WaitAfterMiss);
Self.Move(Target);
end;
end;
procedure TMouse.DragTo(const Target: TPoint; const Button: Integer = mouse_Left);
begin
Self.ValidateButton(Button);
Self.Hold(Button);
Self.Move(Target);
Self.Release(Button);
end;
procedure TMouse.Scroll(Scrolls: Integer; Down: Boolean);
var
i,step,k:Int32=1;
p:TPoint;
downInt, upInt: Integer;
begin
{$IFDEF LINUX}
downInt := 1; upInt := -1;
{$ELSE}
{$IFDEF SMART}
downInt := 1; upInt := -1;
{$ELSE}
downInt := -1; upInt := 1;
{$ENDIF}
{$ENDIF}
p := Self.GetPosition();
step := Round(srl.GaussRand(6.0,0.6));
for i:=1 to Scrolls do
begin
if Down then
ScrollMouse(p.x, p.y, downInt)
else
ScrollMouse(p.x, p.y, upInt);
if k mod step = 0 then
begin
step := Round(srl.GaussRand(6.0,0.6));
srl.NormalWait(215,410);
k := 0;
end else
srl.NormalWait(25,46);
Inc(k);
end;
end;
procedure TMouse.Scroll(Target: TPoint; scrolls: Integer; Down: Boolean); overload;
begin
Self.Move(Target);
Self.Scroll(Scrolls, Down);
end;
procedure TMouse.Scroll(Box: TBox; scrolls: Integer; Down: Boolean); overload;
begin
Self.Move(Box);
Self.Scroll(Scrolls, Down);
end;
var
Mouse: TMouse;
begin
Mouse.__Setup();
end;