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

How To Code This Grid Into A Mining Turtle.

Started by dom123, 29 October 2013 - 11:04 AM
dom123 #1
Posted 29 October 2013 - 12:04 PM
so i was looking at grid of the perfect sugar cane farm and thought that would be really nice for spot mining minerals only. I need to have the turtle mine straight down based off the grid i provided. Please any help would be great.

i need him to stop at each area i have identified with blue because i have a line of code to have him dig down and compare the walls for ore.
popdog15 #2
Posted 29 October 2013 - 12:33 PM
I believe something like may work, but that only supports 1 rim.

x = 0

function firstRim()
for x >= 0 do
  turtle.forward()
  turtle.forward()
  turtle.forward()
  turtle.forward()
  turtle.dig()
  x = x-1
end
end
firstRim()
if x == 0 then
turtle.turnRight()
firstRim()
end
spdkils #3
Posted 29 October 2013 - 02:00 PM
edit: I was wrong… I counted wrong.

My solution, only worked for the 1st two rows, I didn't see the pattern right.

Maybe build an array of {fd, fd, fd, fd, dig, fd, fd, fd, fd, dig, fd, fd, fd, fd, dig, fd, fd, fd ,fd ,dig}

Then pass that to a function that does the commands.

Then pass that to a function that pop 2 off the back, and pastes them in the front.

Then run it in reverse

Then pop 2 off the back, and put them in front.

then run it forward.

Crazy, but that is the right pattern.

….x….x….x….x

two off the back into the front….
.x….x….x….x…

seems sane…. I'd rather do it without passing an array, but the pattern is obnoxous.
dom123 #4
Posted 29 October 2013 - 02:53 PM
Ok i put my best foot forward. take a look at this and let me know what you think.

Any feed back is welcome

http://pastebin.com/EctP4Ndb
dom123 #5
Posted 29 October 2013 - 03:04 PM
how would i get the turtle to get back to complete the action? i think i am a little confused. I am sorry i just started computer craft this week.

I used your code format and added in my functions.

http://pastebin.com/Pz0fJYcA

how would the uturn be put in?
spdkils #6
Posted 29 October 2013 - 03:56 PM
This may be crazy, and I haven't tested it, but…


local tf = turtle.forward
local function td() turtle.digDown() turtle.forward() end
local tn = turtle.turnRight
local tCom = {tf,tf,tf,tf,td,tf,tf,tf,tf,td,tf,tf,tf,tf,td,tf,tf,tf,tf,td,tf,tf,tf,tf,td}

local function arrayShift(tMyArray)
for i=1,2 do
table.insert(tMyArray,1,tMyArray[#tMyArray])
table.remove(tMyArray,#tMyArray)
end
return tMyArray
end
local function myDoit(tArray,dir)
if dir ~= 0 then
  for i=1,#tArray do
  tArray[i]()
  end
else
  for i=#tArray,1, -1 do
  tArray[i]()
  end
end
end
tf()
for i=1,25 do
myDoit(tCom,i%2)
tCom = arrayShift(tCom)
if i%2 == 0 then
tn = turtle.turnLeft
else
tn = turtle.turnRight
end
tn()
tf()
tn()
tf()
end

I modified one function, tested some functions in computercraft, and I'm pretty sure this will work!!!

You can replace the td function with your dig/turn/scan if you want. That is fine. It just has to finish facing the correct direction, and take 1 step forward at the finish.
spdkils #7
Posted 29 October 2013 - 04:04 PM
You don't want to use my loop. :(/> I didn't count it out right.

My second attempt with the table shifting is the correct pattern.. (Assuming my table shift logic works, I'm testing.)
The u-turn is shown in that i%2 snip.

If I do simple turns I normally use a width counter and %2 it. If I want to do more interesting things I normally set a variable and toggle it when I alternate turns.


local alternate = true
if alternate then
 turtle.turnRight()
 turtle.forward()
 turtle.turnRight()
else
 turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
end
alternate = not alternate
dom123 #8
Posted 29 October 2013 - 04:19 PM
Lol this is the point where I stop being able to follow the code in my head….. I have not used arrays for turtle or anything in computercraft. my only array experience is with java and even that can be limited in what i am used to doing.

hmm. how could i mix in my dig function into that code?
spdkils #9
Posted 29 October 2013 - 04:30 PM
I define the td fucntion at the very top of the code…

You can just overwrite that with anything you want.

It is a "TD" step, so you could just loop a dig to bedrock, then spin all the way back up checking the walls, then end facing the right way and take a step.


local function td(a,b,c,d)
-- insert anything you want
-- just end at the same height you started
-- and facing the correct direction
-- and one last thing...turtle.forward() so you don't get your steps off
end
dom123 #10
Posted 30 October 2013 - 09:53 AM

local y = 100
local z = 0
local i = 0
local tf = turtle.forward
local function td()
for z = 1, y do
  turtle.digDown()
  FullStorage()
  RefuelDigger()
  for t = 1, 4 do
   turtle.select(t)
   if not turtle.compare() then
    turtle.dig()
   end
   turtle.turnRight()
  end
end
for z = 1, y do
  turtle.up()
end
end
function RefuelDigger()
if turtle.getFuelLevel() < 30 then
  if turtle.getItemCount(16) == 1 then
   turtle.select(16)
   turtle.refuel(1)
   turtle.select(15)
   if turtle.placeUp()then
    turtle.select(16)
    turtle.suckUp()
    turtle.select(15)
    turtle.digUp()
   end
  else
   turtle.select(16)
   turtle.refuel(1)
  end
end
end
function FullStorage()
if turtle.getItemCount(13) > 0 then
  turtle.select(14)
  if turtle.placeUp() then
   for i = 5, 13 do
    turtle.select(i)
    turtle.dropUp()
   end
   turtle.select(14)
   turtle.digUp()
  end
end
end
local tn = turtle.turnRight
local tCom = {tf,tf,tf,tf,td,tf,tf,tf,tf,td,tf,tf,tf,tf,td,tf,tf,tf,tf,td,tf,tf,tf,tf,td}
local function arrayShift(tMyArray)
for i=1,2 do
  table.insert(tMyArray,1,tMyArray[#tMyArray])
  table.remove(tMyArray,#tMyArray)
end
return tMyArray
end
local function myDoit(tArray,dir)
if dir ~= 0 then
  for i=1,#tArray do
  tArray[i]()
  end
else
    for i=#tArray,1, -1 do
    tArray[i]()
  end
end
end
tf()
for i=1,25 do
myDoit(tCom,i%2)
tCom = arrayShift(tCom)
if i%2 == 0 then
  tn = turtle.turnLeft
else
  tn = turtle.turnRight
end
tn()
tf()
tn()
tf()
end
will this work?
spdkils #11
Posted 30 October 2013 - 10:01 AM
One way to find out! :D/>/&amp;gt;

I'm not sure I understand why you're placing up, and sucking up…

if you're drilling down, and you run low on fuel, you select 16 and refuel, then you select 15, and place up? sealing your miner in? Why would you ever placeUp? What is in slot 15 you want to dig it, and then refuel from it?
It appears you also try and place a chest, then fill it, then break it?


I'm not too clear on your dig down moves…

Also if you go down 100 (assuming that loop doesn't deadlock on a mob) you come back up 100. But you don't actually count steps on the way down, so you're not sure if you're going to end in the same spot.

Also you you never move down, also you don't loop your checks on all 4 sites… You have to build a for loop around your turtle.select()/turtle.compare() logic to ensure you check all 4 sides.

(I'm running that code in a super flat world.)

I fixed most of the issues with this, however because you're not actually verifying moves, if you hit a mob, the count goes off. Your TD function didn't deal with gathering loot properly, and I have no clue about your refuel function, nor your "i'm full" function. Those still seem… very odd to me.

http://pastebin.com/CwTEk33r
dom123 #12
Posted 30 October 2013 - 11:25 AM
Thank you for all of your help… I want to hammer out this code before i get home so that i can get some turtle progress going. Plus this is teaching me many core coding tricks to use on all my future turtles like step counting and such. It is good to see examples from people who know what works and what doesn't :)/>

– slot 15 is a enderstorage chest that only has turtle fuel
– slot 14 is a enderstorage chest that is a deposit for my sorting station that will auto remove things from the chest.
(this build is for FTB)

doesnt this loop account for all side checking?
for t = 1, 4 do
turtle.select(t)
if not turtle.compare() then
turtle.dig()
end

Where would you edit the code to make these corrections. I am having a difficult time getting any of my turtles to work properly


local y = 100
local z = 0
local i = 0local tf = turtle.forward
local function td()
for z = 1, y do
  turtle.digDown()
  FullStorage()
  RefuelDigger()
  turtle.down()
  for t = 1, 4 do
   turtle.select(t)
   if not turtle.compare() then
	turtle.dig()
   end
   turtle.turnRight()
  end
end
for z = 1, y do
  turtle.up()
end
end
function RefuelDigger()
if turtle.getFuelLevel() < 30 then
  if turtle.getItemCount(16) == 1 then
   turtle.select(16)
   turtle.refuel(1)
   turtle.select(15)
   if turtle.placeUp()then
	turtle.select(16)
	turtle.suckUp()
	turtle.select(15)
	turtle.digUp()
   end
  else
   turtle.select(16)
   turtle.refuel(1)
  end
end
endfunction FullStorage()
if turtle.getItemCount(13) > 0 then
  turtle.select(14)
  if turtle.placeUp() then
   for i = 5, 13 do
	turtle.select(i)
	turtle.dropUp()
   end
   turtle.select(14)
   turtle.digUp()
  end
end
end
local tn = turtle.turnRight
local tCom = {tf,tf,tf,tf,td,tf,tf,tf,tf,td,tf,tf,tf,tf,td,tf,tf,tf,tf,td,tf,tf,tf,tf,td}local function arrayShift(tMyArray)
for i=1,2 do
  table.insert(tMyArray,1,tMyArray[#tMyArray])
  table.remove(tMyArray,#tMyArray)
end
return tMyArray
end
local function myDoit(tArray,dir)
if dir ~= 0 then
  for i=1,#tArray do
  tArray[i]()
  end
else
	for i=#tArray,1, -1 do
	tArray[i]()
  end
end
endtf()
for i=1,25 do
myDoit(tCom,i%2)
tCom = arrayShift(tCom)
if i%2 == 0 then
  tn = turtle.turnLeft
else
  tn = turtle.turnRight
end
tn()
tf()
tn()
tf()
end
spdkils #13
Posted 30 October 2013 - 12:14 PM
I fixed most of the issues with this, however because you're not actually verifying moves, if you hit a mob, the count goes off. Your TD function didn't deal with gathering loot properly, and I have no clue about your refuel function, nor your "i'm full" function. Those still seem… very odd to me.

http://pastebin.com/CwTEk33r
spdkils #14
Posted 30 October 2013 - 12:16 PM
doesnt this loop account for all side checking?
for t = 1, 4 do
turtle.select(t)
if not turtle.compare() then
turtle.dig()
end

Not really, look how I fixed it and think it thru…

You face foward, only compare vs 1 of the 4 blocks, and if it isn't 1 of those four you dig it… So you're going to di a LOT of blocks.

You want to face, set the dig check as TRUE, and false it, if it matches any of the 4 resources you do not want.

So dig it? YES!
is it stone
is it dirt
is it gravel
is it sand
if all those came back FALSE then yes still dig it….
if any of those toggle dig it false, then it's garbage, move on.

Then turn, and loop again. It's fixed it that pastebin I posted.
spdkils #15
Posted 30 October 2013 - 12:20 PM
Oh I also commented almost all my changes, so you understand or can see what I modified simply.
dom123 #16
Posted 30 October 2013 - 12:24 PM
doesnt this loop account for all side checking?
for t = 1, 4 do
turtle.select(t)
if not turtle.compare() then
turtle.dig()
end

Not really, look how I fixed it and think it thru…

You face foward, only compare vs 1 of the 4 blocks, and if it isn't 1 of those four you dig it… So you're going to di a LOT of blocks.

You want to face, set the dig check as TRUE, and false it, if it matches any of the 4 resources you do not want.

So dig it? YES!
is it stone
is it dirt
is it gravel
is it sand
if all those came back FALSE then yes still dig it….
if any of those toggle dig it false, then it's garbage, move on.

Then turn, and loop again. It's fixed it that pastebin I posted.

AH now i see exactly what you mean,, i was only comparing side 1 to slot 1, side 2 to slot 2 and so on.

do you have a suggested code implant i can use to handle the block count and the mob issue?

the Refuel and I'm full function what seems odd about them maybe i did something wrong?
dom123 #17
Posted 30 October 2013 - 12:26 PM
i can follow your code very well.. if i could just add in some count and some insurance against mobs this might be a viable code for me to use tonight.
spdkils #18
Posted 30 October 2013 - 12:31 PM
while not turtle.forward() do
turtle.dig()
turtle.attack()
end

here is a simple way to deadlock until you CAN move forward.
dom123 #19
Posted 30 October 2013 - 12:34 PM
I see
I added the suggested code what do you think will this work?

– also with all the help i would like to give credit to you in the code's heading for all of the assistance you have provided is that acceptable?


local y = 2
local z = 0
local i = 0
local tf = turtle.forward
local function td()
local gooditem = true
for z = 1, y do
turtle.digDown()
while not turtle.down() do
  turtle.digDown()
  turtle.attackDown()
end
FullStorage()
RefuelDigger()
for i = 1, 4 do
  gooditem = true
  for t = 1, 4 do
   turtle.select(t)
   if turtle.compare() then
    gooditem = false
   end
  end
  if gooditem then
   turtle.select(1)
   turtle.dig()  
  end
	 turtle.turnRight()
  end
end -- added end
for z = 1, y do
while not turtle.up() do
turtle.digUp()
turtle.attackUp()
end
end
turtle.forward()
end
function RefuelDigger()
if turtle.getFuelLevel() < 30 then
  if turtle.getItemCount(16) == 1 then
   turtle.select(16)
   turtle.refuel(1)
   turtle.select(15)
   if turtle.placeUp()then
    turtle.select(16)
    turtle.suckUp()
    turtle.select(15)
    turtle.digUp()
   end
  else
   turtle.select(16)
   turtle.refuel(1)
  end
end
end
function FullStorage()
if turtle.getItemCount(13) > 0 then
  turtle.select(14)
  if turtle.placeUp() then
   for i = 5, 13 do
    turtle.select(i)
    turtle.dropUp()
   end
   turtle.select(14)
   turtle.digUp()
  end
end
end
local tn = turtle.turnRight
local tCom = {tf,tf,tf,tf,td,tf,tf,tf,tf,td,tf,tf,tf,tf,td,tf,tf,tf,tf,td,tf,tf,tf,tf,td}
local function arrayShift(tMyArray)
for i=1,2 do
  table.insert(tMyArray,1,tMyArray[#tMyArray])
  table.remove(tMyArray,#tMyArray)
end
return tMyArray
end
local function myDoit(tArray,dir)
if dir ~= 0 then
  for i=1,#tArray do
  tArray[i]()
  end
else
    for i=#tArray,1, -1 do
    tArray[i]()
  end
end
end
tf()
for i=1,25 do
myDoit(tCom,i%2)
tCom = arrayShift(tCom)
if i%2 == 0 then
  tn = turtle.turnLeft
else
  tn = turtle.turnRight
end
tn()
tf()
tn()
tf()
end
spdkils #20
Posted 30 October 2013 - 12:44 PM
you have one forward in TD that doesn't deadlock.

replace the tf with a simple move function.

local function tf() while not turtle.forward() turtle.attack() end

that will fix that issue.

you can just replace any turtle.forward() with tf()
dom123 #21
Posted 30 October 2013 - 12:48 PM
ok i added in your change to the code

http://pastebin.com/UgBe80FK

any chance you could look it over one more time or run a test on it to make sure I did not scramble it?
spdkils #22
Posted 30 October 2013 - 01:20 PM
I updated it, fixed a typo I made. also, you must have an ender chest mod, because when you break the default ender chest it just turns into obsidian… :(/> so I don't have that mod loaded, so testing the refuel isn't easy without adding that mod. Also when you break an ender chest, doesn't it lose the color code, so you won't be able to have 2 ender chests? Or am I thinking about it wrong?

http://pastebin.com/CwTEk33r
spdkils #23
Posted 30 October 2013 - 01:26 PM
Oh the other issue I have is with that y coord of 100, you have no idea if you'll hit bedrock, so a step counter on decent would be needed. So instead of running y 100 times… use a while loop, count as you dig down, when you can't move down, can turtle detect, but can't dig… you know you've hit bedrock… Then loop back up.

Also the dig down, isn't bias, it just digs down… So when you loop the inventory slots of 1-4, you should discard anything above 1, so that you don't fill on dirt/cobble on accident.
spdkils #24
Posted 30 October 2013 - 01:36 PM
Also, I keep updating the code in that pastebin. I have an account, so I keep updating it with all the fixes we're making. So, if you keep looking @ that, I keep all the changes up to date in there.
dom123 #25
Posted 30 October 2013 - 01:53 PM
Thank you that will be great i have so many pastebin's saved in this notepad file i think it wants to burst lol

Enderstorage keeps the color when it breaks all the meta data stays on the block as it converts into a item. so i am good there. I have seen the refuel and the store actually work already as I took them from one of my other codes.

– does the td() turtle.down() need the mob checking???

for the y could i add a read? so the user could id how high from bedrock they are. On my server we run voxel map which gives an alt or the person can f3

but your count until you get to an unbreakable block is also very nice. – how would you code it in.

**
with unlimited fuel you could run without the need of refuel code

and for the place you could just have it dump its inventory on the ground when it is full to simulate my deposit code.


function FullStorage()
if turtle.getItemCount(13) > 0 then
   for i = 5, 13 do
    turtle.select(i)
    turtle.dropUp()
   end
   turtle.select(14)
   turtle.digUp()
end
**
spdkils #26
Posted 30 October 2013 - 02:06 PM
Always assume it will fail. :D/>

So I would check all the time. Also unless you're digging "flat" ground, you're going ot run into the issue of not being tall enough. So, there are still some assumptions we make while running this code.
If you cut thru a hill, then you've got that issue. But we can just swiss cheeze the hill, and cut forward every turtle forward, to ensure it's clean. at least it will be a flat plane of empty.

The program still ends far corner because we're doing 25 wide, if we move it 26 it will at least end on the same side as the start. (even cuts, I almost always use them.)

Let me hack in a few more things.

Same pastebin as always.
spdkils #27
Posted 30 October 2013 - 02:55 PM
http://pastebin.com/CwTEk33r

Added a check for bed rock, functioned the search…

Still need to discard cobble, and a few other things, and optimize code for speed!


I'd rewrite a lot of this, because it's grown organically it makes it hard to hack in new code. Also, I'd function so much stuff, so the loop would be insanely clean.
dom123 #28
Posted 30 October 2013 - 03:14 PM
i like the bedrock check,,, do you think that a cobble dump is needed?

i like where you move the Refuel and item check areas.
spdkils #29
Posted 30 October 2013 - 03:37 PM
You can dump it, but that takes a slot, or you can just toss it into the ender chest… If you're not in sand, you could just replace slot 4 with cobble… then it deals with it. So have cobble, dirt, gravel, stone. Then you keep sand! (Great unless you're in a desert.)
whatxDxDxD #30
Posted 30 October 2013 - 04:44 PM
To be honest I didn't read all the answers. But i thought i could maybe help, because I wrote pretty much the same program.
So far the program worked fine for me.
"size" is just the length of one side of the square
fuelcheck() checks the fuel and
refuel() refuels the turtle.
This is the block I wrote for moving the turtle.
Spoiler


local x = 0
local y = 0
local z = 0
local side = 0

function turn(times)
  local i  
  for i=1, times, 1 do
	turtle.turnRight()
	side = (side +1)%4
  end
end

function forward(dist)
  local i
  for i=1, dist, 1 do
	while not turtle.forward() do
	  turtle.dig()
	  turtle.attack()
	end
	if side == 0 then
	  x = x+1
	elseif side == 1 then
	  y = y+1
	elseif side == 2 then
	  x = x-1
	elseif side == 3 then
	  y = y-1
	end
  end
end

function mover()
  if fuelcheck(10) then
	if side == 0 then
	  if x+5 <= size then
		forward(5)
	  elseif y+1 <= size then
		local dist = size-x
		forward(dist)
		turn(1)
		forward(1)
		turn(1)
		  if (x-(dist+3)%5) > 0 then
			forward((dist+3)%5)
		  else
			mover()
		  end
	  else
		return false
	  end
	elseif side == 2 then
	  if x-5 >= 0 then
		forward(5)
	  elseif y+1 <= size then
		local dist = x
		forward(x)
		turn(3)
		forward(1)
		turn(3)
		  if (x-(dist+2)%5) < size then
			forward((dist+2)%5)
		  else
			mover()
		  end
	  else
		return false
	  end
	end
  else
	print("Not enough fuel!")
	refuel()
  end
  return true
end
spdkils #31
Posted 30 October 2013 - 05:57 PM
Well, everything works in the code I wrote. I've tested it… It's slow… very slow… Because turtle.select() is kinda slow. It makes lots of checks because it is spinning.

If you were to cut every 3rd layer, and turtle compare top/bottom at the same time, you would need 1/2 the selects, but more moves. Calling the drop, and how it works out, it isn't the fastest thing I've ever worked on, but it works completly.

I never thought of using the enderchest addon, to give the turtle infinite storage. I always just speed things up by making MORE turtles!!!! (I have 16 of them that I run in mass with wireless controlling to do my "big" jobs.)

You could speed it up by 'sewing' dig down, move across dig up, move dig down, move dig up. etc…. So you could cut up and down thru the job to prevent a lot of wasted movements. 40 down, 40 up (wasted), 40 down, 40 up (wasted).

There are lots of patterns to use.

I just think it's fun to write code, and solve problems. So I'll probably tweak this some more. See if I can come up with a faster way of doing the compares. (I typically just dig, and discard, but that leaves a BIG hole)
dom123 #32
Posted 31 October 2013 - 07:47 AM
Ya i tested it as well it worked great. I was wondering how to code a turtle to pick up were it leaves off for example if the turtle is unloaded or the server is restarted. is there a code that would have it start from were it left off?
sens #33
Posted 31 October 2013 - 08:46 AM
I was wondering how to code a turtle to pick up were it leaves off for example if the turtle is unloaded or the server is restarted.
The other day, I noticed an API that handles this case well:

http://www.computercraft.info/forums2/index.php?/topic/13919-lama-location-aware-movement-api-v14/
spdkils #34
Posted 31 October 2013 - 10:27 AM
Generally
1) record what row you're on. (Change for loop to while loop, with manual counter)
2) record what hole you're on. (Duplicate the array, remove each command as they are executed.)
3) upon startup check those values, to see if it's a resume. (That is why we have to use a while loop.)
4) if you finish, delete the files that save that state.

Not TOO hard. But you may get stuck in strange places if you don't account for turning. So you would have to also record facing… So maybe change the computer label every turn, so you relable, and then turn. OR fire those methods at the same time. IF you're really paranoid about losing state.

^^ Or that API, that looks pretty cool, and you just override the turtle, and refuel functions… maybe easier!