Hi
Yes its just to help me get started ( everyone has to start somewhere i guess )
Now i realise the capitals issue, i can progress.
Couple of questions...
1 is it easier to construct a working tree farm, then write a program to get the turtle to make the same, but obviously automatically.....wondered if it might be easier to have a template first.
2 i hear there is a way of creating a 3D map of where the turtle is during a build....could this be used to send the turtle to a certain location within the construction .. Also is there a piece of code i could ' borrow' to see how this works....
Thanks
I'd think writing a program to build a tree farm from nothing would be a lot easier than writing a program that attempts to scan an existing design. Remember that turtles can only "see" the block immediately in front, above, and below them. And to make matters even more difficult, a turtle can't "see" arbitrary blocks, it can only compare blocks around it to block types in its inventory. Because of this, a scanning program would only be able to identify 16 kinds of blocks; one block type for each turtle inventory slot.
You can use ComputerCraft GPS to help you keep track of where a turtle is, but for your tree farm build it's hardly necessary; relative positioning will do. At program start you'll want to initialize your X, Y, and Z co-ordinates and the direction you are facing. Remember, these will be relative to the start position, not actual Minecraft world position and direction values.
Code:
-- Program start
myX = 0
myY = 0
myZ = 0
facing = 0
As the turtle moves, you need to appropriately update the current relative position of the turtle. Remember that the turtle API will return "true" or "false" if the movement command was successful; don't update the relative positions unless the move was successful. The easiest is the Y co-ordinate since it is not dependent on the direction the turtle is facing.
Code:
...
if turtle.up() then myY = myY + 1 end
...
if turtle.down() then myY = myY - 1 end
Because you'll need to know the direction the turtle is facing to accurately update your relative X & Z co-ordinates, you'll want to write your own "turnLeft" and "turnRight" routines which will use the turtle API but also update the relative facing
Code:
function myTurnRight()
turtle.turnRight() -- According to the API doc, this can't fail
facing = (facing + 1) % 4 -- facing will always be 0 to 3
end --myTurnRight()
Do something similar for turnLeft, but in that case you'll need to subtract 1 from facing and deal with the fact that it can go from 0 to -1.
As long as you know your facing you can update your X & Z:
Code:
if turtle.forward() then
if facing == 0 then
myX = myX + 1
elseif facing == 2 then
myX = myX - 1
elseif facing == 1 then
myZ = myZ + 1
else -- facing must be 3
myZ = myZ - 1
end
end
You can use the relative co-ordinates and facing values to put your turtle exactly where you want it to be for your build. Let's say you want the turtle to be 10 blocks forward and 20 blocks right of the starting position. We decided that when "facing" is 0, the turtle is facing in the original placement direction and that X will increase as the turtle moves in that direction.
Code:
-- Note that this won't properly position the turtle if
-- it's already at an X that's more than 10
while myX <= 10 do
myForward()
end
myTurnRight()
-- Note that this won't properly position the turtle if
-- it's already at an Z that's more than 20
while myZ <= 20 do
myForward()
end
Most importantly: create functions for these various operations so your actual code is more compact, readable, and robust.
Edit: typo correction