All attack scripts are going to be some variation on
Code:
while true do
turtle.attack
turtle.sleep(<number of seconds between attacks>)
end
I personally use a sleep timer of 0.5 sec, but have a redstone toggle in the loop so it's not running constantly.
While using a rudimentary system like this it is recommended that you stuff the turtle's inventory with cobble or something beforehand so that you don't need to worry about getting items out of the turtle. However, it's a fairly simple matter to add turtle.drop() to your loop (as egor does) or to add in a loop that clears the inventory when the turtle starts to get full. For instance: add this in to the above loop and it will empty its inventory whenever an item lands in slot 16.
Code:
if turtle.getItemCount(16) > 0 then
for i = 1, 16 do
turtle.select(i)
turtle.drop()
end
turtle.select(1)
end
You of course can tweak this to fit your tastes. Whether that means reducing the number of slots you throw out or disposing of the if statement all together to handle mobs that have multiple drops. Though if you scrap the if statement, I would strongly recommend only throwing out the first 4 slots or so - that should cover all the random drops like stone swords and such.
The code for using an xp turtle all depends on how automated your system is. If you're just manually feeding the turtle items and taking them out it's really easy:
Code:
xp = peripheral.wrap("right")
xp.setAutoCollect(true)
if xp.getLevels() >= 30 then
xp.enchant(30)
end
If you want it to drop off the enchanted item into a chest, just add in a turtle.drop after xp.enchant(30)
It gets a bit more complicated if you want to handle books because the turtle can't enchant a stack of books, just 1 at a time. The basic version of that look like this:
Code:
xp=peripheral.wrap("right")
xp.setAutoCollect(true)
if xp.getLevels() >= 30 then
turtle.select(16)
turtle.transferTo(15, 1)
turtle.select(15)
xp.enchant(30)
turtle.drop()
turtle.select(1)
end
Using this, if you place a bunch of books in slot 16 whenever the turtle has 30 levels of experience it will move 1 of those books to slot 15, enchant it, then drop it off in a chest in front of it. It finishes with turtle.select(1) because I'm building this with the assumption that your xp turtle will also be killing stuff, so we want mob drops to accrue in the earlier slots and not interfere with the enchanting process.
To bring these together, we should really make the enchanting process its own function so everything is easy to see:
Code:
function enchantBook()
turtle.select(16)
<rest of what I wrote above, etc>
end
So we end up with something like this:
Code:
xp = peripheral.wrap("right")
xp.setAutoCollect(true)
function enchantBook()
turtle.select(16)
turtle.transferTo(15, 1)
turtle.select(15)
xp.enchant(30)
turtle.drop()
turtle.select(1)
end
while true do
if xp.getLevels() >= 30 then
enchantBook()
end
turtle.attack()
for i = 1, 4 do
turtle.select(i)
turtle.drop()
end
turtle.select(1)
turtle.sleep(0.5)
end
This will automatically enchant books placed in slot 16 (the last slot), can handle up to 4 mob drops at a time, and will attack quite quickly. However, there are still 3 problems with it: 1st it drops mob drops and enchanted books in the same place. 2nd it doesn't turn off. 3rd it needs to be manually refilled with books.
The 1st problem is easy to correct. If we change turtle.drop() into turtle.dropUp() or turtle.dropDown() we can send items to different places. Moreover, since this will be a melee turtle, we can assume there won't be a chest in front of it, so we really should set one chest to the top and one to the bottom, leaving the front clear for killing.
Fixing the 2nd problem is pretty simple in terms of coding, but this is where you start running into questions about your mob grinder setup. If you run a redstone signal into the back of the turtle or one of the sides, you can turn off the turtle by replacing
with (for example)
Code:
while rs.getInput("back") do
however, if you have multiple turtles next to each other, you obviously can't run redstone to the sides, limiting your input to the back of the turtle. 99% of the time this is fine, but blazes have a nasty habit of every now and then shooting around your turtle and setting you on fire. It's rare, but it sucks. However, for now, let's just write that off as an acceptable risk.
Finally, the 3rd problem is a real doosey if we're trying to keep things simple. If you've incorporated a redstone shutoff then you don't have an extra face of the turtle available to use. Even if you didn't, the top, bottom, and front faces of the turtle are already occupied, making the process of having the turtle automatically refill its books tricky. The solution I use on occasion and have seen other people use is to have your enchanted book chest (usually an ender chest) serve double duty as both the receptacle for enchanted books and the holding area for extra unenchanted books. However, using this sort of system isn't very practical unless you use outside infrastructure to pull the enchanted books away for storage elsewhere (i.e. use an emerald pipe). Checking whether there are books in slot 16 is accomplished via our old friend turtle.getItemCount(16).Retrieving new books can be accomplished via turtle.suck().
So, let's bring this all together:
Assumptions:
--mob drops go into an Ender Chest/Relay/however you want to pass them into your storage system placed below the turtle.
--enchanted books deposited in the chest/whatever above it. unenchanted books are also pulled out of this chest.
--if you don't want to worry about refilling the turtle from the chest, put 2 hyphens ("--") in front of the 3 lines that start with "if turtle.getItemCount(16) == 0 then"
--on/off switch connected to the back of the turtle (redstone signal on = turtle on)
--you will only ever need to handle 4 mob drops at a time (max number a wither skeleton can drop)
--the xp module is on the right side of the turtle. I actually can't remember what side it's on, but if yours is on the left side of the turtle, change "right" to "left" in the first line of the code
Code:
xp = peripheral.wrap("right")
xp.setAutoCollect(true)
function enchantBook()
turtle.select(16)
if turtle.getItemCount(16) == 0 then
turtle.suckUp()
end
turtle.transferTo(15, 1)
turtle.select(15)
xp.enchant(30)
turtle.dropUp()
turtle.select(1)
end
while rs.getIntput("back") do
if xp.getLevels() >= 30 then
enchantbook()
end
turtle.attack()
for i = 1, 4 do
turtle.select(i)
turtle.drop()
end
turtle.select(1)
turtle.sleep(0.5)
end
pastebin:
http://pastebin.com/P5zXbw3C
to get it on your turtle use
Code:
pastebin get P5zXbw3C killer
(you can make the name whatever you like, I just picked "killer" out of thin air)
Finally, to make sure the program is always running you need to set it as your startup script. This is done by typing "edit startup" and then entering this code:
Then press ctrl to bring up the menu and save. To restart your turtle (and thus start the program running for the first time) either hold down ctrl+r or log out/back in (or unload the chunk and load it back in, whatever).
For what it's worth, most of the really nice auto-enchanting programs you can find on the CC forums aren't really much more than this. They incorporate things like safety checks (i.e. if the book chest is full, don't throw books on the ground), movement, monitor support, and other nice features, but the functional bit is all here. So, if you followed along at all you're probably already ready to jump into actual ComputerCraft programming. If you'd like a rundown of some of the basics that went into making this program, I highly recommend Direwolf20's series of CC
tutorials. For info on more of the awesome peripherals like the XP module added by Misc. Peripherals, you can refer to its
thread on the CC forums. Finally, if you get serious about programming, I strongly suggest doing your programming with some of the
API lists from the CC wiki open in another window until you've got the hang of things.