Script:LoadScript("scripts/Default/ClientStuff.lua");		-- derive functionality
Script:LoadScript("SCRIPTS/Client/ClientCmds.lua");
Script:LoadScript("SCRIPTS/MULTIPLAYER/MultiplayerClassDefiniton.lua");		-- global MultiplayerClassDefiniton

ClientStuff.idScoreboard=nil;										-- scoreboard entity id
ClientStuff.Snd1=Sound:LoadSound("Sound/blow.wav");
ClientStuff.Snd2=Sound:LoadSound("Sound/clear.wav");
ClientStuff.Snd3=Sound:LoadSound("Sound/com_followcom.wav");
ClientStuff.Snd4=Sound:LoadSound("Sound/com_getinpos.wav");
ClientStuff.Snd5=Sound:LoadSound("Sound/com_go.wav");
ClientStuff.Snd6=Sound:LoadSound("Sound/com_reportin.wav");
ClientStuff.Snd7=Sound:LoadSound("Sound/ct_affirm.wav");
ClientStuff.Snd8=Sound:LoadSound("Sound/ct_backup.wav");
ClientStuff.Snd9=Sound:LoadSound("Sound/ct_coverme.wav");
ClientStuff.Snd10=Sound:LoadSound("Sound/ct_enemys.wav");
ClientStuff.Snd11=Sound:LoadSound("Sound/ct_fireinhole.wav");
ClientStuff.Snd13=Sound:LoadSound("Sound/ct_inpos.wav");
ClientStuff.Snd16=Sound:LoadSound("Sound/enemydown.wav");
ClientStuff.Snd17=Sound:LoadSound("Sound/fallback.wav");
ClientStuff.Snd18=Sound:LoadSound("Sound/fireassis.wav");
ClientStuff.Snd19=Sound:LoadSound("Sound/followme.wav");
ClientStuff.Snd20=Sound:LoadSound("Sound/getout.wav");
ClientStuff.Snd21=Sound:LoadSound("Sound/go.wav");
ClientStuff.Snd22=Sound:LoadSound("Sound/hitassist.wav");
ClientStuff.Snd24=Sound:LoadSound("Sound/locknload.wav");
ClientStuff.Snd25=Sound:LoadSound("Sound/matedown.wav");
ClientStuff.Snd26=Sound:LoadSound("Sound/meetme.wav");
ClientStuff.Snd27=Sound:LoadSound("Sound/moveout.wav");
ClientStuff.Snd28=Sound:LoadSound("Sound/negative.wav");
ClientStuff.Snd29=Sound:LoadSound("Sound/position.wav");
ClientStuff.Snd30=Sound:LoadSound("Sound/regroup.wav");
ClientStuff.Snd31=Sound:LoadSound("Sound/roger.wav");
ClientStuff.Snd32=Sound:LoadSound("Sound/sticktog.wav");
ClientStuff.Snd33=Sound:LoadSound("Sound/stormfront.wav");

---------------------------------------------------------------------------------
-- This is called whenever an entity is spawned on the client
function ClientStuff:OnSpawnEntity(entity)
	-- in multiplayer we want to have all entities updating even if we don't look at them
	entity:EnableUpdate(1);
	
	if (entity.type == "Player") then
		entity:SetViewDistRatio(255);										 	-- viewdistratio to be max (== 255)
		entity:RenderShadow( 1 );												 	-- enable rendering of player shadow
		BasicPlayer.SecondShader_TeamColoring(entity);		-- enable colorize shader (individual or team color)
		
		Hud:ResetRadar(entity);
	end
	
	if (entity.type == "Synched2DTable") then
		ClientStuff.idScoreboard=entity.id;
	end
end


-------------------------------------------------------------------------------
--
function ClientStuff:SetPlayerScore(Stream)
	local iClientID = Stream:ReadByte();
	local iPing = Stream:ReadShort();

	local SBEntityEnt = System:GetEntity(ClientStuff.idScoreboard);

	-- ClientOnly [ClientID] = Ping, update when the scoreboard packet from the server comes in
	if SBEntityEnt and SBEntityEnt.PingTable then
		SBEntityEnt.PingTable[iClientID]=iPing;
	end
end

-------------------------------------------------------------------------------
--
function ClientStuff:ShowScoreBoard(bShow)	
--	System:Log("ClientStuff:ShowScoreBoard "..tostring(bShow));  -- debug

	if (ScoreBoardManager) then
		ScoreBoardManager.SetVisible(bShow);
	end
end

-------------------------------------------------------------------------------
--
function ClientStuff:ResetScores()
end

-------------------------------------------------------------------------------
--
function ClientStuff:CalcEfficiency(score, deaths, suicides)
	if (not score or not deaths or not suicides) then
		return 0;
	end
	
--	System:Log("CalcEfficiency val "..tostring(score)..","..tostring(deaths)..","..tostring(suicides)); -- debug
--	System:Log("CalcEfficiency type "..type(score)..","..type(deaths)..","..type(suicides)); -- debug
	
	if (score <= 0) then
		return 0;
	end
	
	return floor(max(100 * score / (score + deaths + suicides) + 0.5, 0));
end
---------------------------------------------------------------------------------
-- Hang lejátszás

ClientStuff.ServerCommandTable["PLA"]=function(String,TokTable)
	local tokcount=count(TokTable);
	if tokcount>2 then		
		local team = Game:GetEntityTeam(_localplayer.id);
		if(_localplayer and TokTable[2]==team)then
--			Radar szín változás engedélye
			Hud.talking = 1;
			Hud.plid = tonumber(TokTable[4]);
			local player = System:GetEntity(Hud.plid);
			Game:SoundEvent(player:GetPos(),20,1,Hud.plid);
--			System:ExecuteCommand("sayteam "..TokTable[4]);
			Hud.fRefreshingTime = _time;
			if TokTable[3]=="blow" then			--Get out of here, it's gonna blow!-
				Sound:PlaySound(ClientStuff.Snd1);
			elseif TokTable[3]=="clear" then		--Sector Clear!-
				Sound:PlaySound(ClientStuff.Snd2);  
			elseif TokTable[3]=="cfollow" then		--OK Team, Follow My Command!-
				Sound:PlaySound(ClientStuff.Snd3);
			elseif TokTable[3]=="cgpos" then		--Get in Position, and wait for I go!-
				Sound:PlaySound(ClientStuff.Snd4);
			elseif TokTable[3]=="cgo" then			--GO! GO! GO!
				Sound:PlaySound(ClientStuff.Snd5);
			elseif TokTable[3]=="crep" then			--Report In, Team!
				Sound:PlaySound(ClientStuff.Snd6);
			elseif TokTable[3]=="caffirm" then		--Affirmative!
				Sound:PlaySound(ClientStuff.Snd7);
			elseif TokTable[3]=="cbackup" then		--Need Backup!
				Sound:PlaySound(ClientStuff.Snd8);
			elseif TokTable[3]=="ccover" then		--Cover Me!
				Sound:PlaySound(ClientStuff.Snd9);
			elseif TokTable[3]=="cenemy" then		--Enemy Spotted!
				Sound:PlaySound(ClientStuff.Snd10);
			elseif TokTable[3]=="cfireh" then		--Fire in the Hole!
				Sound:PlaySound(ClientStuff.Snd11);
			elseif TokTable[3]=="cinpos" then		--I'm in Position!
				Sound:PlaySound(ClientStuff.Snd13);  
			elseif TokTable[3]=="enemy" then		--Enemy Down!
				Sound:PlaySound(ClientStuff.Snd16);  
			elseif TokTable[3]=="fallbck" then		--Team Fall Back!
				Sound:PlaySound(ClientStuff.Snd17);  
			elseif TokTable[3]=="fireas" then		--Taking Fire, need Assistance!
				Sound:PlaySound(ClientStuff.Snd18);  
			elseif TokTable[3]=="follow" then		--Follow Me!
				Sound:PlaySound(ClientStuff.Snd19);  
			elseif TokTable[3]=="getout" then		--Team, Let's get out of here!
				Sound:PlaySound(ClientStuff.Snd20);  
			elseif TokTable[3]=="go" then			--Ok, Let's Go!
				Sound:PlaySound(ClientStuff.Snd21);  
			elseif TokTable[3]=="hiiass" then		--I'm hit, Need Assistance!
				Sound:PlaySound(ClientStuff.Snd22);    
			elseif TokTable[3]=="locknl" then		--Lock 'n Load!
				Sound:PlaySound(ClientStuff.Snd24);  
			elseif TokTable[3]=="mated" then		--Teammate Down!
				Sound:PlaySound(ClientStuff.Snd25);  
			elseif TokTable[3]=="meet" then			--Meet at the sw!
				Sound:PlaySound(ClientStuff.Snd26);  
			elseif TokTable[3]=="move" then			--All right, Let's Move Out!
				Sound:PlaySound(ClientStuff.Snd27);  
			elseif TokTable[3]=="negat" then		--Negative!
				Sound:PlaySound(ClientStuff.Snd28);  
			elseif TokTable[3]=="posit" then		--Hold This Position!
				Sound:PlaySound(ClientStuff.Snd29);  
			elseif TokTable[3]=="regrp" then		--Regroup Team!
				Sound:PlaySound(ClientStuff.Snd30);  
			elseif TokTable[3]=="roger" then		--Roger That!
				Sound:PlaySound(ClientStuff.Snd31);  
			elseif TokTable[3]=="stcktog" then		--Stick together Team!
				Sound:PlaySound(ClientStuff.Snd32);  
			elseif TokTable[3]=="stormf" then		--Storm the Front!
				Sound:PlaySound(ClientStuff.Snd33);  
			end
		end
	end
end;

---------------------------------------------------------------------------------

ClientStuff.ServerCommandTable["TMN"]=function(String,TokTable)
	if tonumber(TokTable[2])>=2 then
--		GotoPage("MyMenu");
		setglobal("gr_mntgle",1);
		Game:SendMessage("Switch");
  	end
end;

ClientStuff.ServerCommandTable["GNM"]=function(String,TokTable)
	if (count(TokTable)>=3) then
  		System:ExecuteCommand("say ***$"..tostring(TokTable[3]).."Nextmap:$"..tostring(TokTable[4]).." "..tostring(TokTable[2]).."$1 ***");
  	end
end;

ClientStuff.ServerCommandTable["TMB"]=function(String,TokTable)
	if tonumber(TokTable[2])>=2 then
		GotoPage("Messages");
  	end
end;
ClientStuff.ServerCommandTable["TBL"]=function(String,TokTable)
	if tonumber(TokTable[2])>=2 then
		GotoPage("BotMenu");
  	end
end;  
 
ClientStuff.ServerCommandTable["GST"]=function(String,TokTable)
	
	UI.PageMyMenu.GUI.TeambalanceBtn:SetChecked(tonumber(TokTable[2]));
	UI.PageMyMenu.GUI.TeamSwitchBtn:SetChecked(tonumber(TokTable[3]));
	UI.PageMyMenu.GUI.VotingBtn:SetChecked(tonumber(TokTable[4]));
	if tonumber(TokTable[5])== 0  then 
		UI.PageMyMenu.GUI.TKkickBtn:SetChecked(1);
	end
	if tonumber(TokTable[6])~=0 then
		UI.PageMyMenu.GUI.MaxPingkickBtn:SetChecked(1);
	end
	UI.PageMyMenu.GUI.BadwordkickBtn:SetChecked(tonumber(TokTable[7]));
	UI.PageMyMenu.GUI.LockBlueBtn:SetChecked(tonumber(TokTable[8]));
	UI.PageMyMenu.GUI.LockRedBtn:SetChecked(tonumber(TokTable[9]));
	if tonumber(TokTable[10])== 0  then
		UI.PageMyMenu.GUI.RLenableBtn:SetChecked(1);
	end
	UI.PageMyMenu.GUI.DiscardClipBtn:SetChecked(tonumber(TokTable[11]));
	UI.PageMyMenu.GUI.KeepLockBtn:SetChecked(tonumber(TokTable[12]));
	UI.PageMyMenu.GUI.KeepScoreBtn:SetChecked(tonumber(TokTable[13]));
	UI.PageMyMenu.GUI.FulltimeBtn:SetChecked(tonumber(TokTable[14]));
	UI.PageMyMenu.GUI.SpectatorBtn:SetChecked(tonumber(TokTable[15]));
end;

ClientStuff.ServerCommandTable["GSN"]=function(String,TokTable)
	local InGameMenu = ClientStuff:GetInGameMenuName();
	
	if (not InGameMenu) then
		return
	end
	local ServerName = UI:GetWidget("ServerName", InGameMenu);
	local szName=TokTable[2];
	local tokcount=count(TokTable);
	for i=3, tokcount do
		szName=szName.." "..TokTable[i];
	end
	ServerName:SetText(szName);
end;

ClientStuff.ServerCommandTable["MLA"]=function(String,TokTable)
	
	local tokcount=count(TokTable);
	if tokcount>=2 then
--		local InGameMenu = ClientStuff:GetInGameMenuName();
--		local MapList = UI:GetWidget("MapList", "MyMenu");

		for i=2, tokcount do
			UI.PageMyMenu.GUI.MapList:AddItem(TokTable[i]);
		end
	end
end;

ClientStuff.ServerCommandTable["GSD"]=function(String,TokTable)

	-- check if there is an ingamemenu
	local InGameMenu = ClientStuff:GetInGameMenuName();
	
	if (not InGameMenu) then
		return
	end
	
	local MaxTeamPlayers = UI:GetWidget("MaxTeamPlayers", InGameMenu);
	MaxTeamPlayers:SetText(tonumber(TokTable[2]));
	local MinTeamPlayers = UI:GetWidget("MinTeamPlayers", InGameMenu);
	MinTeamPlayers:SetText(tonumber(TokTable[3]));
	local MaxServerPlayers = UI:GetWidget("MaxServerPlayers", InGameMenu);
	MaxServerPlayers:SetText(tonumber(TokTable[4]));
	local RespawnTime = UI:GetWidget("RespawnTime", InGameMenu);
	RespawnTime:SetText(tonumber(TokTable[5]));
	local ServerPassword = UI:GetWidget("ServerPassword", InGameMenu);
	ServerPassword:SetText(TokTable[6]);
end;
---------------------------------------------------------------------------------
-- change player objective
ClientStuff.ServerCommandTable["CPO"]=function(String,toktable)

	local tokcount=count(toktable);

	local newobjective;
		
	if tokcount>=2 then
		newobjective=toktable[2];
	end
		
	Hud.PlayerObjective = newobjective;
	
	if newobjective then
		Hud.PlayerObjectiveAnim = 1;	-- 1 is the pop up
		Hud.PlayerObjectiveAnimStart = _time;
		
		if newobjective == "POAtt" then
			Hud.PlayerObjectiveAnimTex = System:LoadImage("textures/hud/multiplayer/attacker_logo");
		else
			Hud.PlayerObjectiveAnimTex = System:LoadImage("textures/hud/multiplayer/defender_logo");
		end
	else
		Hud.PlayerObjectiveAnim=nil;
	end
end;


---------------------------------------------------------------------------------
-- respawn timer reset
ClientStuff.ServerCommandTable["RTR"]=function(String,toktable)

	if count(toktable)==2 then
		-- currently only used in the Hud
		-- set to the hud, and limboscreen
		local iRCL = tonumber(toktable[2]);
		
		if (iRCL) then
			Hud.iRespawnCycleLength = iRCL;
			Hud.iRespawnCycleStart = _time;
		end
	else
		Hud.iRespawnCycleLength = nil;
		Hud.iRespawnCycleStart = nil;
	end
end;


---------------------------------------------------------------------------------
-- your class is now <MultiplayerClassDefiniton.PlayerClasses>
-- needed to synchroize the player speed
ClientStuff.ServerCommandTable["YCN"]=function(String,toktable)
	if count(toktable)==3 then
		local idPlayer=toktable[2];
		local classname=toktable[3];
		local myclass=MultiplayerClassDefiniton.PlayerClasses[classname];
		
		if not myclass then				-- e.g. DefaultMultiPlayer
			myclass=MultiplayerClassDefiniton[classname];
		end
		
		if myclass then
			local player=System:GetEntity(idPlayer);
			
			if player and player.cnt then
				player.move_params = myclass.move_params;
				player.cnt:SetMoveParams(myclass.move_params);
				player.cnt.max_health=myclass.health;						-- is not netsynced
				player.cnt.max_armor=myclass.max_armor;					-- is not netsynced
			else
				System:Error("ServerCommandTable YCN failed - internal network error");
			end
		end
	end
end;


---------------------------------------------------------------------------------
-- receive player count
ClientStuff.ServerCommandTable["RPC"] = function(String,TokTable)

	if not Hud then
		return;
	end

	Hud.iTeam01Count = tonumber(TokTable[2]);
	Hud.iTeam02Count = tonumber(TokTable[2+4]);
	Hud.iTeamSpecCount = tonumber(TokTable[2+4+4]);
	
	Hud.Team01ClassCount = {};
	Hud.Team02ClassCount = {};

	local iClassIndex = 1;
	for iClassIndex=1, 3 do
		Hud.Team01ClassCount[iClassIndex] = tonumber(TokTable[2 + iClassIndex]);
	end
	for iClassIndex=1, 3 do
		Hud.Team02ClassCount[iClassIndex] = tonumber(TokTable[2 + 4 + iClassIndex]);
	end

	Hud.bReceivedPlayerCount = 1;
end

---------------------------------------------------------------------------------
-- 
ClientStuff.ServerCommandTable["GPC"] = function(String,TokTable)
	
	if not Hud then
		return;
	end

	Hud.iSelfWeapon1 = nil;
	Hud.iSelfWeapon2 = nil;
	Hud.iSelfWeapon3 = nil;
	Hud.iSelfWeapon4 = nil;

	Hud.szSelfTeam = tostring(TokTable[2]);
	Hud.szSelfClass = tostring(TokTable[3]);
	
	for i=1, 4 do
		if (TokTable[2 + i]) then
			Hud["iSelfWeapon"..i] = tonumber(TokTable[3 + i]);
		end
	end
	
	Hud.bReceivedSelfStat = 1;
end


---------------------------------------------------------------------------------
--
ClientStuff.ServerCommandTable["UTL"] = function(String,TokTable)
	if (Hud) then
		Hud.fTimeLimit = tonumber(TokTable[2]);
	end
end

---------------------------------------------------------------------------------
--
function ClientStuff:GetInGameMenuVideoOn()
	return 0;
end


-------------------------------------------------------------------------------
-- callback for getting the in game menu
function ClientStuff:GetInGameMenuName()
	return "InGameNonTeam";
end

--------------------------------------------------------------------------------
-- called when a text message arrives
-- command is "say", "sayteam" or "sayone"
function ClientStuff:OnTextMessage(command, sendername, text)

	-- check if there is an ingamemenu
	local InGameMenu = ClientStuff:GetInGameMenuName();
	
	if (not InGameMenu) then
		return
	end
	
	-- check if the menu has a chatbox
	local ChatBox = UI:GetWidget("ChatBox", InGameMenu);
	
	if (not ChatBox) then
		return
	end
	
	-- at this point, nothing should go wrong...
	if (command == "sayone") then
		ChatBox:AddLine(sendername.." @MPChatPrivate "..text);
	elseif (command == "sayteam") then
		ChatBox:AddLine(sendername.." @MPChatTeam "..text);
	elseif (command == "say") then
		ChatBox:AddLine(sendername..":$1 "..text);
	end
end


---------------------------------------------------------------------------------
-- player killed player TargetEntityID,ShooterEntityID,situation=0/1/2,center=1/nil
ClientStuff.ServerCommandTable["PKP"]=function(String,toktable)
	local size=count(toktable);

-- 	System:Log(">>>> size "..size);

	if size==5 then
		local target=System:GetEntity(toktable[2]);
		local shooter=System:GetEntity(toktable[3]);
		local situation=toktable[4];
		local weapon=toktable[5];

--   	System:Log(">>>> 4 "..tostring(target));
--   	System:Log(">>>> 5 "..tostring(shooter));
--   	System:Log(">>>> 6 "..tostring(situation));

		if target and situation then
			local killmsg = {};
			
			killmsg.situation = situation;
			killmsg.weapon = weapon;
			
			if situation=="1" then
				killmsg.target = target:GetName();
			else
				if not shooter then
					return;
				end
				
				killmsg.shooter = shooter:GetName();
				killmsg.target = target:GetName();
			end

			-- add kill message
			Hud:AddMessage(killmsg, 7.5, 0, 1);
		end
	end
end;


function ClientStuff:OnUpdate()				
	self.vlayers:Update();

	self:UpdateScoreboard();
end
