-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCore.cs
696 lines (624 loc) · 23.6 KB
/
Core.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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using StardewModdingAPI;
using StardewValley;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Starbot
{
public static class Core
{
public static bool WantsToStop = false;
private static bool MovingDown = false;
private static bool MovingLeft = false;
private static bool MovingRight = false;
private static bool MovingUp = false;
public static bool ShouldBeMoving { get { return MovingDown || MovingLeft || MovingRight || MovingUp; } }
private static bool IsStuck = false;
private static int LastTileX = -3, LastTileY = -3;
private static string LastLocationName = null;
private static int UpdatesSinceCoordinateChange = 0;
private static int LastGameDay = -3;
public static bool IsSleeping = false;
//Objectives
private static bool IsBored = true;
private static Objective Objective = null;
private static List<string> ObjectivesCompletedToday = new List<string>();
public static List<Objective> ObjectivePool = new List<Objective>();
private static void FindNewObjective()
{
if(ObjectivePool.Count == 0)
{
//sleep time
Mod.instance.Monitor.Log("Bot has no remaining objectives for today. Time for bed!", LogLevel.Alert);
Objective = new Objectives.ObjectiveSleep();
Mod.instance.Monitor.Log("New objective: " + Objective.AnnounceMessage, LogLevel.Info);
} else
{
int randomObjective = Mod.RNG.Next(ObjectivePool.Count);
Objective = ObjectivePool[randomObjective];
ObjectivePool.RemoveAt(randomObjective);
Mod.instance.Monitor.Log("New objective: " + Objective.AnnounceMessage, LogLevel.Info);
if (Game1.IsMultiplayer && !Objective.Cooperative)
{
Mod.instance.Helper.Multiplayer.SendMessage<string>(Objective.UniquePoolId, "taskAssigned");
}
}
}
private static void ResetObjectivePool()
{
ObjectivePool.Clear();
ObjectivePool.Add(new Objectives.ObjectiveForage("BusStop"));
ObjectivePool.Add(new Objectives.ObjectiveForage("Beach"));
ObjectivePool.Add(new Objectives.ObjectiveForage("Forest"));
ObjectivePool.Add(new Objectives.ObjectiveForage("Backwoods"));
ObjectivePool.Add(new Objectives.ObjectiveForage("Mountain"));
ObjectivePool.Add(new Objectives.ObjectiveForage("Town"));
ObjectivePool.Add(new Objectives.ObjectiveClearDebris("Farm"));
}
public static void FailObjective()
{
if(Objective != null)
{
Mod.instance.Monitor.Log("Objective failed: " + Objective.AnnounceMessage, LogLevel.Info);
Objective.Fail();
if (Objective.FailureCount < 3) ObjectivePool.Add(Objective);
else
{
Mod.instance.Monitor.Log("Skipping objective for today (too many failures): " + Objective.AnnounceMessage, LogLevel.Info);
}
Objective = null;
IsRouting = false;
IsPathfinding = false;
StopMovingDown();
StopMovingLeft();
StopMovingRight();
StopMovingUp();
}
}
//Routing
public static bool IsRouting = false;
private static bool IsCriticalRoute = false;
private static int RoutingDestinationX = -3, RoutingDestinationY = -3;
private static bool HasRoutingDestination { get { return RoutingDestinationX != -3 && RoutingDestinationY != -3; } }
private static List<string> Route = null;
public static bool RouteTo(string targetMap, int targetX = -3, int targetY = -3, bool critical = false, int localCutoff = -1)
{
if (Game1.player.currentLocation.NameOrUniqueName != targetMap)
{
//Mod.instance.Monitor.Log("Routing to: " + targetMap + (targetY == -1 ? targetX + ", " + targetY : ""), LogLevel.Trace);
//calculate a route to the destination
var route = Routing.GetRoute(targetMap);
if (route == null || route.Count < 2)
{
if (critical)
{
Mod.instance.Monitor.Log("Routing failed: no route!", LogLevel.Warn);
FailObjective();
}
return false;
} else
{
//debug, print route:
//string routeInfo = "Route: ";
//foreach (string s in route) routeInfo += s + ", ";
//Mod.instance.Monitor.Log(routeInfo.Substring(0, routeInfo.Length - 2), LogLevel.Trace);
}
//set the bot's route
IsRouting = true;
IsCriticalRoute = critical;
RoutingDestinationX = targetX;
RoutingDestinationY = targetY;
Route = route;
AdvanceRoute();
return true;
} else if(targetX != -3 && targetY != -3)
{
RoutingDestinationX = targetX;
RoutingDestinationY = targetY;
return PathfindTo(targetX, targetY, critical, false, localCutoff);
}
return false;
}
private static void ClearRoutingDestination()
{
RoutingDestinationX = -3;
RoutingDestinationY = -3;
}
//call on location change
private static void AdvanceRoute()
{
if (!IsRouting) return;
//Mod.instance.Monitor.Log("Advancing route...", LogLevel.Trace);
Route.RemoveAt(0); //remove the current map from the list
if (Route.Count == 0)
{
//route complete
IsRouting = false;
Route = null;
if(HasRoutingDestination)
{
//pathfind to final destination coordinates
PathfindTo(RoutingDestinationX, RoutingDestinationY, IsCriticalRoute);
}
} else
{
//pathfind to the next map
foreach (var w in Game1.player.currentLocation.warps)
{
if (w.TargetName == Route[0])
{
PathfindTo(w.X, w.Y, IsCriticalRoute);
return;
}
}
foreach (var w in Game1.player.currentLocation.doors.Keys)
{
if (Game1.player.currentLocation.doors[w] == Route[0])
{
PathfindTo(w.X, w.Y + 1, IsCriticalRoute, true);
return;
}
}
if (Game1.player.currentLocation is StardewValley.Locations.BuildableGameLocation)
{
StardewValley.Locations.BuildableGameLocation bl = Game1.player.currentLocation as StardewValley.Locations.BuildableGameLocation;
foreach (var b in bl.buildings)
{
if(b.indoors.Value.NameOrUniqueName == Route[0])
{
PathfindTo(b.getPointForHumanDoor().X, b.getPointForHumanDoor().Y + 1, IsCriticalRoute, true);
return;
}
}
}
}
}
//Pathfinding
private static bool IsPathfinding = false;
private static int PathfindingDestinationX = -3, PathfindingDestinationY = -3;
private static bool PathfindingOpenDoor = false;
private static List<Tuple<int,int>> Path = null;
private static bool PathfindTo(int x, int y, bool critical = false, bool openDoor = false, int cutoff = -1)
{
//Mod.instance.Monitor.Log("Pathfinding to: " + x + ", " + y, LogLevel.Trace);
var path = Pathfinder.Pathfinder.FindPath(Game1.player.currentLocation, Game1.player.getTileX(), Game1.player.getTileY(), x, y, cutoff);
if (path == null)
{
if (critical)
{
Mod.instance.Monitor.Log("Pathfinding failed: no path!", LogLevel.Alert);
FailObjective();
}
return false;
}
//set the bot's path
IsPathfinding = true;
PathfindingDestinationX = x;
PathfindingDestinationY = y;
PathfindingOpenDoor = openDoor;
Path = path;
return true;
}
private static void ClearPathfindingDestination()
{
PathfindingDestinationX = -3;
PathfindingDestinationY = -3;
}
private static void AdvancePath()
{
if (!IsPathfinding) return;
if (Path.Count == 0)
{
Path = null;
IsPathfinding = false;
//ClearMoveTarget();
ClearPathfindingDestination();
//Mod.instance.Monitor.Log("Pathfinding complete.", LogLevel.Alert);
if (PathfindingOpenDoor)
{
DoOpenDoor();
}
return;
}
var next = Path[0];
Path.RemoveAt(0);
MoveTo(next.Item1, next.Item2);
}
//Movement
private static bool HasMoveTarget { get { return MoveTargetX != -3 && MoveTargetY != -3; } }
private static int MoveTargetX = -3;
private static int MoveTargetY = -3;
public static void MoveTo(int x, int y)
{
MoveTargetX = x;
MoveTargetY = y;
}
private static void ClearMoveTarget()
{
MoveTargetX = -3;
MoveTargetY = -3;
}
private static int StopX = 0; //a delay on releasing keys so the animation isnt janky from spamming the button on and off
private static int StopY = 0;
private static readonly int StopDelay = 16;
private static void AdvanceMove()
{
float px = Game1.player.Position.X;
float py = Game1.player.Position.Y;
float tx = ((float)MoveTargetX * Game1.tileSize);// + (Game1.tileSize / 2);
float ty = ((float)MoveTargetY * Game1.tileSize) + (Game1.tileSize / 3);
float epsilon = Game1.tileSize * 0.1f;
if (tx - px > epsilon) StartMovingRight();
else if (px - tx > epsilon) StartMovingLeft();
else
{
if (StopX > StopDelay)
{
StopX = 0;
StopMovingRight();
StopMovingLeft();
}
else StopX++;
}
if (ty - py > epsilon) StartMovingDown();
else if (py - ty > epsilon) StartMovingUp();
else
{
if (StopY > StopDelay)
{
StopY = 0;
StopMovingDown();
StopMovingUp();
}
else StopY++;
}
if(Math.Abs(px - tx) < epsilon && Math.Abs(py - ty) < epsilon)
{
ClearMoveTarget();
}
}
public static void Reset()
{
ReleaseKeys();
Routing.Reset();
ClearMoveTarget();
ClearPathfindingDestination();
IsPathfinding = false;
PathfindingOpenDoor = false;
Path = null;
IsRouting = false;
ClearRoutingDestination();
Route = null;
LastTileX = -3;
LastTileY = -3;
LastLocationName = null;
IsStuck = false;
UpdatesSinceCoordinateChange = 0;
WantsToStop = false;
MovingDown = false;
MovingLeft = false;
MovingRight = false;
MovingUp = false;
Objective = null;
ObjectivesCompletedToday.Clear();
ObjectivePool.Clear();
IsBored = false;
LastGameDay = -3;
}
public static void Update()
{
if (!Routing.Ready) return;
Mod.Input.Update();
//cutscenes break it anyway
if (Game1.eventUp)
{
WantsToStop = true;
}
//are we waiting on action button
if (ActionButtonTimer > 0)
{
ActionButtonTimer--;
StopMovingDown();
StopMovingLeft();
StopMovingRight();
StopMovingUp();
if (ActionButtonTimer == 0)
{
StopActionButton();
}
return;
}
//are we waiting on a tool swing
if (SwingToolTimer > 0)
{
SwingToolTimer--;
StopMovingDown();
StopMovingLeft();
StopMovingRight();
StopMovingUp();
if (SwingToolTimer == 0)
{
StopUseTool();
}
return;
}
//only update navigation while navigation is possible
if (Context.CanPlayerMove)
{
//new day
if (LastGameDay != Game1.dayOfMonth)
{
//new day
IsSleeping = false;
ObjectivesCompletedToday.Clear();
Objective = null;
ResetObjectivePool();
LastGameDay = Game1.dayOfMonth;
}
//shh don't wake the bot
if (IsSleeping) return;
//cache player position
int px = Game1.player.getTileX();
int py = Game1.player.getTileY();
//for now, if stuck let's just shut it down
if (IsStuck)
{
WantsToStop = true;
return;
}
//on logical location change
if(Game1.currentLocation.NameOrUniqueName != LastLocationName)
{
if (OpeningDoor)
{
StopActionButton();
OpeningDoor = false;
}
LastLocationName = Game1.currentLocation.NameOrUniqueName;
ClearMoveTarget();
ReleaseKeys();
if(IsRouting && Route[0] == Game1.currentLocation.NameOrUniqueName) AdvanceRoute();
}
if (OpeningDoor)
{
return; //let's not interfere
}
//if we don't have a move target, check the path for one
if (!HasMoveTarget && IsPathfinding)
{
//is pathfinding complete?
if(px == PathfindingDestinationX && py == PathfindingDestinationY)
{
//move to the next node in the path
AdvancePath();
//check for route destination and announce
if (HasRoutingDestination && px == RoutingDestinationX && py == RoutingDestinationY)
{
//Mod.instance.Monitor.Log("Routing complete.", LogLevel.Alert);
ClearMoveTarget();
ClearPathfindingDestination();
ClearRoutingDestination();
ReleaseKeys();
}
} else
{
//move to the next node in the path
AdvancePath();
}
}
if (HasMoveTarget)
{
AdvanceMove();
}
//stuck detection
if (ShouldBeMoving)
{
//on logical tile change
if (px != LastTileX || py != LastTileY)
{
LastTileX = px;
LastTileY = py;
UpdatesSinceCoordinateChange = 0;
}
UpdatesSinceCoordinateChange++;
if (!IsStuck && UpdatesSinceCoordinateChange > 60 * 5) OnStuck();
}
//bored?
if(!HasMoveTarget && !IsPathfinding && !IsRouting)
{
IsBored = true;
}
if (IsBored)
{
if(Objective != null)
{
if (Objective.IsComplete)
{
string objName = Objective.GetType().Name;
Mod.instance.Monitor.Log("Objective completed: " + Objective.AnnounceMessage, LogLevel.Info);
ObjectivesCompletedToday.Add(objName);
Objective = null;
} else
{
IsBored = false;
Objective.Step();
}
} else
{
FindNewObjective();
}
}
} else
{
if(Objective != null && !Objective.IsComplete)
{
Objective.CantMoveUpdate();
}
}
}
public static bool EquipToolIfOnHotbar(string name)
{
var t = Game1.player.getToolFromName(name);
if (t == null)
{
Mod.instance.Monitor.Log("Could not equip tool: " + name + " (not found in inventory)", LogLevel.Info);
return false;
}
for (int index = 0; index < 12; ++index)
{
if (Game1.player.items.Count > index && Game1.player.items.ElementAt<Item>(index) != null)
{
if(Game1.player.items[index] == t)
{
//found it
if(Game1.player.CurrentToolIndex != index)
{
Mod.instance.Monitor.Log("Equipping tool: " + name, LogLevel.Info);
Game1.player.CurrentToolIndex = index;
}
return true;
}
}
}
Mod.instance.Monitor.Log("Could not equip tool: " + name + " (not found on hotbar)", LogLevel.Info);
return false;
}
public static bool OpeningDoor = false;
private static void DoOpenDoor()
{
OpeningDoor = true;
//StartMovingUp();
FaceTile(Game1.player.getTileX(), Game1.player.getTileY() - 1);
StartActionButton();
}
private static void OnStuck()
{
IsStuck = true;
Mod.instance.Monitor.Log("Bot is stuck.", LogLevel.Info);
UpdatesSinceCoordinateChange = 0;
ReleaseKeys();
if(Objective != null)
{
FailObjective();
IsStuck = false;
}
}
private static readonly int ActionButtonTime = 30;
private static int ActionButtonTimer = 0;
public static void DoActionButton(bool stop = true)
{
if (stop) {
StopMovingDown();
StopMovingLeft();
StopMovingRight();
StopMovingUp();
}
StartActionButton();
ActionButtonTimer = ActionButtonTime;
}
private static void StartActionButton()
{
Mod.Input.StartActionButton();
}
private static void StopActionButton()
{
Mod.Input.StopActionButton();
}
private static readonly int SwingToolTime = 30;
private static int SwingToolTimer = 0;
public static void SwingTool()
{
StopMovingDown();
StopMovingLeft();
StopMovingRight();
StopMovingUp();
StartUseTool();
SwingToolTimer = SwingToolTime;
}
private static void StartUseTool()
{
Mod.Input.StartUseTool();
}
private static void StopUseTool()
{
Mod.Input.StopUseTool();
}
public static void FaceTile(int x, int y)
{
var v = new Vector2(((float)x * Game1.tileSize) + (Game1.tileSize / 2f), ((float)y * Game1.tileSize) + (Game1.tileSize / 2f));
Game1.player.faceGeneralDirection(v);
Game1.setMousePosition(Utility.Vector2ToPoint(Game1.GlobalToLocal(v)));
}
private static void StartMovingDown()
{
if (MovingUp) StopMovingUp();
Mod.Input.StartMoveDown();
MovingDown = true;
}
private static void StopMovingDown()
{
Mod.Input.StopMoveDown();
MovingDown = false;
}
private static void StartMovingLeft()
{
if (MovingRight) StopMovingRight();
Mod.Input.StartMoveLeft();
MovingLeft = true;
}
private static void StopMovingLeft()
{
Mod.Input.StopMoveLeft();
MovingLeft = false;
}
private static void StartMovingUp()
{
if (MovingDown) StopMovingDown();
Mod.Input.StartMoveUp();
MovingUp = true;
}
private static void StopMovingUp()
{
Mod.Input.StopMoveUp();
MovingUp = false;
}
private static void StartMovingRight()
{
if (MovingLeft) StopMovingLeft();
Mod.Input.StartMoveRight();
MovingRight = true;
}
private static void StopMovingRight()
{
Mod.Input.StopMoveRight();
MovingRight = false;
}
public static void ReleaseKeys()
{
StopMovingDown();
StopMovingLeft();
StopMovingRight();
StopMovingUp();
}
public static void AnswerGameLocationDialogue(int selection)
{
if(Game1.activeClickableMenu is StardewValley.Menus.DialogueBox)
{
var db = Game1.activeClickableMenu as StardewValley.Menus.DialogueBox;
var responses = (List < Response > )typeof(StardewValley.Menus.DialogueBox).GetField("responses", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(db);
Mod.instance.Monitor.Log("Responding to dialogue with selection: " + responses[selection].responseKey);
Game1.currentLocation.answerDialogue(responses[selection]);
Game1.dialogueUp = false;
if (!Game1.IsMultiplayer)
{
Game1.activeClickableMenu = null;
Game1.playSound("dialogueCharacterClose");
Game1.currentDialogueCharacterIndex = 0;
}
}
}
}
}