diff --git a/game/theater/landmap.py b/game/theater/landmap.py index 0f7395c8..6e510087 100644 --- a/game/theater/landmap.py +++ b/game/theater/landmap.py @@ -2,8 +2,10 @@ import pickle from typing import Collection, Optional, Tuple import logging +from shapely import geometry + Zone = Collection[Tuple[float, float]] -Landmap = Tuple[Collection[Zone], Collection[Zone], Collection[Zone]] +Landmap = Tuple[Collection[geometry.Polygon], Collection[geometry.Polygon], Collection[geometry.Polygon]] def load_landmap(filename: str) -> Optional[Landmap]: @@ -15,22 +17,9 @@ def load_landmap(filename: str) -> Optional[Landmap]: return None -def poly_contains(x, y, poly): - n = len(poly) - inside = False - xints = 0.0 - p1x, p1y = poly[0] - for i in range(n+1): - p2x, p2y = poly[i % n] - if y > min(p1y, p2y): - if y <= max(p1y, p2y): - if x <= max(p1x, p2x): - if p1y != p2y: - xints = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x - if p1x == p2x or x <= xints: - inside = not inside - p1x, p1y = p2x, p2y - return inside +def poly_contains(x, y, poly:geometry.Polygon): + return poly.contains(geometry.Point(x, y)) + def poly_centroid(poly) -> Tuple[float, float]: x_list = [vertex[0] for vertex in poly] diff --git a/mypy.ini b/mypy.ini index 045a50e6..e397c985 100644 --- a/mypy.ini +++ b/mypy.ini @@ -9,4 +9,7 @@ ignore_missing_imports = True ignore_missing_imports = True [mypy-winreg.*] +ignore_missing_imports = True + +[mypy-shapely.*] ignore_missing_imports = True \ No newline at end of file diff --git a/qt_ui/displayoptions.py b/qt_ui/displayoptions.py index 28a9ae4d..55dcb10b 100644 --- a/qt_ui/displayoptions.py +++ b/qt_ui/displayoptions.py @@ -20,8 +20,9 @@ class DisplayRule: def value(self, value: bool) -> None: from qt_ui.widgets.map.QLiberationMap import QLiberationMap self._value = value - QLiberationMap.instance.reload_scene() - QLiberationMap.instance.update() + if QLiberationMap.instance is not None: + QLiberationMap.instance.reload_scene() + QLiberationMap.instance.update() def __bool__(self) -> bool: return self.value diff --git a/qt_ui/widgets/map/QLiberationMap.py b/qt_ui/widgets/map/QLiberationMap.py index d6742f68..8b49077c 100644 --- a/qt_ui/widgets/map/QLiberationMap.py +++ b/qt_ui/widgets/map/QLiberationMap.py @@ -744,7 +744,7 @@ class QLiberationMap(QGraphicsView): for sea_zone in self.game.theater.landmap[2]: print(sea_zone) - poly = QPolygonF([QPointF(*self._transform_point(Point(point[0], point[1]))) for point in sea_zone]) + poly = QPolygonF([QPointF(*self._transform_point(Point(point[0], point[1]))) for point in sea_zone.exterior.coords]) if self.reference_point_setup_mode: color = "sea_blue_transparent" else: @@ -752,14 +752,14 @@ class QLiberationMap(QGraphicsView): scene.addPolygon(poly, CONST.COLORS[color], CONST.COLORS[color]) for inclusion_zone in self.game.theater.landmap[0]: - poly = QPolygonF([QPointF(*self._transform_point(Point(point[0], point[1]))) for point in inclusion_zone]) + poly = QPolygonF([QPointF(*self._transform_point(Point(point[0], point[1]))) for point in inclusion_zone.exterior.coords]) if self.reference_point_setup_mode: scene.addPolygon(poly, CONST.COLORS["grey_transparent"], CONST.COLORS["dark_grey_transparent"]) else: scene.addPolygon(poly, CONST.COLORS["grey"], CONST.COLORS["dark_grey"]) for exclusion_zone in self.game.theater.landmap[1]: - poly = QPolygonF([QPointF(*self._transform_point(Point(point[0], point[1]))) for point in exclusion_zone]) + poly = QPolygonF([QPointF(*self._transform_point(Point(point[0], point[1]))) for point in exclusion_zone.exterior.coords]) if self.reference_point_setup_mode: scene.addPolygon(poly, CONST.COLORS["grey_transparent"], CONST.COLORS["dark_dark_grey_transparent"]) else: diff --git a/requirements.txt b/requirements.txt index 41233a08..1bedb462 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,4 @@ tabulate~=0.8.7 mypy==0.782 mypy-extensions==0.4.3 jinja2>=2.11.2 +shapely==1.7.1 diff --git a/resources/caulandmap.p b/resources/caulandmap.p index b7a3ecf3..a65459cd 100644 Binary files a/resources/caulandmap.p and b/resources/caulandmap.p differ diff --git a/resources/channellandmap.p b/resources/channellandmap.p index fbbb6e1e..9f5ba1f8 100644 Binary files a/resources/channellandmap.p and b/resources/channellandmap.p differ diff --git a/resources/gulflandmap.p b/resources/gulflandmap.p index 83a4d573..3e2e566e 100644 Binary files a/resources/gulflandmap.p and b/resources/gulflandmap.p differ diff --git a/resources/nevlandmap.p b/resources/nevlandmap.p index 1086ba36..1b92631a 100644 Binary files a/resources/nevlandmap.p and b/resources/nevlandmap.p differ diff --git a/resources/normandylandmap.p b/resources/normandylandmap.p index ab08b7db..808a2a1e 100644 Binary files a/resources/normandylandmap.p and b/resources/normandylandmap.p differ diff --git a/resources/syrialandmap.p b/resources/syrialandmap.p index 5cbe1025..658394d6 100644 Binary files a/resources/syrialandmap.p and b/resources/syrialandmap.p differ diff --git a/resources/tools/generate_landmap.py b/resources/tools/generate_landmap.py index 1cdf2931..1812ac4b 100644 --- a/resources/tools/generate_landmap.py +++ b/resources/tools/generate_landmap.py @@ -1,6 +1,7 @@ import pickle from dcs.mission import Mission +from shapely import geometry for terrain in ["cau", "nev", "syria", "channel", "normandy", "gulf"]: print("Terrain " + terrain) @@ -15,16 +16,16 @@ for terrain in ["cau", "nev", "syria", "channel", "normandy", "gulf"]: if terrain == "cau" and inclusion_zones: # legacy - exclusion_zones.append(zone) + exclusion_zones.append(geometry.Polygon(zone)) else: if plane_group.units[0].type == "F-15C": - exclusion_zones.append(zone) + exclusion_zones.append(geometry.Polygon(zone)) else: - inclusion_zones.append(zone) + inclusion_zones.append(geometry.Polygon(zone)) for ship_group in m.country("USA").ship_group: zone = [(x.position.x, x.position.y) for x in ship_group.points] - seas_zones.append(zone) + seas_zones.append(geometry.Polygon(zone)) with open("../{}landmap.p".format(terrain), "wb") as f: print(len(inclusion_zones), len(exclusion_zones), len(seas_zones))