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

Cant Find The Bug! (turtle Miner)

Started by Kees987, 24 October 2013 - 12:35 PM
Kees987 #1
Posted 24 October 2013 - 02:35 PM
What it does:
You give depth and width, and it will miner 3 high layer (above, same and below turtle lvl)
When the turtle is full, it checks if it has cobble and dirt, and tosses that away.
The remaining stuff he puts in an enderchest.

The bug:
When running this script, somehow the turtle gets confused in the cobble/dirt drop check.
I cant wrap my finger round it yet.
He forgets to place the chest, or he is confused and forgets to place it or something, but he starts to throw away all the good stuff too.

Can anyone help me out here, i just cant find what is wrong

Spoiler

-- made by 1869_Flame
local EnderchestSlot = 1
local ignoreCobbleSlot = 2
local ignoreDirtSlot = 3
local FuelSlot = 16
local function clear()
		term.clear()
		term.setCursorPos(1,1)
end
local function DigForward()
while turtle.detect() do
  turtle.dig()
end
end
local function DigDown()
while turtle.detectDown() do
  turtle.digDown()
end
end
local function DigUp()
while turtle.detectUp() do
  turtle.digUp()
end
end
local function CobbleDirt()
DigDown()
for i=1, 16 do
  turtle.select(i)
  if i <= 3 or i == 16 then
  -- we do nothing, as this is he ender and ignore slots
  else
   if turtle.compareTo(2) then
	if i ~= FuelSlot and i ~= EnderchestSlot and i ~= ignoreCobbleSlot and i ~= ignoreDirtSlot then
	 turtle.dropDown()
	end
   end
   if turtle.compareTo(3) then
	if i ~= FuelSlot and i ~= EnderchestSlot and i ~= ignoreCobbleSlot and i ~= ignoreDirtSlot then
	 turtle.dropDown()
	end
   end
  end
end
end
local function CheckEmptySlots()
	for i=1, 16 do
		if turtle.getItemCount(i) < 1 then
			return true
   else
		end
	end
CobbleDirt()
	return false
end
local function storeStuff()
if not CheckEmptySlots() then
  if EnderchestSlot >= 1 and EnderchestSlot <= 16 then
   turtle.turnLeft()
   turtle.turnLeft()
   sleep(0.8)
   for i = 1,5 do
	DigForward()
   end
   turtle.select(EnderchestSlot)
   turtle.place()
   sleep(0.1)
   for i = 1, 16 do
	turtle.select(i)
	if i ~= FuelSlot and i ~= EnderchestSlot and i ~= ignoreCobbleSlot and i ~= ignoreDirtSlot then
	 turtle.drop()
	end
   end
   sleep(0.1)  
   turtle.select(EnderchestSlot)
   turtle.dropDown()
   sleep(0.1)
   turtle.dig()
   turtle.select(1)
   turtle.turnLeft()
   turtle.turnLeft()
  end
end
end
local function CheckForRefuel()
if turtle.getFuelLevel() < 500 then
  oldFuel = turtle.getFuelLevel()
  turtle.select(FuelSlot)
  turtle.refuel(1)
  newFuel = turtle.getFuelLevel()
  if oldFuel >= newFuel then
   count = 1
   turtle.select(count)
   while not turtle.refuel() do
	count = count + 1
	turtle.select(count)
	if count >= 16 then
	 count = 0
	end
   end
  end
end
end
function slotsEmpty()
	for i=1, 16 do
		if turtle.getItemCount(i) < 1 then
			return true
		end
	end
	return false
end

local function clearDepthLine()
		for i = 1, depth do
   while not turtle.forward() do
	DigForward()
	DigUp()
	DigDown()
   end
   storeStuff()
   CheckForRefuel()
   DigUp()
   DigDown()
		end
end
local function clearWidthLine()
		for i = 1, width do
   while not turtle.forward() do
	DigForward()
	DigUp()
	DigDown()
	storeStuff()
   end
   CheckForRefuel()
   DigUp()
   DigDown()
		end
end

function Mine()
turns = 0
if depth >= width then
  turns = width
end

if width >= depth then
  turns = depth
end

clearDepthLine()
turtle.turnLeft()

clearWidthLine()
turtle.turnLeft()

clearDepthLine()
turtle.turnLeft()

while turns >=0 do
  turns = turns -1

  width = width - 1
  clearWidthLine()
  turtle.turnLeft()
  depth = depth - 1
  clearDepthLine()
  turtle.turnLeft()
end
end
--  Start  --
clear()
print("a Minerbot by 1869_Flame")
print("")
print("Please put an enderchest in slot 1")
print("Please put fuel in slot 16")
term.write("Insert depth: ")
depth = tonumber(read())
print(" ")

term.write("Insert width: ")
width = tonumber(read())
print(" ")

CheckForRefuel()
Mine()

print("im done!")

EDIT:
pastebin here: http://pastebin.com/mv77D8N4
Bomb Bloke #2
Posted 25 October 2013 - 08:51 AM
The whole CobbleDirt() function could be reduced to:

local function CobbleDirt()
        DigDown()
        for i=2,3 do
                turtle.select(i)
                turtle.dropDown(turtle.getItemCount(i)-1)
        for i=4, 15 do
                turtle.select(i)
                if turtle.compareTo(2) or turtle.compareTo(3) then turtle.dropDown() end
        end
end

This is assuming you don't ever change your "specially assigned" slots, though your current version of that function will also break were you to try that. Either way, at least making use of "or" is worthwhile.

Dropping most of the cobble/stone in the "ignore" slots'd allow more digging in between loot dropoffs. Likewise, if you had
CheckEmptySlots() call CobbleDirt() before checking to see if any slots are free, it'd not have to place the enderchest as often.

I'd set line 97 to "turtle.refuel(turtle.getItemCount(FuelSlot)-1)". May as well not be stingy with it. Assuming you're using coal, you may also want to try to top up that slot prior to dumping into your enderchest - no point in throwing out fuel you might soon have a use for.

"if turtle.getItemCount(i) < 1 then" is the same as "if turtle.getItemCount(i) == 0 then".

I can't see why the turtle would throw "good" stuff away without placing the chest - unless I'm missing something, you're taking more precautions than required. Does it always do it? I'm guessing that something unusual is preventing it from placing the chest, but I dunno what (you put it in the right slot, yes? I'm sorta thinking a plugin like PermissionsEx); I'd recommend expanding out line 73 a bit and see what you learn from it:

                        if not turtle.place() then error("I couldn't place the enderchest!") end

The script will stop if the turtle tries to place the chest yet fails, and you'll specifically know that it's trying yet can't.
Kees987 #3
Posted 27 October 2013 - 08:26 AM
thanx. ill improve the script based on this.