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.
144 lines
5.4 KiB
GDScript
144 lines
5.4 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 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
|