-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathNPCMonitor.cs
319 lines (285 loc) · 11.7 KB
/
NPCMonitor.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
using StardewValley;
using StardewValley.Buildings;
using StardewValley.Characters;
using StardewValley.Locations;
using StardewValleyMP.Packets;
using StardewValleyMP.States;
using System.Collections.Generic;
using System.Linq;
namespace StardewValleyMP
{
class NPCMonitor
{
public static bool ignoreUpdates = false;
private static Dictionary<string, NPCState> npcs = new Dictionary<string, NPCState>();
private static Dictionary<long, FarmAnimalState> animals = new Dictionary<long, FarmAnimalState>();
private static List<long> checkMissing = new List<long>();
public static void startChecks()
{
checkMissing = animals.Keys.ToList();
}
public static void endChecks()
{
foreach (long id in checkMissing)
{
if ( !Multiplayer.COOP )
{
Log.warn("NOT IMPLEMENTED:ANIMAL DELETION");
continue;
}
BuildableGameLocation farm = ( BuildableGameLocation ) Game1.getLocationFromName( "Farm" );
Building buildingAt = null;
foreach (Building building in farm.buildings)
{
if (building.tileX == animals[ id ].homeLoc.X && building.tileY == animals[ id ].homeLoc.Y)
{
buildingAt = building;
break;
}
}
if (buildingAt != null)
{
Log.trace("Sending animal deletion packet");
animals.Remove(id);
Multiplayer.sendFunc(new FarmAnimalPacket(buildingAt, id));
}
}
checkMissing.Clear();
}
public static void check( GameLocation loc )
{
checkNPCs( loc );
if ( loc is Farm )
{
Farm farm = loc as Farm;
foreach (KeyValuePair<long, FarmAnimal> animal in farm.animals)
{
checkAnimal(animal.Value);
if (checkMissing.Contains(animal.Key))
checkMissing.Remove(animal.Key);
}
}
else if ( loc is AnimalHouse )
{
AnimalHouse house = loc as AnimalHouse;
foreach (KeyValuePair<long, FarmAnimal> animal in house.animals)
{
checkAnimal(animal.Value);
if (checkMissing.Contains(animal.Key))
checkMissing.Remove(animal.Key);
}
}
}
public static void reset()
{
npcs.Clear();
animals.Clear();
checkMissing.Clear();
}
public static void updateNPC( string name, NPCState state )
{
NPC npc = Game1.getCharacterFromName(name);
if ( npc == null ) return;
Log.trace("Updating NPC " + name);
Log.trace("Married: " + npc.isMarried() + " -> " + state.married);
Log.trace("Default Map: " + npc.defaultMap + " -> " + state.defaultMap);
Log.trace("Default Pos: (" + npc.DefaultPosition.X + ", " + npc.DefaultPosition.Y + ") -> (" + state.defaultX + " , " + state.defaultY + ")");
npc.setMarried(state.married);
npc.defaultMap = ( state.defaultMap != "" ) ? state.defaultMap : null;
npc.DefaultPosition = new Microsoft.Xna.Framework.Vector2(state.defaultX, state.defaultY);
npcs[name] = state;
}
public static void addAnimal( string loc, FarmAnimal animal )
{
Log.debug("DEBUG:Adding farm animal");
AnimalHouse home = (AnimalHouse) Game1.getLocationFromName(loc);
(home as AnimalHouse).animals.Add(animal.myID, animal);
(home as AnimalHouse).animalsThatLiveHere.Add(animal.myID);
if ( !Multiplayer.COOP )
{
Log.warn("NOT IMPLEMENTED:ANIMAL ADDITION");
return;
}
BuildableGameLocation farm = ( BuildableGameLocation ) Game1.getLocationFromName( "Farm" );
Building buildingAt = null;
foreach (Building building in farm.buildings)
{
if (building.tileX == animal.homeLocation.X && building.tileY == animal.homeLocation.Y)
{
buildingAt = building;
break;
}
}
if (buildingAt != null)
{
animal.home = buildingAt;
}
animals[animal.myID] = new FarmAnimalState( animal );
}
public static void destroyAnimal(long id)
{
Log.debug("DEBUG:Removing farm animal");
foreach (GameLocation loc in Game1.locations)
{
if (!(loc is Farm)) continue;
Farm farm = loc as Farm;
foreach (KeyValuePair<long, FarmAnimal> pair in farm.animals)
{
if (pair.Key == id)
{
destroyAnimal(pair.Value);
farm.animals.Remove(pair.Key);
return;
}
}
foreach (Building building in farm.buildings)
{
AnimalHouse house = building.indoors as AnimalHouse;
if (house != null)
{
foreach (KeyValuePair<long, FarmAnimal> animal in house.animals)
{
if (animal.Key == id)
{
destroyAnimal(animal.Value);
farm.animals.Remove(animal.Key);
return;
}
}
}
}
}
}
private static void destroyAnimal( FarmAnimal animal )
{
AnimalHouse home = (AnimalHouse)animal.home.indoors;
(home as AnimalHouse).animals.Remove(animal.myID);
(home as AnimalHouse).animalsThatLiveHere.Remove(animal.myID);
animals.Remove(animal.myID);
if ( checkMissing.Contains( animal.myID ) )
{
checkMissing.Remove(animal.myID);
}
}
public static void updateAnimal(long id, FarmAnimalState state)
{
foreach ( GameLocation loc in Game1.locations )
{
if (!(loc is Farm)) continue;
Farm farm = loc as Farm;
foreach ( KeyValuePair< long, FarmAnimal > pair in farm.animals )
{
if (pair.Key == id )
{
updateAnimal(farm, pair.Value, state);
return;
}
}
foreach (Building building in farm.buildings)
{
AnimalHouse house = building.indoors as AnimalHouse;
if (house != null)
{
foreach (KeyValuePair<long, FarmAnimal> animal in house.animals)
{
if (animal.Key == id)
{
updateAnimal(farm, animal.Value, state);
return;
}
}
}
}
}
}
private static void updateAnimal(BuildableGameLocation farm, FarmAnimal animal, FarmAnimalState state)
{/*
Log.Async("Updating animal " + animal.myID);
Log.Async("Name: " + animal.name + " -> " + state.name);
Log.Async("Reproduction: " + animal.allowReproduction + " -> " + state.reproduce);
Log.Async("Fullness: " + animal.fullness + " -> " + state.fullness);
Log.Async("Product: " + animal.currentProduce + " -> " + state.product);
Log.Async("Petted: " + animal.wasPet + " -> " + state.pet);
Log.Async("Affection: " + animal.friendshipTowardFarmer + " -> " + state.friendship);
Log.Async("Home: (" + animal.homeLocation.X + ", " + animal.homeLocation.Y + ") -> (" + state.homeLoc.X + ", " + state.homeLoc.Y + ")");
*/
animal.name = state.name;
animal.allowReproduction = state.reproduce;
animal.fullness = (byte)state.fullness;
animal.currentProduce = state.product;
animal.wasPet = state.pet;
animal.friendshipTowardFarmer = state.friendship;
if ( animal.homeLocation.X != state.homeLoc.X && animal.homeLocation.Y != state.homeLoc.Y )
{
Building buildingAt = null;
foreach ( Building building in farm.buildings )
{
if ( building.tileX == state.homeLoc.X && building.tileY == state.homeLoc.Y )
{
buildingAt = building;
break;
}
}
if (buildingAt != null)
{
// From AnimalQueryMenu
(animal.home.indoors as AnimalHouse).animalsThatLiveHere.Remove(animal.myID);
if ((animal.home.indoors as AnimalHouse).animals.ContainsKey(animal.myID))
{
(buildingAt.indoors as AnimalHouse).animals.Add(animal.myID, animal);
(animal.home.indoors as AnimalHouse).animals.Remove(animal.myID);
}
animal.home = buildingAt;
animal.homeLocation = state.homeLoc;
(buildingAt.indoors as AnimalHouse).animalsThatLiveHere.Add(animal.myID);
}
}
animals[animal.myID] = state;
}
private static void checkNPCs(GameLocation loc)
{
foreach (NPC npc in loc.characters)
{
if (npc.name == "Junimo" || npc.name == "Green Slime" || npc.name == "Frost Helly" || npc.IsMonster || npc is Child) continue;
if ( npc.isMarried() && npc.name != Game1.player.spouse )
{
continue;
}
NPCState state = new NPCState(npc);
if (!npcs.ContainsKey(npc.name))
{
npcs.Add(npc.name, state);
continue;
}
NPCState oldState = npcs[npc.name];
if (state.isDifferentEnoughFromOldStateToSend(oldState))
{
npcs[npc.name] = state;
if ( !ignoreUpdates )
Multiplayer.sendFunc(new NPCUpdatePacket(npc));
}
}
}
private static void checkAnimal( FarmAnimal animal )
{
FarmAnimalState state = new FarmAnimalState(animal);
if (!animals.ContainsKey(animal.myID))
{
animals.Add(animal.myID, state);
if (!ignoreUpdates)
{
Log.trace("Sending animal creation packet");
Multiplayer.sendFunc(new FarmAnimalPacket( animal ));
}
return;
}
FarmAnimalState oldState = animals[animal.myID];
if (state.isDifferentEnoughFromOldStateToSend(oldState))
{
animals[animal.myID] = state;
if (!ignoreUpdates)
Multiplayer.sendFunc(new FarmAnimalUpdatePacket(animal));
}
}
}
}