Export.lua 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. --[[
  2. dcsw server map: Export.lua
  3. by Sacha Ligthert on 2024-01-14
  4. ----
  5. Add the following lines to your Export.lua in your ...\Saved Games\DCS.openbeta\Scripts folder.
  6. If they functions already exist, you will need to do some editing yourselves.
  7. ]]--
  8. function LuaExportStart()
  9. package.path = package.path..";"..lfs.currentdir().."/LuaSocket/?.lua"
  10. package.cpath = package.cpath..";"..lfs.currentdir().."/LuaSocket/?.dll"
  11. socket = require("socket")
  12. IPAddress = "192.168.1.2"
  13. Port = 12345
  14. MySocket = socket.try(socket.connect(IPAddress, Port))
  15. MySocket:setoption("tcp-nodelay",true)
  16. end
  17. function LuaExportBeforeNextFrame()
  18. end
  19. function LuaExportAfterNextFrame()
  20. end
  21. function LuaExportStop()
  22. -- Terminate TCP connection
  23. if MySocket then
  24. --socket.try(MySocket:send("exit\n"))
  25. MySocket:close()
  26. end
  27. end
  28. function LuaExportActivityNextEvent(t)
  29. -- Required to function normal
  30. local tNext = t
  31. -- Grab objects. Create JSON. Send off to server
  32. local o = LoGetWorldObjects()
  33. local json = '{"units":['
  34. for k,v in pairs(o) do
  35. print(v.Type) -- add this line for debugging purposes
  36. local objectType = "unknown"
  37. objectType = type(v.Type)
  38. 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 )
  39. end
  40. -- Remove trialing comma
  41. json = json:sub(1, -2)
  42. -- Wrap up JSON
  43. json = json .. "]}"
  44. -- Send it awaaaaay!
  45. socket.try(MySocket:send(json))
  46. -- Come back in 10 seconds
  47. tNext = tNext + 10.0
  48. return tNext
  49. end