Files
Cinderwild/tests/test_survival_core.gd
Paul Bailey 4640f5e577 M1a: survival core, pause menu, title screen and save/load
Design: docs/SURVIVAL_DESIGN.md (core loop, systems, architecture, phases).

Survival core (M1a):
- Items/recipes as data (data/*.json) in the addon SDK's define_item /
  define_recipe shape; procedural item meshes and offscreen-rendered icons
- Inventory class (40 slots, hotbar = row 1) shared by player and chests;
  click / right-click split / shift-click quick-move
- Actions autoload: the intent layer (craft, pickup, harvest_hit, place,
  transfer) that validates every change; becomes the RPC surface in M2
- Gathering: ground pickups (sticks, stones, flint); trees chopped with an
  axe fall and leave stumps; boulders mined with a pickaxe shrink per hit
- Crafting screen with station checks (workbench); placement mode with
  green/red ghost, rotation and overlap/slope validation
- Pieces: campfire (light, flames, embers), workbench, chest (16 slots)
- HUD: hotbar, held tool view model with swing, prompts, toasts

Menus and saving:
- ESC pause menu: resume, controls, fullscreen, mouse sensitivity, quit
- Title screen over a flyover of the island: Continue / New World (name +
  seed) / Load World (with delete) / Quit
- Saves autoload: one folder per world under user://worlds, atomic writes
  with a .bak; stores only player changes (player, pieces, chest contents,
  harvested resources, taken pickups) and regenerates the rest from the seed
- Autosave every 2 min, Save & Quit to title/desktop, save on window close

Fixes: UI screens now fill the viewport (were pinned top-left); title
backdrop no longer captures the mouse.

Tests: test_survival_core (31 checks), test_save_load (round trip + slots),
test_terrain_edges now checks 8 seeds for deep pits / unbanked water.
2026-11-04 00:34:46 +00:00

176 lines
7.1 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 _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)
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)
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