-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEventWorld.cs
330 lines (292 loc) · 12.5 KB
/
EventWorld.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Microsoft.Xna.Framework;
using Razorwing.Framework.Threading;
using Terraria;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using TwitchChat.Events;
using TwitchChat.Overrides.Razorwing.Timing;
namespace TwitchChat
{
public class EventWorld : ModWorld
{
public WorldEvent CurrentEvent;
private bool lastDayState;
private int pucd = 1500;
public Scheduler RealtimeScheduler;
internal Action<bool> TickUpdate;
public Scheduler WorldScheduler;
public override TagCompound Save()
{
//Save events cooldown
TagCompound tags = new TagCompound();
try
{
//foreach(var it in TwitchChat.EventsPool)
//{
// cooldowns.Add(it.Key.GetType().Name, it.Value);
//}
foreach (WorldEvent it in TwitchChat.EventsPool) tags.Add($"Event.Cooldown.{it.GetType()}", it.Cd);
}
catch (Exception e)
{
mod.Logger.Error($"Exception caught in {nameof(Load)} when saving events cooldown:\n" +
$"{e.Message}\n" +
$"{e.StackTrace}\n");
}
//return new TagCompound()
//{
// ["EventCooldows"] = cooldowns,
//};
return tags;
}
public override void Initialize()
{
base.Initialize();
if (CurrentEvent != null)
{
CurrentEvent.EventEnd(this, (TwitchChat) mod);
CurrentEvent = null;
}
TickUpdate?.Invoke(false);
//Build new Scheduler based on main thread with clock based on world ticks (kind'a'like we root to game time instead real time schedule)
WorldScheduler = new Scheduler(Thread.CurrentThread, new GameTickClock(this));
RealtimeScheduler = new Scheduler(Thread.CurrentThread);
lastDayState = Main.dayTime;
if (ModLoader.version < new Version(0, 11, 5))
if (Main.netMode == NetmodeID.MultiplayerClient && mod.IsNetSynced)
{
ModPacket p = mod.GetPacket();
p.Write((byte) TwitchChat.NetPacketType.Custom);
p.Write("NetSend");
p.Write(true);
p.Send(256);
}
}
public override void Load(TagCompound tag)
{
if (tag != null && tag.ContainsKey("EventCooldows"))
try
{
//var arr = tag.Get<Dictionary<string, int>>("EventCooldows");
//I know it is shit, very slow shit, but it is terraria what you want? The whole terraria code is shit so shut up
foreach (WorldEvent it in TwitchChat.EventsPool)
foreach (KeyValuePair<string, object> tg in tag)
if (tg.Key == $"Event.Cooldown.{it.GetType()}")
it.Cd = (int) tg.Value;
}
catch (Exception e)
{
mod.Logger.Error($"Exception caught in {nameof(Load)} when reading events cooldown:\n" +
$"{e.Message}\n" +
$"{e.StackTrace}\n");
}
}
public void StartWorldEvent(WorldEvent ev)
{
try
{
if (Main.invasionType > 0 || Main.bloodMoon || Main.eclipse || Main.slimeRain)
return;
CurrentEvent?.EventEnd(this, (TwitchChat) mod);
CurrentEvent = ev;
ev.Cd = ev.Cooldown;
if (!ev.UseWarning || ev.StartDelay == 0)
{
CurrentEvent?.EventStart(this, (TwitchChat) mod);
}
else
{
if (Main.netMode == NetmodeID.Server) // Server
{
NetMessage.BroadcastChatMessage(NetworkText.FromKey(LanguageManager.Instance.GetTextValue(ev.Warning)), ev.WarnColor);
}
else if (Main.netMode == NetmodeID.SinglePlayer) // Single Player
{
Main.NewText(Language.GetTextValue(LanguageManager.Instance.GetTextValue(ev.Warning)), ev.WarnColor);
}
WorldScheduler.AddDelayed(() =>
{
CurrentEvent?.EventStart(this, (TwitchChat) mod); //We need to check because we can get null for now
}, ev.StartDelay);
}
}
catch (Exception e)
{
mod.Logger.Error($"Warning! Exception caught in {nameof(StartWorldEvent)} ! Report mod author with stacktrace:\n" +
$"{e.Message}\n" +
$"{e.StackTrace}\n");
//If we executing it as command, rethrow if
//if (e.StackTrace.Contains(typeof(StartEventCommand).Name))
//{
// throw e;
//}
}
}
//~EventWorld()
//{
// //CurrentEvent.EventEnd(this, (TwitchChat)mod);
//}
public void AddTask(Action task) { WorldScheduler.Add(task); }
public void AddDelayedTask(Action task, double delay) { WorldScheduler.AddDelayed(task, delay); }
public override void NetSend(BinaryWriter writer)
{
// Hope it get fixed in next update
// https://github.com/tModLoader/tModLoader/issues/635
if (ModLoader.version < new Version(0, 11, 5))
return;
WriteNetSendData(writer);
}
internal void WriteNetSendData(BinaryWriter writer)
{
if (CurrentEvent != null)
{
writer.Write(true);
writer.Write(CurrentEvent.GetType().Name);
CurrentEvent.WriteWaveData(ref writer);
}
else
{
writer.Write(false);
}
}
public override void NetReceive(BinaryReader reader)
{
CurrentEvent = null;
if (reader.ReadBoolean())
{
var st = reader.ReadString();
foreach (WorldEvent it in TwitchChat.EventsPool)
if (it.GetType().Name == st)
{
StartWorldEvent(it);
if (it.Type == InvasionType.Invasion)
{
Main.invasionProgressWave = reader.ReadInt32();
Main.invasionSizeStart = reader.ReadInt32();
Main.invasionSize = reader.ReadInt32();
Main.invasionType = reader.ReadInt32();
Main.invasionX = reader.ReadDouble();
Main.invasionProgress = reader.ReadInt32();
}
break;
}
if (CurrentEvent == null) Main.NewTextMultiline($"You use outdated version of mod, were {st} event not registered!", c: Color.Red);
}
}
public override void PostUpdate()
{
if (WorldScheduler == null)
//Mod in bad state and not loaded
Load(null);
TickUpdate?.Invoke(true);
if (CurrentEvent == null)
pucd -= 1;
WorldScheduler?.Update();
RealtimeScheduler?.Update();
try
{
//CurrentEvent?.PerformTick(this, (TwitchChat)mod);
if (Main.netMode != 1 && CurrentEvent == null && TwitchChat.Instance.Fun && (pucd <= 0 || Main.dayTime != lastDayState))
{
pucd = 1500;
foreach (WorldEvent ev in TwitchChat.EventsPool)
{
//Decrease one tick
ev.Cd -= pucd;
if (ev.Cd <= 0)
ev.Cd = 0;
else continue;
if (lastDayState != Main.dayTime && Main.time < 30) //If day transfer to night or rev. Main.time < 30 to not to trigger in midnight when join world
{
if (ev.Condition.HasFlag(TriggerCondition.SwitchTriggered))
if (ev.IsDayStateValid && (ev.ConditionAction?.Invoke() ?? true))
{
if (ev.ChanceAction != null) //In case event has own start logic
{
if (ev.ChanceAction.Invoke())
{
StartWorldEvent(ev);
break;
}
}
else if (Main.rand.Next(1000) > 1000 - 1000 * ev.Chance)
{
StartWorldEvent(ev);
break;
}
}
}
else if (!ev.Condition.HasFlag(TriggerCondition.SwitchTriggered))
{
if (ev.IsDayStateValid && (ev.ConditionAction?.Invoke() ?? true))
{
if (ev.ChanceAction != null) //In case event has own start logic
{
if (ev.ChanceAction.Invoke())
{
StartWorldEvent(ev);
break;
}
}
else if (Main.rand.Next(1000) > 1000 - 1000 * ev.Chance)
{
StartWorldEvent(ev);
break;
}
}
}
ev.Cd = ev.ChanceCooldown;
}
}
else if (CurrentEvent != null && CurrentEvent.IsStarted)
{
if (CurrentEvent.Condition.HasFlag(TriggerCondition.SwitchTriggered) && CurrentEvent.Type != InvasionType.Invasion)
{
if (CurrentEvent.Condition.HasFlag(TriggerCondition.Day) || CurrentEvent.Condition.HasFlag(TriggerCondition.Night))
{
if (lastDayState != Main.dayTime)
{
CurrentEvent.EventEnd(this, (TwitchChat) mod);
CurrentEvent = null;
}
else
{
CurrentEvent.PerformTick(this, (TwitchChat) mod);
}
}
}
else
{
if (CurrentEvent.Type != InvasionType.Invasion)
CurrentEvent.TimeLeft -= 1;
if (CurrentEvent.TimeLeft <= 0 && CurrentEvent.Type != InvasionType.Invasion)
{
CurrentEvent.EventEnd(this, (TwitchChat) mod);
CurrentEvent = null;
}
else
{
CurrentEvent.PerformTick(this, (TwitchChat) mod);
}
}
}
//else if(CurrentEvent!=null && !CurrentEvent.IsStarted)
//{
// CurrentEvent.EventEnd(this, (TwitchChat)mod);
// CurrentEvent = null;
//}
}
catch (Exception e)
{
mod.Logger.Error($"Exception caught in event update! {e.Message}\n{e.StackTrace}\n");
}
lastDayState = Main.dayTime;
}
}
}