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

Attempt at making my own quarry, need help with code.

Started by LulzCode, 03 May 2013 - 12:16 PM
LulzCode #1
Posted 03 May 2013 - 02:16 PM
Title:Attempt at making my own quarry, need help with code.

I believe that the goTo() function or the goToChest() function is absolutely messed up, but I can't figure out where. The turtle goes absolutely berserk when trying to go back to the chest. Anyone willing to do a new member a favor and search through my code?
Here are the main functions:

function setDirection(a)
  if a > 3 or a < 0 then
    msg("setDirection error 1.")
    error()
  end
  if direction ~= a then
    while true do
	  left()
	  if direction == a then break end
    end
  end
end
function goTo(a, b, c)
  local oppX = a < x
  local oppY = b < y
  local oppZ = c < z
  if oppX then
    setDirection(2)
    for i=a,x do forward() end
  else
    setDirection(0)
    for i=x,a do forward() end
  end
 
  if oppY then
    for j=b,y do down() end
  else
    for j=y,b do up() end
  end
 
  if oppZ then
    setDirection(3)
    for k=c,z do forward() end
  else
    setDirection(1)
    for k=z,c do forward() end
  end
 
  msg("Went to position X" .. x .. ", Y" .. y .. ", Z" .. z) 
end
function doChest()
  mineX = x
  mineY = y
  mineZ = z
  goTo(0,0,0)
  setDirection(2)
  turtle.select(2)
  while true do
    if turtle.compare() then break end
    up()
  end 
  for i=3,16 do
    turtle.select(i)
    local continue = true
    if turtle.getItemCount(i) == 0 then continue = false end
    if continue then
	  while true do
	    if turtle.drop(turtle.getItemCount(i)) then break end
	    up()
	    turtle.select(2)
	    if not turtle.compare() then
		  setChest()
	    end
	  end
    end
  end
  msg("Empied to chest. Going back.")
  goTo(mineX, mineY, mineZ)
  msg("Back to mining spot.")
end
function start()
  testFuel()
  setChest()
  msg("Starting at X" .. x .. ", Y" .. y .. ", Z" .. z)
 
  right()
  forward()
  forward()
  doChest()
 
  starting = false
end

And here's the entire code, most likely not needed:

-- Must have modem
-- Fuel in slot 1
-- Chests in slot 2
-- All the area above the turtle must
-- be empty space

-- Variables that can be changed
local startY = 70
local radius = 10
-- End of variables to change
rednet.open("right")
rednet.broadcast("Starting turtle!")
local x = 0
local y = 0
local z = 0
local mineX = 0
local mineY = 0
local mineZ = 0
local direction = 0
local starting = true
function msg(str)
  print(str)
  rednet.broadcast(str)
end
function testFuel()
  if turtle.getFuelLevel() < 5 then
	turtle.select(1)
	msg("Refueling turtle.")
	local count = turtle.getItemCount(1)
	if count == 0 then
	  msg("Fuel is 100% out. Stopping turtle.")
	  error()
	elseif count == 1 then msg ("Fuel is really close to out. :(/>/>/>")
	elseif count < 5 then msg("Fuel level at " .. count .. ".") end
	if count ~= 0 then turtle.refuel(1) end
  end
end
function left()
  if turtle.turnLeft() then
	direction = direction - 1
	if direction < 0 then direction = 3 end
  else
	msg("Could not turn left???")
  end
end
function right()
  if turtle.turnRight() then
	direction = direction + 1
	if direction > 3 then direction = 0 end
  else
	msg("Could not turn right???")
  end
end
function forward()
  testFuel()
  if turtle.detect() then turtle.dig() end
  if turtle.forward() then
	if direction == 0 then x = x + 1
	elseif direction == 1 then z = z + 1
	elseif direction == 2 then x = x - 1
	elseif direction == 3 then z = z - 1
	else msg("Directional error.")
	end
  else
	msg("Could not go forward???")
  end
end
function up()
  testFuel()
  if turtle.detectUp() then turtle.digUp() end
  if turtle.up() then y = y + 1 end
end
function down()
  testFuel()
  if turtle.detectDown() then turtle.digDown() end
  if turtle.down() then y = y - 1 end
end
function setChest()
  turtle.select(2)
  local count = turtle.getItemCount(2)
  if count < 2 then
	msg("Not enough chests in Slot 2. Stopping.")
	error()
  end
  while true do
	if turtle.detect() then up()
	else break end
  end
  turtle.place()
  right()
  forward()
  left()
  if turtle.detect() then turtle.dig() end
  turtle.place()
  left()
  forward()
  right()
  for i=1,y do
	down()
  end
  msg("Placed a double chest.")
end
function setDirection(a)
  if a > 3 or a < 0 then
	msg("setDirection error 1.")
	error()
  end
  if direction ~= a then
	while true do
	  left()
	  if direction == a then break end
	end
  end
end
function goTo(a, b, c)
  local oppX = a < x
  local oppY = b < y
  local oppZ = c < z
  if oppX then
	setDirection(2)
	for i=a,x do forward() end
  else
	setDirection(0)
	for i=x,a do forward() end
  end

  if oppY then
	for j=b,y do down() end
  else
	for j=y,b do up() end
  end

  if oppZ then
	setDirection(3)
	for k=c,z do forward() end
  else
	setDirection(1)
	for k=z,c do forward() end
  end

  msg("Went to position X" .. x .. ", Y" .. y .. ", Z" .. z)
end
function doChest()
  mineX = x
  mineY = y
  mineZ = z
  goTo(0,0,0)
  setDirection(2)
  turtle.select(2)
  while true do
	if turtle.compare() then break end
	up()
  end
  for i=3,16 do
	turtle.select(i)
	local continue = true
	if turtle.getItemCount(i) == 0 then continue = false end
	if continue then
	  while true do
		if turtle.drop(turtle.getItemCount(i)) then break end
		up()
		turtle.select(2)
		if not turtle.compare() then
		  setChest()
		end
	  end
	end
  end
  msg("Empied to chest. Going back.")
  goTo(mineX, mineY, mineZ)
  msg("Back to mining spot.")
end
function start()
  testFuel()
  setChest()
  msg("Starting at X" .. x .. ", Y" .. y .. ", Z" .. z)

  right()
  forward()
  forward()
  doChest()

  starting = false
end
start()
Lyqyd #2
Posted 16 July 2013 - 01:38 PM
It may be beneficial to review the built-in excavate program and compare what it does to what you're doing. You may find some answers there.