-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaze.py
More file actions
229 lines (175 loc) · 6.57 KB
/
maze.py
File metadata and controls
229 lines (175 loc) · 6.57 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import misc
isInitialized = False
successDirectionSet = set() # {(x,y,direction)}
unknownDirectionSet = set() # {(x,y,direction)}
lastDirectionDict = {} # {(x,y)} = direction
def init( doOptimization ):
preTick = get_tick_count()
quick_print("maze.init() was called")
global isInitialized
global successDirectionSet # {(x,y,direction)}
global unknownDirectionSet # {(x,y,direction)}
global lastDirectionDict # {(x,y)} = direction
isInitialized = True
successDirectionSet = set()
unknownDirectionSet = set()
lastDirectionDict = {}
directionList = [North,East,South,West]
(minX,maxX,minY,maxY) = misc.getWorldMinMaxXXYY()
addUnknownCount = 0
for idx in range( misc.getEntityMaxCount() ):
x = (idx % get_world_size())
y = (idx / get_world_size()) // 1
for dir in directionList:
if( doOptimization ):
if( (x == minX and dir == West) or (x == maxX and dir == East) or (y == minY and dir == South) or (y == maxY and dir == North) ):
continue
unknownDirectionSet.add((x,y,dir))
addUnknownCount = addUnknownCount+1
quick_print("DEBUG: maze.init() executed in", get_tick_count()-preTick, "ms", "with doOptimization =",doOptimization)
return addUnknownCount
def saveMove(moveSuccessful, moveDirection, fromPos):
(fromX,fromY) = fromPos
#remove unknown
if( (fromX,fromY,moveDirection) in unknownDirectionSet):
unknownDirectionSet.remove( (fromX,fromY,moveDirection) )
if( moveSuccessful == True ):
#MOVE SUCCESSFUL
successDirectionSet.add( (fromX,fromY,moveDirection) )
elif( moveSuccessful == False ):
#MOVE FAILED or REMOVE DEAD END
if( (fromX,fromY,moveDirection) in successDirectionSet ):
successDirectionSet.remove( (fromX,fromY,moveDirection) )
return True #success
def getPosFromDirection(fromPos, direction):
(minX,maxX,minY,maxY) = misc.getWorldMinMaxXXYY()
(fromX,fromY) = fromPos
toX = fromX
toY = fromY
if( direction == North):
toY = toY+1
if(toY > maxY):
toY = maxY
elif( direction == South):
toY = toY-1
if(toY < minY):
toY = minY
elif( direction == East):
toX = toX+1
if(toX > maxX):
toX = maxX
elif( direction == West):
toX = toX-1
if(toX < minX):
toX = minX
else:
return (None,None) #unknown direction
return (toX,toY)
def turnDegrees(direction, degrees):
directionList = [North,East,South,West]
directionIdx = None
for idx in range( len(directionList) ):
if(directionList[idx] == direction):
directionIdx = idx
break
if(directionIdx == None):
return None #direction not found
if(degrees % 90 != 0):
return None #must be divideable by 90 without remainder
degrees = degrees % 360 #fixes degrees, for example: -90 = 270, (360+90) = 90
turnCount = (degrees / 90) #amount of 90 degree turns
newIndex = (directionIdx + turnCount) % 4 #new direction index
return directionList[newIndex] #new direction
def getUnknownDirectionsAtPos(x,y):
directionList = [North,East,South,West]
unknownDirectionList = []
for dir in directionList:
if ((x,y,dir) in unknownDirectionSet):
unknownDirectionList.append(dir)
return unknownDirectionList
def getSuccessDirectionsAtPos(x,y):
directionList = [North,East,South,West]
successDirectionList = []
for dir in directionList:
if ((x,y,dir) in successDirectionSet):
successDirectionList.append(dir)
return successDirectionList
def createMaze( mazeSize ):
if( num_unlocked(Unlocks.Mazes) == 0 ):
return False
if( num_unlocked(Unlocks.Fertilizer) == 0 ):
return False
if( num_unlocked(Unlocks.Plant) == 0 ):
return False
itemRequired = Items.Weird_Substance
substanceRequired = mazeSize * num_unlocked(Unlocks.Mazes)
if( substanceRequired > num_items(itemRequired) ):
return False
clear()
if( plant(Entities.Bush) == False):
return False
if( use_item(itemRequired, substanceRequired) == False ):
return False
return True
def findTreasure( mazeSize = get_world_size() ):
if( createMaze( mazeSize ) == False ):
return False
init( False ) #parameter is doOptimization True/False
getEntity = get_entity_type()
fromX = get_pos_x()
fromY = get_pos_y()
while( getEntity != Entities.Treasure ):
eraseDeadEnd = False
moveDirection = None
while(moveDirection == None):
unknownDir = getUnknownDirectionsAtPos(fromX,fromY)
successDir = getSuccessDirectionsAtPos(fromX,fromY)
#get last direction at this position
lastDirection = None
if( (fromX,fromY) in lastDirectionDict ):
lastDirection = lastDirectionDict[(fromX,fromY)]
if( len(unknownDir) > 0 ):
#unknown direction(s) found, trying
moveDirection = unknownDir[0]
elif( len(unknownDir) == 0 and len(successDir) == 1):
moveDirection = successDir[0] #only one way to go
eraseDeadEnd = True #dead-end, erase from data to avoid returning
elif( len(unknownDir) == 0 and len(successDir) > 1):
#several ways to go
if(lastDirection == None):
randomIdx = (random() * len(successDir)) // 1
moveDirection = successDir[randomIdx]
else:
moveDirection = turnDegrees(lastDirection, 90)
successSet = misc.listToSet(successDir) #get successful directions, but in a set
while( not moveDirection in successSet ):
moveDirection = turnDegrees(moveDirection, 90)
elif( len(unknownDir) == 0 and len(successDir) == 0 ):
break #all paths are explored but all failed (error)
if(moveDirection == None):
init( False ) #parameter is doOptimization True/False
quick_print("failed to find a move")
continue
moveSuccessful = move(moveDirection) # attempt move
if(eraseDeadEnd == True):
saveMove(False, moveDirection, (fromX,fromY) ) #erase this movement
(nextX,nextY) = getPosFromDirection( (fromX,fromY), moveDirection )
if( (nextX,nextY) != (fromX,fromY) ):
oppositeDirection = turnDegrees(moveDirection, 180)
saveMove(False, oppositeDirection, (nextX,nextY) ) #erase this opposite movement
else:
saveMove(moveSuccessful, moveDirection, (fromX,fromY) ) #save this movement
(nextX,nextY) = getPosFromDirection((fromX,fromY), moveDirection)
if( (nextX,nextY) != (fromX,fromY) ):
oppositeDirection = turnDegrees(moveDirection, 180)
saveMove(moveSuccessful, oppositeDirection, (nextX,nextY) ) #save this opposite movement
if( moveSuccessful == False ):
continue
#save this direction in this position for later
lastDirectionDict[(fromX,fromY)] = moveDirection
(fromX,fromY) = getPosFromDirection( (fromX,fromY), moveDirection )
if( (fromX,fromY) != ( get_pos_x(),get_pos_y() ) ):
quick_print("ERROR: position is off!")
getEntity = get_entity_type()
harvest() #this destroys the maze regardless if at treasure or not
return getEntity == Entities.Treasure