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

Creating Bridge Program

Started by BowWhalley, 17 July 2015 - 11:05 AM
BowWhalley #1
Posted 17 July 2015 - 01:05 PM
I am trying to make a turtle go forward and make a bridge, when it gets to the other side it comes back making the 2nd side of the bridge so it is a 2*2 bridge. Unfortunetly this is not working, I have tried debuging this and solved quite a few issues but I cant seem to find the current issue.


if turtle.detect() ~= "minecraft:air" then
turtle.dig()
turtle.forward()
turtle.placeDown()
else
turtle.forward()
turtle.placeDown()
end
X1 = false
while true do

if turtle.detectDown() ~= "minecraft:air" and turtle.detectDown() ~= "minecraft:stone" and X1 == false then
turtle.turnRight()
if turtle.detect() ~= "minecraft:air" then
turtle.dig()
turtle.forward()
else
turtle.forward()
end
if turtle.detect() ~= "minecraft:air" then
turtle.dig()
turtle.forward()
turtle.placeDown()
else
turtle.forward()
turtle.placeDown()
end
turtle.turnRight()
X1 = true
end

if turtle.detect() ~= "minecraft:air" then
turtle.dig()
turtle.forward()
else
turtle.forward()
end

if turtle.detectDown() ~= "minecraft:air" and turtle.detectDown() ~= "minecraft:stone" and X1 == true then
turtle.turnRight()
turtle.turnRight()
return
end
end

Here is an gif of what it is doing. I have realised it reacts to the stone even though I have tried telling it to ignore the stone.
Edited on 17 July 2015 - 11:12 AM
Bomb Bloke #2
Posted 17 July 2015 - 02:50 PM
if turtle.detect() ~= "minecraft:air" then
turtle.dig()
turtle.forward()
turtle.placeDown()
else
turtle.forward()
turtle.placeDown()
end

Two points about the above code snippet;

One, turtle.detect() doesn't return a string with the block type - it returns true (a boolean) if there's any block (other than air, or liquids), or otherwise it returns false. It'll never be "minecraft:air" or anything similar. You're thinking of turtle.inspect(), which actually returns two values - but truth be told, detect will probably suit your purposes just fine, if used properly.

Two, there's not much point in having that "else" block there, given that it contains identical code to perform as compared to when the statement is true. Think about which lines are the same and move them out of the "if" block completely, stating them once underneath it.

Eg, there's no point in doing this:

if <condition> then
  do X
  do Y
else do Y end

… when you can just do this:

if <condition> then do X end
do Y

Really though, this snippet I gave you in your other thread is the way to go:

  while not turtle.forward() do
    turtle.dig()
    turtle.attack()
  end

That'll make the turtle move forward once. The script won't progress until it happens. If it can't happen (eg because the turtle ran out of fuel or there's bedrock or something in the way), the loop prevents the script from carrying on with later instructions that don't make sense to execute any more (because the turtle's not where it should be).

You can even move it into a function of its own, making it easier to use:

local function goForward()
  while not turtle.forward() do
    turtle.dig()
    turtle.attack()
  end
end

.
.
.

goForward()
Edited on 17 July 2015 - 12:52 PM
BowWhalley #3
Posted 17 July 2015 - 03:09 PM
if turtle.detect() ~= "minecraft:air" then
turtle.dig()
turtle.forward()
turtle.placeDown()
else
turtle.forward()
turtle.placeDown()
end

Two points about the above code snippet;

One, turtle.detect() doesn't return a string with the block type - it returns true (a boolean) if there's any block (other than air, or liquids), or otherwise it returns false. It'll never be "minecraft:air" or anything similar. You're thinking of turtle.inspect(), which actually returns two values - but truth be told, detect will probably suit your purposes just fine, if used properly.

Two, there's not much point in having that "else" block there, given that it contains identical code to perform as compared to when the statement is true. Think about which lines are the same and move them out of the "if" block completely, stating them once underneath it.

Eg, there's no point in doing this:

if <condition> then
  do X
  do Y
else do Y end

… when you can just do this:

if <condition> then do X end
do Y

Really though, this snippet I gave you in your other thread is the way to go:

  while not turtle.forward() do
	turtle.dig()
	turtle.attack()
  end

That'll make the turtle move forward once. The script won't progress until it happens. If it can't happen (eg because the turtle ran out of fuel or there's bedrock or something in the way), the loop prevents the script from carrying on with later instructions that don't make sense to execute any more (because the turtle's not where it should be).

You can even move it into a function of its own, making it easier to use:

local function goForward()
  while not turtle.forward() do
	turtle.dig()
	turtle.attack()
  end
end

.
.
.

goForward()


Thankyou, I have taken done fixed those mistakes. Thanks for helping me compact the code down so I dont need to use else.
Here is my current code.

if turtle.detect() ~= "minecraft:air" then
turtle.dig()
end
turtle.attack()
turtle.forward()
turtle.placeDown()
X1 = false
while true do
--Turn back, In other words it is a U-Turn
if turtle.inspectDown() ~= "minecraft:air" and turtle.inspectDown() ~= "minecraft:stone" and X1 == false then
turtle.turnRight()
if turtle.detect() ~= "minecraft:air" then
turtle.dig()
end
turtle.attack()
turtle.forward()
if turtle.detect() ~= "minecraft:air" then
turtle.dig()
end
turtle.attack()
turtle.forward()
turtle.placeDown()
turtle.turnRight()
X1 = true
end
--Dig Process
if turtle.detect() ~= "minecraft:air" then
turtle.dig()
end
turtle.attack()
turtle.forward()
--End Process
if turtle.inspectDown() ~= "minecraft:air" and turtle.inspectDown() ~= "minecraft:stone" and X1 == true then
turtle.turnRight()
turtle.turnRight()
return
end
end
Unfortunetly it is still doing the same thing as before.
Edit: Fixed a minor error in code and updated problem.
Edited on 17 July 2015 - 02:18 PM
jerimo #4
Posted 17 July 2015 - 06:56 PM
Have you ever made a bedrock miner? As in it goes down to bedrock then comes back up? If so this is exactly the same thing but horizontal.

bridgeLength = 0
turtle.forward()
while not turtle.detectDown() do
  turtle.placeDown()
  turtle.forward() --# or bomb blokes go forward
  bridgeLength = bridgeLength + 1
end

turtle.turnRight()
turtle.forward()
turtle.turnRight()

for i=1, bridgeLength do
  turtle.forward()
  turtle.placeDown()
end

Essentially you are making it until you find ground, then turn around and make it the same length to the right of the original one

Obviously there is no inventory management nor refueling, I'll leave that up to you
Edited on 17 July 2015 - 05:14 PM
HPWebcamAble #5
Posted 17 July 2015 - 07:01 PM
Spoiler

if turtle.detect() ~= "minecraft:air" then
turtle.dig()
end
turtle.attack()
turtle.forward()
turtle.placeDown()
X1 = false
while true do
--Turn back, In other words it is a U-Turn
if turtle.inspectDown() ~= "minecraft:air" and turtle.inspectDown() ~= "minecraft:stone" and X1 == false then
turtle.turnRight()
if turtle.detect() ~= "minecraft:air" then
turtle.dig()
end
turtle.attack()
turtle.forward()
if turtle.detect() ~= "minecraft:air" then
turtle.dig()
end
turtle.attack()
turtle.forward()
turtle.placeDown()
turtle.turnRight()
X1 = true
end
--Dig Process
if turtle.detect() ~= "minecraft:air" then
turtle.dig()
end
turtle.attack()
turtle.forward()
--End Process
if turtle.inspectDown() ~= "minecraft:air" and turtle.inspectDown() ~= "minecraft:stone" and X1 == true then
turtle.turnRight()
turtle.turnRight()
return
end
end
Unfortunetly it is still doing the same thing as before.

You still didn't fix everything Bomb suggested.
turtle.detect() will ONLY return 'true' or 'false', if there is, or isn't, a block in front of the turtle.



  turtle.forward()//or bomb blokes go forward


Lua comments start with two dashes ( – )
On the forums, you might want to add a # ( –# )

Also, your ending code tag has the wrong slash, that's why it isn't formatted correctly.
jerimo #6
Posted 17 July 2015 - 07:11 PM
Spoiler

if turtle.detect() ~= "minecraft:air" then
turtle.dig()
end
turtle.attack()
turtle.forward()
turtle.placeDown()
X1 = false
while true do
--Turn back, In other words it is a U-Turn
if turtle.inspectDown() ~= "minecraft:air" and turtle.inspectDown() ~= "minecraft:stone" and X1 == false then
turtle.turnRight()
if turtle.detect() ~= "minecraft:air" then
turtle.dig()
end
turtle.attack()
turtle.forward()
if turtle.detect() ~= "minecraft:air" then
turtle.dig()
end
turtle.attack()
turtle.forward()
turtle.placeDown()
turtle.turnRight()
X1 = true
end
--Dig Process
if turtle.detect() ~= "minecraft:air" then
turtle.dig()
end
turtle.attack()
turtle.forward()
--End Process
if turtle.inspectDown() ~= "minecraft:air" and turtle.inspectDown() ~= "minecraft:stone" and X1 == true then
turtle.turnRight()
turtle.turnRight()
return
end
end
Unfortunetly it is still doing the same thing as before.

You still didn't fix everything Bomb suggested.
turtle.detect() will ONLY return 'true' or 'false', if there is, or isn't, a block in front of the turtle.



  turtle.forward()//or bomb blokes go forward


Lua comments start with two dashes ( – )
On the forums, you might want to add a # ( –# )

Also, your ending code tag has the wrong slash, that's why it isn't formatted correctly.
Riiiight! Haven't touched lua in a while, will fix it asap, thanks
Bomb Bloke #7
Posted 18 July 2015 - 01:14 PM
You still didn't fix everything Bomb suggested.
turtle.detect() will ONLY return 'true' or 'false', if there is, or isn't, a block in front of the turtle.

… this, plus that's not how turtle.inspect() works. Two return values! Gotta handle both! Read the links I gave you, ask if any bits don't make sense to you!

local blockFound, inspected = turtle.inspectDown()
if blockFound and inspected.name ~= "minecraft:air" and inspected.name ~= "minecraft:stone" and X1 == false then