Need a melee turtle script :)

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here

irishpancake33

New Member
Jul 29, 2019
16
0
0
Could anyone write me a script that has a turtle kill the mob in front of him and collects the xp? Im also unaware how to get the xp of the turtle so any know hows?
THANKS!
 

egor66

New Member
Jul 29, 2019
1,235
0
0
First off normally I use 2 turtles the attack turtle script is:

i = true
while i do turtle.attack()
turtle.drop() {or turtle.dropDown depending on where you add chest or collection system}
turtle.sleep(5)
end

The XP turtle is a turtle with an enchanting table, I normally use DW's one & edit to suit, hope its okay to post his pastebin here,
do the normal things like name the turtle, then type pastebin get R1jes23i booker, in this case I named it booker but you can name it as you like, just type booker in the turtle to fire it up, edit booker as/if need to grab/place books in chests as required.

I try not to use On start scripts as this then loads turtles every time you port back from a mystcraft age or twilight forrest, on a server can be minor lag for some ppl, & turtles reserve upto 300mb for looping programs so limit the number of turtles & the looping scripts.
 

Neirin

New Member
Jul 29, 2019
590
0
0
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
Code:
while true do
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:
Code:
shell.run("killer")
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.
 
  • Like
Reactions: Geometry

irishpancake33

New Member
Jul 29, 2019
16
0
0
Ok so I have a turtle(melee) killing the mob infront of him and transporting the goods to where ever. I now have an XP turtle but is there any way he can just collect the XP and i can take it off him or does he have to enchant?
 

Neirin

New Member
Jul 29, 2019
590
0
0
Ok so I have a turtle(melee) killing the mob infront of him and transporting the goods to where ever. I now have an XP turtle but is there any way he can just collect the XP and i can take it off him or does he have to enchant?
You cannot - to my knowledge - extract the xp from an xp turtle. Your best option for manual enchanting is a Brain in a Jar from Thaumcraft.