52#define DBG_NG LOG_STREAM(debug, log_engine)
53#define LOG_NG LOG_STREAM(info, log_engine)
54#define WRN_NG LOG_STREAM(warn, log_engine)
55#define ERR_NG LOG_STREAM(err, log_engine)
58#define DBG_DP LOG_STREAM(debug, log_display)
59#define LOG_DP LOG_STREAM(info, log_display)
62#define LOG_WML LOG_STREAM(info, log_wml)
63#define WRN_WML LOG_STREAM(warn, log_wml)
64#define ERR_WML LOG_STREAM(err, log_wml)
67#define ERR_CF LOG_STREAM(err, log_config)
92 std::string type =
cfg[
"type"];
93 std::string variation =
cfg[
"variation"];
94 std::string img_mods =
cfg[
"image_mods"];
96 std::size_t side_num =
cfg[
"side"].to_int(1);
106 if(!variation.empty()) {
109 effect[
"apply_to"] =
"variation";
110 effect[
"name"] = variation;
111 fake->add_modification(
"variation",mod);
114 if(!img_mods.empty()) {
117 effect[
"apply_to"] =
"image_mod";
118 effect[
"add"] = img_mods;
119 fake->add_modification(
"image_mod",mod);
125 std::vector<map_location> fake_unit_path(
const unit& fake_unit,
const std::vector<std::string>& xvals,
const std::vector<std::string>& yvals)
128 std::vector<map_location> path;
131 for(std::size_t
i = 0;
i != std::min(xvals.size(),yvals.size()); ++
i) {
134 src.set_wml_x(std::stoi(xvals[
i]));
135 src.set_wml_y(std::stoi(yvals[
i]));
136 }
catch(std::invalid_argument&) {
137 ERR_CF <<
"Invalid move_unit_fake source: " << xvals[
i] <<
", " << yvals[
i];
141 ERR_CF <<
"Invalid move_unit_fake source: " <<
src;
153 dst.set_wml_x(std::stoi(xvals[
i]));
154 dst.set_wml_y(std::stoi(yvals[
i]));
155 }
catch(std::invalid_argument&) {
156 ERR_CF <<
"Invalid move_unit_fake destination: " << xvals[
i] <<
", " << yvals[
i];
159 ERR_CF <<
"Invalid move_unit_fake destination: " <<
dst;
164 game_map->
w(), game_map->
h());
166 if (route.
steps.empty()) {
167 WRN_NG <<
"Could not find move_unit_fake route from " <<
src <<
" to " <<
dst <<
": ignoring complexities";
171 game_map->
w(), game_map->
h());
172 if(route.
steps.empty()) {
176 WRN_NG <<
"Could not find move_unit_fake route from " <<
src <<
" to " <<
dst <<
": ignoring terrain";
178 route = a_star_search(
src,
dst, 10000, dummy_calc, game_map->
w(), game_map->
h());
179 assert(!route.
steps.empty());
185 path.insert(path.end(),
234#define WML_HANDLER_FUNCTION(pname, pei, pcfg) \
235 static void wml_func_##pname(const queued_event& pei, const vconfig& pcfg); \
236 static wml_action wml_action_##pname(#pname, &wml_func_##pname); \
237 static void wml_func_##pname(const queued_event& pei, const vconfig& pcfg)
260 ERR_NG <<
"[do_command] called while whiteboard is applied, ignoring";
264 static const std::set<std::string> allowed_tags {
"attack",
"move",
"recruit",
"recall",
"disband",
"fire_event",
"custom_command"};
271 ERR_NG <<
"[do_command] called too early, only allowed at START or later";
274 if(is_unsynced && !is_during_turn)
276 ERR_NG <<
"[do_command] can only be used during a turn when a user woudl also be able to invoke commands";
281 ERR_NG <<
"[do_command] cannot invoke synced commands while commands are blocked";
286 ERR_NG <<
"[do_command] can only be used from clients that control the currently playing side";
289 for(
const auto& [key, child] :
cfg.all_ordered())
291 if(allowed_tags.find(key) == allowed_tags.end()) {
292 ERR_NG <<
"unsupported tag [" << key <<
"] in [do_command]";
294 std::copy(allowed_tags.begin(), allowed_tags.end(), std::ostream_iterator<std::string>(o,
" "));
295 ERR_NG <<
"allowed tags: " << o.str();
304 ERR_NG <<
"Error via [do_command]:";
309 child.get_parsed_config(),
328 std::string add =
cfg[
"add"];
333 }
else if(!
value.empty()) {
337 if(!current.empty()) {
338 const unsigned int current_turn_number = tod_man.
turn();
339 int new_turn_number = current.to_int(current_turn_number);
340 const unsigned int new_turn_number_u =
static_cast<unsigned int>(new_turn_number);
342 ERR_NG <<
"attempted to change current turn number to one out of range (" << new_turn_number <<
")";
343 }
else if(new_turn_number_u != current_turn_number) {
358 LOG_NG <<
"Processing [move_unit_fake]";
360 if(!dummy_unit.
get())
363 const bool force_scroll =
cfg[
"force_scroll"].to_bool(
true);
365 const std::string x =
cfg[
"x"];
366 const std::string y =
cfg[
"y"];
371 const std::vector<map_location>& path = fake_unit_path(*dummy_unit, xvals, yvals);
383 LOG_NG <<
"Processing [move_units_fake]";
385 const bool force_scroll =
cfg[
"force_scroll"].to_bool();
386 const bool parallel =
cfg[
"parallel"].to_bool(
false);
388 std::size_t num_units = unit_cfgs.size();
389 std::vector<fake_unit_ptr > units;
390 units.reserve(num_units);
391 std::vector<std::vector<map_location>> paths;
392 paths.reserve(num_units);
394 LOG_NG <<
"Moving " << num_units <<
" units (parallel=" << (parallel ?
"yes" :
"no") <<
")";
396 std::size_t longest_path = 0;
401 int skip_steps =
config[
"skip_steps"].to_int();
404 paths.push_back(fake_unit_path(*u, xvals, yvals));
406 paths.back().insert(paths.back().begin(), skip_steps, paths.back().front());
407 longest_path = std::max(longest_path, paths.back().size());
408 DBG_NG <<
"Path " << paths.size() - 1 <<
" has length " << paths.back().size();
410 u->set_location(paths.back().front());
414 LOG_NG <<
"Units placed, longest path is " << longest_path <<
" long";
418 if(!parallel || !animate) {
420 std::vector<map_location> path_step(2);
422 for(std::size_t step = 1; step < longest_path; ++step) {
423 DBG_NG <<
"Doing step " << step <<
"...";
424 for(std::size_t un = 0; un < num_units; ++un) {
425 if (step >= paths[un].size() || paths[un][step - 1] == paths[un][step]) {
428 DBG_NG <<
"Moving unit " << un <<
", doing step " << step;
429 path_step[0] = paths[un][step - 1];
430 path_step[1] = paths[un][step];
432 units[un]->set_location(path_step[1]);
433 units[un]->anim_comp().set_standing(
false);
438 DBG_NG <<
"Parallel movement: initializing " << num_units <<
" animators.";
439 std::vector<std::unique_ptr<unit_display::unit_movement_animator>> animators;
440 animators.reserve(num_units);
441 std::size_t max_steps = 0;
442 for(std::size_t
i = 0;
i < num_units; ++
i) {
443 max_steps = std::max(max_steps, paths[
i].size() > 1 ? paths[
i].size() - 1 : std::size_t(0));
447 for(std::size_t
i = 0;
i < num_units; ++
i) {
448 animators.push_back(std::make_unique<unit_display::unit_movement_animator>(paths[
i],
true, force_scroll));
449 animators[
i]->start(units[
i].get_unit_ptr());
450 if(paths[
i].size() > 1) {
451 animators[
i]->proceed_to(units[
i].get_unit_ptr(), 1,
false,
false);
452 DBG_NG <<
"Unit " <<
i <<
" started movement (path length " << paths[
i].size()-1 <<
")";
457 for(std::size_t step = 1; step <= max_steps; ++step) {
458 for(std::size_t
i = 0;
i < num_units; ++
i) {
459 if(step >= paths[
i].size()) {
462 animators[
i]->wait_for_anims();
463 DBG_NG <<
"Unit " <<
i <<
" arrived at hex " << step <<
" (" << paths[
i][step] <<
")";
464 units[
i]->set_location(paths[
i][step]);
465 if(step + 1 < paths[
i].size()) {
466 animators[
i]->proceed_to(units[
i].get_unit_ptr(), step + 1,
false,
false);
468 DBG_NG <<
"Unit " <<
i <<
" reached its final destination.";
469 units[
i]->anim_comp().set_standing(
true);
475 DBG_NG <<
"All units stopped. Finalizing states.";
476 for(std::size_t
i = 0;
i < num_units; ++
i) {
477 animators[
i]->finish(units[
i].get_unit_ptr());
492 LOG_NG <<
"recalling unit...";
501 temp_config[
"x"] =
"recall";
502 temp_config[
"y"] =
"recall";
504 vconfig unit_filter_cfg(temp_config);
505 const vconfig & leader_filter =
cfg.child(
"secondary_unit");
508 LOG_NG <<
"for side " <<
t.side() <<
"...";
509 const std::string player_id =
t.save_id_or_number();
511 if(
t.recall_list().size() < 1) {
512 DBG_NG <<
"recall list is empty when trying to recall!";
513 DBG_NG <<
"player_id: " << player_id <<
" side: " <<
t.side();
522 for(std::vector<unit_ptr>::iterator u = avail.
begin(); u != avail.
end(); ++u) {
523 DBG_NG <<
"checking unit against filter...";
526 DBG_NG << (*u)->id() <<
" matched the filter...";
528 const unit* pass_check = to_recruit.get();
529 if(!
cfg[
"check_passability"].to_bool(
true)) pass_check =
nullptr;
533 const auto& iter = special_locs.find(
cfg[
"location_id"]);
534 if(iter != special_locs.end()) {
535 cfg_loc = iter->second;
540 DBG_NG <<
"...considering " + leader->id() +
" as the recalling leader...";
542 if ( lfilt(*leader) &&
544 DBG_NG <<
"...matched the leader filter and is able to recall the unit.";
546 loc = leader->get_location();
550 DBG_NG <<
"...valid location for the recall found. Recalling.";
554 cfg[
"show"].to_bool(
true),
cfg[
"fire_event"].to_bool(
false),
566 DBG_NG <<
"No usable leader found, but found usable location. Recalling.";
571 cfg[
"show"].to_bool(
true),
cfg[
"fire_event"].to_bool(
false),
579 LOG_WML <<
"A [recall] tag with the following content failed:\n" <<
cfg.get_config().
debug();
587 virtual config query_user(
int )
const override
590 return config {
"map_data", res};
592 virtual config random_choice(
int )
const override
596 virtual std::string description()
const override
600 virtual bool is_visible()
const override
628 if(!
cfg[
"map_file"].empty()) {
630 map.
read(file_cfg[
"map_data"].str(),
false);
631 }
else if(!
cfg[
"map_data"].empty()) {
632 map.
read(
cfg[
"map_data"].str(),
false);
635 map.
read(
cfg[
"map"].str(),
false);
638 const std::string log_map_name =
cfg[
"map"].
empty() ?
cfg[
"map_file"] : std::string(
"from inline data");
639 lg::log_to_chat() <<
"replace_map: Unable to load map " << log_map_name <<
'\n';
640 ERR_WML <<
"replace_map: Unable to load map " << log_map_name;
649 if (!
cfg[
"expand"].to_bool()) {
650 lg::log_to_chat() <<
"replace_map: Map dimension(s) increase but expand is not set\n";
651 ERR_WML <<
"replace_map: Map dimension(s) increase but expand is not set";
658 if (!
cfg[
"shrink"].to_bool()) {
659 lg::log_to_chat() <<
"replace_map: Map dimension(s) decrease but shrink is not set\n";
660 ERR_WML <<
"replace_map: Map dimension(s) decrease but shrink is not set";
694 if (!
cfg.child(
"source")) {
695 WRN_NG <<
"No source in [store_relative_direction]";
698 if (!
cfg.child(
"destination")) {
699 WRN_NG <<
"No destination in [store_relative_direction]";
703 WRN_NG <<
"No variable in [store_relative_direction]";
710 std::string variable =
cfg[
"variable"];
720 ERR_NG <<
"Cannot do [store_relative_direction] with invalid destination variable: " << variable <<
" with " <<
cfg.get_config().
debug();
732 if (!
cfg.child(
"source")) {
733 WRN_NG <<
"No source in [store_rotate_map_location]";
736 if (!
cfg.child(
"destination")) {
737 WRN_NG <<
"No destination in [store_rotate_map_location]";
741 WRN_NG <<
"No variable in [store_rotate_map_location]";
748 std::string variable =
cfg[
"variable"];
749 int angle =
cfg[
"angle"].to_int(1);
759 ERR_NG <<
"Cannot do [store_rotate_map_location] with invalid destination variable: " << variable <<
" with " <<
cfg.get_config().
debug();
765 const bool remove =
cfg[
"remove"].to_bool(
false);
766 const bool delay =
cfg[
"delayed_variable_substitution"].to_bool(
true);
769 for (
const std::string &
id : ids) {
772 }
else if (
cfg.get_children(
"source").
empty() ||
773 cfg.get_children(
"target").
empty() ||
774 cfg.get_children(
"filter").
empty()) {
775 ERR_WML <<
"[tunnel] is missing a mandatory tag:\n"
777 }
else if (
cfg.get_children(
"source").size() > 1 ||
778 cfg.get_children(
"target").size() > 1 ||
779 cfg.get_children(
"filter").size() > 1) {
780 ERR_WML <<
"[tunnel] should have exactly one of each mandatory tag:\n"
786 if(
cfg[
"bidirectional"].to_bool(
true)) {
797 config parsed_cfg =
cfg.get_parsed_config();
800 if (!to_variable.
blank())
808 new_unit->write(var);
814 ERR_NG <<
"Cannot do [unit] with invalid to_variable: " << to_variable <<
" with " <<
cfg.get_config().
debug();
820 int side = parsed_cfg[
"side"].to_int(1);
846 ERR_WML <<
"Error occured inside [unit]: " <<
e.what();
static lg::log_domain log_engine("engine")
static lg::log_domain log_display("display")
static lg::log_domain log_wml("wml")
#define WML_HANDLER_FUNCTION(pname, pei, pcfg)
WML_HANDLER_FUNCTION macro handles auto registration for wml handlers.
static lg::log_domain log_config("config")
Define actions for the game's events mechanism.
Managing the AIs lifecycle - headers TODO: Refactor history handling and internal commands.
static manager & get_singleton()
Variant for storing WML attributes.
bool blank() const
Tests for an attribute that was never set.
A config object defines a single node in a WML file, with access to child nodes.
void remove_attribute(std::string_view key)
config & add_child(std::string_view key)
const attribute_value * get(std::string_view key) const
Returns a pointer to the attribute with the given key or nullptr if it does not exist.
bool has_attribute(std::string_view key) const
std::string debug() const
void reload_map()
Updates internals that cache map size.
static display * get_singleton()
Returns the display object if a display object exists.
Holds a temporary unit that can be drawn on the map without being placed in the unit_map.
internal_ptr get_unit_ptr()
Get a copy of the internal unit pointer.
void place_on_fake_unit_manager(fake_unit_manager *d)
Place this on manager's fake_units_ dequeue.
unit * get()
Get a raw pointer to the underlying unit.
virtual const gamemap & map() const override
virtual const std::vector< team > & teams() const override
utils::optional< std::string > replace_map(const gamemap &r)
virtual const unit_map & units() const override
@ PRELOAD
the preload [event] is fired next phase: PRESTART (normal game), TURN_STARTING_WAITING (reloaded game...
@ INITIAL
creating intitial [unit]s, executing toplevel [lua] etc.
@ TURN_PLAYING
The User is controlling the game and invoking actions The game can be saved here.
variable_access_create get_variable_access_write(const std::string &varname)
returns a variable_access that can be used to change the game variables
config & get_variable_cfg(const std::string &varname)
throws invalid_variablename_exception if varname is no valid variable name.
static game_display * get_singleton()
void new_turn()
Update lighting settings.
void needs_rebuild(bool b)
Sets whether the screen (map visuals) needs to be rebuilt.
static map registry_
Tracks the known action handlers.
std::map< std::string, handler > map
wml_action(const std::string &tag, handler function)
Using this constructor for a static object outside action_wml.cpp will likely lead to a static initia...
bool context_skip_messages()
Returns whether or not we are skipping messages.
int w() const
Effective map width.
location_map & special_locations()
int h() const
Effective map height.
int total_width() const
Real width of the map, including borders.
int total_height() const
Real height of the map, including borders.
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Encapsulates the map of the game.
void read(std::string_view data, const bool allow_invalid=true)
A RAII object to temporary leave the synced context like in wesnoth.synchronize_choice.
void add(const teleport_group &group)
void remove(const std::string &id)
bool is_skipping_actions() const
game_events::wml_event_pump & pump()
This class encapsulates the recall list of a team.
iterator end()
end iterator
iterator erase(const iterator &it)
Erase an iterator to this object.
iterator begin()
begin iterator
An object to leave the synced context during draw or unsynced wml items when we don’t know whether we...
static bool run_in_synced_context_if_not_already(const std::string &commandname, const config &data, action_spectator &spectator=get_default_spectator())
Checks whether we are currently running in a synced context, and if not we enters it.
static synced_state get_synced_state()
This class stores all the data for a single 'side' (in game nomenclature).
int number_of_turns() const
void modify_turns_by_wml(const std::string &mod)
void set_turn_by_wml(const int num, game_data *vars=nullptr, const bool increase_limit_if_needed=true)
Dynamically change the current turn number.
void set_number_of_turns_by_wml(int num)
unit_creator & allow_invalidate(bool b)
unit_creator & allow_get_village(bool b)
void add_unit(const config &cfg, const vconfig *vcfg=nullptr)
adds a unit on map without firing any events (so, usable during team construction in gamestatus)
unit_creator & allow_discover(bool b)
unit_creator & allow_show(bool b)
unit_creator & allow_rename_side(bool b)
unit_creator & allow_add_to_recall(bool b)
bool matches(const unit &u, const map_location &loc) const
Determine if *this matches filter at a specified location.
std::vector< unit_iterator > find_leaders(int side)
const unit_type * find(const std::string &key, unit_type::BUILD_STATUS status=unit_type::FULL) const
Finds a unit_type by its id() and makes sure it is built to the specified level.
A single unit type that the player may recruit.
This class represents a single unit of a specific type.
static unit_ptr create(const config &cfg, bool use_traits=false, const vconfig *vcfg=nullptr)
Initializes a unit from a config.
Additional functionality for a non-const variable_info.
maybe_const_t< config::attribute_value, V > & as_scalar() const
If instantiated with vi_policy_const, the lifetime of the returned const attribute_value reference mi...
maybe_const_t< config, V > & as_container() const
If instantiated with vi_policy_const, the lifetime of the returned const attribute_value reference mi...
A variable-expanding proxy for the config class.
std::vector< vconfig > child_list
Various functions related to the creation of units (recruits, recalls, and placed units).
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
Declarations for File-IO.
int side() const
The side this unit belongs to.
Standard logging facilities (interface).
place_recruit_result place_recruit(const unit_ptr &u, const map_location &recruit_location, const map_location &recruited_from, int cost, bool is_recall, map_location::direction facing, bool show, bool fire_event, bool full_movement, bool wml_triggered)
Place a unit into the game.
std::string read_map(const std::string &name)
std::stringstream & log_to_chat()
Use this to show WML errors in the ingame chat.
config get_user_choice(const std::string &name, const user_choice &uch, int side=0)
plain_route a_star_search(const map_location &src, const map_location &dst, double stop_at, const cost_calculator &calc, const std::size_t width, const std::size_t height, const teleport_map *teleports, bool border)
map_location find_vacant_tile(const map_location &loc, VACANT_TILE_TYPE vacancy, const unit *pass_check, const team *shroud_check, const game_board *board)
Function that will find a location on the board that is as near to loc as possible,...
::tod_manager * tod_manager
fake_unit_manager * fake_units
pathfind::manager * tunnels
play_controller * controller
std::shared_ptr< wb::manager > whiteboard
void move_unit(const std::vector< map_location > &path, const unit_ptr &u, bool animate, map_location::direction dir, bool force_scroll)
Display a unit moving along a given path.
std::vector< std::string > split(const config_attribute_value &val)
This module contains various pathfinding functions and utilities.
static void clear_global_variable(persist_context &ctx, const vconfig &pcfg)
static void set_global_variable(persist_context &ctx, const vconfig &pcfg)
void verify_and_set_global_variable(const vconfig &pcfg)
void verify_and_clear_global_variable(const vconfig &pcfg)
static void get_global_variable(persist_context &ctx, const vconfig &pcfg)
void verify_and_get_global_variable(const vconfig &pcfg)
std::shared_ptr< unit > unit_ptr
unit_race::GENDER string_gender(const std::string &str, unit_race::GENDER def)
std::unique_ptr< MIX_Audio, decltype(&MIX_DestroyAudio)> value
rect dst
Location on the final composed sheet.
rect src
Non-transparent portion of the surface to compose.
std::string filename
Filename.
Encapsulates the map of the game.
static std::string write_direction(direction dir)
static const map_location & null_location()
static direction parse_direction(const std::string &str)
Interface for querying local choices.
Function which doesn't take anything into account.
Function which only uses terrain, ignoring shroud, enemies, etc.
Structure which holds a single route between one location and another.
std::vector< map_location > steps
Helper class, don't construct this directly.
unit_type_data unit_types
Display units performing various actions: moving, attacking, and dying.
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...