-- name: AutoSplit
-- description: Put stuff into STDOUT that we can split off of

    -- LEVEL_BOB = 9,
    -- LEVEL_WF = 24,
    -- LEVEL_CCM = 5,
    -- LEVEL_HMC = 7,
    -- LEVEL_DDD = 23,
    -- LEVEL_BITDW = 17,
    -- LEVEL_BITFS = 19,
    -- LEVEL_BITS = 21,
    -- LEVEL_BOWSER_1 = 30,
    -- LEVEL_BOWSER_2 = 33,
    -- LEVEL_BOWSER_3 = 34,

bob = false
wf = false
ccm = false
hmc = false
ddd = false
bitdw = false
bitfs = false
bits = false
b1 = false
b2 = false
b3 = false

skip_one = true

split_stars = {1,3,6,8,12,15,16}

num_stars = 0

function level_hook(type, levelnum)
    if skip_one then
        skip_one = false
        return
    end
    if levelnum == 9 and not bob then
        print("split")
        bob = true
        return
    end

    if levelnum == 24 and not wf then
        print("split")
        wf = true
        return
    end

    if levelnum == 5 and not ccm then
        print("split")
        ccm = true
        return
    end

    if levelnum == 7 and not hmc then
        print("split")
        hmc = true
        return
    end

    if levelnum == 23 and not ddd then
        print("split")
        ddd = true
        return
    end

    if levelnum == 17 and not bitdw then
        print("split")
        bitdw = true
        return
    end

    if levelnum == 19 and not bitfs then
        print("split")
        bitfs = true
        return
    end

    if levelnum == 21 and not bits then
        print("split")
        bits = true
        return
    end

    if levelnum == 30 and not b1 then
        print("split")
        b1 = true
        return
    end

    if levelnum == 33 and not b2 then
        print("split")
        b2 = true
        return
    end

    if levelnum == 34 and not b3 then
        print("split")
        b3 = true
        return
    end

end

function has_value (tab, val)
    for index, value in ipairs(tab) do
        if value == val then
            return true
        end
    end
    return false
end


function detect_obj(mario, obj, act_type, act_value)
    if act_type == INTERACT_STAR_OR_KEY then
        -- if numstars hasn't changed then we grabbed a key and can split 
        if num_stars == mario.numStars then
            print("split")
            return
        end
        num_stars = mario.numStars
        if has_value(split_stars, num_stars) then
            print("split")
        end
        -- elseif act_type == INTERACT_POLE then
        --     print("tree")
    end
end

function start_run()
    print("split")
end

hook_event(HOOK_ON_INTERACT, detect_obj)
hook_event(HOOK_ON_LEVEL_INIT, level_hook)
hook_event(HOOK_ON_MODS_LOADED, start_run)
