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