Skip to content

Commit

Permalink
Hackfix: Improve ramp code speed
Browse files Browse the repository at this point in the history
  • Loading branch information
Dentosal committed Oct 14, 2018
1 parent cd464cd commit 53540da
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/terran/ramp_wall.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async def on_step(self, iteration):


def main():
sc2.run_game(sc2.maps.get("(2)CatalystLE"), [
sc2.run_game(sc2.maps.get("OdysseyLE"), [
Bot(Race.Terran, RampWallBot()),
Computer(Race.Zerg, Difficulty.Hard)
], realtime=False)
Expand Down
9 changes: 8 additions & 1 deletion sc2/game_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from .position import Point2, Size, Rect
from .pixel_map import PixelMap
from .player import Player
from typing import List, Dict, Set, Tuple, Any, Optional, Union # mypy type checking
from .cache import property_cache_forever

from typing import List, Dict, Set, Tuple, Any, Optional, Union


class Ramp:
Expand Down Expand Up @@ -49,6 +51,11 @@ def upper(self) -> Set[Point2]:
@property
def upper2_for_ramp_wall(self) -> Set[Point2]:
""" Returns the 2 upper ramp points of the main base ramp required for the supply depot and barracks placement properties used in this file. """
if len(self.upper) > 2:
# NOTE: this was way too slow on large ramps
return set() # HACK: makes this work for now
# FIXME: please do

upper2 = sorted(list(self.upper), key=lambda x: x.distance_to(self.bottom_center), reverse=True)
while len(upper2) > 2:
upper2.pop()
Expand Down
5 changes: 5 additions & 0 deletions sc2/position.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ def to2(self) -> "Point2":
def to3(self) -> "Point3":
return Point3((*self, 0))

def distance2_to(self, other: "Point2"):
"""Squared distance to a point."""
assert isinstance(other, Point2)
return (self[0] - other[0])**2 + (self[1] - other[1])**2

def random_on_distance(self, distance):
if isinstance(distance, (tuple, list)): # interval
distance = distance[0] + random.random() * (distance[1] - distance[0])
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
setup(
name = "sc2",
packages = find_packages(),
version = "0.10.5",
version = "0.10.6",
description = "A StarCraft II API Client for Python 3",
license="MIT",
author = "Hannes Karppila",
Expand Down

0 comments on commit 53540da

Please sign in to comment.