Skip to content

Commit

Permalink
refactor: add_influence to add_cost, r to radius, p to position, arr …
Browse files Browse the repository at this point in the history
…to grid
  • Loading branch information
eladyaniv01 committed Aug 19, 2020
1 parent 003dc16 commit 4bfbfee
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 27 deletions.
22 changes: 11 additions & 11 deletions MapAnalyzer/MapData.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def get_pyastar_grid(self, default_weight: int = 1, include_destructables: bool
* :meth:`.MapData.get_climber_grid`
* :meth:`.MapData.get_air_vs_ground_grid`
* :meth:`.MapData.get_clean_air_grid`
* :meth:`.MapData.add_influence`
* :meth:`.MapData.add_cost`
* :meth:`.MapData.pathfind`
"""
Expand Down Expand Up @@ -156,7 +156,7 @@ def get_climber_grid(self, default_weight: int = 1) -> ndarray:
* :meth:`.MapData.get_pyastar_grid`
* :meth:`.MapData.get_air_vs_ground_grid`
* :meth:`.MapData.get_clean_air_grid`
* :meth:`.MapData.add_influence`
* :meth:`.MapData.add_cost`
* :meth:`.MapData.pathfind`
"""
return self.pather.get_climber_grid(default_weight)
Expand All @@ -178,7 +178,7 @@ def get_air_vs_ground_grid(self, default_weight: int = 100):
* :meth:`.MapData.get_pyastar_grid`
* :meth:`.MapData.get_climber_grid`
* :meth:`.MapData.get_clean_air_grid`
* :meth:`.MapData.add_influence`
* :meth:`.MapData.add_cost`
* :meth:`.MapData.pathfind`
"""
Expand All @@ -199,7 +199,7 @@ def pathfind(self, start: Union[Tuple[int, int], Point2], goal: Union[Tuple[int,
grid: Optional[ndarray] = None,
allow_diagonal: bool = False, sensitivity: int = 1) -> Union[List[Point2], None]:
"""
:rtype: Union[List[:class:`.Point2`], None]
:rtype: Union[List[:class:`sc2.position.Point2`], None]
Will return the path with lowest cost (sum) given a weighted array (``grid``), ``start`` , and ``goal``.
**If no** ``grid`` **has been provided**, will request a fresh grid from :mod:`.Pather`
Expand Down Expand Up @@ -239,18 +239,18 @@ def pathfind(self, start: Union[Tuple[int, int], Point2], goal: Union[Tuple[int,
return self.pather.pathfind(start=start, goal=goal, grid=grid, allow_diagonal=allow_diagonal,
sensitivity=sensitivity)

def add_influence(self, p: Tuple[int, int], r: int, arr: ndarray, weight: int = 100, safe: bool = True,
) -> ndarray:
def add_cost(self, position: Tuple[int, int], radius: int, grid: ndarray, weight: int = 100, safe: bool = True,
) -> ndarray:
"""
:rtype: numpy.ndarray
Will add cost to a `circle-shaped` area with a center ``p`` and radius ``r``
Will add cost to a `circle-shaped` area with a center ``position`` and radius ``radius``
weight of 100
Warning:
When ``safe=False`` the Pather will not adjust illegal values below 1 which could result in a crash`
"""
return self.pather.add_influence(p=p, r=r, arr=arr, weight=weight, safe=safe)
return self.pather.add_influence(p=position, r=radius, arr=grid, weight=weight, safe=safe)

"""Utility methods"""

Expand Down Expand Up @@ -283,7 +283,7 @@ def indices_to_points(
indices: Union[ndarray, Tuple[ndarray, ndarray]]
) -> Set[Union[Tuple[int64, int64], Point2]]:
"""
:rtype: Set[Union[Tuple[int64, int64], :class:`.Point2`]]
:rtype: Set[Union[Tuple[int64, int64], :class:`sc2.position.Point2`]]
Convert indices to a set of points(``tuples``, not ``Point2`` )
Will only work when both dimensions are of same length
"""
Expand Down Expand Up @@ -337,7 +337,7 @@ def in_bounds_y(y):
@staticmethod
def distance(p1: Point2, p2: Point2) -> float64:
"""
:rtype: float64
:rtype: numpy.float64
Euclidean distance
"""
return abs(p2[0] - p1[0]) + abs(p2[1] - p1[1])
Expand All @@ -358,7 +358,7 @@ def closest_towards_point(
self, points: List[Point2], target: Union[Point2, tuple]
) -> Point2:
"""
:rtype: :class:`.Point2`
:rtype: :class:`sc2.position.Point2`
Given a list/set of points, and a target,
will return the point that is closest to that target
Expand Down
3 changes: 2 additions & 1 deletion MapAnalyzer/Polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def free_pct(self) -> float:

def update(self) -> None:
parr = self.polygon.map_data.points_to_numpy_array(self.polygon.points)
[self.polygon.map_data.add_influence(p=(unit.position.x, unit.position.y), r=unit.radius, arr=parr, safe=False)
[self.polygon.map_data.add_cost(position=(unit.position.x, unit.position.y), radius=unit.radius, grid=parr,
safe=False)
for unit in
self.polygon.map_data.bot.all_units]
buildable_indices = np.where(parr == 1)
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ getting the basic pathing grid :

Adding influence :
------------------
`def add_influence(p: Tuple[int, int], r: int, arr: ndarray, weight: int = 100) -> ndarray:`
`def add_cost(p: Tuple[int, int], r: int, arr: ndarray, weight: int = 100) -> ndarray:`

Usage:

`map_data.add_influence(p, r, arr, weight)`
`map_data.add_cost(p, r, arr, weight)`

* `p`: center point (for example p could be an enemy units position)
* `r`: radius (for example r -> attack range)
Expand Down Expand Up @@ -257,7 +257,7 @@ for idx in range(8):
r = 7 + idx
# note that we use the default weight of 100, we could pass custom weights for each point though
for p in pts:
arr = map_data.add_influence(p, r, arr, weight=-100)
arr = map_data.add_cost(p, r, arr, weight=-100)

"""Plot path on weighted grid"""
map_data.plot_influenced_path(start=p0, goal=p1, weight_array=arr, name=f"Added {NUM_POINTS} of influence",
Expand Down Expand Up @@ -287,9 +287,9 @@ from typing import List

import sc2
from sc2.player import Bot, Computer
from sc2.position import Point3
from sc2.position import Point3, Point2

from MapAnalyzer import MapData, Point2
from MapAnalyzer import MapData

GREEN = Point3((0, 255, 0))
RED = Point3((255, 0, 0))
Expand Down
6 changes: 3 additions & 3 deletions dummybot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

import sc2
from sc2.player import Bot, Computer
from sc2.position import Point3
from sc2.position import Point3, Point2

from MapAnalyzer import MapData, Point2
from MapAnalyzer import MapData

GREEN = Point3((0, 255, 0))
RED = Point3((255, 0, 0))
Expand Down Expand Up @@ -57,7 +57,7 @@ async def on_start(self):
# for tup in self.influence_points:
# p = tup[0]
# r = tup[1]
# self.map_data.add_influence(p, r=r, arr=self.influence_grid)
# self.map_data.add_cost(p, r=r, arr=self.influence_grid)
self.path = self.map_data.pathfind(start=self.p0, goal=self.p1, grid=self.influence_grid, sensitivity=self.sens,
allow_diagonal=True)
self.hero_tag = self.workers[0].tag
Expand Down
4 changes: 2 additions & 2 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def get_map_file_list() -> List[str]:
# for tup in influence_points:
# p = tup[0]
# r = tup[1]
# map_data.add_influence(p, r=r, arr=influence_grid)
# map_data.add_cost(p, r=r, arr=influence_grid)
map_data.plot_influenced_path(start=p0, goal=p1, weight_array=influence_grid)
map_data.show()
# map_data.plot_map()
Expand Down Expand Up @@ -107,7 +107,7 @@ def get_map_file_list() -> List[str]:
# r = 7 + idx
# # note that we use the default weight of 100, we could pass custom weights for each point though
# for p in pts:
# arr = map_data.add_influence(p, r, arr)
# arr = map_data.add_cost(p, r, arr)
#
# """Plot path on weighted grid"""
# map_data.plot_influenced_path(start=p0, goal=p1, weight_array=arr,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_pathihng.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_handle_illegal_values(self, map_data: MapData) -> None:

arr = map_data.get_pyastar_grid()
for p in pts:
arr = map_data.add_influence(p, r, arr)
arr = map_data.add_cost(p, r, arr)
path = map_data.pathfind(p0, p1, grid=arr)
assert (path is not None), f"path = {path}"

Expand Down Expand Up @@ -138,6 +138,6 @@ def test_pathing_influence(self, map_data: MapData, caplog: LogCaptureFixture) -

arr = map_data.get_pyastar_grid()
for p in pts:
arr = map_data.add_influence(p, r, arr)
arr = map_data.add_cost(p, r, arr)
path = map_data.pathfind(p0, p1, grid=arr)
assert (path is not None)
6 changes: 3 additions & 3 deletions tmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def get_map_file_list() -> List[str]:

arr = map_data.get_pyastar_grid()
for p in pts:
arr = map_data.add_influence(p, r, arr)
arr = map_data.add_cost(p, r, arr)
path = map_data.pathfind(p0, p1, grid=arr)
map_data.plot_influenced_path(start=p0, goal=p1, weight_array=arr)
map_data.show()
Expand All @@ -69,12 +69,12 @@ def get_map_file_list() -> List[str]:
# start = (110, 95)
# goal = (110, 40)
# grid = map_data.get_pyastar_grid()
# grid = map_data.add_influence((170, 140), r=20, arr=grid, weight=np.inf)
# grid = map_data.add_cost((170, 140), r=20, arr=grid, weight=np.inf)
# resource_blockers = [Point2(m.position) for m in map_data.mineral_fields if "rich" in m.name.lower()]
# for pos in resource_blockers:
# radius = 1
# map_data.log(pos)
# grid = map_data.add_influence(p=pos, r=radius, arr=grid, weight=np.inf)
# grid = map_data.add_cost(p=pos, r=radius, arr=grid, weight=np.inf)
# path = map_data.pathfind(start=start, goal=goal, grid=grid)
#
# map_data.plot_influenced_path(start=start, goal=goal, weight_array=grid)
Expand Down

0 comments on commit 4bfbfee

Please sign in to comment.