The Battle for Wesnoth  1.19.0-dev
replay.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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 <iterator>
28 class terrain_label;
29 class config;
30 
31 class chat_msg {
32 public:
33  const std::string &text() const { return text_; }
34  const std::string &nick() const { return nick_; }
35  const std::string &color() const { return color_; }
36  const std::time_t &time() const { return time_; }
37  chat_msg(const config &cfg);
38  virtual ~chat_msg();
39 private:
40  std::string color_;
41  std::string nick_;
42  std::string text_;
43  std::time_t time_;
44 };
45 
46 class replay
47 {
48 public:
49  explicit replay(replay_recorder_base& base);
50 
51 
52  void add_start();
53  void add_surrender(int side_number);
54  void add_countdown_update(int value,int team);
55 
56  void add_synced_command(const std::string& name, const config& command);
57  void init_side();
58  /*
59  returns a reference to the newest config that us not dependent or has undo =no
60 
61  */
63  /**
64  * adds a user_input to the replay
65  * @param name The tag name of the config to add
66  * @param input the contents of the config to add
67  * @param from_side the side that had to make the decision, -1 for 'server'
68  */
69  void user_input(const std::string &name, const config &input, int from_side);
70  void add_label(const terrain_label*);
71  void clear_labels(const std::string&, bool);
72  void add_rename(const std::string& name, const map_location& loc);
73  void end_turn(int next_player_number);
74  void add_unit_checksum(const map_location& loc,config& cfg);
75  void add_log_data(const std::string &key, const std::string &var);
76  void add_log_data(const std::string &category, const std::string &key, const std::string &var);
77  void add_log_data(const std::string &category, const std::string &key, const config& c);
78 
79  /**
80  adds a chat message if it wasn't added yet.
81  @returns true if a message location was added
82  */
84  bool add_chat_message_location(int pos);
85  void speak(const config& cfg);
86  const std::vector<chat_msg>& build_chat_log() const;
87 
88  //get data range will get a range of moves from the replay system.
89  //if data_type is 'ALL_DATA' then it will return all data in this range
90  //except for undoable data that has already been sent. If data_type is
91  //NON_UNDO_DATA, then it will only retrieve undoable data, and will mark
92  //it as already sent.
93  //undoable data includes moves such as placing a label or speaking, which is
94  //ignored by the undo system.
96  config get_data_range(int cmd_start, int cmd_end, DATA_TYPE data_type=ALL_DATA) const;
97 
98  void undo();
99  /*
100  undoes the last move and puts it into given config to be reone with redo
101  The retuned config also contains the depended commands for that user action.
102  This is needed be because we also want to readd those dependent commands to the replay when redoing the command.
103  */
104  void undo_cut(config& dst);
105  /*
106  puts the given config which was cut with undo_cut back in the replay.
107  */
108  void redo(const config& dst, bool set_to_end = false);
109 
110  void start_replay();
111  void revert_action();
113 
114  bool at_end() const;
115  void set_to_end();
116 
117  bool empty() const;
118 
120  void add_config(const config& cfg, MARK_SENT mark=MARK_AS_UNSENT);
121 
122  int ncommands() const;
123 
124  static void process_error(const std::string& msg);
125  /*
126  adds a [start] at the begnning of the replay if there is none.
127  returns true if a [start] was added.
128  */
131 private:
132 
133  void add_chat_log_entry(const config &speak, std::back_insert_iterator< std::vector<chat_msg>> &i) const;
134 
135  config &command(int) const;
136  void remove_command(int);
137  /** Adds a new empty command to the command list at the end.
138  *
139  * @return a reference to the added command
140  */
141  config& add_command();
142  /**
143  * adds a new command to the command list at the current position.
144  *
145  * @return a reference to the added command
146  */
149  std::vector<int> message_locations;
150 };
151 
153 {
160 };
161 //replays up to one turn from the recorder object
162 //returns true if it got to the end of the turn without data running out
163 REPLAY_RETURN do_replay(bool one_move = false);
164 
165 REPLAY_RETURN do_replay_handle(bool one_move = false);
166 
168 {
169 public:
172 
173  void sync_non_undoable();
174  void commit_and_sync();
175 private:
177  int upto_;
178 };
const std::string & color() const
Definition: replay.hpp:35
virtual ~chat_msg()
Definition: replay.cpp:178
std::string text_
Definition: replay.hpp:42
std::time_t time_
Definition: replay.hpp:43
std::string color_
Definition: replay.hpp:40
const std::time_t & time() const
Definition: replay.hpp:36
std::string nick_
Definition: replay.hpp:41
const std::string & text() const
Definition: replay.hpp:33
const std::string & nick() const
Definition: replay.hpp:34
chat_msg(const config &cfg)
Definition: replay.cpp:147
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
void sync_non_undoable()
Definition: replay.cpp:914
replay_network_sender(replay &obj)
Definition: replay.cpp:903
int ncommands() const
Definition: replay.cpp:580
void add_start()
Definition: replay.cpp:226
void add_rename(const std::string &name, const map_location &loc)
Definition: replay.cpp:293
config & add_nonundoable_command()
adds a new command to the command list at the current position.
Definition: replay.cpp:595
bool add_start_if_not_there_yet()
Definition: replay.cpp:662
void undo_cut(config &dst)
Definition: replay.cpp:500
void set_to_end()
Definition: replay.cpp:638
config & get_last_real_command()
Definition: replay.cpp:436
void add_config(const config &cfg, MARK_SENT mark=MARK_AS_UNSENT)
Definition: replay.cpp:648
void add_label(const terrain_label *)
Definition: replay.cpp:272
void add_synced_command(const std::string &name, const config &command)
Definition: replay.cpp:247
MARK_SENT
Definition: replay.hpp:119
@ MARK_AS_SENT
Definition: replay.hpp:119
@ MARK_AS_UNSENT
Definition: replay.hpp:119
DATA_TYPE
Definition: replay.hpp:95
@ ALL_DATA
Definition: replay.hpp:95
@ NON_UNDO_DATA
Definition: replay.hpp:95
void end_turn(int next_player_number)
Definition: replay.cpp:304
void add_surrender(int side_number)
Definition: replay.cpp:233
void speak(const config &cfg)
Definition: replay.cpp:350
void clear_labels(const std::string &, bool)
Definition: replay.cpp:283
const std::vector< chat_msg > & build_chat_log() const
Definition: replay.cpp:379
config & command(int) const
Definition: replay.cpp:574
replay(replay_recorder_base &base)
Definition: replay.cpp:182
void remove_command(int)
Definition: replay.cpp:365
void user_input(const std::string &name, const config &input, int from_side)
adds a user_input to the replay
Definition: replay.cpp:257
void redo(const config &dst, bool set_to_end=false)
Definition: replay.cpp:415
bool at_end() const
Definition: replay.cpp:632
config get_data_range(int cmd_start, int cmd_end, DATA_TYPE data_type=ALL_DATA) const
Definition: replay.cpp:396
void add_unit_checksum(const map_location &loc, config &cfg)
Definition: replay.cpp:205
void revert_action()
Definition: replay.cpp:612
void undo()
Definition: replay.cpp:568
std::vector< int > message_locations
Definition: replay.hpp:149
static void process_error(const std::string &msg)
Definition: replay.cpp:198
void init_side()
Definition: replay.cpp:218
void start_replay()
Definition: replay.cpp:607
void delete_upcoming_commands()
Definition: replay.cpp:187
void add_countdown_update(int value, int team)
Definition: replay.cpp:239
replay_recorder_base * base_
Definition: replay.hpp:148
bool add_chat_message_location()
adds a chat message if it wasn't added yet.
Definition: replay.cpp:333
bool empty() const
Definition: replay.cpp:643
void add_chat_log_entry(const config &speak, std::back_insert_iterator< std::vector< chat_msg >> &i) const
Definition: replay.cpp:357
void add_log_data(const std::string &key, const std::string &var)
Definition: replay.cpp:313
config & add_command()
Adds a new empty command to the command list at the end.
Definition: replay.cpp:585
config * get_next_action()
Definition: replay.cpp:619
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
std::size_t i
Definition: function.cpp:968
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_RETURN do_replay(bool one_move=false)
Definition: replay.cpp:683
REPLAY_RETURN
Definition: replay.hpp:153
@ REPLAY_FOUND_DEPENDENT
Definition: replay.hpp:155
@ REPLAY_FOUND_INIT_TURN
Definition: replay.hpp:157
@ REPLAY_FOUND_END_LEVEL
Definition: replay.hpp:159
@ REPLAY_RETURN_AT_END
Definition: replay.hpp:154
@ REPLAY_FOUND_END_MOVE
Definition: replay.hpp:158
@ REPLAY_FOUND_END_TURN
Definition: replay.hpp:156
REPLAY_RETURN do_replay_handle(bool one_move=false)
Definition: replay.cpp:700
Encapsulates the map of the game.
Definition: location.hpp:38
mock_char c
static std::string mark
Definition: tstring.cpp:70