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

XP Turtle Farm

Started by Shadow_Assailant, 27 March 2013 - 05:40 AM
Shadow_Assailant #1
Posted 27 March 2013 - 06:40 AM
Hey, guys! I have a new XP farm that I'm trying out and rather than punching mobs to death I've decided to use XP Melee Turtles.
My system looks like this:
Spoiler

The idea is for the mobs to fall in the 1x1 hole and get chopped up by the turtles who will not only pick up the mobs drops and xp but also drop the items into the chests below from which the items will be sorted and to also enchant books upon reaching 30 levels. For some reason though, I keep getting this error:
startup:2: attemtp to index ? (a nil value)

This is what my code looks like:



m=peripheral.wrap("right")
mPeripheral.setAutoCollect(true)

while true do
  if turtle.attack()==false then
	for i=1,12 do
	  if turtle.getItemCount(i)>0 then
		turtle.select(i)
		turtle.dropDown()
	  else
		break
	  end
	end
  end
  if mPeripheral.getLevels()>=30 then
	for i=13,16 do
	  turtle.select(i)
	  mPeripheral.enchant(30)
	end
  end
  turtle.select(1)
end

So yeah. Like I said, I would like the turtles to kill away, drop their items down below (except for the unenchanted books), and enchant books, but this doesn't seem to work. From this, though, I see two problems with the over all idea: how do I get the turtle to drop the enchanted books? and how do I keep a supply of books inside the turtle?

One more thing, if this renders somewhat hard or impossible to do, I'd be willing to conjure up some RedPower2 managers and throw them beneath the turtles. In theory, these would automatically pull anything out of the turtle by themselves that isn't in the manager's list of items to keep, that is saying if managers even cooperate with turtles.

Anyways, take into account that I am no Lua expert. I only know a little bit of C++ and even that I'm not a pro at. Any help would be much appreciated. :)/>
jeppebra #2
Posted 27 March 2013 - 07:41 AM

if mPeripheral.getLevels() >=30 then
   turtle.turnRight()
   turtle.turnRight()
   turtle.select(2)
   turtle.dropDown()
   turtle.select(1)
   turtle.dropDown()
   turtle.suck()
   turtle.transferTo(2, 1)
   turtle.drop()
   turtle.select(2)
   mPeripheral.enchant(30)
   turtle.drop()
   turtle.select(1)
   turtle.turnRight
   turtle.turnRight
end
I know this is a very long code for something very simple and there is a way to make this a lot smaller, but this is what i came up with. It basically makes the turtle turn around, grab one book from a chest that was behind it, enchants it, puts it in the same chest and turns around again. Hope this could help you a bit :)/>
jeppebra #3
Posted 27 March 2013 - 07:46 AM
ohh and the reason why you're getting the error could be because you have to do "if not turtle.attack() then" and not "if turtle.attack() == false then"
jeppebra #4
Posted 27 March 2013 - 08:03 AM

m=peripheral.wrap("right")
mPeripheral.setAutoCollect(true)
while true do
   if not turtle.attack() then
	  for i = 1,16 do
		  if turtle.getItemCount(i) >0 then
			 turtle.select(i)
			 turtle.dropDown()
   else
	  break
	  end
   end
end  
if mPeripheral.getLevels() >=30 then
   turtle.turnRight()
   turtle.turnRight()
   turtle.select(2)
   turtle.dropDown()
   turtle.select(1)
   turtle.dropDown()
   turtle.suck()
   turtle.transferTo(2, 1)
   turtle.drop()
   turtle.select(2)
   mPeripheral.enchant(30)
   turtle.drop()
   turtle.select(1)
   turtle.turnRight
   turtle.turnRight
end
  end   turtle.select(1) end
/bin/bash #5
Posted 29 March 2013 - 08:08 AM
m=peripheral.wrap("right")
mPeripheral.setAutoCollect(true)

There's your issue, I'm surprised nobody pointed it out earlier. You have wrapped the peripheral as "m", not "mPeripheral".

Change the first line to the following instead:

mPeripheral=peripheral.wrap("right")

Edit: As far as inventory management is concerned, you could use 3 different chests. One where you'll store the monster drops, a second chest for regular books and a third for the enchanted books. When your turtle reaches the appropriate experience level, have it clear it's inventory into the first chest (the one with the monster drops), grab a regular book from the second, enchant, place the finished product into the third chest then rinse and repeat.

You will probably have to hook some pipes up to the chests so they don't overflow, but that's it.
Shadow_Assailant #6
Posted 29 March 2013 - 02:48 PM
Spoiler

mPeripheral=peripheral.wrap("right")
mPeripheral.setAutoCollect(true)
	
while true do
  if turtle.attack()==false then
	for i=1,15 do
	  if turtle.getItemCount(i)>0 then
		print("Depositing...")
		turtle.select(i)
		turtle.dropDown()
		turtle.select(1)
		print("Completed!")
	  elseif mPeripheral.getLevels()>=30 then
		if turtle.getItemCount(16)>2 then
		  print("30 Levels achieved!")
		  print("Commence enchanting!")
		  turtle.select(16)
		  turtle.transferTo(15,1)
		  turtle.select(15)
		  mPeripheral.enchant(30)
		  print("Enchanting completed!")
		  print("Depositing...")
		  turtle.transferTo(14,1)
		  turtle.select(14)
		  turtle.dropDown()
		  print("Completed!")
		  turtle.select(1)
		else
		  print("Running low on books!")
		  print("Fetching books...")
		  turtle.turnRight()
		  turtle.turnRight()
		  turtle.forward()
		  turtle.down()	
		  turtle.suckDown()
		  turtle.select(1)
		  if turtle.compareTo(16)==true then
			turtle.transferTo(16,64)
			turtle.select(i)
			turtle.dropDown()
			turtle.select(1)
			print("Books fetched!")
			print("Returning home...")
			turtle.turnRight()
			turtle.turnRight()
			turtle.up()
			turtle.forward()
			print("Returned!")
		  else
			turtle.select(i)
			turtle.dropDown()
			turtle.select(1)
			turtle.up()
			print("Error...")
			print("No more books...")
			print("Maitenance needed!")
		  end
		end
	  end
	end
  end
end  

Alright. So this is what my code looks like now. It does everything I want it to do minus one exception. This does require the turtle to move and therefore to have fuel, but that's not a problem. The only thing I would like it to do yet I cannot figure out how is to get it to break the infinite loop. I realize the way I used the while command will make the program continue endlessly, but where in my code I tell the turtle to fetch more books I would like the turtle to stop all activities. If you notice, I have the turtle going up one space after it checks and finds it has run out of books in the chest so I would notice it not being the correct place and therefore notice it's out of books. I would like the turtle to move up that one spot and print the text I've told it to and then cease all actions, but I cannot figure out a way to do so without the while loop at the beginning and the if command that checks for the amount of books being greater than 2 conflicting. I've considered throwing this at the beginning of the code:


mPeripheral=peripheral.wrap("right")
mPeripheral.setAutoCollect(true)
local collection = mPeripheral.setAutoCollect(true)
	
while collection==true do

and then at the end of the code after the turtle prints that there's a malfunction saying "collection=false" but this doesn't seem to work. I'm lost as for what to do.

Edit: Also, the way it stands right now when the turtle cannot pick up any more books and does not have more than two, it becomes stuck in a infinite loop of turning right twice, going forward once, going down once, and then undoing that and going back to its original spot. No idea…
Shadow_Assailant #7
Posted 04 April 2013 - 03:26 PM
No ideas anyone?
Aptik #8
Posted 05 April 2013 - 09:50 PM
Sorry for noob question, but may I know, is every melee turtle get XP or need to craft some special turtle?
Shadow_Assailant #9
Posted 06 April 2013 - 03:43 PM
They are not. A turtle (of any kind so long as it doesn't have two peripherals already) needs to be crafted with an enchanting table in order to collect XP and be able to enchant books and weapons and armor.
Enclave_reborn #10
Posted 06 July 2013 - 07:45 AM
is there any way to stop a turtle picking up items from a mob it has killed?
Nic #11
Posted 19 July 2013 - 01:23 AM
Can you add a pase bin so it will make thing easier
NOTUSEDPLEASEDELETE #12
Posted 03 August 2013 - 05:14 AM
Found the issue:

Line 2 is:

mPeripheral.setAutoCollect(true)
When it should be:

m.setAutoCollect(true)
And Line 17 should be:

m.enchant(30)

So don't write Peripheral at the beginning of a line of code about a peripheral.
crackpot #13
Posted 06 August 2013 - 07:12 PM
I've found the best way to accomplish this with a similar setup I am running is to keep books in slots 15 and 16 of the Wireless XP Turtle (I use wireless to control the 3 turtles in the farm) and when the command to enchant is sent the turtle will determine how many it can enchant, move a regular book to slot 1, enchant it, drop the enchanted book down (to a chest with item sorting leading to my ME system) and then either repeat until finished or return to waiting for a command and picking up XP.

Here is the code for the enchant portion, it seems to work well, though I think I am going to eventually rewrite it to automatic enchant the books upon reaching level 30. Once the code is polished a bit more I will release it for you guys to tear apart.

Hope this helps your issues.


local level = m.getLevels()
if level >= 30 then
  if turtle.getItemCount(16) == 0 and turtle.getItemCount(15) == 0 then
   rednet.send(sender, "You are out of books.")
  else
   local num = math.floor(level / 30)
   local good, bad = 0, 0
   rednet.send(sender, ("Creating %d enchanting books."):format(num), true)
  
   for x = 1, num do
	local i = turtle.getItemCount(16) == 0 and 15 or 16
	turtle.select(i)
	turtle.transferTo(1, 1)
	turtle.select(1)
	-- do the enchant
	if m.enchant(30) then
	 good = good + 1
	 turtle.select(1)
	 turtle.dropDown()
	else
	 bad = bad + 1
	 turtle.select(1)
	 turtle.transferTo(i, 1)
	end
  
	sleep(1)
   end
  
   -- send the results
   rednet.send(sender, ("Finished enchanting. %d Succeeded, %d Failed."):format(good, bad), true)	
  end  
else
  rednet.send(sender, ("Level 30 is required, I am only %d"):format(m.getLevels()), true)
end
Tha70neguy #14
Posted 10 August 2013 - 02:26 AM
http://pastebin.com/PQzZM8fu

That is the code I wrote for my turtle xp farms
(yes the xp values are correct but there seems to be an issue with 28-29 and 29-30 being at the proper value even though I used the formula from Mojang)

Edit: Also the local kills = XP/5 the 5 can be changed depending on the type(s) of mobs killed
This is for Wither Skeletons and you can find the amount of XP given per kill here http://www.minecraftwiki.net/wiki/Experience