Overview | Package | Class | Source | Class tree | Glossary | UnrealScript Documentation
previous class next class | frames no frames

# Tyrion.AI_DodgeSensor

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

    //=====================================================================
    // AI_DodgeSensor
    // Keeps tracks of this pawn's enemies
    // Value (object): pointer to the shooter that triggered dodge
    //=====================================================================

    class AI_DodgeSensor extends AI_Sensor implements IShotNotification;

    //=====================================================================
    // Constants

    const DODGE_DURATION = 2.0f;		// how many seconds will the dodge displacement be valid for
    const DEFAULT_DODGE_DISTANCE = 250;			// how far the pawn dodges
    const MIN_DODGE_BY_JETPACKING_TIME = 0.3f;	// min. time needed to dodge by jetpacking
    const MAX_DODGE_BY_JETPACKING_TIME = 0.8f;	// max. time needed to dodge by jetpacking
    const MAX_COLLISION_PREDICTION_ITERATIONS = 100;
    //const RUN_SPEED = 555.0f;			// 25km/h / 3.6f * 80.0f

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

    var Rook protectee;					// thing being protected; set if this AI is trying to block shots (None otherwise)
    var float shieldPackTime;			// use shield pack if impact in less than this many seconds

    var AI_EnemySensor enemySensor;
    var BaseAICharacter ai;				// the character this sensor is running on

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

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

    function onSensorMessage( AI_Sensor sensor, AI_SensorData value, Object userData )
    {
    	//log( name @ "sensorMessage called by" @ sensor.name @ "with value" @ value.integerData );

    	if ( enemySensor.lastSeen != None )
    		enemySensor.lastSeen.RegisterShotNotification( self );

    	if ( enemySensor.lastLost != None )
    		enemySensor.lastLost.UnRegisterShotNotification( self );
    }

    //---------------------------------------------------------------------
    // shot callbacks

    function OnShooterFiredShot( Pawn shooter, Actor projectile )
    {
    	local Weapon weapon;
    	local Vector hitLocation;
    	local float timeToHit;		// estimate of when projectile will hit me
    	local Vector dodgeDisplacement;
    	local bool bHitProtectee;		// will this projectile do damage to 'protectee'?
    	local bool bHitMe;				// will this projectile do damage to me
    	local AI_Controller controller;
    	local Vector lastValidLocation;
    	//local float timeToDodge;

    	local array<Actor> ignore; // passed to canPointBe Reached

    	assert( ai != None );
    	if ( ai.Controller == None )
    		return;					// ai is in a vehicle

    	if ( ai.Physics != PHYS_Movement )
    	{
    		log( "AI WARNING: OnShooterFiredShot called on" @ ai.name @ "but AI is is physics mode" @ ai.Physics );
    		return;
    	}

    	weapon = Rook(shooter).firingMotor().getWeapon();
    	controller = AI_Controller(ai.controller);

    	//log( "---->" @ ai.name @ "sez:" @ shooter.name @ "fired" @ projectile.name );

    	if ( projectile.IsA( 'MortarProjectile' ) || projectile.IsA( 'GrenadeLauncherProjectile' ) || projectile.IsA( 'SniperRifleProjectile' ))
    		ai.level.speechManager.PlayDynamicSpeech( ai, 'Detect', None, projectile );

    	if ( protectee != None )
    		bHitProtectee = willHurt( hitLocation, timeToHit, protectee, weapon, projectile );

    	if ( !bHitProtectee )
    		bHitMe = willHurt( hitLocation, timeToHit, ai, weapon, projectile );	// todo: take account of projectiles still en route

    	if ( bHitMe || bHitProtectee )
    	{
    		// "dodgeDirection" will contain a displacement vector that has to be reduced to 0
    		if ( bHitProtectee )
    		{
    			// dodge to block shots /////////////////////////////

    			dodgeDisplacement = class'ActionBase'.static.closestPointOnALine( ai.Location, shooter.Location, projectile.Velocity ) - ai.Location;

    			if ( ai.jetCompetency == JC_NONE )
    				dodgeDisplacement.Z = 0;

    			//log( ai.name @ "Dodging to protect" @ protectee.name $ ". Displacement:" @ dodgeDisplacement @ "( length:" @ VSize( dodgeDisplacement ) @ ")" );
    		}
    		else
    		{
    			// dodge to protect yourself ////////////////////////

    			if ( ai.jetCompetency == JC_NONE || ai.Energy < 10 ||
    				timeToHit < MIN_DODGE_BY_JETPACKING_TIME || timeToHit > MAX_DODGE_BY_JETPACKING_TIME ||
    				!projectile.IsA( 'ExplosiveProjectile' ) )
    			{
    				//log( "dodging by walking" );
    				// dodging by walking: dodge direction is orthogonal to projectile direction
    				dodgeDisplacement.X = -projectile.Velocity.Y;
    				dodgeDisplacement.Y = projectile.Velocity.X;
    				dodgeDisplacement.Z = 0;

    				// scale
    				if ( projectile.IsA( 'ExplosiveProjectile' ) )
    					dodgeDisplacement *= weapon.aimClass.static.getProjectileDamageRadius( weapon.projectileClass ) / VSize( dodgeDisplacement );
    				else
    					dodgeDisplacement *= DEFAULT_DODGE_DISTANCE / VSize( dodgeDisplacement );
    			}
    			else
    			{
    				//log( "dodging by jetpacking" );
    				// dodging by jetpacking
    				dodgeDisplacement.X = 0;
    				dodgeDisplacement.Y = 0;
    				dodgeDisplacement.Z = 100 + weapon.aimClass.static.getProjectileDamageRadius( weapon.projectileClass );

    				// if you've got an energy pack, use it to boost your jump!
    				if ( ai.pack != None && ai.bUsePackActiveEffect && ai.pack.IsInState( 'Charged' ) &&
    					ClassIsChildOf( ai.pack.class, class'EnergyPack' ) )
    				{
    					ai.level.speechManager.PlayDynamicSpeech( ai, 'UsePackEnergy' );
    					ai.pack.activate();
    				}
    			}

    			// flip dodge direction if projectile is heading towards the side you would move to
    			if ( ((hitLocation - ai.Location) Dot dodgeDisplacement) > 0 )
    			{
    				dodgeDisplacement.X = -dodgeDisplacement.X;
    				dodgeDisplacement.Y = -dodgeDisplacement.Y;
    			}

    			//timeToDodge = VSize( dodgeDisplacement ) / max( VSize(ai.Velocity), RUN_SPEED );
    			//if ( !controller.canPointBeReached( ai.Location, ai.predictedLocation( timeToDodge ) - dodgeDisplacement, ai, lastValidLocation ) )
    			if ( !controller.canPointBeReached( ai.Location, ai.Location + dodgeDisplacement, ai, ignore, lastValidLocation ) )
    			{
    				//log( "Ledge! Dodge displacement reduced from" @ VSize(dodgeDisplacement) @ "to" @ VSize2D(lastValidLocation - ai.Location));
    				dodgeDisplacement.X = 0.9f*(lastValidLocation.X - ai.Location.X);
    				dodgeDisplacement.Y = 0.9f*(lastValidLocation.Y - ai.Location.Y);
    			}

    			if ( ai.dodgingCategory == DODGING_POOR &&
    				// not already dodging
    				(ai.level.TimeSeconds >= controller.dodgeExpirationTime ||
    				IsZero( controller.dodgeDisplacement )) )
    			{
    				controller.dodgeStartTime = ai.level.TimeSeconds + class'BaseAICharacter'.const.DODGING_DELAY_TIME;
    			}

    			if ( weapon.bGenerateMissSpeechEvents )
    				class'Setup'.static.setTarget( shooter, ai, timeToHit );	// for taunt miss speech events
    		}

    		controller.dodgeDisplacement = dodgeDisplacement;
    		controller.dodgeExpirationTime = ai.level.TimeSeconds + DODGE_DURATION;

    		// if you've got a shield pack and can't avoid projectile, use it!
    		if ( ai.pack != None && ai.bUsePackActiveEffect && (timeToHit < shieldPackTime || bHitProtectee) &&
    				ai.pack.IsInState( 'Charged' ) && ClassIsChildOf( ai.pack.class, class'ShieldPack' ) )
    		{
    			ai.level.speechManager.PlayDynamicSpeech( ai, 'UsePackShield' );
    			ai.pack.activate();
    		}

    		setObjectValue( shooter );

    		//ai.health = 1000;
    	}
    }

    //---------------------------------------------------------------------
    // Will 'projectile', fired by 'weapon', damage 'rook'?

    private final function bool willHurt( out Vector hitLocation, out float timeToHit, Rook rook, Weapon weapon, Actor projectile )
    {
    	local Actor thingHit;

    	// will splash damage projectile impact within its damage radius?
    	if ( projectile.IsA( 'ExplosiveProjectile' ) )
    	{
    		thingHit = weapon.aimClass.static.getThingHit( hitLocation, timeToHit, weapon, projectile.Velocity );
    		if ( thingHit == rook ||				// projectile will hit me!
    			(thingHit != None &&
    				weapon.aimClass.static.willHurt( weapon, rook, hitLocation, timeToHit )))	// splash damage will hurt me!
    		{
    			//log( "splash damage:" @ timeToHit );
    			return true;
    		}
    	}

    	// will other projectiles pass close to me? (Problem: there might be an obstacle on path (energy barrier for example) that will prevent impact
    	if ( class'ActionBase'.static.minDistBetweenTrajectories( timeToHit, projectile.Location, projectile.Velocity, rook.Location, rook.averageVelocity() )
    			< ai.CollisionHeight + projectile.CollisionHeight )
    	{
    		hitLocation = projectile.Location + timeToHit * projectile.Velocity;
    		//log( "direct hit:" @ timeToHit );
    		return true;
    	}

    	return false;
    }

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

    function begin()
    {
    	local int i;

    	ai = BaseAICharacter(sensorAction.resource.pawn());
    	enemySensor = AI_EnemySensor(class'AI_Sensor'.static.activateSensor( self, class'AI_EnemySensor', sensorAction.resource, 0, 1000000 ));
    	enemySensor.setParameters();

    	for( i = 0; i < enemySensor.enemies.length; i++ )
    		Rook(enemySensor.enemies[i]).RegisterShotNotification( self );
    }

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

    function cleanup()
    {
    	local int i;

    	//if ( rook().controller != None )
    	//	AI_Controller(rook().controller).bDodging = false;

    	if ( enemySensor != None )
    	{
    		for( i = 0; i < enemySensor.enemies.length; i++ )
    			if ( enemySensor.enemies[i] != None )
    				Rook(enemySensor.enemies[i]).UnRegisterShotNotification( self );

    		enemySensor.deactivateSensor( self );
    		enemySensor = None;
    	}
    }

    //---------------------------------------------------------------------
    // Initialize set the sensor's parameters
    // 'protectee': the Rook to be protected from incoming shots
    // 'shieldPackTime': use shield pack if impact in less than this many seconds

    function setParameters( Rook _protectee, optional float _shieldPackTime )
    {
    	protectee = _protectee;

    	if ( _shieldPackTime != 0 )
    		shieldPackTime = _shieldPackTime;
    }

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

Overview | Package | Class | Source | Class tree | Glossary | UnrealScript Documentation
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.056 - Created with UnCodeX