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


BoatAddPlayer(player) {

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

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


	player endon("disconnect");



	// * Wenn für diese MAP keine Positionen existieren, dann auch keine Boote *
	if(!isDefined(game["boat_map_pos"]) ) return false;
	if(!isDefined(game["boat_map_pos"][0][game["am_mapname"] + "_start"]) ) return false;


	while(isDefined(level.add_player_in_boat)) wait 0.1;
	level.add_player_in_boat = true;
	

	
	team = player.pers["team"];
	
	
	if(!isDefined(level.boat_counter)) {
	
		level.boat_counter = 1;
		
		
		boat_id = level.boat_counter - 1;

		start 	= game["boat_map_pos"][boat_id][game["am_mapname"] + "_start"];
		end   	= game["boat_map_pos"][boat_id][game["am_mapname"] + "_end"];
		
		angles 	= vectortoangles(vectornormalize(end - start));
		speed   = 20;
		
		
		CreateBoat(boat_id, speed, start, end, angles, team);
		player PlacePlayerInSeat(boat_id);
		
		
		
		level.add_player_in_boat = undefined;		
		return true;
	}
	else {
	
	
		// * Prüfen ob Boot mit dem selben Team grade fährt *	
		boat_id = GetBoat(team);


		// * Wenn Boot gefunden, dann Player dort hineinsetzen *
		if(boat_id > -1) {
		
			player PlacePlayerInSeat(boat_id);


			level.add_player_in_boat = undefined;
			return true;
		}
		else {
		
			level.boat_counter++;
			if(level.boat_counter > game["boat_map_pos"].size) level.boat_counter = 1;
			
			if(!isDefined(level.boat[level.boat_counter-1])) {
			
				boat_id = level.boat_counter - 1;

				start 	= game["boat_map_pos"][boat_id][game["am_mapname"] + "_start"];
				end   	= game["boat_map_pos"][boat_id][game["am_mapname"] + "_end"];

				angles 	= vectortoangles(vectornormalize(end - start));
				speed   = 20;


				CreateBoat(boat_id, speed, start, end, angles, team);
				player PlacePlayerInSeat(boat_id);


				level.add_player_in_boat = undefined;
				return true;
			
			}
			else {
			
				level.add_player_in_boat = undefined;
				return false;
			}
		}
	}
	
	
	level.add_player_in_boat = undefined;
	return false;
	
	
}


GetBoat(team) {

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


	// * fahrendes Boot mit dem selben Team und freien Sitzplaetzen suchen *

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

		if(isDefined(level.boat[i]) && level.boat[i].driving == true && level.boat[i].team == team) {

			boat_id = i;
			
			// * Wenn in diesem Boot noch ein Platz frei ist, dann OK *
			if(GetFreeSeat(boat_id) >= 0) return boat_id;				
		}
	}		

	return -1;
}




CreateBoat(boat_id, speed, start, end, angles, team) {

	if(game["am_error_logfile"] == 1) logPrint("CreateBoat\n");
	
		
	level.boat[boat_id] = spawn("script_model", (0,0,0));	
	level.boat[boat_id].angles = (0,0,0);
	level.boat[boat_id].origin = start;
	level.boat[boat_id] setModel("xmodel/vehicle_higgins_vm");
	level.boat[boat_id].targetname = "boat";	   

	level.boat[boat_id].boat_mortors = spawn("script_model", (0,0,0));	
	level.boat[boat_id].boat_mortors.angles = level.boat[boat_id].angles;
	level.boat[boat_id].boat_mortors.origin = level.boat[boat_id].origin;
	level.boat[boat_id].boat_mortors setModel("xmodel/vehicle_higgins_mortors");
	level.boat[boat_id].boat_mortors.targetname = "boat";
	level.boat[boat_id].boat_mortors linkto(level.boat[boat_id]);
	
	level.boat[boat_id].boat_hook_1 = spawn("script_model",(0,0,0));	
	level.boat[boat_id].boat_hook_1.angles = level.boat[boat_id].angles;
	level.boat[boat_id].boat_hook_1.origin = level.boat[boat_id].origin + (59,-60,70);
	level.boat[boat_id].boat_hook_1.angles = level.boat[boat_id].boat_hook_1.angles + (22,0,0);
	level.boat[boat_id].boat_hook_1 setModel("xmodel/vehicle_higgins_grapple_hook");
	level.boat[boat_id].boat_hook_1.targetname = "boat";	   
	level.boat[boat_id].boat_hook_1 linkto(level.boat[boat_id]);
	
	level.boat[boat_id].boat_hook_2 = spawn("script_model",(0,0,0));	
	level.boat[boat_id].boat_hook_2.angles = level.boat[boat_id].angles;
	level.boat[boat_id].boat_hook_2.origin = level.boat[boat_id].origin + (59,60,70);
	level.boat[boat_id].boat_hook_2.angles = level.boat[boat_id].boat_hook_2.angles + (22,0,0);
	level.boat[boat_id].boat_hook_2 setModel("xmodel/vehicle_higgins_grapple_hook");
	level.boat[boat_id].boat_hook_2.targetname = "boat";	
	level.boat[boat_id].boat_hook_2 linkto(level.boat[boat_id]);
	
	level.boat[boat_id].boat_hook_3 = spawn("script_model",(0,0,0));	
	level.boat[boat_id].boat_hook_3.angles = level.boat[boat_id].angles;
	level.boat[boat_id].boat_hook_3.origin = level.boat[boat_id].origin + (-82,60,70);
	level.boat[boat_id].boat_hook_3.angles = level.boat[boat_id].boat_hook_3.angles + (22,0,0);
	level.boat[boat_id].boat_hook_3 setModel("xmodel/vehicle_higgins_grapple_hook");
	level.boat[boat_id].boat_hook_3.targetname = "boat";		
	level.boat[boat_id].boat_hook_3 linkto(level.boat[boat_id]);
	
	level.boat[boat_id].boat_hook_4 = spawn("script_model",(0,0,0));	
	level.boat[boat_id].boat_hook_4.angles = level.boat[boat_id].angles;
	level.boat[boat_id].boat_hook_4.origin = level.boat[boat_id].origin + (-82,-60,70);
	level.boat[boat_id].boat_hook_4.angles = level.boat[boat_id].boat_hook_4.angles + (22,0,0);
	level.boat[boat_id].boat_hook_4 setModel("xmodel/vehicle_higgins_grapple_hook");
	level.boat[boat_id].boat_hook_4.targetname = "boat";	
	level.boat[boat_id].boat_hook_4 linkto(level.boat[boat_id]);
	
	
	
	level.boat[boat_id].team 	= team;
	level.boat[boat_id].boat_id = boat_id;	

	wait 0.05;
	level.boat[boat_id].angles = angles;
	
	wait 0.05;

	vorne1  = maps\mp\_utility::vectorScale(anglestoforward((level.boat[boat_id].angles)),135);
	vorne2  = maps\mp\_utility::vectorScale(anglestoforward((level.boat[boat_id].angles)),80);
	vorne3  = maps\mp\_utility::vectorScale(anglestoforward((level.boat[boat_id].angles)),30);
	links   = maps\mp\_utility::vectorScale(anglesToForward(level.boat[boat_id].angles + (0,90,0)),25) + (0,0,1);	
	rechts  = maps\mp\_utility::vectorScale(anglesToForward(level.boat[boat_id].angles + (0,-90,0)),25)+ (0,0,1);

	level.boat_seats[boat_id] = [];
	level.boat_seats[boat_id][0] = vorne1 + links;
	level.boat_seats[boat_id][1] = vorne1 + rechts;
	level.boat_seats[boat_id][2] = vorne2 + links;
	level.boat_seats[boat_id][3] = vorne2 + rechts;
	level.boat_seats[boat_id][4] = vorne3 + links;
	level.boat_seats[boat_id][5] = vorne3 + rechts;
	
	
	// * Start Driving *	
	level.boat[boat_id] thread StartDrivingBoat(speed, start, end);	
	
	
	return boat_id;	
}


StartDrivingBoat(speed, start_position, end_position) {

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


	level endon("round_ended");
	
	
	self.driving = true;
	self.in_use  = true;

	helper = spawn("script_model",self.origin);		
	self linkto(helper);	
	
	
	
	zeit = maps\mp\gametypes\_admiral_mod_utils::ZeitSpeedBerechnung(speed, start_position, end_position);
	
	helper moveto(end_position, zeit);	    
    
	
	
	// * Effekte *
	self thread BoatSmallSplash();
	self thread BoatFloorSplash();
	self thread BoatWakeSplash();
	self thread BoatEngineSpray();
	self thread BoatAttack();
	self thread BoatDriveSound();
 
 	wait zeit;
  
  
  	
  	 
  	self.driving = false;
  	

	if(isDefined(self)) playfx(game["boat_explosion_fx"],self.origin + (100,0,0));
	if(isDefined(self)) self playSound("mortar_explosion_water");	
	wait 0.3;
	
	if(isDefined(self)) playfx(game["boat_explosion_fx"],self.origin + (-100,0,0));
	if(isDefined(self)) self playSound("mortar_explosion_water");	
	wait 0.1;
  	  	

	UnlinkPlayersInBoat(self.boat_id);	
	wait 0.05;

	
	level.boat[self.boat_id].boat_mortors delete();
	level.boat[self.boat_id].boat_hook_1 delete();
	level.boat[self.boat_id].boat_hook_2 delete();
	level.boat[self.boat_id].boat_hook_3 delete();
	level.boat[self.boat_id].boat_hook_4 delete();

	level.boat[self.boat_id] setModel("xmodel/vehicle_higgins_destroyed");
	


  	for(i=0;i<RandomInt(2)+1;i++) {
  		
  		teamLetters = GetTeamLetters(self.team);
  	
  		self playSound(teamLetters + "_mp_cmd_movein");	
  		wait 0.60;
  	}

 	self notify("landing");   
 	
 	
 	wait 30;
 	self.in_use = undefined;

 	
 	self delete();
 
}




GetFreeSeat(boat_id) {
	
	if(game["am_error_logfile"] == 1) logPrint("GetFreeSeat\n");


	for(i=0;i<level.boat_seats[boat_id].size;i++) {
		
	
		if( isDefined(level.boat_seats[boat_id][i]) && distance(level.boat_seats[boat_id][i],(-1,-1,-1)) > 0 ) {
			
			seat_nr = i;
			return seat_nr;			
		}
	}	


	
	return -1;
}


PlacePlayerInSeat(boat_id) {

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


	self endon("disconnect");

	helper = spawn("script_model",self.origin);		
	self linkto(helper);	
	
	seat = GetFreeSeat(boat_id);
	helper.origin = level.boat[boat_id].origin + level.boat_seats[boat_id][seat];
	wait 0.05;
	
	self unlink();
	wait 0.05;
	
	self linkto(level.boat[boat_id]);
	
	self.pers["am_boat_id"] = boat_id;
	
	
	helper delete();
	
	// * Sitz als belegt deklarieren *
	level.boat_seats[boat_id][seat] = (-1,-1,-1);
}


UnlinkPlayersInBoat(boat_id) {
	
	if(game["am_error_logfile"] == 1) logPrint("UnlinkPlayersInBoat\n");


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

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

		player = players[i];

		if(isDefined(player.pers["am_boat_id"]) && player.pers["am_boat_id"] == boat_id) {

			player.pers["am_boat_id"] = undefined;
			player unlink();
		}
	}

}




BoatSmallSplash() {

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


	level endon("round_ended");
	self endon("landing");

	for (;;) {	
		
	
		if(isDefined(self)) {
		
			playfxOnTag(game["boat_splash_fx"],self,"tag_splash");
			
			pos1 = maps\mp\_utility::vectorScale(anglestoforward((self.angles)),1) + (79,-60,70);
			pos2 = maps\mp\_utility::vectorScale(anglestoforward((self.angles)),1) + (79,60,70);
			pos3 = maps\mp\_utility::vectorScale(anglestoforward((self.angles)),1) + (-62,-60,70);
			pos4 = maps\mp\_utility::vectorScale(anglestoforward((self.angles)),1) + (-62,60,70);

			playfx(game["boat_sidesplash_fx"],self.origin + pos1);
			playfx(game["boat_sidesplash_fx"],self.origin + pos2);
			playfx(game["boat_sidesplash_fx"],self.origin + pos3);
			playfx(game["boat_sidesplash_fx"],self.origin + pos4);

		}
		
		wait 0.3;	
	}   
}


BoatWakeSplash() {

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


	level endon("round_ended");
	self endon("landing");

	for (;;) {	
		
	
		if(isDefined(self)) {
		
			playfxOnTag(game["boat_wake_fx"],self,"tag_wake");
			
		}

		wait 1;	
	}   
}



BoatFloorSplash() {

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


	level endon("round_ended");
	self endon("landing");

	for (;;) {	
		

		if(isDefined(self)) {
		
			playfxOnTag(game["boat_floorsplash_fx"],self,"tag_splash");						
		}
		
		wait 4;	
	}   
}



BoatEngineSpray() {

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


	level endon("round_ended");
	self endon("landing");
 
	for (;;) {
		
		playfxOnTag (game["boat_enginespray_fx"], self, "tag_engineFX");	

		playfxOnTag(game["boat_sidesplash_fx"],self,"tag_engine_right");
		playfxOnTag(game["boat_sidesplash_fx"],self,"tag_engine_left");

		wait 0.8;
	}
}


BoatDriveSound() {

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


	level endon("round_ended");
	self endon("landing");

 
	for (;;) {

		
		pos = maps\mp\_utility::vectorScale(anglestoforward((self.angles)),320) + (0,0,30);

		helper = spawn("script_model",(0,0,0));		
		helper.origin = self.origin + pos;
		wait 0.05;
	
		helper playSound("water_splash_shipbow");
		
		wait 1 + RandomFloat(2);
		
		helper delete();
		wait 0.05;
	}
}



BoatAttack() {

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


	level endon("round_ended");
	self endon("landing");


	attackBegin 	= RandomInt(1) + 3;
	attackStrength 	= 2 - RandomInt(2);


	wait attackBegin;
	
	// * Attack begin *
	teamLetters = GetTeamLetters(self.team);
	
	self playSound(teamLetters + "_mp_rsp_squad");	
	wait 1;
	
	cmd_speaker = RandomInt(3);
	if(cmd_speaker == 0) 	{
			
		if(isDefined(self)) self playSound(teamLetters + "_mp_cmd_suppressfire");	
	}
	else if(cmd_speaker == 1) {
	
		if(isDefined(self)) self playSound(teamLetters + "_mp_cmd_holdposition");	
	}
	else {
	
		if(isDefined(self)) self playSound(teamLetters + "_mp_stm_holdyourfire");	
	}
	

	for (;;) {
		
		x = RandomInt(1600)-800;
		y = RandomInt(1600)-800;
		
		
		if(isDefined(self)) self playSound("mortar_incoming");
		wait (RandomFloat(1) + 0.3);

		if(isDefined(self)) playfx(game["boat_water_explosions_fx"], self.origin + (x,y,-20));
		
		if(isDefined(self)) self playSound("mortar_explosion_water");		
		
		
		earthquake( 0.25, 3, self.origin, 1050 );
		
		
		// * Wie oft die Mortars einschlagen *
		wait RandomFloat(attackStrength);
	}
	

}



PlaneAddPlayer() {


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


	position = (400,0,350);
	

	level.tester0 = spawn("script_model",(0,0,0));	
	level.tester0.origin = (0,0,260);
	level.tester0 setModel("xmodel/vehicle_condor");
	level.tester0.angles = (0,0,0);


	level.tester = spawn("script_model",(0,0,0));	
	level.tester.origin = (-260,0,400);
	level.tester setModel("xmodel/railpipe_d01");
	level.tester.angles = (90,0,0);
	
	level.tester2 = spawn("script_model",(0,0,0));	
	level.tester2.origin = (-40,0,400.5);
	level.tester2 setModel("xmodel/railpipe_d01");
	level.tester2.angles =(90,0,0);
	
	level.tester3 = spawn("script_model",(0,0,0));	
	level.tester3.origin = (150,0,400.25);
	level.tester3 setModel("xmodel/railpipe_d01");
	level.tester3.angles = (90,0,0);


	level.xta = spawn("script_model",(0,0,0));	
	level.xta.angles = level.tester;

    level.xta = spawn("script_model",(0,0,0));	
	level.xta.origin = self.origin;

	self linkto(level.xta);
	
	level.xta moveto(position,1);



}





GetTeamLetters(team) {

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


	team_letters = "US";
	
	if(team == "allies") 	{
	
		if(game[team] == "american") 	team_letters = "US";
		if(game[team] == "russian") 	team_letters = "RU";
		if(game[team] == "british") 	team_letters = "UK";		
	}
	else team_letters = "GE";	
	
	return team_letters;
}

