Posted 25 July 2013 - 11:14 PM
!!! WARNING !!!
Note: I have written this tutorial from my final code source (see pastebin below) and broke it up in steps retroactively
I cannot test each part of the code for now, but I will do as soon as I get back home tonight and will edit any error found.
If you experiment any error, post a comment and I will have a look in the meantime. (it should be alright though! Enjoy)
Introduction
Hello! I am pretty new to both the forum and Computer Craft but I would like share with you my first script as a tutorial for beginners (like me).
It is quite basic in both logic and usage but I do think it can be useful (I use it to build my base in survival!).
You can find the source code on my Pastebin (here) and a video on Youtube (here) which demonstrate the result (in creative mode)
Please note that this script/tutorial is intended for real beginners and can be improved in a lot of ways (which is planned), do not hesitate to comment or message me if you see something wrong or which can be improved (I wish to keep this tutorial for beginners however).
Context
I always say that we - coders - should code things which are useful for us, and that's a really good motivation to keep going on and it makes life easier.
My current Minecraft base (Playing on Feed the Beast Ultimate) is made from a Turtle 'excavation' from the surface (~70m) to the bedrock with dimensions of 16x16.
Making a base from a big hole on the ground can generate some problems:
Feature Requirements
At this stage we know that we want our turtle to automatically place provided blocks to make a floor, which can look pretty basic/easy to do..
But! There are some things to take in consideration:
As we will write this program step by step I strongly advice to setup a test area to avoid losing resources, turtle, time, etc.
To get a better idea of the recomended setup, have a look to the Youtube video (link at the top of the tutorial).
Basically create a square (or rectangular) hole, I would advise to not go deeper than one or two layers deep as you will have to reset the area after each run:
You also need to 'simulate' walls for your floor to define the area limits, a little bit like the landmarks for the Quarry, it's the green blocks in the video, so you need at least the following:
GBGGGG
GHHHHHB
GHHHHHG
GHHHHHG
GHHHHHG
BTHHHHG
GGGGBG
Where:
It should look like that:
[attachment=1252:test-area-01.png]
[attachment=1253:test-area-02.png]
Note: make sure your Turtle is facing the right way (- or it will go wild!):
Script Coding
First of all, we need to create a new program, so right click on your favourite turtle (and give her a label! 'label set paupiette') and type the following command in the shell:
From here, you have two options:
Anyway, whatever the way you do it, just do it :)/>
Step1: Script Base
Let's write the base of our script:
I can hear you crying: What, my turtle only count until 4 and tell me the job is done, where is my floor?
Be patient, this is only the first step to make a brand new floor ;)/>
What we have done here is to setup the base:
Let's make this turtle move:
Note: do not forget to put fuel in the turtle's inventory (in slot 1: top-left)
So, now everything makes more sense!
And my turtle is now moving following the walls to come back to its original spot, well done!
With this code update, we have:
Step 3: The first round!
So now that our turtle is following the wall let's make it work for us:
Before running this program, add some cobblestone (or any other solid block) in the bottom-right slot of the turtle's inventory - put one stack (minimum: 16 blocks)
Some new code from the Turtle API here, let me explain:
[indent=1]Noticed the code?[/indent]
[indent=1]
[indent=1]It allow use to manage if something go wrong, ie. the turtle cannot place a block here for any reason (in a future version)[/indent]
If everything is done properly, your turtle should now go all around the room and place a block all around below her.
That's nice, but it's not a proper floor, and what if one stack of block is not enough??
Step 4: I want more stone!
In this step we will finish the floor (expecting a 5x5 hole!), for now one stack of stone is more than enough (5x5 = 25 blocks used).
On top of detecting walls, our turtle now detects where she already placed a block previously with turtle.detectDown() and start a new row by moving backward (turtle.back()), turning right and starts again.
Now you should have a proper floor for your 5x5 hole, which is nice (I guess?) but..
Remember my base is made from a 16x16 hole which means one floor is 256 blocks, wait a minute, it's more than one stack, it's not gonna work!
Step 5: Basic Multi-Stack Management
Here is how I do manage this problem in my current program:
Here we go!
Now our turtle will automatically switch to the next slot if the current one is empty, unless there is really no more available slots.
Having said that, it is still 15 stacks = 960 blocks, enough to fill a 30x30 hole, which should cover your immedate needs.
Conclusion and possible improvements
This will conclude this quick tutorial - not so quick to write though.
We have seen how to automatically move a turtle based on the surrounding environment (you can use that for a lot of scenarii!) which keep the algorithm simple, no coordinates, direction, depth, etc to manage.
We have also seen some other basic feature of the turtle like how to place and detect block, select an item slot.
Let's be honest this is really basic stuff, and a lot of improvement are possible, here are some:
If you want an exercice to test your new knowledge and go to the next step (I think) try to program the following:
[indent=1]Write a program called 'rewall' which do the following:[/indent]
Go even further with this exercice:
That's it, I hope you enjoyed the tutorial, I have tried to make it the simplest (but useful) as possible and hope it will help at least one person :D/>
As said previously any comment/opinion/improvements are more than welcome.
Statistics:
Note: I have written this tutorial from my final code source (see pastebin below) and broke it up in steps retroactively
I cannot test each part of the code for now, but I will do as soon as I get back home tonight and will edit any error found.
If you experiment any error, post a comment and I will have a look in the meantime. (it should be alright though! Enjoy)
Introduction
Hello! I am pretty new to both the forum and Computer Craft but I would like share with you my first script as a tutorial for beginners (like me).
It is quite basic in both logic and usage but I do think it can be useful (I use it to build my base in survival!).
You can find the source code on my Pastebin (here) and a video on Youtube (here) which demonstrate the result (in creative mode)
Please note that this script/tutorial is intended for real beginners and can be improved in a lot of ways (which is planned), do not hesitate to comment or message me if you see something wrong or which can be improved (I wish to keep this tutorial for beginners however).
Context
I always say that we - coders - should code things which are useful for us, and that's a really good motivation to keep going on and it makes life easier.
My current Minecraft base (Playing on Feed the Beast Ultimate) is made from a Turtle 'excavation' from the surface (~70m) to the bedrock with dimensions of 16x16.
Making a base from a big hole on the ground can generate some problems:
- I have to manually build floors for each level of my base which can be boring,
- Making those floors can be quite dangerous (trust me I experienced the 50m fall few times before discovering the sneaking command :)/> )
- It is time consuming and I cannot take care of my chickens in the meantime :(/>
Feature Requirements
At this stage we know that we want our turtle to automatically place provided blocks to make a floor, which can look pretty basic/easy to do..
But! There are some things to take in consideration:
- How do my Turtle knows where to go?
- How do my Turtle knows the job is done?
- What if one stack of Cobblestone (or any other block) isn't enough for the whole floor? (16x16 hole = 256 = 4 stacks blocks to place!)
As we will write this program step by step I strongly advice to setup a test area to avoid losing resources, turtle, time, etc.
To get a better idea of the recomended setup, have a look to the Youtube video (link at the top of the tutorial).
Basically create a square (or rectangular) hole, I would advise to not go deeper than one or two layers deep as you will have to reset the area after each run:
You also need to 'simulate' walls for your floor to define the area limits, a little bit like the landmarks for the Quarry, it's the green blocks in the video, so you need at least the following:
GBGGGG
GHHHHHB
GHHHHHG
GHHHHHG
GHHHHHG
BTHHHHG
GGGGBG
Where:
- 'G' is for Grass (or any other block, it's outside - doesn't matter) it's depth = 0 (ground level)
- 'B' is for Block (or blocker) it has to be at depth +1 (above ground level, same as the Turtle)
- 'H' is for Hole it has to be at least depth -1 (-2 is fine as well for test)
- 'T' is your Turtle it has to be placed at depth +1 (ground level, above the first piece of hole)
It should look like that:
[attachment=1252:test-area-01.png]
[attachment=1253:test-area-02.png]
Note: make sure your Turtle is facing the right way (- or it will go wild!):
- she (I say she because in French 'turtle' is a female word ;)/>) has to face the hole,
- the tool (whichever it is) has to be on the left side of the turtle (next to the blocker)
Script Coding
First of all, we need to create a new program, so right click on your favourite turtle (and give her a label! 'label set paupiette') and type the following command in the shell:
edit mkfloor
From here, you have two options:
- Write your program directly from inside the game (can be long)
- Edit the mkfloor file from your computer (outside the game)
Anyway, whatever the way you do it, just do it :)/>
Step1: Script Base
Let's write the base of our script:
local tArgs={ ... }
print("Making a brand new floor for you master..")
-- flag for job done
local finished=false
-- counter to stop the turtle (temporary)
local counter=0
-- make the turtle work hard until the job is done
while not finished do
-- increment the counter
counter=counter+1
print("Turtle say: " .. counter)
-- consider the job done if counter reach 4
if counter == 4 then
finished=true
end
end
I can hear you crying: What, my turtle only count until 4 and tell me the job is done, where is my floor?
Be patient, this is only the first step to make a brand new floor ;)/>
What we have done here is to setup the base:
- Define a flag to tell the turtle the job is done (finished)
- Define a counter as an exit condition (counter)
- Loop 4 times and consider the job is done
Let's make this turtle move:
Note: do not forget to put fuel in the turtle's inventory (in slot 1: top-left)
local tArgs={ ... }
print("Making a brand new floor for you master..")
-- feed the turtle with coal
turtle.refuel()
local counter=0
local finished=false
while not finished do
-- move forward until we hit a wall
while not turtle.detect() do
turtle.forward()
end
-- when we hit the wall turn right
turtle.turnRight()
counter=counter+1
print("Turtle say: bump!")
-- finish the job when hitting 4 walls (back to initial spot)
if counter == 4 then
finished=true
print("Hey master, I'm done!")
end
end
So, now everything makes more sense!
And my turtle is now moving following the walls to come back to its original spot, well done!
With this code update, we have:
- Fed our turtle with coal (turtle.refuel())
- Added wall detection to our logic (turtle.detect())
- Make the turtle turn on the right when hitting a wall (turtle.turnRight())
Step 3: The first round!
So now that our turtle is following the wall let's make it work for us:
Before running this program, add some cobblestone (or any other solid block) in the bottom-right slot of the turtle's inventory - put one stack (minimum: 16 blocks)
local tArgs={ ... }
print("Making a brand new floor for you master..")
turtle.refuel()
local counter=0
local finished=false
-- add a counter for consumed blocks
local total=0
-- variable for the inventory slot
local selected=16
while not finished do
while not (turtle.detect() or turtle.detectDown()) do
-- try to place a block below the turtle
if turtle.placeDown() then
-- increment the blocks counter
total=total+1
end
turtle.forward()
end
turtle.turnRight()
counter=counter+1
print("Turtle say: bump!")
if counter == 4 then
finished=true
print("Hey master, I'm done!")
end
end
Some new code from the Turtle API here, let me explain:
- We added a counter to count the number of consumed blocks (optional)
- We tell the turtle to use the slot 16 (bottom-right) to find the blocks to be used for the floor with turtle.select(16)
- Before moving forward the turtle tries to place a block below her: turtle.placeDown()
[indent=1]Noticed the code?[/indent]
[indent=1]
if turtle.placeDown() then
-- increment the blocks counter
total=total+1
end
[/indent][indent=1]It allow use to manage if something go wrong, ie. the turtle cannot place a block here for any reason (in a future version)[/indent]
If everything is done properly, your turtle should now go all around the room and place a block all around below her.
That's nice, but it's not a proper floor, and what if one stack of block is not enough??
Step 4: I want more stone!
In this step we will finish the floor (expecting a 5x5 hole!), for now one stack of stone is more than enough (5x5 = 25 blocks used).
local tArgs={ ... }
print("Making a brand new floor for you master..")
turtle.refuel()
local total=0
local selected=16
print("I've got "..turtle.getItemCount(selected).." blocks for now!")
local finished=false
while not finished do
while not (turtle.detect() or turtle.detectDown()) do
if turtle.placeDown() then
total=total+1
end
turtle.forward()
end
-- the turtle reached our floor (already visited that spot)
if turtle.detectDown() then
-- move back of 1 block
turtle.back()
-- turn on the right
turtle.turnRight()
-- and start a new row
turtle.forward()
-- hey we are done! we reached the previous spot
if turtle.detectDown() then
finished=true
print("Floor finished!")
end
else -- turtle hit a wall
turtle.turnRight()
print("Turtle say: bump!")
end
end
print(total .. " blocks have been consumed in the operation.")
On top of detecting walls, our turtle now detects where she already placed a block previously with turtle.detectDown() and start a new row by moving backward (turtle.back()), turning right and starts again.
Now you should have a proper floor for your 5x5 hole, which is nice (I guess?) but..
Remember my base is made from a 16x16 hole which means one floor is 256 blocks, wait a minute, it's more than one stack, it's not gonna work!
Step 5: Basic Multi-Stack Management
Here is how I do manage this problem in my current program:
local tArgs={ ... }
print("Making a brand new floor for you master..")
turtle.refuel()
local total=0
local selected=16
print("I've got "..turtle.getItemCount(selected).." blocks for now!")
local finished=false
while not finished do
while not (turtle.detect() or turtle.detectDown()) do
if turtle.placeDown() then
total=total+1
end
turtle.forward()
-- if there is no more block on that slot
if turtle.getItemCount(selected) == 0 then
-- decrement the slot number (from bottom-right to top-left)
selected=selected-1
-- do not consume coal (experimental not tested yet)
assert(selected ~= 1, "No more blocks left, sorry master")
-- select the new item slot
turtle.select(selected)
print("Switched to slot ".. selected .. " now having ".. turtle.getItemCount(selected) .." for use!")
end
end
if turtle.detectDown() then
turtle.back()
turtle.turnRight()
turtle.forward()
if turtle.detectDown() then
finished=true
print("Floor finished!")
end
else
turtle.turnRight()
print("Turtle say: bump!")
end
end
print(total .. " blocks have been consumed in the operation.")
Here we go!
Now our turtle will automatically switch to the next slot if the current one is empty, unless there is really no more available slots.
Having said that, it is still 15 stacks = 960 blocks, enough to fill a 30x30 hole, which should cover your immedate needs.
Conclusion and possible improvements
This will conclude this quick tutorial - not so quick to write though.
We have seen how to automatically move a turtle based on the surrounding environment (you can use that for a lot of scenarii!) which keep the algorithm simple, no coordinates, direction, depth, etc to manage.
We have also seen some other basic feature of the turtle like how to place and detect block, select an item slot.
Let's be honest this is really basic stuff, and a lot of improvement are possible, here are some:
- Manage more stacks for bigger size floor - with come back to a chest for instance
- Manage possible obstacles, for now the turtle will think it is a wall and start a new row from here
- Provide specific floor size like: mkfloor 4 12
- Provide a depth for the floor, ultimately for my base I would like floors of 3 depth thick (I run mkfloor 3 times for now) like: 'mkfloor 3', or 'mkfloor 4 12 3' (with size)
- Add wireless control/reports to not have to check every 5 minutes where is your turtle going
If you want an exercice to test your new knowledge and go to the next step (I think) try to program the following:
[indent=1]Write a program called 'rewall' which do the following:[/indent]
- Start with the turtle inside an empty room (let's say 6x6x6)
- Turtle should move along each wall and dig the wall for 1 depth (remove the external layer)
- Replace each block with the provided one (be careful with gravel/sand with the gravity aspect it may need to be dug multiple times!)
Go even further with this exercice:
- Manage already existing block of the same resource (i.e if you want to put cobblestone, do not dig cobblestone block to put a new one, just skip it!)
- Manage properly placed torches to avoid monster pop (detect the torch, take it off, dig the block, put new block, put back the torche)
- Automatically put a torche every 14 blocks (see above)
That's it, I hope you enjoyed the tutorial, I have tried to make it the simplest (but useful) as possible and hope it will help at least one person :D/>
As said previously any comment/opinion/improvements are more than welcome.
Statistics:
- Block used: 467
- Number of turtles escaped in the wild: 2
- Number of hours to write this tutorial: 3