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

ccrraazzyyman's -Direwolf20 9x9 House Builder-

Started by ccrraazzyyman, 14 May 2012 - 08:50 AM
ccrraazzyyman #1
Posted 14 May 2012 - 10:50 AM
The task of building 9x9's getting to me, I decided to make my turtles build 9x9's instead. This program does not place torches, or make a hole for the door. That is expected of the user to do :P/>/>

If there is any criticism for my program, please let me know. I just started learning lua for this and I know I have plenty of room to improve my coding.

Also, this forum post completly ruins my code formatting, making it look like garbage. Here's the pastbin link that makes it look much more clear.

http://pastebin.com/MH1JNqpJ


--coded by ccrraazzyyman.
--Do what you want with this code, just give me credit if you
--use it :D/>/>
slot = 1
print("Direwolf20's 9x9 House builder")
print("Slots 1-3 = Walls (2.5 Stacks)")
print("Slot 4 = Floor (49 blocks)")
print("Slot 5 = Ceiling Middle (33 blocks)")
print("Slot 6 = Glass (16 blocks)")
print("Press any key to start")
io.read()
function wall9()
digg()
for x=1, 9, 1 do
  turtle.forward()
  digDown()
  placeDown()
  digg()
end
end
function wall8()
digg()
for x=1, 8, 1 do
  turtle.forward()
  digDown()
  placeDown()
  digg()
end
end
function wall7()
digg()
for x=1, 7, 1 do
  turtle.forward()
  digDown()
  placeDown()
  digg()
end
end
function layer()
turtle.up()
wall9()
turtle.turnRight()
wall8()
turtle.turnRight()
wall8()
turtle.turnRight()
wall7()
turtle.forward()
turtle.turnRight()
turtle.back()
print("Layer completed")
end

function ceiling()
turtle.select(5)
turtle.forward()
ceilingRow()
rightTurn()
ceilingRowGlass()
leftTurn()
ceilingRowGlass()
rightTurn()
ceilingRow()
leftTurn()
ceilingRowGlass()
rightTurn()
ceilingRowGlass()
leftTurn()
ceilingRow()
print("Ceiling Completed")
end
function ceilingRow()
for x=1, 7, 1 do
  turtle.placeDown()
  if x==7 then
   break end
  turtle.forward()
end
end
function ceilingRowGlass()
turtle.placeDown()
turtle.forward()
turtle.select(6)
for x=1, 2, 1 do
  turtle.placeDown()
  turtle.forward()
end
turtle.select(5)
turtle.placeDown()
turtle.forward()
turtle.select(6)
for x=1, 2, 1 do
  turtle.placeDown()
  turtle.forward()
end
turtle.select(5)
turtle.placeDown()
end
function floor()
turtle.select(4)
turtle.forward()
turtle.forward()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
for x=1, 5, 1 do
  turtle.down()
end
for x=1, 3, 1 do
  floorRow()
  rightTurn()
  floorRow()
  leftTurn()
end
floorRow()
for x=1, 5, 1 do
  turtle.up()
end
turtle.forward()
turtle.turnLeft()
turtle.turnLeft()
print("Floor Completed")
end
function floorRow()
for x=1, 7, 1 do
  digDown()
  turtle.placeDown()
  turtle.forward()
end
end
function rightTurn()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
end
function leftTurn()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
end
function digg()
while turtle.detect() do
  turtle.dig()
  sleep(.4)
end
end
function digDown()
if turtle.detectDown() then
  turtle.digDown() end
end
function detect()
turtle.select(slot)
if turtle.getItemCount(slot) == 0 then
  slot = slot + 1
end
end
function place()
detect()
turtle.place()
end
function placeDown()
detect()
turtle.placeDown()
end
function goHome()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.forward()
for x=1, 5, 1 do
  turtle.down()
end
print("Arriving at starting point")
end
for x=1, 5, 1 do
layer()
end
floor()
ceiling()
goHome()
print("---9x9 Completed---")
BigSHinyToys #2
Posted 15 May 2012 - 04:32 AM
OK so there is a slight problem. Any object in the way of the turtle causes the program to get messed up and do some crazy stuff.
all turtle commands return true if they worked and false if they didn't. might want to write that in.

ow and a picture of the completed house would probably increase the amount of downloads as most people go by the motto "pics or it didn't happen"
ccrraazzyyman #3
Posted 15 May 2012 - 08:03 PM
OK so there is a slight problem. Any object in the way of the turtle causes the program to get messed up and do some crazy stuff.
all turtle commands return true if they worked and false if they didn't. might want to write that in.

ow and a picture of the completed house would probably increase the amount of downloads as most people go by the motto "pics or it didn't happen"

I'm not sure what you mean? Can you give me an example code piece so I can rework mine?
BigSHinyToys #4
Posted 16 May 2012 - 08:29 AM
in this code turtle.forward() will return true if it made the turtle move and will return false if it did not. the program will print weather this worked or not. This can be used to check the turtle did what you wanted it to.
for more in depth example you could look at
Function Turtle Move (with position tracing) in my topic
http://www.computercraft.info/forums2/index.php?/topic/1729-programs-big-shiny-toys/


if turtle.forward() then
print("Turtle Moved")
else
print("ERROR turtle didn't move ")
end
Luanub #5
Posted 16 May 2012 - 08:39 AM
What I do is simply this, it will wait incase its a player/mob infront of it as well as dig in case an unexpected block gets in the way


while not turtle.forward() do
sleep(.5)
turtle.dig()
end
ccrraazzyyman #6
Posted 16 May 2012 - 09:44 AM
What I do is simply this, it will wait incase its a player/mob infront of it as well as dig in case an unexpected block gets in the way


while not turtle.forward() do
sleep(.5)
turtle.dig()
end

Thanks. That looks amazingly easy to use and exactly what the problem is. I have a while dig sleep for detecting blocks, didn't know it could work for moving as well. Looks like I need to rewrite all my turtle code :P/>/>
Luanub #7
Posted 16 May 2012 - 11:07 AM
Just make it a function, then replace all turtle.foward()'s with that function. Should make implementing it fairly painless.
PixelToast #8
Posted 16 May 2012 - 03:28 PM
why not just do while true do turtle.forward() end
it will prevent it from messing up completely
Luanub #9
Posted 16 May 2012 - 08:57 PM
Thats not going to remove any obstructions and you may get a failure to yield error. Not only that if nothing blocked the turtle it would keep going forward and not stop since that is an infinite loop. The sleep and dig are essential, especially if you run into sand or gravel.

You could add some arguments so it will take x number of steps forward so you can replace multiple turtle.forwards() with one function call here an example.


function fwd( steps )
if not steps then steps = 1 end -- if nothing entered only take 1 step
for x=1, steps do
while not turtle.forward() do
sleep(.5)
turtle.dig()
end
end


fwd( 3 ) -- 3 steps forward
ccrraazzyyman #10
Posted 17 May 2012 - 04:07 AM
Thats not going to remove any obstructions and you may get a failure to yield error. Not only that if nothing blocked the turtle it would keep going forward and not stop since that is an infinite loop. The sleep and dig are essential, especially if you run into sand or gravel.

You could add some arguments so it will take x number of steps forward so you can replace multiple turtle.forwards() with one function call here an example.


function fwd( steps )
if not steps then steps = 1 end -- if nothing entered only take 1 step
for x=1, steps do
while not turtle.forward() do
sleep(.5)
turtle.dig()
end
end


fwd( 3 ) -- 3 steps forward

I have accounted for and testes this program against gravel and sand. I use the following code to handle gravel falling in front of the turtle. I like the function idea and it will make it painless to implement. I just need to find time between all this Diablo 3 :P/>/>
function digg() while turtle.detect() do   turtle.dig()   sleep(.4) end
Luanub #11
Posted 17 May 2012 - 04:27 AM
Thats not going to remove any obstructions and you may get a failure to yield error. Not only that if nothing blocked the turtle it would keep going forward and not stop since that is an infinite loop. The sleep and dig are essential, especially if you run into sand or gravel.

You could add some arguments so it will take x number of steps forward so you can replace multiple turtle.forwards() with one function call here an example.


function fwd( steps )
if not steps then steps = 1 end -- if nothing entered only take 1 step
for x=1, steps do
while not turtle.forward() do
sleep(.5)
turtle.dig()
end
end


fwd( 3 ) -- 3 steps forward

I have accounted for and testes this program against gravel and sand. I use the following code to handle gravel falling in front of the turtle. I like the function idea and it will make it painless to implement. I just need to find time between all this Diablo 3 :P/>/>
function digg() while turtle.detect() do   turtle.dig()   sleep(.4) end

If you create a forward fuction and implement the sleep and dig your digg function will be obsolete as the forward function should remove any obtructions. It should always report false for the detect. However its not a bad thing to have some redundancy to ensure your turtle is moving exactly to where you want it. I have some redundancy built into my turtle programs just for that reason.
slango20 #12
Posted 10 October 2012 - 12:08 AM
Speaking of avoiding sand and gravel, any way we can get it to try and attack an entity in front of it?
ScruffyRules #13
Posted 10 October 2012 - 12:55 PM
turtle.attack() and turtle.attackUp() and turtle.attackDown() You're Welcome! (Link for the page i found this from)
slango20 #14
Posted 11 October 2012 - 12:44 AM
turtle.attack() and turtle.attackUp() and turtle.attackDown() You're Welcome! (Link for the page i found this from)
thank you, that works great, now it will attack any zombie that is dumb enough to get in its way, feisty turtle
sci4me #15
Posted 04 September 2013 - 05:16 PM
thanks for this
Mitchfizz05 #16
Posted 05 September 2013 - 10:10 AM
Looks great! (I love 9x9s!)
However, one thing…
Your functions are being declared globally.
What I recommend is that you declare your functions like this:
local function funcName()

This is just a general LUA coding convention and isn't exactly required, but is recommended.