Skip to content

Commit

Permalink
Check facing.
Browse files Browse the repository at this point in the history
  • Loading branch information
alanwatsonforster committed Sep 22, 2023
1 parent 24a46e1 commit ff2eb7a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
16 changes: 12 additions & 4 deletions airpower/aircraft.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import airpower.draw as apdraw
import airpower.altitude as apaltitude
import airpower.azimuth as apazimuth
import airpower.draw as apdraw
import airpower.hex as aphex
import airpower.hexcode as aphexcode

import math
Expand All @@ -9,13 +10,17 @@ class Aircraft:

def __init__(self, name, hexcode, azimuth, altitude):

x, y = aphexcode.toxy(hexcode)
facing = apazimuth.tofacing(azimuth)

apaltitude._checkaltitude(altitude)
aphex.checkisvalidfacing(x, y, facing)

self._turn = 0
self._name = name

self._x, self._y = aphexcode.toxy(hexcode)
self._facing = apazimuth.tofacing(azimuth)
self._x = x
self._y = y
self._facing = facing
self._altitude = altitude
self._altitudecarry = 0
self._destroyed = False
Expand Down Expand Up @@ -350,6 +355,9 @@ def next(self, actions):
break

assert self._ifp <= self._nfp
aphex.checkiscenteroredge(self._x, self._y)
aphex.checkisvalidfacing(self._x, self._y, self._facing)
apaltitude._checkaltitude(self._altitude)

if self._ifp == self._nfp:

Expand Down
37 changes: 36 additions & 1 deletion airpower/hex.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,43 @@ def checkiscenteroredge(x, y):

"""
Raise a ValueError exception if the point (x,y) in hex coordinates does not
correspond to the center of a hex or to (the center of) the edge of a hex
correspond to the center of a hex or to (the center of) the edge of a hex.
"""

if not iscenteroredge(x, y):
raise ValueError("(%s,%s) is not the center or edge of a hex." % (x,y))

def isvalidfacing(x, y, facing):

"""
Return True if facing is a valid facing at the point (x, y) in hex
coordinates, which must correspond to to the center of a hex or to (the
center of) the edge of a hex.
"""

checkiscenteroredge(x, y)

if iscenter(x, y):
return facing % 30 == 0

if x % 2 == 0.5 and y % 1.0 == 0.25:
return facing % 180 == 120
elif x % 2 == 0.5 and y % 1.0 == 0.75:
return facing % 180 == 60
elif x % 2 == 1.0 and y % 1 == 0.25:
return facing % 180 == 60
elif x % 2 == 1.0 and y % 1.0 == 0.75:
return facing % 180 == 120
else:
return facing % 180 == 0

def checkisvalidfacing(x, y, facing):

"""
Raise a ValueError exception if facing is not a valid facing at the point
(x, y) in hex coordinates, which must correspond to to the center of a hex or
to (the center of) the edge of a hex.
"""

if not isvalidfacing(x, y, facing):
raise ValueError("(%s,%s) is not the center or edge of a hex." % (x,y))

0 comments on commit ff2eb7a

Please sign in to comment.