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

Tyrion.AI_CombatMovement


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
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
//=====================================================================
// AI_CombatMovement
// Does AI's positioning movement while it's attacking something
//
// never succeeds; fails when pursue fails
//=====================================================================

class AI_CombatMovement extends AI_MovementAction implements IBooleanGoalCondition
	editinlinenew;

import enum CombatRangeCategories from BaseAICharacter;
import enum CombatMovementCategories from BaseAICharacter;
import enum SpeedPackUseCategories from BaseAICharacter;

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

const PROXIMITY_HYSTERESIS_STAY_AT_RANGE = 0.5f;	// percentage of "ideal firing range distance" proximity can fall to before AI backs up
const PROXIMITY_HYSTERESIS_DRAW_OUT = 0.9f;
const DRAW_OUT_DISTANCE = 2000.0f;					// how far to draw out the opponent
const FLANKING_UPDATE_PERIOD = 2.0f;				// look for flanking positions every this many seconds
const COVER_UPDATE_PERIOD = 4.0f;					// look for cover positions every this many seconds
const SIDESTEP_RADIUS = 800.0f;						// how far AI's side step

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

var(Parameters) float proximity;
var(Parameters) float energyUsage;
var(Parameters) float sideStepInterval "how frequently AI's side steps when in CM_SIDE_STEP";

var(InternalParameters) editconst Pawn target;
var(InternalParameters) editconst IFollowFunction followFunction;

var BaseAICharacter ai;			// pointer to the AI this action is running on
var CombatRangeCategories combatRange;
var CombatMovementCategories combatMovement;
var float proximityHysteresis;
var AI_Goal followGoal;
var AI_Goal moveGoal;
var Vector movePoint;			// good point to flank from or to back up to
var bool bCover;				// does "movePoint" offer cover? (bot used)
var float lastFlankingUpdateTime;	// last time AI checked for flanking positions
var float lastCoverUpdateTime;		// last time AI checked for cover positions
var float lastMovementTime;			// last time AI adjusted position
var ACT_Errorcodes errorCode;
var AI_TargetMemorySensor targetMemorySensor;
var Character.GroundMovementLevels moveSpeed;

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

//---------------------------------------------------------------------
// callbacks from pursueGoal

function goalNotAchievedCB( AI_Goal goal, AI_Action child, ACT_ErrorCodes anErrorCode ) 
{
	super.goalNotAchievedCB( goal, child, anErrorCode );

	errorCode = anErrorCode;
}

//---------------------------------------------------------------------
// function to interrupt follow if attacker gets too close

static function bool goalTest( AI_Goal goal )
{
	local AI_CombatMovement cm;
	local Pawn pawn;

	cm = AI_CombatMovement(goal.parentAction);
	pawn = goal.resource.pawn();
	cm.movePoint = pawn.Location;
	cm.bCover = false;

	// interrupt action when target dies
	if ( class'Pawn'.static.checkDead( pawn ) || class'Pawn'.static.checkDead( cm.target ) )
		return true;

	// decide whether to check for a flanking spot
	if ( cm.combatRange == CR_FLANKING &&
		pawn.level.timeSeconds - cm.lastFlankingUpdateTime >= FLANKING_UPDATE_PERIOD &&	// don't look for flanking spots too often
		VDist( pawn.Location, cm.target.Location ) < cm.proximityFunction() + 500.0f )	// don't try to flank if far away from target
	{
		if ( cm.ai.logTyrion )
			log( cm.name $ ":" @ pawn.name @ "looking for flanking point!" );

		cm.lastFlankingUpdateTime = pawn.level.timeSeconds;
		cm.movePoint = cm.getFlankPoint();
		cm.moveSpeed = GM_SPRINT;
	}

	// decide whether to check for a backup spot
	if ( cm.combatRange >= CR_STAY_AT_RANGE &&
		cm.movePoint == pawn.Location &&
		VDist( cm.target.Location, pawn.Location ) < cm.proximityHysteresis * cm.proximityFunction() )
	{
		if ( cm.ai.logTyrion )
			log( cm.name $ ":" @ pawn.name @ "looking for back up point!" );

		cm.movePoint = cm.getBackupPoint();
		cm.moveSpeed = GM_RUN;
	}

	// look for cover
	if ( cm.combatMovement == CM_SEEK_COVER &&
		cm.movePoint == pawn.Location &&
		pawn.level.timeSeconds - cm.lastMovementTime >= COVER_UPDATE_PERIOD &&	// don't seek cover if you've just moved
		pawn.level.timeSeconds - cm.lastCoverUpdateTime >= COVER_UPDATE_PERIOD )// don't look for cover too often
	{
		if ( cm.ai.logTyrion )
			log( cm.name $ ":" @ pawn.name @ "looking for cover!" );

		cm.lastCoverUpdateTime = pawn.level.timeSeconds;
		cm.movePoint = cm.getCoverPoint();
		cm.moveSpeed = GM_RUN;
	}

	// look for place to side-step to
	if ( (cm.combatMovement == CM_SIDE_STEP || cm.combatMovement == CM_SEEK_COVER) &&
		cm.movePoint == pawn.Location &&
		IsNearlyZero( pawn.Velocity ) &&
		pawn.level.timeSeconds - cm.lastMovementTime >= cm.sideStepInterval &&	// don't side step if you've just moved
		cm.proximityFunction() > SIDESTEP_RADIUS )		// don't sidestep if you need to be close to your target to attack
	{
		if ( cm.ai.logTyrion )
			log( cm.name $ ":" @ pawn.name @ "doing a side step!" );

		cm.movePoint = cm.getSidestepPoint();
		cm.moveSpeed = GM_RUN;
	}

	return cm.movePoint != pawn.Location;
}

//---------------------------------------------------------------------
// if too close to target, get point to back up to

private final function Vector getBackupPoint()
{
	local Vector targetVector;				// vector from AI to target
	local float targetVectorSize;			// length
	local float backupDistance;				// how far AI needs to backup by
	local Vector backupPoint;				// location to back up to

	targetVector = target.Location - ai.Location;
	targetVectorSize = VSize( targetVector );
	if ( combatRange == CR_DRAW_OUT )
		backupDistance = DRAW_OUT_DISTANCE;
	else
		backupDistance = proximityFunction() - targetVectorSize;

	targetVector *= backupDistance / targetVectorSize;

	backupPoint = AI_Controller(ai.controller).getRandomLocation( ai, ai.Location - targetVector, backupDistance, 0 );

	if ( bValidLocation( backupPoint ) )
		return backupPoint;
	else
		return ai.Location;
}

//---------------------------------------------------------------------
// get point on flank of target

private final function Vector getFlankPoint()
{
	local Vector targetVector;
	local Vector flankPoint;
	local float prox;

	prox = proximityFunction();
	targetVector = target.Location - ai.Location;
	targetVector *= prox / VSize( targetVector );
	targetVector = rotateZ( targetVector, PI/2 );

	flankPoint = AI_Controller(ai.controller).getRandomLocation( ai, target.Location - targetVector, FMin(prox, 500.0f), 0 );

	if ( bValidLocation( flankPoint ) )
		return flankPoint;
	else
		return ai.Location;
}

//---------------------------------------------------------------------
// get nearby point that offers cover

private final function Vector getCoverPoint()
{
	local Vector coverPoint;

	if ( class'AI_TakeCover'.static.findCover( coverPoint, resource.pawn(), target ) )
	{
		bCover = true;
		return coverPoint;
	}
	else
		return ai.location;		// no cover found
}

//---------------------------------------------------------------------
// get nearby point to side step to

private final function Vector getSidestepPoint()
{
	local Vector sideStepPoint;

	sidestepPoint = AI_Controller(ai.controller).getRandomLocation( ai, ai.Location, SIDESTEP_RADIUS, 100 );

	if ( bValidLocation( sidestepPoint ) )
		return sidestepPoint;
	else
		return ai.Location;
}

//---------------------------------------------------------------------
// Is "loc" a valid location to move to?

private final function bool bValidLocation( Vector loc )
{
	local Actor hitActor;

	if ( loc == ai.Location || (followFunction != None && !followFunction.validDestination( loc )))
		return false;

	// check that backup point has LOS to target
	hitActor = ai.AITrace( target.Location, loc, Vect(1, 1, 1) );
	if ( hitActor == None || hitActor == target )
	{
		return true;
	}
	else
	{
		//log( name @ "candidate position occluded" );
		return false;
	}
}

//---------------------------------------------------------------------
// what's your preferred weapon range?

private final function float proximityFunction()
{
	if ( followFunction != None )
		return followFunction.proximityFunction();
	else
		return proximity;
}

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

function cleanup()
{
	super.cleanup();

	followFunction = None;

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

	if ( followGoal != None )
	{
		followGoal.Release();
		followGoal = None;
	}
	if ( moveGoal != None )
	{
		moveGoal.Release();
		moveGoal = None;
	}
}

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

state Running
{
Begin:
	ai = baseAICharacter();

	if ( ai.logTyrion )
		log( name @ "started." @ ai.name @ "is maneuvering to attack" @ target.name );

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

	// set up purely for Pursue: memory function won't work on this sensor if it's always started from scratch
	targetMemorySensor = AI_TargetMemorySensor( class'AI_Sensor'.static.activateSensor( self, class'AI_TargetMemorySensor', characterResource() ) );
	targetMemorySensor.setParameters( target, ai.visionMemory );

	// if you got a speed pack, use it on first approach!
	if ( ai.pack != None && ai.bUsePackActiveEffect && ai.speedPackUseCategory == SP_ON_FIRST_ATTACK &&
		ai.pack.IsInState( 'Charged' ) && ClassIsChildOf( ai.pack.class, class'SpeedPack' ) )
	{
		ai.level.speechManager.PlayDynamicSpeech( ai, 'UsePackSpeed' );
		ai.pack.activate();
	}

	combatRange = ai.combatRangeCategory;
	combatMovement = ai.combatMovementCategory;

	if ( combatRange == CR_DRAW_OUT )
		proximityHysteresis = PROXIMITY_HYSTERESIS_DRAW_OUT;
	else
		proximityHysteresis = PROXIMITY_HYSTERESIS_STAY_AT_RANGE;

	// keep pursuing the target unless goalTest interrupts to do a special move (backing up, flanking, look for cover)
	while ( class'Pawn'.static.checkAlive( ai ) && class'Pawn'.static.checkAlive( target ) && (followGoal == None || !followGoal.wasNotAchieved() ))
	{
		if ( followGoal != None )
		{
			followGoal.unPostGoal( self );	// "interruptGoalIf" unposts goal when interrupted but not when goal fails
			followGoal.Release();			// must be done here as well as cleanup because followGoal could be overwritten
			followGoal = None;
		}

		//log( name $ ":" @ ai.name @ "pursuing!" );
		followGoal = (new class'AI_PursueGoal'( movementResource(), achievingGoal.priority, target, proximity, followFunction,, energyUsage )).postGoal( self ).myAddRef();
		interruptGoalIf( followGoal, class'AI_CombatMovement' );

		if ( class'Pawn'.static.checkAlive( target ) && !followGoal.wasNotAchieved() )	// only continue if pursue didn't fail
		{
			if ( movePoint != ai.Location )
			{
				moveGoal = (new class'AI_MoveToGoal'( movementResource(), achievingGoal.priority, movePoint, ,,moveSpeed, energyUsage )).postGoal( self ).myAddRef();
				waitForGoal( moveGoal );
				lastMovementTime = ai.level.timeSeconds;

				// unpost if goal failed (unnecessary if there was a flag on the goal to do this...)
				moveGoal.unPostGoal( self );
				moveGoal.Release();
				moveGoal = None;

				if ( class'Pawn'.static.checkAlive( ai ) && class'Pawn'.static.checkAlive( target ) )
				{
					waitForAction( class'NS_Turn'.static.startAction( AI_Controller(ai.controller), self,
						Rotator(target.Location - ai.Location) ));	// cheat: accessing target's location
				}
			}
			else
				yield();
		}
	}

	if ( ai.logTyrion )
		log( name @ "(" @ ai.name @ ") stopped with errorCode" @ errorCode );

	if ( class'Pawn'.static.checkDead( target ) )
		succeed();
	else
		fail( errorCode );
}

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

defaultproperties
{
	satisfiesGoal = class'AI_CombatMovementGoal'
}

Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames
Class file time: čt 7.10.2004 10:30:38.000 - Creation time: st 23.5.2018 00:10:40.999 - Created with UnCodeX