Advertisement
klindley

door

Jan 28th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. -- Turbolift Door Client
  2. -- For use in the NCC-1701-D
  3. -- This client resides inside the turbolift
  4. -- doors. It talks to the controller
  5. -- to successfully operate the lift.
  6. -- This computer has a modem and a
  7. -- bundled cable attached to it.
  8.  
  9. function loadSettings()
  10. settings = json.decode("/sys/turbolift/conf.json")
  11. if settings == nil then
  12. settings = {modemSide = "top", rsSide = "left", proxSide = "right", buttonBC = "left:blue", doorBC = "left:orange", name = "", deck = ""}
  13. end
  14. end
  15.  
  16. function writeSettings()
  17. local s = fs.open("/sys/turbolift/conf.json", "w")
  18. s.write(json.encode(settings))
  19. s.close()
  20. end
  21.  
  22. function onCallLift()
  23. -- this happens when the turbolift
  24. -- button is pressed
  25. os.queueEvent("tl_state_changed",2)
  26. state = 2
  27. local req = {doorId = settings.id, player = p , cmd = "find"}
  28. rednet.send(settings.serv, json.encode(req))
  29. end
  30.  
  31. function onLiftReady(c)
  32. -- when the lift arrives at the door
  33. -- open the doors
  34. state = 3
  35. rs.setBundledOutput(rsSide, colors.blue)
  36. end
  37.  
  38. function onPlayerEnter(player)
  39. -- when prox fires for player
  40. -- close the door and signal that the
  41. -- player is ready for the carriage
  42. state = 4
  43. rs.setBundlesOutput(rsSide, colors.subtract(rs.getBundledOutput(rsSide),colors.blue))
  44. local req = {cmd = "playerEnter", cabId = carriage, player = player}
  45. rednet.send(settings.serv, req))
  46. end
  47.  
  48. function onPlayerSent()
  49. state = 1
  50. end
  51.  
  52. function onExpectPlayer()
  53. state = 5
  54. end
  55.  
  56. function onPlayerReceived(p)
  57. state = 6
  58. rs.setBundledOutput(rsSide, colors.blue)
  59. end
  60.  
  61. function onPlayerExit(p)
  62. state = 1
  63. rs.setBundlesOutput(rsSide, colors.subtract(rs.getBundledOutput(rsSide),colors.blue))
  64. end
  65.  
  66.  
  67. function distance(pos)
  68. local xd = pos.X - settings.offset.X
  69. local yd = pos.Y - settings.offset.Y
  70. local zd = pos.Z - settings.offset.Z
  71. return math.sqrt(xd*xd + yd*yd + zd*zd)
  72. end
  73.  
  74. -- this function tests the prox
  75. function getPlayers()
  76. local prox = peripheral.wrap(proxSide)
  77. while true do
  78. local ps = prox.getTargets()
  79. for k, v in pairs(ps) do
  80. if v.IsPlayer and distance(v.Position) > 2 then
  81. if state == 3 then
  82. -- doors are open and player
  83. -- walked in get then to the
  84. -- carriage
  85. onPlayerEnter(v.Username)
  86. elseif state == 5
  87. onPlayerReceived(v.Username)
  88. end
  89. elseif state == 6 then
  90. onPlayerExit(v.Username)
  91. end
  92. end
  93. sleep(2)
  94. end
  95. end
  96.  
  97. -- this function tests the redstone
  98. function getRedstone()
  99. while true do
  100. local _ = os.pullEvent("tl_button")
  101. if state == 1 then
  102. onCallLift()
  103. end
  104. sleep(2)
  105. end
  106. end
  107.  
  108. -- this function waits for rednet
  109. -- responses from the server
  110. function getRednet()
  111. rednet.open("modemSide")
  112. while true do
  113. local sId, msg, dist = os.pullEvent("rednet_message")
  114. if sId == settings.serv then
  115. local req = json.decode(msg)
  116.  
  117. if req.cmd == "find" then
  118. if req.status == 202 then
  119. onLiftReady(req.cabId)
  120. else
  121. onCallLift()
  122. end
  123.  
  124. elseif req.cmd == "enter" then
  125. onPlayerSent()
  126. elseIf req.cmd == "receive" then
  127. onExpectPlayer()
  128. end
  129. end
  130.  
  131. end
  132. end
  133.  
  134. function main()
  135. loadSettings()
  136. while true do
  137. parallel.waitForAny(getRednet, getRedstone, getPlayers)
  138. end
  139.  
  140. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement