Jump to content




How To Code This Grid Into A Mining Turtle.


33 replies to this topic

#1 dom123

  • Members
  • 21 posts

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.
Posted Image

#2 popdog15

  • Members
  • 82 posts

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


#3 spdkils

  • Members
  • 49 posts

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.

#4 dom123

  • Members
  • 21 posts

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

#5 dom123

  • Members
  • 21 posts

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?

#6 spdkils

  • Members
  • 49 posts

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.

#7 spdkils

  • Members
  • 49 posts

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


#8 dom123

  • Members
  • 21 posts

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?

#9 spdkils

  • Members
  • 49 posts

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


#10 dom123

  • Members
  • 21 posts

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?

#11 spdkils

  • Members
  • 49 posts

Posted 30 October 2013 - 10:01 AM

One way to find out! :D/&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

#12 dom123

  • Members
  • 21 posts

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


#13 spdkils

  • Members
  • 49 posts

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

#14 spdkils

  • Members
  • 49 posts

Posted 30 October 2013 - 12:16 PM

Quote

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.

#15 spdkils

  • Members
  • 49 posts

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.

#16 dom123

  • Members
  • 21 posts

Posted 30 October 2013 - 12:24 PM

View Postspdkils, on 30 October 2013 - 12:16 PM, said:

Quote

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?

#17 dom123

  • Members
  • 21 posts

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.

#18 spdkils

  • Members
  • 49 posts

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.

#19 dom123

  • Members
  • 21 posts

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


#20 spdkils

  • Members
  • 49 posts

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()





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users