-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObservations.py
111 lines (98 loc) · 3.93 KB
/
Observations.py
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
import MalmoPython
import json
import time
class Observations(object):
def __init__(self, malmo_run):
self.mr = malmo_run
self.agent_host = self.mr.agent_host
def update(self):
world_state = self.mr.checkWorldState()
while len(world_state.observations) == 0:
if not world_state.is_mission_running:
return False
world_state = self.mr.checkWorldState()
# print "Observations: World state observations: " + str(world_state.observations[0].text)
try:
self.observations = json.loads(world_state.observations[0].text)
self.direction = self.observations['Yaw']
self.grid = self.observations['AgentGrid']
self.gridFloat = map(self.mr.b.blockId, self.grid)
self.x = self.observations['XPos']
self.z = self.observations['ZPos']
except KeyError:
return False
return True
def frontBlocked(self):
if (self.direction <= 45 or self.direction > 315):
return self.grid[16] != 'air'
elif (self.direction <= 135 and self.direction > 45):
return self.grid[12] != 'air'
elif (self.direction <= 225 and self.direction > 135):
return self.grid[10] != 'air'
else:
return self.grid[14] != 'air'
def backBlocked(self):
if (self.direction <= 45 or self.direction > 315):
return self.grid[10] != 'air'
elif (self.direction <= 135 and self.direction > 45):
return self.grid[14] != 'air'
elif (self.direction <= 225 and self.direction > 135):
return self.grid[16] != 'air'
else:
return self.grid[12] != 'air'
def leftBlocked(self):
if (self.direction <= 45 or self.direction > 315):
return self.grid[14] != 'air'
elif (self.direction <= 135 and self.direction > 45):
return self.grid[16] != 'air'
elif (self.direction <= 225 and self.direction > 135):
return self.grid[12] != 'air'
else:
return self.grid[10] != 'air'
def rightBlocked(self):
if (self.direction <= 45 or self.direction > 315):
return self.grid[12] != 'air'
elif (self.direction <= 135 and self.direction > 45):
return self.grid[10] != 'air'
elif (self.direction <= 225 and self.direction > 135):
return self.grid[14] != 'air'
else:
return self.grid[16] != 'air'
def frontBlock(self):
if (self.direction <= 45 or self.direction > 315):
return self.grid[16]
elif (self.direction <= 135 and self.direction > 45):
return self.grid[12]
elif (self.direction <= 225 and self.direction > 135):
return self.grid[10]
else:
return self.grid[14]
def backBlock(self):
if (self.direction <= 45 or self.direction > 315):
return self.grid[10]
elif (self.direction <= 135 and self.direction > 45):
return self.grid[14]
elif (self.direction <= 225 and self.direction > 135):
return self.grid[16]
else:
return self.grid[12]
def leftBlock(self):
if (self.direction <= 45 or self.direction > 315):
return self.grid[14]
elif (self.direction <= 135 and self.direction > 45):
return self.grid[16]
elif (self.direction <= 225 and self.direction > 135):
return self.grid[12]
else:
return self.grid[10]
def rightBlock(self):
if (self.direction <= 45 or self.direction > 315):
return self.grid[12]
elif (self.direction <= 135 and self.direction > 45):
return self.grid[10]
elif (self.direction <= 225 and self.direction > 135):
return self.grid[14]
else:
return self.grid[16]
def getDirection(self):
return self.direction