-------------------------------------------------------------------------
--	Helper functions for GameRules with Menucommands support implementation
--
--	created by [HOC]KoRi
-------------------------------------------------------------------------

Script:LoadScript("SCRIPTS/MULTIPLAYER/GameRulesTeamLib.lua");				-- we derive from this
Script:LoadScript("SCRIPTS/MULTIPLAYER/MultiplayerClassDefiniton.lua");		-- global MultiplayerClassDefiniton
Script:LoadScript("SCRIPTS/MULTIPLAYER/GameRulesClassLib.lua");				-- we derive from this


GameRules.ClientCommandTable["GSS"]=function(String,server_slot,toktable)	--get screenshot
	if count(toktable) >= 2 then
		System:ExecuteCommand("PB_SV_SSSRATE 4");
		System:ExecuteCommand("PB_SV_XPCT 50");
		System:ExecuteCommand("PB_SV_XPCT 50");
		System:ExecuteCommand("PB_SV_GETSS "..toktable[2]);
	end
end

GameRules.ClientCommandTable["GSR"]=function(String,server_slot,toktable)	--get screenshot from radar
	if count(toktable) >= 2 then
		System:ExecuteCommand("PB_SV_SSSRATE 2");
		System:ExecuteCommand("PB_SV_XPCT 0");
		System:ExecuteCommand("PB_SV_XPCT 100");
		System:ExecuteCommand("PB_SV_GETSS "..toktable[2]);
	end
end

GameRules.ClientCommandTable["PLY"]=function(String,server_slot,toktable)
	if count(toktable) >= 2 then
		local PlayerEntity=GetSlotPlayer(server_slot);
		local team = Game:GetEntityTeam(PlayerEntity.id);
		if team~="spectators" then
			Server:BroadcastCommand("PLA "..team.." "..toktable[2].." "..tostring(PlayerEntity.id));
		end
	end
end

GameRules.ClientCommandTable["BOT"]=function(String,server_slot,toktable)
	if count(toktable) >= 2 then
		local team;
		local team_color;
		local iSpecies=0;
		local _model = server_slot:GetModel();
		if (tostring(toktable[2])=="addb" ) then 
			team="blue";
			team_color={0,0,1};
			iSpecies=0;
		elseif (tostring(toktable[2])=="addr" ) then
			team="red";
			team_color={1,0,0};
			iSpecies=1;
		end
		local rp=GameRules:GetTeamRespawnPoint(team,System:GetEntity(server_slot:GetPlayerId()));
		if (rp==nil) then
			System:Log("GameRules:SpawnPlayer haven't found a TeamRespawnPoint")
			rp=Server:GetRandomRespawnPoint();
			if (rp==nil) then
				rp={x=0, y=0, z=0, xA=0, yA=0, zA=0};
			end
		end
		local pos = {x = rp.x, y = rp.y, z = rp.z};
		local ang = {x = rp.xA, y = rp.yA, z = rp.zA};
	---------
		local pclass;
		local weapon1=1;
		local weapon2;
		local weapon3;
		local weapon4;
		local rnd=random(1,6); --pclass: 1:2-Grunt, 1:3-Support, 1:6-Sniper
		local clas;
		if rnd<=3 then pclass=MultiplayerClassDefiniton.PlayerClasses["Grunt"];
			weapon2=1;
			weapon3=random(1,2);
			weapon4=random(1,3);
			clas="Grunt";
		elseif rnd==6 then pclass=MultiplayerClassDefiniton.PlayerClasses["Sniper"];
			weapon2=1;
			if tonumber(getglobal("gr_Disable_SniperRL"))==1 then
				weapon3=1;
			else
				weapon3=random(1,2);
			end
			clas="Sniper";
		elseif (rnd==5 or rnd==4) then pclass=MultiplayerClassDefiniton.PlayerClasses["Support"];
			weapon2=random(1,2);
			weapon3=1;
			weapon4=1;
			clas="Support";
		end
	---------		
		local sEntName="$0[$4Bot$0]";
		if (NameGenerator) then
			sEntName= "$0[$4Bot$0]$6"..NameGenerator:GetHumanName();
		end
		local num=toNumberOrZero(getglobal("Botnumbers"))+1;
		setglobal("Botnumbers", num);
		local mypack={};
		local no=4;	
			mypack[1]={ Type="Weapon", Name=pclass.weapon1[weapon1], };
			mypack[2]={ Type="Weapon", Name=pclass.weapon2[weapon2], };
			mypack[3]={ Type="Weapon", Name=pclass.weapon3[weapon3], };
			if weapon4 and pclass.weapon4[weapon4] then
				mypack[4]={ Type="Weapon", Name=pclass.weapon4[weapon4], };
				no=no+1;
			end
			-- assign a primary weapon if we have specified one
			if (pclass.primaryWeaponSlot ~= nil) then				
				if clas=="Support" and weapon2==2 then
					mypack[2].Primary = 1;
				else	mypack[pclass.primaryWeaponSlot].Primary = 1;
				end				
			end
			mypack.Ammo=pclass.InitialAmmo;
			-- add items to equipment pack
			if (pclass.items ~= nil) then
				for i, item in pclass.items do
					mypack[no] = { Type="Item", Name = item, };
					no = no + 1;
				end
			end
			local packname  = "NetPlayerPack" .. count(EquipPacks);
			EquipPacks[ packname ] = mypack;
		local cproperties = --{bHasArmor = 0,bHelmetOnStart = 0,eye_height = 2.0,	species = iSpecies,bots=num,max_health = 255,groupid = iSpecies,bTrackable=1,}
			{
			KEYFRAME_TABLE = "BASE_HUMAN_MODEL",
			SOUND_TABLE = "MERC_SCOUT",
			bots = num,
			suppressedThrhld = 5.5,
			bSleepOnSpawn = 0,
			dropArmor = 0,
			aggression = 0.3,	-- 0 = passive, 1 = total aggression
			bAffectSOM = 1,
			bSmartMelee = 0,
		-- Original from player
			bHasArmor = 0,
			bHelmetOnStart = 0,
			eye_height = 2.0,
			species = iSpecies,
			max_health = 255,
			groupid = iSpecies,
			bTrackable=1,
			sCurrentPlayerClass=clas,
		-- Original from player end
			commrange = 30.0,
			attackrange = 70,
			horizontal_fov = 160,
			forward_speed = 1.27,
			back_speed = 1.27,
			accuracy = 0.6,
			responsiveness = 7,
			special = 0,
	
			fSpeciesHostility = 2,
			fGroupHostility = 0,
			fPersistence = 0.8,
			equipEquipment = packname,
			equipDropPack = "none",
			AnimPack = "Basic",
			SoundPack = "dialog_template",
	
			aicharacter_character = "Scout",
			pathname = "none",
			pathsteps = 0,
			pathstart = 0,
			ReinforcePoint = "none",
			fileModel = pclass.model,
			}	
		local newt={}
		mergef(newt,cproperties,1); 
			
		local newPlayer=Server:SpawnEntity({classid=85,name=sEntName,pos=pos,angles=ang,color=team_color,properties=newt,}); --85= Bot
		newPlayer.cnt.health		= pclass.health;
		newPlayer.cnt.max_health	= pclass.health;
		newPlayer.cnt.armor		= pclass.armor;			
		newPlayer.cnt:InitStaminaTable( pclass.StaminaTable );
		newPlayer.cnt:SetMoveParams( pclass.move_params );
	 	
--		newPlayer.Properties.equipEquipment = packname;
		newPlayer.currentEquipment = new(EquipPacks[newPlayer.Properties.equipEquipment]);
		Server:BroadcastCommand("YCN "..newPlayer.id.." "..clas);
		BasicPlayer.InitAllWeapons(newPlayer, 1);
		Server:AddToTeam(team,newPlayer.id);
		GameRules:AddPlayerToScoreboard(num+30);
		GLplayerTrack:AddPlayer(num+30);
--		local valami = Server:CreateBotSlot(newPlayer.id); -- C++ 
--			if (valami~=nil) then 
--			Server:BroadcastText("debug: ok");
--		else 	Server:BroadcastText("debug: failed");
--		end
--		GameRules:RespawnPlayer(valami,newPlayer);
--		self:OnAfterSpawnEntity(server_slot);
	end
end

GameRules.ClientCommandTable["TMN"]=function(String,ServerSlot,TokTable)
	if (count(TokTable) ~= 1) then
		return;
	end
	local szReplyString = TokTable[1];
	szReplyString = szReplyString.." "..tostring(5);
	ServerSlot:SendCommand(szReplyString);
end

GameRules.ClientCommandTable["TMB"]=function(String,ServerSlot,TokTable)
	if (count(TokTable) ~= 1) then
		return;
	end
	local szReplyString = TokTable[1];
	szReplyString = szReplyString.." "..tostring(5);
	ServerSlot:SendCommand(szReplyString);
end
GameRules.ClientCommandTable["TBL"]=function(String,ServerSlot,TokTable)
	if (count(TokTable) ~= 1) then
		return;
	end
	local szReplyString = TokTable[1];
	szReplyString = szReplyString.." "..tostring(5);
	ServerSlot:SendCommand(szReplyString);
end
-------------------------------------------------------------------------------
--
GameRules.ClientCommandTable["GNM"]=function(String,ServerSlot,TokTable)
	if (count(TokTable) ~=3) then
		return;
	end
	local szReplyString = TokTable[1];
	szReplyString = szReplyString.." "..tostring(gr_NextMap);
	szReplyString = szReplyString.." "..TokTable[2];
	szReplyString = szReplyString.." "..TokTable[3];
	ServerSlot:SendCommand(szReplyString);
end

-------------------------------------------------------------------------------
-- available assault maps list
GameRules.ClientCommandTable["MLA"]=function(String,ServerSlot,TokTable)
	if (count(TokTable) ~= 1) then
		return;
	end

	local szReplyString = TokTable[1];	
	local MapList = Game:GetLevelList();
	local leveldb =0;
	for LevelIndex, Level in MapList do
		for MissionIndex, MissionName in Level.MissionList do	
			if (strlower(MissionName) == "assault") then
				szReplyString = szReplyString.." "..Level.Name;
				leveldb=leveldb+1;
				if leveldb>=10 then 
					ServerSlot:SendCommand(szReplyString);
					leveldb=0;
					szReplyString = TokTable[1];
				end;
			end
		end
	end
	ServerSlot:SendCommand(szReplyString);
end
-------------------------------------------------------------------------------
-- server config query
GameRules.ClientCommandTable["GST"]=function(String,ServerSlot,TokTable)
	if (count(TokTable) ~= 1) then
		return;
	end

	local szReplyString = TokTable[1];
	szReplyString = szReplyString.." "..tostring(getglobal("gr_team_balance"));
	szReplyString = szReplyString.." "..tostring(getglobal("gr_allow_teamswitch"));
	szReplyString = szReplyString.." "..tostring(getglobal("gr_allow_voting"));
	szReplyString = szReplyString.." "..tostring(getglobal("gl_allow_teamkilling"));
	szReplyString = szReplyString.." "..tostring(getglobal("gl_max_average_ping"));
	szReplyString = szReplyString.." "..tostring(getglobal("gr_bwk_enable"));
	szReplyString = szReplyString.." "..tostring(getglobal("gl_forceredjoin"));
	szReplyString = szReplyString.." "..tostring(getglobal("gl_forcebluejoin"));
	szReplyString = szReplyString.." "..tostring(getglobal("gr_Disable_SniperRL"));
	szReplyString = szReplyString.." "..tostring(getglobal("gr_discardclip"));
	szReplyString = szReplyString.." "..tostring(getglobal("gl_keep_lock"));
	szReplyString = szReplyString.." "..tostring(getglobal("gl_keep_score"));
	szReplyString = szReplyString.." "..tostring(getglobal("gl_fulltime"));
	szReplyString = szReplyString.." "..tostring(getglobal("gl_allow_spectators"));
	ServerSlot:SendCommand(szReplyString);
end

GameRules.ClientCommandTable["GSN"]=function(String,ServerSlot,TokTable)
	if (count(TokTable) ~= 1) then
		return;
	end

	local szReplyString = TokTable[1];
	szReplyString = szReplyString.." "..tostring(getglobal("sv_name"));
	ServerSlot:SendCommand(szReplyString); 
end

GameRules.ClientCommandTable["GSD"]=function(String,ServerSlot,TokTable)
	if (count(TokTable) ~= 1) then
		return;
	end

	local szReplyString = TokTable[1];
	szReplyString = szReplyString.." "..tostring(getglobal("gr_MaxTeamLimit"));
	szReplyString = szReplyString.." "..tostring(getglobal("gr_MinTeamLimit"));
	szReplyString = szReplyString.." "..tostring(getglobal("sv_maxplayers"));
	szReplyString = szReplyString.." "..tostring(getglobal("gr_BackupRPTime"));
	local szValue = tostring(getglobal("sv_password"));
	szReplyString = szReplyString.." "..szValue;
	ServerSlot:SendCommand(szReplyString); 
end