Ruch lotniczy z TCAS (sim/cockpit2/tcas) zamiast sim/multiplayer — zgodność z LiveTraffic
This commit is contained in:
1 parent
36cf23232c
commit
91eec1d95e
1 file changed
+89
-60
+89
-60
@@ -89,8 +89,17 @@ FACILITY_NAMES = {
|
||||
5: "Center",
|
||||
}
|
||||
|
||||
# Ile samolotow ruchu sprawdzac (X-Plane wspiera do 20 samolotow AI/multiplayer)
|
||||
NUM_TRAFFIC_PLANES = 20
|
||||
# Datarefy TCAS (ruch z LiveTraffic / AI / multiplayer)
|
||||
# TCAS daje gotowe: namiar wzgledny, dystans, wysokosc wzgledna.
|
||||
TCAS_BEARING_DATAREF = "sim/cockpit2/tcas/indicators/relative_bearing_degs"
|
||||
TCAS_DISTANCE_DATAREF = "sim/cockpit2/tcas/indicators/relative_distance_mtrs"
|
||||
TCAS_ALTITUDE_DATAREF = "sim/cockpit2/tcas/indicators/relative_altitude_mtrs"
|
||||
TCAS_NUM_ACF_DATAREF = "sim/cockpit2/tcas/indicators/tcas_num_acf"
|
||||
TCAS_FLIGHT_ID_DATAREF = "sim/cockpit2/tcas/targets/flight_id"
|
||||
TCAS_ICAO_TYPE_DATAREF = "sim/cockpit2/tcas/targets/icao_type"
|
||||
|
||||
# Ile slotow TCAS sprawdzac (X-Plane ma tablice float[64])
|
||||
NUM_TCAS_SLOTS = 64
|
||||
|
||||
|
||||
class XSayATC:
|
||||
@@ -153,8 +162,13 @@ class XSayATC:
|
||||
self._tuned1_ref = xp.findDataRef(TUNED1_DATAREF)
|
||||
self._tuned2_ref = xp.findDataRef(TUNED2_DATAREF)
|
||||
self._elevation_ref = xp.findDataRef(ELEVATION_DATAREF)
|
||||
# Datarefy ruchu lotniczego (AI/multiplayer) - leniwie rozwijane
|
||||
self._traffic_refs = {}
|
||||
# Datarefy TCAS (ruch z LiveTraffic / AI / multiplayer)
|
||||
self._tcas_bearing_ref = xp.findDataRef(TCAS_BEARING_DATAREF)
|
||||
self._tcas_distance_ref = xp.findDataRef(TCAS_DISTANCE_DATAREF)
|
||||
self._tcas_altitude_ref = xp.findDataRef(TCAS_ALTITUDE_DATAREF)
|
||||
self._tcas_num_ref = xp.findDataRef(TCAS_NUM_ACF_DATAREF)
|
||||
self._tcas_flightid_ref = xp.findDataRef(TCAS_FLIGHT_ID_DATAREF)
|
||||
self._tcas_icao_ref = xp.findDataRef(TCAS_ICAO_TYPE_DATAREF)
|
||||
self._resolved_datarefs = True
|
||||
|
||||
def _get_dataref(self, name, is_float=True):
|
||||
@@ -243,71 +257,77 @@ class XSayATC:
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def _get_traffic_ref(self, idx, field):
|
||||
"""Zwraca (i cachuje) dataref dla samolotu idx (1-based) i pola field."""
|
||||
key = (idx, field)
|
||||
if key not in self._traffic_refs:
|
||||
self._traffic_refs[key] = xp.findDataRef(
|
||||
f"sim/multiplayer/position/plane{idx}_{field}")
|
||||
return self._traffic_refs.get(key)
|
||||
def _read_float_array(self, ref, count):
|
||||
"""Czyta tablice float z datarefu. Zwraca liste lub []."""
|
||||
if ref is None:
|
||||
return []
|
||||
try:
|
||||
values = []
|
||||
xp.getDatavf(ref, values, 0, count)
|
||||
return values
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
def _read_packed_strings(self, ref, count, strlen=8):
|
||||
"""Czyta byte[512] jako 64 stringow po strlen bajtow (7 znakow + NUL)."""
|
||||
if ref is None:
|
||||
return []
|
||||
try:
|
||||
raw = xp.getDatas(ref)
|
||||
if not raw:
|
||||
return []
|
||||
out = []
|
||||
for i in range(count):
|
||||
chunk = raw[i * strlen:(i + 1) * strlen]
|
||||
s = chunk.split("\x00", 1)[0].strip()
|
||||
out.append(s)
|
||||
return out
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
def _collect_traffic(self):
|
||||
"""Zbiera liste pobliskiego ruchu. WYWOLYWAC TYLKO na glownym watku."""
|
||||
"""Zbiera liste pobliskiego ruchu z TCAS (LiveTraffic / AI / multiplayer)."""
|
||||
out = []
|
||||
# pozycja wlasna (lat/lon/elevacja)
|
||||
lat = self._get_dataref("sim/flightmodel/position/latitude", is_float=True)
|
||||
lon = self._get_dataref("sim/flightmodel/position/longitude", is_float=True)
|
||||
if lat is None or lon is None:
|
||||
bearing = self._read_float_array(self._tcas_bearing_ref, NUM_TCAS_SLOTS)
|
||||
distance = self._read_float_array(self._tcas_distance_ref, NUM_TCAS_SLOTS)
|
||||
altitude = self._read_float_array(self._tcas_altitude_ref, NUM_TCAS_SLOTS)
|
||||
flight_ids = self._read_packed_strings(self._tcas_flightid_ref, NUM_TCAS_SLOTS)
|
||||
icao_types = self._read_packed_strings(self._tcas_icao_ref, NUM_TCAS_SLOTS)
|
||||
|
||||
if not distance:
|
||||
return out
|
||||
my_el = 0.0
|
||||
try:
|
||||
my_el = xp.getDatad(self._elevation_ref) or 0.0
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
import math
|
||||
for i in range(1, NUM_TRAFFIC_PLANES + 1):
|
||||
lat_r = self._get_traffic_ref(i, "lat")
|
||||
lon_r = self._get_traffic_ref(i, "lon")
|
||||
el_r = self._get_traffic_ref(i, "el")
|
||||
if lat_r is None or lon_r is None:
|
||||
for i in range(NUM_TCAS_SLOTS):
|
||||
if i >= len(distance):
|
||||
break
|
||||
dist_m = distance[i]
|
||||
# 0 lub blisko 0 = pusty slot
|
||||
if not dist_m or dist_m <= 0:
|
||||
continue
|
||||
try:
|
||||
tlat = xp.getDatad(lat_r)
|
||||
tlon = xp.getDatad(lon_r)
|
||||
except Exception:
|
||||
continue
|
||||
# 0,0 oznacza zwykle brak samolotu
|
||||
if tlat == 0.0 and tlon == 0.0:
|
||||
continue
|
||||
tel = 0.0
|
||||
if el_r is not None:
|
||||
try:
|
||||
tel = xp.getDatad(el_r)
|
||||
except Exception:
|
||||
tel = 0.0
|
||||
|
||||
# przyblizony namiar i dystans (kula)
|
||||
dlat = math.radians(tlat - lat)
|
||||
dlon = math.radians(tlon - lon)
|
||||
a = math.sin(dlat / 2) ** 2 + math.cos(math.radians(lat)) * math.cos(math.radians(tlat)) * math.sin(dlon / 2) ** 2
|
||||
dist_m = 2 * 6371000.0 * math.asin(min(1.0, math.sqrt(a)))
|
||||
dist_nm = dist_m / 1852.0
|
||||
rel_alt_m = altitude[i] if i < len(altitude) else 0.0
|
||||
rel_alt_ft = rel_alt_m * 3.28084
|
||||
|
||||
# bearing (true)
|
||||
y = math.sin(dlon) * math.cos(math.radians(tlat))
|
||||
x = math.cos(math.radians(lat)) * math.sin(math.radians(tlat)) - \
|
||||
math.sin(math.radians(lat)) * math.cos(math.radians(tlat)) * math.cos(dlon)
|
||||
brg = (math.degrees(math.atan2(y, x)) + 360.0) % 360.0
|
||||
brg_rel = bearing[i] if i < len(bearing) else 0.0
|
||||
brg_rel = brg_rel % 360.0
|
||||
|
||||
# wzgledna wysokosc (feet)
|
||||
rel_alt_ft = (tel - my_el) * 3.28084
|
||||
# pozycja zegarowa (12-godzinna) wzgledem namiaru wzglednego
|
||||
clock = int(round((brg_rel / 30.0) % 12)) or 12
|
||||
|
||||
# relatywna pozycja zegarowa (12-godzinna) dla ATC
|
||||
clock = int(round((brg / 30.0) % 12)) or 12
|
||||
callsign = flight_ids[i] if i < len(flight_ids) else ""
|
||||
icao = icao_types[i] if i < len(icao_types) else ""
|
||||
|
||||
tag = ""
|
||||
if callsign:
|
||||
tag += callsign
|
||||
if icao:
|
||||
tag += ((" " + icao) if tag else icao)
|
||||
if tag:
|
||||
tag = " (" + tag + ")"
|
||||
|
||||
out.append(
|
||||
f" {dist_nm:.1f} nm, {clock}:00, alt {rel_alt_ft:+.0f} ft (brg {brg:.0f})"
|
||||
f" {dist_nm:.1f} nm, {clock}:00, alt {rel_alt_ft:+.0f} ft (brg {brg_rel:.0f}){tag}"
|
||||
)
|
||||
|
||||
return out
|
||||
@@ -547,9 +567,14 @@ class XSayATC:
|
||||
self._cmd = xp.createCommand("xsayatc/ptt", "XSayATC push-to-talk")
|
||||
xp.registerCommandHandler(self._cmd, self._ptt_handler, 1, None)
|
||||
|
||||
# Komenda toggle okna (przypisz klawisz, zeby wlaczac/wylaczac okno)
|
||||
self._cmd_toggle = xp.createCommand("xsayatc/toggle_window", "XSayATC toggle window")
|
||||
xp.registerCommandHandler(self._cmd_toggle, self._toggle_handler, 1, None)
|
||||
|
||||
# Menu
|
||||
self._menu = xp.createMenu("XSayATC", None, 0, self._menu_handler, None)
|
||||
self._menu_open_idx = xp.appendMenuItem(self._menu, "Pokaz okno", None)
|
||||
self._menu_open_idx = xp.appendMenuItemWithCommand(
|
||||
self._menu, "Pokaz/ukryj okno", self._cmd_toggle)
|
||||
self._menu_cfg_idx = xp.appendMenuItem(self._menu, "Konfiguracja", None)
|
||||
|
||||
# Okno glowne
|
||||
@@ -614,10 +639,14 @@ class XSayATC:
|
||||
return 0
|
||||
return 1
|
||||
|
||||
def _menu_handler(self, menuRef, itemRef):
|
||||
if itemRef == self._menu_open_idx:
|
||||
def _toggle_handler(self, cmdRef, phase, refCon):
|
||||
if phase == xp.CommandBegin:
|
||||
self._toggle_main_window()
|
||||
elif itemRef == self._menu_cfg_idx:
|
||||
return 0
|
||||
return 1
|
||||
|
||||
def _menu_handler(self, menuRef, itemRef):
|
||||
if itemRef == self._menu_cfg_idx:
|
||||
self._toggle_config_window()
|
||||
|
||||
def _toggle_main_window(self):
|
||||
|
||||
Reference in new issue
Block a user