The Battle for Wesnoth  1.19.4+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 
47 {
48  // Chat and similar actions that don't change the gamestate
49  UNSYNCED,
50  // Local choices
51  DEPENDENT,
52  // Commands the invoke a synced user actions
53  SYNCED,
54  // The actions has a wrong format.
55  INVALID
56 };
57 
58 class replay
59 {
60 public:
61  explicit replay(replay_recorder_base& base);
62 
63 
64  void add_start();
65  void add_surrender(int side_number);
66  void add_countdown_update(int value,int team);
67 
68  void add_synced_command(const std::string& name, const config& command);
69  void init_side();
70  /*
71  returns a reference to the newest config that us not dependent or has undo =no
72 
73  */
75  /**
76  * adds a user_input to the replay
77  * @param name The tag name of the config to add
78  * @param input the contents of the config to add
79  * @param from_side the side that had to make the decision, -1 for 'server'
80  */
81  void user_input(const std::string &name, const config &input, int from_side);
82  void add_label(const terrain_label*);
83  void clear_labels(const std::string&, bool);
84  void add_rename(const std::string& name, const map_location& loc);
85  void end_turn(int next_player_number);
86  void add_unit_checksum(const map_location& loc,config& cfg);
87  void add_log_data(const std::string &key, const std::string &var);
88  void add_log_data(const std::string &category, const std::string &key, const std::string &var);
89  void add_log_data(const std::string &category, const std::string &key, const config& c);
90 
91  /**
92  adds a chat message if it wasn't added yet.
93  @returns true if a message location was added
94  */
96  bool add_chat_message_location(int pos);
97  void speak(const config& cfg);
98  const std::vector<chat_msg>& build_chat_log() const;
99 
100  //get data range will get a range of moves from the replay system.
101  //if data_type is 'ALL_DATA' then it will return all data in this range
102  //except for undoable data that has already been sent. If data_type is
103  //NON_UNDO_DATA, then it will only retrieve undoable data, and will mark
104  //it as already sent.
105  //undoable data includes moves such as placing a label or speaking, which is
106  //ignored by the undo system.
109 
110  void undo();
111  /*
112  undoes the last move and puts it into given config to be reone with redo
113  The retuned config also contains the depended commands for that user action.
114  This is needed be because we also want to readd those dependent commands to the replay when redoing the command.
115  */
116  void undo_cut(config& dst);
117  /*
118  puts the given config which was cut with undo_cut back in the replay.
119  */
120  void redo(const config& dst, bool set_to_end = false);
121 
122  void start_replay();
123  void revert_action();
126 
127  bool at_end() const;
128  void set_to_end();
129 
130  bool empty() const;
131 
133  void add_config(const config& cfg, MARK_SENT mark=MARK_AS_UNSENT);
134 
135  int ncommands() const;
136 
137  static void process_error(const std::string& msg);
138  /*
139  adds a [start] at the begnning of the replay if there is none.
140  returns true if a [start] was added.
141  */
144 private:
145 
146  void add_chat_log_entry(const config &speak, std::back_insert_iterator< std::vector<chat_msg>> &i) const;
147 
148  config &command(int) const;
149  void remove_command(int);
150  /** Adds a new empty command to the command list at the end.
151  *
152  * @return a reference to the added command
153  */
154  config& add_command();
155  /**
156  * adds a new command to the command list at the current position.
157  *
158  * @return a reference to the added command
159  */
163  std::vector<int> message_locations;
164 };
165 
167 {
174 };
175 
177 
178 //replays up to one turn from the recorder object
179 //returns true if it got to the end of the turn without data running out
180 REPLAY_RETURN do_replay(bool one_move = false);
181 
182 REPLAY_RETURN do_replay_handle(bool one_move = false);
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:172
int ncommands() const
Definition: replay.cpp:582
config * peek_next_action()
Definition: replay.cpp:633
void add_start()
Definition: replay.cpp:227
void add_rename(const std::string &name, const map_location &loc)
Definition: replay.cpp:294
config & add_nonundoable_command()
adds a new command to the command list at the current position.
Definition: replay.cpp:597
bool add_start_if_not_there_yet()
Definition: replay.cpp:675
void undo_cut(config &dst)
Definition: replay.cpp:502
void set_to_end()
Definition: replay.cpp:651
config & get_last_real_command()
Definition: replay.cpp:438
void add_config(const config &cfg, MARK_SENT mark=MARK_AS_UNSENT)
Definition: replay.cpp:661
void add_label(const terrain_label *)
Definition: replay.cpp:273
void add_synced_command(const std::string &name, const config &command)
Definition: replay.cpp:248
MARK_SENT
Definition: replay.hpp:132
@ MARK_AS_SENT
Definition: replay.hpp:132
@ MARK_AS_UNSENT
Definition: replay.hpp:132
DATA_TYPE
Definition: replay.hpp:107
@ ALL_DATA
Definition: replay.hpp:107
@ NON_UNDO_DATA
Definition: replay.hpp:107
void end_turn(int next_player_number)
Definition: replay.cpp:305
void add_surrender(int side_number)
Definition: replay.cpp:234
void speak(const config &cfg)
Definition: replay.cpp:351
void clear_labels(const std::string &, bool)
Definition: replay.cpp:284
const std::vector< chat_msg > & build_chat_log() const
Definition: replay.cpp:380
config & command(int) const
Definition: replay.cpp:576
replay(replay_recorder_base &base)
Definition: replay.cpp:182
void remove_command(int)
Definition: replay.cpp:366
void user_input(const std::string &name, const config &input, int from_side)
adds a user_input to the replay
Definition: replay.cpp:258
void redo(const config &dst, bool set_to_end=false)
Definition: replay.cpp:417
bool at_end() const
Definition: replay.cpp:645
void add_unit_checksum(const map_location &loc, config &cfg)
Definition: replay.cpp:206
void revert_action()
Definition: replay.cpp:614
void undo()
Definition: replay.cpp:570
std::vector< int > message_locations
Definition: replay.hpp:163
static void process_error(const std::string &msg)
Definition: replay.cpp:199
void init_side()
Definition: replay.cpp:219
void start_replay()
Definition: replay.cpp:609
void delete_upcoming_commands()
Definition: replay.cpp:188
config get_unsent_commands(DATA_TYPE data_type)
Definition: replay.cpp:397
void add_countdown_update(int value, int team)
Definition: replay.cpp:240
replay_recorder_base * base_
Definition: replay.hpp:161
bool add_chat_message_location()
adds a chat message if it wasn't added yet.
Definition: replay.cpp:334
bool empty() const
Definition: replay.cpp:656
int sent_upto_
Definition: replay.hpp:162
void add_chat_log_entry(const config &speak, std::back_insert_iterator< std::vector< chat_msg >> &i) const
Definition: replay.cpp:358
void add_log_data(const std::string &key, const std::string &var)
Definition: replay.cpp:314
config & add_command()
Adds a new empty command to the command list at the end.
Definition: replay.cpp:587
config * get_next_action()
Definition: replay.cpp:621
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:1023
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:696
REPLAY_RETURN do_replay(bool one_move=false)
Definition: replay.cpp:711
REPLAY_RETURN
Definition: replay.hpp:167
@ REPLAY_FOUND_DEPENDENT
Definition: replay.hpp:169
@ REPLAY_FOUND_INIT_TURN
Definition: replay.hpp:171
@ REPLAY_FOUND_END_LEVEL
Definition: replay.hpp:173
@ REPLAY_RETURN_AT_END
Definition: replay.hpp:168
@ REPLAY_FOUND_END_MOVE
Definition: replay.hpp:172
@ REPLAY_FOUND_END_TURN
Definition: replay.hpp:170
REPLAY_ACTION_TYPE
Definition: replay.hpp:47
REPLAY_RETURN do_replay_handle(bool one_move=false)
Definition: replay.cpp:728
rect dst
Location on the final composed sheet.
Encapsulates the map of the game.
Definition: location.hpp:44
mock_char c
static std::string mark
Definition: tstring.cpp:70