Advertisement
NixillUmbreon

ComputerCraft Elevator Turtle Code

Nov 19th, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.08 KB | None | 0 0
  1. -- Define some constants for the elevator - make editing for other elevators easier
  2.  
  3. -- Optionally give names to some floors. If a name is specified, it will be matched against this list to be converted to a floor number.
  4. floorNames =
  5. {lobby = 1, ground = 1, cafe = 7,
  6. penthouse = 19, basement = 0,
  7. cloudcafe = 7, library = 3,
  8. storage = 2, cave = -13}
  9.  
  10. -- The highest floor number the elevator can travel to.
  11. highFloor = 19
  12.  
  13. -- The lowest floor number to which the elevator can travel. Not required to be 1 - 1 usually represents a ground floor, and there can be basements and sub-basements as well.
  14. lowFloor = -13
  15.  
  16. -- The floor it is assumed the turtle is on if it can move no further down. This can be lower, but not higher, than lowFloor.
  17. shaftBottom = -13
  18.  
  19. -- Floors which cannot be chosen as elevator destinations. Attempting to take an elevator to such a floor from within will fail. Attempting to call the elevator to the floor from outside will still succeed.
  20. invalidFloors =
  21. {6, 8, 18, -10, -11, -12}
  22.  
  23. -- Use the above list as a blacklist instead of a blacklist.
  24. blacklistFloors = true
  25.  
  26. -- The distance in blocks an elevator must travel from one floor to be on the next.
  27. floorDistance = 6
  28.  
  29. -- Do lighting?
  30. doLighting = false
  31.  
  32. -- These functions are for moving.
  33. function u(m)
  34.   n = m or 1
  35.   for a = 1, n do
  36.     if turtle.detectUp() then
  37.       return false
  38.     end
  39.     if doLighting then rs.setOutput("front", false) end
  40.     while not turtle.up() do end
  41.     if doLighting then rs.setOutput("front", true) end
  42.   end
  43.   return true
  44. end
  45. function d(m)
  46.   n = m or 1
  47.   for a = 1, n do
  48.     if turtle.detectDown() then
  49.       return false
  50.     end
  51.     if doLighting then rs.setOutput("front", false) end
  52.     while not turtle.down() do end
  53.     if doLighting then rs.setOutput("front", true) end
  54.   end
  55.   return true
  56. end
  57.  
  58. currFloor = 0
  59. callToFloor = 0
  60.  
  61. function startup()
  62.   if doLighting then rs.setOutput("front", true)
  63.     else rs.setOutput("front", false) end
  64.   rednet.open("right")
  65.   rednet.broadcast("TO " .. shaftBottom)
  66.   rs.setOutput("left", true)
  67.   while d() do end
  68.   currFloor = shaftBottom
  69.   callToFloor = currFloor
  70.   rednet.broadcast("AT " .. currFloor)
  71.   run = true
  72.   while run do
  73.     callToFloor = -32768
  74.     rs.setOutput("back", true)
  75.     parallel.waitForAny(acceptCall, queryFloor)
  76.     rs.setOutput("back", false)
  77.     if callToFloor ~= -32768 then
  78.       travelToFloor(callToFloor)
  79.     else
  80.       run = false
  81.     end
  82.   end
  83. end
  84.  
  85. acceptCall = function()
  86.   sleep(10)
  87.   waitingForCall = true
  88.   while waitingForCall do
  89.     sender, message, distance = rednet.receive()
  90.     if string.sub(message, 1, 5) == "CALL " then
  91.       callToFloor = tonumber(string.sub(message, 6))
  92.       if callToFloor ~= nil then waitingForCall = false
  93.         else callToFloor = currFloor end
  94.     end
  95.   end
  96. end
  97.  
  98. queryFloor = function()
  99.   invalidInput = true
  100.   while invalidInput do
  101.     print("Enter the floor to which to travel: ")
  102.     query = io.read():lower()
  103.     if tonumber(query) == nil then query = floorNames[query]
  104.       else query = tonumber(query) end
  105.     if query ~= nil then
  106.       query = math.floor(query)
  107.       if query >= lowFloor and query <= highFloor then
  108.         invalidFloor = false
  109.         for a = 1, #invalidFloors do
  110.           if query == invalidFloors[a] then invalidFloor = true end
  111.         end
  112.         invalidInput = (invalidFloor and blacklistFloors) or (not invalidFloor and not blacklistFloors)
  113.       end
  114.     end
  115.     if invalidInput then print("I'm sorry but that is not a valid floor. Please try again.") end
  116.   end
  117.   callToFloor = query
  118. end
  119.  
  120. function travelToFloor(destination)
  121.   print("Traveling to floor " .. destination)
  122.   sleep(0.1)
  123.   rednet.broadcast("TO " .. destination)
  124.   while currFloor ~= destination do
  125.     if currFloor > destination then travelOneFloor(false)
  126.     else travelOneFloor(true) end
  127.   end
  128. end
  129.  
  130. function travelOneFloor(up)
  131.   if up then
  132.     u(floorDistance)
  133.     currFloor = currFloor + 1
  134.   else
  135.     d(floorDistance)
  136.     currFloor = currFloor - 1
  137.   end
  138.   rednet.broadcast("AT " .. currFloor)
  139. end
  140.  
  141. startup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement