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

Automatic mob farm with Turtles

Discussion in 'General FTB chat' started by Servillo, May 30, 2013.

  1. Servillo

    Servillo Member

    I'm going to preface this topic with letting you know that I am a complete novice when it comes to programming. I've taken one class of C++, which thankfully translates fairly well to LUA. However, that means the extend of my knowledge with programming with turtles basically extends to loops, functions to keep things concise, and using the API's to the best of my abilities. I've learned how to use a couple of the slightly more complicated aspects (such as tArgs, if that counts as complex), but since I'm the only turtle programmer on our server, I don't use them in the name of user-friendliness.

    That said, I would like to make a program that will let a turtle automatically kill mobs for me with a grinder, and to collect the EXP that drops. I know all the API functions I need to do this with, it's just a matter of getting it to work right. One thing I do know is that I've read that constant attack loops with a turtle, i.e.

    while true do
    turtle.attack()
    end

    Can cause lag or framerate issues overtime. In order to avoid that, I tried something like this

    while true do
    if redstone.getInput("front") then
    turtle.attack()
    end
    end

    I put a pressure plate in front of the turtle, and whenever something stands on it, the turtle will attack it. Very brute force, and not very elegant. Of course, I ran into the only natural thing; if the while loop goes too long without doing anything, the program terminates. I didn't expect anything different, this was just the starting step to make sure my thought process on this was working.

    So now my only idea is to make the program temporary via a for loop, i.e.

    for i=1 to ## do
    if redstone.getInput("front") then
    turtle.attack()
    end
    end

    However, I'm pretty sure the same thing will happen. The only other thing I can think of is to put a sleep of a couple minutes or so after the if statement, that way mobs can accumulate, the turtle will attack them until they all die, and life continues as normal. I would use a similar program with my EXP turtles, having them collect experience every few minutes or so automatically.

    Again, I'm a complete novice at programming, so anything really complex and out there is just going to be beyond me. I program turtles for fun, and I know my programs are incredibly inefficient, clumsy, and any real programmer would probably cry at my attempts. I love my turtles though, and I try to use them often, and this is just the next thing I want to use them for.
     
  2. Dkittrell

    Dkittrell Well-Known Member

    First I'm not an expert at programming so I won't comment on any of those questions but relating to the lag and attack turtles in my experience I didn't experience any lag with the loop attack code and i had 8 attack turtles plus on dedicated xp turtle but at the same I'm dedicating 4GB of ram to FTB so that would help any frame rate related issues but everyone on my old sever used my xp farm and didn't complain of lag. Just stating my experience take it how you will :)
     
  3. Silent_007

    Silent_007 Active Member

    The other way to reduce lag using code along the lines of your first example is just to reduce how often the turtle tries to attack. The sleep command is what you're looking for, though I would use it in a simpler way than you were suggesting. For instance:

    Code:
    while true do
        turtle.attack()
        turtle.sleep(1)
    end
    Also, there are TONS of topics on these forums about this exact issue. I would suggest checking some of them out. ;)
     
  4. Abdiel

    Abdiel Well-Known Member

    Code:
    while true do
        while redstone.getInput("front") do
            turtle.attack();
            sleep(0.5);
        end
        os.pullEvent("redstone")
    end
     
    droid20 and tompy97 like this.
  5. tompy97

    tompy97 Active Member

    abdiel has a very good code, use it, it's almost identical to my set up and its the most lag free way to run a turtle farm
     
  6. Servillo

    Servillo Member

    Abdiel, thank you very much for that code. Like I said, I know very little about LUA and the various API's (especially the OS API, which I've pretty much never used), so this helps a lot. I think that may give me a good idea on how to use it elsewhere as well.
     
  7. Bomb Bloke

    Bomb Bloke Well-Known Member

    The reason a "while" loop might halt is because it goes too long without yielding (or "waiting"). Beats me as to why this matters but the Lua interpreter doesn't like it.

    os.pullEvent yields, as does sleep, as do most turtle actions. It doesn't matter how long your program yields for so long as you're doing it at least once per iteration of your loop.
     
  8. droid20

    droid20 New Member

    Do you use pressure plates in front of this?
     
  9. tompy97

    tompy97 Active Member

    yep, a pressure plate in front of the turtle is the way to go
     

Share This Page