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

# Tyrion.AI_Pursue

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

    //=====================================================================
    // AI_Pursue
    // Move towards a target, look for it if you lose sight
    //=====================================================================

    class AI_Pursue extends AI_MovementAction
    	editinlinenew;

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

    const SEARCH_TIME = 6.0f;			// max time spent looking for lost target

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

    var(Parameters) editinline Name targetName "A pawn to follow";
    var(Parameters) editinline float proximity "How close to get while following";
    var(Parameters) float energyUsage;
    var(Parameters) float terminalVelocity;
    var(Parameters) float terminalHeight;

    // todo: add parameter for walk in a slinky fashion (for combat movement category)

    var(InternalParameters) editconst Pawn target;
    var(InternalParameters) editconst IFollowFunction followFunction;
    var(InternalParameters) editconst int positionIndex;	// index of this pawn in a formation (starts at 0)

    var BaseAICharacter ai;
    var ACT_ErrorCodes errorCode;		// errorcode from child action
    var AI_TargetMemorySensor targetMemorySensor;
    var NS_Action follow;
    var AI_Goal investigateGoal;

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

    //---------------------------------------------------------------------
    // sensor callback

    function OnSensorMessage( AI_Sensor sensor, AI_SensorData value, Object userData )
    {
    	if ( ai.logTyrion )
    		log( name @ "receiving message from" @ sensor.name @ value.objectData );
    	runAction();
    }

    //---------------------------------------------------------------------
    // callbacks from investigateGoal

    function goalAchievedCB( AI_Goal goal, AI_Action child )
    {
    	if ( ai.logTyrion )
    		log( name @ "receiving goal achieved message from" @ goal.name );
    	super.goalAchievedCB( goal, child );
    	runAction();
    }

    function goalNotAchievedCB( AI_Goal goal, AI_Action child, ACT_ErrorCodes errorCode )
    {
    	if ( ai.logTyrion )
    		log( name @ "receiving goal failed message from" @ goal.name );
    	super.goalNotAchievedCB( goal, child, errorCode );
    	runAction();
    }

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

    function actionSucceededCB( NS_Action child )
    {
    	if ( ai.logTyrion )
    		log( name @ "receiving action succeeded message from" @ child.name );
    	super.actionSucceededCB( child );
    	errorCode = ACT_SUCCESS;
    	runAction();
    }

    function actionFailedCB( NS_Action child, ACT_ErrorCodes anErrorCode )
    {
    	if ( ai.logTyrion )
    		log( name @ "receiving action failed message from" @ child.name );
    	super.actionFailedCB( child, anErrorCode );
    	errorCode = anErrorCode;
    	runAction();
    }

    //---------------------------------------------------------------------
    // how close do you want to get?

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

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

    function string actionDebuggingString()
    {
    	if ( target == None )
    		return String(name) @ "None," $ proximityFunction();
    	else
    		return String(name) @ target.label $ "," $ proximityFunction();
    }

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

    function cleanup()
    {
    	super.cleanup();

    	followFunction = None;

    	if ( class'Pawn'.static.checkAlive( resource.pawn() ) )
    		AI_Controller(resource.pawn().controller).stopMove();

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

    	if ( investigateGoal != None )
    	{
    		investigateGoal.Release();
    		investigateGoal = None;
    	}

    	if ( follow != None )
    	{
    		follow.Release();
    		follow = None;
    	}
    }

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

    state Running
    {
    Begin:
    	ai = BaseAICharacter(resource.pawn());

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

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

    	if ( ai.logTyrion )
    		log( name @ "started." @ ai.name @ "is pursuing" @ target.name @ achievingGoal.bTryOnlyOnce );

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

    	targetMemorySensor = AI_TargetMemorySensor( class'AI_Sensor'.static.activateSensor( self, class'AI_TargetMemorySensor', characterResource() ) );
    	targetMemorySensor.setParameters( target, ai.visionMemory );

    	while ( targetMemorySensor.queryObjectValue() != None )
    	{
    		if ( ai.logTyrion )
    			log( name @ "starting to follow" );

    		if ( follow != None )
    		{
    			follow.Release();
    			follow = None;
    		}

    		follow = class'NS_Follow'.static.startAction( AI_Controller(ai.controller), self, target, proximity, followFunction, positionIndex, energyUsage, terminalVelocity, terminalHeight ).myAddRef();
    		pause();
    		if ( class'Pawn'.static.checkDead( ai ) || class'Pawn'.static.checkDead( target ) )
    			goto 'exit';

    		// if action is woken up it's because target was lost or follow failed
    		if ( follow.hasCompleted() )
    		{
    			if (  errorCode != ACT_SUCCESS )
    			{
    				if ( ai.logTyrion )
    					log( name @ "stopped because follow failed" );
    				fail( errorCode );
    			}
    		}
    		else
    		{
    			follow.interruptAction();
    		}

    		if ( followFunction == None || followFunction.validDestination( targetMemorySensor.lastPlaceSeen ))
    		{
    			if ( ai.logTyrion )
    				log( name @ "going to investigate at target's last known location" );

    			//ai.level.speechManager.PlayDynamicSpeech( ai, 'CombatLose', None, None, "You" );

    			// target must have been lost
    			investigateGoal = (new class'AI_InvestigateGoal'( movementResource(), achievingGoal.priority, targetMemorySensor.lastPlaceSeen, SEARCH_TIME, target )).postGoal( self ).myAddRef();
    			pause();
    			if ( class'Pawn'.static.checkDead( ai ) || class'Pawn'.static.checkDead( target ) )
    				goto 'exit';

    			if ( !investigateGoal.wasAchieved() )
    			{
    				investigateGoal.unPostGoal( self );
    				investigateGoal.Release();			// must be done here as well as cleanup because investigateGoal could be overwritten
    				investigateGoal = None;
    			}

    			//if ( targetMemorySensor.queryObjectValue() != None )
    			//	ai.level.speechManager.PlayDynamicSpeech( ai, 'CombatLose', None, None, "Find" );
    		}
    	}

    	//ai.level.speechManager.PlayDynamicSpeech( ai, 'CombatLose', None, None, "GiveUp" );

    exit:
    	if ( ai.logTyrion )
    		log( name @ "stopped because target can't be found" );
    	fail( ACT_LOST_TARGET );
    }

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

    defaultproperties
    {
    	satisfiesGoal = class'AI_PursueGoal'
    }

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.548 - Created with UnCodeX