Files
transcience/tests/unit/test_regen.gd
T
claudeandClaude Opus 5 4aeb6d229d Health survives a door, and experience needs you to be there
Arriving anywhere set health to maximum, so a run cost nothing: walk in,
take what you could, walk out, walk back in whole. Current health now
lives on the character record for the same reason the inventory does --
SimWorld builds a blank player on every instance change, so anything that
must survive a door has to be written down -- and it is banked at every
transfer, every disconnect, and on every item transaction, since a potion
moves an item and hit points in one action.

The hub is where that damage is paid off, at 10% of maximum per second:
about ten seconds from nothing, fast enough not to be a chore and slow
enough to be a decision before walking back in. The dungeon rate is
unchanged, so waiting one out is still not a plan. Regeneration is a
property of the world now rather than a constant.

adopt() no longer touches current health. It is called mid-session by
on_choose_upgrade, and reading health from the record there would snap a
player back to whatever was last written to disk; recompute_max_hp still
clamps downwards so Glass Cannon takes the hit points above the new
ceiling with it. ServerRuntime._adopt_character stays the one place a
record becomes a live player.

Experience is still undivided, but now only among players within
XP_AWARD_RADIUS of the kill -- ten percent past what a player can see, so
anyone who watched it die is inside and someone parked in the entrance
room is not. The death events carry a position for this, and deliberately
do not put it on the wire: encode_events still writes the id alone.

diag_progression covers the whole chain end to end -- wounded in a
dungeon, escaped to the hub with the wound intact, healing there, and
walking back in still hurt -- because a unit test cannot transfer a peer.
Both halves were mutation-tested against it; the unit suite alone missed
the arrival heal entirely.

The Proving Grounds is recorded in the roadmap as a debugging tool that
does not ship: it pays full experience for a boss with 8% health.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-07 21:48:20 +02:00

146 lines
4.6 KiB
GDScript

extends GutTest
## Passive health regeneration.
var world: SimWorld
const PEER := 4
func before_each() -> void:
world = SimWorld.new(1)
world.add_player(PEER, "tester")
func _player() -> SimPlayer:
return world.players[PEER]
func test_it_heals_the_configured_share_of_maximum_per_second() -> void:
var p := _player()
p.hp = 1
for _i in SimConfig.TICK_RATE:
world.step()
var expected := float(p.max_hp) * SimConfig.HP_REGEN_PERCENT_PER_SEC / 100.0
assert_almost_eq(float(p.hp - 1), expected, 1.0,
"one second should restore %.1f%% of maximum" % SimConfig.HP_REGEN_PERCENT_PER_SEC)
## The reason for the fractional carry: at this rate a tick heals far less than
## one hit point, so truncating every tick would heal exactly nothing forever.
func test_fractions_accumulate_rather_than_being_lost() -> void:
var p := _player()
p.hp = 1
var per_tick := float(p.max_hp) * (SimConfig.HP_REGEN_PERCENT_PER_SEC / 100.0) \
* SimConfig.TICK_DELTA
assert_lt(per_tick, 1.0, "setup: a single tick must heal less than one hp")
for _i in SimConfig.TICK_RATE * 10:
world.step()
assert_gt(p.hp, 1, "ten seconds of sub-integer healing must still add up")
func test_it_scales_with_maximum_health_so_levels_do_not_dilute_it() -> void:
var low := _player()
# Taken from the curve rather than written down, so a rescale of the health
# numbers cannot leave this test comparing two arbitrary constants.
low.max_hp = Progression.max_hp_for_level(Progression.START_LEVEL)
low.hp = 1
for _i in SimConfig.TICK_RATE:
world.step()
var low_gain := low.hp - 1
var world2 := SimWorld.new(1)
var high := world2.add_player(PEER, "tester")
high.max_hp = Progression.max_hp_for_level(Progression.MAX_LEVEL)
high.hp = 1
for _i in SimConfig.TICK_RATE:
world2.step()
assert_gt(high.hp - 1, low_gain,
"a levelled character should regain more per second, not the same")
func test_it_never_exceeds_maximum() -> void:
var p := _player()
p.hp = p.max_hp - 1
for _i in SimConfig.TICK_RATE * 30:
world.step()
assert_eq(p.hp, p.max_hp)
func test_the_dead_do_not_heal() -> void:
var p := _player()
p.alive = false
p.hp = 0
for _i in SimConfig.TICK_RATE * 10:
world.step()
assert_eq(p.hp, 0, "regeneration must not quietly revive a downed player")
## A replica never heals anyone: health, like damage, is the server's to decide.
func test_a_replica_world_does_not_regenerate() -> void:
world.authoritative = false
var p := _player()
p.hp = 1
for _i in SimConfig.TICK_RATE * 5:
world.step()
assert_eq(p.hp, 1)
## Slow enough that it cannot out-heal being shot, which is what makes an
## out-of-combat gate unnecessary.
func test_it_cannot_outpace_incoming_fire() -> void:
var p := _player()
p.pos = Vector2.ZERO
p.spawn_grace = 0
var start := p.hp
for _i in 60:
world.pool.spawn(Vector2.ZERO, Vector2.ZERO, 6.0, 60, 5,
SimConfig.TEAM_ENEMY, SimConfig.KIND_ORB)
world.step()
assert_lt(p.hp, start, "standing in fire must still lose health")
# --- Where you are decides how fast you heal ---------------------------------
# Arriving anywhere used to set health to maximum, which made a run free: walk
# in, take what you can, walk out, walk back in whole. The hub pays that off
# over about ten seconds instead.
func test_the_hub_heals_far_faster_than_a_dungeon() -> void:
var hub := Instance.make_lobby(1)
assert_eq(hub.world.regen_percent_per_sec,
SimConfig.LOBBY_HP_REGEN_PERCENT_PER_SEC)
var run := Instance.make_dungeon(2, 99)
assert_eq(run.world.regen_percent_per_sec, SimConfig.HP_REGEN_PERCENT_PER_SEC)
assert_gt(hub.world.regen_percent_per_sec, run.world.regen_percent_per_sec,
"recovering is what the hub is for; a dungeon must not be a rest stop")
func test_a_full_bar_takes_about_ten_seconds_in_the_hub() -> void:
var hub := Instance.make_lobby(1)
var p := hub.world.add_player(1, "hurt")
p.hp = 1
for _t in SimConfig.TICK_RATE * 10:
hub.step()
assert_eq(p.hp, p.max_hp, "ten seconds of standing in the hub is a full bar")
func test_the_hub_still_takes_a_noticeable_moment() -> void:
# Fast, but not the instant heal this replaced: half a second must not do it.
var hub := Instance.make_lobby(1)
var p := hub.world.add_player(1, "hurt")
p.hp = 1
for _t in SimConfig.TICK_RATE / 2:
hub.step()
assert_lt(p.hp, p.max_hp, "arriving in the hub is not a heal button")
assert_gt(p.hp, 1, "but it is visibly happening")
func test_a_dungeon_cannot_be_waited_out() -> void:
var run := Instance.make_dungeon(2, 99)
var p := run.world.add_player(1, "hurt")
p.hp = 1
p.spawn_grace = 0
var before := p.hp
for _t in SimConfig.TICK_RATE * 10:
p.regenerate(run.world.regen_percent_per_sec)
assert_lt(p.hp - before, p.max_hp / 10,
"ten seconds in a dungeon must not undo a fight")