// * AdmiralMOD by Matthias Lorenz, http://www.cod2mod.com *



Vote() {


	if(game["am_error_logfile"] == 1) logPrint("Vote\n");


	level notify("am_vote_start");

	level.vote_starting = true;


	players = getentarray("player", "classname");
	if(players.size < 1) return;


	
	maps\mp\gametypes\_admiral_mod_hud::deleteLevelHudElementAll();
		
		
	if(isDefined(level.clock))	level.clock destroy();		



	// * Alle Player spawnen *        
	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++) {

		// * Nur die Spieler Spawnen, die eine Teamzuordnung haben *
		if(isDefined(players[i].pers["team"]) && (players[i].pers["team"] == "allies" || players[i].pers["team"] == "axis") ) {
		
			players[i] maps\mp\gametypes\_admiral_mod_player::SpawnVotePlayer();		
			players[i] freezeControls(true);	
		}
		
		
		// * Für Spectators nur Vote-Tabelle anzeigen *
		if(isDefined(players[i].pers["team"]) && players[i].pers["team"] == "spectator") {
		
			players[i].pers["team"] 	= "axis";
			players[i].pers["weapon"] 	= "shotgun_mp";
			players[i] maps\mp\gametypes\_admiral_mod_player::SpawnVotePlayer();		
			players[i] freezeControls(true);			
		}
				
	}


	wait 0.2;


	players = getentarray("player","classname");

	for(i=0;i<players.size;i++) {

		players[i] openMenu("close_highscore"); 		
		players[i] closeMenu(); 

		players[i] maps\mp\gametypes\_admiral_mod_player::ClearHud();

		if(isDefined(players[i].hud_teamicon))		players[i].hud_teamicon destroy();		
		if(isDefined(players[i].hud_enemyicon))		players[i].hud_enemyicon destroy();		
		if(isDefined(players[i].hud_teamscore))		players[i].hud_teamscore destroy();		
		if(isDefined(players[i].hud_enemyscore))	players[i].hud_enemyscore destroy();		
	

 		// * Wenn nur Clanmember, dann Check *
		if(game["am_vote_only_clanmembers"] == 1) {
			
			if(!isDefined(players[i].pers["clanmember"])) {
			
				players[i] setClientCvar("am_ui_show_vote_only_for_clanmembers", "1");					
				continue;
			}
			
			// * Vote nur für Clanmembers *
			if(game["am_vote_map"] == 1) 		players[i] thread maps\mp\gametypes\_admiral_mod_player::VoteMap(game["am_vote_maps"]);
			if(game["am_vote_gametype"] == 1) 	players[i] thread maps\mp\gametypes\_admiral_mod_player::VoteGametype(game["am_vote_gametypes"]);

		}
		else {

			// * Vote für alle *
			if(game["am_vote_map"] == 1) 		players[i] thread maps\mp\gametypes\_admiral_mod_player::VoteMap(game["am_vote_maps"]);
			if(game["am_vote_gametype"] == 1) 	players[i] thread maps\mp\gametypes\_admiral_mod_player::VoteGametype(game["am_vote_gametypes"]);
		}
	}



	
	level maps\mp\gametypes\_admiral_mod_hud::createLevelHudElement("cinema_vote_top", 0,0,"left","top","fullscreen","fullscreen",false,"black",640,80,-50,1,0,0,0);
	level maps\mp\gametypes\_admiral_mod_hud::createLevelHudElement("cinema_vote_bottom", 0,400,"left","top","fullscreen","fullscreen",false,"black",640,80,-50,1,0,0,0);
	level maps\mp\gametypes\_admiral_mod_hud::createLevelHudElement("cinema_vote_top_line", 0,80,"left","top","fullscreen","fullscreen",false,"white",640,2,-50,1,0.7,0.7,0.7);
	level maps\mp\gametypes\_admiral_mod_hud::createLevelHudElement("cinema_vote_bottom_line", 0,398,"left","top","fullscreen","fullscreen",false,"white",640,2,-50,1,0.7,0.7,0.7);

	maps\mp\gametypes\_admiral_mod_hud::showVoteBoard();




	level thread ShowMostVotedMaps();
	level thread ShowMostVotedGametypes();


	vote_zeit = game["am_vote_time"];

	maps\mp\gametypes\_admiral_mod_hud::createLevelTimerHudElement("vote_timer", 320,450,"center","middle","fullscreen","fullscreen",false,"default",2,4,&"AM_MP_TIME_TO_VOTE",vote_zeit,1,0.7,0,0);
	wait vote_zeit;  // Vote Zeit

	maps\mp\gametypes\_admiral_mod_hud::deleteLevelHudElementByName("vote_timer");





	// *** Auswertung ***

	level notify("am_vote_end");

	wait 1;
	
	// * Most Votes Anzeige beenden *
	level notify("am_vote_end_most_votes");
	
	
	
	// * Wenn kein Spieler mehr da, dann weiter in der normalen Map-Rotation *
	players = getentarray("player", "classname");
	if(players.size < 1) return;




	// **  Map  ** 
	gewaehlte_map = -2;
	
	if(game["am_vote_map"] == 1) {

		map_vote_punkte = [];

		// * Punkte-Liste auf 0 setzen (für die Maps) *
		for(i=0;i<game["am_vote_maps"].size;i++) map_vote_punkte[i] = 0;


		// * Ergebnis ermitteln *
		players = getentarray("player", "classname");

		for(i = 0; i < players.size; i++) {
	
			if(isDefined(players[i].vote_position_map)) {

				pos = players[i].vote_position_map;            
				if(!isDefined(map_vote_punkte[pos])) map_vote_punkte[pos] = 0; 
				map_vote_punkte[pos]++;           
			}
		}

		map_meisten_punkte  = 0;
		gewaehlte_maps = [];

		for(i = 0; i < game["am_vote_maps"].size; i++) {
			
			if(isDefined(map_vote_punkte[i]) && map_vote_punkte[i] > map_meisten_punkte) {
				map_meisten_punkte = map_vote_punkte[i];
			}
		}		
		gewaehlte_maps_count = 0;
		for(i = 0; i < game["am_vote_maps"].size; i++) {
			
			if(isDefined(map_vote_punkte[i]) && map_vote_punkte[i] == map_meisten_punkte) {
				gewaehlte_maps[gewaehlte_maps_count] = i;		
				gewaehlte_maps_count++;
			}
		}
		gewaehlte_map = gewaehlte_maps[RandomInt(gewaehlte_maps.size)];
	}



	// **  Gameytpe  ** 
	gewaehlte_gametype = -2;

	if(game["am_vote_gametype"] == 1) {

		gametype_vote_punkte = [];

		// * Punkte-Liste auf 0 setzen (für die gametypes) *
		for(i=0;i<game["am_vote_gametypes"].size;i++) gametype_vote_punkte[i] = 0;


		// * Ergebnis ermitteln *
		players = getentarray("player", "classname");

		for(i = 0; i < players.size; i++) {
			
			if(isDefined(players[i].vote_position_gametype)) {

				pos = players[i].vote_position_gametype;    
				if(!isDefined(gametype_vote_punkte[pos])) gametype_vote_punkte[pos] = 0; 
				gametype_vote_punkte[pos]++;           
			}
		}

		gametype_meisten_punkte  = 0;
		gewaehlte_gametypes = [];

		for(i = 0; i < game["am_vote_gametypes"].size; i++) {

			if(isDefined(gametype_vote_punkte[i]) && gametype_vote_punkte[i] > gametype_meisten_punkte) {
				gametype_meisten_punkte = gametype_vote_punkte[i];
			}
		}
		gewaehlte_gametypes_count = 0;
		for(i = 0; i < game["am_vote_maps"].size; i++) {
			
			if(isDefined(gametype_vote_punkte[i]) && gametype_vote_punkte[i] == gametype_meisten_punkte) {
				gewaehlte_gametypes[gewaehlte_gametypes_count] = i;		
				gewaehlte_gametypes_count++;
			}
		}
		gewaehlte_gametype = gewaehlte_gametypes[RandomInt(gewaehlte_gametypes.size)];
	}



	// *** Ergebnis anzeigen ***


	//* gewählte Map und Gametype hervorheben (neue Position + Farben der Balken) *
	
	players = getentarray("player", "classname");

	for(i = 0; i < players.size; i++) {

		if(game["am_vote_map"] == 1) {
		
			data = players[i] maps\mp\gametypes\_admiral_mod_hud::getPlayerHudElementDataByName("vote_point_map");
			if(isDefined(data)) players[i] maps\mp\gametypes\_admiral_mod_hud::changePlayerHudElementByName("vote_point_map",data["shader"],data["width"],data["height"],0.7,data["color"]);
		}
		
		if(game["am_vote_gametype"] == 1) {
		
			data = players[i] maps\mp\gametypes\_admiral_mod_hud::getPlayerHudElementDataByName("vote_point_gametype");
			if(isDefined(data)) players[i] maps\mp\gametypes\_admiral_mod_hud::changePlayerHudElementByName("vote_point_gametype",data["shader"],data["width"],data["height"],0.7,data["color"]);
		}

		players[i] maps\mp\gametypes\_admiral_mod_hud::changePlayerHudElementPositionByName("vote_point_map",170,120 + gewaehlte_map * 15);
		players[i] maps\mp\gametypes\_admiral_mod_hud::changePlayerHudElementPositionByName("vote_point_gametype",470,120 + gewaehlte_gametype * 15);
	}
	
	wait 5;


	// * Hud befreien *
	players = getentarray("player", "classname");

	for(i = 0; i < players.size; i++) {

		players[i] maps\mp\gametypes\_admiral_mod_player::ClearHud();

		players[i] setClientCvar("am_ui_show_vote_headline", "0");
		players[i] setClientCvar("am_ui_show_vote_map_button", "0");
		players[i] setClientCvar("am_ui_show_vote_gametype_button", "0");
		players[i] setClientCvar("am_ui_show_vote_board_maps", "0");
		players[i] setClientCvar("am_ui_show_vote_board_gametypes", "0");
		players[i] setClientCvar("am_ui_show_vote_only_for_clanmembers", "0");	
	}




	maps\mp\gametypes\_admiral_mod_hud::deleteLevelHudElementAll();
	
	level maps\mp\gametypes\_admiral_mod_hud::createLevelHudElement("cinema_vote_top", 0,0,"left","top","fullscreen","fullscreen",false,"black",640,80,-50,1,0,0,0);
	level maps\mp\gametypes\_admiral_mod_hud::createLevelHudElement("cinema_vote_bottom", 0,400,"left","top","fullscreen","fullscreen",false,"black",640,80,-50,1,0,0,0);
	level maps\mp\gametypes\_admiral_mod_hud::createLevelHudElement("cinema_vote_top_line", 0,80,"left","top","fullscreen","fullscreen",false,"white",640,2,-50,1,0.7,0.7,0.7);
	level maps\mp\gametypes\_admiral_mod_hud::createLevelHudElement("cinema_vote_bottom_line", 0,398,"left","top","fullscreen","fullscreen",false,"white",640,2,-50,1,0.7,0.7,0.7);



	// * Nächste Map und nächster Gametype aus der normalen Maprotation holen *
	map 		= maps\mp\gametypes\_admiral_mod_hud::getMapNameAsLocalizedString(maps\mp\gametypes\_admiral_mod_utils::getNextMap());
	gametype 	= maps\mp\gametypes\_admiral_mod_hud::getGametypeNameAsLocalizedString(maps\mp\gametypes\_admiral_mod_utils::getNextGametype());


	// * Wenn eine Map gewählt wurde, oder ein Gametype, dann diesen nehmen anstatt die/den aus der Maprotation *
	if(game["am_vote_map"] == 1 && isDefined(game["am_vote_maps"][gewaehlte_map])) {
	
		if(game["am_allow_vote_custom_maps"] == 1) {
		
			// * Wenn Stock Map, dann mp_ *
			if(maps\mp\gametypes\_admiral_mod_utils::isMapNameExists("mp_" + game["am_vote_maps"][gewaehlte_map])) 	map 		= maps\mp\gametypes\_admiral_mod_hud::getMapNameAsLocalizedString("mp_" + game["am_vote_maps"][gewaehlte_map]);
			else 																									map 		= game["am_vote_maps"][gewaehlte_map];
		}	
		else {
		
			if(maps\mp\gametypes\_admiral_mod_utils::isMapNameExists("mp_" + game["am_vote_maps"][gewaehlte_map])) 	map 		= maps\mp\gametypes\_admiral_mod_hud::getMapNameAsLocalizedString("mp_" + game["am_vote_maps"][gewaehlte_map]);
		}
	}
	
	
	if(game["am_vote_gametype"] == 1 && isDefined(game["am_vote_gametypes"][gewaehlte_gametype])  && maps\mp\gametypes\_admiral_mod_utils::isGametypeNameExists(game["am_vote_gametypes"][gewaehlte_gametype])) 		gametype 	= maps\mp\gametypes\_admiral_mod_hud::getGametypeNameAsLocalizedString(game["am_vote_gametypes"][gewaehlte_gametype]);



	iprintln(&"AM_MP_NEXT_MAP_AND_GAMETYPE",map,gametype);

    
      
    
	if(game["am_allow_vote_custom_maps"] == 1)    {
	
		if(game["am_vote_map"] == 1 && isDefined(game["am_vote_maps"][gewaehlte_map]) && maps\mp\gametypes\_admiral_mod_utils::isMapNameExists("mp_" + game["am_vote_maps"][gewaehlte_map])) 	v_map = "mp_" + game["am_vote_maps"][gewaehlte_map];
		else 																																													v_map = game["am_vote_maps"][gewaehlte_map];
	}
	else {
	
		if(game["am_vote_map"] == 1 && isDefined(game["am_vote_maps"][gewaehlte_map]) && maps\mp\gametypes\_admiral_mod_utils::isMapNameExists("mp_" + game["am_vote_maps"][gewaehlte_map])) {

			v_map = "mp_" + game["am_vote_maps"][gewaehlte_map];
		}
		else {

			v_map = maps\mp\gametypes\_admiral_mod_utils::getNextMap();
		}
	}
	
	

    if(game["am_vote_gametype"] == 1 && isDefined(game["am_vote_gametypes"][gewaehlte_gametype]) &&  maps\mp\gametypes\_admiral_mod_utils::isGametypeNameExists(game["am_vote_gametypes"][gewaehlte_gametype])) {
    
    	v_gametype = game["am_vote_gametypes"][gewaehlte_gametype];
    }
    else {
    
    	v_gametype = maps\mp\gametypes\_admiral_mod_utils::getNextGametype();
    }
     
    
    next_map =  "gametype " + v_gametype + " map " + v_map;
   
    setcvar("sv_maprotationcurrent",next_map);


	wait 7;    

}



ShowMostVotedMaps() {

	level endon("am_vote_end_most_votes");


	// ***  Map  *** 

	if(game["am_vote_map"] != 1) return;
	
	
	if(game["am_error_logfile"] == 1) logPrint("ShowMostVotedMaps\n");
	
	
	while(1) {

		map_vote_punkte = [];
		m = 0;

		// * Punkte-Liste auf 0 setzen (für die Maps) *
		for(i=0;i<game["am_vote_maps"].size;i++) map_vote_punkte[i] = 0;


		// * Ergebnis ermitteln *
		players = getentarray("player", "classname");

		for(i = 0; i < players.size; i++) {
	
			if(isDefined(players[i].vote_position_map) && players[i].vote_position_map > -1) {

				pos = players[i].vote_position_map;            
				map_vote_punkte[pos]++;           
			}
		}


		meistgewaehlte_maps 		= [];
		meistgewaehlte_maps_votes 	= [];

		while(m<3) {

			map_meisten_punkte  = 0;

			for(i = 0; i < game["am_vote_maps"].size; i++) {

				if(map_vote_punkte[i] > map_meisten_punkte && !InMeistgewaehlteMapListe(i,meistgewaehlte_maps)) {

					map_meisten_punkte 				= map_vote_punkte[i];					
					meistgewaehlte_maps[m] 		 	= i;				
					meistgewaehlte_maps_votes[m] 	= map_meisten_punkte;
				}
			}
		
			m++;
		}					
		
		wait 0.5;
		
		
		for(i=0;i<3;i++) {
		
			level maps\mp\gametypes\_admiral_mod_hud::deleteLevelHudElementByName("most_votes_maps_" + i); 
		
			if(isDefined(meistgewaehlte_maps_votes[i]) && meistgewaehlte_maps_votes[i] > 0)	level maps\mp\gametypes\_admiral_mod_hud::createLevelTextHudElement("most_votes_maps_" + i, 100,126 + 15 * meistgewaehlte_maps[i],"center","middle","fullscreen","fullscreen",false,undefined,meistgewaehlte_maps_votes[i],1.3,50,1,1,1,1); 
		}
	}
}




InMeistgewaehlteMapListe(map_nr,meistgewaehlte_maps) {

	if(game["am_error_logfile"] == 1) logPrint("InMeistgewaehlteMapListe\n");


	for(i=0;i<meistgewaehlte_maps.size;i++) {
	
		if(map_nr == meistgewaehlte_maps[i]) return true;	
	}
	
	return false;
}


ShowMostVotedGametypes() {

	if(game["am_error_logfile"] == 1) logPrint("ShowMostVotedGametypes\n");


	level endon("am_vote_end_most_votes");


	// ***  Map  *** 

	if(game["am_vote_gametype"] != 1) return;
	
	
	while(1) {

		gametype_vote_punkte = [];
		m = 0;

		// * Punkte-Liste auf 0 setzen (für die Gametypes) *
		for(i=0;i<game["am_vote_gametypes"].size;i++) gametype_vote_punkte[i] = 0;


		// * Ergebnis ermitteln *
		players = getentarray("player", "classname");

		for(i = 0; i < players.size; i++) {
	
			if(isDefined(players[i].vote_position_gametype) && players[i].vote_position_gametype > -1) {

				pos = players[i].vote_position_gametype;            
				gametype_vote_punkte[pos]++;           
			}
		}


		meistgewaehlte_gametypes 		= [];
		meistgewaehlte_gametypes_votes 	= [];

		while(m<3) {

			gametype_meisten_punkte  = 0;

			for(i = 0; i < game["am_vote_gametypes"].size; i++) {

				if(gametype_vote_punkte[i] > gametype_meisten_punkte && !InMeistgewaehlteGametypeListe(i,meistgewaehlte_gametypes)) {

					gametype_meisten_punkte 			= gametype_vote_punkte[i];					
					meistgewaehlte_gametypes[m] 		= i;				
					meistgewaehlte_gametypes_votes[m] 	= gametype_meisten_punkte;
				}
			}
		
			m++;
		}					
		
		wait 0.5;
		
		
		for(i=0;i<3;i++) {
		
			level maps\mp\gametypes\_admiral_mod_hud::deleteLevelHudElementByName("most_votes_gametypes_" + i); 
		
			if(isDefined(meistgewaehlte_gametypes_votes[i]) && meistgewaehlte_gametypes_votes[i] > 0)	level maps\mp\gametypes\_admiral_mod_hud::createLevelTextHudElement("most_votes_gametypes_" + i, 395,126 + 15 * meistgewaehlte_gametypes[i],"center","middle","fullscreen","fullscreen",false,undefined,meistgewaehlte_gametypes_votes[i],1.3,50,1,1,1,1); 
		}
	}
}


InMeistgewaehlteGametypeListe(gametype_nr,meistgewaehlte_gametypes) {

	if(game["am_error_logfile"] == 1) logPrint("InMeistgewaehlteGametypeListe\n");


	for(i=0;i<meistgewaehlte_gametypes.size;i++) {
	
		if(gametype_nr == meistgewaehlte_gametypes[i]) return true;	
	}
	
	return false;
}
