Update commander.py and mission.py

This commit is contained in:
Richard Bronkhorst 2023-06-22 14:56:51 +02:00
parent ddd693a66e
commit 7c3eaa825f
2 changed files with 51 additions and 53 deletions

View File

@ -126,7 +126,7 @@ class Commander(CommandLine):
self.set_mission('mine')
self.centcom.set_mission_param(self.ship, 'site', site)
self.centcom.set_mission_param(self.ship, 'resource', resource)
self.centcom.set_mission_param(self.ship, 'destination', destination)
self.centcom.set_mission_param(self.ship, 'dest', destination)
self.centcom.set_mission_param(self.ship, 'contract', contract.symbol)
self.print_mission()
@ -148,10 +148,16 @@ class Commander(CommandLine):
self.set_mission('haul')
self.centcom.set_mission_param(self.ship, 'site', site.symbol)
self.centcom.set_mission_param(self.ship, 'resource', resource)
self.centcom.set_mission_param(self.ship, 'destination', destination)
self.centcom.set_mission_param(self.ship, 'dest', destination)
self.centcom.set_mission_param(self.ship, 'contract', contract.symbol)
self.print_mission()
def do_travel(self, dest):
dest = self.resolve('Waypoint', dest)
self.set_mission('travel')
self.centcom.set_mission_param(self.ship, 'dest', dest.symbol)
self.print_mission()
def do_register(self, faction):
self.api.register(faction.upper())
site = self.ship.location_str

View File

@ -7,6 +7,7 @@ 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 *
@ -172,16 +173,15 @@ class BaseMission(Mission):
resource = self.st('resource')
self.api.buy(self.ship, resource, cargo_space)
def travel(self, nm):
dest = self.rst(Waypoint, nm)
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'
elif traject is None:
print(f'calculating path to {dest}')
traject = self.calculate_traject(dest)
hop = traject.pop(0)
if len(hop.split('-')) == 3:
self.api.navigate(self.ship, hop)
@ -194,7 +194,9 @@ class BaseMission(Mission):
self.sts('traject', traject)
return 'more'
def calculate_traject(self, dest):
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)
@ -202,47 +204,59 @@ class BaseMission(Mission):
dest_jg = self.analyzer.get_jumpgate(dest_sys)
path = self.analyzer.find_path(loc_sys, dest_sys)
result = []
print(loc.symbol, loc_jg.symbol)
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_travel_site(self):
return self.travel('site')
def step_travel_dest(self):
return self.travel('destination')
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)
}
class MiningMission(BaseMission):
@classmethod
def params(cls):
return {
'site': MissionParam(Waypoint, True),
'resource': MissionParam(str, True),
'destination': MissionParam(Waypoint, True),
'dest': MissionParam(Waypoint, True),
'delivery': MissionParam(str, True, 'deliver'),
'contract': MissionParam(Contract, False)
}
def start_state(self):
return 'go_site'
return 'travel-to'
def steps(self):
return {
**self.travel_steps('to', 'site', 'extract'),
'extract': (self.step_extract, {
'done': 'dock',
'more': 'extract'
@ -256,17 +270,13 @@ class MiningMission(BaseMission):
'jettison': (self.step_dispose, {
'more': 'jettison',
'done': 'extract',
'full': 'go_dest'
'full': 'travel-back'
}),
'go_dest': (self.step_go_dest, 'dock_dest'),
'dock_dest': (self.step_dock, 'unload'),
**self.travel_steps('back', 'dest', 'unload'),
'unload': (self.step_unload, {
'done': 'refuel',
'done': 'travel-to',
'more': 'unload'
}),
'refuel': (self.step_refuel, 'orbit_dest'),
'orbit_dest': (self.step_orbit, 'go_site'),
'go_site': (self.step_go_site, 'extract')
}
def get_survey(self):
@ -319,56 +329,38 @@ class SurveyMission(BaseMission):
class HaulMission(BaseMission):
def start_state(self):
return 'orbit2'
return 'travel-to'
@classmethod
def params(cls):
return {
'site': MissionParam(Waypoint, True),
'resource': MissionParam(str, True),
'destination': MissionParam(Waypoint, True),
'dest': MissionParam(Waypoint, True),
'delivery': MissionParam(str, True, 'deliver'),
'contract': MissionParam(Contract, False)
}
def steps(self):
return {
'travel': (self.step_travel_site, {
'more': 'travel',
'done': 'dock'
}),
'dock': (self.step_dock, 'refuel'),
'refuel': (self.step_unload, 'load'),
'load': (self.step_load, 'orbit'),
'orbit': (self.step_orbit, 'travel_back'),
'travel_back': (self.step_travel_dest, {
'more': 'travel_back',
'done': 'dock2'
}),
'dock2': (self.step_dock, 'unload'),
'unload': (self.step_unload, 'refuel2'),
'refuel2': (self.step_refuel, 'orbit2'),
'orbit2': (self.step_orbit, 'travel')
**self.travel_steps('to', 'site', 'load'),
'load': (self.step_load, 'travel-back'),
**self.travel_steps('back', 'dest', 'load'),
'unload': (self.step_unload, 'travel-to'),
}
class TravelMission(BaseMission):
def start_state(self):
return 'orbit'
return 'travel-to'
@classmethod
def params(cls):
return {
'destination': MissionParam(Waypoint, True)
'dest': MissionParam(Waypoint, True)
}
def steps(self):
return {
'orbit': (self.step_orbit, 'travel'),
'travel': (self.step_travel_dest, {
'more': 'travel',
'done': 'done'
})
}
return self.travel_steps('to', 'dest', 'done')
def create_mission(mtype, ship, store, api):
types = {