Advertisement
Jishh_

quarry.lua

Dec 28th, 2022 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.94 KB | None | 0 0
  1. local role = nil
  2. local server = nil
  3. local chunkPartner = nil
  4. local acceptedProtocols = {}
  5. acceptedProtocols["config"] = 1
  6.  
  7. local REMOTE_METADATA_PASTE = "5LPswLxb"
  8. local ITEM_QUARRY = "quarryplus:quarry"
  9. local ITEM_POWER = "mekanism:quantum_entangloporter"
  10. local ITEM_OUTPUT_CHEST = "enderchests:ender_chest"
  11. local ITEM_FUEL_CHEST = "enderchests:ender_chest"
  12. local ITEM_FILLER = "minecraft:iron_block"
  13.  
  14. local SLOT_QUARRY = 1
  15. local SLOT_POWER = 2
  16. local SLOT_OUTPUT_CHEST = 3
  17. local SLOT_FUEL_CHEST = 4
  18. local SLOT_FILLER = 5
  19.  
  20. local SIDE_DOWN = -1
  21. local SIDE_FRONT = 0
  22. local SIDE_UP = 1
  23.  
  24. local turtleSlots = {
  25.     miner = {
  26.         {ITEM_QUARRY, 1}, {ITEM_POWER, 1}, {ITEM_OUTPUT_CHEST, 1}, {ITEM_FUEL_CHEST, 1},
  27.         {ITEM_FILLER, 1}, nil, nil, nil,
  28.         nil, nil, nil, nil,
  29.         nil, nil, nil, nil,
  30.     },
  31.     loader = {
  32.         nil, nil, nil, nil,
  33.         nil, nil, nil, nil,
  34.         nil, nil, nil, nil,
  35.         nil, nil, nil, nil,
  36.     }
  37. }
  38.  
  39. function checkForUpdates()
  40.     local updateMetadata
  41.  
  42.     shell.run("pastebin", "get", REMOTE_METADATA_PASTE, "version.new")
  43.     if not fs.exists("version.new") then
  44.         print("Remote metadata download failure, will not update")
  45.         return nil
  46.     end
  47.     fh = fs.open("version.new", "r")
  48.     updateMetadata = textutils.unserialiseJSON(fh.readAll())
  49.     fh.close()
  50.     fs.delete("version.new")
  51.  
  52.     if updateMetadata['version'] == nil or updateMetadata['paste'] == nil then
  53.         print("Remote metadata malformed, will not update")
  54.         return nil
  55.     end
  56.  
  57.     if not fs.exists("version") then
  58.         print("Local version missing, update needed")
  59.         return updateMetadata
  60.     end
  61.     fh = fs.open("version", "r")
  62.     localVersion = fh.readAll()
  63.     fh.close()
  64.  
  65.     if localVersion ~= updateMetadata['version'] then
  66.         print("Local version: "..localVersion)
  67.         print("Remote version: "..updateMetadata['version'])
  68.         print("Local version differs, update needed")
  69.         return updateMetadata
  70.     end
  71.  
  72.     print("Local version: "..localVersion)
  73.     print("Remote version: "..updateMetadata['version'])
  74.     print("Local version matches remote, no update needed")
  75.     return nil
  76. end
  77.  
  78. function downloadUpdate(metadata)
  79.     print("Downloading update")
  80.     fs.delete("startup")
  81.     shell.run("pastebin", "get", metadata['paste'], "startup")
  82.     fh = fs.open("version", "w")
  83.     localVersion = fh.write(metadata['version'])
  84.     fh.close()
  85.     os.reboot()
  86. end
  87.  
  88. function setup()
  89.     role = determineRole(args)
  90.     chunkPartner = determinePartner(args)
  91.  
  92.     print("Ready for initial setup. Insert fuel in the last slot before continuing.")
  93.     print("Hit enter to calibrate")
  94.     waitForKey(keys.enter)
  95.  
  96.     turtle.select(16)
  97.     turtle.refuel(64)
  98.  
  99.     setupBTA()
  100.     inventoryAssert()
  101.  
  102.     print("Setup complete. Fill with correct items and hit enter to begin")
  103.     waitForKey(keys.enter)
  104. end
  105.  
  106. function setupBTA()
  107.     if not fs.exists("bta") then
  108.         shell.run("pastebin", "get", "MdD051uF", "bta")
  109.     end
  110.    
  111.     if gps.locate(5) == nil then
  112.         error("Failed to locate self. Is GPS setup/reachable?")
  113.     end
  114.  
  115.     bta = require("bta")
  116. end
  117.  
  118. function determineRole(args)
  119.     local role = nil
  120.     local roleArg = args[1]
  121.  
  122.     if fs.exists("role") then
  123.         fh = fs.open("role", "r")
  124.         role = fh.readAll()
  125.         fh.close()
  126.     end
  127.     if roleArg ~= nil then
  128.         role = roleArg
  129.     end
  130.     if role == nil then
  131.         error("Must pass a role name as first argument")
  132.     end
  133.  
  134.     fh = fs.open("role", "w")
  135.     fh.write(role)
  136.     fh.close()
  137.     return role
  138. end
  139.  
  140. function determinePartner(args)
  141.     local partner = nil
  142.     local partnerArg = args[2]
  143.  
  144.     if fs.exists("partner") then
  145.         fh = fs.open("role", "r")
  146.         partner = fh.readAll()
  147.         fh.close()
  148.     end
  149.  
  150.     fh = fs.open("partner", "w")
  151.     fh.write(partner)
  152.     fh.close()
  153.     return partner
  154. end
  155.  
  156. function main()
  157.     print("Setup complete, starting main loop")
  158.     if role == "miner" then
  159.         mainLoopMiner()
  160.     elseif role == "loader" then
  161.         mainLoopLoader()
  162.     end
  163. end
  164.  
  165. function mainLoopMiner()
  166.     rednet.open("left")
  167.  
  168.     while true do
  169.         local r = placeMiningWell()
  170.         if isError(r) then error(r.r) end
  171.  
  172.         print("Press enter when completed")
  173.         waitForKey(keys.enter)
  174.  
  175.         r = removeMiningWell()
  176.         if isError(r) then error(r.r) end
  177.  
  178.         rednet.broadcast("", "loader")
  179.         bta.move("forward", nil, 9)
  180.  
  181.         r = refuel()
  182.         if isError(r) then error(r.r) end
  183.     end
  184. end
  185.  
  186. function mainLoopLoader()
  187.     rednet.open("left")
  188.  
  189.     while true do
  190.         print("Waiting for signal from chunk partner")
  191.         id = rednet.receive("loader")
  192.  
  193.         if id == chunkPartner then
  194.             rednet.send(id, "", "loader")
  195.  
  196.             bta.move("forward", nil, 9)
  197.  
  198.             local r = refuel()
  199.             if isError(r) then error(r.r) end
  200.         end
  201.     end
  202. end
  203.  
  204. function handleRednetMessage(sender, message, protocol)
  205.     if acceptedProtocols[protocol] == nil then return end
  206.  
  207.     server = sender
  208.     local command = textutils.unserialise(message)
  209.     if command.type == "goto" then
  210.         moveToCoordinates(command)
  211.     elseif command.type == "place" then
  212.         placeMiningWell()
  213.     end
  214. end
  215.  
  216. function moveToCoordinates(command)
  217.     bta.goTo(command.payload.position)
  218.     bta.turn(command.payload.direction)
  219. end
  220.  
  221. function placeMiningWell()
  222.     turtle.turnRight()
  223.     turtle.forward()
  224.     turtle.turnLeft()
  225.     turtle.forward()
  226.  
  227.     r = assertSlotAndSelect(SLOT_OUTPUT_CHEST, ITEM_OUTPUT_CHEST, 1)
  228.     if isError(r) then return r end
  229.     r = assertPlace(SIDE_FRONT)
  230.     if isError(r) then return r end
  231.  
  232.     turtle.turnLeft()
  233.     turtle.forward()
  234.     turtle.turnRight()
  235.  
  236.     turtle.forward()
  237.     turtle.digDown()
  238.     turtle.dig()
  239.  
  240.     inventoryFilter()
  241.  
  242.     r = assertSlotAndSelect(SLOT_FILLER, ITEM_FILLER, 1)
  243.     if isError(r) then return r end
  244.     r = assertPlace(SIDE_DOWN)
  245.     if isError(r) then return r end
  246.  
  247.     turtle.back()
  248.  
  249.     r = assertSlotAndSelect(SLOT_QUARRY, ITEM_QUARRY, 1)
  250.     if isError(r) then return r end
  251.     r = assertPlace(SIDE_FRONT)
  252.     if isError(r) then return r end
  253.  
  254.     turtle.turnLeft()
  255.     turtle.forward()
  256.     turtle.turnRight()
  257.  
  258.     r = assertSlotAndSelect(SLOT_POWER, ITEM_POWER, 1)
  259.     if isError(r) then return r end
  260.     r = assertPlace(SIDE_FRONT)
  261.     if isError(r) then return r end
  262.  
  263.     turtle.back()
  264.     turtle.turnRight()
  265.     turtle.forward()
  266.     turtle.turnLeft()
  267. end
  268.  
  269. function removeMiningWell()
  270.     turtle.forward()
  271.  
  272.     r = assertSelect(SLOT_QUARRY)
  273.     if isError(r) then return r end
  274.     r = assertDig(SIDE_FRONT)
  275.     if isError(r) then return r end
  276.  
  277.     inventoryFilter()
  278.     inventoryCrudeSort()
  279.  
  280.     turtle.turnLeft()
  281.     turtle.forward()
  282.     turtle.turnRight()
  283.  
  284.     r = assertSelect(SLOT_POWER)
  285.     if isError(r) then return r end
  286.     r = assertDig(SIDE_FRONT)
  287.     if isError(r) then return r end
  288.  
  289.     turtle.turnRight()
  290.     turtle.forward()
  291.     turtle.forward()
  292.     turtle.turnLeft()
  293.  
  294.     r = assertSelect(SLOT_OUTPUT_CHEST)
  295.     if isError(r) then return r end
  296.     r = assertDig(SIDE_FRONT)
  297.     if isError(r) then return r end
  298.  
  299.     turtle.turnLeft()
  300.     turtle.forward()
  301.     turtle.turnRight()
  302.     turtle.forward()
  303.  
  304.     r = assertSelect(SLOT_FILLER)
  305.     if isError(r) then return r end
  306.     r = assertDig(SIDE_DOWN)
  307.     if isError(r) then return r end
  308.     r = assertDig(SIDE_FRONT)
  309.     if isError(r) then return r end
  310.  
  311.     turtle.back()
  312.     turtle.back()
  313. end
  314.  
  315.  
  316. function waitForKey(keyToWaitFor)
  317.     repeat
  318.         local event, key = os.pullEvent("key")
  319.     until key == keyToWaitFor
  320. end
  321.  
  322. function refuel()
  323.     turtle.turnRight()
  324.     turtle.turnRight()
  325.  
  326.     local r = assertSlotAndSelect(4, ITEM_FUEL_CHEST, 1)
  327.     if isError(r) then return r end
  328.     r = assertPlace(SIDE_FRONT)
  329.     if isError(r) then return r end
  330.  
  331.     r = assertSelect(16)
  332.     if isError(r) then return r end
  333.  
  334.     turtle.suck(1)
  335.     turtle.refuel(1)
  336.  
  337.     r = assertSelect(4)
  338.     if isError(r) then return r end
  339.  
  340.     r = assertDig(SIDE_FRONT)
  341.     if isError(r) then return r end
  342.  
  343.     turtle.turnRight()
  344.     turtle.turnRight()
  345. end
  346.  
  347. function assertSlot(slot, item, qty)
  348.     local data = turtle.getItemDetail(slot)
  349.     if item == nil and data ~= nil then
  350.         return commandErrorSlot(slot, "should be nil, has "..data['name'])
  351.     end
  352.     if item ~= nil and data == nil then
  353.         return commandErrorSlot(slot, "is nil, expecting "..item)
  354.     end
  355.     if item ~= data['name'] then
  356.         return commandErrorSlot(slot, "is "..data['name']..", expecting "..item)
  357.     end
  358.     if qty ~= data['count'] then
  359.         return commandErrorSlot(slot, "has "..data['count']..", expecting "..qty)
  360.     end
  361.     return nil
  362. end
  363.  
  364. function assertSlotAndSelect(slot, item, qty)
  365.     local res = assertSlot(slot, item, qty)
  366.     if res ~= nil then return res end
  367.  
  368.     res = turtle.select(slot)
  369.     if res == false then return commandErrorSlot(slot, "select fail") end
  370.     return nil
  371. end
  372.  
  373. function assertSelect(slot)
  374.     res = turtle.select(slot)
  375.     if res == false then return commandErrorSlot(slot, "select fail") end
  376.     return nil
  377. end
  378.  
  379. function assertPlace(dir)
  380.     local r
  381.     if dir == SIDE_DOWN then
  382.         r = turtle.placeDown()
  383.     elseif dir == SIDE_FRONT then
  384.         r = turtle.place()
  385.     elseif dir == SIDE_UP then
  386.         r = turtle.placeUp()
  387.     end
  388.     if r == false then
  389.         return commandError("place dir "..dir.." fail")
  390.     end
  391.     return nil
  392. end
  393.  
  394. function assertDig(dir)
  395.     local r
  396.     if dir == SIDE_DOWN then
  397.         r = turtle.digDown()
  398.     elseif dir == SIDE_FRONT then
  399.         r = turtle.dig()
  400.     elseif dir == SIDE_UP then
  401.         r = turtle.digUp()
  402.     end
  403.     if r == false then
  404.         return commandError("dig dir "..dir.." fail")
  405.     end
  406.     return nil
  407. end
  408.  
  409. function inventoryAssert()
  410.     if turtleSlots[role] == nil then error(role.." seems to be an invalid role") end
  411.  
  412.     local r
  413.     for index, slot in pairs(turtleSlots[role]) do
  414.         r = assertSlot(index, slot[1], slot[2])
  415.         if isError(r) then error(r.r) end
  416.     end
  417. end
  418.  
  419. function inventoryFilter()
  420.     if turtleSlots[role] == nil then error(role.." seems to be an invalid role") end
  421.  
  422.     turtle.turnRight()
  423.     turtle.turnRight()
  424.  
  425.     for i=1,16 do
  426.         turtle.select(i)
  427.         local data = turtle.getItemDetail()
  428.         if data ~= nil then
  429.             local found = false
  430.             for _, info in pairs(turtleSlots[role]) do
  431.                 if data['name'] == info[1] then
  432.                     found = true
  433.                 end
  434.             end
  435.  
  436.             if not found then
  437.                 local success = turtle.drop()
  438.                 if not success then error("failed to drop item") end
  439.             end
  440.         end
  441.     end
  442.  
  443.     turtle.turnLeft()
  444.     turtle.turnLeft()
  445. end
  446.  
  447. function inventoryCrudeSort()
  448.     if turtleSlots[role] == nil then error(role.." seems to be an invalid role") end
  449.  
  450.     for i=1,16 do
  451.         turtle.select(i)
  452.         local data = turtle.getItemDetail()
  453.         if data ~= nil then
  454.             if data['name'] ~= ITEM_FUEL_CHEST then
  455.                 for destSlot, info in pairs(turtleSlots[role]) do
  456.                     if data['name'] == info[1] then
  457.                         turtle.transferTo(destSlot)
  458.                     end
  459.                 end
  460.             end
  461.         end
  462.     end
  463. end
  464.  
  465. function commandErrorSlot(slot, reason)
  466.     return commandError("slot "..slot.." "..reason)
  467. end
  468.  
  469. function commandError(reason)
  470.     local r = {}
  471.     r['s'] = false
  472.     r['r'] = reason
  473.     return r
  474. end
  475.  
  476. function isError(error)
  477.     if error == nil then return false end
  478.     if error['s'] == nil or error['s'] == true then return false end
  479.     return true
  480. end
  481.  
  482. args = {...}
  483.  
  484. updateMetadata = checkForUpdates()
  485. if updateMetadata ~= nil then
  486.     downloadUpdate(updateMetadata)
  487. end
  488.  
  489. setup()
  490. main()
  491.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement