feat: add safe rooms and repeatable Bobka rations
This commit is contained in:
@@ -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
|
||||
_:
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1 @@
|
||||
uid://b2is0iusjmnhn
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user