Skip to content

Commit

Permalink
Fix the BARRIER_EMPTY singleton being mutated
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSpen210 committed May 16, 2024
1 parent a751059 commit d606c34
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
21 changes: 17 additions & 4 deletions src/precomp/barriers.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,27 @@ def parse(cls, kv: Keyvalues, barrier_id: utils.ObjectID) -> BarrierType:
)


def _check_barrier_type_assignment(
self: Barrier,
attr: attrs.Attribute[BarrierType],
new_type: BarrierType,
) -> None:
"""Prevent altering BARRIER_EMPTY."""
try:
if self is BARRIER_EMPTY:
raise ValueError('BARRIER_EMPTY is immutable!')
except NameError:
pass # We're constructing BARRIER_EMPTY.


@attrs.define(eq=False, kw_only=True)
class Barrier:
"""A glass/grating item.
BARRIER_EMPTY is unique, but others may not be if created dynamically!
"""
name: str
type: BarrierType
type: BarrierType = attrs.field(validator=_check_barrier_type_assignment)
item: connections.Item | None = None
instances: List[Entity] = attrs.Factory(list)
# Set only for vanilla glass/grating items. Stores a list of the
Expand Down Expand Up @@ -829,6 +842,7 @@ def parse_map(vmf: VMF, conn_items: Mapping[str, connections.Item]) -> None:
The frames are updated with a fixup var, as appropriate.
Requires connection items to be parsed!
"""
LOGGER.info('Parsing barrier items...')
frame_inst = instanceLocs.resolve_filter('[glass_frames]', silent=True)
segment_inst = instanceLocs.resolve_filter('[glass_128]', silent=True)
barrier_pos_lists: Dict[str, List[Tuple[PlaneKey, int, int]]] = {}
Expand Down Expand Up @@ -905,9 +919,8 @@ def parse_map(vmf: VMF, conn_items: Mapping[str, connections.Item]) -> None:
plane_pos = center + offset * norm
plane = PlaneKey(-norm, plane_pos)
local = plane.world_to_plane(plane_pos)
try:
barrier = BARRIERS[plane][local.x // 32, local.y // 32]
except KeyError:
barrier = BARRIERS[plane][local.x // 32, local.y // 32]
if barrier is BARRIER_EMPTY:
continue # Try other side?
found = True

Expand Down
13 changes: 11 additions & 2 deletions src/test/test_plane.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ def _points(*pattern: str) -> List[Tuple[int, int]]:
(3, 3), (1, 0), (0, 1), (1, 2), (2, 2), (4, 4), (0, 0), (-2, 10), (10, -5), (50, 50),
])
@pytest.mark.parametrize('pattern', [
[(x, y) for x in range(5) for y in range(5)],
[(x, y) for x in range(5) for y in range(5)],
_points(
'0...3',
'.....',
'...1.',
'.....',
'2...4',
),
_points(
'.7..8',
'..6..',
Expand All @@ -76,7 +83,7 @@ def _points(*pattern: str) -> List[Tuple[int, int]]:
'..3..',
'54..0',
),
], ids=['order', 'patA', 'patB', 'patC'])
], ids=['order', 'patA', 'patB', 'patC', 'patD'])
def test_grid_insertion_complex(pattern: List[Tuple[int, int]], off_x: int, off_y: int) -> None:
"""Insert in various patterns, to test the dynamic resizing."""
grid = PlaneGrid[object]()
Expand All @@ -103,6 +110,8 @@ def test_grid_insertion_complex(pattern: List[Tuple[int, int]], off_x: int, off_
assert grid.maxes == (max_x, max_y)
assert grid.dimensions == (max_x - min_x, max_y - min_y)

assert set(grid) == set(backup.keys())
assert set(grid.keys()) == set(backup.keys())
assert dict(grid.items()) == backup
for (chk_x, chk_y), check in backup.items():
assert grid[chk_x, chk_y] == check, backup
Expand Down

0 comments on commit d606c34

Please sign in to comment.