Lua_exports/Export.lua

65 lines
1.8 KiB
Lua

--[[
dcsw server map: Export.lua
by Sacha Ligthert on 2024-01-14
----
Add the following lines to your Export.lua in your ...\Saved Games\DCS.openbeta\Scripts folder.
If they functions already exist, you will need to do some editing yourselves.
]]--
function LuaExportStart()
package.path = package.path..";"..lfs.currentdir().."/LuaSocket/?.lua"
package.cpath = package.cpath..";"..lfs.currentdir().."/LuaSocket/?.dll"
socket = require("socket")
IPAddress = "192.168.1.2"
Port = 12345
MySocket = socket.try(socket.connect(IPAddress, Port))
MySocket:setoption("tcp-nodelay",true)
end
function LuaExportBeforeNextFrame()
end
function LuaExportAfterNextFrame()
end
function LuaExportStop()
-- Terminate TCP connection
if MySocket then
--socket.try(MySocket:send("exit\n"))
MySocket:close()
end
end
function LuaExportActivityNextEvent(t)
-- Required to function normal
local tNext = t
-- Grab objects. Create JSON. Send off to server
local o = LoGetWorldObjects()
local json = '{"units":['
for k,v in pairs(o) do
print(v.Type) -- add this line for debugging purposes
local objectType = "unknown"
objectType = type(v.Type)
json = json .. string.format('{"age_in_seconds": %.1f, "unit_name":"%s", "group_name":"%s", "name":"%s", "latitude":%f, "longitude":%f, "altitude_in_feet":%f, "heading_in_rads":%f, "pitch_in_rads":%f, "bank_in_rads":%f, "coalition":"%s", "type":"%s"},', t, v.UnitName, v.GroupName, v.Name, v.LatLongAlt.Lat, v.LatLongAlt.Long, v.LatLongAlt.Alt, v.Heading, v.Pitch, v.Bank, v.Coalition, objectType )
end
-- Remove trialing comma
json = json:sub(1, -2)
-- Wrap up JSON
json = json .. "]}"
-- Send it awaaaaay!
socket.try(MySocket:send(json))
-- Come back in 10 seconds
tNext = tNext + 10.0
return tNext
end