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

GPS-less movement and Cactus Farm

Started by MundoDaLua, 26 November 2012 - 12:44 PM
MundoDaLua #1
Posted 26 November 2012 - 01:44 PM
Hello everyone! This is my first post, and I'm not a programmer, but I did my best to make something that is useful, instead of posting a "hack safe lock" for the millionth time.
I never liked the idea of making GPS towers, so I created a set of programs that let the turtle know it's own position without them:

First programs to load:
SavePos: Program that saves the current position into a PosFile. Internal use, do not use it directly, use the program DefinePos instead.
Link: http://pastebin.com/LC5AsnEH

DefinePos: The first program you should use. Pass 4 arguments to it: Xposition, YPosition, ZPosition and Facing, in this order. That means the turtle's current X,Y and Z position, and the direction which the turtle is facing (pass the number, from 0 to 3, not the cardinal direction). Just to remember, you can get these information, including the facing number, by pressing F3. After you define the position of the turtle for the first time, you won't need to do that again, even if you reboot the turtle or quit the game.
Link: http://pastebin.com/unxvuVnp

LoadPos: Program that reads the PosFile and loads the information into 4 global variables: x, y,z and facing. Note that, since these variables are global, not local, if you use them in other programs you might get a conflict. I suggest you run this program from startup, so your turtle always know it's own position, even after a reboot.
Link: http://pastebin.com/HeQZkZ6L

DefineHome: This program is only necessary if you're going to use the GoHome program, which is a specialized version of the GoTo. Pass as arguments: homeXPosition, homeYPosition, homeZPosition (differently from DefinePos, you don't need to pass a Facing direction as argument)
Link: http://pastebin.com/nr1ErtQp

Movement programs:
Forward - Link: http://pastebin.com/CR9X9DX8
Back - Link: http://pastebin.com/Zfi7gRAJ
Up - Link: http://pastebin.com/CWL8qHbd
Down - Link: http://pastebin.com/YCsnSLR8
TurnRight - Link: http://pastebin.com/vJjasv8D
TurnLeft - Link: http://pastebin.com/78BQdFep

You can use all these programs without arguments, and it will only run once, or pass the number of times that you want the program to run as an argument. Use these programs instead of the built-in movement programs (Go, Turn, etc.), because they do not update the turtles position.

Specialized movement programs:
GoTo - Link: http://pastebin.com/04DC6yY1
GoHome - Link: http://pastebin.com/YPSvC5Jb
SetFacing - Link: http://pastebin.com/uYsXgTbK

When using GoTo, pass the TargetX, TargetY and TargetZ as arguments. GoHome doesn't take arguments (it loads the file created when using defineHome). When using SetFacing, pass the direction that you want the turtle to face (it's not just a program that turns right until it reaches the apropriate direction, it checks which direction it needs to turn and use turnRight or turnLeft appropriately. If it needs to turn to its back, it will use TurnRight twice).

Now a really cool program:
CactusFarm: A complex program that is completely reboot safe. That means that, once everything is set up, if you quit the game or reboot the turtle, when you run the program again, it will continue from where it left off. If you add this program to your startup, you can quit the game, come back as many times as you want without needing to check the turtle, resetting anything or typing any code. Well, you will need to add fuel to it, eventually…
Before running it, you need to use the DefinePos and DefineHome programs.
It runs in cycles, and the turtle's Home will be it's station, the place where it will wait for the beginning of the next cycle. Put a chest next to this position so the turtle can place the collected cacti. (When you run the program for the first time, it will ask the position of the chest relative to Home. That means the direction (again, in numbers, not the cardinal direction) the turtle would have to face in order to drop stuff on the chest).
The farm will be a rectangle, and it needs to be all in the same Y level (Farm Y). The bottom left square of the Farm will be the Initial block, which will be the closest one to Home. The top right square will be the final Block. The Farm can be any size you want.
F = Farm, H = home

……………….F..F..F..F<-Final Block
……………….F..F..F..F
…..InitialBlock->F..F..F..F
……………….
……………….H

The program will also ask how long the turtle has to wait before beginning the next cycle. I suggest 1100 seconds, it seemed to be a good interval for me, but feel free to set the interval that you want.
After you have set these variables, the program is set. If you run this program from startup, it will work indefinitely, even if you quit the game and come back. It will show you it's current state, fuel level and you can type orders. Right now, the only orders avaiable are quit and resetFarm (it will quit the program and, the next time you run it, it will be like the first time you ran it, it will ask you to set all the farm variables).
You need to have all the programs above before you can use this program.
Link: http://pastebin.com/qRXVv3Qc

Fell free to ask any doubts. I know it's a lot of programs to load, but I didn't know an easier way to make them available. Feedback is appreciated.
Alekso56 #2
Posted 27 November 2012 - 10:55 PM
I packed the links togheter in a install script;

http/pastebin get 2Pkh5igz install




(http://pastebin.com/2Pkh5igz)
MundoDaLua #3
Posted 28 November 2012 - 04:42 AM
Thanks!