Skip to content

Commit

Permalink
Merge pull request #133 from gavinp/test/recycler-bounding-box
Browse files Browse the repository at this point in the history
Add test for recycler get_world_bounding_box()
  • Loading branch information
redruin1 authored Jan 6, 2025
2 parents 7bf15e3 + c969783 commit d7252b8
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 36 deletions.
24 changes: 12 additions & 12 deletions draftsman/prototypes/legacy_curved_rail.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
_collision_set_rotation = {}
_collision_set_rotation[Direction.NORTH] = _left_turn
_collision_set_rotation[Direction.NORTHEAST] = _right_turn
_collision_set_rotation[Direction.EAST] = _left_turn.rotate(2)
_collision_set_rotation[Direction.SOUTHEAST] = _right_turn.rotate(2)
_collision_set_rotation[Direction.SOUTH] = _left_turn.rotate(4)
_collision_set_rotation[Direction.SOUTHWEST] = _right_turn.rotate(4)
_collision_set_rotation[Direction.WEST] = _left_turn.rotate(6)
_collision_set_rotation[Direction.NORTHWEST] = _right_turn.rotate(6)
_collision_set_rotation[Direction.EAST] = _left_turn.rotate(4)
_collision_set_rotation[Direction.SOUTHEAST] = _right_turn.rotate(4)
_collision_set_rotation[Direction.SOUTH] = _left_turn.rotate(8)
_collision_set_rotation[Direction.SOUTHWEST] = _right_turn.rotate(8)
_collision_set_rotation[Direction.WEST] = _left_turn.rotate(12)
_collision_set_rotation[Direction.NORTHWEST] = _right_turn.rotate(12)

_left_turn = CollisionSet(
[AABB(0.25, 1.8, 1.75, 3.9), Rectangle((-0.375, -0.7175), 1.4, 5.45, -35)]
Expand All @@ -40,12 +40,12 @@
_collision_set_rotation = {}
_collision_set_rotation[Direction.NORTH] = _left_turn
_collision_set_rotation[Direction.NORTHEAST] = _right_turn
_collision_set_rotation[Direction.EAST] = _left_turn.rotate(2)
_collision_set_rotation[Direction.SOUTHEAST] = _right_turn.rotate(2)
_collision_set_rotation[Direction.SOUTH] = _left_turn.rotate(4)
_collision_set_rotation[Direction.SOUTHWEST] = _right_turn.rotate(4)
_collision_set_rotation[Direction.WEST] = _left_turn.rotate(6)
_collision_set_rotation[Direction.NORTHWEST] = _right_turn.rotate(6)
_collision_set_rotation[Direction.EAST] = _left_turn.rotate(4)
_collision_set_rotation[Direction.SOUTHEAST] = _right_turn.rotate(4)
_collision_set_rotation[Direction.SOUTH] = _left_turn.rotate(8)
_collision_set_rotation[Direction.SOUTHWEST] = _right_turn.rotate(8)
_collision_set_rotation[Direction.WEST] = _left_turn.rotate(12)
_collision_set_rotation[Direction.NORTHWEST] = _right_turn.rotate(12)


class LegacyCurvedRail(DoubleGridAlignedMixin, EightWayDirectionalMixin, Entity):
Expand Down
16 changes: 8 additions & 8 deletions draftsman/prototypes/legacy_straight_rail.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,29 @@
# TODO: currently hardcoded just for straight rail
eps = 0.001
_vertical_collision = CollisionSet([AABB(-0.75, -1.0 + eps, 0.75, 1.0 - eps)])
_horizontal_collision = _vertical_collision.rotate(2)
_horizontal_collision = _vertical_collision.rotate(4)
_diagonal_collision = CollisionSet([Rectangle((-0.5, -0.5), 1.25, 1.40, 45)])
_collision_set_rotation = {}
_collision_set_rotation[Direction.NORTH] = _vertical_collision
_collision_set_rotation[Direction.NORTHEAST] = _diagonal_collision.rotate(2)
_collision_set_rotation[Direction.NORTHEAST] = _diagonal_collision.rotate(4)
_collision_set_rotation[Direction.EAST] = _horizontal_collision
_collision_set_rotation[Direction.SOUTHEAST] = _diagonal_collision.rotate(4)
_collision_set_rotation[Direction.SOUTHEAST] = _diagonal_collision.rotate(8)
_collision_set_rotation[Direction.SOUTH] = _vertical_collision
_collision_set_rotation[Direction.SOUTHWEST] = _diagonal_collision.rotate(-2)
_collision_set_rotation[Direction.SOUTHWEST] = _diagonal_collision.rotate(-4)
_collision_set_rotation[Direction.WEST] = _horizontal_collision
_collision_set_rotation[Direction.NORTHWEST] = _diagonal_collision

eps = 0.001
_vertical_collision = CollisionSet([AABB(-0.75, -1.0 + eps, 0.75, 1.0 - eps)])
_horizontal_collision = _vertical_collision.rotate(2)
_horizontal_collision = _vertical_collision.rotate(4)
_diagonal_collision = CollisionSet([Rectangle((-0.5, -0.5), 1.25, 1.40, 45)])
_collision_set_rotation = {}
_collision_set_rotation[Direction.NORTH] = _vertical_collision
_collision_set_rotation[Direction.NORTHEAST] = _diagonal_collision.rotate(2)
_collision_set_rotation[Direction.NORTHEAST] = _diagonal_collision.rotate(4)
_collision_set_rotation[Direction.EAST] = _horizontal_collision
_collision_set_rotation[Direction.SOUTHEAST] = _diagonal_collision.rotate(4)
_collision_set_rotation[Direction.SOUTHEAST] = _diagonal_collision.rotate(8)
_collision_set_rotation[Direction.SOUTH] = _vertical_collision
_collision_set_rotation[Direction.SOUTHWEST] = _diagonal_collision.rotate(-2)
_collision_set_rotation[Direction.SOUTHWEST] = _diagonal_collision.rotate(-4)
_collision_set_rotation[Direction.WEST] = _horizontal_collision
_collision_set_rotation[Direction.NORTHWEST] = _diagonal_collision

Expand Down
16 changes: 8 additions & 8 deletions draftsman/prototypes/straight_rail.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,29 @@
# TODO: currently hardcoded just for straight rail
eps = 0.001
_vertical_collision = CollisionSet([AABB(-0.75, -1.0 + eps, 0.75, 1.0 - eps)])
_horizontal_collision = _vertical_collision.rotate(2)
_horizontal_collision = _vertical_collision.rotate(4)
_diagonal_collision = CollisionSet([Rectangle((-0.5, -0.5), 1.25, 1.40, 45)])
_collision_set_rotation = {}
_collision_set_rotation[Direction.NORTH] = _vertical_collision
_collision_set_rotation[Direction.NORTHEAST] = _diagonal_collision.rotate(2)
_collision_set_rotation[Direction.NORTHEAST] = _diagonal_collision.rotate(4)
_collision_set_rotation[Direction.EAST] = _horizontal_collision
_collision_set_rotation[Direction.SOUTHEAST] = _diagonal_collision.rotate(4)
_collision_set_rotation[Direction.SOUTHEAST] = _diagonal_collision.rotate(8)
_collision_set_rotation[Direction.SOUTH] = _vertical_collision
_collision_set_rotation[Direction.SOUTHWEST] = _diagonal_collision.rotate(-2)
_collision_set_rotation[Direction.SOUTHWEST] = _diagonal_collision.rotate(-4)
_collision_set_rotation[Direction.WEST] = _horizontal_collision
_collision_set_rotation[Direction.NORTHWEST] = _diagonal_collision

eps = 0.001
_vertical_collision = CollisionSet([AABB(-0.75, -1.0 + eps, 0.75, 1.0 - eps)])
_horizontal_collision = _vertical_collision.rotate(2)
_horizontal_collision = _vertical_collision.rotate(4)
_diagonal_collision = CollisionSet([Rectangle((-0.5, -0.5), 1.25, 1.40, 45)])
_collision_set_rotation = {}
_collision_set_rotation[Direction.NORTH] = _vertical_collision
_collision_set_rotation[Direction.NORTHEAST] = _diagonal_collision.rotate(2)
_collision_set_rotation[Direction.NORTHEAST] = _diagonal_collision.rotate(4)
_collision_set_rotation[Direction.EAST] = _horizontal_collision
_collision_set_rotation[Direction.SOUTHEAST] = _diagonal_collision.rotate(4)
_collision_set_rotation[Direction.SOUTHEAST] = _diagonal_collision.rotate(8)
_collision_set_rotation[Direction.SOUTH] = _vertical_collision
_collision_set_rotation[Direction.SOUTHWEST] = _diagonal_collision.rotate(-2)
_collision_set_rotation[Direction.SOUTHWEST] = _diagonal_collision.rotate(-4)
_collision_set_rotation[Direction.WEST] = _horizontal_collision
_collision_set_rotation[Direction.NORTHWEST] = _diagonal_collision

Expand Down
12 changes: 6 additions & 6 deletions draftsman/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,14 @@ def rotate(self, amt: int) -> "AABB":
:param amt: The amount to rotate, expressed as an increments of 45
degrees.
"""
if amt % 2 != 0:
raise ValueError("Cannot rotate an AABB by 45 degree increments")
if amt % 4 != 0:
raise ValueError("Cannot rotate an AABB by anything other than 90 degree increments")

# TODO: do this routine with a lookup table instead of float math and
# min/max

rot_top_left = rotate_point(self.top_left, math.radians(amt * 45))
rot_bot_right = rotate_point(self.bot_right, math.radians(amt * 45))
rot_top_left = rotate_point(self.top_left, math.radians(amt * 22.5))
rot_bot_right = rotate_point(self.bot_right, math.radians(amt * 22.5))

# top_left = Vector(
# min(rot_top_left.x, rot_bot_right.x), min(rot_top_left.y, rot_bot_right.y)
Expand Down Expand Up @@ -350,10 +350,10 @@ def rotate(self, amt: int) -> "Rectangle":
degrees.
"""
return Rectangle(
rotate_point(self.position, math.radians(amt * 45)),
rotate_point(self.position, math.radians(amt * 22.5)),
self.width,
self.height,
self.angle + amt * 45,
self.angle + amt * 22.5,
)

def __eq__(self, other: "Rectangle") -> bool:
Expand Down
5 changes: 4 additions & 1 deletion test/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ def test_set_tags(self):
with pytest.raises(DataFormatError):
container.tags = "incorrect"

# @pytest.mark.xfail(reason="issue 134 https://github.com/redruin1/factorio-draftsman/issues/134")
def test_get_world_bounding_box(self):
combinator = DeciderCombinator(tile_position=[3, 3], direction=Direction.EAST)
assert combinator.get_world_bounding_box() == AABB(3.65, 2.85, 4.35, 4.15)
assert combinator.get_world_bounding_box() == AABB(3.35, 3.15, 4.65, 3.85)
recycler = new_entity('recycler',tile_position=[3, 3], direction=Direction.EAST)
assert recycler.get_world_bounding_box() == AABB(3,3, 3.3, 6.7, 4.7)

def test_set_name(self):
iron_chest = Container("iron-chest")
Expand Down
2 changes: 1 addition & 1 deletion test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_get_bounding_box(self):

def test_rotate(self):
aabb = utils.AABB(0, 0, 1, 1)
rotated_aabb = aabb.rotate(2)
rotated_aabb = aabb.rotate(4)
assert round(abs(rotated_aabb.top_left[0] - -1), 7) == 0
assert round(abs(rotated_aabb.top_left[1] - 0), 7) == 0
assert round(abs(rotated_aabb.bot_right[0] - 0), 7) == 0
Expand Down

0 comments on commit d7252b8

Please sign in to comment.