maxsar

elevator for create and cc

Jun 1st, 2022 (edited)
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.31 KB | None | 0 0
  1. --setup : rope pulley, monitor in elevator connected by wired network, motor direct force to pulley, advanced computer
  2. engine = peripheral.find("electric_motor")
  3. monitor = peripheral.find("monitor")
  4. modem = peripheral.find( "modem" )
  5.  
  6. modem.open(420)
  7. position = 0 --current position
  8. speed = 128 --normal speed
  9. block = 152.5 --degree per block
  10. floors = {} --floors table
  11. fcount = 0 --amount of floors
  12.  
  13. function startElevator()
  14.     height = 256
  15.     engine.setSpeed(256)
  16.     print("Setting up elevator, its gonna take " .. engine.rotate(256 * block) .. " seconds.")
  17.     monitor.setBackgroundColor(colors.red)
  18.     monitor.setTextColor(colors.white)
  19.     monitor.clear()
  20.     monWrite("Setting",1,2)
  21.     monWrite("things",1,3)
  22.     monWrite("up...",2,4)
  23.     sleep(engine.rotate(block * height))
  24.     position = 0
  25.     engine.stop()
  26.     print("position = 0")
  27.     sleep(1)
  28.     print("Everything set up.")
  29. end
  30.  
  31. function setPos(pos)
  32.     vec = pos - position
  33.     speedTemp = 1
  34.     if vec > 0 then
  35.         speedTemp = -speed
  36.     elseif vec < 0 then
  37.         speedTemp = speed
  38.     else
  39.         speedTemp = 0
  40.     end
  41.     --print("Vector : " .. math.abs(vec) - slow - superslow)
  42.     engine.setSpeed(speedTemp)
  43.     sleep(engine.rotate(block * math.abs(vec)))
  44.  
  45.     engine.stop()
  46.     print("Pos: " .. position .. " to pos: " .. position + vec)
  47.     position = position + vec
  48. end  
  49.  
  50. function addFloor(y)
  51.     print("Floor " .. fcount .. " set to " .. y .. " block.")
  52.     floors[fcount] = y
  53.     fcount = fcount + 1
  54. end
  55.  
  56. function goFloor(fnumber)
  57.     setPos(floors[fnumber])
  58.     print("Going to floor " .. fnumber .. " block " .. floors[fnumber] .. ".")
  59. end
  60.  
  61. function clearFloors()
  62.     for i = 0,fcount,1 do
  63.         floors[i] = nil
  64.     end
  65.     print("Floors cleared from ram.")
  66. end
  67.  
  68. function saveFloors()
  69.     local savefile = fs.open("floors","w")
  70.     for i = 0,fcount - 1,1 do
  71.         if floors[i] ~= nil then
  72.             savefile.writeLine(floors[i])
  73.         end
  74.     end
  75.     savefile.close()
  76.     print("Floors saved.")
  77. end
  78.  
  79. function loadFloors()
  80.     local savefile = fs.open("floors","r")
  81.     local line = "hehepenis"
  82.     fcount = 0
  83.     while line ~= nil do
  84.         line = savefile.readLine()
  85.         if line ~= nil then
  86.             floors[fcount] = tonumber(line)
  87.             print("Floor  " .. fcount .. " block " .. line .. " added.")
  88.             fcount = fcount + 1
  89.         end
  90.     end
  91.     savefile.close()
  92.     print("Floors loaded.")
  93. end
  94.  
  95. function monWrite(str,x,y)
  96.     if monitor then
  97.         monitor.setCursorPos(x, y)
  98.         monitor.write(str)
  99.     end
  100. end
  101.  
  102. function monitorStart()
  103.     if monitor then
  104.         monitor.setBackgroundColor(colors.black)
  105.         monitor.clear()
  106.         monitor.setBackgroundColor(colors.white)
  107.         monitor.setTextColor(colors.black)
  108.         monitor.setCursorBlink(false)
  109.         for i = 0 , fcount - 1,1 do
  110.             --print(i)
  111.             if i+1 <= 5 then
  112.                 monWrite(tostring(i),3,i+1)
  113.             elseif i+1 > 5 and i+1 < 10 then
  114.                 monWrite(tostring(i),5,i+1-5)
  115.             end
  116.         end
  117.     end
  118. end
  119.  
  120. function monitorPress(x,y)
  121.     for i = 0,fcount - 1,1 do
  122.         if x == 3 and y == i+1 then
  123.             monWrite("X",3,i+1)
  124.             goFloor(i)
  125.         elseif x == 5 and y == i+1-5 then
  126.             monWrite("X",5,i+1-5)
  127.             goFloor(i)
  128.         end
  129.     end
  130. end
  131.  
  132. function main()
  133.     loadFloors()
  134.     saveFloors()
  135.     startElevator()
  136.     while true do
  137.         sleep(0.5)
  138.         monitorStart()
  139.         event, side, xPos, yPos, message = os.pullEvent()
  140.         if event == "monitor_touch" then
  141.             monitorPress(xPos,yPos)
  142.         elseif event == "modem_message" then
  143.             goFloor(message)
  144.         end
  145.     end
  146. end
  147.  
  148. main()
Add Comment
Please, Sign In to add comment