Jetro

TankSlave.lua

Dec 5th, 2021 (edited)
381
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     ProgramName = "Tank/TankSlave.lua"
  2.     Version = "1.0"
  3.     Date = "5/12/2021"
  4.     Time = "15:20"
  5.     Author = "Jetro"
  6.  
  7.  
  8.  
  9. -- Peripherals
  10.  
  11. local peripherals = peripheral.getNames()
  12. for i = 1, #peripherals do
  13.     if peripheral.getType(peripherals[i]) == "modem" then
  14.         ModemSide = peripherals[i]
  15.     end
  16. end
  17. if ModemSide == nil then
  18.     error("no modem")
  19. end
  20.  
  21. rednet.open(ModemSide)
  22.  
  23. -- Variables
  24.  
  25. local MainPCID = 0
  26. local TankName
  27. local LastAmount
  28. local Update = false
  29.  
  30. -- Functions
  31.  
  32. function RednetSend( toID, SendMSG)
  33.     WaitTime = 0
  34.     id = nil
  35.     msg = nil
  36.     while id == nil and msg ~= "received" do
  37.         rednet.send(toID,SendMSG)
  38.         print("SND: #"..toID..": "..SendMSG)
  39.         id, msg = rednet.receive(1)
  40.     end
  41. end
  42.  
  43. -- Main
  44.  
  45. if fs.exists("Tank/TankName.lua") then
  46.     myTankName = fs.open("Tank/TankName.lua","r")
  47.     TankName = myTankName.readAll()
  48.     myTankName.close()
  49. else
  50.     error("No TankName file")
  51. end
  52.  
  53. if not(fs.exists("startup.lua")) then
  54.     os.setComputerLabel(TankName)
  55.     myStartup = fs.open("startup.lua","w")
  56.     myStartup.write("shell.run('"..ProgramName.."')")
  57.     myStartup.close()
  58.     os.reboot()
  59. end
  60.  
  61.  
  62. while true do
  63.     id, msg = rednet.receive(1)
  64.     if msg == "Update" then
  65.         Update = true
  66.     elseif msg == "UpdateProgram" then
  67.         fs.delete(ProgramName)
  68.         shell.run("pastebin get nAS2Debh "..ProgramName)
  69.         os.reboot()
  70.     else
  71.     end
  72.     Tank = peripheral.find("railcraft:tank_steel_valve")
  73.     Tank = Tank.getTanks()[1]
  74.     if Tank.amount then
  75.         if (LastAmount ~= Tank.amount) or (Update == true) then
  76.             RednetSend(MainPCID,TankName.."|"..Tank.name.."|"..Tank.amount.."|"..Tank.capacity)
  77.             LastAmount = Tank.amount
  78.         end
  79.     else
  80.         if (LastAmount ~= 0) or (Update == true) then
  81.             Tank.name = "empty"
  82.             Tank.amount = 0
  83.             RednetSend(MainPCID,TankName.."|"..Tank.name.."|"..Tank.amount.."|"..Tank.capacity)
  84.             LastAmount = 0
  85.         end
  86.     end
  87.     Update = false
  88. end
RAW Paste Data