Correct int/float confusion in Point APIs.

The heading and distance calculations always return floats.
This commit is contained in:
Dan Albert 2021-07-11 14:33:46 -07:00
parent a19a0b6789
commit 6ce02282e7
6 changed files with 23 additions and 19 deletions

View File

@ -1,10 +1,11 @@
import itertools
import logging
import math
import random
import sys
from datetime import date, datetime, timedelta
from enum import Enum
from typing import Any, List, Type, Union
from typing import Any, List, Type, Union, cast
from dcs.action import Coalition
from dcs.mapping import Point
@ -614,7 +615,7 @@ class Game:
# If there is no conflict take the center point between the two nearest opposing bases
if len(zones) == 0:
cpoint = None
min_distance = sys.maxsize
min_distance = math.inf
for cp in self.theater.player_points():
for cp2 in self.theater.enemy_points():
d = cp.position.distance_to_point(cp2.position)
@ -651,7 +652,7 @@ class Game:
self.__culling_zones = zones
def add_destroyed_units(self, data: dict[str, Union[float, str]]) -> None:
pos = Point(data["x"], data["z"])
pos = Point(cast(float, data["x"]), cast(float, data["z"]))
if self.theater.is_on_land(pos):
self.__destroyed_units.append(data)

View File

@ -3,7 +3,7 @@ from __future__ import annotations
import logging
import os
from pathlib import Path
from typing import Iterable, List, Set, TYPE_CHECKING
from typing import Iterable, List, Set, TYPE_CHECKING, cast
from dcs import Mission
from dcs.action import DoScript, DoScriptFile
@ -261,7 +261,7 @@ class Operation:
except KeyError:
continue
pos = Point(d["x"], d["z"])
pos = Point(cast(float, d["x"]), cast(float, d["z"]))
if (
utype is not None
and not cls.game.position_culled(pos)

View File

@ -21,7 +21,7 @@ class MissionTarget:
self.name = name
self.position = position
def distance_to(self, other: MissionTarget) -> int:
def distance_to(self, other: MissionTarget) -> float:
"""Computes the distance to the given mission target."""
return self.position.distance_to_point(other.position)

View File

@ -1,6 +1,7 @@
from __future__ import annotations
import logging
import math
import random
from dataclasses import dataclass
from typing import TYPE_CHECKING, List, Optional, Tuple
@ -655,7 +656,7 @@ class GroundConflictGenerator:
@param group Group for which we should find the nearest ennemy
@param enemy_groups Potential enemy groups
"""
min_distance = 99999999
min_distance = math.inf
target = None
for dcs_group, _ in enemy_groups:
dist = player_group.points[0].position.distance_to_point(
@ -693,7 +694,7 @@ class GroundConflictGenerator:
"""
For artilery group, decide the distance from frontline with the range of the unit
"""
rg = getattr(group.unit_type.dcs_unit_type, "threat_range", 0) - 7500
rg = group.unit_type.dcs_unit_type.threat_range - 7500
if rg > DISTANCE_FROM_FRONTLINE[CombatGroupRole.ARTILLERY][1]:
rg = random.randint(
DISTANCE_FROM_FRONTLINE[CombatGroupRole.ARTILLERY][0],

View File

@ -375,9 +375,9 @@ class ObjectiveFinder:
def _targets_by_range(
self, targets: Iterable[MissionTargetType]
) -> Iterator[MissionTargetType]:
target_ranges: List[Tuple[MissionTargetType, int]] = []
target_ranges: list[tuple[MissionTargetType, float]] = []
for target in targets:
ranges: List[int] = []
ranges: list[float] = []
for cp in self.friendly_control_points():
ranges.append(target.distance_to(cp))
target_ranges.append((target, min(ranges)))
@ -392,7 +392,7 @@ class ObjectiveFinder:
Targets are sorted by their closest proximity to any friendly control
point (airfield or fleet).
"""
targets: List[Tuple[TheaterGroundObject[Any], int]] = []
targets: list[tuple[TheaterGroundObject[Any], float]] = []
# Building objectives are made of several individual TGOs (one per
# building).
found_targets: Set[str] = set()
@ -431,7 +431,7 @@ class ObjectiveFinder:
continue
if ground_object.name in found_targets:
continue
ranges: List[int] = []
ranges: list[float] = []
for friendly_cp in self.friendly_control_points():
ranges.append(ground_object.distance_to(friendly_cp))
targets.append((ground_object, min(ranges)))

View File

@ -1207,10 +1207,12 @@ class FlightPlanBuilder:
target = self.package.target.position
heading = self.package.waypoints.join.heading_between_point(target)
start = target.point_from_heading(heading, -self.doctrine.sweep_distance.meters)
start_pos = target.point_from_heading(
heading, -self.doctrine.sweep_distance.meters
)
builder = WaypointBuilder(flight, self.game, self.is_player)
start, end = builder.sweep(start, target, self.doctrine.ingress_altitude)
start, end = builder.sweep(start_pos, target, self.doctrine.ingress_altitude)
hold = builder.hold(self._hold_point(flight))
@ -1865,23 +1867,23 @@ class FlightPlanBuilder:
return self._retreating_rendezvous_point(attack_transition)
return self._advancing_rendezvous_point(attack_transition)
def _ingress_point(self, heading: int) -> Point:
def _ingress_point(self, heading: float) -> Point:
return self.package.target.position.point_from_heading(
heading - 180 + 15, self.doctrine.ingress_egress_distance.meters
)
def _egress_point(self, heading: int) -> Point:
def _egress_point(self, heading: float) -> Point:
return self.package.target.position.point_from_heading(
heading - 180 - 15, self.doctrine.ingress_egress_distance.meters
)
def _target_heading_to_package_airfield(self) -> int:
def _target_heading_to_package_airfield(self) -> float:
return self._heading_to_package_airfield(self.package.target.position)
def _heading_to_package_airfield(self, point: Point) -> int:
def _heading_to_package_airfield(self, point: Point) -> float:
return self.package_airfield().position.heading_between_point(point)
def _distance_to_package_airfield(self, point: Point) -> int:
def _distance_to_package_airfield(self, point: Point) -> float:
return self.package_airfield().position.distance_to_point(point)
def package_airfield(self) -> ControlPoint: