From 9055d8b2573f77453896d93596e560963bc0544d Mon Sep 17 00:00:00 2001 From: Patrick Marsee Date: Wed, 6 Feb 2019 12:47:48 -0500 Subject: [PATCH] Added quote support for shell.py, so spaces can be put in single arguments --- gamemap.py | 29 ++++++++++++++++++++--------- shell.py | 31 +++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/gamemap.py b/gamemap.py index e436a98..e438d4a 100644 --- a/gamemap.py +++ b/gamemap.py @@ -583,18 +583,28 @@ list of lists of tuples.""" if len(self.thingPos[oldPos]) == 0: del self.thingPos[oldPos] del self.thingNames[name] - + def path(self, x1, y1, x2, y2, closeEnough = True): - """Uses Dijkstra's Algorithm to find the shortest path from (x1, y1) to (x2, y2) -The closeEnough parameter will create a path that lands beside the source if necessary.""" - # first test to see that the start point is passable startThing = self.getThingAtCoords(x1, y1) if not closeEnough: if startThing and not startThing.passable: return -1, [] # meaning you can't get there - startPoint = self.coordsToInt(x1, y1) + dist, prev = self.dijkstra(x1, y1, closeEnough) endPoint = self.coordsToInt(x2, y2) numVertex = self.dimensions[0] * self.dimensions[1] + if dist[endPoint] < numVertex + 1: + return dist[endPoint], prev + else: + return -1, [] # meaning you can't get there + + def dijkstra(self, x1, y1, closeEnough = True): + """Uses Dijkstra's Algorithm to find the shortest path from (x1, y1) to (x2, y2) +The closeEnough parameter will create a path that lands beside the source if necessary.""" + # first test to see that the start point is passable + startThing = self.getThingAtCoords(x1, y1) + startPoint = self.coordsToInt(x1, y1) + #endPoint = self.coordsToInt(x2, y2) + numVertex = self.dimensions[0] * self.dimensions[1] dist = [numVertex + 1 for i in range(numVertex)] prev = [-1 for i in range(numVertex)] dist[startPoint] = 0 @@ -617,10 +627,11 @@ The closeEnough parameter will create a path that lands beside the source if nec prev[v] = u heapq.heappush(queue, (dist[v], v)) - if dist[endPoint] < numVertex + 1: - return dist[endPoint], prev - else: - return -1, [] # meaning you can't get there + return dist, prev + #if dist[endPoint] < numVertex + 1: + # return dist[endPoint], prev + #else: + # return -1, [] # meaning you can't get there def lineOfSight(self, x1, y1, x2, y2): pass diff --git a/shell.py b/shell.py index df4bcee..1603b31 100644 --- a/shell.py +++ b/shell.py @@ -20,7 +20,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. # -# Ver. 0.1.0028 +# Ver. 0.1.0029 import types as _types @@ -137,6 +137,7 @@ class Shell(object): else: self.handleUnknownCommand(command) self.update() + self.__exit = False def man(self, args): help(self.__commands[args[0]]) @@ -164,10 +165,32 @@ conventionally called args or argv""" # Beyond this point are functions that are called within the main loop. def scanInput(self): """Parses input. Override this for custom input parsing, or input source.""" - ret = input() - if ret == '': + instr = input() + if instr == '': return [] - ret = ret.split() + inQuotes = False + ret = [] + argStart = 0 + c = 0 + while c < len(instr): + if inQuotes: + if instr[c] == '"': + inQuotes = False + ret.append(instr[argStart:c]) + argStart = c+1 + else: + if instr[c] == '"': + inQuotes = True + if argStart != c: + ret.append(instr[argStart:c]) + argStart = c+1 + elif instr[c] in ' \t\n': + if argStart != c: + ret.append(instr[argStart:c]) + argStart = c + 1 + c += 1 + if argStart != c: + ret.append(instr[argStart:c]) a = self.getAlias(ret[0]) if a: ret = a[:] + ret[1:]