-
Notifications
You must be signed in to change notification settings - Fork 21
/
PathFinding.cs
778 lines (657 loc) · 35.7 KB
/
PathFinding.cs
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using static Enums;
using static MapAreaStruc;
public class PathFinding
{
// Define directions (assuming 4-direction movement)
private int[] dx = { 1, 0, -1, 0, -1, 1, -1, 1 };
private int[] dy = { 0, 1, 0, -1, -1, -1, 1, 1 };
public Form1 Form1_0;
public int TeleportAcceptSize = 20; //not implemented yet
public int AcceptMoveOffset = 25;
public int ThisPlayerAreaID = 0;
public int ThisNextAreaID = 0;
public bool IsMovingToNextArea = false;
public Position LastMovingLocation = new Position { X = 0, Y = 0 };
public Position ThisFinalPosition = new Position { X = 0, Y = 0 };
public Position ThisOffsetPosition = new Position { X = 0, Y = 0 };
public bool[,] ThisCollisionGrid = new bool[0, 0];
public bool IsMovingThruPath = false;
public int CurrentPathIndex = 0;
public List<Point> path = new List<Point>();
public Position PlayerOffsetInCollisiongrid = new Position { X = 0, Y = 0 };
public Position TargetOffsetInCollisiongrid = new Position { X = 0, Y = 0 };
public bool CheckingForCloseToTargetPos = true;
public void SetForm1(Form1 form1_1)
{
Form1_0 = form1_1;
}
public void CheckForBadMapData(int Thisindex)
{
while (Thisindex > Form1_0.MapAreaStruc_0.AllMapData.Count - 1)
{
Form1_0.MapAreaStruc_0.GetMapData(Form1_0.PlayerScan_0.mapSeedValue.ToString(), (Difficulty)Form1_0.PlayerScan_0.difficulty);
}
}
public void DebugMapCollision()
{
Form1_0.ClearDebugCollision();
ThisCollisionGrid = Form1_0.MapAreaStruc_0.CollisionGrid((Enums.Area)Form1_0.comboBoxCollisionArea.SelectedIndex + 1);
for (int i = 0; i < ThisCollisionGrid.GetLength(1); i++)
{
for (int k = 0; k < ThisCollisionGrid.GetLength(0); k++)
{
if (ThisCollisionGrid[k, i]) Form1_0.AppendTextDebugCollision("-");
if (!ThisCollisionGrid[k, i]) Form1_0.AppendTextDebugCollision("X");
}
Form1_0.AppendTextDebugCollision(Environment.NewLine);
}
}
public bool MoveToNextArea(Area ThisID, int AcceptOffset = 4, bool ClearAreaOnMoving = false)
{
Form1_0.PlayerScan_0.GetPositions();
IsMovingToNextArea = true;
ThisNextAreaID = (int)ThisID;
ThisPlayerAreaID = (int)Form1_0.PlayerScan_0.levelNo;
//ThisCollisionGrid = ExpandGrid(ThisID);
ThisCollisionGrid = MergeCollisionGrids(ThisID);
CheckingForCloseToTargetPos = true;
//dump data to txt file
/*string ColisionMapTxt = "";
//ThisCollisionGrid = Form1_0.MapAreaStruc_0.CollisionGrid((Enums.Area)Form1_0.PlayerScan_0.levelNo);
for (int i = 0; i < ThisCollisionGrid.GetLength(1); i++)
{
for (int k = 0; k < ThisCollisionGrid.GetLength(0); k++)
{
if (ThisCollisionGrid[k, i]) ColisionMapTxt += "-";
if (!ThisCollisionGrid[k, i]) ColisionMapTxt += "X";
}
ColisionMapTxt += Environment.NewLine;
}
File.Create(Form1_0.ThisEndPath + "CollisionMap.txt").Dispose();
File.WriteAllText(Form1_0.ThisEndPath + "CollisionMap.txt", ColisionMapTxt);*/
try
{
CheckForBadMapData(ThisPlayerAreaID - 1);
//The Exit or Object find is only for a reference for pathing to the next AreaID, when we enter the next AreaID it will go out of the pathing loop
//Get any 'exit' object in the next areaID for path reference (ignore object name)
ThisFinalPosition = Form1_0.MapAreaStruc_0.GetPositionOfObject("exit", Form1_0.Town_0.getAreaName((int)ThisID), (int)ThisID, new List<int>() { }, true);
//Get any 'object' in the next areaID for path reference in case we didn't find any exit (ignore object name)
if (ThisFinalPosition.X == 0 && ThisFinalPosition.Y == 0) ThisFinalPosition = Form1_0.MapAreaStruc_0.GetPositionOfObject("object", Form1_0.Town_0.getAreaName((int)ThisID), (int)ThisID, new List<int>() { }, true);
//Console.WriteLine("Going to Pos: " + ThisFinalPosition.X + ", " + ThisFinalPosition.Y);
return Form1_0.PathFinding_0.GetPathFinding(AcceptOffset, ClearAreaOnMoving);
}
catch { }
return false;
}
public bool MoveToExit(Area ThisID, int AcceptOffset = 4, bool ClearAreaOnMoving = false)
{
Form1_0.PlayerScan_0.GetPositions();
IsMovingToNextArea = false;
CheckingForCloseToTargetPos = false;
//Form1_0.method_1("ToExit " + Form1_0.Town_0.getAreaName((int)ThisID), Color.Red);
try
{
ThisPlayerAreaID = (int)Form1_0.PlayerScan_0.levelNo;
CheckForBadMapData(ThisPlayerAreaID - 1);
ThisFinalPosition = Form1_0.MapAreaStruc_0.GetPositionOfObject("exit", Form1_0.Town_0.getAreaName((int)ThisID), ThisPlayerAreaID, new List<int>() { });
ThisOffsetPosition = new Position { X = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X, Y = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y };
ThisCollisionGrid = Form1_0.MapAreaStruc_0.CollisionGrid((Enums.Area)Form1_0.PlayerScan_0.levelNo);
try
{
ThisCollisionGrid[ThisFinalPosition.X - ThisOffsetPosition.X, ThisFinalPosition.Y - ThisOffsetPosition.Y] = true; //make sure the exit is walkable
}
catch { }
PlayerOffsetInCollisiongrid = new Position { X = 0, Y = 0 };
TargetOffsetInCollisiongrid = new Position { X = 0, Y = 0 };
//Console.WriteLine("Going to Pos: " + ThisFinalPosition.X + ", " + ThisFinalPosition.Y);
return Form1_0.PathFinding_0.GetPathFinding(AcceptOffset, ClearAreaOnMoving);
}
catch { }
return false;
}
public bool MoveToNPC(string NPCName, int AcceptOffset = 4, bool ClearAreaOnMoving = false)
{
Form1_0.PlayerScan_0.GetPositions();
IsMovingToNextArea = false;
CheckingForCloseToTargetPos = false;
//Form1_0.method_1("ToNPC " + NPCName, Color.Red);
try
{
ThisPlayerAreaID = (int)Form1_0.PlayerScan_0.levelNo;
CheckForBadMapData(ThisPlayerAreaID - 1);
ThisFinalPosition = Form1_0.MapAreaStruc_0.GetPositionOfObject("npc", NPCName, ThisPlayerAreaID, new List<int>() { });
ThisOffsetPosition = new Position { X = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X, Y = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y };
ThisCollisionGrid = Form1_0.MapAreaStruc_0.CollisionGrid((Enums.Area)Form1_0.PlayerScan_0.levelNo);
PlayerOffsetInCollisiongrid = new Position { X = 0, Y = 0 };
TargetOffsetInCollisiongrid = new Position { X = 0, Y = 0 };
return Form1_0.PathFinding_0.GetPathFinding(AcceptOffset, ClearAreaOnMoving);
}
catch { }
return false;
}
public bool MoveToObject(string ObjectName, int AcceptOffset = 4, bool ClearAreaOnMoving = false)
{
Form1_0.PlayerScan_0.GetPositions();
IsMovingToNextArea = false;
CheckingForCloseToTargetPos = false;
//Form1_0.method_1("ToObject " + ObjectName, Color.Red);
try
{
ThisPlayerAreaID = (int)Form1_0.PlayerScan_0.levelNo;
CheckForBadMapData(ThisPlayerAreaID - 1);
ThisFinalPosition = Form1_0.MapAreaStruc_0.GetPositionOfObject("object", ObjectName, ThisPlayerAreaID, new List<int>() { });
ThisOffsetPosition = new Position { X = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X, Y = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y };
ThisCollisionGrid = Form1_0.MapAreaStruc_0.CollisionGrid((Enums.Area)Form1_0.PlayerScan_0.levelNo);
PlayerOffsetInCollisiongrid = new Position { X = 0, Y = 0 };
TargetOffsetInCollisiongrid = new Position { X = 0, Y = 0 };
return Form1_0.PathFinding_0.GetPathFinding(AcceptOffset, ClearAreaOnMoving);
}
catch { }
return false;
}
public bool MoveToThisPos(Position ThisPositionn, int AcceptOffset = 4, bool ClearAreaOnMoving = false)
{
if (Form1_0.PlayerScan_0.levelNo == 0) Form1_0.PlayerScan_0.GetPositions();
IsMovingToNextArea = false;
CheckingForCloseToTargetPos = true;
//Form1_0.method_1("ToThisPos", Color.Red);
try
{
ThisPlayerAreaID = (int)Form1_0.PlayerScan_0.levelNo;
CheckForBadMapData(ThisPlayerAreaID - 1);
ThisFinalPosition = ThisPositionn;
ThisOffsetPosition = new Position { X = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X, Y = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y };
ThisCollisionGrid = Form1_0.MapAreaStruc_0.CollisionGrid((Enums.Area)Form1_0.PlayerScan_0.levelNo);
PlayerOffsetInCollisiongrid = new Position { X = 0, Y = 0 };
TargetOffsetInCollisiongrid = new Position { X = 0, Y = 0 };
return GetPathFinding(AcceptOffset, ClearAreaOnMoving);
}
catch { }
return false;
}
public bool GetPathFinding(int AcceptOffset = 4, bool ClearAreaOnMoving = false)
{
bool MovedCorrectly = false;
//Console.WriteLine("player: " + Form1_0.PlayerScan_0.xPos + ", " + Form1_0.PlayerScan_0.yPos);
//Console.WriteLine("offset: " + ThisOffsetPosition.X + ", " + ThisOffsetPosition.Y);
//Console.WriteLine("final: " + ThisFinalPosition.X + ", " + ThisFinalPosition.Y);
Point startPos = new Point(Form1_0.PlayerScan_0.xPos - ThisOffsetPosition.X, Form1_0.PlayerScan_0.yPos - ThisOffsetPosition.Y);
Point targetPos = new Point(ThisFinalPosition.X - ThisOffsetPosition.X, ThisFinalPosition.Y - ThisOffsetPosition.Y);
//Point startPos = new Point(115, 579);
//Point targetPos = new Point(41, 429);
startPos.X += PlayerOffsetInCollisiongrid.X;
startPos.Y += PlayerOffsetInCollisiongrid.Y;
targetPos.X += TargetOffsetInCollisiongrid.X;
targetPos.Y += TargetOffsetInCollisiongrid.Y;
//no need to move we are close already!
if (CheckingForCloseToTargetPos)
{
if (startPos.X >= (targetPos.X - Form1_0.Mover_0.MoveAcceptOffset)
&& startPos.X <= (targetPos.X + Form1_0.Mover_0.MoveAcceptOffset)
&& startPos.Y >= (targetPos.Y - Form1_0.Mover_0.MoveAcceptOffset)
&& startPos.Y <= (targetPos.Y + Form1_0.Mover_0.MoveAcceptOffset))
{
return true;
}
}
//Form1_0.method_1("Start pos: " + startPos.X + ", " + startPos.Y, Color.Red);
//Form1_0.method_1("Target pos: " + targetPos.X + ", " + targetPos.Y, Color.Red);
/*if (ThisOffsetPosition.X == 0 && ThisOffsetPosition.Y == 0)
{
Form1_0.method_1("Offsets are bad!", Color.Red);
}*/
if (targetPos.X <= 0 || targetPos.Y <= 0)
{
Form1_0.method_1("Target pos are bad: " + targetPos.X + ", " + targetPos.Y, Color.OrangeRed);
return false;
}
path = FindPath(startPos, targetPos);
if (path == null)
{
Form1_0.method_1("No path found.", Color.Red);
//Form1_0.MapAreaStruc_0.DumpMap();
Form1_0.GoToNextScript();
return false;
}
//################################################
//Shorten the path so we don't go at each single unit
int ThisOffsetToUse = AcceptMoveOffset;
if (Form1_0.Town_0.IsInTown) ThisOffsetToUse = 5;
else if (!CharConfig.UseTeleport) ThisOffsetToUse = 5;
else if (CharConfig.UseTeleport && !Form1_0.Town_0.GetInTown() && (Enums.Area)Form1_0.PlayerScan_0.levelNo == Enums.Area.ArcaneSanctuary) ThisOffsetToUse = 1;
//else if(CharConfig.UseTeleport && !Form1_0.Town_0.GetInTown()) ThisOffsetToUse = 1;
List<Point> pathShortened = new List<Point>();
int LastPathAdded = 0;
Point LastPoint = new Point();
for (int i = 0; i < path.Count; i += ThisOffsetToUse)
{
if (ThisOffsetToUse == 1)
{
if (path[i].X >= LastPoint.X - 2 && path[i].X <= LastPoint.X + 2
&& path[i].Y >= LastPoint.Y - 2 && path[i].Y <= LastPoint.Y + 2)
{
LastPoint = path[i];
continue;
}
}
LastPoint = path[i];
pathShortened.Add(path[i]);
LastPathAdded = i;
}
if (LastPathAdded != path.Count - 1) pathShortened.Add(path[path.Count - 1]);
path = pathShortened;
//################################################
IsMovingThruPath = false;
CurrentPathIndex = 0;
int BadPathIndexount = 0;
if (path != null)
{
IsMovingThruPath = true;
while (IsMovingThruPath)
{
Form1_0.PlayerScan_0.GetPositions();
ThisPlayerAreaID = (int)Form1_0.PlayerScan_0.levelNo;
//we are close to accept offset, stop moving to path
if (IsCloseToLocation(ThisFinalPosition, AcceptOffset))
{
IsMovingThruPath = false;
break;
}
//we are within the next area, stop moving to path
if (IsMovingToNextArea)
{
if (ThisPlayerAreaID == ThisNextAreaID)
{
path = null;
CurrentPathIndex = 0;
IsMovingThruPath = false;
break;
}
}
if (CurrentPathIndex > path.Count - 1)
{
IsMovingThruPath = false;
break;
}
DetectCloserPathPositions();
if (CurrentPathIndex < path.Count - 1)
{
Form1_0.Mover_0.MoveAcceptOffset = 7;
}
else
{
Form1_0.Mover_0.MoveAcceptOffset = 4;
}
//Console.WriteLine("Pos test: " + path[CurrentPathIndex].X + ", " + path[CurrentPathIndex].Y);
if (Form1_0.Mover_0.MoveToLocation(path[CurrentPathIndex].X + ThisOffsetPosition.X - PlayerOffsetInCollisiongrid.X, path[CurrentPathIndex].Y + ThisOffsetPosition.Y - PlayerOffsetInCollisiongrid.Y, false, false))
{
BadPathIndexount = 0;
CurrentPathIndex++;
Form1_0.overlayForm.SetPathPoints(path, CurrentPathIndex, ThisOffsetPosition, PlayerOffsetInCollisiongrid);
if (CurrentPathIndex >= path.Count - 1)
{
IsMovingThruPath = false;
}
else
{
if (ClearAreaOnMoving)
{
if (Form1_0.Battle_0.ClearingFullArea && Form1_0.Battle_0.AllRooms_InArea.Count > 0)
{
//Remove the Rooms we just done clearing
Form1_0.Battle_0.RemoveCurrentRoomFromClearing();
}
//Form1_0.method_1("Clearing area of mobs...", Color.Red);
if (Form1_0.Battle_0.ClearAreaOfMobs(path[CurrentPathIndex].X + ThisOffsetPosition.X - PlayerOffsetInCollisiongrid.X, path[CurrentPathIndex].Y + ThisOffsetPosition.Y - PlayerOffsetInCollisiongrid.Y, AcceptMoveOffset + 2))
{
IsMovingToNextArea = true;
IsMovingThruPath = false;
}
}
}
}
else
{
if (path[CurrentPathIndex].X != LastMovingLocation.X || path[CurrentPathIndex].Y != LastMovingLocation.Y)
{
LastMovingLocation.X = path[CurrentPathIndex].X;
LastMovingLocation.Y = path[CurrentPathIndex].Y;
}
else
{
BadPathIndexount++;
CurrentPathIndex++;
Form1_0.overlayForm.SetPathPoints(path, CurrentPathIndex, ThisOffsetPosition, PlayerOffsetInCollisiongrid);
if (BadPathIndexount >= 3)
{
IsMovingThruPath = false;
}
}
}
}
if (!IsMovingToNextArea)
{
if (AcceptOffset == 4) Form1_0.Mover_0.MoveToLocation(ThisFinalPosition.X - PlayerOffsetInCollisiongrid.X, ThisFinalPosition.Y - PlayerOffsetInCollisiongrid.Y);
//int tryyy = 0;
//while (Form1_0.PlayerScan_0.levelNo == ThisPlayerAreaID && tryyy <= 25)
//{
Position itemScreenPos = Form1_0.GameStruc_0.World2Screen(Form1_0.PlayerScan_0.xPosFinal, Form1_0.PlayerScan_0.yPosFinal, ThisFinalPosition.X - PlayerOffsetInCollisiongrid.X, ThisFinalPosition.Y - PlayerOffsetInCollisiongrid.Y);
Form1_0.KeyMouse_0.MouseClicc_RealPos(itemScreenPos.X, itemScreenPos.Y);
Form1_0.PlayerScan_0.GetPositions();
//tryyy++;
//}
}
int FinalX = path[path.Count - 1].X + ThisOffsetPosition.X - PlayerOffsetInCollisiongrid.X;
int FinalY = path[path.Count - 1].Y + ThisOffsetPosition.Y - PlayerOffsetInCollisiongrid.Y;
if (Form1_0.Mover_0.IsPositionNearOf(FinalX, FinalY, AcceptOffset)) MovedCorrectly = true;
}
else
{
Form1_0.method_1("No path found.", Color.Red);
Form1_0.GoToNextScript();
}
return MovedCorrectly;
}
public void DetectCloserPathPositions()
{
if (IsMovingThruPath)
{
int DistX = Form1_0.PlayerScan_0.xPos - path[CurrentPathIndex].X;
int DistY = Form1_0.PlayerScan_0.yPos - path[CurrentPathIndex].Y;
if (DistX < 0) DistX = -DistX;
if (DistY < 0) DistY = -DistY;
int CheckIndexxx = CurrentPathIndex + 1;
int CheckCount = 0;
while (CheckIndexxx < path.Count - 1 && CheckCount < 1)
{
int DistNextX = Form1_0.PlayerScan_0.xPos - path[CheckIndexxx].X;
int DistNextY = Form1_0.PlayerScan_0.yPos - path[CheckIndexxx].Y;
if (DistNextX < 0) DistNextX = -DistNextX;
if (DistNextY < 0) DistNextY = -DistNextY;
if (DistNextX < DistX && DistNextY < DistY)
{
CurrentPathIndex++; //increase pathing index, we are closer to the next destination than the current one!
Form1_0.overlayForm.SetPathPoints(path, CurrentPathIndex, ThisOffsetPosition, PlayerOffsetInCollisiongrid);
}
CheckIndexxx++;
CheckCount++;
}
}
}
public bool[,] MergeCollisionGrids(Enums.Area ThisNewArea)
{
bool[,] CurrentAreaGrid = Form1_0.MapAreaStruc_0.CollisionGrid((Enums.Area)Form1_0.PlayerScan_0.levelNo);
bool[,] NextAreaGrid = Form1_0.MapAreaStruc_0.CollisionGrid(ThisNewArea);
PlayerOffsetInCollisiongrid = new Position { X = 0, Y = 0 };
TargetOffsetInCollisiongrid = new Position { X = 0, Y = 0 };
// Calculate the size of the merged grid
int minX = Math.Min(Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X, Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.X);
int minY = Math.Min(Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y, Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.Y);
int maxX = Math.Max(Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X + Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Size.Width, Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.X + Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Size.Width);
int maxY = Math.Max(Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y + Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Size.Height, Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.Y + Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Size.Height);
int width = maxX - minX;
int height = maxY - minY;
// Create the merged grid
bool[,] mergedGrid = new bool[width, height];
// Copy collision data from map1 to the merged grid
for (int y = 0; y < Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Size.Height; y++)
{
for (int x = 0; x < Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Size.Width; x++)
{
mergedGrid[x - minX + Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X, y - minY + Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y] = CurrentAreaGrid[x, y];
}
}
// Copy collision data from map2 to the merged grid
for (int y = 0; y < Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Size.Height; y++)
{
for (int x = 0; x < Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Size.Width; x++)
{
mergedGrid[x - minX + Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.X, y - minY + Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.Y] = NextAreaGrid[x, y];
}
}
SetOffsets(ThisNewArea);
return mergedGrid;
}
public void SetOffsets(Enums.Area ThisNewArea)
{
/*Point startPos = new Point(Form1_0.PlayerScan_0.xPos - ThisOffsetPosition.X, Form1_0.PlayerScan_0.yPos - ThisOffsetPosition.Y);
Point targetPos = new Point(ThisFinalPosition.X - ThisOffsetPosition.X, ThisFinalPosition.Y - ThisOffsetPosition.Y);
startPos.X += PlayerOffsetInCollisiongrid.X;
startPos.Y += PlayerOffsetInCollisiongrid.Y;
targetPos.X += TargetOffsetInCollisiongrid.X;
targetPos.Y += TargetOffsetInCollisiongrid.Y;*/
PlayerOffsetInCollisiongrid = new Position { X = 0, Y = 0 };
TargetOffsetInCollisiongrid = new Position { X = 0, Y = 0 };
if (Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.Y == Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y + Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Size.Height)
{
//Expend Bottom
//#####
/*if (Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X > Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.X)
{
PlayerOffsetInCollisiongrid.X = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X - Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.X;
TargetOffsetInCollisiongrid.X = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X - Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.X;
}
if (Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X < Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.X)
{
PlayerOffsetInCollisiongrid.X = Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.X - Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X;
TargetOffsetInCollisiongrid.X = Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.X - Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X;
}*/
//#####
ThisOffsetPosition = new Position { X = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X, Y = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y };
}
if (Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.Y == Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y - Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Size.Height)
{
//Expend Up
//#####
/*if (Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X > Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.X)
{
PlayerOffsetInCollisiongrid.X = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X - Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.X;
TargetOffsetInCollisiongrid.X = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X - Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.X;
}
if (Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X < Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.X)
{
PlayerOffsetInCollisiongrid.X = Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.X - Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X;
TargetOffsetInCollisiongrid.X = Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.X - Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X;
}*/
//#####
ThisOffsetPosition = new Position { X = Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.X, Y = Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.Y };
}
if (Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.X == Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X + Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Size.Width)
{
//Expend Right
//#####
/*if (Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y > Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.Y)
{
PlayerOffsetInCollisiongrid.Y = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y - Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.Y;
//TargetOffsetInCollisiongrid.Y = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y - Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.Y;
}
if (Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y < Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.Y)
{
//PlayerOffsetInCollisiongrid.Y = Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.Y - Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y;
TargetOffsetInCollisiongrid.Y = Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.Y - Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y;
}*/
//#####
ThisOffsetPosition = new Position { X = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X, Y = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y };
}
if (Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.X == Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.X - Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Size.Width)
{
//Expend Left
//#####
/*if (Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y > Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.Y)
{
PlayerOffsetInCollisiongrid.Y = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y - Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.Y;
//TargetOffsetInCollisiongrid.Y = Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y - Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.Y;
}
if (Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y < Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.Y)
{
//PlayerOffsetInCollisiongrid.Y = Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.Y - Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y;
TargetOffsetInCollisiongrid.Y = Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.Y - Form1_0.MapAreaStruc_0.AllMapData[ThisPlayerAreaID - 1].Offset.Y;
}*/
//#####
ThisOffsetPosition = new Position { X = Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.X, Y = Form1_0.MapAreaStruc_0.AllMapData[(int)ThisNewArea - 1].Offset.Y };
}
}
public List<Point> FindPath(Point startPos, Point targetPos)
{
int width = ThisCollisionGrid.GetLength(0);
int height = ThisCollisionGrid.GetLength(1);
bool[,] closedSet = new bool[width, height];
List<Node> openSet = new List<Node>();
openSet.Add(new Node(startPos, null, 0, Heuristic(startPos, targetPos)));
try
{
while (openSet.Any())
{
Node current = openSet.OrderBy(node => node.F).First();
if (current.Position.Equals(targetPos)) return ReconstructPath(current);
openSet.Remove(current);
closedSet[current.Position.X, current.Position.Y] = true;
for (int p = 0; p < 8; p++) // Assuming 4-direction movement
{
int nx = current.Position.X + dx[p];
int ny = current.Position.Y + dy[p];
if (nx >= 0 && nx < width && ny >= 0 && ny < height)
{
if (nx < ThisCollisionGrid.GetLength(0) && ny < ThisCollisionGrid.GetLength(1)
&& nx < closedSet.GetLength(0) && ny < closedSet.GetLength(1))
{
if (ThisCollisionGrid[nx, ny])
{
Point neighborPos = new Point(nx, ny);
if (closedSet[nx, ny]) continue;
double tentativeG = current.G + 1;
if (p > 3) tentativeG += 1;
Node neighbor = openSet.FirstOrDefault(node => node.Position.Equals(neighborPos));
if (neighbor == null || tentativeG < neighbor.G)
{
if (neighbor == null)
{
neighbor = new Node(neighborPos);
openSet.Add(neighbor);
}
neighbor.Parent = current;
neighbor.G = tentativeG;
neighbor.H = Heuristic(neighborPos, targetPos);
}
}
}
}
if (CharConfig.UseTeleport && !Form1_0.Town_0.GetInTown() && (Enums.Area)Form1_0.PlayerScan_0.levelNo == Enums.Area.ArcaneSanctuary)
//if (CharConfig.UseTeleport && !Form1_0.Town_0.GetInTown())
{
nx = current.Position.X + (dx[p] * AcceptMoveOffset);
ny = current.Position.Y + (dy[p] * AcceptMoveOffset);
if (nx >= 0 && nx < width && ny >= 0 && ny < height)
{
if (nx < ThisCollisionGrid.GetLength(0) && ny < ThisCollisionGrid.GetLength(1)
&& nx < closedSet.GetLength(0) && ny < closedSet.GetLength(1))
{
if (ThisCollisionGrid[nx, ny])
{
Point neighborPos = new Point(nx, ny);
if (closedSet[nx, ny]) continue;
double tentativeG = current.G + AcceptMoveOffset + 1;
//double tentativeG = current.G + AcceptMoveOffset;
//double tentativeG = current.G + 2;
//if (p > 3) tentativeG += 1;
Node neighbor = openSet.FirstOrDefault(node => node.Position.Equals(neighborPos));
if (neighbor == null || tentativeG < neighbor.G)
{
if (neighbor == null)
{
neighbor = new Node(neighborPos);
openSet.Add(neighbor);
}
neighbor.Parent = current;
neighbor.G = tentativeG;
neighbor.H = Heuristic(neighborPos, targetPos);
}
}
}
}
}
}
}
}
catch
{
Form1_0.method_1("Issue with PathFinding CollisionGrid 'Indexes'!", Color.Red);
}
// No path found
return null;
}
public bool IsCloseToLocation(Position ThissP, int AcceptOffset)
{
bool MovedCorrectly = false;
if (Form1_0.PlayerScan_0.xPosFinal >= (ThissP.X - AcceptOffset)
&& Form1_0.PlayerScan_0.xPosFinal <= (ThissP.X + AcceptOffset)
&& Form1_0.PlayerScan_0.yPosFinal >= (ThissP.Y - AcceptOffset)
&& Form1_0.PlayerScan_0.yPosFinal <= (ThissP.Y + AcceptOffset))
{
MovedCorrectly = true;
}
return MovedCorrectly;
}
public Point TeleportPoint = new Point(0, 0);
private bool IsWalkable(Point point)
{
return point.X >= 0 && point.X < ThisCollisionGrid.GetLength(0) && point.Y >= 0 && point.Y < ThisCollisionGrid.GetLength(1) && ThisCollisionGrid[point.X, point.Y];
}
private double Heuristic(Point a, Point b)
{
return Math.Sqrt(Math.Pow(a.X - b.X, 2) + Math.Pow(a.Y - b.Y, 2));
}
private List<Point> ReconstructPath(Node node)
{
List<Point> path = new List<Point>();
while (node != null)
{
path.Add(node.Position);
node = node.Parent;
}
path.Reverse();
return path;
}
private class Node
{
public Point Position { get; }
public Node Parent { get; set; }
public double G { get; set; } // g score: cost from start to current
public double H { get; set; } // h score: estimated cost from current to target
public double F => G + H; // f score: total estimated cost
public Node(Point position, Node parent = null, double g = 0, double h = 0)
{
Position = position;
Parent = parent;
G = g;
H = h;
}
}
public struct Point
{
public int X { get; set; }
public int Y { get; set; }
public Point(int x, int y)
{
X = x;
Y = y;
}
public override bool Equals(object obj)
{
if (!(obj is Point))
return false;
var other = (Point)obj;
return X == other.X && Y == other.Y;
}
public override int GetHashCode()
{
return X.GetHashCode() ^ Y.GetHashCode();
}
}
}