/
GuardianShopHandler.cs
350 lines (333 loc) · 13.8 KB
/
GuardianShopHandler.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Terraria;
namespace giantsummon
{
public class GuardianShopHandler
{
private static List<GuardianShop> Shops = new List<GuardianShop>();
public static GuardianShop CreateShop(int OwnerID, string OwnerModID)
{
GuardianShop shop = new GuardianShop
{
OwnerID = OwnerID,
OwnerModID = OwnerModID
};
Shops.Add(shop);
return shop;
}
public static void SaveShops(Terraria.ModLoader.IO.TagCompound tag)
{
tag.Add("Shop_Count", Shops.Count);
for (int s = 0; s < Shops.Count; s++)
{
string ShopTag = "Shop_s"+s+">";
GuardianShop shop = Shops[s];
tag.Add(ShopTag + "OwnerID", shop.OwnerID);
tag.Add(ShopTag + "OwnerModID", shop.OwnerModID);
tag.Add(ShopTag + "ItemCount", shop.Items.Count);
for (int i = 0; i < shop.Items.Count; i++)
{
string ItemTag = ShopTag + "i" + i + ">";
GuardianShopItem item = shop.Items[i];
bool HasItem = item.ItemID != 0;
tag.Add(ItemTag + "hasitem", HasItem);
if (HasItem)
{
Item dummyItem = new Item();
dummyItem.SetDefaults(item.ItemID, true);
bool IsModItem = dummyItem.modItem != null;
tag.Add(ItemTag + "ismoditem", IsModItem);
if (IsModItem)
{
tag.Add(ItemTag + "itemname", dummyItem.modItem.Name);
tag.Add(ItemTag + "itemmodname", dummyItem.modItem.mod.Name);
}
else
{
tag.Add(ItemTag + "itemid", item.ItemID);
}
tag.Add(ItemTag + "saletime", item.SaleTime);
tag.Add(ItemTag + "timedsaletime", item.TimedSaleTime);
tag.Add(ItemTag + "stack", item.Stack);
}
}
}
}
public static void LoadShops(Terraria.ModLoader.IO.TagCompound tag, int ModVersion)
{
int ShopCount = tag.GetInt("Shop_Count");
for (int s = 0; s < ShopCount; s++)
{
string ShopTag = "Shop_s" + s + ">";
int OwnerID = tag.GetInt(ShopTag + "OwnerID");
string OwnerModID = tag.GetString(ShopTag + "OwnerModID");
GuardianBase gb = GuardianBase.GetGuardianBase(OwnerID, OwnerModID);
GuardianShop shop = GetShop(OwnerID, OwnerModID);
if (shop == null)
{
gb.SetupShop(OwnerID, OwnerModID);
shop = GetShop(OwnerID, OwnerModID);
}
if (shop != null)
{
int ItemCount = tag.GetInt(ShopTag + "ItemCount");
for (int i = 0; i < ItemCount; i++)
{
string ItemTag = ShopTag + "i" + i + ">";
if (tag.GetBool(ItemTag + "hasitem"))
{
bool IsModItem = tag.GetBool(ItemTag + "ismoditem");
string ItemInternalName = null, ItemModInternalName = null;
int ItemID = 0;
if (IsModItem)
{
ItemInternalName = tag.GetString(ItemTag + "itemname");
ItemModInternalName = tag.GetString(ItemTag + "itemmodname");
}
else
{
ItemID = tag.GetInt(ItemTag + "itemid");
}
foreach (GuardianShopItem item in shop.Items)
{
if (item.ItemID == 0)
continue;
if (ItemInternalName != null)
{
Item anotheritem = new Item();
anotheritem.SetDefaults(item.ItemID, true);
if (anotheritem.modItem == null || anotheritem.modItem.Name != ItemInternalName || anotheritem.modItem.mod.Name != ItemModInternalName)
continue;
}
else if (ItemID != item.ItemID)
{
continue;
}
item.SaleTime = tag.GetInt(ItemTag + "saletime");
item.TimedSaleTime = tag.GetInt(ItemTag + "timedsaletime");
item.Stack = tag.GetInt(ItemTag + "stack");
break;
}
}
}
}
}
}
public static GuardianShop GetShop(int OwnerID, string OwnerModID)
{
return GetShop(new GuardianID(OwnerID, OwnerModID));
}
public static GuardianShop GetShop(GuardianID ID)
{
foreach (GuardianShop shop in Shops)
{
if (ID.ID == shop.OwnerID && ID.ModID == shop.OwnerModID)
return shop;
}
return null;
}
public static bool HasShop(GuardianID ID)
{
foreach (GuardianShop shop in Shops)
{
if (ID.ID == shop.OwnerID && ID.ModID == shop.OwnerModID)
return true;
}
return false;
}
public static void UpdateShops()
{
bool PassedAnHour = (int)Main.time % 3600 == 0;
foreach (GuardianShop shop in Shops)
{
foreach (GuardianShopItem item in shop.Items)
{
if (item.disponibility == GuardianShopItem.DisponibilityTime.Timed)
{
if (item.TimedSaleTime > 0)
{
item.TimedSaleTime--;
}
else
{
if (PassedAnHour && Main.rand.NextDouble() < item.TimedSaleChance)
{
item.TimedSaleTime = Main.rand.Next(5, 9) * 3600 + Main.rand.Next(4, 8) * 60;
}
}
}
if (item.SaleTime > 0)
item.SaleTime--;
else if (PassedAnHour && item.IsItemDisponible() && item.SaleTime == 0 && Main.rand.NextDouble() < 0.01f)
{
item.TimedSaleTime = Main.rand.Next(3, 6) * 3600 + Main.rand.Next(10, 26) * 60;
int SaleStack = 1;
if (item.Stack >= 25)
{
SaleStack++;
}
if (item.Stack >= 50)
{
SaleStack++;
}
if (item.Price > 3000)
{
SaleStack++;
}
if (item.Price > 9000)
{
SaleStack++;
}
if (item.Price > 20000)
{
SaleStack++;
}
if (item.Price > 500000)
{
SaleStack++;
}
item.SaleFactor = Main.rand.Next(1, SaleStack) * 5 * 0.01f;
}
if (PassedAnHour && item.Stack > -1 && (!item.UniqueItem || item.Stack == 0))
{
int Difficulty = 1;
if (item.UniqueItem)
Difficulty += 4;
if (item.Price >= 500)
Difficulty++;
if (item.Price >= 5000)
Difficulty++;
if (item.Price >= 50000)
Difficulty++;
if (item.Price >= 500000)
Difficulty++;
if (item.Stack >= 25)
Difficulty++;
if (item.Stack >= 50)
Difficulty++;
if (item.Stack >= 100)
Difficulty++;
if (item.Stack >= 250)
Difficulty++;
if (item.Stack >= 500)
Difficulty++;
if (Main.rand.Next(Difficulty) == 0)
item.Stack++;
}
}
}
}
public class GuardianShop
{
public int OwnerID = 0;
public string OwnerModID = "";
public List<GuardianShopItem> Items = new List<GuardianShopItem>();
public GuardianShopItem AddNewItem(int ItemID, int Price = -1, string Name = "", int FixedSellStack = 1)
{
//Main.NewText("Tried to add ID zero item to shop.", 200, 128, 0);
GuardianShopItem item = new GuardianShopItem();
item.SetItemForSale(ItemID, Price, Name, FixedSellStack);
if(ItemID > 0)
Items.Add(item);
return item;
}
}
public class GuardianShopItem
{
public string ItemName = "";
public int ItemID = 0;
public int Price = 0, Stack = -1, SellStack = 1;
public DisponibilityTime disponibility = DisponibilityTime.Anytime;
public float SaleFactor = 0, TimedSaleChance = 0;
public int SaleTime = 0, TimedSaleTime = 0;
public bool ItemOnDisplay = false;
public bool UniqueItem = false;
public delegate bool DisponibilityDel(Player player);
/// <summary>
/// Use this to set the extra disponibility function.
/// </summary>
public DisponibilityDel GetIfItemIsDisponible = delegate(Player player) { return true; };
public bool CanItemBeVisibleAtTheStore(Player player)
{
return GetIfItemIsDisponible(player) && IsItemDisponible();
}
public bool IsItemDisponible()
{
if (disponibility != DisponibilityTime.Anytime)
{
switch (disponibility)
{
case DisponibilityTime.Day:
return Main.dayTime;
case DisponibilityTime.Bloodmoon:
return Main.bloodMoon;
case DisponibilityTime.Eclipse:
return Main.eclipse;
case DisponibilityTime.EarlyNight:
return !Main.dayTime && Main.time < 16200;
case DisponibilityTime.LateNight:
return !Main.dayTime && Main.time >= 16200;
case DisponibilityTime.EarlyDay:
return Main.dayTime && Main.time < 27000;
case DisponibilityTime.Afternoon:
return Main.dayTime && Main.time >= 27000;
case DisponibilityTime.LunarApocalypse:
return NPC.LunarApocalypseIsUp;
case DisponibilityTime.Night:
return !Main.dayTime;
case DisponibilityTime.Timed:
return TimedSaleTime > 0;
}
}
return true;
}
public void SetLimitedDisponibility(DisponibilityTime disponibility)
{
this.disponibility = disponibility;
}
public void SetToBeRandomlySold(float TimedSaleChance)
{
disponibility = DisponibilityTime.Timed;
this.TimedSaleChance = TimedSaleChance;
}
public void SetItemForSale(int ItemID, int Price = -1, string Name = "", int SellFixedStack = 1)
{
this.ItemID = ItemID;
Item SoldItem = new Item();
SoldItem.SetDefaults(ItemID, true);
UniqueItem = SoldItem.maxStack == 1;
if (Price == -1)
{
this.Price = SoldItem.value;
}
else
{
this.Price = Price;
}
if (Name != "")
ItemName = Name;
else
ItemName = SoldItem.Name;
SellStack = SellFixedStack;
}
public enum DisponibilityTime : byte
{
Anytime,
Timed,
Day,
EarlyDay,
Afternoon,
Night,
EarlyNight,
LateNight,
Bloodmoon,
Eclipse,
LunarApocalypse,
Count
}
}
}
}