The Battle for Wesnoth  1.19.0-dev
replay_helper.cpp
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 #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  */
121 {
122  config child;
123  child["active"] = turned_on;
124  return child;
125 }
126 
127 /**
128  * Records that the player has manually updated fog/shroud.
129  */
131 {
132  return config();
133 }
134 
135 
137 {
138  config init_side;
139  init_side["side_number"] = resources::controller->current_side();
140  return init_side;
141 }
142 
143 config replay_helper::get_event(const std::string& name, const map_location& loc, const map_location* last_select_loc)
144 {
145  config ev;
146  ev["raise"] = name;
147  if(loc.valid()) {
148  config& source = ev.add_child("source");
149  loc.write(source);
150  }
151  if(last_select_loc != nullptr && last_select_loc->valid())
152  {
153  config& source = ev.add_child("last_select");
154  last_select_loc->write(source);
155  }
156  return ev;
157 }
158 
159 config replay_helper::get_lua_ai(const std::string& lua_code)
160 {
161  config child;
162  child["code"] = lua_code;
163  return child;
164 }
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:159
config & add_child(config_key_type key)
Definition: config.cpp:441
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_auto_shroud(bool turned_on)
Records that the player has toggled automatic shroud updates.
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_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:454
play_controller * controller
Definition: resources.cpp:21
Encapsulates the map of the game.
Definition: location.hpp:38
bool valid() const
Definition: location.hpp:89
void write(config &cfg) const
Definition: location.cpp:211
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:57
#define a
#define b