Skip to content

Commit

Permalink
Add unit tests for Lua API wesnoth.simulate_combat
Browse files Browse the repository at this point in the history
  • Loading branch information
stevecotton committed Mar 22, 2022
1 parent 4dc923e commit 25992e8
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
39 changes: 39 additions & 0 deletions data/test/scenarios/simulate_combat_clone_adjacent.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# wmllint: no translatables

#####
# API(s) being tested: wesnoth.simulate_combat
##
# Actions:
# Start with Alice and Bob on separate keeps (the GENERIC_UNIT_TEST macro).
# Make a private-to-Lua clone of Alice, move her next to Bob.
# Simulate combat.
##
# Expected end state:
# The simulation with the original Alice shows that neither can reach the other.
# The simulation with the cloned Alice shows combat stats.
#####
{GENERIC_UNIT_TEST simulate_combat_clone_adjacent (
[event]
name=start

[lua]
code=<<
local alice = wesnoth.units.find({id="alice"})[1]
local bob = wesnoth.units.find({id="bob"})[1]
local clone_alice = alice:clone()
clone_alice.x = bob.x
clone_alice.y = bob.y + 1

local att_stats, def_stats = wesnoth.simulate_combat(clone_alice, bob)
unit_test.assert_approx_equal(att_stats.untouched, 1.0, 0.01, "Alice should have used her ranged attack")
unit_test.assert_approx_equal(def_stats.untouched, 0.6 ^ 4, 0.01, "Bob should be on 60% def")

att_stats, def_stats = wesnoth.simulate_combat(alice, bob)
unit_test.assert_approx_equal(att_stats.untouched, 1.0, 0.01, "Alice should be out of reach of combat")
unit_test.assert_approx_equal(def_stats.untouched, 1.0, 0.01, "Bob should be out of reach of combat")
>>
[/lua]

{SUCCEED}
[/event]
)}
55 changes: 55 additions & 0 deletions data/test/scenarios/simulate_combat_occupied.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#textdomain wesnoth-test

#####
# API(s) being tested: wesnoth.simulate_combat
##
# Actions:
# Start with Alice and Bob on separate keeps (the GENERIC_UNIT_TEST macro).
# Spawn a unit called Dave on Bob's side next to Bob.
# Make a private-to-Lua clone of Alice.
# Simulate combat as if Alice was on the hex occupied by Dave.
# While the units are in place, simulate combat between Dave and Bob too.
##
# Expected end state:
# The simulation for Alice shows the same stats as if the hex was unoccupied.
#
# The simulation for Dave allows the fight to happen, and doesn't log any
# errors. Possibly something that should change, but the test is included to
# check for API changes in 1.16.
#####
{GENERIC_UNIT_TEST simulate_combat_occupied (
[event]
name=start

[unit]
side=2
name=_ "Dave"
type=Orcish Grunt
id=dave
placement=leader
[/unit]

[lua]
code=<<
local alice = wesnoth.units.find({id="alice"})[1]
local bob = wesnoth.units.find({id="bob"})[1]
local dave = wesnoth.units.find({id="dave"})[1]
local clone_alice = alice:clone()
clone_alice.x = dave.x
clone_alice.y = dave.y

local att_stats, def_stats = wesnoth.simulate_combat(clone_alice, bob)
unit_test.assert_approx_equal(att_stats.untouched, 1.0, 0.01, "Alice should have used her ranged attack")
unit_test.assert_approx_equal(def_stats.untouched, 0.6 ^ 4, 0.01, "Bob should be on 60% def")

-- While the test is set up, check what happens if Dave attacks his own leader.
-- In 1.16.x and at least 1.17.2 the attack gets simulated as if they were on different sides.
att_stats, def_stats = wesnoth.simulate_combat(dave, bob)
unit_test.assert_approx_equal(att_stats.untouched, 0.4 ^ 2, 0.01, "Dave should have attacked from 40% def")
unit_test.assert_approx_equal(def_stats.untouched, 0.6 ^ 2, 0.01, "Bob should have retaliated from 60% def")
>>
[/lua]

{SUCCEED}
[/event]
)}
2 changes: 2 additions & 0 deletions wml_test_schedule
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,5 @@
0 lua_map_find
0 mapgen_filter_range
0 mapgen_filter_terrain
0 simulate_combat_clone_adjacent
0 simulate_combat_occupied

0 comments on commit 25992e8

Please sign in to comment.