Skip to content

Commit

Permalink
Framework to rework error handling in flight.
Browse files Browse the repository at this point in the history
  • Loading branch information
alanwatsonforster committed Oct 11, 2023
1 parent 54b06a9 commit 72bc983
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 85 deletions.
2 changes: 1 addition & 1 deletion airpower/aircraft/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class aircraft:
_startflightpath, _continueflightpath, _drawflightpath

from ._log import \
_log, _logposition, _logevent, _logbreak
_log, _logaction, _logevent, _logbreak

#############################################################################

Expand Down
4 changes: 2 additions & 2 deletions airpower/aircraft/_departedflight.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _dodepartedflight(self, action):
"""

self._log("---")
self._logposition("start", "")
self._logaction("start", "", self.position())

# See rule 6.4 "Abnormal FLight (Stalls and Departures)" and rule 7.7
# "Manuevering Departures".
Expand Down Expand Up @@ -74,7 +74,7 @@ def _dodepartedflight(self, action):
self._altitude, self._altitudecarry = apaltitude.adjustaltitude(self._altitude, self._altitudecarry, -altitudechange)
self._altitudeband = apaltitude.altitudeband(self._altitude)

self._logposition("end", action)
self._logaction("end", action, self.position())
if initialaltitudeband != self._altitudeband:
self._logevent("altitude band changed from %s to %s." % (initialaltitudeband, self._altitudeband))
self.checkforterraincollision()
Expand Down
8 changes: 2 additions & 6 deletions airpower/aircraft/_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
Logging for the aircraft class.
"""

import airpower.azimuth as apazimuth
import airpower.hexcode as aphexcode
import airpower.log as aplog
import airpower.map as apmap
import airpower as ap

def _log(self, s):
Expand All @@ -14,9 +11,8 @@ def _log(self, s):
def _logbreak(self):
aplog.logbreak()

def _logposition(self, s, t):

self._log("%-5s : %-16s : %s" % (s, t, self.position()))
def _logaction(self, s, t, u):
self._log("%-5s : %-16s : %s" % (s, t, u))

def _logevent(self, s):
self._log("%-5s : %s" % ("", s))
154 changes: 80 additions & 74 deletions airpower/aircraft/_normalflight.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,93 +771,99 @@ def doaction(action):
Carry out an action for normal flight.
"""

# Check we have at least one FP remaining.
if self._fp + 1 > self._maxfp:
raise RuntimeError("only %.1f FPs are available." % self._maxfp)
try:

# Determine if this FP is the last FP of the move.
self._lastfp = (self._fp + 2 > self._maxfp)
# Check we have at least one FP remaining.
if self._fp + 1 > self._maxfp:
raise RuntimeError("only %.1f FPs are available." % self._maxfp)

# Determine if this FP is the last FP of the move.
self._lastfp = (self._fp + 2 > self._maxfp)

initialaltitude = self._altitude
initialaltitudeband = self._altitudeband
initialaltitude = self._altitude
initialaltitudeband = self._altitudeband

if doelements(action, "maneuvering departure", False):
if doelements(action, "maneuvering departure", False):

self._maneuveringdeparture = True
self._maneuveringdeparture = True

assert aphex.isvalid(self._x, self._y, facing=self._facing)
assert apaltitude.isvalidaltitude(self._altitude)
assert aphex.isvalid(self._x, self._y, facing=self._facing)
assert apaltitude.isvalidaltitude(self._altitude)

self._logposition("FP %d" % (self._hfp + self._vfp), action)
self._continueflightpath()
self._logaction("end", action, self.position())
self._continueflightpath()

return
return

doelements(action, "turn declaration or bank", False)
doelements(action, "turn declaration or bank", False)

self._horizontal = doelements(action, "H", False)
self._vertical = doelements(action, "C or D", False)
self._horizontal = doelements(action, "H", False)
self._vertical = doelements(action, "C or D", False)

if not self._horizontal and not self._vertical:
raise RuntimeError("%r is not a valid action." % action)
elif self._horizontal and self._vertical:
if not flighttype == "UD" and not flighttype == "LVL":
raise RuntimeError("%r is not a valid action when the flight type is %s." % (action, flighttype))
if not self._horizontal and not self._vertical:
raise RuntimeError("%r is not a valid action." % action)
elif self._horizontal and self._vertical:
if not flighttype == "UD" and not flighttype == "LVL":
raise RuntimeError("%r is not a valid action when the flight type is %s." % (action, flighttype))

self._fp += 1
if self._horizontal:
self._hfp += 1
elif self._hfp < self._mininitialhfp:
raise RuntimeError("insufficient initial HFPs.")
else:
self._vfp += 1
self._fp += 1
if self._horizontal:
self._hfp += 1
elif self._hfp < self._mininitialhfp:
raise RuntimeError("insufficient initial HFPs.")
else:
self._vfp += 1

self._unloaded = (self._flighttype == "UD" and self._vertical)
if self._unloaded:
if self._firstunloadedfp == None:
self._firstunloadedfp = self._hfp
self._lastunloadedfp = self._hfp

# See rule 8.2.2.
if not self._unloaded:
self._turnfp += 1

turn = doelements(action, "turn" , False)
roll = doelements(action, "roll" , False)
slide = doelements(action, "slide", False)
prep = doelements(action, "prep" , False)
if roll and slide:
raise RuntimeError("an aircraft cannot roll and slide on the same FP.")
if turn and (roll or slide):
raise RuntimeError("an aircraft cannot turn and maneuver on the same FP.")
if prep and (roll or slide):
raise RuntimeError("an aircraft cannot maneuver and prepare for a maneuver on the same FP.")
if turn and prep:
raise RuntimeError("an aircraft cannot turn and prepare for a maneuver on the same FP.")

# See rule 13.1.
if prep and not self._horizontal:
raise RuntimeError("an aircraft can only prepare for a maneuver during an HFP.")
# See rule 8.2.2.
if prep and self._unloaded:
raise RuntimeError("an aircraft may not prepare for a maneuver during an unloaded HFP.")

if slide and not self._horizontal:
raise RuntimeError("an aircraft can only slide on a HFP.")

# See rule 13.1.
if prep:
self._prepfp += 1
self._turnrate = None
self._turnfp = 0
elif turn or slide or roll:
self._prepfp = 0
self._turnfp = 0
self._unloaded = (self._flighttype == "UD" and self._vertical)
if self._unloaded:
if self._firstunloadedfp == None:
self._firstunloadedfp = self._hfp
self._lastunloadedfp = self._hfp

# See rule 8.2.2.
if not self._unloaded:
self._turnfp += 1

turn = doelements(action, "turn" , False)
roll = doelements(action, "roll" , False)
slide = doelements(action, "slide", False)
prep = doelements(action, "prep" , False)
if roll and slide:
raise RuntimeError("an aircraft cannot roll and slide on the same FP.")
if turn and (roll or slide):
raise RuntimeError("an aircraft cannot turn and maneuver on the same FP.")
if prep and (roll or slide):
raise RuntimeError("an aircraft cannot maneuver and prepare for a maneuver on the same FP.")
if turn and prep:
raise RuntimeError("an aircraft cannot turn and prepare for a maneuver on the same FP.")

# See rule 13.1.
if prep and not self._horizontal:
raise RuntimeError("an aircraft can only prepare for a maneuver during an HFP.")
# See rule 8.2.2.
if prep and self._unloaded:
raise RuntimeError("an aircraft may not prepare for a maneuver during an unloaded HFP.")

if slide and not self._horizontal:
raise RuntimeError("an aircraft can only slide on a HFP.")

# See rule 13.1.
if prep:
self._prepfp += 1
self._turnrate = None
self._turnfp = 0
elif turn or slide or roll:
self._prepfp = 0
self._turnfp = 0

assert aphex.isvalid(self._x, self._y, facing=self._facing)
assert apaltitude.isvalidaltitude(self._altitude)
assert aphex.isvalid(self._x, self._y, facing=self._facing)
assert apaltitude.isvalidaltitude(self._altitude)

except RuntimeError as e:
self._logaction("FP %d" % self._fp, action, "")
raise e

self._logposition("FP %d" % (self._hfp + self._vfp), action)
self._logaction("FP %d" % self._fp, action, self.position())
self._continueflightpath()

# See rules 7.7 and 8.5.
Expand Down Expand Up @@ -1192,7 +1198,7 @@ def determinerequiredhfpvfpmix():
determinerequiredhfpvfpmix()

self._log("---")
self._logposition("start", "")
self._logaction("start", "", self.position())

self._continuenormalflight(actions)

Expand Down
4 changes: 2 additions & 2 deletions airpower/aircraft/_stalledflight.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def dojettison(configuration):

self._log("- carrying %+.2f APs." % self._apcarry)
self._log("---")
self._logposition("start", "")
self._logaction("start", "", self.position())

altitudechange = math.ceil(self._speed + self._turnsstalled)

Expand All @@ -57,7 +57,7 @@ def dojettison(configuration):
else:
self._altitudeap = 1.0 * altitudechange

self._logposition("end", action)
self._logaction("end", action, self.position())

if initialaltitudeband != self._altitudeband:
self._logevent("altitude band changed from %s to %s." % (initialaltitudeband, self._altitudeband))
Expand Down

0 comments on commit 72bc983

Please sign in to comment.