The Battle for Wesnoth  1.15.1+dev
replay_helper.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 #include "replay_helper.hpp"
15 
16 #include <string>
17 #include <cassert>
18 #include "map/location.hpp"
19 #include "time_of_day.hpp"
20 #include "resources.hpp"
21 #include "play_controller.hpp"
22 
23 config replay_helper::get_recruit(const std::string& type_id, const map_location& loc, const map_location& from)
24 {
25  config val;
26  val["type"] = type_id;
27  loc.write(val);
28  config& leader_position = val.add_child("from");
29  from.write(leader_position);
30 
31  return val;
32 }
33 
34 config replay_helper::get_recall(const std::string& unit_id, const map_location& loc, const map_location& from)
35 {
36 
37  config val;
38  val["value"] = unit_id;
39  loc.write(val);
40  config& leader_position = val.add_child("from");
41  from.write(leader_position);
42 
43  return val;
44 }
45 
46 config replay_helper::get_disband(const std::string& unit_id)
47 {
48  config val;
49 
50  val["value"] = unit_id;
51 
52  return val;
53 }
54 
55 
56 /**
57  * Records a move that follows the provided @a steps.
58  * This should be the steps to be taken this turn, ending in an
59  * apparently-unoccupied (from the moving team's perspective) hex.
60  */
61 config replay_helper::get_movement(const std::vector<map_location>& steps, bool skip_sighted, bool skip_ally_sighted)
62 {
63  assert(!steps.empty());
64 
65  config move;
66  if(skip_sighted)
67  {
68  //note, that skip_ally_sighted has no effect if skip_sighted is true
69  move["skip_sighted"] = "all";
70  }
71  else if(skip_ally_sighted && !skip_sighted)
72  {
73  move["skip_sighted"] = "only_ally";
74  }
75  else
76  {
77  //leave it empty
78  }
79  write_locations(steps, move);
80 
81  return move;
82 }
83 
84 
85 
87  int att_weapon, int def_weapon, const std::string& attacker_type_id,
88  const std::string& defender_type_id, int attacker_lvl,
89  int defender_lvl, const std::size_t turn, const time_of_day &t)
90 {
91 
92  config move, src, dst;
93  a.write(src);
94  b.write(dst);
95 
96  move.add_child("source", std::move(src));
97  move.add_child("destination", std::move(dst));
98 
99 
100  move["weapon"] = att_weapon;
101  move["defender_weapon"] = def_weapon;
102  move["attacker_type"] = attacker_type_id;
103  move["defender_type"] = defender_type_id;
104  move["attacker_lvl"] = attacker_lvl;
105  move["defender_lvl"] = defender_lvl;
106  move["turn"] = static_cast<int>(turn);
107  move["tod"] = t.id;
108  /*
109  add_unit_checksum(a,current_);
110  add_unit_checksum(b,current_);
111  */
112  return move;
113 }
114 
115 /**
116  * Records that the player has toggled automatic shroud updates.
117  */
119 {
120  config child;
121  child["active"] = turned_on;
122  return child;
123 }
124 
125 /**
126  * Records that the player has manually updated fog/shroud.
127  */
129 {
130  return config();
131 }
132 
133 
135 {
136  config init_side;
137  init_side["side_number"] = resources::controller->current_side();
138  return init_side;
139 }
140 
141 config replay_helper::get_event(const std::string& name, const map_location& loc, const map_location* last_select_loc)
142 {
143  config ev;
144  ev["raise"] = name;
145  if(loc.valid()) {
146  config& source = ev.add_child("source");
147  loc.write(source);
148  }
149  if(last_select_loc != nullptr && last_select_loc->valid())
150  {
151  config& source = ev.add_child("last_select");
152  last_select_loc->write(source);
153  }
154  return ev;
155 }
156 
157 config replay_helper::get_lua_ai(const std::string& lua_code)
158 {
159  config child;
160  child["code"] = lua_code;
161  return child;
162 }
static config get_lua_ai(const std::string &lua_code)
play_controller * controller
Definition: resources.cpp:21
static config get_recall(const std::string &unit_id, const map_location &loc, const map_location &from)
#define a
std::string id
Definition: time_of_day.hpp:91
static config get_auto_shroud(bool turned_on)
Records that the player has toggled automatic shroud updates.
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_update_shroud()
Records that the player has manually updated fog/shroud.
#define b
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:57
static const char * name(const std::vector< SDL_Joystick *> &joysticks, const std::size_t index)
Definition: joystick.cpp:48
bool valid() const
Definition: location.hpp:93
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)
Encapsulates the map of the game.
Definition: location.hpp:42
static config get_disband(const std::string &unit_id)
config & add_child(config_key_type key)
Definition: config.cpp:476
double t
Definition: astarsearch.cpp:64
int current_side() const
Returns the number of the side whose turn it is.
static config get_recruit(const std::string &type_id, const map_location &loc, const map_location &from)
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
static config get_event(const std::string &name, const map_location &loc, const map_location *last_select_loc)
static config get_init_side()
void write(config &cfg) const
Definition: location.cpp:210
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,..,yn.
Definition: location.cpp:497