Advertisement
enderpro100

Elevator

Apr 24th, 2023 (edited)
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.71 KB | Gaming | 0 0
  1. --- Program : farming
  2. --- Author : LightKnight51
  3. --- last modification : 30/04/2023
  4.  
  5.  
  6. --- Utils API
  7.  
  8. -- Variables
  9. local LibraryName = "MarquitoLuaUtils"
  10. local LibraryCode = "mVTSwvw1"
  11. local ElevatorRednetProtocol = "Marquito.Elevator"
  12. local ElevatorLevel = -1
  13. local NumberOfLevels = -1
  14. local ElevatorName = ""
  15. local RedstoneSides = {"front", "bottom"}
  16. -- ~= 570 / 600 ms for each level before (or more ?)
  17. local ConstantTimeBetweenEachLevel = 0.8
  18.  
  19. -- Unlock current elevator level
  20. function UnlockLevel(sides)
  21.     for _, side in ipairs(sides) do
  22.         redstone.setOutput(side, false)
  23.         sleep(0.2)
  24.     end
  25. end
  26.  
  27. -- Lock current elevator level
  28. function LockLevel(side)
  29.     redstone.setOutput(side, true)
  30. end
  31.  
  32. function Push(side)
  33.     --sleep(ConstantTimeBetweenEachLevel)
  34.     redstone.setOutput(side, true)
  35.     sleep(0.3)
  36.     redstone.setOutput(side, false)
  37. end
  38.  
  39. -- Calculate time before start launch up
  40. function CalculateTimeForLaunchUp(startLevel)
  41.     local timeBeforeLaunch = 0
  42.  
  43.     if startLevel < ElevatorLevel then
  44.         timeBeforeLaunch = (ElevatorLevel - startLevel) * (ConstantTimeBetweenEachLevel + ((ElevatorLevel - startLevel) * 0.02))
  45.     end
  46.  
  47.     return timeBeforeLaunch
  48. end
  49.  
  50. -- Calculate time before start launch down
  51. function CalculateTimeForLaunchDown(startLevel)
  52.     local timeBeforeLaunch = 0
  53.  
  54.     timeBeforeLaunch = (startLevel - ElevatorLevel) * 1.2
  55.  
  56.     return timeBeforeLaunch
  57. end
  58.  
  59. function ElevatorAskConfData()
  60.     print("Computer ID : " .. os.getComputerID())
  61.     -- Set the ElevatorLevel from the conf
  62.     ElevatorLevel = tonumber(MarquitoLuaUtils.GetConfValue("elevator_level"))
  63.     while ElevatorLevel == nil or ElevatorLevel < 0 or ElevatorLevel == "" do
  64.         print("What is the level of this elevator level ?")
  65.         ElevatorLevel = tonumber(read())
  66.     end
  67.     MarquitoLuaUtils.SetConfValue("elevator_level", ElevatorLevel)
  68.  
  69.     -- Set the number of levels from the conf
  70.     NumberOfLevels = tonumber(MarquitoLuaUtils.GetConfValue("number_of_levels"))
  71.     while NumberOfLevels == nil or NumberOfLevels < 0 or NumberOfLevels == "" do
  72.         print("What is the number of elevator levels ?")
  73.         NumberOfLevels = tonumber(read())
  74.     end
  75.     MarquitoLuaUtils.SetConfValue("number_of_levels", NumberOfLevels)
  76.  
  77.     -- Set the elevator name from the conf
  78.     ElevatorName = tostring(MarquitoLuaUtils.GetConfValue("elevator_name"))
  79.     while ElevatorName == nil or ElevatorName == "" do
  80.         print("What is the elevator name ?")
  81.         ElevatorName = tostring(read())
  82.     end
  83.     MarquitoLuaUtils.SetConfValue("elevator_name", ElevatorName)
  84. end
  85.  
  86. function GetElevatorRednetProtocol()
  87.     return ElevatorRednetProtocol .. "." .. ElevatorName
  88. end
  89.  
  90. function LaunchElevatorMain()
  91.     if MarquitoLuaUtils.OpenWirelessRednetModem() then
  92.         LockLevel("bottom")
  93.         while true do
  94.             local data = MarquitoLuaUtils.ReceiveDataFromRednet(GetElevatorRednetProtocol())
  95.             ManageElevator(data)
  96.         end
  97.     end
  98. end
  99.  
  100. function ManageElevator(data)
  101.     if data ~= nil then
  102.         local startLevel, endLevel = 0
  103.         local i = 0
  104.         for value in (data .. "|"):gmatch("([^|]*)|") do
  105.             if i == 0 then
  106.                 startLevel = tonumber(value)
  107.             elseif i == 1 then
  108.                 endLevel = tonumber(value)
  109.             else
  110.                 break
  111.             end
  112.             i = i + 1
  113.         end
  114.         if startLevel < endLevel then
  115.             -- We want to up
  116.             if ElevatorLevel <= endLevel then
  117.                 if startLevel == ElevatorLevel then
  118.                     sleep(0.4)
  119.                     Push("front")
  120.                 elseif ElevatorLevel ~= 0 and startLevel < ElevatorLevel then
  121.                     -- Unlock
  122.                     UnlockLevel(RedstoneSides)
  123.                     -- If start level is before the current level, launch sleep
  124.                     sleep(CalculateTimeForLaunchUp(startLevel))
  125.                     -- Lock
  126.                     LockLevel("bottom")
  127.                     if ElevatorLevel < endLevel then
  128.                         -- Push
  129.                         Push("front")
  130.                     end
  131.                 end
  132.             end
  133.         elseif startLevel > endLevel then
  134.             -- We want to down
  135.             if ElevatorLevel > endLevel and ElevatorLevel <= startLevel then
  136.                 -- Unlock
  137.                 sleep(CalculateTimeForLaunchDown(startLevel))
  138.                 UnlockLevel(RedstoneSides)
  139.                 -- Lock
  140.                 sleep(3)
  141.                 LockLevel("bottom")
  142.             else
  143.  
  144.             end
  145.         end
  146.     end
  147. end
  148.  
  149. function LaunchFloorScreenProgram()
  150.     -- Find screen
  151.     local elevatorScreen = MarquitoLuaUtils.FindScreen()
  152.     elevatorScreen.setTextScale(0.5)
  153.     elevatorScreen.clear()
  154.  
  155.     local monitorSide
  156.     for _,name in ipairs(peripheral.getNames()) do   --gets the side the monitor is on
  157.        if peripheral.getType(name) == "monitor" then
  158.           monitorSide = name
  159.        end
  160.     end
  161.    
  162.     -- Adjust screen size
  163.     local monitorWidth, monitorHeight = MarquitoLuaUtils.GetScreenSize(elevatorScreen)
  164.     if monitorWidth >= 36 and monitorHeight >= 24 then
  165.         elevatorScreen.setTextScale(1)
  166.         monitorWidth, monitorHeight = MarquitoLuaUtils.GetScreenSize(elevatorScreen)
  167.     else
  168.         elevatorScreen.setTextScale(0.5)
  169.     end
  170.     elevatorScreen.clear()
  171.     term.setCursorPos(1,1)
  172.  
  173.     local buttonsPerColumn = math.floor((monitorHeight) / 2)
  174.     local numberOfColumns = math.min(math.ceil(NumberOfLevels / buttonsPerColumn), 3)
  175.     local maxButtonsInOnePage = buttonsPerColumn * 3
  176.     local numberOfPages = math.ceil(NumberOfLevels / maxButtonsInOnePage)
  177.     local buttonWidth = monitorWidth - 2
  178.  
  179.     -- The current page
  180.     local currentPage = 1
  181.  
  182.     buttonWidth, currentPage = SettingScreen(monitorWidth, monitorHeight, numberOfColumns, 1, numberOfPages)
  183.     -- Manage pages
  184.     local page = {}
  185.     for i = 1, numberOfPages do
  186.         page[i] = touchpoint.new(monitorSide)  
  187.         -- Add pages
  188.         if i ~= numberOfPages then
  189.             page[i]:add(">>", nil, monitorWidth-3, monitorHeight, monitorWidth, monitorHeight, colors.black, colors.white)
  190.         end
  191.         if i ~= 1 then
  192.             page[i]:add("<<", nil, 1, monitorHeight, 4, monitorHeight, colors.black, colors.white)
  193.         end
  194.         if numberOfPages > 1 then
  195.             local pageLabel
  196.             if monitorWidth == 15 then
  197.                 pageLabel = "P." .. i
  198.             else                      
  199.                 -- Add page number
  200.                 pageLabel = "Page " .. i
  201.             end
  202.             page[i]:add(pageLabel, nil, 5, monitorHeight, monitorWidth-4, monitorHeight, colors.black, colors.black)
  203.         end
  204.     end
  205.     -- Manage buttons
  206.  
  207.     local minX = 2
  208.     local minY = 1
  209.     local maxX = monitorWidth - 1
  210.     local maxY = minY
  211.  
  212.     local pageIndex = 1
  213.     local topFloor = NumberOfLevels - 1
  214.     if topFloor < buttonsPerColumn then
  215.         for i = topFloor, 0, -1 do
  216.             local buttonColor = colors.red
  217.             if i == ElevatorLevel then
  218.                 buttonColor = colors.blue
  219.             end
  220.             page[pageIndex]:add(tostring(i), nil, minX, minY, maxX, maxY, buttonColor, colors.lime)   --adds buttons in reverse
  221.             minY = minY + 2
  222.             maxY = minY
  223.         end
  224.      else
  225.         minX = 2
  226.         maxX = minX + buttonWidth
  227.         minY = monitorHeight - 1
  228.         maxY = minY
  229.         for i = 0, topFloor do
  230.             local buttonColor = colors.red
  231.             if i == ElevatorLevel then
  232.                 buttonColor = colors.blue
  233.             end
  234.             page[pageIndex]:add(tostring(i), nil, minX, minY, maxX, maxY, buttonColor, colors.lime)
  235.             local remainingFloors = topFloor - i
  236.             minY = maxY - 2
  237.             maxY = minY
  238.             if maxY <= 0 then  
  239.                 -- Move buttons to the next column
  240.                 minX = maxX + 2
  241.                 maxX = minX + buttonWidth
  242.                 minY = monitorHeight - 1
  243.                 maxY = minY
  244.                 if maxX > monitorWidth then  
  245.                     -- Change to next page
  246.                     pageIndex = pageIndex + 1
  247.                     minX = 2
  248.                     maxX = minX + buttonWidth
  249.                     minY = monitorHeight - 1
  250.                     maxY = minY
  251.                 end
  252.                 if remainingFloors < buttonsPerColumn then  
  253.                     -- Moves the buttons up if there are spaces
  254.                     minY = monitorHeight - 1 - ((buttonsPerColumn - remainingFloors) * 2)
  255.                     maxY = minY
  256.                 end
  257.             end
  258.         end
  259.      end
  260.  
  261.     -- Floor choice loop
  262.     while true do
  263.         if MarquitoLuaUtils.OpenWirelessRednetModem() then
  264.             -- Draws the buttons on the monitor
  265.             page[currentPage]:draw()  
  266.             local event, p1 = page[currentPage]:handleEvents(os.pullEvent())
  267.             -- Wait for button clicks
  268.             if event == "button_click" then  
  269.                 local chosen = tonumber(p1)
  270.                 page[currentPage]:flash(p1)
  271.                 if chosen ~= nil then
  272.                     MarquitoLuaUtils.BroadcastDataWithRednet(ElevatorLevel .. "|" .. chosen, GetElevatorRednetProtocol())
  273.                     MarquitoLuaUtils.SendDataWithRednetForOneDevice(os.getComputerID(), ElevatorLevel .. "|" .. chosen, GetElevatorRednetProtocol())
  274.                     --ManageElevator(ElevatorLevel .. "|" .. chosen)
  275.                 elseif p1 == ">>" then
  276.                     currentPage = currentPage + 1
  277.                 elseif p1 == "<<" then
  278.                     currentPage = currentPage - 1
  279.                 end
  280.             end
  281.         end
  282.     end
  283. end
  284.  
  285. function SettingScreen(monitorWidth, monitorHeight, numberOfColumns, currentPage, numberOfPages)
  286.     -- Calculate width of buttons
  287.     if NumberOfLevels * 2 >= monitorHeight then
  288.         buttonWidth = math.floor((monitorWidth - numberOfColumns - 3) / numberOfColumns)
  289.      end
  290.      -- Set the number of pages
  291.      if currentPage > numberOfPages then
  292.         currentPage = numberOfPages
  293.      end
  294.  
  295.      return buttonWidth, currentPage
  296. end
  297.  
  298. -- Download program
  299. function DownloadProgram(pastebinCode, programName)
  300.     if not fs.exists(programName) then
  301.         shell.run("pastebin get " .. pastebinCode .. " " .. programName)
  302.     end
  303. end
  304.  
  305. -- We need this library for other programs work
  306. DownloadProgram(LibraryCode, LibraryName)
  307. os.loadAPI("MarquitoLuaUtils")
  308. -- Library for level buttons on monitor
  309. DownloadProgram("pFHeia96", "touchpoint")
  310. os.loadAPI("touchpoint")
  311.  
  312. -- Program
  313. ElevatorAskConfData()
  314. parallel.waitForAny(LaunchElevatorMain, LaunchFloorScreenProgram)
  315.  
  316. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement