Difference between revisions of "LuaAPI/wesnoth/units"
(Document the units module (mostly a copy-paste, reorder, and adjustment of LuaWML/Units)) |
(Link to aliased functions in wesnoth.interface) |
||
Line 120: | Line 120: | ||
u.status.poisoned = false | u.status.poisoned = false | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | ==== wesnoth.units.select ==== | ||
+ | |||
+ | Alias of [[LuaAPI/wesnoth/interface#wesnoth.interface.select_unit]]. | ||
==== wesnoth.units.ability ==== | ==== wesnoth.units.ability ==== | ||
Line 273: | Line 277: | ||
local unit = wesnoth.units.get(args.x1, args.y1) | local unit = wesnoth.units.get(args.x1, args.y1) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | ==== wesnoth.units.get_hovered ==== | ||
+ | |||
+ | Alias of [[LuaAPI/wesnoth/interface#wesnoth.interface.get_displayed_unit]]. |
Revision as of 21:58, 16 November 2019
(Version 1.15.0 and later only)
The units module is only available in the game. It is not available to plugins or map generators.
Contents
- 1 wesnoth.units.advance
- 2 wesnoth.units.clone
- 3 wesnoth.units.erase
- 4 wesnoth.units.extract
- 5 wesnoth.units.matches
- 6 wesnoth.units.to_map
- 7 wesnoth.units.to_recall
- 8 wesnoth.units.transform
- 9 wesnoth.units.select
- 10 wesnoth.units.ability
- 11 wesnoth.units.defense
- 12 wesnoth.unit_resistance
- 13 wesnoth.units.jamming
- 14 wesnoth.units.movement
- 15 wesnoth.units.vision
- 16 wesnoth.units.add_modification
- 17 wesnoth.units.remove_modifications
- 18 wesnoth.units.create
- 19 wesnoth.units.find_on_map
- 20 wesnoth.units.find_on_recall
- 21 wesnoth.units.find
- 22 wesnoth.units.get
- 23 wesnoth.units.get_hovered
wesnoth.units.advance
- wesnoth.units.advance(unit [, animate, fire_events])
- unit:advance([animate, fire_events])
Advances the unit (and shows the advance unit dialog if needed) if the unit has enough XP. This function should be called after modifying the unit's experience directly. A similar function is called by Wesnoth internally after unit combat. The second argument is a boolean value that specifies whether the advancement should be animated. The third argument is a boolean value that specifies whether advancement related events should be fired.
This function only works for units on the map.
This function can also trigger multiple advancements if the unit has enough XP.
wesnoth.units.clone
- wesnoth.units.clone(unit) → copy of unit
- unit:clone()
Creates a private unit from another unit.
-- extract a unit from the map
local u = wesnoth.units.find_on_map{ type = "Thug" }[1]:clone()
wesnoth.units.erase(u.x, u.y) -- note: not the same as u:erase(), which would be an error
-- u is still valid at this point
wesnoth.units.erase
- wesnoth.units.erase(unit)
- wesnoth.units.erase(x, y)
- unit:erase()
Erases a unit from the map. After calling this on a unit, the unit is no longer valid. Does not work on private units - this usage will raise an error.
wesnoth.units.extract
- wesnoth.units.extract(unit)
- unit:extract()
Removes a unit from the map or from a recall list and makes it private.
-- remove all the units from the recall list of side 1 and put them in a WML container
local list = {}
for i,u in ipairs(wesnoth.units.find_on_recall{ side = 1 }) do
u:extract()
table.insert(list, u.__cfg)
end
wml.array_access.set("player_recall_list", list)
Note: if the unit is on the map, this is just a shortcut for calling #wesnoth.units.clone and then #wesnoth.units.erase. It is, however, the only way to remove a unit from a recall list without putting it on the map.
wesnoth.units.matches
- wesnoth.units.matches(unit, filter [, other_unit]) → matched?
- wesnoth.units.matches(unit, filter [, location]) → matched?
- unit:matches(filter [, other_unit]) → matched?
- unit:matches(filter [, location]) → matched?
Returns true if the given unit matches the WML filter passed as the second argument. If other_unit is specified, it is used for the $other_unit auto-stored variable in the filter. Otherwise, this variable is not stored for the filter. If an extra location is specified, the filter matches as if the unit were at that location.
assert(unit.canrecruit == wesnoth.units.matches(unit, { canrecruit = true }))
wesnoth.units.to_map
- wesnoth.units.to_map(unit)
- wesnoth.units.to_map(unit, x, y)
- unit:to_map([x, y])
Places a unit on the map. This unit is described either by a WML table or by a proxy unit (to use the third form, it must be a proxy). Coordinates can be passed as additional arguments; otherwise the table is expected to have two fields x and y, which indicate where the unit will be placed.
-- create a unit with random traits, then erase it
wesnoth.units.to_map({ type = "Elvish Lady" }, 17, 42)
wesnoth.units.erase(17, 42)
When the argument is a proxy unit, no duplicate is created. In particular, if the unit was private or on a recall list, it no longer is; and if the unit was on the map, it has been moved to the new location. Note: passing a WML table is just a shortcut for calling #wesnoth.units.create and then putting the resulting unit on the map.
-- move the leader back to the top-left corner
wesnoth.units.find_on_map({ canrecruit = true })[1]:to_map(1, 1)
If x,y is a village, this function does not capture it (as of 1.14). Use wesnoth.set_village_owner(x, y, unit.side) if that's what you want.
wesnoth.units.to_recall
- wesnoth.units.to_recall(unit, [side])
- unit:to_recall([side])
Places a unit on a recall list. This unit is described either by a WML table or by a proxy unit (to use the third form, it must be a proxy). The side of the recall list is given by the second argument if present, or by the side of the unit otherwise.
-- put the unit at location 17,42 on the recall list for side 2
wesnoth.units.find_on_map{ x= 17, y = 42 })[1]:to_recall(2)
When the argument is a proxy unit, no duplicate is created. In particular, if the unit was private or on the map, it no longer is. Note: passing a WML table is just a shortcut for calling #wesnoth.units.create and then putting the resulting unit on a recall list.
wesnoth.units.transform
- wesnoth.units.transform(unit, to_type [, to_variation])
- unit:transform(to_type [, to_variation])
Changes the type of a unit and adjust attributes accordingly. Note that hit points are only changed if necessary to accommodate the new maximum hit points. Poison is automatically removed if the transformed unit is immune.
local ev = wesnoth.current.event_context
local u = wesnoth.units.find_on_map{x=ev.x1, y=ev.y1}[1]
u:transform("Spearman")
-- If a full heal is desired:
u.hitpoints = u.max_hitpoints
u.status.poisoned = false
wesnoth.units.select
Alias of LuaAPI/wesnoth/interface#wesnoth.interface.select_unit.
wesnoth.units.ability
- wesnoth.units.ability(unit, ability_tag) → affected
- unit:ability(ability_tag) → affected
Returns true if the unit is currently affected by an ability with this given tag name. This means that the ability could be owned by the unit itself, or by an adjacent unit.
function has_teleport(u)
return u:ability("teleport")
end
wesnoth.units.defense
- wesnoth.units.defense(unit, terrain_code) → defense value
- unit:defense(terrain_code) → defense value
Returns the defense of a unit on a particular terrain. (Note: it is a WML defense. So the higher it is, the weaker the unit is.)
local flat_defense = 100 - u:defense("Gt")
wesnoth.unit_resistance
- wesnoth.units.resistance(unit, damage_type [, as_attacker] [, x, y]) → resistance value
- unit:resistance(damage_type [, as_attacker] [, x, y]) → resistance value
Returns the resistance of a unit against an attack type. (Note: it is a WML resistance. So the higher it is, the weaker the unit is.) The third argument indicates whether the unit is the attacker. Last arguments are the coordinates of an optional map location (for the purpose of taking abilities into account).
local fire_resistance = 100 - u:resistance("fire")
wesnoth.units.jamming
- wesnoth.units.jamming(unit, terrain_code) → cost
- unit:jamming(terrain_code) → cost
Returns the jamming cost of a unit on a particular terrain.
local jam_cost = u:jamming("Gt")
wesnoth.units.movement
- wesnoth.units.movement(unit, terrain_code) → cost
- unit:movement(terrain_code) → cost
Returns the movement cost of a unit on a particular terrain.
local move_cost = u:movement("Gt")
wesnoth.units.vision
- wesnoth.units.vision(unit, terrain_code) → cost
- unit:vision(terrain_code) → cost
Returns the vision cost of a unit on a particular terrain.
local see_cost = u:vision("Gt")
wesnoth.units.add_modification
- wesnoth.units.add_modification(unit, type, effects, [write_to_mods])
- unit:add_modification(type, effects, [write_to_mods])
Modifies a given unit. It needs to be a proxy unit. The second argument is the type of the modification (one of "trait", "object", or "advancement"). The option "advancement" applies effects as if the unit would advance (e.g. AMLA effects). The third argument is a WML table describing the effect, so mostly containing [effect] children. See EffectWML for details about effects. If write_to_mods false, causes it to not write the modification tag to the unit's [modifications] (as would be done with an [object] with no_write=true).
local T = wml.tag
local u = wesnoth.units.find_on_map{ canrecruit = true }[1]
local effects = {
id = "my_effect_id",
T.effect {
apply_to = "image_mod",
replace = "RC(red>blue)"
},
T.effect {
apply_to = "new_animation",
T.standing_animation {
-- AnimationWML
}
}
}
u:add_modification("object", effects)
wesnoth.units.remove_modifications
- wesnoth.remove_modifications(unit, filter [, type])
- unit:remove_modifications(filter [, type])
Modifies a given unit. The unit needs to be a proxy unit. The second argument is a filter for the modifications to remove. It takes the same syntax as [filter_wml]; all matching modifications will be removed. The third argument is the type (tag name) of the modifications to search for; it defaults to "object", but you can also pass "trait" or "advancement".
local u = wesnoth.units.find_on_map{ canrecruit = true }[1]
u:remove_modifications({ id = "my_effect_id" })
wesnoth.units.create
- wesnoth.units.create(unit_info) → unit
Creates a private unit from a WML table.
local u = wesnoth.units.create{ type = "White Mage", gender = "female" }
wesnoth.units.find_on_map
- wesnoth.units.find_on_map(filter) → array of units
- wesnoth.units.find_on_map(filter, fake_location) → array of units
- wesnoth.units.find_on_map(filter, other_unit) → array of units
Returns an array of all the units on the map matching the WML filter passed as the first argument. See StandardUnitFilter for details about filters. If a second unit is passed, it can be referenced via the $other_unit variable in the main filter as well as via the "other" variable in WFL formulas used in the main filter. If a location is passed, the filter is run as if the unit were at that location (rather than its real location). This affects things such as [filter_adjacent] and ability_active, and should work even for a unit on the recall list.
local leaders_on_side_two = wesnoth.units.find_on_map{ side = 2, canrecruit = true }
local name_of_leader = leaders_on_side_two[1].name
wesnoth.units.find_on_recall
- wesnoth.units.find_on_recall(filter) → array of units
Returns an array of all the units on the recall lists matching the WML filter passed as the first argument.
wesnoth.units.find
- wesnoth.units.find(filter) → array of units
Returns a list of all units matching the WML filter, both those on the map and those on the recall lists. It is a shorthand for calling both find_on_map and find_on_recall and combining the resulting arrays.
wesnoth.units.get
- wesnoth.units.get(x, y) → unit or nil
- wesnoth.units.get(id) → unit or nil
Returns the unit at the given location or with the given ID.
local args = ...
local unit = wesnoth.units.get(args.x1, args.y1)
wesnoth.units.get_hovered
Alias of LuaAPI/wesnoth/interface#wesnoth.interface.get_displayed_unit.