The Battle for Wesnoth  1.19.15+dev
replay_helper.cpp
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 #include "replay_helper.hpp"
17 
18 #include <string>
19 #include <cassert>
20 #include "map/location.hpp"
21 #include "time_of_day.hpp"
22 #include "resources.hpp"
23 #include "play_controller.hpp"
24 
25 config replay_helper::get_recruit(const std::string& type_id, const map_location& loc, const map_location& from)
26 {
27  config val;
28  val["type"] = type_id;
29  loc.write(val);
30  config& leader_position = val.add_child("from");
31  from.write(leader_position);
32 
33  return val;
34 }
35 
36 config replay_helper::get_recall(const std::string& unit_id, const map_location& loc, const map_location& from)
37 {
38 
39  config val;
40  val["value"] = unit_id;
41  loc.write(val);
42  config& leader_position = val.add_child("from");
43  from.write(leader_position);
44 
45  return val;
46 }
47 
48 config replay_helper::get_disband(const std::string& unit_id)
49 {
50  config val;
51 
52  val["value"] = unit_id;
53 
54  return val;
55 }
56 
57 
58 /**
59  * Records a move that follows the provided @a steps.
60  * This should be the steps to be taken this turn, ending in an
61  * apparently-unoccupied (from the moving team's perspective) hex.
62  */
63 config replay_helper::get_movement(const std::vector<map_location>& steps, bool skip_sighted, bool skip_ally_sighted)
64 {
65  assert(!steps.empty());
66 
67  config move;
68  if(skip_sighted)
69  {
70  //note, that skip_ally_sighted has no effect if skip_sighted is true
71  move["skip_sighted"] = "all";
72  }
73  else if(skip_ally_sighted && !skip_sighted)
74  {
75  move["skip_sighted"] = "only_ally";
76  }
77  else
78  {
79  //leave it empty
80  }
81  write_locations(steps, move);
82 
83  return move;
84 }
85 
86 
87 
89  int att_weapon, int def_weapon, const std::string& attacker_type_id,
90  const std::string& defender_type_id, int attacker_lvl,
91  int defender_lvl, const std::size_t turn, const time_of_day &t)
92 {
93 
94  config move, src, dst;
95  a.write(src);
96  b.write(dst);
97 
98  move.add_child("source", std::move(src));
99  move.add_child("destination", std::move(dst));
100 
101 
102  move["weapon"] = att_weapon;
103  move["defender_weapon"] = def_weapon;
104  move["attacker_type"] = attacker_type_id;
105  move["defender_type"] = defender_type_id;
106  move["attacker_lvl"] = attacker_lvl;
107  move["defender_lvl"] = defender_lvl;
108  move["turn"] = static_cast<int>(turn);
109  move["tod"] = t.id;
110  /*
111  add_unit_checksum(a,current_);
112  add_unit_checksum(b,current_);
113  */
114  return move;
115 }
116 
117 /**
118  * Records that the player has toggled automatic shroud updates.
119  */
120 config replay_helper::get_auto_shroud(bool turned_on, bool block_undo)
121 {
122  config child;
123  child["active"] = turned_on;
124  child["block_undo"] = block_undo;
125  return child;
126 }
127 
128 /**
129  * Records that the player has manually updated fog/shroud.
130  */
132 {
133  return config();
134 }
135 
136 
138 {
139  config init_side;
140  init_side["side_number"] = resources::controller->current_side();
141  return init_side;
142 }
143 
144 config replay_helper::get_event(const std::string& name, const map_location& loc, const map_location* last_select_loc)
145 {
146  config ev;
147  ev["raise"] = name;
148  if(loc.valid()) {
149  config& source = ev.add_child("source");
150  loc.write(source);
151  }
152  if(last_select_loc != nullptr && last_select_loc->valid())
153  {
154  config& source = ev.add_child("last_select");
155  last_select_loc->write(source);
156  }
157  return ev;
158 }
159 
160 config replay_helper::get_lua_ai(const std::string& lua_code)
161 {
162  config child;
163  child["code"] = lua_code;
164  return child;
165 }
map_location loc
Definition: move.cpp:172
double t
Definition: astarsearch.cpp:63
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
config & add_child(config_key_type key)
Definition: config.cpp:436
int current_side() const
Returns the number of the side whose turn it is.
static config get_lua_ai(const std::string &lua_code)
static config get_movement(const std::vector< map_location > &steps, bool skip_sighted, bool skip_ally_sighted)
Records a move that follows the provided steps.
static config get_init_side()
static config get_attack(const map_location &a, const map_location &b, int att_weapon, int def_weapon, const std::string &attacker_type_id, const std::string &defender_type_id, int attacker_lvl, int defender_lvl, const std::size_t turn, const time_of_day &t)
static config get_recall(const std::string &unit_id, const map_location &loc, const map_location &from)
static config get_recruit(const std::string &type_id, const map_location &loc, const map_location &from)
static config get_update_shroud()
Records that the player has manually updated fog/shroud.
static config get_disband(const std::string &unit_id)
static config get_auto_shroud(bool turned_on, bool block_undo=true)
Records that the player has toggled automatic shroud updates.
static config get_event(const std::string &name, const map_location &loc, const map_location *last_select_loc)
void write_locations(const std::vector< map_location > &locs, config &cfg)
Write a vector of locations into a config adding keys x=x1,x2,..,xn and y=y1,y2,.....
Definition: location.cpp:492
play_controller * controller
Definition: resources.cpp:21
rect dst
Location on the final composed sheet.
rect src
Non-transparent portion of the surface to compose.
Encapsulates the map of the game.
Definition: location.hpp:46
bool valid() const
Definition: location.hpp:111
void write(config &cfg) const
Definition: location.cpp:223
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:57
#define b