Exemple #1
0
void CAIUnitList::AddUnit( DWORD dwID )
{
	int iPlayer = 0;
	int iType = 0xFFFE;
	int iUnitType = 0xFFFE;
	BOOL bControl = FALSE;

	EnterCriticalSection (&cs);
	CVehicle *pVehicle = theVehicleMap.GetVehicle( dwID );
	if( pVehicle != NULL )
	{
		iPlayer = pVehicle->GetOwner()->GetPlyrNum();
		iType = CUnit::vehicle;
		iUnitType = pVehicle->GetData()->GetType();
		bControl = pVehicle->GetOwner()->IsAI();
	}
	else
	{
		CBuilding *pBldg = theBuildingMap.GetBldg( dwID );
		if( pBldg != NULL )
		{
			iPlayer = pBldg->GetOwner()->GetPlyrNum();
			iType = CUnit::building;
			iUnitType = pBldg->GetData()->GetType();
			bControl = pBldg->GetOwner()->IsAI();
		}
		else
			return;
	}
	LeaveCriticalSection (&cs);

	if( !iPlayer )
		return;

	CAIUnit *pUnit = NULL;
	try
	{
		// CAIUnit( DWORD dwID, int iOwner, class, type );
		pUnit = new CAIUnit( dwID, iPlayer, iType, iUnitType );
		ASSERT_VALID( pUnit );
		AddTail( (CObject *)pUnit );
	}
	catch( CException e )
	{
		// need to report this error occurred
		throw(ERR_CAI_BAD_NEW);
	}
	
	if( pUnit == NULL )
		return;

	// indicate what controls this unit
	pUnit->SetControl(bControl);
}
Exemple #2
0
//
// retrieve a pointer to a unit controlled by another player
// either HP or AI.
//
CAIUnit *CAIUnitList::GetOpForUnit( DWORD dwID )
{
	// if already known, then return it
	CAIUnit *pOpFor = GetUnit( dwID );
	if( pOpFor != NULL )
		return( pOpFor );

	// else get a copy from the game and save it

	// NOTE: the thread must be granted exclusive access
	EnterCriticalSection (&cs);

	CVehicle *pVehicle =
		theVehicleMap.GetVehicle( dwID ); 
	if( pVehicle != NULL )
	{
		if( !pVehicle->IsFlag(CUnit::dying) )
		{
			try
			{
			// CAIUnit::CAIUnit( DWORD dwID, int iOwner, int iType )
			pOpFor = new CAIUnit( dwID, 
				pVehicle->GetOwner()->GetPlyrNum(), 
				pVehicle->GetUnitType(), pVehicle->GetData()->GetType() );

			AddTail( (CObject *)pOpFor );
			}
			catch( CException e )
			{
			LeaveCriticalSection (&cs);
			throw(ERR_CAI_BAD_NEW);
			}
		}
	}
	else
	{
		CBuilding *pBldg = theBuildingMap.GetBldg( dwID );
		if( pBldg != NULL )
		{
			if( !pBldg->IsFlag(CUnit::dying) )
			{
				try
				{
				// CAIUnit::CAIUnit( DWORD dwID, int iOwner, int iType )
				pOpFor = new CAIUnit( dwID, 
				pBldg->GetOwner()->GetPlyrNum(), 
				pBldg->GetUnitType(), pBldg->GetData()->GetType() );

				AddTail( (CObject *)pOpFor );
				}
				catch( CException e )
				{
				LeaveCriticalSection (&cs);
				throw(ERR_CAI_BAD_NEW);
				}
			}
		}
	}
	LeaveCriticalSection (&cs);
	
	return( pOpFor );
}