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

# Tyrion.AI_EnemySensor

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

    //=====================================================================
    // AI_EnemySensor
    // Keeps tracks of this pawn's enemies
    // Value (int): number of enemies visible
    //=====================================================================

    class AI_EnemySensor extends AI_Sensor implements IVisionNotification, Engine.IInterestedTeamChanged;

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

    const MAX_GREETING_ANIMATION_DISTANCE = 1000.0f;
    const MAX_ALLYKILLED_DIST = 3000.0f;

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

    var array<Pawn> enemies;						// the enemies this AI is currently worrying about
    var Rook lastSeen;								// last rook spotted (cleared if a pawn is lost)
    var Rook lastLost;								// last rook lost (cleared if a pawn is spotted)
    var Rook player;								// points to the player character if s/he is an enemy and visible (None otherwise)
    												// If several players are present, the first one spotted is stored until it dies/vanishes; then it can be replaced

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

    //---------------------------------------------------------------------
    // convenience function to set all the variables (and the sensor value itself)

    function setValue( Rook aLastSeen, Rook aLastLost )
    {
    	lastSeen = aLastSeen;
    	lastLost = aLastLost;
    	setIntegerValue( enemies.length );
    }

    //---------------------------------------------------------------------
    // Initialize set the sensor's parameters
    // (only used to ensure that a message is sent when sensor is first started up)

    function setParameters()
    {
    	setValue( None, None );
    }

    //---------------------------------------------------------------------
    // vision callbacks

    function OnViewerSawPawn(Pawn Viewer, Pawn Seen)
    {
    	local int i;
    	local Rook rookViewer, rookSeen;

    	//log( "OnViewerSawPawn called on" @ sensorAction.resource.localRook().name @ "for" @ Seen.name );

    	rookViewer = Rook(Viewer);
    	rookSeen = Rook(Seen);

    	// debug only
    	if ( Viewer == None || AI_Controller(Viewer.Controller) == None )
    	{
    		if ( Viewer == None )
    			log( "AI WARNING: Viewer is None in" @ name @ "(" @ sensorAction.resource.name @ sensorAction.resource.localRook() @ ")" );
    		else
    			log( "AI WARNING:" @ Viewer.name @ "has a" @ Viewer.Controller @ "instead of an AI_Controller" );
    		return;
    	}

    	if ( !rookViewer.isFriendly( rookSeen ) )
    	{
    		for ( i = 0; i < enemies.length; i++ )
    			if ( enemies[i] == Seen )
    				return;

    		if ( rookViewer.getAlertnessLevel() == ALERTNESS_Neutral &&
    			rookViewer.IsA( 'Character' ) &&
    			rookViewer.vision.isLocallyVisible( Seen ))
    		{
    			Character(rookViewer).playAnimation( "AI_spotEnemy" );	// todo: move this to a separate action so I can turn and stuff
    		}

    		if ( player == None && rookSeen.Controller != None && rookSeen.Controller.bIsPlayer )
    			player = rookSeen;

    		AI_Controller(Viewer.controller).setAlertnessLevel( ALERTNESS_Alert );

    		enemies[enemies.length] = Seen;
    		//log( "==> Adding" @ Seen.name @ "to enemySensor of" @ sensorAction.resource.localRook().name @ "(length:" @ enemies.length $ ")" );
    		setValue( rookSeen, None );
    	}
    	else if ( rookViewer.getAlertnessLevel() == ALERTNESS_Neutral &&
    				VDistSquared( Viewer.Location, Seen.Location ) < MAX_GREETING_ANIMATION_DISTANCE * MAX_GREETING_ANIMATION_DISTANCE &&
    				rookViewer.IsA( 'Character' ) &&
    				rookViewer.vision.isLocallyVisible( Seen ))
    	{
    		if ( Viewer.logTyrion )
    			log( "Animation:" @ Viewer.name @ "spotted friendly" @ Seen.name );
    		Character(rookViewer).playAnimation( "AI_spotFriendly" );
    	}
    }

    function OnViewerLostPawn(Pawn Viewer, Pawn Seen)
    {
    	local int i;

    	if ( player == Seen )
    		player = None;

    	// for now: forget about opponents that can't be seen
    	for( i = 0; i < enemies.length; i++ )
    		if ( enemies[i] == Seen )
    		{
    			enemies.remove( i, 1 );	// removes element - shifts the rest
    			//log( "<== Removing" @ Seen.name @ "from enemySensor of" @ sensorAction.resource.localRook().name @ "(length:" @ enemies.length $ ")" );
    			setValue( None, Rook(Seen) );
    			break;
    		}

    	// send AllyKilled message?
    	if ( class'Pawn'.static.checkDead( Seen ) &&
    		VDistSquared( Viewer.Location, Seen.Location ) <= MAX_ALLYKILLED_DIST * MAX_ALLYKILLED_DIST &&
    		Rook(Viewer).isFriendly( Rook(Seen) ) &&
    		!Viewer.IsA( 'AICivilian' ))
    	{
    		Viewer.level.speechManager.PlayDynamicSpeech( Viewer, 'AllyKilled', None, Vehicle(Seen) );
    	}
    }

    //---------------------------------------------------------------------
    // called whenever a rook changes team

    function onTeamChanged( Pawn changedPawn )
    {
    	local Rook changedRook;
    	local Rook me;

    	me = sensorAction.resource.localRook();
    	changedRook = Rook(changedPawn);

    	//log( "onTeamChanged called on" @ me.name @ "because" @ changedPawn.name @ "changed team" );

    	if ( changedRook == me )
    	{
    		initEnemiesList( me );
    		setValue( None, None );		// send sensor message
    	}
    	else if ( me.isFriendly( changedRook ) )
    		OnViewerLostPawn( me, changedRook );
    	else if ( me.vision != None && me.vision.isVisible( changedRook ) )
    		OnViewerSawPawn( me, changedRook );
    }

    //---------------------------------------------------------------------
    // initialize enemies list

    private function initEnemiesList( Rook me )
    {
    	local int i;
    	local Rook squadMember;

    	enemies.length = 0;		// clear enemies list
    	player = None;

    	if ( me.squad() == None )
    		initEnemiesListFromMember( me, me );
    	else
    	{
    		for ( i = 0; i < me.squad().pawns.length; ++i )
    		{
    			squadMember = Rook(me.squad().pawns[i]);
    			if ( class'Pawn'.static.checkAlive( squadMember ) )
    				initEnemiesListFromMember( me, squadMember );
    		}
    	}
    }

    private function initEnemiesListFromMember( Rook me, Rook squadMember )
    {
    	local int i;
    	local int outi;
    	local Rook seenRook;
    	local array<Rook> enemyList;

    	enemyList = squadMember.vision.getEnemyList();

    	for ( i = 0; i < enemyList.length; ++i )
    	{
    		seenRook = enemyList[i];
    		//log( "initEnemies:" @ me.name @ seenRook.name );
    		if ( !isInEnemiesList( outi, seenRook ) )
    		{
    			if ( player == None && seenRook.Controller != None && seenRook.Controller.bIsPlayer )
    				player = seenRook;
    			enemies[enemies.length] = seenRook;
    		}
    	}
    }

    //---------------------------------------------------------------------
    // is 'rook' in the enemies list? If yes, return index (index is undefined otherwise)

    private function bool isInEnemiesList( out int i, Rook rook )
    {
    	for( i = 0; i < enemies.length; i++ )
    		if ( enemies[i] == rook )
    			return true;

    	i = -1;
    	return false;
    }

    //---------------------------------------------------------------------
    // perform sensor-specific startup initializations when sensor is activated

    function begin()
    {
    	local Rook rook;

    	rook = sensorAction.resource.localRook();
    	//log( "EnemySensor started on" @ sensorAction.resource.name @ sensorAction.resource.localRook().name );

    	initEnemiesList( rook );
    	rook.RegisterVisionNotification( self );
    	rook.level.registerNotifyTeamChanged( self );
    }

    //---------------------------------------------------------------------
    // perform sensor-specific cleanup when sensor is deactivated

    function cleanup()
    {
    	local Rook rook;

    	rook = sensorAction.resource.localRook();

    	if ( rook == None )
    	{
    		log( "AI ERROR:" @ name @ "has no attached rook; can't unregister vision notification" );
    	}
    	else
    	{
    		//log( name @ rook.name @ "UNREGISTERING VISION" );
    		rook.UnregisterVisionNotification( self );
    		rook.level.unRegisterNotifyTeamChanged( self );
    	}
    }

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

    defaultproperties
    {
    	bNotifyIfResourceInactive = true	// vision based sensors have to pass through pawn lost messages when vision shuts down
    }

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