Add [test_do_attack_by_id], a wrapper for [do_command][attack]

The new tag takes ids instead of needing coordinates. So instead of storing
units or hardcoding values, an attack can be done like this:

```
[test_do_attack_by_id]
	attacker=alice
	defender=bob
	weapon=0
[/test_do_attack_by_id]
```

Add a simpler unit for unit tests than the Elvish Archer / Orcish Grunt
combo, cherry-picked from gfgtdf's PR #7589. This is a unit with a 10x1
melee attack, and 0% defense on all terrains.

Remove the test race's undead_variation=
For testing custom plague abilities, this triggered a warning when
converting to any unit type that doesn't have variation_id=wolf,
restricting tests to using only Walking Corpse and Soulless.
This commit is contained in:
Steve Cotton 2023-06-24 21:20:54 +02:00 committed by Steve Cotton
parent 8f1c7481de
commit aaf2735c1c
4 changed files with 130 additions and 1 deletions

View File

@ -29,6 +29,10 @@
#Load test macros
{test/macros}
[lua]
code = "wesnoth.dofile 'test/lua/wml_tags.lua'"
[/lua]
# Load automated test scenarios.
#
# They can be run individually with Wesnoth's `-u` command line option, but are usually run by

View File

@ -0,0 +1,48 @@
--! #textdomain wesnoth-test
local T = wml.tag
---Wrapper for [do_command][attack] that takes unit ids instead of map coordinates,
---as tests are likely to already know the id.
---
---cfg.attacker,cfg.defender are unit ids, and map to [attack]source,destination=
---cfg.weapon,cfg.defender_weapon are optional, and map to [attack]weapon,defender_weapon=
---
---cfg.resupply_attacks_left is an optional int, if set then it will ensure the attacker has
---at least that many attacks_left before calling [do_command][attack].
function wesnoth.wml_actions.test_do_attack_by_id(cfg)
-- can't be called "source", otherwise wmllint wants to add translation marks
if not cfg.attacker then
wml.error("[test_do_attack] missing required attacker= attribute")
end
local attacker = wesnoth.units.find_on_map { id = cfg.attacker }[1]
if not attacker or not attacker.valid then
wml.error("[test_do_attack] attacker did not match a unit")
end
if not cfg.defender then
wml.error("[test_do_attack] missing required defender= attribute")
end
local defender = wesnoth.units.find_on_map { id = cfg.defender }[1]
if not defender or not defender.valid then
wml.error("[test_do_attack] defender did not match a unit")
end
local weapon = cfg.weapon or ""
local defender_weapon = cfg.defender_weapon or ""
-- Avoid needing to modify units to give them multiple attacks per round
-- This attribute is a number rather than a boolean, to support [attack]attacks_used=
if cfg.resupply_attacks_left and attacker.attacks_left < cfg.resupply_attacks_left then
attacker.attacks_left = cfg.resupply_attacks_left
end
wesnoth.wml_actions.do_command {
T.attack {
T.source { x = attacker.x, y = attacker.y },
T.destination { x = defender.x, y = defender.y },
weapon = weapon,
defender_weapon = defender_weapon,
}
}
end

View File

@ -10,7 +10,6 @@
plural_name= _ "tests"
description= _ "test race"
num_traits=2
undead_variation=wolf
{ORCISH_NAMES}
[/race]
@ -32,4 +31,48 @@
note={INTERNAL:SPECIAL_NOTES_DEFENSE_CAP}
[/special_note]
[/movetype]
# The main feature of this movetype is that it has 0% defense on all terrains,
# so there's no need to use CHANCE_TO_HIT macros.
[movetype]
name=test_movetype_no_defense
[movement_costs]
shallow_water=2
reef=2
swamp_water=2
flat=2
sand=2
forest=2
hills=2
village=2
castle=2
cave=2
frozen=2
fungus=2
[/movement_costs]
[defense]
shallow_water=100
reef=100
swamp_water=100
flat=100
sand=100
forest=100
hills=100
village=100
castle=100
cave=100
frozen=100
fungus=100
[/defense]
[resistance]
blade=100
pierce=100
impact=100
fire=100
cold=100
arcane=100
[/resistance]
[/movetype]
[/units]

View File

@ -0,0 +1,34 @@
#textdomain wesnoth-test
# A Unit with simple stats for testing attack & ability releated functions.
#
# Neutral, and a single melee attack that only has one strike.
[unit_type]
id=Test Melee Quintain
name=_ "test_unit^Test Melee Quintain"
race=test
# Image is a sword, to fit the melee-blade attack type. The icon is a non-flaming
# sword, because "-bare" is the version before the flames are added.
image="items/flame-sword-bare.png"
ignore_race_traits=yes
hitpoints=100
movement_type=test_movetype_no_defense
movement=20
experience=50
level=1
alignment=neutral
advances_to=null
cost=1
hide_help=yes
do_not_list=yes
num_traits=0
[attack]
name="melee_attack"
description=_"Melee Attack"
icon=attacks/sword-human.png
type=blade
range=melee
damage=10
number=1
[/attack]
[/unit_type]