Ejemplo n.º 1
0
bool CEntityPool::CreatePoolEntity(CEntity* &pOutEntity, bool bAddToInactiveList)
{
	FUNCTION_PROFILER(GetISystem(), PROFILE_ENTITY);

	assert(!gEnv->IsEditor());

	MEMSTAT_CONTEXT(EMemStatContextTypes::MSC_Other, EMemStatContextFlags::MSF_Instance, "Create Pool Entity");

	bool bResult = false;

	assert(m_pEntityPoolManager);
	CEntitySystem* pEntitySystem = m_pEntityPoolManager->GetEntitySystem();

	if (pEntitySystem)
	{
		SEntitySpawnParams params;
		CreateSpawnParams(params);
		params.vScale.Set(1.0f,1.0f,1.0f);

		CEntity* pEntity = (CEntity*)pEntitySystem->SpawnEntity(params);
		if (pEntity)
		{
			pEntity->SetPoolControl(true);

			// Put into map
			if (bAddToInactiveList)
			{
				const EntityId poolId = pEntity->GetId();
				stl::push_back_unique(m_InactivePoolIds, poolId);
			}

			pOutEntity = pEntity;
			bResult    = true;
		}
	}

	return bResult;
}
Ejemplo n.º 2
0
void CEntityPool::DebugDraw(IRenderer* pRenderer, float &fColumnX, float &fColumnY, bool bExtraInfo) const
{
	assert(pRenderer);

	assert(m_pEntityPoolManager);
	CEntitySystem* pEntitySystem = m_pEntityPoolManager->GetEntitySystem();

	const float colWhite[]  = {1.0f,1.0f,1.0f,1.0f};
	const float colYellow[] = {1.0f,1.0f,0.0f,1.0f};
	const float colOrange[] = {1.0f,0.5f,0.25f,1.0f};
	const float colRed[]    = {1.0f,0.0f,0.0f,1.0f};

	const string::size_type activeCount   = m_ActivePoolIds.size();
	const string::size_type inactiveCount = m_InactivePoolIds.size();
	const bool              bAtMax        = (m_uMaxSize > 0 && activeCount >= m_uMaxSize);

	const float* pColor = colWhite;
	if (m_bDebug_HasExpanded)
		pColor = colRed;
	else if (m_iDebug_TakenFromActiveForced > 0)
		pColor = colOrange;
	else if (inactiveCount <= 0)
		pColor = colYellow;

	pRenderer->Draw2dLabel(fColumnX + 5.0f, fColumnY, 1.2f, pColor, false, "Pool \'%s\' (Default Class = \'%s\'):", m_sName.c_str(), m_sDefaultClass.c_str());
	fColumnY += 15.0f;

	if (m_iDebug_TakenFromActiveForced > 0)
	{
		pRenderer->Draw2dLabel(fColumnX + 5.0f, fColumnY, 1.5f, colRed, false, "Force Reused Counter: %d", m_iDebug_TakenFromActiveForced);
		fColumnY += 20.0f;
	}

	string sInactiveCount;
	sInactiveCount.Format("Not In Use - Count: %" PRISIZE_T, inactiveCount);
	pRenderer->Draw2dLabel(fColumnX + 10.0f, fColumnY, 1.0f, colWhite, false, "%s", sInactiveCount.c_str());
	fColumnY += 12.0f;

	if (bExtraInfo && inactiveCount > 0)
	{
		string sInactiveList;

		TPoolIdsVec::const_iterator itInactiveId    = m_InactivePoolIds.begin();
		TPoolIdsVec::const_iterator itInactiveIdEnd = m_InactivePoolIds.end();
		for (bool bFirstIt = true; itInactiveId != itInactiveIdEnd; ++itInactiveId, bFirstIt = false)
		{
			string         sId;
			const EntityId inactiveId = itInactiveId->poolId;
			sId.Format("%u", inactiveId);

			sInactiveList.append(bFirstIt ? " (" : ", ");
			sInactiveList += sId;
		}

		pRenderer->Draw2dLabel(fColumnX + 15.0f, fColumnY, 1.0f, colWhite, false, "%s", sInactiveList.c_str());
		fColumnY += 12.0f;
	}

	string sActiveCount;
	sActiveCount.Format("In Use - Count: %" PRISIZE_T, activeCount);
	pRenderer->Draw2dLabel(fColumnX + 10.0f, fColumnY, 1.0f, colWhite, false, "%s", sActiveCount.c_str());
	fColumnY += 12.0f;

	if (bExtraInfo && activeCount > 0)
	{
		TReturnToPoolWeights returnToPoolWeights;
		returnToPoolWeights.reserve(m_ActivePoolIds.size());

		// Go through the active list and ask if any are available for removal
		TPoolIdsVec::const_iterator itActiveId    = m_ActivePoolIds.begin();
		TPoolIdsVec::const_iterator itActiveIdEnd = m_ActivePoolIds.end();
		for (; itActiveId != itActiveIdEnd; ++itActiveId)
		{
			const EntityId activeId      = itActiveId->usingId;
			CEntity*       pActiveEntity = pEntitySystem->GetEntityFromID(activeId);
			if (!pActiveEntity)
				continue;

			SReturnToPoolWeight returnToPoolWeight;
			returnToPoolWeight.pEntity = pActiveEntity;
			returnToPoolWeight.weight  = m_pEntityPoolManager->GetReturnToPoolWeight(pActiveEntity, bAtMax);
			returnToPoolWeights.push_back(returnToPoolWeight);
		}
		if (!returnToPoolWeights.empty())
		{
			std::sort(returnToPoolWeights.begin(), returnToPoolWeights.end());
		}

		// Debug output each one
		TReturnToPoolWeights::iterator itActiveWeight    = returnToPoolWeights.begin();
		TReturnToPoolWeights::iterator itActiveWeightEnd = returnToPoolWeights.end();
		for (; itActiveWeight != itActiveWeightEnd; ++itActiveWeight)
		{
			SReturnToPoolWeight &activeWeight  = *itActiveWeight;
			CEntity*             pActiveEntity = activeWeight.pEntity;
			assert(pActiveEntity);

			const float* pActiveColor = colWhite;
			string       sActiveInfo;
			sActiveInfo.Format("%s (%u)", pActiveEntity->GetName(), pActiveEntity->GetId());

			if (activeWeight.weight > 0.0f)
			{
				string sWeight;
				sWeight.Format(" - Return Weight: %.3f", activeWeight.weight);
				sActiveInfo += sWeight;

				pActiveColor = colYellow;
			}

			pRenderer->Draw2dLabel(fColumnX + 15.0f, fColumnY, 1.0f, pActiveColor, false, "%s", sActiveInfo.c_str());
			fColumnY += 12.0f;
		}
	}

	pRenderer->Draw2dLabel(fColumnX + 10.0f, fColumnY, 1.0f, colWhite, false, "Pool Definitions:");
	fColumnY += 12.0f;

	TPoolDefinitionIds::const_iterator itDefinitionId    = m_PoolDefinitionIds.begin();
	TPoolDefinitionIds::const_iterator itDefinitionIdEnd = m_PoolDefinitionIds.end();
	for (; itDefinitionId != itDefinitionIdEnd; ++itDefinitionId)
	{
		const TEntityPoolDefinitionId definitionId = *itDefinitionId;

		const CEntityPoolDefinition* pDefinition = m_pEntityPoolManager->GetEntityPoolDefinition(definitionId);
		if (pDefinition)
			pDefinition->DebugDraw(pRenderer, fColumnX, fColumnY);
	}
}