Posted 08 September 2012 - 03:03 AM
So, I've just started with ComputerCraft and LUA, but I come from a programming background so it hasn't been too hard to pick up. Until now.
As a part of learning, I'm trying to write a simple program for a mining turtle so that, starting from a chest, it will dig a shaft N by M down. When it runs low on fuel or fills it's inventory up, it will return to the chest and dump everything before refuelling. I'm doing my path-finding with vectors. The start location is vector <0,0,0>, the mineshaft starts at vector <2,0,0> (2 blocks away from the chest), and every time I move I increase the vector appropriately.
The plan was, when the turtle does need to return to the chest, it can record it's current location as a vector, navigate to <0,0,0> to drop everything, and then use the previously recorded position to return to where it was last.
The problem is that reading the vector in, that I thought I'd stored, always returns <0,0,0> no matter what, so the turtle starts from where it's at and destroys the chest it just dumped everything into.
I think it's a scoping issue, but despite researching it for the last couple of hours, I haven't figured it out.
The relevant code is as follows:
As a part of learning, I'm trying to write a simple program for a mining turtle so that, starting from a chest, it will dig a shaft N by M down. When it runs low on fuel or fills it's inventory up, it will return to the chest and dump everything before refuelling. I'm doing my path-finding with vectors. The start location is vector <0,0,0>, the mineshaft starts at vector <2,0,0> (2 blocks away from the chest), and every time I move I increase the vector appropriately.
The plan was, when the turtle does need to return to the chest, it can record it's current location as a vector, navigate to <0,0,0> to drop everything, and then use the previously recorded position to return to where it was last.
The problem is that reading the vector in, that I thought I'd stored, always returns <0,0,0> no matter what, so the turtle starts from where it's at and destroys the chest it just dumped everything into.
I think it's a scoping issue, but despite researching it for the last couple of hours, I haven't figured it out.
The relevant code is as follows:
Spoiler
local mineLocation = vector.new(2,0,0)
local location = vector.new(0,0,0)
-- miners start pointing backwards towards their chest.
-- '0' indicates positive x, '1' positive y, '2' negative x, '3' negative y
-- a right turn adds 1 to direction, a left turn subtracts
-- every time direction is modified, it is also normalized to remain between 0 and 3 inclusive
local direction = 2
local toMine = function()
print("Going to Mine")
print("Retreiving Mine Location")
-- THIS ALWAYS PRINTS (0,0,0) --
print("(" .. mineLocation.x .. "," .. mineLocation.y .. "," .. mineLocation.z .. ")")
print("")
tmpVec = mineLocation - location
-- if positive x
if tmpVec.x > 0 then
-- face positive x
while direction ~= 0 do
right()
end
-- move in that direction
for i=1,tmpVec.x do
forward()
end
end
-- if negative x
if tmpVec.x < 0 then
-- face negative x
while direction ~= 2 do
right()
end
-- move in that direction
for i=tmpVec.x,-1 do
forward()
end
end
-- if positive y
if tmpVec.y > 0 then
-- face positive y
while direction ~= 1 do
right()
end
-- move in that direction
for i=1,tmpVec.y do
forward()
end
end
--if negative y
if tmpVec.y < 0 then
-- face negative y
while direction ~= 3 do
right()
end
-- move in that direction
for i=tmpVec.y,-1 do
forward()
end
end
-- if positive z
if tmpVec.z > 0 then
for i=1,tmpVec.z do
down()
end
end
-- if negative z
if tmpVec.z < 0 then
for i=tmpVec.z,-1 do
up()
end
end
end
while true do
if refuel() then
toMine(mineLocation) -- THIS WORKS FINE AND MOVES TURTLE TO <2,0,0>
while (turtle.getFuelLevel() > 1600) do
for j=1,tArgs[2] do -- ROWS
for i=2,tArgs[1] do -- COLUMNS (-1 from column length as you need to move one less space than you need to dig)
if turtle.detectDown() then
turtle.digDown()
end
forward()
end
turtle.digDown()
if turtle.getItemCount(15) ~= 0 then -- If all slots now have something in them we're pretty full. Might as well empty the inventory.
mineLocation = location -- THIS IS WHERE I THINK EVERYTHING IS GOING WRONG --
print("Recording Mine Location")
-- THIS PRINTS THE RIGHT VALUES --
print("(" .. mineLocation.x .. "," .. mineLocation.y .. "," .. mineLocation.z .. ")")
print("")
returnToStart() -- PRACTICALLY THE SAME ALGORITHM AS toMine() BUT DOESN'T TOUCH mineLocation AT ALL --
refuel()
toMine()
end
end
returnToCorner()
down()
end
else
print("Ran out of fuel")
sleep(120)
end
end