mirror of
https://github.com/dcs-retribution/dcs-retribution.git
synced 2025-11-10 15:41:24 +00:00
Fixes an unlikely bug with JTAC laser code allocation (#1477)
* Fixes an unlikely bug with JTAC laser code allocation, allows for future allocation of codes to a/c with TGPs * Fixing typing issues * Changelog
This commit is contained in:
@@ -44,6 +44,7 @@ from .airsupport import AirSupport, JtacInfo
|
||||
from .callsigns import callsign_for_support_unit
|
||||
from .conflictgen import Conflict
|
||||
from .ground_forces.combat_stance import CombatStance
|
||||
from .lasercoderegistry import LaserCodeRegistry
|
||||
from .naming import namegen
|
||||
from .radios import MHz, RadioFrequency, RadioRegistry
|
||||
|
||||
@@ -81,6 +82,7 @@ class GroundConflictGenerator:
|
||||
unit_map: UnitMap,
|
||||
radio_registry: RadioRegistry,
|
||||
air_support: AirSupport,
|
||||
laser_code_registry: LaserCodeRegistry,
|
||||
) -> None:
|
||||
self.mission = mission
|
||||
self.conflict = conflict
|
||||
@@ -92,6 +94,7 @@ class GroundConflictGenerator:
|
||||
self.unit_map = unit_map
|
||||
self.radio_registry = radio_registry
|
||||
self.air_support = air_support
|
||||
self.laser_code_registry = laser_code_registry
|
||||
|
||||
def generate(self) -> None:
|
||||
position = Conflict.frontline_position(
|
||||
@@ -140,7 +143,7 @@ class GroundConflictGenerator:
|
||||
# Add JTAC
|
||||
if self.game.blue.faction.has_jtac:
|
||||
n = "JTAC" + str(self.conflict.blue_cp.id) + str(self.conflict.red_cp.id)
|
||||
code = 1688 - len(self.air_support.jtacs)
|
||||
code: int = self.laser_code_registry.get_next_laser_code()
|
||||
freq = self.radio_registry.alloc_uhf()
|
||||
|
||||
utype = self.game.blue.faction.jtac_unit
|
||||
|
||||
37
gen/lasercoderegistry.py
Normal file
37
gen/lasercoderegistry.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from collections import deque
|
||||
from typing import Iterator
|
||||
|
||||
|
||||
class OutOfLaserCodesError(RuntimeError):
|
||||
def __init__(self) -> None:
|
||||
super().__init__(
|
||||
f"All JTAC laser codes have been allocated. No available codes."
|
||||
)
|
||||
|
||||
|
||||
class LaserCodeRegistry:
|
||||
def __init__(self) -> None:
|
||||
self.allocated_codes: set[int] = set()
|
||||
self.allocator: Iterator[int] = LaserCodeRegistry.__laser_code_generator()
|
||||
|
||||
def get_next_laser_code(self) -> int:
|
||||
try:
|
||||
while (code := next(self.allocator)) in self.allocated_codes:
|
||||
pass
|
||||
self.allocated_codes.add(code)
|
||||
return code
|
||||
except StopIteration:
|
||||
raise OutOfLaserCodesError
|
||||
|
||||
@staticmethod
|
||||
def __laser_code_generator() -> Iterator[int]:
|
||||
# Valid laser codes are as follows
|
||||
# First digit is always 1
|
||||
# Second digit is 5-7
|
||||
# Third and fourth digits are 1 - 8
|
||||
# We iterate backward (reversed()) so that 1687 follows 1688
|
||||
q = deque(int(oct(code)[2:]) + 11 for code in reversed(range(0o1500, 0o2000)))
|
||||
|
||||
# We start with the default of 1688 and wrap around when we reach the end
|
||||
q.rotate(-q.index(1688))
|
||||
return iter(q)
|
||||
Reference in New Issue
Block a user