-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSDVX3Mod.cs
364 lines (334 loc) · 17.9 KB
/
SDVX3Mod.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
using System;
using Microsoft.Xna.Framework;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using System.Xml.Serialization;
using System.Linq;
using StardewValley.Monsters;
using StardewValley.Characters;
using StardewValley.Objects;
using StardewValley.TerrainFeatures;
using System.Collections.Generic;
using StardewValley.Buildings;
using StardewValley.Locations;
namespace SDVX3
{
public class SDVX3Mod : Mod
{
internal static SDVX3Mod instance;
public static Texture2D texture;
public const int itemID = 921;
public override void Entry(IModHelper helper)
{
instance = this;
this.Monitor.Log("SDVX3 is loaded.");
//always create paths like so: string path = Path.Combine(helper.DirectoryPath, "assets", "asset.xnb");
//step -1: install code patches
Patcher.InstallPatches();
//step zero: try to add an item
//both Game1.objectInformation and Game1.objectSpriteSheet are loaded by Game1.TranslateFields
//add base object information
Game1.objectInformation.Add(itemID, "CustomItem / 375 / -300 / Basic - 26 / CustomItem / A custom item that isn't loaded properly.");
//load our custom texture
texture = helper.Content.Load<Texture2D>(Path.Combine("Content", "test.png"));
//step one: let's create a deliberate forage spot every day
TimeEvents.AfterDayStarted += TimeEvents_AfterDayStarted;
//debug printy stuff for coords/map id
ControlEvents.KeyReleased += ControlEvents_KeyReleased;
//step two: let's attempt to create a custom crop
this.Monitor.Log("Texture loaded: " + texture.Width);
//step three: hookup custom serializer for SimpleItems
SaveEvents.AfterLoad += SaveEvents_AfterLoad;
}
private void ControlEvents_KeyReleased(object sender, EventArgsKeyPressed e)
{
//debug, press V to max friendship with all aquaintences
if(e.KeyPressed == Microsoft.Xna.Framework.Input.Keys.V)
{
int maxFriend = 10 * 250;
foreach (NPC current in Utility.getAllCharacters())
{
if (Game1.player.friendships.ContainsKey(current.name))
{
Game1.player.friendships[current.name][0] = maxFriend;
} else
{
Game1.player.friendships.Add(current.name, new int[4]);
Game1.player.friendships[current.name][0] = maxFriend;
}
}
}
//debug, press Z to get location/position
if (e.KeyPressed == Microsoft.Xna.Framework.Input.Keys.Z)
{
this.Monitor.Log("Map id: " + Game1.player.currentLocation.Map.Id);
this.Monitor.Log("Map name: " + Game1.player.currentLocation.Name);
//tiles are 64px, per Game1.tileSize
this.Monitor.Log("Map position: " + Game1.player.position.X + " (" + (int)(Game1.player.position.X / Game1.tileSize) + "), " + Game1.player.position.Y + " (" + (int)(Game1.player.position.Y / Game1.tileSize) + ")");
}
}
private void TimeEvents_AfterDayStarted(object sender, EventArgs e)
{
//a couple forage tests - these happen every day for now
//try to spawn a forage in haley's house, just outside her room by the dresser:
//HaleyHouse - 6, 15
SpawnSimpleItem("HaleyHouse", 6, 15, "testObject", 0);
//spawn another item on the farm for easy testing
SpawnSimpleItem("Farm", 61, 15, "testObject", 4);
//SpawnForageLike(Game1.getLocationFromName("HaleyHouse"), new Vector2(6, 15), "testItem", 4);
//SpawnForageLike(Game1.getLocationFromName("Farm"), new Vector2(61, 15), "testItem", 0);
}
public bool SpawnSimpleItem(string location, int x, int y, string modItemId, int quality, bool destructive = false)
{
Spawnpoint spawn = new Spawnpoint(location, x, y);
if (!spawn.isClear())
{
if (!destructive) return false;
spawn.AttemptToClear();
}
SimpleObject item = SimpleItemFactory.CreateItem(spawn, modItemId, quality);
spawn.PlaceItem(item);
return true;
}
private void SaveEvents_AfterReturnToTitle(object sender, EventArgs e)
{
SaveEvents.AfterSave -= SaveEvents_AfterSave;
SaveEvents.BeforeSave -= SaveEvents_BeforeSave;
SaveEvents.AfterReturnToTitle -= SaveEvents_AfterReturnToTitle;
}
private void SaveEvents_AfterSave(object sender, EventArgs e)
{
LoadAndAdd();
}
private void SaveEvents_BeforeSave(object sender, EventArgs e)
{
SaveAndRemove();
}
private void SaveEvents_AfterLoad(object sender, EventArgs e)
{
SaveEvents.BeforeSave += SaveEvents_BeforeSave;
SaveEvents.AfterSave += SaveEvents_AfterSave;
SaveEvents.AfterReturnToTitle += SaveEvents_AfterReturnToTitle;
LoadAndAdd();
}
//custom stuff crashes the serializer.
//soooo. we serialize it ourselves, and remove it from play prior to saving.
public void SaveAndRemove()
{
//check the world for placed items
foreach (GameLocation location in Game1.locations)
{
//check placed objects
Vector2[] keys = location.objects.Keys.ToArray<Vector2>();
for(int ik = 0; ik < keys.Length; ik++)
{
Vector2 key = keys[ik];
if (location.objects[key] is SimpleObject)
{
location.objects[key] = (location.objects[key] as SimpleObject).Pack();
}
else if (location.objects[key] is Chest)
{
for (int i = 0; i < (location.objects[key] as Chest).items.Count; i++)
{
if ((location.objects[key] as Chest).items[i] is SimpleObject)
{
(location.objects[key] as Chest).items[i] = ((location.objects[key] as Chest).items[i] as SimpleObject).Pack();
}
}
}
}
if (location is StardewValley.Locations.BuildableGameLocation bgl)
{
foreach (StardewValley.Buildings.Building building in bgl.buildings)
{
if (building.indoors is GameLocation bl)
{
if (bl.objects is SerializableDictionary<Vector2, StardewValley.Object>)
{
keys = bl.objects.Keys.ToArray<Vector2>();
for (int ik = 0; ik < keys.Length; ik++)
{
Vector2 key = keys[ik];
if (location.objects[key] is SimpleObject)
{
location.objects[key] = (location.objects[key] as SimpleObject).Pack();
}
else if (location.objects[key] is Chest)
{
for (int i = 0; i < (location.objects[key] as Chest).items.Count; i++)
{
if ((location.objects[key] as Chest).items[i] is SimpleObject)
{
(location.objects[key] as Chest).items[i] = ((location.objects[key] as Chest).items[i] as SimpleObject).Pack();
}
}
}
}
}
}
if (building is JunimoHut)
{
for (int i = 0; i < (building as JunimoHut).output.items.Count; i++) {
if((building as JunimoHut).output.items[i] is SimpleObject)
{
(building as JunimoHut).output.items[i] = ((building as JunimoHut).output.items[i] as SimpleObject).Pack();
}
}
}
if (building is Mill)
{
for (int i = 0; i < (building as Mill).output.items.Count; i++)
{
if ((building as Mill).output.items[i] is SimpleObject)
{
(building as Mill).output.items[i] = ((building as Mill).output.items[i] as SimpleObject).Pack();
}
}
for (int i = 0; i < (building as Mill).input.items.Count; i++)
{
if ((building as Mill).input.items[i] is SimpleObject)
{
(building as Mill).input.items[i] = ((building as Mill).input.items[i] as SimpleObject).Pack();
}
}
}
}
}
}
//check various inventories
for(int i = 0; i < Game1.player.items.Count; i++)
{
if(Game1.player.items[i] is SimpleObject)
{
Game1.player.items[i] = (Game1.player.items[i] as SimpleObject).Pack();
}
}
for (int i = 0; i < (Game1.getLocationFromName("FarmHouse") as FarmHouse).fridge.items.Count; i++)
{
if ((Game1.getLocationFromName("FarmHouse") as FarmHouse).fridge.items[i] is SimpleObject)
{
Game1.player.items[i] = ((Game1.getLocationFromName("FarmHouse") as FarmHouse).fridge.items[i] as SimpleObject).Pack();
}
}
for (int i = 0; i < (Game1.getLocationFromName("SeedShop") as SeedShop).itemsFromPlayerToSell.Count; i++)
{
if ((Game1.getLocationFromName("SeedShop") as SeedShop).itemsFromPlayerToSell[i] is SimpleObject)
{
Game1.player.items[i] = ((Game1.getLocationFromName("SeedShop") as SeedShop).itemsFromPlayerToSell[i] as SimpleObject).Pack();
}
}
}
//now we reconstitute our simpleitem class from saved placeholder objects
public void LoadAndAdd()
{
//check the world for placed items
foreach (GameLocation location in Game1.locations)
{
//check placed objects
Vector2[] keys = location.objects.Keys.ToArray<Vector2>();
for (int ik = 0; ik < keys.Length; ik++)
{
Vector2 key = keys[ik];
if (location.objects[key].name.Contains("SDVX3.SimpleObjects"))
{
location.objects[key] = SimpleObject.Unpack(location.objects[key]);
}
else if (location.objects[key] is Chest)
{
for (int i = 0; i < (location.objects[key] as Chest).items.Count; i++)
{
if (((location.objects[key] as Chest).items[i] is StardewValley.Object) && (((location.objects[key] as Chest).items[i] as StardewValley.Object).name.Contains("SDVX3.SimpleObjects")))
{
(location.objects[key] as Chest).items[i] = SimpleObject.Unpack((location.objects[key] as Chest).items[i] as StardewValley.Object);
}
}
}
}
if (location is StardewValley.Locations.BuildableGameLocation bgl)
{
foreach (StardewValley.Buildings.Building building in bgl.buildings)
{
if (building.indoors is GameLocation bl)
{
if (bl.objects is SerializableDictionary<Vector2, StardewValley.Object>)
{
keys = bl.objects.Keys.ToArray<Vector2>();
for (int ik = 0; ik < keys.Length; ik++)
{
Vector2 key = keys[ik];
if (location.objects[key].name.Contains("SDVX3.SimpleObjects"))
{
location.objects[key] = SimpleObject.Unpack(location.objects[key]);
}
else if (location.objects[key] is Chest)
{
for (int i = 0; i < (location.objects[key] as Chest).items.Count; i++)
{
if (((location.objects[key] as Chest).items[i] is StardewValley.Object) && (((location.objects[key] as Chest).items[i] as StardewValley.Object).name.Contains("SDVX3.SimpleObjects")))
{
(location.objects[key] as Chest).items[i] = SimpleObject.Unpack((location.objects[key] as Chest).items[i] as StardewValley.Object);
}
}
}
}
}
}
if (building is JunimoHut)
{
for (int i = 0; i < (building as JunimoHut).output.items.Count; i++)
{
if (((building as JunimoHut).output.items[i] is StardewValley.Object) && ((building as JunimoHut).output.items[i] as StardewValley.Object).name.Contains("SDVX3.SimpleObjects"))
{
(building as JunimoHut).output.items[i] = SimpleObject.Unpack((building as JunimoHut).output.items[i] as StardewValley.Object);
}
}
}
if (building is Mill)
{
for (int i = 0; i < (building as Mill).output.items.Count; i++)
{
if (((building as Mill).output.items[i] is StardewValley.Object) && ((building as Mill).output.items[i] as StardewValley.Object).name.Contains("SDVX3.SimpleObjects"))
{
(building as Mill).output.items[i] = SimpleObject.Unpack((building as Mill).output.items[i] as StardewValley.Object);
}
}
for (int i = 0; i < (building as Mill).input.items.Count; i++)
{
if (((building as Mill).input.items[i] is StardewValley.Object) && ((building as Mill).input.items[i] as StardewValley.Object).name.Contains("SDVX3.SimpleObjects"))
{
(building as Mill).input.items[i] = SimpleObject.Unpack((building as Mill).input.items[i] as StardewValley.Object);
}
}
}
}
}
}
//check various inventories
for (int i = 0; i < Game1.player.items.Count; i++)
{
if (Game1.player.items[i] is StardewValley.Object && (Game1.player.items[i] as StardewValley.Object).name.Contains("SDVX3.SimpleObjects"))
{
Game1.player.items[i] = SimpleObject.Unpack(Game1.player.items[i] as StardewValley.Object);
}
}
for (int i = 0; i < (Game1.getLocationFromName("FarmHouse") as FarmHouse).fridge.items.Count; i++)
{
if ((Game1.getLocationFromName("FarmHouse") as FarmHouse).fridge.items[i] is StardewValley.Object && ((Game1.getLocationFromName("FarmHouse") as FarmHouse).fridge.items[i] as StardewValley.Object).name.Contains("SDVX3.SimpleItems"))
{
(Game1.getLocationFromName("FarmHouse") as FarmHouse).fridge.items[i] = SimpleObject.Unpack((Game1.getLocationFromName("FarmHouse") as FarmHouse).fridge.items[i] as StardewValley.Object);
}
}
for (int i = 0; i < (Game1.getLocationFromName("SeedShop") as SeedShop).itemsFromPlayerToSell.Count; i++)
{
if ((Game1.getLocationFromName("SeedShop") as SeedShop).itemsFromPlayerToSell[i] is StardewValley.Object && ((Game1.getLocationFromName("SeedShop") as SeedShop).itemsFromPlayerToSell[i] as StardewValley.Object).name.Contains("SDVX3.SimpleItems"))
{
(Game1.getLocationFromName("SeedShop") as SeedShop).itemsFromPlayerToSell[i] = SimpleObject.Unpack((Game1.getLocationFromName("SeedShop") as SeedShop).itemsFromPlayerToSell[i] as StardewValley.Object);
}
}
}
}
}