feat: add deterministic world chest loot

This commit is contained in:
goose
2026-10-03 13:34:12 -04:00
parent 795d25fb4a
commit a251359885
19 changed files with 423 additions and 7 deletions
+4 -1
View File
@@ -34,7 +34,10 @@ func test_real_input_opens_door_then_chest_and_preserves_stairs_route() -> void:
assert_eq(game.last_turn_results[0].kind, &"chest_opened")
assert_eq(state.map.get_cell(Vector2i(4, 7)), GridMapState.CellKind.FLOOR)
assert_eq(hud.get_context_prompt(), "")
assert_string_contains(event_log.get_parsed_text(), "You open the chest.")
assert_string_contains(event_log.get_parsed_text(), "Coffre ouvert :")
assert_true(game.get_node("HUD/LootPanel").visible)
game._unhandled_input(_key_event(KEY_SPACE))
assert_false(game.get_node("HUD/LootPanel").visible)
game._unhandled_input(_key_event(KEY_UP))
assert_eq(state.player.cell, Vector2i(4, 7))
@@ -23,6 +23,9 @@ func test_real_input_event_completes_door_to_stairs_descent_and_arrives_on_floor
game._unhandled_input(_key_event(keycode))
assert_eq(state.player.cell, Vector2i(4, 8))
game._unhandled_input(_key_event(KEY_E))
assert_true(game.get_node("HUD/LootPanel").visible)
game._unhandled_input(_key_event(KEY_SPACE))
assert_false(game.get_node("HUD/LootPanel").visible)
for keycode in [KEY_UP, KEY_UP, KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, KEY_UP, KEY_UP]:
game._unhandled_input(_key_event(keycode))
+70
View File
@@ -0,0 +1,70 @@
extends GutTest
const GameScene = preload("res://scenes/Game.tscn")
var game: Game
func before_each() -> void:
game = GameScene.instantiate()
add_child_autofree(game)
await get_tree().process_frame
func test_real_chest_open_displays_french_reward_then_take_all_updates_inventory_and_closes_panel() -> void:
var state := game.get_game_state()
state.enemies.clear()
var chest_cell := Vector2i(4, 7)
state.floor_state.chests[chest_cell] = {
"loot_table_id": &"floor1_reward",
"opened": false,
"reward_seed": "e2e-reward",
}
var journal := game.get_node("HUD/EventLog") as RichTextLabel
for keycode in [KEY_E, KEY_UP, KEY_UP, KEY_UP, KEY_UP]:
await _dispatch_key(keycode)
assert_eq(state.player.cell, Vector2i(4, 8))
await _dispatch_key(KEY_E)
var chest_event := game.get_run_state().recent_events(1)[0]
var panel := game.get_node("HUD/LootPanel") as Control
var entries := game.get_node("HUD/LootPanel/Entries") as RichTextLabel
assert_eq(chest_event.kind, &"chest_opened")
assert_true(chest_event.payload.has("items"))
assert_true(chest_event.payload.has("gold"))
assert_true(chest_event.payload.has("empty"))
assert_true(chest_event.payload.has("trap"))
assert_true(state.floor_state.chests[chest_cell].opened)
assert_eq(state.map.get_cell(chest_cell), GridMapState.CellKind.FLOOR)
assert_true(panel.visible)
assert_string_contains(journal.get_parsed_text(), "Coffre ouvert :")
assert_ne(entries.get_parsed_text(), "")
var expected_items: Array = chest_event.payload.items
var expected_gold := int(chest_event.payload.gold)
await _dispatch_key(KEY_ENTER)
assert_false(panel.visible)
assert_eq(game.get_run_state().gold, expected_gold)
for item: Dictionary in expected_items:
assert_true(_inventory_has(item.item_id, int(item.quantity)))
assert_eq(game.get_run_state().recent_events(1)[0].kind, &"loot_taken")
func _inventory_has(item_id: StringName, quantity: int) -> bool:
for item in game.get_run_state().inventory.items:
if item.definition.id == item_id and item.quantity >= quantity:
return true
return false
func _dispatch_key(keycode: Key) -> void:
Input.parse_input_event(_key_event(keycode, true))
await get_tree().process_frame
Input.parse_input_event(_key_event(keycode, false))
await get_tree().process_frame
func _key_event(keycode: Key, pressed: bool) -> InputEventKey:
var event := InputEventKey.new()
event.keycode = keycode
event.physical_keycode = keycode
event.pressed = pressed
return event
@@ -0,0 +1 @@
uid://ckcocm2kp2i45
+5 -1
View File
@@ -41,7 +41,11 @@ func test_opening_chest_replaces_it_with_floor_refreshes_fov_and_gets_one_enemy_
enemy_response_count += 1
assert_eq(results[0].kind, &"chest_opened")
assert_eq(results[0].message, "You open the chest.")
assert_string_contains(results[0].message, "Coffre ouvert :")
assert_true(results[0].payload.has("items"))
assert_true(results[0].payload.has("gold"))
assert_true(results[0].payload.has("empty"))
assert_true(results[0].payload.has("trap"))
assert_eq(results[0].to_cell, chest)
assert_eq(state.map.get_cell(chest), GridMapState.CellKind.FLOOR)
assert_true(state.map.is_walkable(chest))
+54
View File
@@ -0,0 +1,54 @@
extends GutTest
const ContentCatalogType = preload("res://scripts/domain/content/content_catalog.gd")
const LootResolverType = preload("res://scripts/domain/items/loot_resolver.gd")
const WorldChestType = preload("res://scripts/domain/items/world_chest.gd")
var catalog: ContentCatalog
func before_each() -> void:
catalog = ContentCatalogType.load_from_directory("res://data/ascii")
func test_catalog_table_resolution_is_deterministic_and_preserves_gold_items_and_empty_shape() -> void:
var table := catalog.get_loot_table(&"floor1_reward")
var first: Dictionary = LootResolverType.resolve(table, "D2-LOOT-SEED", "floor-1/chest-4-7")
var second: Dictionary = LootResolverType.resolve(table, "D2-LOOT-SEED", "floor-1/chest-4-7")
assert_eq(first, second)
assert_eq(first.items is Array, true)
assert_typeof(first.gold, TYPE_INT)
assert_typeof(first.empty, TYPE_BOOL)
assert_eq(first.empty, first.items.is_empty() and first.gold == 0)
for item: Dictionary in first.items:
assert_true(item.has("item_id"))
assert_true(item.has("quantity"))
assert_gt(int(item.quantity), 0)
func test_world_chest_keeps_table_trap_lock_and_opened_state_and_stores_its_single_reward() -> void:
var chest: WorldChest = WorldChestType.new({
"loot_table_id": "floor1_reward",
"trap_id": "trap_spikes",
"lock_id": "tutorial_lock",
"opened": false,
})
assert_eq(chest.loot_table_id, &"floor1_reward")
assert_eq(chest.trap_id, &"trap_spikes")
assert_eq(chest.lock_id, &"tutorial_lock")
assert_false(chest.opened)
assert_true(chest.is_locked())
assert_false(chest.open({"items": [], "gold": 0, "empty": true, "trap": &"trap_spikes"}))
assert_false(chest.opened)
chest.lock_id = &""
var reward := {"items": [{"item_id": &"consumable_bandage", "quantity": 2}], "gold": 12, "empty": false, "trap": &"trap_spikes"}
assert_true(chest.open(reward))
assert_true(chest.opened)
assert_eq(chest.reward, reward)
assert_false(chest.open(reward))
assert_eq(chest.to_dict().loot_table_id, &"floor1_reward")
assert_eq(chest.to_dict().trap_id, &"trap_spikes")
assert_eq(chest.to_dict().lock_id, &"")
assert_true(chest.to_dict().opened)
assert_eq(chest.to_dict().reward, reward)
+1
View File
@@ -0,0 +1 @@
uid://bytnd0jif37jo