| … | … | @@ -89,12 +89,13 @@ def build_house(x, y, z, width, length, height, blocktype): |
| 89 | 89 | x_cur = x |
| 90 | 90 | for j in range(width): |
| 91 | 91 | genstring = genstring + drawBlock(x_front, y_cur, z_cur, "glass") + "\n" |
| 92 | | genstring = ( |
| 93 | | genstring + drawBlock(x_back, y_cur, z_cur, "diamond_block") + "\n" |
| 94 | | ) |
| 95 | | genstring = ( |
| 96 | | genstring + drawBlock(x_back - 1, y_cur, z_cur, blocktype) + "\n" |
| 97 | | ) |
| 92 | genstring = genstring + drawBlock(x_back, y_cur, z_cur, blocktype) + "\n" |
| 93 | if j > 0 and j < width - 1: |
| 94 | genstring = ( |
| 95 | genstring |
| 96 | + drawBlock(x_back + 1, y_cur, z_cur, "diamond_block") |
| 97 | + "\n" |
| 98 | ) |
| 98 | 99 | z_cur = z_cur + 1 if z_cur >= 0 else z_cur - 1 |
| 99 | 100 | |
| 100 | 101 | for k in range(length): |
| … | … | @@ -164,7 +165,6 @@ def stop_condition(agent, blocktype, sec=10): |
| 164 | 165 | if world_state.number_of_observations_since_last_state > 0: |
| 165 | 166 | ob = json.loads(world_state.observations[-1].text) |
| 166 | 167 | if blocktype in ob["BlocksInFront"]: |
| 167 | | print("reached at {}".format(blocktype)) |
| 168 | 168 | agent.sendCommand("move 0") |
| 169 | 169 | done = True |
| 170 | 170 | |
| … | … | @@ -218,6 +218,10 @@ def is_mission_running(agent): |
| 218 | 218 | # reward=0 |
| 219 | 219 | def go_to_the_key(agent): |
| 220 | 220 | global holds_key |
| 221 | reward = 0 |
| 222 | observation = {"holds": ["self", "key"]} |
| 223 | if holds_key: |
| 224 | return observation, reward, is_mission_running |
| 221 | 225 | curr_x, curr_y, curr_z = get_curr_loc(agent) |
| 222 | 226 | turn_to(agent, curr_x, curr_y, curr_z, key_x, key_y, key_z) |
| 223 | 227 | time.sleep(0.2) |
| … | … | @@ -225,30 +229,36 @@ def go_to_the_key(agent): |
| 225 | 229 | action_complete = stop_condition(agent, "tripwire_hook") |
| 226 | 230 | if action_complete: |
| 227 | 231 | agent.sendCommand("hotbar.2 1") |
| 228 | | observation = {"observation": ["holds_key"]} |
| 229 | 232 | holds_key = True |
| 230 | 233 | else: |
| 231 | 234 | observation = [] |
| 232 | | reward = 0 |
| 233 | 235 | return observation, reward, is_mission_running |
| 234 | 236 | |
| 235 | 237 | |
| 236 | 238 | # Move the agent towards the house and enter the house. |
| 237 | 239 | # reward = 0 |
| 238 | 240 | def go_to_the_house(agent): |
| 241 | global holds_key |
| 242 | reward = 0 |
| 243 | observation = {} |
| 239 | 244 | curr_x, curr_y, curr_z = get_curr_loc(agent) |
| 245 | if curr_x == door_x and curr_z == door_z: |
| 246 | observation["inside"] = ["self", "house"] |
| 247 | return observation, reward, is_mission_running |
| 248 | |
| 240 | 249 | turn_to(agent, curr_x, curr_y, curr_z, door_x, door_y, door_z) |
| 241 | 250 | time.sleep(0.2) |
| 242 | 251 | agent.sendCommand("move 1") |
| 243 | 252 | action_complete = stop_condition(agent, "dark_oak_door") |
| 244 | 253 | if action_complete: |
| 245 | | observation = {"observation": []} |
| 246 | 254 | if holds_key: |
| 255 | observation["holds"] = ["self", "key"] |
| 247 | 256 | agent.sendCommand("tp {} {} {}".format(door_x, door_y, door_z)) |
| 248 | | observation["observation"].append("inside_the_house") |
| 249 | | observation["observation"].append("holds_key") |
| 257 | observation["inside"] = ["self", "house"] |
| 258 | agent.sendCommand("hotbar.1 1") |
| 259 | holds_key = False |
| 250 | 260 | else: |
| 251 | | observation["observation"].append("nextto_closed_door") |
| 261 | observation["nextto"] = ["self", "closed_door"] |
| 252 | 262 | |
| 253 | 263 | else: |
| 254 | 264 | observation = [] |
| … | … | @@ -266,6 +276,7 @@ def go_to_the_diamonds(agent): |
| 266 | 276 | action_complete = stop_condition(agent, "diamond_block") |
| 267 | 277 | if action_complete: |
| 268 | 278 | reward = 1 |
| 279 | relocate_agent(agent) |
| 269 | 280 | else: |
| 270 | 281 | reward = 0 |
| 271 | 282 | return [], reward, is_mission_running |
| … | … | @@ -285,7 +296,7 @@ missionXML = ( |
| 285 | 296 | <ServerInitialConditions> |
| 286 | 297 | <Time> |
| 287 | 298 | <StartTime>1000</StartTime> |
| 288 | | <AllowPassageOfTime>true</AllowPassageOfTime> |
| 299 | <AllowPassageOfTime>false</AllowPassageOfTime> |
| 289 | 300 | </Time> |
| 290 | 301 | <Weather>clear</Weather> |
| 291 | 302 | <AllowSpawning>true</AllowSpawning> |
| … | … | @@ -309,7 +320,7 @@ missionXML = ( |
| 309 | 320 | + """ |
| 310 | 321 | |
| 311 | 322 | </DrawingDecorator> |
| 312 | | <ServerQuitFromTimeUp description="" timeLimitMs="1000000"/> |
| 323 | <ServerQuitFromTimeUp description="" timeLimitMs="10000000"/> |
| 313 | 324 | <ServerQuitWhenAnyAgentFinishes/> |
| 314 | 325 | </ServerHandlers> |
| 315 | 326 | </ServerSection> |
| … | … | @@ -365,22 +376,23 @@ if __name__ == "__main__": |
| 365 | 376 | rw, ob, done = malmoWrapper.restart() |
| 366 | 377 | total_reward = 0 |
| 367 | 378 | agent = malmoWrapper.agent_host |
| 368 | | _, rw, _ = go_to_the_key(agent) |
| 369 | | print(ob) |
| 370 | | total_reward += rw |
| 371 | 379 | world_state = agent.getWorldState() |
| 380 | relocate_agent(agent) |
| 372 | 381 | while world_state.is_mission_running: |
| 373 | 382 | world_state = agent.getWorldState() |
| 374 | 383 | if world_state.number_of_rewards_since_last_state > 0: |
| 375 | 384 | total_reward += world_state.rewards[-1].getValue() |
| 376 | 385 | print("total reward minecraft = {}".format(total_reward)) |
| 386 | ob, rw, _ = go_to_the_key(agent) |
| 387 | print("Observation: {}, reward: {}".format(ob, rw)) |
| 388 | total_reward += rw |
| 377 | 389 | |
| 378 | 390 | ob, rw, _ = go_to_the_house(agent) |
| 379 | | print(ob) |
| 391 | print("Observation: {}, reward: {}".format(ob, rw)) |
| 380 | 392 | total_reward += rw |
| 381 | 393 | |
| 382 | 394 | ob, rw, _ = go_to_the_diamonds(agent) |
| 383 | | print(ob) |
| 395 | print("Observation: {}, reward: {}".format(ob, rw)) |
| 384 | 396 | total_reward += rw |
| 385 | 397 | |
| 386 | 398 | time.sleep(2) |