The Battle for Wesnoth  1.19.19+dev
replay.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2025
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 "color.hpp"
24 #include "map/location.hpp"
25 
26 #include <chrono>
27 #include <iterator>
28 
30 class terrain_label;
31 class config;
32 
33 class chat_msg {
34 public:
35  const std::string &text() const { return text_; }
36  const std::string &nick() const { return nick_; }
37  const color_t& color() const { return color_; }
38  const auto& time() const { return time_; }
39  chat_msg(const config &cfg);
40  virtual ~chat_msg();
41 private:
43  std::string nick_;
44  std::string text_;
45  std::chrono::system_clock::time_point time_;
46 };
47 
49 {
50  // Chat and similar actions that don't change the gamestate
51  UNSYNCED,
52  // Local choices
53  DEPENDENT,
54  // Commands the invoke a synced user actions
55  SYNCED,
56  // The actions has a wrong format.
57  INVALID
58 };
59 
60 class replay
61 {
62 public:
63  explicit replay(replay_recorder_base& base);
64 
65 
66  void add_start();
67  void add_surrender(int side_number);
68  void add_countdown_update(int value,int team);
69 
70  void add_synced_command(const std::string& name, const config& command);
71  void init_side();
72  /*
73  returns a reference to the newest config that us not dependent or has undo =no
74 
75  */
77  /**
78  * adds a user_input to the replay
79  * @param name The tag name of the config to add
80  * @param input the contents of the config to add
81  * @param from_side the side that had to make the decision, -1 for 'server'
82  */
83  void user_input(const std::string &name, const config &input, int from_side);
84  void add_label(const terrain_label*);
85  void clear_labels(const std::string&, bool);
86  void add_rename(const std::string& name, const map_location& loc);
87  void end_turn(int next_player_number);
89  void add_log_data(const std::string &key, const std::string &var);
90  void add_log_data(const std::string &category, const std::string &key, const std::string &var);
91  void add_log_data(const std::string &category, const std::string &key, const config& c);
92 
93  /**
94  adds a chat message if it wasn't added yet.
95  @returns true if a message location was added
96  */
98  bool add_chat_message_location(int pos);
99  void speak(const config& cfg);
100  const std::vector<chat_msg>& build_chat_log() const;
101 
102  //get data range will get a range of moves from the replay system.
103  //if data_type is 'ALL_DATA' then it will return all data in this range
104  //except for undoable data that has already been sent. If data_type is
105  //NON_UNDO_DATA, then it will only retrieve undoable data, and will mark
106  //it as already sent.
107  //undoable data includes moves such as placing a label or speaking, which is
108  //ignored by the undo system.
111 
112  void undo();
113  /*
114  undoes the last move and puts it into given config to be reone with redo
115  The retuned config also contains the depended commands for that user action.
116  This is needed be because we also want to readd those dependent commands to the replay when redoing the command.
117  */
118  void undo_cut(config& dst);
119  /*
120  puts the given config which was cut with undo_cut back in the replay.
121  */
122  void redo(const config& dst, bool set_to_end = false);
123 
124  void start_replay();
125  void revert_action();
128 
129  bool at_end() const;
130  void set_to_end();
131 
132  bool empty() const;
133 
136 
137  int ncommands() const;
138 
139  static void process_error(const std::string& msg);
140  /*
141  adds a [start] at the begnning of the replay if there is none.
142  returns true if a [start] was added.
143  */
146 private:
147 
148  void add_chat_log_entry(const config &speak, std::back_insert_iterator< std::vector<chat_msg>> &i) const;
149 
150  config &command(int) const;
151  void remove_command(int);
152  /** Adds a new empty command to the command list at the end.
153  *
154  * @return a reference to the added command
155  */
156  config& add_command();
157  /**
158  * adds a new command to the command list at the current position.
159  *
160  * @return a reference to the added command
161  */
165  std::vector<int> message_locations;
166 };
167 
169 {
176 };
177 
179 
180 //replays up to one turn from the recorder object
181 //returns true if it got to the end of the turn without data running out
182 REPLAY_RETURN do_replay(bool one_move = false);
183 
184 REPLAY_RETURN do_replay_handle(bool one_move = false);
map_location loc
Definition: move.cpp:172
virtual ~chat_msg()
Definition: replay.cpp:162
std::string text_
Definition: replay.hpp:44
std::string nick_
Definition: replay.hpp:43
std::chrono::system_clock::time_point time_
Definition: replay.hpp:45
const std::string & text() const
Definition: replay.hpp:35
const color_t & color() const
Definition: replay.hpp:37
color_t color_
Definition: replay.hpp:42
const std::string & nick() const
Definition: replay.hpp:36
chat_msg(const config &cfg)
Definition: replay.cpp:143
const auto & time() const
Definition: replay.hpp:38
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:157
int ncommands() const
Definition: replay.cpp:566
config * peek_next_action()
Definition: replay.cpp:617
void add_start()
Definition: replay.cpp:211
void add_rename(const std::string &name, const map_location &loc)
Definition: replay.cpp:278
config & add_nonundoable_command()
adds a new command to the command list at the current position.
Definition: replay.cpp:581
bool add_start_if_not_there_yet()
Definition: replay.cpp:659
void undo_cut(config &dst)
Definition: replay.cpp:486
void set_to_end()
Definition: replay.cpp:635
config & get_last_real_command()
Definition: replay.cpp:422
void add_config(const config &cfg, MARK_SENT mark=MARK_AS_UNSENT)
Definition: replay.cpp:645
void add_label(const terrain_label *)
Definition: replay.cpp:257
void add_synced_command(const std::string &name, const config &command)
Definition: replay.cpp:232
MARK_SENT
Definition: replay.hpp:134
@ MARK_AS_SENT
Definition: replay.hpp:134
@ MARK_AS_UNSENT
Definition: replay.hpp:134
DATA_TYPE
Definition: replay.hpp:109
@ ALL_DATA
Definition: replay.hpp:109
@ NON_UNDO_DATA
Definition: replay.hpp:109
void end_turn(int next_player_number)
Definition: replay.cpp:289
void add_surrender(int side_number)
Definition: replay.cpp:218
void speak(const config &cfg)
Definition: replay.cpp:335
void clear_labels(const std::string &, bool)
Definition: replay.cpp:268
const std::vector< chat_msg > & build_chat_log() const
Definition: replay.cpp:364
config & command(int) const
Definition: replay.cpp:560
replay(replay_recorder_base &base)
Definition: replay.cpp:166
void remove_command(int)
Definition: replay.cpp:350
void user_input(const std::string &name, const config &input, int from_side)
adds a user_input to the replay
Definition: replay.cpp:242
void redo(const config &dst, bool set_to_end=false)
Definition: replay.cpp:401
bool at_end() const
Definition: replay.cpp:629
void add_unit_checksum(const map_location &loc, config &cfg)
Definition: replay.cpp:190
void revert_action()
Definition: replay.cpp:598
void undo()
Definition: replay.cpp:554
std::vector< int > message_locations
Definition: replay.hpp:165
static void process_error(const std::string &msg)
Definition: replay.cpp:183
void init_side()
Definition: replay.cpp:203
void start_replay()
Definition: replay.cpp:593
void delete_upcoming_commands()
Definition: replay.cpp:172
config get_unsent_commands(DATA_TYPE data_type)
Definition: replay.cpp:381
void add_countdown_update(int value, int team)
Definition: replay.cpp:224
replay_recorder_base * base_
Definition: replay.hpp:163
bool add_chat_message_location()
adds a chat message if it wasn't added yet.
Definition: replay.cpp:318
bool empty() const
Definition: replay.cpp:640
int sent_upto_
Definition: replay.hpp:164
void add_chat_log_entry(const config &speak, std::back_insert_iterator< std::vector< chat_msg >> &i) const
Definition: replay.cpp:342
void add_log_data(const std::string &key, const std::string &var)
Definition: replay.cpp:298
config & add_command()
Adds a new empty command to the command list at the end.
Definition: replay.cpp:571
config * get_next_action()
Definition: replay.cpp:605
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:74
To store label data Class implements logic for rendering.
Definition: label.hpp:111
const config * cfg
std::size_t i
Definition: function.cpp:1031
int side_number
Definition: game_info.hpp:40
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
REPLAY_ACTION_TYPE get_replay_action_type(const config &command)
Definition: replay.cpp:675
REPLAY_RETURN do_replay(bool one_move=false)
Definition: replay.cpp:690
REPLAY_RETURN
Definition: replay.hpp:169
@ REPLAY_FOUND_DEPENDENT
Definition: replay.hpp:171
@ REPLAY_FOUND_INIT_TURN
Definition: replay.hpp:173
@ REPLAY_FOUND_END_LEVEL
Definition: replay.hpp:175
@ REPLAY_RETURN_AT_END
Definition: replay.hpp:170
@ REPLAY_FOUND_END_MOVE
Definition: replay.hpp:174
@ REPLAY_FOUND_END_TURN
Definition: replay.hpp:172
REPLAY_ACTION_TYPE
Definition: replay.hpp:49
REPLAY_RETURN do_replay_handle(bool one_move=false)
Definition: replay.cpp:707
rect dst
Location on the final composed sheet.
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:51
Encapsulates the map of the game.
Definition: location.hpp:46
mock_char c
static std::string mark
Definition: tstring.cpp:70