| … | … | @@ -0,0 +1,663 @@ |
| 1 | import os |
| 2 | import time |
| 3 | |
| 4 | from rocca.envs.wrappers.malmo_wrapper import MalmoWrapper |
| 5 | from rocca.envs.wrappers.utils import * |
| 6 | from builtins import range |
| 7 | import json |
| 8 | import math |
| 9 | import signal |
| 10 | from contextlib import contextmanager |
| 11 | import random |
| 12 | |
| 13 | |
| 14 | @contextmanager |
| 15 | def timeout(time): |
| 16 | # Register a function to raise a TimeoutError on the signal. |
| 17 | signal.signal(signal.SIGALRM, raise_timeout) |
| 18 | # Schedule the signal to be sent after ``time``. |
| 19 | signal.alarm(time) |
| 20 | try: |
| 21 | yield |
| 22 | except TimeoutError: |
| 23 | pass |
| 24 | finally: |
| 25 | # Unregister the signal so it won't be triggered |
| 26 | # if the timeout is not reached. |
| 27 | signal.signal(signal.SIGALRM, signal.SIG_IGN) |
| 28 | |
| 29 | |
| 30 | def raise_timeout(signum, frame): |
| 31 | raise TimeoutError |
| 32 | |
| 33 | |
| 34 | def drawBlock(x, y, z, type, variant=False, face=False): |
| 35 | if variant: |
| 36 | return """<DrawBlock x='{0}' y='{1}' z='{2}' type='{3}' variant='{4}' face='{5}'/>""".format( |
| 37 | x, y, z, type, variant, face |
| 38 | ) |
| 39 | else: |
| 40 | return """<DrawBlock x='{0}' y='{1}' z='{2}' type='{3}' />""".format( |
| 41 | x, y, z, type, variant |
| 42 | ) |
| 43 | |
| 44 | |
| 45 | def GenCuboid(x1, y1, z1, x2, y2, z2, blocktype): |
| 46 | return ( |
| 47 | '<DrawCuboid x1="' |
| 48 | + str(x1) |
| 49 | + '" y1="' |
| 50 | + str(y1) |
| 51 | + '" z1="' |
| 52 | + str(z1) |
| 53 | + '" x2="' |
| 54 | + str(x2) |
| 55 | + '" y2="' |
| 56 | + str(y2) |
| 57 | + '" z2="' |
| 58 | + str(z2) |
| 59 | + '" type="' |
| 60 | + blocktype |
| 61 | + '"/>' |
| 62 | ) |
| 63 | |
| 64 | |
| 65 | def drawItem(x, y, z, item_type): |
| 66 | return ( |
| 67 | '<DrawItem x="' |
| 68 | + str(x) |
| 69 | + '" y="' |
| 70 | + str(y) |
| 71 | + '" z="' |
| 72 | + str(z) |
| 73 | + '" type="' |
| 74 | + item_type |
| 75 | + '"/>' |
| 76 | ) |
| 77 | |
| 78 | |
| 79 | def drawLine(x1, y1, z1, x2, y2, z2, blocktype, face): |
| 80 | return ( |
| 81 | '<DrawLine x1="' |
| 82 | + str(x1) |
| 83 | + '" y1="' |
| 84 | + str(y1) |
| 85 | + '" z1="' |
| 86 | + str(z1) |
| 87 | + '" x2="' |
| 88 | + str(x2) |
| 89 | + '" y2="' |
| 90 | + str(y2) |
| 91 | + '" z2="' |
| 92 | + str(z2) |
| 93 | + '" type="' |
| 94 | + blocktype |
| 95 | + '" face="' |
| 96 | + face |
| 97 | + '"/>' |
| 98 | ) |
| 99 | |
| 100 | |
| 101 | def draw_fence(x, y, z): |
| 102 | fence = "" |
| 103 | xy_coord = [(-x, z), (x, z), (x, -z), (-x, -z)] |
| 104 | for i in range(4): |
| 105 | fence += drawLine( |
| 106 | x1=xy_coord[i][0], |
| 107 | y1=y, |
| 108 | z1=xy_coord[i][1], |
| 109 | x2=xy_coord[i + 1][0] if i < 3 else xy_coord[0][0], |
| 110 | y2=y, |
| 111 | z2=xy_coord[i + 1][1] if i < 3 else xy_coord[0][1], |
| 112 | blocktype="fence", |
| 113 | face="WEST", |
| 114 | ) |
| 115 | return fence |
| 116 | |
| 117 | |
| 118 | def build_house(x, y, z, width, length, height, blocktype): |
| 119 | x_front = x |
| 120 | x_back = x + length if x >= 0 else x - length |
| 121 | z_left = z + width if z >= 0 else z - width |
| 122 | z_right = z |
| 123 | |
| 124 | genstring = "" |
| 125 | y_cur = y |
| 126 | for i in range(height + 1): |
| 127 | z_cur = z |
| 128 | x_cur = x |
| 129 | for j in range(width + 1): |
| 130 | genstring = genstring + drawBlock(x_front, y_cur, z_cur, "glass") + "\n" |
| 131 | genstring = genstring + drawBlock(x_back, y_cur, z_cur, blocktype) + "\n" |
| 132 | if j > 0 and j < width: |
| 133 | genstring = ( |
| 134 | genstring |
| 135 | + drawBlock(x_back + 1, y_cur, z_cur, "diamond_ore") |
| 136 | + "\n" |
| 137 | ) |
| 138 | z_cur = z_cur + 1 if z_cur >= 0 else z_cur - 1 |
| 139 | |
| 140 | for k in range(length): |
| 141 | if k % 2 != 0: |
| 142 | genstring = genstring + drawBlock(x_cur, y_cur, z_left, "glass") + "\n" |
| 143 | genstring = genstring + drawBlock(x_cur, y_cur, z_right, "glass") + "\n" |
| 144 | else: |
| 145 | genstring = ( |
| 146 | genstring + drawBlock(x_cur, y_cur, z_left, blocktype) + "\n" |
| 147 | ) |
| 148 | genstring = ( |
| 149 | genstring + drawBlock(x_cur, y_cur, z_right, blocktype) + "\n" |
| 150 | ) |
| 151 | x_cur = x_cur + 1 if x_cur >= 0 else x_cur - 1 |
| 152 | |
| 153 | y_cur = y_cur + 1 if y_cur >= 0 else y_cur - 1 |
| 154 | |
| 155 | # floor |
| 156 | genstring = genstring + GenCuboid( |
| 157 | -15, |
| 158 | y, |
| 159 | 15, |
| 160 | 15, |
| 161 | y, |
| 162 | -15, |
| 163 | "obsidian", |
| 164 | ) |
| 165 | # ceiling |
| 166 | genstring = genstring + GenCuboid( |
| 167 | x_back + 1 if x_back >= 0 else x_back - 1, |
| 168 | y_cur, |
| 169 | z_left + 1 if z_left >= 0 else z_left - 1, |
| 170 | x - 1 if x >= 0 else x + 1, |
| 171 | y_cur, |
| 172 | z - 1 if x >= 0 else z + 1, |
| 173 | blocktype, |
| 174 | ) |
| 175 | genstring = genstring + GenCuboid( |
| 176 | x_back, |
| 177 | y_cur + 1, |
| 178 | z_left, |
| 179 | x, |
| 180 | y_cur + 1, |
| 181 | z, |
| 182 | blocktype, |
| 183 | ) |
| 184 | genstring = genstring + GenCuboid( |
| 185 | x_back - 1 if x_back >= 0 else x_back + 1, |
| 186 | y_cur + 2, |
| 187 | z_left - 1 if z_left >= 0 else z_left + 1, |
| 188 | x + 1 if x >= 0 else x - 1, |
| 189 | y_cur + 2, |
| 190 | z + 1 if x >= 0 else z - 1, |
| 191 | blocktype, |
| 192 | ) |
| 193 | genstring = genstring + GenCuboid( |
| 194 | x_back, |
| 195 | y_cur, |
| 196 | z_left, |
| 197 | x, |
| 198 | y_cur, |
| 199 | z, |
| 200 | "air", |
| 201 | ) |
| 202 | genstring = genstring + GenCuboid( |
| 203 | x_back - 1 if x_back >= 0 else x_back + 1, |
| 204 | y_cur + 1, |
| 205 | z_left - 1 if z_left >= 0 else z_left + 1, |
| 206 | x + 1 if x >= 0 else x - 1, |
| 207 | y_cur + 1, |
| 208 | z + 1 if x >= 0 else z - 1, |
| 209 | "air", |
| 210 | ) |
| 211 | genstring = genstring + GenCuboid( |
| 212 | x_back - 2 if x_back >= 0 else x_back + 2, |
| 213 | y_cur + 2, |
| 214 | z_left - 2 if z_left >= 0 else z_left + 2, |
| 215 | x + 2 if x >= 0 else x - 2, |
| 216 | y_cur + 2, |
| 217 | z + 2 if x >= 0 else z - 2, |
| 218 | "glass", |
| 219 | ) |
| 220 | |
| 221 | return genstring |
| 222 | |
| 223 | |
| 224 | # Initialize the location coordinates |
| 225 | agent_x, agent_y, agent_z, agent_yaw = 13, 230, -2, 90 |
| 226 | key_x, key_y, key_z = 7, 228, -10 |
| 227 | door_x, door_y, door_z = -4, 228, -3 |
| 228 | exit_x, exit_y, exit_z = -9, 228, -1 |
| 229 | diamond_x, diamond_y, diamond_z = -18, 228, -3 |
| 230 | start_x, start_y, start_z, width, length, height = -4, 226, -1, 6, 9, 4 |
| 231 | hold_key = False |
| 232 | fence_max = 15 |
| 233 | |
| 234 | place_count = 0 |
| 235 | pitch_adjusted = False |
| 236 | total_reward = 0 |
| 237 | |
| 238 | |
| 239 | def adjust_pitch(agent): |
| 240 | global pitch_adjusted |
| 241 | if not pitch_adjusted: |
| 242 | agent.sendCommand("pitch 0.5") |
| 243 | time.sleep(0.5) |
| 244 | agent.sendCommand("pitch 0") |
| 245 | pitch_adjusted = True |
| 246 | |
| 247 | |
| 248 | # relocates the agent to the starting point |
| 249 | def relocate_agent(agent, msg=""): |
| 250 | print(msg) |
| 251 | agent.sendCommand("tp {} {} {}".format(agent_x, agent_y, agent_z)) |
| 252 | if not hold_key: |
| 253 | agent.sendCommand("hotbar.4 1") |
| 254 | agent.sendCommand("setYaw {}".format(agent_yaw)) |
| 255 | time.sleep(0.2) |
| 256 | |
| 257 | |
| 258 | blocks = { |
| 259 | "tripwire_hook": "key", |
| 260 | "dark_oak_door": "house", |
| 261 | "diamond_block": "diamonds", |
| 262 | "planks": "planks", |
| 263 | "diamond_ore": "collect diamonds", |
| 264 | } |
| 265 | |
| 266 | # stop the agent |
| 267 | def stop_condition(agent, blocktype, sec=10): |
| 268 | print("\nMoving to {} \n".format(blocks[blocktype])) |
| 269 | |
| 270 | def stop(agent, blocktype): |
| 271 | done = False |
| 272 | world_state = agent.peekWorldState() |
| 273 | while not done: |
| 274 | world_state = agent.peekWorldState() |
| 275 | if world_state.number_of_observations_since_last_state > 0: |
| 276 | ob = json.loads(world_state.observations[-1].text) |
| 277 | if blocktype in ob["BlocksInFront"]: |
| 278 | agent.sendCommand("move 0") |
| 279 | done = True |
| 280 | |
| 281 | with timeout(sec): |
| 282 | try: |
| 283 | stop(agent, blocktype) |
| 284 | return True |
| 285 | except TimeoutError: |
| 286 | agent.sendCommand("move 0") |
| 287 | relocate_agent(agent, msg="*** RELOCATE: Unable to reach the target ***") |
| 288 | return False |
| 289 | |
| 290 | |
| 291 | # Get realtime location coordinates |
| 292 | def get_curr_loc(agent): |
| 293 | loc = (agent_x, agent_y, agent_z) |
| 294 | world_state = agent.peekWorldState() |
| 295 | while world_state.is_mission_running: |
| 296 | world_state = agent.peekWorldState() |
| 297 | if world_state.number_of_observations_since_last_state > 0: |
| 298 | ob = json.loads(world_state.observations[-1].text) |
| 299 | yaw = int(ob["Yaw"]) |
| 300 | loc = (int(ob[u"XPos"]), int(ob[u"YPos"]), int(ob[u"ZPos"]), yaw) |
| 301 | break |
| 302 | return loc |
| 303 | |
| 304 | |
| 305 | # Turns the agent towards the location given |
| 306 | def turn_to(agent, x1, y1, z1, x2, y2, z2, curr_yaw=False): |
| 307 | def get_angle(x1, y1, z1, x2, y2, z2): |
| 308 | x_dist = abs(x1 - x2) |
| 309 | z_dist = abs(z1 - z2) |
| 310 | h = math.sqrt(x_dist ** 2 + z_dist ** 2) |
| 311 | return math.degrees(z_dist / h) if h != 0 else 0 |
| 312 | |
| 313 | angle = get_angle(x1, y1, z1, x2, y2, z2) |
| 314 | yaw = 90 if x1 > x2 else -90 |
| 315 | if yaw > 0: |
| 316 | angle = yaw - angle if z1 < z2 else yaw + angle |
| 317 | else: |
| 318 | angle = yaw + angle if z1 < z2 else yaw - angle |
| 319 | if curr_yaw: |
| 320 | if curr_yaw < angle: |
| 321 | agent.sendCommand("turn 0.5") |
| 322 | else: |
| 323 | agent.sendCommand("turn -0.5") |
| 324 | while is_mission_running: |
| 325 | world_state = agent.peekWorldState() |
| 326 | if world_state.number_of_observations_since_last_state > 0: |
| 327 | ob = json.loads(world_state.observations[-1].text) |
| 328 | yaw = int(ob["Yaw"]) |
| 329 | if abs(yaw - angle) < 5: |
| 330 | agent.sendCommand("turn 0") |
| 331 | agent.sendCommand("setYaw {}".format(angle)) |
| 332 | break |
| 333 | else: |
| 334 | agent.sendCommand("setYaw {}".format(angle)) |
| 335 | |
| 336 | |
| 337 | def is_mission_running(agent): |
| 338 | world_state = agent.peekWorldState() |
| 339 | return world_state.is_mission_running |
| 340 | |
| 341 | |
| 342 | def inside_house(x, z): |
| 343 | return ( |
| 344 | True |
| 345 | if (x in range(start_x, start_x - length, -1)) |
| 346 | and (z in range(start_z, start_z - width, -1)) |
| 347 | else False |
| 348 | ) |
| 349 | |
| 350 | |
| 351 | def get_txt(obs): |
| 352 | if obs: |
| 353 | return ",".join( |
| 354 | ["{}({},{})".format(ob, obs[ob][0], obs[ob][1]) for ob in obs.keys()] |
| 355 | ) |
| 356 | else: |
| 357 | return "" |
| 358 | |
| 359 | |
| 360 | # Move the agent towards the key |
| 361 | # reward=0 |
| 362 | def go_to_key(agent): |
| 363 | adjust_pitch(agent) |
| 364 | global hold_key |
| 365 | reward = 0 |
| 366 | observation = {} |
| 367 | curr_x, curr_y, curr_z, curr_yaw = get_curr_loc(agent) |
| 368 | if inside_house(curr_x, curr_z): |
| 369 | observation["inside"] = ["self", "house"] |
| 370 | print("Observation: {}".format(get_txt(observation))) |
| 371 | return observation, reward, is_mission_running |
| 372 | else: |
| 373 | observation["outside"] = ["self", "house"] |
| 374 | if hold_key: |
| 375 | observation["hold"] = ["self", "key"] |
| 376 | print("Observation: {}".format(get_txt(observation))) |
| 377 | return observation, reward, is_mission_running |
| 378 | turn_to(agent, curr_x, curr_y, curr_z, key_x, key_y, key_z, curr_yaw=curr_yaw) |
| 379 | time.sleep(0.2) |
| 380 | agent.sendCommand("move 1") |
| 381 | action_complete = stop_condition(agent, "tripwire_hook") |
| 382 | if action_complete: |
| 383 | observation["hold"] = ["self", "key"] |
| 384 | agent.sendCommand("hotbar.2 1") |
| 385 | hold_key = True |
| 386 | else: |
| 387 | observation = [] |
| 388 | print("Observation: {}".format(get_txt(observation))) |
| 389 | return observation, reward, is_mission_running |
| 390 | |
| 391 | |
| 392 | # Move the agent towards the house and enter the house. |
| 393 | # reward = 0 |
| 394 | def go_to_house(agent): |
| 395 | adjust_pitch(agent) |
| 396 | global hold_key |
| 397 | reward = 0 |
| 398 | observation = {} |
| 399 | curr_x, curr_y, curr_z, curr_yaw = get_curr_loc(agent) |
| 400 | if inside_house(curr_x, curr_z): |
| 401 | observation["inside"] = ["self", "house"] |
| 402 | print("Observation: {}".format(get_txt(observation))) |
| 403 | return observation, reward, is_mission_running |
| 404 | turn_to( |
| 405 | agent, curr_x, curr_y, curr_z, door_x + 2, door_y, door_z, curr_yaw=curr_yaw |
| 406 | ) |
| 407 | time.sleep(0.2) |
| 408 | agent.sendCommand("move 1") |
| 409 | action_complete = stop_condition(agent, "dark_oak_door") |
| 410 | if action_complete: |
| 411 | if hold_key: |
| 412 | agent.sendCommand("move 1") |
| 413 | agent.sendCommand("tp {} {} {}".format(door_x, door_y, door_z)) |
| 414 | time.sleep(0.2) |
| 415 | agent.sendCommand("move 0") |
| 416 | observation["inside"] = ["self", "house"] |
| 417 | agent.sendCommand("hotbar.1 1") |
| 418 | hold_key = False |
| 419 | else: |
| 420 | observation["outside"] = ["self", "house"] |
| 421 | observation["nextto"] = ["self", "closed_door"] |
| 422 | reward = 0 |
| 423 | print("Observation: {}".format(get_txt(observation))) |
| 424 | return observation, reward, is_mission_running |
| 425 | |
| 426 | |
| 427 | def place_diamonds(agent): |
| 428 | global place_count |
| 429 | curr_x, curr_y, curr_z, curr_yaw = get_curr_loc(agent) |
| 430 | turn_to( |
| 431 | agent, |
| 432 | curr_x, |
| 433 | curr_y, |
| 434 | curr_z, |
| 435 | (exit_x + 2) + (2 * place_count), |
| 436 | exit_y, |
| 437 | fence_max, |
| 438 | ) |
| 439 | agent.sendCommand("move 1") |
| 440 | time.sleep(2 - (place_count * 0.01)) |
| 441 | placed = False |
| 442 | while not placed: |
| 443 | world_state = agent.getWorldState() |
| 444 | if world_state.number_of_observations_since_last_state > 0: |
| 445 | msg = world_state.observations[-1].text |
| 446 | ob = json.loads(msg) |
| 447 | # Use the line-of-sight observation to determine where to place: |
| 448 | if u"LineOfSight" in ob: |
| 449 | agent.sendCommand("turn {}".format(random.choice([0.05, -0.05]))) |
| 450 | los = ob[u"LineOfSight"] |
| 451 | x = int(math.floor(los["x"])) |
| 452 | z = int(math.floor(los["z"])) |
| 453 | if los["inRange"]: |
| 454 | agent.sendCommand("move 0") |
| 455 | time.sleep(2) |
| 456 | agent.sendCommand("use") |
| 457 | agent.sendCommand("turn 0") |
| 458 | agent.sendCommand("hotbar.5 1") |
| 459 | time.sleep(2) |
| 460 | place_count += 1 |
| 461 | placed = True |
| 462 | |
| 463 | |
| 464 | def exit(agent): |
| 465 | curr_x, curr_y, curr_z, curr_yaw = get_curr_loc(agent) |
| 466 | turn_to(agent, curr_x, curr_y, curr_z, exit_x, exit_y, exit_z) |
| 467 | agent.sendCommand("move 1") |
| 468 | agent.sendCommand("use 1") |
| 469 | time.sleep(0.2) |
| 470 | while is_mission_running(agent): |
| 471 | curr_x, curr_y, curr_z, _ = get_curr_loc(agent) |
| 472 | if not inside_house(curr_x, curr_z): |
| 473 | agent.sendCommand("move 0") |
| 474 | agent.sendCommand("tp {} {} {}".format(exit_x + 0.5, curr_y, exit_z + 1)) |
| 475 | time.sleep(0.1) |
| 476 | curr_x, curr_y, curr_z, _ = get_curr_loc(agent) |
| 477 | turn_to(agent, curr_x, curr_y, curr_z, exit_x, exit_y, exit_z) |
| 478 | agent.sendCommand("use 1") |
| 479 | time.sleep(0.05) |
| 480 | agent.sendCommand("use 0") |
| 481 | break |
| 482 | agent.sendCommand("hotbar.3 1") |
| 483 | place_diamonds(agent) |
| 484 | |
| 485 | |
| 486 | # Move the agent towards the diamond and collect it. |
| 487 | # reward = 1 |
| 488 | def go_to_diamonds(agent): |
| 489 | global total_reward |
| 490 | adjust_pitch(agent) |
| 491 | # agent.sendCommand("chat Collect diamonds") |
| 492 | observation = {} |
| 493 | curr_x, curr_y, curr_z, curr_yaw = get_curr_loc(agent) |
| 494 | turn_to( |
| 495 | agent, |
| 496 | curr_x, |
| 497 | curr_y, |
| 498 | curr_z, |
| 499 | diamond_x, |
| 500 | diamond_y, |
| 501 | diamond_z, |
| 502 | curr_yaw=curr_yaw, |
| 503 | ) |
| 504 | time.sleep(0.2) |
| 505 | agent.sendCommand("move 1") |
| 506 | action_complete = stop_condition(agent, "diamond_ore") |
| 507 | if action_complete: |
| 508 | agent.sendCommand("attack 1") |
| 509 | time.sleep(0.2) |
| 510 | agent.sendCommand("attack 0") |
| 511 | time.sleep(0.2) |
| 512 | reward = 1 |
| 513 | total_reward += 1 |
| 514 | exit(agent) |
| 515 | relocate_agent(agent) |
| 516 | time.sleep(0.3) |
| 517 | observation["outside"] = ["self", "house"] |
| 518 | else: |
| 519 | reward = 0 |
| 520 | if not inside_house(curr_x, curr_z): |
| 521 | observation["outside"] = ["self", "house"] |
| 522 | if hold_key: |
| 523 | observation["hold"] = ["self", "key"] |
| 524 | if reward > 0: |
| 525 | agent.sendCommand("chat Reward = {}".format(reward)) |
| 526 | agent.sendCommand("chat: Total Reward of {} diamonds!".format(total_reward)) |
| 527 | print("Reward = {}".format(reward)) |
| 528 | print("Total Reward of {} diamonds!".format(total_reward)) |
| 529 | |
| 530 | else: |
| 531 | print("Observation: {}".format(get_txt(observation))) |
| 532 | |
| 533 | return observation, reward, is_mission_running |
| 534 | |
| 535 | |
| 536 | missionXML = ( |
| 537 | """ |
| 538 | <?xml version="1.0" encoding="UTF-8" standalone="no" ?> |
| 539 | <Mission xmlns="http://ProjectMalmo.microsoft.com" |
| 540 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> |
| 541 | |
| 542 | <About> |
| 543 | <Summary>Collect diamonds!</Summary> |
| 544 | </About> |
| 545 | |
| 546 | <ServerSection> |
| 547 | <ServerInitialConditions> |
| 548 | <Time> |
| 549 | <StartTime>1000</StartTime> |
| 550 | <AllowPassageOfTime>false</AllowPassageOfTime> |
| 551 | </Time> |
| 552 | <Weather>clear</Weather> |
| 553 | <AllowSpawning>true</AllowSpawning> |
| 554 | </ServerInitialConditions> |
| 555 | <ServerHandlers> |
| 556 | <FlatWorldGenerator generatorString="3;7,220*1,5*3,2;1;village,decoration"/> |
| 557 | <DrawingDecorator> |
| 558 | """ |
| 559 | + build_house(start_x, start_y, start_z, width, length, height, "brick_block") |
| 560 | + drawBlock(diamond_x + 5, diamond_y, diamond_z - 1, "diamond_block") |
| 561 | + drawBlock( |
| 562 | door_x, door_y - 1, door_z, "dark_oak_door", variant="lower", face="WEST" |
| 563 | ) |
| 564 | + drawBlock(door_x, door_y, door_z, "dark_oak_door", variant="upper", face="WEST") |
| 565 | + drawBlock(exit_x, exit_y, exit_z, "dark_oak_door", variant="upper", face="NORTH") |
| 566 | + drawBlock( |
| 567 | exit_x, exit_y - 1, exit_z, "dark_oak_door", variant="lower", face="NORTH" |
| 568 | ) |
| 569 | + drawBlock(key_x, key_y - 1, key_z, "stone") |
| 570 | + drawBlock(key_x, key_y, key_z, "tripwire_hook") |
| 571 | + draw_fence(fence_max, start_y + 1, fence_max) |
| 572 | + """ |
| 573 | |
| 574 | </DrawingDecorator> |
| 575 | <ServerQuitWhenAnyAgentFinishes/> |
| 576 | </ServerHandlers> |
| 577 | </ServerSection> |
| 578 | |
| 579 | <AgentSection mode="Survival"> |
| 580 | <Name>Cog Agent</Name> |
| 581 | <AgentStart> |
| 582 | """ |
| 583 | + '<Placement x="{}" y="{}" z="{}" yaw="{}"/>'.format( |
| 584 | agent_x, agent_y, agent_z, agent_yaw |
| 585 | ) |
| 586 | + """ |
| 587 | <Inventory> |
| 588 | <InventoryItem slot="0" type="diamond_pickaxe"/> |
| 589 | <InventoryItem slot="1" type="tripwire_hook"/> |
| 590 | <InventoryBlock slot="2" type="diamond_block" quantity="63"/> |
| 591 | </Inventory> |
| 592 | </AgentStart> |
| 593 | <AgentHandlers> |
| 594 | <ObservationFromFullStats/> |
| 595 | <ObservationFromGrid> |
| 596 | <Grid name="BlocksInFront"> |
| 597 | <min x="-1" y="1" z="-1"/> |
| 598 | <max x="1" y="1" z="1"/> |
| 599 | </Grid> |
| 600 | </ObservationFromGrid> |
| 601 | <ObservationFromRay/> |
| 602 | <ObservationFromRecentCommands/> |
| 603 | <ContinuousMovementCommands turnSpeedDegs="80"/> |
| 604 | <AbsoluteMovementCommands /> |
| 605 | <DiscreteMovementCommands /> |
| 606 | <InventoryCommands/> |
| 607 | <ChatCommands/> |
| 608 | <RewardForTouchingBlockType> |
| 609 | <Block type="diamond_block" reward="1.0"/> |
| 610 | </RewardForTouchingBlockType> |
| 611 | </AgentHandlers> |
| 612 | </AgentSection> |
| 613 | |
| 614 | </Mission> |
| 615 | |
| 616 | """ |
| 617 | ) |
| 618 | |
| 619 | if __name__ == "__main__": |
| 620 | """ |
| 621 | The following is some random heuristics to demo how you |
| 622 | would use the MalmoWrapper and the environment defined |
| 623 | with the xml above. |
| 624 | """ |
| 625 | |
| 626 | a = AtomSpace() |
| 627 | set_default_atomspace(a) |
| 628 | |
| 629 | malmoWrapper = MalmoWrapper(missionXML=missionXML, validate=True) |
| 630 | |
| 631 | rw, ob, done = malmoWrapper.restart() |
| 632 | total_reward = 0 |
| 633 | agent = malmoWrapper.agent_host |
| 634 | world_state = agent.getWorldState() |
| 635 | # relocate_agent(agent) |
| 636 | adjust_pitch(agent) |
| 637 | while world_state.is_mission_running: |
| 638 | # curr_x, curr_y, curr_z = get_curr_loc(agent) |
| 639 | # print(curr_x, curr_y, curr_z) |
| 640 | # nb = input("Enter command: ") |
| 641 | |
| 642 | # nx, ny, nz = nb.split(",") |
| 643 | # turn_to(agent, curr_x, curr_y, curr_z, int(nx), int(ny), int(nz)) |
| 644 | |
| 645 | # agent.sendCommand(nb) |
| 646 | |
| 647 | world_state = agent.getWorldState() |
| 648 | if world_state.number_of_rewards_since_last_state > 0: |
| 649 | total_reward += world_state.rewards[-1].getValue() |
| 650 | print("total reward minecraft = {}".format(total_reward)) |
| 651 | ob, rw, _ = go_to_key(agent) |
| 652 | print("Observation: {}, reward: {}".format(ob, rw)) |
| 653 | |
| 654 | ob, rw, _ = go_to_house(agent) |
| 655 | print("Observation: {}, reward: {}".format(ob, rw)) |
| 656 | |
| 657 | ob, rw, _ = go_to_diamonds(agent) |
| 658 | print("Observation: {}, reward: {}".format(ob, rw)) |
| 659 | |
| 660 | time.sleep(2) |
| 661 | print("total reward = {}".format(total_reward)) |
| 662 | print() |
| 663 | print("Mission ended") |