This repository was archived by the owner on Jul 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathMoving.cs
More file actions
860 lines (764 loc) · 17.8 KB
/
Copy pathMoving.cs
File metadata and controls
860 lines (764 loc) · 17.8 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
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
/*
This file is part of PPather.
PPather is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PPather is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PPather. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Globalization;
using System.Collections.Generic;
using System.Threading;
using System.Reflection;
using System.Text;
using Glider.Common.Objects;
using Pather;
using Pather.Graph;
/*
* Classes to move the toon around
*
* Mover is a raw mover controlling what keys are pushed to move around
*
* EasyMover easier access to MoveAlonger
*
* MoveAlonger has the main logic to run along a computed path
*
*/
namespace Pather
{
public class Mover
{
private bool runForwards = false;
private bool runBackwards = false;
private bool strafeLeft = false;
private bool strafeRight = false;
private bool rotateLeft = false;
private bool rotateRight = false;
private const float PI = (float)Math.PI;
private GContext Context;
public Mover(GContext Context)
{
this.Context = Context;
}
private GSpellTimer KeyT = new GSpellTimer(50);
private bool old_runForwards = false;
private bool old_runBackwards = false;
private bool old_strafeLeft = false;
private bool old_strafeRight = false;
private bool old_rotateLeft = false;
private bool old_rotateRight = false;
void PushKeys()
{
if (old_runForwards != runForwards)
{
KeyT.Wait();
KeyT.Reset();
if (runForwards)
PressKey("Common.Forward");
else
ReleaseKey("Common.Forward");
//PPather.WriteLine("Forwards: " + runForwards);
}
/*
if(runForwards)
{
GContext.Main.StartRun(); //
}
else
{
GContext.Main.ReleaseRun(); //
}
*/
if (old_runBackwards != runBackwards)
{
KeyT.Wait();
KeyT.Reset();
if (runBackwards)
PressKey("Common.Back");
else
ReleaseKey("Common.Back");
//PPather.WriteLine("Backwards: " + runBackwards);
}
if (old_strafeLeft != strafeLeft)
{
KeyT.Wait();
KeyT.Reset();
if (strafeLeft)
PressKey("Common.StrafeLeft");
else
ReleaseKey("Common.StrafeLeft");
//PPather.WriteLine("StrageLeft: " + strafeLeft);
}
if (old_strafeRight != strafeRight)
{
KeyT.Wait();
KeyT.Reset();
if (strafeRight)
PressKey("Common.StrafeRight");
else
ReleaseKey("Common.StrafeRight");
//PPather.WriteLine("StrageRight: " + strafeRight);
}
/*
if(rotateRight || rotateLeft)
{
double head = GContext.Main.Me.Heading;
if(rotateRight) head -= Math.PI/2;
else if(rotateLeft) head += Math.PI/2;
GContext.Main.StartSpinTowards(head);
}
else
{
GContext.Main.ReleaseSpin();
}
*/
if (old_rotateLeft != rotateLeft)
{
KeyT.Wait();
KeyT.Reset();
if (rotateLeft)
PressKey("Common.RotateLeft");
else
ReleaseKey("Common.RotateLeft");
//PPather.WriteLine("RotateLeft: " + rotateLeft);
}
if (old_rotateRight != rotateRight)
{
KeyT.Wait();
KeyT.Reset();
if (rotateRight)
PressKey("Common.RotateRight");
else
ReleaseKey("Common.RotateRight");
//PPather.WriteLine("RotateRight: " + rotateRight);
}
old_runForwards = runForwards;
old_runBackwards = runBackwards;
old_strafeLeft = strafeLeft;
old_strafeRight = strafeRight;
old_rotateLeft = rotateLeft;
old_rotateRight = rotateRight;
}
void PressKey(string name)
{
//PPather.WriteLine("Press : " + name);
Context.PressKey(name);
}
void ReleaseKey(string name)
{
//PPather.WriteLine("Release: " + name);
Context.ReleaseKey(name);
}
void SendKey(string name)
{
//PPather.WriteLine("Send: " + name);
Context.SendKey(name);
}
public void Jump()
{
SendKey("Common.Jump");
}
public void SwimUp(bool go)
{
if (go)
PressKey("Common.Jump");
else
ReleaseKey("Common.Jump");
}
public void ResyncKeys()
{
KeyT.ForceReady();
PushKeys();
}
public void MoveRandom()
{
int d = PPather.random.Next(4);
if (d == 0)
Forwards(true);
if (d == 1)
StrafeRight(true);
if (d == 2)
Backwards(true);
if (d == 3)
StrafeLeft(true);
}
public void StrafeLeft(bool go)
{
strafeLeft = go;
if (go)
strafeRight = false;
PushKeys();
}
public void StrafeRight(bool go)
{
strafeRight = go;
if (go)
strafeLeft = false;
PushKeys();
}
public void RotateLeft(bool go)
{
rotateLeft = go;
if (go)
rotateRight = false;
PushKeys();
}
public void RotateRight(bool go)
{
rotateRight = go;
if (go)
rotateLeft = false;
PushKeys();
}
public void Forwards(bool go)
{
runForwards = go;
if (go)
runBackwards = false;
PushKeys();
}
public void Backwards(bool go)
{
runBackwards = go;
if (go)
runForwards = false;
PushKeys();
}
public void StopRotate()
{
rotateLeft = false;
rotateRight = false;
PushKeys();
}
public void StopMove()
{
runForwards = false;
runBackwards = false;
strafeLeft = false;
strafeRight = false;
rotateLeft = false;
rotateRight = false;
SwimUp(false);
PushKeys();
}
public void Stop()
{
StopMove();
StopRotate();
}
public bool IsMoving()
{
return runForwards || runBackwards || strafeLeft || strafeRight;
}
public bool IsRotating()
{
return rotateLeft || rotateRight;
}
public bool IsRotatingLeft()
{
return rotateLeft;
}
public bool IsRotatingRight()
{
return rotateLeft;
}
/*
1 - location is front
2 - location is right
3 - location is back
4 - location is left
*/
int GetLocationDirection(GLocation loc)
{
int dir = 0;
double b = loc.Bearing;
if (b > -PI / 4 && b <= PI / 4) // Front
{
dir = 1;
}
if (b > -3 * PI / 4 && b <= -PI / 4) // Left
{
dir = 4;
}
if (b <= -3 * PI / 4 || b > 3 * PI / 4) // Back
{
dir = 3;
}
if (b > PI / 4 && b <= 3 * PI / 4) // Right
{
dir = 2;
}
if (dir == 0)
PPather.WriteLine("Odd, no known direction");
return dir;
}
public static double GetDistance3D(GLocation l0, GLocation l1)
{
double dx = l0.X - l1.X;
double dy = l0.Y - l1.Y;
double dz = l0.Z - l1.Z;
return Math.Sqrt(dx * dx + dy * dy + dz * dz);
}
public bool moveTowardsFacing(GPlayerSelf Me, GLocation to, double distance, GLocation facing)
{
bool moving = false;
double d = Me.Location.GetDistanceTo(to);
if (d > distance)
{
int dir = GetLocationDirection(to);
if (dir != 0)
moving |= true;
if (dir == 1 || dir == 3 || dir == 0)
{
StrafeLeft(false);
StrafeRight(false);
};
if (dir == 2 || dir == 4 || dir == 0)
{
Forwards(false);
Backwards(false);
};
if (dir == 1)
Forwards(true);
if (dir == 2)
StrafeRight(true);
if (dir == 3)
Backwards(true);
if (dir == 4)
StrafeLeft(true);
//PPather.WriteLine("Move dir: " + dir);
}
else
{
//PPather.WriteLine("Move is close");
StrafeLeft(false);
StrafeRight(false);
Forwards(false);
Backwards(false);
}
double bearing = Me.GetHeadingDelta(facing);
if (bearing < -PI / 8)
{
moving |= true;
RotateLeft(true);
}
else if (bearing > PI / 8)
{
moving |= true;
RotateRight(true);
}
else
StopRotate();
return moving;
}
public double GetMoveHeading(out double speed)
{
double head = GContext.Main.Me.Heading;
double r = 0;
;
speed = 0.0;
if (runForwards)
{
speed = 7.0;
r = head;
if (strafeRight)
r += PI / 2;
if (strafeLeft)
r -= PI / 2;
if (runBackwards)
speed = 0.0;
}
else if (runBackwards)
{
speed = 4.5;
r = head + PI;
if (strafeRight)
r -= PI / 2;
if (strafeLeft)
r += PI / 2;
if (runBackwards)
speed = 0.0;
}
else if (strafeLeft)
{
speed = 7.0;
r = head + PI * 3.0 / 4.0;
if (strafeRight)
speed = 0;
}
else if (strafeRight)
{
speed = 7.0;
r = head + PI / 4;
}
if (head >= 2 * PI)
head -= 2 * PI;
return head;
}
}
public class StuckDetecter
{
GLocation oldLocation = null;
float predictedDX;
float predictedDY;
GSpellTimer StuckTimeout = new GSpellTimer(333); // Check every 333ms
GPlayerSelf Me;
GContext Context;
Mover mover;
int stuckSensitivity;
int abortSensitivity;
int stuckMove = 0;
GSpellTimer lastStuckCheck = new GSpellTimer(0);
bool firstStuckCheck = true;
//GLocation StuckLocation = null;
public StuckDetecter(PPather pather, int stuckSensitivity, int abortSensitivity)
{
this.Me = GContext.Main.Me;
this.Context = GContext.Main;
this.stuckSensitivity = stuckSensitivity;
this.abortSensitivity = abortSensitivity;
this.mover = PPather.mover;
firstStuckCheck = true;
}
public bool checkStuck()
{
if (firstStuckCheck)
{
oldLocation = GContext.Main.Me.Location;
predictedDX = 0;
predictedDY = 0;
firstStuckCheck = false;
lastStuckCheck.Reset();
}
else
{
// update predicted location
double h;
double speed;
h = mover.GetMoveHeading(out speed);
float dt = (float)-lastStuckCheck.TicksLeft / 1000f;
float dx = (float)Math.Cos(h) * (float)speed * dt;
float dy = (float)Math.Sin(h) * (float)speed * dt;
//PPather.WriteLine("speed: " + speed + " dt: " + dt + " dx: " + dx + " dy : " + dy);
predictedDX += dx;
predictedDY += dy;
lastStuckCheck.Reset();
if (StuckTimeout.IsReady)
{
// Check stuck
GLocation loc = Me.Location;
float realDX = loc.X - oldLocation.X;
float realDY = loc.Y - oldLocation.Y;
//PPather.WriteLine(" dx: " + predictedDX + " dy : " + predictedDY + " Real dx: " + realDX + " dy : " + realDY );
float predictDist = (float)Math.Sqrt(predictedDX * predictedDX + predictedDY * predictedDY);
float realDist = (float)Math.Sqrt(realDX * realDX + realDY * realDY);
//PPather.WriteLine(" pd " + predictDist + " rd " + realDist);
int multiplier = 3;
if (GPlayerSelf.Me.IsStealth)
multiplier = 4;
if (predictDist > realDist * multiplier)
{
// moving a lot slower than predicted
// check direction
GLocation excpected = new GLocation(loc.X + predictedDX, loc.Y + predictedDY);
PPather.WriteLine("I am stuck " + stuckMove); //. Jumping to get free");
if (stuckMove == 0)
{
mover.Forwards(false);
mover.Forwards(true);
mover.StrafeLeft(true);
//mover.Jump();
//mover.StrafeRight(false);
}
else if (stuckMove == 1)
{
mover.Forwards(false);
mover.Forwards(true);
mover.StrafeLeft(true);
//PPather.WriteLine(" strafe left");
//mover.Jump();
//mover.StrafeLeft(false);
}
else if (stuckMove == 2)
{
mover.Forwards(false);
mover.Forwards(true);
mover.StrafeRight(true);
//PPather.WriteLine(" strafe left");
//mover.StrafeLeft(true);
}
else if (stuckMove == 2)
{
mover.Forwards(false);
mover.Forwards(true);
mover.StrafeRight(true);
//PPather.WriteLine(" strafe left");
//mover.StrafeLeft(true);
}
stuckMove++;
if (stuckMove >= abortSensitivity)
{
return true;
}
}
else
{
stuckMove = 0;
}
predictedDX = 0;
predictedDY = 0;
oldLocation = loc;
StuckTimeout.Reset();
}
}
return false;
}
}
public class EasyMover
{
Location target;
GPlayerSelf Me;
GContext Context;
PathGraph world = null;
Mover mover;
PPather pather;
bool GiveUpIfStuck = false;
bool GiveUpIfUnsafe = false;
GSpellTimer PathTimeout = new GSpellTimer(5000); // no path can be older than this
MoveAlonger MoveAlong;
public enum MoveResult
{
Reached,
Stuck,
CantFindPath,
Unsafe,
Moving,
GotThere
};
public EasyMover(PPather pather, Location target, bool GiveUpIfStuck,
bool GiveUpIfUnsafe)
{
this.target = target;
this.Me = GContext.Main.Me;
this.Context = GContext.Main;
this.world = PPather.world;
mover = PPather.mover;
this.GiveUpIfStuck = GiveUpIfStuck;
this.GiveUpIfUnsafe = GiveUpIfUnsafe;
this.pather = pather;
}
public void SetPathTimeout(int ms)
{
PathTimeout = new GSpellTimer(ms); // no path can be older than this
}
public void SetNewTarget(Location target)
{
MoveAlong = null;
this.target = target;
}
public MoveResult move()
{
return move(Context.MeleeDistance);
}
public MoveResult move(double howClose)
{
if (GiveUpIfUnsafe)
{
if (!pather.IsItSafeAt(Me.Target, Me.Location))
return MoveResult.Unsafe;
}
if (PathTimeout.IsReady)
{
MoveAlong = null;
}
if (MoveAlong == null)
{
Location from = new Location(Me.Location);
mover.Stop();
Path path = world.CreatePath(from, target, (float)howClose, PPather.radar);
PathTimeout.Reset();
if (path == null || path.Count() == 0)
{
PPather.WriteLine("EasyMover: Can not create path . giving up");
mover.MoveRandom();
Thread.Sleep(200);
mover.Stop();
return MoveResult.CantFindPath;
}
else
{
//PPather.WriteLine("Save path to " + pathfile);
//path.Save(pathfile);
MoveAlong = new MoveAlonger(pather, path);
}
}
if (MoveAlong != null)
{
Location from = new Location(Me.Location);
if (MoveAlong.path.Count() == 0 || from.GetDistanceTo(target) < howClose)
{
//PPather.WriteLine("Move along used up");
MoveAlong = null;
mover.Stop();
return MoveResult.GotThere;
}
else if (!MoveAlong.MoveAlong())
{
MoveAlong = null; // got stuck!
if (GiveUpIfStuck)
return MoveResult.Stuck;
}
}
return MoveResult.Moving;
}
}
public class MoveAlonger
{
public Path path;
StuckDetecter sd;
GPlayerSelf Me;
GContext Context;
Location prev;
Location current;
Location next;
Mover mover;
PathGraph world = null;
int blockCount = 0;
public MoveAlonger(PPather pather, Path path)
{
this.Context = GContext.Main;
this.Me = Context.Me;
this.path = path;
this.world = PPather.world;
mover = PPather.mover;
sd = new StuckDetecter(pather, 1, 2);
prev = null;
current = path.GetFirst();
next = path.GetSecond();
}
public bool MoveAlong()
{
double max = 3.0;
GLocation loc = GContext.Main.Me.Location;
Location isAt = new Location(loc.X, loc.Y, loc.Z);
/*
while (isAt.GetDistanceTo(current) < max && next != null)
{
//PPather.WriteLine(current + " - " + next);
path.RemoveFirst();
if (path.Count() == 0)
{
//PPather.WriteLine("ya");
//return true; // good in some way
}
else
{
prev = current;
current = path.GetFirst();
next = path.GetSecond();
}
}
*/
bool consume = false;
do
{
bool blocked = false;
consume = false;
if (next != null)
world.triangleWorld.IsStepBlocked(loc.X, loc.Y, loc.Z, next.X, next.Y, next.Z,
PathGraph.toonHeight, PathGraph.toonSize, null);
double d = isAt.GetDistanceTo(current);
if ((d < max && !blocked) ||
d < 1.5)
consume = true;
if (consume)
{
//PPather.WriteLine("Consume spot " + current + " d " + d + " block " + blocked);
path.RemoveFirst();
if (path.Count() == 0)
{
break;
}
else
{
prev = current;
current = path.GetFirst();
next = path.GetSecond();
}
}
} while (consume);
{
//PPather.WriteLine("Move towards " + current);
GLocation gto = new GLocation((float)current.X, (float)current.Y, (float)current.Z);
GLocation face;
if (next != null)
face = new GLocation(next.X, next.Y, next.Z);
else
face = gto;
if (!mover.moveTowardsFacing(Me, gto, 0.5, face))
{
PPather.WriteLine("Can't move " + current);
world.BlacklistStep(prev, current);
//world.MarkStuckAt(loc, Me.Heading);
mover.MoveRandom();
Thread.Sleep(500);
mover.Stop();
return false;
// hmm, mover does not want to move, must be up or down
}
{
double h;
double speed;
h = mover.GetMoveHeading(out speed);
float stand_z = 0.0f;
int flags = 0;
float x = isAt.X + (float)Math.Cos(h) * 1.0f;
float y = isAt.Y + (float)Math.Sin(h) * 1.0f;
float z = isAt.Z;
bool aheadOk = world.triangleWorld.FindStandableAt(x, y,
z - 4,
z + 6,
out stand_z, out flags, 0, 0);
if (!aheadOk)
{
blockCount++;
PPather.WriteLine("Heading into a wall or off a cliff " + blockCount);
world.MarkStuckAt(isAt, (float)Me.Heading);
if (prev != null)
{
GLocation gprev = new GLocation((float)prev.X, (float)prev.Y, (float)prev.Z);
if (!mover.moveTowardsFacing(Me, gprev, 0.5, face))
{
mover.Stop();
return false;
}
}
if (blockCount > 1)
{
world.BlacklistStep(prev, current);
return false;
}
return true;
}
else
blockCount = 0;
}
if (sd.checkStuck())
{
PPather.WriteLine("Stuck at " + isAt);
world.MarkStuckAt(isAt, (float)Me.Heading);
world.BlacklistStep(prev, current);
mover.Stop();
return false;
}
}
return true;
}
}
}