The Battle for Wesnoth  1.19.11+dev
game_lua_kernel.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2025
3  by Guillaume Melquiond <guillaume.melquiond@gmail.com>
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  * Provides a Lua interpreter, to be embedded in WML.
19  *
20  * @note Naming conventions:
21  * - intf_ functions are exported in the wesnoth domain,
22  * - impl_ functions are hidden inside metatables,
23  * - cfun_ functions are closures,
24  * - luaW_ functions are helpers in Lua style.
25  */
26 
28 
29 #include "actions/attack.hpp" // for battle_context_unit_stats, etc
30 #include "actions/advancement.hpp" // for advance_unit_at, etc
31 #include "actions/move.hpp" // for clear_shroud
32 #include "actions/vision.hpp" // for clear_shroud and create_jamming_map
33 #include "actions/undo.hpp" // for clear_shroud and create_jamming_map
34 #include "actions/undo_action.hpp" // for clear_shroud and create_jamming_map
35 #include "ai/composite/ai.hpp" // for ai_composite
36 #include "ai/composite/component.hpp" // for component, etc
37 #include "ai/composite/contexts.hpp" // for ai_context
38 #include "ai/lua/engine_lua.hpp" // for engine_lua
39 #include "ai/composite/rca.hpp" // for candidate_action
40 #include "ai/composite/stage.hpp" // for stage
41 #include "ai/configuration.hpp" // for configuration
42 #include "ai/lua/core.hpp" // for lua_ai_context, etc
43 #include "ai/manager.hpp" // for manager, holder
44 #include "attack_prediction.hpp" // for combatant
45 #include "chat_events.hpp" // for chat_handler, etc
46 #include "config.hpp" // for config, etc
47 #include "display_chat_manager.hpp" // for clear_chat_messages
48 #include "floating_label.hpp"
49 #include "formatter.hpp"
50 #include "game_board.hpp" // for game_board
51 #include "game_classification.hpp" // for game_classification, etc
52 #include "game_config.hpp" // for debug, base_income, etc
53 #include "game_config_manager.hpp" // for game_config_manager
54 #include "game_data.hpp" // for game_data, etc
55 #include "game_display.hpp" // for game_display
56 #include "game_errors.hpp" // for game_error
57 #include "game_events/conditional_wml.hpp" // for conditional_passed
59 #include "game_events/handlers.hpp"
60 #include "game_events/manager_impl.hpp" // for pending_event_handler
61 #include "game_events/pump.hpp" // for queued_event
62 #include "preferences/preferences.hpp" // for encountered_units
63 #include "log.hpp" // for LOG_STREAM, logger, etc
64 #include "map/map.hpp" // for gamemap
65 #include "map/label.hpp"
66 #include "map/location.hpp" // for map_location
67 #include "mouse_events.hpp" // for mouse_handler
68 #include "mp_game_settings.hpp" // for mp_game_settings
69 #include "overlay.hpp"
70 #include "pathfind/pathfind.hpp" // for full_cost_map, plain_route, etc
71 #include "pathfind/teleport.hpp" // for get_teleport_locations, etc
72 #include "play_controller.hpp" // for play_controller
74 #include "recall_list_manager.hpp" // for recall_list_manager
75 #include "replay.hpp" // for get_user_choice, etc
76 #include "reports.hpp" // for register_generator, etc
77 #include "resources.hpp" // for whiteboard
79 #include "scripting/lua_audio.hpp"
80 #include "scripting/lua_unit.hpp"
82 #include "scripting/lua_common.hpp"
84 #include "scripting/lua_gui2.hpp" // for show_gamestate_inspector
86 #include "scripting/lua_race.hpp"
87 #include "scripting/lua_team.hpp"
90 #include "scripting/push_check.hpp"
91 #include "synced_commands.hpp"
92 #include "color.hpp" // for surface
93 #include "side_filter.hpp" // for side_filter
94 #include "sound.hpp" // for commit_music_changes, etc
95 #include "synced_context.hpp" // for synced_context, etc
96 #include "synced_user_choice.hpp"
97 #include "team.hpp" // for team, village_owner
98 #include "terrain/terrain.hpp" // for terrain_type
99 #include "terrain/filter.hpp" // for terrain_filter
100 #include "terrain/translation.hpp" // for read_terrain_code, etc
101 #include "time_of_day.hpp" // for time_of_day
102 #include "tod_manager.hpp" // for tod_manager
103 #include "tstring.hpp" // for t_string, operator+
104 #include "units/unit.hpp" // for unit
105 #include "units/animation_component.hpp" // for unit_animation_component
106 #include "units/udisplay.hpp"
107 #include "units/filter.hpp"
108 #include "units/map.hpp" // for unit_map, etc
109 #include "units/ptr.hpp" // for unit_const_ptr, unit_ptr
110 #include "units/types.hpp" // for unit_type_data, unit_types, etc
111 #include "utils/scope_exit.hpp"
112 #include "variable.hpp" // for vconfig, etc
113 #include "variable_info.hpp"
114 #include "video.hpp" // only for faked
115 #include "whiteboard/manager.hpp" // for whiteboard
116 #include "deprecation.hpp"
117 
118 #include <functional> // for bind_t, bind
119 #include <array>
120 #include <cassert> // for assert
121 #include <cstring> // for strcmp
122 #include <iterator> // for distance, advance
123 #include <map> // for map, map<>::value_type, etc
124 #include <set> // for set
125 #include <sstream> // for operator<<, basic_ostream, etc
126 #include <thread>
127 #include <utility> // for pair
128 #include <algorithm>
129 #include <vector> // for vector, etc
130 
131 #ifdef DEBUG_LUA
132 #include "scripting/debug_lua.hpp"
133 #endif
134 
135 static lg::log_domain log_scripting_lua("scripting/lua");
136 #define DBG_LUA LOG_STREAM(debug, log_scripting_lua)
137 #define LOG_LUA LOG_STREAM(info, log_scripting_lua)
138 #define WRN_LUA LOG_STREAM(warn, log_scripting_lua)
139 #define ERR_LUA LOG_STREAM(err, log_scripting_lua)
140 
141 static lg::log_domain log_wml("wml");
142 #define ERR_WML LOG_STREAM(err, log_wml)
143 
144 std::vector<config> game_lua_kernel::preload_scripts;
146 
147 // Template which allows to push member functions to the lua kernel base into lua as C functions, using a shim
148 typedef int (game_lua_kernel::*member_callback)(lua_State *);
149 
150 template <member_callback method>
151 int dispatch(lua_State *L) {
152  return ((lua_kernel_base::get_lua_kernel<game_lua_kernel>(L)).*method)(L);
153 }
154 
155 // Pass a const bool also...
156 typedef int (game_lua_kernel::*member_callback2)(lua_State *, bool);
157 
158 template <member_callback2 method, bool b>
159 int dispatch2(lua_State *L) {
160  return ((lua_kernel_base::get_lua_kernel<game_lua_kernel>(L)).*method)(L, b);
161 }
162 
164 {
165  map_locker(game_lua_kernel* kernel) : kernel_(kernel)
166  {
167  ++kernel_->map_locked_;
168  }
170  {
171  --kernel_->map_locked_;
172  }
174 };
175 
176 
178 {
180  for (const config& cfg : game_config.child_range("lua")) {
181  game_lua_kernel::preload_scripts.push_back(cfg);
182  }
183  game_lua_kernel::preload_config = game_config.mandatory_child("game_config");
184 }
185 
186 void game_lua_kernel::log_error(char const * msg, char const * context)
187 {
189  lua_chat(context, msg);
190 }
191 
192 void game_lua_kernel::lua_chat(const std::string& caption, const std::string& msg)
193 {
194  if (game_display_) {
195  game_display_->get_chat_manager().add_chat_message(std::time(nullptr), caption, 0, msg,
197  }
198 }
199 
200 /**
201  * Gets a vector of sides from side= attribute in a given config node.
202  * Promotes consistent behavior.
203  */
204 std::vector<int> game_lua_kernel::get_sides_vector(const vconfig& cfg)
205 {
206  const config::attribute_value sides = cfg["side"];
207  const vconfig &ssf = cfg.child("filter_side");
208 
209  if (!ssf.null()) {
210  if(!sides.empty()) { WRN_LUA << "ignoring duplicate side filter information (inline side=)"; }
212  return filter.get_teams();
213  }
214 
215  side_filter filter(sides.str(), &game_state_);
216  return filter.get_teams();
217 }
218 
219 namespace {
220  /**
221  * Temporary entry to a queued_event stack
222  */
223  struct queued_event_context
224  {
225  typedef game_events::queued_event qe;
226  std::stack<qe const *> & stack_;
227 
228  queued_event_context(qe const *new_qe, std::stack<qe const*> & stack)
229  : stack_(stack)
230  {
231  stack_.push(new_qe);
232  }
233 
234  ~queued_event_context()
235  {
236  stack_.pop();
237  }
238  };
239 }//unnamed namespace for queued_event_context
240 
241 /**
242  * Gets currently viewing side.
243  * - Ret 1: integer specifying the currently viewing side
244  * - Ret 2: Bool whether the vision is not limited to that team, this can for example be true during replays.
245  */
246 static int intf_get_viewing_side(lua_State *L)
247 {
248  if(const display* disp = display::get_singleton()) {
249  lua_pushinteger(L, disp->viewing_team().side());
250  lua_pushboolean(L, disp->show_everything());
251  return 2;
252  }
253  else {
254  return 0;
255  }
256 }
257 
258 static int intf_handle_user_interact(lua_State *)
259 {
261  return 0;
262 }
263 
264 static const char animatorKey[] = "unit animator";
265 
266 static int impl_animator_collect(lua_State* L) {
267  unit_animator& anim = *static_cast<unit_animator*>(luaL_checkudata(L, 1, animatorKey));
268  anim.~unit_animator();
269  return 0;
270 }
271 
272 static int impl_add_animation(lua_State* L)
273 {
274  unit_animator& anim = *static_cast<unit_animator*>(luaL_checkudata(L, 1, animatorKey));
275  unit_ptr up = luaW_checkunit_ptr(L, 2, false);
276  unit& u = *up;
277  std::string which = luaL_checkstring(L, 3);
278 
279  std::string hits_str = luaL_checkstring(L, 4);
280  strike_result::type hits = strike_result::get_enum(hits_str).value_or(strike_result::type::invalid);
281 
282  map_location dest;
283  int v1 = 0, v2 = 0;
284  bool bars = false;
285  t_string text;
286  color_t color{255, 255, 255};
287  const_attack_ptr primary, secondary;
288 
289  if(lua_istable(L, 5)) {
290  lua_getfield(L, 5, "target");
291  if(luaW_tolocation(L, -1, dest)) {
292  if(dest == u.get_location()) {
293  return luaL_argerror(L, 5, "target location must be different from animated unit's location");
294  } else if(!tiles_adjacent(dest, u.get_location())) {
295  return luaL_argerror(L, 5, "target location must be adjacent to the animated unit");
296  }
297  } else {
298  // luaW_tolocation may set the location to (0,0) if it fails
299  dest = map_location();
300  if(!lua_isnoneornil(L, -1)) {
301  return luaW_type_error(L, 5, "target", "location table");
302  }
303  }
304  lua_pop(L, 1);
305 
306  lua_getfield(L, 5, "value");
307  if(lua_isnumber(L, -1)) {
308  v1 = lua_tointeger(L, -1);
309  } else if(lua_istable(L, -1)) {
310  lua_rawgeti(L, -1, 1);
311  v1 = lua_tointeger(L, -1);
312  lua_pop(L, 1);
313  lua_rawgeti(L, -1, 2);
314  v2 = lua_tointeger(L, -1);
315  lua_pop(L, 1);
316  } else if(!lua_isnoneornil(L, -1)) {
317  return luaW_type_error(L, 5, "value", "number or array of two numbers");
318  }
319  lua_pop(L, 1);
320 
321  lua_getfield(L, 5, "with_bars");
322  if(lua_isboolean(L, -1)) {
323  bars = luaW_toboolean(L, -1);
324  } else if(!lua_isnoneornil(L, -1)) {
325  return luaW_type_error(L, 5, "with_bars", lua_typename(L, LUA_TBOOLEAN));
326  }
327  lua_pop(L, 1);
328 
329  lua_getfield(L, 5, "text");
330  if(lua_isstring(L, -1)) {
331  text = lua_tostring(L, -1);
332  } else if(luaW_totstring(L, -1, text)) {
333  // Do nothing; luaW_totstring already assigned the value
334  } else if(!lua_isnoneornil(L, -1)) {
335  return luaW_type_error(L, 5, "text", lua_typename(L, LUA_TSTRING));
336  }
337  lua_pop(L, 1);
338 
339  lua_getfield(L, 5, "color");
340  if(lua_istable(L, -1) && lua_rawlen(L, -1) == 3) {
341  int idx = lua_absindex(L, -1);
342  lua_rawgeti(L, idx, 1); // red @ -3
343  lua_rawgeti(L, idx, 2); // green @ -2
344  lua_rawgeti(L, idx, 3); // blue @ -1
345  color = color_t(lua_tointeger(L, -3), lua_tointeger(L, -2), lua_tointeger(L, -1));
346  lua_pop(L, 3);
347  } else if(!lua_isnoneornil(L, -1)) {
348  return luaW_type_error(L, 5, "color", "array of three numbers");
349  }
350  lua_pop(L, 1);
351 
352  lua_getfield(L, 5, "primary");
353  primary = luaW_toweapon(L, -1);
354  if(!primary && !lua_isnoneornil(L, -1)) {
355  return luaW_type_error(L, 5, "primary", "weapon");
356  }
357  lua_pop(L, 1);
358 
359  lua_getfield(L, 5, "secondary");
360  secondary = luaW_toweapon(L, -1);
361  if(!secondary && !lua_isnoneornil(L, -1)) {
362  return luaW_type_error(L, 5, "secondary", "weapon");
363  }
364  lua_pop(L, 1);
365  } else if(!lua_isnoneornil(L, 5)) {
366  return luaW_type_error(L, 5, "table of options");
367  }
368 
369  anim.add_animation(up, which, u.get_location(), dest, v1, bars, text, color, hits, primary, secondary, v2);
370  return 0;
371 }
372 
374 {
375  if(video::headless() || resources::controller->is_skipping_replay()) {
376  return 0;
377  }
378  events::command_disabler command_disabler;
379  unit_animator& anim = *static_cast<unit_animator*>(luaL_checkudata(L, 1, animatorKey));
381  anim.start_animations();
382  anim.wait_for_end();
383  anim.set_all_standing();
384  anim.clear();
385  return 0;
386 }
387 
388 static int impl_clear_animation(lua_State* L)
389 {
390  unit_animator& anim = *static_cast<unit_animator*>(luaL_checkudata(L, 1, animatorKey));
391  anim.clear();
392  return 0;
393 }
394 
395 static int impl_animator_get(lua_State* L)
396 {
397  const char* m = lua_tostring(L, 2);
398  return luaW_getmetafield(L, 1, m);
399 }
400 
402 {
403  new(L) unit_animator;
404  if(luaL_newmetatable(L, animatorKey)) {
405  luaL_Reg metafuncs[] {
406  {"__gc", impl_animator_collect},
407  {"__index", impl_animator_get},
408  {"add", impl_add_animation},
409  {"run", &dispatch<&game_lua_kernel::impl_run_animation>},
410  {"clear", impl_clear_animation},
411  {nullptr, nullptr},
412  };
413  luaL_setfuncs(L, metafuncs, 0);
414  lua_pushstring(L, "__metatable");
415  lua_setfield(L, -2, animatorKey);
416  }
417  lua_setmetatable(L, -2);
418  return 1;
419 }
420 
422 {
423  if (game_display_) {
425  std::string name;
426  if(luaW_tovconfig(L, 1, cfg)) {
427  name = cfg["name"].str();
428  deprecated_message("gui.show_inspector(cfg)", DEP_LEVEL::INDEFINITE, {1, 19, 0}, "Instead of {name = 'title' }, pass just 'title'.");
429  } else {
430  name = luaL_optstring(L, 1, "");
431  }
433  }
434  return 0;
435 }
436 
437 /**
438  * Gets the unit at the given location or with the given id.
439  * - Arg 1: location
440  * OR
441  * - Arg 1: string ID
442  * - Ret 1: full userdata with __index pointing to impl_unit_get and
443  * __newindex pointing to impl_unit_set.
444  */
446 {
448  if(lua_isstring(L, 1) && !lua_isnumber(L, 1)) {
449  std::string id = luaL_checkstring(L, 1);
450  for(const unit& u : units()) {
451  if(u.id() == id) {
452  luaW_pushunit(L, u.underlying_id());
453  return 1;
454  }
455  }
456  return 0;
457  }
458  if(!luaW_tolocation(L, 1, loc)) {
459  return luaL_argerror(L, 1, "expected string or location");
460  }
462 
463  if (!ui.valid()) return 0;
464 
465  luaW_pushunit(L, ui->underlying_id());
466  return 1;
467 }
468 
469 /**
470  * Gets the unit displayed in the sidebar.
471  * - Ret 1: full userdata with __index pointing to impl_unit_get and
472  * __newindex pointing to impl_unit_set.
473  */
475 {
476  if (!game_display_) {
477  return 0;
478  }
479 
484  if (!ui.valid()) return 0;
485 
486  luaW_pushunit(L, ui->underlying_id());
487  return 1;
488 }
489 
490 /**
491  * Gets all the units matching a given filter.
492  * - Arg 1: optional table containing a filter
493  * - Arg 2: optional location (to find all units that would match on that location)
494  * OR unit (to find all units that would match adjacent to that unit)
495  * - Ret 1: table containing full userdata with __index pointing to
496  * impl_unit_get and __newindex pointing to impl_unit_set.
497  */
499 {
500  vconfig filter = luaW_checkvconfig(L, 1, true);
501  unit_filter filt(filter);
502  std::vector<const unit*> units;
503 
504  if(unit* u_adj = luaW_tounit(L, 2)) {
505  if(!u_adj) {
506  return luaL_argerror(L, 2, "unit not found");
507  }
508  units = filt.all_matches_with_unit(*u_adj);
509  } else if(!lua_isnoneornil(L, 2)) {
511  luaW_tolocation(L, 2, loc);
512  if(!loc.valid()) {
513  return luaL_argerror(L, 2, "invalid location");
514  }
515  units = filt.all_matches_at(loc);
516  } else {
517  units = filt.all_matches_on_map();
518  }
519 
520  // Go through all the units while keeping the following stack:
521  // 1: return table, 2: userdata
522  lua_settop(L, 0);
523  lua_newtable(L);
524  int i = 1;
525 
526  for (const unit * ui : units) {
527  luaW_pushunit(L, ui->underlying_id());
528  lua_rawseti(L, 1, i);
529  ++i;
530  }
531  return 1;
532 }
533 
534 /**
535  * Matches a unit against the given filter.
536  * - Arg 1: full userdata.
537  * - Arg 2: table containing a filter
538  * - Arg 3: optional location OR optional "adjacent" unit
539  * - Ret 1: boolean.
540  */
542 {
543  lua_unit& u = *luaW_checkunit_ref(L, 1);
544 
545  vconfig filter = luaW_checkvconfig(L, 2, true);
546 
547  if (filter.null()) {
548  lua_pushboolean(L, true);
549  return 1;
550  }
551 
552  if(unit* u_adj = luaW_tounit(L, 3)) {
553  if(int side = u.on_recall_list()) {
554  WRN_LUA << "wesnoth.units.matches called with a secondary unit (3rd argument), ";
555  WRN_LUA << "but unit to match was on recall list. ";
556  WRN_LUA << "Thus the 3rd argument is ignored.";
557  team &t = board().get_team(side);
558  scoped_recall_unit auto_store("this_unit", t.save_id_or_number(), t.recall_list().find_index(u->id()));
559  lua_pushboolean(L, unit_filter(filter).matches(*u, map_location()));
560  return 1;
561  }
562  if (!u_adj) {
563  return luaL_argerror(L, 3, "unit not found");
564  }
565  lua_pushboolean(L, unit_filter(filter).matches(*u, *u_adj));
566  } else if(int side = u.on_recall_list()) {
568  luaW_tolocation(L, 3, loc); // If argument 3 isn't a location, loc is unchanged
569  team &t = board().get_team(side);
570  scoped_recall_unit auto_store("this_unit", t.save_id_or_number(), t.recall_list().find_index(u->id()));
571  lua_pushboolean(L, unit_filter(filter).matches(*u, loc));
572  return 1;
573  } else {
575  luaW_tolocation(L, 3, loc); // If argument 3 isn't a location, loc is unchanged
576  lua_pushboolean(L, unit_filter(filter).matches(*u, loc));
577  }
578  return 1;
579 }
580 
581 /**
582  * Gets the numeric ids of all the units matching a given filter on the recall lists.
583  * - Arg 1: optional table containing a filter
584  * - Ret 1: table containing full userdata with __index pointing to
585  * impl_unit_get and __newindex pointing to impl_unit_set.
586  */
588 {
589  vconfig filter = luaW_checkvconfig(L, 1, true);
590 
591  // Go through all the units while keeping the following stack:
592  // 1: return table, 2: userdata
593  lua_settop(L, 0);
594  lua_newtable(L);
595  int i = 1, s = 1;
596  const unit_filter ufilt(filter);
597  for (team &t : teams())
598  {
599  for (unit_ptr & u : t.recall_list())
600  {
601  if (!filter.null()) {
602  scoped_recall_unit auto_store("this_unit",
603  t.save_id_or_number(), t.recall_list().find_index(u->id()));
604  if (!ufilt( *u, map_location() ))
605  continue;
606  }
607  luaW_pushunit(L, s, u->underlying_id());
608  lua_rawseti(L, 1, i);
609  ++i;
610  }
611  ++s;
612  }
613  return 1;
614 }
615 
616 /**
617  * Fires an event.
618  * - Arg 1: string containing the event name or id.
619  * - Arg 2: optional first location.
620  * - Arg 3: optional second location.
621  * - Arg 4: optional WML table used used as the event data
622  * Typically this contains [first] as the [weapon] tag and [second] as the [second_weapon] tag.
623  * - Ret 1: boolean indicating whether the event was processed or not.
624  */
625 int game_lua_kernel::intf_fire_event(lua_State *L, const bool by_id)
626 {
627  char const *m = luaL_checkstring(L, 1);
628 
629  int pos = 2;
630  map_location l1, l2;
631  config data;
632 
633  if (luaW_tolocation(L, 2, l1)) {
634  if (luaW_tolocation(L, 3, l2)) {
635  pos = 4;
636  } else {
637  pos = 3;
638  }
639  }
640 
641  luaW_toconfig(L, pos, data);
642 
643  // Support WML names for some common data
644  if(data.has_child("primary_attack")) {
645  data.add_child("first", data.mandatory_child("primary_attack"));
646  data.remove_children("primary_attack");
647  }
648  if(data.has_child("secondary_attack")) {
649  data.add_child("second", data.mandatory_child("secondary_attack"));
650  data.remove_children("secondary_attack");
651  }
652 
653  bool b = false;
654 
655  if (by_id) {
656  b = std::get<0>(play_controller_.pump().fire("", m, l1, l2, data));
657  }
658  else {
659  b = std::get<0>(play_controller_.pump().fire(m, l1, l2, data));
660  }
661  lua_pushboolean(L, b);
662  return 1;
663 }
664 
665 
666 /**
667  * Fires a wml menu item.
668  * - Arg 1: id of the item. it is not possible to fire items that don't have ids with this function.
669  * - Arg 2: optional first location.
670  * - Ret 1: boolean, true indicating that the event was fired successfully
671  *
672  * NOTE: This is not an "official" feature, it may currently cause assertion failures if used with
673  * menu items which have "needs_select". It is not supported right now to use it this way.
674  * The purpose of this function right now is to make it possible to have automated sanity tests for
675  * the wml menu items system.
676  */
678 {
679  char const *m = luaL_checkstring(L, 1);
680 
681  map_location l1 = luaW_checklocation(L, 2);
682 
684  lua_pushboolean(L, b);
685  return 1;
686 }
687 
688 /**
689  * Gets a WML variable.
690  * - Arg 1: string containing the variable name.
691  * - Arg 2: optional bool indicating if tables for containers should be left empty.
692  * - Ret 1: value of the variable, if any.
693  */
695 {
696  char const *m = luaL_checkstring(L, 1);
698  return luaW_pushvariable(L, v) ? 1 : 0;
699 }
700 
701 /**
702  * Sets a WML variable.
703  * - Arg 1: string containing the variable name.
704  * - Arg 2: boolean/integer/string/table containing the value.
705  */
707 {
708  const std::string m = luaL_checkstring(L, 1);
709  if(m.empty()) return luaL_argerror(L, 1, "empty variable name");
710  if (lua_isnoneornil(L, 2)) {
712  return 0;
713  }
715  luaW_checkvariable(L, v, 2);
716  return 0;
717 }
718 
719 
721 {
722  config cfg = luaW_checkconfig(L, 1);
723  cfg["side"] = teams().size() + 1;
725  lua_pushinteger(L, teams().size());
726 
727  return 1;
728 }
729 
731 {
732  game_state_.get_wml_menu_items().set_item(luaL_checkstring(L, 1), luaW_checkvconfig(L,2));
733  return 0;
734 }
735 
737 {
738  std::string ids(luaL_checkstring(L, 1));
739  for(const std::string& id : utils::split(ids, ',', utils::STRIP_SPACES)) {
740  if(id.empty()) {
741  WRN_LUA << "[clear_menu_item] has been given an empty id=, ignoring";
742  continue;
743  }
745  }
746  return 0;
747 }
748 
749 /**
750  * Toggle shroud on some locations
751  * Arg 1: Side number
752  * Arg 2: List of locations on which to place/remove shroud
753  */
754 int game_lua_kernel::intf_toggle_shroud(lua_State *L, bool place_shroud)
755 {
756  team& t = luaW_checkteam(L, 1, board());
757 
758  if(lua_istable(L, 2)) {
759  std::set<map_location> locs = luaW_check_locationset(L, 2);
760 
761  for (const map_location& loc : locs)
762  {
763  if (place_shroud) {
764  t.place_shroud(loc);
765  } else {
766  t.clear_shroud(loc);
767  }
768  }
769  } else {
770  return luaL_argerror(L, 2, "expected list of locations");
771  }
772 
776 
777  return 0;
778 }
779 
780 /**
781  * Overrides the shroud entirely. All locations are shrouded, except for the ones passed in as argument 2.
782  * Arg 1: Side number
783  * Arg 2: List of locations that should be unshrouded
784  */
786 {
787  team& t = luaW_checkteam(L, 1, board());
788 
789  if(lua_istable(L, 2)) {
790  std::set<map_location> locs = luaW_check_locationset(L, 2);
791  t.reshroud();
792  for(const map_location& loc : locs) {
793  t.clear_shroud(loc);
794  }
795  } else {
796  return luaW_type_error(L, 2, "list of locations");
797  }
798 
802 
803  return 0;
804 }
805 
806 /**
807  * Highlights the given location on the map.
808  * - Arg 1: location.
809  */
811 {
812  if (!game_display_) {
813  return 0;
814  }
815 
816  const map_location loc = luaW_checklocation(L, 1);
817  if(!map().on_board(loc)) return luaL_argerror(L, 1, "not on board");
820 
821  return 0;
822 }
823 
824 /**
825  * Returns whether the first side is an enemy of the second one.
826  * - Args 1,2: side numbers.
827  * - Ret 1: boolean.
828  */
830 {
831  unsigned side_1, side_2;
832  if(team* t = luaW_toteam(L, 1)) {
833  side_1 = t->side();
834  } else {
835  side_1 = luaL_checkinteger(L, 1);
836  }
837  if(team* t = luaW_toteam(L, 2)) {
838  side_2 = t->side();
839  } else {
840  side_2 = luaL_checkinteger(L, 2);
841  }
842  if (side_1 > teams().size() || side_2 > teams().size()) return 0;
843  lua_pushboolean(L, board().get_team(side_1).is_enemy(side_2));
844  return 1;
845 }
846 
847 /**
848  * Gets whether gamemap scrolling is disabled for the user.
849  * - Ret 1: boolean.
850  */
852 {
853  if (!game_display_) {
854  return 0;
855  }
856 
857  lua_pushboolean(L, game_display_->view_locked());
858  return 1;
859 }
860 
861 /**
862  * Sets whether gamemap scrolling is disabled for the user.
863  * - Arg 1: boolean, specifying the new locked/unlocked status.
864  */
866 {
867  bool lock = luaW_toboolean(L, 1);
868  if (game_display_) {
870  }
871  return 0;
872 }
873 
874 static void luaW_push_tod(lua_State* L, const time_of_day& tod)
875 {
876  lua_newtable(L);
877  lua_pushstring(L, tod.id.c_str());
878  lua_setfield(L, -2, "id");
879  lua_pushinteger(L, tod.lawful_bonus);
880  lua_setfield(L, -2, "lawful_bonus");
881  lua_pushinteger(L, tod.bonus_modified);
882  lua_setfield(L, -2, "bonus_modified");
883  lua_pushstring(L, tod.image.c_str());
884  lua_setfield(L, -2, "image");
885  luaW_pushtstring(L, tod.name);
886  lua_setfield(L, -2, "name");
887  lua_pushstring(L, tod.sounds.c_str());
888  lua_setfield(L, -2, "sound");
889  lua_pushstring(L, tod.image_mask.c_str());
890  lua_setfield(L, -2, "mask");
891 
892  lua_pushinteger(L, tod.color.r);
893  lua_setfield(L, -2, "red");
894  lua_pushinteger(L, tod.color.g);
895  lua_setfield(L, -2, "green");
896  lua_pushinteger(L, tod.color.b);
897  lua_setfield(L, -2, "blue");
898 }
899 
900 // A schedule object is an index with a special metatable.
901 // The global schedule uses index -1
902 void game_lua_kernel::luaW_push_schedule(lua_State* L, int area_index)
903 {
904  lua_newuserdatauv(L, 0, 1);
905  lua_pushinteger(L, area_index);
906  lua_setiuservalue(L, -2, 1);
907  if(luaL_newmetatable(L, "schedule")) {
908  static luaL_Reg const schedule_meta[] {
909  {"__index", &dispatch<&game_lua_kernel::impl_schedule_get>},
910  {"__newindex", &dispatch<&game_lua_kernel::impl_schedule_set>},
911  {"__dir", &dispatch<&game_lua_kernel::impl_schedule_dir>},
912  {"__len", &dispatch<&game_lua_kernel::impl_schedule_len>},
913  { nullptr, nullptr }
914  };
915  luaL_setfuncs(L, schedule_meta, 0);
916  }
917  lua_setmetatable(L, -2);
918 }
919 
920 static int luaW_check_schedule(lua_State* L, int idx)
921 {
922  int save_top = lua_gettop(L);
923  luaL_checkudata(L, idx, "schedule");
924  lua_getiuservalue(L, idx, 1);
925  int i = luaL_checkinteger(L, -1);
926  lua_settop(L, save_top);
927  return i;
928 }
929 
930 struct schedule_tag {
934  auto& tod_man() const { return ref.tod_man(); }
935 };
936 #define SCHEDULE_GETTER(name, type) LATTR_GETTER(name, type, schedule_tag, sched)
937 #define SCHEDULE_SETTER(name, type) LATTR_SETTER(name, type, schedule_tag, sched)
938 #define SCHEDULE_VALID(name) LATTR_VALID(name, schedule_tag, sched)
940 
941 template<> struct lua_object_traits<schedule_tag> {
942  inline static auto metatable = "schedule";
943  inline static schedule_tag get(lua_State* L, int n) {
944  schedule_tag sched{lua_kernel_base::get_lua_kernel<game_lua_kernel>(L)};
945  sched.area_index = luaW_check_schedule(L, n);
946  return sched;
947  }
948 };
949 
951 {
952  int area_index = luaW_check_schedule(L, 1);
953  if(lua_isnumber(L, 2)) {
954  const auto& times = area_index < 0 ? tod_man().times() : tod_man().times(area_index);
955  int i = lua_tointeger(L, 2) - 1;
956  if(i < 0 || i >= static_cast<int>(times.size())) {
957  return luaL_argerror(L, 2, "invalid time of day index");
958  }
959  luaW_push_tod(L, times[i]);
960  return 1;
961  }
962  return scheduleReg.get(L);
963 }
964 
966 {
967  int area_index = luaW_check_schedule(L, 1);
968  const auto& times = area_index < 0 ? tod_man().times() : tod_man().times(area_index);
969  lua_pushinteger(L, times.size());
970  return 1;
971 }
972 
974 {
975  int area_index = luaW_check_schedule(L, 1);
976  if(lua_isnumber(L, 2)) {
977  std::vector<time_of_day> times = area_index < 0 ? tod_man().times() : tod_man().times(area_index);
978  int i = lua_tointeger(L, 2) - 1;
979  if(i < 0 || i >= static_cast<int>(times.size())) {
980  return luaL_argerror(L, 2, "invalid time of day index");
981  }
982  config time_cfg = luaW_checkconfig(L, 3);
983  times[i] = time_of_day(time_cfg);
984  if(area_index < 0) {
985  tod_man().replace_schedule(times);
986  } else {
987  tod_man().replace_local_schedule(times, area_index);
988  }
989  }
990  return scheduleReg.set(L);
991 }
992 
994  return scheduleReg.dir(L);
995 }
996 
997 namespace {
998 SCHEDULE_GETTER("time_of_day", std::string) {
999  if(sched.area_index >= 0) {
1000  return sched.tod_man().get_area_time_of_day(sched.area_index).id;
1001  }
1002  return sched.tod_man().get_time_of_day().id;
1003 }
1004 
1005 SCHEDULE_SETTER("time_of_day", std::string) {
1006  const auto& times = sched.area_index < 0 ? sched.tod_man().times() : sched.tod_man().times(sched.area_index);
1007  auto iter = std::find_if(times.begin(), times.end(), [&value](const time_of_day& tod) {
1008  return tod.id == value;
1009  });
1010  if(iter == times.end()) {
1011  std::ostringstream err;
1012  err << "invalid time of day ID for ";
1013  if(sched.area_index < 0) {
1014  err << "global schedule";
1015  } else {
1016  const std::string& id = sched.tod_man().get_area_id(sched.area_index);
1017  if(id.empty()) {
1018  const auto& hexes = sched.tod_man().get_area_by_index(sched.area_index);
1019  if(hexes.empty()) {
1020  err << "anonymous empty time area";
1021  } else {
1022  err << "anonymous time area at (" << hexes.begin()->wml_x() << ',' << hexes.begin()->wml_y() << ")";
1023  }
1024  } else {
1025  err << "time area with id=" << id;
1026  }
1027  }
1028  lua_push(L, err.str());
1029  throw lua_error(L);
1030  }
1031  int n = std::distance(times.begin(), iter);
1032  if(sched.area_index < 0) {
1033  sched.tod_man().set_current_time(n);
1034  } else {
1035  sched.tod_man().set_current_time(n, sched.area_index);
1036  }
1037 }
1038 
1039 SCHEDULE_VALID("liminal_bonus") {
1040  return sched.area_index < 0;
1041 }
1042 
1043 SCHEDULE_GETTER("liminal_bonus", utils::optional<int>) {
1044  if(sched.area_index >= 0) return utils::nullopt;
1045  return sched.tod_man().get_max_liminal_bonus();
1046 }
1047 
1048 SCHEDULE_SETTER("liminal_bonus", utils::optional<int>) {
1049  if(sched.area_index >= 0) {
1050  throw luaL_error(L, "liminal_bonus can only be set on the global schedule");
1051  }
1052  if(value) {
1053  sched.tod_man().set_max_liminal_bonus(*value);
1054  } else {
1055  sched.tod_man().reset_max_liminal_bonus();
1056  }
1057 }
1058 
1059 SCHEDULE_VALID("id") {
1060  return sched.area_index >= 0;
1061 }
1062 
1063 SCHEDULE_GETTER("id", utils::optional<std::string>) {
1064  if(sched.area_index < 0) return utils::nullopt;
1065  return sched.tod_man().get_area_id(sched.area_index);
1066 }
1067 
1068 SCHEDULE_SETTER("id", std::string) {
1069  if(sched.area_index < 0) {
1070  throw luaL_error(L, "can't set id of global schedule");
1071  }
1072  sched.tod_man().set_area_id(sched.area_index, value);
1073 }
1074 
1075 SCHEDULE_VALID("hexes") {
1076  return sched.area_index >= 0;
1077 }
1078 
1079 SCHEDULE_GETTER("hexes", utils::optional<std::set<map_location>>) {
1080  if(sched.area_index < 0) return utils::nullopt;
1081  return sched.tod_man().get_area_by_index(sched.area_index);
1082 }
1083 
1084 SCHEDULE_SETTER("hexes", std::set<map_location>) {
1085  if(sched.area_index < 0) {
1086  throw luaL_error(L, "can't set hexes of global schedule");
1087  }
1088  sched.tod_man().replace_area_locations(sched.area_index, value);
1089 }
1090 }
1091 
1092 /**
1093  * Gets details about a terrain.
1094  * - Arg 1: terrain code string.
1095  * - Ret 1: table.
1096  */
1098 {
1099  char const *m = luaL_checkstring(L, 2);
1101  if (t == t_translation::NONE_TERRAIN || !board().map().tdata()->is_known(t)) return 0;
1102  const terrain_type& info = board().map().tdata()->get_terrain_info(t);
1103 
1104  lua_newtable(L);
1105  lua_pushstring(L, info.id().c_str());
1106  lua_setfield(L, -2, "id");
1107  luaW_pushtstring(L, info.name());
1108  lua_setfield(L, -2, "name");
1109  luaW_pushtstring(L, info.editor_name());
1110  lua_setfield(L, -2, "editor_name");
1111  luaW_pushtstring(L, info.description());
1112  lua_setfield(L, -2, "description");
1113  lua_push(L, info.icon_image());
1114  lua_setfield(L, -2, "icon");
1115  lua_push(L, info.editor_image());
1116  lua_setfield(L, -2, "editor_image");
1117  lua_pushinteger(L, info.light_bonus(0));
1118  lua_setfield(L, -2, "light");
1119  lua_pushboolean(L, info.is_village());
1120  lua_setfield(L, -2, "village");
1121  lua_pushboolean(L, info.is_castle());
1122  lua_setfield(L, -2, "castle");
1123  lua_pushboolean(L, info.is_keep());
1124  lua_setfield(L, -2, "keep");
1125  lua_pushinteger(L, info.gives_healing());
1126  lua_setfield(L, -2, "healing");
1127 
1128  // movement alias
1129  lua_newtable(L);
1130  int idx = 1;
1131  for (const auto& terrain : info.mvt_type()) {
1132  const terrain_type& base = board().map().tdata()->get_terrain_info(terrain);
1133  if (!base.id().empty()) {
1134  lua_pushstring(L, t_translation::write_terrain_code(base.number()).c_str());
1135  lua_rawseti(L, -2, idx++);
1136  }
1137  }
1138  lua_setfield(L, -2, "mvt_alias");
1139 
1140  // defense alias
1141  lua_newtable(L);
1142  idx = 1;
1143  for (const auto& terrain : info.def_type()) {
1144  const terrain_type& base = board().map().tdata()->get_terrain_info(terrain);
1145  if (!base.id().empty()) {
1146  lua_pushstring(L, t_translation::write_terrain_code(base.number()).c_str());
1147  lua_rawseti(L, -2, idx++);
1148  }
1149  }
1150  lua_setfield(L, -2, "def_alias");
1151 
1152  return 1;
1153 }
1154 
1155 /**
1156  * Gets a list of known terrain codes.
1157  * - Ret 1: array of terrain codes
1158  */
1160 {
1161  auto codes = board().map().tdata()->list();
1162  std::vector<std::string> terrains;
1163  terrains.reserve(codes.size());
1164  for(auto code : codes) {
1165  terrains.push_back(t_translation::write_terrain_code(code));
1166  }
1167  lua_push(L, terrains);
1168  return 1;
1169 }
1170 
1171 /**
1172  * Gets time of day information.
1173  * - Arg 1: schedule object, location, time area ID, or nil
1174  * - Arg 2: optional turn number
1175  * - Ret 1: table.
1176  */
1177 template<bool consider_illuminates>
1179 {
1180  int for_turn = tod_man().turn();
1182 
1183  if(luaW_tolocation(L, 1, loc)) {
1184  if(!board().map().on_board_with_border(loc)) {
1185  return luaL_argerror(L, 1, "coordinates are not on board");
1186  }
1187  } else if(lua_isstring(L, 1)) {
1188  auto area = tod_man().get_area_by_id(lua_tostring(L, 1));
1189  if(area.empty()) {
1190  return luaL_error(L, "invalid or empty time_area ID");
1191  }
1192  // We just need SOME location in that area, it doesn't matter which one.
1193  loc = *area.begin();
1194  } else if(!lua_isnil(L, 1)) {
1195  auto area = tod_man().get_area_by_index(luaW_check_schedule(L, 1));
1196  if(area.empty()) {
1197  return luaL_error(L, "empty time_area");
1198  }
1199  // We just need SOME location in that area, it doesn't matter which one.
1200  loc = *area.begin();
1201  }
1202 
1203  if(lua_isnumber(L, 2)) {
1204  for_turn = luaL_checkinteger(L, 2);
1205  int number_of_turns = tod_man().number_of_turns();
1206  if(for_turn < 1 || (number_of_turns != -1 && for_turn > number_of_turns)) {
1207  return luaL_argerror(L, 2, "turn number out of range");
1208  }
1209  }
1210 
1211  const time_of_day& tod = consider_illuminates ?
1212  tod_man().get_illuminated_time_of_day(board().units(), board().map(), loc, for_turn) :
1213  tod_man().get_time_of_day(loc, for_turn);
1214 
1215  luaW_push_tod(L, tod);
1216 
1217  return 1;
1218 }
1219 
1220 /**
1221  * Gets the side of a village owner.
1222  * - Arg 1: map location.
1223  * - Ret 1: integer.
1224  */
1226 {
1228  if (!board().map().is_village(loc))
1229  return 0;
1230 
1231  int side = board().village_owner(loc);
1232  if (!side) return 0;
1233  lua_pushinteger(L, side);
1234  return 1;
1235 }
1236 
1237 /**
1238  * Sets the owner of a village.
1239  * - Arg 1: map location.
1240  * - Arg 2: integer for the side or empty to remove ownership.
1241  */
1243 {
1245  if(!board().map().is_village(loc)) {
1246  return 0;
1247  }
1248 
1249  const int old_side_num = board().village_owner(loc);
1250  const int new_side_num = lua_isnoneornil(L, 2) ? 0 : luaL_checkinteger(L, 2);
1251 
1252  team* old_side = nullptr;
1253  team* new_side = nullptr;
1254 
1255  if(old_side_num == new_side_num) {
1256  return 0;
1257  }
1258 
1259  try {
1260  old_side = &board().get_team(old_side_num);
1261  } catch(const std::out_of_range&) {
1262  // old_side_num is invalid, most likely because the village wasn't captured.
1263  old_side = nullptr;
1264  }
1265 
1266  try {
1267  new_side = &board().get_team(new_side_num);
1268  } catch(const std::out_of_range&) {
1269  // new_side_num is invalid.
1270  new_side = nullptr;
1271  }
1272 
1273  // The new side was valid, but already defeated. Do nothing.
1274  if(new_side && board().team_is_defeated(*new_side)) {
1275  return 0;
1276  }
1277 
1278  // Even if the new side is not valid, we still want to remove the village from the old side.
1279  // This covers the case where new_side_num equals 0. The behavior in that case is to simply
1280  // un-assign the village from the old side, which of course we also want to happen if the new
1281  // side IS valid. If the village in question hadn't been captured, this won't fire (old_side
1282  // will be a nullptr).
1283  if(old_side) {
1284  old_side->lose_village(loc);
1285  }
1286 
1287  // If the new side was valid, re-assign the village.
1288  if(new_side) {
1289  new_side->get_village(loc, old_side_num, (luaW_toboolean(L, 3) ? &gamedata() : nullptr));
1290  }
1291 
1292  return 0;
1293 }
1294 
1295 /**
1296  * Returns the currently overed tile.
1297  * - Ret 1: x.
1298  * - Ret 2: y.
1299  */
1301 {
1302  if (!game_display_) {
1303  return 0;
1304  }
1305 
1307  if (!board().map().on_board(loc)) return 0;
1308  lua_pushinteger(L, loc.wml_x());
1309  lua_pushinteger(L, loc.wml_y());
1310  return 2;
1311 }
1312 
1313 /**
1314  * Returns the currently selected tile.
1315  * - Ret 1: x.
1316  * - Ret 2: y.
1317  */
1319 {
1320  if (!game_display_) {
1321  return 0;
1322  }
1323 
1325  if (!board().map().on_board(loc)) return 0;
1326  lua_pushinteger(L, loc.wml_x());
1327  lua_pushinteger(L, loc.wml_y());
1328  return 2;
1329 }
1330 
1331 
1332 /**
1333  * Gets a table for an resource tag.
1334  * - Arg 1: userdata (ignored).
1335  * - Arg 2: string containing id of the desired resource
1336  * - Ret 1: config for the era
1337  */
1338 static int intf_get_resource(lua_State *L)
1339 {
1340  std::string m = luaL_checkstring(L, 1);
1341  if(auto res = game_config_manager::get()->game_config().find_child("resource","id",m)) {
1342  luaW_pushconfig(L, *res);
1343  return 1;
1344  }
1345  else {
1346  return luaL_argerror(L, 1, ("Cannot find resource with id '" + m + "'").c_str());
1347  }
1348 }
1349 
1350 /**
1351  * Gets a table for an era tag.
1352  * - Arg 1: userdata (ignored).
1353  * - Arg 2: string containing id of the desired era
1354  * - Ret 1: config for the era
1355  */
1356 static int intf_get_era(lua_State *L)
1357 {
1358  std::string m = luaL_checkstring(L, 1);
1359  if(auto res = game_config_manager::get()->game_config().find_child("era","id",m)) {
1360  luaW_pushconfig(L, *res);
1361  return 1;
1362  }
1363  else {
1364  return luaL_argerror(L, 1, ("Cannot find era with id '" + m + "'").c_str());
1365  }
1366  return 1;
1367 }
1368 
1369 extern luaW_Registry& gameConfigReg();
1370 static auto& dummy = gameConfigReg(); // just to ensure it's constructed.
1371 
1375  auto& pc() const { return ref.play_controller_; }
1376  auto& gamedata() const { return ref.gamedata(); }
1377  auto& disp() const { return ref.game_display_; }
1378 };
1379 #define GAME_CONFIG_SIMPLE_SETTER(name) \
1380 GAME_CONFIG_SETTER(#name, decltype(game_config::name), game_lua_kernel) { \
1381  (void) k; \
1382  game_config::name = value; \
1383 }
1384 
1385 namespace {
1386 GAME_CONFIG_GETTER("do_healing", bool, game_lua_kernel) {
1387  game_config_glk_tag k2{k.ref};
1388  return k2.pc().gamestate().do_healing_;
1389 }
1390 
1391 GAME_CONFIG_SETTER("do_healing", bool, game_lua_kernel) {
1392  game_config_glk_tag k2{k.ref};
1393  k2.pc().gamestate().do_healing_ = value;}
1394 
1395 GAME_CONFIG_GETTER("theme", std::string, game_lua_kernel) {
1396  game_config_glk_tag k2{k.ref};
1397  return k2.gamedata().get_theme();
1398 }
1399 
1400 GAME_CONFIG_SETTER("theme", std::string, game_lua_kernel) {
1401  game_config_glk_tag k2{k.ref};
1402  k2.gamedata().set_theme(value);
1403  k2.disp()->set_theme(value);
1404 }
1405 
1406 using traits_map = std::map<std::string, config>;
1407 GAME_CONFIG_GETTER("global_traits", traits_map, game_lua_kernel) {
1408  (void)k;
1409  std::map<std::string, config> result;
1410  for(const config& trait : unit_types.traits()) {
1411  //It seems the engine never checks the id field for emptiness or duplicates
1412  //However, the worst that could happen is that the trait read later overwrites the older one,
1413  //and this is not the right place for such checks.
1414  result.emplace(trait["id"], trait);
1415  }
1416  return result;
1417 }
1418 
1427 }
1428 
1429 namespace {
1430  static config find_addon(const std::string& type, const std::string& id)
1431  {
1433  }
1434 }
1435 
1436 static int impl_end_level_data_get(lua_State* L)
1437 {
1438  const end_level_data& data = *static_cast<end_level_data*>(lua_touserdata(L, 1));
1439  const char* m = luaL_checkstring(L, 2);
1440 
1441  return_bool_attrib("linger_mode", data.transient.linger_mode);
1442  return_bool_attrib("reveal_map", data.transient.reveal_map);
1443  return_bool_attrib("carryover_report", data.transient.carryover_report);
1444  return_bool_attrib("prescenario_save", data.prescenario_save);
1445  return_bool_attrib("replay_save", data.replay_save);
1446  return_bool_attrib("proceed_to_next_level", data.proceed_to_next_level);
1447  return_bool_attrib("is_victory", data.is_victory);
1448  return_bool_attrib("is_loss", !data.is_victory);
1449  return_cstring_attrib("result", data.is_victory ? level_result::victory : "loss"); // to match wesnoth.end_level()
1450  return_string_attrib("test_result", data.test_result);
1451  return_cfg_attrib("__cfg", data.to_config_full());
1452 
1453  return 0;
1454 }
1455 
1456 namespace {
1457  struct end_level_committer {
1458  end_level_committer(end_level_data& data, play_controller& pc) : data_(data), pc_(pc) {}
1459  ~end_level_committer() {
1460  pc_.set_end_level_data(data_);
1461  }
1462  private:
1463  end_level_data& data_;
1464  play_controller& pc_;
1465  };
1466 }
1467 
1469 {
1470  end_level_data& data = *static_cast<end_level_data*>(lua_touserdata(L, 1));
1471  const char* m = luaL_checkstring(L, 2);
1472  end_level_committer commit(data, play_controller_);
1473 
1474  modify_bool_attrib("linger_mode", data.transient.linger_mode = value);
1475  modify_bool_attrib("reveal_map", data.transient.reveal_map = value);
1476  modify_bool_attrib("carryover_report", data.transient.carryover_report = value);
1477  modify_bool_attrib("prescenario_save", data.prescenario_save = value);
1478  modify_bool_attrib("replay_save", data.replay_save = value);
1479  modify_string_attrib("test_result", data.test_result = value);
1480 
1481  return 0;
1482 }
1483 
1484 static int impl_end_level_data_collect(lua_State* L)
1485 {
1486  end_level_data* data = static_cast<end_level_data*>(lua_touserdata(L, 1));
1487  data->~end_level_data();
1488  return 0;
1489 }
1490 
1491 static int impl_mp_settings_get(lua_State* L)
1492 {
1493  void* p = lua_touserdata(L, lua_upvalueindex(1));
1494  const mp_game_settings& settings = static_cast<play_controller*>(p)->get_mp_settings();
1495  if(lua_type(L, 2) == LUA_TNUMBER) {
1496  // Simulates a WML table with one [options] child and a variable number of [addon] children
1497  // TODO: Deprecate this -> mp_settings.options and mp_settings.addons
1498  size_t i = luaL_checkinteger(L, 2);
1499  if(i == 1) {
1500  lua_createtable(L, 2, 0);
1501  lua_pushstring(L, "options");
1502  lua_seti(L, -2, 1);
1503  luaW_pushconfig(L, settings.options);
1504  lua_seti(L, -2, 2);
1505  return 1;
1506  } else if(i >= 2) {
1507  i -= 2;
1508  if(i < settings.addons.size()) {
1509  auto iter = settings.addons.begin();
1510  std::advance(iter, i);
1511  config cfg;
1512  iter->second.write(cfg);
1513  cfg["id"] = iter->first;
1514 
1515  lua_createtable(L, 2, 0);
1516  lua_pushstring(L, "addon");
1517  lua_seti(L, -2, 1);
1518  luaW_pushconfig(L, cfg);
1519  lua_seti(L, -2, 2);
1520  return 1;
1521  }
1522  }
1523  } else {
1524  char const *m = luaL_checkstring(L, 2);
1525  return_string_attrib("scenario", settings.name);
1526  return_string_attrib("game_name", settings.name);
1527  return_string_attrib("hash", settings.hash);
1528  return_string_attrib("mp_era_name", settings.mp_era_name);
1529  return_string_attrib("mp_scenario", settings.mp_scenario);
1530  return_string_attrib("mp_scenario_name", settings.mp_scenario_name);
1531  return_string_attrib("mp_campaign", settings.mp_campaign);
1532  return_string_attrib("side_users", utils::join_map(settings.side_users));
1533  return_int_attrib("experience_modifier", settings.xp_modifier);
1534  return_bool_attrib("mp_countdown", settings.mp_countdown);
1535  return_int_attrib("mp_countdown_init_time", settings.mp_countdown_init_time.count());
1536  return_int_attrib("mp_countdown_turn_bonus", settings.mp_countdown_turn_bonus.count());
1537  return_int_attrib("mp_countdown_reservoir_bonus", settings.mp_countdown_reservoir_time.count());
1538  return_int_attrib("mp_countdown_action_bonus", settings.mp_countdown_action_bonus.count());
1539  return_int_attrib("mp_num_turns", settings.num_turns);
1540  return_int_attrib("mp_village_gold", settings.village_gold);
1541  return_int_attrib("mp_village_support", settings.village_support);
1542  return_bool_attrib("mp_fog", settings.fog_game);
1543  return_bool_attrib("mp_shroud", settings.shroud_game);
1544  return_bool_attrib("mp_use_map_settings", settings.use_map_settings);
1545  return_bool_attrib("mp_random_start_time", settings.random_start_time);
1546  return_bool_attrib("observer", settings.allow_observers);
1547  return_bool_attrib("allow_observers", settings.allow_observers);
1548  return_bool_attrib("private_replay", settings.private_replay);
1549  return_bool_attrib("shuffle_sides", settings.shuffle_sides);
1550  return_string_attrib("random_faction_mode", random_faction_mode::get_string(settings.mode));
1551  return_cfgref_attrib("options", settings.options);
1552  if(strcmp(m, "savegame") == 0) {
1553  auto savegame = settings.saved_game;
1554  if(savegame == saved_game_mode::type::no) {
1555  lua_pushboolean(L, false);
1556  } else {
1558  }
1559  return 1;
1560  }
1561  if(strcmp(m, "side_players") == 0) {
1562  lua_push(L, settings.side_users);
1563  return 1;
1564  }
1565  if(strcmp(m, "addons") == 0) {
1566  for(const auto& [id, addon] : settings.addons) {
1567  lua_createtable(L, 0, 4);
1568  lua_push(L, id);
1569  lua_setfield(L, -2, "id");
1570  lua_push(L, addon.name);
1571  lua_setfield(L, -2, "name");
1572  lua_pushboolean(L, addon.required);
1573  lua_setfield(L, -2, "required");
1574  if(addon.min_version) {
1575  luaW_getglobal(L, "wesnoth", "version");
1576  lua_push(L, addon.min_version->str());
1577  lua_call(L, 1, 1);
1578  lua_setfield(L, -2, "min_version");
1579  }
1580  if(addon.version) {
1581  luaW_getglobal(L, "wesnoth", "version");
1582  lua_push(L, addon.version->str());
1583  lua_call(L, 1, 1);
1584  lua_setfield(L, -2, "version");
1585  }
1586  lua_createtable(L, addon.content.size(), 0);
1587  for(const auto& content : addon.content) {
1588  lua_createtable(L, 0, 3);
1589  lua_push(L, content.id);
1590  lua_setfield(L, -2, "id");
1591  lua_push(L, content.name);
1592  lua_setfield(L, -2, "name");
1593  lua_push(L, content.type);
1594  lua_setfield(L, -2, "type");
1595  lua_seti(L, -2, lua_rawlen(L, -2) + 1);
1596  }
1597  lua_setfield(L, -2, "content");
1598  }
1599  return 1;
1600  }
1601  // Deprecated things that were moved out of mp_settings and into game_classification
1602  const game_classification& game = static_cast<play_controller*>(p)->get_classification();
1603  return_string_attrib_deprecated("mp_era", "wesnoth.scenario.mp_settings", INDEFINITE, "1.17", "Use wesnoth.scenario.era.id instead", game.era_id);
1604  return_string_attrib_deprecated("active_mods", "wesnoth.scenario.mp_settings", INDEFINITE, "1.17", "Use wesnoth.scenario.modifications instead (returns an array of modification tables)", utils::join(game.active_mods));
1605  // Expose the raw config; this is a way to ensure any new stuff can be accessed even if someone forgot to add it here.
1606  return_cfgref_attrib("__cfg", settings.to_config());
1607  }
1608  return 0;
1609 }
1610 
1611 static int impl_mp_settings_len(lua_State* L)
1612 {
1613  void* p = lua_touserdata(L, lua_upvalueindex(1));
1614  const mp_game_settings& settings = static_cast<play_controller*>(p)->get_mp_settings();
1615  lua_pushinteger(L, settings.addons.size() + 1);
1616  return 1;
1617 }
1618 
1622  auto& tod_man() const { return ref.tod_man(); }
1623  auto& gamedata() const { return ref.gamedata(); }
1624  auto& pc() const { return ref.play_controller_; }
1625  auto& cls() const { return ref.play_controller_.get_classification(); }
1626  auto end_level_set() const { return &dispatch<&game_lua_kernel::impl_end_level_data_set>; }
1627 };
1628 #define SCENARIO_GETTER(name, type) LATTR_GETTER(name, type, scenario_tag, k)
1629 #define SCENARIO_SETTER(name, type) LATTR_SETTER(name, type, scenario_tag, k)
1630 #define SCENARIO_VALID(name) LATTR_VALID(name, scenario_tag, k)
1632 
1633 template<> struct lua_object_traits<scenario_tag> {
1634  inline static auto metatable = "scenario";
1635  inline static scenario_tag get(lua_State* L, int) {
1636  return lua_kernel_base::get_lua_kernel<game_lua_kernel>(L);
1637  }
1638 };
1639 
1640 namespace {
1641 SCENARIO_GETTER("turns", int) {
1642  return k.tod_man().number_of_turns();
1643 }
1644 
1645 SCENARIO_SETTER("turns", int) {
1646  k.tod_man().set_number_of_turns_by_wml(value);
1647 }
1648 
1649 SCENARIO_GETTER("next", std::string) {
1650  return k.gamedata().next_scenario();
1651 }
1652 
1653 SCENARIO_SETTER("next", std::string) {
1654  k.gamedata().set_next_scenario(value);
1655 }
1656 
1657 SCENARIO_GETTER("id", std::string) {
1658  return k.gamedata().get_id();
1659 }
1660 
1661 SCENARIO_GETTER("name", t_string) {
1662  return k.pc().get_scenario_name();
1663 }
1664 
1665 SCENARIO_GETTER("defeat_music", std::vector<std::string>) {
1666  return k.gamedata().get_defeat_music();
1667 }
1668 
1669 SCENARIO_SETTER("defeat_music", std::vector<std::string>) {
1670  k.gamedata().set_defeat_music(value);
1671 }
1672 
1673 SCENARIO_GETTER("victory_music", std::vector<std::string>) {
1674  return k.gamedata().get_victory_music();
1675 }
1676 
1677 SCENARIO_SETTER("victory_music", std::vector<std::string>) {
1678  k.gamedata().set_victory_music(value);
1679 }
1680 
1681 SCENARIO_GETTER("resources", std::vector<config>) {
1682  std::vector<config> resources;
1683  for(const std::string& rsrc : utils::split(k.pc().get_loaded_resources())) {
1684  resources.push_back(find_addon("resource", rsrc));
1685  }
1686  return resources;
1687 }
1688 
1689 SCENARIO_GETTER("type", std::string) {
1690  return campaign_type::get_string(k.cls().type);
1691 }
1692 
1693 SCENARIO_GETTER("difficulty", std::string) {
1694  return k.cls().difficulty;
1695 }
1696 
1697 SCENARIO_GETTER("show_credits", bool) {
1698  return k.cls().end_credits;
1699 }
1700 
1701 SCENARIO_SETTER("show_credits", bool) {
1702  k.cls().end_credits = value;
1703 }
1704 
1705 SCENARIO_GETTER("end_text", t_string) {
1706  return k.cls().end_text;
1707 }
1708 
1709 SCENARIO_SETTER("end_text", t_string) {
1710  k.cls().end_text = value;
1711 }
1712 
1713 SCENARIO_GETTER("end_text_duration", int) {
1714  return k.cls().end_text_duration.count();
1715 }
1716 
1717 SCENARIO_SETTER("end_text_duration", int) {
1718  k.cls().end_text_duration = std::chrono::milliseconds{value};
1719 }
1720 
1721 SCENARIO_VALID("campaign") {
1722  return !k.cls().campaign.empty();
1723 }
1724 
1725 SCENARIO_GETTER("campaign", utils::optional<config>) {
1726  if(k.cls().campaign.empty()) return utils::nullopt;
1727  return find_addon("campaign", k.cls().campaign);
1728 }
1729 
1730 SCENARIO_GETTER("modifications", std::vector<config>) {
1731  std::vector<config> mods;
1732  for(const std::string& mod : k.cls().active_mods) {
1733  mods.push_back(find_addon("modification", mod));
1734  }
1735  return mods;
1736 }
1737 
1738 SCENARIO_GETTER("end_level_data", lua_index_raw) {
1739  if (!k.pc().is_regular_game_end()) {
1740  lua_pushnil(L);
1741  return lua_index_raw(L);
1742  }
1743  auto data = k.pc().get_end_level_data();
1744  new(L) end_level_data(data);
1745  if(luaL_newmetatable(L, "end level data")) {
1746  static luaL_Reg const callbacks[] {
1747  { "__index", &impl_end_level_data_get},
1748  { "__newindex", k.end_level_set()},
1749  { "__gc", &impl_end_level_data_collect},
1750  { nullptr, nullptr }
1751  };
1752  luaL_setfuncs(L, callbacks, 0);
1753  }
1754  lua_setmetatable(L, -2);
1755  return lua_index_raw(L);
1756 }
1757 
1758 SCENARIO_SETTER("end_level_data", vconfig) {
1760 
1761  data.proceed_to_next_level = value["proceed_to_next_level"].to_bool(true);
1762  data.transient.carryover_report = value["carryover_report"].to_bool(true);
1763  data.prescenario_save = value["save"].to_bool(true);
1764  data.replay_save = value["replay_save"].to_bool(true);
1765  data.transient.linger_mode = value["linger_mode"].to_bool(true) && !k.ref.teams().empty();
1766  data.transient.reveal_map = value["reveal_map"].to_bool(k.pc().reveal_map_default());
1767  data.is_victory = value["result"] == level_result::victory;
1768  data.test_result = value["test_result"].str();
1769  k.pc().set_end_level_data(data);
1770 }
1771 
1772 SCENARIO_VALID("mp_settings") {
1773  return k.cls().is_multiplayer();
1774 }
1775 
1776 SCENARIO_GETTER("mp_settings", lua_index_raw) {
1777  if(!k.cls().is_multiplayer()) {
1778  lua_pushnil(L);
1779  return lua_index_raw(L);
1780  }
1781  lua_newuserdatauv(L, 0, 0);
1782  if(luaL_newmetatable(L, "mp settings")) {
1783  lua_pushlightuserdata(L, &k.pc());
1784  lua_pushcclosure(L, impl_mp_settings_get, 1);
1785  lua_setfield(L, -2, "__index");
1786  lua_pushlightuserdata(L, &k.pc());
1787  lua_pushcclosure(L, impl_mp_settings_len, 1);
1788  lua_setfield(L, -2, "__len");
1789  lua_pushstring(L, "mp settings");
1790  lua_setfield(L, -2, "__metatable");
1791  }
1792  lua_setmetatable(L, -2);
1793  return lua_index_raw(L);
1794 }
1795 
1796 SCENARIO_VALID("era") {
1797  return k.cls().is_multiplayer();
1798 }
1799 
1800 SCENARIO_GETTER("era", utils::optional<config>) {
1801  if(!k.cls().is_multiplayer()) return utils::nullopt;
1802  return find_addon("era", k.cls().era_id);
1803 }
1804 }
1805 
1806 /**
1807  * Gets some scenario data (__index metamethod).
1808  * - Arg 1: userdata (ignored).
1809  * - Arg 2: string containing the name of the property.
1810  * - Ret 1: something containing the attribute.
1811  */
1813 {
1814  DBG_LUA << "impl_scenario_get";
1815  return scenarioReg.get(L);
1816 }
1817 
1818 /**
1819  * Sets some scenario data (__newindex metamethod).
1820  * - Arg 1: userdata (ignored).
1821  * - Arg 2: string containing the name of the property.
1822  * - Arg 3: something containing the attribute.
1823  */
1825 {
1826  DBG_LUA << "impl_scenario_set";
1827  return scenarioReg.set(L);
1828 }
1829 
1830 /**
1831  * Get a list of scenario data (__dir metamethod).
1832  */
1834 {
1835  DBG_LUA << "impl_scenario_dir";
1836  return scenarioReg.dir(L);
1837 }
1838 
1839 /**
1840  converts synced_context::get_synced_state() to a string.
1841 */
1843 {
1844  //maybe return "initial" for game_data::INITIAL?
1845  if(gamedata().phase() == game_data::PRELOAD || gamedata().phase() == game_data::INITIAL)
1846  {
1847  return "preload";
1848  }
1850  {
1852  return "local_choice";
1854  return "synced";
1856  return "unsynced";
1857  default:
1858  throw game::game_error("Found corrupt synced_context::synced_state");
1859  }
1860 }
1861 
1862 struct current_tag {
1865  auto& pc() const { return ref.play_controller_; }
1866  auto ss() const { return ref.synced_state(); }
1867  auto& gd() const { return ref.gamedata(); }
1868  auto& ev() const { return ref.get_event_info(); }
1869  void push_schedule(lua_State* L) const { ref.luaW_push_schedule(L, -1); }
1870 };
1871 #define CURRENT_GETTER(name, type) LATTR_GETTER(name, type, current_tag, k)
1873 
1874 template<> struct lua_object_traits<current_tag> {
1875  inline static auto metatable = "current";
1876  inline static game_lua_kernel& get(lua_State* L, int) {
1877  return lua_kernel_base::get_lua_kernel<game_lua_kernel>(L);
1878  }
1879 };
1880 
1881 namespace {
1882 CURRENT_GETTER("side", int) {
1883  return k.pc().current_side();
1884 }
1885 
1886 CURRENT_GETTER("turn", int) {
1887  return k.pc().turn();
1888 }
1889 
1890 CURRENT_GETTER("synced_state", std::string) {
1891  return k.ss();
1892 }
1893 
1894 CURRENT_GETTER("user_can_invoke_commands", bool) {
1895  return !events::commands_disabled && k.gd().phase() == game_data::TURN_PLAYING;
1896 }
1897 
1898 CURRENT_GETTER("map", lua_index_raw) {
1899  (void)k;
1901  return lua_index_raw(L);
1902 }
1903 
1904 CURRENT_GETTER("schedule", lua_index_raw) {
1905  k.push_schedule(L);
1906  return lua_index_raw(L);
1907 }
1908 
1909 CURRENT_GETTER("user_is_replaying", bool) {
1910  return k.pc().is_replay();
1911 }
1912 
1913 CURRENT_GETTER("event_context", config) {
1914  const game_events::queued_event &ev = k.ev();
1915  config cfg;
1916  cfg["name"] = ev.name;
1917  cfg["id"] = ev.id;
1918  cfg.add_child("data", ev.data);
1919  if (auto weapon = ev.data.optional_child("first")) {
1920  cfg.add_child("weapon", *weapon);
1921  }
1922  if (auto weapon = ev.data.optional_child("second")) {
1923  cfg.add_child("second_weapon", *weapon);
1924  }
1925 
1926  const config::attribute_value di = ev.data["damage_inflicted"];
1927  if(!di.empty()) {
1928  cfg["damage_inflicted"] = di;
1929  }
1930 
1931  if (ev.loc1.valid()) {
1932  cfg["x1"] = ev.loc1.filter_loc().wml_x();
1933  cfg["y1"] = ev.loc1.filter_loc().wml_y();
1934  // The position of the unit involved in this event, currently the only case where this is different from x1/y1 are enter/exit_hex events
1935  cfg["unit_x"] = ev.loc1.wml_x();
1936  cfg["unit_y"] = ev.loc1.wml_y();
1937  }
1938  if (ev.loc2.valid()) {
1939  cfg["x2"] = ev.loc2.filter_loc().wml_x();
1940  cfg["y2"] = ev.loc2.filter_loc().wml_y();
1941  }
1942  return cfg;
1943 }
1944 }
1945 
1946 /**
1947  * Gets some data about current point of game (__index metamethod).
1948  * - Arg 1: userdata (ignored).
1949  * - Arg 2: string containing the name of the property.
1950  * - Ret 1: something containing the attribute.
1951  */
1953 {
1954  return currentReg.get(L);
1955 }
1956 
1957 /**
1958  * Gets a list of date about current point of game (__dir metamethod).
1959  */
1961 {
1962  return currentReg.dir(L);
1963 }
1964 
1965 /**
1966  * Displays a message in the chat window and in the logs.
1967  * - Arg 1: optional message header.
1968  * - Arg 2 (or 1): message.
1969  */
1971 {
1972  t_string m = luaW_checktstring(L, 1);
1973  t_string h = m;
1974  if (lua_isnone(L, 2)) {
1975  h = "Lua";
1976  } else {
1977  m = luaW_checktstring(L, 2);
1978  }
1979  lua_chat(h, m);
1980  LOG_LUA << "Script says: \"" << m << "\"";
1981  return 0;
1982 }
1983 
1985 {
1986  if(!game_display_) {
1987  return 0;
1988  }
1989  double factor = luaL_checknumber(L, 1);
1990  bool relative = luaW_toboolean(L, 2);
1991  if(relative) {
1992  factor *= game_display_->get_zoom_factor();
1993  }
1994  // Passing true explicitly to avoid casting to int.
1995  // Without doing one of the two, the call is ambiguous.
1997  lua_pushnumber(L, game_display_->get_zoom_factor());
1998  return 1;
1999 }
2000 
2001 /**
2002  * Removes all messages from the chat window.
2003  */
2005 {
2006  if (game_display_) {
2008  }
2009  return 0;
2010 }
2011 
2013 {
2014  //note that next_player_number = 1, next_player_number = nteams+1 both set the next team to be the first team
2015  //but the later will make the turn counter change aswell fire turn end events accoringly etc.
2016  if (!lua_isnoneornil(L, 1)) {
2017  int max = 2 * teams().size();
2018  int npn = luaL_checkinteger(L, 1);
2019  if (npn <= 0 || npn > max) {
2020  return luaL_argerror(L, 1, "side number out of range");
2021  }
2023  }
2025  return 0;
2026 }
2027 
2028 /**
2029  * Evaluates a boolean WML conditional.
2030  * - Arg 1: WML table.
2031  * - Ret 1: boolean.
2032  */
2033 static int intf_eval_conditional(lua_State *L)
2034 {
2035  vconfig cond = luaW_checkvconfig(L, 1);
2036  bool b = game_events::conditional_passed(cond);
2037  lua_pushboolean(L, b);
2038  return 1;
2039 }
2040 
2041 
2042 /**
2043  * Finds a path between two locations.
2044  * - Arg 1: source location. (Or Arg 1: unit.)
2045  * - Arg 2: destination.
2046  * - Arg 3: optional cost function or
2047  * table (optional fields: ignore_units, ignore_teleport, max_cost, viewing_side).
2048  * - Ret 1: array of pairs containing path steps.
2049  * - Ret 2: path cost.
2050  */
2052 {
2053  int arg = 1;
2054  map_location src, dst;
2055  const unit* u = nullptr;
2056  int viewing_side = 0;
2057 
2058  if (lua_isuserdata(L, arg))
2059  {
2060  u = &luaW_checkunit(L, arg);
2061  src = u->get_location();
2062  viewing_side = u->side();
2063  ++arg;
2064  }
2065  else
2066  {
2067  src = luaW_checklocation(L, arg);
2069  if (ui.valid()) {
2070  u = ui.get_shared_ptr().get();
2071  viewing_side = u->side();
2072  }
2073  ++arg;
2074  }
2075 
2076  dst = luaW_checklocation(L, arg);
2077 
2078  if (!board().map().on_board(src))
2079  return luaL_argerror(L, 1, "invalid location");
2080  if (!board().map().on_board(dst))
2081  return luaL_argerror(L, arg, "invalid location");
2082  ++arg;
2083 
2084  const gamemap &map = board().map();
2085  bool ignore_units = false, see_all = false, ignore_teleport = false;
2086  double stop_at = 10000;
2087  std::unique_ptr<pathfind::cost_calculator> calc;
2088 
2089  if (lua_istable(L, arg))
2090  {
2091  ignore_units = luaW_table_get_def<bool>(L, arg, "ignore_units", false);
2092  see_all = luaW_table_get_def<bool>(L, arg, "ignore_visibility", false);
2093  ignore_teleport = luaW_table_get_def<bool>(L, arg, "ignore_teleport", false);
2094 
2095  stop_at = luaW_table_get_def<double>(L, arg, "max_cost", stop_at);
2096 
2097 
2098  lua_pushstring(L, "viewing_side");
2099  lua_rawget(L, arg);
2100  if (!lua_isnil(L, -1)) {
2101  int i = luaL_checkinteger(L, -1);
2102  if (i >= 1 && i <= static_cast<int>(teams().size())) viewing_side = i;
2103  else {
2104  // If there's a unit, we have a valid side, so fall back to legacy behaviour.
2105  // If we don't have a unit, legacy behaviour would be a crash, so let's not.
2106  if(u) see_all = true;
2107  deprecated_message("wesnoth.paths.find_path with viewing_side=0 (or an invalid side)", DEP_LEVEL::FOR_REMOVAL, {1, 17, 0}, "To consider fogged and hidden units, use ignore_visibility=true instead.");
2108  }
2109  }
2110  lua_pop(L, 1);
2111 
2112  lua_pushstring(L, "calculate");
2113  lua_rawget(L, arg);
2114  if(lua_isfunction(L, -1)) {
2115  calc.reset(new lua_pathfind_cost_calculator(L, lua_gettop(L)));
2116  }
2117  // Don't pop, the lua_pathfind_cost_calculator requires it to stay on the stack.
2118  }
2119  else if (lua_isfunction(L, arg))
2120  {
2121  deprecated_message("wesnoth.paths.find_path with cost_function as last argument", DEP_LEVEL::FOR_REMOVAL, {1, 17, 0}, "Use calculate=cost_function inside the path options table instead.");
2122  calc.reset(new lua_pathfind_cost_calculator(L, arg));
2123  }
2124 
2125  pathfind::teleport_map teleport_locations;
2126 
2127  if(!ignore_teleport) {
2128  if(viewing_side == 0) {
2129  lua_warning(L, "wesnoth.paths.find_path: ignore_teleport=false requires a valid viewing_side; continuing with ignore_teleport=true", false);
2130  ignore_teleport = true;
2131  } else {
2132  teleport_locations = pathfind::get_teleport_locations(*u, board().get_team(viewing_side), see_all, ignore_units);
2133  }
2134  }
2135 
2136  if (!calc) {
2137  if(!u) {
2138  return luaL_argerror(L, 1, "unit not found OR custom cost function not provided");
2139  }
2140 
2141  calc.reset(new pathfind::shortest_path_calculator(*u, board().get_team(viewing_side),
2142  teams(), map, ignore_units, false, see_all));
2143  }
2144 
2145  pathfind::plain_route res = pathfind::a_star_search(src, dst, stop_at, *calc, map.w(), map.h(),
2146  &teleport_locations);
2147 
2148  int nb = res.steps.size();
2149  lua_createtable(L, nb, 0);
2150  for (int i = 0; i < nb; ++i)
2151  {
2152  luaW_pushlocation(L, res.steps[i]);
2153  lua_rawseti(L, -2, i + 1);
2154  }
2155  lua_pushinteger(L, res.move_cost);
2156 
2157  return 2;
2158 }
2159 
2160 /**
2161  * Finds all the locations reachable by a unit.
2162  * - Arg 1: source location OR unit.
2163  * - Arg 2: optional table (optional fields: ignore_units, ignore_teleport, additional_turns, viewing_side).
2164  * - Ret 1: array of triples (coordinates + remaining movement).
2165  */
2167 {
2168  int arg = 1;
2169  const unit* u = nullptr;
2170 
2171  if (lua_isuserdata(L, arg))
2172  {
2173  u = &luaW_checkunit(L, arg);
2174  ++arg;
2175  }
2176  else
2177  {
2180  if (!ui.valid())
2181  return luaL_argerror(L, 1, "unit not found");
2182  u = ui.get_shared_ptr().get();
2183  ++arg;
2184  }
2185 
2186  int viewing_side = u->side();
2187  bool ignore_units = false, see_all = false, ignore_teleport = false;
2188  int additional_turns = 0;
2189 
2190  if (lua_istable(L, arg))
2191  {
2192  ignore_units = luaW_table_get_def<bool>(L, arg, "ignore_units", false);
2193  see_all = luaW_table_get_def<bool>(L, arg, "ignore_visibility", false);
2194  ignore_teleport = luaW_table_get_def<bool>(L, arg, "ignore_teleport", false);
2195  additional_turns = luaW_table_get_def<int>(L, arg, "max_cost", additional_turns);
2196 
2197  lua_pushstring(L, "viewing_side");
2198  lua_rawget(L, arg);
2199  if (!lua_isnil(L, -1)) {
2200  int i = luaL_checkinteger(L, -1);
2201  if (i >= 1 && i <= static_cast<int>(teams().size())) viewing_side = i;
2202  else {
2203  // If there's a unit, we have a valid side, so fall back to legacy behaviour.
2204  // If we don't have a unit, legacy behaviour would be a crash, so let's not.
2205  if(u) see_all = true;
2206  deprecated_message("wesnoth.find_reach with viewing_side=0 (or an invalid side)", DEP_LEVEL::FOR_REMOVAL, {1, 17, 0}, "To consider fogged and hidden units, use ignore_visibility=true instead.");
2207  }
2208  }
2209  lua_pop(L, 1);
2210  }
2211 
2212  const team& viewing_team = board().get_team(viewing_side);
2213 
2214  pathfind::paths res(*u, ignore_units, !ignore_teleport,
2215  viewing_team, additional_turns, see_all, ignore_units);
2216 
2217  int nb = res.destinations.size();
2218  lua_createtable(L, nb, 0);
2219  for (int i = 0; i < nb; ++i)
2220  {
2222  luaW_push_namedtuple(L, {"x", "y", "moves_left"});
2223  lua_pushinteger(L, s.curr.wml_x());
2224  lua_rawseti(L, -2, 1);
2225  lua_pushinteger(L, s.curr.wml_y());
2226  lua_rawseti(L, -2, 2);
2227  lua_pushinteger(L, s.move_left);
2228  lua_rawseti(L, -2, 3);
2229  lua_rawseti(L, -2, i + 1);
2230  }
2231 
2232  return 1;
2233 }
2234 
2235 /**
2236  * Finds all the locations for which a given unit would remove the fog (if there was fog on the map).
2237  *
2238  * - Arg 1: source location OR unit.
2239  * - Ret 1: array of triples (coordinates + remaining vision points).
2240  */
2242 {
2243  int arg = 1;
2244  const unit* u = nullptr;
2245 
2246  if (lua_isuserdata(L, arg))
2247  {
2248  u = &luaW_checkunit(L, arg);
2249  ++arg;
2250  }
2251  else
2252  {
2255  if (!ui.valid())
2256  return luaL_argerror(L, 1, "unit not found");
2257  u = ui.get_shared_ptr().get();
2258  ++arg;
2259  }
2260 
2261  if(!u)
2262  {
2263  return luaL_error(L, "wesnoth.find_vision_range: requires a valid unit");
2264  }
2265 
2266  std::map<map_location, int> jamming_map;
2267  actions::create_jamming_map(jamming_map, resources::gameboard->get_team(u->side()));
2268  pathfind::vision_path res(*u, u->get_location(), jamming_map);
2269 
2270  lua_createtable(L, res.destinations.size() + res.edges.size(), 0);
2271  for(const auto& d : res.destinations) {
2272  luaW_push_namedtuple(L, {"x", "y", "vision_left"});
2273  lua_pushinteger(L, d.curr.wml_x());
2274  lua_rawseti(L, -2, 1);
2275  lua_pushinteger(L, d.curr.wml_y());
2276  lua_rawseti(L, -2, 2);
2277  lua_pushinteger(L, d.move_left);
2278  lua_rawseti(L, -2, 3);
2279  lua_rawseti(L, -2, lua_rawlen(L, -2) + 1);
2280  }
2281  for(const auto& e : res.edges) {
2282  luaW_push_namedtuple(L, {"x", "y", "vision_left"});
2283  lua_pushinteger(L, e.wml_x());
2284  lua_rawseti(L, -2, 1);
2285  lua_pushinteger(L, e.wml_y());
2286  lua_rawseti(L, -2, 2);
2287  lua_pushinteger(L, -1);
2288  lua_rawseti(L, -2, 3);
2289  lua_rawseti(L, -2, lua_rawlen(L, -2) + 1);
2290  }
2291  return 1;
2292 }
2293 
2294 template<typename T> // This is only a template so I can avoid typing out the long typename. >_>
2295 static int load_fake_units(lua_State* L, int arg, T& fake_units)
2296 {
2297  for (int i = 1, i_end = lua_rawlen(L, arg); i <= i_end; ++i)
2298  {
2299  map_location src;
2300  lua_rawgeti(L, arg, i);
2301  int entry = lua_gettop(L);
2302  if (!lua_istable(L, entry)) {
2303  goto error;
2304  }
2305 
2306  if (!luaW_tolocation(L, entry, src)) {
2307  goto error;
2308  }
2309 
2310  lua_rawgeti(L, entry, 3);
2311  if (!lua_isnumber(L, -1)) {
2312  lua_getfield(L, entry, "side");
2313  if (!lua_isnumber(L, -1)) {
2314  goto error;
2315  }
2316  }
2317  int side = lua_tointeger(L, -1);
2318 
2319  lua_rawgeti(L, entry, 4);
2320  if (!lua_isstring(L, -1)) {
2321  lua_getfield(L, entry, "type");
2322  if (!lua_isstring(L, -1)) {
2323  goto error;
2324  }
2325  }
2326  std::string unit_type = lua_tostring(L, -1);
2327 
2328  fake_units.emplace_back(src, side, unit_type);
2329 
2330  lua_settop(L, entry - 1);
2331  }
2332  return 0;
2333 error:
2334  return luaL_argerror(L, arg, "unit type table malformed - each entry should be either array of 4 elements or table with keys x, y, side, type");
2335 }
2336 
2337 /**
2338  * Is called with one or more units and builds a cost map.
2339  * - Arg 1: source location. (Or Arg 1: unit. Or Arg 1: table containing a filter)
2340  * - Arg 2: optional array of tables with 4 elements (coordinates + side + unit type string)
2341  * - Arg 3: optional table (optional fields: ignore_units, ignore_teleport, viewing_side, debug).
2342  * - Arg 4: optional table: standard location filter.
2343  * - Ret 1: array of triples (coordinates + array of tuples(summed cost + reach counter)).
2344  */
2346 {
2347  int arg = 1;
2348  unit* unit = luaW_tounit(L, arg, true);
2350  luaW_tovconfig(L, arg, filter);
2351 
2352  std::vector<const ::unit*> real_units;
2353  typedef std::vector<std::tuple<map_location, int, std::string>> unit_type_vector;
2354  unit_type_vector fake_units;
2355 
2356 
2357  if (unit) // 1. arg - unit
2358  {
2359  real_units.push_back(unit);
2360  }
2361  else if (!filter.null()) // 1. arg - filter
2362  {
2363  for(const ::unit* match : unit_filter(filter).all_matches_on_map()) {
2364  if(match->get_location().valid()) {
2365  real_units.push_back(match);
2366  }
2367  }
2368  }
2369  else // 1. arg - coordinates
2370  {
2373  if (ui.valid())
2374  {
2375  real_units.push_back(&(*ui));
2376  }
2377  }
2378  ++arg;
2379 
2380  if (lua_istable(L, arg)) // 2. arg - optional types
2381  {
2382  load_fake_units(L, arg, fake_units);
2383  ++arg;
2384  }
2385 
2386  if(real_units.empty() && fake_units.empty())
2387  {
2388  return luaL_argerror(L, 1, "unit(s) not found");
2389  }
2390 
2391  int viewing_side = 0;
2392  bool ignore_units = true, see_all = true, ignore_teleport = false, debug = false, use_max_moves = false;
2393 
2394  if (lua_istable(L, arg)) // 4. arg - options
2395  {
2396  lua_pushstring(L, "ignore_units");
2397  lua_rawget(L, arg);
2398  if (!lua_isnil(L, -1))
2399  {
2400  ignore_units = luaW_toboolean(L, -1);
2401  }
2402  lua_pop(L, 1);
2403 
2404  lua_pushstring(L, "ignore_teleport");
2405  lua_rawget(L, arg);
2406  if (!lua_isnil(L, -1))
2407  {
2408  ignore_teleport = luaW_toboolean(L, -1);
2409  }
2410  lua_pop(L, 1);
2411 
2412  lua_pushstring(L, "viewing_side");
2413  lua_rawget(L, arg);
2414  if (!lua_isnil(L, -1))
2415  {
2416  int i = luaL_checkinteger(L, -1);
2417  if (i >= 1 && i <= static_cast<int>(teams().size()))
2418  {
2419  viewing_side = i;
2420  see_all = false;
2421  }
2422  }
2423 
2424  lua_pushstring(L, "debug");
2425  lua_rawget(L, arg);
2426  if (!lua_isnil(L, -1))
2427  {
2428  debug = luaW_toboolean(L, -1);
2429  }
2430  lua_pop(L, 1);
2431 
2432  lua_pushstring(L, "use_max_moves");
2433  lua_rawget(L, arg);
2434  if (!lua_isnil(L, -1))
2435  {
2436  use_max_moves = luaW_toboolean(L, -1);
2437  }
2438  lua_pop(L, 1);
2439  ++arg;
2440  }
2441 
2442  // 5. arg - location filter
2444  std::set<map_location> location_set;
2445  luaW_tovconfig(L, arg, filter);
2446  if (filter.null())
2447  {
2448  filter = vconfig(config(), true);
2449  }
2450  filter_context & fc = game_state_;
2451  const terrain_filter t_filter(filter, &fc, false);
2452  t_filter.get_locations(location_set, true);
2453  ++arg;
2454 
2455  // build cost_map
2456  const team& viewing_team = viewing_side
2457  ? board().get_team(viewing_side)
2458  : board().teams()[0];
2459 
2460  pathfind::full_cost_map cost_map(
2461  ignore_units, !ignore_teleport, viewing_team, see_all, ignore_units);
2462 
2463  for (const ::unit* const u : real_units)
2464  {
2465  cost_map.add_unit(*u, use_max_moves);
2466  }
2467  for (const unit_type_vector::value_type& fu : fake_units)
2468  {
2469  const unit_type* ut = unit_types.find(std::get<2>(fu));
2470  cost_map.add_unit(std::get<0>(fu), ut, std::get<1>(fu));
2471  }
2472 
2473  if (debug)
2474  {
2475  if (game_display_) {
2477  for (const map_location& loc : location_set)
2478  {
2479  std::stringstream s;
2480  s << cost_map.get_pair_at(loc).first;
2481  s << " / ";
2482  s << cost_map.get_pair_at(loc).second;
2483  game_display_->labels().set_label(loc, s.str());
2484  }
2485  }
2486  }
2487 
2488  // create return value
2489  lua_createtable(L, location_set.size(), 0);
2490  int counter = 1;
2491  for (const map_location& loc : location_set)
2492  {
2493  luaW_push_namedtuple(L, {"x", "y", "cost", "reach"});
2494 
2495  lua_pushinteger(L, loc.wml_x());
2496  lua_rawseti(L, -2, 1);
2497 
2498  lua_pushinteger(L, loc.wml_y());
2499  lua_rawseti(L, -2, 2);
2500 
2501  lua_pushinteger(L, cost_map.get_pair_at(loc).first);
2502  lua_rawseti(L, -2, 3);
2503 
2504  lua_pushinteger(L, cost_map.get_pair_at(loc).second);
2505  lua_rawseti(L, -2, 4);
2506 
2507  lua_rawseti(L, -2, counter);
2508  ++counter;
2509  }
2510  return 1;
2511 }
2512 
2513 const char* labelKey = "floating label";
2514 
2515 static int* luaW_check_floating_label(lua_State* L, int idx)
2516 {
2517  return reinterpret_cast<int*>(luaL_checkudata(L, idx, labelKey));
2518 }
2519 
2520 static int impl_floating_label_getmethod(lua_State* L)
2521 {
2522  const char* m = luaL_checkstring(L, 2);
2523  return_bool_attrib("valid", *luaW_check_floating_label(L, 1) != 0);
2524  return luaW_getmetafield(L, 1, m);
2525 }
2526 
2528 {
2529  int* handle = luaW_check_floating_label(L, 1);
2530  std::chrono::milliseconds fade{luaL_optinteger(L, 2, -1)};
2531  if(*handle != 0) {
2532  // Passing -1 as the second argument means it uses the fade time that was set when the label was created
2534  }
2535  *handle = 0;
2536  return 0;
2537 }
2538 
2540 {
2541  int* handle = luaW_check_floating_label(L, 1);
2542  if(*handle != 0) {
2543  font::move_floating_label(*handle, luaL_checknumber(L, 2), luaL_checknumber(L, 3));
2544  }
2545  return 0;
2546 }
2547 
2548 /**
2549  * Arg 1: text - string
2550  * Arg 2: options table
2551  * - size: font size
2552  * - max_width: max width for word wrapping
2553  * - color: font color
2554  * - bgcolor: background color
2555  * - bgalpha: background opacity
2556  * - duration: display duration (integer or the string "unlimited")
2557  * - fade_time: duration of fade-out
2558  * - location: screen offset
2559  * - valign: vertical alignment and anchoring - "top", "center", or "bottom"
2560  * - halign: horizontal alignment and anchoring - "left", "center", or "right"
2561  * Returns: label handle
2562  */
2563 int game_lua_kernel::intf_set_floating_label(lua_State* L, bool spawn)
2564 {
2565  t_string text = luaW_checktstring(L, 1);
2566  int size = font::SIZE_SMALL;
2567  int width = 0;
2568  double width_ratio = 0;
2569  color_t color = font::LABEL_COLOR, bgcolor{0, 0, 0, 0};
2570  int lifetime = 2'000, fadeout = 100;
2571  font::ALIGN alignment = font::ALIGN::CENTER_ALIGN, vertical_alignment = font::ALIGN::CENTER_ALIGN;
2572  // This is actually a relative screen location in pixels, but map_location already supports
2573  // everything needed to read in a pair of coordinates.
2574  // Depending on the chosen alignment, it may be relative to centre, an edge centre, or a corner.
2575  map_location loc{0, 0, wml_loc()};
2576  if(lua_istable(L, 2)) {
2577  if(luaW_tableget(L, 2, "size")) {
2578  size = luaL_checkinteger(L, -1);
2579  }
2580  if(luaW_tableget(L, 2, "max_width")) {
2581  int found_number;
2582  width = lua_tointegerx(L, -1, &found_number);
2583  if(!found_number) {
2584  auto value = luaW_tostring(L, -1);
2585  try {
2586  if(!value.empty() && value.back() == '%') {
2587  value.remove_suffix(1);
2588  width_ratio = std::stoi(std::string(value)) / 100.0;
2589  } else throw std::invalid_argument(value.data());
2590  } catch(std::invalid_argument&) {
2591  return luaL_argerror(L, -1, "max_width should be integer or percentage");
2592  }
2593 
2594  }
2595  }
2596  if(luaW_tableget(L, 2, "color")) {
2597  if(lua_isstring(L, -1)) {
2598  color = color_t::from_hex_string(lua_tostring(L, -1));
2599  } else {
2600  auto vec = lua_check<std::vector<int>>(L, -1);
2601  if(vec.size() != 3) {
2602  int idx = lua_absindex(L, -1);
2603  if(luaW_tableget(L, idx, "r") && luaW_tableget(L, idx, "g") && luaW_tableget(L, idx, "b")) {
2604  color.r = luaL_checkinteger(L, -3);
2605  color.g = luaL_checkinteger(L, -2);
2606  color.b = luaL_checkinteger(L, -1);
2607  } else {
2608  return luaL_error(L, "floating label text color should be a hex string, an array of 3 integers, or a table with r,g,b keys");
2609  }
2610  } else {
2611  color.r = vec[0];
2612  color.g = vec[1];
2613  color.b = vec[2];
2614  }
2615  }
2616  }
2617  if(luaW_tableget(L, 2, "bgcolor")) {
2618  if(lua_isstring(L, -1)) {
2619  bgcolor = color_t::from_hex_string(lua_tostring(L, -1));
2620  } else {
2621  auto vec = lua_check<std::vector<int>>(L, -1);
2622  if(vec.size() != 3) {
2623  int idx = lua_absindex(L, -1);
2624  if(luaW_tableget(L, idx, "r") && luaW_tableget(L, idx, "g") && luaW_tableget(L, idx, "b")) {
2625  bgcolor.r = luaL_checkinteger(L, -3);
2626  bgcolor.g = luaL_checkinteger(L, -2);
2627  bgcolor.b = luaL_checkinteger(L, -1);
2628  } else {
2629  return luaL_error(L, "floating label background color should be a hex string, an array of 3 integers, or a table with r,g,b keys");
2630  }
2631  } else {
2632  bgcolor.r = vec[0];
2633  bgcolor.g = vec[1];
2634  bgcolor.b = vec[2];
2635  }
2636  bgcolor.a = ALPHA_OPAQUE;
2637  }
2638  if(luaW_tableget(L, 2, "bgalpha")) {
2639  bgcolor.a = luaL_checkinteger(L, -1);
2640  }
2641  }
2642  if(luaW_tableget(L, 2, "duration")) {
2643  int found_number;
2644  lifetime = lua_tointegerx(L, -1, &found_number);
2645  if(!found_number) {
2646  auto value = luaW_tostring(L, -1);
2647  if(value == "unlimited") {
2648  lifetime = -1;
2649  } else {
2650  return luaL_argerror(L, -1, "duration should be integer or 'unlimited'");
2651  }
2652  }
2653  }
2654  if(luaW_tableget(L, 2, "fade_time")) {
2655  fadeout = lua_tointeger(L, -1);
2656  }
2657  if(luaW_tableget(L, 2, "location")) {
2658  loc = luaW_checklocation(L, -1);
2659  }
2660  if(luaW_tableget(L, 2, "halign")) {
2661  static const char* options[] = {"left", "center", "right"};
2662  alignment = font::ALIGN(luaL_checkoption(L, -1, nullptr, options));
2663  }
2664  if(luaW_tableget(L, 2, "valign")) {
2665  static const char* options[] = {"top", "center", "bottom"};
2666  vertical_alignment = font::ALIGN(luaL_checkoption(L, -1, nullptr, options));
2667  }
2668  }
2669 
2670  int* handle = nullptr;
2671  if(spawn) {
2672  // Creating a new label, allocate a new handle
2673  handle = new(L)int();
2674  } else {
2675  // First argument is the label handle
2677  }
2678  int handle_idx = lua_gettop(L);
2679 
2680  if(*handle != 0) {
2682  }
2683 
2684  const SDL_Rect rect = game_display_->map_outside_area();
2685  if(width_ratio > 0) {
2686  width = static_cast<int>(std::round(rect.w * width_ratio));
2687  }
2688  int x = 0, y = 0;
2689  switch(alignment) {
2691  x = rect.x + loc.wml_x();
2692  break;
2694  x = rect.x + rect.w / 2 + loc.wml_x();
2695  break;
2697  x = rect.x + rect.w - loc.wml_x();
2698  break;
2699  }
2700  switch(vertical_alignment) {
2701  case font::ALIGN::LEFT_ALIGN: // top
2702  y = rect.y + loc.wml_y();
2703  break;
2705  y = rect.y + rect.h / 2 + loc.wml_y();
2706  break;
2707  case font::ALIGN::RIGHT_ALIGN: // bottom
2708  // The size * 1.5 adjustment avoids the text being cut off if placed at y = 0
2709  // This is necessary because the text is positioned by the top edge but we want it to
2710  // seem like it's positioned by the bottom edge.
2711  // This wouldn't work for multiline text, but we don't expect that to be common in this API anyway.
2712  y = rect.y + rect.h - loc.wml_y() - static_cast<int>(size * 1.5);
2713  break;
2714  }
2715 
2716  using std::chrono::milliseconds;
2717  font::floating_label flabel(text);
2718  flabel.set_font_size(size);
2719  flabel.set_color(color);
2720  flabel.set_bg_color(bgcolor);
2721  flabel.set_alignment(alignment);
2722  flabel.set_position(x, y);
2723  flabel.set_lifetime(milliseconds{lifetime}, milliseconds{fadeout});
2724  flabel.set_clip_rect(rect);
2725 
2726  // Adjust fallback width (map area) to avoid text escaping when location != 0
2727  if(width > 0) {
2728  flabel.set_width(std::min(width, rect.w - loc.wml_x()));
2729  } else {
2730  flabel.set_width(rect.w - loc.wml_x());
2731  }
2732 
2733  *handle = font::add_floating_label(flabel);
2734  lua_settop(L, handle_idx);
2735  if(luaL_newmetatable(L, labelKey)) {
2736  // Initialize the metatable
2737  static const luaL_Reg methods[] = {
2738  {"remove", &dispatch<&game_lua_kernel::intf_remove_floating_label>},
2739  {"move", &dispatch<&game_lua_kernel::intf_move_floating_label>},
2740  {"replace", &dispatch2<&game_lua_kernel::intf_set_floating_label, false>},
2741  {"__index", &impl_floating_label_getmethod},
2742  { nullptr, nullptr }
2743  };
2744  luaL_setfuncs(L, methods, 0);
2745  luaW_table_set(L, -1, "__metatable", std::string(labelKey));
2746  }
2747  lua_setmetatable(L, handle_idx);
2748  lua_settop(L, handle_idx);
2749  return 1;
2750 }
2751 
2753 {
2754  if(game_display_) {
2756  }
2757 
2758  resources::whiteboard->on_kill_unit();
2759 }
2760 
2761 /**
2762  * Places a unit on the map.
2763  * - Arg 1: (optional) location.
2764  * - Arg 2: Unit (WML table or proxy), or nothing/nil to delete.
2765  * OR
2766  * - Arg 1: Unit (WML table or proxy)
2767  * - Arg 2: (optional) location
2768  * - Arg 3: (optional) boolean
2769  */
2771 {
2772  if(map_locked_) {
2773  return luaL_error(L, "Attempted to move a unit while the map is locked");
2774  }
2775 
2776  map_location loc;
2777  if (luaW_tolocation(L, 2, loc)) {
2778  if (!map().on_board(loc)) {
2779  return luaL_argerror(L, 2, "invalid location");
2780  }
2781  }
2782 
2783  if((luaW_isunit(L, 1))) {
2784  lua_unit& u = *luaW_checkunit_ref(L, 1);
2785  if(u.on_map() && u->get_location() == loc) {
2786  return 0;
2787  }
2788  if (!loc.valid()) {
2789  loc = u->get_location();
2790  if (!map().on_board(loc))
2791  return luaL_argerror(L, 1, "invalid location");
2792  }
2793 
2795  u.put_map(loc);
2796  u.get_shared()->anim_comp().set_standing();
2798  } else if(!lua_isnoneornil(L, 1)) {
2799  const vconfig* vcfg = nullptr;
2800  config cfg = luaW_checkconfig(L, 1, vcfg);
2801  if (!map().on_board(loc)) {
2802  loc.set_wml_x(cfg["x"].to_int());
2803  loc.set_wml_y(cfg["y"].to_int());
2804  if (!map().on_board(loc))
2805  return luaL_argerror(L, 2, "invalid location");
2806  }
2807 
2808  unit_ptr u = unit::create(cfg, true, vcfg);
2809  units().erase(loc);
2811  u->set_location(loc);
2812  units().insert(u);
2813  u->anim_comp().reset_affect_adjacent(units());
2814  }
2815 
2816  // Fire event if using the deprecated version or if the final argument is not false
2817  // If the final boolean argument is omitted, the actual final argument (the unit or location) will always yield true.
2818  if(luaW_toboolean(L, -1)) {
2819  play_controller_.pump().fire("unit_placed", loc);
2820  }
2821  return 0;
2822 }
2823 
2824 /**
2825  * Erases a unit from the map
2826  * - Arg 1: Unit to erase OR Location to erase unit
2827  */
2829 {
2830  if(map_locked_) {
2831  return luaL_error(L, "Attempted to remove a unit while the map is locked");
2832  }
2833  map_location loc;
2834 
2835  if(luaW_isunit(L, 1)) {
2836  lua_unit& u = *luaW_checkunit_ref(L, 1);
2837  if (u.on_map()) {
2838  loc = u->get_location();
2839  if (!map().on_board(loc)) {
2840  return luaL_argerror(L, 1, "invalid location");
2841  }
2843  } else if (int side = u.on_recall_list()) {
2844  team &t = board().get_team(side);
2845  // Should it use underlying ID instead?
2846  t.recall_list().erase_if_matches_id(u->id());
2847  } else {
2848  return luaL_argerror(L, 1, "can't erase private units");
2849  }
2850  } else if (luaW_tolocation(L, 1, loc)) {
2851  if (!map().on_board(loc)) {
2852  return luaL_argerror(L, 1, "invalid location");
2853  }
2854  } else {
2855  return luaL_argerror(L, 1, "expected unit or location");
2856  }
2857 
2858  units().erase(loc);
2859  resources::whiteboard->on_kill_unit();
2860  return 0;
2861 }
2862 
2863 /**
2864  * Puts a unit on a recall list.
2865  * - Arg 1: WML table or unit.
2866  * - Arg 2: (optional) side.
2867  */
2869 {
2870  if(map_locked_) {
2871  return luaL_error(L, "Attempted to move a unit while the map is locked");
2872  }
2873  lua_unit *lu = nullptr;
2874  unit_ptr u = unit_ptr();
2875  int side = lua_tointeger(L, 2);
2876  if (static_cast<unsigned>(side) > teams().size()) side = 0;
2877 
2878  if(luaW_isunit(L, 1)) {
2879  lu = luaW_checkunit_ref(L, 1);
2880  u = lu->get_shared();
2881  if(lu->on_recall_list() && lu->on_recall_list() == side) {
2882  return luaL_argerror(L, 1, "unit already on recall list");
2883  }
2884  } else {
2885  const vconfig* vcfg = nullptr;
2886  config cfg = luaW_checkconfig(L, 1, vcfg);
2887  u = unit::create(cfg, true, vcfg);
2888  }
2889 
2890  if (!side) {
2891  side = u->side();
2892  } else {
2893  u->set_side(side);
2894  }
2895  team &t = board().get_team(side);
2896  // Avoid duplicates in the recall list.
2897  std::size_t uid = u->underlying_id();
2898  t.recall_list().erase_by_underlying_id(uid);
2899  t.recall_list().add(u);
2900  if (lu) {
2901  if (lu->on_map()) {
2902  units().erase(u->get_location());
2903  resources::whiteboard->on_kill_unit();
2904  u->anim_comp().clear_haloes();
2905  u->anim_comp().reset_affect_adjacent(units());
2906  }
2907  lu->lua_unit::~lua_unit();
2908  new(lu) lua_unit(side, uid);
2909  }
2910 
2911  return 0;
2912 }
2913 
2914 /**
2915  * Extracts a unit from the map or a recall list and gives it to Lua.
2916  * - Arg 1: unit userdata.
2917  */
2919 {
2920  if(map_locked_) {
2921  return luaL_error(L, "Attempted to remove a unit while the map is locked");
2922  }
2923  lua_unit* lu = luaW_checkunit_ref(L, 1);
2924  unit_ptr u = lu->get_shared();
2925 
2926  if (lu->on_map()) {
2927  u = units().extract(u->get_location());
2928  assert(u);
2929  u->anim_comp().clear_haloes();
2930  u->anim_comp().reset_affect_adjacent(units());
2931  } else if (int side = lu->on_recall_list()) {
2932  team &t = board().get_team(side);
2933  unit_ptr v = u->clone();
2934  t.recall_list().erase_if_matches_id(u->id());
2935  u = v;
2936  } else {
2937  return 0;
2938  }
2939 
2940  lu->lua_unit::~lua_unit();
2941  new(lu) lua_unit(u);
2942  return 0;
2943 }
2944 
2945 /**
2946  * Finds a vacant tile.
2947  * - Arg 1: location.
2948  * - Arg 2: optional unit for checking movement type.
2949  * - Rets 1,2: location.
2950  */
2952 {
2954 
2955  unit_ptr u;
2956  if (!lua_isnoneornil(L, 2)) {
2957  if(luaW_isunit(L, 2)) {
2958  u = luaW_checkunit_ptr(L, 2, false);
2959  } else {
2960  const vconfig* vcfg = nullptr;
2961  config cfg = luaW_checkconfig(L, 2, vcfg);
2962  u = unit::create(cfg, false, vcfg);
2963  if (u->get_location().valid()) {
2964  u->anim_comp().reset_affect_adjacent(units());
2965  }
2966  }
2967  }
2968 
2970 
2971  if (!res.valid()) return 0;
2972  lua_pushinteger(L, res.wml_x());
2973  lua_pushinteger(L, res.wml_y());
2974  return 2;
2975 }
2976 
2977 /**
2978  * Floats some text on the map.
2979  * - Arg 1: location.
2980  * - Arg 2: string.
2981  * - Arg 3: color.
2982  */
2984 {
2986  color_t color = font::LABEL_COLOR;
2987 
2988  t_string text = luaW_checktstring(L, 2);
2989  if (!lua_isnoneornil(L, 3)) {
2990  color = color_t::from_rgb_string(luaL_checkstring(L, 3));
2991  }
2992 
2993  if (game_display_) {
2994  game_display_->float_label(loc, text, color);
2995  }
2996  return 0;
2997 }
2998 
2999 namespace
3000 {
3001  const unit_map& get_unit_map()
3002  {
3003  // Used if we're in the game, including during the construction of the display_context
3004  if(resources::gameboard) {
3005  return resources::gameboard->units();
3006  }
3007 
3008  // If we get here, we're in the scenario editor
3009  assert(display::get_singleton());
3010  return display::get_singleton()->context().units();
3011  }
3012  void reset_affect_adjacent(const unit& unit)
3013  {
3014  unit.anim_comp().reset_affect_adjacent(get_unit_map());
3015  }
3016 }
3017 
3018 /**
3019  * Creates a unit from its WML description.
3020  * - Arg 1: WML table.
3021  * - Ret 1: unit userdata.
3022  */
3023 static int intf_create_unit(lua_State *L)
3024 {
3025  const vconfig* vcfg = nullptr;
3026  config cfg = luaW_checkconfig(L, 1, vcfg);
3027  unit_ptr u = unit::create(cfg, true, vcfg);
3028  luaW_pushunit(L, u);
3029  if (u->get_location().valid()) {
3030  reset_affect_adjacent(*u);
3031  }
3032  return 1;
3033 }
3034 
3035 /**
3036  * Copies a unit.
3037  * - Arg 1: unit userdata.
3038  * - Ret 1: unit userdata.
3039  */
3040 static int intf_copy_unit(lua_State *L)
3041 {
3042  unit& u = luaW_checkunit(L, 1);
3043  luaW_pushunit(L, u.clone());
3044  return 1;
3045 }
3046 
3047 /**
3048  * Returns unit resistance against a given attack type.
3049  * - Arg 1: unit userdata.
3050  * - Arg 2: string containing the attack type.
3051  * - Arg 3: boolean indicating if attacker.
3052  * - Arg 4: optional location.
3053  * - Ret 1: integer.
3054  */
3055 static int intf_unit_resistance(lua_State *L)
3056 {
3057  const unit& u = luaW_checkunit(L, 1);
3058  char const *m = luaL_checkstring(L, 2);
3059  bool a = false;
3061 
3062  if(lua_isboolean(L, 3)) {
3063  a = luaW_toboolean(L, 3);
3064  if(!lua_isnoneornil(L, 4)) {
3065  loc = luaW_checklocation(L, 4);
3066  }
3067  } else if(!lua_isnoneornil(L, 3)) {
3068  loc = luaW_checklocation(L, 3);
3069  }
3070 
3071  lua_pushinteger(L, 100 - u.resistance_against(m, a, loc));
3072  return 1;
3073 }
3074 
3075 /**
3076  * Returns unit movement cost on a given terrain.
3077  * - Arg 1: unit userdata.
3078  * - Arg 2: string containing the terrain type.
3079  * - Ret 1: integer.
3080  */
3081 static int intf_unit_movement_cost(lua_State *L)
3082 {
3083  const unit& u = luaW_checkunit(L, 1);
3085  map_location loc;
3086  if(luaW_tolocation(L, 2, loc)) {
3087  t = static_cast<const game_board*>(resources::gameboard)->map()[loc];
3088  } else if(lua_isstring(L, 2)) {
3089  char const *m = luaL_checkstring(L, 2);
3091  } else return luaW_type_error(L, 2, "location or terrain string");
3092  lua_pushinteger(L, u.movement_cost(t));
3093  return 1;
3094 }
3095 
3096 /**
3097  * Returns unit vision cost on a given terrain.
3098  * - Arg 1: unit userdata.
3099  * - Arg 2: string containing the terrain type.
3100  * - Ret 1: integer.
3101  */
3102 static int intf_unit_vision_cost(lua_State *L)
3103 {
3104  const unit& u = luaW_checkunit(L, 1);
3106  map_location loc;
3107  if(luaW_tolocation(L, 2, loc)) {
3108  t = static_cast<const game_board*>(resources::gameboard)->map()[loc];
3109  } else if(lua_isstring(L, 2)) {
3110  char const *m = luaL_checkstring(L, 2);
3112  } else return luaW_type_error(L, 2, "location or terrain string");
3113  lua_pushinteger(L, u.vision_cost(t));
3114  return 1;
3115 }
3116 
3117 /**
3118  * Returns unit jamming cost on a given terrain.
3119  * - Arg 1: unit userdata.
3120  * - Arg 2: string containing the terrain type.
3121  * - Ret 1: integer.
3122  */
3123 static int intf_unit_jamming_cost(lua_State *L)
3124 {
3125  const unit& u = luaW_checkunit(L, 1);
3127  map_location loc;
3128  if(luaW_tolocation(L, 2, loc)) {
3129  t = static_cast<const game_board*>(resources::gameboard)->map()[loc];
3130  } else if(lua_isstring(L, 2)) {
3131  char const *m = luaL_checkstring(L, 2);
3133  } else return luaW_type_error(L, 2, "location or terrain string");
3134  lua_pushinteger(L, u.jamming_cost(t));
3135  return 1;
3136 }
3137 
3138 /**
3139  * Returns unit defense on a given terrain.
3140  * - Arg 1: unit userdata.
3141  * - Arg 2: string containing the terrain type.
3142  * - Ret 1: integer.
3143  */
3144 static int intf_unit_defense(lua_State *L)
3145 {
3146  const unit& u = luaW_checkunit(L, 1);
3148  map_location loc;
3149  if(luaW_tolocation(L, 2, loc)) {
3150  t = static_cast<const game_board*>(resources::gameboard)->map()[loc];
3151  } else if(lua_isstring(L, 2)) {
3152  char const *m = luaL_checkstring(L, 2);
3154  } else return luaW_type_error(L, 2, "location or terrain string");
3155  lua_pushinteger(L, 100 - u.defense_modifier(t));
3156  return 1;
3157 }
3158 
3159 /**
3160  * Returns true if the unit has the given ability enabled.
3161  * - Arg 1: unit userdata.
3162  * - Arg 2: string.
3163  * - Ret 1: boolean.
3164  */
3166 {
3167  const unit& u = luaW_checkunit(L, 1);
3168  char const *m = luaL_checkstring(L, 2);
3169  lua_pushboolean(L, u.get_ability_bool(m));
3170  return 1;
3171 }
3172 
3173 /**
3174  * Changes a unit to the given unit type.
3175  * - Arg 1: unit userdata.
3176  * - Arg 2: unit type name
3177  * - Arg 3: (optional) unit variation name
3178  */
3179 static int intf_transform_unit(lua_State *L)
3180 {
3181  unit& u = luaW_checkunit(L, 1);
3182  char const *m = luaL_checkstring(L, 2);
3183  const unit_type *utp = unit_types.find(m);
3184  if (!utp) return luaL_argerror(L, 2, "unknown unit type");
3185  if(lua_isstring(L, 3)) {
3186  const std::string& m2 = lua_tostring(L, 3);
3187  if(!utp->has_variation(m2)) return luaL_argerror(L, 2, "unknown unit variation");
3188  utp = &utp->get_variation(m2);
3189  }
3190  u.advance_to(*utp);
3191  if (u.get_location().valid()) {
3192  reset_affect_adjacent(u);
3193  }
3194 
3195  return 0;
3196 }
3197 
3198 /**
3199  * Puts a table at the top of the stack with some combat result.
3200  */
3201 static void luaW_pushsimdata(lua_State *L, const combatant &cmb)
3202 {
3203  int n = cmb.hp_dist.size();
3204  lua_createtable(L, 0, 4);
3205  lua_pushnumber(L, cmb.poisoned);
3206  lua_setfield(L, -2, "poisoned");
3207  lua_pushnumber(L, cmb.slowed);
3208  lua_setfield(L, -2, "slowed");
3209  lua_pushnumber(L, cmb.untouched);
3210  lua_setfield(L, -2, "untouched");
3211  lua_pushnumber(L, cmb.average_hp());
3212  lua_setfield(L, -2, "average_hp");
3213  lua_createtable(L, n, 0);
3214  for (int i = 0; i < n; ++i) {
3215  lua_pushnumber(L, cmb.hp_dist[i]);
3216  lua_rawseti(L, -2, i);
3217  }
3218  lua_setfield(L, -2, "hp_chance");
3219 }
3220 
3221 /**
3222  * Puts a table at the top of the stack with information about the combatants' weapons.
3223  */
3224 static void luaW_pushsimweapon(lua_State *L, const battle_context_unit_stats &bcustats)
3225 {
3226 
3227  lua_createtable(L, 0, 16);
3228 
3229  lua_pushnumber(L, bcustats.num_blows);
3230  lua_setfield(L, -2, "num_blows");
3231  lua_pushnumber(L, bcustats.damage);
3232  lua_setfield(L, -2, "damage");
3233  lua_pushnumber(L, bcustats.chance_to_hit);
3234  lua_setfield(L, -2, "chance_to_hit");
3235  lua_pushboolean(L, bcustats.poisons);
3236  lua_setfield(L, -2, "poisons");
3237  lua_pushboolean(L, bcustats.slows);
3238  lua_setfield(L, -2, "slows");
3239  lua_pushboolean(L, bcustats.petrifies);
3240  lua_setfield(L, -2, "petrifies");
3241  lua_pushboolean(L, bcustats.plagues);
3242  lua_setfield(L, -2, "plagues");
3243  lua_pushstring(L, bcustats.plague_type.c_str());
3244  lua_setfield(L, -2, "plague_type");
3245  lua_pushnumber(L, bcustats.rounds);
3246  lua_setfield(L, -2, "rounds");
3247  lua_pushboolean(L, bcustats.firststrike);
3248  lua_setfield(L, -2, "firststrike");
3249  lua_pushboolean(L, bcustats.drains);
3250  lua_setfield(L, -2, "drains");
3251  lua_pushnumber(L, bcustats.drain_constant);
3252  lua_setfield(L, -2, "drain_constant");
3253  lua_pushnumber(L, bcustats.drain_percent);
3254  lua_setfield(L, -2, "drain_percent");
3255 
3256 
3257  //if we called simulate_combat without giving an explicit weapon this can be useful.
3258  lua_pushnumber(L, bcustats.attack_num);
3259  lua_setfield(L, -2, "attack_num"); // DEPRECATED
3260  lua_pushnumber(L, bcustats.attack_num + 1);
3261  lua_setfield(L, -2, "number");
3262  //this is nullptr when there is no counter weapon
3263  if(bcustats.weapon != nullptr)
3264  {
3265  lua_pushstring(L, bcustats.weapon->id().c_str());
3266  lua_setfield(L, -2, "name");
3267  luaW_pushweapon(L, bcustats.weapon);
3268  lua_setfield(L, -2, "weapon");
3269  }
3270 
3271 }
3272 
3273 /**
3274  * Simulates a combat between two units.
3275  * - Arg 1: attacker userdata.
3276  * - Arg 2: optional weapon index.
3277  * - Arg 3: defender userdata.
3278  * - Arg 4: optional weapon index.
3279  *
3280  * - Ret 1: attacker results.
3281  * - Ret 2: defender results.
3282  * - Ret 3: info about the attacker weapon.
3283  * - Ret 4: info about the defender weapon.
3284  */
3286 {
3287  int arg_num = 1, att_w = -1, def_w = -1;
3288 
3289  unit_const_ptr att = luaW_checkunit(L, arg_num).shared_from_this();
3290  ++arg_num;
3291  if (lua_isnumber(L, arg_num)) {
3292  att_w = lua_tointeger(L, arg_num) - 1;
3293  if (att_w < 0 || att_w >= static_cast<int>(att->attacks().size()))
3294  return luaL_argerror(L, arg_num, "weapon index out of bounds");
3295  ++arg_num;
3296  }
3297 
3298  unit_const_ptr def = luaW_checkunit(L, arg_num).shared_from_this();
3299  ++arg_num;
3300  if (lua_isnumber(L, arg_num)) {
3301  def_w = lua_tointeger(L, arg_num) - 1;
3302  if (def_w < 0 || def_w >= static_cast<int>(def->attacks().size()))
3303  return luaL_argerror(L, arg_num, "weapon index out of bounds");
3304  ++arg_num;
3305  }
3306 
3307  battle_context context(units(), att->get_location(),
3308  def->get_location(), att_w, def_w, 0.0, nullptr, att, def);
3309 
3312  luaW_pushsimweapon(L, context.get_attacker_stats());
3313  luaW_pushsimweapon(L, context.get_defender_stats());
3314  return 4;
3315 }
3316 
3317 /**
3318  * Plays a sound, possibly repeated.
3319  * - Arg 1: string.
3320  * - Arg 2: optional integer.
3321  */
3323 {
3324  if (play_controller_.is_skipping_replay()) return 0;
3325  char const *m = luaL_checkstring(L, 1);
3326  int repeats = luaL_optinteger(L, 2, 0);
3327  sound::play_sound(m, sound::SOUND_FX, repeats);
3328  return 0;
3329 }
3330 
3331 /**
3332  * Sets an achievement as being completed.
3333  * - Arg 1: string - content_for.
3334  * - Arg 2: string - id.
3335  */
3337 {
3338  const char* content_for = luaL_checkstring(L, 1);
3339  const char* id = luaL_checkstring(L, 2);
3340 
3341  for(achievement_group& group : game_config_manager::get()->get_achievements()) {
3342  if(group.content_for_ == content_for) {
3343  for(achievement& achieve : group.achievements_) {
3344  if(achieve.id_ == id) {
3345  // already achieved
3346  if(achieve.achieved_) {
3347  return 0;
3348  }
3349  // found the achievement - mark it as completed
3350  if(!play_controller_.is_replay()) {
3351  prefs::get().set_achievement(content_for, id);
3352  }
3353  achieve.achieved_ = true;
3354  // progressable achievements can also check for current progress equals -1
3355  if(achieve.max_progress_ != 0) {
3356  achieve.current_progress_ = -1;
3357  }
3358  if(achieve.sound_path_ != "") {
3360  }
3361  // show the achievement popup
3362  luaW_getglobal(L, "gui", "show_popup");
3363  luaW_pushtstring(L, achieve.name_completed_);
3365  lua_pushstring(L, achieve.icon_completed_.c_str());
3366  luaW_pcall(L, 3, 0, 0);
3367  return 0;
3368  }
3369  }
3370  // achievement not found - existing achievement group but non-existing achievement id
3371  ERR_LUA << "Achievement " << id << " not found for achievement group " << content_for;
3372  return 0;
3373  }
3374  }
3375 
3376  // achievement group not found
3377  ERR_LUA << "Achievement group " << content_for << " not found";
3378  return 0;
3379 }
3380 
3381 /**
3382  * Returns whether an achievement has been completed.
3383  * - Arg 1: string - content_for.
3384  * - Arg 2: string - id.
3385  * - Ret 1: boolean.
3386  */
3388 {
3389  const char* content_for = luaL_checkstring(L, 1);
3390  const char* id = luaL_checkstring(L, 2);
3391 
3392  if(resources::controller->is_networked_mp() && synced_context::is_synced()) {
3393  ERR_LUA << "Returning false for whether a player has completed an achievement due to being networked multiplayer.";
3394  lua_pushboolean(L, false);
3395  } else {
3396  lua_pushboolean(L, prefs::get().achievement(content_for, id));
3397  }
3398 
3399  return 1;
3400 }
3401 
3402 /**
3403  * Returns information on a single achievement, or no data if the achievement is not found.
3404  * - Arg 1: string - content_for.
3405  * - Arg 2: string - id.
3406  * - Ret 1: WML table returned by the function.
3407  */
3409 {
3410  const char* content_for = luaL_checkstring(L, 1);
3411  const char* id = luaL_checkstring(L, 2);
3412 
3413  config cfg;
3414  for(const auto& group : game_config_manager::get()->get_achievements()) {
3415  if(group.content_for_ == content_for) {
3416  for(const auto& achieve : group.achievements_) {
3417  if(achieve.id_ == id) {
3418  // found the achievement - return it as a config
3419  cfg["id"] = achieve.id_;
3420  cfg["name"] = achieve.name_;
3421  cfg["name_completed"] = achieve.name_completed_;
3422  cfg["description"] = achieve.description_;
3423  cfg["description_completed"] = achieve.description_completed_;
3424  cfg["icon"] = achieve.icon_;
3425  cfg["icon_completed"] = achieve.icon_completed_;
3426  cfg["hidden"] = achieve.hidden_;
3427  cfg["achieved"] = achieve.achieved_;
3428  cfg["max_progress"] = achieve.max_progress_;
3429  cfg["current_progress"] = achieve.current_progress_;
3430 
3431  for(const auto& sub_ach : achieve.sub_achievements_) {
3432  config& sub = cfg.add_child("sub_achievement");
3433  sub["id"] = sub_ach.id_;
3434  sub["description"] = sub_ach.description_;
3435  sub["icon"] = sub_ach.icon_;
3436  sub["achieved"] = sub_ach.achieved_;
3437  }
3438 
3439  luaW_pushconfig(L, cfg);
3440  return 1;
3441  }
3442  }
3443  // return empty config - existing achievement group but non-existing achievement id
3444  ERR_LUA << "Achievement " << id << " not found for achievement group " << content_for;
3445  luaW_pushconfig(L, cfg);
3446  return 1;
3447  }
3448  }
3449  // return empty config - non-existing achievement group
3450  ERR_LUA << "Achievement group " << content_for << " not found";
3451  luaW_pushconfig(L, cfg);
3452  return 1;
3453 }
3454 
3455 /**
3456  * Progresses the provided achievement.
3457  * - Arg 1: string - content_for.
3458  * - Arg 2: string - achievement id.
3459  * - Arg 3: int - the amount to progress the achievement.
3460  * - Arg 4: int - the limit the achievement can progress by
3461  * - Ret 1: int - the achievement's current progress after adding amount or -1 if not a progressable achievement (including if it's already achieved)
3462  * - Ret 2: int - the achievement's max progress or -1 if not a progressable achievement
3463  */
3465 {
3466  const char* content_for = luaL_checkstring(L, 1);
3467  const char* id = luaL_checkstring(L, 2);
3468  int amount = luaL_checkinteger(L, 3);
3469  int limit = luaL_optinteger(L, 4, 999999999);
3470 
3471  for(achievement_group& group : game_config_manager::get()->get_achievements()) {
3472  if(group.content_for_ == content_for) {
3473  for(achievement& achieve : group.achievements_) {
3474  if(achieve.id_ == id) {
3475  // check that this is a progressable achievement
3476  if(achieve.max_progress_ == 0 || achieve.sub_achievements_.size() > 0) {
3477  ERR_LUA << "Attempted to progress achievement " << id << " for achievement group " << content_for << ", is not a progressible achievement.";
3478  lua_pushinteger(L, -1);
3479  lua_pushinteger(L, -1);
3480  return 2;
3481  }
3482 
3483  if(!achieve.achieved_) {
3484  int progress = 0;
3485  if(!play_controller_.is_replay()) {
3486  progress = prefs::get().progress_achievement(content_for, id, limit, achieve.max_progress_, amount);
3487  }
3488  if(progress >= achieve.max_progress_) {
3490  achieve.current_progress_ = -1;
3491  } else {
3492  achieve.current_progress_ = progress;
3493  }
3494  lua_pushinteger(L, progress);
3495  } else {
3496  lua_pushinteger(L, -1);
3497  }
3498  lua_pushinteger(L, achieve.max_progress_);
3499 
3500  return 2;
3501  }
3502  }
3503  // achievement not found - existing achievement group but non-existing achievement id
3504  lua_push(L, "Achievement " + std::string(id) + " not found for achievement group " + content_for);
3505  return lua_error(L);
3506  }
3507  }
3508 
3509  // achievement group not found
3510  lua_push(L, "Achievement group " + std::string(content_for) + " not found");
3511  return lua_error(L);
3512 }
3513 
3514 /**
3515  * Returns whether an achievement has been completed.
3516  * - Arg 1: string - content_for.
3517  * - Arg 2: string - achievement id.
3518  * - Arg 3: string - sub-achievement id
3519  * - Ret 1: boolean.
3520  */
3522 {
3523  const char* content_for = luaL_checkstring(L, 1);
3524  const char* id = luaL_checkstring(L, 2);
3525  const char* sub_id = luaL_checkstring(L, 3);
3526 
3527  if(resources::controller->is_networked_mp() && synced_context::is_synced()) {
3528  ERR_LUA << "Returning false for whether a player has completed an achievement due to being networked multiplayer.";
3529  lua_pushboolean(L, false);
3530  } else {
3531  lua_pushboolean(L, prefs::get().sub_achievement(content_for, id, sub_id));
3532  }
3533 
3534  return 1;
3535 }
3536 
3537 /**
3538  * Marks a single sub-achievement as completed.
3539  * - Arg 1: string - content_for.
3540  * - Arg 2: string - achievement id.
3541  * - Arg 3: string - sub-achievement id
3542  */
3544 {
3545  const char* content_for = luaL_checkstring(L, 1);
3546  const char* id = luaL_checkstring(L, 2);
3547  const char* sub_id = luaL_checkstring(L, 3);
3548 
3549  for(achievement_group& group : game_config_manager::get()->get_achievements()) {
3550  if(group.content_for_ == content_for) {
3551  for(achievement& achieve : group.achievements_) {
3552  if(achieve.id_ == id) {
3553  // the whole achievement is already completed
3554  if(achieve.achieved_) {
3555  return 0;
3556  }
3557 
3558  for(sub_achievement& sub_ach : achieve.sub_achievements_) {
3559  if(sub_ach.id_ == sub_id) {
3560  // this particular sub-achievement is already achieved
3561  if(sub_ach.achieved_) {
3562  return 0;
3563  } else {
3564  if(!play_controller_.is_replay()) {
3565  prefs::get().set_sub_achievement(content_for, id, sub_id);
3566  }
3567  sub_ach.achieved_ = true;
3568  achieve.current_progress_++;
3569  if(achieve.current_progress_ == achieve.max_progress_) {
3571  }
3572  return 0;
3573  }
3574  }
3575  }
3576  // sub-achievement not found - existing achievement group and achievement but non-existing sub-achievement id
3577  lua_push(L, "Sub-achievement " + std::string(id) + " not found for achievement" + id + " in achievement group " + content_for);
3578  return lua_error(L);
3579  }
3580  }
3581  // achievement not found - existing achievement group but non-existing achievement id
3582  lua_push(L, "Achievement " + std::string(id) + " not found for achievement group " + content_for);
3583  return lua_error(L);
3584  }
3585  }
3586 
3587  // achievement group not found
3588  lua_push(L, "Achievement group " + std::string(content_for) + " not found");
3589  return lua_error(L);
3590 }
3591 
3592 /**
3593  * Scrolls to given tile.
3594  * - Arg 1: location.
3595  * - Arg 2: boolean preventing scroll to fog.
3596  * - Arg 3: boolean specifying whether to warp instantly.
3597  * - Arg 4: boolean specifying whether to skip if already onscreen
3598  */
3600 {
3602  bool check_fogged = luaW_toboolean(L, 2);
3604  ? luaW_toboolean(L, 3)
3607  : luaW_toboolean(L, 3)
3610  ;
3611  if (game_display_) {
3612  game_display_->scroll_to_tile(loc, scroll, check_fogged);
3613  }
3614  return 0;
3615 }
3616 
3617 /**
3618  * Selects and highlights the given location on the map.
3619  * - Arg 1: location.
3620  * - Args 2,3: booleans
3621  */
3623 {
3624  events::command_disabler command_disabler;
3625  if(lua_isnoneornil(L, 1)) {
3627  return 0;
3628  }
3629  const map_location loc = luaW_checklocation(L, 1);
3630  if(!map().on_board(loc)) return luaL_argerror(L, 1, "not on board");
3631  bool highlight = true;
3632  if(!lua_isnoneornil(L, 2))
3633  highlight = luaW_toboolean(L, 2);
3634  const bool fire_event = luaW_toboolean(L, 3);
3636  loc, false, highlight, fire_event);
3637  return 0;
3638 }
3639 
3640 /**
3641  * Deselects any highlighted hex on the map.
3642  * No arguments or return values
3643  */
3645 {
3646  if(game_display_) {
3648  }
3649 
3650  return 0;
3651 }
3652 
3653 /**
3654  * Return true if a replay is in progress but the player has chosen to skip it
3655  */
3657 {
3659  if (!skipping) {
3660  skipping = game_state_.events_manager_->pump().context_skip_messages();
3661  }
3662  lua_pushboolean(L, skipping);
3663  return 1;
3664 }
3665 
3666 /**
3667  * Set whether to skip messages
3668  * Arg 1 (optional) - boolean
3669  */
3671 {
3672  bool skip = true;
3673  if (!lua_isnone(L, 1)) {
3674  skip = luaW_toboolean(L, 1);
3675  }
3676  game_state_.events_manager_->pump().context_skip_messages(skip);
3677  return 0;
3678 }
3679 
3680 namespace
3681 {
3682  struct lua_synchronize : mp_sync::user_choice
3683  {
3684  lua_State *L;
3685  int user_choice_index;
3686  int random_choice_index;
3687  int ai_choice_index;
3688  std::string desc;
3689  lua_synchronize(lua_State *l, const std::string& descr, int user_index, int random_index = 0, int ai_index = 0)
3690  : L(l)
3691  , user_choice_index(user_index)
3692  , random_choice_index(random_index)
3693  , ai_choice_index(ai_index != 0 ? ai_index : user_index)
3694  , desc(descr)
3695  {}
3696 
3697  virtual config query_user(int side) const override
3698  {
3699  bool is_local_ai = lua_kernel_base::get_lua_kernel<game_lua_kernel>(L).board().get_team(side).is_local_ai();
3700  config cfg;
3701  query_lua(side, is_local_ai ? ai_choice_index : user_choice_index, cfg);
3702  return cfg;
3703  }
3704 
3705  virtual config random_choice(int side) const override
3706  {
3707  config cfg;
3708  if(random_choice_index != 0 && lua_isfunction(L, random_choice_index)) {
3709  query_lua(side, random_choice_index, cfg);
3710  }
3711  return cfg;
3712  }
3713 
3714  virtual std::string description() const override
3715  {
3716  return desc;
3717  }
3718 
3719  void query_lua(int side, int function_index, config& cfg) const
3720  {
3721  lua_pushvalue(L, function_index);
3722  lua_pushnumber(L, side);
3723  if (luaW_pcall(L, 1, 1, false)) {
3724  if(!luaW_toconfig(L, -1, cfg)) {
3725  static const char* msg = "function returned to wesnoth.sync.[multi_]evaluate a table which was partially invalid";
3726  lua_kernel_base::get_lua_kernel<game_lua_kernel>(L).log_error(msg);
3727  lua_warning(L, msg, false);
3728  }
3729  }
3730  }
3731  //Although lua's sync_choice can show a dialog, (and will in most cases)
3732  //we return false to enable other possible things that do not contain UI things.
3733  //it's in the responsibility of the umc dev to not show dialogs during prestart events.
3734  virtual bool is_visible() const override { return false; }
3735  };
3736 }//unnamed namespace for lua_synchronize
3737 
3738 /**
3739  * Ensures a value is synchronized among all the clients.
3740  * - Arg 1: optional string specifying the type id of the choice.
3741  * - Arg 2: function to compute the value, called if the client is the master.
3742  * - Arg 3: optional function, called instead of the first function if the user is not human.
3743  * - Arg 4: optional integer specifying, on which side the function should be evaluated.
3744  * - Ret 1: WML table returned by the function.
3745  */
3746 static int intf_synchronize_choice(lua_State *L)
3747 {
3748  std::string tagname = "input";
3749  t_string desc = _("input");
3750  int human_func = 0;
3751  int ai_func = 0;
3752  int side_for;
3753 
3754  int nextarg = 1;
3755  if(!lua_isfunction(L, nextarg) && luaW_totstring(L, nextarg, desc) ) {
3756  ++nextarg;
3757  }
3758  if(lua_isfunction(L, nextarg)) {
3759  human_func = nextarg++;
3760  }
3761  else {
3762  return luaL_argerror(L, nextarg, "expected a function");
3763  }
3764  if(lua_isfunction(L, nextarg)) {
3765  ai_func = nextarg++;
3766  }
3767  side_for = lua_tointeger(L, nextarg);
3768 
3769  config cfg = mp_sync::get_user_choice(tagname, lua_synchronize(L, desc, human_func, 0, ai_func), side_for);
3770  luaW_pushconfig(L, cfg);
3771  return 1;
3772 }
3773 /**
3774  * Ensures a value is synchronized among all the clients.
3775  * - Arg 1: optional string the id of this type of user input, may only contain characters a-z and '_'
3776  * - Arg 2: function to compute the value, called if the client is the master.
3777  * - Arg 3: an optional function to compute the value, if the side was null/empty controlled.
3778  * - Arg 4: an array of integers specifying, on which side the function should be evaluated.
3779  * - Ret 1: a map int -> WML tabls.
3780  */
3781 static int intf_synchronize_choices(lua_State *L)
3782 {
3783  std::string tagname = "input";
3784  t_string desc = _("input");
3785  int human_func = 0;
3786  int null_func = 0;
3787  std::vector<int> sides_for;
3788 
3789  int nextarg = 1;
3790  if(!lua_isfunction(L, nextarg) && luaW_totstring(L, nextarg, desc) ) {
3791  ++nextarg;
3792  }
3793  if(lua_isfunction(L, nextarg)) {
3794  human_func = nextarg++;
3795  }
3796  else {
3797  return luaL_argerror(L, nextarg, "expected a function");
3798  }
3799  if(lua_isfunction(L, nextarg)) {
3800  null_func = nextarg++;
3801  };
3802  sides_for = lua_check<std::vector<int>>(L, nextarg++);
3803 
3804  lua_push(L, mp_sync::get_user_choice_multiple_sides(tagname, lua_synchronize(L, desc, human_func, null_func), std::set<int>(sides_for.begin(), sides_for.end())));
3805  return 1;
3806 }
3807 
3808 
3809 /**
3810  * Calls a function in an unsynced context (this specially means that all random calls used by that function will be unsynced).
3811  * This is usually used together with an unsynced if like 'if controller != network'
3812  * - Arg 1: function that will be called during the unsynced context.
3813  */
3814 static int intf_do_unsynced(lua_State *L)
3815 {
3816  set_scontext_unsynced sync;
3817  lua_pushvalue(L, 1);
3818  luaW_pcall(L, 0, 0, false);
3819  return 0;
3820 }
3821 
3822 /**
3823  * Gets all the locations matching a given filter.
3824  * - Arg 1: WML table.
3825  * - Arg 2: Optional reference unit (teleport_unit)
3826  * - Ret 1: array of integer pairs.
3827  */
3829 {
3831 
3832  std::set<map_location> res;
3833  filter_context & fc = game_state_;
3834  const terrain_filter t_filter(filter, &fc, false);
3835  if(luaW_isunit(L, 2)) {
3836  t_filter.get_locations(res, *luaW_tounit(L, 2), true);
3837  } else {
3838  t_filter.get_locations(res, true);
3839  }
3840 
3841  luaW_push_locationset(L, res);
3842  return 1;
3843 }
3844 
3845 /**
3846  * Matches a location against the given filter.
3847  * - Arg 1: location.
3848  * - Arg 2: WML table.
3849  * - Arg 3: Optional reference unit (teleport_unit)
3850  * - Ret 1: boolean.
3851  */
3853 {
3855  vconfig filter = luaW_checkvconfig(L, 2, true);
3856 
3857  if (filter.null()) {
3858  lua_pushboolean(L, true);
3859  return 1;
3860  }
3861 
3862  filter_context & fc = game_state_;
3863  const terrain_filter t_filter(filter, &fc, false);
3864  if(luaW_isunit(L, 3)) {
3865  lua_pushboolean(L, t_filter.match(loc, *luaW_tounit(L, 3)));
3866  } else {
3867  lua_pushboolean(L, t_filter.match(loc));
3868  }
3869  return 1;
3870 }
3871 
3872 
3873 
3874 /**
3875  * Matches a side against the given filter.
3876  * - Args 1: side number.
3877  * - Arg 2: WML table.
3878  * - Ret 1: boolean.
3879  */
3881 {
3882  vconfig filter = luaW_checkvconfig(L, 2, true);
3883 
3884  if (filter.null()) {
3885  lua_pushboolean(L, true);
3886  return 1;
3887  }
3888 
3889  filter_context & fc = game_state_;
3890  side_filter s_filter(filter, &fc);
3891 
3892  if(team* t = luaW_toteam(L, 1)) {
3893  lua_pushboolean(L, s_filter.match(*t));
3894  } else {
3895  unsigned side = luaL_checkinteger(L, 1) - 1;
3896  if (side >= teams().size()) return 0;
3897  lua_pushboolean(L, s_filter.match(side + 1));
3898  }
3899  return 1;
3900 }
3901 
3903 {
3904  int team_i;
3905  if(team* t = luaW_toteam(L, 1)) {
3906  team_i = t->side();
3907  } else {
3908  team_i = luaL_checkinteger(L, 1);
3909  }
3910  std::string flag = luaL_optlstring(L, 2, "", nullptr);
3911  std::string color = luaL_optlstring(L, 3, "", nullptr);
3912 
3913  if(flag.empty() && color.empty()) {
3914  return 0;
3915  }
3916  if(team_i < 1 || static_cast<std::size_t>(team_i) > teams().size()) {
3917  return luaL_error(L, "set_side_id: side number %d out of range", team_i);
3918  }
3919  team& side = board().get_team(team_i);
3920 
3921  if(!color.empty()) {
3922  side.set_color(color);
3923  }
3924  if(!flag.empty()) {
3925  side.set_flag(flag);
3926  }
3927 
3929  return 0;
3930 }
3931 
3932 static int intf_modify_ai(lua_State *L, const char* action)
3933 {
3934  int side_num;
3935  if(team* t = luaW_toteam(L, 1)) {
3936  side_num = t->side();
3937  } else {
3938  side_num = luaL_checkinteger(L, 1);
3939  }
3940  std::string path = luaL_checkstring(L, 2);
3941  config cfg {
3942  "action", action,
3943  "path", path
3944  };
3945  if(strcmp(action, "delete") == 0) {
3947  return 0;
3948  }
3949  config component = luaW_checkconfig(L, 3);
3950  std::size_t len = std::string::npos, open_brak = path.find_last_of('[');
3951  std::size_t dot = path.find_last_of('.');
3952  if(open_brak != len) {
3953  len = open_brak - dot - 1;
3954  }
3955  cfg.add_child(path.substr(dot + 1, len), component);
3957  return 0;
3958 }
3959 
3960 static int intf_switch_ai(lua_State *L)
3961 {
3962  int side_num;
3963  if(team* t = luaW_toteam(L, 1)) {
3964  side_num = t->side();
3965  } else {
3966  side_num = luaL_checkinteger(L, 1);
3967  }
3968  if(lua_isstring(L, 2)) {
3969  std::string file = luaL_checkstring(L, 2);
3970  if(!ai::manager::get_singleton().add_ai_for_side_from_file(side_num, file)) {
3971  std::string err = formatter() << "Could not load AI for side " << side_num << " from file " << file;
3972  lua_pushlstring(L, err.c_str(), err.length());
3973  return lua_error(L);
3974  }
3975  } else {
3977  }
3978  return 0;
3979 }
3980 
3981 static int intf_append_ai(lua_State *L)
3982 {
3983  int side_num;
3984  if(team* t = luaW_toteam(L, 1)) {
3985  side_num = t->side();
3986  } else {
3987  side_num = luaL_checkinteger(L, 1);
3988  }
3989  config cfg = luaW_checkconfig(L, 2);
3990  if(!cfg.has_child("ai")) {
3991  cfg = config {"ai", cfg};
3992  }
3993  bool added_dummy_stage = false;
3994  if(!cfg.mandatory_child("ai").has_child("stage")) {
3995  added_dummy_stage = true;
3996  cfg.mandatory_child("ai").add_child("stage", config {"name", "empty"});
3997  }
3999  if(added_dummy_stage) {
4000  cfg.remove_children("stage", [](const config& stage_cfg) { return stage_cfg["name"] == "empty"; });
4001  }
4003  return 0;
4004 }
4005 
4007 {
4008  unsigned i = luaL_checkinteger(L, 1);
4009  if(i < 1 || i > teams().size()) return 0;
4010  luaW_pushteam(L, board().get_team(i));
4011  return 1;
4012 }
4013 
4014 /**
4015  * Returns a proxy table array for all sides matching the given SSF.
4016  * - Arg 1: SSF
4017  * - Ret 1: proxy table array
4018  */
4020 {
4021  LOG_LUA << "intf_get_sides called: this = " << std::hex << this << std::dec << " myname = " << my_name();
4022  std::vector<int> sides;
4023  const vconfig ssf = luaW_checkvconfig(L, 1, true);
4024  if(ssf.null()) {
4025  for(const team& t : teams()) {
4026  sides.push_back(t.side());
4027  }
4028  } else {
4029  filter_context & fc = game_state_;
4030 
4031  side_filter filter(ssf, &fc);
4032  sides = filter.get_teams();
4033  }
4034 
4035  lua_settop(L, 0);
4036  lua_createtable(L, sides.size(), 0);
4037  unsigned index = 1;
4038  for(int side : sides) {
4039  luaW_pushteam(L, board().get_team(side));
4040  lua_rawseti(L, -2, index);
4041  ++index;
4042  }
4043 
4044  return 1;
4045 }
4046 
4047 /**
4048  * Adds a modification to a unit.
4049  * - Arg 1: unit.
4050  * - Arg 2: string.
4051  * - Arg 3: WML table.
4052  * - Arg 4: (optional) Whether to add to [modifications] - default true
4053  */
4054 static int intf_add_modification(lua_State *L)
4055 {
4056  unit& u = luaW_checkunit(L, 1);
4057  char const *m = luaL_checkstring(L, 2);
4058  std::string sm = m;
4059  if (sm == "advance") { // Maintain backwards compatibility
4060  sm = "advancement";
4061  deprecated_message("\"advance\" modification type", DEP_LEVEL::PREEMPTIVE, {1, 15, 0}, "Use \"advancement\" instead.");
4062  }
4063  if (sm != "advancement" && sm != "object" && sm != "trait") {
4064  return luaL_argerror(L, 2, "unknown modification type");
4065  }
4066  bool write_to_mods = true;
4067  if (!lua_isnone(L, 4)) {
4068  write_to_mods = luaW_toboolean(L, 4);
4069  }
4070  if(sm.empty()) {
4071  write_to_mods = false;
4072  }
4073 
4074  config cfg = luaW_checkconfig(L, 3);
4075  u.add_modification(sm, cfg, !write_to_mods);
4076  return 0;
4077 }
4078 
4079 /**
4080  * Removes modifications from a unit
4081  * - Arg 1: unit
4082  * - Arg 2: table (filter as [filter_wml])
4083  * - Arg 3: type of modification (default "object")
4084  */
4085 static int intf_remove_modifications(lua_State *L)
4086 {
4087  unit& u = luaW_checkunit(L, 1);
4088  config filter = luaW_checkconfig(L, 2);
4089  std::vector<std::string> tags;
4090  if(lua_isstring(L, 3)) {
4091  tags.push_back(lua_check<std::string>(L, 3));
4092  } else if (lua_istable(L, 3)){
4093  tags = lua_check<std::vector<std::string>>(L, 3);
4094  } else {
4095  tags.push_back("object");
4096  }
4097  //TODO
4098  if(filter.attribute_count() == 1 && filter.all_children_count() == 0 && filter.attribute_range().front().first == "duration") {
4099  u.expire_modifications(filter["duration"]);
4100  } else {
4101  for(const std::string& tag : tags) {
4102  for(config& obj : u.get_modifications().child_range(tag)) {
4103  if(obj.matches(filter)) {
4104  obj["duration"] = "now";
4105  }
4106  }
4107  }
4108  u.expire_modifications("now");
4109  }
4110  return 0;
4111 }
4112 
4113 /**
4114  * Advances a unit if the unit has enough xp.
4115  * - Arg 1: unit.
4116  * - Arg 2: optional boolean whether to animate the advancement.
4117  * - Arg 3: optional boolean whether to fire advancement events.
4118  */
4119 static int intf_advance_unit(lua_State *L)
4120 {
4121  events::command_disabler command_disabler;
4122  unit& u = luaW_checkunit(L, 1, true);
4124  if(lua_isboolean(L, 2)) {
4125  par.animate(luaW_toboolean(L, 2));
4126  }
4127  if(lua_isboolean(L, 3)) {
4128  par.fire_events(luaW_toboolean(L, 3));
4129  }
4130  advance_unit_at(par);
4131  return 0;
4132 }
4133 
4134 
4135 /**
4136  * Adds a new known unit type to the help system.
4137  * - Arg 1: string.
4138  */
4139 static int intf_add_known_unit(lua_State *L)
4140 {
4141  char const *ty = luaL_checkstring(L, 1);
4142  if(!unit_types.find(ty))
4143  {
4144  std::stringstream ss;
4145  ss << "unknown unit type: '" << ty << "'";
4146  return luaL_argerror(L, 1, ss.str().c_str());
4147  }
4148  prefs::get().encountered_units().insert(ty);
4149  return 0;
4150 }
4151 
4152 /**
4153  * Adds an overlay on a tile.
4154  * - Arg 1: location.
4155  * - Arg 2: WML table.
4156  */
4158 {
4160  vconfig cfg = luaW_checkvconfig(L, 2);
4161  const vconfig &ssf = cfg.child("filter_team");
4162 
4163  std::string team_name;
4164  if (!ssf.null()) {
4165  const std::vector<int>& teams = side_filter(ssf, &game_state_).get_teams();
4166  std::vector<std::string> team_names;
4167  std::transform(teams.begin(), teams.end(), std::back_inserter(team_names),
4168  [&](int team) { return game_state_.get_disp_context().get_team(team).team_name(); });
4169  team_name = utils::join(team_names);
4170  } else {
4171  team_name = cfg["team_name"].str();
4172  }
4173 
4174  if (game_display_) {
4176  cfg["image"],
4177  cfg["halo"],
4178  team_name,
4179  cfg["name"], // Name is treated as the ID
4180  cfg["visible_in_fog"].to_bool(true),
4181  cfg["submerge"].to_double(0),
4182  cfg["z_order"].to_double(0)
4183  ));
4184  }
4185  return 0;
4186 }
4187 
4188 /**
4189  * Removes an overlay from a tile.
4190  * - Arg 1: location.
4191  * - Arg 2: optional string.
4192  */
4194 {
4196  char const *m = lua_tostring(L, 2);
4197 
4198  if (m) {
4199  if (game_display_) {
4201  }
4202  } else {
4203  if (game_display_) {
4205  }
4206  }
4207  return 0;
4208 }
4209 
4211 {
4213  const int nargs = lua_gettop(L);
4214  if(nargs < 2 || nargs > 3) {
4215  return luaL_error(L, "Wrong number of arguments to ai.log_replay() - should be 2 or 3 arguments.");
4216  }
4217  const std::string key = nargs == 2 ? luaL_checkstring(L, 1) : luaL_checkstring(L, 2);
4218  config cfg;
4219  if(nargs == 2) {
4220  recorder.add_log_data(key, luaL_checkstring(L, 2));
4221  } else if(luaW_toconfig(L, 3, cfg)) {
4222  recorder.add_log_data(luaL_checkstring(L, 1), key, cfg);
4223  } else if(!lua_isstring(L, 3)) {
4224  return luaL_argerror(L, 3, "accepts only string or config");
4225  } else {
4226  recorder.add_log_data(luaL_checkstring(L, 1), key, luaL_checkstring(L, 3));
4227  }
4228  return 0;
4229 }
4230 
4232 {
4233  lua_event_filter(game_lua_kernel& lk, int idx, const config& args) : lk(lk), args_(args)
4234  {
4235  ref_ = lk.save_wml_event(idx);
4236  }
4237  bool operator()(const game_events::queued_event& event_info) const override
4238  {
4239  bool result;
4240  return lk.run_wml_event(ref_, args_, event_info, &result) && result;
4241  }
4243  {
4245  }
4246  void serialize(config& cfg) const override {
4247  cfg.add_child("filter_lua")["code"] = "<function>";
4248  }
4249 private:
4251  int ref_;
4253 };
4254 
4255 static std::string read_event_name(lua_State* L, int idx)
4256 {
4257  if(lua_isstring(L, idx)) {
4258  return lua_tostring(L, idx);
4259  } else {
4260  return utils::join(lua_check<std::vector<std::string>>(L, idx));
4261  }
4262 }
4263 
4264 /**
4265  * Add undo actions for the current active event
4266  * Arg 1: Either a table of ActionWML or a function to call
4267  * Arg 2: (optional) If Arg 1 is a function, this is a WML table that will be passed to it
4268  */
4270 {
4271  config cfg;
4272  if(luaW_toconfig(L, 1, cfg)) {
4274  } else {
4275  luaW_toconfig(L, 2, cfg);
4277  }
4278  return 0;
4279 }
4280 
4281 /** Add a new event handler
4282  * Arg 1: Table of options.
4283  * name: Event to handle, as a string or list of strings
4284  * id: Event ID
4285  * menu_item: True if this is a menu item (an ID is required); this means removing the menu item will automatically remove this event. Default false.
4286  * first_time_only: Whether this event should fire again after the first time; default true.
4287  * priority: Number that determines execution order. Events execute in order of decreasing priority, and secondarily in order of addition.
4288  * filter: Event filters as a config with filter tags, a table of the form {filter_type = filter_contents}, or a function
4289  * filter_args: Arbitrary data that will be passed to the filter, if it is a function. Ignored if the filter is specified as WML or a table.
4290  * content: The content of the event. This is a WML table passed verbatim into the event when it fires. If no function is specified, it will be interpreted as ActionWML.
4291  * action: The function to call when the event triggers. Defaults to wesnoth.wml_actions.command.
4292  *
4293  * Lua API: wesnoth.game_events.add
4294  */
4296 {
4298  using namespace std::literals;
4299  std::string name, id = luaW_table_get_def(L, 1, "id", ""s);
4300  bool repeat = !luaW_table_get_def(L, 1, "first_time_only", true), is_menu_item = luaW_table_get_def(L, 1, "menu_item", false);
4301  double priority = luaW_table_get_def(L, 1, "priority", 0.);
4302  if(luaW_tableget(L, 1, "name")) {
4303  name = read_event_name(L, -1);
4304  } else if(is_menu_item) {
4305  if(id.empty()) {
4306  return luaL_argerror(L, 1, "non-empty id is required for a menu item");
4307  }
4308  name = "menu item " + id;
4309  }
4310  if(id.empty() && name.empty()) {
4311  return luaL_argerror(L, 1, "either a name or id is required");
4312  }
4313  auto new_handler = man.add_event_handler_from_lua(name, id, repeat, priority, is_menu_item);
4314  if(new_handler.valid()) {
4315  bool has_lua_filter = false;
4316  new_handler->set_arguments(luaW_table_get_def(L, 1, "content", config{"__empty_lua_event", true}));
4317 
4318  if(luaW_tableget(L, 1, "filter")) {
4319  int filterIdx = lua_gettop(L);
4320  config filters;
4321  if(!luaW_toconfig(L, filterIdx, filters)) {
4322  if(lua_isfunction(L, filterIdx)) {
4323  int fcnIdx = lua_absindex(L, -1);
4324  new_handler->add_filter(std::make_unique<lua_event_filter>(*this, fcnIdx, luaW_table_get_def(L, 1, "filter_args", config())));
4325  has_lua_filter = true;
4326  } else {
4327 #define READ_ONE_FILTER(key, tag) \
4328  do { \
4329  if(luaW_tableget(L, filterIdx, key)) { \
4330  if(lua_isstring(L, -1)) { \
4331  filters.add_child("insert_tag", config{ \
4332  "name", tag, \
4333  "variable", luaL_checkstring(L, -1) \
4334  }); \
4335  } else { \
4336  filters.add_child(tag, luaW_checkconfig(L, -1)); \
4337  } \
4338  } \
4339  } while(false);
4340  READ_ONE_FILTER("condition", "filter_condition");
4341  READ_ONE_FILTER("side", "filter_side");
4342  READ_ONE_FILTER("unit", "filter");
4343  READ_ONE_FILTER("attack", "filter_attack");
4344  READ_ONE_FILTER("second_unit", "filter_second");
4345  READ_ONE_FILTER("second_attack", "filter_second_attack");
4346 #undef READ_ONE_FILTER
4347  if(luaW_tableget(L, filterIdx, "formula")) {
4348  filters["filter_formula"] = luaL_checkstring(L, -1);
4349  }
4350  }
4351  }
4352  new_handler->read_filters(filters);
4353  }
4354 
4355  if(luaW_tableget(L, 1, "action")) {
4356  new_handler->set_event_ref(save_wml_event(-1), has_preloaded_);
4357  } else {
4358  if(has_lua_filter) {
4359  // This just sets the appropriate flags so the engine knows it cannot be serialized.
4360  // The register_wml_event call will override the actual event_ref so just pass LUA_NOREF here.
4361  new_handler->set_event_ref(LUA_NOREF, has_preloaded_);
4362  }
4363  new_handler->register_wml_event(*this);
4364  }
4365  }
4366  return 0;
4367 }
4368 
4369 /**
4370  * Upvalue 1: The event function
4371  * Upvalue 2: The undo function
4372  * Arg 1: The event content
4373  */
4375 {
4376  lua_pushvalue(L, lua_upvalueindex(1));
4377  lua_push(L, 1);
4378  luaW_pcall(L, 1, 0);
4379  game_state_.undo_stack_->add_custom<actions::undo_event>(lua_upvalueindex(2), config(), get_event_info());
4380  return 0;
4381 }
4382 
4383 /** Add a new event handler
4384  * Arg 1: Event to handle, as a string or list of strings; or menu item ID if this is a menu item
4385  * Arg 2: The function to call when the event triggers
4386  * Arg 3: (optional) Event priority
4387  * Arg 4: (optional, non-menu-items only) The function to call when the event is undone
4388  *
4389  * Lua API:
4390  * - wesnoth.game_events.add_repeating
4391  * - wesnoth.game_events.add_menu
4392  */
4393 template<bool is_menu_item>
4395 {
4397  bool repeat = true;
4398  std::string name = read_event_name(L, 1), id;
4399  double priority = luaL_optnumber(L, 3, 0.);
4400  if(name.empty()) {
4401  return luaL_argerror(L, 1, "must not be empty");
4402  }
4403  if(is_menu_item) {
4404  id = name;
4405  name = "menu item " + name;
4406  } else if(lua_absindex(L, -1) > 2 && lua_isfunction(L, -1)) {
4407  // If undo is provided as a separate function, link them together into a single function
4408  // The function can be either the 3rd or 4th argument.
4409  lua_pushcclosure(L, &dispatch<&game_lua_kernel::cfun_undoable_event>, 2);
4410  }
4411  auto new_handler = man.add_event_handler_from_lua(name, id, repeat, priority, is_menu_item);
4412  if(new_handler.valid()) {
4413  // An event with empty arguments is not added, so set some dummy arguments
4414  new_handler->set_arguments(config{"__quick_lua_event", true});
4415  new_handler->set_event_ref(save_wml_event(2), has_preloaded_);
4416  }
4417  return 0;
4418 }
4419 
4420 /** Add a new event handler
4421  * Arg: A full event specification as a WML config
4422  *
4423  * WML API: [event]
4424  */
4426 {
4428  vconfig cfg(luaW_checkvconfig(L, 1));
4429  bool delayed_variable_substitution = cfg["delayed_variable_substitution"].to_bool(true);
4430  if(delayed_variable_substitution) {
4431  man.add_event_handler_from_wml(cfg.get_config(), *this);
4432  } else {
4434  }
4435  return 0;
4436 }
4437 
4439 {
4440  game_state_.events_manager_->remove_event_handler(luaL_checkstring(L, 1));
4441  return 0;
4442 }
4443 
4445 {
4446  if (game_display_) {
4447  game_display_->adjust_color_overlay(luaL_checkinteger(L, 1), luaL_checkinteger(L, 2), luaL_checkinteger(L, 3));
4449  }
4450  return 0;
4451 }
4452 
4454 {
4455  if(game_display_) {
4456  auto color = game_display_->get_color_overlay();
4457  lua_pushinteger(L, color.r);
4458  lua_pushinteger(L, color.g);
4459  lua_pushinteger(L, color.b);
4460  return 3;
4461  }
4462  return 0;
4463 }
4464 
4466 {
4467  if(game_display_) {
4468  auto vec = lua_check<std::vector<uint8_t>>(L, 1);
4469  if(vec.size() != 4) {
4470  return luaW_type_error(L, 1, "array of 4 integers");
4471  }
4472  color_t fade{vec[0], vec[1], vec[2], vec[3]};
4473  game_display_->fade_to(fade, std::chrono::milliseconds{luaL_checkinteger(L, 2)});
4474  }
4475  return 0;
4476 }
4477 
4478 /**
4479  * Delays engine for a while.
4480  * - Arg 1: integer.
4481  * - Arg 2: boolean (optional).
4482  */
4484 {
4485  if(gamedata().phase() == game_data::PRELOAD || gamedata().phase() == game_data::PRESTART || gamedata().phase() == game_data::INITIAL) {
4486  //don't call play_slice if the game ui is not active yet.
4487  return 0;
4488  }
4489  events::command_disabler command_disabler;
4490  using namespace std::chrono_literals;
4491  std::chrono::milliseconds delay{luaL_checkinteger(L, 1)};
4492  if(delay == 0ms) {
4494  return 0;
4495  }
4496  if(luaW_toboolean(L, 2) && game_display_ && game_display_->turbo_speed() > 0) {
4497  delay /= game_display_->turbo_speed();
4498  }
4499  const auto end_time = std::chrono::steady_clock::now() + delay;
4500  do {
4502  std::this_thread::sleep_for(10ms);
4503  } while (std::chrono::steady_clock::now() < end_time);
4504  return 0;
4505 }
4506 
4508 {
4509  // TODO: Support color = {r = 0, g = 0, b = 0}
4510  if (game_display_) {
4511  vconfig cfg(luaW_checkvconfig(L, 1));
4512 
4513  game_display &screen = *game_display_;
4514 
4515  terrain_label label(screen.labels(), cfg.get_config());
4516 
4517  screen.labels().set_label(label.location(), label.text(), label.creator(), label.team_name(), label.color(),
4518  label.visible_in_fog(), label.visible_in_shroud(), label.immutable(), label.category(), label.tooltip());
4519  }
4520  return 0;
4521 }
4522 
4524 {
4525  if (game_display_) {
4527  std::string team_name;
4528 
4529  // If there's only one parameter and it's a table, check if it contains team_name
4530  if(lua_gettop(L) == 1 && lua_istable(L, 1)) {
4531  using namespace std::literals;
4532  team_name = luaW_table_get_def(L, 1, "team_name", ""sv);
4533  } else {
4534  team_name = luaL_optstring(L, 2, "");
4535  }
4536 
4537  game_display_->labels().set_label(loc, "", -1, team_name);
4538  }
4539  return 0;
4540 }
4541 
4543 {
4544  if(game_display_) {
4545  game_display &screen = *game_display_;
4546  auto loc = luaW_checklocation(L, 1);
4547  const terrain_label* label = nullptr;
4548  switch(lua_type(L, 2)) {
4549  // Missing 2nd argument - get global label
4550  case LUA_TNONE: case LUA_TNIL:
4551  label = screen.labels().get_label(loc, "");
4552  break;
4553  // Side number - get label belonging to that side's team
4554  case LUA_TNUMBER:
4555  if(size_t n = luaL_checkinteger(L, 2); n > 0 && n <= teams().size()) {
4556  label = screen.labels().get_label(loc, teams().at(n - 1).team_name());
4557  }
4558  break;
4559  // String - get label belonging to the team with that name
4560  case LUA_TSTRING:
4561  label = screen.labels().get_label(loc, luaL_checkstring(L, 2));
4562  break;
4563  // Side userdata - get label belonging to that side's team
4564  case LUA_TUSERDATA:
4565  label = screen.labels().get_label(loc, luaW_checkteam(L, 2).team_name());
4566  break;
4567  }
4568  if(label) {
4569  config cfg;
4570  label->write(cfg);
4571  luaW_pushconfig(L, cfg);
4572  return 1;
4573  }
4574  }
4575  return 0;
4576 }
4577 
4579 {
4580  if (game_display_) {
4581  game_display & screen = *game_display_;
4582 
4583  vconfig cfg(luaW_checkvconfig(L, 1));
4584  bool clear_shroud(luaW_toboolean(L, 2));
4585 
4586  // We do this twice so any applicable redraws happen both before and after
4587  // any events caused by redrawing shroud are fired
4588  bool result = screen.maybe_rebuild();
4589  if (!result) {
4590  screen.invalidate_all();
4591  }
4592 
4593  if (clear_shroud) {
4595  for (const int side : filter.get_teams()){
4596  actions::clear_shroud(side);
4597  }
4598  screen.recalculate_minimap();
4599  }
4600 
4601  result = screen.maybe_rebuild();
4602  if (!result) {
4603  screen.invalidate_all();
4604  }
4605  }
4606  return 0;
4607 }
4608 
4609 /**
4610  * Lua frontend to the modify_ai functionality
4611  * - Arg 1: config.
4612  */
4613 static int intf_modify_ai_old(lua_State *L)
4614 {
4615  config cfg;
4616  luaW_toconfig(L, 1, cfg);
4617  int side = cfg["side"].to_int();
4619  return 0;
4620 }
4621 
4622 static int cfun_exec_candidate_action(lua_State *L)
4623 {
4624  bool exec = luaW_toboolean(L, -1);
4625  lua_pop(L, 1);
4626 
4627  lua_getfield(L, -1, "ca_ptr");
4628 
4629  ai::candidate_action *ca = static_cast<ai::candidate_action*>(lua_touserdata(L, -1));
4630  lua_pop(L, 2);
4631  if (exec) {
4632  ca->execute();
4633  return 0;
4634  }
4635  lua_pushnumber(L, ca->evaluate());
4636  return 1;
4637 }
4638 
4639 static int cfun_exec_stage(lua_State *L)
4640 {
4641  lua_getfield(L, -1, "stg_ptr");
4642  ai::stage *stg = static_cast<ai::stage*>(lua_touserdata(L, -1));
4643  lua_pop(L, 2);
4644  stg->play_stage();
4645  return 0;
4646 }
4647 
4648 static void push_component(lua_State *L, ai::component* c, const std::string &ct = "")
4649 {
4650  lua_createtable(L, 0, 0); // Table for a component
4651 
4652  lua_pushstring(L, "name");
4653  lua_pushstring(L, c->get_name().c_str());
4654  lua_rawset(L, -3);
4655 
4656  lua_pushstring(L, "engine");
4657  lua_pushstring(L, c->get_engine().c_str());
4658  lua_rawset(L, -3);
4659 
4660  lua_pushstring(L, "id");
4661  lua_pushstring(L, c->get_id().c_str());
4662  lua_rawset(L, -3);
4663 
4664  if (ct == "candidate_action") {
4665  lua_pushstring(L, "ca_ptr");
4666  lua_pushlightuserdata(L, c);
4667  lua_rawset(L, -3);
4668 
4669  lua_pushstring(L, "exec");
4670  lua_pushcclosure(L, &cfun_exec_candidate_action, 0);
4671  lua_rawset(L, -3);
4672  }
4673 
4674  if (ct == "stage") {
4675  lua_pushstring(L, "stg_ptr");
4676  lua_pushlightuserdata(L, c);
4677  lua_rawset(L, -3);
4678 
4679  lua_pushstring(L, "exec");
4680  lua_pushcclosure(L, &cfun_exec_stage, 0);
4681  lua_rawset(L, -3);
4682  }
4683 
4684 
4685  std::vector<std::string> c_types = c->get_children_types();
4686 
4687  for (std::vector<std::string>::const_iterator t = c_types.begin(); t != c_types.end(); ++t)
4688  {
4689  std::vector<ai::component*> children = c->get_children(*t);
4690  std::string type = *t;
4691  if (type == "aspect" || type == "goal" || type == "engine")
4692  {
4693  continue;
4694  }
4695 
4696  lua_pushstring(L, type.c_str());
4697  lua_createtable(L, 0, 0); // this table will be on top of the stack during recursive calls
4698 
4699  for (std::vector<ai::component*>::const_iterator i = children.begin(); i != children.end(); ++i)
4700  {
4701  lua_pushstring(L, (*i)->get_name().c_str());
4702  push_component(L, *i, type);
4703  lua_rawset(L, -3);
4704 
4705  //if (type == "candidate_action")
4706  //{
4707  // ai::candidate_action *ca = dynamic_cast<ai::candidate_action*>(*i);
4708  // ca->execute();
4709  //}
4710  }
4711 
4712  lua_rawset(L, -3); // setting the child table
4713  }
4714 
4715 
4716 }
4717 
4718 /**
4719  * Debug access to the ai tables
4720  * - Arg 1: int
4721  * - Ret 1: ai table
4722  */
4723 static int intf_debug_ai(lua_State *L)
4724 {
4725  if (!game_config::debug) { // This function works in debug mode only
4726  return 0;
4727  }
4728  int side;
4729  if(team* t = luaW_toteam(L, 1)) {
4730  side = t->side();
4731  } else {
4732  side = luaL_checkinteger(L, 1);
4733  }
4734  lua_pop(L, 1);
4735 
4737 
4738  // Bad, but works
4739  std::vector<ai::component*> engines = c->get_children("engine");
4740  ai::engine_lua* lua_engine = nullptr;
4741  for (std::vector<ai::component*>::const_iterator i = engines.begin(); i != engines.end(); ++i)
4742  {
4743  if ((*i)->get_name() == "lua")
4744  {
4745  lua_engine = dynamic_cast<ai::engine_lua *>(*i);
4746  }
4747  }
4748 
4749  // Better way, but doesn't work
4750  //ai::component* e = ai::manager::get_singleton().get_active_ai_holder_for_side_dbg(side).get_component(c, "engine[lua]");
4751  //ai::engine_lua* lua_engine = dynamic_cast<ai::engine_lua *>(e);
4752 
4753  if (lua_engine == nullptr)
4754  {
4755  //no lua engine is defined for this side.
4756  //so set up a dummy engine
4757 
4758  ai::ai_composite * ai_ptr = dynamic_cast<ai::ai_composite *>(c);
4759 
4760  assert(ai_ptr);
4761 
4762  ai::ai_context& ai_context = ai_ptr->get_ai_context();
4764 
4765  lua_engine = new ai::engine_lua(ai_context, cfg);
4766  LOG_LUA << "Created new dummy lua-engine for debug_ai().";
4767 
4768  //and add the dummy engine as a component
4769  //to the manager, so we could use it later
4770  cfg.add_child("engine", lua_engine->to_config());
4771  ai::component_manager::add_component(c, "engine[]", cfg);
4772  }
4773 
4774  lua_engine->push_ai_table(); // stack: [-1: ai_context]
4775 
4776  lua_pushstring(L, "components");
4777  push_component(L, c); // stack: [-1: component tree; -2: ai context]
4778  lua_rawset(L, -3);
4779 
4780  return 1;
4781 }
4782 
4783 /** Allow undo sets the flag saying whether the event has mutated the game to false. */
4785 {
4786  bool allow;
4787  t_string reason;
4788  // The extra iststring is required to prevent totstring from converting a bool value
4789  if(luaW_iststring(L, 1) && luaW_totstring(L, 1, reason)) {
4790  allow = false;
4791  } else {
4792  allow = luaW_toboolean(L, 1);
4793  luaW_totstring(L, 2, reason);
4794  }
4795  gamedata().set_allow_end_turn(allow, reason);
4796  return 0;
4797 }
4798 
4799 /** Allow undo sets the flag saying whether the event has mutated the game to false. */
4801 {
4802  if(lua_isboolean(L, 1)) {
4804  }
4805  else {
4807  }
4808  return 0;
4809 }
4810 
4812 {
4814  return 0;
4815 }
4816 
4817 /** Adding new time_areas dynamically with Standard Location Filters.
4818  * Arg 1: Area ID
4819  * Arg 2: Area locations (either a filter or a list of locations)
4820  * Arg 3: (optional) Area schedule - WML table with [time] tags and optional current_time=
4821  */
4823 {
4824  log_scope("time_area");
4825 
4826  std::string id;
4827  std::set<map_location> locs;
4828  config times;
4829 
4830  if(lua_gettop(L) == 1) {
4831  vconfig cfg = luaW_checkvconfig(L, 1);
4832  deprecated_message("Single-argument wesnoth.map.place_area is deprecated. Instead, pass ID, filter, and schedule as three separate arguments.", DEP_LEVEL::INDEFINITE, {1, 17, 0});
4833  id = cfg["id"].str();
4834  const terrain_filter filter(cfg, &game_state_, false);
4835  filter.get_locations(locs, true);
4836  times = cfg.get_parsed_config();
4837  } else {
4838  id = luaL_checkstring(L, 1);
4839  if(!lua_isnoneornil(L, 3))
4840  times = luaW_checkconfig(L, 3);
4841  vconfig cfg{config()};
4842  if(luaW_tovconfig(L, 2, cfg)) {
4843  // Second argument is a location filter
4844  const terrain_filter filter(cfg, &game_state_, false);
4845  filter.get_locations(locs, true);
4846  } else {
4847  // Second argument is an array of locations
4848  luaW_check_locationset(L, 2);
4849  }
4850  }
4851 
4852  tod_man().add_time_area(id, locs, times);
4853  LOG_LUA << "Lua inserted time_area '" << id << "'";
4854  return 0;
4855 }
4856 
4857 /** Removing new time_areas dynamically with Standard Location Filters. */
4859 {
4860  log_scope("remove_time_area");
4861 
4862  const char * id = luaL_checkstring(L, 1);
4863  tod_man().remove_time_area(id);
4864  LOG_LUA << "Lua removed time_area '" << id << "'";
4865 
4866  return 0;
4867 }
4868 
4870 {
4871  map_location loc;
4872  if(luaW_tolocation(L, 1, loc)) {
4873  int area_index = tod_man().get_area_on_hex(loc).first;
4874  if(area_index < 0) {
4875  lua_pushnil(L);
4876  return 1;
4877  }
4878  luaW_push_schedule(L, area_index);
4879  return 1;
4880  } else {
4881  std::string area_id = luaL_checkstring(L, 1);
4882  const auto& area_ids = tod_man().get_area_ids();
4883  if(auto iter = std::find(area_ids.begin(), area_ids.end(), area_id); iter == area_ids.end()) {
4884  lua_pushnil(L);
4885  return 1;
4886  } else {
4887  luaW_push_schedule(L, std::distance(area_ids.begin(), iter));
4888  return 1;
4889  }
4890  }
4891 }
4892 
4893 /** Replacing the current time of day schedule. */
4895 {
4896  map_location loc;
4897  if(luaL_testudata(L, 1, "schedule")) {
4898  // Replace the global schedule with a time area's schedule
4899  // Replacing the global schedule with the global schedule
4900  // is also supported but obviously a no-op
4901  int area = luaW_check_schedule(L, 1);
4902  if(area >= 0) tod_man().replace_schedule(tod_man().times(area));
4903  } else {
4904  vconfig cfg = luaW_checkvconfig(L, 1);
4905 
4906  if(cfg.get_children("time").empty()) {
4907  ERR_LUA << "attempted to to replace ToD schedule with empty schedule";
4908  } else {
4910  if (game_display_) {
4912  }
4913  LOG_LUA << "replaced ToD schedule";
4914  }
4915  }
4916  return 0;
4917 }
4918 
4920 {
4921  if (game_display_) {
4922  point scroll_to(luaL_checkinteger(L, 1), luaL_checkinteger(L, 2));
4923  game_display_->scroll(scroll_to, true);
4924 
4925  lua_remove(L, 1);
4926  lua_remove(L, 1);
4927  lua_push(L, 25);
4928  intf_delay(L);
4929  }
4930 
4931  return 0;
4932 }
4933 
4934 namespace {
4935  struct lua_report_generator : reports::generator
4936  {
4937  lua_State *mState;
4938  std::string name;
4939  lua_report_generator(lua_State *L, const std::string &n)
4940  : mState(L), name(n) {}
4941  virtual config generate(const reports::context & rc);
4942  };
4943 
4944  config lua_report_generator::generate(const reports::context & /*rc*/)
4945  {
4946  lua_State *L = mState;
4947  config cfg;
4948  if (!luaW_getglobal(L, "wesnoth", "interface", "game_display", name))
4949  return cfg;
4950  if (!luaW_pcall(L, 0, 1)) return cfg;
4951  luaW_toconfig(L, -1, cfg);
4952  lua_pop(L, 1);
4953  return cfg;
4954  }
4955 }//unnamed namespace for lua_report_generator
4956 
4957 /**
4958  * Executes its upvalue as a theme item generator.
4959  */
4960 int game_lua_kernel::impl_theme_item(lua_State *L, const std::string& m)
4961 {
4963  luaW_pushconfig(L, reports_.generate_report(m.c_str(), temp_context , true));
4964  return 1;
4965 }
4966 
4967 /**
4968  * Creates a field of the theme_items table and returns it (__index metamethod).
4969  */
4971 {
4972  char const *m = luaL_checkstring(L, 2);
4973  lua_cpp::push_closure(L, std::bind(&game_lua_kernel::impl_theme_item, this, std::placeholders::_1, std::string(m)), 0);
4974  lua_pushvalue(L, 2);
4975  lua_pushvalue(L, -2);
4976  lua_rawset(L, 1);
4977  reports_.register_generator(m, new lua_report_generator(L, m));
4978  return 1;
4979 }
4980 
4981 /**
4982  * Sets a field of the theme_items table (__newindex metamethod).
4983  */
4985 {
4986  char const *m = luaL_checkstring(L, 2);
4987  lua_pushvalue(L, 2);
4988  lua_pushvalue(L, 3);
4989  lua_rawset(L, 1);
4990  reports_.register_generator(m, new lua_report_generator(L, m));
4991  return 0;
4992 }
4993 
4994 /**
4995  * Get all available theme_items (__dir metamethod).
4996  */
4998 {
5000  return 1;
5001 }
5002 
5003 /**
5004  * Gets all the WML variables currently set.
5005  * - Ret 1: WML table
5006  */
5008  luaW_pushconfig(L, gamedata().get_variables());
5009  return 1;
5010 }
5011 
5012 /**
5013  * Teeleports a unit to a location.
5014  * Arg 1: unit
5015  * Arg 2: target location
5016  * Arg 3: bool (ignore_passability)
5017  * Arg 4: bool (clear_shroud)
5018  * Arg 5: bool (animate)
5019  */
5021 {
5022  events::command_disabler command_disabler;
5023  unit_ptr u = luaW_checkunit_ptr(L, 1, true);
5025  bool check_passability = !luaW_toboolean(L, 3);
5026  bool clear_shroud = luaW_toboolean(L, 4);
5027  bool animate = luaW_toboolean(L, 5);
5028 
5029  if (dst == u->get_location() || !map().on_board(dst)) {
5030  return 0;
5031  }
5032  const map_location vacant_dst = find_vacant_tile(dst, pathfind::VACANT_ANY, check_passability ? u.get() : nullptr);
5033  if (!map().on_board(vacant_dst)) {
5034  return 0;
5035  }
5036  // Clear the destination hex before the move (so the animation can be seen).
5037  actions::shroud_clearer clearer;
5038  if ( clear_shroud ) {
5039  clearer.clear_dest(vacant_dst, *u);
5040  }
5041 
5042  map_location src_loc = u->get_location();
5043 
5044  std::vector<map_location> teleport_path;
5045  teleport_path.push_back(src_loc);
5046  teleport_path.push_back(vacant_dst);
5047  unit_display::move_unit(teleport_path, u, animate);
5048 
5049  units().move(src_loc, vacant_dst);
5051 
5052  u = units().find(vacant_dst).get_shared_ptr();
5053  u->anim_comp().set_standing();
5054 
5055  if ( clear_shroud ) {
5056  // Now that the unit is visibly in position, clear the shroud.
5057  clearer.clear_unit(vacant_dst, *u);
5058  }
5059 
5060  if (map().is_village(vacant_dst)) {
5061  actions::get_village(vacant_dst, u->side());
5062  }
5063 
5064  game_display_->invalidate_unit_after_move(src_loc, vacant_dst);
5065 
5066  // Sighted events.
5067  clearer.fire_events();
5068  return 0;
5069 }
5070 
5071 /**
5072  * Logs a message
5073  * Arg 1: (optional) Logger; "wml" for WML errors or deprecations
5074  * Arg 2: Message
5075  * Arg 3: Whether to print to chat (always true if arg 1 is "wml")
5076  */
5077 int game_lua_kernel::intf_log(lua_State *L)
5078 {
5079  const std::string& logger = lua_isstring(L, 2) ? luaL_checkstring(L, 1) : "";
5080  const std::string& msg = lua_isstring(L, 2) ? luaL_checkstring(L, 2) : luaL_checkstring(L, 1);
5081 
5082  if(logger == "wml" || logger == "WML") {
5083  lg::log_to_chat() << msg << '\n';
5084  ERR_WML << msg;
5085  } else {
5086  bool in_chat = luaW_toboolean(L, -1);
5087  game_state_.events_manager_->pump().put_wml_message(logger,msg,in_chat);
5088  }
5089  return 0;
5090 }
5091 
5092 int game_lua_kernel::intf_get_fog_or_shroud(lua_State *L, bool fog)
5093 {
5094  team& t = luaW_checkteam(L, 1, board());
5096  lua_pushboolean(L, fog ? t.fogged(loc) : t.shrouded(loc));
5097  return 1;
5098 }
5099 
5100 /**
5101  * Implements the lifting and resetting of fog via WML.
5102  * Keeping affect_normal_fog as false causes only the fog override to be affected.
5103  * Otherwise, fog lifting will be implemented similar to normal sight (cannot be
5104  * individually reset and ends at the end of the turn), and fog resetting will, in
5105  * addition to removing overrides, extend the specified teams' normal fog to all
5106  * hexes.
5107  *
5108  * Arg 1: (optional) Side number, or list of side numbers
5109  * Arg 2: List of locations; each is a two-element array or a table with x and y keys
5110  * Arg 3: (optional) boolean
5111  */
5112 int game_lua_kernel::intf_toggle_fog(lua_State *L, const bool clear)
5113 {
5114  bool affect_normal_fog = false;
5115  if(lua_isboolean(L, -1)) {
5116  affect_normal_fog = luaW_toboolean(L, -1);
5117  }
5118  std::set<int> sides;
5119  if(team* t = luaW_toteam(L, 1)) {
5120  sides.insert(t->side());
5121  } else if(lua_isnumber(L, 1)) {
5122  sides.insert(lua_tointeger(L, 1));
5123  } else if(lua_istable(L, 1) && lua_istable(L, 2)) {
5124  const auto& v = lua_check<std::vector<int>>(L, 1);
5125  sides.insert(v.begin(), v.end());
5126  } else {
5127  for(const team& t : teams()) {
5128  sides.insert(t.side()+1);
5129  }
5130  }
5131  const auto& locs = luaW_check_locationset(L, lua_istable(L, 2) ? 2 : 1);
5132 
5133  for(const int &side_num : sides) {
5134  if(side_num < 1 || static_cast<std::size_t>(side_num) > teams().size()) {
5135  continue;
5136  }
5137  team &t = board().get_team(side_num);
5138  if(!clear) {
5139  // Extend fog.
5140  t.remove_fog_override(locs);
5141  if(affect_normal_fog) {
5142  t.refog();
5143  }
5144  } else if(!affect_normal_fog) {
5145  // Force the locations clear of fog.
5146  t.add_fog_override(locs);
5147  } else {
5148  // Simply clear fog from the locations.
5149  for(const map_location &hex : locs) {
5150  t.clear_fog(hex);
5151  }
5152  }
5153  }
5154 
5155  // Flag a screen update.
5158  return 0;
5159 }
5160 
5161 // Invokes a synced command
5162 static int intf_invoke_synced_command(lua_State* L)
5163 {
5164  const std::string name = luaL_checkstring(L, 1);
5165  auto it = synced_command::registry().find(name);
5166  config cmd;
5167  if(it == synced_command::registry().end()) {
5168  // Custom command
5169  if(!luaW_getglobal(L, "wesnoth", "custom_synced_commands", name)) {
5170  return luaL_argerror(L, 1, "Unknown synced command");
5171  }
5172  config& cmd_tag = cmd.child_or_add("custom_command");
5173  cmd_tag["name"] = name;
5174  if(!lua_isnoneornil(L, 2)) {
5175  cmd_tag.add_child("data", luaW_checkconfig(L, 2));
5176  }
5177  } else {
5178  // Built-in command
5179  cmd.add_child(name, luaW_checkconfig(L, 2));
5180  }
5181  // Now just forward to the WML action.
5182  luaW_getglobal(L, "wesnoth", "wml_actions", "do_command");
5183  luaW_pushconfig(L, cmd);
5184  luaW_pcall(L, 1, 0);
5185  return 0;
5186 }
5187 
5191 };
5192 #define CALLBACK_GETTER(name, type) LATTR_GETTER(name, lua_index_raw, callbacks_tag, ) { lua_pushcfunction(L, &impl_null_callback<type>); return lua_index_raw(L); }
5194 
5195 template<typename Ret>
5196 static int impl_null_callback(lua_State* L) {
5197  if constexpr(std::is_same_v<Ret, void>) return 0;
5198  else lua_push(L, Ret());
5199  return 1;
5200 };
5201 
5202 template<> struct lua_object_traits<callbacks_tag> {
5203  inline static auto metatable = "game_events";
5204  inline static game_lua_kernel& get(lua_State* L, int) {
5205  return lua_kernel_base::get_lua_kernel<game_lua_kernel>(L);
5206  }
5207 };
5208 
5209 namespace {
5210 CALLBACK_GETTER("on_event", void);
5211 CALLBACK_GETTER("on_load", void);
5212 CALLBACK_GETTER("on_save", config);
5213 CALLBACK_GETTER("on_mouse_action", void);
5214 CALLBACK_GETTER("on_mouse_button", bool);
5215 CALLBACK_GETTER("on_mouse_move", void);
5216 }
5217 
5218 static int impl_game_events_dir(lua_State* L) {
5219  return callbacksReg.dir(L);
5220 }
5221 
5222 static int impl_game_events_get(lua_State* L) {
5223  return callbacksReg.get(L);
5224 }
5225 
5226 template<typename Ret = void>
5227 static bool impl_get_callback(lua_State* L, const std::string& name) {
5228  int top = lua_gettop(L);
5229  if(!luaW_getglobal(L, "wesnoth", "game_events")) {
5230  return false;
5231  }
5232  lua_getfield(L, -1, name.c_str()); // calls impl_game_events_get
5233  lua_pushcfunction(L, &impl_null_callback<Ret>);
5234  if(lua_rawequal(L, -1, -2)) {
5235  lua_settop(L, top);
5236  return false;
5237  }
5238  lua_pop(L, 1);
5239  lua_remove(L, -2);
5240  return true;
5241 }
5242 
5243 // END CALLBACK IMPLEMENTATION
5244 
5246  return game_state_.board_;
5247 }
5248 
5250  return game_state_.board_.units();
5251 }
5252 
5253 std::vector<team> & game_lua_kernel::teams() {
5254  return game_state_.board_.teams();
5255 }
5256 
5258  return game_state_.board_.map();
5259 }
5260 
5262  return game_state_.gamedata_;
5263 }
5264 
5266  return game_state_.tod_manager_;
5267 }
5268 
5270  return *queued_events_.top();
5271 }
5272 
5273 
5275  : lua_kernel_base()
5276  , game_display_(nullptr)
5277  , game_state_(gs)
5278  , play_controller_(pc)
5279  , reports_(reports_object)
5280  , level_lua_()
5281  , EVENT_TABLE(LUA_NOREF)
5282  , queued_events_()
5283  , map_locked_(0)
5284 {
5285  static game_events::queued_event default_queued_event("_from_lua", "", map_location(), map_location(), config());
5286  queued_events_.push(&default_queued_event);
5287 
5288  lua_State *L = mState;
5289 
5290  cmd_log_ << "Registering game-specific wesnoth lib functions...\n";
5291 
5292  // Put some callback functions in the scripting environment.
5293  static luaL_Reg const callbacks[] {
5294  { "add_known_unit", &intf_add_known_unit },
5295  { "get_era", &intf_get_era },
5296  { "get_resource", &intf_get_resource },
5297  { "modify_ai", &intf_modify_ai_old },
5298  { "cancel_action", &dispatch<&game_lua_kernel::intf_cancel_action > },
5299  { "log_replay", &dispatch<&game_lua_kernel::intf_log_replay > },
5300  { "log", &dispatch<&game_lua_kernel::intf_log > },
5301  { "redraw", &dispatch<&game_lua_kernel::intf_redraw > },
5302  { "simulate_combat", &dispatch<&game_lua_kernel::intf_simulate_combat > },
5303  { nullptr, nullptr }
5304  };lua_getglobal(L, "wesnoth");
5305  if (!lua_istable(L,-1)) {
5306  lua_newtable(L);
5307  }
5308  luaL_setfuncs(L, callbacks, 0);
5309 
5310  lua_setglobal(L, "wesnoth");
5311 
5312  lua_getglobal(L, "gui");
5313  lua_pushcfunction(L, &dispatch<&game_lua_kernel::intf_gamestate_inspector>);
5314  lua_setfield(L, -2, "show_inspector");
5315  lua_pushcfunction(L, &lua_gui2::intf_show_recruit_dialog);
5316  lua_setfield(L, -2, "show_recruit_dialog");
5317  lua_pushcfunction(L, &lua_gui2::intf_show_recall_dialog);
5318  lua_setfield(L, -2, "show_recall_dialog");
5319  lua_pop(L, 1);
5320 
5322  // Create the unit_test module
5323  lua_newtable(L);
5324  static luaL_Reg const test_callbacks[] {
5325  { "fire_wml_menu_item", &dispatch<&game_lua_kernel::intf_fire_wml_menu_item> },
5326  { nullptr, nullptr }
5327  };
5328  luaL_setfuncs(L, test_callbacks, 0);
5329  lua_setglobal(L, "unit_test");
5330  }
5331 
5332  // Create the getside metatable.
5334 
5335  // Create the gettype metatable.
5337 
5338  //Create the getrace metatable
5340 
5341  //Create the unit metatables
5344 
5345  // Create the vconfig metatable.
5347 
5348  // Create the unit_types table
5350 
5351  // Create the terrainmap metatables
5353 
5354  // Create the terrain_types table
5355  cmd_log_ << "Adding terrain_types table...\n";
5356  lua_getglobal(L, "wesnoth");
5357  lua_newuserdatauv(L, 0, 0);
5358  lua_createtable(L, 0, 2);
5359  lua_pushcfunction(L, &dispatch<&game_lua_kernel::impl_get_terrain_info>);
5360  lua_setfield(L, -2, "__index");
5361  lua_pushcfunction(L, &dispatch<&game_lua_kernel::impl_get_terrain_list>);
5362  lua_setfield(L, -2, "__dir");
5363  lua_pushstring(L, "terrain types");
5364  lua_setfield(L, -2, "__metatable");
5365  lua_setmetatable(L, -2);
5366  lua_setfield(L, -2, "terrain_types");
5367  lua_pop(L, 1);
5368 
5369  // Create the ai elements table.
5370  cmd_log_ << "Adding ai elements table...\n";
5371 
5373 
5374  // Create the current variable with its metatable.
5375  cmd_log_ << "Adding wesnoth current table...\n";
5376 
5377  lua_getglobal(L, "wesnoth");
5378  lua_newuserdatauv(L, 0, 0);
5379  lua_createtable(L, 0, 2);
5380  lua_pushcfunction(L, &dispatch<&game_lua_kernel::impl_current_get>);
5381  lua_setfield(L, -2, "__index");
5382  lua_pushcfunction(L, &dispatch<&game_lua_kernel::impl_current_dir>);
5383  lua_setfield(L, -2, "__dir");
5384  lua_pushboolean(L, true);
5385  lua_setfield(L, -2, "__dir_tablelike");
5386  lua_pushstring(L, "current config");
5387  lua_setfield(L, -2, "__metatable");
5388  lua_setmetatable(L, -2);
5389  lua_setfield(L, -2, "current");
5390  lua_pop(L, 1);
5391 
5392  // Add functions to the WML module
5393  lua_getglobal(L, "wml");
5394  static luaL_Reg const wml_callbacks[] {
5395  {"tovconfig", &lua_common::intf_tovconfig},
5396  {"eval_conditional", &intf_eval_conditional},
5397  // These aren't actually part of the API - they're used internally by the variable metatable.
5398  { "get_variable", &dispatch<&game_lua_kernel::intf_get_variable>},
5399  { "set_variable", &dispatch<&game_lua_kernel::intf_set_variable>},
5400  { "get_all_vars", &dispatch<&game_lua_kernel::intf_get_all_vars>},
5401  { nullptr, nullptr }
5402  };
5403  luaL_setfuncs(L, wml_callbacks, 0);
5404  lua_pop(L, 1);
5405 
5406  // Add functions to the map module
5407  luaW_getglobal(L, "wesnoth", "map");
5408  static luaL_Reg const map_callbacks[] {
5409  // Map methods
5410  {"terrain_mask", &intf_terrain_mask},
5411  {"on_board", &intf_on_board},
5412  {"on_border", &intf_on_border},
5413  {"iter", &intf_terrainmap_iter},
5414  // Village operations
5415  {"get_owner", &dispatch<&game_lua_kernel::intf_get_village_owner>},
5416  {"set_owner", &dispatch<&game_lua_kernel::intf_set_village_owner>},
5417  // Label operations
5418  {"add_label", &dispatch<&game_lua_kernel::intf_add_label>},
5419  {"remove_label", &dispatch<&game_lua_kernel::intf_remove_label>},
5420  {"get_label", &dispatch<&game_lua_kernel::intf_get_label>},
5421  // Time area operations
5422  {"place_area", &dispatch<&game_lua_kernel::intf_add_time_area>},
5423  {"remove_area", &dispatch<&game_lua_kernel::intf_remove_time_area>},
5424  {"get_area", &dispatch<&game_lua_kernel::intf_get_time_area>},
5425  // Filters
5426  {"find", &dispatch<&game_lua_kernel::intf_get_locations>},
5427  {"matches", &dispatch<&game_lua_kernel::intf_match_location>},
5428  {"replace_if_failed", intf_replace_if_failed},
5429  { nullptr, nullptr }
5430  };
5431  luaL_setfuncs(L, map_callbacks, 0);
5432  lua_pop(L, 1);
5433 
5434  // Create the units module
5435  cmd_log_ << "Adding units module...\n";
5436  static luaL_Reg const unit_callbacks[] {
5437  {"advance", &intf_advance_unit},
5438  {"clone", &intf_copy_unit},
5439  {"erase", &dispatch<&game_lua_kernel::intf_erase_unit>},
5440  {"extract", &dispatch<&game_lua_kernel::intf_extract_unit>},
5441  {"matches", &dispatch<&game_lua_kernel::intf_match_unit>},
5442  {"select", &dispatch<&game_lua_kernel::intf_select_unit>},
5443  {"to_map", &dispatch<&game_lua_kernel::intf_put_unit>},
5444  {"to_recall", &dispatch<&game_lua_kernel::intf_put_recall_unit>},
5445  {"transform", &intf_transform_unit},
5446  {"teleport", &dispatch<&game_lua_kernel::intf_teleport>},
5447 
5448  {"ability", &dispatch<&game_lua_kernel::intf_unit_ability>},
5449  {"defense_on", &intf_unit_defense},
5450  {"jamming_on", &intf_unit_jamming_cost},
5451  {"movement_on", &intf_unit_movement_cost},
5452  {"resistance_against", intf_unit_resistance},
5453  {"vision_on", &intf_unit_vision_cost},
5454 
5455  {"add_modification", &intf_add_modification},
5456  {"remove_modifications", &intf_remove_modifications},
5457  // Static functions
5458  {"create", &intf_create_unit},
5459  {"find_on_map", &dispatch<&game_lua_kernel::intf_get_units>},
5460  {"find_on_recall", &dispatch<&game_lua_kernel::intf_get_recall_units>},
5461  {"get", &dispatch<&game_lua_kernel::intf_get_unit>},
5462  {"get_hovered", &dispatch<&game_lua_kernel::intf_get_displayed_unit>},
5463  {"create_animator", &dispatch<&game_lua_kernel::intf_create_animator>},
5464  {"create_weapon", intf_create_attack},
5465 
5466  { nullptr, nullptr }
5467  };
5468  lua_getglobal(L, "wesnoth");
5469  lua_newtable(L);
5470  luaL_setfuncs(L, unit_callbacks, 0);
5471  lua_setfield(L, -2, "units");
5472  lua_pop(L, 1);
5473 
5474  // Create sides module
5475  cmd_log_ << "Adding sides module...\n";
5476  static luaL_Reg const side_callbacks[] {
5477  { "is_enemy", &dispatch<&game_lua_kernel::intf_is_enemy> },
5478  { "matches", &dispatch<&game_lua_kernel::intf_match_side> },
5479  { "set_id", &dispatch<&game_lua_kernel::intf_set_side_id> },
5480  { "append_ai", &intf_append_ai },
5481  { "debug_ai", &intf_debug_ai },
5482  { "switch_ai", &intf_switch_ai },
5483  // Static functions
5484  { "find", &dispatch<&game_lua_kernel::intf_get_sides> },
5485  { "get", &dispatch<&game_lua_kernel::intf_get_side> },
5486  { "create", &dispatch<&game_lua_kernel::intf_create_side> },
5487  // Shroud operations
5488  {"place_shroud", &dispatch2<&game_lua_kernel::intf_toggle_shroud, true>},
5489  {"remove_shroud", &dispatch2<&game_lua_kernel::intf_toggle_shroud, false>},
5490  {"override_shroud", &dispatch<&game_lua_kernel::intf_override_shroud>},
5491  {"is_shrouded", &dispatch2<&game_lua_kernel::intf_get_fog_or_shroud, false>},
5492  // Fog operations
5493  {"place_fog", &dispatch2<&game_lua_kernel::intf_toggle_fog, false>},
5494  {"remove_fog", &dispatch2<&game_lua_kernel::intf_toggle_fog, true>},
5495  {"is_fogged", &dispatch2<&game_lua_kernel::intf_get_fog_or_shroud, true>},
5496  { nullptr, nullptr }
5497  };
5498  std::vector<lua_cpp::Reg> const cpp_side_callbacks {
5499  {"add_ai_component", std::bind(intf_modify_ai, std::placeholders::_1, "add")},
5500  {"delete_ai_component", std::bind(intf_modify_ai, std::placeholders::_1, "delete")},
5501  {"change_ai_component", std::bind(intf_modify_ai, std::placeholders::_1, "change")},
5502  {nullptr, nullptr}
5503  };
5504 
5505  lua_getglobal(L, "wesnoth");
5506  lua_newtable(L);
5507  luaL_setfuncs(L, side_callbacks, 0);
5508  lua_cpp::set_functions(L, cpp_side_callbacks);
5509  lua_setfield(L, -2, "sides");
5510  lua_pop(L, 1);
5511 
5512  // Create the interface module
5513  cmd_log_ << "Adding interface module...\n";
5514  static luaL_Reg const intf_callbacks[] {
5515  {"add_hex_overlay", &dispatch<&game_lua_kernel::intf_add_tile_overlay>},
5516  {"remove_hex_overlay", &dispatch<&game_lua_kernel::intf_remove_tile_overlay>},
5517  {"get_color_adjust", &dispatch<&game_lua_kernel::intf_get_color_adjust>},
5518  {"color_adjust", &dispatch<&game_lua_kernel::intf_color_adjust>},
5519  {"screen_fade", &dispatch<&game_lua_kernel::intf_screen_fade>},
5520  {"delay", &dispatch<&game_lua_kernel::intf_delay>},
5521  {"deselect_hex", &dispatch<&game_lua_kernel::intf_deselect_hex>},
5522  {"highlight_hex", &dispatch<&game_lua_kernel::intf_highlight_hex>},
5523  {"float_label", &dispatch<&game_lua_kernel::intf_float_label>},
5524  {"get_displayed_unit", &dispatch<&game_lua_kernel::intf_get_displayed_unit>},
5525  {"get_hovered_hex", &dispatch<&game_lua_kernel::intf_get_mouseover_tile>},
5526  {"get_selected_hex", &dispatch<&game_lua_kernel::intf_get_selected_tile>},
5527  {"lock", &dispatch<&game_lua_kernel::intf_lock_view>},
5528  {"is_locked", &dispatch<&game_lua_kernel::intf_view_locked>},
5529  {"scroll", &dispatch<&game_lua_kernel::intf_scroll>},
5530  {"scroll_to_hex", &dispatch<&game_lua_kernel::intf_scroll_to_tile>},
5531  {"skip_messages", &dispatch<&game_lua_kernel::intf_skip_messages>},
5532  {"is_skipping_messages", &dispatch<&game_lua_kernel::intf_is_skipping_messages>},
5533  {"zoom", &dispatch<&game_lua_kernel::intf_zoom>},
5534  {"clear_menu_item", &dispatch<&game_lua_kernel::intf_clear_menu_item>},
5535  {"set_menu_item", &dispatch<&game_lua_kernel::intf_set_menu_item>},
5536  {"allow_end_turn", &dispatch<&game_lua_kernel::intf_allow_end_turn>},
5537  {"clear_chat_messages", &dispatch<&game_lua_kernel::intf_clear_messages>},
5538  {"end_turn", &dispatch<&game_lua_kernel::intf_end_turn>},
5539  {"get_viewing_side", &intf_get_viewing_side},
5540  {"add_chat_message", &dispatch<&game_lua_kernel::intf_message>},
5541  {"add_overlay_text", &dispatch2<&game_lua_kernel::intf_set_floating_label, true>},
5542  {"handle_user_interact", &intf_handle_user_interact},
5543  { nullptr, nullptr }
5544  };
5545  lua_getglobal(L, "wesnoth");
5546  lua_newtable(L);
5547  luaL_setfuncs(L, intf_callbacks, 0);
5548  lua_setfield(L, -2, "interface");
5549  lua_pop(L, 1);
5550 
5551  // Create the achievements module
5552  cmd_log_ << "Adding achievements module...\n";
5553  static luaL_Reg const achievement_callbacks[] {
5554  { "set", &dispatch<&game_lua_kernel::intf_set_achievement> },
5555  { "has", &dispatch<&game_lua_kernel::intf_has_achievement> },
5556  { "get", &dispatch<&game_lua_kernel::intf_get_achievement> },
5557  { "progress", &dispatch<&game_lua_kernel::intf_progress_achievement> },
5558  { "has_sub_achievement", &dispatch<&game_lua_kernel::intf_has_sub_achievement> },
5559  { "set_sub_achievement", &dispatch<&game_lua_kernel::intf_set_sub_achievement> },
5560  { nullptr, nullptr }
5561  };
5562  lua_getglobal(L, "wesnoth");
5563  lua_newtable(L);
5564  luaL_setfuncs(L, achievement_callbacks, 0);
5565  lua_setfield(L, -2, "achievements");
5566  lua_pop(L, 1);
5567 
5568  // Create the audio module
5569  cmd_log_ << "Adding audio module...\n";
5570  static luaL_Reg const audio_callbacks[] {
5571  { "play", &dispatch<&game_lua_kernel::intf_play_sound > },
5572  { nullptr, nullptr }
5573  };
5574  lua_getglobal(L, "wesnoth");
5575  lua_newtable(L);
5576  luaL_setfuncs(L, audio_callbacks, 0);
5577  lua_setfield(L, -2, "audio");
5578  lua_pop(L, 1);
5579 
5580  // Create the paths module
5581  cmd_log_ << "Adding paths module...\n";
5582  static luaL_Reg const path_callbacks[] {
5583  { "find_cost_map", &dispatch<&game_lua_kernel::intf_find_cost_map > },
5584  { "find_path", &dispatch<&game_lua_kernel::intf_find_path > },
5585  { "find_reach", &dispatch<&game_lua_kernel::intf_find_reach > },
5586  { "find_vacant_hex", &dispatch<&game_lua_kernel::intf_find_vacant_tile > },
5587  { "find_vision_range", &dispatch<&game_lua_kernel::intf_find_vision_range > },
5588  { nullptr, nullptr }
5589  };
5590  lua_getglobal(L, "wesnoth");
5591  lua_newtable(L);
5592  luaL_setfuncs(L, path_callbacks, 0);
5593  lua_setfield(L, -2, "paths");
5594  lua_pop(L, 1);
5595 
5596  // Create the sync module
5597  cmd_log_ << "Adding sync module...\n";
5598  static luaL_Reg const sync_callbacks[] {
5599  { "invoke_command", &intf_invoke_synced_command },
5600  { "run_unsynced", &intf_do_unsynced },
5601  { "evaluate_single", &intf_synchronize_choice },
5602  { "evaluate_multiple", &intf_synchronize_choices },
5603  { nullptr, nullptr }
5604  };
5605  lua_getglobal(L, "wesnoth");
5606  lua_newtable(L);
5607  luaL_setfuncs(L, sync_callbacks, 0);
5608  lua_setfield(L, -2, "sync");
5609  lua_pop(L, 1);
5610 
5611  // Create the schedule module
5612  cmd_log_ << "Adding schedule module...\n";
5613  static luaL_Reg const schedule_callbacks[] {
5614  { "get_time_of_day", &dispatch<&game_lua_kernel::intf_get_time_of_day<false>>},
5615  { "get_illumination", &dispatch<&game_lua_kernel::intf_get_time_of_day<true>>},
5616  { "replace", &dispatch<&game_lua_kernel::intf_replace_schedule>},
5617  { nullptr, nullptr }
5618  };
5619  lua_getglobal(L, "wesnoth");
5620  lua_newtable(L);
5621  luaL_setfuncs(L, schedule_callbacks, 0);
5622  lua_createtable(L, 0, 2);
5623  lua_setmetatable(L, -2);
5624  lua_setfield(L, -2, "schedule");
5625  lua_pop(L, 1);
5626 
5627  // Create the playlist table with its metatable
5629 
5630  // Create the wml_actions table.
5631  cmd_log_ << "Adding wml_actions table...\n";
5632 
5633  lua_getglobal(L, "wesnoth");
5634  lua_newtable(L);
5635  lua_setfield(L, -2, "wml_actions");
5636  lua_pop(L, 1);
5637 
5638  // Create the wml_conditionals table.
5639  cmd_log_ << "Adding wml_conditionals table...\n";
5640 
5641  lua_getglobal(L, "wesnoth");
5642  lua_newtable(L);
5643  lua_setfield(L, -2, "wml_conditionals");
5644  lua_pop(L, 1);
5648 
5649  // Create the effects table.
5650  cmd_log_ << "Adding effects table...\n";
5651 
5652  lua_getglobal(L, "wesnoth");
5653  lua_newtable(L);
5654  lua_setfield(L, -2, "effects");
5655  lua_pop(L, 1);
5656 
5657  // Create the custom_synced_commands table.
5658  cmd_log_ << "Adding custom_synced_commands table...\n";
5659 
5660  lua_getglobal(L, "wesnoth");
5661  lua_newtable(L);
5662  lua_setfield(L, -2, "custom_synced_commands");
5663  lua_pop(L, 1);
5664 
5665  // Create the game_events table.
5666  cmd_log_ << "Adding game_events module...\n";
5667  static luaL_Reg const event_callbacks[] {
5668  { "add", &dispatch<&game_lua_kernel::intf_add_event> },
5669  { "add_repeating", &dispatch<&game_lua_kernel::intf_add_event_simple<false>> },
5670  { "add_menu", &dispatch<&game_lua_kernel::intf_add_event_simple<true>> },
5671  { "add_wml", &dispatch<&game_lua_kernel::intf_add_event_wml> },
5672  { "remove", &dispatch<&game_lua_kernel::intf_remove_event> },
5673  { "fire", &dispatch2<&game_lua_kernel::intf_fire_event, false> },
5674  { "fire_by_id", &dispatch2<&game_lua_kernel::intf_fire_event, true> },
5675  { "add_undo_actions", &dispatch<&game_lua_kernel::intf_add_undo_actions> },
5676  { "set_undoable", &dispatch<&game_lua_kernel::intf_allow_undo > },
5677  { nullptr, nullptr }
5678  };
5679  lua_getglobal(L, "wesnoth");
5680  lua_newtable(L);
5681  luaL_setfuncs(L, event_callbacks, 0);
5682  lua_createtable(L, 0, 2);
5683  lua_pushcfunction(L, &impl_game_events_dir);
5684  lua_setfield(L, -2, "__dir");
5685  lua_pushcfunction(L, &impl_game_events_get);
5686  lua_setfield(L, -2, "__index");
5687  lua_pushstring(L, "game_events");
5688  lua_setfield(L, -2, "__metatable");
5689  lua_setmetatable(L, -2);
5690  lua_setfield(L, -2, "game_events");
5691  lua_pop(L, 1);
5692 
5693  // Create the theme_items table.
5694  cmd_log_ << "Adding game_display table...\n";
5695 
5696  luaW_getglobal(L, "wesnoth", "interface");
5697  lua_newtable(L);
5698  lua_createtable(L, 0, 2);
5699  lua_pushcfunction(L, &dispatch<&game_lua_kernel::impl_theme_items_get>);
5700  lua_setfield(L, -2, "__index");
5701  lua_pushcfunction(L, &dispatch<&game_lua_kernel::impl_theme_items_set>);
5702  lua_setfield(L, -2, "__newindex");
5703  lua_pushcfunction(L, &dispatch<&game_lua_kernel::impl_theme_items_dir>);
5704  lua_setfield(L, -2, "__dir");
5705  lua_setmetatable(L, -2);
5706  lua_setfield(L, -2, "game_display");
5707  lua_pop(L, 1);
5708 
5709  // Create the scenario table.
5710  cmd_log_ << "Adding scenario table...\n";
5711 
5712  luaW_getglobal(L, "wesnoth");
5713  lua_newtable(L);
5714  lua_createtable(L, 0, 2);
5715  lua_pushcfunction(L, &dispatch<&game_lua_kernel::impl_scenario_get>);
5716  lua_setfield(L, -2, "__index");
5717  lua_pushcfunction(L, &dispatch<&game_lua_kernel::impl_scenario_set>);
5718  lua_setfield(L, -2, "__newindex");
5719  lua_pushcfunction(L, &dispatch<&game_lua_kernel::impl_scenario_dir>);
5720  lua_setfield(L, -2, "__dir");
5721  lua_setmetatable(L, -2);
5722  lua_setfield(L, -2, "scenario");
5723  lua_pop(L, 1);
5724 
5725  lua_settop(L, 0);
5726 
5727  for(const auto& handler : game_events::wml_action::registry())
5728  {
5729  set_wml_action(handler.first, handler.second);
5730  }
5731  luaW_getglobal(L, "wesnoth", "effects");
5732  for(const std::string& effect : unit::builtin_effects) {
5733  lua_pushstring(L, effect.c_str());
5735  lua_rawset(L, -3);
5736  }
5737  lua_settop(L, 0);
5738 
5739  // Set up the registry table for event handlers
5740  lua_newtable(L);
5741  EVENT_TABLE = luaL_ref(L, LUA_REGISTRYINDEX);
5742 }
5743 
5745 {
5746  lua_State *L = mState;
5747  assert(level_lua_.empty());
5749 
5750  //Create the races table.
5751  cmd_log_ << "Adding races table...\n";
5752 
5753  lua_settop(L, 0);
5754  lua_getglobal(L, "wesnoth");
5755  luaW_pushracetable(L);
5756  lua_setfield(L, -2, "races");
5757  lua_pop(L, 1);
5758 
5759  // Execute the preload scripts.
5760  cmd_log_ << "Running preload scripts...\n";
5761 
5763  for (const config &cfg : game_lua_kernel::preload_scripts) {
5764  run_lua_tag(cfg);
5765  }
5766  for (const config &cfg : level_lua_.child_range("lua")) {
5767  run_lua_tag(cfg);
5768  }
5769 }
5770 
5772  game_display_ = gd;
5773 }
5774 
5775 /**
5776  * These are the child tags of [scenario] (and the like) that are handled
5777  * elsewhere (in the C++ code).
5778  * Any child tags not in this list will be passed to Lua's on_load event.
5779  */
5780 static bool is_handled_file_tag(std::string_view s)
5781 {
5782  // Make sure this is sorted, since we binary_search!
5783  using namespace std::literals::string_view_literals;
5784  static constexpr std::array handled_file_tags {
5785  "color_palette"sv,
5786  "color_range"sv,
5787  "display"sv,
5788  "end_level_data"sv,
5789  "era"sv,
5790  "event"sv,
5791  "generator"sv,
5792  "label"sv,
5793  "lua"sv,
5794  "map"sv,
5795  "menu_item"sv,
5796  "modification"sv,
5797  "modify_unit_type"sv,
5798  "music"sv,
5799  "options"sv,
5800  "side"sv,
5801  "sound_source"sv,
5802  "story"sv,
5803  "terrain_graphics"sv,
5804  "time"sv,
5805  "time_area"sv,
5806  "tunnel"sv,
5807  "undo_stack"sv,
5808  "variables"sv
5809  };
5810 
5811  return std::binary_search(handled_file_tags.begin(), handled_file_tags.end(), s);
5812 }
5813 
5814 /**
5815  * Executes the game_events.on_load function and passes to it all the
5816  * scenario tags not yet handled.
5817  */
5819 {
5820  lua_State *L = mState;
5821 
5822  if(!impl_get_callback(L, "on_load"))
5823  return;
5824 
5825  lua_newtable(L);
5826  int k = 1;
5827  for(const auto [child_key, child_cfg] : level.all_children_view())
5828  {
5829  if (is_handled_file_tag(child_key)) continue;
5830  lua_createtable(L, 2, 0);
5831  lua_pushstring(L, child_key.c_str());
5832  lua_rawseti(L, -2, 1);
5833  luaW_pushconfig(L, child_cfg);
5834  lua_rawseti(L, -2, 2);
5835  lua_rawseti(L, -2, k++);
5836  }
5837 
5838  luaW_pcall(L, 1, 0, true);
5839 }
5840 
5841 /**
5842  * Executes the game_events.on_save function and adds to @a cfg the
5843  * returned tags. Also flushes the [lua] tags.
5844  */
5846 {
5847  lua_State *L = mState;
5848 
5849  if(!impl_get_callback<config>(L, "on_save"))
5850  return;
5851 
5852  if (!luaW_pcall(L, 0, 1, false))
5853  return;
5854 
5855  config v;
5856  luaW_toconfig(L, -1, v);
5857  lua_pop(L, 1);
5858 
5859  // Make a copy of the source tag names. Since splice is a destructive operation,
5860  // we can't guarantee that the view will remain valid during iteration.
5861  const auto temp = v.child_name_view();
5862  const std::vector<std::string> src_tags(temp.begin(), temp.end());
5863 
5864  for(const auto& key : src_tags) {
5865  if(is_handled_file_tag(key)) {
5866  /*
5867  * It seems the only tags appearing in the config v variable here
5868  * are the core-lua-handled (currently [item] and [objectives])
5869  * and the extra UMC ones.
5870  */
5871  const std::string m = "Tag is already used: [" + key + "]";
5872  log_error(m.c_str());
5873  continue;
5874  } else {
5875  cfg.splice_children(v, key);
5876  }
5877  }
5878 }
5879 
5880 /**
5881  * Executes the game_events.on_event function.
5882  * Returns false if there was no lua handler for this event
5883  */
5885 {
5886  lua_State *L = mState;
5887 
5888  if(!impl_get_callback(L, "on_event"))
5889  return false;
5890 
5891  queued_event_context dummy(&ev, queued_events_);
5892  lua_pushstring(L, ev.name.c_str());
5893  luaW_pcall(L, 1, 0, false);
5894  return true;
5895 }
5896 
5897 void game_lua_kernel::custom_command(const std::string& name, const config& cfg)
5898 {
5899  lua_State *L = mState;
5900 
5901  if (!luaW_getglobal(L, "wesnoth", "custom_synced_commands", name)) {
5902  return;
5903  }
5904  luaW_pushconfig(L, cfg);
5905  luaW_pcall(L, 1, 0, false);
5906 }
5907 
5908 /**
5909  * Applies its upvalue as an effect
5910  * Arg 1: The unit to apply to
5911  * Arg 3: The [effect] tag contents
5912  * Arg 3: If false, only build description
5913  * Return: The description of the effect
5914  */
5916 {
5917  std::string which_effect = lua_tostring(L, lua_upvalueindex(1));
5918  bool need_apply = luaW_toboolean(L, lua_upvalueindex(2));
5919  // Argument 1 is the implicit "self" argument, which isn't needed here
5920  lua_unit u(luaW_checkunit(L, 2));
5921  config cfg = luaW_checkconfig(L, 3);
5922 
5923  // The times= key is supposed to be ignored by the effect function.
5924  // However, just in case someone doesn't realize this, we will set it to 1 here.
5925  cfg["times"] = 1;
5926 
5927  if(need_apply) {
5928  u->apply_builtin_effect(which_effect, cfg);
5929  return 0;
5930  } else {
5931  std::string description = u->describe_builtin_effect(which_effect, cfg);
5932  lua_pushstring(L, description.c_str());
5933  return 1;
5934  }
5935 }
5936 
5937 /**
5938 * Registers a function for use as an effect handler.
5939 */
5941 {
5942  lua_State *L = mState;
5943 
5944  // The effect name is at the top of the stack
5945  int str_i = lua_gettop(L);
5946  lua_newtable(L); // The functor table
5947  lua_newtable(L); // The functor metatable
5948  lua_pushstring(L, "__call");
5949  lua_pushvalue(L, str_i);
5950  lua_pushboolean(L, true);
5951  lua_pushcclosure(L, &dispatch<&game_lua_kernel::cfun_builtin_effect>, 2);
5952  lua_rawset(L, -3); // Set the call metafunction
5953  lua_pushstring(L, "__descr");
5954  lua_pushvalue(L, str_i);
5955  lua_pushboolean(L, false);
5956  lua_pushcclosure(L, &dispatch<&game_lua_kernel::cfun_builtin_effect>, 2);
5957  lua_rawset(L, -3); // Set the descr "metafunction"
5958  lua_setmetatable(L, -2); // Apply the metatable to the functor table
5959 }
5960 
5961 
5962 /**
5963  * Executes its upvalue as a wml action.
5964  */
5966 {
5968  (lua_touserdata(L, lua_upvalueindex(1)));
5969 
5970  vconfig vcfg = luaW_checkvconfig(L, 1);
5971  h(get_event_info(), vcfg);
5972  return 0;
5973 }
5974 
5975 /**
5976  * Registers a function for use as an action handler.
5977  */
5979 {
5980  lua_State *L = mState;
5981 
5982  lua_getglobal(L, "wesnoth");
5983  lua_pushstring(L, "wml_actions");
5984  lua_rawget(L, -2);
5985  lua_pushstring(L, cmd.c_str());
5986  lua_pushlightuserdata(L, reinterpret_cast<void *>(h));
5987  lua_pushcclosure(L, &dispatch<&game_lua_kernel::cfun_wml_action>, 1);
5988  lua_rawset(L, -3);
5989  lua_pop(L, 2);
5990 }
5991 
5992 using wml_conditional_handler = bool(*)(const vconfig&);
5993 
5994 /**
5995  * Executes its upvalue as a wml condition and returns the result.
5996  */
5997 static int cfun_wml_condition(lua_State *L)
5998 {
6000  (lua_touserdata(L, lua_upvalueindex(1)));
6001 
6002  vconfig vcfg = luaW_checkvconfig(L, 1);
6003  lua_pushboolean(L, h(vcfg));
6004  return 1;
6005 }
6006 
6007 /**
6008  * Registers a function for use as a conditional handler.
6009  */
6011 {
6012  lua_State *L = mState;
6013 
6014  lua_getglobal(L, "wesnoth");
6015  lua_pushstring(L, "wml_conditionals");
6016  lua_rawget(L, -2);
6017  lua_pushstring(L, cmd.c_str());
6018  lua_pushlightuserdata(L, reinterpret_cast<void *>(h));
6019  lua_pushcclosure(L, &cfun_wml_condition, 1);
6020  lua_rawset(L, -3);
6021  lua_pop(L, 2);
6022 }
6023 
6024 /**
6025  * Runs a command from an event handler.
6026  * @return true if there is a handler for the command.
6027  * @note @a cfg should be either volatile or long-lived since the Lua
6028  * code may grab it for an arbitrary long time.
6029  */
6030 bool game_lua_kernel::run_wml_action(const std::string& cmd, const vconfig& cfg,
6031  const game_events::queued_event& ev)
6032 {
6033  lua_State *L = mState;
6034 
6035 
6036  if (!luaW_getglobal(L, "wesnoth", "wml_actions", cmd))
6037  return false;
6038 
6039  queued_event_context dummy(&ev, queued_events_);
6040  luaW_pushvconfig(L, cfg);
6041  luaW_pcall(L, 1, 0, true);
6042  return true;
6043 }
6044 
6045 
6046 /**
6047  * Evaluates a WML conidition.
6048  *
6049  * @returns Whether the condition passed.
6050  * @note @a cfg should be either volatile or long-lived since the Lua
6051  * code may grab it for an arbitrarily long time.
6052  */
6053 bool game_lua_kernel::run_wml_conditional(const std::string& cmd, const vconfig& cfg)
6054 {
6055  lua_State* L = mState;
6056 
6057  // If an invalid coniditional tag is used, consider it a pass.
6058  if(!luaW_getglobal(L, "wesnoth", "wml_conditionals", cmd)) {
6059  lg::log_to_chat() << "unknown conditional wml: [" << cmd << "]\n";
6060  ERR_WML << "unknown conditional wml: [" << cmd << "]";
6061  return true;
6062  }
6063 
6064  luaW_pushvconfig(L, cfg);
6065 
6066  // Any runtime error is considered a fail.
6067  if(!luaW_pcall(L, 1, 1, true)) {
6068  return false;
6069  }
6070 
6071  bool b = luaW_toboolean(L, -1);
6072 
6073  lua_pop(L, 1);
6074  return b;
6075 }
6076 
6077 static int intf_run_event_wml(lua_State* L)
6078 {
6079  int argIdx = lua_gettop(L);
6080  if(!luaW_getglobal(L, "wesnoth", "wml_actions", "command")) {
6081  return luaL_error(L, "wesnoth.wml_actions.command is missing");
6082  }
6083  lua_pushvalue(L, argIdx);
6084  lua_call(L, 1, 0);
6085  return 0;
6086 }
6087 
6089 {
6090  lua_State* L = mState;
6091  lua_geti(L, LUA_REGISTRYINDEX, EVENT_TABLE);
6092  int evtIdx = lua_gettop(L);
6093  ON_SCOPE_EXIT(L) {
6094  lua_pop(L, 1);
6095  };
6096  lua_pushcfunction(L, intf_run_event_wml);
6097  return luaL_ref(L, evtIdx);
6098 }
6099 
6100 int game_lua_kernel::save_wml_event(const std::string& name, const std::string& id, const std::string& code)
6101 {
6102  lua_State* L = mState;
6103  lua_geti(L, LUA_REGISTRYINDEX, EVENT_TABLE);
6104  int evtIdx = lua_gettop(L);
6105  ON_SCOPE_EXIT(L) {
6106  lua_pop(L, 1);
6107  };
6108  std::ostringstream lua_name;
6109  lua_name << "event ";
6110  if(name.empty()) {
6111  lua_name << "<anon>";
6112  } else {
6113  lua_name << name;
6114  }
6115  if(!id.empty()) {
6116  lua_name << "[id=" << id << "]";
6117  }
6118  if(!load_string(code.c_str(), lua_name.str())) {
6119  ERR_LUA << "Failed to register WML event: " << lua_name.str();
6120  return LUA_NOREF;
6121  }
6122  return luaL_ref(L, evtIdx);
6123 }
6124 
6126 {
6127  lua_State* L = mState;
6128  idx = lua_absindex(L, idx);
6129  lua_geti(L, LUA_REGISTRYINDEX, EVENT_TABLE);
6130  int evtIdx = lua_gettop(L);
6131  ON_SCOPE_EXIT(L) {
6132  lua_pop(L, 1);
6133  };
6134  lua_pushvalue(L, idx);
6135  return luaL_ref(L, evtIdx);
6136 }
6137 
6139 {
6140  lua_State* L = mState;
6141  lua_geti(L, LUA_REGISTRYINDEX, EVENT_TABLE);
6142  luaL_unref(L, -1, ref);
6143  lua_pop(L, 1);
6144 }
6145 
6146 bool game_lua_kernel::run_wml_event(int ref, const vconfig& args, const game_events::queued_event& ev, bool* out)
6147 {
6148  lua_State* L = mState;
6149  lua_geti(L, LUA_REGISTRYINDEX, EVENT_TABLE);
6150  ON_SCOPE_EXIT(L) {
6151  lua_pop(L, 1);
6152  };
6153  lua_geti(L, -1, ref);
6154  if(lua_isnil(L, -1)) return false;
6155  luaW_pushvconfig(L, args);
6156  queued_event_context dummy(&ev, queued_events_);
6157  if(luaW_pcall(L, 1, out ? 1 : 0, true)) {
6158  if(out) {
6159  *out = luaW_toboolean(L, -1);
6160  lua_pop(L, 1);
6161  }
6162  return true;
6163  }
6164  return false;
6165 }
6166 
6167 
6168 /**
6169 * Runs a script from a location filter.
6170 * The script is an already compiled function given by its name.
6171 */
6172 bool game_lua_kernel::run_filter(char const *name, const map_location& l)
6173 {
6174  lua_pushinteger(mState, l.wml_x());
6175  lua_pushinteger(mState, l.wml_y());
6176  return run_filter(name, 2);
6177 }
6178 
6179 /**
6180 * Runs a script from a location filter.
6181 * The script is an already compiled function given by its name.
6182 */
6183 bool game_lua_kernel::run_filter(char const *name, const team& t)
6184 {
6185  //TODO: instead of passing the lua team object we coudl also jsut pass its
6186  // number. then we wouldn't need this const cast.
6187  luaW_pushteam(mState, const_cast<team&>(t));
6188  return run_filter(name, 1);
6189 }
6190 /**
6191 * Runs a script from a unit filter.
6192 * The script is an already compiled function given by its name.
6193 */
6194 bool game_lua_kernel::run_filter(char const *name, const unit& u)
6195 {
6196  lua_State *L = mState;
6197  lua_unit* lu = luaW_pushlocalunit(L, const_cast<unit&>(u));
6198  // stack: unit
6199  // put the unit to the stack twice to prevent gc.
6200  lua_pushvalue(L, -1);
6201  // stack: unit, unit
6202  bool res = run_filter(name, 1);
6203  // stack: unit
6204  lu->clear_ref();
6205  lua_pop(L, 1);
6206  return res;
6207 }
6208 /**
6209 * Runs a script from a filter.
6210 * The script is an already compiled function given by its name.
6211 */
6212 bool game_lua_kernel::run_filter(char const *name, int nArgs)
6213 {
6214  auto ml = map_locker(this);
6215  lua_State *L = mState;
6216  // Get the user filter by name.
6217  const std::vector<std::string>& path = utils::split(name, '.', utils::STRIP_SPACES);
6218  if (!luaW_getglobal(L, path))
6219  {
6220  std::string message = std::string() + "function " + name + " not found";
6221  log_error(message.c_str(), "Lua SUF Error");
6222  //we pushed nothing and can safeley return.
6223  return false;
6224  }
6225  lua_insert(L, -nArgs - 1);
6226 
6227  if (!luaW_pcall(L, nArgs, 1)) return false;
6228 
6229  bool b = luaW_toboolean(L, -1);
6230  lua_pop(L, 1);
6231  return b;
6232 }
6233 
6234 std::string game_lua_kernel::apply_effect(const std::string& name, unit& u, const config& cfg, bool need_apply)
6235 {
6236  lua_State *L = mState;
6237  int top = lua_gettop(L);
6238  std::string descr;
6239  // Stack: nothing
6240  lua_unit* lu = luaW_pushlocalunit(L, u);
6241  // Stack: unit
6242  // (Note: The unit needs to be on the stack twice to prevent untimely GC.)
6243  luaW_pushconfig(L, cfg);
6244  // Stack: unit, cfg
6245  if(luaW_getglobal(L, "wesnoth", "effects", name)) {
6246  auto ml = map_locker(this);
6247  // Stack: unit, cfg, effect
6248  if(lua_istable(L, -1)) {
6249  // Effect is implemented by a table with __call and __descr
6250  if(need_apply) {
6251  lua_pushvalue(L, -1);
6252  // Stack: unit, cfg, effect, effect
6253  lua_pushvalue(L, top + 1);
6254  // Stack: unit, cfg, effect, effect, unit
6255  lua_pushvalue(L, top + 2);
6256  // Stack: unit, cfg, effect, effect, unit, cfg
6257  luaW_pcall(L, 2, 0);
6258  // Stack: unit, cfg, effect
6259  }
6260  if(luaL_getmetafield(L, -1, "__descr")) {
6261  // Stack: unit, cfg, effect, __descr
6262  if(lua_isstring(L, -1)) {
6263  // __descr was a static string
6264  descr = lua_tostring(L, -1);
6265  } else {
6266  lua_pushvalue(L, -2);
6267  // Stack: unit, cfg, effect, __descr, effect
6268  lua_pushvalue(L, top + 1);
6269  // Stack: unit, cfg, effect, __descr, effect, unit
6270  lua_pushvalue(L, top + 2);
6271  // Stack: unit, cfg, effect, __descr, effect, unit, cfg
6272  luaW_pcall(L, 3, 1);
6273  if(lua_isstring(L, -1) && !lua_isnumber(L, -1)) {
6274  descr = lua_tostring(L, -1);
6275  } else {
6276  ERR_LUA << "Effect __descr metafunction should have returned a string, but instead returned ";
6277  if(lua_isnone(L, -1)) {
6278  ERR_LUA << "nothing";
6279  } else {
6280  ERR_LUA << lua_typename(L, lua_type(L, -1));
6281  }
6282  }
6283  }
6284  }
6285  } else if(need_apply) {
6286  // Effect is assumed to be a simple function; no description is provided
6287  lua_pushvalue(L, top + 1);
6288  // Stack: unit, cfg, effect, unit
6289  lua_pushvalue(L, top + 2);
6290  // Stack: unit, cfg, effect, unit, cfg
6291  luaW_pcall(L, 2, 0);
6292  // Stack: unit, cfg
6293  }
6294  }
6295  lua_settop(L, top);
6296  lu->clear_ref();
6297  return descr;
6298 }
6299 
6301 {
6302  return ai::lua_ai_context::create(mState,code,engine);
6303 }
6304 
6306 {
6307  return ai::lua_ai_action_handler::create(mState,code,context);
6308 }
6309 
6311 {
6312  lua_State *L = mState;
6313 
6314  if(!impl_get_callback(L, "on_mouse_move")) {
6315  return;
6316  }
6317  lua_push(L, loc.wml_x());
6318  lua_push(L, loc.wml_y());
6319  luaW_pcall(L, 2, 0, false);
6320  return;
6321 }
6322 
6323 bool game_lua_kernel::mouse_button_callback(const map_location& loc, const std::string &button, const std::string &event)
6324 {
6325  lua_State *L = mState;
6326 
6327  if(!impl_get_callback<bool>(L, "on_mouse_button")) {
6328  return false;
6329  }
6330 
6331  lua_push(L, loc.wml_x());
6332  lua_push(L, loc.wml_y());
6333  lua_push(L, button);
6334  lua_push(L, event);
6335 
6336  if (!luaW_pcall(L, 4, 1)) return false;
6337  bool result = luaW_toboolean(L, -1);
6338  lua_pop(L, 1);
6339  return result;
6340 }
6341 
6343 {
6344  lua_State *L = mState;
6345 
6346  if(!impl_get_callback(L, "on_mouse_action")) {
6347  return;
6348  }
6349  lua_push(L, loc.wml_x());
6350  lua_push(L, loc.wml_y());
6351  luaW_pcall(L, 2, 0, false);
6352  return;
6353 }
Various functions that implement attacks and attack calculations.
map_location loc
Definition: move.cpp:172
Various functions related to moving units.
void advance_unit_at(const advance_unit_params &params)
Various functions that implement advancements of units.
Managing the AIs lifecycle - headers TODO: Refactor history handling and internal commands.
GAME_CONFIG_SETTER("debug", bool, application_lua_kernel)
double t
Definition: astarsearch.cpp:63
#define debug(x)
Class to encapsulate fog/shroud clearing and the resultant sighted events.
Definition: vision.hpp:72
bool clear_dest(const map_location &dest, const unit &viewer)
Clears shroud (and fog) at the provided location and its immediate neighbors.
Definition: vision.cpp:490
game_events::pump_result_t fire_events()
Fires the sighted events that were earlier recorded by fog/shroud clearing.
Definition: vision.cpp:541
bool clear_unit(const map_location &view_loc, team &view_team, std::size_t viewer_id, int sight_range, bool slowed, const movetype::terrain_costs &costs, const map_location &real_loc, const std::set< map_location > *known_units=nullptr, std::size_t *enemy_count=nullptr, std::size_t *friend_count=nullptr, move_unit_spectator *spectator=nullptr, bool instant=true)
Clears shroud (and fog) around the provided location for view_team based on sight_range,...
Definition: vision.cpp:330
virtual ai_context & get_ai_context()
unwrap
Definition: ai.cpp:193
virtual double evaluate()=0
Evaluate the candidate action, resetting the internal state of the action.
virtual void execute()=0
Execute the candidate action.
static bool add_component(component *root, const std::string &path, const config &cfg)
Definition: component.cpp:172
static void expand_simplified_aspects(side_number side, config &cfg)
Expand simplified aspects, similar to the change from 1.7.2 to 1.7.3 but with some additional syntax ...
static const config & get_default_ai_parameters()
get default AI parameters
virtual config to_config() const
Serialize to config.
Definition: engine_lua.cpp:391
virtual void push_ai_table()
Method that pushes the AI table of the lua_context on the stack for debugging purposes.
Definition: engine_lua.cpp:297
component * get_component(component *root, const std::string &path)
Definition: manager.cpp:310
Proxy class for calling AI action handlers defined in Lua.
Definition: core.hpp:71
static lua_ai_action_handler * create(lua_State *L, char const *code, lua_ai_context &context)
Definition: core.cpp:1020
Proxy table for the AI context.
Definition: core.hpp:34
static void init(lua_State *L)
Definition: core.cpp:53
static lua_ai_context * create(lua_State *L, char const *code, engine_lua *engine)
Definition: core.cpp:978
bool add_ai_for_side_from_config(side_number side, const config &cfg, bool replace=true)
Adds active AI for specified side from cfg.
Definition: manager.cpp:632
void append_active_ai_for_side(ai::side_number side, const config &cfg)
Appends AI parameters to active AI of the given side.
Definition: manager.cpp:677
void raise_user_interact()
Notifies all observers of 'ai_user_interact' event.
Definition: manager.cpp:424
static manager & get_singleton()
Definition: manager.hpp:140
void modify_active_ai_for_side(ai::side_number side, const config &cfg)
Modifies AI parameters for active AI of the given side.
Definition: manager.cpp:672
ai::holder & get_active_ai_holder_for_side_dbg(side_number side)
Gets the active AI holder for debug purposes.
Definition: manager.cpp:697
bool play_stage()
Play the turn - strategy.
Definition: stage.cpp:55
Computes the statistics of a battle between an attacker and a defender unit.
Definition: attack.hpp:167
const battle_context_unit_stats & get_defender_stats() const
This method returns the statistics of the defender.
Definition: attack.hpp:199
const combatant & get_attacker_combatant(const combatant *prev_def=nullptr)
Get the simulation results.
Definition: attack.cpp:441
const battle_context_unit_stats & get_attacker_stats() const
This method returns the statistics of the attacker.
Definition: attack.hpp:193
const combatant & get_defender_combatant(const combatant *prev_def=nullptr)
Definition: attack.cpp:448
Variant for storing WML attributes.
std::string str(const std::string &fallback="") const
bool empty() const
Tests for an attribute that either was never set or was set to "".
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
config & mandatory_child(config_key_type key, int n=0)
Returns the nth child with the given key, or throws an error if there is none.
Definition: config.cpp:362
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
Definition: config.cpp:312
void remove_children(config_key_type key, const std::function< bool(const config &)> &p={})
Removes all children with tag key for which p returns true.
Definition: config.cpp:650
child_itors child_range(config_key_type key)
Definition: config.cpp:268
config & child_or_add(config_key_type key)
Returns a reference to the first child with the given key.
Definition: config.cpp:401
auto child_name_view() const
A non-owning view over all child tag names.
Definition: config.hpp:904
void append_children(const config &cfg)
Adds children from cfg.
Definition: config.cpp:167
bool empty() const
Definition: config.cpp:845
void splice_children(config &src, config_key_type key)
Moves all the children with tag key from src to this.
Definition: config.cpp:577
optional_config_impl< config > optional_child(config_key_type key, int n=0)
Equivalent to mandatory_child, but returns an empty optional if the nth child was not found.
Definition: config.cpp:380
config & add_child(config_key_type key)
Definition: config.cpp:436
virtual void play_slice()
void add_chat_message(const std::time_t &time, const std::string &speaker, int side, const std::string &msg, events::chat_handler::MESSAGE_TYPE type, bool bell)
int village_owner(const map_location &loc) const
Given the location of a village, will return the 1-based number of the team that currently owns it,...
virtual const unit_map & units() const =0
Sort-of-Singleton that many classes, both GUI and non-GUI, use to access the game data.
Definition: display.hpp:96
const team & viewing_team() const
Definition: display.cpp:335
void remove_overlay(const map_location &loc)
remove_overlay will remove all overlays on a tile.
Definition: display.cpp:131
void recalculate_minimap()
Schedule the minimap for recalculation.
Definition: display.cpp:1474
void remove_single_overlay(const map_location &loc, const std::string &toDelete)
remove_single_overlay will remove a single overlay from a tile
Definition: display.cpp:136
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:2979
double turbo_speed() const
Definition: display.cpp:2023
@ ONSCREEN
Definition: display.hpp:504
@ ONSCREEN_WARP
Definition: display.hpp:504
@ SCROLL
Definition: display.hpp:504
void adjust_color_overlay(int r, int g, int b)
Add r,g,b to the colors for all images displayed on the map.
Definition: display.cpp:398
static double get_zoom_factor()
Returns the current zoom factor.
Definition: display.hpp:269
map_labels & labels()
Definition: display.cpp:2444
void scroll_to_tile(const map_location &loc, SCROLL_TYPE scroll_type=ONSCREEN, bool check_fogged=true, bool force=true)
Scroll such that location loc is on-screen.
Definition: display.cpp:1873
bool view_locked() const
Definition: display.hpp:499
bool set_zoom(bool increase)
Zooms the display in (true) or out (false).
Definition: display.cpp:1701
tod_color get_color_overlay() const
Definition: display.hpp:215
void invalidate_all()
Function to invalidate all tiles.
Definition: display.cpp:2972
rect map_outside_area() const
Returns the available area for a map, this may differ from the above.
Definition: display.cpp:514
void fade_to(const color_t &color, const std::chrono::milliseconds &duration)
Screen fade.
Definition: display.cpp:2120
const display_context & context() const
Definition: display.hpp:192
void add_overlay(const map_location &loc, overlay &&ov)
Functions to add and remove overlays from locations.
Definition: display.cpp:118
const map_location & selected_hex() const
Definition: display.hpp:308
bool show_everything() const
Definition: display.hpp:112
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:110
void reinit_flags_for_team(const team &)
Rebuild the flag list (not team colors) for a single side.
Definition: display.cpp:263
const map_location & mouseover_hex() const
Definition: display.hpp:309
void set_view_locked(bool value)
Sets whether the map view is locked (e.g.
Definition: display.hpp:502
bool scroll(const point &amount, bool force=false)
Scrolls the display by amount pixels.
Definition: display.cpp:1598
void select_hex(const map_location &hex, const bool browse, const bool highlight=true, const bool fire_event=true, const bool force_unhighlight=false)
void set_position(double xpos, double ypos)
void set_alignment(ALIGN align)
void set_lifetime(const std::chrono::milliseconds &lifetime, const std::chrono::milliseconds &fadeout=std::chrono::milliseconds{100})
void set_color(const color_t &color)
void set_clip_rect(const SDL_Rect &r)
void set_bg_color(const color_t &bg_color)
void set_font_size(int font_size)
std::ostringstream wrapper.
Definition: formatter.hpp:40
Game board class.
Definition: game_board.hpp:47
virtual const std::vector< team > & teams() const override
Definition: game_board.hpp:80
team & get_team(int i)
Definition: game_board.hpp:92
unit_map::iterator find_visible_unit(const map_location &loc, const team &current_team, bool see_all=false)
Definition: game_board.cpp:185
virtual const unit_map & units() const override
Definition: game_board.hpp:107
virtual const gamemap & map() const override
Definition: game_board.hpp:97
static game_config_manager * get()
const game_config_view & game_config() const
A class grating read only view to a vector of config objects, viewed as one config with all children ...
const config & find_mandatory_child(config_key_type key, const std::string &name, const std::string &value) const
void clear_variable(const std::string &varname)
Clears attributes config children does nothing if varname is no valid variable name.
Definition: game_data.cpp:118
@ PRELOAD
the preload [event] is fired next phase: PRESTART (normal game), TURN_STARTING_WAITING (reloaded game...
Definition: game_data.hpp:76
@ INITIAL
creating intitial [unit]s, executing toplevel [lua] etc.
Definition: game_data.hpp:73
@ PRESTART
the prestart [event] is fired next phase: START (default), GAME_ENDING
Definition: game_data.hpp:79
@ TURN_PLAYING
The User is controlling the game and invoking actions The game can be saved here.
Definition: game_data.hpp:93
const std::string & get_theme() const
Definition: game_data.hpp:136
variable_access_create get_variable_access_write(const std::string &varname)
returns a variable_access that can be used to change the game variables
Definition: game_data.hpp:51
void set_theme(const std::string &value)
Definition: game_data.hpp:137
void set_allow_end_turn(bool value, const t_string &reason="")
Definition: game_data.hpp:120
variable_access_const get_variable_access_read(const std::string &varname) const
returns a variable_access that cannot be used to change the game variables
Definition: game_data.hpp:45
void display_unit_hex(map_location hex)
Change the unit to be displayed in the sidebar.
void invalidate_unit_after_move(const map_location &src, const map_location &dst)
Same as invalidate_unit() if moving the displayed unit.
virtual void highlight_hex(map_location hex) override
Function to highlight a location.
virtual const map_location & displayed_unit_hex() const override
Virtual functions shadowed in game_display.
void new_turn()
Update lighting settings.
bool maybe_rebuild()
Rebuilds the screen if needs_rebuild(true) was previously called, and resets the flag.
display_chat_manager & get_chat_manager()
void float_label(const map_location &loc, const std::string &text, const color_t &color)
Function to float a label above a tile.
void set_arguments(const config &cfg)
Definition: handlers.hpp:127
The game event manager loads the scenario configuration object, and ensures that events are handled a...
Definition: manager.hpp:45
void add_event_handler_from_wml(const config &handler, game_lua_kernel &lk, bool is_menu_item=false)
Create an event handler from an [event] tag.
Definition: manager.cpp:66
pending_event_handler add_event_handler_from_lua(const std::string &name, const std::string &id, bool repeat=false, double priority=0., bool is_menu_item=false)
Create an empty event handler.
Definition: manager.cpp:106
bool fire_item(const std::string &id, const map_location &hex, game_data &gamedata, filter_context &fc, unit_map &units, bool is_key_hold_repeat=false) const
Fires the menu item with the given id.
Definition: wmi_manager.cpp:79
bool erase(const std::string &id)
Erases the item with the provided id.
Definition: wmi_manager.cpp:53
void set_item(const std::string &id, const vconfig &menu_item)
Updates or creates (as appropriate) the menu item with the given id.
void(* handler)(const queued_event &, const vconfig &)
Definition: action_wml.hpp:44
static const map & registry()
Definition: action_wml.hpp:57
void set_action_canceled()
Sets whether or not wml wants to abort the currently executed user action.
Definition: pump.cpp:363
pump_result_t fire(const std::string &event, const entity_location &loc1=entity_location::null_entity, const entity_location &loc2=entity_location::null_entity, const config &data=config())
Function to fire an event.
Definition: pump.cpp:399
void set_undo_disabled(bool mutated)
[allow_undo] implementation
Definition: pump.cpp:351
int intf_get_mouseover_tile(lua_State *L)
Returns the currently overed tile.
void custom_command(const std::string &, const config &)
int intf_put_unit(lua_State *L)
Places a unit on the map.
int intf_get_label(lua_State *L)
int intf_cancel_action(lua_State *)
int intf_set_floating_label(lua_State *L, bool spawn)
Arg 1: text - string Arg 2: options table.
int intf_replace_schedule(lua_State *l)
Replacing the current time of day schedule.
int intf_select_unit(lua_State *L)
Selects and highlights the given location on the map.
int intf_deselect_hex(lua_State *L)
Deselects any highlighted hex on the map.
std::stack< game_events::queued_event const * > queued_events_
int intf_get_all_vars(lua_State *L)
Gets all the WML variables currently set.
int intf_remove_tile_overlay(lua_State *L)
Removes an overlay from a tile.
int intf_screen_fade(lua_State *L)
int intf_erase_unit(lua_State *L)
Erases a unit from the map.
int intf_get_fog_or_shroud(lua_State *L, bool fog)
int intf_override_shroud(lua_State *L)
Overrides the shroud entirely.
int intf_get_variable(lua_State *L)
Gets a WML variable.
int intf_scroll(lua_State *L)
game_lua_kernel(game_state &, play_controller &, reports &)
int intf_set_sub_achievement(lua_State *L)
Marks a single sub-achievement as completed.
int intf_lock_view(lua_State *L)
Sets whether gamemap scrolling is disabled for the user.
void put_unit_helper(const map_location &loc)
int intf_toggle_shroud(lua_State *L, bool place_shroud)
Toggle shroud on some locations Arg 1: Side number Arg 2: List of locations on which to place/remove ...
int intf_match_side(lua_State *L)
Matches a side against the given filter.
int intf_get_selected_tile(lua_State *L)
Returns the currently selected tile.
int cfun_wml_action(lua_State *L)
Executes its upvalue as a wml action.
virtual void log_error(char const *msg, char const *context="Lua error") override
Error reporting mechanisms, used by virtual methods protected_call and load_string.
int cfun_builtin_effect(lua_State *L)
Applies its upvalue as an effect Arg 1: The unit to apply to Arg 3: The [effect] tag contents Arg 3: ...
int impl_theme_item(lua_State *L, const std::string &name)
Executes its upvalue as a theme item generator.
ai::lua_ai_context * create_lua_ai_context(char const *code, ai::engine_lua *engine)
int intf_has_achievement(lua_State *L)
Returns whether an achievement has been completed.
int intf_gamestate_inspector(lua_State *)
int intf_float_label(lua_State *L)
Floats some text on the map.
tod_manager & tod_man()
int intf_find_reach(lua_State *L)
Finds all the locations reachable by a unit.
bool run_event(const game_events::queued_event &)
Executes the game_events.on_event function.
int intf_end_turn(lua_State *)
int save_wml_event()
Store a WML event in the Lua registry, as a function.
int impl_scenario_dir(lua_State *L)
Get a list of scenario data (__dir metamethod).
int intf_set_achievement(lua_State *L)
Sets an achievement as being completed.
int intf_get_units(lua_State *)
Gets all the units matching a given filter.
int intf_color_adjust(lua_State *L)
int intf_redraw(lua_State *L)
int intf_set_village_owner(lua_State *L)
Sets the owner of a village.
int impl_current_dir(lua_State *L)
Gets a list of date about current point of game (__dir metamethod).
static config preload_config
int intf_find_cost_map(lua_State *L)
Is called with one or more units and builds a cost map.
int intf_get_time_area(lua_State *)
int intf_highlight_hex(lua_State *L)
Highlights the given location on the map.
bool run_wml_event(int ref, const vconfig &args, const game_events::queued_event &ev, bool *out=nullptr)
Run a WML stored in the Lua registry.
int intf_scroll_to_tile(lua_State *L)
Scrolls to given tile.
int intf_get_side(lua_State *L)
int intf_remove_event(lua_State *L)
int intf_is_enemy(lua_State *L)
Returns whether the first side is an enemy of the second one.
static void extract_preload_scripts(const game_config_view &game_config)
int impl_get_terrain_info(lua_State *L)
Gets details about a terrain.
virtual std::string my_name() override
User-visible name of the lua kernel that they are talking to.
int intf_remove_label(lua_State *L)
bool run_wml_action(const std::string &, const vconfig &, const game_events::queued_event &)
Runs a command from an event handler.
int intf_move_floating_label(lua_State *L)
int intf_match_unit(lua_State *L)
Matches a unit against the given filter.
int intf_add_time_area(lua_State *)
Adding new time_areas dynamically with Standard Location Filters.
void clear_wml_event(int ref)
Clear a WML event store in the Lua registry.
int intf_create_animator(lua_State *)
int impl_theme_items_dir(lua_State *L)
Get all available theme_items (__dir metamethod).
int intf_add_event_wml(lua_State *L)
Add a new event handler Arg: A full event specification as a WML config.
int impl_scenario_set(lua_State *L)
Sets some scenario data (__newindex metamethod).
int intf_message(lua_State *L)
Displays a message in the chat window and in the logs.
int intf_set_side_id(lua_State *L)
int intf_view_locked(lua_State *L)
Gets whether gamemap scrolling is disabled for the user.
int intf_get_displayed_unit(lua_State *)
Gets the unit displayed in the sidebar.
bool run_wml_conditional(const std::string &, const vconfig &)
Evaluates a WML conidition.
int intf_get_recall_units(lua_State *L)
Gets the numeric ids of all the units matching a given filter on the recall lists.
int intf_add_label(lua_State *L)
int intf_zoom(lua_State *L)
int intf_log_replay(lua_State *L)
const game_events::queued_event & get_event_info()
int intf_unit_ability(lua_State *L)
Returns true if the unit has the given ability enabled.
void push_builtin_effect()
Registers a function for use as an effect handler.
int impl_run_animation(lua_State *)
std::string apply_effect(const std::string &name, unit &u, const config &cfg, bool need_apply)
game_board & board()
int intf_fire_wml_menu_item(lua_State *L)
Fires a wml menu item.
int intf_clear_menu_item(lua_State *L)
void mouse_over_hex_callback(const map_location &loc)
int impl_theme_items_get(lua_State *L)
Creates a field of the theme_items table and returns it (__index metamethod).
int intf_find_vacant_tile(lua_State *L)
Finds a vacant tile.
play_controller & play_controller_
int impl_schedule_dir(lua_State *L)
const gamemap & map() const
int map_locked_
A value != 0 means that the shouldn't remove any units from the map, usually because we are currently...
static std::vector< config > preload_scripts
int intf_get_unit(lua_State *)
Gets the unit at the given location or with the given id.
int impl_current_get(lua_State *L)
Gets some data about current point of game (__index metamethod).
int impl_schedule_set(lua_State *L)
void select_hex_callback(const map_location &loc)
void set_wml_condition(const std::string &, bool(*)(const vconfig &))
Registers a function for use as a conditional handler.
game_state & game_state_
int impl_end_level_data_set(lua_State *)
std::string synced_state()
converts synced_context::get_synced_state() to a string.
int intf_find_path(lua_State *L)
Finds a path between two locations.
int intf_remove_floating_label(lua_State *L)
void set_wml_action(const std::string &, game_events::wml_action::handler)
Registers a function for use as an action handler.
int intf_put_recall_unit(lua_State *L)
Puts a unit on a recall list.
int intf_teleport(lua_State *L)
Teeleports a unit to a location.
void luaW_push_schedule(lua_State *L, int area_index)
int intf_add_tile_overlay(lua_State *L)
Adds an overlay on a tile.
int intf_play_sound(lua_State *L)
Plays a sound, possibly repeated.
void lua_chat(const std::string &caption, const std::string &msg)
int intf_allow_end_turn(lua_State *)
Allow undo sets the flag saying whether the event has mutated the game to false.
int intf_get_village_owner(lua_State *L)
Gets the side of a village owner.
int intf_get_sides(lua_State *L)
Returns a proxy table array for all sides matching the given SSF.
int intf_has_sub_achievement(lua_State *L)
Returns whether an achievement has been completed.
int intf_create_side(lua_State *L)
void initialize(const config &level)
int intf_add_undo_actions(lua_State *L)
Add undo actions for the current active event Arg 1: Either a table of ActionWML or a function to cal...
void save_game(config &level)
Executes the game_events.on_save function and adds to cfg the returned tags.
int impl_scenario_get(lua_State *L)
Gets some scenario data (__index metamethod).
int intf_remove_time_area(lua_State *)
Removing new time_areas dynamically with Standard Location Filters.
int impl_schedule_len(lua_State *L)
int intf_get_locations(lua_State *L)
Gets all the locations matching a given filter.
int intf_add_event(lua_State *L)
Add a new event handler Arg 1: Table of options.
int intf_simulate_combat(lua_State *L)
Simulates a combat between two units.
int impl_get_terrain_list(lua_State *L)
Gets a list of known terrain codes.
std::vector< team > & teams()
int intf_progress_achievement(lua_State *L)
Progresses the provided achievement.
int intf_match_location(lua_State *L)
Matches a location against the given filter.
int intf_toggle_fog(lua_State *L, const bool clear)
Implements the lifting and resetting of fog via WML.
int intf_extract_unit(lua_State *L)
Extracts a unit from the map or a recall list and gives it to Lua.
int intf_log(lua_State *L)
Logs a message Arg 1: (optional) Logger; "wml" for WML errors or deprecations Arg 2: Message Arg 3: W...
int intf_clear_messages(lua_State *)
Removes all messages from the chat window.
int intf_allow_undo(lua_State *)
Allow undo sets the flag saying whether the event has mutated the game to false.
int impl_schedule_get(lua_State *L)
int intf_set_variable(lua_State *L)
Sets a WML variable.
int impl_theme_items_set(lua_State *L)
Sets a field of the theme_items table (__newindex metamethod).
void set_game_display(game_display *gd)
int intf_set_menu_item(lua_State *L)
ai::lua_ai_action_handler * create_lua_ai_action_handler(char const *code, ai::lua_ai_context &context)
bool mouse_button_callback(const map_location &loc, const std::string &button, const std::string &event)
int intf_get_achievement(lua_State *L)
Returns information on a single achievement, or no data if the achievement is not found.
std::vector< int > get_sides_vector(const vconfig &cfg)
Gets a vector of sides from side= attribute in a given config node.
int intf_fire_event(lua_State *L, const bool by_id)
Fires an event.
int intf_delay(lua_State *L)
Delays engine for a while.
int intf_add_event_simple(lua_State *L)
Add a new event handler Arg 1: Event to handle, as a string or list of strings; or menu item ID if th...
bool run_filter(char const *name, const unit &u)
Runs a script from a unit filter.
int intf_find_vision_range(lua_State *L)
Finds all the locations for which a given unit would remove the fog (if there was fog on the map).
int intf_skip_messages(lua_State *L)
Set whether to skip messages Arg 1 (optional) - boolean.
game_display * game_display_
int cfun_undoable_event(lua_State *L)
Upvalue 1: The event function Upvalue 2: The undo function Arg 1: The event content.
void load_game(const config &level)
Executes the game_events.on_load function and passes to it all the scenario tags not yet handled.
int intf_get_time_of_day(lua_State *L)
Gets time of day information.
game_data & gamedata()
int intf_get_color_adjust(lua_State *L)
int intf_is_skipping_messages(lua_State *L)
Return true if a replay is in progress but the player has chosen to skip it.
void add_side_wml(config cfg)
creates a new side during a game.
Definition: game_state.cpp:430
int next_player_number_
Definition: game_state.hpp:58
const std::unique_ptr< actions::undo_list > undo_stack_
undo_stack_ is never nullptr.
Definition: game_state.hpp:56
game_board board_
Definition: game_state.hpp:44
const std::unique_ptr< game_events::manager > events_manager_
Definition: game_state.hpp:50
game_events::wmi_manager & get_wml_menu_items()
Definition: game_state.cpp:383
tod_manager tod_manager_
Definition: game_state.hpp:45
game_data gamedata_
Definition: game_state.hpp:43
int w() const
Effective map width.
Definition: map.hpp:50
int h() const
Effective map height.
Definition: map.hpp:53
Encapsulates the map of the game.
Definition: map.hpp:172
const std::shared_ptr< terrain_type_data > & tdata() const
Definition: map.hpp:204
virtual void log_error(char const *msg, char const *context="Lua error")
Error reporting mechanisms, used by virtual methods protected_call and load_string.
command_log cmd_log_
lua_State * mState
bool load_string(const std::string &prog, const std::string &name, const error_handler &, bool allow_unsafe=false)
void run_lua_tag(const config &cfg)
Runs a [lua] tag.
Storage for a unit, either owned by the Lua code (ptr != 0), a local variable unit (c_ptr !...
Definition: lua_unit.hpp:81
bool on_map() const
Definition: lua_unit.hpp:100
bool put_map(const map_location &loc)
Definition: lua_unit.cpp:74
int on_recall_list() const
Definition: lua_unit.hpp:102
unit_ptr get_shared() const
Definition: lua_unit.cpp:59
const terrain_label * set_label(const map_location &loc, const t_string &text, const int creator=-1, const std::string &team="", const color_t color=font::NORMAL_COLOR, const bool visible_in_fog=true, const bool visible_in_shroud=false, const bool immutable=false, const std::string &category="", const t_string &tooltip="")
Definition: label.cpp:147
const terrain_label * get_label(const map_location &loc, const std::string &team_name) const
Definition: label.hpp:48
void recalculate_shroud()
Definition: label.cpp:278
void clear_all()
Definition: label.cpp:240
game_classification & get_classification()
events::mouse_handler & get_mouse_handler_base() override
Get a reference to a mouse handler member a derived class uses.
bool is_skipping_story() const
game_state & gamestate()
bool is_skipping_replay() const
std::shared_ptr< wb::manager > get_whiteboard() const
bool is_replay() const
virtual void force_end_turn()=0
game_events::wml_event_pump & pump()
std::set< std::string > & encountered_units()
static prefs & get()
int progress_achievement(const std::string &content_for, const std::string &id, int limit=999999, int max_progress=999999, int amount=0)
Increments the achievement's current progress by amount if it hasn't already been completed.
void set_achievement(const std::string &content_for, const std::string &id)
Marks the specified achievement as completed.
void set_sub_achievement(const std::string &content_for, const std::string &id, const std::string &sub_id)
Marks the specified sub-achievement as completed.
void add_log_data(const std::string &key, const std::string &var)
Definition: replay.cpp:299
config generate_report(const std::string &name, const context &ct, bool only_static=false)
Definition: reports.cpp:1829
void register_generator(const std::string &name, generator *)
Definition: reports.cpp:1823
const std::set< std::string > & report_list()
Definition: reports.cpp:1843
An object to leave the synced context during draw or unsynced wml items when we don’t know whether we...
std::vector< int > get_teams() const
Definition: side_filter.cpp:59
bool match(const team &t) const
static map & registry()
using static function variable instead of static member variable to prevent static initialization fia...
static synced_state get_synced_state()
static bool is_synced()
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:75
game_events::pump_result_t get_village(const map_location &, const int owner_side, game_data *fire_event)
Acquires a village from owner_side.
Definition: team.cpp:426
const std::string & team_name() const
Definition: team.hpp:287
void set_color(const std::string &color)
Definition: team.hpp:249
void set_flag(const std::string &flag)
Definition: team.hpp:295
void lose_village(const map_location &)
Definition: team.cpp:452
bool match(const map_location &loc) const
Definition: filter.hpp:43
void get_locations(std::set< map_location > &locs, bool with_border=false) const
gets all locations on the map that match this filter
Definition: filter.hpp:63
To store label data Class implements logic for rendering.
Definition: label.hpp:111
const std::string & id() const
Definition: terrain.hpp:52
t_translation::terrain_code number() const
Definition: terrain.hpp:66
void replace_schedule(const config &time_cfg)
Replace the time of day schedule.
int number_of_turns() const
const time_of_day get_illuminated_time_of_day(const unit_map &units, const gamemap &map, const map_location &loc, int for_turn=0) const
Returns time of day object for the passed turn at a location.
const std::vector< time_of_day > & times(const map_location &loc=map_location::null_location()) const
std::vector< std::string > get_area_ids() const
const std::set< map_location > & get_area_by_index(int index) const
void remove_time_area(const std::string &id)
Removes a time area from config, making it follow the scenario's normal time-of-day sequence.
void add_time_area(const gamemap &map, const config &cfg)
Adds a new local time area from config, making it follow its own time-of-day sequence.
void replace_local_schedule(const std::vector< time_of_day > &schedule, int area_index, int initial_time=0)
int turn() const
const time_of_day & get_time_of_day(int for_turn=0) const
Returns global time of day for the passed turn.
Definition: tod_manager.hpp:56
const std::set< map_location > & get_area_by_id(const std::string &id) const
std::pair< int, std::string > get_area_on_hex(const map_location &loc) const
void reset_affect_adjacent(const unit_map &units)
Refresh map around unit if has ability with [affect_adjacent/distant] tag.
void wait_for_end() const
Definition: animation.cpp:1437
void add_animation(unit_const_ptr animated_unit, const unit_animation *animation, const map_location &src=map_location::null_location(), bool with_bars=false, const std::string &text="", const color_t text_color={0, 0, 0})
Definition: animation.cpp:1319
void start_animations()
Definition: animation.cpp:1371
void set_all_standing()
Definition: animation.cpp:1498
std::vector< const unit * > all_matches_with_unit(const unit &u) const
Definition: filter.hpp:162
std::vector< const unit * > all_matches_at(const map_location &loc) const
Definition: filter.hpp:158
std::vector< const unit * > all_matches_on_map(const map_location *loc=nullptr, const unit *other_unit=nullptr) const
Definition: filter.cpp:67
Container associating units to locations.
Definition: map.hpp:98
unit_ptr extract(const map_location &loc)
Extracts a unit from the map.
Definition: map.cpp:259
unit_iterator find(std::size_t id)
Definition: map.cpp:302
std::size_t erase(const map_location &l)
Erases the unit at location l, if any.
Definition: map.cpp:289
umap_retval_pair_t insert(const unit_ptr &p)
Inserts the unit pointed to by p into the map.
Definition: map.cpp:135
umap_retval_pair_t move(const map_location &src, const map_location &dst)
Moves a unit from location src to location dst.
Definition: map.cpp:92
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.
Definition: types.cpp:1265
config_array_view traits() const
Definition: types.hpp:408
A single unit type that the player may recruit.
Definition: types.hpp:43
const unit_type & get_variation(const std::string &id) const
Definition: types.cpp:476
bool has_variation(const std::string &variation_id) const
Definition: types.cpp:759
This class represents a single unit of a specific type.
Definition: unit.hpp:133
unit_ptr clone() const
Definition: unit.hpp:221
static void clear_status_caches()
Clear this unit status cache for all units.
Definition: unit.cpp:750
static unit_ptr create(const config &cfg, bool use_traits=false, const vconfig *vcfg=nullptr)
Initializes a unit from a config.
Definition: unit.hpp:201
Additional functionality for a non-const variable_info.
Information on a WML variable.
A variable-expanding proxy for the config class.
Definition: variable.hpp:45
static vconfig unconstructed_vconfig()
This is just a wrapper for the default constructor; it exists for historical reasons and to make it c...
Definition: variable.cpp:152
bool null() const
Definition: variable.hpp:72
vconfig child(const std::string &key) const
Returns a child of *this whose key is key.
Definition: variable.cpp:288
const config & get_config() const
Definition: variable.hpp:75
config get_parsed_config() const
Definition: variable.cpp:176
child_list get_children(const std::string &key) const
Definition: variable.cpp:226
constexpr uint8_t ALPHA_OPAQUE
Definition: color.hpp:47
A component of the AI framework.
Composite AI with turn sequence which is a vector of stages.
Composite AI contexts.
Define conditionals for the game's events mechanism, a.k.a.
Definitions for the interface to Wesnoth Markup Language (WML).
Managing the AIs configuration - headers.
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
Definition: deprecation.cpp:29
LUA AI Support engine - creating specific ai components from config.
Define locations as used by the game's events mechanism.
std::size_t i
Definition: function.cpp:1030
static int intf_modify_ai(lua_State *L, const char *action)
int dispatch(lua_State *L)
static int load_fake_units(lua_State *L, int arg, T &fake_units)
static int impl_animator_get(lua_State *L)
#define SCENARIO_SETTER(name, type)
static int impl_null_callback(lua_State *L)
static int impl_animator_collect(lua_State *L)
#define SCENARIO_GETTER(name, type)
static int intf_unit_resistance(lua_State *L)
Returns unit resistance against a given attack type.
static int intf_synchronize_choice(lua_State *L)
Ensures a value is synchronized among all the clients.
static int luaW_check_schedule(lua_State *L, int idx)
#define SCENARIO_VALID(name)
#define ERR_LUA
static lg::log_domain log_scripting_lua("scripting/lua")
static int intf_add_known_unit(lua_State *L)
Adds a new known unit type to the help system.
static int intf_append_ai(lua_State *L)
static int impl_end_level_data_collect(lua_State *L)
static int impl_mp_settings_len(lua_State *L)
#define CURRENT_GETTER(name, type)
#define READ_ONE_FILTER(key, tag)
luaW_Registry currentReg
#define SCHEDULE_SETTER(name, type)
luaW_Registry scenarioReg
bool(*)(const vconfig &) wml_conditional_handler
static int cfun_exec_candidate_action(lua_State *L)
#define ERR_WML
static int intf_run_event_wml(lua_State *L)
static int intf_eval_conditional(lua_State *L)
Evaluates a boolean WML conditional.
luaW_Registry & gameConfigReg()
static int impl_clear_animation(lua_State *L)
const char * labelKey
static int intf_advance_unit(lua_State *L)
Advances a unit if the unit has enough xp.
static int impl_add_animation(lua_State *L)
static int intf_debug_ai(lua_State *L)
Debug access to the ai tables.
#define GAME_CONFIG_SIMPLE_SETTER(name)
static int intf_unit_movement_cost(lua_State *L)
Returns unit movement cost on a given terrain.
#define LOG_LUA
static int intf_add_modification(lua_State *L)
Adds a modification to a unit.
#define SCHEDULE_GETTER(name, type)
static void luaW_pushsimdata(lua_State *L, const combatant &cmb)
Puts a table at the top of the stack with some combat result.
#define CALLBACK_GETTER(name, type)
static std::string read_event_name(lua_State *L, int idx)
static int intf_remove_modifications(lua_State *L)
Removes modifications from a unit.
int dispatch2(lua_State *L)
static int intf_copy_unit(lua_State *L)
Copies a unit.
static void push_component(lua_State *L, ai::component *c, const std::string &ct="")
#define WRN_LUA
static int impl_mp_settings_get(lua_State *L)
static int cfun_exec_stage(lua_State *L)
static int impl_game_events_get(lua_State *L)
static int intf_modify_ai_old(lua_State *L)
Lua frontend to the modify_ai functionality.
static int intf_get_resource(lua_State *L)
Gets a table for an resource tag.
static const char animatorKey[]
static bool is_handled_file_tag(std::string_view s)
These are the child tags of [scenario] (and the like) that are handled elsewhere (in the C++ code).
static int intf_synchronize_choices(lua_State *L)
Ensures a value is synchronized among all the clients.
static int intf_switch_ai(lua_State *L)
static int intf_create_unit(lua_State *L)
Creates a unit from its WML description.
static int intf_transform_unit(lua_State *L)
Changes a unit to the given unit type.
static int impl_floating_label_getmethod(lua_State *L)
static int intf_get_viewing_side(lua_State *L)
Gets currently viewing side.
static auto & dummy
int(game_lua_kernel::* member_callback)(lua_State *)
int(game_lua_kernel::* member_callback2)(lua_State *, bool)
static int intf_invoke_synced_command(lua_State *L)
static void luaW_push_tod(lua_State *L, const time_of_day &tod)
#define DBG_LUA
static int intf_do_unsynced(lua_State *L)
Calls a function in an unsynced context (this specially means that all random calls used by that func...
luaW_Registry scheduleReg
static void luaW_pushsimweapon(lua_State *L, const battle_context_unit_stats &bcustats)
Puts a table at the top of the stack with information about the combatants' weapons.
static int intf_handle_user_interact(lua_State *)
static int intf_unit_jamming_cost(lua_State *L)
Returns unit jamming cost on a given terrain.
static int impl_end_level_data_get(lua_State *L)
static lg::log_domain log_wml("wml")
static int intf_unit_defense(lua_State *L)
Returns unit defense on a given terrain.
static int intf_get_era(lua_State *L)
Gets a table for an era tag.
static bool impl_get_callback(lua_State *L, const std::string &name)
#define SCHEDULE_VALID(name)
static int cfun_wml_condition(lua_State *L)
Executes its upvalue as a wml condition and returns the result.
luaW_Registry callbacksReg
static int * luaW_check_floating_label(lua_State *L, int idx)
static int impl_game_events_dir(lua_State *L)
static int intf_unit_vision_cost(lua_State *L)
Returns unit vision cost on a given terrain.
static std::string _(const char *str)
Definition: gettext.hpp:97
bool get_ability_bool(const std::string &tag_name, const map_location &loc) const
Checks whether this unit currently possesses or is affected by a given ability.
Definition: abilities.cpp:187
const std::string & id() const
Gets this unit's id.
Definition: unit.hpp:380
int side() const
The side this unit belongs to.
Definition: unit.hpp:343
void advance_to(const unit_type &t, bool use_traits=false)
Advances this unit to another type.
Definition: unit.cpp:979
int defense_modifier(const t_translation::terrain_code &terrain) const
The unit's defense on a given terrain.
Definition: unit.cpp:1726
int resistance_against(const std::string &damage_name, bool attacker, const map_location &loc, const_attack_ptr weapon=nullptr, const const_attack_ptr &opp_weapon=nullptr) const
The unit's resistance against a given damage type.
Definition: unit.cpp:1790
unit_animation_component & anim_comp() const
Definition: unit.hpp:1623
void add_modification(const std::string &type, const config &modification, bool no_add=false)
Add a new modification to the unit.
Definition: unit.cpp:2437
static const std::set< std::string > builtin_effects
Definition: unit.hpp:1596
std::string describe_builtin_effect(const std::string &type, const config &effect)
Construct a string describing a built-in effect.
Definition: unit.cpp:1968
config & get_modifications()
Get the raw modifications.
Definition: unit.hpp:1535
void expire_modifications(const std::string &duration)
Clears those modifications whose duration has expired.
Definition: unit.cpp:1289
void apply_builtin_effect(const std::string &type, const config &effect)
Apply a builtin effect to the unit.
Definition: unit.cpp:2033
const map_location & get_location() const
The current map location this unit is at.
Definition: unit.hpp:1413
int movement_cost(const t_translation::terrain_code &terrain) const
Get the unit's movement cost on a particular terrain.
Definition: unit.hpp:1496
int vision_cost(const t_translation::terrain_code &terrain) const
Get the unit's vision cost on a particular terrain.
Definition: unit.hpp:1506
int jamming_cost(const t_translation::terrain_code &terrain) const
Get the unit's jamming cost on a particular terrain.
Definition: unit.hpp:1516
std::string label
What to show in the filter's drop-down list.
Definition: manager.cpp:201
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:199
Define the handlers for the game's events mechanism.
bool tiles_adjacent(const map_location &a, const map_location &b)
Function which tells if two locations are adjacent.
Definition: location.cpp:507
Standard logging facilities (interface).
#define log_scope(description)
Definition: log.hpp:275
std::decay_t< T > lua_check(lua_State *L, int n)
Definition: push_check.hpp:411
void luaW_pushconfig(lua_State *L, const config &cfg)
Converts a config object to a Lua table pushed at the top of the stack.
Definition: lua_common.cpp:905
bool luaW_iststring(lua_State *L, int index)
Definition: lua_common.cpp:643
void luaW_push_namedtuple(lua_State *L, const std::vector< std::string > &names)
Push an empty "named tuple" onto the stack.
Definition: lua_common.cpp:763
config luaW_checkconfig(lua_State *L, int index)
Converts an optional table or vconfig to a config object.
void luaW_pushlocation(lua_State *L, const map_location &ml)
Converts a map location object to a Lua table pushed at the top of the stack.
Definition: lua_common.cpp:808
std::set< map_location > luaW_check_locationset(lua_State *L, int idx)
Converts a table of integer pairs to a set of map location objects.
Definition: lua_common.cpp:888
void luaW_pushtstring(lua_State *L, const t_string &v)
Pushes a t_string on the top of the stack.
Definition: lua_common.cpp:545
bool luaW_pushvariable(lua_State *L, variable_access_const &v)
bool luaW_tovconfig(lua_State *L, int index, vconfig &vcfg)
Gets an optional vconfig from either a table or a userdata.
void luaW_pushvconfig(lua_State *L, const vconfig &cfg)
Pushes a vconfig on the top of the stack.
Definition: lua_common.cpp:539
bool luaW_toboolean(lua_State *L, int n)
int luaW_type_error(lua_State *L, int narg, const char *tname)
std::string_view luaW_tostring(lua_State *L, int index)
bool luaW_getmetafield(lua_State *L, int idx, const char *key)
Like luaL_getmetafield, but returns false if key is an empty string or begins with two underscores.
Definition: lua_common.cpp:524
bool luaW_totstring(lua_State *L, int index, t_string &str)
Converts a scalar to a translatable string.
Definition: lua_common.cpp:610
bool luaW_tableget(lua_State *L, int index, const char *key)
bool luaW_checkvariable(lua_State *L, variable_access_create &v, int n)
bool luaW_pcall(lua_State *L, int nArgs, int nRets, bool allow_wml_error)
Calls a Lua function stored below its nArgs arguments at the top of the stack.
bool luaW_toconfig(lua_State *L, int index, config &cfg)
Converts an optional table or vconfig to a config object.
Definition: lua_common.cpp:934
int luaW_push_locationset(lua_State *L, const std::set< map_location > &locs)
Converts a set of map locations to a Lua table pushed at the top of the stack.
Definition: lua_common.cpp:876
vconfig luaW_checkvconfig(lua_State *L, int index, bool allow_missing)
Gets an optional vconfig from either a table or a userdata.
bool luaW_tolocation(lua_State *L, int index, map_location &loc)
Converts an optional table or pair of integers to a map location object.
Definition: lua_common.cpp:819
map_location luaW_checklocation(lua_State *L, int index)
Converts an optional table or pair of integers to a map location object.
Definition: lua_common.cpp:868
bool luaW_getglobal(lua_State *L, const std::vector< std::string > &path)
Pushes the value found by following the variadic names (char *), if the value is not nil.
t_string luaW_checktstring(lua_State *L, int index)
Converts a scalar to a translatable string.
Definition: lua_common.cpp:635
#define return_cstring_attrib(name, accessor)
Definition: lua_common.hpp:256
#define return_string_attrib(name, accessor)
Definition: lua_common.hpp:266
#define return_cfgref_attrib(name, accessor)
Definition: lua_common.hpp:319
#define return_int_attrib(name, accessor)
Definition: lua_common.hpp:277
#define modify_bool_attrib(name, accessor)
Definition: lua_common.hpp:414
#define return_bool_attrib(name, accessor)
Definition: lua_common.hpp:297
#define return_cfg_attrib(name, accessor)
Definition: lua_common.hpp:307
#define modify_string_attrib(name, accessor)
Definition: lua_common.hpp:357
#define return_string_attrib_deprecated(name, prefix, level, version, msg, accessor)
Definition: lua_common.hpp:274
#define GAME_CONFIG_GETTER(name, type, kernel_type)
void luaW_pushracetable(lua_State *L)
Definition: lua_race.cpp:130
void luaW_pushteam(lua_State *L, team &tm)
Create a full userdata containing a pointer to the team.
Definition: lua_team.cpp:536
team * luaW_toteam(lua_State *L, int idx)
Test if the top stack element is a team, and if so, return it.
Definition: lua_team.cpp:562
team & luaW_checkteam(lua_State *L, int idx)
Test if the top stack element is a team, and if not, error.
Definition: lua_team.cpp:543
std::set< map_location > location_set
int intf_terrain_mask(lua_State *L)
Replaces part of the map.
int intf_on_border(lua_State *L)
int intf_on_board(lua_State *L)
int intf_terrainmap_iter(lua_State *L)
int intf_terrainmap_get(lua_State *L)
int intf_replace_if_failed(lua_State *L)
lua_unit * luaW_checkunit_ref(lua_State *L, int index)
Similar to luaW_checkunit but returns a lua_unit; use this if you need to handle map and recall units...
Definition: lua_unit.cpp:199
unit & luaW_checkunit(lua_State *L, int index, bool only_on_map)
Converts a Lua value to a unit pointer.
Definition: lua_unit.cpp:191
bool luaW_isunit(lua_State *L, int index)
Test if a Lua value is a unit.
Definition: lua_unit.cpp:113
unit_ptr luaW_checkunit_ptr(lua_State *L, int index, bool only_on_map)
Similar to luaW_checkunit but returns a unit_ptr; use this instead of luaW_checkunit when using an ap...
Definition: lua_unit.cpp:183
lua_unit * luaW_pushlocalunit(lua_State *L, unit &u)
Pushes a private unit on the stack.
Definition: lua_unit.cpp:212
unit * luaW_tounit(lua_State *L, int index, bool only_on_map)
Converts a Lua value to a unit pointer.
Definition: lua_unit.cpp:142
lua_unit * luaW_pushunit(lua_State *L, Args... args)
Definition: lua_unit.hpp:116
int intf_create_attack(lua_State *L)
const_attack_ptr luaW_toweapon(lua_State *L, int idx)
void luaW_pushweapon(lua_State *L, const attack_ptr &weapon)
std::map< std::string, config > traits_map
bool clear_shroud(int side, bool reset_fog, bool fire_events)
Function that will clear shroud (and fog) based on current unit positions.
Definition: vision.cpp:746
game_events::pump_result_t get_village(const map_location &loc, int side, bool *action_timebonus, bool fire_event)
Makes it so the village at the given location is owned by the given side.
Definition: move.cpp:221
void create_jamming_map(std::map< map_location, int > &jamming, const team &view_team)
Helper function that creates the map of enemy anti-vision that's needed when creating a pathfinding::...
Definition: vision.cpp:47
void clear()
Clear the current render target.
Definition: draw.cpp:42
const color_t LABEL_COLOR
int add_floating_label(const floating_label &flabel)
add a label floating on the screen above everything else.
const int SIZE_SMALL
Definition: constants.cpp:24
void remove_floating_label(int handle, const std::chrono::milliseconds &fadeout)
removes the floating label given by 'handle' from the screen
void move_floating_label(int handle, double xmove, double ymove)
moves the floating label given by 'handle' by (xmove,ymove)
@ CENTER_ALIGN
Game configuration data as global variables.
Definition: build_info.cpp:61
std::string path
Definition: filesystem.cpp:93
int rest_heal_amount
Definition: game_config.cpp:48
int village_income
Definition: game_config.cpp:41
const bool & debug
Definition: game_config.cpp:95
int kill_experience
Definition: game_config.cpp:44
unsigned int tile_size
Definition: game_config.cpp:55
int combat_experience
Definition: game_config.cpp:45
void load_config(const config &v)
int village_support
Definition: game_config.cpp:42
bool variable_matches(const vconfig &values)
bool have_location(const vconfig &cfg)
bool have_unit(const vconfig &cfg)
bool conditional_passed(const vconfig &cond)
bool fire_event(const ui_event event, const std::vector< std::pair< widget *, ui_event >> &event_chain, widget *dispatcher, widget *w, F &&... params)
Helper function for fire_event.
std::shared_ptr< halo_record > handle
Definition: halo.hpp:31
logger & err()
Definition: log.cpp:306
std::stringstream & log_to_chat()
Use this to show WML errors in the ingame chat.
Definition: log.cpp:517
logger & info()
Definition: log.cpp:318
std::string register_table(lua_State *L)
Definition: lua_audio.cpp:509
std::string register_vconfig_metatable(lua_State *L)
Adds the vconfig metatable.
Definition: lua_common.cpp:473
int intf_tovconfig(lua_State *L)
Creates a vconfig containing the WML table.
Definition: lua_common.cpp:411
void set_functions(lua_State *L, const std::vector< lua_cpp::Reg > &functions)
Analogous to lua_setfuncs, it registers a collection of function wrapper objects into a table,...
void push_closure(lua_State *L, const lua_function &f, int nup)
Pushes a closure which retains a std::function object as its first up-value.
int intf_show_recall_dialog(lua_State *L)
Definition: lua_gui2.cpp:318
int intf_show_recruit_dialog(lua_State *L)
Definition: lua_gui2.cpp:262
int show_gamestate_inspector(const std::string &name, const game_data &data, const game_state &state)
Definition: lua_gui2.cpp:256
std::string register_metatable(lua_State *L)
Definition: lua_race.cpp:104
std::string register_metatable(lua_State *L)
Definition: lua_team.cpp:500
std::string register_metatables(lua_State *L)
std::string register_metatable(lua_State *L)
std::string register_table(lua_State *L)
std::string register_metatables(lua_State *L)
Definition: lua_unit.cpp:942
std::string register_attacks_metatables(lua_State *L)
std::string tag(std::string_view tag, Args &&... data)
Wraps the given data in the specified tag.
Definition: markup.hpp:45
config get_user_choice(const std::string &name, const user_choice &uch, int side=0)
std::map< int, config > get_user_choice_multiple_sides(const std::string &name, const user_choice &uch, std::set< int > sides)
Performs a choice for multiple sides for WML events.
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)
@ VACANT_ANY
Definition: pathfind.hpp:39
const teleport_map get_teleport_locations(const unit &u, const team &viewing_team, bool see_all, bool ignore_units, bool check_vision)
Definition: teleport.cpp:251
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,...
Definition: pathfind.cpp:54
Unit and team statistics.
game_board * gameboard
Definition: resources.cpp:20
fake_unit_manager * fake_units
Definition: resources.cpp:30
replay * recorder
Definition: resources.cpp:28
play_controller * controller
Definition: resources.cpp:21
std::shared_ptr< wb::manager > whiteboard
Definition: resources.cpp:33
Contains the general settings which have a default.
void play_sound(const std::string &files, channel_group group, unsigned int repeats)
Definition: sound.cpp:1043
@ SOUND_FX
Definition: sound.hpp:34
terrain_code read_terrain_code(std::string_view str, const ter_layer filler)
Reads a single terrain from a string.
std::string write_terrain_code(const terrain_code &tcode)
Writes a single terrain code to a string.
const terrain_code NONE_TERRAIN
Definition: translation.hpp:58
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.
Definition: udisplay.cpp:509
std::size_t size(std::string_view str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
std::size_t index(std::string_view str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:70
constexpr auto transform
Definition: ranges.hpp:41
constexpr auto filter
Definition: ranges.hpp:38
@ STRIP_SPACES
REMOVE_EMPTY: remove empty elements.
int stoi(std::string_view str)
Same interface as std::stoi and meant as a drop in replacement, except:
Definition: charconv.hpp:154
std::string join_map(const T &v, const std::string &major=",", const std::string &minor=":")
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
std::vector< std::string > split(const config_attribute_value &val)
auto * find(Container &container, const Value &value)
Convenience wrapper for using find on a container without needing to comare to end()
Definition: general.hpp:140
bool headless()
The game is running headless.
Definition: video.cpp:139
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
This module contains various pathfinding functions and utilities.
std::string_view data
Definition: picture.cpp:178
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
std::shared_ptr< const attack_type > const_attack_ptr
Definition: ptr.hpp:34
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26
Define the game's event mechanism.
void lua_push(lua_State *L, const T &val)
Definition: push_check.hpp:425
std::decay_t< T > luaW_table_get_def(lua_State *L, int index, std::string_view k, const T &def)
returns t[k] where k is the table at index index and k is k or def if it is not convertible to the co...
Definition: push_check.hpp:435
void luaW_table_set(lua_State *L, int index, std::string_view k, const T &value)
Definition: push_check.hpp:457
candidate action framework
Replay control code.
#define ON_SCOPE_EXIT(...)
Run some arbitrary code (a lambda) when the current scope exits The lambda body follows this header,...
Definition: scope_exit.hpp:43
rect dst
Location on the final composed sheet.
rect src
Non-transparent portion of the surface to compose.
Composite AI stages.
A set of achievements tied to a particular content.
Represents a single achievement and its data.
std::string icon_completed_
The icon of the achievement to show on the UI if the achievement is completed.
t_string name_completed_
The name of the achievement to show on the UI if the achievement is completed.
t_string description_completed_
The name of the achievement to show on the UI if the achievement is completed.
bool achieved_
Whether the achievement has been completed.
std::string id_
The ID of the achievement.
int max_progress_
When the achievement's current progress matches or equals this value, then it should be marked as com...
std::string sound_path_
The path to a sound to play when an achievement is completed.
int current_progress_
The current progress value of the achievement.
std::vector< sub_achievement > sub_achievements_
The list of distinct sub-achievements for this achievement.
advances the unit at loc if it has enough experience, maximum 20 times.
Definition: advancement.hpp:39
advance_unit_params & animate(bool value)
Definition: advancement.hpp:43
advance_unit_params & fire_events(bool value)
Definition: advancement.hpp:42
Structure describing the statistics of a unit involved in the battle.
Definition: attack.hpp:51
bool slows
Attack slows opponent when it hits.
Definition: attack.hpp:57
unsigned int num_blows
Effective number of blows, takes swarm into account.
Definition: attack.hpp:76
std::string plague_type
The plague type used by the attack, if any.
Definition: attack.hpp:80
bool petrifies
Attack petrifies opponent when it hits.
Definition: attack.hpp:59
int drain_percent
Percentage of damage recovered as health.
Definition: attack.hpp:74
bool drains
Attack drains opponent when it hits.
Definition: attack.hpp:58
const_attack_ptr weapon
The weapon used by the unit to attack the opponent, or nullptr if there is none.
Definition: attack.hpp:52
unsigned int rounds
Berserk special can force us to fight more than one round.
Definition: attack.hpp:68
int damage
Effective damage of the weapon (all factors accounted for).
Definition: attack.hpp:72
bool poisons
Attack poisons opponent when it hits.
Definition: attack.hpp:61
unsigned int chance_to_hit
Effective chance to hit as a percentage (all factors accounted for).
Definition: attack.hpp:71
int drain_constant
Base HP drained regardless of damage dealt.
Definition: attack.hpp:75
bool firststrike
Attack has firststrike special.
Definition: attack.hpp:63
int attack_num
Index into unit->attacks() or -1 for none.
Definition: attack.hpp:53
bool plagues
Attack turns opponent into a zombie when fatal.
Definition: attack.hpp:60
game_lua_kernel & ref
callbacks_tag(game_lua_kernel &k)
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:61
static color_t from_hex_string(std::string_view c)
Creates a new color_t object from a string variable in hex format.
Definition: color.cpp:64
static color_t from_rgb_string(std::string_view c)
Creates a new opaque color_t object from a string variable in "R,G,B" format.
Definition: color.cpp:44
All combat-related info.
double slowed
Resulting chance we are slowed.
std::vector< double > hp_dist
Resulting probability distribution (might be not as large as max_hp)
double poisoned
Resulting chance we are poisoned.
double average_hp(unsigned int healing=0) const
What's the average hp (weighted average of hp_dist).
double untouched
Resulting chance we were not hit by this opponent (important if it poisons)
void push_schedule(lua_State *L) const
auto & pc() const
current_tag(game_lua_kernel &k)
auto & gd() const
game_lua_kernel & ref
auto & ev() const
auto ss() const
Additional information on the game outcome which can be provided by WML.
Error used for any general game error, e.g.
Definition: game_errors.hpp:47
game_config_glk_tag(lua_kernel_base &k)
game_lua_kernel & ref
const map_location & filter_loc() const
Represents a single filter condition on an event.
Definition: handlers.hpp:38
entity_location loc1
Definition: pump.hpp:65
entity_location loc2
Definition: pump.hpp:66
std::string name
Definition: pump.hpp:63
Holds a lookup table for members of one type of object.
int dir(lua_State *L)
Implement __dir metamethod.
int set(lua_State *L)
Implement __newindex metamethod.
int get(lua_State *L)
Implement __index metamethod.
void serialize(config &cfg) const override
Serializes the filter into a config, if possible.
game_lua_kernel & lk
lua_event_filter(game_lua_kernel &lk, int idx, const config &args)
bool operator()(const game_events::queued_event &event_info) const override
Runs the filter and returns whether it passes on the given event.
static game_lua_kernel & get(lua_State *L, int)
static game_lua_kernel & get(lua_State *L, int)
static scenario_tag get(lua_State *L, int)
static schedule_tag get(lua_State *L, int n)
Cost function object relying on a Lua function.
Encapsulates the map of the game.
Definition: location.hpp:45
void set_wml_y(int v)
Definition: location.hpp:221
bool valid() const
Definition: location.hpp:110
int wml_y() const
Definition: location.hpp:218
void set_wml_x(int v)
Definition: location.hpp:220
static const map_location & null_location()
Definition: location.hpp:102
int wml_x() const
Definition: location.hpp:217
game_lua_kernel * kernel_
map_locker(game_lua_kernel *kernel)
Interface for querying local choices.
virtual config query_user(int side) const =0
virtual std::string description() const
virtual bool is_visible() const
whether the choice is visible for the user like an advancement choice a non-visible choice is for exa...
virtual config random_choice(int side) const =0
Structure which uses find_routes() to build a cost map This maps each hex to a the movements a unit w...
Definition: pathfind.hpp:268
void add_unit(const unit &u, bool use_max_moves=true)
Adds a units cost map to cost_map (increments the elements in cost_map)
Definition: pathfind.cpp:919
std::pair< int, int > get_pair_at(map_location loc) const
Accessor for the cost/reach-amount pairs.
Definition: pathfind.cpp:970
Object which contains all the possible locations a unit can move to, with associated best routes to t...
Definition: pathfind.hpp:73
dest_vect destinations
Definition: pathfind.hpp:101
Structure which holds a single route between one location and another.
Definition: pathfind.hpp:133
std::vector< map_location > steps
Definition: pathfind.hpp:135
int move_cost
Movement cost for reaching the end of the route.
Definition: pathfind.hpp:137
A refinement of paths for use when calculating vision.
Definition: pathfind.hpp:108
std::set< map_location > edges
The edges are the non-destination hexes bordering the destinations.
Definition: pathfind.hpp:117
Holds a 2D point.
Definition: point.hpp:25
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:49
auto & cls() const
auto & gamedata() const
auto & tod_man() const
game_lua_kernel & ref
scenario_tag(game_lua_kernel &k)
auto end_level_set() const
auto & pc() const
auto & tod_man() const
schedule_tag(game_lua_kernel &k)
game_lua_kernel & ref
static std::string get_string(enum_type key)
Converts a enum to its string equivalent.
Definition: enum_base.hpp:46
static constexpr utils::optional< enum_type > get_enum(const std::string_view value)
Converts a string into its enum equivalent.
Definition: enum_base.hpp:57
Represents a distinct sub-achievement within another achievement.
std::string id_
The ID of the sub-achievement.
bool achieved_
Whether the sub-achievement has been completed.
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:49
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:57
int bonus_modified
Definition: time_of_day.hpp:84
std::string id
Definition: time_of_day.hpp:90
tod_color color
The color modifications that should be made to the game board to reflect the time of day.
int lawful_bonus
The % bonus lawful units receive.
Definition: time_of_day.hpp:83
t_string name
Definition: time_of_day.hpp:88
std::string image
The image to be displayed in the game status.
Definition: time_of_day.hpp:87
std::string sounds
List of "ambient" sounds associated with this time_of_day, Played at the beginning of turn.
std::string image_mask
The image that is to be laid over all images while this time of day lasts.
Definition: time_of_day.hpp:96
bool valid() const
Definition: map.hpp:273
pointer get_shared_ptr() const
This is exactly the same as operator-> but it's slightly more readable, and can replace &*iter syntax...
Definition: map.hpp:217
mock_char c
mock_party p
static map_location::direction n
static map_location::direction s
unit_type_data unit_types
Definition: types.cpp:1504
Display units performing various actions: moving, attacking, and dying.
Various functions that implement the undoing (and redoing) of in-game commands.
Various functions implementing vision (through fog of war and shroud).
#define d
#define e
#define h
#define b