From 9bd003102cebc00bd76a668d7c761a308c1b3c30 Mon Sep 17 00:00:00 2001 From: goose Date: Sun, 4 Oct 2026 11:08:19 -0400 Subject: [PATCH] feat: add safe rooms and repeatable Bobka rations --- scripts/domain/combat/condition_engine.gd | 11 ++- scripts/domain/inventory/inventory_state.gd | 7 ++ scripts/domain/run/run_state.gd | 3 + scripts/domain/world/floor_generator.gd | 7 ++ scripts/domain/world/floor_populator.gd | 14 +++- scripts/domain/world/floor_state.gd | 23 ++++++ scripts/domain/world/safe_room_service.gd | 56 +++++++++++++++ scripts/domain/world/safe_room_service.gd.uid | 1 + scripts/game/game.gd | 4 ++ task-PR-2-report | 37 ++++++++++ tests/e2e/test_bobka_safe_room_e2e.gd | 70 +++++++++++++++++++ tests/e2e/test_bobka_safe_room_e2e.gd.uid | 1 + .../integration/test_safe_room_generation.gd | 29 ++++++++ .../test_safe_room_generation.gd.uid | 1 + tests/unit/test_safe_room_service.gd | 34 +++++++++ tests/unit/test_safe_room_service.gd.uid | 1 + 16 files changed, 297 insertions(+), 2 deletions(-) create mode 100644 scripts/domain/world/safe_room_service.gd create mode 100644 scripts/domain/world/safe_room_service.gd.uid create mode 100644 task-PR-2-report create mode 100644 tests/e2e/test_bobka_safe_room_e2e.gd create mode 100644 tests/e2e/test_bobka_safe_room_e2e.gd.uid create mode 100644 tests/integration/test_safe_room_generation.gd create mode 100644 tests/integration/test_safe_room_generation.gd.uid create mode 100644 tests/unit/test_safe_room_service.gd create mode 100644 tests/unit/test_safe_room_service.gd.uid diff --git a/scripts/domain/combat/condition_engine.gd b/scripts/domain/combat/condition_engine.gd index 3d2060f..998556b 100644 --- a/scripts/domain/combat/condition_engine.gd +++ b/scripts/domain/combat/condition_engine.gd @@ -2,6 +2,7 @@ class_name ConditionEngine extends RefCounted const StatusEffectType = preload("res://scripts/domain/combat/status_effect.gd") +const SafeRoomServiceType = preload("res://scripts/domain/world/safe_room_service.gd") var diagnostics := PackedStringArray() @@ -116,6 +117,14 @@ func apply_effect(effect: Dictionary, context: Dictionary) -> Dictionary: if not moderation_fn.is_valid(): return {"applied": false, "type": kind, "reason": &"missing_moderation_context"} return moderation_fn.call(StringName(effect.get("reason", "propos_interdits"))) + &"give_item": + var run: RunState = context.get("run_state") + var npc_id: StringName = StringName(context.get("npc_id", "")) + var npc_content_id: StringName = StringName(context.get("npc_content_id", "")) + if run == null or npc_id.is_empty() or npc_content_id != SafeRoomServiceType.BOBKA_CONTENT_ID or StringName(effect.get("item", "")) != SafeRoomServiceType.RATION_ITEM_ID: + return {"applied": false, "type": kind, "reason": &"unsupported_item_grant"} + var ration_events := SafeRoomServiceType.give_bobka_ration(run, npc_id) + return {"applied": not ration_events.is_empty(), "type": kind, "events": ration_events} &"join_party": var party: PartyState = context.get("party") var recruit: ActorState = context.get("recruit") @@ -124,7 +133,7 @@ func apply_effect(effect: Dictionary, context: Dictionary) -> Dictionary: return {"applied": false, "type": kind, "reason": &"missing_party_context"} var result := party.recruit(recruit, player.cell, map, context.get("blocked_cells", {})) return {"applied": bool(result.get("accepted", false)), "type": kind, "result": result} - &"message", &"give_item", &"remove_item", &"learn_spell", &"learn_random_spell", &"complete_objective", &"endanger_tourist", &"augment_companion", &"tourist_selfie": + &"message", &"remove_item", &"learn_spell", &"learn_random_spell", &"complete_objective", &"endanger_tourist", &"augment_companion", &"tourist_selfie": # Later content slices own the corresponding inventory, quest, NPC, and spell mutations. pass _: diff --git a/scripts/domain/inventory/inventory_state.gd b/scripts/domain/inventory/inventory_state.gd index da6feff..d96d460 100644 --- a/scripts/domain/inventory/inventory_state.gd +++ b/scripts/domain/inventory/inventory_state.gd @@ -22,6 +22,13 @@ func add_item(item: ItemInstance) -> Dictionary: items.append(item) return _result(true, &"", item) +func count_by_definition(item_id: StringName) -> int: + var total := 0 + for item: ItemInstance in items: + if item.definition.id == item_id: + total += item.quantity + return total + func item_by_id(instance_id: StringName) -> ItemInstance: for item in items: if item.instance_id == instance_id: diff --git a/scripts/domain/run/run_state.gd b/scripts/domain/run/run_state.gd index 44b8a09..95e8e70 100644 --- a/scripts/domain/run/run_state.gd +++ b/scripts/domain/run/run_state.gd @@ -16,6 +16,9 @@ var log: Array[GameEvent] = [] var gold := 0 # D1 owns inventory at run scope so it persists while the player changes floors. var inventory: InventoryState = InventoryState.new() +# Imported dialogue effects resolve item definitions through this authoritative +# runtime catalog; it is assigned by Game for new and loaded runs. +var content_catalog: ContentCatalog # D3 keeps item identity and equipment with the run so swaps persist across floors. var equipment: EquipmentState = EquipmentState.new() var identification: ItemIdentification = ItemIdentification.new() diff --git a/scripts/domain/world/floor_generator.gd b/scripts/domain/world/floor_generator.gd index ebed650..46dce31 100644 --- a/scripts/domain/world/floor_generator.gd +++ b/scripts/domain/world/floor_generator.gd @@ -206,6 +206,13 @@ static func _assign_room_kinds(rng: DeterministicRng, rooms: Array[RoomState], e if UNIQUE_ROOM_KINDS.has(kind): used_unique[kind] = true + # Weighted room kinds can exhaust either safe kind because both are unique. + # Reserve one deterministic non-entrance/non-exit candidate when that occurs. + var has_safe_room := candidates.any(func(room_index: int) -> bool: return rooms[room_index].kind in [&"refuge", &"sanctuary"]) + if not has_safe_room and not candidates.is_empty(): + var safe_index := candidates[rng.next_int() % candidates.size()] + rooms[safe_index].kind = &"refuge" if rng.next_int() % 2 == 0 else &"sanctuary" + static func _weighted_room_kind(rng: DeterministicRng, floor_number: int, used_unique: Dictionary[StringName, bool]) -> StringName: var available: Array[StringName] = [] var total_weight := 0 diff --git a/scripts/domain/world/floor_populator.gd b/scripts/domain/world/floor_populator.gd index 0e236a5..5e71374 100644 --- a/scripts/domain/world/floor_populator.gd +++ b/scripts/domain/world/floor_populator.gd @@ -26,8 +26,10 @@ static func populate(floor: FloorState, catalog: ContentCatalog, population_seed _spawn_chest(floor, catalog, room, rng, occupied) &"trap": _spawn_trap(floor, catalog, room, rng, occupied) - &"shop", &"guild", &"sanctuary", &"refuge": + &"shop", &"guild": serial = _spawn_npc(floor, catalog, room, rng, occupied, serial) + &"sanctuary", &"refuge": + serial = _spawn_bobka(floor, catalog, room, rng, occupied, serial) &"narrative", &"mystery", &"puzzle": _spawn_event(floor, catalog, room, rng, occupied) @@ -71,6 +73,16 @@ static func _spawn_trap(floor: FloorState, catalog: ContentCatalog, room: RoomSt floor.traps[cell] = {"trap_id": definition["id"]} occupied[cell] = true +static func _spawn_bobka(floor: FloorState, catalog: ContentCatalog, room: RoomState, rng: DeterministicRng, occupied: Dictionary[Vector2i, bool], serial: int) -> int: + var definition := catalog.get_npc(&"npc_bobka") + var cell := _available_cell(floor, room, rng, occupied) + if definition.is_empty() or cell == Vector2i(-1, -1): + return serial + var actor_id := StringName("npc_bobka_%02d" % serial) + floor.npcs[actor_id] = ActorStateType.new(actor_id, cell, 1, 0, &"npc_bobka") + occupied[cell] = true + return serial + 1 + static func _spawn_npc(floor: FloorState, catalog: ContentCatalog, room: RoomState, rng: DeterministicRng, occupied: Dictionary[Vector2i, bool], serial: int) -> int: var definition := SpawnRulesType.npc_for_role(catalog, room.kind) var cell := _available_cell(floor, room, rng, occupied) diff --git a/scripts/domain/world/floor_state.gd b/scripts/domain/world/floor_state.gd index 8e0fc54..cedb8d2 100644 --- a/scripts/domain/world/floor_state.gd +++ b/scripts/domain/world/floor_state.gd @@ -33,6 +33,29 @@ func _init(new_map: GridMapState, new_seed: int = 0, new_floor_number: int = 0) seed = new_seed floor_number = new_floor_number +func room_at(cell: Vector2i) -> RoomState: + var room_id := map.get_room_id(cell) + for room: RoomState in rooms: + if room.id == room_id: + return room + return null + +func safe_rooms() -> Array[RoomState]: + var safe_rooms: Array[RoomState] = [] + for room: RoomState in rooms: + if room.kind in [&"refuge", &"sanctuary"] and not room.bounds.has_point(entrance): + safe_rooms.append(room) + return safe_rooms + +func bed_cells_in(room: RoomState) -> Array[Vector2i]: + var beds: Array[Vector2i] = [] + if room == null: + return beds + for cell: Vector2i in metadata.get("bed_cells", []): + if room.bounds.has_point(cell): + beds.append(cell) + return beds + func to_dict() -> Dictionary: var serialized_rooms: Array[Dictionary] = [] for room in rooms: diff --git a/scripts/domain/world/safe_room_service.gd b/scripts/domain/world/safe_room_service.gd new file mode 100644 index 0000000..11aba69 --- /dev/null +++ b/scripts/domain/world/safe_room_service.gd @@ -0,0 +1,56 @@ +class_name SafeRoomService +extends RefCounted + +const SAFE_ROOM_KINDS := [&"refuge", &"sanctuary"] +const BOBKA_CONTENT_ID := &"npc_bobka" +const RATION_ITEM_ID := &"consumable_ration" + +const ItemDefinitionType = preload("res://scripts/domain/items/item_definition.gd") +const ItemInstanceType = preload("res://scripts/domain/items/item_instance.gd") +const GameEventType = preload("res://scripts/domain/run/game_event.gd") + +static func is_safe_room(room: RoomState) -> bool: + return room != null and room.kind in SAFE_ROOM_KINDS + +# Diagnostics are intentionally data-only so generated-floor callers can assert +# the same contract without instantiating a scene. +static func validate_floor(floor: FloorState) -> PackedStringArray: + var diagnostics := PackedStringArray() + if floor == null: + diagnostics.append("floor is missing") + return diagnostics + var entrance_room := floor.room_at(floor.entrance) + if is_safe_room(entrance_room): + diagnostics.append("entrance must not be a safe room") + if floor.metadata.get("bed_cells", []).has(floor.entrance): + diagnostics.append("entrance must not contain a bed") + if floor.safe_rooms().is_empty(): + diagnostics.append("floor requires a non-entrance safe room") + for room: RoomState in floor.safe_rooms(): + if floor.bed_cells_in(room).size() != 1: + diagnostics.append("safe room %s requires exactly one bed" % room.id) + if not floor.npcs.values().any(func(npc: ActorState) -> bool: return npc.content_id == BOBKA_CONTENT_ID and room.bounds.has_point(npc.cell)): + diagnostics.append("safe room %s requires Bobka" % room.id) + return diagnostics + +static func can_rest(floor: FloorState, cell: Vector2i) -> bool: + if floor == null or not floor.metadata.get("bed_cells", []).has(cell): + return false + return is_safe_room(floor.room_at(cell)) + +# Bobka's imported dialogue intentionally calls this on every chosen ration; +# no dialogue/session flag is consulted, and gold is never read or modified. +static func give_bobka_ration(run: RunState, npc_id: StringName) -> Array[GameEvent]: + if run == null or npc_id.is_empty() or run.content_catalog == null: + return [] + var definition := ItemDefinitionType.from_catalog(run.content_catalog, RATION_ITEM_ID) + var added := run.inventory.add_item(ItemInstanceType.new(definition)) + if not bool(added.get("accepted", false)): + return [] + var turn := run.next_turn() + var events: Array[GameEvent] = [ + GameEventType.new(turn, run.current_floor, &"item_granted", {"item_id": RATION_ITEM_ID, "quantity": 1, "npc_id": npc_id}, "Bobka vous donne une ration de survie.", npc_id), + GameEventType.new(turn, run.current_floor, &"bobka_ration_given", {"item_id": RATION_ITEM_ID, "quantity": 1, "npc_id": npc_id}, "Ration gratuite reçue de Bobka.", npc_id), + ] + run.append_events(events) + return events diff --git a/scripts/domain/world/safe_room_service.gd.uid b/scripts/domain/world/safe_room_service.gd.uid new file mode 100644 index 0000000..4759d19 --- /dev/null +++ b/scripts/domain/world/safe_room_service.gd.uid @@ -0,0 +1 @@ +uid://b2is0iusjmnhn diff --git a/scripts/game/game.gd b/scripts/game/game.gd index aed918e..d0f38a4 100644 --- a/scripts/game/game.gd +++ b/scripts/game/game.gd @@ -173,6 +173,7 @@ func _initialize_run_state() -> void: run_state.player_name = pending_player_name run_state.base_archetype = pending_base_archetype spell_catalog = ContentCatalogType.load_from_directory("res://data/ascii") + run_state.content_catalog = spell_catalog crafting_engine = CraftingEngineType.new(spell_catalog) dialogue_engine = DialogueEngineType.new() event_engine = EventEngineType.new() @@ -231,6 +232,7 @@ func _apply_loaded_run(loaded: Dictionary) -> void: push_error("Cannot apply an incomplete loaded run.") return spell_catalog = ContentCatalogType.load_from_directory("res://data/ascii") + run_state.content_catalog = spell_catalog crafting_engine = CraftingEngineType.new(spell_catalog) dialogue_engine = DialogueEngineType.new() event_engine = EventEngineType.new() @@ -1092,6 +1094,8 @@ func _quest_context() -> Dictionary: return { "player": dialogue_player, "payload": {"gold": run_state.gold, "xp": game_state.player.xp}, + "npc_id": StringName(active_dialogue_npc.get("actor_id", "")), + "npc_content_id": StringName(active_dialogue_npc.get("id", "")), "catalog": spell_catalog, "inventory": run_state.inventory, "quest_manager": quest_manager, diff --git a/task-PR-2-report b/task-PR-2-report new file mode 100644 index 0000000..083606a --- /dev/null +++ b/task-PR-2-report @@ -0,0 +1,37 @@ +# PR-2 — Safe Rooms and Bobka + +## Scope + +PR-2 adds procedural safe-room support and the Bobka ration interaction without changing unrelated product areas. + +- Generated floors reserve at least one non-entrance `refuge` or `sanctuary` when weighted unique-room selection would otherwise produce none. +- Safe rooms materialize exactly one reserved bed, are populated with Bobka, and expose floor/service helpers for validation and rest eligibility. +- Bobka's imported `give_item` dialogue effect is accepted only for Bobka and the ration item. It grants one ration per selection, appends explicit item/journal events, and does not read or mutate gold. +- The active run receives the authoritative content catalog for dialogue-time item construction; inventory gains a definition-ID count helper. +- Added unit, integration, and real Game E2E coverage for the safe-room contract. + +## Verification finding and root-cause fix + +The initial full verification found one genuine PR-2 E2E failure. The test refreshed `GameHud` with an untyped literal `[]`, while `GameHud.refresh` requires a typed `Array[GameEvent]`; Godot rejected the call before the Bobka scenario executed. The production path was not at fault. The test now consistently passes its declared `Array[GameEvent]`, preserving the intended E2E coverage. No workaround or production behavior change was added. + +## Required confirmations + +- **Bobka repeat ration / no gold:** the unit test invokes `give_bobka_ration` twice with a run starting at 47 gold and verifies two rations, the same gold total, and the expected grant/journal events. The real Game E2E selects Bobka's ration dialogue three times, verifies ration counts 1/2/3, asserts gold remains unchanged after every selection, and confirms the Bobka journal event. +- **Generator 20-seed invariants:** the integration suite generates 20 deterministic floor-four seeds. For every seed it verifies that the entrance is neither safe nor bed/NPC occupied, that at least one non-entrance safe room exists, and that every safe room has exactly one bed and a Bobka. `SafeRoomService.validate_floor` is empty for all 20 seeds. + +## Final verification + +`./scripts/verify.sh` completed successfully with Godot 4.5.0: + +- Unit: **195 / 195** tests passed, **2,437** assertions. +- Integration: **14 / 14** tests passed, **235,636** assertions. +- E2E: **60 / 60** tests passed, **600** assertions. +- Visual capture: passed. + - `artifacts/visual-check.png`: `dungeon_non_black_pixels=489308`, `visible_pixels=17524`, `explored_pixels=10438`, `explored_luminance=0.012`, `unknown_pixels=365325`, `unknown_luminance=0.001`. + - `artifacts/visual-check-exit.png`: `dungeon_non_black_pixels=488708`, `visible_pixels=29407`, `explored_pixels=17146`, `explored_luminance=0.012`, `unknown_pixels=277574`, `unknown_luminance=0.001`. + +The standard GUT invalid-UID and Godot cleanup/resource warnings occurred; there were no parse errors, script errors, test failures, or visual verification failures. Capture artifacts are intentionally ignored by `.gitignore`. + +## Git + +All intended PR-2 sources, generated UIDs, tests, and this report are committed locally in one commit. No push and no pull request were created. diff --git a/tests/e2e/test_bobka_safe_room_e2e.gd b/tests/e2e/test_bobka_safe_room_e2e.gd new file mode 100644 index 0000000..4e86173 --- /dev/null +++ b/tests/e2e/test_bobka_safe_room_e2e.gd @@ -0,0 +1,70 @@ +extends GutTest + +const ContentCatalogType = preload("res://scripts/domain/content/content_catalog.gd") +const FloorGeneratorType = preload("res://scripts/domain/world/floor_generator.gd") +const SafeRoomServiceType = preload("res://scripts/domain/world/safe_room_service.gd") +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_bobka_dialogue_gives_three_free_rations_in_safe_room() -> void: + var catalog := ContentCatalogType.load_from_directory("res://data/ascii") + var floor := FloorGeneratorType.generate("BOBKA-E2E", 4, catalog) + var safe_room: RoomState = floor.safe_rooms()[0] + var bobka := _bobka_in_room(floor, safe_room) + var bed: Vector2i = floor.bed_cells_in(safe_room)[0] + var run := game.get_run_state() + var state := game.get_game_state() + run.set_floor_state(floor) + run.current_floor = 4 + state.set_floor_state(floor) + state.content_catalog = catalog + state.player.cell = bobka.cell + Vector2i.LEFT + var gold_before := run.gold + var no_events: Array[GameEvent] = [] + game.get_node("HUD").refresh(state, no_events, run) + + assert_ne(floor.room_at(floor.entrance).kind, &"refuge") + assert_ne(floor.room_at(floor.entrance).kind, &"sanctuary") + assert_eq(Array(SafeRoomServiceType.validate_floor(floor)), []) + assert_true(SafeRoomServiceType.can_rest(floor, bed)) + state.player.cell = bed + game.get_node("HUD").refresh(state, no_events, run) + assert_string_contains((game.get_node("HUD/ContextPromptLabel") as Label).text, "Se reposer") + state.player.cell = bobka.cell + Vector2i.LEFT + + for ration_number in range(3): + await _dispatch_key(KEY_E) + var panel := game.get_node("HUD/DialoguePanel") as PanelContainer + assert_true(panel.visible, "ration %d opens Bobka dialogue" % (ration_number + 1)) + assert_string_contains((game.get_node("HUD/DialoguePanel/Entries") as RichTextLabel).get_parsed_text(), "Prendre une ration.") + await _dispatch_key(KEY_ENTER) + assert_eq(run.inventory.count_by_definition(&"consumable_ration"), ration_number + 1) + assert_eq(run.gold, gold_before) + assert_true(run.recent_events(3).any(func(event: GameEvent) -> bool: return event.kind == &"bobka_ration_given")) + await _dispatch_key(KEY_ENTER) + assert_false(panel.visible) + +func _bobka_in_room(floor: FloorState, room: RoomState) -> ActorState: + for npc: ActorState in floor.npcs.values(): + if npc.content_id == &"npc_bobka" and room.bounds.has_point(npc.cell): + return npc + return null + +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 diff --git a/tests/e2e/test_bobka_safe_room_e2e.gd.uid b/tests/e2e/test_bobka_safe_room_e2e.gd.uid new file mode 100644 index 0000000..6a69c3a --- /dev/null +++ b/tests/e2e/test_bobka_safe_room_e2e.gd.uid @@ -0,0 +1 @@ +uid://cuwm0yskud28x diff --git a/tests/integration/test_safe_room_generation.gd b/tests/integration/test_safe_room_generation.gd new file mode 100644 index 0000000..db00630 --- /dev/null +++ b/tests/integration/test_safe_room_generation.gd @@ -0,0 +1,29 @@ +extends GutTest + +const ContentCatalogType = preload("res://scripts/domain/content/content_catalog.gd") +const FloorGeneratorType = preload("res://scripts/domain/world/floor_generator.gd") +const SafeRoomServiceType = preload("res://scripts/domain/world/safe_room_service.gd") + +const ASCII_CATALOG_PATH := "res://data/ascii" + +func test_twenty_seeds_keep_entrance_non_safe_and_bed_free() -> void: + for seed_index in range(20): + var floor := _generate("SAFE-START-%d" % seed_index) + var entrance_room: RoomState = floor.room_at(floor.entrance) + assert_false(entrance_room.kind in SafeRoomServiceType.SAFE_ROOM_KINDS, "seed %d entrance is never safe" % seed_index) + assert_false(floor.metadata.get("bed_cells", []).has(floor.entrance), "seed %d entrance has no bed" % seed_index) + assert_false(floor.npcs.values().any(func(npc: ActorState) -> bool: return npc.cell == floor.entrance), "seed %d entrance has no NPC" % seed_index) + +func test_twenty_seeds_guarantee_safe_rooms_beds_and_bobka() -> void: + for seed_index in range(20): + var floor := _generate("SAFE-%d" % seed_index) + var safe_rooms: Array[RoomState] = floor.safe_rooms() + assert_gt(safe_rooms.size(), 0, "seed %d has a non-entrance safe room" % seed_index) + assert_eq(Array(SafeRoomServiceType.validate_floor(floor)), [], "seed %d satisfies safe-room invariants" % seed_index) + for room: RoomState in safe_rooms: + var beds: Array[Vector2i] = floor.bed_cells_in(room) + assert_eq(beds.size(), 1, "seed %d safe room %s has one bed" % [seed_index, room.id]) + assert_true(floor.npcs.values().any(func(npc: ActorState) -> bool: return npc.content_id == &"npc_bobka" and room.bounds.has_point(npc.cell)), "seed %d safe room %s has Bobka" % [seed_index, room.id]) + +func _generate(seed: String) -> FloorState: + return FloorGeneratorType.generate(seed, 4, ContentCatalogType.load_from_directory(ASCII_CATALOG_PATH)) diff --git a/tests/integration/test_safe_room_generation.gd.uid b/tests/integration/test_safe_room_generation.gd.uid new file mode 100644 index 0000000..5339de8 --- /dev/null +++ b/tests/integration/test_safe_room_generation.gd.uid @@ -0,0 +1 @@ +uid://dsysgwqhw1hf8 diff --git a/tests/unit/test_safe_room_service.gd b/tests/unit/test_safe_room_service.gd new file mode 100644 index 0000000..1edd6ff --- /dev/null +++ b/tests/unit/test_safe_room_service.gd @@ -0,0 +1,34 @@ +extends GutTest + +const ContentCatalogType = preload("res://scripts/domain/content/content_catalog.gd") +const FloorGeneratorType = preload("res://scripts/domain/world/floor_generator.gd") +const RunSettingsType = preload("res://scripts/domain/run/run_settings.gd") +const RunStateType = preload("res://scripts/domain/run/run_state.gd") +const SafeRoomServiceType = preload("res://scripts/domain/world/safe_room_service.gd") + +const ASCII_CATALOG_PATH := "res://data/ascii" + +func test_safe_room_contract_accepts_a_generated_non_surface_floor() -> void: + var catalog := ContentCatalogType.load_from_directory(ASCII_CATALOG_PATH) + var floor := FloorGeneratorType.generate("SAFE-SERVICE", 4, catalog) + + assert_eq(Array(SafeRoomServiceType.validate_floor(floor)), []) + assert_gt(floor.safe_rooms().size(), 0) + for room: RoomState in floor.safe_rooms(): + assert_true(SafeRoomServiceType.is_safe_room(room)) + assert_true(SafeRoomServiceType.can_rest(floor, room.bounds.get_center())) + +func test_bobka_ration_is_repeatable_and_free() -> void: + var catalog := ContentCatalogType.load_from_directory(ASCII_CATALOG_PATH) + var run := RunStateType.new("SAFE-RATION", RunSettingsType.new("SAFE-RATION")) + run.content_catalog = catalog + run.gold = 47 + var gold_before := run.gold + + var first: Array[GameEvent] = SafeRoomServiceType.give_bobka_ration(run, &"npc_bobka_00") + var second: Array[GameEvent] = SafeRoomServiceType.give_bobka_ration(run, &"npc_bobka_00") + + assert_eq(run.inventory.count_by_definition(&"consumable_ration"), 2) + assert_eq(run.gold, gold_before) + assert_eq(first.map(func(event: GameEvent) -> StringName: return event.kind), [&"item_granted", &"bobka_ration_given"]) + assert_eq(second.map(func(event: GameEvent) -> StringName: return event.kind), [&"item_granted", &"bobka_ration_given"]) diff --git a/tests/unit/test_safe_room_service.gd.uid b/tests/unit/test_safe_room_service.gd.uid new file mode 100644 index 0000000..fe469ef --- /dev/null +++ b/tests/unit/test_safe_room_service.gd.uid @@ -0,0 +1 @@ +uid://donevcqeo5rwg