Spaces:
Sleeping
Sleeping
Deploy Myco from CI
Browse files- game/agent_controller.py +63 -0
game/agent_controller.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import engine
|
| 2 |
+
import json
|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
class MycoController:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self.position = [1, 1] # 3x3 Grid (0-2)
|
| 8 |
+
self.history = []
|
| 9 |
+
|
| 10 |
+
def get_agent_decision(self, current_mushroom, collection):
|
| 11 |
+
"""
|
| 12 |
+
Queries Gemma for a decision.
|
| 13 |
+
Note: We use engine._llm but provide a specific 'Agent' prompt.
|
| 14 |
+
"""
|
| 15 |
+
prompt = f"""
|
| 16 |
+
Current Position: {self.position}
|
| 17 |
+
Mushroom in clearing: {'Yes' if current_mushroom else 'No'}
|
| 18 |
+
Collection count: {len(collection)}
|
| 19 |
+
|
| 20 |
+
Decide your next action: 'move', 'search', 'study', 'collect', or 'wait'.
|
| 21 |
+
If 'move', provide target coordinate (e.g., [1, 2]).
|
| 22 |
+
|
| 23 |
+
Respond ONLY in JSON format:
|
| 24 |
+
{{"action": "...", "target": [x, y], "thought": "..."}}
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
# We reuse the existing engine._llm functionality
|
| 28 |
+
response = engine._llm(prompt)
|
| 29 |
+
|
| 30 |
+
# Simple parser
|
| 31 |
+
try:
|
| 32 |
+
# Extract JSON from potential markdown text
|
| 33 |
+
start = response.find("{")
|
| 34 |
+
end = response.rfind("}") + 1
|
| 35 |
+
return json.loads(response[start:end])
|
| 36 |
+
except:
|
| 37 |
+
return {"action": "wait", "target": None, "thought": "Thinking..."}
|
| 38 |
+
|
| 39 |
+
def run_tick(self, current_mushroom, collection):
|
| 40 |
+
"""
|
| 41 |
+
This is the heartbeat of the AI.
|
| 42 |
+
It decides and then executes via engine functions.
|
| 43 |
+
"""
|
| 44 |
+
decision = self.get_agent_decision(current_mushroom, collection)
|
| 45 |
+
action = decision.get("action")
|
| 46 |
+
|
| 47 |
+
result = {"action_taken": action, "thought": decision.get("thought")}
|
| 48 |
+
|
| 49 |
+
# EXECUTION LAYER (calling existing engine functions)
|
| 50 |
+
if action == "move":
|
| 51 |
+
self.position = decision.get("target", self.position)
|
| 52 |
+
|
| 53 |
+
elif action == "search":
|
| 54 |
+
# We call the engine function directly
|
| 55 |
+
mushroom, current, history = engine.discover_mushroom(collection)
|
| 56 |
+
result["data"] = {"mushroom": mushroom, "current": current}
|
| 57 |
+
|
| 58 |
+
elif action == "collect":
|
| 59 |
+
if current_mushroom:
|
| 60 |
+
coll, hist = engine.collect_current(current_mushroom, collection, self.history)
|
| 61 |
+
result["data"] = {"collection": coll}
|
| 62 |
+
|
| 63 |
+
return result
|