Fixed most input validation bugs, and did too many improvements to remember. Did I mention that I am typing these words with my hands?
This commit is contained in:
parent
ee5c4da549
commit
4a398cc2a3
11 changed files with 546 additions and 253 deletions
42
gamemap.py
42
gamemap.py
|
@ -338,7 +338,6 @@ list of lists of tuples."""
|
|||
# if startThing and not startThing.passable:
|
||||
# return -1, [], -1 # meaning you can't get there
|
||||
dist, prev, endPoint = self.dijkstra(x1, y1, loc, closeEnough)
|
||||
#endPoint = self.coordsToInt(x2, y2)
|
||||
numVertex = self.dimensions[0] * self.dimensions[1]
|
||||
if endPoint > -1 and dist[endPoint] < numVertex + 1:
|
||||
pathList = [endPoint]
|
||||
|
@ -356,35 +355,39 @@ list of lists of tuples."""
|
|||
The closeEnough parameter will create a path that lands beside the source if
|
||||
necessary. The loc parameter is an optional locus which will cause the function
|
||||
to return once it finds a point that's in the locus."""
|
||||
# first test to see that the start point is passable
|
||||
startThing = self.getThingAtCoords(x1, y1)
|
||||
startPoint = self.coordsToInt(x1, y1)
|
||||
endPoint = -1 # until one matches the locus, which it might not.
|
||||
numVertex = self.dimensions[0] * self.dimensions[1]
|
||||
dist = [numVertex + 1 for i in range(numVertex)]
|
||||
prev = [-1 for i in range(numVertex)]
|
||||
# validation
|
||||
if loc == None or not self.__validateCoords(x1, y1):
|
||||
# start point is out-of-bounds or there is no destination
|
||||
return dist, prev, endPoint
|
||||
# first test to see that the start point is passable
|
||||
startThing = self.getThingAtCoords(x1, y1)
|
||||
startPoint = self.coordsToInt(x1, y1)
|
||||
dist[startPoint] = 0
|
||||
#if closeEnough:
|
||||
# if startThing and not startThing.passable:
|
||||
# dist[startPoint] = -1 # This is so it doesn't path into a non-passable end point.
|
||||
queue = []
|
||||
heapq.heappush(queue, (dist[startPoint], startPoint))
|
||||
|
||||
while len(queue) > 0:
|
||||
u = heapq.heappop(queue)[1]
|
||||
if loc != None and self.intToCoords(u) in loc:
|
||||
return dist, prev, u
|
||||
if endPoint == -1 or dist[u] < dist[endPoint]:
|
||||
endPoint = u
|
||||
#print(f"endPoint: {endPoint}; Reason: in locus")
|
||||
for v in self.mapGraph[u]:
|
||||
thing = self.getThingAtPos(v)
|
||||
if thing and not thing.passable:
|
||||
if closeEnough and self.intToCoords(v) in loc:
|
||||
return dist, prev, u
|
||||
if closeEnough and self.intToCoords(v) in loc and (endPoint == -1 or dist[u] < dist[endPoint]):
|
||||
endPoint = u
|
||||
#print(f"endPoint: {endPoint}; Reason: good enough")
|
||||
else:
|
||||
continue
|
||||
tempDist = dist[u] + 1
|
||||
if tempDist < dist[v]:
|
||||
dist[v] = tempDist
|
||||
if dist[u] != -1:
|
||||
if dist[u] != numVertex + 1:
|
||||
prev[v] = u
|
||||
heapq.heappush(queue, (dist[v], v))
|
||||
|
||||
|
@ -396,6 +399,8 @@ to return once it finds a point that's in the locus."""
|
|||
|
||||
def lineOfSight(self, x1, y1, x2, y2):
|
||||
"""Test for line of signt from one tile to another."""
|
||||
if not (self.__validateCoords(x1, y1) and self.__validateCoords(x2, y2)):
|
||||
return False
|
||||
# Trivial case first:
|
||||
if abs(x1 - x2) <= 1 and abs(y1 - y2) <= 1:
|
||||
return True
|
||||
|
@ -410,10 +415,14 @@ to return once it finds a point that's in the locus."""
|
|||
return True
|
||||
|
||||
def isPassable(self, x, y = -1):
|
||||
pos = x
|
||||
if y == -1:
|
||||
if not self.__validatePos(x):
|
||||
return False
|
||||
pos = x
|
||||
x, y = self.intToCoords(x)
|
||||
else:
|
||||
if not self.__validateCoords(x, y):
|
||||
return False
|
||||
pos = self.coordsToInt(x, y)
|
||||
if self.mapMatrix[y][x][0] == 'w':
|
||||
return False
|
||||
|
@ -422,6 +431,12 @@ to return once it finds a point that's in the locus."""
|
|||
if not thing.passable:
|
||||
return False
|
||||
return True
|
||||
|
||||
def __validateCoords(self, x: int, y: int) -> bool:
|
||||
return x >= 0 and x < self.dimensions[0] and y >= 0 and y < self.dimensions[1]
|
||||
|
||||
def __validatePos(self, pos: int) -> bool:
|
||||
return pos >= 0 and pos < self.dimensions[0] * self.dimensions[1]
|
||||
|
||||
@staticmethod
|
||||
def __coordsToInt(x, y, width):
|
||||
|
@ -633,3 +648,6 @@ class LoSLocus(_gl.Locus):
|
|||
ret.append(point)
|
||||
return iter(ret)
|
||||
|
||||
# |\_/|
|
||||
# /0 0\
|
||||
# \o/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue