Update central_command.py, command_line.py and seven other files
This commit is contained in:
18
nullptr/missions/__init__.py
Normal file
18
nullptr/missions/__init__.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from nullptr.missions.survey import SurveyMission
|
||||
from nullptr.missions.mine import MiningMission
|
||||
from nullptr.missions.haul import HaulMission
|
||||
from nullptr.missions.travel import TravelMission
|
||||
|
||||
|
||||
def create_mission(mtype, ship, store, api):
|
||||
types = {
|
||||
'survey': SurveyMission,
|
||||
'mine': MiningMission,
|
||||
'haul': HaulMission,
|
||||
'travel': TravelMission
|
||||
}
|
||||
if mtype not in types:
|
||||
logging.warning(f'invalid mission type {mtype}')
|
||||
return
|
||||
m = types[mtype](ship, store, api)
|
||||
return m
|
||||
241
nullptr/missions/base.py
Normal file
241
nullptr/missions/base.py
Normal file
@@ -0,0 +1,241 @@
|
||||
from nullptr.store import Store
|
||||
from nullptr.models.base import Base
|
||||
from nullptr.models.waypoint import Waypoint
|
||||
from nullptr.models.contract import Contract
|
||||
from nullptr.models.system import System
|
||||
from nullptr.models.survey import Survey
|
||||
from nullptr.models.ship import Ship
|
||||
from nullptr.analyzer import Analyzer
|
||||
from time import time
|
||||
from functools import partial
|
||||
import logging
|
||||
from nullptr.util import *
|
||||
|
||||
class MissionError(Exception):
|
||||
pass
|
||||
|
||||
class MissionParam:
|
||||
def __init__(self, cls, required=True, default=None):
|
||||
self.cls = cls
|
||||
self.required = required
|
||||
self.default = default
|
||||
|
||||
def parse(self, val, store):
|
||||
if self.cls == str:
|
||||
return str(val)
|
||||
elif self.cls == int:
|
||||
return int(val)
|
||||
elif issubclass(self.cls, Base):
|
||||
data = store.get(self.cls, val)
|
||||
if data is None:
|
||||
raise ValueError('object not found')
|
||||
return data.symbol
|
||||
else:
|
||||
raise ValueError('unknown param typr')
|
||||
|
||||
class Mission:
|
||||
@classmethod
|
||||
def params(cls):
|
||||
return {
|
||||
|
||||
}
|
||||
|
||||
def __init__(self, ship, store, api):
|
||||
self.ship = ship
|
||||
self.store = store
|
||||
self.api = api
|
||||
self.next_step = 0
|
||||
self.analyzer = Analyzer(self.store)
|
||||
|
||||
def sts(self, nm, v):
|
||||
if issubclass(type(v), Base):
|
||||
v = v.symbol
|
||||
self.ship.set_mission_state(nm, v)
|
||||
|
||||
def rst(self, typ, nm):
|
||||
symbol = self.st(nm)
|
||||
return self.store.get(typ, symbol)
|
||||
|
||||
def st(self, nm):
|
||||
if not nm in self.ship.mission_state:
|
||||
return None
|
||||
return self.ship.mission_state[nm]
|
||||
|
||||
def status(self, nw=None):
|
||||
if nw is None:
|
||||
return self.ship.mission_status
|
||||
else:
|
||||
self.ship.mission_status = nw
|
||||
|
||||
def start_state(self):
|
||||
return 'done'
|
||||
|
||||
def error(self, msg):
|
||||
self.status('error')
|
||||
print(msg)
|
||||
|
||||
def init_state(self):
|
||||
for name, param in self.params().items():
|
||||
if param.required and param.default is None:
|
||||
if not name in self.ship.mission_state:
|
||||
return self.error(f'Param {name} not set')
|
||||
self.status(self.start_state())
|
||||
|
||||
def steps(self):
|
||||
return {
|
||||
|
||||
}
|
||||
|
||||
def step_done(self):
|
||||
logging.info(f'mission finished for {self.ship}')
|
||||
|
||||
def is_waiting(self):
|
||||
return self.next_step > time()
|
||||
|
||||
def is_finished(self):
|
||||
return self.status() in ['done','error']
|
||||
|
||||
def is_ready(self):
|
||||
return not self.is_waiting() and not self.is_finished()
|
||||
|
||||
def step(self):
|
||||
steps = self.steps()
|
||||
if self.status() == 'init':
|
||||
self.init_state()
|
||||
status = self.status()
|
||||
if not status in steps:
|
||||
logging.warning(f"Invalid mission status {status}")
|
||||
self.status('error')
|
||||
return
|
||||
handler, next_step = steps[status]
|
||||
try:
|
||||
result = handler()
|
||||
except Exception as e:
|
||||
logging.error(e, exc_info=True)
|
||||
self.status('error')
|
||||
return
|
||||
if type(next_step) == str:
|
||||
self.status(next_step)
|
||||
elif type(next_step) == dict:
|
||||
if result not in next_step:
|
||||
logging.warning(f'Invalid step result {result}')
|
||||
self.status('error')
|
||||
return
|
||||
else:
|
||||
self.status(next_step[result])
|
||||
print(f'{self.ship} {status} -> {self.status()}')
|
||||
|
||||
class BaseMission(Mission):
|
||||
def step_go_dest(self):
|
||||
destination = self.rst(Waypoint, 'destination')
|
||||
if self.ship.location() == destination:
|
||||
return
|
||||
self.api.navigate(self.ship, destination)
|
||||
self.next_step = self.ship.arrival
|
||||
|
||||
def step_go_site(self):
|
||||
site = self.rst(Waypoint,'site')
|
||||
if self.ship.location() == site:
|
||||
return
|
||||
self.api.navigate(self.ship, site)
|
||||
self.next_step = self.ship.arrival
|
||||
|
||||
def step_unload(self):
|
||||
contract = self.rst(Contract, 'contract')
|
||||
delivery = self.st('delivery')
|
||||
if delivery == 'sell':
|
||||
return self.step_sell(False)
|
||||
typs = self.ship.deliverable_cargo(contract)
|
||||
if len(typs) == 0:
|
||||
return 'done'
|
||||
self.api.deliver(self.ship, typs[0], contract)
|
||||
if len(typs) == 1:
|
||||
return 'done'
|
||||
else:
|
||||
return 'more'
|
||||
|
||||
def step_sell(self, except_resource=True):
|
||||
target = self.st('resource')
|
||||
market = self.store.get('Marketplace', self.ship.location_str)
|
||||
sellables = market.sellable_items(self.ship.cargo.keys())
|
||||
if target in sellables and except_resource:
|
||||
sellables.remove(target)
|
||||
if len(sellables) == 0:
|
||||
return 'done'
|
||||
self.api.sell(self.ship, sellables[0])
|
||||
if len(sellables) == 1:
|
||||
return 'done'
|
||||
else:
|
||||
return 'more'
|
||||
|
||||
def step_load(self):
|
||||
cargo_space = self.ship.cargo_capacity - self.ship.cargo_units
|
||||
resource = self.st('resource')
|
||||
self.api.buy(self.ship, resource, cargo_space)
|
||||
|
||||
def step_travel(self):
|
||||
traject = self.st('traject')
|
||||
if traject is None or traject == []:
|
||||
return 'done'
|
||||
dest = self.store.get(Waypoint, traject[-1])
|
||||
loc = self.ship.location()
|
||||
if dest == loc:
|
||||
self.sts('traject', None)
|
||||
return 'done'
|
||||
hop = traject.pop(0)
|
||||
if len(hop.split('-')) == 3:
|
||||
self.api.navigate(self.ship, hop)
|
||||
self.next_step = self.ship.arrival
|
||||
else:
|
||||
self.api.jump(self.ship, hop)
|
||||
self.next_step = self.ship.cooldown
|
||||
if traject == []:
|
||||
traject= None
|
||||
self.sts('traject', traject)
|
||||
return 'more'
|
||||
|
||||
def step_calculate_traject(self, dest):
|
||||
if type(dest) == str:
|
||||
dest = self.store.get(Waypoint, dest)
|
||||
loc = self.ship.location()
|
||||
loc_sys = self.store.get(System, loc.system())
|
||||
loc_jg = self.analyzer.get_jumpgate(loc_sys)
|
||||
dest_sys = self.store.get(System, dest.system())
|
||||
dest_jg = self.analyzer.get_jumpgate(dest_sys)
|
||||
path = self.analyzer.find_path(loc_sys, dest_sys)
|
||||
result = []
|
||||
if loc.symbol != loc_jg.symbol:
|
||||
result.append(loc_jg.symbol)
|
||||
result += [s.symbol for s in path[1:]]
|
||||
if dest_jg.symbol != dest.symbol:
|
||||
result.append(dest.symbol)
|
||||
self.sts('traject', result)
|
||||
print(result)
|
||||
return result
|
||||
|
||||
def step_dock(self):
|
||||
self.api.dock(self.ship)
|
||||
|
||||
def step_refuel(self):
|
||||
if self.ship.fuel_current < 100:
|
||||
try:
|
||||
self.api.refuel(self.ship)
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
def step_orbit(self):
|
||||
self.api.orbit(self.ship)
|
||||
|
||||
def travel_steps(self, nm, destination, next_step):
|
||||
destination = self.st(destination)
|
||||
calc = partial(self.step_calculate_traject, destination)
|
||||
return {
|
||||
f'travel-{nm}': (self.step_orbit, f'calc-trav-{nm}'),
|
||||
f'calc-trav-{nm}': (calc, f'go-{nm}'),
|
||||
f'go-{nm}': (self.step_travel, {
|
||||
'done': f'dock-{nm}',
|
||||
'more': f'go-{nm}'
|
||||
}),
|
||||
f'dock-{nm}': (self.step_dock, f'refuel-{nm}'),
|
||||
f'refuel-{nm}': (self.step_refuel, next_step)
|
||||
}
|
||||
25
nullptr/missions/haul.py
Normal file
25
nullptr/missions/haul.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from nullptr.missions.base import BaseMission, MissionParam
|
||||
from nullptr.models.waypoint import Waypoint
|
||||
from nullptr.models.survey import Survey
|
||||
from nullptr.models.contract import Contract
|
||||
class HaulMission(BaseMission):
|
||||
def start_state(self):
|
||||
return 'travel-to'
|
||||
|
||||
@classmethod
|
||||
def params(cls):
|
||||
return {
|
||||
'site': MissionParam(Waypoint, True),
|
||||
'resource': MissionParam(str, True),
|
||||
'dest': MissionParam(Waypoint, True),
|
||||
'delivery': MissionParam(str, True, 'deliver'),
|
||||
'contract': MissionParam(Contract, False)
|
||||
}
|
||||
|
||||
def steps(self):
|
||||
return {
|
||||
**self.travel_steps('to', 'site', 'load'),
|
||||
'load': (self.step_load, 'travel-back'),
|
||||
**self.travel_steps('back', 'dest', 'load'),
|
||||
'unload': (self.step_unload, 'travel-to'),
|
||||
}
|
||||
78
nullptr/missions/mine.py
Normal file
78
nullptr/missions/mine.py
Normal file
@@ -0,0 +1,78 @@
|
||||
from nullptr.missions.base import BaseMission, MissionParam
|
||||
from nullptr.models.waypoint import Waypoint
|
||||
from nullptr.models.survey import Survey
|
||||
from nullptr.models.contract import Contract
|
||||
|
||||
class MiningMission(BaseMission):
|
||||
@classmethod
|
||||
def params(cls):
|
||||
return {
|
||||
'site': MissionParam(Waypoint, True),
|
||||
'resource': MissionParam(str, True),
|
||||
'dest': MissionParam(Waypoint, True),
|
||||
'delivery': MissionParam(str, True, 'deliver'),
|
||||
'contract': MissionParam(Contract, False)
|
||||
}
|
||||
|
||||
def start_state(self):
|
||||
return 'travel-to'
|
||||
|
||||
def steps(self):
|
||||
return {
|
||||
**self.travel_steps('to', 'site', 'extract'),
|
||||
'extract': (self.step_extract, {
|
||||
'done': 'dock',
|
||||
'more': 'extract'
|
||||
}),
|
||||
'dock': (self.step_dock, 'sell'),
|
||||
'sell': (self.step_sell, {
|
||||
'more': 'sell',
|
||||
'done': 'orbit',
|
||||
}),
|
||||
'orbit': (self.step_orbit, 'jettison'),
|
||||
'jettison': (self.step_dispose, {
|
||||
'more': 'jettison',
|
||||
'done': 'extract',
|
||||
'full': 'travel-back'
|
||||
}),
|
||||
**self.travel_steps('back', 'dest', 'unload'),
|
||||
'unload': (self.step_unload, {
|
||||
'done': 'travel-to',
|
||||
'more': 'unload'
|
||||
}),
|
||||
}
|
||||
|
||||
def get_survey(self):
|
||||
resource = self.st('resource')
|
||||
site = self.rst(Waypoint,'site')
|
||||
# todo optimize
|
||||
for s in self.store.all(Survey):
|
||||
if resource in s.deposits and site.symbol == s.waypoint():
|
||||
return s
|
||||
return None
|
||||
|
||||
def step_extract(self):
|
||||
survey = self.get_survey()
|
||||
print('using survey:', str(survey))
|
||||
result = self.api.extract(self.ship, survey)
|
||||
symbol = sg(result,'extraction.yield.symbol')
|
||||
units = sg(result,'extraction.yield.units')
|
||||
print('extracted:', units, symbol)
|
||||
self.next_step = self.ship.cooldown
|
||||
if self.ship.cargo_units < self.ship.cargo_capacity:
|
||||
return 'more'
|
||||
else:
|
||||
return 'done'
|
||||
|
||||
def step_dispose(self):
|
||||
contract = self.rst(Contract, 'contract')
|
||||
typs = self.ship.nondeliverable_cargo(contract)
|
||||
if len(typs) > 0:
|
||||
self.api.jettison(self.ship, typs[0])
|
||||
if len(typs) > 1:
|
||||
return 'more'
|
||||
elif self.ship.cargo_units > self.ship.cargo_capacity - 3:
|
||||
return 'full'
|
||||
else:
|
||||
return 'done'
|
||||
|
||||
15
nullptr/missions/survey.py
Normal file
15
nullptr/missions/survey.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from nullptr.missions.base import BaseMission, MissionParam
|
||||
|
||||
class SurveyMission(BaseMission):
|
||||
def start_state(self):
|
||||
return 'survey'
|
||||
|
||||
def steps(self):
|
||||
return {
|
||||
'survey': (self.step_survey, 'survey')
|
||||
}
|
||||
|
||||
def step_survey(self):
|
||||
result = self.api.survey(self.ship)
|
||||
#pprint(result, 2)
|
||||
self.next_step = self.ship.cooldown
|
||||
16
nullptr/missions/travel.py
Normal file
16
nullptr/missions/travel.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from nullptr.missions.base import BaseMission, MissionParam
|
||||
from nullptr.models.waypoint import Waypoint
|
||||
|
||||
class TravelMission(BaseMission):
|
||||
def start_state(self):
|
||||
return 'travel-to'
|
||||
|
||||
@classmethod
|
||||
def params(cls):
|
||||
return {
|
||||
'dest': MissionParam(Waypoint, True)
|
||||
}
|
||||
|
||||
def steps(self):
|
||||
return self.travel_steps('to', 'dest', 'done')
|
||||
|
||||
Reference in New Issue
Block a user