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

Tyrion.NS_Action


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
//=====================================================================
// NS_Action
// An action that can run or be idle, and can create an interruptable
// child
//
// Notes on Usage:
// - every subclassed action should have a 'Running' state in which
//   the state code resides
//=====================================================================

class NS_Action extends ActionBase
	native
	abstract
	threaded;

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

var AI_Controller controller;	// controller this action is attached to
var NS_Action childAction;		// action may have up to one child
var ActionBase parentAction;	// and usually a parent
var ACT_ErrorCodes errorCode;	// set when a child completes
var bool bWakeUpParent;			// when true, parent gets moved to 'Running' state upon child's completion

// nav system tick control
var float tickTime;				// when tickTime < 0, we tick the AI
var float tickTimeOrg;			// last generated tickTime value

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

//---------------------------------------------------------------------
// Constructor

overloaded function construct( AI_Controller c, ActionBase parent )
{
	controller = c;
	parentAction = parent;
	AddRef();			// balanced by Release in deleteRemovedActions (actions are pushed onto that list in removeAction)
	//log( "++" @ name );
	if ( parent != None )
		parent.setChildReference( self );

	// sanity check
	assert( checkForUniqueness( c, self ) );

	GotoState( 'Running' );
}

//---------------------------------------------------------------------
// Check that "action" is the only action of its type running on the
// controller. Returns true if atcion is unique.

function bool checkForUniqueness( AI_Controller c, NS_Action action )
{
	local int i;

	for ( i = 0; i < controller.idleActions.length; i++ )
		if ( controller.idleActions[i] != action && 
			controller.idleActions[i].class == action.class )
		{
			log( "AI ERROR: Duplicate navigation actions in" @ c.Pawn.name $ ":" @ controller.idleActions[i].name @ "(parent:" @ controller.idleActions[i].parentAction.name$ ")," @ action.name @ "(parent:" @ action.parentAction.name $ ")" );
			return false;
		}

	for ( i = 0; i < controller.runningActions.length; i++ )
		if ( controller.runningActions[i] != action && 
			controller.runningActions[i].class == action.class )
		{
			log( "AI ERROR: Duplicate navigation actions in" @ c.Pawn.name $ ":" @ controller.runningActions[i].name @ "(parent:" @ controller.runningActions[i].parentAction.name$ ")," @ action.name @ "(parent:" @ action.parentAction.name $ ")" );
			return false;
		}

	return true;
}

//---------------------------------------------------------------------
// Called by an action when it has successfully executed
// (No state code following this function is executed)

latent function succeed()
{
	//log( "Succeed called on" @ name );

	// call callback in parent
	if (parentAction != None )
		parentAction.actionSucceededCB( self );

	removeAction();
	yield();
}

//---------------------------------------------------------------------
// Called by an action when it has failed
// (called from non-state code)

event instantFail( ACT_ErrorCodes errorCode, optional bool bRemoveGoal )
{
	// call callback in parent
	if (parentAction != None )
		parentAction.actionFailedCB( self, errorCode );

	removeAction();
}

//---------------------------------------------------------------------
// Called by an action when it has failed
// (No state code following this function is executed)k)

latent function fail( ACT_ErrorCodes errorCode )
{
	instantFail( errorCode );
	yield();
}

//---------------------------------------------------------------------
// increments refCount and returns object

function NS_Action myAddRef()
{
	AddRef();
	return self;
}

//---------------------------------------------------------------------
// Interrupt an action
// Stop a running (or idle) action
// Typically called by the controller

function interruptAction()
{
	removeAction();
}

//---------------------------------------------------------------------
// Terminate an action
// Typically called by the controller

function removeAction()
{
	local int i;

	if ( controller.Pawn != None && controller.Pawn.logNavigationSystem )
		log( "NS_removeAction: Removing" @ name @ "from" @ controller.Pawn.name );

	// Remove action from idle list (if it's in there)
	for ( i = 0; i < controller.idleActions.length; i++ )
		if ( controller.idleActions[i] == self )
		{
			controller.idleActions.remove(i, 1);	// removes element - shifts the rest
			break;
		}

	// Remove action from running list (if it's in there)
	for ( i = 0; i < controller.runningActions.length; i++ )
		if ( controller.runningActions[i] == self )
		{
			controller.runningActions.remove(i, 1);	// removes element - shifts the rest
			break;
		}

	// Remove action from parent's childAction variable
	if ( parentAction != None )
	{
		parentAction.removeChildReference( self );
		parentAction = None;
	}

	// Recursively remove all the children
	if ( childAction != None )
	{
		childAction.removeAction();
		childAction = None;
	}

	if ( bCompleted == false )
	{
		controller.removedActions[controller.removedActions.length] = self;	// removedActions.Push(self)
		bCompleted = true;
		
		//log( "--" @ name );
	}

	cleanup();

	bDeleted = true;
}

//---------------------------------------------------------------------
// Run an action
// Typically called by the controller

function runAction()
{
	local int i;

	if ( controller == None )
		log( "AI WARNING:" @ name @ "has no controller" );

	if ( controller == None || class'Pawn'.static.checkDead( controller.Pawn ))
	{
		if ( controller.pawn.logNavigationSystem )
			log( name @ "stopped. Pawn is dead" );

		instantFail( ACT_ALL_RESOURCES_DIED );
		return;
	}

	// Remove action from idle list (if it's in there)
	for ( i = 0; i < controller.idleActions.length; i++ )
		if ( controller.idleActions[i] == self )
		{
			controller.idleActions.remove(i, 1);	// removes element - shifts the rest
			break;
		}

	if ( isIdle() )
	{
		// runningActions.Push(self)
		controller.runningActions[controller.runningActions.length] = self;

		bIdle = false;
	}
}

//---------------------------------------------------------------------
// Pause an action (put it onto idle list)
// Typically called by the controller

function pauseAction()
{
	// Remove action from running list (if it's in there)
	local int i;
	for ( i = 0; i < controller.runningActions.length; i++ )
		if ( controller.runningActions[i] == self )
		{
			controller.runningActions.remove(i, 1);	// removes element - shifts the rest

			// idleActions.Push(self)
			controller.idleActions[controller.idleActions.length] = self;
			break;
		}

	bIdle = true;
}

//---------------------------------------------------------------------
// Add action to childAction list

function setChildReference( NS_Action child )
{
	childAction = child;
}

//---------------------------------------------------------------------
// Remove action from childAction list

function removeChildReference( NS_Action child )
{
	if ( child == childAction )
		childAction = None;
}

//---------------------------------------------------------------------
// Return the Cersei child?

function NS_Action getChildReference()
{
	return childAction;
}

//---------------------------------------------------------------------
// Message notification functions (callbacks) for child actions
// Note: When writing action-specific callbacks, call super.<callback> first

function actionSucceededCB( NS_Action child )
{
	// the child succeeded
	errorCode = ACT_ErrorCodes.ACT_SUCCESS;

	super.actionSucceededCB( child );
}

function actionFailedCB( NS_Action child, ACT_ErrorCodes _errorCode )
{
	// the child failed
	errorCode = _errorCode;
	controller.lastErrorCode = _errorCode;	// for debug only

	super.actionFailedCB( child, _errorCode );
}

//=====================================================================
// States

state Running
{
Begin:
}

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

defaultproperties
{
	bWakeUpParent = false
}

Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames
Class file time: ne 5.9.2004 15:54:28.000 - Creation time: st 23.5.2018 00:10:46.139 - Created with UnCodeX