← Back to team overview

widelands-dev team mailing list archive

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

 

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

Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #1201398 in widelands: "Save game claims win condition is Scenario, even when it isn't"
  https://bugs.launchpad.net/widelands/+bug/1201398

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

This corrects a few things with my previous save dialog changes.
Thanks hjd for pointing it out, there was a few other issues.

A new field in game has been created to remember players amount
Game preload data version has been increased to make use of player amounts
Save game shows current game info on first save and remembers save file
Save/Load/Replay have been updated to correctly show the informations
-- 
https://code.launchpad.net/~widelands-dev/widelands/bug1201398/+merge/174809
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/bug1201398 into lp:widelands.
=== modified file 'src/game_io/game_preload_data_packet.cc'
--- src/game_io/game_preload_data_packet.cc	2013-07-14 17:05:25 +0000
+++ src/game_io/game_preload_data_packet.cc	2013-07-15 16:07:32 +0000
@@ -25,50 +25,47 @@
 #include "logic/map.h"
 #include "profile/profile.h"
 #include "scripting/scripting.h"
+#include <boost/concept_check.hpp>
 
 namespace Widelands {
 
 // Note: releases up to build15 used version number 1 to indicate
 // a savegame without interactive player
-#define CURRENT_PACKET_VERSION 3
+#define CURRENT_PACKET_VERSION 4
+#define PLAYERS_AMOUNT_KEY_V4 "player_amount"
 
 
 void Game_Preload_Data_Packet::Read
-	(FileSystem & fs, Game & game, Map_Map_Object_Loader * const)
+	(FileSystem & fs, Game &, Map_Map_Object_Loader * const)
 {
 	try {
 		Profile prof;
 		prof.read("preload", 0, fs);
 		Section & s = prof.get_safe_section("global");
 		int32_t const packet_version = s.get_int("packet_version");
-		std::string win_condition = "";
+
 		if (1 <= packet_version && packet_version <= CURRENT_PACKET_VERSION) {
 			m_gametime   = s.get_safe_int   ("gametime");
 			m_mapname    = s.get_safe_string("mapname");
 
-			if (packet_version >= 2) {
-				m_background = s.get_safe_string("background");
-				m_player_nr  = s.get_safe_int   ("player_nr");
-				if (packet_version >= 3)
-					win_condition = s.get_safe_string("win_condition");
-				else
-					win_condition = "00_endless_game";
-			} else {
+			if (packet_version < 2) {
 				m_background = "pics/progress.png";
 				// Of course this is wrong, but at least player 1 is always in game
 				// so widelands won't crash with this setting.
 				m_player_nr  = 1;
-				win_condition = "00_endless_game";
-			}
-			// Get win condition string
-			try {
-				boost::shared_ptr<LuaTable> t = game.lua().run_script
-				("win_conditions", win_condition);
-				m_win_condition = t->get_string("name");
-			} catch (LuaScriptNotExistingError &) {
-				m_win_condition = _("Scenario");
-			} catch (LuaTableKeyError &) {
-				m_win_condition = win_condition;
+			} else {
+				m_background = s.get_safe_string("background");
+				m_player_nr  = s.get_safe_int   ("player_nr");
+			}
+			if (packet_version < 3) {
+				m_win_condition = "Endless game";
+			} else {
+				m_win_condition = s.get_safe_string("win_condition");
+			}
+			if (packet_version < 4) {
+				m_player_amount = 0;
+			} else {
+				m_player_amount = s.get_safe_int(PLAYERS_AMOUNT_KEY_V4);
 			}
 		} else {
 			throw game_data_error
@@ -95,19 +92,18 @@
 	s.set_int   ("gametime",       game.get_gametime());
 	const Map & map = game.map();
 	s.set_string("mapname",        map.get_name());  // Name of map
+
 	if (ipl) {
 		// player that saved the game.
 		s.set_int("player_nr", ipl->player_number());
 	} else {
-		// pretend that the first player that is actually
-		// there has saved the game
-		for (int i = 1; i <= MAX_PLAYERS; ++i) {
+		for (int i = 1; i <= game.get_players_amount(); ++i) {
 			if (game.get_player(i)) {
 				s.set_int("player_nr", i);
-				break;
 			}
 		}
 	}
+	s.set_int(PLAYERS_AMOUNT_KEY_V4, game.get_players_amount());
 
 	std::string bg(map.get_background());
 	if (bg.empty())

=== modified file 'src/game_io/game_preload_data_packet.h'
--- src/game_io/game_preload_data_packet.h	2012-02-15 21:25:34 +0000
+++ src/game_io/game_preload_data_packet.h	2013-07-15 16:07:32 +0000
@@ -38,15 +38,17 @@
 	char const * get_mapname()      {return m_mapname.c_str();}
 	std::string get_background()    {return m_background;}
 	std::string get_win_condition() {return m_win_condition;}
-	uint32_t get_gametime() {return m_gametime;}
-	uint8_t get_player_nr() {return m_player_nr;}
+	const uint32_t get_gametime() {return m_gametime;}
+	const uint8_t get_player_nr() {return m_player_nr;}
+	const uint8_t get_player_amount() {return m_player_amount;}
 
 private:
 	std::string m_mapname;
 	std::string m_background;
 	std::string m_win_condition;
 	uint32_t m_gametime;
-	uint8_t  m_player_nr;
+	uint8_t  m_player_nr; // The local player idx
+	uint8_t  m_player_amount;
 };
 
 }

=== modified file 'src/logic/game.cc'
--- src/logic/game.cc	2013-07-15 05:18:12 +0000
+++ src/logic/game.cc	2013-07-15 16:07:32 +0000
@@ -136,7 +136,7 @@
 	m_state               (gs_notrunning),
 	m_cmdqueue            (*this),
 	m_replaywriter        (0),
-	m_win_condition_displayname("not_set")
+	m_win_condition_displayname(_("Not set"))
 {
 	// Preload win_conditions as they are displayed in UI
 	lua().register_scripts(*g_fs, "win_conditions", "scripting/win_conditions");
@@ -336,9 +336,26 @@
 		boost::shared_ptr<LuaTable> table
 			(lua().run_script
 			 (*g_fs, "scripting/win_conditions/" + settings.win_condition + ".lua", "win_conditions"));
-		m_win_condition_displayname = table->get_string("name");
+		try {
+			m_win_condition_displayname = table->get_string("name");
+		} catch (...) {
+			m_win_condition_displayname = _("Unknown");
+		}
 		LuaCoroutine * cr = table->get_coroutine("func");
 		enqueue_command(new Cmd_LuaCoroutine(get_gametime() + 100, cr));
+	} else {
+		m_win_condition_displayname = _("Scenario");
+	}
+	m_players_amount = 0;
+	std::vector<PlayerSettings>::const_iterator it;
+	for (it = settings.players.begin(); it != settings.players.end(); ++it) {
+		PlayerSettings ps = *it;
+		if
+			(ps.state == PlayerSettings::stateHuman
+			|| ps.state == PlayerSettings::stateComputer)
+		{
+			m_players_amount++;
+		}
 	}
 }
 

=== modified file 'src/logic/game.h'
--- src/logic/game.h	2013-07-14 17:05:25 +0000
+++ src/logic/game.h	2013-07-15 16:07:32 +0000
@@ -190,6 +190,7 @@
 	void sample_statistics();
 
 	const std::string & get_win_condition_displayname() {return m_win_condition_displayname;}
+	const uint8_t get_players_amount() {return m_players_amount;}
 
 private:
 	void SyncReset();
@@ -255,6 +256,7 @@
 
 	/// For save games and statistics generation
 	std::string          m_win_condition_displayname;
+	uint8_t              m_players_amount;
 };
 
 inline Coords Game::random_location(Coords location, uint8_t radius) {

=== modified file 'src/ui_fsmenu/loadgame.cc'
--- src/ui_fsmenu/loadgame.cc	2013-07-15 05:18:12 +0000
+++ src/ui_fsmenu/loadgame.cc	2013-07-15 16:07:32 +0000
@@ -213,7 +213,11 @@
 		sprintf(buf, "%02i:%02i", hours, minutes);
 		m_tagametime.set_text(buf);
 
-		sprintf(buf, "%i", gpdp.get_player_nr());
+		if (gpdp.get_player_amount() > 0) {
+			sprintf(buf, "%i", gpdp.get_player_amount());
+		} else {
+			sprintf(buf, "%s", _("Unknown"));
+		}
 		m_ta_players.set_text(buf);
 		m_ta_win_condition.set_text(gpdp.get_win_condition());
 	} else {

=== modified file 'src/ui_fsmenu/loadreplay.cc'
--- src/ui_fsmenu/loadreplay.cc	2013-07-14 16:11:41 +0000
+++ src/ui_fsmenu/loadreplay.cc	2013-07-15 16:07:32 +0000
@@ -196,7 +196,11 @@
 		sprintf(buf, "%02i:%02i", hours, minutes);
 		m_tagametime.set_text(buf);
 
-		sprintf(buf, "%i", gpdp.get_player_nr());
+		if (gpdp.get_player_amount() > 0) {
+			sprintf(buf, "%i", gpdp.get_player_amount());
+		} else {
+			sprintf(buf, "%s", _("Unknown"));
+		}
 		m_ta_players.set_text(buf);
 
 		m_ta_win_condition.set_text(gpdp.get_win_condition());

=== modified file 'src/wui/game_main_menu_save_game.cc'
--- src/wui/game_main_menu_save_game.cc	2013-07-15 11:01:59 +0000
+++ src/wui/game_main_menu_save_game.cc	2013-07-15 16:07:32 +0000
@@ -32,6 +32,7 @@
 #include "io/filesystem/layered_filesystem.h"
 #include "logic/game.h"
 #include "profile/profile.h"
+#include "interactive_player.h"
 
 using boost::format;
 
@@ -39,10 +40,6 @@
 	return ref_cast<Interactive_GameBase, UI::Panel>(*get_parent());
 }
 
-
-Game_Main_Menu_Save_Game::Game_Main_Menu_Save_Game
-	(Interactive_GameBase & parent, UI::UniqueWindow::Registry & registry)
-:
 #define WINDOW_WIDTH                                                        440
 #define WINDOW_HEIGHT                                                       440
 #define VMARGIN                                                               5
@@ -58,13 +55,31 @@
 #define CANCEL_Y                      (WINDOW_HEIGHT - BUTTON_HEIGHT - VMARGIN)
 #define DELETE_Y                          (CANCEL_Y - BUTTON_HEIGHT - VSPACING)
 #define OK_Y                              (DELETE_Y - BUTTON_HEIGHT - VSPACING)
+
+#define SPLIT_GAMETIME(unit, factor) \
+   uint32_t const unit = gametime / factor; gametime %= factor;
+
+#define PARSE_GAMETIME(buf, gametime)                     \
+	SPLIT_GAMETIME(days, 86400000);                      \
+	SPLIT_GAMETIME(hours, 3600000);                      \
+	SPLIT_GAMETIME(minutes, 60000);                      \
+	SPLIT_GAMETIME(seconds,  1000);                      \
+	sprintf                                              \
+		(buf,                                            \
+		 _("%02ud%02uh%02u'%02u\"%03u"),                 \
+		 days, hours, minutes, seconds, gametime);       \
+
+Game_Main_Menu_Save_Game::Game_Main_Menu_Save_Game
+	(Interactive_GameBase & parent, UI::UniqueWindow::Registry & registry)
+:
+
 	UI::UniqueWindow
 		(&parent, "save_game", &registry,
 		 WINDOW_WIDTH, WINDOW_HEIGHT, _("Save Game")),
 	m_ls     (this, HSPACING, VSPACING,  LIST_WIDTH, LIST_HEIGHT),
 	m_name_label
 		(this, DESCRIPTION_X,  5, 0, 20, _("Map Name: "),  UI::Align_CenterLeft),
-	m_name
+	m_mapname
 		(this, DESCRIPTION_X, 20, 0, 20, " ",              UI::Align_CenterLeft),
 	m_gametime_label
 		(this, DESCRIPTION_X, 45, 0, 20, _("Game Time: "), UI::Align_CenterLeft),
@@ -122,6 +137,20 @@
 	std::string cur_filename = parent.game().save_handler().get_cur_filename();
 	if (!cur_filename.empty()) {
 		select_by_name(cur_filename);
+	} else {
+		// Display current game infos
+		m_mapname.set_text(parent.game().get_map()->get_name());
+		uint32_t gametime = parent.game().get_gametime();
+		char buf[200];
+		PARSE_GAMETIME(buf, gametime);
+		m_gametime.set_text(buf);
+		// May be wrong if some slots are closed
+		uint8_t player_nr = parent.game().get_players_amount();
+		sprintf
+		(buf, "%i %s", player_nr,
+		 ngettext(_("player"), _("players"),  player_nr));
+		m_players_label.set_text(buf);
+		m_win_condition.set_text(parent.game().get_win_condition_displayname());
 	}
 
 	m_editbox->focus();
@@ -143,24 +172,18 @@
 	}
 	m_button_ok->set_enabled(true);
 
-	m_name.set_text(gpdp.get_mapname());
+	m_mapname.set_text(gpdp.get_mapname());
 	char buf[200];
 	uint32_t gametime = gpdp.get_gametime();
-#define SPLIT_GAMETIME(unit, factor) \
-   uint32_t const unit = gametime / factor; gametime %= factor;
-	SPLIT_GAMETIME(days, 86400000);
-	SPLIT_GAMETIME(hours, 3600000);
-	SPLIT_GAMETIME(minutes, 60000);
-	SPLIT_GAMETIME(seconds,  1000);
-	sprintf
-		(buf,
-		 _("%02ud%02uh%02u'%02u\"%03u"),
-		 days, hours, minutes, seconds, gametime);
+	PARSE_GAMETIME(buf, gametime);
 	m_gametime.set_text(buf);
-
-	sprintf
-		(buf, "%i %s", gpdp.get_player_nr(),
-		 ngettext(_("player"), _("players"), gpdp.get_player_nr()));
+	if (gpdp.get_player_amount() > 0) {
+		sprintf
+			(buf, "%i %s", gpdp.get_player_amount(),
+			ngettext(_("player"), _("players"), gpdp.get_player_amount()));
+	} else {
+		sprintf(buf, "%s", _("Unknown"));
+	}
 	m_players_label.set_text(buf);
 	m_win_condition.set_text(gpdp.get_win_condition());
 }
@@ -233,6 +256,7 @@
 			(&igbase, _("Save Game Error!!"), s, UI::WLMessageBox::OK);
 		mbox.run();
 	}
+	game.save_handler().set_current_filename(complete_filename);
 }
 
 struct SaveWarnMessageBox : public UI::WLMessageBox {

=== modified file 'src/wui/game_main_menu_save_game.h'
--- src/wui/game_main_menu_save_game.h	2013-07-13 15:17:51 +0000
+++ src/wui/game_main_menu_save_game.h	2013-07-15 16:07:32 +0000
@@ -28,7 +28,6 @@
 #include "ui_basic/unique_window.h"
 
 #include "i18n.h"
-
 #include "ref_cast.h"
 
 class Interactive_GameBase;
@@ -54,7 +53,7 @@
 
 	UI::Listselect<std::string> m_ls;
 	UI::EditBox * m_editbox;
-	UI::Textarea m_name_label, m_name, m_gametime_label, m_gametime, m_players_label,
+	UI::Textarea m_name_label, m_mapname, m_gametime_label, m_gametime, m_players_label,
 		m_win_condition_label, m_win_condition;
 	UI::Button * m_button_ok;
 	std::string m_curdir;


Follow ups