-- Exercise scripting a bit in our test-level

-- write out that we loaded sucessfully (more or less, anyway)
io.write("Starting script for Level0 in BlinkenLUA / ",_VERSION,"!\n")

-- declare our global data structure
GLOBALS = {};


-- Required function: Initializing the level
function initLevel ()
	-- Play animation
	PlayVideo("startanim.bmf", 0);
	-- Init base striuctures
	GLOBALS.gfx = {};
	GLOBALS.snd = {};
	GLOBALS.obj = {};

	-- Init variables
	GLOBALS.hs_on = {};
	for i = 1, 5, 1 do
		GLOBALS.hs_on[i] = 0;
	end
	GLOBALS.switchon = 0;
	GLOBALS.timer = 0;
	GLOBALS.hs_switch_cnt = 0;
	GLOBALS.hs_timer = 30;
	GLOBALS.pixelAdded = 0;
	GLOBALS.pixelTimer = 0;
	GLOBALS.pixelStep = 0;
	GLOBALS.hintTimer = 1000;
	GLOBALS.fireTimer = 25;
	GLOBALS.fireShown = 0;
	GLOBALS.transVTimer = 25;
	GLOBALS.transVShown = 0;
	GLOBALS.transVY = 639;
    GLOBALS.transVdir = -1;
	GLOBALS.antigravTimer = 50;
	GLOBALS.antigravShown = 1;
	GLOBALS.conveyerOffset = 1;
	GLOBALS.conveyerTimer = 1;
	GLOBALS.extramonsterPos = 0;
	GLOBALS.extramonsterTimer = 1;
	GLOBALS.extramonsterShown = 1;
	GLOBALS.freemovement = 0;
	GLOBALS.agravbgShown = 0;
	GLOBALS.agravbgTimer = 1;

    -- preload all gfx (on-the-fly loading is NOT recommended)
    GLOBALS.gfx.door_closed = LoadGFX("fg_door_closed.bmp");
    GLOBALS.gfx.door_open = LoadGFX("fg_door_open.bmp");
    GLOBALS.gfx.switch_red = LoadGFX("fg_switch_red.bmp");
    GLOBALS.gfx.switch_green = LoadGFX("fg_switch_green.bmp");
	GLOBALS.gfx.hs = {};
    GLOBALS.gfx.hs[1] = LoadGFX("fg_huntingseason.bmp");
    GLOBALS.gfx.hs[2] = LoadGFX("fg_huntingseason_neon.bmp");
	GLOBALS.gfx.progr = {};
    GLOBALS.gfx.progr[1] = LoadGFX("fg_progress_0.bmp");
    GLOBALS.gfx.progr[2] = LoadGFX("fg_progress_1.bmp");
    GLOBALS.gfx.progr[3] = LoadGFX("fg_progress_2.bmp");
    GLOBALS.gfx.progr[4] = LoadGFX("fg_progress_3.bmp");
    GLOBALS.gfx.progr[5] = LoadGFX("fg_progress_4.bmp");
    GLOBALS.gfx.progr[6] = LoadGFX("fg_progress_5.bmp");
	GLOBALS.gfx.machine = {};
    GLOBALS.gfx.machine[1] = LoadGFX("fg_machine1.bmp");
    GLOBALS.gfx.machine[2] = LoadGFX("fg_machine2.bmp");
    GLOBALS.gfx.stop = LoadGFX("fg_emergency_stop.bmp");
	GLOBALS.gfx.expl = {};
	GLOBALS.gfx.expl[0] = LoadGFX("fg_explode_0.bmp");
	GLOBALS.gfx.expl[1] = LoadGFX("fg_explode_1.bmp");
	GLOBALS.gfx.expl[2] = LoadGFX("fg_explode_2.bmp");
	GLOBALS.gfx.expl[3] = LoadGFX("fg_explode_3.bmp");
	GLOBALS.gfx.hint = LoadGFX("fg_hint_level1.jpg");
	GLOBALS.gfx.fire = {};
	GLOBALS.gfx.fire[0] = LoadGFX("fire1.bmp");
	GLOBALS.gfx.fire[1] = LoadGFX("fire2.bmp");
	GLOBALS.gfx.transV = {};
	GLOBALS.gfx.transV[0] = LoadGFX("vtrans1.bmp");
	GLOBALS.gfx.transV[1] = LoadGFX("vtrans2.bmp");
	GLOBALS.gfx.conveyer = {};
	GLOBALS.gfx.conveyer[0] = LoadGFX("conveyer_1.bmp");
	GLOBALS.gfx.conveyer[1] = LoadGFX("conveyer_2.bmp");
	GLOBALS.gfx.conveyer[2] = LoadGFX("conveyer_3.bmp");
	GLOBALS.gfx.conveyer[3] = LoadGFX("conveyer_4.bmp");
	GLOBALS.gfx.conveyer[4] = LoadGFX("conveyer_5.bmp");
	GLOBALS.gfx.conveyer[5] = LoadGFX("conveyer_6.bmp");
	GLOBALS.gfx.conveyer[6] = LoadGFX("conveyer_7.bmp");
	GLOBALS.gfx.conveyer[7] = LoadGFX("conveyer_8.bmp");
	GLOBALS.gfx.extramonster = {};
	GLOBALS.gfx.extramonster[0] = LoadGFX("monster6_1.bmp");
	GLOBALS.gfx.extramonster[1] = LoadGFX("monster6_2.bmp");
	GLOBALS.gfx.extramonster[2] = LoadGFX("monster6_3.bmp");
	GLOBALS.gfx.extramonster[3] = GLOBALS.gfx.extramonster[1];
	GLOBALS.gfx.agravbg = {};
	GLOBALS.gfx.agravbg[0] = LoadGFX("agravbg1.jpg");
	GLOBALS.gfx.agravbg[1] = LoadGFX("agravbg2.jpg");
	GLOBALS.gfx.agravbg[2] = LoadGFX("agravbg3.jpg");
	GLOBALS.gfx.agravbg[3] = LoadGFX("agravbg4.jpg");
	GLOBALS.gfx.agravbg[4] = LoadGFX("agravbg5.jpg");
	GLOBALS.gfx.agravbg[5] = GLOBALS.gfx.agravbg[3];
	GLOBALS.gfx.agravbg[6] = GLOBALS.gfx.agravbg[2];
	GLOBALS.gfx.agravbg[7] = GLOBALS.gfx.agravbg[1];


    -- create door and switch and fire
	AddBonusPoints(10, 21);
    GLOBALS.obj.switch = AddObject(GLOBALS.gfx.switch_red, 11, 21, 0, 1, 0, 0);
    GLOBALS.obj.door = AddObject(GLOBALS.gfx.door_closed, 13, 21, 1, 1, 1, 0);
    GLOBALS.obj.hs_sign = AddObject(GLOBALS.gfx.hs[1], 23, 17, 0, 1, 0, 0);
    GLOBALS.obj.fire = AddObject(GLOBALS.gfx.fire[0], 16, 21, 0, 0, 1, 1);

	-- create machines with switches and such
	GLOBALS.obj.hs_switch = {};
	GLOBALS.obj.machine = {};
	GLOBALS.obj.expl = {};
		-- Machine parts over start location
    AddObject(GLOBALS.gfx.machine[1], 1, 17, 0, 1, 0, 0);
    GLOBALS.obj.hs_switch[1] = AddObject(GLOBALS.gfx.switch_red, 2, 17, 0, 1, 0, 0);
    AddObject(GLOBALS.gfx.machine[1], 3, 17, 0, 1, 0, 0);
    AddObject(GLOBALS.gfx.machine[2], 4, 17, 0, 1, 0, 0);
    AddObject(GLOBALS.gfx.machine[1], 5, 17, 0, 1, 0, 0);

		-- Machine parts below sign	

	-- First machine triggers a DOOR OPEN IF IT IS CLOSED
    -- AddObject(GLOBALS.gfx.machine[2], 23, 21, 0, 1, 0, 0);
	GLOBALS.doorTrigger = AddFGTrigger(GLOBALS.gfx.machine[2], 23, 21, "AutoOpenDoor", 1, 1);

    AddObject(GLOBALS.gfx.machine[1], 24, 21, 0, 1, 0, 0);
    AddObject(GLOBALS.gfx.machine[1], 25, 21, 0, 1, 0, 0);
    GLOBALS.obj.hs_switch[2] = AddObject(GLOBALS.gfx.switch_red, 26, 21, 0, 1, 0, 0);
    AddObject(GLOBALS.gfx.machine[2], 27, 21, 0, 1, 0, 0);
    GLOBALS.obj.hs_switch[3] = AddObject(GLOBALS.gfx.switch_red, 28, 21, 0, 1, 0, 0);
    AddObject(GLOBALS.gfx.machine[1], 29, 21, 0, 1, 0, 0);
    GLOBALS.obj.hs_switch[4] = AddObject(GLOBALS.gfx.switch_red, 30, 21, 0, 1, 0, 0);
    GLOBALS.obj.progr =  AddObject(GLOBALS.gfx.progr[1], 31, 21, 0, 1, 0, 0);
    GLOBALS.obj.hs_switch[5] = AddObject(GLOBALS.gfx.switch_red, 32, 21, 0, 1, 0, 0);
    AddObject(GLOBALS.gfx.machine[2], 33, 21, 0, 1, 0, 0);
    GLOBALS.obj.stop = AddObject(GLOBALS.gfx.stop, 34, 21, 0, 1, 0, 0);
    AddObject(GLOBALS.gfx.machine[2], 35, 21, 0, 1, 0, 0);
    AddObject(GLOBALS.gfx.machine[1], 36, 21, 0, 1, 0, 0);
    GLOBALS.obj.expl[1] = AddObject(GLOBALS.gfx.expl[0], 35, 20, 0, 0, 1, 0);
    GLOBALS.obj.expl[2] = AddObject(GLOBALS.gfx.expl[0], 28, 20, 0, 0, 1, 0);
    AddObject(GLOBALS.gfx.hint, 2, 10, 0, 1, 0, 0);


    -- Vertical transporter
    GLOBALS.obj.transV = AddObject(GLOBALS.gfx.fire[0], 49, 20, 1, 1, 1, 0);
	SetElevator(GLOBALS.obj.transV, 1);
	
	-- Anti-Grav
    GLOBALS.gfx.antigrav = LoadGFX("arrow_up.bmp");
	GLOBALS.obj.antigrav = {};
	i = 0;
	while i < 20 do
		GLOBALS.obj.antigrav[i * 2] = AddObject(GLOBALS.gfx.antigrav, 65, i + 1, 0, 1, 0, 0);
		GLOBALS.obj.antigrav[(i * 2) + 1] = AddObject(GLOBALS.gfx.antigrav, 66, i + 1, 0, 1, 0, 0);
		i = i + 1;
	end

	-- Conveyer RIGHT
	GLOBALS.obj.conv_r = {};
	i = 0;
	while i < 9 do
		GLOBALS.obj.conv_r[i] = AddObject(GLOBALS.gfx.conveyer[0], 76+i, 19, 1, 1, 0, 0);
		i = i + 1;
	end

	-- Conveyer LEFT
	GLOBALS.obj.conv_l = {};
	i = 0;
	while i < 6 do
		GLOBALS.obj.conv_l[i] = AddObject(GLOBALS.gfx.conveyer[0], 77+i, 17, 1, 1, 0, 0);
		i = i + 1;
	end

	-- Extramonster
	GLOBALS.obj.extramonster = AddObject(GLOBALS.gfx.extramonster[0], 78, 12, 0, 1, 1, 1);


   -- load extra sound
   GLOBALS.snd.door_open = LoadSoundFX("fx_open_door.ogg");
   GLOBALS.snd.door_close = LoadSoundFX("fx_open_door.ogg");
   GLOBALS.snd.switch = LoadSoundFX("switch.wav");
   GLOBALS.snd.stop = LoadSoundFX("fx_kill_monster.ogg");
   GLOBALS.snd.expl = LoadSoundFX("fx_smallexplosion.ogg");
   GLOBALS.snd.hint = LoadSoundFX("fx_level1_hint.ogg");

	-- Antigrav / Free Movement
    GLOBALS.obj.agravbg = AddObject(GLOBALS.gfx.agravbg[0], 14, 1, 0, 0, 0, 0);
    GLOBALS.obj.switch2 = AddObject(GLOBALS.gfx.switch_red, 33, 4, 0, 1, 0, 0);
    GLOBALS.obj.door2 = AddObject(GLOBALS.gfx.door_open, 34, 4, 0, 1, 1, 0);

   -- Register additional pixel
   IncMaxPixels(1);
end

-- Required function: Physics-loop for scripting part
function scriptPhysics ()

	-- Antigrav visualisation
	GLOBALS.antigravTimer = GLOBALS.antigravTimer - 1;
	if GLOBALS.antigravTimer == 0 then
		GLOBALS.antigravTimer = 50;
		GLOBALS.antigravShown = 1 - GLOBALS.antigravShown;
		i = 0;
		while i < 40 do
			SetVisible(GLOBALS.obj.antigrav[i], GLOBALS.antigravShown);
			i = i + 1;
		end
	end

	-- Antigrav Physics
	px = GetPlayerX();
	py = GetPlayerY();
	if px > 2079 and px < 2144 and py < 608 then
		SetGravity(0);
	else
		SetGravity(1);
	end


	GLOBALS.fireTimer = GLOBALS.fireTimer - 1;
	if GLOBALS.fireTimer == 0 then
		GLOBALS.fireTimer = 25;
		GLOBALS.fireShown = 1 - GLOBALS.fireShown;
		SetGFX(GLOBALS.obj.fire, GLOBALS.gfx.fire[GLOBALS.fireShown]);
	end			

    -- Visualisation of vertical transporter
	GLOBALS.transVTimer = GLOBALS.transVTimer - 1;
	if GLOBALS.transVTimer == 0 then
		GLOBALS.transVShown = 1 - GLOBALS.transVShown;
		SetGFX(GLOBALS.obj.transV, GLOBALS.gfx.transV[GLOBALS.transVShown]);
		if GLOBALS.transVShown == 1 then
			GLOBALS.transVTimer = 15;
		else
			GLOBALS.transVTimer = 50;
		end
	end			

	GLOBALS.transVY = GLOBALS.transVY + GLOBALS.transVdir;
    if GLOBALS.transVY == 640 or GLOBALS.transVY == 128 then
    --if GLOBALS.transVY == 640 or GLOBALS.transVY == 630 then
		GLOBALS.transVdir = GLOBALS.transVdir * -1;
	end
	SetObjPos(GLOBALS.obj.transV, 1568, GLOBALS.transVY);

    if GLOBALS.switchon == 1 then
        -- Block the door after a certain time
        GLOBALS.timer = GLOBALS.timer - 1;
        if GLOBALS.timer == 0 then
            closeDoor();
        end
    end
    if GLOBALS.hs_switch_cnt == 5 then
        GLOBALS.hs_timer = GLOBALS.hs_timer - 1;
        if GLOBALS.hs_timer == 0 then
            GLOBALS.hs_timer = 200;
            SetGFX(GLOBALS.obj.hs_sign, GLOBALS.gfx.hs[2]);
        end
        if GLOBALS.hs_timer == 150 then
            SetGFX(GLOBALS.obj.hs_sign, GLOBALS.gfx.hs[1]);
        end
    end
	if GLOBALS.pixelTimer > 0 then
		GLOBALS.pixelTimer = GLOBALS.pixelTimer - 1;
		if GLOBALS.pixelTimer == 0 then
			GLOBALS.pixelStep = GLOBALS.pixelStep + 1;
			if GLOBALS.pixelStep == 1 then
				SetGFX(GLOBALS.obj.expl[1], GLOBALS.gfx.expl[1]);
				SetGFX(GLOBALS.obj.expl[2], GLOBALS.gfx.expl[1]);
				GLOBALS.pixelTimer = 10;
			end
			if GLOBALS.pixelStep == 2 then
				SetGFX(GLOBALS.obj.expl[1], GLOBALS.gfx.expl[2]);
				SetGFX(GLOBALS.obj.expl[2], GLOBALS.gfx.expl[2]);
				GLOBALS.pixelTimer = 10;
			end
			if GLOBALS.pixelStep == 3 then
				SetGFX(GLOBALS.obj.expl[1], GLOBALS.gfx.expl[3]);
				SetGFX(GLOBALS.obj.expl[2], GLOBALS.gfx.expl[3]);
				AddPixel(36, 21);
				AddBonusLive(29, 21);
				GLOBALS.pixelTimer = 10;
			end
			if GLOBALS.pixelStep == 4 then
				SetGFX(GLOBALS.obj.expl[1], GLOBALS.gfx.expl[2]);
				SetGFX(GLOBALS.obj.expl[2], GLOBALS.gfx.expl[2]);
				GLOBALS.pixelTimer = 10;
			end
			if GLOBALS.pixelStep == 5 then
				SetGFX(GLOBALS.obj.expl[1], GLOBALS.gfx.expl[1]);
				SetGFX(GLOBALS.obj.expl[2], GLOBALS.gfx.expl[1]);
				GLOBALS.pixelTimer = 10;
			end
			if GLOBALS.pixelStep == 6 then
				SetGFX(GLOBALS.obj.expl[1], GLOBALS.gfx.expl[0]);
				SetGFX(GLOBALS.obj.expl[2], GLOBALS.gfx.expl[0]);
				GLOBALS.pixelTimer = 10;
			end
			if GLOBALS.pixelStep == 7 then
				SetVisible(GLOBALS.obj.expl[1], 0);
				SetVisible(GLOBALS.obj.expl[2], 0);
			end
		end
	end

	if GLOBALS.pixelAdded == 0 then
		GLOBALS.hintTimer = GLOBALS.hintTimer - 1;
		if GLOBALS.hintTimer == 0 then
			PlaySoundFX(GLOBALS.snd.hint);
			GLOBALS.hintTimer = 3000;
		end
	end

	-- Conveyer
	GLOBALS.conveyerTimer = GLOBALS.conveyerTimer - 1;
	if GLOBALS.conveyerTimer == 0 then
		GLOBALS.conveyerTimer = 2;
		-- Display
		GLOBALS.conveyerOffset = GLOBALS.conveyerOffset + 1;
		if GLOBALS.conveyerOffset == 8 then
			GLOBALS.conveyerOffset = 0;
		end
		i = 0;
		while i < 9 do
			SetGFX(GLOBALS.obj.conv_r[i],  GLOBALS.gfx.conveyer[GLOBALS.conveyerOffset]);
			i = i + 1;
		end
		i = 0;
		while i < 6 do
			SetGFX(GLOBALS.obj.conv_l[i],  GLOBALS.gfx.conveyer[7 - GLOBALS.conveyerOffset]);
			i = i + 1;
		end

		-- PHYSICS
		if py == 576 then
			if px > 2399 and px < 2720 then
				px = px + 1;
				SetPlayerX(px);
			end
		end
		if py == 512 then
			if px > 2431 and px < 2656 then
				px = px - 1;
				SetPlayerX(px);
			end
		end
	end

	-- ExtraMonster
		-- DISPLAY
	GLOBALS.extramonsterTimer = GLOBALS.extramonsterTimer - 1;
	if GLOBALS.extramonsterTimer == 0 then
		GLOBALS.extramonsterTimer = 20;
		GLOBALS.extramonsterShown = GLOBALS.extramonsterShown + 1;
		if GLOBALS.extramonsterShown == 4 then
			GLOBALS.extramonsterShown = 0;
		end
		SetGFX(GLOBALS.obj.extramonster, GLOBALS.gfx.extramonster[GLOBALS.extramonsterShown]);
	end
		-- PHYSICS
	GLOBALS.extramonsterPos = GLOBALS.extramonsterPos + 1;
	if GLOBALS.extramonsterPos > 360 then
		GLOBALS.extramonsterPos = GLOBALS.extramonsterPos - 360;
	end
	mx = (math.sin(GLOBALS.extramonsterPos * math.pi / 180) * 60) + 2600;
	my = (math.cos(GLOBALS.extramonsterPos * math.pi / 180) * 60) + 300;
	SetObjPos(GLOBALS.obj.extramonster, mx, my);

	-- Antigrav / Free movement visualization
	GLOBALS.agravbgTimer = GLOBALS.agravbgTimer - 1;
	if GLOBALS.agravbgTimer == 0 then
		GLOBALS.agravbgTimer = 10;
		GLOBALS.agravbgShown = GLOBALS.agravbgShown + 1;
		if GLOBALS.agravbgShown == 7 then
			GLOBALS.agravbgShown = 0;
		end
		SetGFX(GLOBALS.obj.agravbg, GLOBALS.gfx.agravbg[GLOBALS.agravbgShown]);
	end

	-- Reset free movement if player has just died
	if GLOBALS.freemovement == 1  and px < 70 then
		GLOBALS.freemovement = 0;
		PlaySoundFX(GLOBALS.snd.switch);
		SetFreeMovement(GLOBALS.freemovement);
		SetVisible(GLOBALS.obj.agravbg, GLOBALS.freemovement);
		SetGFX(GLOBALS.obj.switch2, GLOBALS.gfx.switch_green);
		SetGFX(GLOBALS.obj.door2, GLOBALS.gfx.door_open);
		SetBlocking(GLOBALS.obj.door2, 0);
	end
		

end

-- Required function: Callback on "Action"-Button on FG-Objects
function handleAction (obj_id)
    -- Open door if not open
    if obj_id == GLOBALS.obj.switch and GLOBALS.switchon == 0 then
        openDoor();
    end
    if obj_id == GLOBALS.obj.stop then
        SetGFX(GLOBALS.obj.hs_switch[1], GLOBALS.gfx.switch_red);
        SetGFX(GLOBALS.obj.hs_switch[2], GLOBALS.gfx.switch_red);
        SetGFX(GLOBALS.obj.hs_switch[3], GLOBALS.gfx.switch_red);
        SetGFX(GLOBALS.obj.hs_switch[4], GLOBALS.gfx.switch_red);
        SetGFX(GLOBALS.obj.hs_switch[5], GLOBALS.gfx.switch_red);
        SetGFX(GLOBALS.obj.hs_sign, GLOBALS.gfx.hs[1]);

		if GLOBALS.pixelAdded == 0 and GLOBALS.hs_timer > 150 then
			GLOBALS.pixelAdded = 1;
			GLOBALS.pixelTimer = 10;
			PlayVideo("testmovie.bmf", 0);
			SetVisible(GLOBALS.obj.expl[1], 1);
			SetVisible(GLOBALS.obj.expl[2], 1);
		    PlaySoundFX(GLOBALS.snd.expl);
			SetTriggerActive(GLOBALS.doorTrigger, 0); -- Disable door trigger (machine exploded!!!)
		end

       -- play sound
       PlaySoundFX(GLOBALS.snd.stop);

       GLOBALS.hs_switch_cnt = 0;
	   for i = 1, 5, 1 do
		  GLOBALS.hs_on[i] = 0;
	   end
       GLOBALS.hs_timer = 30;
    end
    if obj_id == GLOBALS.obj.hs_switch[1] and GLOBALS.hs_on[1] == 0 then
        -- exchange GFX for door and button
        SetGFX(GLOBALS.obj.hs_switch[1], GLOBALS.gfx.switch_green);

       -- play sound
       PlaySoundFX(GLOBALS.snd.switch);

       GLOBALS.hs_on[1] = 1;
       GLOBALS.hs_switch_cnt = 1 + GLOBALS.hs_switch_cnt;
    end
    if obj_id == GLOBALS.obj.hs_switch[2] and GLOBALS.hs_on[2] == 0 then
        -- exchange GFX for door and button
        SetGFX(GLOBALS.obj.hs_switch[2], GLOBALS.gfx.switch_green);

       -- play sound
       PlaySoundFX(GLOBALS.snd.switch);

       GLOBALS.hs_on[2] = 1;
       GLOBALS.hs_switch_cnt = 1 + GLOBALS.hs_switch_cnt;
    end
    if obj_id == GLOBALS.obj.hs_switch[3] and GLOBALS.hs_on[3] == 0 then
        -- exchange GFX for door and button
        SetGFX(GLOBALS.obj.hs_switch[3], GLOBALS.gfx.switch_green);

       -- play sound
       PlaySoundFX(GLOBALS.snd.switch);

       GLOBALS.hs_on[3] = 1;
       GLOBALS.hs_switch_cnt = 1 + GLOBALS.hs_switch_cnt;
    end
    if obj_id == GLOBALS.obj.hs_switch[4] and GLOBALS.hs_on[4] == 0 then
        -- exchange GFX for door and button
        SetGFX(GLOBALS.obj.hs_switch[4], GLOBALS.gfx.switch_green);

       -- play sound
       PlaySoundFX(GLOBALS.snd.switch);

       GLOBALS.hs_on[4] = 1;
       GLOBALS.hs_switch_cnt = 1 + GLOBALS.hs_switch_cnt;
    end
    if obj_id == GLOBALS.obj.hs_switch[5] and GLOBALS.hs_on[5] == 0 then
        -- exchange GFX for door and button
        SetGFX(GLOBALS.obj.hs_switch[5], GLOBALS.gfx.switch_green);

       -- play sound
       PlaySoundFX(GLOBALS.snd.switch);

       GLOBALS.hs_on[5] = 1;
       GLOBALS.hs_switch_cnt = 1 + GLOBALS.hs_switch_cnt;
    end
    if GLOBALS.hs_switch_cnt == 0 then
        SetGFX(GLOBALS.obj.progr, GLOBALS.gfx.progr[1]);
    end
    if GLOBALS.hs_switch_cnt == 1 then
        SetGFX(GLOBALS.obj.progr, GLOBALS.gfx.progr[2]);
    end
    if GLOBALS.hs_switch_cnt == 2 then
        SetGFX(GLOBALS.obj.progr, GLOBALS.gfx.progr[3]);
    end
    if GLOBALS.hs_switch_cnt == 3 then
        SetGFX(GLOBALS.obj.progr, GLOBALS.gfx.progr[4]);
    end
    if GLOBALS.hs_switch_cnt == 4 then
        SetGFX(GLOBALS.obj.progr, GLOBALS.gfx.progr[5]);
    end
    if GLOBALS.hs_switch_cnt == 5 then
        SetGFX(GLOBALS.obj.progr, GLOBALS.gfx.progr[6]);
    end

	if obj_id == GLOBALS.obj.switch2 then
		GLOBALS.freemovement = 1 - GLOBALS.freemovement;
        PlaySoundFX(GLOBALS.snd.switch);
		SetFreeMovement(GLOBALS.freemovement);
		SetVisible(GLOBALS.obj.agravbg, GLOBALS.freemovement);
		if GLOBALS.freemovement == 1 then
    		SetGFX(GLOBALS.obj.switch2, GLOBALS.gfx.switch_red);
			SetGFX(GLOBALS.obj.door2, GLOBALS.gfx.door_closed);
    		SetBlocking(GLOBALS.obj.door2, 1);
		else
    		SetGFX(GLOBALS.obj.switch2, GLOBALS.gfx.switch_green);
			SetGFX(GLOBALS.obj.door2, GLOBALS.gfx.door_open);
    		SetBlocking(GLOBALS.obj.door2, 0);
		end
	end


end

-- Script-internal functions
function openDoor()
    GLOBALS.switchon = 1;

    -- exchange GFX for door and button
    SetGFX(GLOBALS.obj.switch, GLOBALS.gfx.switch_green);
    SetGFX(GLOBALS.obj.door, GLOBALS.gfx.door_open);

    -- make door nonblocking
    SetBlocking(GLOBALS.obj.door, 0);

    -- Turn on fire
	SetVisible(GLOBALS.obj.fire, 1);


    -- Add some output filter
    SetOutputFilter(OUTPUTFILTER_GREYSCALE + OUTPUTFILTER_NOISERECT + OUTPUTFILTER_NOISESTRIPE);

    -- set timer
    GLOBALS.timer = 250; -- 5 secs    

   -- play sound
   PlaySoundFX(GLOBALS.snd.door_open);
end

function closeDoor()
    GLOBALS.switchon = 0;

    -- exchange GFX for door and button
    SetGFX(GLOBALS.obj.switch, GLOBALS.gfx.switch_red);
    SetGFX(GLOBALS.obj.door, GLOBALS.gfx.door_closed);

    -- Block the door again
    SetBlocking(GLOBALS.obj.door, 1);

    -- Turn off fire
	SetVisible(GLOBALS.obj.fire, 0);


    -- Remove output filter
    SetOutputFilter(OUTPUTFILTER_NONE);

   -- play sound
   PlaySoundFX(GLOBALS.snd.door_close);
end

function AutoOpenDoor(objid)
	if GLOBALS.switchon == 0 then
		openDoor();
	end
end
