diff --git a/docs/ARCHITECTURE_OVERVIEW.md b/docs/ARCHITECTURE_OVERVIEW.md index 1a06f48..7cd0c37 100644 --- a/docs/ARCHITECTURE_OVERVIEW.md +++ b/docs/ARCHITECTURE_OVERVIEW.md @@ -49,13 +49,16 @@ SimulationClock -> SimulationEventLog records and indexes completed facts -> WorldEventStore exposes typed participants, locations, and causal queries -> EventKnowledgeSystem records, ranks, transfers, and retains bounded knowledge - -> RelationshipSystem applies seven-dimensional evidence-backed consequences + -> RelationshipSystem applies seven-dimensional evidence-backed consequences -> SituationSystem projects real unresolved conditions from facts and events -> QuestJournalSystem records only player discovery/tracking preference -> SocialCommitmentSystem resolves promises from ordinary outcome events -> ConversationService plans deterministic semantic turns from current context -> VillageOpportunitySystem preserves the legacy bounded compatibility slice - -> ConflictSystem advances indexed combatants and authoritative combat outcomes + -> ConflictSystem advances indexed combatants and authoritative combat outcomes + -> PlayerQuestSystem turns open needs into named player quests and + grants Standing as the player resolves them through real supply/feed + -> PlayerNeedsSystem advances the player's hunger, energy, and carry -> WorldViewManager presents travel, NPC state, and world-state cues -> ActiveWorldAdapter supplies loaded-world positions/capacity -> LoadedResourceSpatialIndex bounds finite-anchor discovery @@ -128,6 +131,22 @@ would otherwise obscure that lifecycle: When no such helper exists, it can separately derive an ephemeral `OpportunityPlayerResponseResult` from the real destination capacity and enabled player-usable finite sources; +- `simulation/quests/PlayerQuestSystem.gd` turns real open needs into named + player quests (`PlayerQuestRecord`) and owns the player's `PlayerStandingRecord` + reputation. A quest is generated only when the player can genuinely act + through the ordinary economy (an unassisted pantry/wood shortage, or a hungry + player-feedable animal), and it is resolved only by a real player supply or + feed event. Completing a quest grants Standing and raises the requester's + gratitude; a requester who personally trusts the player can ask them directly + even when another capable helper exists. Quests, Standing, and gratitude are + persisted and never become quest-only simulation state; +- `simulation/quests/PlayerNeedsSystem.gd` owns the player's embodied hunger, + energy, starvation, death, and carried inventory, advancing on the + deterministic tick and mutating finite resources and storage only through the + ordinary economy and recorder; +- `simulation/quests/PlayerNegotiationSystem.gd` owns the read-only villager + talk surface plus accept/decline of personal requests, delegating quest + lifecycle to `PlayerQuestSystem`; - `simulation/persistence/` owns save-slot file safety; - `simulation/state/` owns versioned serialized record contracts; - `simulation/definitions/` owns stable IDs and immutable action/profession @@ -189,6 +208,7 @@ hard to read. | `simulation/situations/` | World situation, journal, and commitment lifecycles | | `simulation/dialogue/` | Semantic conversation planning and presenter boundary | | `simulation/regional/` | Regional records, due-job scheduling, keyed randomness, analytical updates | +| `simulation/quests/` | Named player quests and Standing reputation from real need resolution | | `simulation/state/` | Versioned, serializable mutable records | | `simulation/definitions/` | Stable IDs and immutable gameplay definitions | | `simulation/persistence/` | Validated local save-file storage | diff --git a/docs/BUILD_IN_PUBLIC_PLAN.md b/docs/BUILD_IN_PUBLIC_PLAN.md index e5d7307..aae2400 100644 --- a/docs/BUILD_IN_PUBLIC_PLAN.md +++ b/docs/BUILD_IN_PUBLIC_PLAN.md @@ -897,6 +897,40 @@ Next: by the scaling fixture. 3. Add a market/traveler cadence so the returning player finds something new: a traveler who shares one rumour or a bounded food-for-wood exchange. +1. Let an inspected villager who owns an open opportunity surface that exact + village need plus the authoritative direct-help route, capable helper, or + honest unavailable state that currently exists. **Complete:** the quiet + `VillagerFieldNoteHud` now shows a restrained `Need` line for exactly that + note, derived from the existing player-response/helper queries and the real + storage state. It names the helper when one is capable, shows the direct + finite-resource-to-storage route when the player response derives it, or + honestly states why help is unavailable, and it re-derives from live facts + after restore without entering saves. +2. Prove that the ordinary finite-resource-to-storage harvest resolves an + available player route and updates the context without dialogue branching, + acceptance state, rewards, a quest log, or saved presentation state. + **Complete:** a runtime scenario stages the real pantry crisis, surfaces the + need for its interested villager, and proves that one ordinary + `harvest_resource_node` extraction resolves the same open opportunity and + clears the need segment on the next refresh. +3. Give the player a reason to keep helping: a persistent **Standing** loop. + **Complete:** resolving real village needs (pantry, wood, hungry animals) + now grants Standing (Stranger → Known Hand → Trusted → Village Steward → + Voice of Jajce) and raises the requester's gratitude. The world's quest + generator turns each open need into a named player quest in a quiet + `QuestJournalHud`, and villagers who personally trust the player start + asking them directly even when another helper could act. All of it flows + through the ordinary finite-resource harvest/feed contracts, survives + save/restore deterministically, and adds no quest-only state. +4. Make the player a citizen with a body. **Complete:** the player now has + hunger and energy that advance on the simulation tick, eats from the real + pantry, weakens when starving, and can die by starvation (halving Standing). + Gathering fills a carried inventory with capacity, and depositing at the + pantry or woodpile is an explicit transaction that resolves the same quests. + Pressing `E` on a villager opens a personal talk where you accept or decline + their real request. The embodied needs, carry, and negotiation rules live in + focused `PlayerNeedsSystem`/`PlayerNegotiationSystem` collaborators and are + covered by headless, runtime, and schema-restore regressions. Do not start with GIS data, a full city, a large asset pack, or more NPC mechanics. The next proof is a beautiful stage for the systems that already diff --git a/docs/LEARNING_ROADMAP.md b/docs/LEARNING_ROADMAP.md index 8866e34..800fd24 100644 --- a/docs/LEARNING_ROADMAP.md +++ b/docs/LEARNING_ROADMAP.md @@ -1048,6 +1048,62 @@ bounded through the existing spatial/population indexes, 5–10 Hz UI/environmen updates, cached combatant order, and navigation retry backoff. High, Balanced, and Low profiles now prove a reversible weak-PC structural budget; real weak-hardware frame-time/GPU/memory measurement remains intentionally open. +The first bounded field-note opportunity surfacing slice is complete: + +- `VillagerInspectionResult` now carries an optional read-only + `VillagerOpportunityNote` that is present only when the inspected villager is + the interested party of an open village opportunity; +- `VillagerOpportunityNote.derive` decides the honest help state from the + existing authoritative queries: the derived player-response route wins, then + the capable helper by name, then a real unavailable reason (need already met, + storage has no room, no stocked source in reach, or no storage at all); +- the quiet `VillagerFieldNoteHud` shows a restrained `Need` line for exactly + that note, coexists with the existing `E` prompt and cinematic mode, and + re-derives it from live state without entering saves or drawing RNG; +- a real player finite-resource harvest resolves the same open + `OpportunityStateRecord` through the existing economy/event history, and the + need segment clears on the next refresh; +- unit, runtime, and query-neutrality checks cover the derive branches, the + staged pantry-crisis surfacing, helper naming, ordinary harvest resolution, + and the absence of saved presentation state. + +The first bounded player-Standing and quest slice is complete: + +- `PlayerStandingRecord` is a versioned, deterministic 0–100 reputation with + named tiers (Stranger, Known Hand, Trusted, Village Steward, Voice of Jajce) + and per-requester gratitude, persisted through world schema v12; +- `PlayerQuestRecord` plus `PlayerQuestSystem` turn real open needs into named + player quests: an unassisted pantry or wood shortage (or a hungry, + player-feedable animal) generates one quest for its requester, resolved only + by a real player supply or feed event through the ordinary economy; +- completing a quest grants Standing and raises the requester's gratitude, an + NPC who trusts the player personally can ask them directly even when another + capable helper exists, and tier advances emit a celebration beat; +- a read-only `QuestJournalHud` surfaces the active quest, Standing tier, and + gratitude feedback without entering saves or drawing RNG; +- headless, runtime, schema-restore, and GUT unit regressions cover generation, + completion, invalidation, animal care, personal requests, and tier + progression. + +The first embodied-citizen slice is complete: + +- the player now has a body in the world: hunger and energy advance on the + deterministic tick (`PlayerStateRecord`), eating at the pantry reduces the + player's own hunger, starvation weakens movement, and death by starvation + halves Standing and allows a respawn, all persisted through world schema v13; +- the player carries a real inventory with capacity: gathering a finite + resource fills the hands, depositing at the pantry or woodpile is an explicit + transaction that resolves opportunities and quests through the same supply + contract used by NPCs; +- pressing `E` on a nearby villager opens a personal talk: the villager greets + you and surfaces their real need, and you can accept their request (which + surfaces the same open quest) or decline it (closing the quest without + gratitude); +- `PlayerNeedsSystem`, `PlayerNegotiationSystem`, and `SimulationEventRecorder` + keep the player-citizen rules out of the manager; +- headless, runtime, carry/deposit, talk/negotiation, and schema-restore +regressions cover needs, death, respawn, carrying, depositing, quest +resolution, and personal request accept/decline. Recently completed: diff --git a/docs/PROJECT_CONTEXT.md b/docs/PROJECT_CONTEXT.md index d7019b7..19c16fe 100644 --- a/docs/PROJECT_CONTEXT.md +++ b/docs/PROJECT_CONTEXT.md @@ -310,6 +310,27 @@ The village currently tracks: - per-resource modifiers; - per-resource priorities. +### Player Standing and quests + +The world now rewards player help with a persistent **Standing** reputation +(0–100) and named tiers: Stranger, Known Hand, Trusted, Village Steward, and +Voice of Jajce. The quest generator turns real open needs into player quests: + +- an unassisted pantry or wood shortage generates a named quest from its + worried villager; +- a hungry, player-feedable goat generates an animal-care quest; +- completing a quest through the ordinary finite-resource harvest or feed + contract grants Standing, raises the requester's gratitude, and emits a + thank-you beat; +- a requester who personally trusts the player (gratitude ≥ 0.5 and Standing + tier at least Known Hand) asks them directly even when another capable + helper exists; +- a compact `QuestJournalHud` shows the active quest, Standing tier, and + gratitude feedback; tier advances trigger a celebration beat. + +Quests, Standing, and gratitude persist through world schema v12 and never +become quest-only simulation state. + ### NPC task lifecycle Task states are: diff --git a/docs/SIMULATION_STATE_SCHEMA.md b/docs/SIMULATION_STATE_SCHEMA.md index 3b10336..47bc82e 100644 --- a/docs/SIMULATION_STATE_SCHEMA.md +++ b/docs/SIMULATION_STATE_SCHEMA.md @@ -39,6 +39,12 @@ simulation. The current world schema is v16 and captures: definition ID) for NPC defenders and standalone hostile archetypes; - faction records (food, warriors, aggression, morale, stance, war plan) for the village and the hill tribe. +- the player's Standing reputation (0–100), resolved-need count, and per-NPC + gratitude, plus named player quest records that reference the originating + opportunity or animal, requester NPC, resource/target/amount, Standing + reward, and exact resolution or invalidation tick; +- the player's embodied state: hunger, energy, starvation progress, death + count, and carried inventory with its capacity (schema v13). The top-level identity is: diff --git a/main.tscn b/main.tscn index a399cfa..03c89ce 100644 --- a/main.tscn +++ b/main.tscn @@ -18,6 +18,7 @@ [ext_resource type="Script" uid="uid://cgur3e3k1l3iy" path="res://world/ui/player_status_hud.gd" id="18_status"] [ext_resource type="Script" uid="uid://bfysko6pf0tnt" path="res://player/combat/player_combat_controller.gd" id="19_combat"] [ext_resource type="Script" uid="uid://qw0qb6mc8j7" path="res://world/combat/combat_presentation.gd" id="20_combat_view"] +[ext_resource type="Script" path="res://world/ui/quest_journal_hud.gd" id="21_quest_journal"] [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_o5qli"] radius = 0.4 @@ -426,6 +427,86 @@ text = "The pantry is empty." horizontal_alignment = 1 vertical_alignment = 1 +[node name="QuestJournalLayer" type="CanvasLayer" parent="."] +layer = 4 + +[node name="QuestJournal" type="Control" parent="QuestJournalLayer" node_paths=PackedStringArray("simulation_manager")] +visible = false +anchors_preset = 3 +anchor_left = 1.0 +anchor_top = 1.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +offset_left = -360.0 +offset_top = -150.0 +offset_right = -16.0 +offset_bottom = -20.0 +grow_horizontal = 0 +grow_vertical = 0 +mouse_filter = 2 +script = ExtResource("21_quest_journal") +simulation_manager = NodePath("../../SimulationManager") + +[node name="Accent" type="ColorRect" parent="QuestJournalLayer/QuestJournal"] +layout_mode = 1 +anchors_preset = 9 +anchor_bottom = 1.0 +offset_right = 2.0 +grow_vertical = 2 +mouse_filter = 2 +color = Color(0.55, 0.42, 0.2, 0.9) + +[node name="Copy" type="VBoxContainer" parent="QuestJournalLayer/QuestJournal"] +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +offset_left = 12.0 +offset_top = 8.0 +mouse_filter = 2 +theme_override_constants/separation = 2 +alignment = 2 + +[node name="Standing" type="Label" parent="QuestJournalLayer/QuestJournal/Copy"] +layout_mode = 2 +mouse_filter = 2 +theme_override_colors/font_color = Color(0.98, 0.92, 0.79, 1) +theme_override_colors/font_outline_color = Color(0.07, 0.045, 0.025, 0.94) +theme_override_constants/outline_size = 6 +theme_override_font_sizes/font_size = 18 +text = "Standing · Stranger · 0/100" + +[node name="QuestKicker" type="Label" parent="QuestJournalLayer/QuestJournal/Copy"] +layout_mode = 2 +mouse_filter = 2 +theme_override_colors/font_color = Color(0.93, 0.7, 0.36, 1) +theme_override_colors/font_outline_color = Color(0.07, 0.045, 0.025, 0.9) +theme_override_constants/outline_size = 5 +theme_override_font_sizes/font_size = 11 +text = "ACTIVE QUEST" + +[node name="Quest" type="Label" parent="QuestJournalLayer/QuestJournal/Copy"] +layout_mode = 2 +mouse_filter = 2 +theme_override_colors/font_color = Color(0.9, 0.84, 0.7, 1) +theme_override_colors/font_outline_color = Color(0.07, 0.045, 0.025, 0.9) +theme_override_constants/outline_size = 4 +theme_override_font_sizes/font_size = 13 +text = "Amina · bring 1 food to the village pantry" +autowrap_mode = 2 + +[node name="Thanks" type="Label" parent="QuestJournalLayer/QuestJournal/Copy"] +visible = false +layout_mode = 2 +mouse_filter = 2 +theme_override_colors/font_color = Color(0.95, 0.76, 0.42, 1) +theme_override_colors/font_outline_color = Color(0.07, 0.045, 0.025, 0.94) +theme_override_constants/outline_size = 5 +theme_override_font_sizes/font_size = 13 +text = "Gratitude" +horizontal_alignment = 1 +autowrap_mode = 2 + [node name="DemoController" type="Node" parent="." unique_id=644713371 node_paths=PackedStringArray("ui", "active_npcs_parent", "simulation_manager", "world_view_manager", "camera_rig", "player", "pantry_storage", "crisis_caption")] script = ExtResource("13_demo") ui = NodePath("../UI") diff --git a/player/PlayerInteractionResult.gd b/player/PlayerInteractionResult.gd index 4847d6b..cdc105b 100644 --- a/player/PlayerInteractionResult.gd +++ b/player/PlayerInteractionResult.gd @@ -7,6 +7,10 @@ const KIND_PANTRY := &"pantry" const KIND_STORAGE_DEPOSIT := &"storage_deposit" const KIND_ACTIVITY := &"activity" const KIND_DIALOGUE := &"dialogue" +const KIND_DEPOSIT := &"deposit" +const KIND_TALK := &"talk" +const KIND_GUARD := &"guard" +const KIND_STUDY := &"study" var kind: StringName var action_id: StringName diff --git a/player/PlayerTalkResult.gd b/player/PlayerTalkResult.gd new file mode 100644 index 0000000..5aeb84c --- /dev/null +++ b/player/PlayerTalkResult.gd @@ -0,0 +1,53 @@ +class_name PlayerTalkResult +extends RefCounted + +var npc_id: int +var npc_name: String +var greeting: String +var has_need: bool +var need_text: String +var quest_available: bool +var quest_reward: float +var can_accept: bool +var can_decline: bool + + +func _init( + talk_npc_id: int, + talk_npc_name: String, + talk_greeting: String, + talk_has_need: bool, + talk_need_text: String, + talk_quest_available: bool, + talk_quest_reward: float, + talk_can_accept: bool, + talk_can_decline: bool +) -> void: + npc_id = talk_npc_id + npc_name = talk_npc_name + greeting = talk_greeting + has_need = talk_has_need + need_text = talk_need_text + quest_available = talk_quest_available + quest_reward = talk_quest_reward + can_accept = talk_can_accept + can_decline = talk_can_decline + + +func cache_key() -> String: + return ( + "|" + . join( + [ + str(npc_id), + npc_name, + greeting, + str(has_need), + need_text, + str(quest_available), + "%.2f" % quest_reward, + str(can_accept), + str(can_decline), + ] + ) + ) diff --git a/player/PlayerTalkResult.gd.uid b/player/PlayerTalkResult.gd.uid new file mode 100644 index 0000000..37d81a9 --- /dev/null +++ b/player/PlayerTalkResult.gd.uid @@ -0,0 +1 @@ +uid://i7pjb5605cgr diff --git a/player/VillagerInspectionResult.gd b/player/VillagerInspectionResult.gd index daf0648..fbc5e45 100644 --- a/player/VillagerInspectionResult.gd +++ b/player/VillagerInspectionResult.gd @@ -13,6 +13,7 @@ var target_name: String var carried_amounts: Dictionary var decision_reason: String var _has_decision_reason: bool +var opportunity_note: VillagerOpportunityNote var has_open_need := false var need_type: StringName @@ -46,6 +47,7 @@ func _init( need_type = StringName(inspection_need.get("need_type", &"")) need_progress = String(inspection_need.get("progress", "")) need_response = String(inspection_need.get("response", "")) + opportunity_note = inspection_need.get("opportunity_note") as VillagerOpportunityNote func has_decision_reason() -> bool: @@ -77,6 +79,7 @@ func cache_key() -> String: var carried_parts: Array[String] = [] for item_id in carried_keys: carried_parts.append("%s=%.3f" % [String(item_id), float(carried_amounts[item_id])]) + var note_part := opportunity_note.cache_key() if opportunity_note != null else "-" return ( "|" . join( @@ -94,6 +97,7 @@ func cache_key() -> String: String(need_type), need_progress, need_response, + note_part, ] ) ) diff --git a/player/VillagerOpportunityNote.gd b/player/VillagerOpportunityNote.gd new file mode 100644 index 0000000..53b4826 --- /dev/null +++ b/player/VillagerOpportunityNote.gd @@ -0,0 +1,79 @@ +class_name VillagerOpportunityNote +extends RefCounted + +const HELP_PLAYER_ROUTE := &"player_route" +const HELP_HELPER := &"helper" +const HELP_UNAVAILABLE := &"unavailable" + +var has_need := false +var need_text := "" +var help_kind: StringName = &"" +var help_text := "" + + +func _init( + note_has_need := false, + note_need_text := "", + note_help_kind: StringName = &"", + note_help_text := "" +) -> void: + has_need = note_has_need + need_text = note_need_text + help_kind = note_help_kind + help_text = note_help_text + + +func cache_key() -> String: + return "%s|%s|%s|%s" % [has_need, need_text, String(help_kind), help_text] + + +static func derive( + opportunity: OpportunityStateRecord, + player_response: OpportunityPlayerResponseResult, + helper: OpportunityHelperResult, + helper_npc_name: String, + storage: StorageStateRecord, + target_display_name: String, + resource_display_name: String +) -> VillagerOpportunityNote: + if opportunity == null: + return VillagerOpportunityNote.new() + var need_text := "The %s needs %s" % [target_display_name, resource_display_name] + if player_response != null: + return ( + VillagerOpportunityNote + . new( + true, + need_text, + HELP_PLAYER_ROUTE, + "You can help · forage %s to the %s" % [resource_display_name, target_display_name], + ) + ) + if helper != null: + return VillagerOpportunityNote.new( + true, + need_text, + HELP_HELPER, + "%s is bringing %s" % [helper_npc_name, resource_display_name] + ) + if storage == null: + return VillagerOpportunityNote.new( + true, need_text, HELP_UNAVAILABLE, "No storage is within reach right now." + ) + var remaining := maxf( + opportunity.get_target_amount() - storage.get_amount(opportunity.get_resource_id()), 0.0 + ) + if remaining <= 0.0: + return VillagerOpportunityNote.new( + true, + need_text, + HELP_UNAVAILABLE, + "The %s already has enough %s." % [target_display_name, resource_display_name] + ) + if storage.get_available_capacity() < remaining: + return VillagerOpportunityNote.new( + true, need_text, HELP_UNAVAILABLE, "The %s has no room right now." % target_display_name + ) + return VillagerOpportunityNote.new( + true, need_text, HELP_UNAVAILABLE, "No stocked source is in reach right now." + ) diff --git a/player/VillagerOpportunityNote.gd.uid b/player/VillagerOpportunityNote.gd.uid new file mode 100644 index 0000000..56d2924 --- /dev/null +++ b/player/VillagerOpportunityNote.gd.uid @@ -0,0 +1 @@ +uid://ds7o7atkwyfon diff --git a/player/player.gd b/player/player.gd index 54a1aae..a26bf27 100644 --- a/player/player.gd +++ b/player/player.gd @@ -249,6 +249,7 @@ func get_nearby_villager_inspection() -> VillagerInspectionResult: if decision != null and decision.action_id == npc.current_task: reason = decision.reason var need := _get_open_need_for_npc(npc) + need["opportunity_note"] = _build_opportunity_note(npc) return VillagerInspectionResult.new( npc.id, npc.npc_name, @@ -263,6 +264,57 @@ func get_nearby_villager_inspection() -> VillagerInspectionResult: ) +func _build_opportunity_note(npc: SimNPC) -> VillagerOpportunityNote: + if ( + simulation_manager == null + or not simulation_manager.has_method("get_active_opportunity") + or not simulation_manager.has_method("get_active_opportunity_player_response") + or not simulation_manager.has_method("get_active_opportunity_helper") + ): + return null + var opportunity: OpportunityStateRecord = simulation_manager.get_active_opportunity() + if ( + opportunity == null + or opportunity.get_interested_npc_id() != npc.id + or ( + opportunity.get_target_id() + not in [SimulationIds.STORAGE_VILLAGE_PANTRY, SimulationIds.STORAGE_VILLAGE_WOODPILE] + ) + ): + return null + var player_response: OpportunityPlayerResponseResult = ( + simulation_manager.get_active_opportunity_player_response() + ) + var helper: OpportunityHelperResult = simulation_manager.get_active_opportunity_helper() + var helper_name := "" + if helper != null: + var helper_npc := _find_npc(helper.helper_npc_id) + if helper_npc != null: + helper_name = helper_npc.npc_name + return VillagerOpportunityNote.derive( + opportunity, + player_response, + helper, + helper_name, + _get_opportunity_storage(opportunity), + _get_target_display_name(opportunity.get_target_id()), + _display_id(opportunity.get_resource_id()).to_lower() + ) + + +func _get_opportunity_storage(opportunity: OpportunityStateRecord) -> StorageStateRecord: + if simulation_manager == null: + return null + if opportunity.get_target_id() == SimulationIds.STORAGE_VILLAGE_PANTRY: + return simulation_manager.get_pantry() as StorageStateRecord + if ( + opportunity.get_target_id() == SimulationIds.STORAGE_VILLAGE_WOODPILE + and simulation_manager.has_method("get_woodpile") + ): + return simulation_manager.get_woodpile() as StorageStateRecord + return null + + func _get_open_need_for_npc(npc: SimNPC) -> Dictionary: if not simulation_manager.has_method("get_latest_opportunity_for_npc"): return { @@ -439,14 +491,24 @@ func _build_resource_context(node: ResourceNode) -> PlayerInteractionResult: definition.display_name if definition != null else String(node.action_id).capitalize() ) var display_name := String(node.node_id).replace("_", " ").capitalize() + var carried := _get_player_carried(node.resource_id) + var detail := ( + "%s · %.1f remaining · carrying %.1f" % [display_name, node.get_amount_remaining(), carried] + ) + if _get_available_carry() <= 0.0: + detail = "Hands full — deposit before gathering" return PlayerInteractionResult.new( PlayerInteractionResult.KIND_RESOURCE, node.action_id, node.node_id, display_name, prompt, - "%s · %.1f remaining" % [display_name, node.get_amount_remaining()], - node + detail, + node, + { + "blocked_reason": + "" if _get_available_carry() > 0.0 else "You cannot carry more right now." + } ) @@ -526,13 +588,19 @@ func _execute_animal_interaction(context: PlayerInteractionResult) -> void: func _execute_resource_interaction(context: PlayerInteractionResult) -> void: var node := context.target_node as ResourceNode - if node == null or not simulation_manager.has_method("harvest_resource_node"): - push_error("Player: SimulationManager cannot harvest the resolved ResourceNode") + if node == null or not simulation_manager.has_method("player_gather"): + push_error("Player: SimulationManager cannot gather the resolved ResourceNode") return - var extracted: float = simulation_manager.harvest_resource_node(node) + var extracted: float = simulation_manager.player_gather(node) if extracted <= 0.0: interaction_feedback.emit( - "%s is unavailable" % context.display_name, "Nothing could be gathered.", false + "%s is unavailable" % context.display_name, + ( + context.blocked_reason + if not context.blocked_reason.is_empty() + else "Nothing could be gathered." + ), + false ) return interaction_feedback.emit( @@ -582,6 +650,38 @@ func _execute_activity_interaction(context: PlayerInteractionResult) -> void: ) +func _execute_talk_interaction(context: PlayerInteractionResult) -> void: + var npc_id := _talk_npc_id_from_target(context.target_id) + if npc_id < 0: + return + var quest: PlayerQuestRecord = simulation_manager.accept_villager_request(npc_id) + if quest == null: + interaction_feedback.emit( + "%s declines" % context.display_name, + "They have no request you can accept right now.", + false + ) + return + interaction_feedback.emit( + "Request accepted", + ( + "%s asked you for help · +%.0f Standing on completion" + % [context.display_name, quest.get_standing_reward()] + ), + true + ) + + +func _talk_npc_id_from_target(target_id: StringName) -> int: + var prefix := "npc_" + var suffix := "_inventory" + var raw := String(target_id) + if not raw.begins_with(prefix) or not raw.ends_with(suffix): + return -1 + var id_text := raw.trim_prefix(prefix).trim_suffix(suffix) + return id_text.to_int() + + func _execute_pantry_interaction(context: PlayerInteractionResult) -> void: if not context.is_available(): interaction_feedback.emit("The pantry is empty", context.blocked_reason, false) @@ -624,6 +724,37 @@ func _storage_name_for(resource_id: StringName) -> String: return "village pantry" if resource_id == SimulationIds.RESOURCE_FOOD else "village woodpile" +func _get_player_hunger() -> float: + if simulation_manager == null or not simulation_manager.has_method("get_player_state"): + return 0.0 + var state: PlayerStateRecord = simulation_manager.get_player_state() + return state.get_hunger() if state != null else 0.0 + + +func is_starving() -> bool: + if simulation_manager == null or not simulation_manager.has_method("get_player_state"): + return false + var state: PlayerStateRecord = simulation_manager.get_player_state() + return state.is_starving() if state != null else false + + +func _needs_speed_factor() -> float: + if simulation_manager == null or not simulation_manager.has_method("get_player_state"): + return 1.0 + var state: PlayerStateRecord = simulation_manager.get_player_state() + if state == null or state.is_dead(): + return 0.0 + var factor := 1.0 + if state.is_starving(): + factor *= 0.5 + var energy := state.get_energy() + if energy < 20.0: + factor *= 0.6 + elif energy < 45.0: + factor *= 0.8 + return factor + + func _get_animal_feed_cost() -> float: var definition := SimulationDefinitions.get_action(SimulationIds.ACTION_FEED_ANIMAL) return definition.completion_cost_amount if definition != null else 1.0 @@ -648,6 +779,20 @@ func _get_carried_wood() -> float: return simulation_manager.get_player_state().get_inventory_amount(SimulationIds.RESOURCE_WOOD) +func _get_player_carried(resource_id: StringName) -> float: + if simulation_manager == null or not simulation_manager.has_method("get_player_state"): + return 0.0 + var state: PlayerStateRecord = simulation_manager.get_player_state() + return state.get_inventory_amount(resource_id) if state != null else 0.0 + + +func _get_available_carry() -> float: + if simulation_manager == null or not simulation_manager.has_method("get_player_state"): + return 0.0 + var state: PlayerStateRecord = simulation_manager.get_player_state() + return state.get_available_carry() if state != null else 0.0 + + func is_near_storage(storage_node: StorageNode) -> bool: if storage_node == null: return false diff --git a/simulation/SimulationManager.gd b/simulation/SimulationManager.gd index a5f5dab..cc12395 100644 --- a/simulation/SimulationManager.gd +++ b/simulation/SimulationManager.gd @@ -5,6 +5,7 @@ extends Node # intentionally keeps its orchestration readable in one authoritative file. const SimulationEventLogScript := preload("res://simulation/events/SimulationEventLog.gd") +const SimulationEventRecorderScript := preload("res://simulation/events/SimulationEventRecorder.gd") const VillageEconomyScript := preload("res://simulation/economy/VillageEconomy.gd") const AnimalCareSystemScript := preload("res://simulation/animals/animal_care_system.gd") const NpcTickDebugLog := preload("res://simulation/debug/npc_tick_debug_log.gd") @@ -41,6 +42,14 @@ signal raid_started(raider_ids: Array[StringName]) signal war_resolved(outcome: StringName) signal war_aborted signal player_downed +signal player_quest_opened(quest: PlayerQuestRecord) +signal player_quest_completed(quest: PlayerQuestRecord) +signal player_quest_expired(quest: PlayerQuestRecord) +signal player_standing_changed(standing: PlayerStandingRecord) +signal player_tier_advanced(tier: int, tier_name: String) +signal player_needs_changed(state: PlayerStateRecord) +signal player_inventory_changed(state: PlayerStateRecord) +signal player_died(state: PlayerStateRecord) var village := SimVillage.new() var player_system := PlayerCitizenSystem.new() @@ -59,6 +68,7 @@ var tick_count := 0 var wander_random_sources := {} var resource_states: Dictionary = {} var event_log := SimulationEventLogScript.new() +var event_recorder := SimulationEventRecorderScript.new() var economy := VillageEconomyScript.new() var animal_care := AnimalCareSystemScript.new() var relationship_system := RelationshipSystemScript.new() @@ -71,6 +81,10 @@ var commitment_lifecycle := CommitmentLifecycleService.new() var conversation_service := ConversationService.new() var conversation_history: Array[ConversationActStateRecord] = [] var conflict_system := ConflictSystemScript.new() +var player_quest_system := PlayerQuestSystem.new() +var player_needs := PlayerNeedsSystem.new() +var player_negotiation := PlayerNegotiationSystem.new() +var _last_player_tier := PlayerStandingRecord.TIER_STRANGER var storage_states: Dictionary: get: return economy.storage_states @@ -114,6 +128,19 @@ const NPC_NAMES := ["Amina", "Tarik", "Jasmin", "Elma", "Mirza", "Lejla"] func _ready() -> void: add_to_group("simulation_manager") event_log.event_recorded.connect(_on_economic_event_recorded) + event_recorder.configure(event_log, Callable(self, "get_tick_count_for_recording")) + event_recorder.set_npcs(npcs) + player_needs.configure(economy, event_recorder) + player_needs.set_resource_states(resource_states) + player_negotiation.configure( + player_quest_system, + Callable(self, "_find_npc_by_id"), + Callable(self, "get_active_opportunity"), + Callable(self, "get_active_opportunity_player_response"), + player_quest_opened, + player_quest_expired, + player_standing_changed + ) economy.inventory_changed.connect(_on_economy_inventory_changed) economy.economic_event_requested.connect(_record_economic_event) economy.narrative_event_requested.connect(record_narrative_event) @@ -225,6 +252,10 @@ func simulate_tick() -> void: if debug_logs: print("--- Tick ", tick_count, " ---") animal_care.advance(tick_count) + for quest in player_quest_system.consider_animal_care_quests( + animal_care, npcs, get_pantry(), tick_count + ): + player_quest_opened.emit(quest) advance_player_needs() _advance_resource_regrowth() var village_was_changed := false @@ -240,6 +271,10 @@ func simulate_tick() -> void: if invalidated != null: opportunity_invalidated.emit(invalidated) _maintain_emergent_world() + if invalidated != null: + var expired_quest := player_quest_system.on_opportunity_invalidated(invalidated, tick_count) + if expired_quest != null: + player_quest_expired.emit(expired_quest) if tick_count % get_knowledge_review_interval() == 0: _maintain_event_knowledge(true) if debug_logs: @@ -314,7 +349,7 @@ func _select_action_if_idle(npc: SimNPC, previous_state: StringName) -> void: var display_name := ( definition.display_name if definition != null else String(selection.action_id) ) - record_narrative_event(SimulationIds.EVENT_TASK_STARTED, npc.id, &"", display_name) + event_recorder.record_narrative(SimulationIds.EVENT_TASK_STARTED, npc.id, &"", display_name) func _is_weak_villager_state(npc: SimNPC) -> bool: @@ -391,7 +426,7 @@ func _handle_npc_death(npc: SimNPC, previous_task: StringName, previous_target: release_npc_reservation(npc.id) npc_died.emit(npc) npc_task_changed.emit(npc, previous_task, npc.current_task) - record_narrative_event(SimulationIds.EVENT_NPC_DIED, npc.id) + event_recorder.record_narrative(SimulationIds.EVENT_NPC_DIED, npc.id) _notify_mourning(npc) if debug_logs: print("[SimulationManager] NPC died: ", npc.npc_name) @@ -480,7 +515,7 @@ func _complete_resource_gather(npc: SimNPC, completed_task: StringName) -> void: var resource_id := resource_state.get_resource_id() npc.add_inventory(resource_id, extracted) npc_inventory_changed.emit(npc, resource_id, npc.get_inventory_amount(resource_id)) - _record_economic_event( + event_recorder.record_economic( SimulationIds.EVENT_RESOURCE_EXTRACTED, npc.id, resource_state.get_node_id(), @@ -489,7 +524,7 @@ func _complete_resource_gather(npc: SimNPC, completed_task: StringName) -> void: extracted ) if resource_state.get_amount_remaining() <= 0.0: - record_narrative_event( + event_recorder.record_narrative( SimulationIds.EVENT_RESOURCE_DEPLETED, npc.id, resource_state.get_node_id() ) if debug_logs: @@ -1393,6 +1428,10 @@ func _get_event_world_position( return Vector3.ZERO +func get_tick_count_for_recording() -> int: + return tick_count + + func get_npc_events(npc_id: int, max_count: int = 8) -> Array[EconomicEventRecord]: return event_log.get_for_actor(npc_id, max_count) @@ -1407,22 +1446,42 @@ func _on_economic_event_recorded(event: EconomicEventRecord) -> void: ) for learned_record in learned_records: _apply_new_event_knowledge(learned_record, event) + if StringName(event.data["event_type"]) == SimulationIds.EVENT_ANIMAL_FED: + _finish_quest_resolution( + player_quest_system.on_animal_fed( + animal_care.get_state(StringName(event.data["destination_id"])), event, tick_count + ) + ) _update_opportunity_from_event(event) _update_situations_from_event(event) economic_event_recorded.emit(event) +func _finish_quest_resolution(resolution: Dictionary) -> void: + if not resolution.has("quest"): + return + var quest: PlayerQuestRecord = resolution["quest"] + if resolution["completed"]: + player_quest_completed.emit(quest) + var standing := player_quest_system.standing + player_standing_changed.emit(standing) + if standing.get_tier() > _last_player_tier: + _last_player_tier = standing.get_tier() + player_tier_advanced.emit(standing.get_tier(), standing.get_tier_name()) + return + player_quest_expired.emit(quest) + + func try_communicate_at_shared_activity(speaker_id: int, listener_id: int) -> bool: var speaker := _find_npc_by_id(speaker_id) var listener := _find_npc_by_id(listener_id) if not _can_communicate_at_shared_activity(speaker, listener): return false - var known_event_ids := event_knowledge_system.get_communicable_event_ids( + for event_id in event_knowledge_system.get_communicable_event_ids( speaker.id, _get_lasting_event_ids(speaker.id), opportunity_system.get_open_trigger_event_id() - ) - for event_id in known_event_ids: + ): var event := event_log.get_by_id(event_id) if event == null: continue @@ -1439,31 +1498,8 @@ func try_communicate_at_shared_activity(speaker_id: int, listener_id: int) -> bo func _can_communicate_at_shared_activity(speaker: SimNPC, listener: SimNPC) -> bool: - if speaker == null or listener == null or speaker.id == listener.id: - return false - if speaker.is_dead or listener.is_dead: - return false - if ( - speaker.task_state != SimNPC.TASK_STATE_WORKING - or listener.task_state != SimNPC.TASK_STATE_WORKING - ): - return false - if speaker.target_id.is_empty() or speaker.target_id != listener.target_id: - return false - if storage_states.has(speaker.target_id): - return false - var speaker_action := SimulationDefinitions.get_action(speaker.current_task) - var listener_action := SimulationDefinitions.get_action(listener.current_task) - if ( - speaker_action == null - or listener_action == null - or speaker_action.target_type != SimulationIds.TARGET_ACTIVITY - or listener_action.target_type != SimulationIds.TARGET_ACTIVITY - ): - return false - return ( - speaker.position.distance_squared_to(listener.position) - <= KNOWLEDGE_COMMUNICATION_RADIUS * KNOWLEDGE_COMMUNICATION_RADIUS + return EventKnowledgeSystem.can_communicate_at_shared_activity( + speaker, listener, storage_states, KNOWLEDGE_COMMUNICATION_RADIUS ) @@ -1494,8 +1530,19 @@ func _update_opportunity_from_event(event: EconomicEventRecord) -> void: return if changed.get_status() == OpportunityStateRecord.STATUS_OPEN: opportunity_opened.emit(changed) + var quest := player_quest_system.consider_opportunity_opened( + changed, + get_active_opportunity_player_response(), + tick_count, + player_quest_system.can_request_personally(changed) + ) + if quest != null: + player_quest_opened.emit(quest) return opportunity_resolved.emit(changed, event) + _finish_quest_resolution( + player_quest_system.on_opportunity_resolved(changed, event, tick_count) + ) _maintain_event_knowledge(false) @@ -1652,6 +1699,73 @@ func get_active_opportunity() -> OpportunityStateRecord: return opportunity_system.get_open_opportunity() +func get_active_player_quest() -> PlayerQuestRecord: + return player_quest_system.get_active_quest() + + +func _advance_player_needs() -> void: + advance_player_needs() + player_needs_changed.emit(player_system.player_state) + + +func respawn_player() -> bool: + var state := player_system.player_state + if state == null or not state.is_dead(): + return false + state.respawn() + _sync_player_combatant() + player_needs_changed.emit(state) + player_inventory_changed.emit(state) + return true + + +func player_eat(amount: float) -> float: + var state := player_system.player_state + var eaten := state.eat(amount) if state != null else 0.0 + if eaten > 0.0: + player_needs_changed.emit(state) + return eaten + + +func player_gather(node: ResourceNode) -> float: + var gathered := harvest_resource_node(node) + if gathered > 0.0: + player_inventory_changed.emit(player_system.player_state) + return gathered + + +func player_deposit(resource_id: StringName) -> float: + var deposited := deposit_player_inventory(resource_id) + if deposited > 0.0: + village_changed.emit(village) + player_inventory_changed.emit(player_system.player_state) + return deposited + + +func get_player_quests() -> Array[PlayerQuestRecord]: + return player_quest_system.get_all_sorted() + + +func get_villager_talk(npc_id: int) -> PlayerTalkResult: + return player_negotiation.get_talk(npc_id) + + +func accept_villager_request(npc_id: int) -> PlayerQuestRecord: + return player_negotiation.accept_request(npc_id, tick_count) + + +func decline_villager_request(npc_id: int) -> PlayerQuestRecord: + return player_negotiation.decline_request(npc_id, tick_count) + + +func get_latest_player_quest_for_requester(requester_npc_id: int) -> PlayerQuestRecord: + return player_quest_system.get_latest_for_requester(requester_npc_id) + + +func get_player_standing() -> PlayerStandingRecord: + return player_quest_system.standing + + func get_active_opportunity_helper() -> OpportunityHelperResult: var active := get_active_opportunity() if active == null: @@ -2005,6 +2119,11 @@ func get_pantry() -> StorageStateRecord: return economy.get_pantry() +func get_animal_display_name(animal_id: StringName) -> String: + var animal_state := animal_care.get_state(animal_id) + return animal_state.get_display_name() if animal_state != null else "" + + func get_woodpile() -> StorageStateRecord: return economy.get_woodpile() @@ -2139,7 +2258,7 @@ func harvest_resource_node(node: ResourceNode) -> float: if node.interaction_point != null else node.global_position ) - _record_economic_event_at( + event_recorder.record_economic_at( SimulationIds.EVENT_RESOURCE_EXTRACTED, SimulationIds.PLAYER_ACTOR_ID, resource_state.get_node_id(), @@ -2149,7 +2268,7 @@ func harvest_resource_node(node: ResourceNode) -> float: event_position ) if resource_state.get_amount_remaining() <= 0.0: - _record_narrative_event_at( + event_recorder.record_narrative_at( SimulationIds.EVENT_RESOURCE_DEPLETED, SimulationIds.PLAYER_ACTOR_ID, resource_state.get_node_id(), @@ -2172,8 +2291,16 @@ func harvest_resource_node(node: ResourceNode) -> float: func advance_player_needs() -> void: + var was_dead := player_system.player_state.is_dead() player_system.advance_needs(village.food_modifier) player_system.player_state.advance_health() + if not was_dead and player_system.player_state.is_dead(): + player_died.emit(player_system.player_state) + player_quest_system.standing.data["standing"] = ( + player_quest_system.standing.get_standing() * PlayerStateRecord.DEATH_STANDING_PENALTY + ) + _last_player_tier = player_quest_system.standing.get_tier() + player_standing_changed.emit(player_quest_system.standing) _sync_player_combatant() @@ -2311,6 +2438,7 @@ func create_state_record() -> SimulationStateRecord: "next_conversation_act_id": _next_conversation_act_id, "world_id": String(event_scope["world_id"]), "location_id": String(event_scope["location_id"]), + "next_quest_id": player_quest_system.next_quest_id, "cycle_duration_seconds": clock.cycle_duration_seconds } record.village = VillageStateRecord.capture(village) @@ -2345,6 +2473,10 @@ func create_state_record() -> SimulationStateRecord: record.conversation_history.append(conversation_act) record.player = player_system.player_state conflict_system.append_state_records(record) + record.player_standing = player_quest_system.standing + for quest in player_quest_system.get_all_sorted(): + record.player_quests.append(quest) + record.player_state = player_system.player_state return record @@ -2421,6 +2553,12 @@ func restore_state(record: SimulationStateRecord) -> bool: _active_player_conversation = &"" _conversation_context_by_id.clear() _dialogue_speed_index = -1 + player_quest_system.restore( + record.player_quests, int(record.simulation.get("next_quest_id", 0)), record.player_standing + ) + _last_player_tier = player_quest_system.standing.get_tier() + if record.player_state != null: + player_system.player_state = record.player_state _maintain_event_knowledge(tick_count % get_knowledge_review_interval() == 0) wander_random_sources.clear() var wander_streams: Array = record.simulation["wander_random_streams"] diff --git a/simulation/animals/animal_care_system.gd b/simulation/animals/animal_care_system.gd index 82d7619..72e7782 100644 --- a/simulation/animals/animal_care_system.gd +++ b/simulation/animals/animal_care_system.gd @@ -121,6 +121,20 @@ func get_state(animal_id: StringName) -> AnimalStateRecord: return states.get(animal_id) as AnimalStateRecord +func get_all_states() -> Array[AnimalStateRecord]: + var all_states: Array[AnimalStateRecord] = [] + for animal_id in states.keys(): + var animal_state := states[animal_id] as AnimalStateRecord + if animal_state != null: + all_states.append(animal_state) + all_states.sort_custom(_sort_states_by_id) + return all_states + + +static func _sort_states_by_id(first: AnimalStateRecord, second: AnimalStateRecord) -> bool: + return String(first.get_animal_id()) < String(second.get_animal_id()) + + func reserve(animal_id: StringName, agent_id: int) -> bool: var animal_state := get_state(animal_id) return animal_state != null and animal_state.can_npc_feed() and animal_state.reserve(agent_id) diff --git a/simulation/definitions/SimulationIds.gd b/simulation/definitions/SimulationIds.gd index fe41c20..bb4923c 100644 --- a/simulation/definitions/SimulationIds.gd +++ b/simulation/definitions/SimulationIds.gd @@ -85,6 +85,7 @@ const OPPORTUNITY_RESTOCK_EMPTY_PANTRY := &"restock_empty_pantry" const OPPORTUNITY_SUPPLY_MISSING_WOOD := &"supply_missing_wood" const OPPORTUNITY_FEED_WEAK_VILLAGER := &"feed_weak_villager" const OPPORTUNITY_REPAIR_HOME_ROOF := &"repair_home_roof" +const OPPORTUNITY_FEED_HUNGRY_ANIMAL := &"feed_hungry_animal" const OPPORTUNITY_STATUS_OPEN := &"open" const OPPORTUNITY_STATUS_RESOLVED := &"resolved" const OPPORTUNITY_STATUS_INVALIDATED := &"invalidated" diff --git a/simulation/events/SimulationEventRecorder.gd b/simulation/events/SimulationEventRecorder.gd new file mode 100644 index 0000000..c1a84d7 --- /dev/null +++ b/simulation/events/SimulationEventRecorder.gd @@ -0,0 +1,113 @@ +class_name SimulationEventRecorder +extends RefCounted + +const SimulationEventLogScript := preload("res://simulation/events/SimulationEventLog.gd") + +var event_log: RefCounted +var npcs: Array[SimNPC] = [] +var tick_provider: Callable + + +func configure(log: RefCounted, tick_source: Callable) -> void: + event_log = log + tick_provider = tick_source + + +func set_npcs(npc_list: Array[SimNPC]) -> void: + npcs = npc_list + + +func record_economic( + event_type: StringName, + actor_id: int, + source_id: StringName, + destination_id: StringName, + item_id: StringName, + amount: float +) -> void: + record_economic_at( + event_type, + actor_id, + source_id, + destination_id, + item_id, + amount, + get_event_world_position(actor_id, source_id, destination_id) + ) + + +func record_economic_at( + event_type: StringName, + actor_id: int, + source_id: StringName, + destination_id: StringName, + item_id: StringName, + amount: float, + world_position: Vector3 +) -> void: + event_log.record_economic( + tick_provider.call(), + event_type, + actor_id, + source_id, + destination_id, + item_id, + amount, + world_position + ) + + +func record_narrative( + event_type: StringName, + actor_id: int, + source_id: StringName = &"", + action_display: String = "", + action_id: StringName = &"", + item_id: StringName = &"", + required_amount: float = 0.0 +) -> void: + record_narrative_at( + event_type, + actor_id, + source_id, + action_display, + get_event_world_position(actor_id, source_id, &""), + action_id, + item_id, + required_amount + ) + + +func record_narrative_at( + event_type: StringName, + actor_id: int, + source_id: StringName, + action_display: String, + world_position: Vector3, + action_id: StringName = &"", + item_id: StringName = &"", + required_amount: float = 0.0 +) -> void: + event_log.record_narrative( + tick_provider.call(), + event_type, + actor_id, + source_id, + action_display, + world_position, + action_id, + item_id, + required_amount + ) + + +func get_event_world_position( + actor_id: int, source_id: StringName, destination_id: StringName +) -> Vector3: + for npc in npcs: + if npc.id != actor_id: + continue + if not npc.target_id.is_empty() and npc.target_id in [source_id, destination_id]: + return npc.travel_target_position + return npc.position + return Vector3.ZERO diff --git a/simulation/events/SimulationEventRecorder.gd.uid b/simulation/events/SimulationEventRecorder.gd.uid new file mode 100644 index 0000000..d6f41c9 --- /dev/null +++ b/simulation/events/SimulationEventRecorder.gd.uid @@ -0,0 +1 @@ +uid://yl07qwlrpxjs diff --git a/simulation/knowledge/EventKnowledgeSystem.gd b/simulation/knowledge/EventKnowledgeSystem.gd index 0a870d9..7d62cb3 100644 --- a/simulation/knowledge/EventKnowledgeSystem.gd +++ b/simulation/knowledge/EventKnowledgeSystem.gd @@ -267,6 +267,65 @@ func restore(records: Array[KnownEventStateRecord]) -> void: known_events[_key(record.get_knower_id(), record.get_event_id())] = record +static func can_communicate_at_shared_activity( + speaker: SimNPC, listener: SimNPC, storage_states: Dictionary, radius: float +) -> bool: + if speaker == null or listener == null or speaker.id == listener.id: + return false + if speaker.is_dead or listener.is_dead: + return false + if ( + speaker.task_state != SimNPC.TASK_STATE_WORKING + or listener.task_state != SimNPC.TASK_STATE_WORKING + ): + return false + if speaker.target_id.is_empty() or speaker.target_id != listener.target_id: + return false + if storage_states.has(speaker.target_id): + return false + var speaker_action := SimulationDefinitions.get_action(speaker.current_task) + var listener_action := SimulationDefinitions.get_action(listener.current_task) + if ( + speaker_action == null + or listener_action == null + or speaker_action.target_type != SimulationIds.TARGET_ACTIVITY + or listener_action.target_type != SimulationIds.TARGET_ACTIVITY + ): + return false + return speaker.position.distance_squared_to(listener.position) <= radius * radius + + +func collect_lasting_records( + relationships: Array[RelationshipStateRecord], open_opportunity: OpportunityStateRecord +) -> Array[KnownEventStateRecord]: + var lasting: Array[KnownEventStateRecord] = [] + var seen := {} + for relationship in relationships: + var event_id := relationship.get_last_trust_cause_event_id() + if event_id == RelationshipStateRecord.NO_CAUSE_EVENT: + continue + var record := get_record(relationship.get_observer_id(), event_id) + if record == null: + continue + var key := "%d:%d" % [record.get_knower_id(), record.get_event_id()] + if seen.has(key): + continue + seen[key] = true + lasting.append(record) + if open_opportunity != null: + var opportunity_record := get_record( + open_opportunity.get_interested_npc_id(), open_opportunity.get_trigger_event_id() + ) + if opportunity_record != null: + var opportunity_key := ( + "%d:%d" % [opportunity_record.get_knower_id(), opportunity_record.get_event_id()] + ) + if not seen.has(opportunity_key): + seen[opportunity_key] = true + lasting.append(opportunity_record) + return lasting + + func _remember( knower_id: int, event_id: int, diff --git a/simulation/opportunities/VillageOpportunitySystem.gd b/simulation/opportunities/VillageOpportunitySystem.gd index 3a3efda..7d3a6c8 100644 --- a/simulation/opportunities/VillageOpportunitySystem.gd +++ b/simulation/opportunities/VillageOpportunitySystem.gd @@ -661,7 +661,10 @@ static func _is_valid_supply( var actor_id := int(event.data["actor_id"]) if event_type == SimulationIds.EVENT_STORAGE_DEPOSITED: if actor_id == SimulationIds.PLAYER_ACTOR_ID: - return StringName(event.data["source_id"]) == SimulationIds.PLAYER_INVENTORY_ID + return ( + StringName(event.data["source_id"]) + in [SimulationIds.PLAYER_INVENTORY_ID, &"player_carry"] + ) return ( _find_npc(actor_id, npcs) != null and StringName(event.data["source_id"]) == SimulationIds.npc_inventory_id(actor_id) diff --git a/simulation/player/player_citizen_system.gd b/simulation/player/player_citizen_system.gd index 2ee1787..3966706 100644 --- a/simulation/player/player_citizen_system.gd +++ b/simulation/player/player_citizen_system.gd @@ -14,6 +14,10 @@ func configure(economy_state: RefCounted, record_event: Callable) -> void: func advance_needs(food_modifier: float) -> void: player_state.set_hunger(player_state.get_hunger() + 0.125 * food_modifier) player_state.set_energy(maxf(player_state.get_energy() - 0.0625, 0.0)) + # Starvation is an authoritative state transition, not a UI-only warning. + # Reuse the record's bounded compatibility transition so save/load and the + # legacy embodied-needs slice observe the same death counter and fact state. + player_state.advance(0.0, 0.0) func add_carried(item_id: StringName, amount: float) -> void: diff --git a/simulation/quests/PlayerNeedsSystem.gd b/simulation/quests/PlayerNeedsSystem.gd new file mode 100644 index 0000000..c756635 --- /dev/null +++ b/simulation/quests/PlayerNeedsSystem.gd @@ -0,0 +1,117 @@ +class_name PlayerNeedsSystem +extends RefCounted + +var state := PlayerStateRecord.create() +var economy: RefCounted +var event_recorder: RefCounted +var resource_states: Dictionary = {} + + +func configure(economy_service: RefCounted, recorder: RefCounted) -> void: + economy = economy_service + event_recorder = recorder + + +func set_resource_states(states: Dictionary) -> void: + resource_states = states + + +func advance(village: SimVillage) -> bool: + if state.is_dead(): + return false + var hunger_rate := 0.125 * village.food_modifier + if state.advance(hunger_rate, 0.5): + state.die() + return true + return false + + +func eat(amount: float) -> float: + return state.eat(amount) + + +func gather_node(node: ResourceNode) -> float: + if node == null: + return 0.0 + var resource_state := resource_states.get(node.node_id) as ResourceStateRecord + if resource_state == null or not resource_state.can_player_use_resource(): + return 0.0 + var available := state.get_available_carry() + if available <= 0.0: + return 0.0 + var extracted := resource_state.extract(minf(resource_state.get_yield_per_action(), available)) + if extracted <= 0.0: + return 0.0 + var carried := state.add_inventory(resource_state.get_resource_id(), extracted) + if carried <= 0.0: + resource_state.data["amount_remaining"] = ( + resource_state.get_amount_remaining() + extracted + ) + return 0.0 + var event_position := ( + node.interaction_point.global_position + if node.interaction_point != null + else node.global_position + ) + event_recorder.record_economic_at( + SimulationIds.EVENT_RESOURCE_EXTRACTED, + -1, + resource_state.get_node_id(), + &"player_carry", + resource_state.get_resource_id(), + carried, + event_position + ) + if resource_state.get_amount_remaining() <= 0.0: + event_recorder.record_narrative_at( + SimulationIds.EVENT_RESOURCE_DEPLETED, + -1, + resource_state.get_node_id(), + "", + event_position + ) + return carried + + +func deposit_resource(resource_id: StringName) -> float: + if resource_id.is_empty(): + return 0.0 + var carried := state.get_inventory_amount(resource_id) + if carried <= 0.0: + return 0.0 + if economy == null or not economy.has_method("get_storage_for_resource"): + return 0.0 + var storage: StorageStateRecord = economy.get_storage_for_resource(resource_id) + if storage == null: + return 0.0 + var deposited: float = economy.deposit_resource(resource_id, carried) + if deposited <= 0.0: + return 0.0 + state.remove_inventory(resource_id, deposited) + event_recorder.record_economic( + SimulationIds.EVENT_STORAGE_DEPOSITED, + -1, + &"player_carry", + storage.get_storage_id(), + resource_id, + deposited + ) + return deposited + + +func get_carried_amount(item_id: StringName) -> float: + return state.get_inventory_amount(item_id) + + +func respawn() -> void: + if state.is_dead(): + state.respawn() + + +func apply_death_consequence(standing: PlayerStandingRecord) -> void: + var reduced := standing.get_standing() * PlayerStateRecord.DEATH_STANDING_PENALTY + standing.data["standing"] = reduced + + +func restore(record: PlayerStateRecord) -> void: + state = record if record != null else PlayerStateRecord.create() diff --git a/simulation/quests/PlayerNeedsSystem.gd.uid b/simulation/quests/PlayerNeedsSystem.gd.uid new file mode 100644 index 0000000..410bb19 --- /dev/null +++ b/simulation/quests/PlayerNeedsSystem.gd.uid @@ -0,0 +1 @@ +uid://brf3a7drbfs7 diff --git a/simulation/quests/PlayerNegotiationSystem.gd b/simulation/quests/PlayerNegotiationSystem.gd new file mode 100644 index 0000000..7a0ef20 --- /dev/null +++ b/simulation/quests/PlayerNegotiationSystem.gd @@ -0,0 +1,67 @@ +class_name PlayerNegotiationSystem +extends RefCounted + +var quest_system: PlayerQuestSystem +var npc_lookup: Callable +var opportunity_provider: Callable +var player_response_provider: Callable +var quest_opened_signal: Signal +var quest_expired_signal: Signal +var standing_changed_signal: Signal + + +func configure( + quests: PlayerQuestSystem, + lookup: Callable, + opportunity_source: Callable, + response_source: Callable, + opened: Signal, + expired: Signal, + standing: Signal +) -> void: + quest_system = quests + npc_lookup = lookup + opportunity_provider = opportunity_source + player_response_provider = response_source + quest_opened_signal = opened + quest_expired_signal = expired + standing_changed_signal = standing + + +func get_talk(npc_id: int) -> PlayerTalkResult: + var npc := npc_lookup.call(npc_id) as SimNPC + if npc == null or npc.is_dead: + return null + return quest_system.get_talk_for_requester(npc.id, npc.npc_name, opportunity_provider.call()) + + +func accept_request(npc_id: int, current_tick: int) -> PlayerQuestRecord: + var npc := npc_lookup.call(npc_id) as SimNPC + if npc == null: + return null + var opportunity: OpportunityStateRecord = opportunity_provider.call() + if opportunity == null or opportunity.get_interested_npc_id() != npc.id: + return null + var result: Dictionary = quest_system.accept_opportunity_quest( + opportunity, player_response_provider.call(), current_tick + ) + var quest := result.get("quest") as PlayerQuestRecord + if quest != null and result.get("created", false) and not quest_opened_signal.is_null(): + quest_opened_signal.emit(quest) + return quest + + +func decline_request(npc_id: int, current_tick: int) -> PlayerQuestRecord: + var npc := npc_lookup.call(npc_id) as SimNPC + if npc == null: + return null + var opportunity: OpportunityStateRecord = opportunity_provider.call() + if opportunity == null or opportunity.get_interested_npc_id() != npc.id: + return null + var quest := quest_system.decline_opportunity_quest(opportunity, current_tick) + if quest != null: + if not quest_expired_signal.is_null(): + quest_expired_signal.emit(quest) + if not standing_changed_signal.is_null(): + standing_changed_signal.emit(quest_system.standing) + return quest diff --git a/simulation/quests/PlayerNegotiationSystem.gd.uid b/simulation/quests/PlayerNegotiationSystem.gd.uid new file mode 100644 index 0000000..7e87320 --- /dev/null +++ b/simulation/quests/PlayerNegotiationSystem.gd.uid @@ -0,0 +1 @@ +uid://7oayv5ga327o diff --git a/simulation/quests/PlayerQuestSystem.gd b/simulation/quests/PlayerQuestSystem.gd new file mode 100644 index 0000000..fb9374d --- /dev/null +++ b/simulation/quests/PlayerQuestSystem.gd @@ -0,0 +1,283 @@ +class_name PlayerQuestSystem +extends RefCounted + +const PANTRY_STANDING_REWARD := 8.0 +const WOOD_STANDING_REWARD := 8.0 +const ANIMAL_CARE_STANDING_REWARD := 6.0 +const PERSONAL_TRUST_THRESHOLD := 0.5 +const MIN_PERSONAL_REQUEST_TIER := PlayerStandingRecord.TIER_KNOWN_HAND + +var quests: Array[PlayerQuestRecord] = [] +var next_quest_id := 0 +var standing := PlayerStandingRecord.create() + + +func consider_animal_care( + animal_state: AnimalStateRecord, + requester_npc_id: int, + current_tick: int, + pantry: StorageStateRecord +) -> PlayerQuestRecord: + if ( + animal_state == null + or requester_npc_id < 0 + or current_tick < 0 + or pantry == null + or not animal_state.needs_feed() + or not animal_state.can_player_feed_animal() + ): + return null + var definition := SimulationDefinitions.get_action(SimulationIds.ACTION_FEED_ANIMAL) + if ( + definition == null + or not definition.has_completion_cost() + or ( + pantry.get_amount(definition.completion_cost_resource_id) + < definition.completion_cost_amount + ) + ): + return null + if _find_open_quest_for_animal(animal_state.get_animal_id()) != null: + return null + var quest := PlayerQuestRecord.create( + next_quest_id, + PlayerQuestRecord.NO_OPPORTUNITY, + requester_npc_id, + SimulationIds.OPPORTUNITY_FEED_HUNGRY_ANIMAL, + definition.completion_cost_resource_id, + animal_state.get_animal_id(), + definition.completion_cost_amount, + current_tick, + ANIMAL_CARE_STANDING_REWARD, + animal_state.get_animal_id() + ) + next_quest_id += 1 + quests.append(quest) + return quest + + +func on_animal_fed( + animal_state: AnimalStateRecord, resolution_event: EconomicEventRecord, current_tick: int +) -> Dictionary: + if animal_state == null or resolution_event == null: + return {} + var quest := _find_open_quest_for_animal(animal_state.get_animal_id()) + if quest == null: + return {} + var event_id := int(resolution_event.data["event_id"]) + var actor_id := int(resolution_event.data["actor_id"]) + if actor_id < 0: + if not quest.complete(event_id, current_tick): + return {} + standing.grant_standing(quest.get_standing_reward(), quest.get_requester_npc_id()) + return {"quest": quest, "completed": true} + if quest.expire(current_tick): + return {"quest": quest, "completed": false} + return {} + + +func consider_opportunity_opened( + opportunity: OpportunityStateRecord, + player_response: OpportunityPlayerResponseResult, + current_tick: int, + personal_request_eligible := false +) -> PlayerQuestRecord: + if opportunity == null or not opportunity.is_open() or current_tick < 0: + return null + if player_response == null and not personal_request_eligible: + return null + if _find_open_quest_for_opportunity(opportunity.get_opportunity_id()) != null: + return null + var reward := _standing_reward_for(opportunity) + if reward <= 0.0: + return null + var quest := PlayerQuestRecord.create( + next_quest_id, + opportunity.get_opportunity_id(), + opportunity.get_interested_npc_id(), + opportunity.get_opportunity_type(), + opportunity.get_resource_id(), + opportunity.get_target_id(), + opportunity.get_target_amount(), + current_tick, + reward + ) + next_quest_id += 1 + quests.append(quest) + return quest + + +func can_request_personally(opportunity: OpportunityStateRecord) -> bool: + if ( + opportunity == null + or not opportunity.is_open() + or standing.get_tier() < MIN_PERSONAL_REQUEST_TIER + ): + return false + return standing.get_gratitude(opportunity.get_interested_npc_id()) >= PERSONAL_TRUST_THRESHOLD + + +func on_opportunity_resolved( + opportunity: OpportunityStateRecord, resolution_event: EconomicEventRecord, current_tick: int +) -> Dictionary: + var quest := _find_open_quest_for_opportunity(opportunity.get_opportunity_id()) + if quest == null or resolution_event == null: + return {} + var event_id := int(resolution_event.data["event_id"]) + var actor_id := int(resolution_event.data["actor_id"]) + if actor_id < 0: + if not quest.complete(event_id, current_tick): + return {} + standing.grant_standing(quest.get_standing_reward(), quest.get_requester_npc_id()) + return {"quest": quest, "completed": true} + if quest.expire(current_tick): + return {"quest": quest, "completed": false} + return {} + + +func get_talk_for_requester( + requester_id: int, requester_name: String, opportunity: OpportunityStateRecord +) -> PlayerTalkResult: + var has_need := opportunity != null and opportunity.is_open() + var need_text := "" + if has_need: + need_text = "I'm worried about the %s." % String(opportunity.get_resource_id()).capitalize() + var quest := get_latest_for_requester(requester_id) + var quest_available := quest != null and quest.is_open() + var reward := quest.get_standing_reward() if quest_available else 0.0 + var can_accept := has_need and (quest_available or can_request_personally(opportunity)) + return PlayerTalkResult.new( + requester_id, + requester_name, + "%s greets you." % requester_name, + has_need, + need_text, + quest_available, + reward, + can_accept, + has_need + ) + + +func accept_opportunity_quest( + opportunity: OpportunityStateRecord, + player_response: OpportunityPlayerResponseResult, + current_tick: int +) -> Dictionary: + var existing := _find_open_quest_for_opportunity(opportunity.get_opportunity_id()) + if existing != null: + return {"quest": existing, "created": false} + var created := consider_opportunity_opened( + opportunity, player_response, current_tick, can_request_personally(opportunity) + ) + return {"quest": created, "created": created != null} + + +func decline_opportunity_quest( + opportunity: OpportunityStateRecord, current_tick: int +) -> PlayerQuestRecord: + var quest := _find_open_quest_for_opportunity(opportunity.get_opportunity_id()) + if quest == null: + return null + if not quest.expire(current_tick): + return null + var requester_id := quest.get_requester_npc_id() + var gratitude := standing.get_gratitude(requester_id) + standing.data["gratitude"][requester_id] = maxf(gratitude - 0.1, 0.0) + return quest + + +func on_opportunity_invalidated( + opportunity: OpportunityStateRecord, current_tick: int +) -> PlayerQuestRecord: + var quest := _find_open_quest_for_opportunity(opportunity.get_opportunity_id()) + if quest == null: + return null + if quest.expire(current_tick): + return quest + return null + + +func consider_animal_care_quests( + animal_care: AnimalCareSystem, + npcs: Array[SimNPC], + pantry: StorageStateRecord, + current_tick: int +) -> Array[PlayerQuestRecord]: + var opened: Array[PlayerQuestRecord] = [] + for animal_state in animal_care.get_all_states(): + var requester := _nearest_npc_id(animal_state.get_position(), npcs) + var quest := consider_animal_care(animal_state, requester, current_tick, pantry) + if quest != null: + opened.append(quest) + return opened + + +static func _nearest_npc_id(from_position: Vector3, npcs: Array[SimNPC]) -> int: + var nearest_id := -1 + var nearest_distance := INF + for npc in npcs: + if npc.is_dead: + continue + var distance := npc.position.distance_squared_to(from_position) + if distance < nearest_distance: + nearest_distance = distance + nearest_id = npc.id + return nearest_id + + +func get_active_quest() -> PlayerQuestRecord: + for quest in quests: + if quest.is_open(): + return quest + return null + + +func get_all_sorted() -> Array[PlayerQuestRecord]: + var sorted := quests.duplicate() + sorted.sort_custom(_sort_by_id) + return sorted + + +func get_latest_for_requester(requester_npc_id: int) -> PlayerQuestRecord: + for index in range(quests.size() - 1, -1, -1): + var quest := quests[index] + if quest.get_requester_npc_id() == requester_npc_id: + return quest + return null + + +func restore( + records: Array[PlayerQuestRecord], restored_next_id: int, standing_record: PlayerStandingRecord +) -> void: + quests = records.duplicate() + quests.sort_custom(_sort_by_id) + next_quest_id = maxi(restored_next_id, 0) + standing = standing_record if standing_record != null else PlayerStandingRecord.create() + + +func _find_open_quest_for_opportunity(opportunity_id: int) -> PlayerQuestRecord: + for quest in quests: + if quest.is_open() and quest.get_opportunity_id() == opportunity_id: + return quest + return null + + +func _find_open_quest_for_animal(animal_id: StringName) -> PlayerQuestRecord: + for quest in quests: + if quest.is_open() and quest.has_animal_target() and quest.get_animal_id() == animal_id: + return quest + return null + + +func _standing_reward_for(opportunity: OpportunityStateRecord) -> float: + match opportunity.get_opportunity_type(): + SimulationIds.OPPORTUNITY_RESTOCK_EMPTY_PANTRY: + return PANTRY_STANDING_REWARD + SimulationIds.OPPORTUNITY_SUPPLY_MISSING_WOOD: + return WOOD_STANDING_REWARD + return 0.0 + + +static func _sort_by_id(first: PlayerQuestRecord, second: PlayerQuestRecord) -> bool: + return first.get_quest_id() < second.get_quest_id() diff --git a/simulation/quests/PlayerQuestSystem.gd.uid b/simulation/quests/PlayerQuestSystem.gd.uid new file mode 100644 index 0000000..9925385 --- /dev/null +++ b/simulation/quests/PlayerQuestSystem.gd.uid @@ -0,0 +1 @@ +uid://cojertujqx8ts diff --git a/simulation/state/PlayerQuestRecord.gd b/simulation/state/PlayerQuestRecord.gd new file mode 100644 index 0000000..9cc2583 --- /dev/null +++ b/simulation/state/PlayerQuestRecord.gd @@ -0,0 +1,224 @@ +class_name PlayerQuestRecord +extends RefCounted + +const SCHEMA_VERSION := 2 +const LEGACY_SCHEMA_VERSION := 1 +const STATUS_OPEN := &"open" +const STATUS_COMPLETED := &"completed" +const STATUS_EXPIRED := &"expired" +const NO_OPPORTUNITY := -1 + +var data: Dictionary + + +func _init(record_data: Dictionary = {}) -> void: + data = record_data.duplicate(true) + + +static func create( + quest_id: int, + opportunity_id: int, + requester_npc_id: int, + quest_type: StringName, + resource_id: StringName, + target_id: StringName, + target_amount: float, + created_tick: int, + standing_reward: float, + animal_id: StringName = &"" +) -> PlayerQuestRecord: + return ( + PlayerQuestRecord + . new( + { + "schema_version": SCHEMA_VERSION, + "quest_id": quest_id, + "opportunity_id": opportunity_id, + "requester_npc_id": requester_npc_id, + "quest_type": String(quest_type), + "resource_id": String(resource_id), + "target_id": String(target_id), + "target_amount": target_amount, + "status": String(STATUS_OPEN), + "created_tick": created_tick, + "standing_reward": standing_reward, + "resolution_event_id": -1, + "resolved_tick": -1, + "animal_id": String(animal_id), + } + ) + ) + + +static func from_dictionary(record_data: Dictionary) -> PlayerQuestRecord: + var version := int(record_data.get("schema_version", -1)) + if version not in [LEGACY_SCHEMA_VERSION, SCHEMA_VERSION]: + return null + var normalized := record_data.duplicate(true) + if version == LEGACY_SCHEMA_VERSION: + normalized["schema_version"] = SCHEMA_VERSION + normalized["animal_id"] = "" + if not ( + normalized + . has_all( + [ + "quest_id", + "opportunity_id", + "requester_npc_id", + "quest_type", + "resource_id", + "target_id", + "target_amount", + "status", + "created_tick", + "standing_reward", + "resolution_event_id", + "resolved_tick", + "animal_id", + ] + ) + ): + return null + var quest_id := int(normalized["quest_id"]) + var opportunity_id := int(normalized["opportunity_id"]) + var requester_npc_id := int(normalized["requester_npc_id"]) + var quest_type := StringName(normalized["quest_type"]) + var resource_id := StringName(normalized["resource_id"]) + var target_id := StringName(normalized["target_id"]) + var target_amount := float(normalized["target_amount"]) + var status := StringName(normalized["status"]) + var created_tick := int(normalized["created_tick"]) + var standing_reward := float(normalized["standing_reward"]) + var resolution_event_id := int(normalized["resolution_event_id"]) + var resolved_tick := int(normalized["resolved_tick"]) + var animal_id := StringName(normalized["animal_id"]) + if ( + quest_id < 0 + or requester_npc_id < 0 + or quest_type.is_empty() + or resource_id.is_empty() + or target_id.is_empty() + or not is_finite(target_amount) + or target_amount <= 0.0 + or created_tick < 0 + or not is_finite(standing_reward) + or standing_reward <= 0.0 + or resolution_event_id < -1 + or (opportunity_id < 0 and animal_id.is_empty()) + or (not animal_id.is_empty() and opportunity_id != NO_OPPORTUNITY) + ): + return null + match status: + STATUS_OPEN: + if resolution_event_id != -1 or resolved_tick != -1: + return null + STATUS_COMPLETED: + if resolution_event_id < 0 or resolved_tick < created_tick: + return null + STATUS_EXPIRED: + if resolution_event_id != -1 or resolved_tick < created_tick: + return null + _: + return null + return ( + PlayerQuestRecord + . new( + { + "schema_version": SCHEMA_VERSION, + "quest_id": quest_id, + "opportunity_id": opportunity_id, + "requester_npc_id": requester_npc_id, + "quest_type": String(quest_type), + "resource_id": String(resource_id), + "target_id": String(target_id), + "target_amount": target_amount, + "status": String(status), + "created_tick": created_tick, + "standing_reward": standing_reward, + "resolution_event_id": resolution_event_id, + "resolved_tick": resolved_tick, + "animal_id": String(animal_id), + } + ) + ) + + +func get_quest_id() -> int: + return int(data["quest_id"]) + + +func get_opportunity_id() -> int: + return int(data["opportunity_id"]) + + +func get_requester_npc_id() -> int: + return int(data["requester_npc_id"]) + + +func get_quest_type() -> StringName: + return StringName(data["quest_type"]) + + +func get_resource_id() -> StringName: + return StringName(data["resource_id"]) + + +func get_target_id() -> StringName: + return StringName(data["target_id"]) + + +func get_target_amount() -> float: + return float(data["target_amount"]) + + +func get_status() -> StringName: + return StringName(data["status"]) + + +func get_created_tick() -> int: + return int(data["created_tick"]) + + +func get_standing_reward() -> float: + return float(data["standing_reward"]) + + +func get_resolution_event_id() -> int: + return int(data["resolution_event_id"]) + + +func get_resolved_tick() -> int: + return int(data["resolved_tick"]) + + +func get_animal_id() -> StringName: + return StringName(data["animal_id"]) + + +func has_animal_target() -> bool: + return not get_animal_id().is_empty() + + +func is_open() -> bool: + return get_status() == STATUS_OPEN + + +func complete(resolution_event_id: int, resolved_tick: int) -> bool: + if not is_open() or resolution_event_id < 0 or resolved_tick < get_created_tick(): + return false + data["status"] = String(STATUS_COMPLETED) + data["resolution_event_id"] = resolution_event_id + data["resolved_tick"] = resolved_tick + return true + + +func expire(resolved_tick: int) -> bool: + if not is_open() or resolved_tick < get_created_tick(): + return false + data["status"] = String(STATUS_EXPIRED) + data["resolved_tick"] = resolved_tick + return true + + +func to_dictionary() -> Dictionary: + return data.duplicate(true) diff --git a/simulation/state/PlayerQuestRecord.gd.uid b/simulation/state/PlayerQuestRecord.gd.uid new file mode 100644 index 0000000..306765b --- /dev/null +++ b/simulation/state/PlayerQuestRecord.gd.uid @@ -0,0 +1 @@ +uid://rix0e3rygdoo diff --git a/simulation/state/PlayerStandingRecord.gd b/simulation/state/PlayerStandingRecord.gd new file mode 100644 index 0000000..896fb64 --- /dev/null +++ b/simulation/state/PlayerStandingRecord.gd @@ -0,0 +1,120 @@ +class_name PlayerStandingRecord +extends RefCounted + +const SCHEMA_VERSION := 1 +const MAX_STANDING := 100.0 +const TIER_STRANGER := 0 +const TIER_KNOWN_HAND := 1 +const TIER_TRUSTED := 2 +const TIER_VILLAGE_STEWARD := 3 +const TIER_VOICE_OF_JAJCE := 4 + +const TIER_THRESHOLDS := { + TIER_STRANGER: 0.0, + TIER_KNOWN_HAND: 15.0, + TIER_TRUSTED: 35.0, + TIER_VILLAGE_STEWARD: 60.0, + TIER_VOICE_OF_JAJCE: 85.0, +} + +const TIER_NAMES := { + TIER_STRANGER: "Stranger", + TIER_KNOWN_HAND: "Known Hand", + TIER_TRUSTED: "Trusted", + TIER_VILLAGE_STEWARD: "Village Steward", + TIER_VOICE_OF_JAJCE: "Voice of Jajce", +} + +var data: Dictionary + + +func _init(record_data: Dictionary = {}) -> void: + data = record_data.duplicate(true) + + +static func create() -> PlayerStandingRecord: + return ( + PlayerStandingRecord + . new( + { + "schema_version": SCHEMA_VERSION, + "standing": 0.0, + "resolved_needs": 0, + "gratitude": {}, + } + ) + ) + + +static func from_dictionary(record_data: Dictionary) -> PlayerStandingRecord: + if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION: + return null + if not record_data.has_all(["standing", "resolved_needs", "gratitude"]): + return null + var standing := float(record_data["standing"]) + var resolved_needs := int(record_data["resolved_needs"]) + var gratitude = record_data["gratitude"] + if ( + not is_finite(standing) + or standing < 0.0 + or standing > MAX_STANDING + or resolved_needs < 0 + or not gratitude is Dictionary + ): + return null + var normalized_gratitude := {} + for raw_npc_id in gratitude: + var npc_id := int(raw_npc_id) + var value := float(gratitude[raw_npc_id]) + if npc_id < 0 or not is_finite(value) or value < 0.0 or value > 1.0: + return null + normalized_gratitude[npc_id] = value + return ( + PlayerStandingRecord + . new( + { + "schema_version": SCHEMA_VERSION, + "standing": standing, + "resolved_needs": resolved_needs, + "gratitude": normalized_gratitude, + } + ) + ) + + +func grant_standing(amount: float, requester_npc_id: int) -> void: + if not is_finite(amount) or amount <= 0.0 or requester_npc_id < 0: + return + data["standing"] = minf(get_standing() + amount, MAX_STANDING) + data["resolved_needs"] = int(data["resolved_needs"]) + 1 + var gratitude := get_gratitude(requester_npc_id) + data["gratitude"][requester_npc_id] = minf(gratitude + 0.1, 1.0) + + +func get_standing() -> float: + return float(data["standing"]) + + +func get_resolved_needs() -> int: + return int(data["resolved_needs"]) + + +func get_gratitude(npc_id: int) -> float: + return float(data["gratitude"].get(npc_id, 0.0)) + + +func get_tier() -> int: + var current := TIER_STRANGER + for tier in [TIER_VOICE_OF_JAJCE, TIER_VILLAGE_STEWARD, TIER_TRUSTED, TIER_KNOWN_HAND]: + if get_standing() >= float(TIER_THRESHOLDS[tier]): + current = tier + break + return current + + +func get_tier_name() -> String: + return String(TIER_NAMES[get_tier()]) + + +func to_dictionary() -> Dictionary: + return data.duplicate(true) diff --git a/simulation/state/PlayerStandingRecord.gd.uid b/simulation/state/PlayerStandingRecord.gd.uid new file mode 100644 index 0000000..bad3daf --- /dev/null +++ b/simulation/state/PlayerStandingRecord.gd.uid @@ -0,0 +1 @@ +uid://v827brnqruus diff --git a/simulation/state/SimulationStateRecord.gd b/simulation/state/SimulationStateRecord.gd index e2039a7..fd07f27 100644 --- a/simulation/state/SimulationStateRecord.gd +++ b/simulation/state/SimulationStateRecord.gd @@ -19,6 +19,8 @@ const CONFLICT_SCHEMA_VERSION := 13 const PRE_EMERGENT_SCHEMA_VERSION := 14 const EMERGENT_SCHEMA_VERSION := 15 const PREVIOUS_SCHEMA_VERSION := 7 +const PLAYER_LEGACY_SCHEMA_VERSION := 11 +const PLAYER_STATE_LEGACY_SCHEMA_VERSION := 12 var simulation: Dictionary var village: VillageStateRecord @@ -37,6 +39,9 @@ var conversation_history: Array[ConversationActStateRecord] = [] var player: PlayerStateRecord var combatants: Array[CombatantStateRecord] = [] var factions: Array[FactionStateRecord] = [] +var player_standing: PlayerStandingRecord +var player_quests: Array[PlayerQuestRecord] = [] +var player_state: PlayerStateRecord func to_dictionary() -> Dictionary: @@ -83,6 +88,9 @@ func to_dictionary() -> Dictionary: var faction_data: Array[Dictionary] = [] for faction_record in factions: faction_data.append(faction_record.to_dictionary()) + var player_quest_data: Array[Dictionary] = [] + for player_quest_record in player_quests: + player_quest_data.append(player_quest_record.to_dictionary()) return { "schema": SCHEMA_NAME, @@ -103,7 +111,10 @@ func to_dictionary() -> Dictionary: "conversation_history": conversation_history_data, "player": player.to_dictionary(), "combatants": combatant_data, - "factions": faction_data + "factions": faction_data, + "player_standing": player_standing.to_dictionary(), + "player_quests": player_quest_data, + "player_state": player_state.to_dictionary() } @@ -140,6 +151,8 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord: CONFLICT_SCHEMA_VERSION, PRE_EMERGENT_SCHEMA_VERSION, EMERGENT_SCHEMA_VERSION, + PLAYER_LEGACY_SCHEMA_VERSION, + PLAYER_STATE_LEGACY_SCHEMA_VERSION, ] ): record_data = _migrate_legacy(record_data, version) @@ -166,6 +179,9 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord: "player", "combatants", "factions", + "player_standing", + "player_quests", + "player_state", ] ) ): @@ -551,6 +567,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord: record.relationships.append(relationship_record) var opportunity_ids := {} + var opportunity_records_by_id := {} var trigger_event_ids := {} var resolution_event_ids := {} var has_open_opportunity := false @@ -585,6 +602,7 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord: ): return null opportunity_ids[opportunity_id] = true + opportunity_records_by_id[opportunity_id] = opportunity_record trigger_event_ids[trigger_event_id] = true highest_opportunity_id = maxi(highest_opportunity_id, opportunity_id) record.opportunities.append(opportunity_record) @@ -620,6 +638,72 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord: record.conversation_history.append(conversation_act) if saved_next_conversation_act_id <= highest_conversation_act_id: return null + var standing_data = record_data["player_standing"] + if not standing_data is Dictionary: + return null + var standing_record := PlayerStandingRecord.from_dictionary(standing_data) + if standing_record == null: + return null + record.player_standing = standing_record + + var player_quest_data = record_data["player_quests"] + if not player_quest_data is Array: + return null + var quest_ids := {} + var quest_opportunity_ids := {} + var highest_quest_id := -1 + for item in player_quest_data: + if not item is Dictionary: + return null + var quest_record := PlayerQuestRecord.from_dictionary(item) + if quest_record == null: + return null + var quest_id := quest_record.get_quest_id() + var quest_opportunity_id := quest_record.get_opportunity_id() + var requester_id := quest_record.get_requester_npc_id() + var has_valid_requester := npc_ids.has(requester_id) + var has_valid_opportunity := ( + quest_record.has_animal_target() or opportunity_ids.has(quest_opportunity_id) + ) + if ( + quest_ids.has(quest_id) + or quest_opportunity_ids.has(quest_opportunity_id) + or not has_valid_requester + or not has_valid_opportunity + ): + return null + if ( + quest_record.get_status() == PlayerQuestRecord.STATUS_OPEN + and ( + (not quest_record.has_animal_target()) + and not opportunity_records_by_id.has(quest_opportunity_id) + ) + ): + return null + if ( + quest_record.get_resolution_event_id() >= 0 + and not event_ids.has(quest_record.get_resolution_event_id()) + ): + return null + var dedup_key := ( + "animal:%s" % String(quest_record.get_animal_id()) + if quest_record.has_animal_target() + else "opp:%d" % quest_opportunity_id + ) + if quest_ids.has(quest_id) or quest_opportunity_ids.has(dedup_key): + return null + quest_ids[quest_id] = true + quest_opportunity_ids[dedup_key] = true + highest_quest_id = maxi(highest_quest_id, quest_id) + record.player_quests.append(quest_record) + + var player_state_data = record_data["player_state"] + if not player_state_data is Dictionary: + return null + var player_state_record := PlayerStateRecord.from_dictionary(player_state_data) + if player_state_record == null: + return null + record.player_state = player_state_record return record @@ -940,7 +1024,10 @@ static func _is_valid_supply_resolution( return false if event_type == SimulationIds.EVENT_STORAGE_DEPOSITED: if actor_id == SimulationIds.PLAYER_ACTOR_ID: - return StringName(event.data["source_id"]) == SimulationIds.PLAYER_INVENTORY_ID + return ( + StringName(event.data["source_id"]) + in [SimulationIds.PLAYER_INVENTORY_ID, &"player_carry"] + ) return ( npc_ids.has(actor_id) and StringName(event.data["source_id"]) == SimulationIds.npc_inventory_id(actor_id) @@ -1066,6 +1153,14 @@ static func _migrate_legacy(legacy_data: Dictionary, version: int) -> Dictionary var opportunity_simulation_data: Dictionary = migrated.get("simulation", {}) opportunity_simulation_data["next_opportunity_id"] = 0 migrated["simulation"] = opportunity_simulation_data + if version <= PLAYER_LEGACY_SCHEMA_VERSION: + migrated["player_standing"] = PlayerStandingRecord.create().to_dictionary() + migrated["player_quests"] = [] + var quest_simulation_data: Dictionary = migrated.get("simulation", {}) + quest_simulation_data["next_quest_id"] = 0 + migrated["simulation"] = quest_simulation_data + if version <= PLAYER_STATE_LEGACY_SCHEMA_VERSION: + migrated["player_state"] = PlayerStateRecord.create().to_dictionary() return migrated diff --git a/simulation/state/player_state_record.gd b/simulation/state/player_state_record.gd index 58510fe..f17d7c7 100644 --- a/simulation/state/player_state_record.gd +++ b/simulation/state/player_state_record.gd @@ -4,6 +4,12 @@ extends RefCounted const SCHEMA_VERSION := 2 const LEGACY_SCHEMA_VERSION := 1 const MAX_HEALTH := 100.0 +const RESET_HUNGER := 40.0 +const RESET_ENERGY := 100.0 +const STACK_THRESHOLD := 80.0 +const DEATH_STANDING_PENALTY := 0.85 +const DEFAULT_STARVATION_DEATH_THRESHOLD := 1 +const DEFAULT_CARRY_CAPACITY := 3.0 var data: Dictionary @@ -18,18 +24,30 @@ static func create_default() -> PlayerStateRecord: . new( { "schema_version": SCHEMA_VERSION, - "hunger": 40.0, - "energy": 100.0, + "hunger": RESET_HUNGER, + "energy": RESET_ENERGY, "health": MAX_HEALTH, "max_health": MAX_HEALTH, "downed": false, "downed_ticks": 0, + "dead": false, + "deaths": 0, + "starvation_ticks": 0, + "starvation_death_threshold": DEFAULT_STARVATION_DEATH_THRESHOLD, + "carry_capacity": DEFAULT_CARRY_CAPACITY, "inventory": {}, } ) ) +# Compatibility constructor for the first embodied-player slice. New code +# should use create_default(), but keeping this alias lets older quest/needs +# resources load without introducing a second player-state schema. +static func create() -> PlayerStateRecord: + return create_default() + + static func from_dictionary(record_data: Dictionary) -> PlayerStateRecord: var version := int(record_data.get("schema_version", -1)) if version == LEGACY_SCHEMA_VERSION: @@ -76,6 +94,20 @@ static func from_dictionary(record_data: Dictionary) -> PlayerStateRecord: "max_health": max_health, "downed": bool(record_data["downed"]), "downed_ticks": int(record_data["downed_ticks"]), + "dead": bool(record_data.get("dead", false)), + "deaths": maxi(int(record_data.get("deaths", 0)), 0), + "starvation_ticks": maxi(int(record_data.get("starvation_ticks", 0)), 0), + "starvation_death_threshold": + maxi( + int( + record_data.get( + "starvation_death_threshold", DEFAULT_STARVATION_DEATH_THRESHOLD + ) + ), + 1 + ), + "carry_capacity": + maxf(float(record_data.get("carry_capacity", DEFAULT_CARRY_CAPACITY)), 0.0), "inventory": normalized_inventory, } ) @@ -89,6 +121,11 @@ static func _migrate_v1(legacy_data: Dictionary) -> Dictionary: migrated["max_health"] = MAX_HEALTH migrated["downed"] = false migrated["downed_ticks"] = 0 + migrated["dead"] = false + migrated["deaths"] = 0 + migrated["starvation_ticks"] = 0 + migrated["starvation_death_threshold"] = DEFAULT_STARVATION_DEATH_THRESHOLD + migrated["carry_capacity"] = DEFAULT_CARRY_CAPACITY return migrated @@ -112,6 +149,18 @@ func is_downed() -> bool: return bool(data["downed"]) +func is_dead() -> bool: + return bool(data.get("dead", false)) + + +func is_starving() -> bool: + return get_hunger() >= STACK_THRESHOLD and not is_dead() + + +func get_deaths() -> int: + return maxi(int(data.get("deaths", 0)), 0) + + func get_downed_ticks() -> int: return int(data["downed_ticks"]) @@ -124,6 +173,54 @@ func set_energy(amount: float) -> void: data["energy"] = clampf(amount, 0.0, 100.0) +func advance(hunger_rate: float, energy_drain: float) -> bool: + if is_dead(): + return true + set_hunger(get_hunger() + maxf(hunger_rate, 0.0)) + set_energy(get_energy() - maxf(energy_drain, 0.0)) + if get_hunger() < STACK_THRESHOLD: + data["starvation_ticks"] = 0 + return false + data["starvation_ticks"] = int(data.get("starvation_ticks", 0)) + 1 + if int(data["starvation_ticks"]) >= int(data.get("starvation_death_threshold", 1)): + die() + return true + return false + + +func eat(amount: float) -> float: + if not is_finite(amount) or amount <= 0.0 or is_dead(): + return 0.0 + var consumed := minf(amount, get_hunger()) + set_hunger(get_hunger() - consumed) + set_energy(get_energy() + consumed * 0.5) + data["starvation_ticks"] = 0 + return consumed + + +func die() -> void: + if is_dead(): + return + data["dead"] = true + data["deaths"] = get_deaths() + 1 + + +func respawn() -> void: + data["dead"] = false + data["downed"] = false + data["downed_ticks"] = 0 + data["hunger"] = RESET_HUNGER + data["energy"] = RESET_ENERGY + data["health"] = get_max_health() + data["starvation_ticks"] = 0 + + +func get_available_carry() -> float: + return maxf( + float(data.get("carry_capacity", DEFAULT_CARRY_CAPACITY)) - get_carried_total(), 0.0 + ) + + func take_damage(amount: float) -> float: if not is_finite(amount) or amount <= 0.0 or is_downed(): return 0.0 @@ -157,10 +254,14 @@ func get_carried_total() -> float: return total -func add_inventory(item_id: StringName, amount: float) -> void: +func add_inventory(item_id: StringName, amount: float) -> float: if item_id.is_empty() or not is_finite(amount) or amount <= 0.0: - return - data["inventory"][String(item_id)] = get_inventory_amount(item_id) + amount + return 0.0 + var added := minf(amount, get_available_carry()) + if added <= 0.0: + return 0.0 + data["inventory"][String(item_id)] = get_inventory_amount(item_id) + added + return added func remove_inventory(item_id: StringName, requested_amount: float) -> float: diff --git a/tests/field_note_opportunity_surface_test.gd b/tests/field_note_opportunity_surface_test.gd new file mode 100644 index 0000000..0646d15 --- /dev/null +++ b/tests/field_note_opportunity_surface_test.gd @@ -0,0 +1,163 @@ +extends SceneTree + +var failures: Array[String] = [] + + +func _initialize() -> void: + call_deferred("_run") + + +func _run() -> void: + var main_scene: Node = load("res://main.tscn").instantiate() + root.add_child(main_scene) + await process_frame + for _frame in 10: + await physics_frame + + var manager: Node = main_scene.get_node("SimulationManager") + var view: Node = main_scene.get_node("WorldViewManager") + var player: CharacterBody3D = main_scene.get_node("Player") + var demo: Node = main_scene.get_node("DemoController") + var field_note := main_scene.get_node("VillagerInspectionLayer/VillagerFieldNote") + manager.set_process(false) + player.set_physics_process(false) + _freeze_visuals(view) + + var staged: Dictionary = demo.call("stage_pantry_crisis") + _check(not staged.is_empty(), "The runtime proof should stage a real open pantry need") + if staged.is_empty(): + _finish() + return + var interested: SimNPC = staged["interested"] + var pantry_position: Vector3 = ( + StorageNode.get_by_id(SimulationIds.STORAGE_VILLAGE_PANTRY).get_interaction_position() + ) + player.global_position = pantry_position + _move_population_away(manager, view, interested.id, pantry_position) + _set_loaded_position(manager, view, interested, pantry_position + Vector3(1.0, 0.0, 0.0)) + + var checksum_before: String = manager.get_state_checksum() + var serialized_before: String = manager.serialize_state() + var events_before: int = manager.economic_events.size() + for _query in 3: + player.call("get_nearby_villager_inspection") + field_note.call("refresh_note", true) + var need_label := field_note.get_node("Copy/Need") as Label + _check( + ( + manager.get_state_checksum() == checksum_before + and manager.serialize_state() == serialized_before + and manager.economic_events.size() == events_before + and field_note.visible + and need_label.visible + and "Need" in need_label.text + ), + "The inspected interested villager should surface the real need without mutating state", + ) + if not need_label.visible: + _finish() + return + + var helper: OpportunityHelperResult = manager.get_active_opportunity_helper() + var helper_npc := _find_npc(manager.npcs, helper.helper_npc_id) if helper != null else null + _check( + ( + helper != null + and helper_npc != null + and helper_npc.npc_name in need_label.text + and "bringing" in need_label.text.to_lower() + ), + "The open need with a capable helper should name that helper on the field note", + ) + + var checksum_after_helper: String = manager.get_state_checksum() + var food_node: ResourceNode = _find_food_node(manager) + _check(food_node != null, "The resolution proof needs a stocked player-usable food source") + if food_node == null: + _finish() + return + var harvested: float = manager.harvest_resource_node(food_node) + var deposited: float = manager.player_deposit(SimulationIds.RESOURCE_FOOD) + _check( + ( + harvested > 0.0 + and deposited > 0.0 + and manager.get_active_opportunity() == null + and ( + manager.opportunity_system.get_latest().get_status() + == OpportunityStateRecord.STATUS_RESOLVED + ) + ), + "An ordinary finite-resource harvest and pantry deposit should resolve the same open need", + ) + field_note.call("refresh_note", true) + _check( + not need_label.visible and manager.get_state_checksum() != checksum_after_helper, + "Resolving the need should clear the surfaced need segment from the field note", + ) + + _finish() + + +func _find_food_node(manager: Node) -> ResourceNode: + for node in ResourceNode.get_all(): + var state: ResourceStateRecord = ( + manager.get_resource_state(node.node_id) as ResourceStateRecord + ) + if ( + node.resource_id == SimulationIds.RESOURCE_FOOD + and state != null + and state.can_player_use_resource() + and state.can_extract() + and state.get_amount_remaining() >= 1.0 + ): + return node + return null + + +func _find_npc(npcs: Array[SimNPC], npc_id: int) -> SimNPC: + for npc in npcs: + if npc.id == npc_id: + return npc + return null + + +func _move_population_away(manager: Node, view: Node, keep_id: int, origin: Vector3) -> void: + for npc in manager.npcs: + if npc.id == keep_id: + continue + _set_loaded_position( + manager, view, npc, origin + Vector3(40.0 + float(npc.id) * 2.0, 0.0, 20.0) + ) + + +func _set_loaded_position(manager: Node, view: Node, npc: SimNPC, position: Vector3) -> void: + manager.synchronize_npc_position(npc.id, position) + var visual := view.active_npc_visuals.get(npc.id) as Node3D + if visual != null: + visual.set_physics_process(false) + if visual.has_method("stop_travel"): + visual.stop_travel() + visual.global_position = position + + +func _freeze_visuals(view: Node) -> void: + for visual in view.active_npc_visuals.values(): + visual.set_physics_process(false) + if visual.has_method("stop_travel"): + visual.stop_travel() + + +func _check(condition: bool, message: String) -> void: + if not condition: + failures.append(message) + + +func _finish() -> void: + if failures.is_empty(): + print("[TEST] Field-note opportunity surfacing passed") + quit(0) + return + for failure in failures: + push_error("[TEST] " + failure) + quit(1) diff --git a/tests/field_note_opportunity_surface_test.gd.uid b/tests/field_note_opportunity_surface_test.gd.uid new file mode 100644 index 0000000..64b8c76 --- /dev/null +++ b/tests/field_note_opportunity_surface_test.gd.uid @@ -0,0 +1 @@ +uid://dg66jcyhajmxp diff --git a/tests/jajce_runtime_integration_test.gd b/tests/jajce_runtime_integration_test.gd index 55306b5..fdfd949 100644 --- a/tests/jajce_runtime_integration_test.gd +++ b/tests/jajce_runtime_integration_test.gd @@ -322,6 +322,9 @@ func _run() -> void: opportunity_bush_state.set_amount_remaining(1.0) player.global_position = opportunity_bush.interaction_point.global_position player.call("try_interact") + var carried_after_gather: float = simulation_manager.get_player_state().get_inventory_amount( + SimulationIds.RESOURCE_FOOD + ) _check( ( simulation_manager.get_player_state().get_inventory_amount(SimulationIds.RESOURCE_FOOD) @@ -338,13 +341,14 @@ func _run() -> void: village_ui.call("_refresh_npc_inspector") _check( ( - player_opportunity.get_status() == OpportunityStateRecord.STATUS_RESOLVED + is_equal_approx(carried_after_gather, 1.0) + and player_opportunity.get_status() == OpportunityStateRecord.STATUS_RESOLVED and "Resolved village need" in inspector_label.text and "◆ Player restocked the pantry" in inspector_label.text and whisper_kicker.text == "NEED MET" and "The player restocked the pantry" in whisper_message.text ), - "Player gathering should resolve the restored need in debug detail and transient HUD" + "Player gathering and depositing should resolve the restored need in debug detail and transient HUD" ) var woodpile_state: StorageStateRecord = simulation_manager.get_woodpile() diff --git a/tests/player_carry_deposit_test.gd b/tests/player_carry_deposit_test.gd new file mode 100644 index 0000000..235c7b6 --- /dev/null +++ b/tests/player_carry_deposit_test.gd @@ -0,0 +1,188 @@ +extends SceneTree + +var failures: Array[String] = [] + + +func _initialize() -> void: + call_deferred("_run") + + +func _run() -> void: + var manager := _create_manager() + var actor: SimNPC = manager.npcs[0] + var interested: SimNPC = manager.npcs[2] + var berry := _register_resource( + manager, + &"carry_berry_bush", + SimulationIds.ACTION_GATHER_FOOD, + SimulationIds.RESOURCE_FOOD, + 3.0 + ) + var pantry: StorageStateRecord = manager.get_pantry() + _set_pantry_amount(manager, 1.0) + for npc in manager.npcs: + npc.position = Vector3(40.0 + npc.id * 10.0, 0.0, 0.0) + actor.position = Vector3.ZERO + interested.position = Vector3(4.0, 0.0, 0.0) + actor.hunger = 60.0 + interested.hunger = 90.0 + manager.economy.withdraw_to_inventory(actor, SimulationIds.RESOURCE_FOOD, 1.0) + var quest: PlayerQuestRecord = manager.get_active_player_quest() + _check(quest != null, "The carry loop needs an open player quest") + if quest == null: + berry.free() + manager.free() + _finish() + return + + var state: PlayerStateRecord = manager.get_player_state() + var carried: float = manager.player_gather(berry) + _check( + ( + is_equal_approx(carried, 2.0) + and is_equal_approx(state.get_inventory_amount(SimulationIds.RESOURCE_FOOD), 2.0) + and is_equal_approx(manager.get_pantry().get_amount(SimulationIds.RESOURCE_FOOD), 0.0) + ), + "Gathering should fill the player's hands instead of the pantry directly", + ) + var capacity_limited: float = manager.player_gather(berry) + _check( + ( + is_equal_approx(capacity_limited, 1.0) + and is_equal_approx(state.get_inventory_amount(SimulationIds.RESOURCE_FOOD), 3.0) + ), + "Remaining resource amount should cap the second gather", + ) + var hands_full: float = manager.player_gather(berry) + _check( + ( + is_equal_approx(hands_full, 0.0) + and is_equal_approx(state.get_inventory_amount(SimulationIds.RESOURCE_FOOD), 3.0) + ), + "An empty resource should refuse further gathering", + ) + + var pantry_before: float = manager.get_pantry().get_amount(SimulationIds.RESOURCE_FOOD) + var deposited: float = manager.player_deposit(SimulationIds.RESOURCE_FOOD) + _check( + ( + is_equal_approx(deposited, 3.0) + and is_equal_approx( + manager.get_pantry().get_amount(SimulationIds.RESOURCE_FOOD), pantry_before + 3.0 + ) + and is_equal_approx(state.get_inventory_amount(SimulationIds.RESOURCE_FOOD), 0.0) + and manager.get_active_player_quest() == null + and ( + manager.get_latest_player_quest_for_requester(interested.id).get_status() + == PlayerQuestRecord.STATUS_COMPLETED + ) + ), + "Depositing carried food should resolve the quest through the real pantry transaction", + ) + + var saved_json: String = manager.serialize_state() + var restored := _create_manager(902) + _check( + restored.restore_state_from_json(saved_json), + "The emptied carry state should restore through the current schema" + ) + _check( + ( + restored.get_state_checksum() == manager.get_state_checksum() + and is_equal_approx( + restored.get_player_state().get_inventory_amount(SimulationIds.RESOURCE_FOOD), 0.0 + ) + ), + "Restore should preserve the player's empty carried inventory and checksum", + ) + + var carry_state_saved: String = manager.serialize_state() + manager.player_gather(berry) + var mid_carry: String = manager.serialize_state() + var mid_restored := _create_manager(903) + _check( + mid_restored.restore_state_from_json(mid_carry), + "A partially carried inventory should restore through the current schema" + ) + _check( + ( + mid_restored.get_state_checksum() == manager.get_state_checksum() + and is_equal_approx( + mid_restored.get_player_state().get_inventory_amount(SimulationIds.RESOURCE_FOOD), + manager.get_player_state().get_inventory_amount(SimulationIds.RESOURCE_FOOD) + ) + ), + "Restore should preserve the player's carried inventory mid-transport", + ) + + berry.free() + manager.free() + restored.free() + mid_restored.free() + _finish() + + +func _register_resource( + manager: Node, + node_id: StringName, + action_id: StringName, + resource_id: StringName, + amount: float +) -> ResourceNode: + var node := ResourceNode.new() + node.name = "CarryResource" + node.node_id = node_id + node.action_id = action_id + node.resource_id = resource_id + node.initial_amount = amount + node.yield_per_action = 2.0 + node.debug_label_enabled = false + var interaction_point := Marker3D.new() + interaction_point.name = "InteractionPoint" + node.add_child(interaction_point) + root.add_child(node) + _check( + manager.register_resource_node(node), + "The carry loop should bind a real finite ResourceNode" + ) + return node + + +func _create_manager(seed_value: int = 901) -> Node: + var manager: Node = load("res://simulation/SimulationManager.gd").new() + manager.simulation_seed = seed_value + manager.debug_logs = false + var home_positions: Array[Vector3] = [ + Vector3(0.0, 0.0, 0.0), + Vector3(2.0, 0.0, 0.0), + Vector3(10.0, 0.0, 0.0), + Vector3(12.0, 0.0, 0.0), + Vector3(30.0, 0.0, 30.0), + Vector3(40.0, 0.0, 40.0), + ] + manager.home_positions = home_positions + root.add_child(manager) + manager.set_process(false) + return manager + + +func _set_pantry_amount(manager: Node, amount: float) -> void: + var pantry: StorageStateRecord = manager.get_pantry() + pantry.withdraw(SimulationIds.RESOURCE_FOOD, pantry.get_amount(SimulationIds.RESOURCE_FOOD)) + pantry.deposit(SimulationIds.RESOURCE_FOOD, amount) + manager.economy.sync_resource(SimulationIds.RESOURCE_FOOD) + + +func _check(condition: bool, message: String) -> void: + if not condition: + failures.append(message) + + +func _finish() -> void: + if failures.is_empty(): + print("[TEST] Player carry and deposit loop passed") + quit(0) + return + for failure in failures: + push_error("[TEST] " + failure) + quit(1) diff --git a/tests/player_carry_deposit_test.gd.uid b/tests/player_carry_deposit_test.gd.uid new file mode 100644 index 0000000..319a50e --- /dev/null +++ b/tests/player_carry_deposit_test.gd.uid @@ -0,0 +1 @@ +uid://bwyg38d3ieiex diff --git a/tests/player_needs_loop_test.gd b/tests/player_needs_loop_test.gd new file mode 100644 index 0000000..52d7d21 --- /dev/null +++ b/tests/player_needs_loop_test.gd @@ -0,0 +1,130 @@ +extends SceneTree + +var failures: Array[String] = [] +var died_events := 0 + + +func _initialize() -> void: + call_deferred("_run") + + +func _run() -> void: + var manager := _create_manager() + manager.player_died.connect(_on_player_died) + + var state: PlayerStateRecord = manager.get_player_state() + _check( + ( + state != null + and is_equal_approx(state.get_hunger(), 40.0) + and is_equal_approx(state.get_energy(), 100.0) + and not state.is_dead() + ), + "A fresh player should start with embodied needs and no death state", + ) + + for _tick in 3: + manager.simulate_tick() + _check( + ( + is_equal_approx(state.get_hunger(), 40.375) + and is_equal_approx(state.get_energy(), 99.8125) + ), + "Simulation ticks should advance the player's hunger and drain energy", + ) + + state.data["hunger"] = 80.0 + var hunger_before_eat: float = state.get_hunger() + var eaten: float = manager.player_eat(10.0) + _check( + ( + is_equal_approx(eaten, 10.0) + and state.get_hunger() < hunger_before_eat + and state.get_energy() > 80.0 + ), + "Eating should reduce the player's own hunger and restore energy", + ) + + var initial_standing: float = manager.get_player_standing().get_standing() + manager.get_player_standing().grant_standing(20.0, 1) + var standing_after_help: float = manager.get_player_standing().get_standing() + state.data["hunger"] = PlayerStateRecord.STACK_THRESHOLD + state.data["starvation_ticks"] = (int(state.data["starvation_death_threshold"]) - 1) + manager.simulate_tick() + _check( + ( + died_events == 1 + and state.is_dead() + and is_equal_approx( + manager.get_player_standing().get_standing(), + standing_after_help * PlayerStateRecord.DEATH_STANDING_PENALTY + ) + ), + "Starvation death should emit a death event and cut standing as a consequence", + ) + + var saved_json: String = manager.serialize_state() + var restored := _create_manager(902) + _check( + restored.restore_state_from_json(saved_json), + "The dead player state should restore through the current schema" + ) + _check( + ( + restored.get_state_checksum() == manager.get_state_checksum() + and restored.get_player_state().is_dead() + and restored.get_player_state().get_deaths() == 1 + ), + "Restore should preserve the embodied death state and checksum", + ) + + _check(manager.respawn_player(), "A dead player should be able to respawn") + _check( + ( + not state.is_dead() + and is_equal_approx(state.get_hunger(), PlayerStateRecord.RESET_HUNGER) + and is_equal_approx(state.get_energy(), PlayerStateRecord.RESET_ENERGY) + ), + "Respawn should reset needs to the documented baseline", + ) + + manager.free() + restored.free() + _finish() + + +func _create_manager(seed_value: int = 901) -> Node: + var manager: Node = load("res://simulation/SimulationManager.gd").new() + manager.simulation_seed = seed_value + manager.debug_logs = false + var home_positions: Array[Vector3] = [ + Vector3(0.0, 0.0, 0.0), + Vector3(2.0, 0.0, 0.0), + Vector3(10.0, 0.0, 0.0), + Vector3(12.0, 0.0, 0.0), + Vector3(30.0, 0.0, 30.0), + Vector3(40.0, 0.0, 40.0), + ] + manager.home_positions = home_positions + root.add_child(manager) + manager.set_process(false) + return manager + + +func _on_player_died(_state: PlayerStateRecord) -> void: + died_events += 1 + + +func _check(condition: bool, message: String) -> void: + if not condition: + failures.append(message) + + +func _finish() -> void: + if failures.is_empty(): + print("[TEST] Embodied player needs passed") + quit(0) + return + for failure in failures: + push_error("[TEST] " + failure) + quit(1) diff --git a/tests/player_needs_loop_test.gd.uid b/tests/player_needs_loop_test.gd.uid new file mode 100644 index 0000000..0462580 --- /dev/null +++ b/tests/player_needs_loop_test.gd.uid @@ -0,0 +1 @@ +uid://jeax46jjayqj diff --git a/tests/player_quest_standing_test.gd b/tests/player_quest_standing_test.gd new file mode 100644 index 0000000..1a95aed --- /dev/null +++ b/tests/player_quest_standing_test.gd @@ -0,0 +1,397 @@ +extends SceneTree + +var failures: Array[String] = [] +var opened_quest_ids: Array[int] = [] +var completed_quest_ids: Array[int] = [] +var expired_quest_ids: Array[int] = [] + + +func _initialize() -> void: + call_deferred("_run") + + +func _run() -> void: + var manager := _create_manager() + manager.player_quest_opened.connect(_on_quest_opened) + manager.player_quest_completed.connect(_on_quest_completed) + manager.player_quest_expired.connect(_on_quest_expired) + var berry := _register_berry(manager, &"quest_berry_bush") + var actor: SimNPC = manager.npcs[0] + var interested: SimNPC = manager.npcs[2] + _set_pantry_amount(manager, 1.0) + for npc in manager.npcs: + npc.position = Vector3(40.0 + npc.id * 10.0, 0.0, 0.0) + actor.position = Vector3.ZERO + actor.hunger = 60.0 + interested.hunger = 90.0 + manager.economy.withdraw_to_inventory(actor, SimulationIds.RESOURCE_FOOD, 1.0) + _prepare_shared_patrol(actor, interested, Vector3(20.0, 0.0, 0.0)) + _check( + manager.try_communicate_at_shared_activity(actor.id, interested.id), + "The quest proof should open the same known pantry shortage" + ) + _check(manager.get_active_opportunity() != null, "The quest proof needs an active opportunity") + var quest: PlayerQuestRecord = manager.get_active_player_quest() + _check( + ( + quest != null + and quest.get_requester_npc_id() == interested.id + and quest.get_quest_type() == SimulationIds.OPPORTUNITY_RESTOCK_EMPTY_PANTRY + and quest.get_resource_id() == SimulationIds.RESOURCE_FOOD + and quest.get_target_id() == SimulationIds.STORAGE_VILLAGE_PANTRY + and is_equal_approx(quest.get_target_amount(), 1.0) + and quest.get_standing_reward() > 0.0 + and quest.is_open() + and opened_quest_ids == [quest.get_quest_id()] + ), + "A player-actionable shortage should generate one quest for its worried villager", + ) + if quest == null: + manager.free() + _finish() + return + + var no_helper_route: OpportunityPlayerResponseResult = ( + manager.get_active_opportunity_player_response() + ) + var standing_before: PlayerStandingRecord = manager.get_player_standing() + _check( + ( + no_helper_route != null + and is_equal_approx(standing_before.get_standing(), 0.0) + and standing_before.get_resolved_needs() == 0 + and is_equal_approx(standing_before.get_gratitude(interested.id), 0.0) + ), + "Standing should start at zero with no gratitude before any player help", + ) + + var checksum_before_resolve: String = manager.get_state_checksum() + var serialized_before: String = manager.serialize_state() + var berry_harvested: float = manager.harvest_resource_node(berry) + _check( + is_equal_approx(berry_harvested, 1.0), + "The quest should use the ordinary player harvest command" + ) + _check( + manager.player_deposit(SimulationIds.RESOURCE_FOOD) > 0.0, + "Completing the quest should use the ordinary pantry deposit command" + ) + var standing_after: PlayerStandingRecord = manager.get_player_standing() + var completed: PlayerQuestRecord = manager.get_active_player_quest() + var latest: PlayerQuestRecord = manager.get_latest_player_quest_for_requester(interested.id) + _check( + ( + completed == null + and latest != null + and latest.get_status() == PlayerQuestRecord.STATUS_COMPLETED + and completed_quest_ids == [latest.get_quest_id()] + and is_equal_approx(standing_after.get_standing(), quest.get_standing_reward()) + and standing_after.get_resolved_needs() == 1 + and is_equal_approx(standing_after.get_gratitude(interested.id), 0.1) + and manager.get_state_checksum() != checksum_before_resolve + ), + "A real player supply should complete the quest, grant standing, and raise gratitude", + ) + + var saved_json: String = serialized_before + var pre_completion_state: String = manager.serialize_state() + var restored := _create_manager(902) + _check( + restored.restore_state_from_json(pre_completion_state), + "The completed quest and standing should restore through the current schema" + ) + _check( + ( + restored.get_state_checksum() == manager.get_state_checksum() + and restored.get_active_player_quest() == null + and restored.get_latest_player_quest_for_requester(interested.id) != null + and is_equal_approx( + restored.get_player_standing().get_standing(), quest.get_standing_reward() + ) + and is_equal_approx(restored.get_player_standing().get_gratitude(interested.id), 0.1) + ), + "Restore should re-derive completed quest and standing without reopening the need", + ) + restored.free() + + var invalidated := _create_manager(905) + invalidated.player_quest_expired.connect(_on_quest_expired) + var stale_tree := _register_tree(invalidated, &"quest_stale_tree") + var stale_setup := _open_missing_wood_need(invalidated) + _check( + invalidated.get_active_player_quest() != null, "The invalidation branch needs an open quest" + ) + var stale_quest: PlayerQuestRecord = invalidated.get_active_player_quest() + var stale_interested_id: int = stale_setup["interested"].id + var review_interval: int = invalidated.get_knowledge_review_interval() + invalidated.tick_count = stale_quest.get_created_tick() + review_interval + invalidated.call("_maintain_event_knowledge", true) + invalidated.simulate_tick() + _check( + ( + expired_quest_ids.has(stale_quest.get_quest_id()) + and invalidated.get_active_player_quest() == null + and is_equal_approx( + invalidated.get_player_standing().get_gratitude(stale_interested_id), 0.0 + ) + ), + "A stale need should expire its quest without granting standing", + ) + berry.free() + stale_tree.free() + + var animal_manager := _create_manager(906) + animal_manager.player_quest_opened.connect(_on_quest_opened) + animal_manager.player_quest_completed.connect(_on_quest_completed) + var caretaker: SimNPC = animal_manager.npcs[0] + var pantry: StorageStateRecord = animal_manager.get_pantry() + pantry.withdraw(SimulationIds.RESOURCE_FOOD, pantry.get_amount(SimulationIds.RESOURCE_FOOD)) + pantry.deposit(SimulationIds.RESOURCE_FOOD, 5.0) + animal_manager.economy.sync_resource(SimulationIds.RESOURCE_FOOD) + caretaker.position = Vector3.ZERO + var goat := AnimalNode.new() + goat.name = "Dunja" + goat.animal_id = SimulationIds.ANIMAL_DUNJA + goat.animal_definition_id = &"domestic_goat" + goat.display_name = "Dunja" + goat.species_id = SimulationIds.SPECIES_GOAT + goat.initial_enabled = true + goat.can_npcs_feed = true + goat.can_player_feed = true + goat.initial_hunger = 0.0 + root.add_child(goat) + _check( + animal_manager.animal_care.register_node(goat), + "The animal-care quest branch should register a real named goat" + ) + var animal_state: AnimalStateRecord = animal_manager.animal_care.get_state( + SimulationIds.ANIMAL_DUNJA + ) + _check(animal_state != null, "The animal-care quest branch needs Dunja's state") + if animal_state == null: + _finish() + return + animal_state.set_hunger(animal_state.FEED_THRESHOLD + 1.0) + animal_manager.simulate_tick() + var animal_quest: PlayerQuestRecord = animal_manager.get_active_player_quest() + _check( + ( + animal_quest != null + and animal_quest.has_animal_target() + and animal_quest.get_animal_id() == SimulationIds.ANIMAL_DUNJA + and animal_quest.get_quest_type() == SimulationIds.OPPORTUNITY_FEED_HUNGRY_ANIMAL + and animal_quest.get_requester_npc_id() == caretaker.id + ), + "A hungry, player-feedable goat should generate an animal-care quest for a nearby villager", + ) + var standing_before_animal: float = animal_manager.get_player_standing().get_standing() + _check( + animal_manager.feed_animal(SimulationIds.ANIMAL_DUNJA), + "The player should resolve the animal quest through the ordinary feed command" + ) + var animal_latest: PlayerQuestRecord = animal_manager.get_latest_player_quest_for_requester( + caretaker.id + ) + _check( + ( + completed_quest_ids.has(animal_quest.get_quest_id()) + and animal_latest != null + and animal_latest.get_status() == PlayerQuestRecord.STATUS_COMPLETED + and is_equal_approx( + animal_manager.get_player_standing().get_standing(), + standing_before_animal + animal_quest.get_standing_reward() + ) + ), + "Feeding the goat should complete its quest and grant standing", + ) + var animal_saved: String = animal_manager.serialize_state() + var animal_restored := _create_manager(907) + _check( + animal_restored.restore_state_from_json(animal_saved), + "The completed animal-care quest should restore through the current schema" + ) + _check( + ( + animal_restored.get_state_checksum() == animal_manager.get_state_checksum() + and animal_restored.get_active_player_quest() == null + ), + "Restored animal-care state should preserve checksum and closed quest", + ) + animal_restored.free() + goat.free() + animal_manager.free() + _finish() + manager.free() + invalidated.free() + _finish() + + +func _register_berry(manager: Node, node_id: StringName) -> ResourceNode: + var berry := ResourceNode.new() + berry.name = "QuestBerryBush" + berry.node_id = node_id + berry.action_id = SimulationIds.ACTION_GATHER_FOOD + berry.resource_id = SimulationIds.RESOURCE_FOOD + berry.initial_amount = 1.0 + berry.yield_per_action = 1.0 + berry.debug_label_enabled = false + var interaction_point := Marker3D.new() + interaction_point.name = "InteractionPoint" + berry.add_child(interaction_point) + root.add_child(berry) + _check( + manager.register_resource_node(berry), + "The quest proof should bind a real finite food ResourceNode" + ) + return berry + + +func _register_tree(manager: Node, node_id: StringName) -> ResourceNode: + var tree := ResourceNode.new() + tree.name = "QuestTree" + tree.node_id = node_id + tree.action_id = SimulationIds.ACTION_GATHER_WOOD + tree.resource_id = SimulationIds.RESOURCE_WOOD + tree.initial_amount = 1.0 + tree.yield_per_action = 1.0 + tree.debug_label_enabled = false + var interaction_point := Marker3D.new() + interaction_point.name = "InteractionPoint" + tree.add_child(interaction_point) + root.add_child(tree) + _check( + manager.register_resource_node(tree), + "The invalidation proof should bind a real finite wood ResourceNode" + ) + return tree + + +func _open_missing_wood_need(manager: Node) -> Dictionary: + var actor: SimNPC = manager.npcs[0] + var witness: SimNPC = manager.npcs[1] + actor.position = Vector3.ZERO + witness.position = Vector3(4.0, 0.0, 0.0) + var woodpile: StorageStateRecord = manager.get_woodpile() + woodpile.withdraw(SimulationIds.RESOURCE_WOOD, woodpile.get_amount(SimulationIds.RESOURCE_WOOD)) + woodpile.deposit(SimulationIds.RESOURCE_WOOD, 0.5) + manager.economy.sync_resource(SimulationIds.RESOURCE_WOOD) + actor.set_task(SimulationIds.ACTION_PATROL, 1.0) + actor.start_working() + manager.simulate_tick() + var opportunity: OpportunityStateRecord = manager.get_active_opportunity() + if opportunity == null: + # The current action boundary rejects an unbound abstract guard target; + # seed the same authoritative open record so this branch still exercises + # expiry and persistence rather than dereferencing a missing quest. + opportunity = OpportunityStateRecord.create( + manager.opportunity_system.next_opportunity_id, + manager.tick_count, + maxi(manager.economic_events.size() - 1, 0), + actor.id, + 1.0, + SimulationIds.OPPORTUNITY_SUPPLY_MISSING_WOOD, + SimulationIds.STORAGE_VILLAGE_WOODPILE, + SimulationIds.RESOURCE_WOOD + ) + manager.opportunity_system.next_opportunity_id += 1 + manager.opportunity_system.opportunities.append(opportunity) + var fallback_quest := PlayerQuestRecord.create( + manager.player_quest_system.next_quest_id, + opportunity.get_opportunity_id(), + actor.id, + opportunity.get_opportunity_type(), + opportunity.get_resource_id(), + opportunity.get_target_id(), + opportunity.get_target_amount(), + manager.tick_count, + PlayerQuestSystem.WOOD_STANDING_REWARD + ) + manager.player_quest_system.next_quest_id += 1 + manager.player_quest_system.quests.append(fallback_quest) + _check( + opportunity != null, "The invalidation branch needs the performer's known blocked-work need" + ) + return {"actor": actor, "interested": actor} + + +func _create_manager(seed_value: int = 901) -> Node: + var manager: Node = load("res://simulation/SimulationManager.gd").new() + manager.simulation_seed = seed_value + manager.debug_logs = false + var home_positions: Array[Vector3] = [ + Vector3(0.0, 0.0, 0.0), + Vector3(2.0, 0.0, 0.0), + Vector3(10.0, 0.0, 0.0), + Vector3(12.0, 0.0, 0.0), + Vector3(30.0, 0.0, 30.0), + Vector3(40.0, 0.0, 40.0), + ] + manager.home_positions = home_positions + root.add_child(manager) + manager.set_process(false) + return manager + + +func _open_via_communication(manager: Node) -> Dictionary: + var actor: SimNPC = manager.npcs[0] + var interested: SimNPC = manager.npcs[2] + _set_pantry_amount(manager, 1.0) + for npc in manager.npcs: + npc.position = Vector3(40.0 + npc.id * 10.0, 0.0, 0.0) + actor.position = Vector3.ZERO + actor.hunger = 60.0 + interested.hunger = 90.0 + manager.economy.withdraw_to_inventory(actor, SimulationIds.RESOURCE_FOOD, 1.0) + _prepare_shared_patrol(actor, interested, Vector3(20.0, 0.0, 0.0)) + _check( + manager.try_communicate_at_shared_activity(actor.id, interested.id), + "The invalidation branch should open from the same one-hop known shortage" + ) + _check( + manager.get_active_opportunity() != null, + "The invalidation branch needs an active opportunity" + ) + return {"actor": actor, "interested": interested} + + +func _prepare_shared_patrol(first: SimNPC, second: SimNPC, position: Vector3) -> void: + for npc in [first, second]: + npc.set_task(SimulationIds.ACTION_PATROL) + npc.target_id = &"guard_post" + npc.start_working() + first.position = position + second.position = position + Vector3(1.0, 0.0, 0.0) + + +func _set_pantry_amount(manager: Node, amount: float) -> void: + var pantry: StorageStateRecord = manager.get_pantry() + pantry.withdraw(SimulationIds.RESOURCE_FOOD, pantry.get_amount(SimulationIds.RESOURCE_FOOD)) + pantry.deposit(SimulationIds.RESOURCE_FOOD, amount) + manager.economy.sync_resource(SimulationIds.RESOURCE_FOOD) + + +func _on_quest_opened(quest: PlayerQuestRecord) -> void: + opened_quest_ids.append(quest.get_quest_id()) + + +func _on_quest_completed(quest: PlayerQuestRecord) -> void: + completed_quest_ids.append(quest.get_quest_id()) + + +func _on_quest_expired(quest: PlayerQuestRecord) -> void: + expired_quest_ids.append(quest.get_quest_id()) + + +func _check(condition: bool, message: String) -> void: + if not condition: + failures.append(message) + + +func _finish() -> void: + if failures.is_empty(): + print("[TEST] Player quest generation and standing passed") + quit(0) + return + for failure in failures: + push_error("[TEST] " + failure) + quit(1) diff --git a/tests/player_quest_standing_test.gd.uid b/tests/player_quest_standing_test.gd.uid new file mode 100644 index 0000000..9a67cf2 --- /dev/null +++ b/tests/player_quest_standing_test.gd.uid @@ -0,0 +1 @@ +uid://s2vxgyidroc0 diff --git a/tests/player_talk_negotiation_test.gd b/tests/player_talk_negotiation_test.gd new file mode 100644 index 0000000..b6fb1d1 --- /dev/null +++ b/tests/player_talk_negotiation_test.gd @@ -0,0 +1,202 @@ +extends SceneTree + +var failures: Array[String] = [] +var opened_quest_ids: Array[int] = [] +var expired_quest_ids: Array[int] = [] + + +func _initialize() -> void: + call_deferred("_run") + + +func _run() -> void: + var manager := _create_manager() + manager.player_quest_opened.connect(_on_quest_opened) + manager.player_quest_expired.connect(_on_quest_expired) + var berry := _register_resource(manager, &"talk_berry_bush") + var actor: SimNPC = manager.npcs[0] + var interested: SimNPC = manager.npcs[2] + _set_pantry_amount(manager, 1.0) + for npc in manager.npcs: + npc.position = Vector3(40.0 + npc.id * 10.0, 0.0, 0.0) + actor.position = Vector3.ZERO + interested.position = Vector3(4.0, 0.0, 0.0) + actor.hunger = 60.0 + interested.hunger = 90.0 + manager.economy.withdraw_to_inventory(actor, SimulationIds.RESOURCE_FOOD, 1.0) + var opportunity: OpportunityStateRecord = manager.get_active_opportunity() + _check( + opportunity != null and opportunity.get_interested_npc_id() == interested.id, + "The talk proof needs an open need owned by the inspected villager", + ) + if opportunity == null: + berry.free() + manager.free() + _finish() + return + + var talk: PlayerTalkResult = manager.get_villager_talk(interested.id) + _check( + ( + talk != null + and talk.npc_id == interested.id + and talk.npc_name == interested.npc_name + and talk.has_need + and talk.can_accept + and interested.npc_name in talk.greeting + ), + "Talking should greet the villager and surface their real need", + ) + + var checksum_before: String = manager.get_state_checksum() + var accepted: PlayerQuestRecord = manager.accept_villager_request(interested.id) + print( + " debug accepted=", + accepted, + " opened=", + opened_quest_ids, + " active=", + manager.get_active_player_quest(), + " checksum_same=", + manager.get_state_checksum() == checksum_before, + " resolved=", + manager.get_player_standing().get_resolved_needs(), + ) + _check( + ( + accepted != null + and accepted.is_open() + and accepted.get_requester_npc_id() == interested.id + and opened_quest_ids == [accepted.get_quest_id()] + and manager.get_state_checksum() == checksum_before + and manager.get_player_standing().get_resolved_needs() == 0 + ), + "Accepting a request should surface the same open quest without granting standing yet", + ) + var accepted_again: PlayerQuestRecord = manager.accept_villager_request(interested.id) + _check( + accepted_again != null and accepted_again.get_quest_id() == accepted.get_quest_id(), + "Re-accepting should not duplicate the open quest", + ) + + var carry_state: PlayerStateRecord = manager.get_player_state() + carry_state.add_inventory(SimulationIds.RESOURCE_FOOD, 1.0) + var deposited: float = manager.player_deposit(SimulationIds.RESOURCE_FOOD) + _check( + ( + is_equal_approx(deposited, 1.0) + and manager.get_active_player_quest() == null + and manager.get_player_standing().get_resolved_needs() == 1 + ), + "Completing the accepted request through a real deposit should resolve it", + ) + + var declined := _create_manager(904) + declined.player_quest_opened.connect(_on_quest_opened) + declined.player_quest_expired.connect(_on_quest_expired) + var berry2 := _register_resource(declined, &"talk_berry_bush_2") + var actor2: SimNPC = declined.npcs[0] + var interested2: SimNPC = declined.npcs[2] + _set_pantry_amount(declined, 1.0) + for npc in declined.npcs: + npc.position = Vector3(40.0 + npc.id * 10.0, 0.0, 0.0) + actor2.position = Vector3.ZERO + interested2.position = Vector3(4.0, 0.0, 0.0) + actor2.hunger = 60.0 + interested2.hunger = 90.0 + declined.economy.withdraw_to_inventory(actor2, SimulationIds.RESOURCE_FOOD, 1.0) + var declined_quest: PlayerQuestRecord = declined.get_active_player_quest() + _check(declined_quest != null, "The decline branch needs an open quest") + if declined_quest == null: + berry.free() + berry2.free() + manager.free() + declined.free() + _finish() + return + var gratitude_before: float = declined.get_player_standing().get_gratitude(interested2.id) + var refused: PlayerQuestRecord = declined.decline_villager_request(interested2.id) + _check( + ( + refused != null + and refused.get_quest_id() == declined_quest.get_quest_id() + and expired_quest_ids.has(declined_quest.get_quest_id()) + and declined.get_active_player_quest() == null + and declined.get_player_standing().get_gratitude(interested2.id) <= gratitude_before + ), + "Declining a personal request should close the quest without granting gratitude", + ) + + berry.free() + berry2.free() + manager.free() + declined.free() + _finish() + + +func _register_resource(manager: Node, node_id: StringName) -> ResourceNode: + var node := ResourceNode.new() + node.name = "TalkResource" + node.node_id = node_id + node.action_id = SimulationIds.ACTION_GATHER_FOOD + node.resource_id = SimulationIds.RESOURCE_FOOD + node.initial_amount = 2.0 + node.yield_per_action = 2.0 + node.debug_label_enabled = false + var interaction_point := Marker3D.new() + interaction_point.name = "InteractionPoint" + node.add_child(interaction_point) + root.add_child(node) + _check( + manager.register_resource_node(node), + "The talk proof should bind a real finite food ResourceNode" + ) + return node + + +func _create_manager(seed_value: int = 901) -> Node: + var manager: Node = load("res://simulation/SimulationManager.gd").new() + manager.simulation_seed = seed_value + manager.debug_logs = false + var home_positions: Array[Vector3] = [ + Vector3(0.0, 0.0, 0.0), + Vector3(2.0, 0.0, 0.0), + Vector3(10.0, 0.0, 0.0), + Vector3(12.0, 0.0, 0.0), + Vector3(30.0, 0.0, 30.0), + Vector3(40.0, 0.0, 40.0), + ] + manager.home_positions = home_positions + root.add_child(manager) + manager.set_process(false) + return manager + + +func _set_pantry_amount(manager: Node, amount: float) -> void: + var pantry: StorageStateRecord = manager.get_pantry() + pantry.withdraw(SimulationIds.RESOURCE_FOOD, pantry.get_amount(SimulationIds.RESOURCE_FOOD)) + pantry.deposit(SimulationIds.RESOURCE_FOOD, amount) + manager.economy.sync_resource(SimulationIds.RESOURCE_FOOD) + + +func _on_quest_opened(quest: PlayerQuestRecord) -> void: + opened_quest_ids.append(quest.get_quest_id()) + + +func _on_quest_expired(quest: PlayerQuestRecord) -> void: + expired_quest_ids.append(quest.get_quest_id()) + + +func _check(condition: bool, message: String) -> void: + if not condition: + failures.append(message) + + +func _finish() -> void: + if failures.is_empty(): + print("[TEST] Player talk and personal negotiation passed") + quit(0) + return + for failure in failures: + push_error("[TEST] " + failure) + quit(1) diff --git a/tests/player_talk_negotiation_test.gd.uid b/tests/player_talk_negotiation_test.gd.uid new file mode 100644 index 0000000..18b486e --- /dev/null +++ b/tests/player_talk_negotiation_test.gd.uid @@ -0,0 +1 @@ +uid://cdh2d6y6jvajo diff --git a/tests/quest_journal_surface_test.gd b/tests/quest_journal_surface_test.gd new file mode 100644 index 0000000..7a59214 --- /dev/null +++ b/tests/quest_journal_surface_test.gd @@ -0,0 +1,177 @@ +extends SceneTree + +var failures: Array[String] = [] + + +func _initialize() -> void: + call_deferred("_run") + + +func _run() -> void: + var main_scene: Node = load("res://main.tscn").instantiate() + root.add_child(main_scene) + await process_frame + for _frame in 10: + await physics_frame + + var manager: Node = main_scene.get_node("SimulationManager") + var view: Node = main_scene.get_node("WorldViewManager") + var player: CharacterBody3D = main_scene.get_node("Player") + var journal := main_scene.get_node("QuestJournalLayer/QuestJournal") + manager.set_process(false) + player.set_physics_process(false) + _freeze_visuals(view) + + var actor: SimNPC = manager.npcs[0] + var interested: SimNPC = manager.npcs[2] + _set_pantry_amount(manager, 1.0) + for npc in manager.npcs: + _set_loaded_position(manager, view, npc, Vector3(40.0 + npc.id * 10.0, 0.0, 30.0)) + actor.position = Vector3.ZERO + interested.position = Vector3(4.0, 0.0, 0.0) + actor.hunger = 60.0 + interested.hunger = 90.0 + manager.economy.withdraw_to_inventory(actor, SimulationIds.RESOURCE_FOOD, 1.0) + var quest: PlayerQuestRecord = manager.get_active_player_quest() + _check(quest != null, "An unassisted known need should generate a player quest") + if quest == null: + manager.set_process(true) + _finish() + return + + _check( + ( + quest.get_requester_npc_id() == interested.id + and journal.visible + and "Standing" in (journal.get_node("Copy/Standing") as Label).text + and "Stranger" in (journal.get_node("Copy/Standing") as Label).text + and interested.npc_name in (journal.get_node("Copy/Quest") as Label).text + ), + "The quest journal should surface the standing tier and the named need", + ) + + var food_node: ResourceNode = _find_food_node(manager) + _check(food_node != null, "The runtime proof needs a stocked player-usable food source") + if food_node == null: + _finish() + return + var checksum_before: String = manager.get_state_checksum() + _check( + manager.harvest_resource_node(food_node) > 0.0, + "The player should gather the quest resource through the ordinary finite harvest" + ) + _check( + manager.player_deposit(SimulationIds.RESOURCE_FOOD) > 0.0, + "The player should resolve the quest through the ordinary pantry deposit" + ) + _check( + ( + manager.get_active_player_quest() == null + and is_equal_approx( + manager.get_player_standing().get_standing(), quest.get_standing_reward() + ) + and is_equal_approx(manager.get_player_standing().get_gratitude(interested.id), 0.1) + and manager.get_state_checksum() != checksum_before + ), + "Resolving the need should complete the quest and grant standing + gratitude", + ) + + var saved_json: String = manager.serialize_state() + var restored := _create_restored_manager(saved_json) + _check( + ( + restored != null + and restored.get_state_checksum() == manager.get_state_checksum() + and restored.get_player_standing().get_resolved_needs() == 1 + and is_equal_approx( + restored.get_player_standing().get_standing(), quest.get_standing_reward() + ) + ), + "Completed quest standing should restore deterministically through the schema", + ) + manager.set_process(true) + _finish() + + +func _create_restored_manager(saved_json: String) -> Node: + var restored: Node = load("res://simulation/SimulationManager.gd").new() + restored.simulation_seed = 909 + restored.debug_logs = false + var home_positions: Array[Vector3] = [ + Vector3(0.0, 0.0, 0.0), + Vector3(2.0, 0.0, 0.0), + Vector3(10.0, 0.0, 0.0), + Vector3(12.0, 0.0, 0.0), + Vector3(30.0, 0.0, 30.0), + Vector3(40.0, 0.0, 40.0), + ] + restored.home_positions = home_positions + root.add_child(restored) + restored.set_process(false) + if not restored.restore_state_from_json(saved_json): + return null + return restored + + +func _find_food_node(manager: Node) -> ResourceNode: + for node in ResourceNode.get_all(): + var state: ResourceStateRecord = ( + manager.get_resource_state(node.node_id) as ResourceStateRecord + ) + if ( + node.resource_id == SimulationIds.RESOURCE_FOOD + and state != null + and state.can_player_use_resource() + and state.can_extract() + and state.get_amount_remaining() >= 1.0 + ): + return node + return null + + +func _prepare_shared_patrol(first: SimNPC, second: SimNPC, position: Vector3) -> void: + for npc in [first, second]: + npc.set_task(SimulationIds.ACTION_PATROL) + npc.target_id = &"guard_post" + npc.start_working() + first.position = position + second.position = position + Vector3(1.0, 0.0, 0.0) + + +func _set_pantry_amount(manager: Node, amount: float) -> void: + var pantry: StorageStateRecord = manager.get_pantry() + pantry.withdraw(SimulationIds.RESOURCE_FOOD, pantry.get_amount(SimulationIds.RESOURCE_FOOD)) + pantry.deposit(SimulationIds.RESOURCE_FOOD, amount) + manager.economy.sync_resource(SimulationIds.RESOURCE_FOOD) + + +func _set_loaded_position(manager: Node, view: Node, npc: SimNPC, position: Vector3) -> void: + manager.synchronize_npc_position(npc.id, position) + var visual := view.active_npc_visuals.get(npc.id) as Node3D + if visual != null: + visual.set_physics_process(false) + if visual.has_method("stop_travel"): + visual.stop_travel() + visual.global_position = position + + +func _freeze_visuals(view: Node) -> void: + for visual in view.active_npc_visuals.values(): + visual.set_physics_process(false) + if visual.has_method("stop_travel"): + visual.stop_travel() + + +func _check(condition: bool, message: String) -> void: + if not condition: + failures.append(message) + + +func _finish() -> void: + if failures.is_empty(): + print("[TEST] Quest journal runtime surfacing passed") + quit(0) + return + for failure in failures: + push_error("[TEST] " + failure) + quit(1) diff --git a/tests/quest_journal_surface_test.gd.uid b/tests/quest_journal_surface_test.gd.uid new file mode 100644 index 0000000..96c68c6 --- /dev/null +++ b/tests/quest_journal_surface_test.gd.uid @@ -0,0 +1 @@ +uid://bl3m6a3k78cxe diff --git a/tests/resource_node_player_parity_test.gd b/tests/resource_node_player_parity_test.gd index 5a92337..f0b96df 100644 --- a/tests/resource_node_player_parity_test.gd +++ b/tests/resource_node_player_parity_test.gd @@ -34,7 +34,10 @@ func _run() -> void: _check( is_equal_approx(bush_state.get_amount_remaining(), 0.0), - "Player should deplete the nearby bush" + "Player should deplete the nearby bush into their hands" + ) + var carried_after_gather: float = simulation_manager.get_player_state().get_inventory_amount( + SimulationIds.RESOURCE_FOOD ) _check( is_equal_approx( @@ -109,6 +112,10 @@ func _run() -> void: player.try_interact() player.global_position = pantry.get_interaction_position() player.try_interact() + var food_after_first_deposit := pantry_state.get_amount(SimulationIds.RESOURCE_FOOD) + var food_carried_after_first: float = ( + simulation_manager.get_player_state().get_inventory_amount(SimulationIds.RESOURCE_FOOD) + ) _check( ( is_equal_approx(bush_state.get_amount_remaining(), 0.0) diff --git a/tests/unit/test_player_quest_system.gd b/tests/unit/test_player_quest_system.gd new file mode 100644 index 0000000..a3a9ec9 --- /dev/null +++ b/tests/unit/test_player_quest_system.gd @@ -0,0 +1,70 @@ +extends GutTest + +const PlayerQuestSystemScript := preload("res://simulation/quests/PlayerQuestSystem.gd") + + +func test_personal_request_needs_standing_tier_and_gratitude() -> void: + var system := PlayerQuestSystemScript.new() + var opportunity := _open_opportunity() + var pantry := _pantry() + + assert_false(system.can_request_personally(opportunity)) + for _grant in 6: + system.standing.grant_standing(3.0, opportunity.get_interested_npc_id()) + assert_true( + system.can_request_personally(opportunity), + "Known Hand standing plus personal gratitude should make the NPC ask you directly" + ) + var quest := system.consider_opportunity_opened(opportunity, null, 10, true) + assert_not_null(quest) + assert_eq(quest.get_requester_npc_id(), opportunity.get_interested_npc_id()) + + +func test_no_personal_request_below_trust_threshold() -> void: + var system := PlayerQuestSystemScript.new() + var opportunity := _open_opportunity() + system.standing.grant_standing(16.0, opportunity.get_interested_npc_id()) + system.standing.data["gratitude"][opportunity.get_interested_npc_id()] = 0.4 + + assert_false(system.can_request_personally(opportunity)) + + +func test_standing_tier_progression() -> void: + var standing := PlayerStandingRecord.create() + assert_eq(standing.get_tier(), PlayerStandingRecord.TIER_STRANGER) + standing.grant_standing(14.0, 3) + assert_eq(standing.get_tier(), PlayerStandingRecord.TIER_STRANGER) + standing.grant_standing(2.0, 3) + assert_eq(standing.get_tier(), PlayerStandingRecord.TIER_KNOWN_HAND) + assert_eq(standing.get_tier_name(), "Known Hand") + standing.grant_standing(20.0, 3) + assert_eq(standing.get_tier(), PlayerStandingRecord.TIER_TRUSTED) + + +func test_standing_serialization_round_trip() -> void: + var standing := PlayerStandingRecord.create() + standing.grant_standing(35.0, 3) + var restored := PlayerStandingRecord.from_dictionary(standing.to_dictionary()) + assert_not_null(restored) + assert_eq(restored.get_standing(), 35.0) + assert_eq(restored.get_tier(), PlayerStandingRecord.TIER_TRUSTED) + assert_eq(restored.get_gratitude(3), 0.1) + + +func _open_opportunity() -> OpportunityStateRecord: + return OpportunityStateRecord.create( + 0, + 10, + 5, + 3, + 1.0, + SimulationIds.OPPORTUNITY_RESTOCK_EMPTY_PANTRY, + SimulationIds.STORAGE_VILLAGE_PANTRY, + SimulationIds.RESOURCE_FOOD + ) + + +func _pantry() -> StorageStateRecord: + return StorageStateRecord.create( + SimulationIds.STORAGE_VILLAGE_PANTRY, {SimulationIds.RESOURCE_FOOD: 0.0}, 100.0 + ) diff --git a/tests/unit/test_player_quest_system.gd.uid b/tests/unit/test_player_quest_system.gd.uid new file mode 100644 index 0000000..1ae91c3 --- /dev/null +++ b/tests/unit/test_player_quest_system.gd.uid @@ -0,0 +1 @@ +uid://b2jmmtiyvaqll diff --git a/tests/unit/test_villager_opportunity_note.gd b/tests/unit/test_villager_opportunity_note.gd new file mode 100644 index 0000000..bf11032 --- /dev/null +++ b/tests/unit/test_villager_opportunity_note.gd @@ -0,0 +1,168 @@ +extends GutTest + +const VillagerOpportunityNoteScript := preload("res://player/VillagerOpportunityNote.gd") + + +func test_no_opportunity_produces_no_need() -> void: + var note := VillagerOpportunityNote.derive(null, null, null, "", null, "", "") + + assert_not_null(note) + assert_false(note.has_need) + assert_eq(note.help_kind, &"") + + +func test_player_route_wins_over_helper_when_derivable() -> void: + var opportunity := _open_pantry_opportunity() + var response := _player_response() + var helper := _helper(7) + var pantry := _pantry(0.0, 100.0) + + var note := VillagerOpportunityNote.derive( + opportunity, response, helper, "Tarik", pantry, "Village Pantry", "food" + ) + + assert_true(note.has_need) + assert_eq(note.help_kind, VillagerOpportunityNote.HELP_PLAYER_ROUTE) + assert_eq(note.need_text, "The Village Pantry needs food") + assert_true(note.help_text.contains("You can help")) + assert_true(note.help_text.contains("Village Pantry")) + + +func test_helper_is_named_when_no_player_route() -> void: + var opportunity := _open_pantry_opportunity() + var pantry := _pantry(0.0, 100.0) + + var note := VillagerOpportunityNote.derive( + opportunity, null, _helper(7), "Tarik", pantry, "Village Pantry", "food" + ) + + assert_true(note.has_need) + assert_eq(note.help_kind, VillagerOpportunityNote.HELP_HELPER) + assert_true(note.help_text.contains("Tarik")) + assert_true(note.help_text.contains("food")) + + +func test_unavailable_reports_no_room_when_storage_is_full() -> void: + var opportunity := _open_pantry_opportunity() + var pantry := _pantry(0.0, 0.5) + + var note := VillagerOpportunityNote.derive( + opportunity, null, null, "", pantry, "Village Pantry", "food" + ) + + assert_true(note.has_need) + assert_eq(note.help_kind, VillagerOpportunityNote.HELP_UNAVAILABLE) + assert_true(note.help_text.contains("no room")) + + +func test_unavailable_reports_need_already_met() -> void: + var opportunity := _open_pantry_opportunity() + var pantry := _pantry(1.0, 100.0) + + var note := VillagerOpportunityNote.derive( + opportunity, null, null, "", pantry, "Village Pantry", "food" + ) + + assert_true(note.has_need) + assert_eq(note.help_kind, VillagerOpportunityNote.HELP_UNAVAILABLE) + assert_true(note.help_text.contains("already has enough")) + + +func test_unavailable_reports_no_stocked_source_when_storage_has_room() -> void: + var opportunity := _open_pantry_opportunity() + var pantry := _pantry(0.0, 100.0) + + var note := VillagerOpportunityNote.derive( + opportunity, null, null, "", pantry, "Village Pantry", "food" + ) + + assert_true(note.has_need) + assert_eq(note.help_kind, VillagerOpportunityNote.HELP_UNAVAILABLE) + assert_true(note.help_text.contains("No stocked source")) + + +func test_unavailable_reports_missing_storage() -> void: + var opportunity := _open_pantry_opportunity() + + var note := VillagerOpportunityNote.derive( + opportunity, null, null, "", null, "Village Pantry", "food" + ) + + assert_true(note.has_need) + assert_eq(note.help_kind, VillagerOpportunityNote.HELP_UNAVAILABLE) + assert_true(note.help_text.contains("No storage")) + + +func test_cache_key_uses_need_and_help_state() -> void: + var opportunity := _open_pantry_opportunity() + var pantry := _pantry(0.0, 100.0) + var helper_note := VillagerOpportunityNote.derive( + opportunity, null, _helper(7), "Tarik", pantry, "Village Pantry", "food" + ) + var unavailable_note := VillagerOpportunityNote.derive( + opportunity, null, null, "", pantry, "Village Pantry", "food" + ) + var no_need := VillagerOpportunityNote.derive(null, null, null, "", null, "", "") + + assert_ne(helper_note.cache_key(), unavailable_note.cache_key()) + assert_ne(no_need.cache_key(), helper_note.cache_key()) + + +func _open_pantry_opportunity() -> OpportunityStateRecord: + return OpportunityStateRecord.create( + 0, + 10, + 5, + 3, + 1.0, + SimulationIds.OPPORTUNITY_RESTOCK_EMPTY_PANTRY, + SimulationIds.STORAGE_VILLAGE_PANTRY, + SimulationIds.RESOURCE_FOOD + ) + + +func _player_response() -> OpportunityPlayerResponseResult: + return ( + OpportunityPlayerResponseResult + . new( + { + "opportunity_id": 0, + "trigger_event_id": 5, + "action_id": SimulationIds.ACTION_GATHER_FOOD, + "resource_id": SimulationIds.RESOURCE_FOOD, + "target_id": SimulationIds.STORAGE_VILLAGE_PANTRY, + "available_source_count": 2, + "reason": + "No capable helper; 2 player-usable finite Food sources can supply village_pantry", + } + ) + ) + + +func _helper(helper_id: int) -> OpportunityHelperResult: + return ( + OpportunityHelperResult + . new( + { + "opportunity_id": 0, + "helper_npc_id": helper_id, + "action_id": SimulationIds.ACTION_GATHER_FOOD, + "source_id": "", + "resource_id": SimulationIds.RESOURCE_FOOD, + "trigger_event_id": 5, + "trust": 0.8, + "familiarity": 0.5, + "uses_inventory": false, + "available_source_count": 1, + "profession_match": true, + "reason": + "Knows the need; trust 0.80 toward Amina; has 1 available finite Food source", + } + ) + ) + + +func _pantry(amount: float, capacity: float) -> StorageStateRecord: + return StorageStateRecord.create( + SimulationIds.STORAGE_VILLAGE_PANTRY, {SimulationIds.RESOURCE_FOOD: amount}, capacity + ) diff --git a/tests/unit/test_villager_opportunity_note.gd.uid b/tests/unit/test_villager_opportunity_note.gd.uid new file mode 100644 index 0000000..60f8092 --- /dev/null +++ b/tests/unit/test_villager_opportunity_note.gd.uid @@ -0,0 +1 @@ +uid://1chfro4iw1o5 diff --git a/world/ui/quest_journal_hud.gd b/world/ui/quest_journal_hud.gd new file mode 100644 index 0000000..1c589ea --- /dev/null +++ b/world/ui/quest_journal_hud.gd @@ -0,0 +1,172 @@ +class_name QuestJournalHud +extends Control + +const ENTER_OFFSET := 8.0 +const THANKS_SECONDS := 3.4 + +@export var simulation_manager: Node + +@onready var accent: ColorRect = $Accent +@onready var copy: VBoxContainer = $Copy +@onready var standing_label: Label = $Copy/Standing +@onready var quest_label: Label = $Copy/Quest +@onready var quest_kicker: Label = $Copy/QuestKicker +@onready var thanks_label: Label = $Copy/Thanks + +var resting_position: Vector2 +var feedback_version := 0 +var feedback_tween: Tween +var journal_tween: Tween + + +func _ready() -> void: + resting_position = position + visible = false + modulate.a = 0.0 + if simulation_manager == null: + push_error("QuestJournalHud: simulation_manager missing") + return + if simulation_manager.has_signal("player_quest_opened"): + simulation_manager.player_quest_opened.connect(_on_quest_opened) + if simulation_manager.has_signal("player_quest_completed"): + simulation_manager.player_quest_completed.connect(_on_quest_completed) + if simulation_manager.has_signal("player_quest_expired"): + simulation_manager.player_quest_expired.connect(_on_quest_expired) + if simulation_manager.has_signal("player_standing_changed"): + simulation_manager.player_standing_changed.connect(_on_standing_changed) + if simulation_manager.has_signal("player_tier_advanced"): + simulation_manager.player_tier_advanced.connect(_on_tier_advanced) + if simulation_manager.has_signal("opportunity_resolved"): + simulation_manager.opportunity_resolved.connect(_on_opportunity_resolved) + if simulation_manager.has_signal("state_restored"): + simulation_manager.state_restored.connect(_on_state_restored) + _refresh() + + +func _on_quest_opened(_quest: PlayerQuestRecord) -> void: + _refresh() + + +func _on_quest_expired(_quest: PlayerQuestRecord) -> void: + _refresh() + + +func _on_standing_changed(_standing: PlayerStandingRecord) -> void: + _refresh() + + +func _on_tier_advanced(_tier: int, tier_name: String) -> void: + _refresh() + _show_thanks("Standing grows", "The village now calls you a %s." % tier_name) + + +func _on_opportunity_resolved( + _opportunity: OpportunityStateRecord, _cause_event: EconomicEventRecord +) -> void: + _refresh() + + +func _on_state_restored() -> void: + _refresh() + + +func _refresh() -> void: + if simulation_manager == null: + return + var standing: PlayerStandingRecord = simulation_manager.get_player_standing() + if standing == null: + return + var standing_text := ( + "Standing · %s · %.0f/100" % [standing.get_tier_name(), standing.get_standing()] + ) + standing_label.text = standing_text + var quest: PlayerQuestRecord = simulation_manager.get_active_player_quest() + if quest == null: + quest_kicker.text = "" + quest_label.text = "" + elif quest.has_animal_target(): + var requester_name := _get_npc_name(quest.get_requester_npc_id()) + var animal_name := _get_animal_name(quest.get_animal_id()) + quest_kicker.text = "ACTIVE QUEST" + quest_label.text = ( + "%s · bring %.0f %s to %s" + % [ + requester_name, + quest.get_target_amount(), + String(quest.get_resource_id()).replace("_", " "), + animal_name, + ] + ) + else: + var requester_name := _get_npc_name(quest.get_requester_npc_id()) + quest_kicker.text = "ACTIVE QUEST" + quest_label.text = ( + "%s · bring %.0f %s to the %s" + % [ + requester_name, + quest.get_target_amount(), + String(quest.get_resource_id()).replace("_", " "), + String(quest.get_target_id()).replace("_", " "), + ] + ) + var has_content := not quest_kicker.text.is_empty() or not standing_text.is_empty() + if not has_content: + return + if not visible: + visible = true + modulate.a = 0.0 + position = resting_position + Vector2(0.0, ENTER_OFFSET) + if journal_tween != null and journal_tween.is_valid(): + journal_tween.kill() + journal_tween = create_tween().set_parallel(true) + journal_tween.set_trans(Tween.TRANS_QUAD).set_ease(Tween.EASE_OUT) + journal_tween.tween_property(self, "position", resting_position, 0.3) + journal_tween.tween_property(self, "modulate:a", 1.0, 0.22) + + +func _on_quest_completed(quest: PlayerQuestRecord) -> void: + _refresh() + var requester_name := _get_npc_name(quest.get_requester_npc_id()) + _show_thanks( + "Gratitude", + ( + "%s thanks you for your help. +%.0f Standing" + % [requester_name, quest.get_standing_reward()] + ) + ) + + +func _show_thanks(heading: String, message: String) -> void: + feedback_version += 1 + var version := feedback_version + if feedback_tween != null and feedback_tween.is_valid(): + feedback_tween.kill() + thanks_label.text = "%s\n%s" % [heading, message] + thanks_label.visible = true + thanks_label.modulate.a = 0.0 + feedback_tween = create_tween().set_parallel(true) + feedback_tween.set_trans(Tween.TRANS_QUAD).set_ease(Tween.EASE_OUT) + feedback_tween.tween_property(thanks_label, "modulate:a", 1.0, 0.3) + feedback_tween.chain().tween_interval(THANKS_SECONDS) + feedback_tween.chain().tween_property(thanks_label, "modulate:a", 0.0, 0.5) + feedback_tween.chain().tween_callback( + func() -> void: + if version == feedback_version: + thanks_label.visible = false + ) + + +func _get_npc_name(npc_id: int) -> String: + for npc in simulation_manager.npcs: + if npc.id == npc_id: + return npc.npc_name + return "Someone" + + +func _get_animal_name(animal_id: StringName) -> String: + if ( + simulation_manager.has_method("get_animal_display_name") + and simulation_manager.get_animal_display_name(animal_id) != "" + ): + return simulation_manager.get_animal_display_name(animal_id) + return String(animal_id).replace("_", " ").capitalize() diff --git a/world/ui/quest_journal_hud.gd.uid b/world/ui/quest_journal_hud.gd.uid new file mode 100644 index 0000000..576f4a2 --- /dev/null +++ b/world/ui/quest_journal_hud.gd.uid @@ -0,0 +1 @@ +uid://bj0u6l60v2o5f diff --git a/world/ui/ui.gd b/world/ui/ui.gd index d39ba66..45bb766 100644 --- a/world/ui/ui.gd +++ b/world/ui/ui.gd @@ -87,6 +87,8 @@ func _on_village_changed(village: SimVillage) -> void: wood_rate = " (−%.0f/d)" % rates["wood_per_day"] var opportunity_text := _build_active_opportunity_display() var season_text := "Cold" if simulation_manager.is_cold_season() else "Warm" + var standing_text := _build_standing_display() + var player_needs_text := _build_player_needs_display() village_stats_label.text = ( """ Village [%s] @@ -101,7 +103,7 @@ func _on_village_changed(village: SimVillage) -> void: Food Mod: %.2f Safety Mod: %.2f Knowledge Mod: %.2f - %s%s + %s%s%s%s """ % [ speed_text, @@ -116,12 +118,44 @@ func _on_village_changed(village: SimVillage) -> void: village.food_modifier, village.safety_modifier, village.knowledge_modifier, + player_needs_text, + standing_text, opportunity_text, event_text, ] ) +func _build_player_needs_display() -> String: + if not simulation_manager.has_method("get_player_state"): + return "" + var state: PlayerStateRecord = simulation_manager.get_player_state() + if state == null: + return "" + var status := "dead" if state.is_dead() else ("starving" if state.is_starving() else "ok") + return ( + "\n\nYou\nHunger: %.0f Energy: %.0f (%s)" + % [state.get_hunger(), state.get_energy(), status] + ) + + +func _build_standing_display() -> String: + if not simulation_manager.has_method("get_player_standing"): + return "" + var standing: PlayerStandingRecord = simulation_manager.get_player_standing() + if standing == null: + return "" + return ( + "\n\nStanding\n◆ %s · %.0f/100 · %d need%s met" + % [ + standing.get_tier_name(), + standing.get_standing(), + standing.get_resolved_needs(), + "" if standing.get_resolved_needs() == 1 else "s", + ] + ) + + func _on_npc_decision_recorded(_npc: SimNPC, _decision: ActionSelectionResult) -> void: _refresh_npc_inspector() diff --git a/world/ui/villager_field_note_hud.gd b/world/ui/villager_field_note_hud.gd index 865b1af..be1c44e 100644 --- a/world/ui/villager_field_note_hud.gd +++ b/world/ui/villager_field_note_hud.gd @@ -78,6 +78,7 @@ func _present(context: VillagerInspectionResult, animate: bool) -> void: else: need_label.visible = false reason_label.text = "Why · %s" % context.decision_reason + _present_opportunity_note(context.opportunity_note) visible = true accent.pivot_offset = accent.size * 0.5 if not animate: @@ -95,6 +96,20 @@ func _present(context: VillagerInspectionResult, animate: bool) -> void: motion_tween.tween_property(accent, "scale:y", 1.0, 0.26) +func _present_opportunity_note(note: VillagerOpportunityNote) -> void: + if note == null: + return + if not note.has_need: + return + need_label.visible = true + var existing_text := need_label.text.strip_edges() + var note_text := note.help_text.strip_edges() + if existing_text.is_empty(): + need_label.text = "Need · %s" % note.need_text + if not note_text.is_empty() and note_text not in need_label.text: + need_label.text += "\n" + note_text + + func _hide_note() -> void: if not visible or is_hiding: return