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

U2AI.UtilSquad


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
//=============================================================================
// UtilSquad.uc
// Created By: Mike Fox
// Created On: 8/01/00
// $Author: Mfox $
// $Date: 12/16/02 4:19a $
// $Revision: 1 $
//=============================================================================

class UtilSquad extends Object
	abstract;

/*-----------------------------------------------------------------------------
Placeholder class for squad-related code. Eventually this should become a
component which is shared by squadmates.

Currently, a squad consists of any valid NPCs which are on the same team and 
have "squad" orders (currently either OrdersGoto with an associated destination 
OrdersObject or OrdersSquad).

Members of a squad decide whether or not to actually attempt to carry out their
orders while engaged in combat, i.e. whether to set/clear bExecuteOrders which 
determines whether NPCs will actively engage the enemy (ignoring the orders) or
passively engage the enemy (while falling back towards the orders destination).

(The value of bExecuteOrders only applies to NPCs which have a valid enemy. If
an NPC is scripted or autonomous without an enemy, then the NPC is counted as
be following the squad orders, if it has them.)

If an NPC doesn't have an enemy then it will probably be under script
control and following the script, presumably to the orders destination

*/

//-----------------------------------------------------------------------------
// Does the NPC associated with the given controller belong to the same squad 
// as the given ComparisonPawn?
//
// NOTE: will return true if the given Controller belongs to the given
// Pawn as long as other checks pass.
//
// !!mdf-tbd: check for NPCs having the same orders as each other?

static private function bool ValidSquadMember( Controller C, Pawn ComparisonPawn )
{
	if( C.bStasis )
		return false;
	if( U2NPCControllerBasic(C) == None )
		return false;
	if( !U2NPCControllerBasic(C).HasSquadOrders() )
		return false;
	if( !class'Pawn'.static.ValidPawn( C.Pawn ) )
		return false;
	if( !ComparisonPawn.SameTeam( C.Pawn ) )
		return false;
		
	return true;
}

//-----------------------------------------------------------------------------
// Find all valid members of the squad that the given SourcePawn belongs to 
// (including the given SourcePawn unless this is no longer valid, e.g. dead)
// and return the number of squad members and the number of those which have 
// bExecuteOrders = true. Also finds the ExecuteOrdersThreshold by using the
// largest threshold found for any NPC in the squad.

static private function GetSquadInfo( Pawn SourcePawn, out array<U2NPCControllerBasic> SquadMembers, out int ExecuteOrdersCount, out float SquadExecuteOrdersThreshold )
{
	local Controller C;
	local U2NPCControllerBasic U2NPCBasic;
	
	//SourcePawn.DMTNS( "GetSquadInfo for " $ SourcePawn );
	for( C=SourcePawn.Level.ControllerList; C!=None; C=C.NextController )
	{
		//SourcePawn.DMTNS( "  considering " $ C.Pawn );

		if( ValidSquadMember( C, SourcePawn ) )		
		{
			U2NPCBasic = U2NPCControllerBasic(C);
			
			//SourcePawn.DMTNS( "    accepted " $ C.Pawn );
			SquadMembers.Length = SquadMembers.Length + 1;
			SquadMembers[ SquadMembers.Length - 1 ] = U2NPCBasic;
			
			//NOTE: just use the largest one found (should be stored on a per-squad basis eventually)
			SquadExecuteOrdersThreshold = FMax( SquadExecuteOrdersThreshold, U2NPCBasic.ExecuteOrdersThreshold );
			
			if( U2NPCBasic.bExecuteOrders )
			{
				//SourcePawn.DMTNS( "    " $ C.Pawn $ " has bExecuteOrders );
				ExecuteOrdersCount++;
			}
		}
		else
		{
			//SourcePawn.DMTNS( "    rejected " $ C.Pawn );
		}
	}
	
	//SourcePawn.DMTNS( "GetSquadInfo  NumSquadMembers: " $ SquadMembers.Length $ " ExecuteOrdersCount: " $ ExecuteOrdersCount $ " SquadExecuteOrdersThreshold: " $ SquadExecuteOrdersThreshold );
}

//-----------------------------------------------------------------------------
// See if the squad needs to be rebalanced.

static private function UpdateSquad( Pawn UpdatedPawn )
{
	local int NumSquadMembers;
	local int ExecuteOrdersCount;
	local int DesiredExecuteOrdersCount, ExecuteOrdersDiscrepancy;
	local float Distance, BestDistance;
	local float BestDistanceInitializer, BestDistanceEnemyMultiplier;
	local float SquadExecuteOrdersThreshold;
	local bool bEnableExecuteOrders;
	local U2NPCControllerBasic U2NPCBasic, BestC;
	local array<U2NPCControllerBasic> SquadMembers;
	local int ii, jj;
	
	//Log( "UpdateSquad" );
	
	if( UpdatedPawn == None )
		return; // no information?
		
	GetSquadInfo( UpdatedPawn, SquadMembers, ExecuteOrdersCount, SquadExecuteOrdersThreshold );

	NumSquadMembers = SquadMembers.Length;
	if( NumSquadMembers == 0 )
		return; // no squad members left

	DesiredExecuteOrdersCount = Min( NumSquadMembers, int( SquadExecuteOrdersThreshold * NumSquadMembers + 0.5) );
	//Log( "  DesiredExecuteOrdersCount: " $ DesiredExecuteOrdersCount );
	
	ExecuteOrdersDiscrepancy = ExecuteOrdersCount - DesiredExecuteOrdersCount;
	
	// NOTE: ExecuteOrdersDiscrepancy will generally be in -1..1 so not much point 
	// in sorting the SquadMembers array by distance (ideally this list would be
	// maintained in a central location but updating it whenever an NPC dies
	// or acquires an enemy isn't going to add much overhead at this point).
	if( ExecuteOrdersDiscrepancy != 0 )
	{
		if( ExecuteOrdersDiscrepancy > 0 )
		{
			// need more squad members with bExecuteOrders=false -- pick N furthest candidates from OrdersObject
			//Log( "  reducing bExecuteOrders count" );
			bEnableExecuteOrders = false;
			BestDistanceInitializer = -999999;
			BestDistanceEnemyMultiplier = 0.333;
		}
		else
		{
			// need more squad members with bExecuteOrders=true -- pick N closest candidates to OrdersObject
			//Log( "  increasing bExecuteOrders count" );
			bEnableExecuteOrders = true;
			BestDistanceInitializer = 999999;
			BestDistanceEnemyMultiplier = 3.0;
			ExecuteOrdersDiscrepancy = -ExecuteOrdersDiscrepancy;
		}
		
		for( ii=0; ii<ExecuteOrdersDiscrepancy; ii++ )
		{
			BestC = None;
			BestDistance = BestDistanceInitializer;
			for( jj=0; jj<SquadMembers.Length; jj++ )
			{
				U2NPCBasic = SquadMembers[ jj ];
				
				//Log( "      Consider " $ U2NPCBasic.Pawn $ " bExecuteOrders: " $ U2NPCBasic.bExecuteOrders $ " Orders: " $ U2NPCBasic.Orders $ " ScriptControl: " $ U2NPCBasic.UnderScriptControl() );
				if( U2NPCBasic.bExecuteOrders != bEnableExecuteOrders && U2NPCBasic.Orders == U2NPCBasic.OrdersGoto && !U2NPCBasic.UnderScriptControl() )
				{
					Distance = VSize( U2NPCBasic.Pawn.Location - U2NPCBasic.OrdersObject.Location );
					
					// if the NPC currently has an enemy, prefer other NPCs (scale distance)
					if( U2NPCBasic.ControllerEnemy != None )
						Distance *= BestDistanceEnemyMultiplier;
						
					//Log( "      Distance: " $ Distance $ " BestDistance: " $ BestDistance );
					
					if( (bEnableExecuteOrders && Distance < BestDistance) || // looking for closest to OrdersObject
						(!bEnableExecuteOrders && Distance > BestDistance) ) // looking for furthest from OrdersObject
					{
						BestDistance = Distance;
						BestC = U2NPCBasic;
					}
				}
			}
			
			if( BestC != None )
				BestC.SetExecuteOrders( bEnableExecuteOrders );
			else
				break;
		}
	}
}

//-----------------------------------------------------------------------------
// Given NPC now has squad orders. Call UpdateSquad to see if the squad needs 
// to be rebalanced.

static function CheckSquadOrdersOnMemberAdded( U2NPCControllerBasic AddedController )
{
	//Log( "CheckSquadOrdersOnMemberAdded: " $ AddedController.Pawn );
	
	UpdateSquad( AddedController.Pawn );
}

//-----------------------------------------------------------------------------
// Given Pawn was either killed or no longer has squad orders. Call UpdateSquad
// to see if the squad needs to be rebalanced.
//
// NOTE: Pawn passed in separately since when an NPC dies the Pawn is generally 
// UnPossessed and loses its Controller.

static function CheckSquadOrdersOnMemberLost( U2NPCControllerBasic LostController, Pawn LostPawn )
{
	//Log( "CheckSquadOrdersOnMemberLost: " $ LostPawn );
	
	LostController.SetExecuteOrders( false );
	UpdateSquad( LostPawn );
}

//-----------------------------------------------------------------------------
// Given NPC acquired an enemy (when it didn't have one previously). Set that
// Pawn's

static function CheckSquadOrdersOnEnemyAcquired( Pawn AcquiringPawn )
{
	local array<U2NPCControllerBasic> SquadMembers;
	local int NumSquadMembers;
	local int ExecuteOrdersCount;
	local float ExecuteOrdersRatio;
	local float SquadExecuteOrdersThreshold;

	//Log( "CheckSquadOrdersOnEnemyAcquired: " $ AcquiringPawn );
	
	UpdateSquad( AcquiringPawn );

	/*2002.12.15 (mdf) just call UpdateSquad now?		
	GetSquadInfo( AcquiringController.Pawn, SquadMembers, ExecuteOrdersCount, SquadExecuteOrdersThreshold );

	NumSquadMembers = SquadMembers.Length;
	if( NumSquadMembers == 0 )
		return; // entire squad wiped out
		
	//!!mdf-tbd: reassign orders based on proximity to ordersobject/enemy(s)?
	if( NumSquadMembers <= 1 )
	{
		//AcquiringController.DMTNS( "bExecuteOrders = true " $ GetContext() );
		AcquiringController.SetExecuteOrders( true ); // no one left to to the job but given NPC
	}
	else 
	{
		ExecuteOrdersRatio = float(ExecuteOrdersCount) / float(NumSquadMembers);
		if( ExecuteOrdersRatio < SquadExecuteOrdersThreshold )
		{
			//AcquiringController.DMTNS( "bExecuteOrders = true " $ GetContext() );
			AcquiringController.SetExecuteOrders( true );  // not enough teammates following orders
		}
		else if( ExecuteOrdersRatio > SquadExecuteOrdersThreshold )
		{
			AcquiringController.SetExecuteOrders( false ); // not enough teammates attacking enemies
		}
	}
	*/
	
	//Log( "CheckSquadOrdersOnEnemyAcquired: " $ AcquiringController.ControllerEnemy $ " NumSquadMembers: " $ NumSquadMembers $ " ExecuteOrdersCount: " $ ExecuteOrdersCount $ " bExecuteOrders: " $ AcquiringController.bExecuteOrders );
}

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

defaultproperties
{
}

Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames
Class file time: sk 3-1-2016 10:38:44.000 - Creation time: sk 3-1-2016 10:48:43.704 - Created with UnCodeX