The Battle for Wesnoth  1.15.12+dev
replay.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Replay control code.
18  */
19 
20 #pragma once
21 
22 #include "map/location.hpp"
23 
24 #include <ctime>
25 #include <deque>
26 #include <iterator>
27 #include <map>
28 #include <set>
30 class terrain_label;
31 class config;
32 struct time_of_day;
33 
34 class chat_msg {
35 public:
36  const std::string &text() const { return text_; }
37  const std::string &nick() const { return nick_; }
38  const std::string &color() const { return color_; }
39  const std::time_t &time() const { return time_; }
40  chat_msg(const config &cfg);
41  virtual ~chat_msg();
42 private:
43  std::string color_;
44  std::string nick_;
45  std::string text_;
46  std::time_t time_;
47 };
48 
49 class replay
50 {
51 public:
52  explicit replay(replay_recorder_base& base);
53 
54 
55  void add_start();
56  void add_surrender(int side_number);
57  void add_countdown_update(int value,int team);
58 
59  void add_synced_command(const std::string& name, const config& command);
60  void init_side();
61  /*
62  returns a reference to the newest config that us not dependent or has undo =no
63 
64  */
65  config& get_last_real_command();
66  /**
67  * adds a user_input to the replay
68  * @param name The tag name of the config to add
69  * @param input the contents of the config to add
70  * @param from_side the side that had to make the decision, -1 for 'server'
71  */
72  void user_input(const std::string &name, const config &input, int from_side);
73  void add_label(const terrain_label*);
74  void clear_labels(const std::string&, bool);
75  void add_rename(const std::string& name, const map_location& loc);
76  void end_turn(int next_player_number);
77  void add_unit_checksum(const map_location& loc,config& cfg);
78  void add_log_data(const std::string &key, const std::string &var);
79  void add_log_data(const std::string &category, const std::string &key, const std::string &var);
80  void add_log_data(const std::string &category, const std::string &key, const config& c);
81 
82  /**
83  adds a chat message if it wasn't added yet.
84  @returns true if a message location was added
85  */
86  bool add_chat_message_location();
87  bool add_chat_message_location(int pos);
88  void speak(const config& cfg);
89  const std::vector<chat_msg>& build_chat_log() const;
90 
91  //get data range will get a range of moves from the replay system.
92  //if data_type is 'ALL_DATA' then it will return all data in this range
93  //except for undoable data that has already been sent. If data_type is
94  //NON_UNDO_DATA, then it will only retrieve undoable data, and will mark
95  //it as already sent.
96  //undoable data includes moves such as placing a label or speaking, which is
97  //ignored by the undo system.
98  enum DATA_TYPE { ALL_DATA, NON_UNDO_DATA };
99  config get_data_range(int cmd_start, int cmd_end, DATA_TYPE data_type=ALL_DATA) const;
100 
101  void undo();
102  /*
103  undoes the last move and puts it into given config to be reone with redo
104  The retuned config also contains the depended commands for that user action.
105  This is needed be because we also want to readd those dependent commands to the replay when redoing the command.
106  */
107  void undo_cut(config& dst);
108  /*
109  puts the given config which was cut with undo_cut back in the replay.
110  */
111  void redo(const config& dst, bool set_to_end = false);
112 
113  void start_replay();
114  void revert_action();
115  config* get_next_action();
116 
117  bool at_end() const;
118  void set_to_end();
119 
120  bool empty() const;
121 
122  enum MARK_SENT { MARK_AS_UNSENT, MARK_AS_SENT };
123  void add_config(const config& cfg, MARK_SENT mark=MARK_AS_UNSENT);
124 
125  int ncommands() const;
126 
127  static void process_error(const std::string& msg);
128  /*
129  adds a [start] at the begnning of the replay if there is none.
130  returns true if a [start] was added.
131  */
132  bool add_start_if_not_there_yet();
133  void delete_upcoming_commands();
134 private:
135 
136  void add_chat_log_entry(const config &speak, std::back_insert_iterator< std::vector<chat_msg>> &i) const;
137 
138  config &command(int) const;
139  void remove_command(int);
140  /** Adds a new empty command to the command list at the end.
141  *
142  * @return a reference to the added command
143  */
144  config& add_command();
145  /**
146  * adds a new command to the command list at the current position.
147  *
148  * @return a reference to the added command
149  */
150  config& add_nonundoable_command();
152  std::vector<int> message_locations;
153 };
154 
156 {
163 };
164 //replays up to one turn from the recorder object
165 //returns true if it got to the end of the turn without data running out
166 REPLAY_RETURN do_replay(bool one_move = false);
167 
168 REPLAY_RETURN do_replay_handle(bool one_move = false);
169 
171 {
172 public:
175 
176  void sync_non_undoable();
177  void commit_and_sync();
178 private:
180  int upto_;
181 };
std::time_t time_
Definition: replay.hpp:46
DATA_TYPE
Definition: replay.hpp:98
REPLAY_RETURN do_replay(bool one_move=false)
Definition: replay.cpp:680
REPLAY_RETURN do_replay_handle(bool one_move=false)
Definition: replay.cpp:698
virtual ~chat_msg()
Definition: replay.cpp:175
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
replay_recorder_base * base_
Definition: replay.hpp:151
MARK_SENT
Definition: replay.hpp:122
REPLAY_RETURN
Definition: replay.hpp:155
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:55
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
const std::string & nick() const
Definition: replay.hpp:37
Encapsulates the map of the game.
Definition: location.hpp:37
static std::string mark
Definition: tstring.cpp:70
std::size_t i
Definition: function.cpp:940
std::string color_
Definition: replay.hpp:43
To store label data Class implements logic for rendering.
Definition: label.hpp:109
std::vector< int > message_locations
Definition: replay.hpp:152
std::string nick_
Definition: replay.hpp:44
const std::time_t & time() const
Definition: replay.hpp:39
int side_number
Definition: game_info.hpp:39
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
mock_char c
const std::string & color() const
Definition: replay.hpp:38
chat_msg(const config &cfg)
Definition: replay.cpp:144
const std::string & text() const
Definition: replay.hpp:36
std::string text_
Definition: replay.hpp:45