NindyBun

Quarry2.0

Dec 16th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.46 KB | None | 0 0
  1. --[[
  2.          _ _ _ _ _ _ _ _ _
  3.         |                 |
  4.         |                 |
  5.         |                 |
  6.         |                 |
  7.         |                 |
  8.         |                 |
  9.         |x _ _ _ _ _ _ _ _|
  10.        
  11.         Enclosed area is the space to be mined where
  12.         x is the starting position of the robot.
  13.        
  14.         Notes:
  15.             - Put in the network address of the reciever.
  16.             - Put in the port at which the robot and the reciever will communicate through.
  17.             - When placing the robot, it must face into the quarry.
  18.             - The charger needs to be placed on the left side of the robot. Make sure it gets power and a redstone signal.
  19.             - The tool box/chest needs to be placed above the charger.
  20.             - The storage chest needs to be placed on top of the tools chest.
  21.             - When filling the tools box, a diamond level tool is prefered.
  22.            
  23.         Minimum Robot Specs:
  24.             - Tier 1 Screen
  25.             - 2 Tier 1 Memory cards
  26.             - Tier 1 CPU
  27.             - Tier 2 Computer Case
  28.             - Tier 1 Graphics Card
  29.             - Disk Drive
  30.             - Wireless network card
  31.             - 1 Inventory Upgrade
  32.             - Inventory Controller Upgrade
  33.             - Keyboard
  34.             - EEPROM with lua BIOS
  35.             - Tier 1 Hard Disk Drive with lua BIOS installed
  36.            
  37.            
  38. --]]
  39.  
  40. local robot     = require("robot")
  41. local shell     = require("shell")
  42. local component = require("component")
  43. local text      = require("text")
  44. local computer  = require("computer")
  45.  
  46. local left      = robot.turnLeft
  47. local right     = robot.turnRight
  48. local around    = robot.turnAround
  49. local forward   = robot.forward
  50. local down      = robot.down
  51. local up        = robot.up
  52.  
  53. local swing     = robot.swing
  54. local swingD    = robot.swingDown
  55. local swingU    = robot.swingUp
  56.  
  57. local target    =  --targets address
  58. local port      =  --Port
  59.  
  60. local switch = 1 --1 is for right and -1 is for left
  61. local zcord, xcord, ycord, f = 0, 0, 0, 0
  62.  
  63. function mts(message)
  64.     component.modem.send(target, port, message)
  65. end
  66.  
  67. --Moves robot to the origin
  68. function returnToZero()
  69.     --Saves current cords as old cords
  70.     local zold, xold, yold, fold = zcord, xcord, ycord, math.fmod(f, 4)
  71.     --Moves robot to y=0, x=0, z=0 f=2
  72.     for i = yold, 1, -1 do
  73.         up()
  74.     end
  75.     if (fold == 0) then
  76.         left()
  77.     elseif (fold == 2) then
  78.         right()
  79.     elseif (fold == 1) then
  80.         around()
  81.     end
  82.     fold = 3
  83.     for i = xold, 1, -1 do
  84.         forward()
  85.     end
  86.     left()
  87.     fold = 2
  88.     for i = zold, 1, -1 do
  89.         forward()
  90.     end
  91. end
  92.  
  93. --Moves robot to previous cordinites and face
  94. function returnToCords()
  95.     local fold = math.fmod(f, 4)
  96.     around()
  97.     for i = 1, zcord do
  98.         forward()
  99.     end
  100.     right()
  101.     for i = 1, xcord do
  102.         forward()
  103.     end
  104.     for i = 1, ycord do
  105.         down()
  106.     end
  107.     if (fold == 0) then
  108.         left()
  109.     elseif (fold == 2) then
  110.         right()
  111.     elseif (fold == 3) then
  112.         around()
  113.     end
  114. end
  115.  
  116. --No tool then get tool
  117. function eTool()
  118.     --Goes to Origin
  119.     returnToZero()
  120.     --Equips new tool
  121.     up()
  122.     right()
  123.     robot.select(1)
  124.     robot.suck()
  125.     component.inventory_controller.equip()
  126.     robot.select(2)
  127.     left()
  128.     down()
  129.     --Returns robot to previous cords
  130.     returnToCords()
  131. end
  132.  
  133. --Checks if inventory almost full then dump to storage
  134. function inv(done)
  135.     if ((robot.count(robot.inventorySize()) > 1) or done) then
  136.         --Go to drop off point
  137.         returnToZero()
  138.         --Transport items
  139.         up()
  140.         up()
  141.         right()
  142.         for i = 2, robot.inventorySize() do
  143.             robot.select(i)
  144.             robot.drop()
  145.         end
  146.         robot.select(2)
  147.         left()
  148.         down()
  149.         down()
  150.         --return back
  151.         if not done then
  152.             returnToCords()
  153.         end
  154.     end
  155. end
  156.  
  157. --Robot gets stuck
  158. function stuck()
  159.     --Checks to see if the problem is fixed
  160.     mts("Robot is stuck")
  161.     repeat
  162.         print("Type 'FIXED' to resume")
  163.         local input = string.upper(io.read())
  164.     until string.upper(input) == "FIXED" and robot.detect()
  165. end
  166.  
  167. --Robot needs to charge
  168. function energy()
  169.     if (computer.energy() < (computer.maxEnergy()/3)) then
  170.         returnToZero()
  171.         while (computer.energy() < (computer.maxEnergy()-10)) do --[[ Waits for the robot to charge up--]]end
  172.         returnToCords()
  173.     end
  174. end
  175.  
  176. --Check swinging
  177. function swingSafe(side)
  178.     --Checks if there is a tool
  179.     local _, dur_message = robot.durability()
  180.     if (dur_message == "no tool equipped") then
  181.         eTool()
  182.     end
  183.     --Checks if the side is broken
  184.     if (side == "front") then
  185.         local arg, message = swing()
  186.         if (message == "entity") then
  187.             local stuckCount = 0
  188.             while (swing()) do
  189.                 swing()
  190.                 stuckCount = stuckCount + 1
  191.                 if (stuckCount == 10) then
  192.                     stuck()
  193.                 end
  194.             end
  195.         elseif (arg == false and message == "block") then
  196.             return stuck()
  197.         end
  198.     elseif (side == "down") then
  199.         local arg, message = swingD()
  200.         if (message == "entity") then
  201.             local stuckCount = 0
  202.             while (swingD()) do
  203.                 swingD()
  204.                 stuckCount = stuckCount + 1
  205.                 if (stuckCount == 10) then
  206.                     stuck()
  207.                 end
  208.             end
  209.         elseif (arg == false and message == "block") then
  210.             return stuck()
  211.         end
  212.     elseif (side == "up") then
  213.         local arg, message = swingU()
  214.         if (message == "entity") then
  215.             local stuckCount = 0
  216.             while (swingU()) do
  217.                 swingU()
  218.                 stuckCount = stuckCount + 1
  219.                 if (stuckCount == 10) then
  220.                     stuck()
  221.                 end
  222.             end
  223.         elseif (arg == false and message == "block") then
  224.             return stuck()
  225.         end
  226.     end
  227. end
  228.  
  229. --Check movement
  230. function moveSafe(side)
  231.     --Checks if side is broken
  232.     if (side == "front") then
  233.         local _, message = forward()
  234.         if (message == "entity" or message == "solid") then
  235.             local stuckCount = 0
  236.             while (not(forward())) do
  237.                 swingSafe(side)
  238.                 stuckCount = stuckCount + 1
  239.                 if (stuckCount == 10) then
  240.                     stuck()
  241.                 end
  242.             end
  243.         end
  244.     elseif (side == "down") then
  245.         local _, message = down()
  246.         if (message == "entity" or message == "solid") then
  247.             local stuckCount = 0
  248.             while (not(down())) do
  249.                 swingSafe(side)
  250.                 stuckCount = stuckCount + 1
  251.                 if (stuckCount == 10) then
  252.                     stuck()
  253.                 end
  254.             end
  255.         end
  256.     elseif (side == "up") then
  257.         local _, message = up()
  258.         if (message == "entity" or message == "solid") then
  259.             local stuckCount = 0
  260.             while (not(up())) do
  261.                 swingSafe(side)
  262.                 stuckCount = stuckCount + 1
  263.                 if (stuckCount == 10) then
  264.                     stuck()
  265.                 end
  266.             end
  267.         end
  268.     end
  269. end
  270.  
  271. --Right turn
  272. function rightTurn()
  273.     swingSafe("down")
  274.     right()
  275.     f = f + switch
  276.     moveSafe("front")
  277.     if (math.fmod(ycord, 2) == 0) then
  278.         xcord = xcord + 1
  279.     elseif (math.fmod(ycord, 2) == 1) then
  280.         xcord = xcord - 1
  281.     end
  282.     right()
  283.     f = f + switch
  284. end
  285.  
  286. --Left turn
  287. function leftTurn()
  288.     swingSafe("down")
  289.     left()
  290.     f = f + switch
  291.     moveSafe("front")
  292.     if (math.fmod(ycord, 2) == 0) then
  293.         xcord = xcord + 1
  294.     elseif (math.fmod(ycord, 2) == 1) then
  295.         xcord = xcord - 1
  296.     end
  297.     left()
  298.     f = f + switch
  299. end
  300.  
  301. --Quarry
  302. function dig(width, length, height)
  303.     --Defaults slot to 2
  304.     --Slot one is for grabbing a tool
  305.     robot.select(2)
  306.     --Width is x-axis
  307.     --Length is z-axis
  308.     --Height is y-axis
  309.     for y = 1, height do    --per layer
  310.         for x = 1, width do     --per row
  311.             for z = 1, length - 1 do    --per column
  312.                 swingSafe("down")
  313.                 moveSafe("front")
  314.                 if (math.fmod(ycord, 2) == 0) then
  315.                     zcord = zcord + switch
  316.                 elseif (math.fmod(ycord, 2) == 1) then
  317.                     zcord = zcord - switch
  318.                 end
  319.             end
  320.             energy()    --Checks if robot needs to charge after one column
  321.             if (x < width) then     --Checks if the current column is the last column before the next layer
  322.                 if (switch == 1) then   --to turn into the right column
  323.                     rightTurn()
  324.                 elseif (switch == -1) then  --to turn into the left column
  325.                     leftTurn()
  326.                 end
  327.                 switch = -switch
  328.             else
  329.                 break
  330.             end
  331.         end
  332.         --Dig below and goes to bottom layer
  333.         swingSafe("down")
  334.         if (y < height) then
  335.             moveSafe("down")
  336.             around()
  337.             f = f + (2*switch)
  338.             ycord = ycord + 1
  339.         end
  340.     end
  341.     inv(true)
  342.     forward()
  343.     mts("Quarry is finished")
  344.     component.modem.close(123)
  345.     computer.shutdown()
  346. end
  347.  
  348. --Gets users dimension
  349. function userSize()
  350.     local size
  351.     --Continuously asks user for sizes until those are integers
  352.     repeat
  353.         print("Quarry Dimensions?")
  354.         print("Width Length Height")
  355.         size = text.tokenize(io.read())
  356.         if (tonumber(size[1]) and tonumber(size[2]) and tonumber(size[3])) then
  357.             size[1] = tonumber(size[1])
  358.             size[2] = tonumber(size[2])
  359.             size[3] = tonumber(size[3])
  360.         else
  361.             print("Some of those are not numbers, please try again...")
  362.         end
  363.     until tonumber(size[1]) and tonumber(size[2]) and tonumber(size[3])
  364.     --Size confermation
  365.     repeat
  366.         print("Width: " .. size[1] .. "\nLength: " .. size[2] .. "\nHeight: " .. size[3])
  367.         print("Are those the right sizes? y/n")
  368.         local input = string.lower(io.read())
  369.         if (input == "y") then
  370.             dig(size[1], size[2], size[3])
  371.         elseif (input == "n") then
  372.             userSize()
  373.         end
  374.     until input == "y" or input == "n"
  375. end
  376.  
  377. userSize()
Advertisement
Add Comment
Please, Sign In to add comment