Advertisement
LBPHacker

Elevator itself for tysciman7

Jun 8th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.01 KB | None | 0 0
  1. -- * modem side here
  2. rednet.open("top")
  3.  
  4. -- * current floor - might not be accurate
  5. -- * since you have three computers (the ones that call the elevator),
  6. --   you could use some GPS code to determine the current floor
  7. local current = 1
  8.  
  9. local floorIDs = {
  10.     -- * ["sender IDs in strings"] = the floors they are on
  11.     -- * you can expand this table further
  12.     ["40"] = 1,
  13.     ["41"] = 2,
  14.     ["42"] = 3,
  15. }
  16.  
  17. local gotoFloor = function(target)
  18.     repeat
  19.         if current > target then -- target is lower ...
  20.             for i = 1, 12 do -- ... so go down
  21.                 rs.setOutput("right", true)
  22.                 sleep(0.5)
  23.                 rs.setOutput("right", false)
  24.                 sleep(0.5)
  25.                 rs.setOutput("back", true)
  26.                 sleep(0.5)
  27.                 rs.setOutput("back", false)
  28.                 sleep(0.5)
  29.             end
  30.             current = current - 1
  31.         elseif current < target then -- target is higher ...
  32.             for i = 1, 12 do -- ... so go up
  33.                 rs.setOutput("left", true)
  34.                 sleep(0.5)
  35.                 rs.setOutput("left", false)
  36.                 sleep(0.5)
  37.                 rs.setOutput("buttom", true)
  38.                 sleep(0.5)
  39.                 rs.setOutput("buttom", false)
  40.                 sleep(0.5)
  41.             end
  42.             current = current + 1
  43.         end
  44.     until target == current -- we've reached the target
  45. end
  46.  
  47. while true do
  48.     local eventData = {os.pullEvent()}
  49.     -- * now eventData[2] is the sender and eventData[3] is the message
  50.     if eventData[1] == "rednet_message" and eventData[3] == "elevator_call" then
  51.         -- * every computer will send only "elevator_call"
  52.         local target = floorIDs[tostring(eventData[2])]
  53.         if target then
  54.             -- * call gotoFloor only if target is not nil
  55.             -- * that would mean that a computer which is not in the floorIDs table
  56.             --   above sent the "elevator_call" message
  57.             gotoFloor(target)
  58.         end
  59.     end
  60. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement