Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("Loading Miner")
- print("3 Chests required")
- print("Above Turt = fuel")
- print("Below Turt = torch")
- print("Behind turtle = output")
- print("Loading Complete")
- print(" --- ")
- ----------------
- -- GLOBAL VARIABLES
- ----------------
- -- dig/torch variables
- bCount = 14 --how many branches
- bLength = 36 --how long is each branch
- bGap = 3 --gap between each branch
- tLength = 8 --length before leaving a torch (should be equal to (bGap+1)*multiple)
- -- Inventory maintaining limits
- fuelGoal = 2000
- torchGoal = 64
- -- relative direction and position storage (do not edit)
- dir = "straight" --always matches a key from pos
- pos = {straight=0, left=0, right=0} -- current pos
- posMemory = {straight=0, left=0, right=0} -- stored pos when returning to unload
- finishedBranches = {} -- Memory of complete branches (straight index # where branch exists)
- -- First branch exists at index 1. Index 0 is the home base where chests are placed
- ----------------
- -- MOVEMENT LOGIC
- ----------------
- -- Runs when program is loaded
- function startProgram()
- inputCommand = ""
- while (inputCommand ~= "start") do
- -- Error if start command wasn't detected
- if (inputCommand ~= "") then
- print("Unrecognized Command")
- print(" --- ")
- end
- -- Prompt user to start to begin
- print ("Type 'start' to begin")
- inputCommand = read()
- end
- -- Start command was accepted, start the mining loop
- refuelFromStorage()
- retorchFromStorage()
- determineStartType()
- end
- -- Determine the type of start required (digging or resuming previous dig)
- function determineStartType()
- if (posMemory.straight == 0) then
- -- Digger has never advanced, so resume position not required
- startDigging()
- else
- -- Digger has advanced previously so a resume to previous pos is required
- startResuming()
- end
- end
- -- Moves turtle back to the position it was previously in prior to stopping
- function startResuming()
- -- Advance turtle straight until it matches memory
- dir = "straight"
- advanceToMatchMemory(dir)
- if (posMemory.left ~= 0) then
- -- Turle has to head down left passage
- turtle.turnLeft()
- dir = "left"
- advanceToMatchMemory(dir)
- elseif (posMemory.right ~= 0) then
- -- Turtle has to head down right passage
- turtle.turnRight()
- dir = "right"
- advanceToMatchMemory(dir)
- end
- -- By this point, the turtle should have returned to where it was last positioned when it had to stop
- startDigging()
- end
- -- Performs digging logic loop until inventory is full
- function startDigging()
- -- Loop until all branches have been finished
- while (pos.straight < calculateStraightLength() + 1) do
- --Check if full and if so, return home
- if (turtle.getItemCount(16) > 0) then
- break
- end
- -- Perform a dig/move either straight or side
- if (dir == "straight") then
- -- If pointed straight
- -- Check if current pos would require a branch
- if (isBranchCandidate()) then
- -- requires a branch be made, so start it
- turtle.turnLeft()
- dir = "left"
- digForward()
- else
- -- dig forward 1
- digForward()
- end
- else
- -- Was not straight
- -- If branch hasn't reached its full length yet
- if (pos[dir] < bLength) then
- digForward()
- else
- -- branch max length reached
- if (dir == "left") then
- -- return to straight then dig 1 right
- turtle.turnLeft()
- turtle.turnLeft()
- moveUntilPosZero()
- dir = "right"
- digForward()
- else
- -- return to s traight then dig 1 straight
- turtle.turnLeft()
- turtle.turnLeft()
- moveUntilPosZero()
- turtle.turnRight()
- table.insert(finishedBranches, pos.straight)
- dir = "straight"
- digForward()
- end
- end
- end
- -- Place torch in the spot dug into if applicable
- if (isTorchCandidate()) then
- placeTorch()
- end
- end
- returnHome()
- end
- -- Calculates how long the straight shaft will be based on variables
- function calculateStraightLength()
- return ((bCount-1) * bGap) + bCount
- end
- -- Math formula to determine expected total distance of straight shaft
- --((bCount-1) * bGap) + bCount
- -- Math formula to determine expected total width of branches (far left to far right)
- --(bLength * 2) + 1
- -- Return home from
- function returnHome()
- -- Record current position to memory
- storePosToMemory()
- -- Turn around
- turtle.turnLeft()
- turtle.turnLeft()
- -- Return from side passage and turn to face home
- if (dir ~= "straight") then
- moveUntilPosZero()
- -- Turn to face return home direction
- if (dir == "left") then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- dir = "straight"
- end
- -- Return home
- moveUntilPosZero()
- -- If homeBase succeeds then continue, otherwise stop robot
- if (homeBaseProcedure()) then
- determineStartType()
- else
- print("Mining stopped")
- end
- end
- -- Moves drone in the dir until the pos value = 0 (returning towards home)
- function moveUntilPosZero()
- while (pos[dir] > 0) do
- turtle.forward()
- pos[dir] = pos[dir] - 1
- end
- return
- end
- -- Checks if current straight position could be a branch
- function isBranchCandidate()
- -- Modulo check of if a branch would be placed
- -- If bGap is 3, branches exist on straight position 1, 5, 9.
- -- In other words, every 4th straight position after pos.straight = 1
- if (pos.straight % (bGap + 1) == 1) then
- if has_value(finishedBranches, pos.straight) then
- return false --This is a branch row but it was marked done
- else
- return true --A branch row that isn't finished
- end
- else
- return false --Not a branch row
- end
- end
- -- Checks if currne pos is a candiate for a torch
- function isTorchCandidate()
- if (dir == "straight") then
- -- Using modulo of prefered torch length, comparing to a factor based on evenly spacing torches between branches
- if (pos[dir] % (tLength) == math.ceil((bGap+2)/2)) then
- return true -- candiate for straight passage torch
- end
- else
- if (pos[dir] % (tLength) == 4) then
- return true -- candiate for side passage torch
- end
- end
- return false --if not a candiate spot
- end
- -- Utility function that checks if an array contains a value
- function has_value (tab, val)
- for index, value in ipairs(tab) do
- if value == val then
- return true
- end
- end
- return false
- end
- ----------------
- -- INVENTORY MANAGEMENT
- ----------------
- -- Dump all non-torch inventory into the output storage (purge)
- -- Requires a best be present in front of the robot
- function purgeInventory()
- for i=1, 16 do
- turtle.select(i)
- if (getItemName(i) ~= "minecraft:torch") then
- turtle.drop()
- end
- end
- turtle.select(1)
- return
- end
- -- Refuel turtle if fuel is low
- -- Will use all fuel in storage so should only be called after purging inventory
- function refuelFromStorage()
- turtle.select(1)
- while (turtle.getFuelLevel() < fuelGoal) do
- turtle.suckUp()
- for i=1, 16 do
- turtle.select(i)
- if turtle.refuel(0) then
- turtle.refuel(turtle.getItemCount(i))
- end
- end
- end
- return
- end
- -- Pick up torches back to torch goal
- -- Requires room for torches so it is smart to purge inventory first
- function retorchFromStorage()
- turtle.select(1)
- while (getHeldTorchCount() < torchGoal) do
- numNeeded = torchGoal - getHeldTorchCount()
- turtle.suckDown(numNeeded)
- end
- return
- end
- -- Count the total # of torches held by the turtle
- function getHeldTorchCount()
- torchCount = 0
- for i=1, 16 do
- if (getItemName(i) == "minecraft:torch") then
- torchCount = torchCount + turtle.getItemCount(i)
- end
- end
- return torchCount
- end
- -- Drop off inventory and pick up torches/refuel as needed
- -- Assumes robot is facing storage
- -- return true if ready to proceed, false if not
- function homeBaseProcedure()
- -- empty inventory then turn around
- purgeInventory()
- turtle.turnLeft()
- turtle.turnLeft()
- -- end run if main shaft has reached max length (f*** any in progress branches)
- if (posMemory.straight == calculateStraightLength()) then
- return false
- end
- -- confirm if there is any trash on the ground (if so inv was full during purge)
- if (turtle.suck()) then
- return false
- else
- -- refuel/torch and give ready signal
- refuelFromStorage()
- retorchFromStorage()
- turtle.select(1)
- return true
- end
- end
- ----------------
- -- Navigation instruction
- ----------------
- -- Checks if #1 is divisible (modulo or %) of #2
- -- returns true or false
- function divCheck(n1, n2)
- if (n1 % n2 == 0) then
- return true
- else
- return false
- end
- end
- -- Dig forward in the current direction
- function digForward()
- turtle.dig()
- turtle.forward()
- turtle.digUp()
- pos[dir] = pos[dir] + 1
- end
- -- Places a torch assuming 1 is held
- -- return false if fail, true if success
- function placeTorch()
- torchSlot = findItemSlot("minecraft:torch")
- if (torchSlot > 0) then
- turtle.select(torchSlot)
- turtle.turnRight()
- turtle.placeUp()
- turtle.turnLeft()
- return true
- else
- return false
- end
- end
- -- Locate the index of the turtle held item
- -- return 0 if item not found
- function findItemSlot(item)
- -- Loop over every slot and check name of item
- for i=1, 16, 1 do
- slotItem = turtle.getItemDetail(i)
- if (slotItem and slotItem.name == item) then
- return i
- end
- end
- -- None located
- return 0
- end
- -- Get name of item type in slot if any
- -- returns item name or nil. Takes index # of slot 1-16
- function getItemName(slotNumber)
- slotItem = turtle.getItemDetail(slotNumber)
- if (slotItem ) then
- return slotItem.name
- else
- return nil
- end
- end
- -- Advances the turtle until the recorded pos matches the one in memory
- function advanceToMatchMemory(direction)
- while (posMemory[direction] > pos[direction]) do
- turtle.forward()
- pos[direction] = pos[direction] + 1
- end
- end
- -- Copies key/value pairs
- function storePosToMemory()
- for k, v in pairs(pos) do
- posMemory[k] = v
- end
- return
- end
- startProgram()
Add Comment
Please, Sign In to add comment