Skip to content

Commit

Permalink
Add console command to reload and restart the simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
ZehMatt committed Aug 25, 2024
1 parent df22f07 commit bb28fac
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 33 deletions.
30 changes: 19 additions & 11 deletions Mod/Console/Console.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ private string GetHelpText()
{
return "Usage: walkersim <command>.\n" +
"List of commands:\n" +
" - show\n" +
" Opens a new window with the simulation rendering, works only in singleplayer games.\n" +
" - pause\n" +
" Pauses the simulation which also stops spawning and despawning.\n" +
" - resume\n" +
" Resumes the simulation and also the spawning/despawning." +
" - timescale <value>\n" +
" Sets the timescale of the simulation, can be used to speed it up or slow it down.";
" -> show\n" +
" Opens a new window with the simulation rendering, works only in singleplayer games.\n" +
" -> pause\n" +
" Pauses the simulation which also stops spawning and despawning.\n" +
" -> resume\n" +
" Resumes the simulation and also the spawning/despawning." +
" -> restart\n" +
" Reloads the configuration and restarts the simulation." +
" -> timescale <value>\n" +
" Sets the timescale of the simulation, can be used to speed it up or slow it down.";
;

}
Expand All @@ -37,18 +39,20 @@ public override void Execute(List<string> _params, CommandSenderInfo _senderInfo
return;
}

var simulation = Simulation.Instance;

string cmd = _params[0].ToLower();
if (cmd == "show")
{
LocalPlayerUI.primaryUI.windowManager.Open("walkersim", true);
}
else if (cmd == "pause")
{
Simulation.Instance.SetPaused(true);
simulation.SetPaused(true);
}
else if (cmd == "resume")
{
Simulation.Instance.SetPaused(false);
simulation.SetPaused(false);
}
else if (cmd == "timescale")
{
Expand All @@ -60,7 +64,7 @@ public override void Execute(List<string> _params, CommandSenderInfo _senderInfo
{
if (float.TryParse(_params[1], out var timeScale))
{
Simulation.Instance.TimeScale = timeScale;
simulation.TimeScale = timeScale;
}
else
{
Expand All @@ -69,6 +73,10 @@ public override void Execute(List<string> _params, CommandSenderInfo _senderInfo
}

}
else if (cmd == "restart")
{
WalkerSimMod.RestartSimulation();
}
else
{
ShowHelpText("Unknown command: " + cmd);
Expand Down
59 changes: 37 additions & 22 deletions Mod/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static void GameAwake()
Logging.Out("GameAwake");
}

static Config LoadConfiguration()
internal static Config LoadConfiguration()
{
var worldFolder = PathAbstractions.WorldsSearchPaths.GetLocation(GamePrefs.GetString(EnumGamePrefs.GameWorld)).FullPath;
Logging.Out("World Folder: {0}", worldFolder);
Expand Down Expand Up @@ -77,6 +77,34 @@ static string GetSimulationSaveFile()
return System.IO.Path.Combine(saveFilePath, "walkersim.bin");
}

static void ResetSimulation()
{
var simulation = Simulation.Instance;

var config = LoadConfiguration();
simulation.Reset(config);

// Advance the simulation by specified ticks.
Logging.Out("Advancing simulation for {0} ticks...", Simulation.Limits.TicksToAdvanceOnStartup);

var elapsed = Utils.Measure(() =>
{
simulation.FastAdvance(Simulation.Limits.TicksToAdvanceOnStartup);
});

Logging.Out("... done, took {0}.", elapsed);
}

internal static void RestartSimulation()
{
Logging.Out("Restarting simulation...");

ResetSimulation();

var simulation = Simulation.Instance;
simulation.Start();
}

static void InitializeSimulation()
{
var simulation = Simulation.Instance;
Expand Down Expand Up @@ -113,26 +141,15 @@ static void InitializeSimulation()
// Load or create the state.
{
var simFile = GetSimulationSaveFile();

if (System.IO.File.Exists(simFile) && simulation.Load(simFile))
{
Logging.Out("Using existing simulation from: {0}", simFile);
}
else
{
Logging.Out("No previous simulation found, starting new.");

var config = LoadConfiguration();
simulation.Reset(config);

// Advance the simulation by specified ticks.
Logging.Out("Advancing simulation for {0} ticks...", Simulation.Limits.TicksToAdvanceOnStartup);

var elapsed = Utils.Measure(() =>
{
simulation.FastAdvance(Simulation.Limits.TicksToAdvanceOnStartup);
});

Logging.Out("... done, took {0}.", elapsed);
ResetSimulation();
}

simulation.EnableAutoSave(simFile, 60.0f);
Expand All @@ -142,6 +159,12 @@ static void InitializeSimulation()
simulation.WorldMins,
simulation.WorldMaxs,
simulation.Agents.Count);

// Simulation will be resumed in GameUpdate, there are a couple conditions such as
// the requirement to have players before its resumed.
simulation.SetPaused(true);

simulation.Start();
}

static bool IsHost()
Expand All @@ -165,14 +188,6 @@ static void GameStartDone()
}

InitializeSimulation();

var simulation = Simulation.Instance;

// Simulation will be resumed in GameUpdate, there are a couple conditions such as
// the requirement to have players before its resumed.
simulation.SetPaused(true);

simulation.Start();
}

static void UpdatePlayerPositions()
Expand Down

0 comments on commit bb28fac

Please sign in to comment.