37#define LOG_LUA LOG_STREAM(info, log_scripting_lua)
38#define ERR_LUA LOG_STREAM(err, log_scripting_lua)
56 if (!ui.
valid())
return nullptr;
81 uid = unit_it->underlying_id();
83 ERR_LUA <<
"Could not move unit " <<
ptr->underlying_id() <<
" onto map location " <<
loc;
93 ERR_LUA <<
"Could not find unit " <<
uid <<
" on recall list of side " <<
side;
106 ERR_LUA <<
"Could not find unit " <<
uid <<
" on the map";
115 return luaL_testudata(L, index,
getunitKey) !=
nullptr;
133 if(only_on_map && !lu->on_map()) {
159 return lu->get_shared();
175 luaL_argerror(L, index,
"unit not found");
178 luaL_argerror(L, index,
"unit not found on map");
188 return lu->get_shared();
225 u->lua_unit::~lua_unit();
237 lua_pushboolean(L, equal);
248 std::ostringstream str;
253 }
else if(!u->
id().empty()) {
254 str << u->
id() <<
" ";
259 if(
int side = lu->on_recall_list()) {
260 str <<
"at (side " << side <<
" recall list)";
274#define UNIT_GETTER(name, type) LATTR_GETTER(name, type, unit, u)
275#define UNIT_SETTER(name, type) LATTR_SETTER(name, type, unit, u)
296 (*lu)->set_location(
dst);
298 unit& u = *lu->get();
313 std::string err_msg =
formatter() <<
"destination hex not on map (excluding border): " <<
dst;
314 return void(luaL_argerror(L, 2, err_msg.c_str()));
320 unit_iterator->anim_comp().set_standing();
327 const unit* pu = lu->get();
329 return utils::nullopt;
331 using namespace std::literals;
334 }
else if(lu->on_recall_list()) {
345 if(!lu->get())
return;
352 return u.get_location().wml_y();
356 if(!lu->get())
return;
363 return u.get_location();
367 if(!lu->get())
return;
392 if(!lu->get())
return;
393 if(lu->on_map()) luaL_argerror(L, 3,
"can't modify id of on-map unit");
394 (*lu)->set_id(
value);
402 return u.effect_image_mods();
414 return u.image_ellipse();
418 u.set_image_ellipse(
value);
422 return u.image_halo();
426 u.set_image_halo(
value);
430 return u.hitpoints();
434 u.set_hitpoints(
value);
438 return u.max_hitpoints();
442 u.set_max_hitpoints(
value);
446 return u.experience();
450 u.set_experience(
value);
454 return u.max_experience();
458 u.set_max_experience(
value);
462 return u.recall_cost();
466 u.set_recall_cost(
value);
470 return u.movement_left();
474 u.set_movement(
value);
478 return u.total_movement();
482 u.set_total_movement(
value);
486 return u.max_attacks();
490 u.set_max_attacks(
value);
494 return u.attacks_left();
498 u.set_attacks(
value);
518 return u.unit_description();
522 u.set_unit_description(
value);
526 return u.can_recruit();
530 u.set_can_recruit(
value);
534 return !u.unrenamable();
538 u.set_unrenamable(!
value);
558 u.set_recruits(
value);
562 return u.advances_to();
566 u.set_advances_to(
value);
575 if(!alignment) luaL_argerror(L,
value.index,
"invalid unit alignment");
576 u.set_alignment(*alignment);
583 if(
int* v = utils::get_if<int>(&upkeep)) {
593 if(lua_isnumber(L,
value.index)) {
594 u.set_upkeep(
static_cast<int>(luaL_checkinteger(L, 3)));
597 auto v = lua_check<std::string_view>(L,
value.index);
598 if(v ==
"loyal" || v ==
"free") {
600 }
else if(v ==
"full") {
603 std::string err_msg =
"unknown upkeep value of unit: ";
605 luaL_argerror(L, 2, err_msg.c_str());
611 return u.modification_advancements();
615 u.set_advancements(
value);
623 return u.get_traits_list();
628 return u.get_ability_id_list();
632 return u.get_ability_id_list();
637 lua_createtable(L, 1, 0);
639 lua_rawseti(L, -2, 1);
646 lua_createtable(L, 1, 0);
648 lua_rawseti(L, -2, 1);
661 return u.incapacitated();
665 return u.anim_comp().get_flags();
669 return u.recall_filter();
673 u.set_recall_filter(
value);
677 return u.get_hidden();
689 u.set_resting(
value);
693 return u.is_flying();
697 return u.is_fearless();
701 return u.is_healthy();
705 return u.get_emit_zoc();
709 u.set_emit_zoc(
value);
721 return u.race()->id();
729 return u.variation();
733 return u.undead_variation();
737 u.set_undead_variation(
value);
749 return u.big_profile() == u.absolute_image()
750 ? u.absolute_image() + u.image_mods() +
"~SCALE_SHARP(144,144)"
755 u.set_big_profile(
value);
761 u.get_location().write(
cfg);
806 if(!lua_istable(L, 1)) {
809 lua_rawgeti(L, 1, 1);
812 return luaL_argerror(L, 1,
"unknown unit");
814 char const *m = luaL_checkstring(L, 2);
827 if(!lua_istable(L, 1)) {
830 lua_rawgeti(L, 1, 1);
833 return luaL_argerror(L, 1,
"unknown unit");
835 char const *m = luaL_checkstring(L, 2);
846 if(!lua_istable(L, 1)) {
849 lua_rawgeti(L, 1, 1);
852 return luaL_argerror(L, 1,
"unknown unit");
854 std::vector<std::string> states;
874 if(!lua_istable(L, 1)) {
877 lua_rawgeti(L, 1, 1);
880 return luaL_argerror(L, 2,
"unknown unit");
882 char const *m = luaL_checkstring(L, 2);
897 if(!lua_istable(L, 1)) {
900 lua_rawgeti(L, 1, 1);
903 return luaL_argerror(L, 2,
"unknown unit");
905 char const *m = luaL_checkstring(L, 2);
908 if(lua_isnoneornil(L, 3)) {
925 if(!lua_istable(L, 1)) {
928 lua_rawgeti(L, 1, 1);
931 return luaL_argerror(L, 2,
"unknown unit");
934 std::vector<std::string> variables;
937 variables.push_back(attr.first);
940 variables.push_back(key);
949 std::ostringstream cmd_out;
952 cmd_out <<
"Adding getunit metatable...\n";
956 lua_setfield(L, -2,
"__gc");
958 lua_setfield(L, -2,
"__eq");
960 lua_setfield(L, -2,
"__tostring");
962 lua_setfield(L, -2,
"__index");
964 lua_setfield(L, -2,
"__newindex");
966 lua_setfield(L, -2,
"__dir");
967 lua_pushstring(L,
"unit");
968 lua_setfield(L, -2,
"__metatable");
971 cmd_out <<
"Adding unit status metatable...\n";
975 lua_setfield(L, -2,
"__index");
977 lua_setfield(L, -2,
"__newindex");
979 lua_setfield(L, -2,
"__dir");
980 lua_pushstring(L,
"unit status");
981 lua_setfield(L, -2,
"__metatable");
984 cmd_out <<
"Adding unit variables metatable...\n";
988 lua_setfield(L, -2,
"__index");
990 lua_setfield(L, -2,
"__newindex");
992 lua_setfield(L, -2,
"__dir");
993 lua_pushstring(L,
"unit variables");
994 lua_setfield(L, -2,
"__metatable");
996 return cmd_out.str();
A config object defines a single node in a WML file, with access to child nodes.
std::size_t attribute_count() const
Count the number of non-blank attributes.
const_attr_itors attribute_range() const
auto all_children_view() const
In-order iteration over all children.
std::size_t all_children_count() const
virtual const gamemap & map() const override
virtual const unit_map & units() const override
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Storage for a unit, either owned by the Lua code (ptr != 0), a local variable unit (c_ptr !...
static void setmetatable(lua_State *L)
bool put_map(const map_location &loc)
unit_ptr get_shared() const
unit_ptr find_if_matches_underlying_id(std::size_t uid)
Find a unit by underlying id.
unit_ptr extract_if_matches_underlying_id(std::size_t uid)
Find a unit by underlying id, and extract if found.
recall_list_manager & recall_list()
unit_iterator find(std::size_t id)
umap_retval_pair_t replace(const map_location &l, const unit_ptr &p)
Works like unit_map::add; but l is emptied first, if needed.
std::size_t erase(const map_location &l)
Erases the unit at location l, if any.
umap_retval_pair_t move(const map_location &src, const map_location &dst)
Moves a unit from location src to location dst.
This class represents a single unit of a specific type.
Additional functionality for a non-const variable_info.
Information on a WML variable.
Represents version numbers.
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
Interfaces for manipulating version numbers of engine, add-ons, etc.
void set_state(const std::string &state, bool value)
Set whether the unit is affected by a status effect.
static std::string get_known_boolean_state_name(state_t state)
Convert a built-in status effect ID to a string status effect ID.
bool get_state(const std::string &state) const
Check if the unit is affected by a status effect.
const std::string & type_id() const
The id of this unit's type.
const std::set< std::string > get_states() const
Get the status effects currently affecting the unit.
const std::string & id() const
Gets this unit's id.
state_t
Built-in status effects known to the engine.
config & variables()
Gets any user-defined variables this unit 'owns'.
std::size_t underlying_id() const
This unit's unique internal ID.
@ NUMBER_OF_STATES
The unit is invulnerable - it cannot be hit by any attack.
const map_location & get_location() const
The current map location this unit is at.
utils::variant< upkeep_full, upkeep_loyal, int > upkeep_t
Standard logging facilities (interface).
#define LATTR_SETTER(name, value_type, obj_type, obj_name)
#define LATTR_GETTER(name, value_type, obj_type, obj_name)
bool luaW_pushvariable(lua_State *L, variable_access_const &v)
bool luaW_toboolean(lua_State *L, int n)
int luaW_type_error(lua_State *L, int narg, const char *tname)
bool luaW_checkvariable(lua_State *L, variable_access_create &v, int n)
#define return_cfgref_attrib(name, accessor)
#define modify_cfg_attrib(name, accessor)
static int impl_unit_set(lua_State *L)
Sets some data on a unit (__newindex metamethod).
static int impl_unit_variables_dir(lua_State *L)
List variables on a unit (__dir metamethod)
static int impl_unit_equality(lua_State *L)
Checks two lua proxy units for equality.
static int impl_unit_status_dir(lua_State *L)
List statuses on a unit (__dir metamethod) This returns all known statuses (regardless of state) plus...
static lg::log_domain log_scripting_lua("scripting/lua")
static void unit_show_error(lua_State *L, int index, int error)
static int impl_unit_status_get(lua_State *L)
Gets the status of a unit (__index metamethod).
#define UNIT_GETTER(name, type)
static int impl_unit_collect(lua_State *L)
Destroys a unit object before it is collected (__gc metamethod).
static const char unitvarKey[]
static int impl_unit_variables_get(lua_State *L)
Gets the variable of a unit (__index metamethod).
static int impl_unit_get(lua_State *L)
Gets some data on a unit (__index metamethod).
lua_unit * luaW_pushlocalunit(lua_State *L, unit &u)
Pushes a private unit on the stack.
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...
static lua_unit * internal_get_unit(lua_State *L, int index, bool only_on_map, int &error)
#define UNIT_SETTER(name, type)
lua_unit * luaW_tounit_ref(lua_State *L, int index)
Similar to luaW_tounit but returns a lua_unit; use this if you need to handle map and recall units di...
static int impl_unit_variables_set(lua_State *L)
Sets the variable of a unit (__newindex metamethod).
unit * luaW_tounit(lua_State *L, int index, bool only_on_map)
Converts a Lua value to a unit pointer.
bool luaW_isunit(lua_State *L, int index)
Test if a Lua value is a unit.
static const char getunitKey[]
static const char ustatusKey[]
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...
static void handle_unit_move(lua_State *L, lua_unit *lu, map_location dst)
static int impl_unit_status_set(lua_State *L)
Sets the status of a unit (__newindex metamethod).
unit_ptr luaW_tounit_ptr(lua_State *L, int index, bool only_on_map)
Similar to luaW_tounit but returns a unit_ptr; use this instead of luaW_tounit when using an api that...
static int impl_unit_tostring(lua_State *L)
Turns a lua proxy unit to string.
static int impl_unit_dir(lua_State *L)
Prints valid attributes on a unit (__dir metamethod).
unit & luaW_checkunit(lua_State *L, int index, bool only_on_map)
Converts a Lua value to a unit pointer.
void push_unit_attacks_table(lua_State *L, int idx)
std::string register_metatables(lua_State *L)
T end(const std::pair< T, T > &p)
std::shared_ptr< unit > unit_ptr
void lua_push(lua_State *L, const T &val)
const std::string & gender_string(unit_race::GENDER gender)
std::unique_ptr< MIX_Audio, decltype(&MIX_DestroyAudio)> value
rect dst
Location on the final composed sheet.
rect src
Non-transparent portion of the surface to compose.
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.
static lua_unit * get(lua_State *L, int n)
static unit & get(lua_State *L, int n)
Encapsulates the map of the game.
static std::string write_direction(direction dir)
static direction parse_direction(const std::string &str)
static std::string get_string(enum_type key)
Converts a enum to its string equivalent.
static constexpr utils::optional< enum_type > get_enum(const std::string_view value)
Converts a string into its enum equivalent.
Visitor helper struct to fetch the upkeep type flag if applicable, or the the value otherwise.
pointer get_shared_ptr() const
This is exactly the same as operator-> but it's slightly more readable, and can replace &*iter syntax...
static map_location::direction n
static map_location::direction s
variable_info_mutable< variable_info_implementation::vi_policy_throw > variable_access_throw
'Throw if nonexistent' access.