Skip to content

Commit

Permalink
Merged kfsone/tradedangerous into master
Browse files Browse the repository at this point in the history
  • Loading branch information
maddavo committed Mar 9, 2015
2 parents be1c45a + 8ba08b9 commit f5cd784
Showing 1 changed file with 89 additions and 71 deletions.
160 changes: 89 additions & 71 deletions commands/run_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,79 +334,77 @@ def run(self, route, cr):
sleep(1.5)


def expandForJumps(tdb, cmdenv, origins, jumps, srcName):
def expandForJumps(tdb, cmdenv, calc, origin, jumps, srcName):
"""
Find all the stations you could reach if you made a given
number of jumps away from the origin list.
"""

if not jumps:
stations = [
origin for origin in origins
if isinstance(origin, Station)
]
for origin in origins:
if isinstance(origin, System):
stations.extend(origin.stations)
return list(set(stations))

origSys = set()
for place in origins:
if isinstance(place, Station):
origSys.add(place.system)
elif isinstance(place, System):
origSys.add(place)
assert jumps

maxLyPer = cmdenv.emptyLyPer or cmdenv.maxLyPer
avoidPlaces = cmdenv.avoidPlaces
if cmdenv.debug:
cmdenv.DEBUG0(
"extending {} list {} by {} jumps at {}ly per jump",
srcName,
[sys.dbname for sys in origSys],
jumps,
maxLyPer,
)
cmdenv.DEBUG0(
"expanding {} reach from {} by {} jumps at {}ly per jump",
srcName,
origin.name(),
jumps,
maxLyPer,
)

if srcName == "--to":
tradingList = calc.stationsSelling
elif srcName == "--from":
tradingList = calc.stationsBuying
else:
raise Exception("Unknown src")

stations = set()
origins, avoid = set([origin]), set(place for place in avoidPlaces)

nextJump = set(origSys)
for jump in range(jumps):
if not nextJump:
if not origins:
break
thisJump, nextJump = nextJump, set()
if cmdenv.debug:
cmdenv.DEBUG1(
"Ring {}: {}",
jump,
[sys.dbname for sys in thisJump]
)
cmdenv.DEBUG1(
"Ring {}: {}",
jump,
[sys.dbname for sys in origins]
)
thisJump, origins = origins, set()
for sys in thisJump:
avoid.add(sys)
for stn in sys.stations or []:
if stn.ID not in tradingList:
cmdenv.DEBUG2(
"X {}/{} not in trading list",
stn.system.dbname, stn.dbname,
)
continue
if not checkStationSuitability(cmdenv, calc, stn):
cmdenv.DEBUG2(
"X {}/{} was not suitable",
stn.system.dbname, stn.dbname,
)
continue
cmdenv.DEBUG2(
"- {}/{} meets requirements",
stn.system.dbname, stn.dbname,
)
stations.add(stn)
for dest, dist in tdb.genSystemsInRange(sys, maxLyPer):
if dest not in origSys and dest not in avoidPlaces:
origSys.add(dest)
nextJump.add(dest)
if dest not in avoid:
origins.add(dest)

if cmdenv.debug:
cmdenv.DEBUG0(
"Expanded {} systems: {}",
srcName,
[sys.dbname for sys in origSys]
)
cmdenv.DEBUG0(
"Expanded {} stations: {}",
srcName,
[stn.name() for stn in stations]
)

# Filter down to stations with trade data
origins = []
for sys in origSys:
for stn in sys.stations:
if stn.itemCount and stn not in avoidPlaces:
origins.append(stn)
stations = list(stations)
stations.sort(key=lambda stn: stn.ID)

if cmdenv.debug:
cmdenv.DEBUG0(
"expanded {} stations: {}",
srcName,
[sys.name() for sys in origins]
)

return list(set(origins))
return stations


def checkForEmptyStationList(category, focusPlace, stationList, jumps):
Expand Down Expand Up @@ -453,6 +451,18 @@ def checkAnchorNotInVia(hops, anchorName, place, viaSet):


def checkStationSuitability(cmdenv, calc, station, src=None):
cmdenv.DEBUG2(
"checking {} (ls={}, bm={}, pad={}, mkt={}, shp={}) "
"for {} suitability",
station.name(),
station.lsFromStar,
station.blackMarket,
station.maxPadSize,
station.market,
station.shipyard,
src or "any",
)

if station in cmdenv.avoidPlaces and src != "--from":
if src:
raise CommandLineError(
Expand Down Expand Up @@ -542,6 +552,11 @@ def checkStationSuitability(cmdenv, calc, station, src=None):
def filterStationSet(src, cmdenv, calc, stnList):
if not stnList:
return stnList
cmdenv.DEBUG0(
"filtering {} station list: {}",
src,
",".join(station.name() for station in stnList),
)
filtered = [
place for place in stnList
if isinstance(place, System) or \
Expand All @@ -557,7 +572,14 @@ def filterStationSet(src, cmdenv, calc, stnList):

def checkOrigins(tdb, cmdenv, calc):
if cmdenv.origPlace:
if isinstance(cmdenv.origPlace, System):
if cmdenv.startJumps and cmdenv.startJumps > 0:
cmdenv.origins = expandForJumps(
tdb, cmdenv, calc,
cmdenv.origPlace.system,
cmdenv.startJumps,
"--from"
)
elif isinstance(cmdenv.origPlace, System):
cmdenv.DEBUG0("origPlace: System: {}", cmdenv.origPlace.name())
if not cmdenv.origPlace.stations:
raise CommandLineError(
Expand All @@ -574,12 +596,6 @@ def checkOrigins(tdb, cmdenv, calc):
checkStationSuitability(cmdenv, calc, cmdenv.origPlace, '--from')
cmdenv.origins = [ cmdenv.origPlace ]
cmdenv.startStation = cmdenv.origPlace
cmdenv.origins = expandForJumps(
tdb, cmdenv,
cmdenv.origins,
cmdenv.startJumps,
"--from"
)
checkForEmptyStationList(
"--from", cmdenv.origPlace,
cmdenv.origins, cmdenv.startJumps
Expand Down Expand Up @@ -607,7 +623,14 @@ def checkOrigins(tdb, cmdenv, calc):
def checkDestinations(tdb, cmdenv, calc):
cmdenv.destinations = None
if cmdenv.destPlace:
if isinstance(cmdenv.destPlace, Station):
if cmdenv.endJumps and cmdenv.endJumps > 0:
cmdenv.destinations = expandForJumps(
tdb, cmdenv, calc,
cmdenv.destPlace.system,
cmdenv.endJumps,
"--to"
)
elif isinstance(cmdenv.destPlace, Station):
cmdenv.DEBUG0("destPlace: Station: {}", cmdenv.destPlace.name())
checkStationSuitability(cmdenv, calc, cmdenv.destPlace, '--to')
cmdenv.destinations = [ cmdenv.destPlace ]
Expand All @@ -618,12 +641,6 @@ def checkDestinations(tdb, cmdenv, calc):
for station in cmdenv.destPlace.stations
if checkStationSuitability(cmdenv, calc, station)
]
cmdenv.destinations = expandForJumps(
tdb, cmdenv,
cmdenv.destinations,
cmdenv.endJumps,
"--to"
)
checkForEmptyStationList(
"--to", cmdenv.destPlace,
cmdenv.destinations, cmdenv.endJumps
Expand Down Expand Up @@ -1028,6 +1045,7 @@ def run(results, cmdenv, tdb):
raise NoDataError(
"No routes had reachable trading links at hop #{}".format(hopNo + 1)
)

if not newRoutes:
checkReachability(tdb, cmdenv)
if hopNo > 0:
Expand Down

0 comments on commit f5cd784

Please sign in to comment.