From 965623db3dfc48985949d88c146974c579d771d2 Mon Sep 17 00:00:00 2001 From: alanwatsonforster <68709385+alanwatsonforster@users.noreply.github.com> Date: Sat, 23 Sep 2023 08:30:14 -0600 Subject: [PATCH] Determine when aircraft leave the map. --- airpower/aircraft.py | 44 ++++++++++++++++++++++++++++++-------------- airpower/map.py | 37 +++++++++++++++++++++++-------------- 2 files changed, 53 insertions(+), 28 deletions(-) diff --git a/airpower/aircraft.py b/airpower/aircraft.py index de0546f2..35c1fa77 100644 --- a/airpower/aircraft.py +++ b/airpower/aircraft.py @@ -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) @@ -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 @@ -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)) @@ -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): @@ -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): @@ -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 != "": @@ -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 @@ -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. @@ -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") diff --git a/airpower/map.py b/airpower/map.py index fcbc2f7c..ecc9944c 100644 --- a/airpower/map.py +++ b/airpower/map.py @@ -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) @@ -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): @@ -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() @@ -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. """ @@ -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): @@ -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