The Battle for Wesnoth  1.15.2+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 from_side the side that had to make the decision, -1 for 'server'
69  */
70  void user_input(const std::string &, const config &, int from_side);
71  void add_label(const terrain_label*);
72  void clear_labels(const std::string&, bool);
73  void add_rename(const std::string& name, const map_location& loc);
74  void end_turn(int next_player_number);
75  void add_unit_checksum(const map_location& loc,config& cfg);
76  void add_log_data(const std::string &key, const std::string &var);
77  void add_log_data(const std::string &category, const std::string &key, const std::string &var);
78  void add_log_data(const std::string &category, const std::string &key, const config& c);
79 
80  /**
81  adds a chat message if it wasn't added yet.
82  @returns true if a message location was added
83  */
84  bool add_chat_message_location();
85  bool add_chat_message_location(int pos);
86  void speak(const config& cfg);
87  const std::vector<chat_msg>& build_chat_log() const;
88 
89  //get data range will get a range of moves from the replay system.
90  //if data_type is 'ALL_DATA' then it will return all data in this range
91  //except for undoable data that has already been sent. If data_type is
92  //NON_UNDO_DATA, then it will only retrieve undoable data, and will mark
93  //it as already sent.
94  //undoable data includes moves such as placing a label or speaking, which is
95  //ignored by the undo system.
96  enum DATA_TYPE { ALL_DATA, NON_UNDO_DATA };
97  config get_data_range(int cmd_start, int cmd_end, DATA_TYPE data_type=ALL_DATA) const;
98 
99  void undo();
100  /*
101  undoes the last move and puts it into given config to be reone with redo
102  The retuned config also contains the depended commands for that user action.
103  This is needed be because we also want to readd those dependent commands to the replay when redoing the command.
104  */
105  void undo_cut(config& dst);
106  /*
107  puts the given config which was cut with undo_cut back in the replay.
108  */
109  void redo(const config& dst, bool set_to_end = false);
110 
111  void start_replay();
112  void revert_action();
113  config* get_next_action();
114 
115  bool at_end() const;
116  void set_to_end();
117 
118  bool empty() const;
119 
120  enum MARK_SENT { MARK_AS_UNSENT, MARK_AS_SENT };
121  void add_config(const config& cfg, MARK_SENT mark=MARK_AS_UNSENT);
122 
123  int ncommands() const;
124 
125  static void process_error(const std::string& msg);
126  /*
127  adds a [start] at the begnning of the replay if there is none.
128  returns true if a [start] was added.
129  */
130  bool add_start_if_not_there_yet();
131  void delete_upcoming_commands();
132 private:
133 
134  void add_chat_log_entry(const config &speak, std::back_insert_iterator< std::vector<chat_msg>> &i) const;
135 
136  config &command(int) const;
137  void remove_command(int);
138  /** Adds a new empty command to the command list at the end.
139  *
140  * @return a reference to the added command
141  */
142  config& add_command();
143  /**
144  * adds a new command to the command list at the current position.
145  *
146  * @return a reference to the added command
147  */
148  config& add_nonundoable_command();
150  std::vector<int> message_locations;
151 };
152 
154 {
161 };
162 //replays up to one turn from the recorder object
163 //returns true if it got to the end of the turn without data running out
164 REPLAY_RETURN do_replay(bool one_move = false);
165 
166 REPLAY_RETURN do_replay_handle(bool one_move = false);
167 
169 {
170 public:
173 
174  void sync_non_undoable();
175  void commit_and_sync();
176 private:
178  int upto_;
179 };
std::time_t time_
Definition: replay.hpp:46
DATA_TYPE
Definition: replay.hpp:96
REPLAY_RETURN do_replay(bool one_move=false)
Definition: replay.cpp:678
REPLAY_RETURN do_replay_handle(bool one_move=false)
Definition: replay.cpp:696
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:149
MARK_SENT
Definition: replay.hpp:120
REPLAY_RETURN
Definition: replay.hpp:153
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:57
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:42
static std::string mark
Definition: tstring.cpp:73
std::size_t i
Definition: function.cpp:933
std::string color_
Definition: replay.hpp:43
To store label data Class implements logic for rendering.
Definition: label.hpp:107
std::vector< int > message_locations
Definition: replay.hpp:150
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:68
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