-
Notifications
You must be signed in to change notification settings - Fork 6
/
AssWorld.cs
304 lines (275 loc) · 8.81 KB
/
AssWorld.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
using AssortedCrazyThings.Base;
using AssortedCrazyThings.Base.Netcode.Packets;
using AssortedCrazyThings.NPCs;
using AssortedCrazyThings.NPCs.Harvester;
using AssortedCrazyThings.Tiles;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using System.IO;
using Terraria;
using Terraria.Audio;
using Terraria.Chat;
using Terraria.Graphics.Effects;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
namespace AssortedCrazyThings
{
[Content(ConfigurationSystem.AllFlags)]
public class AssWorld : AssSystem
{
//Announcements
public record struct Announcement(LocalizedText Text, Color Color)
{
public void Announce(params object[] args)
{
if (Main.netMode == NetmodeID.SinglePlayer)
{
Main.NewText(Text.Format(args), Color);
}
else if (Main.netMode == NetmodeID.Server)
{
ChatHelper.BroadcastChatMessage(NetworkText.FromKey(Text.Key, args), Color);
}
}
/// <summary>
/// When <see cref="Announce"/> can't be used due to server not existing in the context
/// </summary>
public void AnnounceClient(params object[] args)
{
if (Main.netMode == NetmodeID.SinglePlayer)
{
Main.NewText(Text.Format(args), Color);
}
else if (Main.netMode == NetmodeID.MultiplayerClient)
{
new RequestChatMessagePacket(NetworkText.FromKey(Text.Key, args), Color).Send();
}
}
}
public static Announcement SigilOfTheWingDeath { get; private set; }
public static Announcement SoulHarvesterDeath { get; private set; }
public static Announcement SoulHarvesterBabyAppear { get; private set; }
public static Announcement SoulHarvesterBabyCageAppear { get; private set; }
public static Announcement SoulHarvesterBossAppear { get; private set; }
public static Announcement SpawnOfOcramAppear { get; private set; }
public static Announcement MegalodonAppear { get; private set; }
public static Announcement BigEnemyDisappear { get; private set; }
//basically "if they were alive last update"
public bool megalodonAlive = false;
public bool miniocramAlive = false;
//"are they alive this update"
bool isMegalodonSpawned;
bool isMiniocramSpawned;
//Soul stuff
public static bool downedHarvester;
public static bool slimeRainSky = false;
private Announcement RegisterAnnouncement(string name, Color color)
{
return new Announcement(Mod.GetLocalization($"Announcements.{name}"), color);
}
public override void OnModLoad()
{
var appearColor = new Color(175, 75, 255);
var harvesterColor = new Color(35, 200, 254);
SigilOfTheWingDeath = RegisterAnnouncement(nameof(SigilOfTheWingDeath), new Color(225, 25, 25));
SoulHarvesterDeath = RegisterAnnouncement(nameof(SoulHarvesterDeath), harvesterColor);
SoulHarvesterBossAppear = RegisterAnnouncement(nameof(SoulHarvesterBossAppear), appearColor);
SoulHarvesterBabyAppear = RegisterAnnouncement(nameof(SoulHarvesterBabyAppear), harvesterColor);
SoulHarvesterBabyCageAppear = RegisterAnnouncement(nameof(SoulHarvesterBabyCageAppear), harvesterColor);
SpawnOfOcramAppear = RegisterAnnouncement(nameof(SpawnOfOcramAppear), appearColor);
MegalodonAppear = RegisterAnnouncement(nameof(MegalodonAppear), appearColor);
BigEnemyDisappear = RegisterAnnouncement(nameof(BigEnemyDisappear), new Color(175, 255, 175));
}
public override void OnWorldLoad()
{
downedHarvester = false;
}
public override void SaveWorldData(TagCompound tag)
{
var downed = new List<string>();
if (downedHarvester)
{
downed.Add("harvester");
}
tag.Add("downed", downed);
}
public override void LoadWorldData(TagCompound tag)
{
var downed = tag.GetList<string>("downed");
downedHarvester = downed.Contains("harvester");
}
public override void NetSend(BinaryWriter writer)
{
BitsByte flags = new BitsByte();
flags[0] = downedHarvester;
writer.Write(flags);
}
public override void NetReceive(BinaryReader reader)
{
BitsByte flags = reader.ReadByte();
downedHarvester = flags[0];
}
public static void AwakeningMessage(Announcement announcement, Vector2? pos, SoundStyle? soundStyle, params object[] args)
{
//Sound only in singleplayer
if (soundStyle != null) SoundEngine.PlaySound(soundStyle.Value, pos);
announcement.Announce(args);
}
public static void Message(string message, Color color)
{
if (Main.netMode == NetmodeID.SinglePlayer)
{
Main.NewText(message, color);
}
else if (Main.netMode == NetmodeID.Server)
{
ChatHelper.BroadcastChatMessage(NetworkText.FromLiteral(message), color);
}
}
public static void ToggleSlimeRainSky()
{
if (!Main.slimeRain && Main.netMode != NetmodeID.MultiplayerClient)
{
var tile = ModContent.GetInstance<SlimeBeaconTile>();
if (!slimeRainSky)
{
SkyManager.Instance.Activate("Slime", default(Vector2));
CombatText.NewText(Main.LocalPlayer.getRect(), CombatText.HealLife, tile.ActivatedText.ToString());
slimeRainSky = true;
}
else
{
SkyManager.Instance.Deactivate("Slime");
CombatText.NewText(Main.LocalPlayer.getRect(), CombatText.DamagedFriendly, tile.DeactivatedText.ToString());
slimeRainSky = false;
}
}
}
public static void DisableSlimeRainSky()
{
if (!Main.slimeRain && slimeRainSky && Main.netMode != NetmodeID.MultiplayerClient)
{
var tile = ModContent.GetInstance<SlimeBeaconTile>();
SkyManager.Instance.Deactivate("Slime");
CombatText.NewText(Main.LocalPlayer.getRect(), CombatText.DamagedFriendly, tile.DeactivatedText.ToString());
slimeRainSky = false;
}
}
private void LimitSoulCount()
{
if (!ContentConfig.Instance.Bosses)
{
return;
}
if (Main.netMode != NetmodeID.MultiplayerClient)
{
if (Main.GameUpdateCount % 30 == 15 && NPC.CountNPCS(ModContent.NPCType<DungeonSoul>()) > 10) //limit soul count in the world to 15
{
short oldest = 200;
int timeleftmin = int.MaxValue;
for (short j = 0; j < Main.maxNPCs; j++)
{
NPC npc = Main.npc[j];
if (npc.active && npc.type == ModContent.NPCType<DungeonSoul>())
{
if (npc.timeLeft < timeleftmin)
{
timeleftmin = npc.timeLeft;
oldest = j;
}
}
}
if (oldest != Main.maxNPCs)
{
NPC oldestnpc = Main.npc[oldest];
oldestnpc.active = false;
oldestnpc.netUpdate = true;
if (Main.netMode == NetmodeID.Server && oldest < Main.maxNPCs)
{
NetMessage.SendData(MessageID.SyncNPC, -1, -1, null, oldest);
}
//poof visual
for (int i = 0; i < 15; i++)
{
Dust dust = Dust.NewDustPerfect(oldestnpc.Center, 59, new Vector2(Main.rand.NextFloat(-2f, 2f), Main.rand.NextFloat(-2f, 1.5f)), 26, Color.White, Main.rand.NextFloat(1.5f, 2.4f));
dust.noLight = true;
dust.noGravity = true;
dust.fadeIn = Main.rand.NextFloat(0.1f, 0.6f);
}
}
}
}
}
private void UpdateEmpoweringFactor()
{
if (NPC.downedPlantBoss && AssPlayer.empoweringTotal < 1f)
AssPlayer.empoweringTotal = 1f;
else if (Main.hardMode && AssPlayer.empoweringTotal < 0.75f)
AssPlayer.empoweringTotal = 0.75f;
}
public override void ResetNearbyTileEffects()
{
Main.LocalPlayer.GetModPlayer<AssPlayer>().wyvernCampfire = false;
}
public override void PostUpdateWorld()
{
CheckSpawns();
}
private void CheckSpawns()
{
if (!ContentConfig.Instance.HostileNPCs)
{
return;
}
//this code is when I first started modding, terrible stuff
//those flags are checked for trueness each update
isMegalodonSpawned = false;
isMiniocramSpawned = false;
for (short j = 0; j < Main.maxNPCs; j++)
{
NPC npc = Main.npc[j];
if (npc.active)
{
if (npc.type == ModContent.NPCType<Megalodon>() && !isMegalodonSpawned)
{
isMegalodonSpawned = true;
//check if it wasnt alive in previous update
if (!megalodonAlive)
{
AwakeningMessage(MegalodonAppear, npc.Center, SoundID.Roar);
megalodonAlive = true;
}
}
if (npc.type == ModContent.NPCType<SpawnOfOcram>() && !isMiniocramSpawned)
{
isMiniocramSpawned = true;
if (!miniocramAlive)
{
AwakeningMessage(SpawnOfOcramAppear, npc.Center, SoundID.Roar);
miniocramAlive = true;
}
}
}
}
//after this we know that either atleast one miniboss is active or not
//if alive, but not active, print disappear message
if (!isMegalodonSpawned && megalodonAlive)
{
megalodonAlive = false;
BigEnemyDisappear.Announce(ModContent.GetInstance<Megalodon>().DisplayName.ToString());
}
if (!isMiniocramSpawned && miniocramAlive)
{
miniocramAlive = false;
BigEnemyDisappear.Announce(ModContent.GetInstance<SpawnOfOcram>().DisplayName.ToString());
}
}
public override void PreUpdateWorld()
{
LimitSoulCount();
UpdateEmpoweringFactor();
}
}
}