solidity

Untitled

Dec 12th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.15 KB | None | 0 0
  1. args = { ... }
  2.  
  3. torchSlot = 1
  4. torchSpacing = 5
  5. hitBedrock = false
  6.  
  7. -- define some relative coordinates
  8. home = {0,0,0}  -- define home position
  9. face = 1        -- define home direction
  10. pos = {0,0,0}
  11. backPos = {0,0,0}
  12. if #args == 6 then  -- if specified, set quarry position
  13.     quarryPos ={tonumber(args[4]), tonumber(args[5]), tonumber(args[6])}
  14. end
  15. movement = {{1,0,0},{0,0,1},{-1,0,0},{0,0,-1}}
  16.  
  17. -- movement functions
  18.  
  19. function quarryRight()
  20.     turtle.turnRight()
  21.     face = face + 1
  22.     if face > 4 then
  23.         face = 1
  24.     end
  25. end
  26.  
  27. function quarryLeft()
  28.     turtle.turnLeft()
  29.     face = face - 1
  30.     if face < 1 then
  31.         face = 4
  32.     end
  33. end
  34.  
  35. function digMove()
  36.     if type(turtle.getFuelLevel()) == "number" then
  37.         if turtle.getFuelLevel() < 10 then
  38.             turtle.select(16)
  39.             turtle.refuel(1)
  40.         end
  41.     end
  42.     if digging then
  43.         if turtle.getItemCount(16) > 0 then
  44.             digging = false
  45.             oldX = pos[1]
  46.             oldY = pos[2]
  47.             oldZ = pos[3]
  48.             oldFace = face
  49.             dropInv()
  50.             goBack(oldX, oldY, oldZ, oldFace)
  51.             print("we should be back now")
  52.             digging = true
  53.         end
  54.     end
  55.     moved = turtle.forward()
  56.     while not moved do
  57.         if not turtle.dig() then
  58.             if not turtle.attack() then
  59.                 hitBedrock = true
  60.                 print("hit Bedrock, bailing out")
  61.                 dropInv()
  62.             end
  63.         end
  64.         moved = turtle.forward()
  65.     end
  66.     if moved then
  67.         pos[1] = pos[1] + movement[face][1]
  68.         pos[2] = pos[2] + movement[face][2]
  69.         pos[3] = pos[3] + movement[face][3]
  70.     end
  71.     print(pos[1].." "..pos[2].." "..pos[3].." "..face)
  72. end
  73.  
  74. function digMoveDown()
  75.     if type(turtle.getFuelLevel()) == "number" then
  76.         if turtle.getFuelLevel() < 10 then
  77.             turtle.select(16)
  78.             turtle.refuel(1)
  79.         end
  80.     end
  81.     moved = turtle.down()
  82.     while not moved do
  83.         if not turtle.digDown() then
  84.             if not turtle.attackDown() then
  85.                 hitBedrock = true
  86.                 print("hit Bedrock, bailing out")
  87.                 dropInv()
  88.             end
  89.         end
  90.         moved = turtle.down()
  91.     end
  92.     if moved then
  93.         pos[2] = pos[2] - 1
  94.     end
  95.     print(pos[1].." "..pos[2].." "..pos[3].." "..face)
  96. end
  97.  
  98. function digMoveUp()
  99.     if type(turtle.getFuelLevel()) == "number" then
  100.         if turtle.getFuelLevel() < 10 then
  101.             turtle.select(16)
  102.             turtle.refuel(1)
  103.         end
  104.     end
  105.     moved = turtle.up()
  106.     while not moved do
  107.         if not turtle.digUp() then
  108.             if not turtle.attackUp() then
  109.                 hitBedrock = true
  110.             end
  111.         end
  112.         moved = turtle.up()
  113.     end
  114.     if moved then
  115.         pos[2] = pos[2] + 1
  116.     end
  117.     print(pos[1].." "..pos[2].." "..pos[3].." "..face)
  118. end
  119.  
  120. -- meta movement functions (gohome, goback)
  121.  
  122. function dropInv()
  123.     print("iventory is getting full, gotta drop some stuff off")
  124.     while pos[2] < 0 do
  125.         digMoveUp()
  126.     end
  127.     if pos[1] ~= home[1] then
  128.         if pos[1] > home[1] then
  129.             while not (face == 3) do
  130.                 quarryLeft()
  131.             end
  132.         elseif pos[1] < home[1] then
  133.             while not (face == 1) do
  134.                 quarryLeft()
  135.             end
  136.         end
  137.     end
  138.     while pos[1] ~= home[1] do
  139.         digMove()
  140.     end
  141.     if pos[3] ~= home[3] then
  142.         if pos[3] > home[3] then
  143.             while not (face == 4) do
  144.                 quarryLeft()
  145.             end
  146.         elseif pos[3] < home[3] then
  147.             while not (face == 2) do
  148.                 quarryLeft()
  149.             end
  150.         end
  151.     end
  152.     while pos[3] ~= home[3] do
  153.         digMove()
  154.     end
  155.     while face ~= 3 do
  156.         quarryLeft()
  157.     end
  158.     for i = 1,16 do
  159.         turtle.select(i)
  160.         turtle.drop()
  161.     end
  162.     turtle.select(1)
  163.     if hitBedrock then
  164.         os.reboot()
  165.     end
  166. end
  167.  
  168. function goBack(oldX, oldY, oldZ, oldFace)
  169.     print("alright, back to "..oldX.." "..oldY.." "..oldZ)
  170.     if pos[1] ~= oldX then
  171.         if pos[1] > oldX then
  172.             while not (face == 3) do
  173.                 quarryLeft()
  174.             end
  175.         elseif pos[1] < oldX then
  176.             while not (face == 1) do
  177.                 quarryLeft()
  178.             end
  179.         end
  180.     end
  181.     while pos[1] ~= oldX do
  182.         digMove()
  183.     end
  184.     if pos[3] ~= oldZ then
  185.         if pos[3] > oldZ then
  186.             while not (face == 4) do
  187.                 quarryLeft()
  188.             end
  189.         elseif pos[3] < oldZ then
  190.             while not (face == 2) do
  191.                 quarryLeft()
  192.             end
  193.         end
  194.     end
  195.     while pos[3] ~= oldZ do
  196.         digMove()
  197.     end
  198.     while pos[2] > oldY do
  199.         digMoveDown()
  200.     end
  201.     while face ~= oldFace do
  202.         quarryLeft()
  203.     end
  204. end
  205.  
  206. function gotoQuarry(qPos)
  207.     print("going to quarry position")
  208.     if pos[1] ~= qPos[1] then
  209.         if pos[1] > qPos[1] then
  210.             while not (face == 3) do
  211.                 quarryLeft()
  212.             end
  213.         elseif pos[1] < qPos[1] then
  214.             while not (face == 1) do
  215.                 quarryLeft()
  216.             end
  217.         end
  218.     end
  219.     while pos[1] ~= qPos[1] do
  220.         digMove()
  221.     end
  222.     if pos[3] ~= qPos[3] then
  223.         if pos[3] > qPos[3] then
  224.             while not (face == 4) do
  225.                 quarryLeft()
  226.             end
  227.         elseif pos[3] < qPos[3] then
  228.             while not (face == 2) do
  229.                 quarryLeft()
  230.             end
  231.         end
  232.     end
  233.     while pos[3] ~= qPos[3] do
  234.         digMove()
  235.     end
  236.     print("going to quarry position")
  237.     if pos[2] ~= qPos[2] then
  238.         if pos[2] > qPos[2] then
  239.             while not (pos[2] == qPos[2]) do
  240.                 digMoveDown()
  241.             end
  242.         elseif pos[2] < qPos[2] then
  243.             while not (pos[2] == qPos[2]) do
  244.                 digMoveUp()
  245.             end
  246.         end
  247.     end
  248.     while not (face == 1) do
  249.         quarryLeft()
  250.     end
  251. end
  252.  
  253. function digLine(length, lNumber)
  254.     -- print("digging a "..length.." long line")
  255.     for i = 2,length do
  256.         turtle.digDown()
  257.         turtle.digUp()
  258.         if ((i - 1) % torchSpacing == 1) and (lNumber % torchSpacing == 1) then
  259.             turtle.select(torchSlot)
  260.             --turtle.placeDown()
  261.         end
  262.         digMove()
  263.     end
  264.     turtle.digDown()
  265.     turtle.digUp()
  266. end
  267.  
  268. function nextLine(oldLine, lDir)
  269.     -- print("turning for next line")
  270.     if oldLine % 2 == 1 then    -- odd linenumber -> turn in layer direction
  271.         if lDir == "r" then     -- layer direction is right
  272.             quarryRight()
  273.             digMove()
  274.             quarryRight()
  275.         else                    -- layer direction is left
  276.             quarryLeft()
  277.             digMove()
  278.             quarryLeft()
  279.         end
  280.     else                        -- even linenumber -> turn against layer direction
  281.         if lDir == "l" then     -- layerdirection is left
  282.             quarryRight()
  283.             digMove()
  284.             quarryRight()
  285.         else                    -- layer direction is right
  286.             quarryLeft()
  287.             digMove()
  288.             quarryLeft()
  289.         end
  290.     end
  291. end
  292.  
  293. function digLayer(lWidth, lLength, lDir)
  294.     -- print("digging a "..lLength.." long and "..lWidth.." wide layer")
  295.     for n = 1,lWidth do
  296.         digLine(lLength,n)
  297.         if n < lWidth then
  298.             nextLine(n, lDir)
  299.         end
  300.     end
  301. end
  302.  
  303. function nextLayer(lWidth, lDir)
  304.     -- print("going down for next layer")
  305.     for n = 1,3 do
  306.         digMoveDown()
  307.     end
  308.     turtle.digDown()
  309.     if lWidth % 2 == 1 then     -- odd width -> turn aginst layerDirection
  310.         -- print("old layer had an odd width, turning against it")
  311.         if lDir == "l" then
  312.             quarryRight()
  313.         else
  314.             quarryLeft()
  315.         end
  316.     else                        -- even width -> turn into layerDirection
  317.         -- print("old layer had an even width, turning with it")
  318.         if lDir == "r" then
  319.             quarryRight()
  320.         else
  321.             quarryLeft()
  322.         end
  323.     end
  324. end
  325.  
  326. function digQuarry(qWidth, qLength, qDir)
  327.     digging = true
  328.     depth = 50
  329.    
  330.     if #args == 6 then      -- goto quarry position
  331.         gotoQuarry(quarryPos)
  332.     end
  333.    
  334.     lWidth = qWidth
  335.     lLength = qLength
  336.     lDir = qDir
  337.     for layer = 1,depth do
  338.         digLayer(lWidth, lLength, lDir)
  339.         if layer < depth then
  340.             nextLayer(lWidth, lDir)
  341.         end
  342.         if lWidth % 2 == 1 then     -- odd layer width -> change direction
  343.             -- print("last layer had an odd width, changing layer direction")
  344.             if lDir == "l" then
  345.                 lDir = "r"
  346.             else
  347.                 lDir = "l"
  348.             end
  349.         end
  350.         lWidth, lLength = lLength, lWidth   --switch dimensions
  351.     end
  352. end
  353.  
  354. digQuarry(tonumber(args[1]),tonumber(args[2]),args[3])
  355. --gotoQuarry({3,-2,-2})
Advertisement
Add Comment
Please, Sign In to add comment