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

# Tyrion.AI_TakeCover

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

    //=====================================================================
    // AI_TakeCover
    // Tries to find a location that offers protection from fire
    //=====================================================================

    class AI_TakeCover extends AI_MovementAction
    	dependsOn(NS_MoveToLocation)
    	editinlinenew;

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

    const MAX_SEARCH_TICKS = 5;					// number of ticks you try looking for cover
    const SEARCH_RADIUS = 2000.0f;				// max distance of cover location
    const MAX_REACHABLE_NODES_CONSIDERED = 10;
    const SEARCH_DURATION = 10;					// time to look around for if no cover is found (seconds)
    const BASE_NODE_GAP = 125.0f;				// manual keep in synch with PFGlobalConstants::BASE_NODE_GAP

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

    var(Parameters) bool bSearch "look around if no cover is found?";
    var(Parameters) float energyUsage;

    var(InternalParameters) editconst Pawn target;

    var BaseAICharacter ai;
    var ACT_ErrorCodes errorCode;		// errorcode from child action
    var NS_MoveToLocation.TerminalConditions terminalCOnditions;
    var Vector destination;
    var int i;

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

    //---------------------------------------------------------------------
    // Callbacks from Navigation System actions

    function actionSucceededCB( NS_Action child )
    {
    	super.actionSucceededCB( child );
    	if ( child.isA( 'NS_MoveToLocation' ))
    		errorCode = ACT_SUCCESS;
    }

    function actionFailedCB( NS_Action child, ACT_ErrorCodes anErrorCode )
    {
    	super.actionFailedCB( child, anErrorCode );
    	if ( child.isA( 'NS_MoveToLocation' ))
    		errorCode = anErrorCode;
    }

    //---------------------------------------------------------------------
    // Find cover from shots fired by "target"
    // returns false if no cover found
    // "result" contains location of cover (BASE_NODE_GAP above the ground) - is undefined if no cover is found

    static final function bool findCover( out Vector result, Pawn pawn, Pawn enemy, optional bool bDontUsePathNodes )
    {
    	local int i,j;
    	local Vector trialDest, pawnDest;
    	local bool bNewTrial;
    	local array<Vector> nodes;
    	local AI_Controller c;
    	local array<Actor> ignore;

    	c = AI_Controller(pawn.controller);

    	c.getNodesWithinSphere( pawn.Location, SEARCH_RADIUS, nodes );
    	for ( i = 0; i < MAX_REACHABLE_NODES_CONSIDERED; ++i )
    	{
    		bNewTrial = false;
    		while ( j < nodes.length && !bNewTrial && !bDontUsePathNodes )
    		{
    			trialDest = nodes[j];
    			pawnDest = trialDest;
    			pawnDest.Z += pawn.CollisionHeight - BASE_NODE_GAP;	// where pawn will be located when it reaches "trialDest"

    			// todo (possibly): call findPath (copy usage in MovetoLocation) to get nodes around corners
    			if ( c.canPointBeReached( pawn.Location, trialDest, pawn, ignore ) &&
    				!c.isPointEncroachedForMovement( pawn, pawnDest ) )
    			{
    				//log( pawn.name @ "looking at point" @ j @ "in nodes array (i:" @ i $ ")" );
    				bNewTrial = true;
    			}
    			++j;
    		}

    		if ( !bNewTrial )
    		{
    			//log( pawn.name @ "looking at random point (i:" @ i $ ")" );
    			trialDest = c.getRandomLocation( pawn, pawn.Location, SEARCH_RADIUS, 200 );
    			bNewTrial = true;
    		}

    		result = trialDest;
    		trialDest.Z += 2.0f * pawn.CollisionHeight - BASE_NODE_GAP;		// trace to top of head

    		if ( c.offersCover( enemy, trialDest ) )	// does trialDest offer cover?
    		{
    			//log( pawn.name @ "can't be seen by" @ enemy.name @ "(i:" @ i $ ")" );
    			return true;
    		}
    	}

    	return false;
    }

    //---------------------------------------------------------------------
    // Make the AI search for a specified time

    latent function search( BaseAICharacter ai, float time )
    {
    	// wait for movement to stop or animation will be clobbered
    	AI_Controller(ai.controller).stopMove();

    	while ( !isZero( ai.Velocity ) )
    		yield();
    	ai.LoopAnimation( "AI_Searching" );

    	Sleep( time );

    	ai.StopAnimation();
    }

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

    state Running
    {
    Begin:
    	ai = baseAICharacter();

    	if ( ai.logTyrion )
    		log( name @ "(" @ ai.name @ ") started on" @ ai.name );

    	if ( class'Pawn'.static.checkDead( target ) )
    	{
    		if ( ai.logTyrion )
    			log( name @ "(" @ ai.name @ ") Target is dead. Succeeding." );
    		succeed();
    	}

    	terminalConditions.distanceXY = 50;	// gotta hit cover fairly exactly

    	for ( i = 0; i < MAX_SEARCH_TICKS; i++ )
    	{
    		if ( findCover( destination, ai, target, i != 0 ) )
    		{
    			waitForAction( class'NS_MoveToLocation'.static.startAction( AI_Controller(ai.controller),
    						self, destination, None,,,, energyUsage, terminalConditions ));

    			break;
    		}
    		yield();

    		if ( class'Pawn'.static.checkDead( ai ) )
    			fail( ACT_ALL_RESOURCES_DIED );
    	}

    	// look towards enemy (unnecessary if a general "stay faced towards enemy" goal is ever added to AI's)
    	waitForAction( class'NS_Turn'.static.startAction( AI_Controller(ai.controller), self, Rotator(target.Location - ai.Location) ));	// cheat: accessing target's location

    	if ( errorCode != ACT_SUCCESS )
    	{
    		if ( ai.logTyrion )
    			log( name @ "(" @ ai.name @ ") couldn't reach cover" );

    		if ( bSearch )
    		{
    			search( ai, SEARCH_DURATION );
    			succeed();
    		}
    		else
    			fail( ACT_CANT_REACH_DESTINATION );
    	}
    	else
    	{
    		if ( i == MAX_SEARCH_TICKS )
    		{
    			if ( ai.logTyrion )
    				log( name @ "(" @ ai.name @ ") couldn't find cover! Eep!" );

    			if ( bSearch )
    			{
    				search( ai, SEARCH_DURATION );
    				succeed();
    			}
    			else
    				fail( ACT_GENERAL_FAILURE );	// couldn't find cover
    		}
    		else
    		{
    			Sleep(10.0f);			// stay in cover for a while... (todo: is there a test I can use here instead?)

    			if ( ai.logTyrion )
    				log( name @ "(" @ ai.name @ ") succeeded" );
    			succeed();
    		}
    	}

    }

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

    defaultproperties
    {
    	satisfiesGoal = class'AI_TakeCoverGoal'
    }

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