From 1697d0105540623ed6f00974ff6bec349c6a01e3 Mon Sep 17 00:00:00 2001 From: Ozzatron Date: Sun, 20 Sep 2020 21:36:27 -0400 Subject: [PATCH 1/4] Tweaked Deus splitting to never hit NPC cap. --- NPCs/CalamityAI.cs | 116 ++++++++++++++++++++++++++++----------------- 1 file changed, 72 insertions(+), 44 deletions(-) diff --git a/NPCs/CalamityAI.cs b/NPCs/CalamityAI.cs index 4dabd1dd0f..bd2e02cdc3 100644 --- a/NPCs/CalamityAI.cs +++ b/NPCs/CalamityAI.cs @@ -2880,56 +2880,84 @@ namespace CalamityMod.NPCs bool despawning = calamityGlobalNPC.newAI[2] == splitAnimationTime; + // + // CODE TWEAKED BY: OZZATRON + // September 20th, 2020 + // reason: fixing Astrum Deus death mode NPC cap bug + // + + // Despawn the unsplit Astrum Deus and spawn two half-size Astrum Deus worms. if (despawning) { - if (!doubleWormPhase) - { - if (Main.netMode != NetmodeID.MultiplayerClient) - { - int startCount = npc.whoAmI + phase1Length + 1; - int npc2 = NPC.NewNPC((int)npc.Center.X, (int)npc.Center.Y, npc.type, startCount); - Main.npc[npc2].Calamity().newAI[0] = 1f; - Main.npc[npc2].velocity = Vector2.Normalize(player.Center - Main.npc[npc2].Center) * 16f; - Main.npc[npc2].timeLeft *= 20; - Main.npc[npc2].netUpdate = true; - if (Main.netMode == NetmodeID.Server) - { - var netMessage = mod.GetPacket(); - netMessage.Write((byte)CalamityModMessageType.SyncCalamityNPCAIArray); - netMessage.Write((byte)npc2); - netMessage.Write(Main.npc[npc2].Calamity().newAI[0]); - netMessage.Write(Main.npc[npc2].Calamity().newAI[1]); - netMessage.Write(Main.npc[npc2].Calamity().newAI[2]); - netMessage.Write(Main.npc[npc2].Calamity().newAI[3]); - netMessage.Send(); - } + // If this is already a split worm (newAI[0] != 0f) then don't do anything. At all. + if (doubleWormPhase) + return; - int npc3 = NPC.NewNPC((int)npc.Center.X, (int)npc.Center.Y, npc.type, startCount + phase2Length + 1); - Main.npc[npc3].Calamity().newAI[0] = 2f; - Main.npc[npc3].Calamity().newAI[3] = 600f; - Main.npc[npc3].velocity = Vector2.Normalize(player.Center - Main.npc[npc3].Center) * 16f; - Main.npc[npc3].timeLeft *= 20; - Main.npc[npc3].netUpdate = true; - if (Main.netMode == NetmodeID.Server) - { - var netMessage = mod.GetPacket(); - netMessage.Write((byte)CalamityModMessageType.SyncCalamityNPCAIArray); - netMessage.Write((byte)npc3); - netMessage.Write(Main.npc[npc3].Calamity().newAI[0]); - netMessage.Write(Main.npc[npc3].Calamity().newAI[1]); - netMessage.Write(Main.npc[npc3].Calamity().newAI[2]); - netMessage.Write(Main.npc[npc3].Calamity().newAI[3]); - netMessage.Send(); - } + // Mark all existing body and tail segments as inactive. This instantly frees up their NPC slots for the freshly spawned worms. + int bodyID = ModContent.NPCType(); + int tailID = ModContent.NPCType(); + for (int i = 0; i < Main.maxNPCs; i++) + { + NPC wormseg = Main.npc[i]; + if (!wormseg.active) + continue; + if (wormseg.type == bodyID || wormseg.type == tailID) + { + wormseg.life = 0; + wormseg.active = false; } } - for (int i = 0; i < Main.maxNPCs; i++) - { - if (i == npc.whoAmI || Main.npc[i].type == ModContent.NPCType() || Main.npc[i].type == ModContent.NPCType()) - Main.npc[i].life = 0; - } + // The unsplit Astrum Deus worm head will die next frame. + npc.life = 0; + // Do not spawn worms client side. The server handles this. + if (Main.netMode != NetmodeID.MultiplayerClient) + { + // Now that the original worm doesn't exist, startCount can be zero. + // int startCount = npc.whoAmI + phase1Length + 1; + int startIndexHeadOne = 1; + int headOneID = NPC.NewNPC((int)npc.Center.X, (int)npc.Center.Y, npc.type, startIndexHeadOne); + Main.npc[headOneID].Calamity().newAI[0] = 1f; + Main.npc[headOneID].velocity = Vector2.Normalize(player.Center - Main.npc[headOneID].Center) * 16f; + Main.npc[headOneID].timeLeft *= 20; + Main.npc[headOneID].netUpdate = true; + + // On server, immediately send the correct extra AI of this head to clients. + if (Main.netMode == NetmodeID.Server) + { + var netMessage = mod.GetPacket(); + netMessage.Write((byte)CalamityModMessageType.SyncCalamityNPCAIArray); + netMessage.Write((byte)headOneID); + netMessage.Write(Main.npc[headOneID].Calamity().newAI[0]); + netMessage.Write(Main.npc[headOneID].Calamity().newAI[1]); + netMessage.Write(Main.npc[headOneID].Calamity().newAI[2]); + netMessage.Write(Main.npc[headOneID].Calamity().newAI[3]); + netMessage.Send(); + } + + // Make sure the second split worm is also contiguous. + int startIndexHeadTwo = startIndexHeadOne + phase2Length + 1; + int headTwoID = NPC.NewNPC((int)npc.Center.X, (int)npc.Center.Y, npc.type, startIndexHeadTwo); + Main.npc[headTwoID].Calamity().newAI[0] = 2f; + Main.npc[headTwoID].Calamity().newAI[3] = 600f; + Main.npc[headTwoID].velocity = Vector2.Normalize(player.Center - Main.npc[headTwoID].Center) * 16f; + Main.npc[headTwoID].timeLeft *= 20; + Main.npc[headTwoID].netUpdate = true; + + // On server, immediately send the correct extra AI of this head to clients. + if (Main.netMode == NetmodeID.Server) + { + var netMessage = mod.GetPacket(); + netMessage.Write((byte)CalamityModMessageType.SyncCalamityNPCAIArray); + netMessage.Write((byte)headTwoID); + netMessage.Write(Main.npc[headTwoID].Calamity().newAI[0]); + netMessage.Write(Main.npc[headTwoID].Calamity().newAI[1]); + netMessage.Write(Main.npc[headTwoID].Calamity().newAI[2]); + netMessage.Write(Main.npc[headTwoID].Calamity().newAI[3]); + netMessage.Send(); + } + } return; } } @@ -2970,7 +2998,7 @@ namespace CalamityMod.NPCs } // Check if other segments are still alive, if not, die - if (npc.type > ModContent.NPCType()) + if (npc.type != ModContent.NPCType()) { bool shouldDespawn = true; for (int i = 0; i < Main.maxNPCs; i++) From 0f801f5b27a868d99ecd61e689f740b45648e7fe Mon Sep 17 00:00:00 2001 From: Ozzatron Date: Sun, 20 Sep 2020 21:43:12 -0400 Subject: [PATCH 2/4] other deus head correctly despawns --- NPCs/AstrumDeus/AstrumDeusHeadSpectral.cs | 24 +++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/NPCs/AstrumDeus/AstrumDeusHeadSpectral.cs b/NPCs/AstrumDeus/AstrumDeusHeadSpectral.cs index 917d7d8f26..f0e490222b 100644 --- a/NPCs/AstrumDeus/AstrumDeusHeadSpectral.cs +++ b/NPCs/AstrumDeus/AstrumDeusHeadSpectral.cs @@ -194,13 +194,7 @@ namespace CalamityMod.NPCs.AstrumDeus } } - public override void BossLoot(ref string name, ref int potionType) - { - if (npc.Calamity().newAI[0] == 0f) - return; - - potionType = ModContent.ItemType(); - } + public override void BossLoot(ref string name, ref int potionType) => potionType = ModContent.ItemType(); public override bool SpecialNPCLoot() { @@ -217,9 +211,23 @@ namespace CalamityMod.NPCs.AstrumDeus public override void NPCLoot() { - if (npc.Calamity().newAI[0] == 0f) + // Unsplit Deus does not drop anything when killed/despawned. + if (npc.Calamity().newAI[0] == 0f) return; + // Killing ANY split Deus makes all other Deus heads die immediately. + for (int i = 0; i < Main.maxNPCs; ++i) + { + NPC otherWormHead = Main.npc[i]; + if (otherWormHead.active && otherWormHead.type == npc.type) + { + // Kill the other worm head after setting it to not drop loot. + otherWormHead.Calamity().newAI[0] = 0f; + otherWormHead.life = 0; + otherWormHead.checkDead(); + } + } + DropHelper.DropBags(npc); DropHelper.DropItem(npc, ItemID.GreaterHealingPotion, 8, 14); From c8a6200d8059b05d1a3ee616e25f5c7090ae4c95 Mon Sep 17 00:00:00 2001 From: Ozzatron Date: Sun, 20 Sep 2020 22:20:57 -0400 Subject: [PATCH 3/4] hardened calamity NPC array netcode --- CalamityNetcode.cs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/CalamityNetcode.cs b/CalamityNetcode.cs index 5033502a83..f505edf221 100644 --- a/CalamityNetcode.cs +++ b/CalamityNetcode.cs @@ -1,4 +1,5 @@ using CalamityMod.Events; +using CalamityMod.NPCs; using CalamityMod.NPCs.NormalNPCs; using CalamityMod.NPCs.Providence; using CalamityMod.TileEntities; @@ -165,13 +166,27 @@ namespace CalamityMod TELabHologramProjector.ReadSyncPacket(mod, reader); break; + // This code has been edited to fail gracefully when trying to provide data for an invalid NPC. case CalamityModMessageType.SyncCalamityNPCAIArray: - byte npcIndex2 = reader.ReadByte(); - Main.npc[npcIndex2].Calamity().newAI[0] = reader.ReadSingle(); - Main.npc[npcIndex2].Calamity().newAI[1] = reader.ReadSingle(); - Main.npc[npcIndex2].Calamity().newAI[2] = reader.ReadSingle(); - Main.npc[npcIndex2].Calamity().newAI[3] = reader.ReadSingle(); + // Read the entire packet regardless of anything + byte npcIdx = reader.ReadByte(); + float ai0 = reader.ReadSingle(); + float ai1 = reader.ReadSingle(); + float ai2 = reader.ReadSingle(); + float ai3 = reader.ReadSingle(); + + // If the NPC in question isn't valid, don't do anything. + NPC npc = Main.npc[npcIdx]; + if (!npc.active) + break; + + CalamityGlobalNPC cgn = npc.Calamity(); + cgn.newAI[0] = ai0; + cgn.newAI[1] = ai1; + cgn.newAI[2] = ai2; + cgn.newAI[3] = ai3; break; + case CalamityModMessageType.ProvidenceDyeConditionSync: byte npcIndex3 = reader.ReadByte(); (Main.npc[npcIndex3].modNPC as Providence).hasTakenDaytimeDamage = reader.ReadBoolean(); From 4ced892c6a986bd728cd34eec86ac95d979cc3f9 Mon Sep 17 00:00:00 2001 From: Ozzatron Date: Sun, 20 Sep 2020 22:50:44 -0400 Subject: [PATCH 4/4] Dom's changes: Levi doesn't flash anymore --- NPCs/Leviathan/Leviathan.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/NPCs/Leviathan/Leviathan.cs b/NPCs/Leviathan/Leviathan.cs index e3ef357480..097f0e4a20 100644 --- a/NPCs/Leviathan/Leviathan.cs +++ b/NPCs/Leviathan/Leviathan.cs @@ -28,11 +28,14 @@ namespace CalamityMod.NPCs.Leviathan int counter = 0; bool initialised = false; int soundDelay = 0; + public static Texture2D AttackTexture = null; public override void SetStaticDefaults() { DisplayName.SetDefault("The Leviathan"); - Main.npcFrameCount[npc.type] = 6; + Main.npcFrameCount[npc.type] = 3; + if (!Main.dedServ) + AttackTexture = ModContent.GetTexture("CalamityMod/NPCs/Leviathan/LeviathanAttack"); } public override void SetDefaults() @@ -95,17 +98,15 @@ namespace CalamityMod.NPCs.Leviathan public override void SendExtraAI(BinaryWriter writer) { writer.Write(npc.dontTakeDamage); - writer.Write(counter); - writer.Write(initialised); writer.Write(soundDelay); + writer.Write(npc.Calamity().newAI[3]); } public override void ReceiveExtraAI(BinaryReader reader) { npc.dontTakeDamage = reader.ReadBoolean(); - counter = reader.ReadInt32(); - initialised = reader.ReadBoolean(); soundDelay = reader.ReadInt32(); + npc.Calamity().newAI[3] = reader.ReadSingle(); } public override void AI() @@ -713,7 +714,7 @@ namespace CalamityMod.NPCs.Leviathan public override bool PreDraw(SpriteBatch spriteBatch, Color drawColor) { - Texture2D texture = ModContent.GetTexture("CalamityMod/NPCs/Leviathan/LeviathanAttack"); + Texture2D texture = AttackTexture; if (npc.ai[0] == 1f || npc.Calamity().newAI[3] < 180f) { texture = Main.npcTexture[npc.type]; @@ -756,7 +757,7 @@ namespace CalamityMod.NPCs.Leviathan npc.frame.Y += height; } - if (counter == Main.npcFrameCount[npc.type]) + if (counter == 6) { counter = 1; npc.frame.Y = 0;