class_name TouristService extends RefCounted const ActorStateType = preload("res://scripts/domain/actor_state.gd") const AudienceStateType = preload("res://scripts/domain/meta/audience_state.gd") const ModerationType = preload("res://scripts/domain/meta/moderation.gd") const GameEventType = preload("res://scripts/domain/run/game_event.gd") const GridMapStateType = preload("res://scripts/domain/grid_map_state.gd") const TOURIST_CONTENT_ID := &"npc_tourist_influencer" const TOURIST_MIN_FLOOR := 6 const TOURIST_MAX_PER_FLOOR := 1 const SELFIE_SPECTACLE := 10 const SPAWN_DENOMINATOR := 4 const SAFE_ROOM_KINDS := [&"sanctuary", &"refuge", &"narrative", &"mystery", &"puzzle"] # Population owns the RNG, while this service owns the tourist contract. The # resulting record is an ordinary NPC with private metadata, never a combatant. static func try_spawn(floor: FloorState, catalog: ContentCatalog, rng: DeterministicRng) -> Array[GameEvent]: if floor.floor_number < TOURIST_MIN_FLOOR or _active_tourists(floor).size() >= TOURIST_MAX_PER_FLOOR: return [] var definition := catalog.get_npc(TOURIST_CONTENT_ID) if definition.is_empty() or rng.next_int() % SPAWN_DENOMINATOR != 0: return [] var candidates: Array[Vector2i] = [] var occupied := _occupied_cells(floor) for room: RoomState in floor.rooms: if room.kind not in SAFE_ROOM_KINDS or room.kind in [&"entrance", &"boss"]: continue for y in range(room.bounds.position.y, room.bounds.end.y): for x in range(room.bounds.position.x, room.bounds.end.x): var cell := Vector2i(x, y) if floor.map.get_cell(cell) == GridMapStateType.CellKind.FLOOR and not occupied.has(cell): candidates.append(cell) if candidates.is_empty(): return [] var actor_id := StringName("npc_tourist_influencer_%02d" % floor.npcs.size()) var tourist := ActorStateType.new(actor_id, candidates[rng.next_int() % candidates.size()], 1, 0, TOURIST_CONTENT_ID) tourist.metadata = { "is_tourist": true, "hidden_health": maxi(1, int(definition.get("hidden_health", 30))), "ejected": false, "moderation_struck": false, } floor.npcs[actor_id] = tourist return [] static func selfie(run: RunState, tourist_id: StringName) -> Array[GameEvent]: var tourist := _tourist(run, tourist_id) if tourist == null or bool(tourist.metadata.get("ejected", false)): return [] var audience := AudienceStateType.new(run.audience_state) var audience_result := audience.add_spectacle(SELFIE_SPECTACLE) return [_event(run, &"tourist_selfie", tourist, { "audience": audience_result, "hidden_health": int(tourist.metadata.get("hidden_health", 0)), }, "Selfie avec le touriste : l'audience s'emballe.")] static func meet(run: RunState, tourist_id: StringName, character_level: int) -> Array[GameEvent]: var tourist := _tourist(run, tourist_id) if tourist == null or character_level < 4: return [] var met_ids: Array = run.audience_state.get("met_tourist_ids", []) if tourist.id in met_ids: return [] met_ids.append(tourist.id) run.audience_state["met_tourist_ids"] = met_ids return [_event(run, &"tourist_met", tourist, {}, "Vous rencontrez un touriste dans les profondeurs.")] static func endanger(run: RunState, tourist_id: StringName, amount: int) -> Array[GameEvent]: var tourist := _tourist(run, tourist_id) if tourist == null or bool(tourist.metadata.get("ejected", false)): return [] var remaining := maxi(0, int(tourist.metadata.get("hidden_health", 0)) - maxi(0, amount)) tourist.metadata["hidden_health"] = remaining var events: Array[GameEvent] = [_event(run, &"tourist_endangered", tourist, {"amount": maxi(0, amount), "hidden_health": remaining}, "Le touriste est mis en danger.")] if remaining > 0: return events tourist.metadata["ejected"] = true run.current_floor_state().npcs.erase(tourist.id) events.append(_event(run, &"tourist_ejected", tourist, {}, "Évacuation du touriste par contrat de protection.")) if not bool(tourist.metadata.get("moderation_struck", false)): tourist.metadata["moderation_struck"] = true var result := ModerationType.new(run.moderation_state).apply(&"mise_en_danger_touriste", run.current_floor_deadline()) events.append(_event(run, &"moderation_strike", tourist, result, "[SYSTÈME] Mise en danger d’un touriste : évacuation et sanction.")) return events static func _tourist(run: RunState, tourist_id: StringName) -> ActorState: var floor := run.current_floor_state() if floor == null: return null var tourist: ActorState = floor.npcs.get(tourist_id) return tourist if tourist != null and bool(tourist.metadata.get("is_tourist", false)) else null static func _active_tourists(floor: FloorState) -> Array[ActorState]: var tourists: Array[ActorState] = [] for npc: ActorState in floor.npcs.values(): if npc.content_id == TOURIST_CONTENT_ID and not bool(npc.metadata.get("ejected", false)): tourists.append(npc) return tourists static func _occupied_cells(floor: FloorState) -> Dictionary[Vector2i, bool]: var occupied: Dictionary[Vector2i, bool] = {floor.entrance: true, floor.exit: true} for bed_cell: Vector2i in floor.metadata.get("bed_cells", []): occupied[bed_cell] = true for actor: ActorState in floor.enemies.values() + floor.npcs.values(): occupied[actor.cell] = true for cell: Vector2i in floor.chests: occupied[cell] = true for cell: Vector2i in floor.traps: occupied[cell] = true for entry: Dictionary in floor.events: var serialized: Array = entry.get("cell", []) if serialized.size() == 2: occupied[Vector2i(int(serialized[0]), int(serialized[1]))] = true return occupied static func _event(run: RunState, kind: StringName, tourist: ActorState, payload: Dictionary, message: String) -> GameEvent: return GameEventType.new(run.current_turn, run.current_floor, kind, payload, message, &"player", tourist.id, tourist.cell)