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

# Tyrion.AI_GunnerFireAt

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
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308

    //=====================================================================
    // AI_GunnerFireAt
    //
    // FireAt action for vehicle mounted turrets and turret/vehicle weapons
    //=====================================================================

    class AI_GunnerFireAt extends AI_GunnerAction
    	editinlinenew;

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

    // maximum allowable angular deviation between where you shooting and where your target is
    const MAX_DEVIATION_POD = 25;			// ...for pods
    const MAX_DEVIATION_POD_ATTACKING = 40;	// ...for pods doing attack runs
    const MAX_DEVIATION_ASSAULTSHIP = 60;	// ...for assault ships
    const MAX_DEVIATION_NORMAL = 10;		// ...for everything else (i.e. turrets of all shapes and sizes)

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

    var(Parameters) editinline Name targetName "Label of target (any Pawn)";
    var(Parameters) bool bGiveUpIfTargetLost "Terminate action if target lost?";
    var(Parameters) bool bFireWithoutLOS "Fire even if you have no LOS on the target";

    var(InternalParameters) editconst Pawn target;

    var Weapon weapon;
    var Vector aimLocation;
    var Vector inaccurateAimLocation;	// where the projectile will actually go
    var Rotator aimRotation;
    var int yawDiff;
    var int pitchDiff;
    var int rollDiff;
    var int maxDeviation;
    var Vector hitLocation;
    var float timeToHit;
    var Actor obstacle;			// possible obstacle in my shot line
    var Rook rookObstacle;
    var AI_TargetSensor targetSensor;
    var Rook mount;
    var IFiringMotor firingMotor;
    var Rotator viewRotation;	// where the weapon is pointing
    var bool bAssaultShip;		// true if running on the assault ship main gun
    var bool bPod;				// true if running on the pod gun
    var bool bChainGun;			// true if firing a chaingun weapon
    var class<ExplosiveProjectile> projectileClass;

    var String debugString;		// indicates what caused the AI to not fire

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

    //---------------------------------------------------------------------
    // sensor callback (from TargetSensor)

    function OnSensorMessage( AI_Sensor sensor, AI_SensorData value, Object userData )
    {
    	if ( value.objectData == None )
    	{
    		if ( rook().logTyrion )
    			log( name @ "target lost" );

    		localRook().firingMotor().releaseFire();

    		if ( class'Pawn'.static.checkAlive( target ) )
    		{
    			if ( bGiveUpIfTargetLost )
    				instantFail( ACT_LOST_TARGET );
    			else
    				pauseAction();
    		}
    	}
    	else
    	{
    		if ( rook().logTyrion )
    			log( name @ "target acquired" );

    		runAction();
    	}
    }

    //---------------------------------------------------------------------
    // compute inaccuracy that will be added to shot

    private final function Rotator computeAIspread()
    {
    	local Rotator r;
    	local float deviation;

    	deviation = rook().shotAngularDeviation * ( 65536 / 360 );

    	r.Yaw = FRand() * 2.f * deviation - deviation;
    	r.Pitch = FRand() * 2.f * deviation - deviation;

    	return r;
    }

    //---------------------------------------------------------------------
    // return pertinent information about an action for debugging

    function string actionDebuggingString()
    {
    	if ( target == None )
    		return String(name) @ "NOTARGET" @ debugString;
    	else
    		return String(name) @ target.Name @ debugString;
    }

    //---------------------------------------------------------------------

    function cleanup()
    {
    	super.cleanup();

    	localRook().firingMotor().releaseFire();

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

    //=====================================================================
    // State code

    state Running
    {
    Begin:
    	mount = localRook();
    	firingMotor = mount.firingMotor();

    	if ( target == None && targetName == '' )
    	{
    		log( "AI WARNING:" @ name @ "has no target" );
    		fail( ACT_INVALID_PARAMETERS, true );
    	}

    	if ( target == None )
    		target = Pawn(mount.findByLabel( class'Pawn', targetName, true ));

    	if ( rook().logTyrion )
    		log( name @ "started." @ mount.name @ "is firing at" @ target.name );

    	if ( target == None )
    	{
    		log( "AI WARNING:" @ name @ "can't find specified pawn" @ targetName );
    		fail( ACT_INVALID_PARAMETERS, true );
    	}

    	if ( !bFireWithoutLOS )
    	{
    		targetSensor = AI_TargetSensor( class'AI_Sensor'.static.activateSensor( self, class'AI_TargetSensor', resource ));
    		targetSensor.setParameters( target );
    	}

    	// play that funky music, white boy!
    	if ( rook().level.MusicMgr != None )
    		rook().level.MusicMgr.TriggerDynamicMusic( MT_Combat );

    	mount.lastShotFiredTime = mount.level.timeSeconds;	// give a new FireAt a chance to shoot at target
    	mount.PlayEffect( "DetectedEnemy" );

    	weapon = firingMotor.getWeapon();
    	bAssaultShip = mount.IsA( 'AssaultShip' );
    	bPod = mount.IsA( 'Pod' );
    	bChainGun = weapon.IsA( 'ChainGun' );
    	//projectileClass = class<ExplosiveProjectile>(weapon.projectileClass); //(hurtCheck disabled for now)

    	if ( mount.reactionDelay > 0 )
    	{
    		if ( rook().logTyrion )
    			log( name $ ":" @ mount.name @ "detected enemy; waiting" @ mount.reactionDelay @ "before firing" );
    		Sleep( mount.reactionDelay );
    	}

    	while( class'Pawn'.static.checkAlive( rook() ) && class'Pawn'.static.checkAlive( target ) )	// (in range checks are done in GuardSensor)
    	{
    		aimLocation = weapon.aimClass.static.getAimLocation( weapon, target );
    		aimRotation = Rotator(aimLocation - mount.Location);

    		firingMotor.setViewRotation( aimRotation );		// aim...

    		if ( bPod || bAssaultShip )
    			viewRotation = rook().Rotation;				// use move rotation
    		else
    			viewRotation = firingMotor.getViewRotation();

    		yawDiff = ( aimRotation.Yaw - viewRotation.Yaw ) & 65535;
    		if ( yawDiff > 32768 )
    			yawDiff = 65536 - yawDiff;

    		pitchDiff = ( aimRotation.Pitch - viewRotation.Pitch ) & 65535;
    		if ( pitchDiff > 32768 )
    			pitchDiff = 65536 - pitchDiff;

    		//rollDiff = ( aimRotation.Roll - viewRotation.Roll ) & 65535;
    		//if ( rollDiff > 32768 )
    		//	rollDiff = 65536 - rollDiff;

    		if ( bAssaultShip )
    		{
    			maxDeviation = MAX_DEVIATION_ASSAULTSHIP*65536/360;
    			pitchDiff = 0;
    		}
    		else if ( bPod )
    		{
    			if ( AI_Controller(mount.controller).aircraftAttacking )
    				maxDeviation = MAX_DEVIATION_POD_ATTACKING*65536/360;
    			else
    				maxDeviation = MAX_DEVIATION_POD*65536/360;
    		}
    		else
    			maxDeviation = MAX_DEVIATION_NORMAL*65536/360;

    		debugString = "not pointing at target (" $ yawDiff * 360 / 65536 $ "," $ pitchDiff * 360 / 65536 $ "," $ rollDiff * 360 / 65536 $ "," $ maxDeviation * 360 / 65536 $ ")";

    		if ( yawDiff < maxDeviation && pitchDiff < maxDeviation  )	// AI is pointing weapon within maxDeviation degrees of target and ready to fire
    		{
    			debugString = "weapon not ready";

    			if ( weapon.fireRatePassed() || bChainGun )		// releaseFire is *not* called when this is false: prevents chainguns from going into idle state
    			{
    				// add inaccuracy to the aimRotation;
    				weapon.AIspread = computeAIspread();
    				weapon.AIAccelerationModifier = 0.0;
    				inaccurateAimLocation = VSize(aimLocation - mount.Location) * Vector(aimRotation + weapon.AIspread) + mount.Location;
    				obstacle = weapon.aimClass.static.obstacleInPath( hitLocation, timeToHit, target, weapon, inaccurateAimLocation );

    				// if target's center is occluded, check if I can hit the head
    				// (simple aimLocation adjustment won't work for arced projectiles so this is skipped for them)
    				if ( obstacle != None && !bFireWithoutLOS && weapon.aimClass != class'AimArcWeapons' )
    				{
    					//log( "Adjusting" @ rook().name @ "aim upward to hit" @ target.name $ "'s head" );
    					inaccurateAimLocation.Z += 0.9f * target.CollisionHeight;
    					firingMotor.setViewRotation( Rotator( inaccurateAimLocation - mount.Location ));
    					obstacle = weapon.aimClass.static.obstacleInPath( hitLocation, timeToHit, target, weapon, inaccurateAimLocation );
    				}

    				// if the obstacle is an enemy, shoot anyway!
    				if ( obstacle != None )
    				{
    					rookObstacle = Rook(obstacle);
    					if ( rookObstacle != None && !rook().isFriendly( rookObstacle ) && rookObstacle.Health > 0 )
    						obstacle = None;
    				}

    				// debug display
    				if ( mount.bShowTyrionWeaponDebug )
    				{
    					if ( bFireWithoutLOS )
    						debugString = "FIREWITHOUTLOS";
    					else if (obstacle == None )
    						debugString = "obst:None";
    					else
    						debugString = "obst:" $ obstacle;

    					if ( projectileClass != None &&
    						VDistSquared( target.Location, mount.Location ) <= projectileClass.default.radiusDamageSize * projectileClass.default.radiusDamageSize )
    						debugString = "TARGET TOO CLOSE";
    				}

    				if ( (obstacle == None || bFireWithoutLOS) &&				// projectile has clear path
    					(projectileClass == None ||								// will damage itself?
    					VDistSquared( target.Location, mount.Location ) > projectileClass.default.radiusDamageSize * projectileClass.default.radiusDamageSize ))
    				{
    					if ( bChainGun )
    					{
    						if ( Chaingun(weapon).overheated )
    							firingMotor.releaseFire();
    						else
    						{
    							mount.lastShotFiredTime = mount.level.timeSeconds;
    							firingMotor.fire(false);	// ... and shoot!
    						}
    					}
    					else
    					{
    						mount.lastShotFiredTime = mount.level.timeSeconds;
    						firingMotor.fire(true);			// ... and shoot!
    					}
    				}
    				else
    				{
    					firingMotor.releaseFire();
    				}
    			}
    		}
    		else
    		{
    			firingMotor.releaseFire();
    		}

    		yield();
    	}

    	if ( rook().logTyrion )
    		log( name @ "(" @ mount.name @ ") stopped" );

    	succeed();
    }

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

    defaultproperties
    {
    	satisfiesGoal = class'AI_GunnerFireAtGoal'

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:00.000 - Creation time: st 23.5.2018 00:10:41.313 - Created with UnCodeX