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

Turtle Lava Caster

Started by KyStyle, 28 December 2016 - 04:39 AM
KyStyle #1
Posted 28 December 2016 - 05:39 AM
This is my first program! Woo!

What this program does is generate a massive cobblestone mountain by lava casting. If you haven't heard of lava casting, it is where you place lava down and then pour water on top of it to create cobblestone structures. Here are some pictures of the script in action:

Just starting:


Near Finished:


If you want to do this, make sure you are far out of the way of any important buildings.
The only things you need are:
- The Turtle (Of course)
- Fuel
- Lava Bucket in Slot 1
- Water Bucket in Slot 2
- The program, which you can get here:
pastebin.com/9xGP95Cd

Have fun!
hermanoid #2
Posted 04 January 2017 - 11:16 PM
That's a cool program. One thing I might suggest:

In your raw code, you repeat this:


turtle.up()
turtle.select(1)
turtle.placeDown()
sleep(7.5)
turtle.placeDown()
turtle.up()
turtle.select(2)
turtle.placeDown()
sleep(5)


Over and over again to accomplish the desired height. The only change between repetitions seems to be the amount of time you wait for the lava to go down.

This could all be very easily compressed into a loop:


for i=0,16 do:
	turtle.up()
	turtle.select(1)
	turtle.placeDown()
	sleep(7.5)
	turtle.placeDown()
	turtle.up()
	turtle.select(2)
	turtle.placeDown()
	sleep(5)
end

- Which will repeat the placing process 16 times. If that syntax is a little scary, you can read on it here.

Of course, that doesn't increase the time it waits each time. That's where multiplication and the "i" variable would come in handy:

lavaSecondMultiplier = 5 --The amount you want the total seconds waited for lava to go up each time the loop repeats (each loop results in two blocks climbed, so this should be decent, if not on the low side.  Experimentation is needed.
for i=1,16 do:
	turtle.up()
	turtle.select(1)
	turtle.placeDown()
	sleep(lavaSecondMultiplier*i)  --Multiply the Seconds Increase per repetition (two blocks climbed) by the number of repetitions the loop has gone through, represented by "i"
	turtle.placeDown()
	turtle.up()
	turtle.select(2)
	turtle.placeDown()
	sleep(5)
end

Loops are much prettier, cleaner, and, should you want to make another thing happen during the cycle (say, for whatever reason, place a cobblestone to the turtle's right) you only have to add that functionality once, not repeat over and over again. This is cleaner, quicker, and less likely to result in a mistake. The last benefit of loops is that they take up significantly less space and are easier to read. Less space, of course, means faster downloads.

The last possible feature you may want is for the user to select how many times to repeat the casting process, This can be determined in several ways, but the easiest are these two:

print("How many times should I cast?  (One cast=two blocks of height)")
repetitions = tonumber(io.read())
or

args = {...}
reptitions = tonumber(args[1])

The first one asks how many casts the person using the program wants, then transforms his reply (accessed via io.read(). This returns a string, a.k.a. a series of characters, of what the user typed in before pressing enter) into number form (strings aren't numbers! However, we know that the user is supposed to enter a number as a string, such as "1","2". and "0" for 120, so we can use what the user types in as a number by turning it into one with
tonumber()
). Of course, the user may neglect to enter a number, in which case
tonumber()
returns nil, for nothing. However, we can use this to verify the user entered a number easily like this:

reptitions = tonumber(io.read())
if not stuff then
	print("That's not a number!!!")
end

Once again, though, comes a trouble. If the user accidentally typed in a not-number, they will likely want to try again. We can't just slip another [CODE}tonumber(io.read(){/CODE] in, because that would be bad programming form. Also. we can't use a for loop, because we don't know exactly how many times it will take the user to get a number typed in. Luckily, Lua provides a different loop that keeps repeating until a boolean value (true/false, also known as yes/no) is false: The while loop. That can be used like this:

while not repetitions do
  io.write("How many times should I cast?  (One cast=two blocks of height)")
  repetitions = tonumber (io.read())
  if not stuff then print ("You done goofed!") end
end

Which will continue nagging the user until repetitions is not nil, which happens when the user entered a valid number.

The second option is used a little differently.
 args 
is filled with everything the user typed in when starting the program (represented by …) as a list. We can get the first item in that list with
args[1]
. Then, we tonumber() it into the number form we need. This means we can set the number of casts we want without having to participate in code-dialog; instead, we just call it with the program. like this:

LavaCast 16

(Where LavaCast is whatever your program is named). 16 is passed on as the first item in the list of arguments. For example, if we were to pass another number after 16, like this:

LavaCast 16 432

432 wouldn't be used, because it is the second item in the argument list. it still can be accessed, but by
args[2]
.

However, our favorite old problem of the user typing in not-numbers comes up again. If the user replaced "16" with "Hello", the code I supplied wouldn't work. The for loop would need to loop from 1 to nil, which is impossible, so we'd get an error if a not-number was supplied. Now, we have to go back to asking the user directly. So, to conclude, we we need to use both methods together:

args = {...}
reptitions = tonumber(args[1])
while not repetitions do
  io.write("How many times should I cast?  (One cast=two blocks of height)")
  repetitions = tonumber (io.read())
  if not stuff then print ("You done goofed!") end
end

Now that we have our number, we can pass into the loop as follows:


for i=1,repetitions do:
	turtle.up()
	turtle.select(1)
	turtle.placeDown()
	sleep(lavaSecondMultiplier*i)  --Multiply the Seconds Increase per repetition (where two blocks is climbed) by the number of repetitions the loop has gone through, represented by "i"
	turtle.placeDown()
	turtle.up()
	turtle.select(2)
	turtle.placeDown()
	sleep(5)
end


I hope this helps you on your learning process. (I'm in that process too!)
The Hermanoid

P.S.
Sorry for the essay of a reply… I get a little long-winded sometimes, especially when I'm procrastinating for a certain English assignment due tomorrow…
KyStyle #3
Posted 24 February 2017 - 08:05 AM
I hope this helps you on your learning process. (I'm in that process too!) The Hermanoid P.S. Sorry for the essay of a reply… I get a little long-winded sometimes, especially when I'm procrastinating for a certain English assignment due tomorrow…

That has really helped, thanks!
And you don't need to be sorry about the essays worth of a reply. You provided so much useful information, there isn't anything to be sorry about for that.
Thanks again, I hope your English essay went well :)/>

I'm now working on a turtle mining software with a GUI and functionality to place torches, mine for a specified distance, dig 2 high and 3 wide or 3 high and 3 wide and also return back once finished. All these are customizable! :D/>

Thanks again for your reply.
~ KyStyle