Advertisement
RavenSH4

LiftClient

May 15th, 2022 (edited)
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.08 KB | None | 0 0
  1. --VAR
  2. ThisLevel = 0
  3. ----
  4. IsReady = false -- lift is ready, don't moving and await for commands
  5. CurrentLevel = 0
  6. TargetLevel = 0
  7. SensorSide = "bottom"
  8. b_start_x = 1
  9. b_start_y = 1
  10. b_width = 6
  11. b_height = 3
  12. --initialize
  13. monitor = peripheral.wrap("top")
  14. modem = peripheral.wrap("back")
  15. modem.open(21)
  16. ------------------
  17.  
  18. ---lift client start up
  19. function Initialize()
  20.   monitor.clear()
  21.   ProcessRedstone()
  22.   WriteButton()
  23.   modem.transmit(21, 22, "request")
  24. end
  25.  
  26. --
  27. function ProcessCommand(command)
  28.   write("received=")
  29.   print(command)
  30.   --
  31.   if (command == "moving 0") then
  32.     IsReady = false
  33.     TargetLevel = 0
  34.   end
  35.   if (command == "moving 1") then
  36.     IsReady = false
  37.     TargetLevel = 1
  38.   end
  39.   if (command == "contact 0") then
  40.     CurrentLevel = 0
  41.   end
  42.   if (command == "contact 1") then
  43.     CurrentLevel = 1
  44.   end
  45.   --
  46.   if (IsReady) then
  47.     if (command == "lock 0 UP") then
  48.         --
  49.     end
  50.     if (command == "lock 0 DOWN") then
  51.         --
  52.     end
  53.   end
  54.   --if IsReady == false
  55.   if (command == "ready" or command == "init") then
  56.     IsReady = true
  57.   end
  58.   WriteButton()
  59. end
  60.  
  61. --
  62. function ProcessRedstone()
  63.   detector = rs.getInput(SensorSide)
  64.   if (detector == true) then
  65.     CurrentLevel = ThisLevel
  66.     modem.transmit(21, 22, "contact "..ThisLevel)
  67.     WriteButton()
  68.   end
  69. end
  70.  
  71. --Write On/Off button
  72. function WriteButton()
  73.     if (IsReady == false) then
  74.       monitor.clear()
  75.       return
  76.     end
  77.    
  78.     monitor.setTextColor(colors.white)
  79.     if (CurrentLevel == ThisLevel) then
  80.         monitor.setBackgroundColour((colours.lime))
  81.         monitor.setCursorPos(b_start_x,b_start_y)
  82.         monitor.write("      ")
  83.         monitor.setCursorPos(b_start_x,b_start_y+1)
  84.         monitor.write(" MOVE ")
  85.         monitor.setCursorPos(b_start_x,b_start_y+2)
  86.         monitor.write("      ")
  87.     else
  88.         monitor.setBackgroundColour((colours.red))
  89.         monitor.setCursorPos(b_start_x,b_start_y)
  90.         monitor.write("      ")
  91.         monitor.setCursorPos(b_start_x,b_start_y+1)
  92.         monitor.write(" CALL ")
  93.         monitor.setCursorPos(b_start_x,b_start_y+2)
  94.         monitor.write("      ")
  95.     end
  96.     monitor.setBackgroundColour((colours.black))  
  97. end
  98.  
  99. --
  100. function ProcessClick(mouseX,  mouseY)
  101.   if (IsReady == false) then
  102.     return
  103.   end
  104.    
  105.   if (mouseX >= b_start_x and mouseX < b_start_x + b_width and mouseY > b_start_y and mouseY < b_start_y + b_height) then
  106.     if (CurrentLevel == ThisLevel) then
  107.       TargetLevel = 1
  108.       modem.transmit(21, 22, "call 1")
  109.     else
  110.       TargetLevel = ThisLevel --0
  111.       modem.transmit(21, 22, "call "..ThisLevel )
  112.     end
  113.   end
  114. end
  115.  
  116. --initialize
  117. Initialize()
  118. --Main loop
  119. repeat
  120.     event,p1,p2,p3,p4,p5 = os.pullEvent()
  121.     --
  122.     if (event=="redstone") then
  123.       ProcessRedstone()
  124.     end
  125.     if (event=="modem_message") then
  126.         --event, side, channel, replyChannel, message, distance
  127.         ProcessCommand(p4)
  128.     end
  129.     if event=="monitor_touch" then
  130.         ProcessClick(p2, p3) -- this runs our function
  131.     end
  132. until event=="char" and p1==("q")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement