Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Program created by Skip_21
- -- Require Advanced Peripherals
- -- Currently a W.I.P (Work In Progress)
- version = "alpha 0.1"
- fails = 0
- pblocks = {}
- knownperipherals = {"inductionPort"}
- function searchlist(searchfor, list)
- for i = 1, #list do
- if list[i] == searchfor then
- return true
- end
- end
- return false
- end
- function addperipheral(side)
- local result = false
- thistype = peripheral.getType(side)
- if searchlist(thistype, knownperipherals) then
- print("Attaching to the " .. side .. " (" .. thistype .. ")")
- table.insert(pblocks, {peripheral.wrap(side), thistype, side})
- result = true
- end
- return result
- end
- function timedmessages()
- local sleepytime = 2
- -- Peripherals checking
- local i = 1
- while i <= #pblocks do
- local rtype = peripheral.getType(pblocks[i][3])
- if rtype == pblocks[i][2] then
- i = i + 1
- else
- print(pblocks[i][2] .. " on " .. pblocks[i][3] .. "has gone missing: removing.")
- table.remove(pblocks, i)
- end
- end
- for i = 1, #pblocks do
- if pblocks[i][2] == "inductionPort" then
- emax = peripheral.wrap(pblocks[i][3]).getMaxEnergy()
- estor = peripheral.wrap(pblocks[i][3]).getEnergy()
- ein = peripheral.wrap(pblocks[i][3]).getLastInput()
- eout = peripheral.wrap(pblocks[i][3]).getLastOutput()
- percent = peripheral.wrap(pblocks[i][3]).getEnergyFilledPercentage()
- local message = {emax, estor, ein, eout, percent}
- rednet.send(arserver, message, "matrix")
- end
- end
- return sleepytime
- end
- function eventlistener()
- local event, p1, p2, p3 = os.pullEvent()
- if event == "rednet_message" and p3 == "arserver" then
- if p2[1] == "reboot" then
- print("Rebooting...")
- sleep(2)
- os.reboot()
- end
- if p2[1] == "br" then
- br(p2)
- end
- elseif event == "timer" and p1 == timedmessage then
- timedmessage = os.startTimer(timedmessages())
- end
- end
- -- Init
- term.clear()
- term.setCursorPos(1, 1)
- print("Skip_21's Smart Helmet " .. version .. " running !")
- sleep(2)
- if (os.getComputerLabel() == nil) then
- print("Setting computer label to 'SHClient'.")
- os.setComputerLabel("SHClient")
- end
- sides = peripheral.getNames()
- modemhere = false
- for i = 1, #sides do
- thistype = peripheral.getType(sides[i])
- if thistype == "modem" then
- print("Modem found on " .. sides[i] .. ", connecting...")
- rednet.open(sides[i])
- modemhere = true
- end
- end
- if not modemhere then
- error("Please connect a Wireless Modem")
- end
- arserver = rednet.lookup("arserver", "arserver")
- while arserver == nil do
- fails = fails + 1
- print("Can't find SHserver. Waiting ... (" .. fails .. ")")
- os.sleep(10)
- arserver = rednet.lookup("arserver", "arserver")
- end
- print("Found SHServer! Sending handshake.")
- rednet.send(arserver, "hello", "handshake")
- print("Running ...")
- -- peripherals Lookup
- local count = 0
- for i = 1, #sides do
- if addperipheral(sides[i]) then
- count = count + 1
- end
- end
- if count == 0 then
- error("No recognised peripherals. Aborting.")
- else
- print("Attached to " .. count .. " recognised peripherals.")
- end
- -- main loop
- timedmessage = os.startTimer(5)
- while true do
- eventlistener()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement