Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turbolift Door Client
- -- For use in the NCC-1701-D
- -- This client resides inside the turbolift
- -- doors. It talks to the controller
- -- to successfully operate the lift.
- -- This computer has a modem and a
- -- bundled cable attached to it.
- function loadSettings()
- settings = json.decode("/sys/turbolift/conf.json")
- if settings == nil then
- settings = {modemSide = "top", rsSide = "left", proxSide = "right", buttonBC = "left:blue", doorBC = "left:orange", name = "", deck = ""}
- end
- end
- function writeSettings()
- local s = fs.open("/sys/turbolift/conf.json", "w")
- s.write(json.encode(settings))
- s.close()
- end
- function onCallLift()
- -- this happens when the turbolift
- -- button is pressed
- os.queueEvent("tl_state_changed",2)
- state = 2
- local req = {doorId = settings.id, player = p , cmd = "find"}
- rednet.send(settings.serv, json.encode(req))
- end
- function onLiftReady(c)
- -- when the lift arrives at the door
- -- open the doors
- state = 3
- rs.setBundledOutput(rsSide, colors.blue)
- end
- function onPlayerEnter(player)
- -- when prox fires for player
- -- close the door and signal that the
- -- player is ready for the carriage
- state = 4
- rs.setBundlesOutput(rsSide, colors.subtract(rs.getBundledOutput(rsSide),colors.blue))
- local req = {cmd = "playerEnter", cabId = carriage, player = player}
- rednet.send(settings.serv, req))
- end
- function onPlayerSent()
- state = 1
- end
- function onExpectPlayer()
- state = 5
- end
- function onPlayerReceived(p)
- state = 6
- rs.setBundledOutput(rsSide, colors.blue)
- end
- function onPlayerExit(p)
- state = 1
- rs.setBundlesOutput(rsSide, colors.subtract(rs.getBundledOutput(rsSide),colors.blue))
- end
- function distance(pos)
- local xd = pos.X - settings.offset.X
- local yd = pos.Y - settings.offset.Y
- local zd = pos.Z - settings.offset.Z
- return math.sqrt(xd*xd + yd*yd + zd*zd)
- end
- -- this function tests the prox
- function getPlayers()
- local prox = peripheral.wrap(proxSide)
- while true do
- local ps = prox.getTargets()
- for k, v in pairs(ps) do
- if v.IsPlayer and distance(v.Position) > 2 then
- if state == 3 then
- -- doors are open and player
- -- walked in get then to the
- -- carriage
- onPlayerEnter(v.Username)
- elseif state == 5
- onPlayerReceived(v.Username)
- end
- elseif state == 6 then
- onPlayerExit(v.Username)
- end
- end
- sleep(2)
- end
- end
- -- this function tests the redstone
- function getRedstone()
- while true do
- local _ = os.pullEvent("tl_button")
- if state == 1 then
- onCallLift()
- end
- sleep(2)
- end
- end
- -- this function waits for rednet
- -- responses from the server
- function getRednet()
- rednet.open("modemSide")
- while true do
- local sId, msg, dist = os.pullEvent("rednet_message")
- if sId == settings.serv then
- local req = json.decode(msg)
- if req.cmd == "find" then
- if req.status == 202 then
- onLiftReady(req.cabId)
- else
- onCallLift()
- end
- elseif req.cmd == "enter" then
- onPlayerSent()
- elseIf req.cmd == "receive" then
- onExpectPlayer()
- end
- end
- end
- end
- function main()
- loadSettings()
- while true do
- parallel.waitForAny(getRednet, getRedstone, getPlayers)
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement