Good vs. Evil

Discussion in 'Forum Games' started by Randomguy404, Oct 12, 2014.

  1. GamerwithnoGame

    GamerwithnoGame Forum Addict

    58. Titans is pretty neat.
     
  2. duckfan77

    duckfan77 Well-Known Member

  3. lenscas

    lenscas Over-Achiever

    60 I think I edited the mining script correctly.

    Assuming I didn't screw up it should be less likely for it to get stuck (Which never happened anyway), the code is a bit easier to understand, less error prone and easier to configure. The turtle should also be faster.

    Before it assumed that after 15 items it should unload, now it only send the items back if the last slot contains any items. This technically means it can loose items if that slot is the only one empty and it mines something that drops more than 1 type of item but.... I can live with that.

    There are some slowdowns that I introduced though, however they are introduced to keep the state as simple as possible and should almost have no effect. For example, before a refuel it now will offload all the items.

    One other feature that I lost now that I can't use enderchests as a peripheral is the ability to call "condense" on them. However, I think that this also becomes less of a problem (And only happened if your ore processing couldn't keep up anyway)
    Code:
    --some config
    local SEND = 1 --the slot with the enderchest to return items
    local FUEL = 2 --the slot with the enderchest to get fuel from
    local FIRST_SLOT = 3 --first slot that will be used as storage
    local LAST_SLOT = 16 --last slot that will be used as storage
    local SLEEP_TIME = 0.5 --prevetion loops getting killed
    local BREAK_TIME = 100 --how long to wait if it gets stuck
    local TIMES_UNTIL_TRY_REFUEL = 2 --how often to try to go forward before trying to get fuel
    local TIMES_UNTIL_TRY_BREAK = 4 --how often to fail going forward before going into "stuck" mode
    local WAIT_FOR_ROOM_TIME = 20 --how long to wait before trying to add items into the send chest if it was full
    
    local turtle = turtle or {} --to make my editor shut up
    
    --run func after selecting the correct chest and placing it at the top of the turtle
    --it also gets the enderchest back and sets the selected itemslot back to what it was before this was called
    local function useChest(slotId, func)
        local selected = turtle.getSelectedSlot()
        turtle.select(slotId)
        --do whatever it takes to make place at the top
        while not turtle.placeUp() do
            turtle.select(selected)
            turtle.digUp()
            turtle.attackUp()
            turtle.select(slotId)
            sleep(SLEEP_TIME)
        end
        func()
        --reset, make it look like this function was never called
        turtle.select(slotId)
        turtle.digUp()
        turtle.select(selected)
    end
    
    --send every item back to the base
    local function sendItems()
       useChest(SEND,function()
        for slot = FIRST_SLOT, LAST_SLOT do
            turtle.select(slot)
            while not turtle.dropUp() do sleep(WAIT_FOR_ROOM_TIME) end
        end
       end)
    end
    
    --refuel, if needed
    local function refuel()
        print(turtle.getFuelLevel())
        if turtle.getFuelLevel() < 200 then
         sendItems()
         useChest(FUEL,function()
             turtle.select(LAST_SLOT)
             turtle.suckUp()
             turtle.refuel()
             if not turtle.dropUp() then
                 turtle.drop()
             end
         end)
        end
        print(turtle.getFuelLevel())
    end
    
    --get the turtle in a known good state
    local function startup()
    
       turtle.select(FIRST_SLOT)
       local hasSendChest = turtle.getItemCount(SEND) == 1
       local hasRefuelChest = turtle.getItemCount(FUEL) == 1
    
       if hasRefuelChest == false and hasSendChest == false then
            print("Missing fuel and send chest")
            --bad state :(
            return false
       end
       --reclaim fuel chest
       if not hasRefuelChest then
            turtle.select(FUEL)
            turtle.digUp()
       end
       --reclaim send chest
       if not hasSendChest then
            turtle.select(SEND)
            turtle.digUp()
       end
       --bring to known good state
       sendItems()
       refuel()
       turtle.select(FIRST_SLOT)
       --we are now in a known good state! HOORAY!
       return true
    end
    
    
    local function mine()
        while turtle.detectUp() do
         turtle.select(FIRST_SLOT)
         turtle.digUp()
         sleep(SLEEP_TIME)
       end
       while turtle.detect() do
         turtle.select(FIRST_SLOT)
         turtle.dig()
         sleep(SLEEP_TIME)
       end
       turtle.select(FIRST_SLOT)
       turtle.digDown()
    end
    
    --something caused the turtle to be unable to fullfill its task
    --waiting may solve the problem, or at least gives you time to notice it and fix it while keeping CPU load to a minimum
    local function takeBreak(reason)
        while true do
            turtle.sleep(BREAK_TIME)
            if reason then
                print("Taking a break because : ",reason)
            end
        end
    end
    
    local function forward()
       local counter=0
       while not turtle.forward() do
         mine()
         turtle.attack()
         counter=counter+1
         if counter > TIMES_UNTIL_TRY_REFUEL then
            print("Something is blocking me. Maybe fuel is low?")
            refuel()
         elseif counter > TIMES_UNTIL_TRY_BREAK then
            takeBreak("Something is really blockig me.")
         end
       end
    end
    
    --here is the main part of the program
    if not startup() then
       error("Failed discover last state :(")
    end
    
    --main loop. Forever, dig forward, if last_slot has items then send everything back
    while true do
        while turtle.getItemCount(LAST_SLOT) == 0 do
            mine()
            forward()
       end
       sendItems()
       refuel()
    end
    
     
  4. duckfan77

    duckfan77 Well-Known Member

  5. GamerwithnoGame

    GamerwithnoGame Forum Addict

    60. It’s a TV series, much more adult oriented than the cartoons like Teen Titans but featuring many of the same characters.
     
  6. lenscas

    lenscas Over-Achiever

    61 and.... I saw a mistake in the script. I still use a function that is too new. So, now I need to think on implementing it myself or work around it
     
  7. GamerwithnoGame

    GamerwithnoGame Forum Addict

    60. Ah, that must be annoying!
     
  8. lenscas

    lenscas Over-Achiever

    61 meh, at least I can work around it, also I kind of like that it isn't a "drag and drop" process this time to set it up but it actually requires me to think.

    It is that I don't like mining but otherwise I could see myself having dumped the script after the first time I used it as setting it up is so boring....
     
  9. GamerwithnoGame

    GamerwithnoGame Forum Addict

    60. Well, thats something.
     
  10. duckfan77

    duckfan77 Well-Known Member

  11. GamerwithnoGame

    GamerwithnoGame Forum Addict

  12. lenscas

    lenscas Over-Achiever

    61 I got the script working and also discovered a stupid "feature" of emerald pipes.

    Because I am not at all setup yet for automatic ore processing (Nor have a decent storage system) I just wanted to automatically void items like cobble stone. I wanted to use emerald pipes for that, as they can extract and have a white list.

    Turns out however that they operate in blocking round-robin mode. Aka, it will loop through the filter and will only do the next item in the list if it extracted the current one. Even if the current one isn't in the chest.

    So, for now I have a turtle put everything from the ender chest into a double chest and I extract only cobble from this double chest.
     
  13. duckfan77

    duckfan77 Well-Known Member

    62 Ick. Why would the emerald pipes work like that
     
  14. lenscas

    lenscas Over-Achiever

    63 I can see some use for that, with auto crafting for example. However, it would have been nice if there was a pipe that didn't work like that, but sadly not even clay pipes are a thing in this version. Let alone a pipe that does do what I want.
     
  15. GamerwithnoGame

    GamerwithnoGame Forum Addict

    62. Wait, no clay pipes?? The liquid versions of those were a godsend in Regrowth.
     
  16. duckfan77

    duckfan77 Well-Known Member

  17. lenscas

    lenscas Over-Achiever

    64 a lot of pipes aren't made yet, and with buildcraft being the main way I can see myself doing item transport for the foreseeable future that can become quite a challenge.

    I know I can dive into red power for its tubes, but I remember its recipes being annoying. And the only other item transport I know of are the conveyors from minefactory reloaded.
     
  18. GamerwithnoGame

    GamerwithnoGame Forum Addict

    63. Ohhh of course *facepalms*

    Redpower tubes were brass, right?
     
  19. duckfan77

    duckfan77 Well-Known Member

  20. GamerwithnoGame

    GamerwithnoGame Forum Addict

    63. The copper and zinc weren't too problematic from what I briefly remember of RedPower?
     

Share This Page