KakarotoCm

Smart Miner Alpha 0.5

Aug 2nd, 2015
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.83 KB | None | 0 0
  1. --Predefinitions
  2. facing = "north"
  3. x = 0                   --current turtle X
  4. y = 0                   --current turtle Y
  5. z = 0                   --current turtle Z
  6. mainY = 0               --main minning level(Y coordinate)
  7. mainFacing = "north"    --main face(direction where the turtle will mine)
  8. totalBlocksMoved = 0    --counts the total of blocks the turtle has walked
  9. totalBlocksMined = 0    --counts the total of blocks the turtle has mined
  10.  
  11. coordSequence = {}      --Creates the table where coordinates will be stored for the AI
  12.  
  13. -- General functions
  14. function table.contains(table, element)
  15.   for _, value in pairs(table) do
  16.     if value == element then
  17.       return true
  18.     end
  19.   end
  20.   return false
  21. end
  22.  
  23. function testString(currentString,tryToFind)
  24.  
  25.     if string.find(currentString,tryToFind) then
  26.         return true
  27.     else
  28.         return false
  29.     end
  30.  
  31. end
  32.  
  33. --Rotation Functions
  34. function turn(side)
  35.     if side == "right" then
  36.         turtle.turnRight()
  37.  
  38.         if  facing == "north" then --adjusts the direction the turtle is facing
  39.             facing = "east"
  40.         elseif facing == "east" then
  41.             facing = "south"
  42.         elseif facing == "south" then
  43.             facing = "west"
  44.         elseif facing == "west" then
  45.             facing = "north"
  46.         end
  47.        
  48.     elseif side == "left" then
  49.         turtle.turnLeft()
  50.        
  51.         if  facing == "north" then --adjusts the direction the turtle is facing
  52.             facing = "west"
  53.         elseif facing == "east" then
  54.             facing = "north"
  55.         elseif facing == "south" then
  56.             facing = "east"
  57.         elseif facing == "west" then
  58.             facing = "south"
  59.         end
  60.  
  61.     else
  62.         print("The side where to turn is not specified!")
  63.     end
  64. end
  65.  
  66. function face(direction)
  67.  
  68.     if (direction == "north" and facing == "east") or (direction == "east" and facing == "south") or (direction == "south" and facing == "west") or (direction == "west" and facing == "north") then
  69.         turn("left")
  70.     elseif (facing == "north" and direction == "east") or (facing == "east" and direction == "south") or (facing == "south" and direction == "west") or (facing == "west" and direction == "north") then
  71.         turn("right")
  72.     elseif (facing == "north" and direction == "south") or (facing == "south" and direction == "north") or (facing == "west" and direction == "east") or (facing == "east" and direction == "west") then
  73.         turn("right")
  74.         turn("right")
  75.     end
  76.  
  77. end
  78.  
  79. --Digging/Placing functions
  80. function dig()
  81.     while turtle.detect() do
  82.         if turtle.dig() then
  83.             totalBlocksMined = totalBlocksMined + 1
  84.         end
  85.     end
  86. end
  87.  
  88. function digUp()
  89.     while turtle.detectUp() do
  90.         if turtle.digUp() then
  91.             totalBlocksMined = totalBlocksMined + 1
  92.         end
  93.     end
  94. end
  95.  
  96. function digDown()
  97.     while turtle.detectDown() do
  98.         if turtle.digDown() then
  99.             totalBlocksMined = totalBlocksMined + 1
  100.         end
  101.     end
  102. end
  103.  
  104. function place()
  105.     while not turtle.place() do
  106.         dig()
  107.         turtle.attack()
  108.     end
  109. end
  110.  
  111. function placeUp()
  112.     while not turtle.placeUp() do
  113.         digUp()
  114.         turtle.attackUp()
  115.     end
  116. end
  117.  
  118. function placeDown()
  119.     while not turtle.placeDown() do
  120.         digDown()
  121.         turtle.attackDown()
  122.     end
  123. end
  124.  
  125. --Movimentation Functions
  126. function forward()
  127.     fuelTest()
  128.     while not turtle.forward() do
  129.         dig()
  130.         turtle.attack()
  131.     end
  132.    
  133.     if facing == "north" then
  134.         z = z - 1
  135.     elseif facing == "east" then
  136.         x = x + 1
  137.     elseif facing == "south" then
  138.         z = z + 1
  139.     elseif facing == "west" then
  140.         x = x - 1
  141.     end
  142.    
  143.     totalBlocksMoved = totalBlocksMoved + 1
  144.    
  145. end
  146.  
  147. function left()
  148.     turn("left")
  149.     forward()
  150. end
  151.  
  152. function right()
  153.     turn("right")
  154.     forward()
  155. end
  156.  
  157. function up()
  158.     fuelTest()
  159.     while not turtle.up() do
  160.         digUp()
  161.         turtle.attackUp()
  162.     end
  163.     y = y + 1
  164.     totalBlocksMoved = totalBlocksMoved + 1
  165. end
  166.  
  167. function down()
  168.     fuelTest()
  169.     while not turtle.down() do
  170.         digDown()
  171.         turtle.attackDown()
  172.     end
  173.     y = y - 1
  174.     totalBlocksMoved = totalBlocksMoved + 1
  175. end
  176.  
  177. function back()
  178.     turn("right") turn("right")
  179.     forward()
  180. end
  181.  
  182. --Inspection Functions
  183.  
  184. function isOre()
  185.     local sucess, blockData = turtle.inspect()
  186.     if sucess then
  187.         if string.find(blockData.name,"ore") or string.find(blockData.name,"Ore") or string.find(blockData.name,"resource") or string.find(blockData.name,"Resource") then
  188.             return true
  189.         elseif string.find(blockData.name,"SearedBrick") then --Actual name of TConstruct ores, may be a bug, check it in your current version!
  190.             return true
  191.         else
  192.             return false
  193.         end
  194.     else
  195.         return false
  196.     end
  197. end
  198.    
  199. function isOreUp()
  200.     local sucess, blockData = turtle.inspectUp()
  201.     if sucess then
  202.         if string.find(blockData.name,"ore") or string.find(blockData.name,"Ore") or string.find(blockData.name,"resource") or string.find(blockData.name,"Resource") then
  203.             return true
  204.         elseif string.find(blockData.name,"SearedBrick") then --Actual name of TConstruct ores, may be a bug, check it in your current version!
  205.             return true
  206.         else
  207.             return false
  208.         end
  209.     else
  210.         return false
  211.     end
  212. end
  213.  
  214. function isOreDown()
  215.     local sucess, blockData = turtle.inspectDown()
  216.     if sucess then
  217.         if string.find(blockData.name,"ore") or string.find(blockData.name,"Ore") or string.find(blockData.name,"resource") or string.find(blockData.name,"Resource") then
  218.             return true
  219.         elseif string.find(blockData.name,"SearedBrick") then --Actual name of TConstruct ores, may be a bug, check it in your current version!
  220.             return true
  221.         else
  222.             return false
  223.         end
  224.     else
  225.         return false
  226.     end
  227. end
  228.  
  229. function isOreRight()
  230.     turn("right")
  231.     local result = isOre()
  232.     turn("left")
  233.     return result
  234. end
  235.  
  236. function isOreLeft()
  237.     turn("left")
  238.     local result = isOre()
  239.     turn("right")
  240.     return result
  241. end
  242.  
  243. function isOreBack()
  244.     turn("right") turn("right")
  245.     local result = isOre()
  246.     turn("left") turn("left")
  247.     return result
  248. end
  249.  
  250. --Inventory Cleaning Functions
  251.  
  252.     function dropAll()
  253.         turtle.select(15)
  254.         placeDown()
  255.  
  256.         for slot = 1,14 do
  257.             turtle.select(slot)
  258.             if  not turtle.dropDown() and turtle.getItemCount() > 0 then
  259.                 while not turtle.dropDown() do
  260.                     print("Please, clear space in the item enderchest!")
  261.                     sleep(10)
  262.                 end
  263.             end
  264.         end
  265.  
  266.         turtle.select(15)
  267.         digDown()
  268.     end
  269.    
  270.  
  271. --Fuel Functions
  272.    
  273. function refuel()
  274.     while turtle.getFuelLevel() < 20 do
  275.         dropAll()
  276.         turtle.select(16)
  277.         placeDown()
  278.  
  279.         if  not turtle.suckDown(1) then
  280.             while not turtle.suckDown(1) do
  281.                 turtle.select(16)
  282.                 print("Please add a source of fuel in the enderchest!")
  283.             end
  284.         end
  285.    
  286.         if not turtle.refuel() then
  287.             while not turtle.refuel() do
  288.                 print("Please, make sure that the items in the enderchest in the 16th slot is filled with a kind of solid fuel!")
  289.                 sleep(10)
  290.             end
  291.         end
  292.    
  293.         turtle.select(16)
  294.         digDown()
  295.         end
  296.     end
  297.    
  298. function fuelTest()
  299.     if turtle.getFuelLevel() < 20 then
  300.         refuel()
  301.     end
  302. end
  303.  
  304. --Positioning functions
  305.  
  306. function headTo(targetX,targetY,targetZ)
  307.  
  308. local lastFacing = facing -- gets which direction the turtle was facing before the function started
  309.  
  310. while (targetX ~= x) or (targetY ~= y) or (targetZ ~= z) do
  311.     local xDistance = (x - targetX)
  312.     local yDistance = (y - targetY)
  313.     local zDistance = (z - targetZ)
  314.  
  315.     if xDistance < 0 then
  316.         face("east")
  317.         forward()
  318.     elseif xDistance > 0 then
  319.         face("west")
  320.         forward()
  321.     end
  322.    
  323.     if yDistance < 0 then
  324.         up()
  325.     elseif yDistance > 0 then
  326.         down()
  327.     end
  328.    
  329.     if zDistance < 0 then
  330.         face("south")
  331.         forward()
  332.     elseif zDistance > 0 then
  333.         face("north")
  334.         forward()
  335.     end
  336. end
  337.    
  338. face(lastFacing) -- makes the turtle face the direction the turtle was facing before the function started
  339.    
  340. end
  341.  
  342. --AI functions
  343.  
  344. function storeCoords()
  345. table.insert(coordSequence, {
  346. x = x,
  347. y = y,
  348. z = z,
  349. })
  350. end
  351.  
  352. function veinMiner()
  353.  
  354.     local baseX = x
  355.     local bazeZ = z
  356.  
  357.     local veinClean = false
  358.  
  359.     while not veinClean  do
  360.    
  361.         if isOre() then
  362.             storeCoords()
  363.             forward()
  364.         elseif isOreUp() then
  365.             storeCoords()
  366.             up()
  367.         elseif isOreDown() then
  368.             storeCoords()
  369.             down()
  370.         elseif isOreLeft() then
  371.             storeCoords()
  372.             left()
  373.         elseif isOreRight() then
  374.             storeCoords()
  375.             right()
  376.         elseif isOreBack() then
  377.             storeCoords()
  378.             back()
  379.         else
  380.             if #coordSequence ~= 0 then
  381.                 headTo(coordSequence[#coordSequence]["x"],coordSequence[#coordSequence]["y"],coordSequence[#coordSequence]["z"])
  382.                 table.remove(coordSequence,#coordSequence)
  383.             else
  384.                 veinClean = true
  385.             end
  386.         end
  387.    
  388.     end
  389.    
  390.     headTo(baseX,mainY,bazeZ)
  391.     face(mainFacing)
  392.  
  393. end
  394.  
  395. --Setup
  396. print("Welcome to Smart Miner alpha 0.3 by KakarotoCm")
  397. sleep(4.2)
  398. print("Please, put an enderchest filled with fuel(Coal or charcoal preferably) in the selected slot")
  399.  
  400. while turtle.getItemCount(16) < 1 do
  401.     turtle.select(16)
  402. end
  403.  
  404. print("Now, please, put an enderchest to receive the items in the selected slot!")
  405.  
  406. while turtle.getItemCount(15) < 1 do
  407.     turtle.select(15)
  408. end
  409.  
  410. print("Now we'll need you to get us some information!")
  411. sleep(3.5)
  412. print("Stand above the turtle, press F3 and please tell us YOUR ")
  413. print("X coordinate(Only non decimal digits): ")
  414. x = read()
  415. print(" ")
  416. print("Y coordinate(Only non decimal digits): ")
  417. y = read()
  418. print(" ")
  419. print("Z coordinate(Only non decimal digits): ")
  420. z = read()
  421. print(" ")
  422. print("OK, now you need to tell us which direction the turtle is facing, to do this look in the same direction its looking and look the tab f: , remember to write everything in lower case!")
  423.  
  424. isFacingReady = false
  425.  
  426. while not isFacingReady do
  427. print("Facing direction(cardinal points): ")
  428. facing = read()
  429.  
  430.     if not (testString(facing,"north") or testString(facing,"south") or testString(facing,"east") or testString(facing,"west")) then
  431.         print("You didn't write a valid answer, the valid answers are north, east, south and west(everything in lower case)")
  432.     else
  433.         isFacingReady = true
  434.     end
  435.  
  436. end
  437.  
  438. isFacingReady = false
  439.  
  440. print("Thank you, we will be running the debug process and will be starting in a while")
  441.  
  442. --Pre-execution debug
  443.  
  444. dropAll()
  445. refuel()
  446.  
  447. mainFacing = facing
  448. mainY = y
  449.  
  450. print("Debug process finished, starting main program!")
  451.  
  452. --Main Program
  453.  
  454. while true do
  455.     veinMiner()
  456.     forward()
  457. end
Advertisement
Add Comment
Please, Sign In to add comment