Skip to content

Commit

Permalink
Determine when aircraft leave the map.
Browse files Browse the repository at this point in the history
  • Loading branch information
alanwatsonforster committed Sep 23, 2023
1 parent 5583477 commit 965623d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 28 deletions.
44 changes: 30 additions & 14 deletions airpower/aircraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self, name, hexcode, azimuth, altitude):
self._altitude = altitude
self._altitudecarry = 0
self._destroyed = False
self._leftmap = False

self._saved = []
self._save(0)
Expand All @@ -41,12 +42,12 @@ def __str__(self):
)

def _restore(self, i):
self._x, self._y, self._facing, self._altitude, self._altitudecarry, self._destroyed = self._saved[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._saved[i] = (self._x, self._y, self._facing, self._altitude, self._altitudecarry, self._destroyed, self._leftmap)

def _maxprevturn(self):
return len(self._saved) - 1
Expand All @@ -58,12 +59,15 @@ def _drawaircraft(self, when):
apdraw.drawaircraft(self._x, self._y, self._facing, self._name, self._altitude, when)

def _position(self):
return "%2s %-9s %-3s %2d" % (
apmap.tosheet(self._x, self._y),
aphexcode.fromxy(self._x, self._y),
apazimuth.fromfacing(self._facing),
self._altitude
)
if apmap.isonmap(self._x, self._y):
sheet = apmap.tosheet(self._x, self._y)
hexcode = aphexcode.fromxy(self._x, self._y)
else:
sheet = "--"
hexcode = "----"
azimuth = apazimuth.fromfacing(self._facing)
altitude = self._altitude
return "%2s %-9s %-3s %2d" % (sheet, hexcode, azimuth, altitude)

def _report(self, s):
print("%s: turn %d: %s" % (self._name, self._turn, s))
Expand Down Expand Up @@ -185,7 +189,7 @@ def _C(self, altitudechange):
self._altitude, self._altitudecarry = apaltitude.adjustaltitude(self._altitude, self._altitudecarry, +altitudechange)

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

def _A(self, what):
Expand All @@ -196,8 +200,13 @@ def checkforterraincollision(self):
if self._altitude <= altitudeofterrain:
self._altitude = altitudeofterrain
self._altitudecarry = 0
self._reportfp("collided with terrain at altitude %d." % altitudeofterrain)
self._reportfp("aircraft has collided with terrain at altitude %d." % altitudeofterrain)
self._destroyed = True

def checkforleavingmap(self):
if not apmap.iswithinmap(self._x, self._y):
self._report("aircraft has left the map.")
self._leftmap = True

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

Expand All @@ -219,6 +228,12 @@ def start(self, turn, nfp, actions):
self._save(self._turn)
return

if self._leftmap:
self._report("aircraft has left the map.")
self._report("--- end of turn ---")
self._save(self._turn)
return

self._reportstatus("start")

if actions != "":
Expand Down Expand Up @@ -302,9 +317,10 @@ def next(self, actions):

]

if self._destroyed:
if self._destroyed or self._leftmap:
return


for action in actions.split(","):

self._ifp = self._ifp + 1
Expand Down Expand Up @@ -337,8 +353,8 @@ def next(self, actions):
self._drawflightpath(lastx, lasty)

self.checkforterraincollision()
if self._destroyed:
self._report("aircraft has been destroyed.")
self.checkforleavingmap()
if self._destroyed or self._leftmap:
break

# Execute other elements.
Expand All @@ -361,7 +377,7 @@ def next(self, actions):
assert aphex.isvalidfacing(self._x, self._y, self._facing)
assert apaltitude.isvalidaltitude(self._altitude)

if self._ifp == self._nfp:
if self._ifp == self._nfp or self._destroyed or self._leftmap:

self._reportstatus("end")
self._drawaircraft("end")
Expand Down
37 changes: 23 additions & 14 deletions airpower/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def drawmap():
def _dotsheet(sheet):

"""
Draw dots on all of the hex positions in the specified sheet that are not on
the edge of the map.
Draw dots on all of the hex positions in the specified sheet that are within
the map.
"""

x0, y0 = sheetorigin(sheet)
Expand All @@ -91,7 +91,7 @@ def _dotsheet(sheet):
dy = 0.25
for iy in range(-2, 60):
y = y0 + iy / 2 + dy
if isinsheet(sheet, x, y) and not isonmapedge(x, y):
if iswithinmap(x, y):
apdraw.drawdot(x, y)

def sheetorigin(sheet):
Expand Down Expand Up @@ -143,11 +143,11 @@ def sheets():

return _sheetlist

def isinsheet(sheet, x, y):
def isonsheet(sheet, x, y):

"""
Returns True if the sheet contains the hex coordinate (x, y). Otherwise returns
false. The sheet must be in the map.
Returns True if the hex coordinate (x, y) is on the specified sheet.
Otherwise returns false. The sheet must be in the map.
"""

assert sheet in sheets()
Expand All @@ -156,11 +156,19 @@ def isinsheet(sheet, x, y):

return xmin <= x and x < xmax and ymin <= y and y < ymax

def isonmap(x, y):

def isonmapedge(x, y):
"""
Returns True if the hex coordinate (x, y) is on the map, including its edges.
Otherwise returns false.
"""

return tosheet(x, y) != None

def iswithinmap(x, y):

"""
Returns True if the hex coordinate (x, y) is on the edge of the map.
Returns True if the hex coordinate (x, y) is on the map, excluding its edges.
Otherwise returns false.
"""

Expand All @@ -169,14 +177,15 @@ def isonmapedge(x, y):

d = 0.1
if tosheet(x + d, y) == None:
return True
return False
if tosheet(x - d, y) == None:
return True
return False
if tosheet(x, y + d) == None:
return True
return False
if tosheet(x, y - d) == None:
return True
return False
return False

return True

def tosheet(x, y):

Expand All @@ -186,6 +195,6 @@ def tosheet(x, y):
"""

for sheet in sheets():
if isinsheet(sheet, x, y):
if isonsheet(sheet, x, y):
return sheet
return None

0 comments on commit 965623d

Please sign in to comment.