Z hat die Gist bearbeitet . Zu Änderung gehen
Keine Änderungen
Z hat die Gist bearbeitet . Zu Änderung gehen
1 file changed, 127 insertions
Gemma4-Quest-System.md(Datei erstellt)
| @@ -0,0 +1,127 @@ | |||
| 1 | + | To implement an **AI-Driven NPC Quest System**, we need to combine almost every feature of our Universal DSL: **AI generation** for the quest, **Fork-Join** for multiple objectives, **Event Listeners** for player actions, and **Saga/Undo** logic for quest failure. | |
| 2 | + | ||
| 3 | + | In this scenario, the NPC doesn't have a hard-coded quest. Instead, the AI looks at the game world, generates a dynamic quest, and monitors the player's progress in real-time. | |
| 4 | + | ||
| 5 | + | ### The Scenario: "The Dynamic Village Savior" | |
| 6 | + | **Goal:** An NPC (The Elder) identifies a problem in the world, creates a multi-part quest for the player, and updates the world state upon completion. | |
| 7 | + | ||
| 8 | + | ```python | |
| 9 | + | # ============================================================================== | |
| 10 | + | # PROJECT: AI-Driven Dynamic Quest System | |
| 11 | + | # NPC: Village Elder | |
| 12 | + | # ============================================================================== | |
| 13 | + | ||
| 14 | + | # --- GLOBAL STATE --- | |
| 15 | + | $quest_id = null | |
| 16 | + | $quest_status = "idle" | |
| 17 | + | $world_state = { "forest_danger": "high", "village_food": "low" } | |
| 18 | + | $player_progress = {} | |
| 19 | + | ||
| 20 | + | # --- MODULE: QUEST GENERATION (The AI Brain) --- | |
| 21 | + | Module QuestGenerator { | |
| 22 | + | # AI analyzes world state and generates a unique quest objective | |
| 23 | + | AI.Generate [model: "gpt-4-game-engine"] { | |
| 24 | + | context: $world_state, | |
| 25 | + | prompt: "Generate a quest to fix the village's main problem. | |
| 26 | + | Provide two distinct objectives: one 'Fetch' and one 'Slay'." | |
| 27 | + | } -> Quest.Validate | |
| 28 | + | ||
| 29 | + | # Ensure the AI didn't generate something impossible (Constraint Check) | |
| 30 | + | Quest.Validate /check /verify {objectives} ? [ | |
| 31 | + | is_feasible -> Quest.Finalize, | |
| 32 | + | impossible -> AI.Generate # Loop back to regenerate | |
| 33 | + | ] | |
| 34 | + | ||
| 35 | + | Quest.Finalize /save {quest_data} -> $quest_id = {qid} | |
| 36 | + | return {quest_active: true} | |
| 37 | + | } | |
| 38 | + | ||
| 39 | + | # --- MODULE: QUEST EXECUTION (The Parallel Game Loop) --- | |
| 40 | + | Module QuestExecution { | |
| 41 | + | # Fork: The player must complete multiple objectives in any order | |
| 42 | + | Quest.Start F-> [Obj.FetchItem, Obj.SlayMonster, Obj.DialogueNPC] | |
| 43 | + | ||
| 44 | + | # Objective 1: Fetch Item (Event-Driven) | |
| 45 | + | # Wait for the player to pick up the specific AI-generated item | |
| 46 | + | Obj.FetchItem /wait ON (Player.InventoryUpdate) { | |
| 47 | + | filter: { item == $quest_data.fetch_item } | |
| 48 | + | } -> Obj.FetchComplete | |
| 49 | + | ||
| 50 | + | # Objective 2: Slay Monster (Event-Driven) | |
| 51 | + | # Wait for the combat system to report the death of the target | |
| 52 | + | Obj.SlayMonster /wait ON (Combat.EntityDeath) { | |
| 53 | + | filter: { target == $quest_data.target_monster } | |
| 54 | + | } -> Obj.SlayComplete | |
| 55 | + | ||
| 56 | + | # Objective 3: Dialogue (Sequential) | |
| 57 | + | Obj.DialogueNPC /interact {npc: "ForestSpirit"} /save {info} -> Obj.DialogueComplete | |
| 58 | + | ||
| 59 | + | # Join: Synchronize all objectives | |
| 60 | + | [Obj.FetchComplete, Obj.SlayComplete, Obj.DialogueComplete] J-> Quest.VerifyCompletion | |
| 61 | + | } | |
| 62 | + | ||
| 63 | + | # --- MODULE: VERIFICATION & WORLD UPDATE --- | |
| 64 | + | Module QuestResolution { | |
| 65 | + | # AI evaluates the "Quality" of completion (e.g., did the player kill too many innocents?) | |
| 66 | + | AI.Evaluate { | |
| 67 | + | input: $player_progress, | |
| 68 | + | criteria: "Evaluate if the quest was completed honorably" | |
| 69 | + | } -> Eval.Result | |
| 70 | + | ||
| 71 | + | Eval.Result ? [ | |
| 72 | + | honorable -> World.UpdateSuccess, | |
| 73 | + | dishonorable -> World.UpdateMixed | |
| 74 | + | ] | |
| 75 | + | ||
| 76 | + | # Server-side Transaction: Update world state and reward player | |
| 77 | + | World.UpdateSuccess { | |
| 78 | + | Lock ($village_id) { | |
| 79 | + | EXT:Server.UpdateWorldState { "forest_danger": "low" } | |
| 80 | + | EXT:Server.GrantReward { player_id: $player_id, amount: 1000, item: "HeroCape" } | |
| 81 | + | } | |
| 82 | + | } | |
| 83 | + | ||
| 84 | + | # If something fails during the server update, rollback the quest state | |
| 85 | + | World.UpdateSuccess /undo { | |
| 86 | + | $quest_status = "error"; | |
| 87 | + | Notify.Player("World update failed. Please contact support."); | |
| 88 | + | } | |
| 89 | + | } | |
| 90 | + | ||
| 91 | + | # --- MAIN FLOW --- | |
| 92 | + | # 1. Trigger: Player interacts with the Elder | |
| 93 | + | Welcome.Interaction /start /on (Player.Interact, "Elder") -> QuestGenerator | |
| 94 | + | ||
| 95 | + | # 2. Transition to Execution | |
| 96 | + | QuestGenerator.Complete -> QuestExecution | |
| 97 | + | ||
| 98 | + | # 3. Transition to Resolution | |
| 99 | + | QuestExecution.Complete -> QuestResolution | |
| 100 | + | ||
| 101 | + | # 4. The "Failure" Path (Saga/Recovery) | |
| 102 | + | # If the player dies during the quest, the quest doesn't vanish; it pauses. | |
| 103 | + | ON (Player.Death) -> Quest.Pause { | |
| 104 | + | $quest_status = "paused" | |
| 105 | + | Saga.Compensate { | |
| 106 | + | # Move player back to graveyard, but keep quest items in inventory | |
| 107 | + | EXT:Server.RelocatePlayer { location: "Graveyard" } | |
| 108 | + | } | |
| 109 | + | -> WAIT (Player.Respawn) -> QuestExecution | |
| 110 | + | } | |
| 111 | + | ||
| 112 | + | # 5. The "Boredom" Timer (Tick) | |
| 113 | + | # If the player ignores the quest for too long, the NPC gets grumpy | |
| 114 | + | Tick [1hour] { | |
| 115 | + | condition: $quest_status == "active", | |
| 116 | + | logic: NPC.Elder.SetMood("Grumpy") | |
| 117 | + | } | |
| 118 | + | ``` | |
| 119 | + | ||
| 120 | + | ### Why this is powerful (The "Universal" breakdown): | |
| 121 | + | ||
| 122 | + | 1. **Non-Deterministic Logic (AI):** Instead of `if (quest_id == 1)`, the system uses `AI.Generate` and `AI.Evaluate`. The quest changes every time based on `$world_state`. | |
| 123 | + | 2. **Asynchronous Event-Driven Architecture:** The `Obj.FetchItem` and `Obj.SlayMonster` nodes don't "poll" the database. They use `ON (Event)` listeners, meaning the workflow sleeps until the game engine pushes a specific event. | |
| 124 | + | 3. **Strict Synchronization (Fork-Join):** The `J->` (Join) ensures the player cannot turn in the quest by just killing the monster; they *must* also find the item and talk to the spirit. | |
| 125 | + | 4. **Infrastructure Safety:** The use of `Lock ($village_id)` prevents a "race condition" where two players completing the same quest at the same time could double-reward themselves or glitch the world state. | |
| 126 | + | 5. **Resilience (Saga/Recovery):** The `ON (Player.Death)` handler acts as a recovery mechanism, ensuring the state is preserved across a "fail state" (Death) and resuming the workflow exactly where it left off. | |
| 127 | + | 6. **Real-time Ambience (Tick):** The `Tick [1hour]` allows the NPC to react to the passage of time, adding a layer of realism that standard linear workflows cannot handle. | |
Neuer
Älter