Skip to content

Commit

Permalink
Implement speedbrakes.
Browse files Browse the repository at this point in the history
  • Loading branch information
alanwatsonforster committed Sep 23, 2023
1 parent e6fe0a1 commit 59be64e
Showing 1 changed file with 114 additions and 78 deletions.
192 changes: 114 additions & 78 deletions airpower/aircraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ def __init__(self, name, hexcode, azimuth, altitude):

self._turn = 0
self._name = name

self._x = x
self._y = y
self._facing = facing
self._altitude = altitude
self._altitudecarry = 0
self._ifp = None

self._destroyed = False
self._leftmap = False

Expand All @@ -40,23 +41,20 @@ def __init__(self, name, hexcode, azimuth, altitude):
def __str__(self):
return "[name: %s]" % self._name

def _restore(self, i):
self._x, self._y, self._facing, self._altitude, self._altitudecarry, self._destroyed, self._leftmap = self._saved[i]
##############################################################################

def _save(self, i):
if len(self._saved) == i:
self._saved.append(None)
self._saved[i] = (self._x, self._y, self._facing, self._altitude, self._altitudecarry, self._destroyed, self._leftmap)

def _maxprevturn(self):
return len(self._saved) - 1
# Drawing

def _drawflightpath(self, lastx, lasty):
apdraw.drawflightpath(lastx, lasty, self._x, self._y)

def _drawaircraft(self, when):
apdraw.drawaircraft(self._x, self._y, self._facing, self._name, self._altitude, when)

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

# Reporting

def _formatposition(self):
if apmap.isonmap(self._x, self._y):
sheet = apmap.tosheet(self._x, self._y)
Expand All @@ -72,7 +70,7 @@ def _formatifp(self):
if self._turn == 0:
return ""
else:
return "FP %d of %d" % (self._ifp, self._nfp)
return "FP %d of %.1f" % (self._ifp, self._nfp)

def _report(self, s):
print("%s: turn %-2d : %s" % (self._name, self._turn, s))
Expand All @@ -81,13 +79,13 @@ def _reportbreak(self):
print()

def _reportposition(self):
self._report("%-11s : %-16s : %s" % ("", "", self._formatposition()))
self._report("%-13s : %-16s : %s" % ("", "", self._formatposition()))

def _reportactionsandposition(self, action):
self._report("%-11s : %-16s : %s" % (self._formatifp(), action, self._formatposition()))
self._report("%-13s : %-16s : %s" % (self._formatifp(), action, self._formatposition()))

def _reportevent(self, s):
self._report("%-11s : %s" % (self._formatifp(), s))
self._report("%-13s : %s" % (self._formatifp(), s))

def _reportstartofturn(self):
self._report("--- start of turn %s -- " % self._turn)
Expand All @@ -99,12 +97,13 @@ def _reportendofturn(self):
def _reportstatus(self, when):

if when != "start":
self._reportevent("%d HFPs and %d VFPs used." % (self._ihfp, self._ivfp))
self._reportevent("%d HFPs, %d VFPs, and %.1f SFPs used." % (self._ihfp, self._ivfp, self._sfp))

if when == "start":
self._reportactionsandposition("")

if when != "start":
self._reportevent("FP carry is %.1f." % (self._nfp - self._ifp))
self._reportevent("altitude carry is %s." % apaltitude.formataltitudecarry(self._altitudecarry))

altitudeband = apaltitude.altitudeband(self._altitude)
Expand All @@ -114,6 +113,10 @@ def _reportstatus(self, when):
if altitudeband!= self._initialaltitudeband:
self._report("altitude band changed from %s to %s." % (self._initialaltitudeband, altitudeband))

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

# Elements

def _H(self):
dx = {
0: +1.00,
Expand Down Expand Up @@ -202,13 +205,24 @@ def _D(self, altitudechange):
def _C(self, altitudechange):
self._altitude, self._altitudecarry = apaltitude.adjustaltitude(self._altitude, self._altitudecarry, +altitudechange)

def _S(self, sfp):
if self._sfp != 0:
raise ValueError("speedbrakes can only be used once per turn.")
fpremaining = self._nfp - self._ifp
if sfp > fpremaining:
raise ValueError("attempt to use speedbrakes to eliminate %s FPs but only %s are remaining." % (sfp, fpremaining))
self._sfp = sfp
self._nfp -= sfp

def _K(self):
self._reportevent("aircraft has been killed.")
self._destroyed = True

def _A(self, what):
self._reportevent("attack with %s." % what)

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

def checkforterraincollision(self):
altitudeofterrain = apaltitude.terrainaltitude()
if self._altitude <= altitudeofterrain:
Expand All @@ -222,6 +236,21 @@ def checkforleavingmap(self):
self._reportevent("aircraft has left the map.")
self._leftmap = True

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

# Turn management

def _restore(self, i):
self._x, self._y, self._facing, self._altitude, self._altitudecarry, self._destroyed, self._leftmap = self._saved[i]

def _save(self, i):
if len(self._saved) == i:
self._saved.append(None)
self._saved[i] = (self._x, self._y, self._facing, self._altitude, self._altitudecarry, self._destroyed, self._leftmap)

def _maxprevturn(self):
return len(self._saved) - 1

def start(self, turn, nfp, actions):

if turn > self._maxprevturn() + 1:
Expand All @@ -232,6 +261,7 @@ def start(self, turn, nfp, actions):
self._ifp = 0
self._ihfp = 0
self._ivfp = 0
self._sfp = 0
self._restore(turn - 1)

self._reportstartofturn()
Expand Down Expand Up @@ -265,68 +295,74 @@ def next(self, actions):

["H" , lambda : self._H() , lambda : None],

["C⅛" , lambda : self._C(1/8), lambda : None],
["C1/8", lambda : self._C(1/8), lambda : None],
["C¼" , lambda : self._C(1/4), lambda : None],
["C1/4", lambda : self._C(1/4), lambda : None],
["C⅜" , lambda : self._C(3/8), lambda : None],
["C3/8", lambda : self._C(3/8), lambda : None],
["C½" , lambda : self._C(1/2), lambda : None],
["C1/2", lambda : self._C(1/2), lambda : None],
["C⅝" , lambda : self._C(5/8), lambda : None],
["C5/8", lambda : self._C(5/8), lambda : None],
["C¾" , lambda : self._C(3/4), lambda : None],
["C3/4", lambda : self._C(3/4), lambda : None],
["C⅞" , lambda : self._C(7/8), lambda : None],
["C7/8", lambda : self._C(7/8), lambda : None],
["CC" , lambda : self._C(2) , lambda : None],
["C2" , lambda : self._C(2) , lambda : None],
["C" , lambda : self._C(1) , lambda : None],
["C1" , lambda : self._C(1) , lambda : None],

["D⅛" , lambda : self._D(1/8), lambda : None],
["D1/8", lambda : self._D(1/8), lambda : None],
["D¼" , lambda : self._D(1/4), lambda : None],
["D1/4", lambda : self._D(1/4), lambda : None],
["D⅜" , lambda : self._D(3/8), lambda : None],
["D3/8", lambda : self._D(3/8), lambda : None],
["D½" , lambda : self._D(1/2), lambda : None],
["D1/2", lambda : self._D(1/2), lambda : None],
["D⅝" , lambda : self._D(5/8), lambda : None],
["D5/8", lambda : self._D(5/8), lambda : None],
["D¾" , lambda : self._D(3/4), lambda : None],
["D3/4", lambda : self._D(3/4), lambda : None],
["D⅞" , lambda : self._D(7/8), lambda : None],
["D7/8", lambda : self._D(7/8), lambda : None],
["DDD" , lambda : self._D(3) , lambda : None],
["D3" , lambda : self._D(3) , lambda : None],
["DD" , lambda : self._D(2) , lambda : None],
["D2" , lambda : self._D(2) , lambda : None],
["D" , lambda : self._D(1) , lambda : None],
["D1" , lambda : self._D(1) , lambda : None],

["LLL" , lambda : self._L(90) , lambda : None],
["L90" , lambda : self._L(90) , lambda : None],
["LL" , lambda : self._L(60) , lambda : None],
["L60" , lambda : self._L(60) , lambda : None],
["L" , lambda : self._L(30) , lambda : None],
["L30" , lambda : self._L(30) , lambda : None],

["RRR" , lambda : self._R(90) , lambda : None],
["R90" , lambda : self._R(90) , lambda : None],
["RR" , lambda : self._R(60) , lambda : None],
["R60" , lambda : self._R(60) , lambda : None],
["R" , lambda : self._R(30) , lambda : None],
["R30" , lambda : self._R(30) , lambda : None],

["K" , lambda : None , lambda : self._K()],

["AGN" , lambda : None , lambda : self._A("guns")],
["AGP" , lambda : None , lambda : self._A("gun pod")],
["ARK" , lambda : None , lambda : self._A("rockets")],
["ARP" , lambda : None , lambda : self._A("rocket pods")],

["/" , lambda : None , lambda : None]
["C1/8", lambda : self._C(1/8), lambda: None],
["C1/4", lambda : self._C(1/4), lambda: None],
["C3/8", lambda : self._C(3/8), lambda: None],
["C1/2", lambda : self._C(1/2), lambda: None],
["C5/8", lambda : self._C(5/8), lambda: None],
["C3/4", lambda : self._C(3/4), lambda: None],
["C7/8", lambda : self._C(7/8), lambda: None],
["C⅛" , lambda : self._C(1/8), lambda: None],
["C¼" , lambda : self._C(1/4), lambda: None],
["C⅜" , lambda : self._C(3/8), lambda: None],
["C½" , lambda : self._C(1/2), lambda: None],
["C⅝" , lambda : self._C(5/8), lambda: None],
["C¾" , lambda : self._C(3/4), lambda: None],
["C⅞" , lambda : self._C(7/8), lambda: None],
["C1" , lambda : self._C(1) , lambda: None],
["C2" , lambda : self._C(2) , lambda: None],
["CC" , lambda : self._C(2) , lambda: None],
["C" , lambda : self._C(1) , lambda: None],

["D1/8", lambda : self._D(1/8), lambda: None],
["D1/4", lambda : self._D(1/4), lambda: None],
["D3/8", lambda : self._D(3/8), lambda: None],
["D1/2", lambda : self._D(1/2), lambda: None],
["D5/8", lambda : self._D(5/8), lambda: None],
["D3/4", lambda : self._D(3/4), lambda: None],
["D7/8", lambda : self._D(7/8), lambda: None],
["D⅛" , lambda : self._D(1/8), lambda: None],
["D¼" , lambda : self._D(1/4), lambda: None],
["D⅜" , lambda : self._D(3/8), lambda: None],
["D½" , lambda : self._D(1/2), lambda: None],
["D⅝" , lambda : self._D(5/8), lambda: None],
["D¾" , lambda : self._D(3/4), lambda: None],
["D⅞" , lambda : self._D(7/8), lambda: None],
["D1" , lambda : self._D(1) , lambda: None],
["D2" , lambda : self._D(2) , lambda: None],
["D3" , lambda : self._D(3) , lambda: None],
["DDD" , lambda : self._D(3) , lambda: None],
["DD" , lambda : self._D(2) , lambda: None],
["D" , lambda : self._D(1) , lambda: None],

["L90" , lambda : self._L(90) , lambda: None],
["L60" , lambda : self._L(60) , lambda: None],
["L30" , lambda : self._L(30) , lambda: None],
["LLL" , lambda : self._L(90) , lambda: None],
["LL" , lambda : self._L(60) , lambda: None],
["L" , lambda : self._L(30) , lambda: None],

["R90" , lambda : self._R(90) , lambda: None],
["R60" , lambda : self._R(60) , lambda: None],
["R30" , lambda : self._R(30) , lambda: None],
["RRR" , lambda : self._R(90) , lambda: None],
["RR" , lambda : self._R(60) , lambda: None],
["R" , lambda : self._R(30) , lambda: None],

["S1/2", lambda: self._S(1/2) , lambda: None],
["S½" , lambda: self._S(1/2) , lambda: None],
["S1" , lambda: self._S(1) , lambda: None],
["SS" , lambda: self._S(1) , lambda: None],
["S" , lambda: self._S(1/2) , lambda: None],

["K" , lambda : None , lambda: self._K()],

["AGN" , lambda : None , lambda: self._A("guns")],
["AGP" , lambda : None , lambda: self._A("gun pod")],
["ARK" , lambda : None , lambda: self._A("rockets")],
["ARP" , lambda : None , lambda: self._A("rocket pods")],

["/" , lambda : None , lambda: None]

]

Expand Down Expand Up @@ -388,7 +424,7 @@ def next(self, actions):
assert aphex.isvalidfacing(self._x, self._y, self._facing)
assert apaltitude.isvalidaltitude(self._altitude)

if self._ifp == self._nfp or self._destroyed or self._leftmap:
if self._ifp + 1 > self._nfp or self._destroyed or self._leftmap:

self._reportstatus("end")
self._drawaircraft("end")
Expand Down

0 comments on commit 59be64e

Please sign in to comment.