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.
This commit is contained in:
143
tests/test_save_load.gd
Normal file
143
tests/test_save_load.gd
Normal file
@@ -0,0 +1,143 @@
|
||||
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 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")
|
||||
|
||||
world.queue_free()
|
||||
S.delete_world(id)
|
||||
DirAccess.remove_absolute(S.root_dir)
|
||||
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
|
||||
1
tests/test_save_load.gd.uid
Normal file
1
tests/test_save_load.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://dkskynbe6enoo
|
||||
175
tests/test_survival_core.gd
Normal file
175
tests/test_survival_core.gd
Normal file
@@ -0,0 +1,175 @@
|
||||
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
|
||||
1
tests/test_survival_core.gd.uid
Normal file
1
tests/test_survival_core.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://b8vgvxcn2b5ho
|
||||
@@ -1,19 +1,39 @@
|
||||
extends SceneTree
|
||||
## Terrain edge check: there must be no ground below the inland waterline in the
|
||||
## band just inside the boundary wall. Ground like that shows up as inland water
|
||||
## hanging over a drop (the lake water surface stops at the wall) and makes pits
|
||||
## the player can fall into and not climb out of.
|
||||
## Terrain edge check, for several seeds: in the band just inside the boundary
|
||||
## wall there must be (1) no water deeper than you can wade out of, and (2) no
|
||||
## water without a bank, i.e. ground that drops away outward. The inland water
|
||||
## surface stops at the wall, so unbanked water hangs in the air over the drop.
|
||||
## This is the bug where the player fell into floating water by the sea cliff.
|
||||
## Shallow ponds at the foot of the mountains are fine: the slope holds them.
|
||||
##
|
||||
## Run: godot --headless --path . --script res://tests/test_terrain_edges.gd
|
||||
|
||||
const BAND := 10.0 # metres inside the wall to check
|
||||
const BAND := 10.0 # metres inside the wall to check
|
||||
const MAX_EDGE_DEPTH := 1.0 # deepest allowed water near the edge (wadeable)
|
||||
const BANK_CHECK := 12.0 # how far outward the bank must rise above water
|
||||
|
||||
## The original island plus a spread of other seeds: new worlds get random
|
||||
## seeds, so the edge rule has to hold for any of them.
|
||||
const SEEDS := [1337, 1, 42, 777, 4242, 9001, 31337, 123456]
|
||||
|
||||
func _initialize() -> void:
|
||||
var terrain: Node3D = load("res://scripts/world/terrain.gd").new()
|
||||
root.add_child(terrain) # builds in _ready
|
||||
_run.call_deferred(terrain)
|
||||
_all.call_deferred()
|
||||
|
||||
func _run(terrain: Node3D) -> void:
|
||||
func _all() -> void:
|
||||
var failed := 0
|
||||
for s in SEEDS:
|
||||
var terrain: Node3D = load("res://scripts/world/terrain.gd").new()
|
||||
terrain.noise_seed = s
|
||||
root.add_child(terrain) # builds in _ready
|
||||
var bad := _check(terrain)
|
||||
print("seed %6d: %s" % [s, "ok" if bad == 0 else "%d bad samples" % bad])
|
||||
if bad > 0:
|
||||
failed += 1
|
||||
terrain.free()
|
||||
print("RESULT: %s (%d/%d seeds clean)" % ["PASS" if failed == 0 else "FAIL", SEEDS.size() - failed, SEEDS.size()])
|
||||
quit(1 if failed > 0 else 0)
|
||||
|
||||
func _check(terrain: Node3D) -> int:
|
||||
var bad := 0
|
||||
var worst := INF
|
||||
var worst_at := Vector2.ZERO
|
||||
@@ -24,7 +44,16 @@ func _run(terrain: Node3D) -> void:
|
||||
while z < half:
|
||||
if terrain.inside_boundary(x, z, 0.0) and not terrain.inside_boundary(x, z, BAND):
|
||||
var h: float = terrain.sample_height(x, z)
|
||||
if h < terrain.water_level + 0.2:
|
||||
# Hazard 1: a pit too deep to wade out of near the edge.
|
||||
var too_deep: bool = h < terrain.water_level - MAX_EDGE_DEPTH
|
||||
# Hazard 2: water that isn't held in by a bank. The inland water
|
||||
# surface stops at the wall, so wet ground must rise again just
|
||||
# outward, or the water edge hangs over a drop.
|
||||
var unbanked := false
|
||||
if h < terrain.water_level:
|
||||
var out := Vector2(x, z).normalized() * BANK_CHECK
|
||||
unbanked = terrain.sample_height(x + out.x, z + out.y) < terrain.water_level
|
||||
if too_deep or unbanked:
|
||||
bad += 1
|
||||
if h < worst:
|
||||
worst = h
|
||||
@@ -32,8 +61,6 @@ func _run(terrain: Node3D) -> void:
|
||||
z += 1.0
|
||||
x += 1.0
|
||||
if bad > 0:
|
||||
print("FAIL: %d samples near the wall are below the waterline; lowest %.1f m at (%.0f, %.0f), sea weight %.2f" % [
|
||||
bad, worst, worst_at.x, worst_at.y, terrain.sea_weight(worst_at.x, worst_at.y)])
|
||||
else:
|
||||
print("PASS: ground near the wall stays above the waterline")
|
||||
quit(1 if bad > 0 else 0)
|
||||
print(" lowest %.1f m at (%.0f, %.0f), sea weight %.2f" % [
|
||||
worst, worst_at.x, worst_at.y, terrain.sea_weight(worst_at.x, worst_at.y)])
|
||||
return bad
|
||||
|
||||
1
tests/test_terrain_edges.gd.uid
Normal file
1
tests/test_terrain_edges.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://s8y1xr0ylelw
|
||||
Reference in New Issue
Block a user