Skip to content

Commit

Permalink
Add is_facing function
Browse files Browse the repository at this point in the history
  • Loading branch information
BurnySc2 committed Oct 1, 2019
1 parent e647fe0 commit 3f566e3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
24 changes: 21 additions & 3 deletions examples/terran/ramp_wall.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ async def on_step(self, iteration):
# Raise depos when enemies are nearby
for depo in self.structures(SUPPLYDEPOT).ready:
for unit in self.enemy_units:
if unit.position.distance_to(depo) < 15:
if unit.distance_to(depo) < 15:
break
else:
self.do(depo(MORPH_SUPPLYDEPOT_LOWER))

# Lower depos when no enemies are nearby
for depo in self.structures(SUPPLYDEPOTLOWERED).ready:
for unit in self.enemy_units:
if unit.position.distance_to(depo) < 10:
if unit.distance_to(depo) < 10:
self.do(depo(MORPH_SUPPLYDEPOT_RAISE))
break

Expand Down Expand Up @@ -66,6 +66,9 @@ async def on_step(self, iteration):
# Draw some example boxes around units, lines towards command center, text on the screen and barracks
# self.draw_example()

# Draw if two selected units are facing each other - green if this guy is facing the other, red if he is not
# self.draw_facing_units()

# Filter locations close to finished supply depots
if depots:
depot_placement_positions = {d for d in depot_placement_positions if depots.closest_distance_to(d) > 1}
Expand Down Expand Up @@ -198,6 +201,21 @@ def draw_example(self):
self._client.debug_text_screen(text="Hello world!", pos=Point2((0, 0)), color=None, size=16)
self._client.debug_text_simple(text="Hello world2!")

def draw_facing_units(self):
""" Draws green box on top of selected_unit2, if selected_unit2 is facing selected_unit1 """
selected_unit1: Unit
selected_unit2: Unit
red = Point3((255, 0, 0))
green = Point3((0, 255, 0))
for selected_unit1 in (self.units | self.structures).selected:
for selected_unit2 in self.units.selected:
if selected_unit1 == selected_unit2:
continue
if selected_unit2.is_facing_unit(selected_unit1):
self._client.debug_box2_out(selected_unit2, half_vertex_length=0.25, color=green)
else:
self._client.debug_box2_out(selected_unit2, half_vertex_length=0.25, color=red)


def main():
map = random.choice(
Expand All @@ -217,7 +235,7 @@ def main():
]
)
sc2.run_game(
sc2.maps.get(map), [Bot(Race.Terran, RampWallBot()), Computer(Race.Zerg, Difficulty.Hard)], realtime=False
sc2.maps.get(map), [Bot(Race.Terran, RampWallBot()), Computer(Race.Zerg, Difficulty.Hard)], realtime=True
)


Expand Down
18 changes: 11 additions & 7 deletions sc2/unit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations
import warnings
import math
from typing import Any, Dict, List, Optional, Set, Tuple, Union, TYPE_CHECKING

from .cache import property_immutable_cache, property_mutable_cache
Expand Down Expand Up @@ -449,15 +450,18 @@ def facing(self) -> Union[int, float]:
""" Returns direction the unit is facing as a float in range [0,2π). 0 is in direction of x axis."""
return self._proto.facing

# TODO: a function that checks if this unit is facing another unit
def is_facing_unit(self, other_unit: Unit, angle_error: float = 1e-3) -> bool:
"""
Function not completed yet
def is_facing(self, other_unit: Unit, angle_error: float = 0.05) -> bool:
""" Check if this unit is facing the target unit. If you make angle_error too small, there might be rounding errors. If you make angle_error too big, this function might return false positives.
:param other_unit:
:param angle_error:
"""
pass
:param angle_error: """
angle = math.atan2(
other_unit.position_tuple[1] - self.position_tuple[1], other_unit.position_tuple[0] - self.position_tuple[0]
)
if angle < 0:
angle += math.pi * 2
angle_difference = math.fabs(angle - self.facing)
return angle_difference < angle_error

@property
def radius(self) -> Union[int, float]:
Expand Down

0 comments on commit 3f566e3

Please sign in to comment.