From e1b623729e3d77c0450c1887e300f901ec81ecfa Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 29 Feb 2020 13:52:50 -0600 Subject: [PATCH] huge reoptimization, fixed a few bugs in the process --- BankHandler.cs | 35 +++++++------ BetterTaxes.cs | 1 + Commands/InfoCommand.cs | 15 ++---- Commands/SetTaxesCommand.cs | 32 ++++++++---- GateParser.cs | 11 ++-- Localization/en-US.lang | 1 + Localization/es-ES.lang | 1 + NPCs/BetterTaxesGlobalNPC.cs | 98 +++++++++++++++++++++++++----------- TaxPlayer.cs | 8 ++- TaxWorld.cs | 35 +++++++++++++ build.txt | 2 +- 11 files changed, 162 insertions(+), 77 deletions(-) diff --git a/BankHandler.cs b/BankHandler.cs index fbb7b34..7dcb02f 100644 --- a/BankHandler.cs +++ b/BankHandler.cs @@ -12,8 +12,14 @@ public static class BankHandler public static bool[] HasBank() { - int collector = NPC.FindFirstNPC(NPCID.TaxCollector); - if (collector >= 0 && !Main.npc[collector].homeless) return HasBank(Main.npc[collector].homeTileX, Main.npc[collector].homeTileY - 1); + if (Main.netMode != 1) + { + int collector = NPC.FindFirstNPC(NPCID.TaxCollector); + if (collector >= 0 && !Main.npc[collector].homeless) + { + if (Main.npc[collector].homeTileX > 10 && Main.npc[collector].homeTileY > 10 && Main.npc[collector].homeTileX < Main.maxTilesX - 10 && Main.npc[collector].homeTileY < Main.maxTilesY) return HasBank(Main.npc[collector].homeTileX, Main.npc[collector].homeTileY - 1); + } + } return new bool[SafeTypes.Length]; } @@ -21,21 +27,22 @@ public static bool[] HasBank(int x, int y) { bool[] data = new bool[SafeTypes.Length]; - if (!WorldGen.StartRoomCheck(x, y)) return data; - int iters = 0; - for (int k = WorldGen.roomY1; k <= WorldGen.roomY2; k++) + bool succeededRoomCheck = false; + try { - for (int j = WorldGen.roomX1; j <= WorldGen.roomX2; j++) - { - if (++iters > 100000) throw new Exception("we got ourselves a problem error code \"cool kid\" please report thanks"); - if (Main.tile[j, k] != null && Main.tile[j, k].active()) - { - ushort type = Main.tile[j, k].type; - if (SafeTypes.Contains(type)) data[Array.IndexOf(SafeTypes, type)] = true; - } - } + succeededRoomCheck = WorldGen.StartRoomCheck(x, y); // this seems to have some problems in some scenarios, so we catch it just in case to avoid outright error + } + catch (Exception e) + { + BetterTaxes.Instance.Logger.Warn("Failed to check the Tax Collector's house (please report): " + e.ToString()); + return data; } + if (!succeededRoomCheck) return data; + for (int i = 0; i < SafeTypes.Length; i++) + { + data[i] = WorldGen.houseTile[SafeTypes[i]]; + } return data; } diff --git a/BetterTaxes.cs b/BetterTaxes.cs index b19b976..b54854d 100644 --- a/BetterTaxes.cs +++ b/BetterTaxes.cs @@ -140,6 +140,7 @@ public override void Unload() ModHandler.delegates = new Dictionary>>(); ModHandler.mods = new Dictionary(); ModHandler.customStatements = new Dictionary(); + ModHandler.hasCheckedForCalamity = false; } public override void PostSetupContent() diff --git a/Commands/InfoCommand.cs b/Commands/InfoCommand.cs index ea61b20..e82ea61 100644 --- a/Commands/InfoCommand.cs +++ b/Commands/InfoCommand.cs @@ -14,17 +14,10 @@ public class InfoCommand : ModCommand public override void Action(CommandCaller caller, string input, string[] args) { - if (NPC.savedTaxCollector) - { - int npcCount = ModHandler.parser.CalculateNPCCount(); - int taxRate = ModHandler.parser.CalculateRate(); - long rate = TaxWorld.serverConfig.TimeBetweenPaychecks; - caller.Reply("Tax rate: " + UsefulThings.ValueToCoins(taxRate * npcCount) + " per " + TimeSpan.FromSeconds(rate / Main.dayRate).ToString(@"mm\:ss") + "\nUnadjusted tax rate: " + UsefulThings.ValueToCoins(taxRate) + " per " + TimeSpan.FromSeconds(rate).ToString(@"mm\:ss") + " per NPC\nHoused NPC Count: " + npcCount, Color.Yellow); - } - else - { - caller.Reply("The Tax Collector has not yet been saved in this world!", Color.OrangeRed); - } + int npcCount = ModHandler.parser.CalculateNPCCount(); + int taxRate = ModHandler.parser.CalculateRate(); + long rate = TaxWorld.serverConfig.TimeBetweenPaychecks; + caller.Reply("Tax rate: " + UsefulThings.ValueToCoins(taxRate * npcCount) + " per " + TimeSpan.FromSeconds(rate / Main.dayRate).ToString(@"mm\:ss") + "\nUnadjusted tax rate: " + UsefulThings.ValueToCoins(taxRate) + " per " + TimeSpan.FromSeconds(rate).ToString(@"mm\:ss") + " per NPC\nHoused NPC Count: " + npcCount, Color.Yellow); } } } diff --git a/Commands/SetTaxesCommand.cs b/Commands/SetTaxesCommand.cs index eaf14d1..25d2709 100644 --- a/Commands/SetTaxesCommand.cs +++ b/Commands/SetTaxesCommand.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics; using Terraria; using Terraria.ModLoader; @@ -88,19 +87,34 @@ public override void Action(CommandCaller caller, string input, string[] args) /*public class DebugCommand : ModCommand { - public override CommandType Type => CommandType.World; + public override CommandType Type => CommandType.World | CommandType.Console; public override string Command => "debug"; - public override string Usage => ""; - public override string Description => ""; + public override string Usage => "a"; + public override string Description => "b"; public override void Action(CommandCaller caller, string input, string[] args) { - Stopwatch sw = new Stopwatch(); - sw.Start(); - ModHandler.parser.CalculateRate(); - sw.Stop(); + Main.dayTime = false; + Main.time = 16200-(Main.dayRate*60); + caller.Reply(Main.time.ToString()); - caller.Reply(sw.Elapsed.TotalMilliseconds + " ms"); + if (Main.netMode == 2) + { + NetMessage.SendData(MessageID.WorldData); + } + } + } + + public class DebugTCommand : ModCommand + { + public override CommandType Type => CommandType.Chat; + public override string Command => "debug2"; + public override string Usage => "a"; + public override string Description => "b"; + + public override void Action(CommandCaller caller, string input, string[] args) + { + caller.Reply(Main.xMas.ToString()); } }*/ } diff --git a/GateParser.cs b/GateParser.cs index 7331b4e..be241f0 100644 --- a/GateParser.cs +++ b/GateParser.cs @@ -22,15 +22,12 @@ public int CalculateRate() } foreach (KeyValuePair entry in TaxWorld.serverConfig.TaxRates) { - if (entry.Value > taxRate && Interpret(entry.Key)) - { - taxRate = entry.Value; - - } + if (entry.Value > taxRate && Interpret(entry.Key)) taxRate = entry.Value; } if (taxRate == -1) throw new InvalidConfigException("No statement evaluated to true. To avoid this error, you should map the statement \"Base.always\" to a value to fall back on"); - if (Main.expertMode && TaxWorld.serverConfig.ExpertModeBoost >= 0) taxRate = (int)(taxRate * TaxWorld.serverConfig.ExpertModeBoost); + if (Main.expertMode && TaxWorld.serverConfig.ExpertModeBoost >= 0) taxRate = (int)(taxRate * TaxWorld.serverConfig.ExpertModeBoost); // Expert mode boost + if (Main.xMas) taxRate = (int)(taxRate * 1.25); // Christmas boost return taxRate; } @@ -244,7 +241,7 @@ public bool InterpretCondition(string condition) } return false; } - else if (terms.Length == 3) // note that this will probably add some lag to world start times + else if (terms.Length == 3) // This probably shouldn't be used, it's much faster and neater for mods to use BetterTaxes's Mod.Call API. I only keep this here for backwards compatibility { if (invalidMods.ContainsKey(terms[0])) return false; Mod customMod = ModLoader.GetMod(terms[0]); diff --git a/Localization/en-US.lang b/Localization/en-US.lang index 774d4d1..eb412bb 100644 --- a/Localization/en-US.lang +++ b/Localization/en-US.lang @@ -9,6 +9,7 @@ Dialog.4=If you're feeling genocidal, the loot some of those "powerful monsters" Dialog.5=How come you expect your money so often? Dialog.6=If you were to give me something to put your coin into, like a piggy bank, you wouldn't have to talk to me anymore! Dialog.7=You would never harvest my soul, would you? +Dialog.8=The Christmas season has always been excellent for consumerism! I'd reckon that the boosted economy will give us some extra cash. -- Status -- Status.StatusMessage=Well, rent's getting charged at %1 every %2 per citizen, which is netting you %3 an hour. Does that answer your question? diff --git a/Localization/es-ES.lang b/Localization/es-ES.lang index 3503197..e1dfe7c 100644 --- a/Localization/es-ES.lang +++ b/Localization/es-ES.lang @@ -9,6 +9,7 @@ Dialog.4=Si quieres cometer genocidio, el botín de los "monstruos poderosos" co Dialog.5=¿Por qué necesitas tu dinero tan a menudo? Dialog.6=Si me darías algo que puede almacenar tu dinero, como una alcancía, ¡ya no tendrías que hablar conmigo! Dialog.7=Nunca cosecharás mi alma, ¿verdad? +Dialog.8=¡La temporada navideña siempre ha sido excelente para el consumismo! Creo que la economía estimulada nos dará un poco dinero extra. -- Estado -- Status.StatusMessage=Pues, cobramos el alquiler por %1 cada %2 y estamos embolsando %3 cada hora. ¿Eso contesta la pregunta? diff --git a/NPCs/BetterTaxesGlobalNPC.cs b/NPCs/BetterTaxesGlobalNPC.cs index 63554f5..db08654 100644 --- a/NPCs/BetterTaxesGlobalNPC.cs +++ b/NPCs/BetterTaxesGlobalNPC.cs @@ -70,38 +70,76 @@ public override void GetChat(NPC npc, ref string chat) if (Main.npc[i].homeless) homelessNpcCount++; } - if (Main.rand.Next(7) == 0 && (float)homelessNpcCount/npcCount >= 0.5) // at least half the population is homeless + if (Main.rand.Next(2) == 0) { - chat = Language.GetTextValue("Mods.BetterTaxes.Dialog.0"); - } - if (Main.rand.Next(7) == 0 && homelessNpcCount == npcCount) // everyone is homeless - { - chat = Language.GetTextValue("Mods.BetterTaxes.Dialog.1"); - } - if (Main.rand.Next(7) == 0 && taxAmount >= TaxWorld.serverConfig.MoneyCap / 10 && taxAmount >= 500000) // more than a tenth of the cap and at least 50 gold - { - chat = Language.GetTextValue("Mods.BetterTaxes.Dialog.2"); - } - if (Main.rand.Next(7) == 0 && TaxWorld.serverConfig.TaxRates.ContainsKey("Base.mechAny") && NPC.downedMechBossAny) // a mechanical boss has been killed - { - chat = Language.GetTextValue("Mods.BetterTaxes.Dialog.3"); - } - if (Main.rand.Next(7) == 0 && TaxWorld.serverConfig.TaxRates.ContainsKey("Base.mechAny") && !NPC.downedMechBossAny) // we haven't killed a mechanical boss yet - { - chat = Language.GetTextValue("Mods.BetterTaxes.Dialog.4"); - } - if (Main.rand.Next(7) == 0 && TaxWorld.serverConfig.TimeBetweenPaychecks <= 1440) // 24 *minutes* (or one in-game day) - { - chat = Language.GetTextValue("Mods.BetterTaxes.Dialog.5"); - } - if (Main.rand.Next(7) == 0 && TaxWorld.serverConfig.EnableAutoCollect && !BankHandler.LastCheckBank) - { - chat = Language.GetTextValue("Mods.BetterTaxes.Dialog.6"); - } - if (Main.rand.Next(7) == 0 && DialogUtils.CheckIfModExists("VendingMachines")) - { - chat = Language.GetTextValue("Mods.BetterTaxes.Dialog.7"); + bool hasChosenDialog = false; + while (!hasChosenDialog) + { + int chosenDialog = Main.rand.Next(9); // 0 - 8 + switch(chosenDialog) + { + case 0: + if ((float)homelessNpcCount / npcCount >= 0.5) // at least half the population is homeless + { + chat = Language.GetTextValue("Mods.BetterTaxes.Dialog.0"); hasChosenDialog = true; + } + break; + case 1: + if (homelessNpcCount == npcCount) // everyone is homeless + { + chat = Language.GetTextValue("Mods.BetterTaxes.Dialog.1"); hasChosenDialog = true; + } + break; + case 2: + if (taxAmount >= TaxWorld.serverConfig.MoneyCap / 10 && taxAmount >= 500000) // more than a tenth of the cap and at least 50 gold + { + chat = Language.GetTextValue("Mods.BetterTaxes.Dialog.2"); hasChosenDialog = true; + } + break; + case 3: + if (TaxWorld.serverConfig.TaxRates.ContainsKey("Base.mechAny") && NPC.downedMechBossAny) // a mechanical boss has been killed + { + chat = Language.GetTextValue("Mods.BetterTaxes.Dialog.3"); hasChosenDialog = true; + } + break; + case 4: + if (TaxWorld.serverConfig.TaxRates.ContainsKey("Base.mechAny") && !NPC.downedMechBossAny) // we haven't killed a mechanical boss yet + { + chat = Language.GetTextValue("Mods.BetterTaxes.Dialog.4"); hasChosenDialog = true; + } + break; + case 5: + if (TaxWorld.serverConfig.TimeBetweenPaychecks <= 1440) // 24 *minutes* (or one in-game day) + { + chat = Language.GetTextValue("Mods.BetterTaxes.Dialog.5"); hasChosenDialog = true; + } + break; + case 6: + if (TaxWorld.serverConfig.EnableAutoCollect && !BankHandler.LastCheckBank) // player doesn't have autocollection set up + { + chat = Language.GetTextValue("Mods.BetterTaxes.Dialog.6"); hasChosenDialog = true; + } + break; + case 7: + if (DialogUtils.CheckIfModExists("VendingMachines")) // has vending machines mod + { + chat = Language.GetTextValue("Mods.BetterTaxes.Dialog.7"); hasChosenDialog = true; + } + break; + case 8: + if (Main.xMas) // currently christmas season! + { + chat = Language.GetTextValue("Mods.BetterTaxes.Dialog.8"); hasChosenDialog = true; + } + break; + default: + chat = Language.GetTextValue("tModLoader.DefaultTownNPCChat"); hasChosenDialog = true; + break; + } + + } } + } } } diff --git a/TaxPlayer.cs b/TaxPlayer.cs index e47f63d..bc8ca56 100644 --- a/TaxPlayer.cs +++ b/TaxPlayer.cs @@ -41,12 +41,10 @@ public override void PostUpdate() if (TaxWorld.serverConfig.EnableAutoCollect && !Main.dayTime && Main.time == 16200 && player.taxMoney > 0) // doesn't work with enchanted sundial but that's okay, it'll do it all the next day { - bool[] bankType = BankHandler.HasBank(); - bool succeeded = false; - if (bankType[0] && !succeeded) succeeded = BankHandler.AddCoins(player.bank, player.taxMoney); - if (bankType[1] && !succeeded) succeeded = BankHandler.AddCoins(player.bank2, player.taxMoney); - if (bankType[2] && !succeeded) succeeded = BankHandler.AddCoins(player.bank3, player.taxMoney); + if (TaxWorld.ClientBanksList[0] && !succeeded) succeeded = BankHandler.AddCoins(player.bank, player.taxMoney); + if (TaxWorld.ClientBanksList[1] && !succeeded) succeeded = BankHandler.AddCoins(player.bank2, player.taxMoney); + if (TaxWorld.ClientBanksList[2] && !succeeded) succeeded = BankHandler.AddCoins(player.bank3, player.taxMoney); if (succeeded) { player.taxMoney = 0; diff --git a/TaxWorld.cs b/TaxWorld.cs index 5178e65..0f93d27 100644 --- a/TaxWorld.cs +++ b/TaxWorld.cs @@ -1,3 +1,5 @@ +using System.IO; +using Terraria; using Terraria.ModLoader; namespace BetterTaxes @@ -5,5 +7,38 @@ namespace BetterTaxes public class TaxWorld : ModWorld { public static BetterTaxesConfig serverConfig; + private bool hasSynced = false; + + public static bool[] ClientBanksList = new bool[BankHandler.SafeTypes.Length]; + public override void NetSend(BinaryWriter writer) + { + var flags = new BitsByte(); + for (int i = 0; i < BankHandler.SafeTypes.Length; i++) + { + flags[i] = ClientBanksList[i]; + } + writer.Write(flags); + } + + public override void NetReceive(BinaryReader reader) + { + BitsByte flags = reader.ReadByte(); + for (int i = 0; i < BankHandler.SafeTypes.Length; i++) + { + ClientBanksList[i] = flags[i]; + } + } + + public override void PostUpdate() + { + if (Main.netMode == 1) return; + + if (Main.dayTime && hasSynced) hasSynced = false; + if (serverConfig.EnableAutoCollect && !hasSynced && !Main.dayTime && Main.time >= 15000 && Main.time < 16200) // 20-second updating window + { + ClientBanksList = BankHandler.HasBank(); + hasSynced = true; + } + } } } \ No newline at end of file diff --git a/build.txt b/build.txt index 58db86e..d8f2239 100644 --- a/build.txt +++ b/build.txt @@ -1,5 +1,5 @@ author = Atenfyr -version = 2.3.1 +version = 2.3.2 displayName = Better Taxes homepage = https://forums.terraria.org/index.php?threads/better-taxes.76764/ hideCode = true