-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes.py
More file actions
238 lines (210 loc) · 9.28 KB
/
Copy pathnodes.py
File metadata and controls
238 lines (210 loc) · 9.28 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
230
231
232
233
234
235
236
237
238
import pygame
from vector import Vector2
from constants import *
from stack import Stack
class Node(object):
def __init__(self, row, column):
self.row, self.column = row, column
self.position = Vector2(column*TILEWIDTH, row*TILEHEIGHT)
self.neighbors = {UP:None, DOWN:None, LEFT:None, RIGHT:None}
self.portalNode = None
self.portalVal = 0
self.homeGuide = False
self.homeEntrance = False
self.spawnNode = False
self.pacmanStart = False
self.fruitStart = False
def render(self, screen):
for n in self.neighbors.keys():
if self.neighbors[n] is not None:
line_start = self.position.asTuple()
line_end = self.neighbors[n].position.asTuple()
pygame.draw.line(screen, WHITE, line_start, line_end, 4)
if not self.homeEntrance and not self.spawnNode:
pygame.draw.circle(screen, RED, self.position.asInt(), 12)
else:
pygame.draw.circle(screen, WHITE, self.position.asInt(), 12)
class NodeGroup(object):
def __init__(self, level):
self.nodeList = []
self.homeList = []
self.level = level
self.grid = None
self.nodeStack = Stack()
self.pathSymbols = ["p", "P"]
self.portalSymbols = ["1", "2"]####
self.nodeSymbols = ["+", "n", "N", "H", "S", "Y", "F"] + self.portalSymbols
self.grid = self.readMazeFile(level)
self.homegrid = self.getHomeArray()
self.createNodeList(self.grid, self.nodeList)
self.createNodeList(self.homegrid, self.homeList)
self.setupPortalNodes()
self.moveHomeNodes()
self.homeList[0].homeEntrance = True
def getHomeArray(self):
return [['0', '0', '+', '0', '0'],
['0', '0', '|', '0', '0'],
['+', '0', '|', '0', '+'],
['+', '-', 'S', '-', '+'],
['+', '0', '0', '0', '+']]
def readMazeFile(self, textfile):
f = open(textfile, "r")
lines = []
for line in f:
temp = line
if '\n' in temp:
temp = temp.rstrip('\n')
if '\r' in temp:
temp = temp.rstrip('\r')
lines.append(temp)
#lines = [line.rstrip('\n') for line in f]
return [line.split(' ') for line in lines]
def createNodeList(self, grid, nodeList):
startNode = self.findFirstNode(grid)
self.nodeStack.push(startNode)
while not self.nodeStack.isEmpty():
node = self.nodeStack.pop()
self.addNode(node, nodeList)
leftNode = self.getPathNode(LEFT, node.row, node.column-1, nodeList, grid)
rightNode = self.getPathNode(RIGHT, node.row, node.column+1, nodeList, grid)
upNode = self.getPathNode(UP, node.row-1, node.column, nodeList, grid)
downNode = self.getPathNode(DOWN, node.row+1, node.column, nodeList, grid)
node.neighbors[LEFT] = leftNode
node.neighbors[RIGHT] = rightNode
node.neighbors[UP] = upNode
node.neighbors[DOWN] = downNode
self.addNodeToStack(leftNode, nodeList)
self.addNodeToStack(rightNode, nodeList)
self.addNodeToStack(upNode, nodeList)
self.addNodeToStack(downNode, nodeList)
def findFirstNode(self, grid):
rows = len(grid)
cols = len(grid[0])
nodeFound = False
for row in range(rows):
for col in range(cols):
if grid[row][col] in self.nodeSymbols:
node = Node(row, col)
if grid[row][col] in self.portalSymbols:
node.portalVal = grid[row][col]
return node
return None
def getNode(self, x, y, nodeList=[]):
for node in nodeList:
if node.position.x == x and node.position.y == y:
return node
return None
def getNodeFromNode(self, node, nodeList):
if node is not None:
for inode in nodeList:
if node.row == inode.row and node.column == inode.column:
return inode
return node
def getPathNode(self, direction, row, col, nodeList, grid):
tempNode = self.followPath(direction, row, col, grid)
return self.getNodeFromNode(tempNode, nodeList)
def addNode(self, node, nodeList):
nodeInList = self.nodeInList(node, nodeList)
if not nodeInList:
nodeList.append(node)
def addNodeToStack(self, node, nodeList):
if node is not None and not self.nodeInList(node, nodeList):
self.nodeStack.push(node)
def nodeInList(self, node, nodeList):
for inode in nodeList:
if node.position.x == inode.position.x and node.position.y == inode.position.y:
return True
return False
def followPath(self, direction, row, col, grid):
rows = len(grid)
columns = len(grid[0])
if direction == LEFT and col >= 0:
return self.pathToFollow(LEFT, row, col, "-", grid)
elif direction == RIGHT and col < columns:
return self.pathToFollow(RIGHT, row, col, "-", grid)
elif direction == UP and row >= 0:
return self.pathToFollow(UP, row, col, "|", grid)
elif direction == DOWN and row < rows:
return self.pathToFollow(DOWN, row, col, "|", grid)
else:
return None
def pathToFollow(self, direction, row, col, path, grid):
tempSymbols = [path]+self.nodeSymbols + self.pathSymbols
if grid[row][col] in tempSymbols:
while grid[row][col] not in self.nodeSymbols:
if direction is LEFT: col -= 1
elif direction is RIGHT: col += 1
elif direction is UP: row -= 1
elif direction is DOWN: row += 1
node = Node(row, col)
if grid[row][col] == "H":
node.homeGuide = True
if grid[row][col] == "S":
node.spawnNode = True
if grid[row][col] == "Y":
node.pacmanStart = True
if grid[row][col] == "F":
node.fruitStart = True
if grid[row][col] in self.portalSymbols:
node.portalVal = grid[row][col]
return node
else:
return None
def setupPortalNodes(self):
portalDict = {}
for i in range(len(self.nodeList)):
if self.nodeList[i].portalVal != 0:
if self.nodeList[i].portalVal not in portalDict.keys():
portalDict[self.nodeList[i].portalVal] = [i]
else:
portalDict[self.nodeList[i].portalVal] += [i]
for key in portalDict.keys():
node1, node2 = portalDict[key]
self.nodeList[node1].portalNode = self.nodeList[node2]
self.nodeList[node2].portalNode = self.nodeList[node1]
def moveHomeNodes(self):
print("Move home nodes")
for node in self.nodeList:
if node.homeGuide:
nodeA = node
break
nodeB = nodeA.neighbors[LEFT]
mid = (nodeA.position + nodeB.position) / 2.0
mid = Vector2(int(mid.x), int(mid.y))
vec = Vector2(self.homeList[0].position.x, self.homeList[0].position.y)
#print("Number of nodes: " + str(len(self.nodeList)))
#print("Number of home nodes: " + str(len(self.homeList)))
for node in self.homeList:
node.position -= vec
node.position += mid
self.addNode(node, self.nodeList)###
#print("Number of nodes: " + str(len(self.nodeList)))
#print("Node A: " + str(nodeA.position) + " <==== " + str(nodeA.neighbors[LEFT].position))
#print("Node B: " + str(nodeB.position) + " ====> " + str(nodeB.neighbors[RIGHT].position))
A = self.getNodeFromNode(nodeA, self.nodeList)
#print(N.position)
B = self.getNodeFromNode(nodeB, self.nodeList)
#print(N.position)
H = self.getNodeFromNode(self.homeList[0], self.nodeList)
#print(N.position)
#print("")
A.neighbors[LEFT] = H
B.neighbors[RIGHT] = H
H.neighbors[RIGHT] = A
H.neighbors[LEFT] = B
#nodeA.neighbors[LEFT] = self.homeList[0]
#nodeB.neighbors[RIGHT] = self.homeList[0]
#self.homeList[0].neighbors[RIGHT] = nodeA
#self.homeList[0].neighbors[LEFT] = nodeB
#print("")
#print("Node A: " + str(nodeA.position) + " <==== " + str(nodeA.neighbors[LEFT].position))
#print("Node B: " + str(nodeB.position) + " ====> " + str(nodeB.neighbors[RIGHT].position))
#print("")
#A = self.getNode(144, 224, self.nodeList)
#print(A.position)
#print(A.neighbors[RIGHT].position)
def render(self, screen):
for node in self.nodeList:
node.render(screen)
for node in self.homeList:
node.render(screen)