-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATIS.lua
More file actions
206 lines (185 loc) · 5.63 KB
/
ATIS.lua
File metadata and controls
206 lines (185 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
--[[
-- ATIS
-- by Chump
--]]
ATISFREQS = {} -- global for Map_Stuff
do
local config = ATIS_CONFIG or {
frequency_add = 250000 -- in Hz
}
assert(BASE ~= nil, "MOOSE must be loaded prior to this script!")
assert(require ~= nil, "REQUIRE must be available for this script!") -- for dofile
local ATISfreqs = {}
local function log(msg)
env.info(string.format("ATIS: %s", msg))
end
local function HzToMHz(freq)
if not freq then return nil end
return freq / 1000000
end
local function CountArray(arr)
local count = 0
for _, _ in pairs(arr) do
count = count + 1
end
return count
end
local oldRadio = radio
dofile(string.format("./Mods/terrains/%s/Radio.lua", env.mission.theatre))
if not radio then
log("Could not load Radio data!")
return
end
for _, obj in ipairs(radio) do
if CountArray(obj.frequency) > 0 then
local vhf = obj.frequency[VHF_HI]
if vhf and vhf[2] then
local atisFreq = HzToMHz(vhf[2] + config.frequency_add)
local ATCfreqs = {}
for _, freqData in pairs(obj.frequency) do
table.insert(ATCfreqs, HzToMHz(freqData[2]))
end
ATISfreqs[obj.radioId] = {atisFreq = atisFreq, ATCfreqs = ATCfreqs}
end
end
end
radio = oldRadio
if CountArray(ATISfreqs) == 0 then
log("No frequency data found!")
return
end
local function GetRunway(direction)
local runway = tostring(math.abs(direction + 180))
if string.len(runway) == 3 then
return tonumber(string.sub(runway, 1, 2))
end
return tonumber(string.sub(runway, 1, 1))
end
local function GetObj(id)
if id == -1 then return nil end
return ATISfreqs[string.format("airfield%i_0", id)]
end
local FindWhat = {
["ILS"] = 1,
["NDB"] = 2,
["PRMG"] = 3
}
local function FindMe(what, freq, isInner)
isInner = isInner or false
for _, obj in pairs(ATISfreqs) do
if what == FindWhat.ILS and obj.ils then
for _, ils in ipairs(obj.ils) do
if ils.freq == freq then
return true
end
end
elseif what == FindWhat.NDB and obj.ndb then
for _, ndb in ipairs(obj.ndb) do
if ndb.freq == freq and ndb.isInner == isInner then
return true
end
end
elseif what == FindWhat.PRMG and obj.prmg then
for _, prmg in ipairs(obj.prmg) do
if prmg.channel == freq then
return true
end
end
end
end
return nil
end
dofile(string.format("./Mods/terrains/%s/Beacons.lua", env.mission.theatre))
if not beacons then
log("Could not load Beacons data!")
return
end
for _, beacon in ipairs(beacons) do
if string.find(beacon.beaconId, "airfield") then
local id = tonumber(string.match(beacon.beaconId, "airfield(%d+)_")) or -1
local obj = GetObj(id)
if obj then
local freq = HzToMHz(beacon.frequency)
if beacon.type == BEACON_TYPE_TACAN then
obj.tacan_channel = beacon.channel
elseif beacon.type == BEACON_TYPE_VOR then
obj.vor_freq = freq
elseif beacon.type == BEACON_TYPE_ILS_LOCALIZER and not FindMe(FindWhat.ILS, freq) then
if not obj.ils then obj.ils = {} end
table.insert(obj.ils, {freq = freq, runway = GetRunway(beacon.direction)})
elseif beacon.type == BEACON_TYPE_ILS_NEAR_HOMER and not FindMe(FindWhat.NDB, freq, true) then
if not obj.ndb then obj.ndb = {} end
table.insert(obj.ndb, {freq = freq, runway = GetRunway(beacon.direction), isInner = true})
elseif beacon.type == BEACON_TYPE_ILS_FAR_HOMER and not FindMe(FindWhat.NDB, freq) then
if not obj.ndb then obj.ndb = {} end
table.insert(obj.ndb, {freq = freq, runway = GetRunway(beacon.direction)})
elseif beacon.type == BEACON_TYPE_PRMG_LOCALIZER and not FindMe(FindWhat.PRMG, beacon.channel) then
if not obj.prmg then obj.prmg = {} end
table.insert(obj.prmg, {channel = beacon.channel, runway = GetRunway(beacon.direction)})
elseif beacon.type == BEACON_TYPE_RSBN then
obj.rsbn_channel = beacon.channel
end
end
end
end
beacons = nil
local airbases = coalition.getAirbases(coalition.side.BLUE)
if #airbases == 0 then
log("No coalition airbases found!")
end
for _, airbase in ipairs(airbases) do
if airbase:getDesc().category == Airbase.Category.AIRDROME then
local callsign = airbase:getCallsign()
local obj = GetObj(airbase:getID())
if obj then
local atis = ATIS
:New(callsign, obj.atisFreq)
:SetSoundfilesPath("ATIS/")
local name = "ATIS_" .. callsign
if Group.getByName(name) then
atis:SetRadioRelayUnitName(name)
else
log(string.format("ATIS Group (%s) not found! Subtitles are disabled.", name))
end
if #obj.ATCfreqs > 0 then
atis:SetTowerFrequencies(obj.ATCfreqs)
end
if obj.tacan_channel then
atis:SetTACAN(obj.tacan_channel)
end
if obj.vor_freq then
atis:SetVOR(obj.vor_freq)
end
if obj.ils then
for _, ils in ipairs(obj.ils) do
atis:AddILS(ils.freq, ils.runway)
end
end
if obj.ndb then
for _, ndb in ipairs(obj.ndb) do
if ndb.isInner then
atis:AddNDBinner(ndb.freq, ndb.runway)
else
atis:AddNDBouter(ndb.freq, ndb.runway)
end
end
end
if obj.prmg then
for _, prmg in ipairs(obj.prmg) do
atis:AddPRMG(prmg.channel, prmg.runway)
end
end
if obj.rsbn_channel then
atis:SetRSBN(obj.rsbn_channel)
end
atis:Start()
ATISFREQS[callsign] = obj.atisFreq
log(string.format("Broadcasting from %s on %.3f MHz", callsign, ATISFREQS[callsign]))
else
log("Airfield Object not found!")
end
end
end
airbases = nil
ATISfreqs = nil
end