mail[Wesnoth-commits] r34174 - in /trunk/src: gamestatus.cpp gamestatus.hpp menu_events.cpp playcampaign.cpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by joerg . hinrichs on March 27, 2009 - 00:53:
Author: jhinrichs
Date: Fri Mar 27 00:53:57 2009
New Revision: 34174

URL: http://svn.gna.org/viewcvs/wesnoth?rev=34174&view=rev
Log:
Some little savegame refactoring: Rename write_game to write_snapshot and 
move it into the game_state class.

Modified:
    trunk/src/gamestatus.cpp
    trunk/src/gamestatus.hpp
    trunk/src/menu_events.cpp
    trunk/src/playcampaign.cpp

Modified: trunk/src/gamestatus.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gamestatus.cpp?rev=34174&r1=34173&r2=34174&view=diff
==============================================================================
--- trunk/src/gamestatus.cpp (original)
+++ trunk/src/gamestatus.cpp Fri Mar 27 00:53:57 2009
@@ -43,6 +43,8 @@
 
 #ifdef _WIN32
 #include <windows.h>
+
+static void write_player(const player_info& player, config& cfg);
 
 /**
  * conv_ansi_utf8()
@@ -565,115 +567,37 @@
        }
 }
 
-static void write_player(const player_info& player, config& cfg)
-{
-       cfg["name"] = player.name;
-
-       char buf[50];
-       snprintf(buf,sizeof(buf),"%d",player.gold);
-
-       cfg["gold"] = buf;
-
-       cfg["gold_add"] = player.gold_add ? "yes" : "no";
-
-       for(std::vector<unit>::const_iterator i = 
player.available_units.begin();
-           i != player.available_units.end(); ++i) {
-               config new_cfg;
-               i->write(new_cfg);
-               cfg.add_child("unit",new_cfg);
-               DBG_NG << "added unit '" << new_cfg["id"] << "' to player '" 
<< player.name << "'\n";
-       }
-
-       std::stringstream can_recruit;
-       
std::copy(player.can_recruit.begin(),player.can_recruit.end(),std::ostream_iterator<std::string>(can_recruit,","));
-       std::string can_recruit_str = can_recruit.str();
-
-       // Remove the trailing comma
-       if(can_recruit_str.empty() == false) {
-               can_recruit_str.resize(can_recruit_str.size()-1);
-       }
-
-       cfg["can_recruit"] = can_recruit_str;
-}
-
-void write_players(game_state& gamestate, config& cfg)
-{
-       for(std::map<std::string, player_info>::const_iterator 
i=gamestate.players.begin();
-               i!=gamestate.players.end(); ++i)
-       {
-               config new_cfg;
-               write_player(i->second, new_cfg);
-               new_cfg["save_id"]=i->first;
-               cfg.add_child("player", new_cfg);
-       }
-}
-
-static void write_player(config_writer &out, const player_info& player)
-{
-       out.write_key_val("name", player.name);
-
-       char buf[50];
-       snprintf(buf,sizeof(buf),"%d",player.gold);
-
-       out.write_key_val("gold", buf);
-
-       const std::string gold_add = player.gold_add ? "true" : "false";
-       out.write_key_val("gold_add", gold_add);
-
-       for(std::vector<unit>::const_iterator i = 
player.available_units.begin();
-           i != player.available_units.end(); ++i) {
-               config new_cfg;
-               i->write(new_cfg);
-               out.write_child("unit",new_cfg);
-               DBG_NG << "added unit '" << new_cfg["id"] << "' to player '" 
<< player.name << "'\n";
-       }
-
-       std::stringstream can_recruit;
-       
std::copy(player.can_recruit.begin(),player.can_recruit.end(),std::ostream_iterator<std::string>(can_recruit,","));
-       std::string can_recruit_str = can_recruit.str();
-
-       // Remove the trailing comma
-       if(can_recruit_str.empty() == false) {
-               can_recruit_str.resize(can_recruit_str.size()-1);
-       }
-
-       out.write_key_val("can_recruit", can_recruit_str);
-}
-
-/** @deprecated, use other write_game below. */
-void write_game(const game_state& gamestate, config& cfg)
+void game_state::write_snapshot(config& cfg) const
 {
        log_scope("write_game");
-       cfg["label"] = gamestate.label;
-       cfg["history"] = gamestate.history;
-       cfg["abbrev"] = gamestate.abbrev;
+       cfg["label"] = label;
+       cfg["history"] = history;
+       cfg["abbrev"] = abbrev;
        cfg["version"] = game_config::version;
 
-       cfg["scenario"] = gamestate.scenario;
-       cfg["next_scenario"] = gamestate.next_scenario;
-
-       cfg["completion"] = gamestate.completion;
-
-       cfg["campaign"] = gamestate.campaign;
-
-       cfg["campaign_type"] = gamestate.campaign_type;
-
-       cfg["difficulty"] = gamestate.difficulty;
-
-       cfg["campaign_define"] = gamestate.campaign_define;
-       cfg["campaign_extra_defines"] = 
utils::join(gamestate.campaign_xtra_defines);
+       cfg["scenario"] = scenario;
+       cfg["next_scenario"] = next_scenario;
+
+       cfg["completion"] = completion;
+
+       cfg["campaign"] = campaign;
+       cfg["campaign_type"] = campaign_type;
+       cfg["difficulty"] = difficulty;
+
+       cfg["campaign_define"] = campaign_define;
+       cfg["campaign_extra_defines"] = utils::join(campaign_xtra_defines);
        cfg["next_underlying_unit_id"] = 
lexical_cast<std::string>(n_unit::id_manager::instance().get_save_id());
 
-       cfg["random_seed"] = 
lexical_cast<std::string>(gamestate.rng().get_random_seed());
-       cfg["random_calls"] = 
lexical_cast<std::string>(gamestate.rng().get_random_calls());
-
-       cfg["end_text"] = gamestate.end_text;
-       cfg["end_text_duration"] = str_cast<unsigned 
int>(gamestate.end_text_duration);
-
-       cfg.add_child("variables",gamestate.get_variables());
-
-       for(std::map<std::string, wml_menu_item *>::const_iterator 
j=gamestate.wml_menu_items.begin();
-           j!=gamestate.wml_menu_items.end(); ++j) {
+       cfg["random_seed"] = 
lexical_cast<std::string>(rng_.get_random_seed());
+       cfg["random_calls"] = 
lexical_cast<std::string>(rng_.get_random_calls());
+
+       cfg["end_text"] = end_text;
+       cfg["end_text_duration"] = str_cast<unsigned int>(end_text_duration);
+
+       cfg.add_child("variables", variables);
+
+       for(std::map<std::string, wml_menu_item *>::const_iterator 
j=wml_menu_items.begin();
+           j!=wml_menu_items.end(); ++j) {
                config new_cfg;
                new_cfg["id"]=j->first;
                new_cfg["image"]=j->second->image;
@@ -688,13 +612,88 @@
                cfg.add_child("menu_item", new_cfg);
        }
 
+       for(std::map<std::string, player_info>::const_iterator 
i=players.begin();
+           i!=players.end(); ++i) {
+               config new_cfg;
+               ::write_player(i->second, new_cfg);
+               new_cfg["save_id"]=i->first;
+               cfg.add_child("player", new_cfg);
+       }
+}
+
+static void write_player(const player_info& player, config& cfg)
+{
+       cfg["name"] = player.name;
+
+       char buf[50];
+       snprintf(buf,sizeof(buf),"%d",player.gold);
+
+       cfg["gold"] = buf;
+
+       cfg["gold_add"] = player.gold_add ? "yes" : "no";
+
+       for(std::vector<unit>::const_iterator i = 
player.available_units.begin();
+           i != player.available_units.end(); ++i) {
+               config new_cfg;
+               i->write(new_cfg);
+               cfg.add_child("unit",new_cfg);
+               DBG_NG << "added unit '" << new_cfg["id"] << "' to player '" 
<< player.name << "'\n";
+       }
+
+       std::stringstream can_recruit;
+       
std::copy(player.can_recruit.begin(),player.can_recruit.end(),std::ostream_iterator<std::string>(can_recruit,","));
+       std::string can_recruit_str = can_recruit.str();
+
+       // Remove the trailing comma
+       if(can_recruit_str.empty() == false) {
+               can_recruit_str.resize(can_recruit_str.size()-1);
+       }
+
+       cfg["can_recruit"] = can_recruit_str;
+}
+
+void write_players(game_state& gamestate, config& cfg)
+{
        for(std::map<std::string, player_info>::const_iterator 
i=gamestate.players.begin();
-           i!=gamestate.players.end(); ++i) {
+               i!=gamestate.players.end(); ++i)
+       {
                config new_cfg;
                write_player(i->second, new_cfg);
                new_cfg["save_id"]=i->first;
                cfg.add_child("player", new_cfg);
        }
+}
+
+static void write_player(config_writer &out, const player_info& player)
+{
+       out.write_key_val("name", player.name);
+
+       char buf[50];
+       snprintf(buf,sizeof(buf),"%d",player.gold);
+
+       out.write_key_val("gold", buf);
+
+       const std::string gold_add = player.gold_add ? "true" : "false";
+       out.write_key_val("gold_add", gold_add);
+
+       for(std::vector<unit>::const_iterator i = 
player.available_units.begin();
+           i != player.available_units.end(); ++i) {
+               config new_cfg;
+               i->write(new_cfg);
+               out.write_child("unit",new_cfg);
+               DBG_NG << "added unit '" << new_cfg["id"] << "' to player '" 
<< player.name << "'\n";
+       }
+
+       std::stringstream can_recruit;
+       
std::copy(player.can_recruit.begin(),player.can_recruit.end(),std::ostream_iterator<std::string>(can_recruit,","));
+       std::string can_recruit_str = can_recruit.str();
+
+       // Remove the trailing comma
+       if(can_recruit_str.empty() == false) {
+               can_recruit_str.resize(can_recruit_str.size()-1);
+       }
+
+       out.write_key_val("can_recruit", can_recruit_str);
 }
 
 void write_game(config_writer &out, const game_state& gamestate, 
WRITE_GAME_MODE mode)

Modified: trunk/src/gamestatus.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gamestatus.hpp?rev=34174&r1=34173&r2=34174&view=diff
==============================================================================
--- trunk/src/gamestatus.hpp (original)
+++ trunk/src/gamestatus.hpp Fri Mar 27 00:53:57 2009
@@ -118,6 +118,9 @@
 
        void set_menu_items(const config::const_child_itors &menu_items);
 
+       //write the gamestate into a config object
+       void write_snapshot(config& cfg) const;
+
        // Variable access
 
        t_string& get_variable(const std::string& varname);
@@ -317,7 +320,6 @@
 void read_save_file(const std::string& name, config& cfg, std::string* 
error_log);
 
 void write_players(game_state& gamestate, config& cfg);
-void write_game(const game_state& gamestate, config& cfg);
 void write_game(config_writer &out, const game_state& gamestate, 
WRITE_GAME_MODE mode=WRITE_FULL_GAME);
 
 /** Returns true iff there is already a savegame with that name. */

Modified: trunk/src/menu_events.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/menu_events.cpp?rev=34174&r1=34173&r2=34174&view=diff
==============================================================================
--- trunk/src/menu_events.cpp (original)
+++ trunk/src/menu_events.cpp Fri Mar 27 00:53:57 2009
@@ -713,8 +713,7 @@
 
                sound::write_music_play_list(start);
 
-               write_game(gamestate_, start);
-               start["completion"] = gamestate_.completion;
+               gamestate_.write_snapshot(start);
 
                //write out the current state of the map
                start["map_data"] = map_.write();

Modified: trunk/src/playcampaign.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/playcampaign.cpp?rev=34174&r1=34173&r2=34174&view=diff
==============================================================================
--- trunk/src/playcampaign.cpp (original)
+++ trunk/src/playcampaign.cpp Fri Mar 27 00:53:57 2009
@@ -545,7 +545,7 @@
                                // Adds player information, and other state
                                // information, to the configuration object
                                assert(cfg.child("store_next_scenario") != 
NULL);
-                               write_game(gamestate, 
*cfg.child("store_next_scenario"));
+                               
gamestate.write_snapshot(*cfg.child("store_next_scenario"));
                                network::send_data(cfg, 0, true);
                        }
                }




Related Messages


Powered by MHonArc, Updated Fri Mar 27 02:00:26 2009