Skip to content

Commit

Permalink
feat: add units center mass function
Browse files Browse the repository at this point in the history
  • Loading branch information
raspersc2 committed Mar 14, 2024
1 parent 60ea8e3 commit 88d88e6
Show file tree
Hide file tree
Showing 7 changed files with 1,110 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ jobs:

steps:
- name: Check out repository
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"

Expand Down
1 change: 1 addition & 0 deletions cython_extensions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from cython_extensions.units_utils import (
cy_center,
cy_closest_to,
cy_find_units_center_mass,
cy_distance_to_squared,
cy_in_attack_range,
cy_sorted_by_distance_to,
Expand Down
38 changes: 38 additions & 0 deletions cython_extensions/units_utils.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,44 @@ def cy_closest_to(
"""
...

def cy_find_units_center_mass(
units: Union[Units, list[Unit]], distance: float
) -> Unit:
"""Given some units, find the center mass
Example:
```py
from cython_functions import cy_find_units_center_mass
from sc2.position import Point2
center_mass: Point2
num_units: int
center_mass, num_units = cy_find_units_center_mass(self.units, 10.0)
```
```
47.8 ms ± 674 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
python alternative:
322 ms ± 5.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
```
Parameters
----------
units :
Position to measure distance from.
distance :
Collection of units we want to check.
Returns
-------
Tuple[Point2, int] :
The center mass, and how many units are within `distance`
of the center mass.
"""
...

def cy_in_attack_range(
unit: Unit, units: Union[Units, list[Unit]], bonus_distance: float = 0.0
) -> list[Unit]:
Expand Down
29 changes: 29 additions & 0 deletions cython_extensions/units_utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,35 @@ cpdef object cy_closest_to((float, float) position, object units):

return closest

@boundscheck(False)
@wraparound(False)
cpdef tuple cy_find_units_center_mass(units, double distance):
cdef:
unsigned int max_units_found = 0
(double, double) center_position
double distance_check = distance * distance
list positions = [u.position for u in units]
(double, double) pos_1, pos_2
unsigned int units_found
unsigned int num_units = len(positions)
double dist_sq
Py_ssize_t i, j

for i in range(num_units):
units_found = 0
pos_1 = positions[i]
for j in range(num_units):
pos_2 = positions[j]
dist_sq = cy_distance_to_squared(pos_1, pos_2)
if dist_sq < distance_check:
units_found += 1

if units_found > max_units_found:
max_units_found = units_found
center_position = pos_1

return center_position, max_units_found

@boundscheck(False)
@wraparound(False)
cpdef list cy_in_attack_range(object unit, object units, double bonus_distance = 0.0):
Expand Down
2 changes: 1 addition & 1 deletion notebooks/building_formation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"outputs": [],
"source": [
"# Change path as needed\n",
"MAP_PATH = \"../tests/pickle_data/HardwireAIE.xz\""
"MAP_PATH = \"../tests/pickle_data/GresvanAIE.xz\""
]
},
{
Expand Down
Loading

0 comments on commit 88d88e6

Please sign in to comment.