Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- Program : farming
- --- Author : LightKnight51
- --- last modification : 30/04/2023
- --- Utils API
- -- Variables
- local LibraryName = "MarquitoLuaUtils"
- local LibraryCode = "mVTSwvw1"
- local ElevatorRednetProtocol = "Marquito.Elevator"
- local ElevatorLevel = -1
- local NumberOfLevels = -1
- local ElevatorName = ""
- local RedstoneSides = {"front", "bottom"}
- -- ~= 570 / 600 ms for each level before (or more ?)
- local ConstantTimeBetweenEachLevel = 0.8
- -- Unlock current elevator level
- function UnlockLevel(sides)
- for _, side in ipairs(sides) do
- redstone.setOutput(side, false)
- sleep(0.2)
- end
- end
- -- Lock current elevator level
- function LockLevel(side)
- redstone.setOutput(side, true)
- end
- function Push(side)
- --sleep(ConstantTimeBetweenEachLevel)
- redstone.setOutput(side, true)
- sleep(0.3)
- redstone.setOutput(side, false)
- end
- -- Calculate time before start launch up
- function CalculateTimeForLaunchUp(startLevel)
- local timeBeforeLaunch = 0
- if startLevel < ElevatorLevel then
- timeBeforeLaunch = (ElevatorLevel - startLevel) * (ConstantTimeBetweenEachLevel + ((ElevatorLevel - startLevel) * 0.02))
- end
- return timeBeforeLaunch
- end
- -- Calculate time before start launch down
- function CalculateTimeForLaunchDown(startLevel)
- local timeBeforeLaunch = 0
- timeBeforeLaunch = (startLevel - ElevatorLevel) * 1.2
- return timeBeforeLaunch
- end
- function ElevatorAskConfData()
- print("Computer ID : " .. os.getComputerID())
- -- Set the ElevatorLevel from the conf
- ElevatorLevel = tonumber(MarquitoLuaUtils.GetConfValue("elevator_level"))
- while ElevatorLevel == nil or ElevatorLevel < 0 or ElevatorLevel == "" do
- print("What is the level of this elevator level ?")
- ElevatorLevel = tonumber(read())
- end
- MarquitoLuaUtils.SetConfValue("elevator_level", ElevatorLevel)
- -- Set the number of levels from the conf
- NumberOfLevels = tonumber(MarquitoLuaUtils.GetConfValue("number_of_levels"))
- while NumberOfLevels == nil or NumberOfLevels < 0 or NumberOfLevels == "" do
- print("What is the number of elevator levels ?")
- NumberOfLevels = tonumber(read())
- end
- MarquitoLuaUtils.SetConfValue("number_of_levels", NumberOfLevels)
- -- Set the elevator name from the conf
- ElevatorName = tostring(MarquitoLuaUtils.GetConfValue("elevator_name"))
- while ElevatorName == nil or ElevatorName == "" do
- print("What is the elevator name ?")
- ElevatorName = tostring(read())
- end
- MarquitoLuaUtils.SetConfValue("elevator_name", ElevatorName)
- end
- function GetElevatorRednetProtocol()
- return ElevatorRednetProtocol .. "." .. ElevatorName
- end
- function LaunchElevatorMain()
- if MarquitoLuaUtils.OpenWirelessRednetModem() then
- LockLevel("bottom")
- while true do
- local data = MarquitoLuaUtils.ReceiveDataFromRednet(GetElevatorRednetProtocol())
- ManageElevator(data)
- end
- end
- end
- function ManageElevator(data)
- if data ~= nil then
- local startLevel, endLevel = 0
- local i = 0
- for value in (data .. "|"):gmatch("([^|]*)|") do
- if i == 0 then
- startLevel = tonumber(value)
- elseif i == 1 then
- endLevel = tonumber(value)
- else
- break
- end
- i = i + 1
- end
- if startLevel < endLevel then
- -- We want to up
- if ElevatorLevel <= endLevel then
- if startLevel == ElevatorLevel then
- sleep(0.4)
- Push("front")
- elseif ElevatorLevel ~= 0 and startLevel < ElevatorLevel then
- -- Unlock
- UnlockLevel(RedstoneSides)
- -- If start level is before the current level, launch sleep
- sleep(CalculateTimeForLaunchUp(startLevel))
- -- Lock
- LockLevel("bottom")
- if ElevatorLevel < endLevel then
- -- Push
- Push("front")
- end
- end
- end
- elseif startLevel > endLevel then
- -- We want to down
- if ElevatorLevel > endLevel and ElevatorLevel <= startLevel then
- -- Unlock
- sleep(CalculateTimeForLaunchDown(startLevel))
- UnlockLevel(RedstoneSides)
- -- Lock
- sleep(3)
- LockLevel("bottom")
- else
- end
- end
- end
- end
- function LaunchFloorScreenProgram()
- -- Find screen
- local elevatorScreen = MarquitoLuaUtils.FindScreen()
- elevatorScreen.setTextScale(0.5)
- elevatorScreen.clear()
- local monitorSide
- for _,name in ipairs(peripheral.getNames()) do --gets the side the monitor is on
- if peripheral.getType(name) == "monitor" then
- monitorSide = name
- end
- end
- -- Adjust screen size
- local monitorWidth, monitorHeight = MarquitoLuaUtils.GetScreenSize(elevatorScreen)
- if monitorWidth >= 36 and monitorHeight >= 24 then
- elevatorScreen.setTextScale(1)
- monitorWidth, monitorHeight = MarquitoLuaUtils.GetScreenSize(elevatorScreen)
- else
- elevatorScreen.setTextScale(0.5)
- end
- elevatorScreen.clear()
- term.setCursorPos(1,1)
- local buttonsPerColumn = math.floor((monitorHeight) / 2)
- local numberOfColumns = math.min(math.ceil(NumberOfLevels / buttonsPerColumn), 3)
- local maxButtonsInOnePage = buttonsPerColumn * 3
- local numberOfPages = math.ceil(NumberOfLevels / maxButtonsInOnePage)
- local buttonWidth = monitorWidth - 2
- -- The current page
- local currentPage = 1
- buttonWidth, currentPage = SettingScreen(monitorWidth, monitorHeight, numberOfColumns, 1, numberOfPages)
- -- Manage pages
- local page = {}
- for i = 1, numberOfPages do
- page[i] = touchpoint.new(monitorSide)
- -- Add pages
- if i ~= numberOfPages then
- page[i]:add(">>", nil, monitorWidth-3, monitorHeight, monitorWidth, monitorHeight, colors.black, colors.white)
- end
- if i ~= 1 then
- page[i]:add("<<", nil, 1, monitorHeight, 4, monitorHeight, colors.black, colors.white)
- end
- if numberOfPages > 1 then
- local pageLabel
- if monitorWidth == 15 then
- pageLabel = "P." .. i
- else
- -- Add page number
- pageLabel = "Page " .. i
- end
- page[i]:add(pageLabel, nil, 5, monitorHeight, monitorWidth-4, monitorHeight, colors.black, colors.black)
- end
- end
- -- Manage buttons
- local minX = 2
- local minY = 1
- local maxX = monitorWidth - 1
- local maxY = minY
- local pageIndex = 1
- local topFloor = NumberOfLevels - 1
- if topFloor < buttonsPerColumn then
- for i = topFloor, 0, -1 do
- local buttonColor = colors.red
- if i == ElevatorLevel then
- buttonColor = colors.blue
- end
- page[pageIndex]:add(tostring(i), nil, minX, minY, maxX, maxY, buttonColor, colors.lime) --adds buttons in reverse
- minY = minY + 2
- maxY = minY
- end
- else
- minX = 2
- maxX = minX + buttonWidth
- minY = monitorHeight - 1
- maxY = minY
- for i = 0, topFloor do
- local buttonColor = colors.red
- if i == ElevatorLevel then
- buttonColor = colors.blue
- end
- page[pageIndex]:add(tostring(i), nil, minX, minY, maxX, maxY, buttonColor, colors.lime)
- local remainingFloors = topFloor - i
- minY = maxY - 2
- maxY = minY
- if maxY <= 0 then
- -- Move buttons to the next column
- minX = maxX + 2
- maxX = minX + buttonWidth
- minY = monitorHeight - 1
- maxY = minY
- if maxX > monitorWidth then
- -- Change to next page
- pageIndex = pageIndex + 1
- minX = 2
- maxX = minX + buttonWidth
- minY = monitorHeight - 1
- maxY = minY
- end
- if remainingFloors < buttonsPerColumn then
- -- Moves the buttons up if there are spaces
- minY = monitorHeight - 1 - ((buttonsPerColumn - remainingFloors) * 2)
- maxY = minY
- end
- end
- end
- end
- -- Floor choice loop
- while true do
- if MarquitoLuaUtils.OpenWirelessRednetModem() then
- -- Draws the buttons on the monitor
- page[currentPage]:draw()
- local event, p1 = page[currentPage]:handleEvents(os.pullEvent())
- -- Wait for button clicks
- if event == "button_click" then
- local chosen = tonumber(p1)
- page[currentPage]:flash(p1)
- if chosen ~= nil then
- MarquitoLuaUtils.BroadcastDataWithRednet(ElevatorLevel .. "|" .. chosen, GetElevatorRednetProtocol())
- MarquitoLuaUtils.SendDataWithRednetForOneDevice(os.getComputerID(), ElevatorLevel .. "|" .. chosen, GetElevatorRednetProtocol())
- --ManageElevator(ElevatorLevel .. "|" .. chosen)
- elseif p1 == ">>" then
- currentPage = currentPage + 1
- elseif p1 == "<<" then
- currentPage = currentPage - 1
- end
- end
- end
- end
- end
- function SettingScreen(monitorWidth, monitorHeight, numberOfColumns, currentPage, numberOfPages)
- -- Calculate width of buttons
- if NumberOfLevels * 2 >= monitorHeight then
- buttonWidth = math.floor((monitorWidth - numberOfColumns - 3) / numberOfColumns)
- end
- -- Set the number of pages
- if currentPage > numberOfPages then
- currentPage = numberOfPages
- end
- return buttonWidth, currentPage
- end
- -- Download program
- function DownloadProgram(pastebinCode, programName)
- if not fs.exists(programName) then
- shell.run("pastebin get " .. pastebinCode .. " " .. programName)
- end
- end
- -- We need this library for other programs work
- DownloadProgram(LibraryCode, LibraryName)
- os.loadAPI("MarquitoLuaUtils")
- -- Library for level buttons on monitor
- DownloadProgram("pFHeia96", "touchpoint")
- os.loadAPI("touchpoint")
- -- Program
- ElevatorAskConfData()
- parallel.waitForAny(LaunchElevatorMain, LaunchFloorScreenProgram)
- term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement