Files
Cinderwild/tests/test_save_load.gd
Paul Bailey fc7eb3cfed M1b: survival stats, day/night, fire fuel, cooking, foraging, torches, death + grave
- Health, stamina, hunger, warmth and wet with HUD bars, status tags and clock
- 20-minute day/night cycle with moon, stars and lit clouds
- Campfire fuel (2 min start, 15 min max), heat radius, cooking needs a lit fire
- Berry bushes (regrow 10 min), mushrooms, roasted mushroom, torch
- Stamina costs for sprint, jump and harvest hits
- Death respawns at island start and leaves a grave bag
- Saves include stats, time of day, fire fuel, regrowth timers and graves
- New test_survival_stats suite; tests fail if a script error aborts them
2026-11-04 01:19:41 +00:00

147 lines
5.5 KiB
GDScript

extends SceneTree
## Save/load round trip: play a bit (chop a tree, mine a boulder, pick
## something up, build a workbench + chest with items in it, walk somewhere),
## save, throw the world away, load it fresh and check everything came back.
## Also covers world slots (create/list/delete) and a seed being honoured.
##
## Uses its own folder (user://test_worlds) so real saves are never touched.
## Run: godot --headless --path . --script res://tests/test_save_load.gd
var _fails := 0
var _finished := false
var A: Node # Actions
var S: Node # Saves
var DB: Node # ItemDB
func check(cond: bool, what: String) -> void:
print((" ok " if cond else " FAIL ") + what)
if not cond:
_fails += 1
func _initialize() -> void:
_run.call_deferred()
func _spawn_world() -> Node3D:
var w: Node3D = load("res://scenes/world.tscn").instantiate()
root.add_child(w)
for _i in range(5):
await physics_frame
return w
func _run() -> void:
A = root.get_node("Actions")
S = root.get_node("Saves")
DB = root.get_node("ItemDB")
S.root_dir = "user://test_worlds"
for w in S.list_worlds():
S.delete_world(w.id)
print("slots")
var id: String = S.create_world("Test Island", 4242)
var id2: String = S.create_world("Test Island", 7)
check(id != id2, "two worlds with the same name get different ids (%s, %s)" % [id, id2])
check(S.list_worlds().size() == 2, "both worlds are listed")
S.delete_world(id2)
check(S.list_worlds().size() == 1, "deleting a world removes it")
S.current_id = id
print("play + save")
var world: Node3D = await _spawn_world()
var terrain: Node3D = world.get_node("Terrain")
check(terrain.noise_seed == 4242, "world generated from its saved seed")
var player: CharacterBody3D = world.get_node("Player")
var inv: Inventory = player.inventory
var props: Node3D = world.get_node("Props")
inv.add("stone_axe", 1); inv.add("stone_pickaxe", 1)
var tree := _nearest(world, player, "tree")
player.global_position = tree.global_position + Vector3(1.5, 0.2, 0)
_hold(player, "stone_axe")
while not tree.depleted:
A.harvest_hit(player, tree, tree.global_position)
var tree_uid: String = tree.uid
var rock := _nearest(world, player, "rock")
var rock_uid: String = rock.uid
player.global_position = rock.global_position + Vector3(2.0, 0.5, 0)
_hold(player, "stone_pickaxe")
A.harvest_hit(player, rock, rock.global_position)
A.harvest_hit(player, rock, rock.global_position)
var rock_hp: float = rock.hp
var pk_uid: String = props.pickups.keys()[0]
var pk: Node3D = props.pickups[pk_uid]
player.global_position = pk.global_position + Vector3(0.4, 0.4, 0)
A.pickup(player, pk)
inv.add("workbench", 1); inv.add("chest", 1); inv.add("wood", 33)
var spot := Vector3(player.global_position.x + 3.0, 0, player.global_position.z)
spot.y = terrain.sample_height(spot.x, spot.z)
world.spawn_piece(DB.get_item("workbench"), Transform3D(Basis(Vector3.UP, 0.7), spot))
var chest: Node = world.spawn_piece(DB.get_item("chest"), Transform3D(Basis(), spot + Vector3(0, 0, 3)))
chest.inventory.add("flint", 7)
chest.inventory.add("wood", 12)
player.global_position = Vector3(5.0, terrain.sample_height(5.0, 6.0) + 1.0, 6.0)
player.rotation.y = 1.25
player.select(3)
var wood_before := inv.count("wood")
var pos_before := player.global_position
check(world.save_now(), "save_now() succeeded")
check(FileAccess.file_exists(S.root_dir.path_join(id).path_join("world.json")), "world.json written")
world.queue_free()
await process_frame
await process_frame
print("load")
world = await _spawn_world()
player = world.get_node("Player")
inv = player.inventory
props = world.get_node("Props")
check(player.global_position.distance_to(pos_before) < 0.6, "player back where they saved (%.2f m off)" % player.global_position.distance_to(pos_before))
check(absf(player.rotation.y - 1.25) < 0.01 and player.selected == 3, "facing and hotbar slot restored")
check(inv.count("wood") == wood_before and inv.count("stone_axe") == 1, "inventory restored")
check(props.resource_nodes[tree_uid].depleted, "felled tree is still a stump")
var r = props.resource_nodes[rock_uid]
check(is_instance_valid(r) and absf(r.hp - rock_hp) < 0.01, "half-mined boulder kept its damage")
var pk2 = props.pickups[pk_uid]
check(not is_instance_valid(pk2) or pk2.taken, "picked-up item stays picked up")
var benches := 0
var chest2: Node = null
for p in world.get_node("Pieces").get_children():
if p.item_id == "workbench":
benches += 1
elif p.item_id == "chest":
chest2 = p
check(benches == 1, "workbench rebuilt")
check(chest2 != null and chest2.inventory.count("flint") == 7 and chest2.inventory.count("wood") == 12, "chest rebuilt with its contents")
check(S.read_meta(id).play_time >= 0.0, "meta updated")
_finished = true
world.queue_free()
S.delete_world(id)
DirAccess.remove_absolute(S.root_dir)
check(_finished, "test ran to the end (no script errors)")
print("RESULT: %s (%d failures)" % ["PASS" if _fails == 0 else "FAIL", _fails])
quit(1 if _fails > 0 else 0)
func _hold(player: Node, id: String) -> void:
var inv: Inventory = player.inventory
for i in range(inv.size()):
var s = inv.get_slot(i)
if s != null and s.id == id:
var tmp = inv.get_slot(0)
inv.set_slot(0, s)
inv.set_slot(i, tmp)
break
player.select(0)
func _nearest(world: Node, player: Node3D, kind: String) -> Node3D:
var best: Node3D = null
var bd := INF
for n in world.get_tree().get_nodes_in_group("harvestable"):
if n.kind == kind and not n.depleted:
var d := player.global_position.distance_to(n.global_position)
if d < bd:
bd = d
best = n
return best