Advertisement
Skip_21

Smart Helmet Client

Nov 24th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.49 KB | None | 0 0
  1. -- Program created by Skip_21
  2. -- Require PeripheralsPlusOne
  3. -- Currently a W.I.P (Work In Progress)
  4.  
  5. -- alpha 1.4 changelog :
  6. --      Added Thermal Expansion support of Energy Storage
  7.  
  8. -- alpha 1.5 changelog :
  9. --      Added IC2 support of Energy Storage
  10.  
  11. version = "alpha 1.5"
  12.  
  13. fails = 0
  14.  
  15. pblocks = {}
  16.  
  17. knownperipherals = {"playerSensor", "oreDictionary", "environmentScanner",
  18.                     "speaker", "aiChatBox", "beeAnalyzer", "treeAnalyzer", "butterflyAnalyzer",
  19.                     "teleporter", "peripheralContainer", "meBridge", "timeSensor",
  20.                     "interactiveSorter", "playerInterface", "Induction Matrix",
  21.                     "BigReactors-Reactor", "BigReactors-Turbine", "thermalexpansion:storage_cell",
  22.                     "techreborn:interdimensional_su", "ic2:batbox", "ic2:oldmfe", "ic2:oldmfsu" }
  23.  
  24.  
  25.  
  26. function searchlist(searchfor,list)
  27.     for i=1,#list do
  28.         if list[i]==searchfor then return true end
  29.     end
  30.     return false
  31. end
  32.  
  33.  
  34.  
  35. function addperipheral(side)
  36.  
  37.     local result = false
  38.     thistype = peripheral.getType(side)
  39.  
  40.     if searchlist(thistype,knownperipherals) then
  41.  
  42.         print("Attaching to the "..side.." ("..thistype..")")
  43.         table.insert(pblocks,{peripheral.wrap(side), thistype, side})
  44.         result = true
  45.  
  46.     end
  47.  
  48.     return result
  49.  
  50. end
  51.  
  52.  
  53. function timedmessages()
  54.  
  55.     local sleepytime = 2
  56.  
  57.     -- Peripherals checking
  58.  
  59.     local i = 1
  60.     while i <= #pblocks do
  61.         local rtype = peripheral.getType(pblocks[i][3])
  62.         if rtype == pblocks[i][2] then
  63.             i = i + 1
  64.  
  65.         else
  66.             print(pblocks[i][2].." on "..pblocks[i][3].."has gone missing: removing.")
  67.             table.remove(pblocks,i)
  68.         end
  69.     end
  70.  
  71.            
  72.     for i = 1, #pblocks do 
  73.         if pblocks[i][2] == "BigReactors-Reactor" then
  74.            
  75.             emax = peripheral.wrap(pblocks[i][3]).getEnergyCapacity()
  76.             estor = peripheral.wrap(pblocks[i][3]).getEnergyStored()
  77.             eout = peripheral.wrap(pblocks[i][3]).getEnergyProducedLastTick()
  78.             heat = peripheral.wrap(pblocks[i][3]).getCasingTemperature()
  79.             fuse = peripheral.wrap(pblocks[i][3]).getFuelConsumedLastTick()
  80.             rin = peripheral.wrap(pblocks[i][3]).getControlRodLevel(1)
  81.            
  82.             percent = (estor/emax)*100
  83.  
  84.             peripheral.wrap(pblocks[i][3]).setAllControlRodLevels(percent)
  85.  
  86.             local message = {estor, emax, eout, heat, fuse, rin, percent}
  87.  
  88.             rednet.send(shserver, message, "reactor")
  89.  
  90.         elseif pblocks[i][2] == "Induction Matrix" then
  91.  
  92.             emax = peripheral.wrap(pblocks[i][3]).getMaxEnergy()
  93.             estor = peripheral.wrap(pblocks[i][3]).getEnergy()
  94.             ein = peripheral.wrap(pblocks[i][3]).getInput()
  95.             eout = peripheral.wrap(pblocks[i][3]).getOutput()
  96.  
  97.             local message = {emax, estor, ein, eout}
  98.  
  99.             rednet.send(shserver, message, "matrix")
  100.  
  101.         elseif pblocks[i][2] == "techreborn:interdimensional_su" then
  102.  
  103.             emax = 100000000
  104.             estor = emax - peripheral.wrap(pblocks[i][3]).getDemandedEnergy()
  105.  
  106.             local message = {emax, estor}
  107.        
  108.             rednet.send(shserver, message, "IDSU")
  109.  
  110.         elseif (pblocks[i][2] == "ic2:batbox") or (pblocks[i][2] == "ic2:oldmfe") or (pblocks[i][2] == "ic2:oldmfsu") then
  111.  
  112.             if pblocks[i][2] == "ic2:batbox" then
  113.                
  114.                 emax = 40000
  115.                 estor = emax - peripheral.wrap(pblocks[i][3]).getDemandedEnergy()
  116.  
  117.                 local message = {emax, estor}
  118.  
  119.                 rednet.send(shserver, message, "ic2")
  120.  
  121.             elseif pblocks[i][2] == "ic2:oldmfe" then
  122.                
  123.                 emax = 600000
  124.                 estor = emax - peripheral.wrap(pblocks[i][3]).getDemandedEnergy()
  125.  
  126.                 local message = {emax, estor}
  127.  
  128.                 rednet.send(shserver, message, "ic2")
  129.  
  130.             elseif pblocks[i][2] == "ic2:oldmfsu" then
  131.                
  132.                 emax = 10000000
  133.                 estor = emax - peripheral.wrap(pblocks[i][3]).getDemandedEnergy()
  134.  
  135.                 local message = {emax, estor}
  136.  
  137.                 rednet.send(shserver, message, "ic2")
  138.  
  139.             end
  140.  
  141.         end
  142.  
  143.     end
  144.  
  145.     return sleepytime
  146.  
  147. end
  148.  
  149.  
  150. function br(x)
  151.  
  152.     print("Yolo !")
  153.  
  154. end
  155.  
  156.  
  157. function eventlistener()
  158.  
  159.     local event, p1, p2, p3 = os.pullEvent()
  160.  
  161.     if event == "rednet_message" and p3 == "shserver" then
  162.  
  163.         if p2[1] == "reboot" then print("Rebooting...") sleep(2) os.reboot() end
  164.         if p2[1] == "br" then br(p2) end
  165.  
  166.     elseif event=="timer" and p1==timedmessage then
  167.    
  168.         timedmessage = os.startTimer(timedmessages())
  169.    
  170.     end
  171.  
  172. end
  173.  
  174.  
  175.  
  176. -- Init
  177.  
  178. term.clear()
  179. term.setCursorPos(1,1)
  180. print("Skip_21's Smart Helmet "..version.." running !")
  181.  
  182. sleep(2)
  183.  
  184. if (os.getComputerLabel()==nil) then
  185.   print("Setting computer label to 'SHClient'.")
  186.   os.setComputerLabel("SHClient")
  187. end
  188.  
  189. sides = peripheral.getNames()
  190.  
  191. modemhere = false
  192.  
  193. for i = 1, #sides do
  194.  
  195.     thistype = peripheral.getType(sides[i])
  196.  
  197.     if thistype == "modem" then
  198.    
  199.         print("Modem found on " .. sides[i] .. ", connecting...")
  200.         rednet.open(sides[i])
  201.         modemhere = true
  202.  
  203.     end
  204.  
  205. end
  206.  
  207. if not modemhere then error("Please connect a Wireless Modem") end
  208.  
  209. shserver = rednet.lookup("shserver","shserver")
  210.  
  211. while shserver == nil do
  212.  
  213.   fails = fails + 1
  214.   print("Can't find SHserver. Waiting ... ("..fails..")")
  215.   os.sleep(10)
  216.   shserver = rednet.lookup("shserver","shserver")
  217.  
  218. end
  219.  
  220. print("Found SHServer! Sending handshake.")
  221.  
  222. rednet.send(shserver,"hello","handshake")
  223.  
  224. print("Running ...")
  225.  
  226. -- peripherals Lookup
  227.  
  228. local count = 0
  229. for i=1, #sides do
  230.   if addperipheral(sides[i]) then count = count + 1 end
  231. end
  232.  
  233. if count==0 then
  234.   error("No recognised peripherals. Aborting.")
  235. else
  236.   print("Attached to "..count.." recognised peripherals.")
  237. end
  238.  
  239. -- main loop
  240.  
  241. timedmessage = os.startTimer(5)
  242.  
  243. while true do
  244.  
  245.     eventlistener()
  246.  
  247. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement