Files
Cinderwild/tests/test_survival_core.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

179 lines
7.2 KiB
GDScript

extends SceneTree
## M1a core loop, end to end through the Actions intent layer: inventory
## stacking, crafting (hand + station), chopping, mining, pickups, placing
## pieces and chest storage.
##
## Run: godot --headless --path . --script res://tests/test_survival_core.gd
var _fails := 0
var _finished := false ## set at the very end; a script error aborts before it
var _actions: Node
var _db: Node
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 _run() -> void:
# Autoloads aren't resolvable by name in a --script SceneTree before the
# main loop runs, so grab them from the root.
_actions = root.get_node("Actions")
_db = root.get_node("ItemDB")
_test_inventory()
var world: Node3D = load("res://scenes/world.tscn").instantiate()
root.add_child(world)
for _i in range(5):
await physics_frame
await _test_world(world)
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 _test_inventory() -> void:
print("inventory")
var inv := Inventory.new(4)
check(inv.add("stick", 120) == 0, "120 sticks fit in 4 slots of 50")
check(inv.get_slot(0).count == 50 and inv.get_slot(2).count == 20, "stacks fill to 50 then spill")
check(inv.add("stone", 200) == 150, "overflow is returned as leftover (1 free slot = 50 fit)")
check(inv.remove("stick", 70) and inv.count("stick") == 50, "remove takes from the end")
check(not inv.remove("flint", 1), "can't remove what you don't have")
var cursor = inv.click(0, null, true)
check(cursor.count == 25 and inv.get_slot(0).count == 25, "right-click picks up half")
cursor = inv.click(0, cursor, false)
check(cursor == null and inv.get_slot(0).count == 50, "left-click merges it back")
func _test_world(world: Node3D) -> void:
var player: CharacterBody3D = world.get_node("Player")
var terrain: Node3D = world.get_node("Terrain")
var inv: Inventory = player.inventory
print("crafting")
check(not _actions.craft(player, "stone_axe"), "can't craft an axe with nothing")
inv.add("stick", 4); inv.add("flint", 3); inv.add("stone", 3)
check(_actions.craft(player, "stone_axe") and inv.count("stone_axe") == 1, "craft stone axe")
check(inv.count("stick") == 2 and inv.count("flint") == 1, "axe consumed 2 sticks + 2 flint")
check(_actions.craft(player, "stone_pickaxe") and inv.count("stone_pickaxe") == 1, "craft stone pickaxe")
print("chopping")
var tree := _nearest(world, player, "tree")
check(tree != null, "found a harvestable tree")
if tree != null:
player.global_position = tree.global_position + Vector3(1.5, 0.2, 0)
_hold(player, "stone_axe")
var hits := 0
while not tree.depleted and hits < 20:
_actions.harvest_hit(player, tree, tree.global_position + Vector3(0, 1, 0))
hits += 1
check(tree.depleted, "tree felled after %d hits" % hits)
check(inv.count("wood") >= 4, "got wood from the tree (%d)" % inv.count("wood"))
check(not _actions.harvest_hit(player, tree, tree.global_position), "a stump can't be chopped again")
print("mining")
var rock := _nearest(world, player, "rock")
check(rock != null, "found a mineable boulder")
if rock != null:
player.global_position = rock.global_position + Vector3(2.0, 0.5, 0)
_hold(player, "stone_axe")
check(not _actions.harvest_hit(player, rock, rock.global_position), "an axe can't mine rock")
_hold(player, "stone_pickaxe")
var stone_before := inv.count("stone")
var hits := 0
while is_instance_valid(rock) and not rock.depleted and hits < 20:
_actions.harvest_hit(player, rock, rock.global_position)
hits += 1
check(inv.count("stone") > stone_before and inv.count("flint") >= 1, "mined stone and flint")
print("pickups")
var pk: Node3D = null
var best := INF
for p in world.get_node("Props/Pickups").get_children():
var d := player.global_position.distance_to(p.global_position)
if d < best:
best = d
pk = p
check(pk != null, "pickups exist on the ground")
if pk != null:
var id: String = pk.item_id
var before := inv.count(id)
player.global_position = pk.global_position + Vector3(30.0, 0.5, 0)
check(not _actions.pickup(player, pk), "out-of-reach pickup is refused")
player.global_position = pk.global_position + Vector3(0.5, 0.5, 0)
var picked: bool = _actions.pickup(player, pk)
check(picked and inv.count(id) == before + 1, "picked up a %s" % id)
print("building")
inv.add("wood", 30); inv.add("flint", 4); inv.add("stick", 8)
check(not _actions.craft(player, "chest"), "chest needs a workbench")
check(_actions.craft(player, "workbench"), "craft workbench by hand")
var spot := _flat_spot(terrain, world)
player.global_position = spot + Vector3(0, 0.5, 3.0)
var slot := _hold(player, "workbench")
var bench_xf := Transform3D(Basis(), spot)
check(world.placement_valid(ItemDB_get(player, "workbench"), bench_xf), "flat clear ground is valid")
check(_actions.place(player, slot, bench_xf, world), "placed the workbench")
check(inv.count("workbench") == 0, "placing consumed the item")
await physics_frame # let the physics server register the new body
check(not world.placement_valid(ItemDB_get(player, "workbench"), bench_xf), "can't place on top of it")
check(_actions.craft(player, "chest"), "chest crafts next to the workbench")
var chest_xf := Transform3D(Basis(), Vector3(spot.x + 2.6, terrain.sample_height(spot.x + 2.6, spot.z), spot.z))
slot = _hold(player, "chest")
check(_actions.place(player, slot, chest_xf, world), "placed the chest")
var chest: Node = null
for c in world.get_node("Pieces").get_children():
if c.item_id == "chest":
chest = c
check(chest != null, "chest exists in the world")
if chest != null:
var wood_slot := -1
for i in range(inv.size()):
if inv.get_slot(i) != null and inv.get_slot(i).id == "wood":
wood_slot = i
var moving: int = inv.get_slot(wood_slot).count
_actions.transfer(player, inv, wood_slot, chest.inventory, chest)
check(chest.inventory.count("wood") == moving, "shift-click stored %d wood in the chest" % moving)
_finished = true
func ItemDB_get(_p, id: String) -> Dictionary:
return _db.get_item(id)
## Puts one of `id` into hotbar slot 0 and selects it. Returns the slot.
func _hold(player: Node, id: String) -> int:
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)
return 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
func _flat_spot(terrain: Node3D, world: Node3D) -> Vector3:
var def: Dictionary = _db.get_item("workbench")
for r in range(0, 60, 3):
for k in range(12):
var a := TAU * k / 12.0
var x := cos(a) * r
var z := sin(a) * r
var p := Vector3(x, terrain.sample_height(x, z), z)
if world.placement_valid(def, Transform3D(Basis(), p)) and world.placement_valid(def, Transform3D(Basis(), p + Vector3(2.6, 0, 0))):
return p
return Vector3.ZERO