forked from ApryllForever/PolyamorySweetLove
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PathFindControllerPatches.cs
81 lines (75 loc) · 3.05 KB
/
PathFindControllerPatches.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
using StardewValley;
using StardewModdingAPI;
using StardewValley.Locations;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
namespace PolyamorySweetLove
{
public class PathFindControllerPatches
{
private static IMonitor Monitor;
private static ModConfig Config;
private static IModHelper Helper;
// call this method from your Entry class
public static void Initialize(IMonitor monitor, ModConfig config, IModHelper helper)
{
Monitor = monitor;
Config = config;
Helper = helper;
}
public static void PathFindController_Prefix(Character c, GameLocation location, ref Point endPoint)
{
try
{
if (!Config.EnableMod || !(c is NPC) || !(c as NPC).isVillager() || !(c as NPC).isMarried() || !(location is FarmHouse) || endPoint == (location as FarmHouse).getEntryLocation())
return;
if (ModEntry.IsInBed(location as FarmHouse, new Rectangle(endPoint.X * 64, endPoint.Y * 64, 64, 64)))
{
Point point = ModEntry.GetSpouseBedEndPoint(location as FarmHouse, c.Name);
if(point.X < 0 || point.Y < 0)
{
Monitor.Log($"Error setting bed endpoint for {c.Name}", LogLevel.Warn);
}
else
{
endPoint = point;
Monitor.Log($"Moved {c.Name} bed endpoint to {endPoint}");
}
}
else if (IsColliding(c, location, endPoint))
{
var point = (location as FarmHouse).getRandomOpenPointInHouse(Game1.random);
if(point != Point.Zero)
{
endPoint = point;
Monitor.Log($"Moved {c.Name} endpoint to random point {endPoint}");
}
}
}
catch (Exception ex)
{
Monitor.Log($"Failed in {nameof(PathFindController_Prefix)}:\n{ex}", LogLevel.Error);
}
}
private static bool IsColliding(Character c, GameLocation location, Point endPoint)
{
Monitor.Log($"Checking {c.Name} endpoint in farmhouse");
using (IEnumerator<Character> characters = location.characters.GetEnumerator())
{
while (characters.MoveNext())
{
if(characters.Current != c)
{
if(characters.Current.TilePoint == endPoint || (characters.Current is NPC && (characters.Current as NPC).controller?.endPoint == endPoint))
{
Monitor.Log($"{c.Name} endpoint {endPoint} collides with {characters.Current.Name}");
return true;
}
}
}
}
return false;
}
}
}