-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadaptive_world_controller.py
More file actions
84 lines (61 loc) · 1.8 KB
/
adaptive_world_controller.py
File metadata and controls
84 lines (61 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import json
class AdaptiveWorldController:
def __init__(self, llm):
self.llm = llm
self.target_difficulty = 0.5 # sweet spot
# ---------------------------------
# COMPUTE WORLD HEALTH
# ---------------------------------
def analyze(self, metrics, world_state):
avg_reward = metrics.get("avg_reward", 0.5)
player_health = world_state["player"]["health"]
popped = world_state["player"]["popped"]
return {
"difficulty_signal": avg_reward,
"player_stability": player_health / 100,
"progress_rate": popped
}
# ---------------------------------
# BUILD ADAPTATION PROMPT
# ---------------------------------
def build_prompt(self, analysis, world):
return f"""
You are a living game ecosystem controller.
Your job is BALANCE:
- If player is struggling → reduce difficulty
- If player is dominating → increase complexity
TARGET: maintain engagement near {self.target_difficulty}
ANALYSIS:
{json.dumps(analysis)}
WORLD STATE:
{json.dumps(world)}
YOU MAY MODIFY:
- nodes (zones)
- entities (balloons/enemies)
- rules (damage, spawn rates)
- difficulty
OUTPUT ONLY VALID JSON:
{{
"adjustments": {{
"difficulty": 0.0
}},
"nodes": [],
"entities": [],
"rules": {{}}
}}
"""
# ---------------------------------
# RUN ONE ADAPTIVE STEP
# ---------------------------------
def step(self, metrics, world):
analysis = self.analyze(metrics, world)
prompt = self.build_prompt(analysis, world)
raw = self.llm.generate(prompt)
try:
import re
match = re.search(r"\{.*\}", raw, re.DOTALL)
if not match:
return {}
return json.loads(match.group(0))
except:
return {}