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

[Lua][Question][Turtle]return to a starting point after running around w/o GPS

Started by jakylorhide, 18 April 2013 - 11:14 PM
jakylorhide #1
Posted 19 April 2013 - 01:14 AM
Hi, so I've been messing around with CC in Tekkitlite and I started working on a "Smart" Miner, the general Idea is to dig a rabbit hole down to near bedrock, then make a 2x1 tunnel as it looks for ore. when it finds it, it runs some sub-routines to hollow out the vein.
The issue I'm having is it doesn't want to return to the main shaft after it finishes with the vein. any ideas or help would be apreciated. (and I know that this isn't the most efficient way to do it. I just thought the idea of a quasi-pathfinding / ore-hunting turtle was cool)
pastebin: MRgMfe3n
Spoiler


--This section sets the variables. WARNING, THESE ARE GLOBAL VARIABLES!!!!--
store, fuel, torches, sand, gravel, dirt, stone = 1, 2, 3, 4, 5, 6, 7
ore = false
--x is "left/right" w/ left = +
--y is "forward/back" w/ forward = +
--z is "up/down" w/ up = +
front, left, rear, right = 0,1,2,3
x, y, z, o = 0,0,0,0 --Current Co-ordinates
xo,yo,zo,oo = 0,0,0,0 --Origin Point, where the turtle starts the program
xb,yb,zb,ob = 0,0,0,0 --Saved Co-ordinates, where the turtle goes back to betwwen routines

function turn() --just keep turning, turning, turning
turtle.turnLeft()
o = o + 1
end
function ff() --short for "Face-Forward"
while (o)%4 ~= 0 do
turn()
end
end
function fl() --"face-left"
while (o)%4 ~= 1 do
turn()
end
end
function fb() --"face-back"
while (o)%4 ~= 2 do
turn()
end
end
function fr() --"face-right"
while (o)%4 ~= 3 do
turn()
end
end

--Actual Movement functions below this line.
function forward()
ff()
turtle.dig()
turtle.forward()
y = y + 1
end
function left()
fl()
turtle.dig()
turtle.forward()
x = x +1
end
function back()
fb()
turtle.dig()
turtle.forward()
y = y - 1
end
function right()
fr()
turtle.dig()
turtle.forward()
x = x - 1
end
function up()
turtle.digUp()
turtle.up()
z = z + 1
end
function down()
turtle.digDown()
turtle.down()
z = z - 1
end

--Go back functions
function savearea() --The all important reference point, tells the turtle to go back to this spot when used
xb,yb,zb,ob = x,y,z,o
end

function goback()
while xb ~= x do
if xb > x then
right()
end
if xb < x then
left()
end
end
while yb ~= y do
if yb > y then
back()
end
if yb < y then
forward()
end
end
while zb ~= z do
if zb > z then
down()
end
if zb < z then
up()
end
end
while ob ~= o do
turn()
end
end

--Checks surrounding blocks against "noise" inventory
function checkforward()
	ff()
if turtle.detect() then
turtle.select(stone)
if turtle.compare() == false then
turtle.select(dirt)
  if turtle.compare() == false then
  turtle.select(gravel)
   if turtle.compare() == false then
   turtle.select(sand)
	if turtle.compare() == false then
	 ore = true
   end
  end
end
end
end
end

function checkleft()
	fl()
if turtle.detect() == true then
turtle.select(stone)
if turtle.compare() == false then
  turtle.select(dirt)
  if turtle.compare() == false then
   turtle.select(gravel)
   if turtle.compare() == false then
	turtle.select(sand)
	if turtle.compare() == false then
	 ore = true
	end
   end
  end
end
end
end

function checkback()
fb()
if turtle.detect() == true then
turtle.select(stone)
if turtle.compare() == false then
  turtle.select(dirt)
  if turtle.compare() == false then
   turtle.select(gravel)
   if turtle.compare() == false then
	turtle.select(sand)
	if turtle.compare() == false then
	 ore = true
	end
   end
  end
end
end
end

function checkright()
fr()
if turtle.detect() == true then
turtle.select(stone)
if turtle.compare() == false then
  turtle.select(dirt)
  if turtle.compare() == false then
   turtle.select(gravel)
   if turtle.compare() == false then
	turtle.select(sand)
	if turtle.compare() == false then
	 ore = true
	end
   end
  end
end
end
end

function checkup()
if turtle.detectUp() == true then
turtle.select(stone)
if turtle.compareUp() == false then
  turtle.select(dirt)
  if turtle.compareUp() == false then
   turtle.select(gravel)
   if turtle.compareUp() == false then
	turtle.select(sand)
	if turtle.compareUp() == false then
	 ore = true
	end
   end
  end
end
end
end

function checkdown()
if turtle.detectDown() == true then
turtle.select(stone)
if turtle.compareDown() == false then
  turtle.select(dirt)
  if turtle.compareDown() == false then
   turtle.select(gravel)
   if turtle.compareDown() == false then
	turtle.select(sand)
	if turtle.compareDown() == false then
	 ore = true
	end
   end
  end
end
end
end

--The "Smart Mine" functions

function deepmine()
	checkforward()
if ore == true then
	forward()
	ore = false
	deepmine()
else checkup()
	if ore == true then
	up()
	ore = false
	deepmine()
else checkdown()
	if ore == true then
	down()
	ore = false
	deepmine()
else checkleft()
	if ore == true then
	left()
	ore = false
	deepmine()
else checkback()
	if ore == true then
	back()
	ore = false
	deepmine()
else checkright()
	if ore == true then
	right()
	ore = false
	deepmine()
	end
end
end
end
end
end
goback()
end

function checkrabbit()
print("rabbit?")
checkforward()
print("check forward")
if ore == true then
	forward()
	ore = false
	deepmine()
else
print("Left?")
checkleft()
print("Check Left")
	if ore == true then
	left()
	ore = false
	deepmine()
else checkback()
	if ore == true then
	back()
	ore = false
	deepmine()
else checkright()
	if ore == true then
	right()
	ore = false
	deepmine()
end
end
end
end
end

function checktunnel()
checkup()
if ore == true then
	forward()
	ore = false
	deepmine()
else checkleft()
	if ore == true then
	left()
	ore = false
	deepmine()
else checkright()
	if ore == true then
	right()
	ore = false
	deepmine()
end
end
end
end

function checkreturn()
	checkdown()
	if ore == true then
	down()
	ore = false
	deepmine()
else checkleft()
	if ore == true then
	left()
	ore = false
	deepmine()
else checkright()
	if ore == true then
	right()
	ore = false
	deepmine()
end
end
end
end

--Functions Actually to be used
function start()
term.clear()
term.setCursorPos(1,1)
print("Please place two unique enderchests in the last two slots, then torches, sand, gravel, dirt, and stone.")
write("How far down should I go? ")
depth = tostring( read())
write("How far forward should I go? ")
length = tostring( read())
end

function rabbithole()
for i=1,depth do
savearea()
print("lets mine")
checkrabbit()
down()
end
end

function tunnel()
for i=1,length do
savearea()
checktunnel()
forward()
turtle.digDown()
end
end

function rere()
down()
for i = 1,length do
savearea()
back()
checkreturn()
end
for i = 1,depth do
savearea()
up()
end
while ob ~= o do
turn()
end
end

--Finally, the end of this crap
start()
rabbithole()
tunnel()
rere()
Lyqyd #2
Posted 20 April 2013 - 08:56 AM
Split into new topic.
Spongy141 #3
Posted 20 April 2013 - 02:18 PM
Try using a GPS…. also make SURE to define a function more calling it…. I'm not sure if you did that.
Lyqyd #4
Posted 20 April 2013 - 02:23 PM
Not a helpful answer. The title stipulates, without GPS.

The best thing to try is to keep track of your movements as you go, kind of like the excavate program does.