replay.hpp

Go to the documentation of this file.
00001 /* $Id: replay.hpp 25333 2008-03-30 13:49:03Z 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 replay.hpp
00016 //! Replay control code.
00017 
00018 #ifndef REPLAY_H_INCLUDED
00019 #define REPLAY_H_INCLUDED
00020 
00021 #include "config.hpp"
00022 #include "gamestatus.hpp"
00023 #include "map.hpp"
00024 #include "random.hpp"
00025 
00026 class config_writer;
00027 class game_display;
00028 class terrain_label;
00029 class unit_map;
00030 
00031 struct verification_manager
00032 {
00033     verification_manager(const unit_map& units);
00034     ~verification_manager();
00035 };
00036 
00037 class replay: public rng
00038 {
00039 public:
00040     replay();
00041     explicit replay(const config& cfg);
00042 
00043     void set_save_info(const game_state& save);
00044     void set_save_info_completion(const std::string &st);
00045 
00046     void set_skip(bool skip);
00047     bool is_skipping() const;
00048 
00049     void save_game(const std::string& label, const config& snapshot,
00050                    const config& starting_pos, bool include_replay = true);
00051 
00052     void add_start();
00053     void add_recruit(int unit_index, const gamemap::location& loc);
00054     void add_recall(int unit_index, const gamemap::location& loc);
00055     void add_disband(int unit_index);
00056     void add_countdown_update(int value,int team);
00057     void add_movement(const gamemap::location& a, const gamemap::location& b);
00058     void add_attack(const gamemap::location& a, const gamemap::location& b,
00059                     int att_weapon, int def_weapon);
00060     void choose_option(int index);
00061     void text_input(std::string input);
00062     void set_random_value(const std::string& choice);
00063     void add_label(const terrain_label*);
00064     void clear_labels(const std::string&);
00065     void add_rename(const std::string& name, const gamemap::location& loc);
00066     void end_turn();
00067     void add_event(const std::string& name,
00068         const gamemap::location& loc=gamemap::location::null_location);
00069     void add_unit_checksum(const gamemap::location& loc,config* const cfg);
00070     void add_checksum_check(const gamemap::location& loc);
00071     /**
00072      * Adds an advancement to the replay, the following option command
00073      * determines which advancement option has been choosen
00074      */
00075     void add_advancement(const gamemap::location& loc);
00076 
00077     void add_chat_message_location();
00078     void speak(const config& cfg);
00079     std::string build_chat_log(const std::string& team);
00080 
00081     //get data range will get a range of moves from the replay system.
00082     //if data_type is 'ALL_DATA' then it will return all data in this range
00083     //except for undoable data that has already been sent. If data_type is
00084     //NON_UNDO_DATA, then it will only retrieve undoable data, and will mark
00085     //it as already sent.
00086     //undoable data includes moves such as placing a label or speaking, which is
00087     //ignored by the undo system.
00088     enum DATA_TYPE { ALL_DATA, NON_UNDO_DATA };
00089     config get_data_range(int cmd_start, int cmd_end, DATA_TYPE data_type=ALL_DATA);
00090     config get_last_turn(int num_turns=1);
00091 
00092     void undo();
00093 
00094     void start_replay();
00095     void revert_action();
00096     config* get_next_action();
00097     void pre_replay();
00098 
00099     bool at_end() const;
00100     void set_to_end();
00101 
00102     void clear();
00103     bool empty();
00104 
00105     enum MARK_SENT { MARK_AS_UNSENT, MARK_AS_SENT };
00106     void add_config(const config& cfg, MARK_SENT mark=MARK_AS_UNSENT);
00107 
00108     int ncommands();
00109 
00110     static void throw_error(const std::string& msg);
00111 
00112     struct error {
00113         error(const std::string& msg) : message(msg) {}
00114         std::string message;
00115     };
00116 
00117     static std::string last_replay_error;
00118 private:
00119     //generic for add_movement and add_attack
00120     void add_pos(const std::string& type,
00121                  const gamemap::location& a, const gamemap::location& b);
00122 
00123     void add_value(const std::string& type, int value);
00124 
00125     void add_chat_log_entry(const config*, std::stringstream&, const std::string&) const;
00126 
00127     const config::child_list& commands() const;
00128     void remove_command(int);
00129     /** Adds a new empty command to the command list.
00130      *
00131      * @param update_random_context  If set to false, do not update the
00132      *           random context variables: all random generation will take
00133      *           place in the previous random context. Used for commands
00134      *           for which "random context" is pointless, and which can be
00135      *           issued while some other commands are still taking place,
00136      *           like, for example, messages during combats.
00137      *
00138      * @return a pointer to the added command
00139      */
00140     config* add_command(bool update_random_context=true);
00141     config cfg_;
00142     unsigned int pos_;
00143 
00144     config* current_;
00145 
00146     game_state saveInfo_;
00147 
00148     bool skip_;
00149 
00150     std::vector<int> message_locations;
00151 };
00152 
00153 replay& get_replay_source();
00154 
00155 extern replay recorder;
00156 
00157 //replays up to one turn from the recorder object
00158 //returns true if it got to the end of the turn without data running out
00159 bool do_replay(game_display& disp, const gamemap& map,
00160     unit_map& units, std::vector<team>& teams, int team_num,
00161     const gamestatus& state, game_state& state_of_game, replay* obj=NULL);
00162 
00163 bool do_replay_handle(game_display& disp, const gamemap& map,
00164                       unit_map& units, std::vector<team>& teams, int team_num,
00165        const gamestatus& state, game_state& state_of_game,
00166     const std::string& do_untill);
00167 
00168 //an object which can be made to undo a recorded move
00169 //unless the transaction is confirmed
00170 struct replay_undo
00171 {
00172     replay_undo(replay& obj) : obj_(&obj) {}
00173     ~replay_undo() { if(obj_) obj_->undo(); }
00174     void confirm_transaction() { obj_ = NULL; }
00175 
00176 private:
00177     replay* obj_;
00178 };
00179 
00180 class replay_network_sender
00181 {
00182 public:
00183     replay_network_sender(replay& obj);
00184     ~replay_network_sender();
00185 
00186     void sync_non_undoable();
00187     void commit_and_sync();
00188 private:
00189     replay& obj_;
00190     int upto_;
00191 };
00192 
00193 #endif

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