Advertisement
IronicPickle

hammer_quarry.lua

Sep 3rd, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.89 KB | None | 0 0
  1. local robot = require("robot")
  2. local component = require("component")
  3. local geo = component.geolyzer
  4. local modem = false
  5. if(component.isAvailable("modem")) then
  6.   modem = component.modem
  7. end
  8. local crafting = false
  9. if(component.isAvailable("crafting")) then
  10.   crafting = component.crafting
  11. end
  12. local invCon = component.inventory_controller
  13. local event = require("event")
  14. local term = require("term")
  15. local computer =  require("computer")
  16. local ser = require("serialization")
  17. local currX = 0
  18. local currZ = 0
  19. local currY = 0
  20. local status = "Nominal"
  21. local currD = "0"
  22. local mined = 0
  23.  
  24. local x = 10
  25. local z = 10
  26. local y = 10
  27. local skip = 0
  28. local port = 90
  29. local strength = 400
  30.  
  31. function turn(direction)
  32.   if direction == "right" then
  33.     currD = currD + 1
  34.     robot.turnRight()
  35.   elseif direction == "left" then
  36.     currD = currD - 1
  37.     robot.turnLeft()
  38.   end
  39.   if currD < 0 then
  40.     currD = 3
  41.   elseif currD > 3 then
  42.     currD = 0
  43.   end
  44. end
  45.  
  46. function statusCheck()
  47.   status = "Checking"
  48.   if(checkPower() < 25 or checkInv() == "full" or not checkTool()) then
  49.     resetPos(currX, currZ, currY, false)
  50.   end
  51.   status = "Nominal"
  52. end
  53.  
  54. function sendStatus()
  55.   if(modem) then
  56.     modem.broadcast(port, ser.serialize({
  57.       status = status,
  58.       currX = currX,
  59.       currZ = currZ,
  60.       currY = currY,
  61.       mined = mined,
  62.       power = checkPower(),
  63.       x = x,
  64.       z = z,
  65.       y = y,
  66.       skip = skip
  67.     }))
  68.   end
  69. end
  70.  
  71. function resetPos(oldX, oldZ, oldY, wait)
  72.   status = "Returning"
  73.   sendStatus()
  74.   print("Returning to Origin...")
  75.   local oldOri = currD
  76.   local ori = 2 - currD
  77.   for i = 1, ori do
  78.     turn("right")
  79.   end
  80.   digForward(currX)
  81.   ori = 3 - currD
  82.   for i = 1, ori do
  83.     turn("right")
  84.   end
  85.   digForward(currZ)
  86.   digUp(currY)
  87.   turn("left")
  88.   local maxInv = robot.inventorySize()
  89.   emptyInv()
  90.   digUp(1)
  91.   getTool()
  92.   digDown(1)
  93.   emptyInv()
  94.   turn("right")
  95.   turn("right")
  96.   print("Recharging...")
  97.   local percent = checkPower()
  98.   while(percent < 99) do
  99.     percent = checkPower()
  100.     status = "Recharging"
  101.     sendStatus()
  102.     print("Recharging: " .. math.floor(percent) .. "%")
  103.     os.sleep(1)
  104.   end
  105.   print("Recharged")
  106.   print("Returning to Position")
  107.   status = "Nominal"
  108.   sendStatus()
  109.   continue(oldX, oldZ, oldY, oldOri)
  110. end
  111.  
  112. function continue(oldX, oldZ, oldY, oldOri)
  113.   digDown(oldY)
  114.   turn("right")
  115.   digForward(oldZ)
  116.   turn("left")
  117.   digForward(oldX)
  118.   local ori =  oldOri - currD
  119.   for i = 1, ori do
  120.     turn("right")
  121.   end
  122. end
  123.  
  124. function emptyInv()
  125.   status = "Emptying"
  126.   sendStatus()
  127.   local maxInv = robot.inventorySize()
  128.   print("Emptying inventory...")
  129.   for i = 1, maxInv do
  130.     robot.select(i)
  131.     robot.drop()
  132.   end
  133.   robot.select(1)
  134. end
  135.  
  136. function getTool()
  137.   if(not checkTool()) then
  138.     while true do
  139.       status = "Equipping"
  140.       sendStatus()
  141.       local targetInvMax = invCon.getInventorySize(3)
  142.       if(targetInvMax == nil) then
  143.         print("No tool inventory")
  144.         return
  145.       end
  146.       print("Checking inventory...")
  147.       for i = 1, targetInvMax do
  148.         if(invCon.getStackInSlot(3, i) ~= nil) then
  149.           local item = invCon.getStackInSlot(3, i)
  150.           invCon.suckFromSlot(3, i, 1)
  151.           if(item["name"] == "tconstruct:sharpening_kit") then
  152.             if(not crafting) then
  153.               print("No crafting upgrade")
  154.               break
  155.             end
  156.             status = "Repairing"
  157.             sendStatus()
  158.             robot.select(2)
  159.             invCon.equip()
  160.             crafting.craft(1)
  161.             robot.select(2)
  162.             invCon.equip()
  163.           else
  164.             robot.select(1)
  165.             invCon.equip()
  166.           end
  167.           print("Tool equipped!")
  168.           return
  169.         end
  170.       end
  171.       print("No tool available!")
  172.       print("Press enter to check again...")
  173.       status = "No tool"
  174.       sendStatus()
  175.       term.read()
  176.     end
  177.   end
  178. end
  179.  
  180. function checkInv()
  181.   local invMax = robot.inventorySize()
  182.   local count = robot.count(invMax)
  183.   if(count > 0) then
  184.     return "full"
  185.   else
  186.     return "available"
  187.   end
  188. end
  189.  
  190. function checkTool()
  191.   local dur = robot.durability()
  192.  
  193.   if(dur == nil or dur <= 0.05) then
  194.     return false
  195.   else
  196.     return true
  197.   end
  198. end
  199.  
  200. function checkPower()
  201.   local power = computer.energy()
  202.   local maxPower = computer.maxEnergy()
  203.   local percent = power/(maxPower/100)
  204.   return percent
  205. end
  206.  
  207. function digDown(distY)
  208.   local move
  209.   for i = 1, distY do
  210.     sendStatus()
  211.     move = nil
  212.     while(move == nil) do
  213.       if status == "Nominal" then
  214.         statusCheck()
  215.       end
  216.       local dig, block = robot.swingDown()
  217.       if block == "block" then
  218.         mined = mined + 1
  219.       end
  220.       move = robot.down()
  221.     end
  222.     currY = currY + 1
  223.   end
  224. end
  225.  
  226. function digUp(distY)
  227.   local move
  228.   for i = 1, distY do
  229.     sendStatus()
  230.     move = nil
  231.     while(move == nil) do
  232.       if status == "Nominal" then
  233.         statusCheck()
  234.       end
  235.       local dig, block = robot.swingUp()
  236.       if block == "block" then
  237.         mined = mined + 1
  238.       end
  239.       move = robot.up()
  240.     end
  241.     currY = currY - 1
  242.   end
  243. end
  244.  
  245. function digForward(distX, axis)
  246.   local move
  247.   for i = 1, distX do
  248.     sendStatus()
  249.     move = nil
  250.     while(move == nil) do
  251.       if status == "Nominal" then
  252.         statusCheck()
  253.       end
  254.       local dig, block = robot.swing(0)
  255.       if block == "block" then
  256.         mined = mined + 1
  257.       end
  258.       move = robot.forward()
  259.     end
  260.     if axis == "+x" then
  261.       currX = currX + 1
  262.     elseif axis == "-x" then
  263.       currX = currX - 1
  264.     elseif axis == "+z" then
  265.       currZ = currZ + 1
  266.     elseif axis == "-z" then
  267.       currZ = currZ - 1
  268.     end
  269.   end
  270. end
  271.  
  272. function row(distX)
  273.   print("Current Segment: " .. math.floor(currZ/3)+1)
  274.   turn("left")
  275.   digForward(distX, "+x")
  276.   turn("right")
  277.   turn("right")
  278.   digForward(distX, "-x")
  279.   turn("left")
  280. end
  281.  
  282. function plane(distX, distZ)
  283.   print("Current Level: " .. math.floor(currY-skip))
  284.   row(distX)
  285.   for i = 1, distZ do
  286.     digForward(1, "+z")
  287.     if(i % 3 == 0) then
  288.       row(distX)
  289.     end
  290.   end
  291.   turn("right")
  292.   turn("right")
  293.   digForward(distZ, "-z")
  294.   turn("right")
  295.   turn("right")
  296. end
  297.  
  298. function start(distX, distZ, distY)
  299.   distY = distY - 1
  300.   distX = distX - 1
  301.   distZ = distZ - 1
  302.   digDown(2 + skip)
  303.   turn("right")
  304.   plane(distX, distZ)
  305.   for i = 1, distY do
  306.     digDown(1)
  307.     if(i % 3 == 0) then
  308.       plane(distX, distZ)
  309.     end
  310.   end
  311.   status = "Returning"
  312.   sendStatus()
  313.   digUp(currY)
  314.   turn("right")
  315.   emptyInv()
  316.   turn("left")
  317.   turn("left")
  318.   status = "Complete"
  319.   sendStatus()
  320.   print("Quarry complete!")
  321. end
  322.  
  323. print("Quarry Config:")
  324. print("- Omit to use default values")
  325. print("- () = Default Values")
  326. term.write("X (" .. x .. "): ")
  327. local confX = term.read()
  328. if string.len(confX) > 1 then
  329.   x = tonumber(confX)
  330. end
  331. term.write("Z (" .. z .. "): ")
  332. local confZ = term.read()
  333. if string.len(confZ) > 1 then
  334.   z = tonumber(confZ)
  335. end
  336. term.write("Y (" .. y .. "): ")
  337. local confY = term.read()
  338. if string.len(confY) > 1 then
  339.   y = tonumber(confY)
  340. end
  341. term.write("Skip Levels (" .. skip .. "): ")
  342. local confSkip = term.read()
  343. if string.len(confSkip) > 1 then
  344.   skip = tonumber(confSkip)
  345. end
  346. term.write("Broadcast Port (" .. port .. "): ")
  347. confPort = term.read()
  348. if string.len(confPort) > 1 then
  349.   port = tonumber(confPort)
  350. end
  351. term.write("Signal Strength (" .. strength .. "): ")
  352. confStrength = term.read()
  353. if string.len(confStrength) > 1 then
  354.   strength = tonumber(confStrength)
  355. end
  356.  
  357. print("\nConfirm Config:")
  358. print("X: " .. x)
  359. print("Z: " .. z)
  360. print("Y: " .. y)
  361. print("Skipping " .. skip)
  362. print("Broadcast Port: " .. port)
  363. print("Signal Strength: " .. strength)
  364. print("\nPress Enter to start")
  365. term.read()
  366. print("\nStarting Quarry...\n")
  367.  
  368. if(modem) then
  369.   modem.setStrength(strength)
  370. end
  371. start(x,z,y,skip)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement