NindroidA

supplyR API

Nov 19th, 2023 (edited)
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.88 KB | None | 0 0
  1. --[[
  2.  
  3.     Vaultium: The Grid Remake
  4.     by NindroidA
  5.  
  6.     File Name: supplyR API
  7.  
  8. ]]--
  9.  
  10. --[[
  11.     x-axis movement: "left"
  12.     x-axis gearshift: "back"
  13.     x-axis z-axis: "right"
  14.     x-axis modem: "top"
  15.    
  16.     y-axis movement: "left"
  17.     y-axis gearshift: "back"
  18.     y-axis a-axis: "right"
  19.     y-axis modem: "top"
  20.  
  21. ]]--
  22.  
  23. local nameM = "[supplyR]> "
  24. local next = "next"
  25. local monitorWidth, monitorHeight = term.getSize()
  26. local halfWidth = monitorWidth / 2
  27. local welcomeMsg = "WELCOME TO VAULTIUM"
  28. local welcomeMsgLen = string.len(welcomeMsg)
  29.  
  30. local function lineBreak(t)
  31.     term.setTextColor(colors.lightGray)
  32.     for i = 1, monitorWidth do
  33.         term.write(t)
  34.     end
  35. end
  36. local function increment(loc, n)
  37.     for i=loc, n-1 do
  38.         loc = loc + 1
  39.     end
  40.     return loc
  41. end
  42. local function decrement(loc, n)
  43.     for i=loc, n-1 do
  44.         loc = loc - 1
  45.     end
  46.     return loc
  47. end
  48.  
  49. local locs = {
  50.     xLoc = 1,
  51.     yLoc = 0,
  52.     zLoc = 0,
  53.     aLoc = 0,
  54. }
  55.  
  56. movement = {
  57.     moveUp = function(steps)
  58.         if rs.getOutput("back") then -- check to make rs signal off
  59.             redUtil.trigger("back")
  60.         end
  61.  
  62.         for i=locs.yLoc, steps-1 do
  63.             print(nameM, "Y-Loc: ", locs.yLoc)
  64.             redUtil.pulse("left")
  65.             locs.yLoc = locs.yLoc + 1
  66.         end
  67.  
  68.         rednet.broadcast(next)
  69.     end,
  70.     moveDown = function(steps)
  71.         if not rs.getOutput("back") then -- check to make rs signal on
  72.             redUtil.trigger("back")
  73.         end
  74.  
  75.         for i=locs.yLoc, steps+1, -1 do
  76.             print(nameM, "Y-Loc: ", locs.yLoc)
  77.             redUtil.pulse("left")
  78.             locs.yLoc = locs.yLoc - 1
  79.         end
  80.  
  81.         redUtil.trigger("back") -- turn rs signal off
  82.         rednet.broadcast(next)
  83.     end,
  84.  
  85.     moveRight = function(steps)
  86.         if rs.getOutput("back") then -- check to make rs signal off
  87.             redUtil.trigger("back")
  88.         end
  89.  
  90.         for i=locs.xLoc, steps-1 do
  91.             print(nameM, "X-Loc: ", locs.xLoc)
  92.             redUtil.pulse("left")
  93.             locs.xLoc = locs.xLoc + 1
  94.         end
  95.         rednet.broadcast(next)
  96.     end,
  97.     moveLeft = function(steps)
  98.         if not rs.getOutput("back") then -- check to make rs signal on
  99.             redUtil.trigger("back")
  100.         end
  101.  
  102.         for i=locs.xLoc, steps+1, -1 do
  103.             print(nameM, "X-Loc: ", locs.xLoc)
  104.             redUtil.pulse("left")
  105.             locs.xLoc = locs.xLoc - 1
  106.         end
  107.  
  108.         redUtil.trigger("back") -- turn rs signal off
  109.         rednet.broadcast(next)
  110.     end,
  111.  
  112.     moveForward = function()
  113.         if rs.getOutput("back") then -- check to make rs signal off
  114.             redUtil.trigger("back")
  115.         end
  116.         print(nameM, "Piston moved Forward")
  117.         redUtil.pulse("right")
  118.         locs.zLoc = locs.zLoc + 1
  119.         rednet.broadcast(next)
  120.     end,
  121.     moveBackward = function()
  122.         if not rs.getOutput("back") then -- check to make rs signal on
  123.             redUtil.trigger("back")
  124.         end
  125.         print(nameM, "Piston moved Backward")
  126.         redUtil.pulse("right")
  127.         redUtil.trigger("back") -- turn rs signal off
  128.         locs.zLoc = locs.zLoc - 1
  129.         rednet.broadcast(next)
  130.     end,
  131.  
  132.     grab = function()
  133.         print(nameM, "Gripped")
  134.         redUtil.pulse("right")
  135.         locs.aLoc = locs.aLoc + 1
  136.         rednet.broadcast(next)
  137.     end,
  138.     release = function()
  139.         print(nameM, "Released")
  140.         redUtil.pulse("right")
  141.         locs.aLoc = locs.aLoc - 1
  142.         rednet.broadcast(next)
  143.     end,
  144. }
  145. communication = {
  146.     supplyR_I = function(cmd)
  147.         local msg = { command = cmd }
  148.         rednet.broadcast(msg)
  149.     end,
  150.    
  151.     supplyR_II = function(cmd, n)
  152.         local msg = { command = cmd, number = n }
  153.         rednet.broadcast(msg)
  154.     end,
  155. }
  156. mainUtil = {
  157.  
  158.     resetMonitor = function(s)
  159.         os.sleep(s)
  160.         term.setTextColor(colors.white)
  161.         term.clear()
  162.         term.setCursorPos(1, 1)
  163.     end,
  164.  
  165.     await = function(m)
  166.         while true do
  167.             local id, msg = rednet.receive()
  168.             if msg == m then
  169.                 break;
  170.             end
  171.         end
  172.     end,
  173.  
  174.     printHeader = function()
  175.         --## Header
  176.         term.clear()
  177.         term.setCursorPos(halfWidth - welcomeMsgLen / 2, 1)
  178.         term.setTextColor(colors.blue)
  179.         print(welcomeMsg)
  180.         lineBreak("=")
  181.     end,
  182.    
  183.     printCmdStn = function()
  184.         --## Command Section
  185.         term.setPaletteColor(colors.yellow, 0xb1b83b)
  186.         term.setTextColor(colors.yellow)
  187.         print("Commands:")
  188.         print(" · get - Gets a Container")
  189.         print(" · store - Stores a Container")
  190.         print('Enter "q" to quit.')
  191.         lineBreak('-')
  192.     end,
  193.    
  194.     printUsrInput = function(row)
  195.         --## User Input Section
  196.         while true do
  197.             term.setTextColor(colors.purple)
  198.             write("Vultium> ")
  199.             local response = read()
  200.             if response == "q" then
  201.                 term.setTextColor(colors.purple)
  202.                 print("Have a nice day, cutie! ;)")
  203.                 --rednet.close("back")
  204.                 mainUtil.resetMonitor(3)
  205.                 break
  206.             elseif response == "get" then
  207.                 mainUtil.getVaultLoc(row)
  208.                 mainUtil.gotoVault(1, 7)
  209.             elseif response == "loc" then
  210.                 mainUtil.getSupplyRLoc()
  211.             end
  212.             --[[
  213.             if response == "up" then
  214.                 while true do
  215.                     write("Number?: ")
  216.                     local response2 = tonumber(read())
  217.                     if response2 > 2 or response2 < 0 then
  218.                         print("Invalid Num!")
  219.                     elseif response2 == locs.yLoc then
  220.                         print("supplyR is already there!")
  221.                     else
  222.                         locs.yLoc = increment(locs.yLoc, response2)
  223.                         supplyR_II(response, response2)
  224.                         break;
  225.                     end
  226.                 end
  227.             end
  228.             if response == "down" then
  229.                 while true do
  230.                     write("Number?: ")
  231.                     local response2 = tonumber(read())
  232.                     if response2 > 2 or response2 < 0 then
  233.                         print("Invalid Num!")
  234.                     elseif response2 == locs.yLoc then
  235.                         print("supplyR is already there!")
  236.                     else
  237.                         locs.yLoc = decrement(locs.yLoc, response2)
  238.                         supplyR_II(response, response2)
  239.                         break;
  240.                     end
  241.                 end
  242.             end
  243.             if response == "right" then
  244.                 while true do
  245.                     write("Number?: ")
  246.                     local response2 = tonumber(read())
  247.                     if response2 > 9 or response2 < 0 or locs.yLoc == 0 then
  248.                         print("Invalid Num!")
  249.                     elseif response2 == locs.xLoc then
  250.                         print("supplyR is already there!")
  251.                     else
  252.                         locs.xLoc = increment(locs.xLoc, response2)
  253.                         supplyR_II(response, response2)
  254.                         break;
  255.                     end
  256.                 end
  257.             end
  258.             if response == "left" then
  259.                 while true do
  260.                     write("Number?: ")
  261.                     local response2 = tonumber(read())
  262.                     if response2 > 9 or response2 < 0 or locs.yLoc == 0 then
  263.                         print("Invalid Num!")
  264.                     elseif response2 == locs.xLoc then
  265.                         print("supplyR is already there!")
  266.                     else
  267.                         locs.xLoc = decrement(locs.xLoc, response2)
  268.                         supplyR_II(response, response2)
  269.                         break;
  270.                     end
  271.                 end
  272.             end
  273.             if response == "forward" or response == "backward" or response == "grab" or response == "release" or response == "vaultForward" or response == "reset" then
  274.                 communication.supplyR_I(response)
  275.             else
  276.                 --term.setTextColor(colors.red)
  277.                 --print("Invalid Command!")
  278.             end
  279.             ]]--
  280.         end
  281.     end,
  282.  
  283.     printExitButton = function()
  284.         local exitButton = "[ Exit ]"
  285.         local width, height = term.getSize()
  286.         term.setCursorPos(width - string.len(exitButton) + 1, height)
  287.         write(exitButton)
  288.     end,
  289.  
  290.     createRow = function()
  291.         local row = {}
  292.         for i=1, 9 do
  293.             row[i] = false
  294.         end
  295.         return row
  296.     end,
  297.    
  298.     editRow = function(row, slotNumber)
  299.         if slotNumber >= 1 and slotNumber <= #row then
  300.             row[slotNumber] = not row[slotNumber]
  301.         else
  302.             print("Error: Slot number out of bounds.")
  303.         end
  304.     end,
  305.    
  306.     printRow = function(row)
  307.         for i, slot in ipairs(row) do
  308.             if slot then
  309.                 term.write("[x]")
  310.             else
  311.                 term.write("[ ]")
  312.             end
  313.         end
  314.         print(" ")
  315.     end,
  316.  
  317.     interfaceClickHandler = function(row, mode, callback)
  318.         mainUtil.resetMonitor(0.5)
  319.         term.setTextColor(colors.white)
  320.         mainUtil.printRow(row)  -- Print the initial state of the row
  321.         mainUtil.printExitButton()  -- Print the exit button
  322.         while true do
  323.             local event, button, x, y = os.pullEvent("mouse_click")
  324.             local width, height = term.getSize()
  325.             local slotNumber = math.ceil(x / 3)
  326.            
  327.             if y == height and x > width - string.len("[ Exit ]") then
  328.                 break  -- Exit the loop if the exit button is clicked
  329.             elseif slotNumber >= 1 and slotNumber <= #row then
  330.                 local slotValue = row[slotNumber]
  331.                 local actionResult, message = callback(row, slotNumber, slotValue, mode)
  332.                 if message then
  333.                     term.setCursorPos(1, height-3)
  334.                     term.setTextColor(colors.red)
  335.                     print(message)  -- Display the message
  336.                     os.sleep(1)  -- Wait a little before clearing the message
  337.                 end
  338.                 term.clear()  -- Clear the terminal
  339.                 term.setCursorPos(1,1)
  340.                 term.setTextColor(colors.white)
  341.                 mainUtil.printRow(row)  -- Reprint the row after any action
  342.                 mainUtil.printExitButton()  -- Reprint the exit button
  343.                 if actionResult then
  344.                     break  -- Exit loop if the callback function indicates
  345.                 end
  346.             else
  347.                 term.setCursorPos(1, height-2)
  348.                 print("Clicked outside the slots.")
  349.                 os.sleep(1.5)
  350.                 mainUtil.resetMonitor(0.5)
  351.                 break
  352.             end
  353.         end
  354.         mainUtil.resetMonitor(0.5)
  355.         term.setTextColor(colors.white)
  356.     end,
  357.  
  358.     getVaultLoc = function(row)
  359.         local mode = "get"
  360.         local function getCallback(row, slotNumber, slotValue, mode)
  361.             if not slotValue then
  362.  
  363.                 return false, "Invalid slot - no vault to get."
  364.             else
  365.                 row[slotNumber] = false
  366.                 -- retrieveVault(slotNumber)  -- Placeholder for additional functionality
  367.                 return true
  368.             end
  369.         end
  370.         mainUtil.interfaceClickHandler(row, mode, getCallback)
  371.     end,
  372.    
  373.     storeVault = function(row)
  374.         local mode = "store"
  375.         local function storeCallback(row, slotNumber, slotValue, mode)
  376.             if slotValue then
  377.                 return false, "Invalid slot - vault already present."
  378.             else
  379.                 row[slotNumber] = true
  380.                 -- depositVault(slotNumber)  -- Placeholder for additional functionality
  381.                 return true
  382.             end
  383.         end
  384.         mainUtil.interfaceClickHandler(row, mode, storeCallback)
  385.     end,
  386.  
  387.     gotoVault = function(row, column)
  388.         term.setTextColor(colors.blue)
  389.         os.sleep(1)
  390.         communication.supplyR_II("up", row)
  391.         mainUtil.await(next)
  392.  
  393.         print(nameM, "Y-Loc: ", locs.yLoc)
  394.         os.sleep(1)
  395.         communication.supplyR_II("right", column)
  396.         mainUtil.await(next)
  397.  
  398.         print(nameM, "X-Loc: ", locs.xLoc)
  399.         os.sleep(1)
  400.         communication.supplyR_I("forward")
  401.         mainUtil.await(next)
  402.  
  403.         print(nameM, "Z-Loc: ", locs.zLoc)
  404.         os.sleep(1)
  405.         communication.supplyR_I("grab")
  406.         --mainUtil.await(next)
  407.  
  408.         --print(nameM, "A-Loc: ", aLoc)
  409.         --os.sleep(1)
  410.         --communication.supplyR_I("backward")
  411.         --mainUtil.await(next)
  412.  
  413.        -- print(nameM, "Z-Loc: ", zLoc)
  414.         --os.sleep(1)
  415.         --mainUtil.await(next)
  416.         --communication.supplyR_II("left", 0)
  417.         --mainUtil.await(next)
  418.         --communication.supplyR_II("down", 0)
  419.         os.sleep(2)
  420.         term.setTextColor(colors.white)
  421.     end,
  422.  
  423.     getSupplyRLoc = function()
  424.         local locString = string.format(
  425.             "supplyR Location - X: %d, Y: %d, Z: %d, A: %d",
  426.             locs.xLoc, locs.yLoc, locs.zLoc, locs.aLoc
  427.         )
  428.         print(locString)
  429.     end,
  430. }
  431. redUtil = {
  432.     pulse = function(side)
  433.         rs.setOutput(side, not rs.getOutput(side))
  434.         os.sleep(1)
  435.         rs.setOutput(side, not rs.getOutput(side))
  436.         os.sleep(1)
  437.     end,
  438.    
  439.     trigger = function(side)
  440.         rs.setOutput(side, not rs.getOutput(side))
  441.         os.sleep(1)
  442.     end,
  443. }
  444.  
  445. --[[
  446. function reset()
  447.     release()
  448.     backward()
  449.     left(xLoc-xLoc)
  450.     down(yLoc-yLoc)
  451. end
  452. ]]--
  453.  
  454. local supplyR_API = {
  455.     mainUtil = mainUtil,
  456.     redUtil = redUtil,
  457.     movement = movement,
  458.     communication = communication,
  459. }
  460. return supplyR_API
Add Comment
Please, Sign In to add comment