Skip to content

Commit

Permalink
autosave?
Browse files Browse the repository at this point in the history
  • Loading branch information
Pannoniae committed Oct 9, 2024
1 parent 3cfcb81 commit 3f0007b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/main/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -667,21 +667,21 @@ private void actualRender(double dt, double interp) {
//GD.BlendingEnabled = false;
}

public TimerAction setInterval(long interval, Action action) {
public static TimerAction setInterval(long interval, Action action) {
var now = permanentStopwatch.ElapsedMilliseconds;
var ta = new TimerAction(action, now, true, interval);
timerQueue.Add(ta);
return ta;
}

public TimerAction setTimeout(long timeout, Action action) {
public static TimerAction setTimeout(long timeout, Action action) {
var now = permanentStopwatch.ElapsedMilliseconds;
var ta = new TimerAction(action, now, false, timeout);
timerQueue.Add(ta);
return ta;
}

private void handleTimers() {
private static void handleTimers() {
foreach (var timerAction in timerQueue) {
var now = permanentStopwatch.ElapsedMilliseconds;
if (timerAction.enabled && now - timerAction.lastCalled > timerAction.interval) {
Expand Down
4 changes: 2 additions & 2 deletions src/ui/screen/GameScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public override void activate() {

switchToMenu(INGAME_MENU);

updateMemory = Game.instance.setInterval(200, INGAME_MENU.updateMemoryMethod);
updateDebugText = Game.instance.setInterval(50, INGAME_MENU.updateDebugTextMethod);
updateMemory = Game.setInterval(200, INGAME_MENU.updateMemoryMethod);
updateDebugText = Game.setInterval(50, INGAME_MENU.updateDebugTextMethod);
}

public void setWorld(World inWorld) {
Expand Down
10 changes: 10 additions & 0 deletions src/world/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class World : IDisposable {
public int worldTick;

public Random random;
private readonly TimerAction saveWorld;

private const long MAX_CHUNKLOAD_FRAMETIME = 20;
private const long MAX_LIGHT_FRAMETIME = 10;
Expand Down Expand Up @@ -101,6 +102,14 @@ public World(string name, int seed, bool loadingSave = false) {
// if we don't save the world, some of the chunks might get saved but no level.xnbt
// so the world is corrupted and we have horrible chunkglitches
worldIO.save(this, name, false);


// setup world saving every 5 mins
saveWorld = Game.setInterval(5 * 60 * 1000, saveWorldMethod);
}

private void saveWorldMethod() {
worldIO.saveWorldData();
}

public void startMeshing() {
Expand Down Expand Up @@ -401,6 +410,7 @@ private void ReleaseUnmanagedResources() {
public void Dispose() {
// of course, we can save it here since WE call it and not the GC
worldIO.save(this, name);
saveWorld.enabled = false;
ReleaseUnmanagedResources();
GC.SuppressFinalize(this);
}
Expand Down
19 changes: 13 additions & 6 deletions src/world/WorldIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ public void save(World world, string filename, bool saveChunks = true) {
Directory.CreateDirectory($"level/{filename}");
}

var tag = new NBTCompound("world");
tag.addInt("seed", world.seed);
tag.addDouble("posX", world.player.position.X);
tag.addDouble("posY", world.player.position.Y);
tag.addDouble("posZ", world.player.position.Z);
NBT.writeFile(tag, $"level/{filename}/level.xnbt");
saveWorldData();

// save chunks
if (saveChunks) {
Expand All @@ -44,6 +39,15 @@ public void save(World world, string filename, bool saveChunks = true) {
//regionCache.Clear();
}

public void saveWorldData() {
var tag = new NBTCompound("world");
tag.addInt("seed", world.seed);
tag.addDouble("posX", world.player.position.X);
tag.addDouble("posY", world.player.position.Y);
tag.addDouble("posZ", world.player.position.Z);
NBT.writeFile(tag, $"level/{world.name}/level.xnbt");
}

public void saveChunk(World world, Chunk chunk) {
var nbt = serialiseChunkIntoNBT(chunk);
// ensure directory is created
Expand Down Expand Up @@ -110,6 +114,9 @@ public static World load(string filename) {
var tag = NBT.readFile($"level/{filename}/level.xnbt");
var seed = tag.getInt("seed");
var world = new World(filename, seed, true);
Console.Out.WriteLine(tag.getDouble("posX"));
Console.Out.WriteLine(tag.getDouble("posY"));
Console.Out.WriteLine(tag.getDouble("posZ"));
world.player.position.X = tag.getDouble("posX");
world.player.position.Y = tag.getDouble("posY");
world.player.position.Z = tag.getDouble("posZ");
Expand Down

0 comments on commit 3f0007b

Please sign in to comment.