-- write out that we loaded sucessfully (mor or less, anyway)
io.write("Starting script for Level 2 of S23-IvoryTower in BlinkenLUA / ",_VERSION,"!\n")

-- declare some global variables

-- Fire-Tiles
-- --Config-- How many Fire-Tiles are there overall?
COUNT_FIRETILES = 5;
-- -- End of Config --
-- --Config-- Where should they be (x,y)
CFG_FIRETILES_VALUES_Y = { 41, 35, 35, 33, 41 };
CFG_FIRETILES_VALUES_X = { 12, 5, 33, 35, 21 };
for i=15,53 do
    COUNT_FIRETILES=COUNT_FIRETILES+1;
    CFG_FIRETILES_VALUES_Y[COUNT_FIRETILES] = 53;
    CFG_FIRETILES_VALUES_X[COUNT_FIRETILES] = i;
end
for i=2,14 do
    COUNT_FIRETILES=COUNT_FIRETILES+1;
    CFG_FIRETILES_VALUES_Y[COUNT_FIRETILES] = 31;
    CFG_FIRETILES_VALUES_X[COUNT_FIRETILES] = i;
end
-- -- End of Config --


GFX_FIRETILES = {};
for i=1,6 do
    GFX_FIRETILES[i]=-1;
end
OBJ_FIRETILES = {};
STATUS_FIRETILES = {};
for i=1,COUNT_FIRETILES do
    OBJ_FIRETILES[i] = -1;
    STATUS_FIRETILES[i] = math.random(6);
end


BLINKEN_TIMER = 0;




-- Required function: Initializing the level
function initLevel ()

    -- Create Fire-Tiles
    --    Load gfx
    GFX_FIRETILES[1] = LoadGFX("firetile1.png");
    GFX_FIRETILES[2] = LoadGFX("firetile2.png");
    GFX_FIRETILES[3] = LoadGFX("firetile3.png");
    GFX_FIRETILES[4] = LoadGFX("firetile4.png");
    GFX_FIRETILES[5] = LoadGFX("firetile5.png");
    GFX_FIRETILES[6] = LoadGFX("firetile6.png");
    
    --    Add Objects
    for i=1,COUNT_FIRETILES do
        OBJ_FIRETILES[i] = AddObject(GFX_FIRETILES[1], (CFG_FIRETILES_VALUES_X[i])-1, (CFG_FIRETILES_VALUES_Y[i])-1, 0, 1, 1, 1);
    end
   
end




-- Required function: Physics-loop for scripting part
function scriptPhysics ()
    
    -- Fire-Tiles display
    BLINKEN_TIMER = BLINKEN_TIMER + 1;
    if BLINKEN_TIMER > 9 then
        for i=1,COUNT_FIRETILES do
            STATUS_FIRETILES[i] = math.mod(STATUS_FIRETILES[i], 6)+1;
            if (math.random(100)>95) then
                STATUS_FIRETILES[i] = math.random(6);
            end
            SetGFX(OBJ_FIRETILES[i], GFX_FIRETILES[STATUS_FIRETILES[i]]);
        end
        BLINKEN_TIMER = 0;
    end
    
end




-- Required function: Callback on "Action"-Button on FG-Objects
function handleAction (obj_id)
    -- not needed this time
end







-- Script-internal functions

--none needed

