From b2384b24cdb03dd3e68b9a5ea0b097c9f2e03b2b Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Sun, 31 Jul 2011 13:11:33 +0000
Subject: [PATCH] Savefiles are marked as dead as soon as death happens

git-svn-id: http://svn.net-core.org/repos/t-engine4@4039 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/engines/default/engine/Game.lua                   | 9 ++++++++-
 game/engines/default/engine/Savefile.lua               | 1 +
 game/engines/default/modules/boot/dialogs/LoadGame.lua | 2 +-
 game/modules/example/class/Game.lua                    | 5 +++++
 game/modules/example_realtime/class/Game.lua           | 5 +++++
 game/modules/tome/class/Game.lua                       | 7 ++++++-
 game/modules/tome/dialogs/DeathDialog.lua              | 6 ++----
 7 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/game/engines/default/engine/Game.lua b/game/engines/default/engine/Game.lua
index 6fc6feffd8..6c003d0e7f 100644
--- a/game/engines/default/engine/Game.lua
+++ b/game/engines/default/engine/Game.lua
@@ -165,10 +165,17 @@ end
 --- Returns the player
 -- Reimplement it in your module, this can just return nil if you dont want/need
 -- the engine adjusting stuff to the player or if you have many players or whatever
-function _M:getPlayer()
+-- @param main if true the game should try to return the "main" player, if any
+function _M:getPlayer(main)
 	return nil
 end
 
+--- Says if this savefile is usable or not
+-- Reimplement it in your module, returning false when the player is dead
+function _M:isLoadable()
+	return true
+end
+
 --- Gets/increment the savefile version
 -- @param token if "new" this will create a new allowed save token and return it. Otherwise this checks the token against the allowed ones and returns true if it is allowed
 function _M:saveVersion(token)
diff --git a/game/engines/default/engine/Savefile.lua b/game/engines/default/engine/Savefile.lua
index c41a1198a2..964356adda 100644
--- a/game/engines/default/engine/Savefile.lua
+++ b/game/engines/default/engine/Savefile.lua
@@ -229,6 +229,7 @@ function _M:saveGame(game, no_dialog)
 	f:write(("name = %q\n"):format(desc.name))
 	f:write(("short_name = %q\n"):format(self.short_name))
 	f:write(("timestamp = %d\n"):format(os.time()))
+	f:write(("loadable = %s\n"):format(game:isLoadable() and "true" or "false"))
 	f:write(("description = %q\n"):format(desc.description))
 	f:close()
 
diff --git a/game/engines/default/modules/boot/dialogs/LoadGame.lua b/game/engines/default/modules/boot/dialogs/LoadGame.lua
index e3a3dd573c..d7f28a3fe7 100644
--- a/game/engines/default/modules/boot/dialogs/LoadGame.lua
+++ b/game/engines/default/modules/boot/dialogs/LoadGame.lua
@@ -47,7 +47,7 @@ function _M:init()
 		for j, save in ipairs(m.savefiles) do
 			local mod_string = ("%s-%d.%d.%d"):format(m.short_name, save.module_version and save.module_version[1] or -1, save.module_version and save.module_version[2] or -1, save.module_version and save.module_version[3] or -1)
 			local mod = list[mod_string]
-			if mod then
+			if mod and save.loadable then
 				save.fct = function()
 					Module:instanciate(mod, save.name, false)
 				end
diff --git a/game/modules/example/class/Game.lua b/game/modules/example/class/Game.lua
index 856e6fd477..8fcb359bfa 100644
--- a/game/modules/example/class/Game.lua
+++ b/game/modules/example/class/Game.lua
@@ -180,6 +180,11 @@ function _M:getPlayer()
 	return self.player
 end
 
+--- Says if this savefile is usable or not
+function _M:isLoadable()
+	return not self:getPlayer(true).dead
+end
+
 function _M:tick()
 	if self.level then
 		self:targetOnTick()
diff --git a/game/modules/example_realtime/class/Game.lua b/game/modules/example_realtime/class/Game.lua
index d0c2e7ae5d..6964833c9c 100644
--- a/game/modules/example_realtime/class/Game.lua
+++ b/game/modules/example_realtime/class/Game.lua
@@ -180,6 +180,11 @@ function _M:getPlayer()
 	return self.player
 end
 
+--- Says if this savefile is usable or not
+function _M:isLoadable()
+	return not self:getPlayer(true).dead
+end
+
 function _M:tick()
 	if self.level then
 		self:targetOnTick()
diff --git a/game/modules/tome/class/Game.lua b/game/modules/tome/class/Game.lua
index 7b54a7d871..4f67bd4cd2 100644
--- a/game/modules/tome/class/Game.lua
+++ b/game/modules/tome/class/Game.lua
@@ -167,7 +167,7 @@ function _M:run()
 	self:setupMouse()
 
 	-- Starting from here we create a new game
-	if not self.player then self:newGame() end
+	if not self.player or self.player.dead then util.showMainMenu() end
 
 	self:initTargeting()
 
@@ -743,6 +743,11 @@ function _M:getPlayer(main)
 	end
 end
 
+--- Says if this savefile is usable or not
+function _M:isLoadable()
+	return not self:getPlayer(true).dead
+end
+
 --- Clones the game world for chronomancy spells
 function _M:chronoClone(name)
 	local d = Dialog:simplePopup("Chronomancy", "Folding the space time structure...")
diff --git a/game/modules/tome/dialogs/DeathDialog.lua b/game/modules/tome/dialogs/DeathDialog.lua
index 5fb97fc287..d076136534 100644
--- a/game/modules/tome/dialogs/DeathDialog.lua
+++ b/game/modules/tome/dialogs/DeathDialog.lua
@@ -35,6 +35,7 @@ function _M:init(actor)
 
 	self:generateList()
 	if self.dont_show then return end
+	game:saveGame()
 
 	self.c_desc = Textzone.new{width=self.iw, auto_height=true, text=[[You have #LIGHT_RED#died#LAST#!
 Death in ToME is usually permanent, but if you have a means of resurrection it will be proposed in the menu below.
@@ -160,6 +161,7 @@ function _M:eidolonPlane()
 
 		game.log("#LIGHT_RED#From the brink of death you seem to be yanked to another plane.")
 		game.player:updateMainShader()
+		game:saveGame()
 	end)
 	return true
 end
@@ -169,10 +171,6 @@ function _M:use(item)
 	local act = item.action
 
 	if act == "exit" then
-		local save = Savefile.new(game.save_name)
-		save:delete()
-		save:close()
-		world:saveWorld()
 		if item.subaction == "none" then
 			util.showMainMenu()
 		elseif item.subaction == "restart" then
-- 
GitLab