/
Api.cs
299 lines (240 loc) · 10.8 KB
/
Api.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
using ItemExtensions.Additions.Clumps;
using ItemExtensions.Models;
using ItemExtensions.Models.Enums;
using Microsoft.Xna.Framework;
using StardewValley;
namespace ItemExtensions;
public interface IApi
{
/// <summary>
/// Checks for resource data with the Stone type.
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
bool IsStone(string id);
/// <summary>
/// Checks for resource data in the mod.
/// </summary>
/// <param name="id">Qualified item ID</param>
/// <param name="health">MinutesUntilReady value</param>
/// <param name="itemDropped">Item dropped by ore</param>
/// <returns>Whether the object has ore data.</returns>
bool IsResource(string id, out int? health, out string itemDropped);
/// <summary>
/// Checks mod's menu behaviors. If a target isn't provided, it'll search whether any exist.
/// </summary>
/// <param name="qualifiedItemId">Qualified item ID.</param>
/// <param name="target">Item to search behavior for. (Qualified item ID)</param>
/// <returns>Whether this item has menu behavior for target.</returns>
bool HasBehavior(string qualifiedItemId, string target);
/// <summary>
/// Checks for a qualified id in modded clump data.
/// </summary>
/// <param name="qualifiedItemId">Qualified item ID.</param>
/// <returns>Whether this id is a clump's.</returns>
bool IsClump(string qualifiedItemId);
/// <summary>
/// Tries to spawn a clump.
/// </summary>
/// <param name="itemId">The clump ID.</param>
/// <param name="position">Tile position.</param>
/// <param name="locationName">Location name or unique name.</param>
/// <param name="error">Error string, if applicable.</param>
/// <param name="avoidOverlap">Avoid overlapping with other clumps.</param>
/// <returns>Whether spawning succeeded.</returns>
bool TrySpawnClump(string itemId, Vector2 position, string locationName, out string error, bool avoidOverlap = false);
/// <summary>
/// Tries to spawn a clump.
/// </summary>
/// <param name="itemId">The clump ID.</param>
/// <param name="position">Tile position.</param>
/// <param name="location">Location to use.</param>
/// <param name="error">Error string, if applicable.</param>
/// <param name="avoidOverlap">Avoid overlapping with other clumps.</param>
/// <returns>Whether spawning succeeded.</returns>
bool TrySpawnClump(string itemId, Vector2 position, GameLocation location, out string error, bool avoidOverlap = false);
/// <summary>
/// Checks custom mixed seeds.
/// </summary>
/// <param name="itemId">The 'main seed' ID.</param>
/// <param name="includeSource">Include the main seed's crop in calculation.</param>
/// <param name="parseConditions">Whether to pase GSQs before adding to list.</param>
/// <returns>All possible seeds.</returns>
List<string> GetCustomSeeds(string itemId, bool includeSource, bool parseConditions = true);
/// <summary>
/// Gets drops for a clump.
/// </summary>
/// <param name="clump">The clump instance.</param>
/// <param name="parseConditions">Whether to pase GSQs before adding to list.</param>
/// <returns>All possible drops, with %.</returns>
Dictionary<string,double> GetClumpDrops(ResourceClump clump, bool parseConditions = false);
/// <summary>
/// Gets drops for a node.
/// </summary>
/// <param name="clump">The node instance.</param>
/// <param name="parseConditions">Whether to pase GSQs before adding to list.</param>
/// <returns>All possible drops, with %.</returns>
Dictionary<string,double> GetObjectDrops(Object node, bool parseConditions = false);
}
//remove all of this ↓ when copying to your mod
public class Api : IApi
{
public bool IsStone(string id)
{
if (string.IsNullOrWhiteSpace(id))
return false;
if (!ModEntry.Ores.TryGetValue(id, out var resource))
return false;
if (resource is null || resource == new ResourceData())
return false;
return resource.Type == CustomResourceType.Stone;
}
public bool IsResource(string id, out int? health, out string itemDropped)
{
health = null;
itemDropped = null;
if (string.IsNullOrWhiteSpace(id))
return false;
if (!ModEntry.Ores.TryGetValue(id, out var resource))
return false;
if (resource is null || resource == new ResourceData())
return false;
health = resource.Health;
itemDropped = resource.ItemDropped;
return true;
}
public bool HasBehavior(string qualifiedItemId, string target = null)
{
if(string.IsNullOrWhiteSpace(target))
return ModEntry.MenuActions.ContainsKey(qualifiedItemId);
if (!ModEntry.MenuActions.TryGetValue(qualifiedItemId, out var value))
return false;
var behavior = value.Find(b => b.TargetId == target);
return behavior != null;
}
public bool IsClump(string qualifiedItemId) => ModEntry.BigClumps.ContainsKey(qualifiedItemId);
public bool TrySpawnClump(string itemId, Vector2 position, string locationName, out string error, bool avoidOverlap = false) => TrySpawnClump(itemId, position, Utility.fuzzyLocationSearch(locationName), out error, avoidOverlap);
public bool TrySpawnClump(string itemId, Vector2 position, GameLocation location, out string error, bool avoidOverlap = true)
{
error = null;
if (string.IsNullOrWhiteSpace(itemId))
{
error = "Id can't be null.";
return false;
}
if(ModEntry.BigClumps.TryGetValue(itemId, out var data) == false)
{
error = "Couldn't find the given ID.";
return false;
}
var clump = ExtensionClump.Create(itemId, data, position);
try
{
if(avoidOverlap)
{
if (location.IsTileOccupiedBy(position))
{
var width = location.map.DisplayWidth / 64;
var height = location.map.DisplayHeight / 64;
for (var i = 0; i < 30; i++)
{
var newPosition = new Vector2(
Game1.random.Next(1, width),
Game1.random.Next(1, height));
if (location.IsTileOccupiedBy(newPosition) || location.IsNoSpawnTile(newPosition) || !location.CanItemBePlacedHere(newPosition))
continue;
clump.Tile = newPosition;
}
}
}
location.resourceClumps.Add(clump);
}
catch (Exception ex)
{
error = $"{ex}";
return false;
}
return true;
}
public List<string> GetCustomSeeds(string itemId, bool includeSource, bool parseConditions = true)
{
if (string.IsNullOrWhiteSpace(itemId))
{
return null;
}
//if no seed data
if (ModEntry.Seeds.TryGetValue(itemId, out var seeds) == false)
return null;
var result = new List<string>();
foreach (var mixedSeed in seeds)
{
if (string.IsNullOrWhiteSpace(mixedSeed.Condition))
continue;
if (GameStateQuery.CheckConditions(mixedSeed.Condition, Game1.player.currentLocation, Game1.player))
result.Add(mixedSeed.ItemId);
}
return result;
}
public Dictionary<string,(double, int)> GetClumpDrops(Clump clump, bool parseConditions = false)
{
var result = new Dictionary<string,(double, int)>();
var location = Game1.player.currentLocation;
var who = Game1.player;
var context = new ItemQueryContext(location, who, Game1.random);
if (node is null)
return result;
if(clump.modData.TryGetValue(ModKeys.ClumpId, out var id) == false)
return result;
if (!ModEntry.BigClumps.TryGetValue(id, out var resource))
return result;
if (resource is null || resource == new ResourceData())
return result;
if(string.IsNullOrWhiteSpace(drop.Condition) == false)
{
result.Add(resource.ItemDropped, (1, Game1.random.Next(resource.MinDrops, resource.MaxDrops)));
}
foreach(var drop in resource.ExtraItems)
{
if(parseConditions && string.IsNullOrWhiteSpace(drop.Condition) == false && GameStateQuery.CheckConditions(drop.Condition, location, who) == false)
continue;
var itemQuery = ItemQueryResolver.TryResolve(item, context, item.Filter, item.AvoidRepeat);
foreach (var result in itemQuery)
{
var parsedItem = ItemRegistry.Create(result.Item.QualifiedItemId, result.Item.Stack, result.Item.Quality);
parsedItem.Stack *= multiplier;
result.Add(parsedItem.QualifiedItemId, (drop.Chance, Game1.random.Next(resource.MinStack, resource.MaxStack)));
}
}
return result;
}
public Dictionary<string,(double, int)> GetObjectDrops(Object node, bool parseConditions = false)
{
var result = new Dictionary<string,(double, int)>();
var location = Game1.player.currentLocation;
var who = Game1.player;
var context = new ItemQueryContext(location, who, Game1.random);
if (node is null)
return result;
if (!ModEntry.Ores.TryGetValue(node.QualifiedItemId, out var resource))
return result;
if (resource is null || resource == new ResourceData())
return result;
if(string.IsNullOrWhiteSpace(drop.Condition) == false)
{
result.Add(resource.ItemDropped, (1, Game1.random.Next(resource.MinDrops, resource.MaxDrops)));
}
foreach(var drop in resource.ExtraItems)
{
if(parseConditions && string.IsNullOrWhiteSpace(drop.Condition) == false && GameStateQuery.CheckConditions(drop.Condition, location, who) == false)
continue;
var itemQuery = ItemQueryResolver.TryResolve(item, context, item.Filter, item.AvoidRepeat);
foreach (var result in itemQuery)
{
var parsedItem = ItemRegistry.Create(result.Item.QualifiedItemId, result.Item.Stack, result.Item.Quality);
parsedItem.Stack *= multiplier;
result.Add(parsedItem.QualifiedItemId, (drop.Chance, Game1.random.Next(resource.MinStack, resource.MaxStack)));
}
}
return result;
}
}