Пример #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);
}
Пример #2
0
//
// get a current pointer to data copy based on 
// the type of data requested
//
CAICopy *CAIUnit::GetCopyData( int iType )
{
	ASSERT_VALID( this );
	ASSERT_VALID( m_plCopyData );

	CVehicle *pVehicle = NULL;
	CBuilding *pBldg = NULL;

	EnterCriticalSection (&cs);

	switch( iType )
	{
		case CAICopy::CUnitData :
			if( m_iType == CUnit::building )
			{
				CBuilding const *pBldg = 
					pGameData->GetBuildingData( m_iOwner, m_dwID );
				if( pBldg == NULL )
					break;
				CStructureData const *pData = pBldg->GetData();
				Update( (CUnitData const *)pData );
			}
			else if( m_iType == CUnit::vehicle )
			{
				CVehicle *pVehicle = 
					pGameData->GetVehicleData( m_iOwner, m_dwID );
				if( pVehicle == NULL )
					break;

				CTransportData const *pData = pVehicle->GetData();
				Update( (CUnitData const *)pData );
			}
			break;

		case CAICopy::CVehicle :
		case CAICopy::CTransportData :
			
			pVehicle = pGameData->GetVehicleData( m_iOwner, m_dwID );
			if( pVehicle == NULL )
				break;

			// update CVehicle copy data
			if( iType == CAICopy::CVehicle )
				Update( pVehicle );
			// update CTransportData copy data
			else if( iType == CAICopy::CTransportData )
				Update( pVehicle->GetData() );

			break;

		case CAICopy::CBuilding :
		case CAICopy::CStructureData :
		case CAICopy::CBuildMaterials :
		case CAICopy::CBuildMine:
		case CAICopy::CBuildFarm:
		// BUGBUG build units are now an array of CBuildUnit objects
		// contained in the CBuildVehicle pointer from CStructureData
		//
		case CAICopy::CBuildUnit :

			// get CBuilding *pBldg from game for this unit
			pBldg = pGameData->GetBuildingData( m_iOwner, m_dwID );
			if( pBldg == NULL )
				break;

			// update CBuilding copy data
			if( iType == CAICopy::CBuilding )
				Update( pBldg );
			// update CStructureData copy data
			else if( iType == CAICopy::CStructureData )
				Update( pBldg->GetData() );
			// update CBuildMaterials copy data
			else if( iType == CAICopy::CBuildMaterials )
				Update( pBldg->GetData()->GetBldMaterials() );
			// update CBuildMine copy data
			else if( iType == CAICopy::CBuildMine )
				Update( pBldg->GetData()->GetBldMine() );
			// update CBuildFarm copy data
			else if( iType == CAICopy::CBuildFarm )
				Update( pBldg->GetData()->GetBldFarm() );
			// update CBuildUnit copy data
			else if( iType == CAICopy::CBuildUnit )
			{
				if( pBldg->GetData()->GetUnionType()
					== CStructureData::UTvehicle )
				{
					// capture data based on what the building
					// is producing or the 1st thing it could
					// produce if it is not producing anything
					CVehicleBuilding *pVehBldg =
						(CVehicleBuilding *)pBldg;
					CBuildUnit const *pBuildVeh = pVehBldg->GetBldUnt();

					if( pBuildVeh != NULL )
						Update( pBuildVeh );

					// if the building is not currently producing
					// a vehicle, then allow a NULL return
				}
				if( pBldg->GetData()->GetUnionType()
					== CStructureData::UTrepair )
				{
					// convert current building to a repair building
					CRepairBuilding *pRepBldg = 
						(CRepairBuilding *)pBldg;
					// is there a vehicle in getting a repair?
					CVehicle *pVehRepairing = pRepBldg->GetVehRepairing();
					if( pVehRepairing == NULL )
						break;
					// get the repair data for that vehicle
					CBuildRepair const *pBuildRep = pRepBldg->GetData();

					// BUGBUG may get total materials needed from using
					// pBuildRep->GetTotalRepair(iInd) 

					CBuildUnit const *pBuildVeh = 
						pBuildRep->GetRepair(pVehRepairing->GetData()->GetType());
					if( pBuildVeh != NULL )
						Update( pBuildVeh );
				}
			}
			break;
	}
	LeaveCriticalSection (&cs);

	return( m_plCopyData->GetCopy(iType) );
}
Пример #3
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 );
}