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";
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)";
270 lua_push(L, str.str());
274 #define UNIT_GETTER(name, type) LATTR_GETTER(name, type, unit, u)
275 #define UNIT_SETTER(name, type) LATTR_SETTER(name, type, unit, u)
276 luaW_Registry unitReg{"wesnoth", "units", getunitKey};
278 template<> struct lua_object_traits<lua_unit*> {
279 inline static auto metatable = getunitKey;
280 inline static lua_unit* get(lua_State* L, int n) {
281 auto lu = luaW_tounit_ref(L, n);
282 if(!lu) unit_show_error(L, n, LU_NOT_UNIT);
287 template<> struct lua_object_traits<unit> {
288 inline static auto metatable = getunitKey;
289 inline static unit& get(lua_State* L, int n) {
290 return luaW_checkunit(L, n);
294 static void handle_unit_move(lua_State* L, lua_unit* lu, map_location dst) {
296 (*lu)->set_location(dst);
298 unit& u = *lu->get();
300 // Handle moving an on-map unit
301 game_board* gb = resources::gameboard;
307 map_location src = u.get_location();
309 // TODO: could probably be relegated to a helper function.
311 // If the dst isn't on the map, the
unit will be clobbered. Guard against that.
312 if(!gb->map().on_board(
dst)) {
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;
356 if(!lu->get())
return;
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);
575 if(!alignment) luaL_argerror(L, value.index,
"invalid unit 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());
632 lua_createtable(L, 1, 0);
634 lua_rawseti(L, -2, 1);
641 lua_createtable(L, 1, 0);
643 lua_rawseti(L, -2, 1);
801 if(!lua_istable(L, 1)) {
804 lua_rawgeti(L, 1, 1);
807 return luaL_argerror(L, 1,
"unknown unit");
809 char const *m = luaL_checkstring(L, 2);
822 if(!lua_istable(L, 1)) {
825 lua_rawgeti(L, 1, 1);
828 return luaL_argerror(L, 1,
"unknown unit");
830 char const *m = luaL_checkstring(L, 2);
841 if(!lua_istable(L, 1)) {
844 lua_rawgeti(L, 1, 1);
847 return luaL_argerror(L, 1,
"unknown unit");
849 std::vector<std::string> states;
869 if(!lua_istable(L, 1)) {
872 lua_rawgeti(L, 1, 1);
875 return luaL_argerror(L, 2,
"unknown unit");
877 char const *m = luaL_checkstring(L, 2);
892 if(!lua_istable(L, 1)) {
895 lua_rawgeti(L, 1, 1);
898 return luaL_argerror(L, 2,
"unknown unit");
900 char const *m = luaL_checkstring(L, 2);
903 if(lua_isnoneornil(L, 3)) {
920 if(!lua_istable(L, 1)) {
923 lua_rawgeti(L, 1, 1);
926 return luaL_argerror(L, 2,
"unknown unit");
929 std::vector<std::string> variables;
932 variables.push_back(attr.first);
935 variables.push_back(attr.key);
944 std::ostringstream cmd_out;
947 cmd_out <<
"Adding getunit metatable...\n";
951 lua_setfield(L, -2,
"__gc");
953 lua_setfield(L, -2,
"__eq");
955 lua_setfield(L, -2,
"__tostring");
957 lua_setfield(L, -2,
"__index");
959 lua_setfield(L, -2,
"__newindex");
961 lua_setfield(L, -2,
"__dir");
962 lua_pushstring(L,
"unit");
963 lua_setfield(L, -2,
"__metatable");
966 cmd_out <<
"Adding unit status metatable...\n";
970 lua_setfield(L, -2,
"__index");
972 lua_setfield(L, -2,
"__newindex");
974 lua_setfield(L, -2,
"__dir");
975 lua_pushstring(L,
"unit status");
976 lua_setfield(L, -2,
"__metatable");
979 cmd_out <<
"Adding unit variables metatable...\n";
983 lua_setfield(L, -2,
"__index");
985 lua_setfield(L, -2,
"__newindex");
987 lua_setfield(L, -2,
"__dir");
988 lua_pushstring(L,
"unit variables");
989 lua_setfield(L, -2,
"__metatable");
991 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
const_all_children_itors all_children_range() const
In-order iteration over all children.
std::size_t all_children_count() const
virtual const unit_map & units() const override
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()
std::vector< std::string > get_flags()
Get the flags of all registered animations.
umap_retval_pair_t replace(const map_location &l, unit_ptr p)
Works like unit_map::add; but l is emptied first, if needed.
unit_iterator find(std::size_t id)
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.
const std::string & id() const
This class represents a single unit of a specific type.
Additional functionality for a non-const variable_info.
Information on a WML variable.
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 write(config &cfg, bool write_all=true) const
Serializes the current unit metadata values.
std::vector< std::string > get_ability_list() const
Get a list of all abilities by ID.
void set_big_profile(const std::string &value)
int max_hitpoints() const
The max number of hitpoints this unit can have.
void set_role(const std::string &role)
Sets a unit's role.
unit_alignments::type alignment() const
The alignment of this unit.
bool incapacitated() const
Check if the unit has been petrified.
void set_state(const std::string &state, bool value)
Set whether the unit is affected by a status effect.
int level() const
The current level of this unit.
std::string usage() const
Gets this unit's usage.
const std::string & get_role() const
Gets this unit's role.
const std::vector< std::string > & recruits() const
The type IDs of the other units this unit may recruit, if possible.
void set_max_experience(int value)
void set_max_hitpoints(int value)
void set_hitpoints(int hp)
Sets the current hitpoint amount.
bool unrenamable() const
Whether this unit can be renamed.
const config & recall_filter() const
Gets the filter constraints upon which units this unit may recall, if able.
int recall_cost() const
How much gold it costs to recall this unit, or -1 if the side's default recall cost is used.
std::string big_profile() const
An optional profile image displays when this unit is 'speaking' via [message].
void set_level(int level)
Sets the current level of this unit.
void set_hidden(bool state) const
Sets whether the unit is hidden on the map.
void set_recall_filter(const config &filter)
Sets the filter constraints upon which units this unit may recall, if able.
const std::string & variation() const
The ID of the variation of this unit's type.
int hitpoints() const
The current number of hitpoints this unit has.
int cost() const
How much gold is required to recruit this unit.
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.
void set_undead_variation(const std::string &value)
The ID of the undead variation (ie, dwarf, swimmer) of this unit.
const std::string & type_id() const
The id of this unit's type.
void set_alignment(unit_alignments::type alignment)
Sets the alignment of this unit.
bool get_hidden() const
Gets whether this unit is currently hidden on the map.
void set_name(const t_string &name)
Sets this unit's translatable display name.
void set_can_recruit(bool canrecruit)
Sets whether this unit can recruit other units.
const std::set< std::string > get_states() const
Get the status effects currently affecting the unit.
void set_side(unsigned int new_side)
Sets the side this unit belongs to.
const std::string & undead_variation() const
const unit_race * race() const
Gets this unit's race.
int experience() const
The current number of experience points this unit has.
bool can_recruit() const
Whether this unit can recruit other units - ie, are they a leader unit.
void set_experience(int xp)
Sets the current experience point amount.
const std::string & id() const
Gets this unit's id.
int side() const
The side this unit belongs to.
state_t
Built-in status effects known to the engine.
void set_unit_description(const t_string &new_desc)
A detailed description of this unit.
void set_unrenamable(bool unrenamable)
Sets the 'unrenamable' flag.
void set_recruits(const std::vector< std::string > &recruits)
Sets the recruit list.
config & variables()
Gets any user-defined variables this unit 'owns'.
void set_recall_cost(int recall_cost)
Sets the cost of recalling this unit.
std::size_t underlying_id() const
This unit's unique internal ID.
void set_usage(const std::string &usage)
Sets this unit's usage.
int max_experience() const
The max number of experience points this unit can have.
unit_race::GENDER gender() const
The gender of this unit.
const t_string & name() const
Gets this unit's translatable display name.
t_string unit_description() const
A detailed description of this unit.
@ NUMBER_OF_STATES
The unit is invulnerable - it cannot be hit by any attack.
const advances_to_t & advances_to() const
Gets the possible types this unit can advance to on level-up.
void set_advancements(std::vector< config > advancements)
Sets the raw modification advancement option data.
void set_advances_to(const std::vector< std::string > &advances_to)
Sets this unit's advancement options.
const std::vector< config > & modification_advancements() const
The raw, unparsed data for modification advancements.
void set_max_attacks(int value)
int max_attacks() const
The maximum number of attacks this unit may perform per turn, usually 1.
int attacks_left() const
Gets the remaining number of attacks this unit can perform this turn.
void set_attacks(int left)
Sets the number of attacks this unit has left this turn.
unit_animation_component & anim_comp() const
const std::string & effect_image_mods() const
Gets any IPF image mods applied by effects.
std::string image_ellipse() const
Get the unit's ellipse image.
std::string image_mods() const
Gets an IPF string containing all IPF image mods.
std::string image_halo() const
Get the unit's halo image.
const std::vector< std::string > & overlays() const
Get the unit's overlay images.
std::string absolute_image() const
The name of the file to game_display (used in menus).
void set_image_ellipse(const std::string &ellipse)
Set the unit's ellipse image.
void set_image_halo(const std::string &halo)
Set the unit's halo image.
bool get_emit_zoc() const
Gets the raw zone-of-control flag, disregarding incapacitated.
int jamming() const
Gets the unit's jamming points.
const map_location & get_location() const
The current map location this unit is at.
void set_movement(int moves, bool unit_action=false)
Set this unit's remaining movement to moves.
void set_resting(bool rest)
Sets this unit's resting status.
void set_facing(map_location::direction dir) const
The this unit's facing.
void set_total_movement(int value)
void set_emit_zoc(bool val)
Sets the raw zone-of-control flag.
void set_goto(const map_location &new_goto)
Sets this unit's long term destination.
int movement_left() const
Gets how far a unit can move, considering the incapacitated flag.
int total_movement() const
The maximum moves this unit has.
int vision() const
Gets the unit's vision points.
const map_location & get_goto() const
The map location to which this unit is moving over multiple turns, if any.
map_location::direction facing() const
The current direction this unit is facing within its hex.
bool resting() const
Checks whether this unit is 'resting'.
bool is_flying() const
Check if the unit is a flying unit.
bool is_healthy() const
Gets whether this unit is healthy - ie, always rest heals.
bool is_fearless() const
Gets whether this unit is fearless - ie, unaffected by time of day.
utils::variant< upkeep_full, upkeep_loyal, int > upkeep_t
std::vector< std::string > get_traits_list() const
Gets a list of the traits this unit currently has, including hidden traits.
upkeep_t upkeep_raw() const
Gets the raw variant controlling the upkeep value.
void set_upkeep(upkeep_t v)
Sets the upkeep value to a specific value value.
T end(const std::pair< T, T > &p)
Standard logging facilities (interface).
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)
LATTR_SETTER("x", int, lua_unit *, lu)
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 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")
unit & luaW_checkunit(lua_State *L, int index, bool only_on_map)
Converts a Lua value to a unit pointer.
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)
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_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 lua_unit * internal_get_unit(lua_State *L, int index, bool only_on_map, int &error)
static int impl_unit_get(lua_State *L)
Gets some data on a unit (__index metamethod).
#define UNIT_SETTER(name, type)
static int impl_unit_variables_set(lua_State *L)
Sets the variable of a unit (__newindex metamethod).
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...
LATTR_GETTER("valid", utils::optional< std::string >, lua_unit *, lu)
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.
lua_unit * luaW_pushlocalunit(lua_State *L, unit &u)
Pushes a private unit on the stack.
static int impl_unit_dir(lua_State *L)
Prints valid attributes on a unit (__dir metamethod).
unit * luaW_tounit(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)
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
std::shared_ptr< unit > unit_ptr
void lua_push(lua_State *L, const T &val)
const std::string & gender_string(unit_race::GENDER gender)
rect dst
Location on the final composed sheet.
rect src
Non-transparent portion of the surface to compose.
int dir(lua_State *L)
Implement __dir metamethod.
int set(lua_State *L)
Implement __newindex metamethod.
int get(lua_State *L)
Implement __index metamethod.
Encapsulates the map of the game.
static std::string write_direction(direction dir)
static direction parse_direction(const std::string &str)
void write(config &cfg) const
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 s
variable_info_mutable< variable_info_implementation::vi_policy_throw > variable_access_throw
'Throw if nonexistent' access.