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

Wall builder basics

Started by travy641, 16 September 2014 - 03:15 AM
travy641 #1
Posted 16 September 2014 - 05:15 AM
I am trying to code a turtle to just build a simple wall for me such as ask user two things (height) and (length.) I managed to get the turtle to build the height the way i wanted only problem is when i say for example type 5 for height and 5 for length it will stack blocks 5 high but then just move 5 spots forward when it gets to the specified height. How do i get the turtle to move only one slot forward and go down until it detects the ground and then repeat the height process 5 times instead of just moving 5 slots forward and stopping. How do i get it to repeat the height process and after each row count as 1. Example 2 high 2 length, it will go up 2 go forward 1 come down 2 build up 2, forward until it has reached the length.

I apologize if this topic already exist but nothing i have found has really been helpful, i am extreme noob at this if you can't tell but i really want to learn how to do all this. Thanks greatly appreciated






function wall()
turtle.up()
print("Number of items in slot 1"..turtle.getItemCount(1))
turtle.select(1)
turtle.placeDown()
if turtle.detectUp() then
print("Block above")
turtle.digUp()
end
end


function frwd()
turtle.forward()

end

function dwn()
turtle.down()
turtle.select(1)
turtle.placeUp()
end



–MAIN PROGRAM

term.write("How many blocks high")
blocks = read()

term.write("How many blocks long")
blockss = read()

for i = 1, blocks do
wall()
end

for k = 1, blockss do
frwd()
end
Bomb Bloke #2
Posted 16 September 2014 - 11:37 AM
You can put loops in loops; try changing this:

for i = 1, blocks do
	wall()
end

for k = 1, blockss do
	frwd()
end

… to this:

for k = 1, blockss do
	for i = 1, blocks do
		wall()
	end

	frwd()
end

You'll need to tweak the logic a bit more to finish it off, but hopefully you can see where to go from here. :)/>
travy641 #3
Posted 16 September 2014 - 11:27 PM
It works great with the new change, but instead of going forward and then down it goes forward and up. Example i do 5 height 5 length it will build the wall like this

|
|
|
|
|
Instead of like this ||||| i want it like that not like the staggered pattern above..

I tryed to work around this but it either does that or doesn't do anything at all so im really confused i put a turtle.down() after wall() but nothing happens.
Edited on 17 September 2014 - 12:06 AM
Bomb Bloke #4
Posted 17 September 2014 - 02:54 AM
Well yeah, the turtle can't go down directly after building a segment of wall under itself. The wall's in the way.

You might find it easier to have the turtle turn around and move backwards. Make it place all blocks in front of itself, as opposed to above/below. That way it won't block its own path, and you'll hopefully find it easier to work out the rest.

You've nearly got a working script here, I reckon you can finish it. :)/>
travy641 #5
Posted 17 September 2014 - 03:45 AM
I know the turtle can't go down directly i ment like it was in a staggered pattern up words i wanted to get the turtle to go up however many blocks over one and down the same exact amount of blocks to repeat the process but what it was doing was just that but instead of going down to start the new row it did that in a upward position so i had a staggered look up high and not however many blocks high across. I tryed to make the dashes look staggered but when it posted it put them on top of eachother so sorry for the problem there, i was trying to figure out how to upload a screen shot so you could see exactly what i ment without having to try and mimic it with text.

But i managed to get it sorted out because what it would do is say i typed in 5 for height and 3 for length it would go up 5 forward once down 3 back up 5 so on so forth and repeat thats how i ended up with the staggered look it worked great if the height and length were the same number. I managed to get it working now just trying to figure out how to get the turtle to check the next inventory slot for items to use because as of now if it runs out it just continues to finish as if it has blocks in the inventory. Is there a way i can make it go to the next inventory slot and complete this process for all 16 slots once each inventory item is empty go to the next until it is empty an so on. I know how to make the turtle check for fuel just cant seem to figure this one out. I have a ton more questions about this topic and other things in computercraft that im working on just dont want to seem like im fludding it, as far as asking a question adn getting a response and then immediately asking another question. Im not the best at tis so i apologize if i have to many questions. Its like i have bits and peices to topics but not everything to tie them together to make a cool program/script. Thanks
Bomb Bloke #6
Posted 17 September 2014 - 04:03 AM
I'd use another loop:

for i=1,16 do
  if turtle.getItemCount(i) > 0 then
    turtle.select(i)
    break  -- Jump out of the loop early.
  end
end