Skip to content

Commit

Permalink
huge reoptimization, fixed a few bugs in the process
Browse files Browse the repository at this point in the history
  • Loading branch information
atenfyr committed Feb 29, 2020
1 parent c6fcf0d commit e1b6237
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 77 deletions.
35 changes: 21 additions & 14 deletions BankHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,37 @@ 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];
}

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;
}

Expand Down
1 change: 1 addition & 0 deletions BetterTaxes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public override void Unload()
ModHandler.delegates = new Dictionary<string, Dictionary<string, Func<bool>>>();
ModHandler.mods = new Dictionary<string, Mod>();
ModHandler.customStatements = new Dictionary<string, int>();
ModHandler.hasCheckedForCalamity = false;
}

public override void PostSetupContent()
Expand Down
15 changes: 4 additions & 11 deletions Commands/InfoCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
32 changes: 23 additions & 9 deletions Commands/SetTaxesCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using Terraria;
using Terraria.ModLoader;

Expand Down Expand Up @@ -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());
}
}*/
}
11 changes: 4 additions & 7 deletions GateParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@ public int CalculateRate()
}
foreach (KeyValuePair<string, SpecialInt> 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;
}

Expand Down Expand Up @@ -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]);
Expand Down
1 change: 1 addition & 0 deletions Localization/en-US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
1 change: 1 addition & 0 deletions Localization/es-ES.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
98 changes: 68 additions & 30 deletions NPCs/BetterTaxesGlobalNPC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
}

}
}
}
Expand Down
8 changes: 3 additions & 5 deletions TaxPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 35 additions & 0 deletions TaxWorld.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
using System.IO;
using Terraria;
using Terraria.ModLoader;

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;
}
}
}
}
2 changes: 1 addition & 1 deletion build.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit e1b6237

Please sign in to comment.