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

189
main.py
View File

@@ -1,195 +1,12 @@
import sys
import signal
from enum import Enum
from mission_control import ShuttleKerbin, ComsatKerbin
from mission import RescueMission, ComSatNetworkMission
from maneuver_scheduler import ManeuverScheduler
from maneuvers.rendezvous import RendezvousManeuver
from maneuvers.approach import ApproachManeuver
from maneuvers.docking import DockingManeuver
from maneuvers.comsat import ComsatManeuver
from connector import get_connexion
from lib import get_contract, get_vessel, get_rescuee_vessel, get_body
class MissionType(Enum):
transport = 1
rescue = 2
comsat = 3
class MissionStatus(Enum):
Backlog = 1
Assigned = 2
InProgress = 3
Done = 4
class MissionReport:
mission_status = None
new_vessel_id = None
new_missions = []
def __int__(self):
pass
class Mission:
pass
class TransportMission(Mission):
type = MissionType.transport
def __init__(self, kerbal_name, destination):
self.kerbal_name = kerbal_name
self.destination = destination
class RescueMission(Mission):
type = MissionType.rescue
def __init__(self, contract_title):
self.contract_title = contract_title
self.contract = get_contract(contract_title)
rescuee_first_name = contract_title.split()[1]
self.rescuee_vessel = get_rescuee_vessel(rescuee_first_name)
self.rescuee_vessel_name = self.rescuee_vessel.name
self.rescuee_name = self.rescuee_vessel.crew[0].name
def execute(self, mission_control):
conn = get_connexion()
sc = conn.space_center
if sc.active_vessel.name != mission_control.vessel.name:
sc.active_vessel = mission_control.vessel
if sc.target_vessel is None or sc.target_vessel.name != self.rescuee_vessel.name:
sc.target_vessel = self.rescuee_vessel
rendezvous_done = RendezvousManeuver(conn, mission_control).execute()
if rendezvous_done:
reference_frame = sc.ReferenceFrame.create_relative(
self.rescuee_vessel.reference_frame,
rotation=(1., 0., 0., 0.)
)
sc.active_vessel.parts.controlling = mission_control.get_grappling()
approach_done = ApproachManeuver(conn, mission_control, reference_frame).start()
if approach_done:
mission_control.toggle_grappling(True)
docking_done = DockingManeuver(conn,
mission_control,
mission_control.get_grappling(),
reference_frame).start()
# Find new creat member name
# sc.transfer_crew(self.rescuee_name, target_part)
# Release Grapple
# Destroy Capsule
report = MissionReport()
report.mission_status = MissionStatus.Done
report.new_missions.append(
TransportMission(self.rescuee_name, 'kerbin_orbit')
)
return report
class ComSatNetworkMission:
type = MissionType.comsat
def __init__(self, body_name):
self.body = get_body(body_name)
def execute(self, mission_control):
conn = get_connexion()
sc = conn.space_center
if sc.active_vessel.name != mission_control.vessel.name:
sc.active_vessel = mission_control.vessel
mission_control.toggle_engines(True)
comsat_done = ComsatManeuver(conn, mission_control, self.body).execute()
if comsat_done:
report = MissionReport()
report.mission_status = MissionStatus.Done
return report
class MissionControl:
mission_types = []
def __init__(self):
self.current_mission = None
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
class ShuttleKerbin(MissionControl):
mission_types = [MissionType.rescue, MissionType.transport]
PISTON_MAX_EXTENSION = 2.4
def __init__(self, vessel_name):
super().__init__()
self.vessel_name = vessel_name
self.vessel = get_vessel(vessel_name)
for e in self.vessel.parts.engines:
e.active = True
self.current_orbit_missions = []
def toggle_grappling(self, value=True):
piston = self.vessel.parts.robotic_pistons[0]
piston.target_extension = self.PISTON_MAX_EXTENSION if value else 0
arm = piston.part.children[0]
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):
piston = self.vessel.parts.robotic_pistons[0]
return piston.part.children[0]
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
class Backlog: