gamestatus.hpp

Go to the documentation of this file.
00001 /* $Id: gamestatus.hpp 25481 2008-04-02 22:10:35Z jhinrichs $ */
00002 /*
00003    Copyright (C) 2003 - 2008 by David White <dave@whitevine.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License version 2
00008    or at your option any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 //! @file gamestatus.hpp
00016 //!
00017 //!
00018 
00019 #ifndef GAME_STATUS_HPP_INCLUDED
00020 #define GAME_STATUS_HPP_INCLUDED
00021 
00022 #include "filesystem.hpp"
00023 #include "random.hpp"
00024 #include "team.hpp"
00025 #include "unit.hpp"
00026 #include "variable.hpp"
00027 
00028 #include <time.h>
00029 #include <string>
00030 #include <vector>
00031 
00032 class scoped_wml_variable;
00033 
00034 //! Object which defines the current time of day.
00035 struct time_of_day
00036 {
00037     explicit time_of_day(const config& cfg);
00038     void write(config& cfg) const;
00039 
00040     // The % bonus lawful units receive.
00041     // Chaotic units will receive -lawful_bonus.
00042     int lawful_bonus;
00043     int bonus_modified;
00044 
00045     // The image to be displayed in the game status.
00046     std::string image;
00047     t_string name;
00048     std::string id;
00049 
00050     // The image that is to be laid over all images
00051     // while this time of day lasts.
00052     std::string image_mask;
00053 
00054     // The colour modifications that should be made
00055     // to the game board to reflect the time of day.
00056     int red, green, blue;
00057 
00058     //! List of "ambient" sounds associated with this time_of_day,
00059     //! Played at the beginning of turn.
00060     std::string sounds;
00061 };
00062 
00063 struct wml_menu_item
00064 {
00065     wml_menu_item(const std::string& id, const config* cfg=NULL);
00066     std::string name;
00067     std::string image;
00068     t_string description;
00069     bool needs_select;
00070     config show_if;
00071     config filter_location;
00072     config command;
00073 };
00074 
00075 //! Information on a particular player of the game.
00076 struct player_info
00077 {
00078     player_info() :
00079         name(),
00080         gold(-1) ,
00081         gold_add(false),
00082         available_units(),
00083         can_recruit()
00084     {}
00085 
00086     std::string name;                  //!< Stores the current_player name
00087     int gold;                          //!< Amount of gold the player has saved
00088     bool gold_add;                     //!< Amount of gold is added to the
00089                                        //!< starting gold, if not it uses the highest
00090                                        //!< of the two.
00091     std::vector<unit> available_units; //!< Units the player may recall
00092     std::set<std::string> can_recruit; //!< Units the player has the ability to recruit
00093 
00094     void debug();
00095 };
00096 
00097 class game_state : public variable_set
00098 {
00099 public:
00100     game_state() :
00101         label(),
00102         version(),
00103         campaign_type(),
00104         campaign_define(),
00105         campaign_xtra_defines(),
00106         campaign(),
00107         history(),
00108         abbrev(),
00109         scenario(),
00110         next_scenario(),
00111         completion(),
00112         players(),
00113         scoped_variables(),
00114         wml_menu_items(),
00115         difficulty("NORMAL"),
00116         replay_data(),
00117         starting_pos(),
00118         snapshot(),
00119         last_selected(gamemap::location::null_location),
00120         variables(),
00121         temporaries()
00122         {}
00123 
00124     game_state(const game_state& state);
00125     game_state(const config& cfg, bool show_replay = false);
00126 
00127     ~game_state();
00128     game_state& operator=(const game_state& state);
00129 
00130     std::string label;                               //!< Name of the game (e.g. name of save file).
00131     std::string version;                             //!< Version game was created with.
00132     std::string campaign_type;                       //!< Type of the game - campaign, multiplayer etc.
00133 
00134     std::string campaign_define;                     //!< If there is a define the campaign uses to customize data
00135     std::vector<std::string> campaign_xtra_defines;  //!< more customization of data
00136 
00137     std::string campaign;                            //!< the campaign being played
00138     std::string history;                             //!< ancestral IDs
00139     std::string abbrev;                          //!< the campaign abbreviation
00140     std::string scenario;                            //!< the scenario being played
00141     std::string next_scenario;                       //!< the scenario coming next (for campaigns)
00142     std::string completion;                          //!< running. victory, or defeat
00143 
00144     //! Information about campaign players who carry resources
00145     //! from previous levels, indexed by a string identifier
00146     // (which is the leader name by default, but can be set
00147     // with the "id" attribute of the "side" tag).
00148     std::map<std::string, player_info> players;
00149 
00150     //! Return the Nth player, or NULL if no such player exists.
00151     player_info* get_player(const std::string& id);
00152 
00153     std::vector<scoped_wml_variable*> scoped_variables;
00154     std::map<std::string, wml_menu_item*> wml_menu_items;
00155 
00156     const config& get_variables() const { return variables; }
00157     void set_variables(const config& vars);
00158 
00159     void set_menu_items(const config::child_list& menu_items);
00160 
00161     // Variable access
00162 
00163     t_string& get_variable(const std::string& varname);
00164     virtual const t_string& get_variable_const(const std::string& varname) const;
00165     config& get_variable_cfg(const std::string& varname);
00166     variable_info::array_range get_variable_cfgs(const std::string& varname);
00167 
00168     void set_variable(const std::string& varname, const t_string& value);
00169     config& add_variable_cfg(const std::string& varname, const config& value=config());
00170 
00171     void clear_variable(const std::string& varname);
00172     void clear_variable_cfg(const std::string& varname); // Clears only the config children
00173 
00174     const simple_rng& rng() const { return rng_; }
00175     simple_rng& rng() { return rng_; }
00176 
00177     std::string difficulty; //!< The difficulty level the game is being played on.
00178 
00179     //! If the game is saved mid-level, we have a series of replay steps
00180     //! to take the game up to the position it was saved at.
00181     config replay_data;
00182 
00183     //! Saved starting state of the game.
00184     //! For multiplayer games, the position the game started in
00185     //! may be different to the scenario,
00186     config starting_pos;
00187 
00188     //! Snapshot of the game's current contents.
00189     //! i.e. unless the player selects to view a replay,
00190     //! the game's settings are read in from this object.
00191     config snapshot;
00192 
00193     //! the last location where a select event fired.
00194     gamemap::location last_selected;
00195 
00196 private:
00197     simple_rng rng_;
00198     config variables;
00199     mutable config temporaries; // lengths of arrays, etc.
00200     friend struct variable_info;
00201 
00202     //! Loads the recall list.
00203     void load_recall_list(const config::child_list& players);
00204 };
00205 
00206 
00207 //! Contains the global status of the game.
00208 //! Namely the current turn, the number of turns, and the time of day.
00209 class gamestatus
00210 {
00211 public:
00212     gamestatus(const config& time_cfg, int num_turns, game_state* s_o_g = NULL);
00213     void write(config& cfg) const;
00214 
00215     time_of_day get_time_of_day() const;
00216     time_of_day get_previous_time_of_day() const;
00217     time_of_day get_time_of_day(int illuminated, const gamemap::location& loc) const;
00218     time_of_day get_time_of_day(int illuminated, const gamemap::location& loc, int n_turn) const;
00219     bool set_time_of_day(int);
00220     size_t turn() const;
00221     int number_of_turns() const;
00222     void modify_turns(const std::string& mod);
00223     void add_turns(int num);
00224 
00225     //! Function to move to the next turn.
00226     //! Returns true iff time has expired.
00227     bool next_turn();
00228 
00229     static bool is_start_ToD(const std::string&);
00230 
00231     //! @todo FIXME: since gamestatus may be constructed with NULL game_state* (by default),
00232     //! you should not rely on this function to return the current game_state.
00233     const game_state& sog() const{return(*state_of_game_);}
00234 
00235     std::vector<team> *teams;
00236 
00237 private:
00238     void set_start_ToD(config&, game_state*);
00239     time_of_day get_time_of_day_turn(int nturn) const;
00240     void next_time_of_day();
00241 
00242     std::vector<time_of_day> times_;
00243 
00244     struct area_time_of_day {
00245         area_time_of_day() :
00246             xsrc(),
00247             ysrc(),
00248             times(),
00249             hexes()
00250             {}
00251 
00252         std::string xsrc, ysrc;
00253         std::vector<time_of_day> times;
00254         std::set<gamemap::location> hexes;
00255     };
00256 
00257     std::vector<area_time_of_day> areas_;
00258 
00259     size_t turn_;
00260     int numTurns_;
00261     int currentTime_;
00262     const game_state* state_of_game_;
00263 };
00264 
00265 std::string generate_game_uuid();
00266 
00267 //! Holds all the data needed to start a scenario.
00268 //! I.e. this is the object serialized to disk when saving/loading a game.
00269 //! It is also the object which needs to be created to start a new game.
00270 struct save_info {
00271     save_info(const std::string& n, time_t t) : name(n), time_modified(t) {}
00272     std::string name;
00273     time_t time_modified;
00274 };
00275 
00276 //! Get a list of available saves.
00277 std::vector<save_info> get_saves_list(const std::string* dir = NULL, const std::string* filter = NULL);
00278 
00279 enum WRITE_GAME_MODE { WRITE_SNAPSHOT_ONLY, WRITE_FULL_GAME };
00280 
00281 void read_save_file(const std::string& name, config& cfg, std::string* error_log);
00282 
00283 void write_game(const game_state& gamestate, config& cfg, WRITE_GAME_MODE mode=WRITE_FULL_GAME);
00284 void write_game(config_writer &out, const game_state& gamestate, WRITE_GAME_MODE mode=WRITE_FULL_GAME);
00285 
00286 //! Returns true iff there is already a savegame with that name.
00287 bool save_game_exists(const std::string & name);
00288 
00289 //! Throws game::save_game_failed
00290 scoped_ostream open_save_game(const std::string &label);
00291 void finish_save_game(config_writer &out, const game_state& gamestate, const std::string &label);
00292 
00293 //! Load/Save games.
00294 void load_game(const std::string& name, game_state& gamestate, std::string* error_log);
00295 void load_game_summary(const std::string& name, config& cfg_summary, std::string* error_log);
00296 //! Throws gamestatus::save_game_failed
00297 void save_game(const game_state& gamestate);
00298 
00299 //! Delete a savegame.
00300 void delete_game(const std::string& name);
00301 
00302 config& save_summary(const std::string& save);
00303 
00304 void write_save_index();
00305 
00306 void replace_underbar2space(std::string &name);
00307 
00308 #endif

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs