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

[program help] advanced tunnel maker

Started by mikesaa309, 22 December 2012 - 07:44 AM
mikesaa309 #1
Posted 22 December 2012 - 08:44 AM
I'm new to the whole programming thing. I've never really done much programming other than a bit of visual basic so I know a bit about loops etc but I've never made a program as complex as this one although people that know how to code will see this code as simple.

(back story skip paragraph for what the code does)

So anyway I was showing my brother the computer craft mod and how i made a turtle create stairs and then he said to make one to make a tunnel. So i decided to give it a go and I know that there's a tunnel program built in but that seems to only work under ground unless i'm doing something wrong.
(what the code is supposed to do)

The program that i'm trying to write builds a tunnel above ground. So far I've managed to get it to ask you how long you want it and then it builds a tunnel which is one block width and 2 height (door size). theres nothing wrong with this code but now what i want to to do it to remove any blocks in it's way. So say it comes to a hill then it digs the block up and replaces it with what ever is in the inventory. But it needs to do this for what ever is in front of it or above it and also loop that code with in the loop for the actual tunnel

so how do i code it. The code is below i know i can use more loops etc to tidy it up but i'm not sure how to do that either unless i work at it but for now i want to do what i said in the above paragraph.

Heres the code: (i tried attempting to write the code my self but it only does it once per loop)


local times = 0
term.write("how many block long?")

length = read()


for i = 1, length do
if turtle.detect() then
turtle.dig()

turtle.turnLeft()
turtle.place()
turtle.turnRight()
turtle.turnRight()
turtle.place()
turtle.up()
turtle.place()
turtle.up()
turtle.place()
turtle.up()
turtle.place()
turtle.turnLeft()
turtle.turnLeft()
turtle.place()
turtle.down()
turtle.placeUp()
turtle.place()
turtle.down()
turtle.place()
turtle.down()
turtle.place()
turtle.turnRight()
turtle.forward()
end
end
Doyle3694 #2
Posted 22 December 2012 - 10:41 AM
That was very bad help mikk, never EVER again just provide code like that. you have to explain what you are doing, and really, all that code was unnessesary.

To OP, the problem with your code I think, is that read() gives a string, which is not usable in a loop. try to change

lenght = read()
to

lenght = tonumber(read())
I don't know how much you know about variables, but there are basically 3 main types of variables(there are more, but those 3 are the most common):
Booleans, which are just true or false.
Strings, which is a bunch of text really, everything enclosed by "" is a string.
"So I am a string"
and I am not a string
Integers, which are numbers. Integer is those you perform maths on, such as 1+1, where both the 1's are integers. however, "1" would be a string
Since you can't perform maths on strings, even if they are just "1", you have to use the functions
tonumber()
tostring()
tonumber() will make a string into a number if it only contains numbers. so "1" would become just 1.
if however the string has a letter, like say the string is "Hello", trying to tonumber() it would result in the yield being nothing, or nil, as it's called in lua.'
tostring() however, can't really fail because a string can accept numbers, but it will basically do the same as tonumber() but backwards, it will make a number a string.
Please leave any question if you are wondering anything more or if I was unclear

EDIT: also, term.write() is a very limited function. try to use write() and print() whenever you can, otherwise you might get some unexpected results
DoubleEDIT: found out another problem. Your code will only run if there is a block infront, because your

if turtle.detect() then
turtle.dig()
doesn't have an end after it. so all that code underneath would only be ran if it detects a block, otherwise, it will skip to everything after that if statements end.

Here is your program fixed:
Spoiler

local times = 0
write("how many blocks long? ")
length = tonumber(read())

for i = 1, length do
   if turtle.detect() then
	  turtle.dig()
   end
   turtle.turnLeft()
   turtle.place()
   turtle.turnRight()
   turtle.turnRight()
   turtle.place()
   turtle.up()
   turtle.place()
   turtle.up()
   turtle.place()
   turtle.up()
   turtle.place()
   turtle.turnLeft()
   turtle.turnLeft()
   turtle.place()
   turtle.down()
   turtle.placeUp()
   turtle.place()
   turtle.down()
   turtle.place()
   turtle.down()
   turtle.place()
   turtle.turnRight()
   turtle.forward()
end
ChunLing #3
Posted 22 December 2012 - 12:14 PM
Actually, a minor but important point is that in Lua there's only numbers (floating point), not integers. In Lua numbers do almost everything integers can do, but they do a lot of other stuff that integers can't (like fractions…and…fractions…yeah, fractions is pretty much the difference).

Anyway, picky technical point aside, didn't you also want to dig out anything that was in the way of the tunnel? If you just add digUps before each up, that would work. As is, it only digs out the bottom block. You don't really need detect tests with dig, if there's nothing to dig then the dig returns false anyway. You use detect if the turtle can't move and can't dig so it knows whether it's dealing with an entity or an unbreakable block.
mikesaa309 #4
Posted 22 December 2012 - 02:50 PM
thanks for your help guys, as i say i'm reasonable new to the programming thing as you can probably tell. Lua maniac i mentioned in one of the paragraphs what i wanted the code to do I know i didn't include comments in the program if that was what you were meaning.

I tested the new code out that you wrote but it still doesn't do what i want it to do. I need it to dig out any blocks in the way and replace it with another block i.e. stone.
Btw is there any in-depth tutorials on lua programming more to do with turtles and/or any good resources online to learn programming. The thing is i'm good at understanding code to the extent where it's simple and where the code does one thing but its when i want to combine loads of different codes to one code, if you get me?

Like for example i'm building a cinema in minecraft using the computercraft the computer program "monitor back = secret/alongtimeago" (i may have wrote that wrong i only found it out today it's something like that) anyway this plays a movie which i'm sure you've seen in computercraft. So i decided to make a program to build the cinema for me but not in one big code. Like i have one for the floor (already) done and now i'm trying to make one for the walls. Which i can do for one block high but then when i want it to go up to the next level i just for the life of me can't get it to work because it (unless im wrong) involves several "For" loops inside one another including code to make it detect when it can't go back because of the first wall of blocks then go up 1 and repeat the process 4 times.
Doyle3694 #5
Posted 22 December 2012 - 04:10 PM
That's funny, because part of my WIP programcollection/OS is a wall maker, I'll give you a straight off link, but be sure to read it carefully, it's in the category of my uncommented programs, but starting to comment it now, so there will probably be comments there in less than 30 min.

http://pastebin.com/LbweXMjB
Orwell #6
Posted 22 December 2012 - 04:33 PM
In Lua numbers do almost everything integers can do, but they do a lot of other stuff that integers can't (like fractions…and…fractions…yeah, fractions is pretty much the difference).
Floating point numbers (or even doubles in Lua) have much a higher range than integers and also have positive/negative infinity and NaN (not a number). Just wanted to add that for future reference. :)/>
Doyle3694 #7
Posted 22 December 2012 - 04:48 PM
Oh and for you question about tutorials, there are many good tutorials in the tutorial section. Also, how I got started, was I watched these turtorials:

[media]http://www.youtube.com/watch?v=QoAvjIucm90&list=PLEC8A2FE422783952&index=1[/media]

EDIT: Done commenting! Recommend copy paste into notepad++ before actually trying to read the comments, pastebin is abit funky about line lenghts.
Also: Make sure to ask if anything in the code is unclear to you
ChunLing #8
Posted 22 December 2012 - 04:48 PM
Yeah, okay, floating point is awesome.

For digging out stuff before placing, just add a dig before each place (the placeUp for the ceiling is already handled, I think).

To handle it all more neatly, you can revise this with some for loops, or even use a replace function:
local function replace()
    turtle.dig()
    return turtle.place()
end
term.write("how many block long?")
local length = tonumber(read())
for i=1,length do
    turtle.turnLeft()
    for i=1,3 do
        replace()
        turtle.digUp()
        turtle.up()
    end
    turtle.turnLeft()
    turtle.turnLeft()
    replace()
    turtle.down()
    turtle.placeUp()
    for i=1,2 do
        turtle.down()
        replace()
    end
    turtle.turnRight()
    turtle.forward()
end