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

[Help] Problem With Wall Digging Script

Started by The Epicosity, 02 February 2013 - 01:15 PM
The Epicosity #1
Posted 02 February 2013 - 02:15 PM
title: [Help] Problem With Wall Digging Script
I have a problem with a pretty simple script for digging some giant walls. It works fine except for one thing; once it gets a seemingly random way through the script, it moves over and starts digging out a different area.
Here's the code:

sleep(0)
function digWall()
turtle.dig()
turtle.forward()
end
function endWall()
turtle.turnLeft()
turtle.turnLeft()
turtle.digDown()
turtle.down()
end
j = 1
repeat
i = 1
repeat
  digWall()
  i = i + 1
until i >= 63
endWall()
j = j + 1
until j >= 55
Kingdaro #2
Posted 02 February 2013 - 02:42 PM
I'm not sure of the exact issue with the script, but I can say that using a fori loop would be easier than that repeat..until logic you have there.


for j=1, 55 do
  for i=1, 63 do
    digWall()
  end
  endWall()
end
The Epicosity #3
Posted 02 February 2013 - 03:29 PM
Thanks for the help with the for loop! I'm relatively new to programming, so I love getting a new tool. I guess I should clarify what the problem is better. Here are some screenshots:

There's a partially mined flat wall that I tried the wall script on. I wanted to dig it out to replace it with smooth stone.
The point where the wall is no longer all flat is when the script went weird. You can see tunnels where the turtle started mining out different areas on either end.

There's a better look down the tunnel it created.
In recent testing, I did notice that if I went in front of the turtle while it was executing the script it turned around. Does this have something to do with entities being in front of the turtle? If so, then my question would become: How could I code it into my script to avoid the entities causing this problem?
raineth #4
Posted 02 February 2013 - 04:34 PM
In recent testing, I did notice that if I went in front of the turtle while it was executing the script it turned around. Does this have something to do with entities being in front of the turtle? If so, then my question would become: How could I code it into my script to avoid the entities causing this problem?
Very probably. A common problem when starting with turtles is running into gravel or sand for the first time. What's probably happening is that a block of gravel is falling in front of your turtle (replacing the block it just mined), which prevents it from moving forward. You probably didn't notice because it went on to the next iteration of your loop and tried digging again, but at that point you've incremented i (even though you haven't moved) and will now move fewer total steps.

Many of the turtle's actions (including moving and digging) return true or false depending on whether they succeeded. You could check whether turtle.forward() returned true before incrementing i and/or you could write a loop that tried dig/move/attack until move returns true.
The Epicosity #5
Posted 02 February 2013 - 04:42 PM
In recent testing, I did notice that if I went in front of the turtle while it was executing the script it turned around. Does this have something to do with entities being in front of the turtle? If so, then my question would become: How could I code it into my script to avoid the entities causing this problem?
Very probably. A common problem when starting with turtles is running into gravel or sand for the first time. What's probably happening is that a block of gravel is falling in front of your turtle (replacing the block it just mined), which prevents it from moving forward. You probably didn't notice because it went on to the next iteration of your loop and tried digging again, but at that point you've incremented i (even though you haven't moved) and will now move fewer total steps.

Many of the turtle's actions (including moving and digging) return true or false depending on whether they succeeded. You could check whether turtle.forward() returned true before incrementing i and/or you could write a loop that tried dig/move/attack until move returns true.
Thanks, I'll have to try this!
Roonagooo #6
Posted 02 February 2013 - 11:18 PM
In recent testing, I did notice that if I went in front of the turtle while it was executing the script it turned around. Does this have something to do with entities being in front of the turtle? If so, then my question would become: How could I code it into my script to avoid the entities causing this problem?

it is quite possible that entities are causing your problem

you could put in somthing like
in you first function
turtle.dig()
if turtle.forward() ~= true
then turtle.dig()
turtle.forward()
end
that i think should deal with most of the mob problem
alternatively you could just put

if turtle.forward() ~= true
then turtle.dig()
i = i -1
and that should run with the original program and should deal with mobs and gravel/sand/ and other gravity block feel free to correct me if im wrong. :P/>