Larvix

framework

Jan 6th, 2024 (edited)
1,175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.00 KB | None | 0 0
  1. local version = "Hivemind v0.0"
  2. local path = "//hivemind/"
  3.  
  4. -- Declare data variable
  5. local data = {}
  6. local dataFile = path.."system.txt"
  7.  
  8. -- Populate data variable if file exists
  9. if fs.exists(dataFile) then
  10.     file = fs.open(dataFile,"r")
  11.     data = textutils.unserialise(file.readAll())
  12.     file.close()
  13. else data = {["system"] = {}, ["network"] = {}} end
  14.  
  15. -- Function to save data to file
  16. local save = function()
  17.     file = fs.open(dataFile,"w")
  18.     file.write(textutils.serialise(data))
  19.     file.close()
  20. end
  21.  
  22. -- Functions for system management
  23. local system = {}
  24.  
  25. system.peripherals = function()
  26.  -- Check peripherals
  27.     data.system.peripherals = {}
  28.     side = peripheral.getNames()
  29.     for i = 1,#side do
  30.         data.system.peripherals[side[i]] = peripheral.getType(side[i])
  31.     end
  32.  -- Check neural interface modules
  33.     if peripheral.getType("back") == "neuralInterface" then
  34.         if peripheral.wrap("back").listModules() then
  35.             data.system.peripherals.modules = peripheral.wrap("back").listModules()
  36.         end
  37.     end
  38.     save()
  39. return data.system.peripherals end
  40.  
  41. system.position = function()
  42.  -- Check position with GPS
  43.     data.system.pos = {}
  44.     data.system.pos["x"],data.system.pos["y"],data.system.pos["z"] = gps.locate()
  45.     if not data.system.pos["x"] then data.system.pos = "Lost" end
  46.     save()
  47. end
  48.  
  49. system.bearing = function()
  50.     system.position()
  51.  -- Check where turtle is facing
  52.     if turtle and data.system.pos then
  53.         if turtle.getFuelLevel() > 1 and data.system.pos["x"] then
  54.             turns = 0
  55.             for attempt = 1,4 do
  56.                 if not data.system.pos["bearing"] and turtle.forward() then
  57.                     local tmpX,tmpY,tmpZ = gps.locate()
  58.                     if tmpX then
  59.                         tmpX = tmpX - data.system.pos["x"]
  60.                         tmpZ = tmpZ - data.system.pos["z"]
  61.                         if tmpX ~= 0 then
  62.                             if tmpX > 0 then
  63.                                 data.system.pos["bearing"] = "east"
  64.                             else
  65.                                 data.system.pos["bearing"] = "west"
  66.                             end
  67.                         elseif tmpZ ~= 0 then
  68.                             if tmpZ > 0 then
  69.                                 data.system.pos["bearing"] = "south"
  70.                             else
  71.                                 data.system.pos["bearing"] = "north"
  72.                             end
  73.                         end
  74.                     end
  75.                     if not turtle.back() then
  76.                         system.position()
  77.                     end
  78.                 end
  79.                 if not data.system.pos["bearing"] then
  80.                     turtle.turnLeft()
  81.                     turns = turns + 1
  82.                 else
  83.                     if turns == 3 then
  84.                         turtle.turnLeft()
  85.                     else
  86.                         for t = 1,turns do
  87.                             turtle.turnRight()
  88.                         end
  89.                     end
  90.                     break
  91.                 end
  92.             end
  93.         end
  94.  -- Check where neural interface is facing
  95.     elseif peripheral.getType("back") == "neuralInterface" then
  96.         if peripheral.wrap("back").hasModule("plethora:sensor") and peripheral.wrap("back").hasModule("plethora:introspection") then
  97.             data.system.pos["bearing"] = peripheral.wrap("back").getMetaOwner().yaw
  98.         end
  99.     end
  100.     save()
  101. return data.system.pos["bearing"] end
  102.  
  103. system.inventory = function()
  104.  -- Check turtle inventory
  105.     if turtle then
  106.         data.system.inventory = {}
  107.         for i = 1,16 do
  108.             if turtle.getItemDetail(i) then
  109.                 data.system.inventory[i] = {turtle.getItemDetail(i).count, turtle.getItemDetail(i).name}
  110.             end
  111.         end
  112.  -- Check neural interface equipment
  113.     elseif peripheral.getType("back") == "neuralInterface" then
  114.         if peripheral.wrap("back").hasModule("plethora:introspection") then
  115.             data.system.inventory = {}
  116.             for itemSlot,itemData in pairs(peripheral.wrap("back").getEquipment().list()) do
  117.                 data.system.inventory[itemSlot] = itemData.name
  118.             end
  119.         end
  120.     end
  121.     save()
  122. return data.system.inventory end
  123.  
  124. system.fuel = function()
  125.  -- Check turtle fuel level
  126.     if turtle then
  127.         data.system.fuel = turtle.getFuelLevel()
  128.     end
  129.     save()
  130. return data.system.fuel end
  131.  
  132. local network = {}
  133.  
  134. network.members = function(id)
  135.     if data.network.type == "server" then
  136.         if fs.exists(path.."network/") then
  137.             data.network.members = fs.list(path.."network/")
  138.             for i = 1,#data.network.members do
  139.                 data.network.members[i] = tonumber(data.network.members[i])
  140.             end
  141.         else data.network.members = {} end
  142.         if id then
  143.             id = tonumber(id)
  144.             exists = false
  145.             for i = 1,#data.network.members do
  146.                 if data.network.members[i] == id then
  147.                     exists = true
  148.                     break
  149.                 end
  150.             end
  151.             return exists
  152.         else return data.network.members end
  153.     end
  154.     save()
  155. end
  156.  
  157. network.create = function()
  158.     data.network.type = "server"
  159.     save()
  160. end
  161.  
  162. network.delete = function()
  163.     data.network = {}
  164.     save()
  165. end
  166.  
  167. network.join = function(id)
  168.     data.network.type = "client"
  169.     data.network.server = id
  170.     save()
  171.     modem.transmit(17007,7,os.getComputerID())
  172. end
  173.  
  174. network.leave = function()
  175.     data.network = {}
  176.     save()
  177. end
  178.  
  179. local checkEvent = function()
  180.     evt = nil
  181.     evt = {os.pullEvent()}
  182.     if evt[1] == "modem_message" then
  183.         if evt[4] == 7 then
  184.             if not network.members(tonumber(evt[5])) then
  185.                 table.insert(data.network.members,tonumber(evt[5]))
  186.                 file = fs.open(path.."network/"..evt[5],"w")
  187.                 file.write("test")
  188.                 file.close()
  189.             end
  190.         else
  191.             print(evt[1]..": "..evt[4].." --> "..evt[3].." ["..math.floor(evt[6]).."]\n"..evt[5])
  192.         end
  193.         sleep(5)
  194.     elseif evt[1] == "peripheral" or evt[1] == "peripheral_detach" then
  195.         system.peripheral()
  196.     elseif evt[1] == "key" and evt[2] == keys.delete then
  197.         data.network = {}
  198.         fs.delete(path)
  199.         running = nil
  200.     end
  201. end
  202.  
  203. -- Run program
  204. while true do
  205.     running = true
  206.     if not peripheral.find("modem") then
  207.         shell.run("clear")
  208.         print("Error: No modem attached")
  209.         while not peripheral.find("modem") do
  210.             os.pullEvent("peripheral")
  211.         end
  212.     else
  213.         modem = peripheral.find("modem")
  214.         modem.closeAll()
  215.         for i = 17001,17071 do
  216.             modem.open(i)
  217.         end
  218.         while running and modem.isOpen(17007) do
  219.             shell.run("clear")
  220.             if not data.network.type then
  221.                 write("[Create] or [Join] Hive? ")
  222.                 input = string.lower(read())
  223.                 if input == "create" then
  224.                     network.create()
  225.                 elseif input == "join" then
  226.                     write("Server ID: ")
  227.                     input = tonumber(read())
  228.                     if input then
  229.                         network.join(input)
  230.                     end
  231.                 end
  232.             elseif data.network.type == "server" then
  233.                 network.members()
  234.                 print(textutils.serialise(data.network.members))
  235.                 checkEvent()
  236.             elseif data.network.type == "client" then
  237.                 print("Server: "..textutils.serialise(data.network.server))
  238.                 checkEvent()
  239.             end
  240.         end
  241.     end
  242. end
Advertisement
Add Comment
Please, Sign In to add comment