1
0

Fixed memory leak in cMobFamilyCollecter.

This commit is contained in:
madmaxoft 2013-10-20 14:16:21 +02:00
parent d8576a7953
commit b6741865f2
3 changed files with 21 additions and 52 deletions

View File

@ -6,32 +6,7 @@
cMobFamilyCollecter::tMobFamilyList cMobFamilyCollecter::InitMobFamilyBeforeCx11(void)
{
std::set<cMonster::eFamily> toReturn;
toReturn.insert(cMonster::mfHostile);
toReturn.insert(cMonster::mfPassive);
toReturn.insert(cMonster::mfAmbient);
toReturn.insert(cMonster::mfWater);
return toReturn;
}
cMobFamilyCollecter::tMobFamilyList & cMobFamilyCollecter::m_AllFamilies(void)
{
// TODO: This memory is leaked:
static tMobFamilyList * AllFamilies = new tMobFamilyList(InitMobFamilyBeforeCx11());
return *AllFamilies;
}
void cMobFamilyCollecter::CollectMob(cMonster& a_Monster)
void cMobFamilyCollecter::CollectMob(cMonster & a_Monster)
{
cMonster::eFamily MobFamily = a_Monster.GetMobFamily();
m_Mobs[MobFamily].insert(&a_Monster);

View File

@ -16,16 +16,12 @@ class cChunk;
/** This class is used to collect, for each Mob, what is it's family. It was first
being designed to check the caps of the mobs (no more than ... hostile mob in the world)
as side effects : it also know what is the spawnrate of each family : MG TODO relocate
/** This class is used to collect the list of mobs for each family
*/
class cMobFamilyCollecter
{
public :
typedef const std::set<cMonster::eFamily> tMobFamilyList;
typedef const std::map<cMonster::eFamily,int> tMobSpawRate;
// collect a mob
void CollectMob(cMonster & a_Monster);
@ -33,15 +29,9 @@ public :
// return the number of mobs for this family
int GetNumberOfCollectedMobs(cMonster::eFamily a_Family);
static tMobFamilyList & m_AllFamilies(void);
static tMobSpawRate & m_SpawnRate(void);
protected :
std::map<cMonster::eFamily, std::set<cMonster*> > m_Mobs;
std::map<cMonster::eFamily, std::set<cMonster *> > m_Mobs;
static tMobFamilyList InitMobFamilyBeforeCx11(void);
static tMobSpawRate InitMobSpawnRateBeforeCx11(void);
} ;

View File

@ -736,38 +736,42 @@ void cWorld::TickWeather(float a_Dt)
void cWorld::TickMobs(float a_Dt)
{
if (!m_bAnimals)
{
return;
}
// before every Mob action, we have to "counts" them depending on the distance to players, on their megatype ...
// before every Mob action, we have to count them depending on the distance to players, on their family ...
cMobCensus MobCensus;
m_ChunkMap->CollectMobCensus(MobCensus);
if (m_bAnimals)
{
for (cMobFamilyCollecter::tMobFamilyList::const_iterator itr = cMobFamilyCollecter::m_AllFamilies().begin(); itr != cMobFamilyCollecter::m_AllFamilies().end(); itr++)
// Spawning is enabled, spawn now:
static const cMonster::eFamily AllFamilies[] =
{
int spawnrate = cMonster::GetSpawnRate(*itr);
cMonster::mfHostile,
cMonster::mfPassive,
cMonster::mfAmbient,
cMonster::mfWater,
} ;
for (int i = 0; i < ARRAYCOUNT(AllFamilies); i++)
{
cMonster::eFamily Family = AllFamilies[i];
int spawnrate = cMonster::GetSpawnRate(Family);
if (
(m_LastSpawnMonster[*itr] > m_WorldAge - spawnrate) || // Not reached the needed tiks before the next round
MobCensus.IsCapped(*itr)
(m_LastSpawnMonster[Family] > m_WorldAge - spawnrate) || // Not reached the needed tiks before the next round
MobCensus.IsCapped(Family)
)
{
continue;
}
m_LastSpawnMonster[*itr] = m_WorldAge;
cMobSpawner Spawner(*itr, m_AllowedMobs);
m_LastSpawnMonster[Family] = m_WorldAge;
cMobSpawner Spawner(Family, m_AllowedMobs);
if (Spawner.CanSpawnAnything())
{
m_ChunkMap->SpawnMobs(Spawner);
// do the spawn
for(cMobSpawner::tSpawnedContainer::const_iterator itr2 = Spawner.getSpawned().begin(); itr2 != Spawner.getSpawned().end(); itr2++)
for (cMobSpawner::tSpawnedContainer::const_iterator itr2 = Spawner.getSpawned().begin(); itr2 != Spawner.getSpawned().end(); itr2++)
{
SpawnMobFinalize(*itr2);
}
}
} // for itr - Families[]
} // for i - AllFamilies[]
} // if (Spawning enabled)
// move close mobs