-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwalk_test.py
More file actions
55 lines (46 loc) · 1.69 KB
/
walk_test.py
File metadata and controls
55 lines (46 loc) · 1.69 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
import tensorflow as tf
from tensorflow.keras import layers # type: ignore
from mcpi.minecraft import Minecraft
import numpy as np
from tensorflow.keras.models import load_model # type: ignore
from time import sleep
"""
Load the trained model to purchase its functionality
"""
# Load the pre-trained model
model = load_model("minecraft_agent_3.h5")
def get_position():
"""Get the player's position in the Minecraft world."""
player_pos = mc.player.getTilePos()
x = player_pos.x
y = player_pos.y
z = player_pos.z
return x, y, z
# Initialize Minecraft
mc = Minecraft.create()
# Wait for the player to be ready
sleep(30)
mc.postToChat("Program Started")
# Loop for executing the model's predictions
for i in range(14):
x0, y0, z0 = get_position()
# Get the blocks around the player
blocks = np.array([list(mc.getBlocks(x0 - 1, y0 - 1, z0 + 1, x0 + 1, y0 +1, z0 - 1))])
# Predict the action using the model
action = model(blocks)
action = np.argmax(action.numpy())
# Define the new positions based on the predicted action
if action == 0: # Move forward
new_pos = (x0 + 1, y0, z0)
print('w') # Corresponds to "move forward"
elif action == 1: # Move left
new_pos = (x0, y0, z0 - 1)
print('a') # Corresponds to "move left"
elif action == 2: # Move backward
new_pos = (x0 - 1, y0, z0)
print('s') # Corresponds to "move backward"
elif action == 3: # Move right
new_pos = (x0, y0, z0 + 1)
print('d') # Corresponds to "move right"
# Execute the action in Minecraft
mc.player.setPos(new_pos[0], new_pos[1], new_pos[2])