This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Urlentine's profile picture

Turtle Branch Mining Program

Started by Urlentine, 10 June 2014 - 09:51 PM
Urlentine #1
Posted 10 June 2014 - 11:51 PM
Hi,

The purpose of the program is to let the turtle go of and do its branch mining, which works fine, and when its inventory gets full for it to return to a chest and dump its items before returning to its original position and to continue to mine,

So I think that I have to some how have 2 functions running as closely as possible side by side one that is controlling the mining and the other that is constantly checking to see whether the turtle is full or not.

The Code when run with the Check Inventory stuff being done in sequence works just fine, Its just trying to get the program to work with coroutines I also tried to get it to work the with parallel api but to no avail

Here is my code for you to have a look at


-- This is Where all the Local Variables are Initialized --
local arg = {...}
local tunnelDistance = tonumber(arg[1])
local digDistance = tonumber(arg[2])
local distanceToChestRestart = tonumber(arg[3])
local distanceTraveled = 0
local inventoryFull = false
local distanceToChestCurrent = 0 + distanceToChestRestart
local FirstItemSlot = 1
local LastIemSlot = 16
local distanceSideTunnelTravelled = 0
local LeftOrRight = "Left"
-- This Function Controls all the Branch Mining --
function dig(DistanceToTravel)

-- This For Loop will dig a 2 High Branch the leg

for i=1,DistanceToTravel,1 do
  if (turtle.detect()) then
   turtle.dig()
  end

  turtle.forward()

  if(turtle.detectUp()) then
   turtle.digUp()
  end

  distanceSideTunnelTravelled = distanceSideTunnelTravelled + 1

end

-- This Resets the Turtle back to the middle --

for i=1,2,1 do
  turtle.turnLeft()
end
for i=1,digDistance,1 do
  turtle.forward()
end
end
-- This is Checking the Inventory of the Turtle to see when it gets full --
function CheckInventory ()
while true do
  inventoryFull = false
  for i=FirstItemSlot,LastIemSlot,1 do
   if (turtle.getItemCount(i)~=0) then
	inventoryFull = true
   else
   inventoryFull = false
   print("yeah")
   end
  end
  print(inventoryFull)
  if (inventoryFull == true) then
   return
   print("hmm")
  end
end
end
-- This is the Function the When the Turtle is full moves it back to the Chest --
function EmptyInventory()
if (inventoryFull == true) then
  -- This IF statement checks to see if the turtle has started to Branch Mine --
  if (distanceSideTunnelTravelled >0) then
   for i=1,distanceSideTunnelTravelled +1,1 do
	turtle.back()
   end

   if (LeftOrRight == "Left") then
	turtle.turnRight()
   elseif (LeftOrRight == "Right") then
	turtle.turnLeft()
   end
  end
  -- This Just Movest The Turtle Back Along the Main Tunnel --

  for i=1,distanceToChestCurrent,1 do
   turtle.back()
  end

  -- This Emptys The Turtle Into the Chest --

  for i=FirstItemSlot,LastIemSlot,1 do
   turtle.select(i)
   turtle.dropDown()
  end

  -- This Moves The Turtle to its original Position --

  for i=1,distanceToChestCurrent,1 do
   turtle.forward()
  end

  -- This Moves the turtle back down its Branch --

  if(distanceSideTunnelTravelled>0) then
  
   if (LeftOrRight == "Left") then
	turtle.turnLeft()
  
   elseif (LeftOrRight == "Right") then
  
	turtle.turnRight()
   end
  
   for i=1,distanceSideTunnelTravelled+1,1 do
  
	turtle.forward()
  
   end
  end
end
end
-- This Function Controls all the Mining That the Turtle does --
function MiningLoop()
while (distanceTraveled<tunnelDistance) do
  --print("Into the Loop")
  shell.run("tunnel", "4")
  turtle.turnLeft()

  LeftOrRight = "Left"
  dig(digDistance)
  LeftOrRight = "Right"
  distanceSideTunnelTravelled = 0
  dig(digDistance)

  turtle.turnRight()
  distanceTraveled = distanceTraveled + 1
  distanceToChestCurrent=distanceToChestCurrent+1
end
end
function MainControl()
local CheckInv = coroutine.create(CheckInventory)
local Mine = coroutine.create(MiningLoop)
while true do
  coroutine.resume(CheckInv)
  coroutine.resume(Mine)
  if (coroutine.status(CheckInv) == "dead") then
   break
  end

  if (coroutine.status(Mine) == "dead") then
   break
  end
  print("We are Off")

end
  if (inventoryFull == true) then
   print("Inventory Full")
   recoveryDistance = digDistance - distanceSideTunnelTravelled
   EmptyInventory()
   dig(recoveryDistance)
   inventoryFull = false
  end
end
MainControl()

Any help would be greatly appreciated

Urlentine
Bomb Bloke #2
Posted 11 June 2014 - 12:28 AM
Well, sorta what you want to do is take this:

local CheckInv = coroutine.create(CheckInventory)
local Mine = coroutine.create(MiningLoop)
while true do
  coroutine.resume(CheckInv)
  coroutine.resume(Mine)
  if (coroutine.status(CheckInv) == "dead") then
   break
  end

  if (coroutine.status(Mine) == "dead") then
   break
  end
  print("We are Off")

end

… and replace it with this:

parallel.waitForAny(CheckInventory,MiningLoop)

However… I can't say I see any need for a co-routine here. Why not just have your dig function call the inventory checker itself, once for every block it mines out? Any more checks than that is a waste of processing power.
Urlentine #3
Posted 11 June 2014 - 12:48 AM
Thanks for the quick reply.

Guess I tried to make it to complex then. How ever for arguments sake, I tried to get the parallel api to work with no avail, any suggestions on how one would get it to work so that I could use it in the future