-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
348 lines (289 loc) · 10.9 KB
/
Copy pathcontroller.py
File metadata and controls
348 lines (289 loc) · 10.9 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
"""
MC-Planner Controller Module
Migrated to gymnasium environment without Java dependencies
"""
import torch
import numpy as np
from typing import Dict, List, Any, Optional, Tuple
from pathlib import Path
import cv2
from src.gymnasium_env import MineDojoEnv
from src.minedojo_core import mc, SimulationState
class CraftAgent:
"""
Craft agent for gymnasium-based Minecraft environment
Simplified version without Java dependencies
"""
def __init__(self, env: MineDojoEnv):
self.env = env
self.simulation_state = SimulationState()
def no_op(self, times: int = 20) -> List[Dict]:
"""Perform no-operation actions"""
actions = []
for _ in range(times):
action = {
'movement': 0,
'camera': np.array([0.0, 0.0]),
'use': 0,
'attack': 0,
'jump': 0
}
actions.append(action)
return actions
def take_forward(self, times: int = 3) -> List[Dict]:
"""Move forward"""
actions = []
for _ in range(times):
action = {
'movement': 0, # forward
'camera': np.array([0.0, 0.0]),
'use': 0,
'attack': 0,
'jump': 0
}
actions.append(action)
return actions
def index_slot(self, goal: str) -> int:
"""Get inventory slot index for item"""
# Simplified slot indexing
item_slots = {
'wooden_pickaxe': 0,
'stone_pickaxe': 1,
'iron_pickaxe': 2,
'wooden_planks': 3,
'stick': 4,
'cobblestone': 5,
}
return item_slots.get(goal, 0)
def equip(self, goal: str) -> List[Dict]:
"""Equip item"""
slot = self.index_slot(goal)
# In real implementation, this would change selected hotbar slot
return self.no_op(1)
def pillar_jump(self, stepping_stone: str = "cobblestone") -> List[Dict]:
"""Perform pillar jumping"""
actions = []
# Place block, jump, repeat
for _ in range(3):
# Place block
actions.extend(self.place(stepping_stone))
# Jump
actions.extend(self.jump())
return actions
def go_surface(self) -> List[Dict]:
"""Go to surface (simplified)"""
# Look up and move forward
actions = []
for _ in range(10):
action = {
'movement': 0, # forward
'camera': np.array([0.0, 0.3]), # look up
'use': 0,
'attack': 0,
'jump': 1 # jump to go up
}
actions.append(action)
return actions
def acquire_info(self) -> Dict:
"""Get current environment information"""
return {
'inventory': self.simulation_state.get_inventory(),
'position': self.simulation_state.position.copy(),
'health': self.simulation_state.health,
'hunger': self.simulation_state.hunger
}
def use(self) -> List[Dict]:
"""Use/interact action"""
action = {
'movement': 0,
'camera': np.array([0.0, 0.0]),
'use': 1, # use item
'attack': 0,
'jump': 0
}
return [action]
def look_to(self, deg: float = 0) -> List[Dict]:
"""Look in specific direction"""
# Convert degrees to camera movement
camera_x = np.sin(np.radians(deg)) * 0.5
camera_y = np.cos(np.radians(deg)) * 0.5
action = {
'movement': 0,
'camera': np.array([camera_x, camera_y]),
'use': 0,
'attack': 0,
'jump': 0
}
return [action]
def jump(self) -> List[Dict]:
"""Jump action"""
action = {
'movement': 0,
'camera': np.array([0.0, 0.0]),
'use': 0,
'attack': 0,
'jump': 1
}
return [action]
def place(self, goal: str) -> List[Dict]:
"""Place block"""
# Equip item first, then use
actions = self.equip(goal)
actions.extend(self.use())
return actions
def place_down(self, goal: str) -> List[Dict]:
"""Place block downward"""
# Look down and place
actions = self.look_to(-90) # Look down
actions.extend(self.place(goal))
return actions
def attack(self, times: int = 20) -> List[Dict]:
"""Attack/mine action"""
actions = []
for _ in range(times):
action = {
'movement': 0,
'camera': np.array([0.0, 0.0]),
'use': 0,
'attack': 1, # attack/mine
'jump': 0
}
actions.append(action)
return actions
def recycle(self, goal: str, times: int = 20) -> List[Dict]:
"""Recycle/break item"""
return self.attack(times)
def craft_wo_table(self, goal: str) -> List[Dict]:
"""Craft without crafting table (2x2 grid)"""
# Open inventory and craft
actions = self.use() # Open inventory
# Simulate crafting
if goal in mc.RECIPES:
recipe = mc.RECIPES[goal]
if self.simulation_state.can_craft(goal):
self.simulation_state.craft_item(goal)
return actions
def forward(self, times: int = 5) -> List[Dict]:
"""Move forward"""
return self.take_forward(times)
def craft_w_table(self, goal: str) -> List[Dict]:
"""Craft with crafting table (3x3 grid)"""
# Find/place crafting table, then craft
actions = self.place("crafting_table")
actions.extend(self.use()) # Use crafting table
# Simulate crafting
if goal in mc.RECIPES:
recipe = mc.RECIPES[goal]
if self.simulation_state.can_craft(goal):
self.simulation_state.craft_item(goal)
return actions
def smelt_w_furnace(self, goal: str) -> List[Dict]:
"""Smelt with furnace"""
# Place/use furnace
actions = self.place("furnace")
actions.extend(self.use()) # Use furnace
# Simulate smelting
if goal in mc.SMELTING:
recipe = mc.SMELTING[goal]
for ingredient, amount in recipe.items():
if self.simulation_state.inventory.get(ingredient, 0) >= amount:
self.simulation_state.update_inventory(ingredient, -amount)
self.simulation_state.update_inventory(goal, 1)
return actions
def smelt_wo_furnace(self, goal: str) -> List[Dict]:
"""Smelt without furnace (not possible, fallback to with furnace)"""
return self.smelt_w_furnace(goal)
def get_action(self, preconditions: Dict, goal_type: str, goal: str) -> List[Dict]:
"""Get action sequence for goal"""
if goal_type == "mine":
# Mine goal
tool_needed = preconditions.get('tool', 'wooden_pickaxe')
actions = self.equip(tool_needed)
actions.extend(self.attack(20))
# Update simulation state
if "cobblestone" in goal:
self.simulation_state.update_inventory("cobblestone", 3)
elif "iron_ore" in goal:
self.simulation_state.update_inventory("iron_ore", 2)
elif "diamond" in goal:
self.simulation_state.update_inventory("diamond", 1)
elif goal_type == "craft":
# Craft goal
if goal in ["wooden_slab", "wooden_planks", "stick"]:
actions = self.craft_wo_table(goal)
else:
actions = self.craft_w_table(goal)
elif goal_type == "smelt":
actions = self.smelt_w_furnace(goal)
else:
# Default action
actions = self.no_op(5)
return actions
class MineAgent:
"""
Mining agent for specific mining tasks
"""
def __init__(self, env: MineDojoEnv, device: str = "cuda"):
self.env = env
self.device = device
self.craft_agent = CraftAgent(env)
def mine_item(self, item: str, quantity: int = 1) -> bool:
"""Mine specific item"""
# Determine required tool
tool_map = {
'cobblestone': 'wooden_pickaxe',
'iron_ore': 'stone_pickaxe',
'diamond': 'iron_pickaxe',
'coal': 'wooden_pickaxe'
}
required_tool = tool_map.get(item, 'wooden_pickaxe')
# Check if we have the tool
inventory = self.craft_agent.simulation_state.get_inventory()
if inventory.get(required_tool, 0) == 0:
print(f"Missing required tool: {required_tool}")
return False
# Execute mining actions
actions = self.craft_agent.get_action(
preconditions={'tool': required_tool},
goal_type='mine',
goal=item
)
# Execute actions in environment
for action in actions:
obs, reward, terminated, truncated, info = self.env.step(action)
if terminated or truncated:
break
return True
class MineAgentWrapper:
"""
Wrapper for mining agent with goal-specific behavior
"""
script_goals = ['cobblestone', 'stone', 'coal', 'iron_ore', 'diamond']
def __init__(self, env: MineDojoEnv, mine_agent: MineAgent, max_ranking: int = 15):
self.env = env
self.mine_agent = mine_agent
self.max_ranking = max_ranking
def can_handle_goal(self, goal: str) -> bool:
"""Check if this wrapper can handle the goal"""
goal_item = goal.replace("mine_", "").replace("obtain_", "")
return goal_item in self.script_goals
def execute_goal(self, goal: str) -> bool:
"""Execute the goal"""
if not self.can_handle_goal(goal):
return False
goal_item = goal.replace("mine_", "").replace("obtain_", "")
return self.mine_agent.mine_item(goal_item)
def resize_image(img: np.ndarray, target_resolution: Tuple[int, int] = (128, 128)) -> np.ndarray:
"""Resize image to target resolution"""
return cv2.resize(img, dsize=target_resolution, interpolation=cv2.INTER_LINEAR)
def accquire_goal_embeddings(clip_path: str, goal_list: List[str], device: str = "cuda") -> Dict:
"""
Acquire goal embeddings (simplified version without actual CLIP model)
"""
# Placeholder implementation - in real version, this would use actual CLIP model
embeddings = {}
for goal in goal_list:
# Generate dummy embedding
embedding = torch.randn(512).to(device)
embeddings[goal] = embedding
return embeddings