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



StartBattleChatter() {
	

	self notify("StartBattleChatter");
	self endon("StartBattleChatter");

	self endon("spawned");
	self endon("killed_player");
	self endon("disconnect");


	if(game["am_battlechatter"] != 1) return;

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

	
	self.bc_isTalking 		= undefined;
	self.bc_lastTalkingTime = undefined;
	
	
	// self.bc_lastMove 		= undefined;
	// self.bc_moveDirection	= undefined;
	// self.bc_nearTeammates	= undefined;
	// self.bc_behindTeammate	= undefined;
	// self.bc_infrontTeammate	= undefined;
	
	// self.bc_lastAttacker		= undefined;
	// self.bc_lastAttackerTime = getTime();
	
	
	range = 1000;
	
	while(1) {
	
		wait 3;
	

		// * Wenn Spieler im Haus, dann checken ob ein Enemy auch im Haus *
		if(self maps\mp\gametypes\_admiral_mod_utils::CheckImRaum() && self maps\mp\gametypes\_admiral_mod_utils::CheckMatesNaehe(self.origin,range) > 0 && self maps\mp\gametypes\_admiral_mod_utils::CheckEnemyNaehe(self.origin,range) > 0) {
		
			self CheckEnemysImHaus(range);
		}
	
	}
}






CheckEnemysImHaus(range) {

	self endon("killed_player");
	self endon("disconnect");



	if(isDefined(self.bc_isTalking)) return;
	
	// * Wenn  *
	if(self CheckTalkEnemysImHaus(range)) return;
	
	
	if(game["am_error_logfile"] == 1) logPrint("CheckEnemysImHaus\n");

	

	stockwerk[0] = "basement";
	stockwerk[1] = "firstfloor";
	stockwerk[2] = "secondfloor";
	stockwerk[3] = "thirdfloor";


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

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

		player = players[i];

		if(player.sessionstate == "playing" && isDefined(player.pers["team"]) && player.pers["team"] != self.pers["team"] && distance(self.origin,player.origin) < range) {

			imRaum = player maps\mp\gametypes\_admiral_mod_utils::CheckImRaum();
			if(imRaum) {
							
				trace = bulletTrace(self.origin + (0,0,50), player.origin + (0,0,50), false, undefined);	
				
				// * Wenn kein Gegenstand zwischen den Spielern (Sichtkontakt), dann melden... *
				if(trace["fraction"] == 1) {
				
    				target_range = vectordot(anglestoforward(self.angles),anglestoforward(vectortoangles(vectornormalize((self.origin) - player.origin)))) * -1;
                   
 					if(target_range > 0.5) {

						team_kurz 	= self maps\mp\gametypes\_admiral_mod_player::GetTeamKurz();	
						enemy_kurz 	= player maps\mp\gametypes\_admiral_mod_player::GetTeamKurz();	
						
						voice = RandomInt(4);
						stock = maps\mp\gametypes\_admiral_mod_utils::GetStockwerk(player.origin);

						self.bc_isTalking 		= "EnemysImHaus";	
						self.bc_lastTalkingTime = getTime();
						
						
						if(team_kurz == "GE") {
												
							self thread DoTalking("am_" + team_kurz + "_" + voice + "_threat_infantry_generic_" + enemy_kurz);	
							wait 1.5;
							self thread DoTalking("am_" + team_kurz + "_" + voice + "_location_" + stockwerk[stock] + "_generic");	
							wait 1.5;
						}
						else {
						
							self thread DoTalking("am_" + team_kurz + "_" + voice + "_threat_infantry_generic");	
							wait 1.5;
							self thread DoTalking("am_" + team_kurz + "_" + voice + "_location_" + stockwerk[stock] + "_generic");	
							wait 1.5;							
						}
						
						
						wait 10; // * ein paar Sekunden warten, damit sich der Ruf nicht ständig wiederholt *
						self.bc_isTalking = undefined;
						
						break;			
					}
				}				
			}			
		}
	}	
}


// * Spricht schon jemand anderes ? *
CheckTeamMateIsTalking(range) {

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


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

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

		player = players[i];
		
		if(player.sessionstate == "playing" && isDefined(player.pers["team"]) && player.pers["team"] == self.pers["team"] && distance(self.origin,player.origin) < range && isDefined(player.bc_isTalking) && isDefined(player.bc_isTalking) ) {
	
			return true;
		}
	}
	
	return false;
}


// * Spricht schon jemand anderes "Enemy im Haus" ? *
CheckTalkEnemysImHaus(range) {

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


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

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

		player = players[i];
		
		if(player.sessionstate == "playing" && isDefined(player.pers["team"]) && player.pers["team"] == self.pers["team"] && distance(self.origin,player.origin) < range && isDefined(player.bc_isTalking) && player.bc_isTalking == "EnemysImHaus") {
	
			return true;
		}
	}
	
	return false;
}


// * Spricht schon jemand anderes "EnemyGekilled" ? *
CheckTalkEnemyGekilled(range) {

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


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

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

		player = players[i];
		
		if(player.sessionstate == "playing" && isDefined(player.pers["team"]) && player.pers["team"] == self.pers["team"] && distance(self.origin,player.origin) < range && isDefined(player.bc_isTalking) && player.bc_isTalking == "EnemyGekilled") {
	
			return true;
		}
	}
	
	return false;
}




TalkDamagedEnemy(damaged_team,eInflictor, eAttacker, iDamage, iDFlags, sMeansOfDeath, sWeapon, vPoint, vDir, sHitLoc, psOffsetTime) {

	self endon("killed_player");
	self endon("disconnect");

	if(game["am_battlechatter"] != 1) return;

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


	if(maps\mp\gametypes\_admiral_mod_weapons::isSniper(sWeapon)) {
	
		wait 1;
	
		self thread DoTalking(damaged_team + "_mp_stm_sniper");								
		self sayAll(&"QUICKMESSAGE_SNIPER");
	}
		
}

TalkDamagedFriend(killed_team) {

	self endon("killed_player");
	self endon("disconnect");

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

	if(game["am_battlechatter"] != 1) return;
	

}




TalkEnemyGekillt(killed_team) {

	self endon("killed_player");
	self endon("disconnect");


	if(game["am_battlechatter"] != 1 || game["am_battlechatter_comments_probability"] == 0) return;

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


	wait 1;

	// * Nicht jedes Mal talken *
	if(RandomInt(100) >= game["am_battlechatter_comments_probability"]) return;


	if(isDefined(self.bc_isTalking)) return;
	
	// * Wenn schon ein TeamMate in 800m Range spricht *
	if(self CheckTalkEnemyGekilled(800)) return;



	self.bc_isTalking 		= "EnemyGekillt";	
	self.bc_lastTalkingTime = getTime();



	voice = RandomInt(4);
	
	if(isDefined(killed_team) && killed_team == "GE") {
		
		team_kurz = "US";
		
		switch(game["allies"]) {

			case "american":
				team_kurz = "US";			
				break;
			case "british":
				team_kurz = "UK";			
				break;		
			case "russian":
				team_kurz = "RU";			
				break;
		}	
		
		self thread DoTalking("am_" + team_kurz + "_" + voice + "_inform_killconfirm_infantry");								
	}
	
	if(isDefined(killed_team) && (killed_team == "RU" || killed_team == "US" || killed_team == "UK")) {
	
		self thread DoTalking("am_GE_" + voice + "_inform_killconfirm_infantry_" + killed_team);	
	}	
	
	wait 5;
	self.bc_isTalking 		= undefined;	
		
}

TalkFriendGekillt(killed_team) {

	self endon("disconnect");


	if(game["am_battlechatter"] != 1) return;


	// * Wenn DM dann kein Sorry *
	if(game["am_gametype"] == "dm") return;


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


	wait 2;

	self thread DoTalking(killed_team + "_mp_rsp_sorry");								
	self sayAll(&"QUICKMESSAGE_SORRY");

}



TalkGrenadeThrow() {

	self notify("TalkGrenadeThrow");
	self endon("TalkGrenadeThrow");


	self endon("killed_player");
	self endon("disconnect");

	if(game["am_battlechatter"] != 1 || game["am_battlechatter_comments_probability"] == 0) return;


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


	granaten_anzahl_old = self maps\mp\gametypes\_weapons::getFragGrenadeCount();


	while(1) {
	
	
		wait 0.5;


		granaten_anzahl = self maps\mp\gametypes\_weapons::getFragGrenadeCount();

		if(granaten_anzahl < granaten_anzahl_old) {



			// * Nicht jedes Mal talken *
			if(RandomInt(100) >= game["am_battlechatter_comments_probability"]) {
			
				granaten_anzahl_old = self maps\mp\gametypes\_weapons::getFragGrenadeCount();
				continue;
			}

				
			// * Wenn er selber schon spricht, dann nicht noch eins draufsetzen *	
			if(isDefined(self.bc_isTalking)) return;

			// * Wenn ein TeamMate grade spricht, dann nicht noch zusätzlich... *
			if(self CheckTeamMateIsTalking(800)) return;


			
			self.bc_isTalking 		= "ThrowGrenade";	
			self.bc_lastTalkingTime = getTime();



			voice = RandomInt(4);

			if(isDefined(self.pers["team"]) && self.pers["team"] == "allies") {

				team_kurz = "US";

				switch(game["allies"]) {

					case "american":
						team_kurz = "US";			
						break;
					case "british":
						team_kurz = "UK";			
						break;		
					case "russian":
						team_kurz = "RU";			
						break;
				}	

				self thread DoTalking("am_" + team_kurz + "_" + voice + "_inform_attacking_grenade");								
			}
			else {

				team_kurz = "US";

				switch(game["allies"]) {

					case "american":
						team_kurz = "US";			
						break;
					case "british":
						team_kurz = "UK";			
						break;		
					case "russian":
						team_kurz = "RU";			
						break;
				}	

				self thread DoTalking("am_GE_" + voice + "_inform_attacking_grenade_" + team_kurz);	
			}	

			wait 5;			
			granaten_anzahl_old = self maps\mp\gametypes\_weapons::getFragGrenadeCount();

			self.bc_isTalking 	= undefined;			
		
		}
	
	}
}


TalkGrenadeWarning(pos, radius) {

	self notify("TalkGrenadeWarning");
	self endon("TalkGrenadeWarning");


	self endon("killed_player");
	self endon("disconnect");

	if(game["am_battlechatter"] != 1 || game["am_battlechatter_comments_probability"] == 0) return;


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


			// * Nur wenn Teammates in der Nähe, dann warnen *
			if(maps\mp\gametypes\_admiral_mod_player::checkOtherPlayerInRange(self.clientid, pos, self.pers["team"], radius) == -1) return;
			
	
			// * Wenn er selber schon spricht, dann nicht noch eins draufsetzen *	
			if(isDefined(self.bc_isTalking)) return;

			// * Wenn ein TeamMate grade spricht, dann nicht noch zusätzlich... *
			if(self CheckTeamMateIsTalking(800)) return;


			
			self.bc_isTalking 		= "GrenadeWarning";	
			self.bc_lastTalkingTime = getTime();



			voice = RandomInt(4);

			if(isDefined(self.pers["team"]) && self.pers["team"] == "allies") {

				team_kurz = "US";

				switch(game["allies"]) {

					case "american":
						team_kurz = "US";			
						break;
					case "british":
						team_kurz = "UK";			
						break;		
					case "russian":
						team_kurz = "RU";			
						break;
				}	

				self thread DoTalking("am_" + team_kurz + "_" + voice + "_inform_incoming_grenade");								
			}
			else {
				

				self thread DoTalking("am_GE_" + voice + "_inform_incoming_grenade");	
			}	

			wait 5;			
			
			self.bc_isTalking 	= undefined;	
			
			
				
	
	
}


TalkDirection(from_origin,to_origin) {

	self endon("killed_player");
	self endon("disconnect");


	if(game["am_battlechatter"] != 1 || game["am_battlechatter_comments_probability"] == 0) return;


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


	wait 1;

	// * Nicht jedes Mal talken *
	if(RandomInt(100) >= game["am_battlechatter_comments_probability"]) return;


	if(isDefined(self.bc_isTalking)) return;
	

	self.bc_isTalking 		= "TalkDirection";	
	self.bc_lastTalkingTime = getTime();




	angles = vectortoangles (to_origin - from_origin);
	angle = angles[1];
	
	northYaw = getnorthyaw();
	angle -= northYaw;
	
	if (angle < 0)
		angle += 360;
	else if (angle > 360)
		angle -= 360;
	
	if (angle < 22.5 || angle > 337.5)
		direction = "north";
	else if (angle < 67.5)
		direction = "northwest";
	else if (angle < 112.5)
		direction = "west";
	else if (angle < 157.5)
		direction = "southwest";
	else if (angle < 202.5)
		direction = "south";
	else if (angle < 247.5)
		direction = "southeast";
	else if (angle < 292.5)
		direction = "east";
	else if (angle < 337.5)
		direction = "northeast";
	else 
		direction = "impossible";
		
		
		
	if(direction == "impossible") return;


	voice = RandomInt(4);
	
	if(isDefined(self.pers["team"])) {
	
		
		if(self.pers["allies"]) {
		
			team_kurz = "US";
		
			switch(game["allies"]) {

				case "american":
					team_kurz = "US";			
					break;
				case "british":
					team_kurz = "UK";			
					break;		
				case "russian":
					team_kurz = "RU";			
					break;
			}	
			self thread DoTalking("am_" + team_kurz + "_" + voice + "_direction_inbound_" + direction);								
		}
		else {
		
			self thread DoTalking("am_GE_" + voice + "_direction_inbound_" + direction);
		}
	}
	
	wait 5;
	self.bc_isTalking 		= undefined;	
		
}


TalkDeath() {

	self endon("disconnect");

	
	if(game["am_battlechatter"] != 1 || game["am_battlechatter_comments_probability"] == 0) return;

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

	// * Nicht jedes Mal *
	if(RandomInt(100) >= 60) return;

	self playSound("am_Death");				
}




DoTalking(soundalias) {

	self endon("killed_player");
	self endon("disconnect");


	if(self.sessionstate != "playing") return;
	
	
	if(game["am_error_logfile"] == 1) logPrint("DoTalking\n");


	self saveHeadIcon();

	self.headiconteam = "none";
	self.headicon = "talkingicon";

	self playSound(soundalias);	
	self pingPlayer();

	self restoreHeadIcon();
}



saveHeadIcon() {

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


	self endon("killed_player");
	self endon("disconnect");

	if(isdefined(self.headicon))
		self.oldheadicon = self.headicon;

	if(isdefined(self.headiconteam))
		self.oldheadiconteam = self.headiconteam;
}

restoreHeadIcon() {

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


	self endon("killed_player");
	self endon("disconnect");

	if(isdefined(self.oldheadicon))
		self.headicon = self.oldheadicon;

	if(isdefined(self.oldheadiconteam))
		self.headiconteam = self.oldheadiconteam;
}





