Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions LuaRules/Utilities/LosInfo.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
local function TranslateLosBitToBools(bit) -- turns Spring.GetUnitLosState into booleans. This makes it easier to read code.
local inLOS = bit%2 == 1
local inRadar = bit%4 >= 2
local prevLOS = bit%8 >= 4
local contRadar = bit > 7
return inLOS, inRadar, prevLOS, contRadar
end

local function IsInLOS(bit)
return bit%2 == 1
end

local function IsInRadar(bit)
return bit%4 >= 2
end

local function WasInLOS(bit)
return bit%8 >= 4
end

local function IsContRadar(bit)
return bit > 7
end

local function TranslateLosBitToTable(bit) -- turns Spring.GetUnitLosState into a nice human readable table.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC the Spring.GetLosState functions that return a bitmask have a bool option to return this table instead already?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure, documentation seems to be is missing. Can't find again.

local inLOS, inRadar, prevLOS, contRadar = TranslateLosBitToBools(bit)
return {inLOS = inLOS, inRadar = inRadar, prevLOS = prevLOS, contRadar = contRadar}
end

local function LosInfoToBit(inLOS, inRadar, prevLOS, contRadar) -- turns bools into a number for SetUnitLosState.
local ret = 0
if inLOS then
ret = ret + 1
end
if inRadar then
ret = ret + 2
end
if prevLOS then
ret = ret + 4
end
if contRadar then
ret = ret + 8
end
return ret
end

local LosInfo = {TranslateLosBitToBools = TranslateLosBitToBools, LosInfoToBit = LosInfoToBit, TranslateLosBitToTable = TranslateLosBitToTable, InLOS = IsInLOS, InRadar = IsInRadar, WasInLOS = WasInLOS, IsContRadar = IsContRadar}
Spring.Utilities.LosInfo = LosInfo