local script = globalObject;

local interface;
local commandMap = {};

local game_started = false;
local active_side = "";
local ingame = false;
local damaged  = false;
local damaged_time = 0;
local game_finish = false;
--local pArrow = CreateArrow("arrow.rdf", 2);
local points = 0;

--zil_130 			azot_flame --zil_130_
--uaz_469 			galogen_lights --uaz
--gaz_z110 			force_wall --gaz_3110_main
--audi_tt 			tornado
--bmw_i745 			lifting --bmw
--Volkswaken_Touareg	outforce --volswaken_toureg	 
local medal = 0;

local cars_abilities = {};
cars_abilities["zil_130"]		= "azot_flame";
cars_abilities["uaz_469"]	= "galogen_lights";
cars_abilities["gaz_z110"]	= "force_wall";   
cars_abilities["audi_tt"]		= "tornado";
cars_abilities["bmw_i745"] 	= "lifting"; 
cars_abilities["volk_t"] 		= "outforce"; 

local cars_abilities_hollow = {};
cars_abilities_hollow["zil_130"]	= "hollow_azot_flame";
cars_abilities_hollow["uaz_469"]	= "hollow_galogen_lights";
cars_abilities_hollow["gaz_z110"]	= "hollow_force_wall"; 
cars_abilities_hollow["audi_tt"]	= "hollow_tornado";
cars_abilities_hollow["bmw_i745"]	= "hollow_lifting";
cars_abilities_hollow["volk_t"]		= "hollow_outforce"; 

local leader = "leader_frame"; 

local players = {}; 
players			= SetPlayersPanel();

local dt			= 0;
local ddt			= 0;
local blink_time 	= 0;
local blink_dt 		= 0;
local blink_ddt		= 0;
local blink_abilitiy 	= 0;
local bBlink		= false;

local round = {}; 

round[1] = "round_1"; 
round[2] = "round_2"; 
round[3] = "round_3"; 
round[4] = "round_4"; 
round[5] = "round_5";
round[6] = "round_6";
round[7] = "round_7";
round[8] = "round_8";
round[9] = "round_9";
round[10] = "round_10";

local round_time = {}; 

round_time[1] = "round_1_time";
round_time[2] = "round_2_time";
round_time[3] = "round_3_time";
round_time[4] = "round_4_time";
round_time[5] = "round_5_time";
round_time[6] = "round_6_time";
round_time[7] = "round_7_time";
round_time[8] = "round_8_time";
round_time[9] = "round_9_time";
round_time[10] = "round_10_time";


local function ExecuteCommands()
	while(SetNextCommand(script))
	do
		local command = GetCurrentCommand(script);
		if (commandMap[command])
		then
--			SaveLog(command);
			commandMap[command]();
			
		end
	end
end
local medaltime = 0;
local function ShowMedal(n)
	if (not game_finish)
	then
		SetWindowVisible(interface, "gold", false);		
		SetWindowVisible(interface, "silver", false);		
		SetWindowVisible(interface, "bronze", false);		
		medaltime = 0;
		medal = tonumber(n);
		SetMedal(medal);
		if (n == 3)
		then
			SetWindowVisible(interface, "gold", true);		
		elseif (n == 2)
		then
			SetWindowVisible(interface, "silver", true);
		elseif (n == 1)
		then
			SetWindowVisible(interface, "bronze", true);
		end
		
		local m = GetMedalType(GetCurrentRace());
		if (medal > m)
		then
			SetMedalType(GetCurrentRace(), medal);
		end
		if (not IsDeathRace())
		then
		
			SendCommandByName("game_interface", "show_message", "finish!");
		end
	end
end


local function ShowMedalCommand()
	local n = tonumber(GetCurrentParams(script));
	ShowMedal(n);
	UpdateInterfaceBuffers(interface);
end

local function DrawAbilities()
--~ 	local n, max_ab	= ActivateAbilities();
--~ 	local acar			= GetActiveCar();
--~ 	
--~ 	local height = (46 / max_ab) * n;
--~ 	SetWindowSize(interface, "bar_ab_f", 6, -height);
--~ 	
--~ 	if((n == 0) and (not bBlink))
--~ 	then
--~ 		SetWindowVisible(interface, cars_abilities_hollow[acar], true);
--~ 		SetWindowVisible(interface, cars_abilities[acar], false);
--~ 	elseif(not bBlink)
--~ 	then
--~ 		SetWindowVisible(interface, cars_abilities_hollow[acar], false);
--~ 		SetWindowVisible(interface, cars_abilities[acar], true);
--~ 	end
--~ 	
--~ 	UpdateInterfaceBuffers(interface);
end

local function Blink()
--~ 	if(bBlink)
--~ 	then		
--~ 		blink_dt = blink_dt + GetDeltaTime();
--~ 		if(blink_dt < blink_time)
--~ 		then
--~ 			if(0 == blink_abilitiy)
--~ 			then
--~ 				blink_dt = 0.0;
--~ 				bBlink = false;
--~ 				DrawAbilities();
--~ 				return;
--~ 			end
--~ 			
--~ 			local acar = GetActiveCar();
--~ 			
--~ 			blink_ddt = blink_ddt + GetDeltaTime();
--~ 			
--~ 			if(blink_ddt > 0.4)
--~ 			then
--~ 				blink_ddt = 0.0;
--~ 			elseif(blink_ddt > 0.2)
--~ 			then
--~ 				SetWindowVisible(interface, cars_abilities[acar], true);
--~ 				SetWindowVisible(interface, cars_abilities_hollow[acar], false);
--~ 			else
--~ 				SetWindowVisible(interface, cars_abilities_hollow[acar], true);
--~ 				SetWindowVisible(interface, cars_abilities[acar], false);
--~ 			end
--~ 		else
--~ 			blink_dt = 0.0;
--~ 			bBlink = false;
--~ 			DrawAbilities();
--~ 		end
--~ 	end
end

local function DrawEnemiesPoints()
	local size;
	local map = {}; 
	size, map = GetAIPositions();
	
	local f = 1;
	local j = 1;
--	SaveLog(size);
	for i = 1, size, 3 
	do 
		local wnd = "enemy_point"..j;
--		if(true == map[i + 2])
--		then
--			wnd = "friend_point"..f;
--			f = f + 1;
--		else
			j = j + 1;
--		end
--		SaveLog(wnd);
		SetWindowPosition(interface, wnd,  map[i], map[i+1]);
		SetWindowVisible(interface, wnd, true);
	end 
	UpdateInterfaceBuffers(interface);
end

local function DrawSummonPanel()
	local mana = GetPlayerMana();
	local height = (46 / 100) * mana;
	SetWindowSize(interface, "bar_summon_f", 6, -height);
	UpdateInterfaceBuffers(interface);
end


local lastscore = 0;
local function DrawPlayersPanel()


	SetWindowVisible(interface, "1st", false);
	SetWindowVisible(interface, "2nd", false);
	SetWindowVisible(interface, "3rd", false);
	SetWindowVisible(interface, "4th", false);
	SetWindowVisible(interface, "5th", false);
	SetWindowVisible(interface, "6th", false);

	if (not IsDeathRace())
	then
		players = SetPlayersPanel(); 
		
	--	SetPlayerName("viper");
		local cur_car = GetPlayerName(); 
		for i = 1, 6 do
		--	SaveLog("Player "..players[i]);
			if (players[i] == "10") 
			then
					if (i == 1)
					then
						SetWindowVisible(interface, "1st", true);
					elseif (i == 2)
					then
						SetWindowVisible(interface, "2nd", true);
					elseif (i == 3)
					then
						SetWindowVisible(interface, "3rd", true);
					elseif (i == 4)
					then
						SetWindowVisible(interface, "4th", true);
					elseif (i == 5)
					then
						SetWindowVisible(interface, "5th", true);
					elseif (i == 6)
					then
						SetWindowVisible(interface, "6th", true);
					end
			end
		end 
		
		local num = points;
		SetText(interface, "low_points_text", string.format("%0.3d", num));
		lastscore = num;
		
	else
		local num = points;
		SetText(interface, "up_points_text", string.format("%0.3d", num));
		if (num >= GetMedalPoints(medal + 1) and lastscore < GetMedalPoints(medal + 1))
		then
			medal = medal + 1;
			ShowMedal(medal);
			if (medal > 3)
			then
				medal = 3;
			end
		end
		lastscore = num;
	end
	
	UpdateInterfaceBuffers(interface);
end

local function InitPointsWindows()

	if (not IsDeathRace())
	then
		SetText(interface, "up_points_text", "");
		SetWindowVisible(interface, "up_points_txt", false);
		
		SetText(interface, "low_points_text", "000");
		SetWindowVisible(interface, "low_points_wnd", true);
		SetWindowVisible(interface, "low_points_txt", true);
		
		SetText(interface, "mid_points_text", "");
		SetWindowVisible(interface, "mid_points_wnd", false);
		SetText(interface, "mid_points_txt", "");
		
		SetWindowVisible(interface, "time_wnd", false);
	  SetText(interface, "time", "");
		
		SetWindowVisible(interface, "armor_wnd", true);
		SetWindowVisible(interface, "life1", true);
		SetWindowVisible(interface, "life2", true);
		SetWindowVisible(interface, "life3", true);
		SetWindowVisible(interface, "life4", true);
		SetWindowVisible(interface, "life5", true);
		SetWindowVisible(interface, "life6", true);
		SetWindowVisible(interface, "life7", true);
		SetWindowVisible(interface, "life8", true);
--		SetWindowVisible(interface, "life9", true);
--		SetWindowVisible(interface, "life10", true);
		
	else
		SetWindowVisible(interface, "time_wnd", true);
		SetText(interface, "time", "");
	
		SetText(interface, "mid_points_text", "000");
		SetWindowVisible(interface, "mid_points_wnd", true);
		SetTextString(interface, "mid_points_txt", "bronze1");
	
		SetText(interface, "low_points_text", "");
		SetWindowVisible(interface, "low_points_wnd", false);
		SetWindowVisible(interface, "low_points_txt", false);
		
		SetText(interface, "up_points_text", "000");
		SetWindowVisible(interface, "up_points_txt", true);
		
		SetWindowVisible(interface, "armor_wnd", false);
		SetWindowVisible(interface, "life1", false);
		SetWindowVisible(interface, "life2", false);
		SetWindowVisible(interface, "life3", false);
		SetWindowVisible(interface, "life4", false);
		SetWindowVisible(interface, "life5", false);
		SetWindowVisible(interface, "life6", false);
		SetWindowVisible(interface, "life7", false);
		SetWindowVisible(interface, "life8", false);
--		SetWindowVisible(interface, "life9", false);
--		SetWindowVisible(interface, "life10", false);
	end
end

local function DrawRoundsInfo()
	
	local zero = "0"; 
	local lap_time = {};
	local count, lap_time = UpdateRoundsTime();
	
	local str = {}; 
	local vis = {};
	
	local j = 1;
	for i = 1, count, 4 do
		str[j] = string.format("%0.2d:%0.2d:%0.2d ", lap_time[i], lap_time[i+1], lap_time[i+2]);
		vis[j] = lap_time[i+3];
		j = j + 1
	end
	
	
	for i = 1, 10 do 
		SetWindowVisible(interface, round[i], vis[i]); 
		if(vis[i])
		then
			SetText(interface, round_time[i], str[i]);
		end;
		SetWindowVisible(interface, round_time[i], vis[i])
	end 
end



local function DrawRoundsUpheader()
	if (IsDeathRace())
	then
			local round_curr 	= GetCurrentRound();
			SetText(interface, "round_curr2", round_curr); 
			SetText(interface, "round_curr", ""); 
			SetText(interface, "rounds", ""); 
			SetText(interface, "slash", ""); 
	else
		local rounds 	= GetRoundsNumber(); 
		local round_curr 	= GetCurrentRound();
		if(round_curr <= rounds)
		then
			SetText(interface, "round_curr2", ""); 
			SetText(interface, "round_curr", round_curr); 
			SetText(interface, "rounds", rounds); 
			SetTextString(interface, "slash", "laps"); 
		end
	end
end 

local function DrawTime()
	if (IsDeathRace())
	then
		local 	minutes, secs, msecs = GetTime(); 
		local 	str = string.format("%0.2d:%0.2d:%0.2d ", minutes, secs, msecs);
		SetText(interface, "time", str); 
	end
end

local bSign 		= false;
local bSignVisible	= true;
local function StopSign()
--~ 	local pangle, spangle = GetPlayerSplineAngle();
--~ 	local angle = pangle - spangle;
--~ 	while(angle > 6.283)
--~ 	do
--~ 		angle = angle - 6.283;
--~ 	end
--~ 	while(angle < 0)
--~ 	do
--~ 		angle = angle + 6.283;
--~ 	end

--~ 	if((angle > 1.570) and (angle < 4.713))
--~ 	then
--~ 		bSign = true;
--~ 	else
--~ 		if(bSignVisible)
--~ 		then
--~ 			SetWindowVisible(interface, "stopsign", false);
--~ 		end
--~ 		bSign = false;
--~ 	end
	
	if(IsPlayerMovingBackwards())
	then
		bSign = true;
	else
		if(bSignVisible)
		then
			SetWindowVisible(interface, "stopsign", false);
		end
		bSign = false;
	end
	
--	SaveLog("updateinterface3_5");

	if(bSign)
	then
		dt = dt + GetDeltaTime();
		if(dt > 0.5)
		then
			if(bSignVisible)
			then
				SetWindowVisible(interface, "stopsign", false);
				bSignVisible = false;
				dt = 0;
			else
				SetWindowVisible(interface, "stopsign", true);
				bSignVisible = true;
				dt = 0;
			end
		end
	end
end

local function DrawEnergy()
	local arm, maxarm = GetPlayerArmor();
	local percent = tonumber(arm) / tonumber(maxarm) * 8.0;
	if (arm > 0 and percent < 1)
	then
		percent = 1;
	end
	for i = 1,8	do
		local sss = string.format("life%d", i);
		SetWindowVisible(interface, sss, false);
		
	end
	
	for i = 1,percent	do
		local sss = string.format("life%d", i);
		SetWindowVisible(interface, sss, true);
		
	end
end
local function DrawNitro()
	local arm = GetPlayerNitro();
	local percent = tonumber(arm) / 100.0 * 8.0;
	if (arm > 0 and percent < 1)
	then
		percent = 1;
	end
	for i = 1,8	do
		local sss = string.format("nitro%d", i);
		SetWindowVisible(interface, sss, false);
		
	end
	for i = 1,percent	do
		local sss = string.format("nitro%d", i);
		SetWindowVisible(interface, sss, true);
		
	end
end
local function DrawHealth()
	if(bDrawHealth)
	then
		SetWindowVisible(interface, "helth_bar", true);
		SetWindowPosition(interface, "helth_bar", GetHelthBarCoord(pCar));
	end
end

local HelthBar = 
{
	fDt		= 0.0;
	pCar0		= nil;
	bDraw0	= false;
	pCar1		= nil;
	bDraw1	= false;
	pCar2		= nil;
	bDraw2	= false;
	pCar3		= nil;
	bDraw3	= false;
	pCar4		= nil;
	bDraw4	= false;
	pCar5		= nil;
	bDraw5	= false;
	Draw		= nil;
	Reset		= nil;
};

HelthBar.Draw = function ()
	if(HelthBar.bDraw0)
	then
		local x, y, width = GetHelthBarCoord(HelthBar.pCar0);
		if(width < 0.0)
		then
			width = 0.0;
		end
		
		SetWindowPosition(interface, "health0", x, y);
		SetWindowPosition(interface, "health_f0", x + 2, y - 8);
		
		SetWindowSize(interface, "health_f0", width, 6);
		
		SetWindowVisible(interface, "health0", true);
		SetWindowVisible(interface, "health_f0", true);
		
--~ 		HelthBar.fDt = HelthBar.fDt +GetDeltaTime();
--~ 		if(HelthBar.fDt >= 5.0)
--~ 		then
--~ 			HelthBar.fDt	= 0.0;
--~ 			HelthBar.bDraw	= false;
--~ 			
--~ 			SetWindowVisible(interface, "health", false);
--~ 			SetWindowVisible(interface, "health_f", false);
--~ 		end
	end
	if(HelthBar.bDraw1)
	then
		local x, y, width = GetHelthBarCoord(HelthBar.pCar1);
		if(width < 0.0)
		then
			width = 0.0;
		end
		
		SetWindowPosition(interface, "health1", x, y);
		SetWindowPosition(interface, "health_f1", x + 2, y - 8);
		
		SetWindowSize(interface, "health_f1", width, 6);
		
		SetWindowVisible(interface, "health1", true);
		SetWindowVisible(interface, "health_f1", true);
		
--~ 		HelthBar.fDt = HelthBar.fDt +GetDeltaTime();
--~ 		if(HelthBar.fDt >= 5.0)
--~ 		then
--~ 			HelthBar.fDt	= 0.0;
--~ 			HelthBar.bDraw	= false;
--~ 			
--~ 			SetWindowVisible(interface, "health", false);
--~ 			SetWindowVisible(interface, "health_f", false);
--~ 		end
	end
	if(HelthBar.bDraw2)
	then
		local x, y, width = GetHelthBarCoord(HelthBar.pCar2);
		if(width < 0.0)
		then
			width = 0.0;
		end
		
		SetWindowPosition(interface, "health2", x, y);
		SetWindowPosition(interface, "health_f2", x + 2, y - 8);
		
		SetWindowSize(interface, "health_f2", width, 6);
		
		SetWindowVisible(interface, "health2", true);
		SetWindowVisible(interface, "health_f2", true);
		
--~ 		HelthBar.fDt = HelthBar.fDt +GetDeltaTime();
--~ 		if(HelthBar.fDt >= 5.0)
--~ 		then
--~ 			HelthBar.fDt	= 0.0;
--~ 			HelthBar.bDraw	= false;
--~ 			
--~ 			SetWindowVisible(interface, "health", false);
--~ 			SetWindowVisible(interface, "health_f", false);
--~ 		end
	end
	if(HelthBar.bDraw3)
	then
		local x, y, width = GetHelthBarCoord(HelthBar.pCar3);
		if(width < 0.0)
		then
			width = 0.0;
		end
		
		SetWindowPosition(interface, "health3", x, y);
		SetWindowPosition(interface, "health_f3", x + 2, y - 8);
		
		SetWindowSize(interface, "health_f3", width, 6);
		
		SetWindowVisible(interface, "health3", true);
		SetWindowVisible(interface, "health_f3", true);
		
--~ 		HelthBar.fDt = HelthBar.fDt +GetDeltaTime();
--~ 		if(HelthBar.fDt >= 5.0)
--~ 		then
--~ 			HelthBar.fDt	= 0.0;
--~ 			HelthBar.bDraw	= false;
--~ 			
--~ 			SetWindowVisible(interface, "health", false);
--~ 			SetWindowVisible(interface, "health_f", false);
--~ 		end
	end
	if(HelthBar.bDraw4)
	then
		local x, y, width = GetHelthBarCoord(HelthBar.pCar4);
		if(width < 0.0)
		then
			width = 0.0;
		end
		
		SetWindowPosition(interface, "health4", x, y);
		SetWindowPosition(interface, "health_f4", x + 2, y - 8);
		
		SetWindowSize(interface, "health_f4", width, 6);
		
		SetWindowVisible(interface, "health4", true);
		SetWindowVisible(interface, "health_f4", true);
		
--~ 		HelthBar.fDt = HelthBar.fDt +GetDeltaTime();
--~ 		if(HelthBar.fDt >= 5.0)
--~ 		then
--~ 			HelthBar.fDt	= 0.0;
--~ 			HelthBar.bDraw	= false;
--~ 			
--~ 			SetWindowVisible(interface, "health", false);
--~ 			SetWindowVisible(interface, "health_f", false);
--~ 		end
	end
	if(HelthBar.bDraw5)
	then
		local x, y, width = GetHelthBarCoord(HelthBar.pCar5);
		if(width < 0.0)
		then
			width = 0.0;
		end
		
		SetWindowPosition(interface, "health5", x, y);
		SetWindowPosition(interface, "health_f5", x + 2, y - 8);
		
		SetWindowSize(interface, "health_f5", width, 6);
		
		SetWindowVisible(interface, "health5", true);
		SetWindowVisible(interface, "health_f5", true);
		
--~ 		HelthBar.fDt = HelthBar.fDt +GetDeltaTime();
--~ 		if(HelthBar.fDt >= 5.0)
--~ 		then
--~ 			HelthBar.fDt	= 0.0;
--~ 			HelthBar.bDraw	= false;
--~ 			
--~ 			SetWindowVisible(interface, "health", false);
--~ 			SetWindowVisible(interface, "health_f", false);
--~ 		end
	end
end
HelthBar.Delete = function ()
		HelthBar.pCar0 = nil;
		HelthBar.pCar1 = nil;
		HelthBar.pCar2 = nil;
		HelthBar.pCar3 = nil;
		HelthBar.pCar4 = nil;
		HelthBar.pCar5 = nil;
		HelthBar.bDraw0	= false;
		HelthBar.bDraw1	= false;
		HelthBar.bDraw2	= false;
		HelthBar.bDraw3	= false;
		HelthBar.bDraw4	= false;
		HelthBar.bDraw5	= false;
end

HelthBar.Reset = function ()
	local pCar, _num = GetCurrentParams(script);
	
	local num = tonumber(_num);
	if (num == 0)
	then
		HelthBar.pCar0 = StringToPointer(pCar);
		HelthBar.bDraw0	= true;
	elseif (num == 1)
	then
		HelthBar.pCar1 = StringToPointer(pCar);
		HelthBar.bDraw1	= true;
	elseif (num == 2)
	then
		HelthBar.pCar2 = StringToPointer(pCar);
		HelthBar.bDraw2	= true;
	elseif (num == 3)
	then
		HelthBar.pCar3 = StringToPointer(pCar);
		HelthBar.bDraw3	= true;
	elseif (num == 4)
	then
		HelthBar.pCar4 = StringToPointer(pCar);
		HelthBar.bDraw4	= true;
	elseif (num == 5)
	then
		HelthBar.pCar5 = StringToPointer(pCar);
		HelthBar.bDraw5	= true;
	end
	HelthBar.fDt	= 0.0;
end

local resultstime = -1;
local res1 = 0;
local res2 = 0;
local res3 = 0;

local function ShowResults(n)
	if (n == 0)
	then
			SetWindowVisible(interface, "results_back", false);
			SetWindowVisible(interface, "results_line1", false);
			SetWindowVisible(interface, "results_line2", false);
			SetWindowVisible(interface, "results_header", false);
			SetText(interface, "race_type", "");
			SetText(interface, "epoints", "");
			SetText(interface, "medal", "");
			SetText(interface, "totalmoney", "");
			SetText(interface, "epoints_credits", "");
			SetText(interface, "medal_credits", "");
			SetText(interface, "ttl", "");
			resultstime = -1;
			res1 = 0;
			res2 = 0;
			res3 = 0;
			medal = 0;
	elseif (n == 1)
	then
			SetText(interface, "mid_points_text", "");
			SetWindowVisible(interface, "mid_points_wnd", false);
			SetText(interface, "mid_points_txt", "");
			SetWindowVisible(interface, "results_back", true);
			SetWindowVisible(interface, "results_line1", true);
			SetWindowVisible(interface, "results_line2", true);
			SetWindowVisible(interface, "results_header", true);
			if (IsDeathRace())
			then
				SetTextString(interface, "race_type", "deathrace");
			else
				SetTextString(interface, "race_type", "circuitrace");
			end
			SetText(interface, "epoints", "");
			SetText(interface, "medal", "");
			SetText(interface, "totalmoney", "");
			SetText(interface, "epoints_credits", "");
			SetText(interface, "medal_credits", "");
			SetText(interface, "ttl", "");
			resultstime = 0;
			res1 = 0;
			res2 = 0;
			res3 = 0;
--			medal = 0;
	end
end

local TIMETOSHOW = 1.5;

local function SetWindowSizeAndPos(wnd, init_width, init_height, init_y, percent)
--	percent = math.max(1, percent);
	SetWindowSize(interface, wnd, init_width * percent, init_height * percent);
	SetWindowPosition(interface, wnd, 512 - init_width * percent / 2.0, init_y - init_height * percent / 2.0);
end

local KillMessage = 
{
	Initialize	= nil;
	ShowMsg	= nil;
	Update	= nil;
	Release	= nil;
	dt		= 0.0;
	wnd		= "";
	bShow	= false;
	type = 1;
};

KillMessage.Initialize = function ()
	SetWindowVisible(interface, "corpse1", false);
	SetWindowVisible(interface, "corpse2", false);
	SetWindowVisible(interface, "corpse3", false);
	
	KillMessage.dt 		= 0.0;
	KillMessage.bShow 	= false;
	KillMessage.wnd 	= "";
	KillMessage.type = 1;
end

KillMessage.ShowMsg = function ()
	if(KillMessage.bShow)
	then
			SetWindowVisible(interface, KillMessage.wnd, false);
			KillMessage.type = math.min(3, KillMessage.type + 1);
	end
	KillMessage.dt		= 0.0;
	KillMessage.bShow	= true;
	
	KillMessage.wnd = GetCurrentParams(script)..KillMessage.type;
--	SaveLog(GameMessage.wnd);
	
	if (not game_finish)
	then
			TIMETOSHOW = 1.0;
			SetWindowVisible(interface, KillMessage.wnd, true);
			if (IsDeathRace() and KillMessage.type < 2)
			then
				SetWindowVisible(interface, KillMessage.wnd, false);
			end
	end	
	
end

KillMessage.Update = function ()
	if(KillMessage.dt < TIMETOSHOW)
	then
		KillMessage.dt = KillMessage.dt + GetDeltaTime();
		if (KillMessage.type == 1)
		then
			SetWindowSizeAndPos(KillMessage.wnd, 290, 194, 200, (100 - (KillMessage.dt / TIMETOSHOW) * (KillMessage.dt / TIMETOSHOW) * (KillMessage.dt / TIMETOSHOW) * 100.0) * 0.015);
		elseif (KillMessage.type == 2)
		then
			SetWindowSizeAndPos(KillMessage.wnd, 284, 212, 200, (100 - (KillMessage.dt / TIMETOSHOW) * (KillMessage.dt / TIMETOSHOW) * (KillMessage.dt / TIMETOSHOW) * 100.0) * 0.015);
		elseif (KillMessage.type == 3)
		then
			SetWindowSizeAndPos(KillMessage.wnd, 473, 206, 200, (100 - (KillMessage.dt / TIMETOSHOW) * (KillMessage.dt / TIMETOSHOW) * (KillMessage.dt / TIMETOSHOW) * 100.0) * 0.015);
		end
	elseif(KillMessage.bShow)
	then
		SetWindowVisible(interface, KillMessage.wnd, false);
		KillMessage.bShow = false;
		KillMessage.type = 1;
	end
end



local GameMessage = 
{
	Initialize	= nil;
	ShowMsg	= nil;
	Update	= nil;
	Release	= nil;
	dt		= 0.0;
	wnd		= "";
	bShow	= false;
	
	cur_x = 0;
};

GameMessage.Initialize = function ()
	SetWindowVisible(interface, "uskorenie!", false);
	SetWindowVisible(interface, "zamedlenie!", false);
	SetWindowVisible(interface, "remont!", false);
	SetWindowVisible(interface, "zavulon!", false);
	SetWindowVisible(interface, "gesser!", false);
	SetWindowVisible(interface, "uron_x2!", false);
	SetWindowVisible(interface, "sposobnost+1!", false);
	SetWindowVisible(interface, "sposobnost-1!", false);
	SetWindowVisible(interface, "start!", false);
	SetWindowVisible(interface, "finish!", false);
	SetWindowVisible(interface, "krug_proiden!", false);
	SetWindowVisible(interface, "proigrish!_1", false);
	SetWindowVisible(interface, "proigrish!_2", false);
	SetWindowVisible(interface, "ready!", false);
	SetWindowVisible(interface, "steady!", false);
	SetWindowVisible(interface, "lost!", false);
	
	GameMessage.dt 		= 0.0;
	GameMessage.bShow 	= false;
	GameMessage.wnd 	= "";
end

GameMessage.ShowMsg = function ()
	if(GameMessage.bShow)
	then
			SetWindowVisible(interface, GameMessage.wnd, false);
	end
	GameMessage.dt		= 0.0;
	GameMessage.bShow	= true;
	
	GameMessage.wnd = GetCurrentParams(script);
--	SaveLog(GameMessage.wnd);
	
	if (not game_finish)
	then
		if ("nil!" ~= GameMessage.wnd)
		then
			PlaySpecialSound("text");
		end
		if("proigrish!" == GameMessage.wnd)
		then
			SetWindowVisible(interface, "proigrish!_1", true);
			SetWindowVisible(interface, "proigrish!_2", true);
			game_finish = true;
			SetLevelStarted(false);
			TIMETOSHOW = 1.5;
		elseif ("finish!" == GameMessage.wnd)
		then
			game_finish = true;
			SetLevelStarted(false);
			SetWindowVisible(interface, GameMessage.wnd, true);
			TIMETOSHOW = 2.5;
	  elseif ("lost!" == GameMessage.wnd)
		then
			game_finish = true;
			SetLevelStarted(false);
			SetWindowVisible(interface, GameMessage.wnd, true);
			TIMETOSHOW = 2.5;
		elseif ("posledniy_krug!" == GameMessage.wnd)
		then
			TIMETOSHOW = 1.5;
			PlaySpecialSound("start_a");
			SetWindowVisible(interface, GameMessage.wnd, true);
		else
			TIMETOSHOW = 1.5;
			SetWindowVisible(interface, GameMessage.wnd, true);
		end
		
		if (GameMessage.wnd ~= "nil!" and GameMessage.wnd ~= "start!" and GameMessage.wnd ~= "ready!" and GameMessage.wnd ~= "steady!")
		then
			SetWindowSizeAndPos(GameMessage.wnd, 363,190, 350, 0);
		end
	end
	
	
	if("start!" == GameMessage.wnd)
	then
		game_started = true;
		SendCommandByName("jeep", "game_started", "");
		SetLevelStarted(true);
	end
	
	UpdateInterfaceBuffers(interface);
	
end

GameMessage.Update = function ()
	if(GameMessage.dt < TIMETOSHOW)
	then
		GameMessage.dt = GameMessage.dt + GetDeltaTime();

		if (GameMessage.wnd == "finish!")
		then
			SetWindowSizeAndPos(GameMessage.wnd, 363,190, 350, (0 + (GameMessage.dt / TIMETOSHOW) * (GameMessage.dt / TIMETOSHOW) * (GameMessage.dt / TIMETOSHOW) * 100.0) * 0.015);
		elseif (GameMessage.wnd == "lost!")
		then
			SetWindowSizeAndPos(GameMessage.wnd, 285,159, 350, (0 + (GameMessage.dt / TIMETOSHOW) * (GameMessage.dt / TIMETOSHOW) * (GameMessage.dt / TIMETOSHOW) * 100.0) * 0.015);
		elseif (GameMessage.wnd == "krug_proiden!")
		then
			SetWindowSizeAndPos(GameMessage.wnd, 432,94, 350, (0 + (GameMessage.dt / TIMETOSHOW) * (GameMessage.dt / TIMETOSHOW) * (GameMessage.dt / TIMETOSHOW) * 100.0) * 0.015);
		elseif (GameMessage.wnd == "posledniy_krug!")
		then
			SetWindowSizeAndPos(GameMessage.wnd, 432,104, 350, (0 + (GameMessage.dt / TIMETOSHOW) * (GameMessage.dt / TIMETOSHOW) * (GameMessage.dt / TIMETOSHOW) * 100.0) * 0.015);
--~ 		elseif (GameMessage.wnd == "start!")
--~ 		then
--~ 			SetWindowSizeAndPos(GameMessage.wnd, 271,94, 350, (0 + (GameMessage.dt / TIMETOSHOW) * (GameMessage.dt / TIMETOSHOW) * (GameMessage.dt / TIMETOSHOW) * 100.0) * 0.015);
--~ 		elseif (GameMessage.wnd == "ready!")
--~ 		then
--~ 			SetWindowSizeAndPos(GameMessage.wnd, 271,94, 350, (0 + (GameMessage.dt / TIMETOSHOW) * (GameMessage.dt / TIMETOSHOW) * (GameMessage.dt / TIMETOSHOW) * 100.0) * 0.015);
--~ 		elseif (GameMessage.wnd == "steady!")
--~ 		then
--~ 			SetWindowSizeAndPos(GameMessage.wnd, 271,94, 350, (0 + (GameMessage.dt / TIMETOSHOW) * (GameMessage.dt / TIMETOSHOW) * (GameMessage.dt / TIMETOSHOW) * 100.0) * 0.015);
--~ 		elseif (GameMessage.wnd == "nil!")
--~ 		then
--~ 			SetWindowSizeAndPos(GameMessage.wnd, 271,94, 350, (0 + (GameMessage.dt / TIMETOSHOW) * (GameMessage.dt / TIMETOSHOW) * (GameMessage.dt / TIMETOSHOW) * 100.0) * 0.015);
		end


	elseif(GameMessage.bShow)
	then
		if("finish!" == GameMessage.wnd)
		then
			SetWindowVisible(interface, GameMessage.wnd, false);
			ShowResults(1);
--			SendCommandByName("main", "player_finish", "");
		elseif("lost!" == GameMessage.wnd)
		then
			SetWindowVisible(interface, GameMessage.wnd, false);
			SendCommandByName("main", "player_exit", "");
		else
			SetWindowVisible(interface, GameMessage.wnd, false);
		end
		GameMessage.bShow = false;
	end
end


local function InitMedals()
		SetWindowVisible(interface, "gold", false);
		SetWindowVisible(interface, "silver", false);
		SetWindowVisible(interface, "bronze", false);
--		SetText(interface, "pointsformedal", "");
end


local function ShowMedalText()
	if (medal < 3)
	then
		local sss = "";
		if (medal + 1 == 1)
		then
			sss = "bronze1";
		elseif (medal + 1 == 2)
		then
			sss = "silver1";
		elseif (medal + 1 == 3)
		then
			sss = "gold1";
		end	
    SetTextString(interface, "mid_points_txt", sss);
    SetText(interface, "mid_points_text", string.format("%0.3d", GetMedalPoints(medal + 1)));
--		SetText(interface, "pointsformedal", string.format("EARN %d POINTS FOR %s MEDAL", GetMedalPoints(medal + 1), sss));
--	else
--		SetText(interface, "pointsformedal", "");
	end
end

local function UpdateMedals()
	if (medaltime < 1.5)
	then
		medaltime = medaltime + GetDeltaTime();
		if (medaltime >= 1.5)
		then
			InitMedals();
		end
	end
end



local function AddObject()
--	SaveLog("add_object");
	local obj = GetCurrentParams(script);
	interface = StringToPointer(obj);
	DrawAbilities(); 
	DrawPlayersPanel(); 
	DrawRoundsInfo(); 
	DrawRoundsUpheader(); 
	DrawTime();
	GameMessage.Initialize();
	KillMessage.Initialize();
		HelthBar.Delete();

--	SaveLog("initialized");
	
	game_started = false;
	dt			= 0;
	blink_time 	= 0;
	blink_dt 		= 0;
	blink_ddt		= 0;
	blink_abilitiy	= 0;
	bBlink		= false;
--	GetPlayerSplineAngle(true);

	SetWindowVisible(interface, "stopsign", false);
--	SetWindowVisible(interface, GetActiveCar().."_", true);
	
		SetWindowVisible(interface, "enemy_point1", false);
		SetWindowVisible(interface, "enemy_point2", false);
		SetWindowVisible(interface, "enemy_point3", false);
		SetWindowVisible(interface, "enemy_point4", false);
		SetWindowVisible(interface, "enemy_point5", false);
		SetWindowVisible(interface, "enemy_point6", false);
	

	SetPlayerMana(0);
	DrawSummonPanel();	
	local acar = GetActiveCar();
	if((acar == "audi_tt") or (acar == "bmw_i745") or (acar == "volk_t"))
	then
		SetWindowVisible(interface, "zavulon", true);
		active_side = "dark";
	else
		SetWindowVisible(interface, "gesser", true);
		active_side = "light";
	end
	
	SetText(interface, "time_curr", "TIME:");
	SetText(interface, "time_best", "BEST:");
	
--	SaveLog("interface_updated");
	ingame = true;
	damaged  = false;
	damaged_time = 0;
	game_finish = false;
	SendCommandByName("game_interface", "show_message", "nil!");
	InitPointsWindows();	
	InitMedals();
	ShowResults(0);
	lastscore = 0;
	points = 0;
	SetMedal(0);
	UpdateInterfaceBuffers(interface);
end

local function DeleteObject()
--~ 	SetWindowVisible(interface, cars_abilities[GetActiveCar()], false);
--~ 	SetWindowVisible(interface, cars_abilities_hollow[GetActiveCar()], false);
--~ 	SetWindowVisible(interface, GetActiveCar().."_", false);
	HelthBar.Delete();
--	SetWindowVisible(interface, "zavulon", false);
--	SetWindowVisible(interface, "gesser", false);
	
	Release(interface);
	interface = nil;
	
	StopScript(script);
	ingame = false;
--	Update();
end

local function ReleaseAll()
--~ 	SetWindowVisible(interface, cars_abilities[GetActiveCar()], false);
--~ 	SetWindowVisible(interface, cars_abilities_hollow[GetActiveCar()], false);
--~ 	SetWindowVisible(interface, GetActiveCar().."_", false);
	
	Release(interface);
	interface = nil;
end

local function AddPoints()
	local num = tonumber(GetCurrentParams(script));
	points = points + num * KillMessage.type;
end

commandMap["release_all"] 		= ReleaseAll;
commandMap["add_object"] 		= AddObject;
commandMap["delete_object"] 	= DeleteObject;
commandMap["redraw_abilities"] 	= DrawAbilities;
commandMap["blink_abilitiy"]		= function () DrawAbilities(); blink_time, blink_abilitiy = GetCurrentParams(script); blink_time = tonumber(blink_time); blink_abilitiy = tonumber(blink_abilitiy); bBlink = true; end;
commandMap["show_health"]		= HelthBar.Reset;
commandMap["show_message"]	= GameMessage.ShowMsg;
commandMap["add_points"]	= AddPoints;
commandMap["kill_message"]	= KillMessage.ShowMsg;
commandMap["show_medal"]	= ShowMedalCommand;

while(true)
do
	ExecuteCommands();
	if (ingame)
	then

			UpdateGameInterface(interface);		
		UpdateInterface(interface);
	--	SaveLog("updateinterface2");
		local x, y = GetWindowPosition(interface, "arrow");
		--DrawArrow(pArrow, 920, 665);
--		DrawArrow(pArrow, x, y);
		DrawEnemiesPoints(); 
		DrawRoundsUpheader();
		DrawRoundsInfo();
		if (not IsDeathRace())
		then
			DrawEnergy();
		else
			if (not game_finish)
			then
	
				ShowMedalText();	
			end
		end
		DrawNitro();
		UpdateMedals();
		if (game_finish)
		then
			if (resultstime >= 0)
			then
				SetText(interface, "mid_points_txt", "");
			SetText(interface, "mid_points_text", "");
			resultstime = resultstime  + GetDeltaTime();
				if (resultstime >= 2 and res1 == 0)
				then
					local sss = string.format("%d earned points", points);
					SetText(interface, "epoints", sss);
					SetText(interface, "epoints_credits", string.format("= %d dollars", points * 10));
					res1 = 1;
				elseif (resultstime >= 4 and res2 == 0)
				then
					local sss = "gold";
					local nn = tonumber(medal);
					if (nn == 3)
					then
						sss = "gold";
					elseif (nn == 2)
					then
						sss = "silver";
					elseif (nn == 1)
					then
						sss = "bronze";
					end
					SetTextString(interface, "medal", sss);
					SetText(interface, "medal_credits", string.format("= %d dollars", nn * 250));
					res2 = 1;
				elseif (resultstime >= 6 and res3 == 0)
				then
					SetTextString(interface, "totalmoney", "totalmoney");
					SetText(interface, "ttl", (GetMoney() + points * 10 + tonumber(GetMedalType(GetCurrentRace())) * 250).." DOLLARS");
					res3 = 1;
				elseif (resultstime >= 9)
				then
					SendCommandByName("main", "player_finish", "");
					SetMoney(GetMoney() + points * 10 + tonumber(GetMedalType(GetCurrentRace())) * 250);
					resultstime = -1;
				end
			end
		end
	--	SaveLog("updateinterface3");
		
		ddt = ddt + GetDeltaTime();
		local dddt;
		if (active_side == "dark")
		then
			dddt = 0.2 / 1.2;
		else
			dddt = 0.2 / 1.6;
		end
		if(ddt >= dddt and game_started)
		then
			local mana = GetPlayerMana();
			if(mana < 100)
			then
				SetPlayerMana((mana + 1));
			end
			
			DrawSummonPanel();
			DrawPlayersPanel();
			ddt = 0.0;
			if (GetPlayerDamage() < 75)
			then
--				SetWindowVisible(interface, GetActiveCar().."_", true);
			end
		end
		
	--	SaveLog("updateinterface3_1");
		HelthBar.Draw();
		GameMessage.Update();
		KillMessage.Update();
	--	SaveLog("updateinterface3_2");
		
		StopSign();
	--	SaveLog("updateinterface3_3");
		Blink();
	--	SaveLog("updateinterface3_4");
		DrawTime();
	--	SaveLog("updateinterface4");
		
		if (GetPlayerDamage() >= 75)
		then
			if (damaged_time > 2 and damaged == true)
			then
				damaged_time = 0;
				damaged = false;
--				SetWindowVisible(interface, GetActiveCar().."_", false);
			end
			if (damaged_time > 0.5 and damaged == false)
			then
				damaged_time = 0;
				damaged = true;
	--			SetWindowVisible(interface, GetActiveCar().."_", true);
				PlaySpecialSound("sos");
			end
			damaged_time = damaged_time + GetDeltaTime();
		end
	end
	Update();
end
