Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Vaultium: The Grid Remake
- by NindroidA
- File Name: supplyR API
- ]]--
- --[[
- x-axis movement: "left"
- x-axis gearshift: "back"
- x-axis z-axis: "right"
- x-axis modem: "top"
- y-axis movement: "left"
- y-axis gearshift: "back"
- y-axis a-axis: "right"
- y-axis modem: "top"
- ]]--
- local nameM = "[supplyR]> "
- local next = "next"
- local monitorWidth, monitorHeight = term.getSize()
- local halfWidth = monitorWidth / 2
- local welcomeMsg = "WELCOME TO VAULTIUM"
- local welcomeMsgLen = string.len(welcomeMsg)
- local function lineBreak(t)
- term.setTextColor(colors.lightGray)
- for i = 1, monitorWidth do
- term.write(t)
- end
- end
- local function increment(loc, n)
- for i=loc, n-1 do
- loc = loc + 1
- end
- return loc
- end
- local function decrement(loc, n)
- for i=loc, n-1 do
- loc = loc - 1
- end
- return loc
- end
- local locs = {
- xLoc = 1,
- yLoc = 0,
- zLoc = 0,
- aLoc = 0,
- }
- movement = {
- moveUp = function(steps)
- if rs.getOutput("back") then -- check to make rs signal off
- redUtil.trigger("back")
- end
- for i=locs.yLoc, steps-1 do
- print(nameM, "Y-Loc: ", locs.yLoc)
- redUtil.pulse("left")
- locs.yLoc = locs.yLoc + 1
- end
- rednet.broadcast(next)
- end,
- moveDown = function(steps)
- if not rs.getOutput("back") then -- check to make rs signal on
- redUtil.trigger("back")
- end
- for i=locs.yLoc, steps+1, -1 do
- print(nameM, "Y-Loc: ", locs.yLoc)
- redUtil.pulse("left")
- locs.yLoc = locs.yLoc - 1
- end
- redUtil.trigger("back") -- turn rs signal off
- rednet.broadcast(next)
- end,
- moveRight = function(steps)
- if rs.getOutput("back") then -- check to make rs signal off
- redUtil.trigger("back")
- end
- for i=locs.xLoc, steps-1 do
- print(nameM, "X-Loc: ", locs.xLoc)
- redUtil.pulse("left")
- locs.xLoc = locs.xLoc + 1
- end
- rednet.broadcast(next)
- end,
- moveLeft = function(steps)
- if not rs.getOutput("back") then -- check to make rs signal on
- redUtil.trigger("back")
- end
- for i=locs.xLoc, steps+1, -1 do
- print(nameM, "X-Loc: ", locs.xLoc)
- redUtil.pulse("left")
- locs.xLoc = locs.xLoc - 1
- end
- redUtil.trigger("back") -- turn rs signal off
- rednet.broadcast(next)
- end,
- moveForward = function()
- if rs.getOutput("back") then -- check to make rs signal off
- redUtil.trigger("back")
- end
- print(nameM, "Piston moved Forward")
- redUtil.pulse("right")
- locs.zLoc = locs.zLoc + 1
- rednet.broadcast(next)
- end,
- moveBackward = function()
- if not rs.getOutput("back") then -- check to make rs signal on
- redUtil.trigger("back")
- end
- print(nameM, "Piston moved Backward")
- redUtil.pulse("right")
- redUtil.trigger("back") -- turn rs signal off
- locs.zLoc = locs.zLoc - 1
- rednet.broadcast(next)
- end,
- grab = function()
- print(nameM, "Gripped")
- redUtil.pulse("right")
- locs.aLoc = locs.aLoc + 1
- rednet.broadcast(next)
- end,
- release = function()
- print(nameM, "Released")
- redUtil.pulse("right")
- locs.aLoc = locs.aLoc - 1
- rednet.broadcast(next)
- end,
- }
- communication = {
- supplyR_I = function(cmd)
- local msg = { command = cmd }
- rednet.broadcast(msg)
- end,
- supplyR_II = function(cmd, n)
- local msg = { command = cmd, number = n }
- rednet.broadcast(msg)
- end,
- }
- mainUtil = {
- resetMonitor = function(s)
- os.sleep(s)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
- end,
- await = function(m)
- while true do
- local id, msg = rednet.receive()
- if msg == m then
- break;
- end
- end
- end,
- printHeader = function()
- --## Header
- term.clear()
- term.setCursorPos(halfWidth - welcomeMsgLen / 2, 1)
- term.setTextColor(colors.blue)
- print(welcomeMsg)
- lineBreak("=")
- end,
- printCmdStn = function()
- --## Command Section
- term.setPaletteColor(colors.yellow, 0xb1b83b)
- term.setTextColor(colors.yellow)
- print("Commands:")
- print(" · get - Gets a Container")
- print(" · store - Stores a Container")
- print('Enter "q" to quit.')
- lineBreak('-')
- end,
- printUsrInput = function(row)
- --## User Input Section
- while true do
- term.setTextColor(colors.purple)
- write("Vultium> ")
- local response = read()
- if response == "q" then
- term.setTextColor(colors.purple)
- print("Have a nice day, cutie! ;)")
- --rednet.close("back")
- mainUtil.resetMonitor(3)
- break
- elseif response == "get" then
- mainUtil.getVaultLoc(row)
- mainUtil.gotoVault(1, 7)
- elseif response == "loc" then
- mainUtil.getSupplyRLoc()
- end
- --[[
- if response == "up" then
- while true do
- write("Number?: ")
- local response2 = tonumber(read())
- if response2 > 2 or response2 < 0 then
- print("Invalid Num!")
- elseif response2 == locs.yLoc then
- print("supplyR is already there!")
- else
- locs.yLoc = increment(locs.yLoc, response2)
- supplyR_II(response, response2)
- break;
- end
- end
- end
- if response == "down" then
- while true do
- write("Number?: ")
- local response2 = tonumber(read())
- if response2 > 2 or response2 < 0 then
- print("Invalid Num!")
- elseif response2 == locs.yLoc then
- print("supplyR is already there!")
- else
- locs.yLoc = decrement(locs.yLoc, response2)
- supplyR_II(response, response2)
- break;
- end
- end
- end
- if response == "right" then
- while true do
- write("Number?: ")
- local response2 = tonumber(read())
- if response2 > 9 or response2 < 0 or locs.yLoc == 0 then
- print("Invalid Num!")
- elseif response2 == locs.xLoc then
- print("supplyR is already there!")
- else
- locs.xLoc = increment(locs.xLoc, response2)
- supplyR_II(response, response2)
- break;
- end
- end
- end
- if response == "left" then
- while true do
- write("Number?: ")
- local response2 = tonumber(read())
- if response2 > 9 or response2 < 0 or locs.yLoc == 0 then
- print("Invalid Num!")
- elseif response2 == locs.xLoc then
- print("supplyR is already there!")
- else
- locs.xLoc = decrement(locs.xLoc, response2)
- supplyR_II(response, response2)
- break;
- end
- end
- end
- if response == "forward" or response == "backward" or response == "grab" or response == "release" or response == "vaultForward" or response == "reset" then
- communication.supplyR_I(response)
- else
- --term.setTextColor(colors.red)
- --print("Invalid Command!")
- end
- ]]--
- end
- end,
- printExitButton = function()
- local exitButton = "[ Exit ]"
- local width, height = term.getSize()
- term.setCursorPos(width - string.len(exitButton) + 1, height)
- write(exitButton)
- end,
- createRow = function()
- local row = {}
- for i=1, 9 do
- row[i] = false
- end
- return row
- end,
- editRow = function(row, slotNumber)
- if slotNumber >= 1 and slotNumber <= #row then
- row[slotNumber] = not row[slotNumber]
- else
- print("Error: Slot number out of bounds.")
- end
- end,
- printRow = function(row)
- for i, slot in ipairs(row) do
- if slot then
- term.write("[x]")
- else
- term.write("[ ]")
- end
- end
- print(" ")
- end,
- interfaceClickHandler = function(row, mode, callback)
- mainUtil.resetMonitor(0.5)
- term.setTextColor(colors.white)
- mainUtil.printRow(row) -- Print the initial state of the row
- mainUtil.printExitButton() -- Print the exit button
- while true do
- local event, button, x, y = os.pullEvent("mouse_click")
- local width, height = term.getSize()
- local slotNumber = math.ceil(x / 3)
- if y == height and x > width - string.len("[ Exit ]") then
- break -- Exit the loop if the exit button is clicked
- elseif slotNumber >= 1 and slotNumber <= #row then
- local slotValue = row[slotNumber]
- local actionResult, message = callback(row, slotNumber, slotValue, mode)
- if message then
- term.setCursorPos(1, height-3)
- term.setTextColor(colors.red)
- print(message) -- Display the message
- os.sleep(1) -- Wait a little before clearing the message
- end
- term.clear() -- Clear the terminal
- term.setCursorPos(1,1)
- term.setTextColor(colors.white)
- mainUtil.printRow(row) -- Reprint the row after any action
- mainUtil.printExitButton() -- Reprint the exit button
- if actionResult then
- break -- Exit loop if the callback function indicates
- end
- else
- term.setCursorPos(1, height-2)
- print("Clicked outside the slots.")
- os.sleep(1.5)
- mainUtil.resetMonitor(0.5)
- break
- end
- end
- mainUtil.resetMonitor(0.5)
- term.setTextColor(colors.white)
- end,
- getVaultLoc = function(row)
- local mode = "get"
- local function getCallback(row, slotNumber, slotValue, mode)
- if not slotValue then
- return false, "Invalid slot - no vault to get."
- else
- row[slotNumber] = false
- -- retrieveVault(slotNumber) -- Placeholder for additional functionality
- return true
- end
- end
- mainUtil.interfaceClickHandler(row, mode, getCallback)
- end,
- storeVault = function(row)
- local mode = "store"
- local function storeCallback(row, slotNumber, slotValue, mode)
- if slotValue then
- return false, "Invalid slot - vault already present."
- else
- row[slotNumber] = true
- -- depositVault(slotNumber) -- Placeholder for additional functionality
- return true
- end
- end
- mainUtil.interfaceClickHandler(row, mode, storeCallback)
- end,
- gotoVault = function(row, column)
- term.setTextColor(colors.blue)
- os.sleep(1)
- communication.supplyR_II("up", row)
- mainUtil.await(next)
- print(nameM, "Y-Loc: ", locs.yLoc)
- os.sleep(1)
- communication.supplyR_II("right", column)
- mainUtil.await(next)
- print(nameM, "X-Loc: ", locs.xLoc)
- os.sleep(1)
- communication.supplyR_I("forward")
- mainUtil.await(next)
- print(nameM, "Z-Loc: ", locs.zLoc)
- os.sleep(1)
- communication.supplyR_I("grab")
- --mainUtil.await(next)
- --print(nameM, "A-Loc: ", aLoc)
- --os.sleep(1)
- --communication.supplyR_I("backward")
- --mainUtil.await(next)
- -- print(nameM, "Z-Loc: ", zLoc)
- --os.sleep(1)
- --mainUtil.await(next)
- --communication.supplyR_II("left", 0)
- --mainUtil.await(next)
- --communication.supplyR_II("down", 0)
- os.sleep(2)
- term.setTextColor(colors.white)
- end,
- getSupplyRLoc = function()
- local locString = string.format(
- "supplyR Location - X: %d, Y: %d, Z: %d, A: %d",
- locs.xLoc, locs.yLoc, locs.zLoc, locs.aLoc
- )
- print(locString)
- end,
- }
- redUtil = {
- pulse = function(side)
- rs.setOutput(side, not rs.getOutput(side))
- os.sleep(1)
- rs.setOutput(side, not rs.getOutput(side))
- os.sleep(1)
- end,
- trigger = function(side)
- rs.setOutput(side, not rs.getOutput(side))
- os.sleep(1)
- end,
- }
- --[[
- function reset()
- release()
- backward()
- left(xLoc-xLoc)
- down(yLoc-yLoc)
- end
- ]]--
- local supplyR_API = {
- mainUtil = mainUtil,
- redUtil = redUtil,
- movement = movement,
- communication = communication,
- }
- return supplyR_API
Add Comment
Please, Sign In to add comment