Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames

Tyrion.AI_GuardSensor


00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
//=====================================================================
// AI_GuardSensor
// Notifies AI if an enemy is inside guard area
// Value (object): target for guard action
//=====================================================================

class AI_GuardSensor extends AI_Sensor;

//=====================================================================
// COnstants

const NO_SHOT_TIMEOUT = 3.0f;	// time after which a new target is chosen because old target couldn't be shot at (multiplier on weapon refire rate)

//=====================================================================
// Variables

// Parameters
var Vector engagementAreaCenter;
var Actor engagementAreaTarget;
var float engagementAreaRadius;

// Internal
var AI_EnemySensor enemySensor;
var AI_Sensor timerSensor;
var Rook rook;							// the character/vehicle/turret this sensor is running on
var VehicleMountedTurret mountedTurret;	// the rook this sensor is running on (or None)
var Turret turret;						// the turret this sensor is running on (or None)
var Vehicle vehicle;					// the vehicle this sensor is running on (or None)
var int nEnemies;						// number of seen enemies on last sensor callback

//=====================================================================
// Functions

//---------------------------------------------------------------------
// Sensor callback (from enemySensor or timerSensor)

function onSensorMessage( AI_Sensor sensor, AI_SensorData value, Object userData )
{
	local Pawn target;
	local Pawn newTarget;

	//log( name @ "(" @ rook.name @ ") sensorMessage called by" @ sensor.name @ "with value" @ value.integerData );

	if ( sensor == enemySensor ) 
	{
		if ( value.integerData == 0 && timerSensor != None )		// no enemies seen
		{
			timerSensor.deactivateSensor( self );
			timerSensor = None;
		}
		if ( value.integerData > 0 && timerSensor == None )			// an enemy in my field of view
		{
			timerSensor = class'AI_Sensor'.static.activateSensor( self, class'AI_TimerSensor', None, 0, 99999999999999 );
			timerSensor.sendMessageOnNextValueUpdate( self );

			// new enemy seen
			if ( value.integerData > nEnemies && !rook.IsA( 'AICivilian' ) )
				rook.level.speechManager.PlayDynamicSpeech( rook, 'Detect', None, enemySensor.lastSeen );
		}
		nEnemies = value.integerData;
	}

	if ( timerSensor != None && sensor == timerSensor )
	{
		//log( "== setting bNotify" );
		timerSensor.sendMessageOnNextValueUpdate( self );	// receive timer message next tick - regardless of value
	}

	target = Pawn(queryObjectValue());

	// sanityCheck( target );

	// already attacking something
	if ( target != None )
	{
		// continue checking if target is within engagement range
		// (doesn't do LOS checks - GuardAttack decides if target should be abandoned because of LOS)
		// (stops attacking if player becomes a target)

		if ( isGuardTarget( target ) &&
			 ( enemySensor.player == target || !noShotTimeout( rook, target ) ) &&
			 ( enemySensor.player == target || enemySensor.player == None || !isGuardTarget( enemySensor.player )))
		{
			//log( name @ "(" @ rook.name @ "): continuing to attack" @ target.name @ "(" @ enemySensor.player @ ")" );
			return;
		}
		else
		{
			newTarget = findGuardTarget();
			if ( newTarget != target )
			{
				setObjectValue( None );			// deactivate the old action
				if ( rook.logTyrion )
					log( name @ "(" @ rook.name @ "):" @ target.name @ "no longer valid target; isGuardTarget:" @
							isGuardTarget( target ) @ "!timout:" @ !noShotTimeout( rook, target ) );
				target = None;
			}
		}
	}

	// not attacking anything
	if ( target == None )
	{
		if ( newTarget == None )
			target = findGuardTarget();	// find a new target
		else
			target = newTarget;
		if ( target != None )
		{
			if ( rook.logTyrion )
				log( name @ "(" @ rook.name @ ") engaging" @ target.name );
			setObjectValue( target );
		}
	}
}

//---------------------------------------------------------------------
// find a suitable enemy to attack in the engagement area

private final function Pawn findGuardTarget()
{
	local int i;
	local int n;

	// player always considered first
	if ( enemySensor.player != None && isGuardTarget( enemySensor.player ) )
		return enemySensor.player;

	n = enemySensor.enemies.length;
	i = Rand( n );

	// now look at other enemies
	while ( n > 0 )
	{
		if ( isGuardTarget( enemySensor.enemies[i] ) )
		{
			return enemySensor.enemies[i];
			break;
		}
		i = (i+1) % enemySensor.enemies.length;
		--n;
	}

	return None;
}

//---------------------------------------------------------------------
// is the specified pawn a valid target for this guard action?

private final function bool isGuardTarget( Pawn target )
{
	local Vector centerLocation;
	local Rook rookTarget;

	rookTarget = Rook(target);

	if ( rookTarget == None )
	{
		log( "AI ERROR: invalid target" @ target @ "in" @ rook.name $"'s 'isGuardTarget'" );
		return false;
	}

	// Is the target something AI's should shoot at? 
	if ( !rookTarget.bAIThreat || rook.isFriendly( rookTarget ))
		return false;

	// if guarder is a turret and turret cannot be pointed at target, don't pick this target
	// (shot leading is not taken into consideration here)
	if ( turret != None && !turret.canTargetPoint( target.Location ) )
		return false;

	if ( vehicle != None && !vehicle.canTargetPoint( target.Location ) )
		return false;

	if ( mountedTurret != None && !mountedTurret.canTargetPoint( target.Location ) )
		return false;

	// add turret type specific checks (if any)....

	if ( engagementAreaTarget == None )
		centerLocation = engagementAreaCenter;
	else
		centerLocation = engagementAreaTarget.Location;

	//log( target.name @ VDist( target.Location, centerLocation ) @ "/" @ engagementAreaRadius);

	return VDistSquared( target.Location, centerLocation ) <= engagementAreaRadius * engagementAreaRadius;
}

//---------------------------------------------------------------------
// sanity check: Guard sensor shouldn't have a value if GuardAttackGoal is inactive

private final function sanityCheck( Pawn target )
{
	local int i;

	if ( target != None && rook.characterAI != None )
	{
		for ( i = 0; i < rook.characterAI.goals.length; ++i )
			if ( AI_Goal(rook.characterAI.goals[i]).activationSentinel.sensor == self && AI_Goal(rook.characterAI.goals[i]).bInActive )
			{
				log( "AI ERROR:" @ rook.name @ "(" $ rook.label $ ")" @ rook.characterAI.goals[i].name @ "is inActive yet sensor is set to" @ target.label );
				assert( false );
			}
	}
}

//---------------------------------------------------------------------
// returns true of GuardAttack should time out because no shot was fired

private final function bool noShotTimeOut( Rook rook, Pawn target )
{
	local Weapon weapon;
	local float maxRange;

	weapon = rook.firingMotor().getWeapon();
	if ( weapon == None )
		return false;

	// if target is out of range of the weapon, give attacker a chance to close in
	maxRange = class'AimFunctions'.static.getMaxEffectiveRange( weapon.class );
	if ( maxRange >= 0 && VDistSquared( rook.Location, target.Location ) > maxRange * maxRange )
		return false;

	return (rook.lastShotFiredTime < rook.level.timeSeconds - NO_SHOT_TIMEOUT / weapon.roundsPerSecond);
}

//---------------------------------------------------------------------
// perform sensor-specific startup initializations when sensor is activated

function begin()
{
	rook = sensorAction.resource.localRook();
	mountedTurret = VehicleMountedTurret(rook);
	turret = Turret(rook);
	vehicle = Vehicle(rook);
	enemySensor = AI_EnemySensor(class'AI_Sensor'.static.activateSensor( self, class'AI_EnemySensor', sensorAction.resource, 0, 1000000 ));
	enemySensor.setParameters();
}

//---------------------------------------------------------------------
// perform sensor-specific cleanup when sensor is deactivated

function cleanup()
{
	if ( enemySensor != None )
	{
		enemySensor.deactivateSensor( self );
		enemySensor = None;
	}

	if ( timerSensor != None )
	{
		timerSensor.deactivateSensor( self );
		timerSensor = None;
	}
}

//---------------------------------------------------------------------
// Initialize set the sensor's parameters
// engagementAreaCenter
// engagementAreaTarget
// engagementAreaRadius

function setParameters( float aEngagementAreaRadius, Vector aEngagementAreaCenter, optional Actor aEngagementAreaTarget )
{
	engagementAreaRadius = aEngagementAreaRadius;
	engagementAreaCenter = aEngagementAreaCenter;
	engagementAreaTarget = aEngagementAreaTarget;

	//log( name @ "(" @ sensorAction.resource.localRook().name @ ") initializing" @ findGuardTarget() );

	setObjectValue( findGuardTarget() ); 
}

//=====================================================================

defaultproperties
{
	bNotifyOnValueChange = true
}

Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames
Class file time: ne 5.9.2004 15:54:30.000 - Creation time: st 23.5.2018 00:10:41.299 - Created with UnCodeX