Xyaneon / Weapons-of-Mass-Domination

A console game for evil geniuses. Try to take over the world!
MIT License
0 stars 0 forks source link

System.OverflowException for land purchase calculation #91

Closed Xyaneon closed 1 year ago

Xyaneon commented 1 year ago

Stacktrace from manual testing while working on #89, during next day display near the end of the game:

Unhandled exception. System.OverflowException: Value was either too large or too small for an Int32.
   at System.Decimal.ToInt32(Decimal d)
   at WMD.Game.State.Utility.LandAreaCalculator.CalculateMaximumLandAreaCurrentPlayerCouldPurchase(GameState gameState) in C:\Users\chris\source\repos\Xyaneon\Weapons-of-Mass-Domination\src\wmd-core\State\Utility\LandAreaCalculator.cs:line 22
   at WMD.Game.State.Utility.GameStateChecks.CurrentPlayerCouldPurchaseLand(GameState gameState) in C:\Users\chris\source\repos\Xyaneon\Weapons-of-Mass-Domination\src\wmd-core\State\Utility\GameStateChecks.cs:line 28
   at WMD.Game.Commands.PurchaseUnclaimedLandCommand.CanExecuteForState(GameState gameState) in C:\Users\chris\source\repos\Xyaneon\Weapons-of-Mass-Domination\src\wmd-core\Commands\PurchaseUnclaimedLandCommand.cs:line 19
   at WMD.Console.UI.Menus.GameMenuFactory.CreateGameCommandMenuItem(String menuItemLabel, Menu menu, GameState gameState, IGameCommand gameCommand) in C:\Users\chris\source\repos\Xyaneon\Weapons-of-Mass-Domination\src\wmd-console\UI\Menus\GameMenuFactory.cs:line 128
   at WMD.Console.UI.Menus.GameMenuFactory.CreateLandCommandsMenuPage(Menu menu, GameState gameState) in C:\Users\chris\source\repos\Xyaneon\Weapons-of-Mass-Domination\src\wmd-console\UI\Menus\GameMenuFactory.cs:line 140
   at WMD.Console.UI.Menus.GameMenuFactory.CreatePlayerActionMenu(GameState gameState) in C:\Users\chris\source\repos\Xyaneon\Weapons-of-Mass-Domination\src\wmd-console\UI\Menus\GameMenuFactory.cs:line 84
   at WMD.Console.UI.Core.UserInput.GetCommand(GameState gameState) in C:\Users\chris\source\repos\Xyaneon\Weapons-of-Mass-Domination\src\wmd-console\UI\Core\UserInput.cs:line 14
   at WMD.Console.HumanTurnRunner.RunTurn(GameState gameState) in C:\Users\chris\source\repos\Xyaneon\Weapons-of-Mass-Domination\src\wmd-console\HumanTurnRunner.cs:line 25
   at WMD.Console.GameRunner.Run() in C:\Users\chris\source\repos\Xyaneon\Weapons-of-Mass-Domination\src\wmd-console\GameRunner.cs:line 38
   at WMD.Console.UI.Menus.MainMenuActions.StartNewSinglePlayerGame() in C:\Users\chris\source\repos\Xyaneon\Weapons-of-Mass-Domination\src\wmd-console\UI\Menus\MainMenuActions.cs:line 19
   at Xyaneon.Console.Menus.MenuPage.ActivateSelection() in C:\Users\chris\source\repos\Xyaneon\Weapons-of-Mass-Domination\src\Xyaneon.Console.Menus\MenuPage.cs:line 84
   at Xyaneon.Console.Menus.Menu.Run() in C:\Users\chris\source\repos\Xyaneon\Weapons-of-Mass-Domination\src\Xyaneon.Console.Menus\Menu.cs:line 124
   at WMD.Console.Program.Main(String[] args) in C:\Users\chris\source\repos\Xyaneon\Weapons-of-Mass-Domination\src\wmd-console\Program.cs:line 15
Xyaneon commented 1 year ago

Additional info output for the turn just before the crash:

Money: $611,295,233,673.91 | Henchmen: 676,262,473 | Land: 12,089,031 km² | Reputation: 29%
You control a land area comparable to Antarctica.
Your secret base is at Level 4.
Your nuke research is at Level 0 (not yet started).

136,850,966 km² of land and 5,645,001,923 people on Earth remain uncontrolled (91.88%).
The government has been defeated.
Xyaneon commented 1 year ago

The issue seems to be with this line:

https://github.com/Xyaneon/Weapons-of-Mass-Domination/blob/046e1cd647b7f632f0fdb30e17d7b9be475a5dd8/src/wmd-core/State/Utility/LandAreaCalculator.cs#L22

This does not take the actual available remaining land area into account. If a player has a sufficiently high amount of money (and the price of remaining land is sufficiently high), then the amount of land they could theoretically purchase may exceed the upper bounds of an Int32.