Advertisement
Gazer29

#01 Engine

Apr 1st, 2021 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require("component")
  2. local internet = require("internet")
  3. local term = require ("term")
  4. local json = require("json")
  5. local serial = require("serialization")
  6. local fs = require("filesystem")
  7. local AllEntities = component.world_link.getLoadedEntities()
  8. local CONFIG_FILE = "/home/EngineCMDs.dat"
  9. local DEFAULT_CONFIG = {}
  10. local TcpIP = "84.65.32.30"
  11. local TcpSendPort = 6667
  12. local TcpRecvPort = 6667
  13. local wait = 0.1
  14.  
  15. local modem = component.modem
  16. local stocktypes = {
  17.     "immersiverailroading:locomotivediesel",
  18.     "immersiverailroading:locomotivesteam"
  19.     }
  20.  
  21. function overArray()
  22.     local b = {}
  23.     count = 0
  24.     for i0, typeX in pairs(stocktypes) do
  25.         AllofType = AllEntities.whereProperty("name", typeX).asList()
  26.         if AllofType ~= nil then
  27.             for i1, entity in pairs(AllofType) do
  28.                 rollingstock = entity.getAPI("immersiverailroading:locomotive")
  29.                 local posit_array = {}
  30.                 local a = {}
  31.                 uuid = rollingstock.getUUID()
  32.                 position = rollingstock.getLocation()
  33.                 xpos = position.getX()
  34.                 ypos = position.getY()
  35.                 zpos = position.getZ()
  36.                 speed = rollingstock.getCurrentSpeed()
  37.                 if locotable[uuid] == nil then
  38.                     setspeed = 0
  39.                     method = "Manual"
  40.                     else
  41.                     setspeed = locotable[uuid].setspeed
  42.                     method = locotable[uuid].method
  43.                     end
  44.                 tag = "" -- add later
  45.                 c = {["x"] = xpos, ["y"] = ypos, ["z"] = zpos}
  46.                 a = {}
  47.                 a["uuid"] = uuid
  48.                 a["speed"] = speed
  49.                 a["setspeed"] = setspeed
  50.                 a["method"] = method
  51.                 a["posit_array"] = c
  52.                 a["name"] = rollingstock.getName()
  53.                 a["tag"] = "" --rollingstock.getTag()
  54.                 table.insert(b,1,a)
  55.                 end
  56.             end
  57.         end
  58.     data = json:encode(b)
  59.     if data ~= "" then
  60.         con:write("#01,"..data.."\r\n")
  61.         con:flush()
  62.         end
  63. end
  64.  
  65. function recvtcp()
  66.     data = nil
  67.     local decoded = nil
  68.     local b = {}
  69.     if (con) then
  70.         index = con:read(10)
  71.         length = tonumber(index)
  72.         data = con:read(length)
  73.         if data ~= nil then
  74.             decoded = json:decode(data)
  75.             a = {}
  76.             for i, v in pairs(decoded) do
  77.                 uuid = v.uuid
  78.                 v.uuid = nil
  79.                 a[uuid] = v
  80.                 end
  81.             end
  82.         end
  83.     if a == {} then
  84.         a = nil
  85.         end
  86.     return a
  87. end
  88.  
  89. function parseResponse(data)
  90.     for uuid, d in pairs(data) do
  91.         locotable[uuid] = d
  92.         end
  93. end
  94.  
  95. function changeloco(data)
  96.     for i, loco in pairs(data) do
  97.         tag = loco.tag
  98.         method = loco.method
  99.         horn = loco.horn
  100.         setspeed = loco.setspeed
  101.         uuid = loco.uuid
  102.         a = AllEntities.whereProperty("uuid", uuid).asList()
  103.         b = a[1]
  104.         c = b.getAPI("immersiverailroading:locomotive")
  105.         c.setTag(loco.tag)
  106.         print(uuid, tag, method, horn, setspeed)
  107.         end
  108.     end
  109.  
  110. function saveConfig(file, cfg)
  111.   local f = io.open(file, "w")
  112.   if f == nil then error("Couldn't open " .. file .. " to write config.") end
  113.   f:write(serial.serialize(cfg, 100000))
  114.   f:close()
  115.   --print("Saved to file")
  116. end
  117.  
  118. function loadConfig()
  119.   local f = io.open(CONFIG_FILE, "r")
  120.   if f == nil then
  121.     print("Could not open " .. CONFIG_FILE .. ".")
  122.     print("Loading default config")
  123.     return DEFAULT_CONFIG
  124.     else
  125.     local config = serial.unserialize(f:read("*a"))
  126.     f:close()
  127.     return config
  128.     end
  129. end
  130.  
  131. local function broadcast(msg)
  132.     print("broadcasting table")
  133.   modem.broadcast(54421, serial.serialize({t = "STATUS",v = msg}))
  134. end
  135.  
  136. -- Main loop --
  137.  
  138. con = internet.open(TcpIP,TcpSendPort)
  139. modem.open(54421)  
  140. while true do
  141.     if (con) then
  142.         locotable = loadConfig()
  143.         overArray()
  144.         response = recvtcp()
  145.         end
  146.     if response ~= nil then
  147.         parseResponse(response)
  148.         saveConfig(CONFIG_FILE, locotable)
  149.         end
  150.     broadcast(locotable)
  151.     os.sleep(wait)
  152. end
  153. modem.close(54421)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement