MrStump

Miner Turtle by MrStump (test)

Nov 7th, 2021 (edited)
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.20 KB | None | 0 0
  1. print("Loading Miner")
  2. print("3 Chests required")
  3. print("Above Turt = fuel")
  4. print("Below Turt = torch")
  5. print("Behind turtle = output")
  6. print("Loading Complete")
  7. print(" --- ")
  8. ----------------
  9. -- GLOBAL VARIABLES
  10. ----------------
  11. -- dig/torch variables
  12. bCount = 14 --how many branches
  13. bLength = 36 --how long is each branch
  14. bGap = 3 --gap between each branch
  15. tLength = 8 --length before leaving a torch (should be equal to (bGap+1)*multiple)
  16. -- Inventory maintaining limits
  17. fuelGoal = 2000
  18. torchGoal = 64
  19. -- relative direction and position storage (do not edit)
  20. dir = "straight" --always matches a key from pos
  21. pos =       {straight=0, left=0, right=0} -- current pos
  22. posMemory = {straight=0, left=0, right=0} -- stored pos when returning to unload
  23. finishedBranches = {} -- Memory of complete branches (straight index # where branch exists)
  24. -- First branch exists at index 1. Index 0 is the home base where chests are placed
  25.  
  26.  
  27.  
  28. ----------------
  29. -- MOVEMENT LOGIC
  30. ----------------
  31.  
  32. --  Runs  when program is loaded
  33. function startProgram()
  34.     inputCommand = ""
  35.     while (inputCommand ~= "start") do
  36.         -- Error if start command wasn't detected
  37.         if (inputCommand ~= "") then
  38.             print("Unrecognized Command")
  39.             print(" --- ")
  40.         end
  41.         -- Prompt user to start to begin
  42.         print ("Type 'start' to begin")
  43.         inputCommand = read()
  44.     end
  45.     -- Start command was accepted, start the mining loop
  46.     refuelFromStorage()
  47.     retorchFromStorage()
  48.     determineStartType()
  49. end
  50.  
  51.  
  52.  
  53. -- Determine the type of start required (digging or resuming previous dig)
  54. function determineStartType()
  55.     if (posMemory.straight == 0) then
  56.         -- Digger has never advanced, so resume position not required
  57.         startDigging()
  58.     else
  59.         -- Digger has advanced previously so a resume to previous pos is required
  60.         startResuming()
  61.     end
  62. end
  63.  
  64. -- Moves turtle back to the position it was previously in prior to stopping
  65. function startResuming()
  66.     -- Advance turtle straight until it matches memory
  67.     dir = "straight"
  68.     advanceToMatchMemory(dir)
  69.  
  70.     if (posMemory.left ~= 0) then
  71.         -- Turle has to head down left passage
  72.         turtle.turnLeft()
  73.         dir = "left"
  74.         advanceToMatchMemory(dir)
  75.     elseif (posMemory.right ~= 0) then
  76.         -- Turtle has to head down right passage
  77.         turtle.turnRight()
  78.         dir = "right"
  79.         advanceToMatchMemory(dir)
  80.     end
  81.    
  82.     -- By this point, the turtle should have returned to where it was last positioned when it had to stop
  83.     startDigging()
  84. end
  85.  
  86. -- Performs digging logic loop until inventory is full
  87. function startDigging()
  88.     -- Loop until all branches have been finished
  89.     while (pos.straight < calculateStraightLength() + 1) do
  90.         --Check if full and if so, return home
  91.         if (turtle.getItemCount(16) > 0) then
  92.             break
  93.         end
  94.  
  95.         -- Perform a dig/move either straight or side
  96.         if (dir == "straight") then
  97.             -- If pointed straight
  98.  
  99.             -- Check if current pos would require a branch
  100.             if (isBranchCandidate()) then
  101.                 -- requires a branch be made, so start it
  102.                 turtle.turnLeft()
  103.                 dir = "left"
  104.                 digForward()
  105.             else
  106.                 -- dig forward 1
  107.                 digForward()
  108.             end
  109.         else
  110.             -- Was not straight
  111.  
  112.             -- If branch hasn't reached its full length yet
  113.             if (pos[dir] < bLength) then
  114.                 digForward()
  115.             else
  116.                 -- branch max length reached
  117.                
  118.                 if (dir == "left") then
  119.                     -- return to straight then dig 1 right
  120.                     turtle.turnLeft()
  121.                     turtle.turnLeft()
  122.                     moveUntilPosZero()
  123.                     dir = "right"
  124.                     digForward()
  125.                 else
  126.                     -- return to s traight then dig 1 straight
  127.                     turtle.turnLeft()
  128.                     turtle.turnLeft()
  129.                     moveUntilPosZero()
  130.                     turtle.turnRight()
  131.                     table.insert(finishedBranches, pos.straight)
  132.                     dir = "straight"
  133.                     digForward()
  134.                 end
  135.             end
  136.         end
  137.         -- Place torch in the spot dug into if applicable
  138.         if (isTorchCandidate()) then
  139.             placeTorch()
  140.         end
  141.     end
  142.  
  143.     returnHome()
  144. end
  145.  
  146. -- Calculates how long the straight shaft will be based on variables
  147. function calculateStraightLength()
  148.     return ((bCount-1) * bGap) + bCount
  149. end
  150.  
  151. -- Math formula to determine expected total distance of straight shaft
  152. --((bCount-1) * bGap) + bCount
  153. -- Math formula to determine expected total width of branches (far left to far right)
  154. --(bLength * 2) + 1
  155.  
  156. -- Return home from
  157. function returnHome()
  158.     -- Record current position to memory
  159.     storePosToMemory()
  160.     -- Turn around
  161.     turtle.turnLeft()
  162.     turtle.turnLeft()
  163.     -- Return from side passage and turn to face home
  164.     if (dir ~= "straight") then
  165.         moveUntilPosZero()
  166.         -- Turn to face return home direction
  167.         if (dir == "left") then
  168.             turtle.turnRight()
  169.         else
  170.             turtle.turnLeft()
  171.         end
  172.         dir = "straight"
  173.     end
  174.     -- Return home
  175.     moveUntilPosZero()
  176.  
  177.     -- If homeBase succeeds then continue, otherwise stop robot
  178.     if (homeBaseProcedure()) then
  179.         determineStartType()
  180.     else
  181.         print("Mining stopped")
  182.     end
  183. end
  184.  
  185. -- Moves drone in the dir until the pos value = 0 (returning towards home)
  186. function moveUntilPosZero()
  187.     while (pos[dir] > 0) do
  188.         turtle.forward()
  189.         pos[dir] = pos[dir] - 1
  190.     end
  191.     return
  192. end
  193.  
  194.  
  195.  
  196. -- Checks if current straight position could be a branch
  197. function isBranchCandidate()
  198.     -- Modulo check of if a branch would be placed
  199.     -- If bGap is 3, branches exist on straight position 1, 5, 9.
  200.     -- In other words, every 4th straight position after pos.straight = 1
  201.     if (pos.straight % (bGap + 1) == 1) then
  202.         if has_value(finishedBranches, pos.straight) then
  203.             return false --This is a branch row but it was marked done
  204.         else
  205.             return true --A branch row that isn't finished
  206.         end
  207.     else
  208.         return false --Not a branch row
  209.     end
  210. end
  211.  
  212. -- Checks if currne pos is a candiate for a torch
  213. function isTorchCandidate()
  214.     if (dir == "straight") then
  215.         -- Using modulo of prefered torch length, comparing to a factor based on evenly spacing torches between branches
  216.         if (pos[dir] % (tLength) == math.ceil((bGap+2)/2)) then
  217.             return true -- candiate for straight passage torch
  218.         end
  219.     else
  220.         if (pos[dir] % (tLength) == 4) then
  221.             return true -- candiate for side passage torch
  222.         end
  223.     end
  224.     return false --if not a candiate spot
  225. end
  226.  
  227. -- Utility function that checks if an array contains a value
  228. function has_value (tab, val)
  229.     for index, value in ipairs(tab) do
  230.         if value == val then
  231.             return true
  232.         end
  233.     end
  234.  
  235.     return false
  236. end
  237.  
  238.  
  239.  
  240.  
  241. ----------------
  242. -- INVENTORY MANAGEMENT
  243. ----------------
  244.  
  245. -- Dump all non-torch inventory into the output storage (purge)
  246. -- Requires a best be present in front of the robot
  247. function purgeInventory()
  248.     for i=1, 16 do
  249.         turtle.select(i)
  250.         if (getItemName(i) ~= "minecraft:torch") then
  251.             turtle.drop()
  252.         end
  253.     end
  254.     turtle.select(1)
  255.     return
  256. end
  257.  
  258.  
  259. -- Refuel turtle if fuel is low
  260. -- Will use all fuel in storage so should only be called after purging inventory
  261. function refuelFromStorage()
  262.     turtle.select(1)
  263.     while (turtle.getFuelLevel() < fuelGoal) do
  264.         turtle.suckUp()
  265.         for i=1, 16 do
  266.             turtle.select(i)
  267.             if turtle.refuel(0) then
  268.                 turtle.refuel(turtle.getItemCount(i))
  269.             end
  270.         end
  271.     end
  272.     return
  273. end
  274.  
  275. -- Pick up torches back to torch goal
  276. -- Requires room for torches so it is smart to purge inventory first
  277. function retorchFromStorage()
  278.     turtle.select(1)
  279.     while (getHeldTorchCount() < torchGoal) do
  280.         numNeeded = torchGoal - getHeldTorchCount()
  281.         turtle.suckDown(numNeeded)
  282.     end
  283.     return
  284. end
  285.  
  286. -- Count the total # of torches held by the turtle
  287. function getHeldTorchCount()
  288.     torchCount = 0
  289.     for i=1, 16 do
  290.         if (getItemName(i) == "minecraft:torch") then
  291.             torchCount = torchCount + turtle.getItemCount(i)
  292.         end
  293.     end
  294.     return torchCount
  295. end
  296.  
  297. -- Drop off inventory and pick up torches/refuel as needed
  298. -- Assumes robot is facing storage
  299. -- return true if ready to proceed, false if not
  300. function homeBaseProcedure()
  301.     -- empty inventory then turn around
  302.     purgeInventory()
  303.     turtle.turnLeft()
  304.     turtle.turnLeft()
  305.     -- end run if main shaft has reached max length (f*** any in progress branches)
  306.     if (posMemory.straight == calculateStraightLength()) then
  307.         return false
  308.     end
  309.     -- confirm if there is any trash on the ground (if so inv was full during purge)
  310.     if (turtle.suck()) then
  311.         return false
  312.     else
  313.         -- refuel/torch and give ready signal
  314.         refuelFromStorage()
  315.         retorchFromStorage()
  316.         turtle.select(1)
  317.         return true
  318.     end
  319. end
  320.  
  321.  
  322. ----------------
  323. -- Navigation instruction
  324. ----------------
  325. -- Checks if #1 is divisible (modulo or %) of #2
  326. -- returns true or false
  327. function divCheck(n1, n2)
  328.     if (n1 % n2 == 0) then
  329.         return true
  330.     else
  331.         return false
  332.     end
  333. end
  334.  
  335. -- Dig forward in the current direction
  336. function digForward()
  337.     turtle.dig()
  338.     turtle.forward()
  339.     turtle.digUp()
  340.     pos[dir] = pos[dir] + 1
  341. end
  342.  
  343.  
  344. -- Places a torch assuming 1 is held
  345. -- return false if fail, true if success
  346. function placeTorch()
  347.     torchSlot = findItemSlot("minecraft:torch")
  348.     if (torchSlot > 0) then
  349.         turtle.select(torchSlot)
  350.         turtle.turnRight()
  351.         turtle.placeUp()
  352.         turtle.turnLeft()
  353.         return true
  354.     else
  355.         return false
  356.     end
  357. end
  358.  
  359. -- Locate the index of the turtle held item
  360. -- return 0 if item not found
  361. function findItemSlot(item)
  362.     -- Loop over every slot and check name of item
  363.     for i=1, 16, 1 do
  364.         slotItem = turtle.getItemDetail(i)
  365.         if (slotItem and slotItem.name == item) then
  366.             return i
  367.         end
  368.     end
  369.     -- None located
  370.     return 0
  371. end
  372.  
  373. -- Get name of item type in slot if any
  374. -- returns item name or nil. Takes index # of slot 1-16
  375. function getItemName(slotNumber)
  376.     slotItem = turtle.getItemDetail(slotNumber)
  377.     if (slotItem ) then
  378.         return slotItem.name
  379.     else
  380.         return nil
  381.     end
  382. end
  383.  
  384. -- Advances the turtle until the recorded pos matches the one in memory
  385. function advanceToMatchMemory(direction)
  386.     while (posMemory[direction] > pos[direction]) do
  387.         turtle.forward()
  388.         pos[direction] = pos[direction] + 1
  389.     end
  390. end
  391.  
  392. -- Copies key/value pairs
  393. function storePosToMemory()
  394.     for k, v in pairs(pos) do
  395.         posMemory[k] = v
  396.     end
  397.     return
  398. end
  399.  
  400. startProgram()
  401.  
Add Comment
Please, Sign In to add comment