Skip to content

Commit

Permalink
109 refactor turn processing into multiple steps (#261)
Browse files Browse the repository at this point in the history
* Refactor turn processing code out of Galaxy into its own class

* Fix crash when getting galaxies mixed up processing AI commands
  • Loading branch information
ekolis committed Jan 21, 2023
1 parent b9abf92 commit 1c1bac6
Show file tree
Hide file tree
Showing 6 changed files with 844 additions and 809 deletions.
12 changes: 7 additions & 5 deletions FrEee.Tests/Game/Objects/Technology/TechnologyTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using FrEee.Game.Enumerations;
using FrEee.Game.Enumerations;
using FrEee.Game.Objects.Civilization;
using FrEee.Game.Objects.Commands;
using FrEee.Game.Objects.Space;
using FrEee.Game.Processes;
using FrEee.Modding;
using FrEee.Utility.Extensions;
using NUnit.Framework;
Expand All @@ -11,6 +12,7 @@ namespace FrEee.Tests.Game.Objects.Technology
public class TechnologyTest
{
private Empire emp;
private TurnProcessor processor = new();

/// <summary>
/// Loads the stock mod. Done once before running ALL the tests (not each individually; that would be pointless).
Expand Down Expand Up @@ -50,7 +52,7 @@ public void PercentageResearch()

// perform research
emp.ResearchCommand = cmd;
Galaxy.ProcessTurn(false);
processor.ProcessTurn(gal, false);

// verify research was done
// 500K for first level, 1M for second
Expand Down Expand Up @@ -89,7 +91,7 @@ public void QueuedResearch()

// perform research
emp.ResearchCommand = cmd;
Galaxy.ProcessTurn(false);
processor.ProcessTurn(gal, false);

// should have level 1 in t1 and level 0 in t2
Assert.AreEqual(1, emp.ResearchedTechnologies[t1]);
Expand All @@ -102,7 +104,7 @@ public void QueuedResearch()
// another turn!
emp.BonusResearch = 100000;
emp.ResearchCommand = cmd;
Galaxy.ProcessTurn(false);
processor.ProcessTurn(gal, false);

// should have level 2+ in t1 and level 1+ in t2
// (levels could be higher if extra RP gets randomly assigned to those techs)
Expand All @@ -122,4 +124,4 @@ public void TestInit()
Galaxy.Current.Empires.Add(emp);
}
}
}
}
8 changes: 5 additions & 3 deletions FrEee.WinForms/Forms/HostConsoleForm.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using FrEee.Game.Interfaces;
using FrEee.Game.Interfaces;
using FrEee.Game.Objects.Civilization;
using FrEee.Game.Objects.Space;
using FrEee.Game.Objects.Vehicles;
using FrEee.Game.Processes;
using FrEee.Modding;
using FrEee.Modding.Interfaces;
using FrEee.Utility;
Expand Down Expand Up @@ -124,7 +125,8 @@ void ProcessTurn()
var t = new Thread(new ThreadStart(() =>
{
status.Message = "Processing turn";
Galaxy.ProcessTurn(false, status, 0.5);
var processor = new TurnProcessor();
processor.ProcessTurn(Galaxy.Current, false, status, 0.5);
Galaxy.SaveAll(status, 1.0);
}));
this.ShowChildForm(new StatusForm(t, status));
Expand Down Expand Up @@ -187,4 +189,4 @@ private void autoProcessTimer_Tick(object sender, EventArgs e)
autoProcessTimer.Start();
}
}
}
}
4 changes: 3 additions & 1 deletion FrEee.WinForms/Forms/MainGameForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using FrEee.Game.Objects.Space;
using FrEee.Game.Objects.Technology;
using FrEee.Game.Objects.Vehicles;
using FrEee.Game.Processes;
using FrEee.Utility;
using FrEee.Utility.Extensions;
using FrEee.WinForms.Controls;
Expand Down Expand Up @@ -1194,7 +1195,8 @@ private void SaveCommands(bool endTurn)
Galaxy.Load(Galaxy.Current.Name, Galaxy.Current.TurnNumber);
status.Progress = 0.25;
status.Message = "Processing turn";
Galaxy.ProcessTurn(false, status, 0.5);
var processor = new TurnProcessor();
processor.ProcessTurn(Galaxy.Current, false, status, 0.5);
status.Message = "Saving game";
Galaxy.SaveAll(status, 0.75);
status.Message = "Loading game";
Expand Down
4 changes: 3 additions & 1 deletion FrEee.WinForms/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using FrEee.Game.Objects.Civilization;
using FrEee.Game.Objects.Space;
using FrEee.Game.Objects.Vehicles;
using FrEee.Game.Processes;
using FrEee.Modding;
using FrEee.Modding.Interfaces;
using FrEee.Utility;
Expand Down Expand Up @@ -289,7 +290,8 @@ private static int ProcessTurn(bool safe)
status.Changed += new Status.ChangedDelegate(status_Changed);

Console.WriteLine("Processing turn...");
var emps = Galaxy.ProcessTurn(false, status);
var processor = new TurnProcessor();
var emps = processor.ProcessTurn(Galaxy.Current, false, status);
foreach (var emp in emps)
Console.WriteLine(emp + " did not submit a PLR file.");
if (safe && emps.Any())
Expand Down
Loading

0 comments on commit 1c1bac6

Please sign in to comment.