Skip to content

Commit

Permalink
feat: Remove buildsables as a concept from polygons
Browse files Browse the repository at this point in the history
The placement grid from burnysc2 isn't necessarily updated anyway so better not rely on this information
  • Loading branch information
spudde123 committed Sep 8, 2023
1 parent e8038b6 commit aedd4de
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 79 deletions.
78 changes: 0 additions & 78 deletions MapAnalyzer/Polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,70 +11,6 @@
from MapAnalyzer import MapData, Region


class Buildables:
"""
Represents the Buildable Points in a :class:`.Polygon`,
"Lazy" class that will only update information when it is needed
Tip:
:class:`.BuildablePoints` that belong to a :class:`.ChokeArea`
are always the edges, this is useful for walling off
"""

def __init__(self, polygon):
self.polygon = polygon
self.points = None

@property
def free_pct(self) -> float:
"""
A simple method for knowing what % of the points
is left available out of the total
"""
if self.points is None:
logger.warning("BuildablePoints needs to update first")
self.update()
return len(self.points) / len(self.polygon.points)

def update(self) -> None:
"""
To be called only by :class:`.Polygon`, this ensures that
updates are done in a lazy fashion,
the update is evaluated only when there is need for the information,
otherwise it is ignored
"""
parr = self.polygon.map_data.points_to_numpy_array(self.polygon.points)
# passing safe false to reduce the warnings,
# which are irrelevant in this case
[
self.polygon.map_data.add_cost(
position=(unit.position.x, unit.position.y),
radius=unit.radius * 0.9,
grid=parr,
safe=False,
)
for unit in self.polygon.map_data.bot.all_units.not_flying
]
buildable_indices = np.where(parr == 1)
buildable_points = []
_points = list(self.polygon.map_data.indices_to_points(buildable_indices))
placement_grid = self.polygon.map_data.placement_arr.T
for p in _points:
if p[0] < placement_grid.shape[0] and p[1] < placement_grid.shape[1]:
if placement_grid[p] == 1:
buildable_points.append(p)
self.points = list(map(Point2, buildable_points))


class Polygon:
"""
Expand All @@ -97,7 +33,6 @@ def __init__(self, map_data: "MapData", array: ndarray) -> None: # pragma: no c
self.is_region = False
self.areas = [] # set by map_data / Region
self.map_data.polygons.append(self)
self._buildables = Buildables(polygon=self)

@property
def top(self):
Expand All @@ -115,19 +50,6 @@ def right(self):
def left(self):
return min(self.points, key=lambda x: x[0])

@property
def buildables(self) -> Buildables:
"""
:rtype: :class:`.BuildablePoints`
Is a responsible for holding and updating the buildable points
of it's respected :class:`.Polygon`
"""
self._buildables.update()
return self._buildables

@property
def regions(self) -> List["Region"]:
"""
Expand Down
1 change: 0 additions & 1 deletion tests/test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ def test_polygon(self, map_data: MapData) -> None:

for point in polygon.corner_points:
assert point in polygon.corner_array
assert polygon.buildables.free_pct is not None

def test_regions(self, map_data: MapData) -> None:
for region in map_data.regions.values():
Expand Down

0 comments on commit aedd4de

Please sign in to comment.