Skip to content

Commit

Permalink
IFF code generation overhaul
Browse files Browse the repository at this point in the history
ricmzn says:

This fixes generation of invalid mode 1 IFF codes, as most aircraft
allow 0-7 in the first digit, but only 0-3 the second digit, by moving
the mission type (1-5) over to the first digit of the code, and also
adds a secondary secondary digit to the generated code, so different
mission types of the same kind (ie. Strike, BAI, Armed Recon, CAS) have
unique mode 1 codes (50, 51, 52, 53).

This also makes so that assets assigned to mission each get an unique
(sequential) mode 3 IFF code, wrapping when the last digit reaches the
maximum value (7), to not collide with other mission codes. This will
hopefully make cooperation with LotATC-based controllers easier if
people follow the mission instructions.

All the new settings are now in the enum.lua file, easily editable
alongside other mission type definitions.
  • Loading branch information
jtoppins committed Jan 8, 2022
2 parents f2145bb + df74fb5 commit a247217
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 40 deletions.
37 changes: 6 additions & 31 deletions src/dct/ai/Commander.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,34 +109,9 @@ local invalidXpdrTbl = {
["7400"] = true,
}

local squawkMissionType = {
["SAR"] = 0,
["SUPT"] = 1,
["A2A"] = 2,
["SEAD"] = 3,
["SEA"] = 4,
["A2G"] = 5,
}

local function map_mission_type(msntype)
local sqwkcode
if msntype == enum.missionType.CAP then
sqwkcode = squawkMissionType.A2A
--elseif msntype == enum.missionType.SAR then
-- sqwkcode = squawkMissionType.SAR
--elseif msntype == enum.missionType.SUPPORT then
-- sqwkcode = squawkMissionType.SUPT
elseif msntype == enum.missionType.SEAD then
sqwkcode = squawkMissionType.SEAD
else
sqwkcode = squawkMissionType.A2G
end
return sqwkcode
end

--[[
-- Generates a mission id as well as generating IFF codes for the
-- mission.
-- mission (in octal).
--
-- Returns: a table with the following:
-- * id (string): is the mission ID
Expand All @@ -146,16 +121,16 @@ end
--]]
function Commander:genMissionCodes(msntype)
local id
local m1 = map_mission_type(msntype)
local digit1 = enum.squawkMissionType[msntype]
while true do
MISSION_ID = (MISSION_ID + 1) % 64
id = string.format("%01o%02o0", m1, MISSION_ID)
if invalidXpdrTbl[id] == nil and
self:getMission(id) == nil then
id = string.format("%01o%02o0", digit1, MISSION_ID)
if invalidXpdrTbl[id] == nil and self:getMission(id) == nil then
break
end
end
local m3 = (512*m1)+(MISSION_ID*8)
local m1 = (8*digit1)+(enum.squawkMissionSubType[msntype] or 0)
local m3 = (512*digit1)+(MISSION_ID*8)
return { ["id"] = id, ["m1"] = m1, ["m3"] = m3, }
end

Expand Down
16 changes: 16 additions & 0 deletions src/dct/ai/Mission.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ function Mission:__init(cmdr, missiontype, tgt, plan)
self:_setComplete(false)
self.state = PrepState()
self.state:enter(self)
self._assignedIds = {}
self._lastAssignedId = 0

-- compose the briefing at mission creation to represent
-- known intel the pilots were given before departing
Expand Down Expand Up @@ -223,6 +225,10 @@ function Mission:addAssigned(asset)
return
end
table.insert(self.assigned, asset.name)
if self._assignedIds[asset.name] == nil then
self._assignedIds[asset.name] = self._lastAssignedId
self._lastAssignedId = self._lastAssignedId + 1
end
Logger:debug("Mission %d: addAssigned(%s)", self.id, asset.name)
asset.missionid = self:getID()
end
Expand Down Expand Up @@ -331,6 +337,16 @@ function Mission:addTime(time)
return time
end

function Mission:getIFFCodes(asset)
local assignedId = 0
if asset ~= nil then
assignedId = (self._assignedIds[asset.name] or 0) % 8
end
local m1 = string.format("%o", self.iffcodes.m1)
local m3 = string.format("%o", self.iffcodes.m3 + assignedId)
return { ["m1"] = m1, ["m3"] = m3 }
end

function Mission:getDescription(fmt)
local tgt = self.cmdr:getAsset(self.target)
if tgt == nil then
Expand Down
35 changes: 29 additions & 6 deletions src/dct/enum.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,33 @@ enum.assetTypePriority = {
enum.missionInvalidID = 0

enum.missionType = {
["CAS"] = 1,
["CAP"] = 2,
["STRIKE"] = 3,
["SEAD"] = 4,
["BAI"] = 5,
["OCA"] = 6,
["CAS"] = 1,
["CAP"] = 2,
["STRIKE"] = 3,
["SEAD"] = 4,
["BAI"] = 5,
["OCA"] = 6,
["ARMEDRECON"] = 7,
}

enum.squawkMissionType = {
[enum.missionType.CAP] = 2,
[enum.missionType.SEAD] = 3,
[enum.missionType.CAS] = 5,
[enum.missionType.STRIKE] = 5,
[enum.missionType.BAI] = 5,
[enum.missionType.OCA] = 5,
[enum.missionType.ARMEDRECON] = 5,
}

enum.squawkMissionSubType = {
[enum.missionType.STRIKE] = 0,
[enum.missionType.OCA] = 0,
[enum.missionType.BAI] = 1,
[enum.missionType.ARMEDRECON] = 2,
[enum.missionType.CAS] = 3,
}

enum.assetClass = {
["INITIALIZE"] = {
[enum.assetType.AMMODUMP] = true,
Expand Down Expand Up @@ -238,4 +256,9 @@ enum.event = {

enum.kickCode = require("dct.libs.kickinfo").kickCode

for _, msntype in pairs(enum.missionType) do
assert(enum.squawkMissionType[msntype],
"not all mission types are mapped to squawk codes")
end

return enum
4 changes: 2 additions & 2 deletions src/dct/ui/cmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ end

local function briefingmsg(msn, asset)
local tgtinfo = msn:getTargetInfo()
local iff = msn:getIFFCodes(asset)
local msg = string.format("Package: #%s\n", msn:getID())..
string.format("IFF Codes: M1(%02o), M3(%04o)\n",
msn.iffcodes.m1, msn.iffcodes.m3)..
string.format("IFF Codes: M1(%s), M3(%s)\n", iff.m1, iff.m3)..
string.format("%s: %s (%s)\n",
human.locationhdr(msn.type),
dctutils.fmtposition(
Expand Down
2 changes: 1 addition & 1 deletion tests/test-ui-cmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ local unit1 = Unit({
}, grp, "bobplayer")

local briefingtxt = "Package: #5720\n"..
"IFF Codes: M1(05), M3(5720)\n"..
"IFF Codes: M1(50), M3(5720)\n"..
"Target AO: 88°07.38'N 063°27.36'W (TOKYO)\n"..
"Briefing:\n"..
"We have reason to believe there is"..
Expand Down

0 comments on commit a247217

Please sign in to comment.