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

Do - The ultimate turtle usage program

Started by jerimo, 07 March 2015 - 01:04 PM
jerimo #1
Posted 07 March 2015 - 02:04 PM
Ever wished you could simply write the following into a turtle, and it would just… work?
do forward 3 ( mineshaft forward 3 ) 5 turnLeft 2 forward 18 drop
this sample would go forward 3 blocks, then dig five mineshafts to bedrock 3 blocks apart from each other, turn around, go back to where it started and drop all it's contents in front of it at your feet.

Of even this?
do set HouseBuilder forward ( set BuildUp up ( set BuildCorner ( set BuildWall placeDown forward ) 4 placeDown turnRight ) 4 ) 3 turnRight forward turnLeft ( set BuildRoof ( placeDown forward ) 4 turnRight forward turnRight ( placeDown forward ) 4 turnLeft forward turnLeft ( placeDown forward ) 4 turnLeft forward 3 turnLeft forward 5 down 3 )
to build a 5 * 5 house ahead and to the right of where the turtle currently is, then head back to where it used to be?

You use your turtles a lot and wish to know what they are currently up to?
Just name the programs and you can know which step it currently is at.
To see just how powerful this new feature is, lets see exactly how your scripts need to be altered, using the above example!
do set Placement forward 3 ( set Mining mineshaft forward 3 ) 5 turnLeft 2 forward 18 drop
While this code is running, the turtle would declare that it is in the "Placement" phase while not in the inner program, and mention that it is currently mining when in the sub-program.

Everything is made simple for you, just type in do and it will do any turtle command or run any program on the turtles internal memory! It works with the turtle API straight out of the box, but best of all, integrating it with your own api takes a few seconds; simply go to line 120 and add in the location of your API in quotation marks and you are done! (Ex: "rom/apis/turtle/turtle" )

Guide
SpoilerBegin your commands with do then any lua option under the loaded API can be used.
To use the sub-programs( the arguments in "("s and ")"s) there must be a space before and after the parenthesis
To be able to call programs on the turtle, the name may not be a number, nor match the name of a function in the loaded API

loadAPIs("rom/apis/turtle/turtle")
Once this is done, do is set to go; simply type do and create your movement patterns and it will execute them.
To test out that all is up and running, use the following program, changing to your own API functions, to make a 3 * 3 platform:
do set Platform ( ( placeDown forward turnRight ) 4 forward ) 4

Advanced guide:
Spoiler

Use in user created programs:
Load up your API using the method used above, then you are ready to begin using the program.
The runProgram() method is accessible when loaded as an API, its use is fairly straight forward: You can either make a list of all the arguments in the program exactly as if you had entered it into the command line, or you can pre-"compile" the code to be run, for this use case see the section on "compiling" the programs.
Once the code to be run is made into a list (a table) use the runProgram. This example assumes that the table is called "table", and the number of times to run is a variable called "timesToRun"
runProgram(table, timesToRun)

"Compiling" the code:
The term compiling is used fairly loosely in this case, but will save the program some time, as it doesnt require to relist subprograms, hence the use of the term compile; now that this is out of the way lets begin.
When the programs run, if they encounter a "(" it will make a list from the starting parenthesis to it's matching ending parenthesis exclusively, to then run the list the appropriate amount of times. If you are aiming to make the use of this program as efficient as possible, it is possible to save a good deal of time on this operation by pre-"compiling" the code into tables, omitting the use of "("s and ")"s
The runProgram method returns the "compiled" code once it has run, allowing for internal reuse of the pre-compiled code if it needs to be run multiple times, also allowing the user to save the code in it's "compiled" form if they so desire.

You are now a do expert!

Changelog
SpoilerV0.2 - Complete rewrite to be much more robust, and easier to use in user programs. Added in the "set" keyword allowing for named programs, helpful when looking for errors in scripts using this tool

If there are any questions please do bring them up, I'd be glad to help out

V0.2 - PasteBin - 1dUgMiH6

Obsoleted Versions:
SpoilerV0.1 PasteBin: vM0qBB7R
Edited on 10 March 2015 - 02:22 PM
jerimo #2
Posted 08 March 2015 - 12:31 AM
Planned features:
- exposed variables within the program itself, such as being able to know the loop count and such.
-custom variables in code, will likely be naturally implemented at the same time as exposing variables already in use
- flow tools, like if and while
- possibility to pass in arguments to programs run
- multiple APIs!

If you have any feature requests please do mention them!

Unstable build, used for development. Feel free to test it out and give feedback/bugs; Would be extremely useful actually! ;)/>
Unstable version: sCZjMM5h

Extra features:
- Multiple API load is working now

Next feature:
- Variables
Edited on 10 March 2015 - 06:41 PM
Chaos_Therum #3
Posted 09 March 2015 - 08:29 AM
Ever wished you could simply write the following into a turtle, and it would just… work?
do forward 3 ( mineshaft forward 3 ) 5 turnLeft 2 forward 18 drop
this sample would go forward 3 blocks, then dig five mineshafts to bedrock 3 blocks apart from each other, turn around, go back to where it started and drop all it's contents in front of it at your feet.

Everything is made simple for you, just type in do and it will do any turtle command or run any program on the turtles internal memory!
And best of all, integrating it with your own api takes under a minute; simply load your API at the begining of the program, and change the two references to turtle in the code, and you are done!

Guide
SpoilerBegin your commands with do then any lua option under the loaded API can be used.
To use the sub-programs( the arguments in "("s and ")"s) there must be a space before and after the parenthesis
To be able to call programs on the turtle, the name may not be a number, nor match the name of a function in the loaded API

You are now a do expert

It's a short post, but shouldn't that just prove how easy to it to use?

If there are any questions please do bring them up, I'd be glad to help out

PasteBin: vM0qBB7R

This looks pretty promising.
jerimo #4
Posted 10 March 2015 - 03:03 AM
– Original Post –

This looks pretty promising.
Thanks! Reworking it so that it can more easily be used in other programs as well as fully functional parentheses (found a bug in them in the previous version)
Hoping that the new version will be up tonight, worked a lot on it today.

The next step will be allowing multiple API checks!
jerimo #5
Posted 10 March 2015 - 05:57 AM
New version posted, see the OP for details.
Also a much needed expansion of the post to show off some of the features and how to use it in the command line as well as in programs
jerimo #6
Posted 10 March 2015 - 07:37 PM
Added the unstable build link in the second post, added features, and should work fine, but haven't run the code yet
jerimo #7
Posted 15 March 2015 - 03:14 PM
Has anyone tried it yet?