From ae5afc7fc41a65804be934c0415a2a8eaf82fe5c Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Wed, 23 Jun 2010 23:31:37 +0000
Subject: [PATCH] Quick Birth Water now uses OpenGL shaders

git-svn-id: http://svn.net-core.org/repos/t-engine4@835 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/engine/Birther.lua          | 35 +++++++++++++++++++++++++++++++-
 game/engine/Game.lua             |  1 +
 game/engine/Savefile.lua         | 29 ++++++++++++++++++++++++++
 game/modules/tome/class/Game.lua | 12 ++++++++++-
 4 files changed, 75 insertions(+), 2 deletions(-)

diff --git a/game/engine/Birther.lua b/game/engine/Birther.lua
index 8348eb5c2e..905b55657f 100644
--- a/game/engine/Birther.lua
+++ b/game/engine/Birther.lua
@@ -60,10 +60,12 @@ end
 
 
 --- Instanciates a birther for the given actor
-function _M:init(actor, order, at_end)
+function _M:init(actor, order, at_end, quickbirth)
+	self.quickbirth = quickbirth
 	self.actor = actor
 	self.order = order
 	self.at_end = at_end
+
 	engine.Dialog.init(self, "Character Creation: "..actor.name, 600, 400)
 
 	self.descriptors = {}
@@ -91,6 +93,34 @@ function _M:init(actor, order, at_end)
 	}
 end
 
+function _M:on_register()
+	if self.quickbirth then
+		self:yesnoPopup("Quick Birth", "Do you want to recreate the same character?", function(ret)
+			self.do_quickbirth = true
+			self:quickBirth()
+		end)
+	end
+end
+
+function _M:quickBirth()
+	if not self.do_quickbirth then return end
+	-- Aborth quickbirth if stage not found
+	if not self.quickbirth[self.current_type] then self.do_quickbirth = false end
+
+	-- Find the corect descriptor
+	for i, d in ipairs(self.list) do
+		if self.quickbirth[self.current_type] == d.name then
+			print("[QUICK BIRTH] using", d.name, "for", self.current_type)
+			self.sel = i
+			self:next()
+			return true
+		end
+	end
+
+	-- Abord if not found
+	self.do_quickbirth = false
+end
+
 function _M:selectType(type)
 	local default = 1
 	self.list = {}
@@ -148,6 +178,9 @@ function _M:next()
 		end
 	end
 	self:selectType(self.order[self.cur_order])
+
+	if self:quickBirth() then return end
+
 	if #self.list == 1 and self.birth_auto[self.current_type] ~= false then
 		self:next()
 	end
diff --git a/game/engine/Game.lua b/game/engine/Game.lua
index fe9ba3c572..9ee202e6da 100644
--- a/game/engine/Game.lua
+++ b/game/engine/Game.lua
@@ -131,6 +131,7 @@ function _M:registerDialog(d)
 	self.dialogs[d] = #self.dialogs
 	if d.key then d.key:setCurrent() end
 	if d.mouse then d.mouse:setCurrent() end
+	if d.on_register then d:on_register() end
 end
 
 --- Undisplay a dialog, removing its own keyhandler if needed
diff --git a/game/engine/Savefile.lua b/game/engine/Savefile.lua
index b4850c6e2e..ba132fe78f 100644
--- a/game/engine/Savefile.lua
+++ b/game/engine/Savefile.lua
@@ -37,6 +37,7 @@ _M.current_save = false
 function _M:init(savefile)
 	self.short_name = savefile:gsub("[^a-zA-Z0-9_-.]", "_")
 	self.save_dir = "/save/"..self.short_name.."/"
+	self.quickbirth_file = "/save/"..self.short_name..".quickbirth"
 	self.load_dir = "/tmp/loadsave/"
 
 	self.tables = {}
@@ -111,6 +112,34 @@ function _M:saveWorld(world)
 	game:unregisterDialog(popup)
 end
 
+--- Save the given birth descriptors, used for quick start
+function _M:saveQuickBirth(descriptor)
+	collectgarbage("collect")
+
+	local f = fs.open(self.quickbirth_file, "w")
+	for k, e in pairs(descriptor) do
+		f:write(("%s = %q\n"):format(tostring(k), tostring(e)))
+	end
+	f:close()
+end
+
+--- Load the given birth descriptors, used for quick start
+function _M:loadQuickBirth()
+	collectgarbage("collect")
+
+	local f = loadfile(self.quickbirth_file)
+	print("[QUICK BIRTH]", f)
+	if f then
+		-- Call the file body inside its own private environment
+		local def = {}
+		setfenv(f, def)
+		if pcall(f) then
+			return def
+		end
+	end
+	return nil
+end
+
 --- Save the given game
 function _M:saveGame(game)
 	collectgarbage("collect")
diff --git a/game/modules/tome/class/Game.lua b/game/modules/tome/class/Game.lua
index a635410f82..9de097703c 100644
--- a/game/modules/tome/class/Game.lua
+++ b/game/modules/tome/class/Game.lua
@@ -120,7 +120,17 @@ function _M:newGame()
 	Map:setViewerActor(self.player)
 	self:setupDisplayMode()
 
+	-- Load for quick birth
+	local save = Savefile.new(self.save_name)
+	local quickbirth = save:loadQuickBirth()
+	save:close()
+
 	local birth = Birther.new(self.player, {"base", "difficulty", "world", "race", "subrace", "sex", "class", "subclass" }, function()
+		-- Save for quick birth
+		local save = Savefile.new(self.save_name)
+		save:saveQuickBirth(self.player.descriptor)
+		save:close()
+
 		self.player.wild_x, self.player.wild_y = self.player.default_wilderness[1], self.player.default_wilderness[2]
 		self.player.last_wilderness = self.player.default_wilderness[3] or "wilderness"
 		self:changeLevel(1, self.player.starting_zone)
@@ -134,7 +144,7 @@ function _M:newGame()
 		print("[PLAYER BIRTH] resolved!")
 		self.player:grantQuest(self.player.starting_quest)
 		self:registerDialog(require("mod.dialogs.IntroDialog").new(self.player))
-	end)
+	end, quickbirth)
 	self:registerDialog(birth)
 end
 
-- 
GitLab