-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSmartSpawner.cs
261 lines (209 loc) · 9.2 KB
/
SmartSpawner.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
using Rocket.API;
using Rocket.Core.Plugins;
using UnityEngine;
using Rocket.Core.Commands;
using SDG.Unturned;
using Rocket.Unturned.Chat;
using System.Collections.Generic;
using System;
using Rocket.API.Collections;
namespace SmartSpawner
{
public class SmartSpawner : RocketPlugin<SmartSpawnerConfiguration>
{
public SmartSpawner Instance;
public double spawnChance = Provider.modeConfigData.Items.Spawn_Chance;
private DateTime lastRespawn;
private bool noticedAlready;
protected override void Load()
{
Instance = this;
lastRespawn = DateTime.Now;
Configuration.Load();
}
public void FixedUpdate()
{
if (!Configuration.Instance.isAutosetted)
{
return;
}
if (DateTime.Now >= lastRespawn + TimeSpan.FromSeconds(Configuration.Instance.interval))
{
lastRespawn = DateTime.Now;
RefreshItems();
noticedAlready = false;
if(Configuration.Instance.resetMessageEnabled)
{
UnturnedChat.Say(Translate("items_have_been_respawned"), Color.cyan);
}
}
if (!Configuration.Instance.isAttentionEnabled)
{
return;
}
if (noticedAlready)
{
return;
}
if (DateTime.Now >= lastRespawn + (TimeSpan.FromSeconds(Configuration.Instance.interval) - TimeSpan.FromSeconds(Configuration.Instance.noticeLatency)))
{
UnturnedChat.Say(Translate("items_going_to_respawn", Configuration.Instance.noticeLatency), Color.cyan);
noticedAlready = true;
}
}
const string ssHelp = "Launches SmartSpawner manually or automatically.";
const string ssSyntax = @"<manual|auto (interval in sec|off) [attention countdown]>";
[RocketCommand("ss", ssHelp, ssSyntax, AllowedCaller.Console)]
public void ExecuteSmartSpawner(string[] command)
{
if (command.Length < 1)
{
print(ssHelp);
return;
}
switch (command[0])
{
case "manual":
RefreshItems();
lastRespawn = DateTime.Now;
if(Configuration.Instance.resetMessageEnabled)
{
UnturnedChat.Say(Translate("items_have_been_respawned"), Color.cyan);
}
break;
case "auto":
if (command.Length > 3)
{
print(ssSyntax);
return;
}
if (command[1] == "off")
{
Configuration.Instance.LoadDefaults();
Configuration.Save();
print("Automatic SmartSpawner has been successfully disabled.");
return;
}
double currentInterval = 0.0, currentNoticeLatency = 0.0;
if (Double.TryParse(command[1], out currentInterval))
{
if (currentInterval < 60.0 || currentInterval > 60.0 * 60.0 * 24.0 * 30.0)
{
print("Respawn interval must be in values between 60 and " + (60 * 60 * 24 * 30).ToString() + "!");
return;
}
Configuration.Instance.LoadDefaults();
lastRespawn = DateTime.Now;
Configuration.Instance.isAutosetted = true;
Configuration.Instance.interval = currentInterval;
Configuration.Save();
}
else
{
print(ssSyntax);
return;
}
if (command.Length == 3)
{
if (Double.TryParse(command[2], out currentNoticeLatency))
{
if (currentNoticeLatency < currentInterval)
{
noticedAlready = false;
Configuration.Instance.isAttentionEnabled = true;
Configuration.Instance.noticeLatency = currentNoticeLatency;
Configuration.Save();
print("Automatic SmartSpawner has been successfully enabled with " + currentInterval.ToString() + " sec interval.");
print("Players will be noticed within " + currentNoticeLatency.ToString() + " seconds.");
return;
}
else
{
Configuration.Instance.LoadDefaults();
Configuration.Save();
print("Respawn interval value can't be higher than notice latency.");
return;
}
}
else
{
print(ssSyntax);
return;
}
}
print("Automatic SmartSpawner has been successfully enabled with " + currentInterval.ToString() + " sec interval.");
break;
default:
print(ssSyntax);
return;
}
}
private void RefreshItems()
{
for (byte x = 0; x < Regions.WORLD_SIZE; x++)
{
for (byte y = 0; y < Regions.WORLD_SIZE; y++)
{
ItemRegion currentItemRegion = ItemManager.regions[x, y];
List<ItemData> droppedItems = new List<ItemData>();
if (!Configuration.Instance.clearPlayerItems)
{
if (currentItemRegion.items.Count > 0)
{
for (int i = 0; i < currentItemRegion.items.Count; i++)
{
if (currentItemRegion.items[i].isDropped)
{
droppedItems.Add(currentItemRegion.items[i]);
}
}
}
}
List<int> currentRegionSpawnsIndexes = new List<int>();
for (int i = 0; i < LevelItems.spawns[x, y].Count; i++)
{
currentRegionSpawnsIndexes.Add(i);
}
ItemManager.askClearRegionItems(x, y);
long itemsSpawnedCount = 0;
if (!Configuration.Instance.clearPlayerItems)
{
for (var i = 0; i < droppedItems.Count; i++)
{
ItemManager.dropItem(droppedItems[i].item, droppedItems[i].point, false, true, false);
}
}
if (currentRegionSpawnsIndexes.Count <= 0)
{
continue;
}
while (currentRegionSpawnsIndexes.Count > 0 && (double)LevelItems.spawns[(int)x, (int)y].Count * spawnChance > (double)itemsSpawnedCount)
{
int randomIndex = UnityEngine.Random.Range(0, currentRegionSpawnsIndexes.Count);
ItemSpawnpoint randomSpawn = LevelItems.spawns[x, y][currentRegionSpawnsIndexes[randomIndex]];
currentRegionSpawnsIndexes.RemoveAt(randomIndex);
ushort itemId = LevelItems.getItem(randomSpawn);
if (itemId == 0)
{
continue;
}
var newItem = new Item(itemId, false);
ItemManager.dropItem(newItem, randomSpawn.point, false, false, false);
itemsSpawnedCount++;
}
}
}
}
public override TranslationList DefaultTranslations
{
get
{
return new TranslationList()
{
{"items_have_been_respawned", "All items have been respawned just now."},
{"items_going_to_respawn", "All items will be respawned within {0} seconds."}
};
}
}
}
}