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

Turtle Planting Program - Laua - Programming Illiterate User

Started by JaWriter, 04 January 2013 - 01:37 PM
JaWriter #1
Posted 04 January 2013 - 02:37 PM
Coding has been completed: Final coding is available at the bottom of this thread. *snuggle* Thanks Luna Nub!


***********************




Hi guys,

I appologise for asking for so much help, but I'm programing illiterate and have been trying to figure this stuff out for two days and I'm completely lost. A guy on my server says that If I can get some help writing the code he can help me load it into the turtle. I've done as much leg work as humanly possible to try and make sure that my request is accurate and as simple as possible (including walking around my field as if I was a turtle to trouble shoot it).

Sorry about the mess, and I appreciate it. I'm still trying to dive through the newbie stuff… but I am completely lost, but slowly starting to learn it.


Program Requirements:

Have an advanced solar powered Farming Turtle plant a 21x21 tree farm with a logger at a user defined timing interval.

The program I need help scripting:

Spoiler

variable [A] - Number of minutes - User defined at program startup

Function 1:

[1A] - Wait [A]
[1B] - Run Startup sequence
[1C] - Run Planting Sequence X3
[1D] - Run Center Movement Sequence X1
[1E] - Run Planting Sequence x3
[1F] - Run Return sequence
[1G] - Return to [1A]

********************
Start Up Sequence:
[2A] Fill invintory from chest below turtle
[2B] - Move Verticle 1
	 - Move Forward 1
	 - Turn Right

********************
Planting Sequence:
[3A] - Move forward 1
[3B] - Turn Left

[3C] - Move Forward 1 - Plant Tree below Turtle (x21)
[3D] - Move forward 1
	 - Turn Right
	 - Move Forward 1
	 - Turn Right
[3E] - Move Forward 1 - Plant Tree below Turtle (x21)
[3F] - Move Forward 1
	 - Turn Left

**********************
Center Movement Sequence

[4A] - Move forward 3

**********************
Return Sequence
[5A] - Turn Right
	 - Move Forward 1
	 - Turn Right
	 - Move Forward X 20
	 - Move Verticle negative 1
	 - Turn Right

ChunLing #2
Posted 04 January 2013 - 03:12 PM
So…you're not even aware of how to load a program into a turtle (or other CC computer)?

Okay…does your server have http enabled?
Loki #3
Posted 04 January 2013 - 03:16 PM
Wait. I'm lost. Are you asking us to write your code for you? Because if you study the official Lua reference manual and read a bit through the computercraft wiki then you will easily learn how to write this program. I suggest looking at the turtle API and then look at functions. :)/>
Also, code is done by doing [CODE1] LOLOLOL [/CODE1] Without the '1'.

LOLOLOL
JaWriter #4
Posted 04 January 2013 - 03:28 PM
So…you're not even aware of how to load a program into a turtle (or other CC computer)?

Okay…does your server have http enabled?

Not sure,

How do I check. It should though.

And you are correct: I don't know how to get a program on a turtle. I've been trying to find the computer craft tutorials, but I've tried a few and I can't seem to get my farming turtle to move forward one space….

Loki, thank you for the bbc coding tip. Which tutorials should I be looking at?
ChunLing #5
Posted 04 January 2013 - 04:12 PM
Okay, start with the default programs. See if you can use your turtle using the go program function. Just enter "go fd" into the shell.

I think the spoiler tag looks like
Spoiler[/spoiler].
Edited on 04 January 2013 - 03:13 PM
JaWriter #6
Posted 04 January 2013 - 04:18 PM
Okay, I can make the turtle turn left and right [ Turn left 1] ect.

But when I type in "go forward 1", the turtle freezes and I have to break / pick it up (Never mind, it won't go through torches).

So now we know that works… where is an example of a similar program I can cannibalize to try and make my program work? Or am I jumping the game a little?

But I can't seem to get these commands to work: http://computercraft.info/wiki/Turtle_(API)
ChunLing #7
Posted 04 January 2013 - 04:53 PM
You can run programs (files containing lua code) from the shell, but you cannot run lua functions directly from the shell.

If you are ready to use functions, then you want to use the lua program. Enter "lua" into the shell and you will get the lua prompt (and a message telling you to enter "exit()" to exit the lua program).

Once in the lua program, you can enter Lua chunks and the lua program will attempt to execute them, or display an error message telling you what went wrong. Entering an individual function call, expression, or value will cause the lua program to display the value of that (so entering "turtle.forward()" should move the turtle forward and show "true", or "false" if the turtle couldn't move forward for some reason).

In the future, use Ctrl+T to terminate a program or Ctrl+R to reboot a system, rather than breaking your turtle (which drops the inventory everywhere). But yeah, the basic go program is pretty primitive.

Once you are comfortable with basic functions and Lua code chunks, you can use the edit program to create your own text files. If the text in the file is valid Lua, then you can then run those programs by entering the file name in the shell. Enter "edit fileName" in the shell (where fileName is the name of the file you wish to edit). You will then be in the edit program. Type in the text of the file. Then press Ctrl (the left Ctrl key) to bring up the options to Save, Exit, or Print. Select Save, and enter. Press Ctrl again and select Exit. You can attempt to run the file you've created as a program by entering the file name in the shell. If it fails, then you will see an error much like you might have gotten while using the lua program.
Edited on 04 January 2013 - 04:04 PM
JaWriter #8
Posted 04 January 2013 - 05:10 PM
Okay, thanks for the the starter crash corse in getting into that part of the system. I'll see what I can write up and post results here. Hopefully someone else will be able to see it and learn this a whole lot faster than what I'm going through.

Thanks Chun.
JaWriter #9
Posted 04 January 2013 - 07:13 PM
Okay, got a program failure here:

bios: 338: [string "LogPlant"]:58: 'end' expected (to close 'function' at line 53)

here is the existing code:

Spoiler** Timer sequence / Hopefully infinate loop repeating once every hour, can adjust timing as needed by boiler **

while true do
funFullSequence()
os.startTimer(3600)

function funFullSequence() ** This is everything together **
funGoTime()
funPlantRow()
funPlantRow()
funPlantRow()
turtle.forward(3)
funPlantRow()
funPlantRow()
funPlantRow()
funGoHome()

function funGoTime() *** Startup ***
turtle.suckdown()
turtle.up()
turtle.forward()
Turtle.turnright()

Function funGoHome() *** Go home when done ***
turtle.turnright()
turtle.forward()
turtle.turnright()
turtle.forward(21)
turtle.down()
turtle.turnright()

function funPlantSingle() ** repetitive motion for row planting **
turtle.forward()
turtle.placeDown()

function funPlantRow()
local varPlant = 1
turtle.turnleft()
Repeat
funPlantSingle()
varPlant = varPlant + 1
until varPlant == 21

function funTurnA()
turtle.forward()
turtle.turnright()
turtle.forward()
turtle.turnright()

function funTurnB()
turtle.forward()
turtle.turnleft()
turtle.forward()

function funPlant() *** <——- Line 53 on the console ****
turtle.turnleft()
funPlantRow()
funTurnA()
funPlantRow()
funTurnB()

I have no idea what that error message means…
Luanub #10
Posted 04 January 2013 - 07:15 PM
You have not put ends in for your functions. You're also missing the end for your while loop.

Function syntax is..

function example()
--put some code here
end


EDIT: You've also got several other issues here. One Lua is case sensitive so where you have Repeat it should be repeat, you have Turtle in one spot should be turtle. You've got turnright() in some spots it should be turnRight(), turnleft is also turnLeft().

Also Lua reads top down so it is best to place functions at the top of the code prior to them being called. So I would move the funFullSequence() function and the while loop to the bottom of the code.

You're while loop is also missing some portions, you start the timer but don't have the code to wait for it to go off.

while true do
  funFullSequence()
  os.startTimer(3600)
  os.pullEvent("timer")
end
Edited on 04 January 2013 - 06:23 PM
JaWriter #11
Posted 04 January 2013 - 08:15 PM
*NOTE* Disregard this, i'm running into capilalization issues….


Still would appreciate a quick explination of how to import and export code to my clip board so I don't have to type / retype so much by hand…


Thanks launub. I have another question. Is there any way to import and export the code I'm writing to the note pad rather than re-typing the thing by hand in and out of the Minecraft console?

I fixed up the "end" markers on the functions, but now I'm having another bad call. This time it's part of the turtle.suckdown() function - http://computercraft...Turtle.suckDown What is the appropriate argument here to tell it to top it's invintory off with the saplings in the chest (turtle invintory is currently empty).

error Message: LogPlant:37: attempt to call nil

Spoilerfunction funPlantSingle()
turtle.forward()
turtle.placeDown()
end

function funPlantRow()
local varPlant = 1
turtle.turnleft()
repeat
funPlantSingle()
varPlant = varPlant + 1
until varPlant == 21
end

function funTurnA()
turtle.forward()
turtle.turnright()
turtle.forward()
turtle.turnright()
end

function funTurnB()
turtle.forward()
turtle.turnleft()
turtle.forward()
end

function funPlant()
turtle.turnleft()
funPlantRow()
funTurnA()
funPlantRow()
funTurnB()
end

function funGoTime()
turtle.suckdown() <———— Line 37
turtle.up()
turtle.forward()
turtle.turnright()
end

Function funGoHome()
turtle.turnright()
turtle.forward()
turtle.turnright()
turtle.forward(21)
turtle.down()
turtle.turnright()
end

function funFullSequence()
funGoTime()
funPlantRow()
funPlantRow()
funPlantRow()
turtle.forward(3)
funPlantRow()
funPlantRow()
funPlantRow()
funGoHome()
end

while true do
funFullSequence()
os.startTimer(3600)
os.pullEvent("timer")
end
Luanub #12
Posted 04 January 2013 - 08:55 PM
So as far as using notepad/notepad++/gedit etc, the files for single player games will be located in %AppData%\roaming\.minecraft\saves\worldname\computer\computerID

This "LogPlant:37: attempt to call nil" is due to it being turtle.suckDown() instead of suckdown() EDIT: this is pretty standard in most of the apis the first word is all lowercase and the first letter of each word after that is caps.

If you are wanting to fill all 16 slots with saplings then this should work as turtle.suckDown() will pull 1 full stack each time(if a full stack is available that is).

for x=1, 16 do
  turtle.select(x)
  turtle.suckDown()
end
Edited on 04 January 2013 - 07:59 PM
JaWriter #13
Posted 04 January 2013 - 09:40 PM
Luanub, thanks for thehelp. This programming is on a live server. Would it be easier to do this programing on a single player game first?

That worked perfectly isolated from the rest of my code.

Does that function need to be set in a different function to be called on or placed into my startup function?

Right now, I got it working,but the turtle is going haywire all over the place. I'm gonna have to scrap the code and re-build it one piece at a time. But I'm a whole lot closer now.
Luanub #14
Posted 04 January 2013 - 10:06 PM
Is the server someone elses? The files are pretty much in the same place on a server. serverFolder\world\computer\computerID if you have a way to access them.

If not do you know if http is enabled? If so you can use pastebin and the pastebin program that comes with CC to make it a little easier.

If you're planning on having the turtle get its saplings during funGoTime() then I would do it like this.

function funGoTime()
for x=1, 16 do
  turtle.select(x) 
  turtle.suckDown()
end
turtle.up()
turtle.forward()
turtle.turnright()
end
JaWriter #15
Posted 04 January 2013 - 10:56 PM
WOOT!

\o/

Rewrote the code and I Fixed the movment paths

1) Takes all items from the chest to fill invintory.
2) correctly moves across the entire planting path and turns at appropriate points

- I havent tested the wait function yet.

Now…… when i re-wrote the code I changed the code for the plant row function…. it's no longer putting saplings down using the turtle.placeDown function. It was earlier when the turtle was goinghaywire…..(I had the directions all goofied). lua Nub, your awesome, one last little bit to fix and were up and going.

Spoilerfunction funChestGrab()
for x=1, 16 do
turtle.select(x)
turtle.suckDown()
end
end

function funUTurnRight()
turtle.forward()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
end

function funUTurnLeft()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.forward()
turtle.turnLeft()
end

function funPlantSap()
turtle.forward()
turtle.placeDown()
end

function funPlantRow()
for i=1, 21 do
funPlantSap()
end
end

function funCenterMove()
turtle.turnRight()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.turnLeft()
end

function funStartup()
funChestGrab()
turtle.up()
turtle.forward()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
end

function funGoHome()
turtle.turnRight()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
for i=1, 22 do
turtle.forward()
end
turtle.turnRight()
turtle.down
end

function funPlantPlot()
funPlantRow()
funUTurnRight()
funPlantRow()
funUTurnLeft()
end

function funFullSequence()
funStartup()
funPlantPlot()
funPlantPlot()
funPlantPlot()
funCenterMove()
funPlantPlot()
funPlantPlot()
funPlantPlot()
funGoHome()
end

funFullSequence()

–while true do *disabled for now*
– funFullSequence()
– os.startTimer(600) — set to 600 seconds for testing,
– os.pullEvent("timer")
– end
JaWriter #16
Posted 04 January 2013 - 11:36 PM
Okay looking through i think I know whats going on.

When we did the funChestGrab, at the end of the sequence the item slot is not set to 16. Where do I find an example of code that will let me attempt to place down a sapling, if it fails it rotates it's slot selection, tries again, etc. until it finally hits a slot with a sapling to put down?

Here is the code that rotated my slot selection to slot 16:


function funChestGrab()
for x=1, 16 do
turtle.select(x)
turtle.suckDown()
end
end

I'll have to add it to this bit here:


function funPlantSap()
turtle.forward()
turtle.placeDown()
end

this is one of those return true of false thingies right? Check for tue…. if not x + 1?
Luanub #17
Posted 04 January 2013 - 11:45 PM
Try something like this

function funPlantSap()
turtle.forward()
local slot = 1
turtle.select(slot)
while not turtle.placeDown() do
  slot = slot + 1
  turtle.select(slot)
end
end
JaWriter #18
Posted 05 January 2013 - 12:36 AM
Yay!

It works top to bottom. The timer adjustment is gonna be on my end and will be fuel requirment specific for my boiler… but thats a whole nother story:

Final Code:

Spoiler


function funChestGrab()
  for x=1, 16 do
	   turtle.select(x)
	   turtle.suckDown()
	   end
end

function funUTurnRight()
  turtle.forward()
  turtle.turnRight()
  turtle.forward()
  turtle.turnRight()
  end

function funUTurnLeft()
		turtle.forward()
		turtle.turnLeft()
		turtle.forward()
		turtle.forward()
		turtle.turnLeft()
		end


function funPlantSap()
	   turtle.forward()
	   local slot = 1
	   turtle.select(slot)
			  while not turtle.placeDown() do
				   slot = slot + 1
				   turtle.select(slot)
			 end
	  end


function funPlantRow()
	  for i=1, 21 do
		  funPlantSap()
		  end
		  end


function funCenterMove()
	   turtle.turnRight()
	   turtle.forward()
	   turtle.forward()
	   turtle.forward()
	   turtle.turnLeft()
	   end

function funStartup()
	   funChestGrab()
	   turtle.up()
	   turtle.forward()
	   turtle.turnRight()
	   turtle.forward()
	   turtle.turnLeft()
	   end

function funGoHome()
	  turtle.turnRight()
	  turtle.turnRight()
	  turtle.forward()
	  turtle.turnRight()
			 for i=1, 22 do
			 turtle.forward()
			 end
	  turtle.turnRight()
	  turtle.down
	 end

function funPlantPlot()
			funPlantRow()
			funUTurnRight()
			funPlantRow()
			funUTurnLeft()
			end

function funFullSequence()
			 funStartup()
			 funPlantPlot()
			 funPlantPlot()
			 funPlantPlot()
			 funCenterMove()
			 funPlantPlot()
			 funPlantPlot()
			 funPlantPlot()
			 funGoHome()
			 end


while true do
  funFullSequence()
  os.startTimer(72000) 
  os.pullEvent("timer")
  end


Lots of props to Luna Nub for the language help and trouble shooting.

Is there anywhere I can post this code if someone else could learn from it? Or is this one of those - Super duper common beginer programming questions everyone asks?

*snuggle*
ChunLing #19
Posted 05 January 2013 - 02:32 AM
Completed programs can be posted in the Programs or Turtle Programs forums to be shared, if you wish.