From b0e8adcb9da6285d93f6dc09f86962299de89e18 Mon Sep 17 00:00:00 2001 From: Oliver Smith Date: Mon, 3 Nov 2014 16:38:11 -0800 Subject: [PATCH] Partial progress converting nav command (and using genSystemsInRange) --- commands/__init__.py | 3 ++- commands/nav_cmd.py | 58 +++++++++++++++++++++++--------------------- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/commands/__init__.py b/commands/__init__.py index 800eda0e..f2001c52 100644 --- a/commands/__init__.py +++ b/commands/__init__.py @@ -10,7 +10,8 @@ commandList = [ 'buildcache', 'buy', - 'local' + 'local', + 'nav' ] ###################################################################### diff --git a/commands/nav_cmd.py b/commands/nav_cmd.py index 44e5add7..63a68524 100644 --- a/commands/nav_cmd.py +++ b/commands/nav_cmd.py @@ -1,4 +1,5 @@ from commands.parsing import MutuallyExclusiveGroup, ParseArgument +import math ###################################################################### # Parser config @@ -29,6 +30,12 @@ metavar='N.NN', type=float, ), + ParseArgument('--aggressive', + help='Try more aggressively.', + action='count', + dest='aggressiveness', + default=1, + ), ] ###################################################################### @@ -40,26 +47,8 @@ def run(results, cmdenv, tdb): from commands.commandenv import ResultRow - ### TODO: Implement - - return results - -###################################################################### -# Transform result set into output - -def render(results, cmdenv, tdb): - from formatting import RowFormat, ColumnFormat - - ### TODO: Implement - -def navCommand(tdb, cmdenv): - """ - Give player directions A->B - """ - srcSystem = cmdenv.startSystem dstSystem = cmdenv.stopSystem - maxLyPer = cmdenv.maxLyPer or tdb.maxSystemLinkLy cmdenv.DEBUG(0, "Route from {} to {} with max {} ly per jump.", @@ -68,20 +57,24 @@ def navCommand(tdb, cmdenv): openList = { srcSystem: 0.0 } distances = { srcSystem: [ 0.0, None ] } - tdb.buildLinks() - # As long as the open list is not empty, keep iterating. - while openList and not dstSystem in distances: + overshoot = cmdenv.aggressiveness * 4 + while openList: + if dstSystem in distances: + overshoot -= 1 + if overshoot == 0: + break + # Expand the search domain by one jump; grab the list of # nodes that are this many hops out and then clear the list. openNodes, openList = openList, {} + gsir = tdb.genSystemsInRange for (node, startDist) in openNodes.items(): - for (destSys, destDist) in node.links.items(): - if destDist > maxLyPer: - continue + for (destSys, destDistSq) in gsir(node, maxLyPer): + destDist = math.sqrt(destDistSq) dist = startDist + destDist - # If we already have a shorter path, do nothing + # If we aready have a shorter path, do nothing try: distNode = distances[destSys] if distNode[0] <= dist: @@ -121,12 +114,12 @@ def navCommand(tdb, cmdenv): print(titleFormat.format(src=srcSystem.name(), dst=dstSystem.name(), mly=maxLyPer)) if labelFormat: - cmdenv.printHeading(labelFormat.format(act='Action', sys='System', jly='Jump Ly', tly='Total Ly')) + print(labelFormat.format(act='Action', sys='System', jly='Jump Ly', tly='Total Ly')) lastHop, totalLy = None, 0.00 def present(action, system): nonlocal lastHop, totalLy - jumpLy = system.links[lastHop] if lastHop else 0.00 + jumpLy = (distances[system][0] - distances[lastHop][0]) if lastHop else 0.00 totalLy += jumpLy print(stepFormat.format(act=action, sys=system.name(), jly=jumpLy, tly=totalLy)) lastHop = system @@ -136,3 +129,14 @@ def present(action, system): present('Via', viaSys) present('Arrive', dstSystem) + + return results + +###################################################################### +# Transform result set into output + +def render(results, cmdenv, tdb): + from formatting import RowFormat, ColumnFormat + + ### TODO: Implement +