function getCounter(key)
    return engine.Scene.Session:GetCounter(key)
end

function setCounter(key, value)
    return engine.Scene.Session:SetCounter(key, value)
end

local returnPoint = ""


function greenshardChecker()
    
end



function onBegin()

    

    if getFlag("from_caverns") then
        returnPoint = "Caverns"
    elseif getFlag("from_ruins")  then
        returnPoint = "Ruins"
    elseif getFlag("from_outskirts") then
        returnPoint = "Outskirts"
    end

    setFlag("from_caverns", false)
    setFlag("from_ruins", false)
    setFlag("from_outskirts", false)

    disableMovement()

    if not getFlag("seenGream") then
        say("GREENcontest25_grog_GREAM00")
        setFlag("seenGream")
        setCounter("mapCompletionIndex", 1)
    else
        say("GREENcontest25_grog_GREAM_WELCOME")
    end

    if getFlag("lockFound") and not getFlag("lockFoundGream") then
        say("GREENcontest25_grog_GREAM_KEYDOOR")
        setFlag("lockFoundGream", true)
    end

    if getFlag("seenGate") and not getFlag("seenGateGream") then
        say("GREENcontest25_grog_GREAM_GREENSIDE")
        setFlag("seenGateGream", true)
    end

    local letters = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'}

    for _, value in ipairs(letters) do
        

        if not getFlag("greenshard_challengerun_collect_"..value) and getFlag("ChallengeCompleted_"..value)  then
            say("GREENcontest25_grog_GREAM_GET_GREENDUST")
            setFlag("greenshard_challengerun_collect_"..value)
            

            local count = 0

            for _, value in ipairs(letters) do
                if getFlag("greenshard_collect_"..value) then
                    count = count + 1
                end

                if getFlag("greenshard_challengerun_collect_"..value) then
                    count = count + 1
                end

                if count >= 10 then

                    for i = 0, 9, 1 do
                        setFlag("GREENSIDE_int0"..i, true)
                    end


                    engine.Scene.Session:SetCounter('CHALLENGE_RUNS', count - 10)

                else

                    for i = 0, count-1, 1 do
                        setFlag("GREENSIDE_int0"..i, true)
                    end

                end

            end

            if getCounter('CHALLENGE_RUNS') > getCounter('BERRIES_GIVEN') then
                say("GREENcontest25_grog_GREAM_GIVE_BERRY")
                setFlag("GREAM_GIVE_BERRY"..getCounter('BERRIES_GIVEN'), true)
                setCounter('BERRIES_GIVEN', getCounter('BERRIES_GIVEN') + 1)
            else
                say("GREENcontest25_grog_GREAM_GIVE_SHARD_A")
                playSound('event:/game/07_summit/gem_get')
                setFlag("flash", true)
                say("GREENcontest25_grog_GREAM_GIVE_SHARD_B")
            end

        

        

        end
    end


    while getCounter("mapCompletionIndex") < 6 do
        if getCounter("exploredTotal") >= getCounter("mapCompletionIndex")*20 then
            say("GREENcontest25_grog_GREAM_GET_"..(getCounter("mapCompletionIndex")*20))
            setCounter("mapCompletionIndex", getCounter("mapCompletionIndex")+1)
            
            if getCounter("mapCompletionIndex") >= 6 then
                break
            end
        
        else
            say("GREENcontest25_grog_GREAM_NOTENOUGH_"..(getCounter("mapCompletionIndex")*20))
            break
        end
    end

    if getCounter("mapCompletionIndex") >= 6 then
        setFlag("giveCompletionBerry", true)
        say("GREENcontest25_grog_GREAM_NOMORE")
        say("GREENcontest25_grog_GREAM_GOODBYE_ALT")
    else
        say("GREENcontest25_grog_GREAM_GOODBYE")
    end

    
end

function onEnd(room, wasSkipped)
    enableMovement()
    setFlag("exitShop_"..returnPoint, true)
end