Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sleep(1)
- sensor = peripheral.wrap("top")
- rednet.open("left")
- securityLevel = 0
- friendly = {"acesoyster", "JPROSP8"}
- host = nil
- function requestSecurityLevel()
- rednet.send(host, "updateRequest", "securityControl")
- return
- end
- function setDoor(state)
- if state == "open" then
- rs.setOutput("front", false)
- rs.setOutput("back", false)
- elseif state == "close" then
- rs.setOutput("front", true)
- rs.setOutput("back", true)
- else
- print("Unknown door state")
- end
- end
- function level1Logic() --Always open
- while true do
- setDoor("open")
- coroutine.yield()
- end
- end
- function level2Logic() --Open unless mobs are nearby
- while true do
- local state = "open"
- local mobs = sensor.getMobIds()
- if #mobs > 0 then
- state = "close"
- end
- local players = sensor.getPlayerNames()
- if #players > 0 then
- state = "open"
- end
- setDoor(state)
- coroutine.yield()
- end
- end
- function level3Logic() --Open unless mobs or enemy players are nearby
- while true do
- local state = "open"
- local safe = false --Carries a successful player match past the for statement
- local mobs = sensor.getMobIds()
- if #mobs > 0 then
- state = "close"
- end
- local players = sensor.getPlayerNames()
- if #players > 0 then
- for i = 1, #players, 1 do
- for j = 1, #friendly, 1 do
- if players[i] == friendly[j] then
- safe = true
- else
- state = "close"
- end
- end
- end
- if safe == true then
- state = "open"
- end
- end
- setDoor(state)
- coroutine.yield()
- end
- end
- function level4Logic() --Open only if friendlies are nearby
- while true do
- local state = "close"
- local mobs = sensor.getMobIds()
- local players = sensor.getPlayerNames()
- if #players > 0 then
- for i = 1, #players, 1 do
- for j = 1, #friendly, 1 do
- if players[i] == friendly[j] then
- state = "open"
- end
- end
- end
- end
- setDoor(state)
- end
- end
- function level5Logic() --Exclusively respond to rednet control, and report nearby friendly playernames
- setDoor("close")
- parallel.waitForAny(level5RednetHandler, level5ProximityHandler)
- end
- function level5RednetHandler()
- while true do
- if host then
- sender, message = rednet.receive("securityControl")
- if (message == "open") and (sender == host) then
- setDoor("open")
- coroutine.yield()
- elseif (message == "close") and (sender == host) then
- setDoor("close")
- coroutine.yield()
- end
- end
- end
- end
- function level5ProximityHandler()
- while true do
- local players = sensor.getPlayerNames()
- if #players > 0 then
- for i = 1, #players, 1 do
- for j = 1, #friendly, 1 do
- if players[i] == friendly[j] then
- if host then
- rednet.send(host, friendly[j], "securityControl")
- sleep(3)
- end
- end
- end
- end
- end
- end
- end
- function defaultLogic()
- level1Logic()
- end
- function rednetHandler()
- while true do
- if host then
- sender, message = rednet.receive("securityControl", 10)
- if message == nil then
- return
- end
- if (string.find(message, "level")) and (sender == host) then
- number = string.gsub(message, "level", "")
- securityLevel = tonumber(number)
- print("Change to security level " .. securityLevel)
- return
- end
- else
- sleep(5)
- return
- end
- end
- end
- function findHost()
- host = rednet.lookup("securityControl", "master")
- if host then
- rednet.send(host, "door", "securityControlConnect")
- sender, message = rednet.receive("securityControlConnect", 20)
- if sender == host then
- print("Connected to " .. host)
- else
- host = nil
- end
- end
- end
- findHost()
- if host then
- requestSecurityLevel()
- end
- while true do
- if securityLevel == 1 then
- parallel.waitForAny(level1Logic, rednetHandler)
- elseif securityLevel == 2 then
- parallel.waitForAny(level2Logic, rednetHandler)
- elseif securityLevel == 3 then
- parallel.waitForAny(level3Logic, rednetHandler)
- elseif securityLevel == 4 then
- parallel.waitForAny(level4Logic, rednetHandler)
- elseif securityLevel == 5 then
- parallel.waitForAny(level5Logic, rednetHandler)
- else
- parallel.waitForAny(defaultLogic, rednetHandler)
- end
- if not host then
- findHost()
- if host then
- requestSecurityLevel()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement