summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--source/MobCensus.cpp87
-rw-r--r--source/MobCensus.h72
-rw-r--r--source/MobFamilyCollecter.cpp26
-rw-r--r--source/MobFamilyCollecter.h45
-rw-r--r--source/World.cpp12
5 files changed, 157 insertions, 85 deletions
diff --git a/source/MobCensus.cpp b/source/MobCensus.cpp
index 612f25916..67b154404 100644
--- a/source/MobCensus.cpp
+++ b/source/MobCensus.cpp
@@ -5,7 +5,9 @@
-cMobCensus::tCapMultipliersMap cMobCensus::CapMultiplierInitializerBeforeCx11()
+
+
+cMobCensus::tCapMultipliersMap cMobCensus::CapMultiplierInitializerBeforeCx11(void)
{
std::map<cMonster::eFamily,int> toReturn;
toReturn[cMonster::mfHostile] = 79;
@@ -15,7 +17,11 @@ cMobCensus::tCapMultipliersMap cMobCensus::CapMultiplierInitializerBeforeCx11()
return toReturn;
}
-cMobCensus::tMobSpawnRate cMobCensus::MobSpawnRateInitializerBeforeCx11()
+
+
+
+
+cMobCensus::tMobSpawnRate cMobCensus::MobSpawnRateInitializerBeforeCx11(void)
{
std::map<cMonster::eFamily,int> toReturn;
toReturn[cMonster::mfHostile] = 1;
@@ -25,65 +31,98 @@ cMobCensus::tMobSpawnRate cMobCensus::MobSpawnRateInitializerBeforeCx11()
return toReturn;
}
-cMobCensus::tCapMultipliersMap& cMobCensus::m_CapMultipliers()
+
+
+
+
+cMobCensus::tCapMultipliersMap & cMobCensus::m_CapMultipliers(void)
{
- static tCapMultipliersMap* value = new tCapMultipliersMap(CapMultiplierInitializerBeforeCx11());
+ // TODO: This memory leaks:
+ static tCapMultipliersMap * value = new tCapMultipliersMap(CapMultiplierInitializerBeforeCx11());
return *value;
}
-cMobCensus::tMobSpawnRate& cMobCensus::m_SpawnRate()
+
+
+
+
+cMobCensus::tMobSpawnRate & cMobCensus::m_SpawnRate(void)
{
+ // TODO: This memory leaks:
static tMobSpawnRate* value = new tMobSpawnRate(MobSpawnRateInitializerBeforeCx11());
return *value;
}
-cMobCensus::cMobCensus()
-{
-}
-void cMobCensus::CollectMob(cMonster& a_Monster, cChunk& a_Chunk, double a_Distance)
+
+
+
+void cMobCensus::CollectMob(cMonster & a_Monster, cChunk & a_Chunk, double a_Distance)
{
m_ProximityCounter.CollectMob(a_Monster,a_Chunk,a_Distance);
m_MobFamilyCollecter.CollectMob(a_Monster);
}
-bool cMobCensus::isCaped(cMonster::eFamily a_MobFamily)
+
+
+
+
+bool cMobCensus::IsCapped(cMonster::eFamily a_MobFamily)
{
bool toReturn = true;
const int ratio = 319; // this should be 256 as we are only supposed to take account from chunks that are in 17x17 from a player
- // but for now, we use all chunks loaded by players. that means 19 x 19 chucks. That's why we use 256 * (19*19) / (17*17) = 319
+ // but for now, we use all chunks loaded by players. that means 19 x 19 chunks. That's why we use 256 * (19*19) / (17*17) = 319
// MG TODO : code the correct count
tCapMultipliersMap::const_iterator capMultiplier = m_CapMultipliers().find(a_MobFamily);
- if (
+ if (
(capMultiplier != m_CapMultipliers().end()) &&
- (capMultiplier->second * getChunkNb()) / ratio >= m_MobFamilyCollecter.getNumberOfCollectedMobs(a_MobFamily)
- )
+ ((capMultiplier->second * GetNumChunks()) / ratio >= m_MobFamilyCollecter.GetNumberOfCollectedMobs(a_MobFamily))
+ )
{
- toReturn = false;
+ return false;
}
- return toReturn;
+ return true;
}
-void cMobCensus::CollectSpawnableChunk(cChunk& a_Chunk)
+
+
+
+
+void cMobCensus::CollectSpawnableChunk(cChunk & a_Chunk)
{
m_EligibleForSpawnChunks.insert(&a_Chunk);
}
-int cMobCensus::getChunkNb()
+
+
+
+
+int cMobCensus::GetNumChunks(void)
{
return m_EligibleForSpawnChunks.size();
}
-cMobProximityCounter& cMobCensus::getProximityCounter()
+
+
+
+
+cMobProximityCounter & cMobCensus::GetProximityCounter(void)
{
return m_ProximityCounter;
}
-void cMobCensus::logd()
+
+
+
+void cMobCensus::Logd()
{
- LOGD((std::string("Hostile mobs : %d") + (isCaped(cMonster::mfHostile)?"(capped)":"")).c_str(),m_MobFamilyCollecter.getNumberOfCollectedMobs(cMonster::mfHostile));
- LOGD((std::string("Ambiant mobs : %d") + (isCaped(cMonster::mfAmbient)?"(capped)":"")).c_str(),m_MobFamilyCollecter.getNumberOfCollectedMobs(cMonster::mfAmbient));
- LOGD((std::string("Water mobs : %d") + (isCaped(cMonster::mfWater)? "(capped)":"")).c_str(),m_MobFamilyCollecter.getNumberOfCollectedMobs(cMonster::mfWater));
- LOGD((std::string("Passive mobs : %d") + (isCaped(cMonster::mfPassive)?"(capped)":"")).c_str(),m_MobFamilyCollecter.getNumberOfCollectedMobs(cMonster::mfPassive));
+ LOGD("Hostile mobs : %d %s", m_MobFamilyCollecter.GetNumberOfCollectedMobs(cMonster::mfHostile), IsCapped(cMonster::mfHostile) ? "(capped)" : "");
+ LOGD("Ambient mobs : %d %s", m_MobFamilyCollecter.GetNumberOfCollectedMobs(cMonster::mfAmbient), IsCapped(cMonster::mfAmbient) ? "(capped)" : "");
+ LOGD("Water mobs : %d %s", m_MobFamilyCollecter.GetNumberOfCollectedMobs(cMonster::mfWater), IsCapped(cMonster::mfWater) ? "(capped)" : "");
+ LOGD("Passive mobs : %d %s", m_MobFamilyCollecter.GetNumberOfCollectedMobs(cMonster::mfPassive), IsCapped(cMonster::mfPassive) ? "(capped)" : "");
}
+
+
+
+
diff --git a/source/MobCensus.h b/source/MobCensus.h
index 8aa8f3a6c..7606efcea 100644
--- a/source/MobCensus.h
+++ b/source/MobCensus.h
@@ -4,55 +4,63 @@
#include "MobProximityCounter.h"
#include "MobFamilyCollecter.h"
+
+
+
+// fwd:
class cChunk;
class cMonster;
-// This class is used to collect, for each Mob, what is the distance of the closest player
-// it was first being designed in order to make mobs spawn / despawn / act
-// as the behaviour and even life of mobs depends on the distance to closest player
-//
-// as side effect : it also collect the chunks that are elligible for spawning
-// as side effect 2 : it also know the caps for mobs number and can compare census to this numbers
-class cMobCensus
-{
-public :
- cMobCensus();
-protected :
- cMobProximityCounter m_ProximityCounter;
- cMobFamilyCollecter m_MobFamilyCollecter;
- typedef const std::map<cMonster::eFamily,int> tCapMultipliersMap;
- static tCapMultipliersMap& m_CapMultipliers();
- std::set<cChunk*> m_EligibleForSpawnChunks;
- // count the chunks that are elligible to spawn (for now, the loaded valide not null chunks)
- int getChunkNb();
+/** This class is used to collect information, for each Mob, what is the distance of the closest player
+it was first being designed in order to make mobs spawn / despawn / act
+as the behaviour and even life of mobs depends on the distance to closest player
+as side effect : it also collect the chunks that are elligible for spawning
+as side effect 2 : it also know the caps for mobs number and can compare census to this numbers
+*/
+class cMobCensus
+{
public:
typedef const std::map<cMonster::eFamily,int> tMobSpawnRate;
- static tMobSpawnRate& m_SpawnRate();
+ static tMobSpawnRate & m_SpawnRate(void);
- // return the nested proximity counter
- cMobProximityCounter& getProximityCounter();
+ /// Returns the nested proximity counter
+ cMobProximityCounter & GetProximityCounter(void);
-public :
// collect an elligible Chunk for Mob Spawning
// MG TODO : code the correct rule (not loaded chunk but short distant from players)
- void CollectSpawnableChunk(cChunk& a_Chunk);
+ void CollectSpawnableChunk(cChunk & a_Chunk);
- // collect a mob - it's distance to player, it's family ...
+ /// Collect a mob - it's distance to player, it's family ...
void CollectMob(cMonster& a_Monster, cChunk& a_Chunk, double a_Distance);
- // return true if the family is caped (i.e. there is more mobs of this family than max)
- bool isCaped(cMonster::eFamily a_MobFamily);
+ /// Returns true if the family is capped (i.e. there are more mobs of this family than max)
+ bool IsCapped(cMonster::eFamily a_MobFamily);
+
+ /// log the results of census to server console
+ void Logd(void);
+
+protected :
+ cMobProximityCounter m_ProximityCounter;
+ cMobFamilyCollecter m_MobFamilyCollecter;
+
+ typedef const std::map<cMonster::eFamily,int> tCapMultipliersMap;
+
+ static tCapMultipliersMap & m_CapMultipliers(void);
+
+ std::set<cChunk *> m_EligibleForSpawnChunks;
+
+ /// Returns the number of chunks that are elligible for spawning (for now, the loaded, valid chunks)
+ int GetNumChunks();
+
+ static tCapMultipliersMap CapMultiplierInitializerBeforeCx11(void);
+ static tCapMultipliersMap MobSpawnRateInitializerBeforeCx11(void);
+} ;
+
- // log the results of census
- void logd();
-protected :
- static tCapMultipliersMap CapMultiplierInitializerBeforeCx11();
- static tCapMultipliersMap MobSpawnRateInitializerBeforeCx11();
-};
diff --git a/source/MobFamilyCollecter.cpp b/source/MobFamilyCollecter.cpp
index 2aa46599a..086fa5f40 100644
--- a/source/MobFamilyCollecter.cpp
+++ b/source/MobFamilyCollecter.cpp
@@ -6,7 +6,7 @@
-cMobFamilyCollecter::tMobFamilyList cMobFamilyCollecter::initMobFamilyBeforeCx11()
+cMobFamilyCollecter::tMobFamilyList cMobFamilyCollecter::InitMobFamilyBeforeCx11(void)
{
std::set<cMonster::eFamily> toReturn;
toReturn.insert(cMonster::mfHostile);
@@ -15,19 +15,37 @@ cMobFamilyCollecter::tMobFamilyList cMobFamilyCollecter::initMobFamilyBeforeCx11
toReturn.insert(cMonster::mfWater);
return toReturn;
}
-cMobFamilyCollecter::tMobFamilyList& cMobFamilyCollecter::m_AllFamilies()
+
+
+
+
+
+cMobFamilyCollecter::tMobFamilyList & cMobFamilyCollecter::m_AllFamilies(void)
{
- static tMobFamilyList* AllFamilies = new tMobFamilyList(initMobFamilyBeforeCx11());
+ // TODO: This memory is leaked:
+ static tMobFamilyList * AllFamilies = new tMobFamilyList(InitMobFamilyBeforeCx11());
return *AllFamilies;
}
+
+
+
+
void cMobFamilyCollecter::CollectMob(cMonster& a_Monster)
{
cMonster::eFamily MobFamily = a_Monster.GetMobFamily();
m_Mobs[MobFamily].insert(&a_Monster);
}
-int cMobFamilyCollecter::getNumberOfCollectedMobs(cMonster::eFamily a_Family)
+
+
+
+
+int cMobFamilyCollecter::GetNumberOfCollectedMobs(cMonster::eFamily a_Family)
{
return m_Mobs[a_Family].size();
}
+
+
+
+
diff --git a/source/MobFamilyCollecter.h b/source/MobFamilyCollecter.h
index 659d2b687..cd05b6adb 100644
--- a/source/MobFamilyCollecter.h
+++ b/source/MobFamilyCollecter.h
@@ -6,35 +6,44 @@
#include "BlockID.h"
#include "Mobs/Monster.h" //this is a side-effect of keeping Mobfamily inside Monster class. I'd prefer to keep both (Mobfamily and Monster) inside a "Monster" namespace MG TODO : do it
+
+
+
+// fwd:
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, 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
+*/
class cMobFamilyCollecter
{
-protected :
- std::map<cMonster::eFamily,std::set<cMonster*> > m_Mobs;
-
public :
+ typedef const std::set<cMonster::eFamily> tMobFamilyList;
+ typedef const std::map<cMonster::eFamily,int> tMobSpawRate;
+
// collect a mob
- void CollectMob(cMonster& a_Monster);
+ void CollectMob(cMonster & a_Monster);
// return the number of mobs for this family
- int getNumberOfCollectedMobs(cMonster::eFamily a_Family);
+ int GetNumberOfCollectedMobs(cMonster::eFamily a_Family);
-public :
- typedef const std::set<cMonster::eFamily> tMobFamilyList;
- static tMobFamilyList& m_AllFamilies();
+ static tMobFamilyList & m_AllFamilies(void);
-public :
- typedef const std::map<cMonster::eFamily,int> tMobSpawRate;
- static tMobSpawRate& m_SpawnRate();
+ static tMobSpawRate & m_SpawnRate(void);
protected :
- static tMobFamilyList initMobFamilyBeforeCx11();
- static tMobSpawRate initMobSpawnRateBeforeCx11();
-};
+ std::map<cMonster::eFamily, std::set<cMonster*> > m_Mobs;
+
+ static tMobFamilyList InitMobFamilyBeforeCx11(void);
+ static tMobSpawRate InitMobSpawnRateBeforeCx11(void);
+} ;
+
+
+
diff --git a/source/World.cpp b/source/World.cpp
index 967cd8f59..039e192d9 100644
--- a/source/World.cpp
+++ b/source/World.cpp
@@ -732,21 +732,19 @@ void cWorld::TickMobs(float a_Dt)
{
cMobCensus::tMobSpawnRate::const_iterator spawnrate = cMobCensus::m_SpawnRate().find(*itr);
// hostile mobs are spawned more often
- if (spawnrate != cMobCensus::m_SpawnRate().end() && m_LastSpawnMonster[*itr] < m_WorldAge - spawnrate->second)
+ if ((spawnrate != cMobCensus::m_SpawnRate().end()) && (m_LastSpawnMonster[*itr] < m_WorldAge - spawnrate->second))
{
m_LastSpawnMonster[*itr] = m_WorldAge;
// each megatype of mob has it's own cap
- if (!(MobCensus.isCaped(*itr)))
+ if (!(MobCensus.IsCapped(*itr)))
{
if (m_bAnimals)
{
-
cMobSpawner Spawner(*itr,m_AllowedMobs);
if (Spawner.CanSpawnSomething())
{
m_ChunkMap->SpawnMobs(Spawner);
// do the spawn
-
for(cMobSpawner::tSpawnedContainer::const_iterator itr2 = Spawner.getSpawned().begin(); itr2 != Spawner.getSpawned().end(); itr2++)
{
SpawnMobFinalize(*itr2);
@@ -758,14 +756,14 @@ void cWorld::TickMobs(float a_Dt)
}
// move close mobs
- cMobProximityCounter::sIterablePair allCloseEnoughToMoveMobs = MobCensus.getProximityCounter().getMobWithinThosesDistances(-1,64*16);// MG TODO : deal with this magic number (the 16 is the size of a block)
+ cMobProximityCounter::sIterablePair allCloseEnoughToMoveMobs = MobCensus.GetProximityCounter().getMobWithinThosesDistances(-1, 64 * 16);// MG TODO : deal with this magic number (the 16 is the size of a block)
for(cMobProximityCounter::tDistanceToMonster::const_iterator itr = allCloseEnoughToMoveMobs.m_Begin; itr != allCloseEnoughToMoveMobs.m_End; itr++)
{
- itr->second.m_Monster.Tick(a_Dt,itr->second.m_Chunk);
+ itr->second.m_Monster.Tick(a_Dt, itr->second.m_Chunk);
}
// remove too far mobs
- cMobProximityCounter::sIterablePair allTooFarMobs = MobCensus.getProximityCounter().getMobWithinThosesDistances(128*16,-1);// MG TODO : deal with this magic number (the 16 is the size of a block)
+ cMobProximityCounter::sIterablePair allTooFarMobs = MobCensus.GetProximityCounter().getMobWithinThosesDistances(128 * 16, -1);// MG TODO : deal with this magic number (the 16 is the size of a block)
for(cMobProximityCounter::tDistanceToMonster::const_iterator itr = allTooFarMobs.m_Begin; itr != allTooFarMobs.m_End; itr++)
{
itr->second.m_Monster.Destroy(true);