← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands/autosave_on_objectives into lp:widelands

 

cghislai has proposed merging lp:~widelands-dev/widelands/autosave_on_objectives into lp:widelands.

Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #536507 in widelands: "Autosave after reaching objective"
  https://bugs.launchpad.net/widelands/+bug/536507

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/autosave_on_objectives/+merge/174546

I just applied the patch provided in the bug report and removed the option checkbox.

Concerning the new lua function, i added it to the const static struct luaL_reg wlgame [] and was able to use it in a newly created win condition, so i guess this was ok?
-- 
https://code.launchpad.net/~widelands-dev/widelands/autosave_on_objectives/+merge/174546
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/autosave_on_objectives into lp:widelands.
=== modified file 'src/logic/game.h'
--- src/logic/game.h	2013-07-08 03:35:09 +0000
+++ src/logic/game.h	2013-07-13 12:57:24 +0000
@@ -92,7 +92,7 @@
 
 	// This friend is for legacy reasons and should probably be removed
 	// at least after summer 2008, maybe even earlier.
-	friend struct Game_Interactive_Player_Data_Packet;
+	//friend struct Game_Interactive_Player_Data_Packet;
 
 	Game();
 	~Game();

=== modified file 'src/save_handler.cc'
--- src/save_handler.cc	2013-02-25 17:03:31 +0000
+++ src/save_handler.cc	2013-07-13 12:57:24 +0000
@@ -37,30 +37,42 @@
  */
 void SaveHandler::think(Widelands::Game & game, int32_t realtime) {
 	initialize(realtime);
-
-	if (not m_allow_autosaving) // Is autosaving allowed atm?
-		return;
-
-	int32_t const autosaveInterval =
-		g_options.pull_section("global").get_int
-			("autosave", DEFAULT_AUTOSAVE_INTERVAL * 60);
-	if (autosaveInterval <= 0)
-		return; // no autosave requested
-
-	int32_t const elapsed = (realtime - m_lastSaveTime) / 1000;
-	if (elapsed < autosaveInterval)
-		return;
-
-	log("Autosave: interval elapsed (%d s), saving\n", elapsed);
+	std::string filename = "wl_autosave";
+
+	if (m_save_requested) {
+		if (m_save_filename.length() > 0)
+			filename = m_save_filename;
+
+		log("Autosave: save requested : %s\n", filename.c_str());
+		m_save_requested = false;
+		m_save_filename = "";
+	} else {
+		if (not m_allow_autosaving) // Is autosaving allowed atm?
+			return;
+
+		int32_t const autosaveInterval =
+			g_options.pull_section("global").get_int
+				("autosave", DEFAULT_AUTOSAVE_INTERVAL * 60);
+		if (autosaveInterval <= 0)
+			return; // no autosave requested
+
+		int32_t const elapsed = (realtime - m_lastSaveTime) / 1000;
+		if (elapsed < autosaveInterval)
+			return;
+
+		log("Autosave: interval elapsed (%d s), saving\n", elapsed);
+	}
+
 
 	// save the game
 	std::string complete_filename =
-		create_file_name (get_base_dir(), "wl_autosave");
+		create_file_name (get_base_dir(), filename);
 	std::string backup_filename;
 
 	// always overwrite a file
 	if (g_fs->FileExists(complete_filename)) {
-		backup_filename = create_file_name (get_base_dir(), "wl_autosave2");
+		filename += "2";
+		backup_filename = create_file_name (get_base_dir(), filename);
 		if (g_fs->FileExists(backup_filename)) {
 			g_fs->Unlink(backup_filename);
 		}

=== modified file 'src/save_handler.h'
--- src/save_handler.h	2013-02-10 19:36:24 +0000
+++ src/save_handler.h	2013-07-13 12:57:24 +0000
@@ -34,12 +34,15 @@
 	int32_t m_lastSaveTime;
 	bool m_initialized;
 	bool m_allow_autosaving;
+	bool m_save_requested;
+	std::string m_save_filename;
 
 	void initialize(int32_t currenttime);
 
 
 public:
-	SaveHandler() : m_lastSaveTime(0), m_initialized(false), m_allow_autosaving(true) {}
+	SaveHandler() : m_lastSaveTime(0), m_initialized(false), m_allow_autosaving(true),
+		m_save_requested(false), m_save_filename("") {}
 	void think(Widelands::Game &, int32_t currenttime);
 	std::string create_file_name(std::string dir, std::string filename);
 	bool save_game
@@ -50,6 +53,8 @@
 	static std::string get_base_dir() {return "save";}
 	void set_allow_autosaving(bool t) {m_allow_autosaving = t;}
 	bool get_allow_autosaving() {return m_allow_autosaving;}
+	void request_save(std::string filename = "") {m_save_requested = true;
+		m_save_filename = std::string(filename);}
 };
 
 #endif

=== modified file 'src/scripting/lua_game.cc'
--- src/scripting/lua_game.cc	2013-07-10 05:09:06 +0000
+++ src/scripting/lua_game.cc	2013-07-13 12:57:24 +0000
@@ -1062,7 +1062,8 @@
 	.. attribute:: done
 
 		(RW) defines if this objective is already fulfilled. If done is
-		:const`true`, the objective will not be shown to the user, no matter what
+		:const`true`, the objective will not be shown to the user, no matter what.
+		A savegame will be created when this attribute is set to :const`true`.
 		:attr:`visible` is set to.
 */
 int L_Objective::get_done(lua_State * L) {
@@ -1073,6 +1074,20 @@
 int L_Objective::set_done(lua_State * L) {
 	Objective & o = get(L, get_game(L));
 	o.set_done(luaL_checkboolean(L, -1));
+
+	int32_t autosave = g_options.pull_section("global")
+		.get_int("autosave", 0);
+	if (autosave <= 0) {
+		return 0;
+	}
+
+	if (o.done()) {
+		std::string filename = get_egbase(L).get_map()->get_name();
+		char buffer[128];
+		snprintf(buffer, sizeof(buffer), _(" (achieved %s)"), o.descname().c_str());
+		filename.append(buffer);
+		get_game(L).save_handler().request_save(filename);
+	}
 	return 0;
 }
 
@@ -1317,6 +1332,23 @@
 	return 0;
 }
 
+/* RST
+.. function:: save_game([filename = ""])
+
+	Save the game.
+
+	:arg filename: A filename may be passed in, otherwhise the autosave
+		file name will be used (wl_autosave).
+	:type filename: :class: string
+*/
+static int L_save_game(lua_State * L) {
+	std::string filename = "";
+	if (lua_gettop(L) >= 1)
+		filename = luaL_checkstring(L, 1);
+
+	get_game(L).save_handler().request_save(filename);
+	return 0;
+}
 /*
  * ========================================================================
  *                            MODULE FUNCTIONS
@@ -1324,6 +1356,7 @@
  */
 const static struct luaL_reg wlgame [] = {
 	{"report_result", &L_report_result},
+	{"save_game", &L_save_game},
 	{0, 0}
 };
 


Follow ups