Gazer29

MultiEngine

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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×