local script = globalObject;

local object = nil;
local lighting_a = nil;
local lighting_b = nil;
local lighting_c = nil;
local flarik1 = nil;
local flarik2 = nil;
local flarik3 = nil;


local commandMap = {};

local dt = 0;
local blight = true;

local dr, dg, db, da, sr, sg, sb, sa, ar, ag, ab, aa, dirx, diry, dirz;
local posx, posy, posz = 0, 0, 0;

local bLight1	= false;
local bLight2	= false;
local bLight3	= false;
local bLight4	= false;

local function UpdateLighting()
	dt = dt + GetDeltaTime();
	
	if(bLight2)
	then
		DrawDynamicObject(lightning_b);
		DrawDynamicObject(lightning_c);
		DrawDynamicObject(flarik2);
		DrawDynamicObject(flarik3);
	end
	
	if((dt > 40.1) and (not bLight1) and (not bLight2) and (not bLight3))
	then
		SetLight(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -posx, -posy, -posz);
		SetVisible(object, true);
		SetVisible(lightning_b, false);
		SetVisible(lightning_c, false);
		SetVisible(flarik1, true);
		SetVisible(flarik2, false);
		SetVisible(flarik3, false);
		bLight1 = true;
		bLight2 = false;
		if(not PlaySpecialSound3D("thunder", posx, posy, posz, false, true))
		then
			PlaySpecialSound3D("thunder2", posx, posy, posz, false, true);
		end
	end
	if((dt > 40.15) and bLight1 and (not bLight2) and (not bLight3))
	then
		SetVisible(object, true);
		SetVisible(lightning_b, true);
		SetVisible(lightning_c, false);
		SetVisible(flarik1, true);
		SetVisible(flarik2, true);
		SetVisible(flarik3, false);
		bLight2 = true;
	end
	if((dt > 40.2) and bLight2 and bLight1 and (not bLight3) )
	then
		SetVisible(object, false);
		SetVisible(lightning_b, true);
		SetVisible(lightning_c, true);
		SetVisible(flarik1, false);
		SetVisible(flarik2, true);
		SetVisible(flarik3, true);
		bLight3 = true;
	end
	if((dt > 40.30) and bLight3 and bLight2 and bLight1 and (not bLight4))
	then
		SetVisible(object, false);
		SetVisible(lightning_b, false);
		SetVisible(lightning_c, true);
		SetVisible(flarik1, false);
		SetVisible(flarik2, false);
		SetVisible(flarik3, true);
		bLight4 = true;
	end
	if((dt > 40.35) and bLight3 and bLight2 and bLight1 and bLight4)
	then
		SetVisible(object, false);
		SetVisible(lightning_b, false);
		SetVisible(lightning_c, false);
		SetVisible(flarik1, false);
		SetVisible(flarik2, false);
		SetVisible(flarik3, false);
		SetLight2(dr, dg, db, da, sr, sg, sb, sa, ar, ag, ab, aa, dirx, diry, dirz);
		bLight1 = false;
		bLight2 = false;
		bLight3 = false;
		bLight4 = false;
		dt = 0;
	end
end

local function ExecuteCommands()
	while(SetNextCommand(script))
	do
		local command = GetCurrentCommand(script);
		if (commandMap[command])
		then
			commandMap[command]();
		end
	end
end

local function AddObject()
	object = StringToPointer(GetCurrentParams(script));
	dr, dg, db, da, sr, sg, sb, sa, ar, ag, ab, aa, dirx, diry, dirz = GetLight();
	posx, posy, posz = GetPosition(object);
	SetVisible(object, false);
	lightning_b = FindObject(object, "lightning_b");
	lightning_c = FindObject(object, "lightning_c");
	flarik1 = FindObject(object, "flarik1");
	flarik2 = FindObject(object, "flarik2");
	flarik3 = FindObject(object, "flarik3");
end

local function DeleteObject()
	StopSpecialSound("thunder");
	StopSpecialSound("thunder2");
	Release(object);
	object = nil;
	Release(lightning_b);
	lightning_b = nil;
	Release(lightning_c);
	lightning_c = nil;
	Release(flarik1);
	flarik1 = nil;
	Release(flarik2);
	flarik2 = nil;
	Release(flarik3);
	flarik3 = nil;
	
	StopScript(script);
end

commandMap["add_object"] = AddObject;
commandMap["delete_object"] = DeleteObject;
commandMap["release_all"] = DeleteObject;

while(true)
do
	ExecuteCommands();
	UpdateLighting();
	Update();
end
