#using scripts\codescripts\struct;

#using scripts\shared\callbacks_shared;
#using scripts\shared\clientfield_shared;
#using scripts\shared\duplicaterender_mgr;
#using scripts\shared\util_shared;

#using scripts\mp\_shoutcaster;

                                                                                
    	   	                                                                                                                                         	                                	                                                                                                                                                                                                                                                                                                                                                                    	        	     	              	    	   	               	                                              	                                                           	                               	                     	                                                                                                           	                                                                 	                                                              	                                                                                                                                             	                            	                                     	                                       	                                                                                                              	   	                  	       	                                                    	                   	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            	                                                                                                 	                                    	                                                                                        	           	                        	                                            	                                             	                                                   	                                                             	                                                         	                                                                    	                                                                                                                                                                                                                                	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  	    	         	                                                              	                                                          	                                   	                                   	                                                    	                                       	                                           	     	                                                                                                                                                                                                                                                      	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         	                                                               	        	                                                                                                                              	                                                                                                                     	                               	                                                      	                          
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         	   	                                                                                                                                                                                                                                  	                                                                                                                                                                                                                                                                                                                                                                                                                                                   






#precache( "client_fx", "ui/fx_uplink_goal_marker" );
#precache( "client_fx", "ui/fx_uplink_goal_marker_flash" );
#precache( "client_fx", "ui/fx_uplink_goal_marker_white" );
#precache( "client_fx", "ui/fx_uplink_goal_marker_white_flash" );

function main()
{	
	clientfield::register( "allplayers", "ballcarrier", 1, 1, "int", &player_ballcarrier_changed, !true, true );
	clientfield::register( "allplayers", "passoption", 1, 1, "int", &player_passoption_changed, !true, !true );
	clientfield::register( "world", "ball_away" , 1, 1, "int",  &world_ball_away_changed, !true, true );
	clientfield::register( "world", "ball_score_allies" , 1, 1, "int",  &world_ball_score_allies, !true, true );
	clientfield::register( "world", "ball_score_axis" , 1, 1, "int",  &world_ball_score_axis, !true, true );
	
	callback::on_localclient_connect( &on_localclient_connect );
	callback::on_spawned( &on_player_spawned );
	
	if ( !GetDvarInt("tu11_programaticallyColoredGameFX") )
	{
		level.effect_scriptbundles = [];
		level.effect_scriptbundles["goal"] = struct::get_script_bundle( "teamcolorfx", "teamcolorfx_uplink_goal" );
		level.effect_scriptbundles["goal_score"] = struct::get_script_bundle( "teamcolorfx", "teamcolorfx_uplink_goal_score" );
	}
}

function on_localclient_connect( localClientNum )
{	
	objective_ids = [];

	while ( !isdefined( objective_ids["allies"] ) )
	{
		objective_ids["allies"] = ServerObjective_GetObjective( localClientNum, "ball_goal_allies" );
		objective_ids["axis"] = ServerObjective_GetObjective( localClientNum, "ball_goal_axis" );
		wait(0.05);
	}
	
	foreach( key, objective in objective_ids )
	{
		level.goals[key] = SpawnStruct();
		level.goals[key].objectiveId = objective;
		setup_goal( localClientNum, level.goals[key] );
	}
	
	setup_fx( localClientNum );
}

function on_player_spawned( localClientNum )
{
	players = GetPlayers( localclientnum );
	foreach( player in players )
	{
		if( player util::IsEnemyPlayer( self ) )
		{
			player duplicate_render::update_dr_flag( localClientNum, "ballcarrier", 0 );
		}
	}
}

function setup_goal( localClientNum, goal )
{
	goal.origin = ServerObjective_GetObjectiveOrigin( localClientNum, goal.objectiveId );
	goal_entity = ServerObjective_GetObjectiveEntity( localClientNum, goal.objectiveId );
	
	if ( isdefined(goal_entity) )
	{
		goal.origin = goal_entity.origin;
	}
	
	goal.team = ServerObjective_GetObjectiveTeam( localClientNum, goal.objectiveId );
}

function setup_goal_fx( localClientNum, goal, effects )
{
	if ( isdefined( goal.base_fx ) )
	{
		StopFx( localClientNum,	goal.base_fx );
	}
	
	goal.base_fx = PlayFx(localClientNum, effects[goal.team], goal.origin );
	SetFxTeam( localClientNum, goal.base_fx, goal.team );
}

function setup_fx( localClientNum )
{
	effects = [];
	
	if ( shoutcaster::is_shoutcaster_using_team_identity(localClientNum) )
	{
		if ( GetDvarInt("tu11_programaticallyColoredGameFX") )
		{
			effects["allies"] = "ui/fx_uplink_goal_marker_white";
			effects["axis"] = "ui/fx_uplink_goal_marker_white";
		}
		else
		{	
		effects = shoutcaster::get_color_fx( localClientNum, level.effect_scriptbundles["goal"] );
		}
	}
	else
	{
		effects["allies"] = "ui/fx_uplink_goal_marker";
		effects["axis"] = "ui/fx_uplink_goal_marker";
	}
	
	foreach( goal in level.goals)
	{
		thread setup_goal_fx(localClientNum, goal, effects );
		thread resetOnDemoJump( localClientNum, goal, effects );
	}
	
	thread watch_for_team_change( localClientNum );
}

function play_score_fx( localClientNum, goal)
{
	effects = [];
	
	if ( shoutcaster::is_shoutcaster_using_team_identity(localClientNum) )
	{
		if ( GetDvarInt("tu11_programaticallyColoredGameFX") )
		{
			effects["allies"] = "ui/fx_uplink_goal_marker_white_flash";
			effects["axis"] = "ui/fx_uplink_goal_marker_white_flash";
		}
		else
		{	
			effects = shoutcaster::get_color_fx( localClientNum, level.effect_scriptbundles["goal_score"] );
		}
	}
	else
	{
		effects["allies"] = "ui/fx_uplink_goal_marker_flash";
		effects["axis"] = "ui/fx_uplink_goal_marker_flash";
	}

	fx_handle = PlayFx(localClientNum, effects[goal.team], goal.origin );
	SetFxTeam( localClientNum, fx_handle, goal.team );
}

function play_goal_score_fx( localClientNum, team, oldVal, newVal, bInitialSnap, bWasTimeJump )
{
	if( ( newVal != oldVal ) && !bInitialSnap && !bWasTimeJump )
	{
		play_score_fx( localClientNum, level.goals[team] );
	}
}

function world_ball_score_allies( localClientNum, oldVal, newVal, bNewEnt, bInitialSnap, fieldName, bWasTimeJump )
{
	play_goal_score_fx( localClientNum, "allies", oldVal, newVal, bInitialSnap, bWasTimeJump );
}

function world_ball_score_axis( localClientNum, oldVal, newVal, bNewEnt, bInitialSnap, fieldName, bWasTimeJump )
{
	play_goal_score_fx( localClientNum, "axis", oldVal, newVal, bInitialSnap, bWasTimeJump );
}

function player_ballcarrier_changed( localClientNum, oldVal, newVal, bNewEnt, bInitialSnap, fieldName, bWasTimeJump )
{
	localplayer = getlocalplayer( localClientNum );
	
	if( localplayer == self )
	{
		if( newVal )
		{
			self._hasBall = true;
		}
		else
		{
			self._hasBall = false;
			SetUIModelValue( CreateUIModel( GetUIModelForController( localClientNum ), "ballGametype.passOption" ), 0 );
		}
	}
	
	if( ( localplayer != self ) && self isFriendly( localClientNum ) )
	{
		self set_player_ball_carrier_dr( localClientNum, newVal );
	}
	else
	{	
		self set_player_ball_carrier_dr( localClientNum, false );
	}
	
//	if ( newVal == 1 )
//	{
//		self set_hud(localClientNum);
//	}
//	else
//	{
//		self clear_hud( localClientNum );
//	}	
	
	if ( isdefined( level.ball_carrier ) && level.ball_carrier != self )
		return;
		
	level notify( "watch_for_death" );

	if ( newVal == 1 )
	{
		// we need to track when the player dies because if they watch killcam we will not get flag updates
		self thread watch_for_death( localClientNum );
	}
}

function set_hud( localClientNum )
{
		level.ball_carrier = self;
		
		if ( shoutcaster::is_shoutcaster( localClientNum ) )
		{
			friendly = self shoutcaster::is_friendly( localclientnum );
		}
		else
		{
			friendly = self isFriendly( localClientNum );
		}
		
		if( isdefined( self.name ) )		
			SetUIModelValue( CreateUIModel( GetUIModelForController( localClientNum ), "ballGametype.ballStatusText" ),  self.name );	
		else 
			SetUIModelValue( CreateUIModel( GetUIModelForController( localClientNum ), "ballGametype.ballStatusText" ),  "" );	
		
		if( isdefined( friendly ) )
		{
			SetUIModelValue( CreateUIModel( GetUIModelForController( localClientNum ), "ballGametype.ballHeldByFriendly" ), friendly );
			SetUIModelValue( CreateUIModel( GetUIModelForController( localClientNum ), "ballGametype.ballHeldByEnemy" ), !friendly );
		}
		else
		{
			SetUIModelValue( CreateUIModel( GetUIModelForController( localClientNum ), "ballGametype.ballHeldByFriendly" ), 0 );
			SetUIModelValue( CreateUIModel( GetUIModelForController( localClientNum ), "ballGametype.ballHeldByEnemy" ), 0 );
		}
}

function clear_hud( localClientNum )
{
		level.ball_carrier = undefined;

		SetUIModelValue( CreateUIModel( GetUIModelForController( localClientNum ), "ballGametype.ballHeldByEnemy" ), 0 );
		SetUIModelValue( CreateUIModel( GetUIModelForController( localClientNum ), "ballGametype.ballHeldByFriendly" ), 0 );
		SetUIModelValue( CreateUIModel( GetUIModelForController( localClientNum ), "ballGametype.ballStatusText" ),  &"MPUI_BALL_AWAY" );
}

function watch_for_death( localClientNum )
{
	level endon( "watch_for_death" );
	
	self waittill( "entityshutdown" );

	//self clear_hud(localClientNum);	
}

function player_passoption_changed( localClientNum, oldVal, newVal, bNewEnt, bInitialSnap, fieldName, bWasTimeJump )
{
	localplayer = getlocalplayer( localClientNum );
	
	if( ( localplayer != self ) && self isFriendly( localClientNum ) )
	{
		if( ( isdefined( localplayer._hasBall ) && localplayer._hasBall ) )
		{
			SetUIModelValue( CreateUIModel( GetUIModelForController( localClientNum ), "ballGametype.passOption" ), newVal );
		}
	}
}

function world_ball_away_changed( localClientNum, oldVal, newVal, bNewEnt, bInitialSnap, fieldName, bWasTimeJump )
{
	SetUIModelValue( CreateUIModel( GetUIModelForController( localClientNum ), "ballGametype.ballAway" ), newVal );
	
	//if( newVal == 0 || bInitialSnap )
	//{
		//SetUIModelValue( CreateUIModel( GetUIModelForController( localClientNum ), "ballGametype.ballHeldByEnemy" ), 0 );
		//SetUIModelValue( CreateUIModel( GetUIModelForController( localClientNum ), "ballGametype.ballHeldByFriendly" ), 0 );
		//SetUIModelValue( CreateUIModel( GetUIModelForController( localClientNum ), "ballGametype.ballStatusText" ),  &"MPUI_BALL_HOME" );
	//}
}

function set_player_ball_carrier_dr( localClientNum, on_off )
{
	self duplicate_render::update_dr_flag( localClientNum, "ballcarrier", on_off );
}

function set_player_pass_option_dr( localClientNum, on_off )
{
	self duplicate_render::update_dr_flag( localClientNum, "passoption", on_off );
}

function resetOnDemoJump( localClientNum, goal, effects )
{
	for (;;)
	{
		level waittill( "demo_jump" + localClientNum ); 
		
		setup_goal_fx( localClientNum, goal, effects );
	}
}

function watch_for_team_change( localClientNum )
{
	level notify( "end_team_change_watch" );
	level endon( "end_team_change_watch" );

	level waittill( "team_changed" );
	
	thread setup_fx( localClientNum );
}