| … | … | @@ -30,8 +30,15 @@ def raise_timeout(signum, frame): |
| 30 | 30 | raise TimeoutError |
| 31 | 31 | |
| 32 | 32 | |
| 33 | | def drawBlock(x, y, z, type): |
| 34 | | return """<DrawBlock x='{0}' y='{1}' z='{2}' type='{3}'/>""".format(x, y, z, type) |
| 33 | def drawBlock(x, y, z, type, variant=False, face=False): |
| 34 | if variant: |
| 35 | return """<DrawBlock x='{0}' y='{1}' z='{2}' type='{3}' variant='{4}' face='{5}'/>""".format( |
| 36 | x, y, z, type, variant, face |
| 37 | ) |
| 38 | else: |
| 39 | return """<DrawBlock x='{0}' y='{1}' z='{2}' type='{3}' />""".format( |
| 40 | x, y, z, type, variant |
| 41 | ) |
| 35 | 42 | |
| 36 | 43 | |
| 37 | 44 | def GenCuboid(x1, y1, z1, x2, y2, z2, blocktype): |
| … | … | @@ -183,12 +190,12 @@ def stop_condition(agent, blocktype, sec=10): |
| 183 | 190 | def get_curr_loc(agent): |
| 184 | 191 | loc = (agent_x, agent_y, agent_z) |
| 185 | 192 | world_state = agent.peekWorldState() |
| 186 | | # while world_state.is_mission_running: |
| 187 | | # world_state = agent.peekWorldState() |
| 188 | | if world_state.number_of_observations_since_last_state > 0: |
| 189 | | ob = json.loads(world_state.observations[-1].text) |
| 190 | | loc = (int(ob[u"XPos"]), int(ob[u"YPos"]), int(ob[u"ZPos"])) |
| 191 | | |
| 193 | while world_state.is_mission_running: |
| 194 | world_state = agent.peekWorldState() |
| 195 | if world_state.number_of_observations_since_last_state > 0: |
| 196 | ob = json.loads(world_state.observations[-1].text) |
| 197 | loc = (int(ob[u"XPos"]), int(ob[u"YPos"]), int(ob[u"ZPos"])) |
| 198 | break |
| 192 | 199 | return loc |
| 193 | 200 | |
| 194 | 201 | |
| … | … | @@ -214,20 +221,36 @@ def is_mission_running(agent): |
| 214 | 221 | return world_state.is_mission_running |
| 215 | 222 | |
| 216 | 223 | |
| 224 | def inside_house(x, z): |
| 225 | return ( |
| 226 | True |
| 227 | if (x in range(start_x, start_x - length, -1)) |
| 228 | and (z in range(start_z, start_z - width, -1)) |
| 229 | else False |
| 230 | ) |
| 231 | |
| 232 | |
| 217 | 233 | # Move the agent towards the key |
| 218 | 234 | # reward=0 |
| 219 | 235 | def go_to_the_key(agent): |
| 220 | 236 | global holds_key |
| 221 | 237 | reward = 0 |
| 222 | | observation = {"holds": ["self", "key"]} |
| 238 | observation = {} |
| 239 | curr_x, curr_y, curr_z = get_curr_loc(agent) |
| 240 | if inside_house(curr_x, curr_z): |
| 241 | observation["inside"] = ["self", "house"] |
| 242 | return observation, reward, is_mission_running |
| 243 | else: |
| 244 | observation["outside"] = ["self", "house"] |
| 223 | 245 | if holds_key: |
| 246 | observation["holds"] = ["self", "key"] |
| 224 | 247 | return observation, reward, is_mission_running |
| 225 | | curr_x, curr_y, curr_z = get_curr_loc(agent) |
| 226 | 248 | turn_to(agent, curr_x, curr_y, curr_z, key_x, key_y, key_z) |
| 227 | 249 | time.sleep(0.2) |
| 228 | 250 | agent.sendCommand("move 1") |
| 229 | 251 | action_complete = stop_condition(agent, "tripwire_hook") |
| 230 | 252 | if action_complete: |
| 253 | observation["holds"] = ["self", "key"] |
| 231 | 254 | agent.sendCommand("hotbar.2 1") |
| 232 | 255 | holds_key = True |
| 233 | 256 | else: |
| … | … | @@ -242,26 +265,23 @@ def go_to_the_house(agent): |
| 242 | 265 | reward = 0 |
| 243 | 266 | observation = {} |
| 244 | 267 | curr_x, curr_y, curr_z = get_curr_loc(agent) |
| 245 | | if curr_x == door_x and curr_z == door_z: |
| 268 | if inside_house(curr_x, curr_z): |
| 246 | 269 | observation["inside"] = ["self", "house"] |
| 247 | 270 | return observation, reward, is_mission_running |
| 248 | | |
| 249 | | turn_to(agent, curr_x, curr_y, curr_z, door_x, door_y, door_z) |
| 271 | turn_to(agent, curr_x, curr_y, curr_z, door_x + 2, door_y, door_z) |
| 250 | 272 | time.sleep(0.2) |
| 251 | 273 | agent.sendCommand("move 1") |
| 252 | 274 | action_complete = stop_condition(agent, "dark_oak_door") |
| 253 | 275 | if action_complete: |
| 254 | 276 | if holds_key: |
| 255 | | observation["holds"] = ["self", "key"] |
| 256 | 277 | agent.sendCommand("tp {} {} {}".format(door_x, door_y, door_z)) |
| 278 | time.sleep(0.2) |
| 257 | 279 | observation["inside"] = ["self", "house"] |
| 258 | 280 | agent.sendCommand("hotbar.1 1") |
| 259 | 281 | holds_key = False |
| 260 | 282 | else: |
| 283 | observation["outside"] = ["self", "house"] |
| 261 | 284 | observation["nextto"] = ["self", "closed_door"] |
| 262 | | |
| 263 | | else: |
| 264 | | observation = [] |
| 265 | 285 | reward = 0 |
| 266 | 286 | return observation, reward, is_mission_running |
| 267 | 287 | |
| … | … | @@ -279,7 +299,12 @@ def go_to_the_diamonds(agent): |
| 279 | 299 | relocate_agent(agent) |
| 280 | 300 | else: |
| 281 | 301 | reward = 0 |
| 282 | | return [], reward, is_mission_running |
| 302 | observation = {} |
| 303 | if not inside_house(curr_x, curr_z): |
| 304 | observation["outside"] = ["self", "house"] |
| 305 | if holds_key: |
| 306 | observation["holds"] = ["self", "key"] |
| 307 | return observation, reward, is_mission_running |
| 283 | 308 | |
| 284 | 309 | |
| 285 | 310 | missionXML = ( |
| … | … | @@ -306,16 +331,11 @@ missionXML = ( |
| 306 | 331 | <DrawingDecorator> |
| 307 | 332 | """ |
| 308 | 333 | + build_house(start_x, start_y, start_z, width, length, height, "planks") |
| 309 | | + """ |
| 310 | | """ |
| 311 | | + drawLine( |
| 312 | | door_x, door_y - 1, door_z, door_x, door_y, door_z, "dark_oak_door", "EAST" |
| 334 | + drawBlock( |
| 335 | door_x, door_y - 1, door_z, "dark_oak_door", variant="lower", face="WEST" |
| 313 | 336 | ) |
| 314 | | + """ |
| 315 | | """ |
| 337 | + drawBlock(door_x, door_y, door_z, "dark_oak_door", variant="upper", face="WEST") |
| 316 | 338 | + drawBlock(key_x, key_y - 1, key_z, "command_block") |
| 317 | | + """ |
| 318 | | """ |
| 319 | 339 | + drawBlock(key_x, key_y, key_z, "tripwire_hook") |
| 320 | 340 | + """ |
| 321 | 341 | |