Moving the mission and mission control in their own file. Adapting the rescue mission for the new shuttle

This commit is contained in:
2023-08-31 17:31:43 +02:00
parent c403da605b
commit 1499e737c1
8 changed files with 450 additions and 246 deletions

161
mission_control/__init__.py Normal file
View File

@@ -0,0 +1,161 @@
from time import sleep
from krpc.services.spacecenter import CargoBayState, DockingPortState
from maneuver_scheduler import ManeuverScheduler
from mission import MissionStatus, MissionType
from lib import get_connexion, get_vessel
class MissionControl:
mission_types = []
def __init__(self):
self.current_mission = None
self.current_orbit_missions = []
self.vessel = NotImplemented
def pick_missions(self, backlog):
current_body_name = self.vessel.orbit.body.name
for i, mission in enumerate(backlog.missions[current_body_name]):
if mission.type in self.mission_types:
if self.current_mission is None:
self.plan_mission(mission)
else:
self.current_orbit_missions.append(mission)
del (backlog.missions[current_body_name][i])
def plan_mission(self, mission):
self.current_mission = mission
planning_duration = 60
ut = ManeuverScheduler.next_free_timeslot(
from_ut=get_connexion().space_center.ut + planning_duration,
duration=planning_duration)
ManeuverScheduler.book_timeslot(ut, self.vessel, duration=planning_duration)
def execute_mission(self, alarm):
if self.current_mission is None:
if self.current_orbit_missions:
self.current_mission = self.current_orbit_missions.pop(0)
else:
return
report = self.current_mission.execute(self)
if report and report.mission_status == MissionStatus.Done:
self.current_mission = None
def get_kerbal(self, name):
for k in self.vessel.crew:
if k.name == "name":
return k
raise LookupError('Kerbal {} not found onboard vessel {}'.format(name, self.vessel.name))
def get_docking_port(self, name):
for p in self.vessel.parts.docking_ports:
for m in p.modules:
if m.name == "ModuleDockingNodeNamed" and m.get_field('portName') == name:
return p
class ShuttleKerbin(MissionControl):
mission_types = [MissionType.orbit_rescue, MissionType.transport]
PISTON_MAX_EXTENSION = 2.4
def __init__(self, vessel_name):
super().__init__()
self.vessel_name = vessel_name
self.mothership = get_vessel(vessel_name)
self.probe = None
for e in self.vessel.parts.engines:
e.active = True
self.current_orbit_missions = []
self.first_cabin = self.mothership.parts.with_title("Mk2 Inline Cockpit")[0]
self.second_cabin = self.mothership.parts.with_title("MK2 Crew Cabin")[0]
self.third_cabin = self.mothership.parts.with_title("MK2 Crew Cabin")[1]
self.docking_ports = self.mothership.parts.docking_ports
@property
def vessel(self):
if self.probe is None:
return self.mothership
else:
return self.probe
def toggle_grappling(self, value=True):
arm = self.get_grappling()
if arm.modules[1].has_event('Arm') and not value \
or arm.modules[1].has_event('Disarm') and value:
arm.modules[1].set_action_by_id('ToggleAction', True)
def get_grappling(self):
if self.probe is None:
raise Exception('Probe is not ready')
return self.probe.parts.with_title("Advanced Grabbing Unit")[0]
def get_probe_port(self):
# return self.probe.parts.with_name('Probe Port')[0]
return self.get_docking_port('Probe Port')
def get_bay_port(self):
#return self.mothership.with_name('Shuttle Bay Port')[0]
return self.get_docking_port('Shuttle Bay Port')
def deliver_probe(self):
bay = self.mothership.cargo_bays[0]
bay.open = True
while bay.state != CargoBayState.open:
sleep(1)
hinge = self.mothership.robotic_hinges[0]
hinge.target_extension = 90
while hinge.current_angle < 90:
sleep(.1)
bay_port = self.get_bay_port()
self.probe = bay_port.undock()
return self.probe
def recover_probe(self):
bay_port = self.mothership.parts.with_name('Shuttle Bay Port')[0]
if bay_port.state != DockingPortState.docked:
raise Exception("Probe is not parked in shuttle")
self.probe = None
hinge = self.mothership.robotic_hinges[0]
hinge.target_extension = 0
while hinge.current_angle > 0:
sleep(.1)
bay = self.mothership.cargo_bays[0]
bay.open = False
def get_part_with_free_seat(self):
crew_size = self.mothership.crew_count
if self.mothership.crew_capacity > 10:
crew_size = crew_size - 1
if crew_size <= 2:
return self.first_cabin
elif crew_size <= 6:
return self.second_cabin
elif crew_size <= 10:
return self.third_cabin
else:
raise Exception('Vessel already has no free sit')
class ComsatKerbin(MissionControl):
mission_types = [MissionType.comsat]
def __init__(self, vessel_name):
super().__init__()
self.vessel_name = vessel_name
self.vessel = get_vessel(vessel_name)
def toggle_engines(self, value=True):
self.vessel.parts.engines[0].active = value