1
0

Adapted code style.

This commit is contained in:
madmaxoft 2013-10-18 20:02:53 +02:00
parent 24aad485b9
commit ca538d5323
5 changed files with 164 additions and 92 deletions

View File

@ -5,7 +5,9 @@
cMobCensus::tCapMultipliersMap cMobCensus::CapMultiplierInitializerBeforeCx11()
cMobCensus::tCapMultipliersMap cMobCensus::CapMultiplierInitializerBeforeCx11(void)
{ {
std::map<cMonster::eFamily,int> toReturn; std::map<cMonster::eFamily,int> toReturn;
toReturn[cMonster::mfHostile] = 79; toReturn[cMonster::mfHostile] = 79;
@ -15,7 +17,11 @@ cMobCensus::tCapMultipliersMap cMobCensus::CapMultiplierInitializerBeforeCx11()
return toReturn; return toReturn;
} }
cMobCensus::tMobSpawnRate cMobCensus::MobSpawnRateInitializerBeforeCx11()
cMobCensus::tMobSpawnRate cMobCensus::MobSpawnRateInitializerBeforeCx11(void)
{ {
std::map<cMonster::eFamily,int> toReturn; std::map<cMonster::eFamily,int> toReturn;
toReturn[cMonster::mfHostile] = 1; toReturn[cMonster::mfHostile] = 1;
@ -25,65 +31,98 @@ cMobCensus::tMobSpawnRate cMobCensus::MobSpawnRateInitializerBeforeCx11()
return toReturn; 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; return *value;
} }
cMobCensus::tMobSpawnRate& cMobCensus::m_SpawnRate()
cMobCensus::tMobSpawnRate & cMobCensus::m_SpawnRate(void)
{ {
// TODO: This memory leaks:
static tMobSpawnRate* value = new tMobSpawnRate(MobSpawnRateInitializerBeforeCx11()); static tMobSpawnRate* value = new tMobSpawnRate(MobSpawnRateInitializerBeforeCx11());
return *value; 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_ProximityCounter.CollectMob(a_Monster,a_Chunk,a_Distance);
m_MobFamilyCollecter.CollectMob(a_Monster); m_MobFamilyCollecter.CollectMob(a_Monster);
} }
bool cMobCensus::isCaped(cMonster::eFamily a_MobFamily)
bool cMobCensus::IsCapped(cMonster::eFamily a_MobFamily)
{ {
bool toReturn = true; 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 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 // MG TODO : code the correct count
tCapMultipliersMap::const_iterator capMultiplier = m_CapMultipliers().find(a_MobFamily); tCapMultipliersMap::const_iterator capMultiplier = m_CapMultipliers().find(a_MobFamily);
if ( if (
(capMultiplier != m_CapMultipliers().end()) && (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); m_EligibleForSpawnChunks.insert(&a_Chunk);
} }
int cMobCensus::getChunkNb()
int cMobCensus::GetNumChunks(void)
{ {
return m_EligibleForSpawnChunks.size(); return m_EligibleForSpawnChunks.size();
} }
cMobProximityCounter& cMobCensus::getProximityCounter()
cMobProximityCounter & cMobCensus::GetProximityCounter(void)
{ {
return m_ProximityCounter; 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("Hostile mobs : %d %s", m_MobFamilyCollecter.GetNumberOfCollectedMobs(cMonster::mfHostile), IsCapped(cMonster::mfHostile) ? "(capped)" : "");
LOGD((std::string("Ambiant mobs : %d") + (isCaped(cMonster::mfAmbient)?"(capped)":"")).c_str(),m_MobFamilyCollecter.getNumberOfCollectedMobs(cMonster::mfAmbient)); LOGD("Ambient mobs : %d %s", m_MobFamilyCollecter.GetNumberOfCollectedMobs(cMonster::mfAmbient), IsCapped(cMonster::mfAmbient) ? "(capped)" : "");
LOGD((std::string("Water mobs : %d") + (isCaped(cMonster::mfWater)? "(capped)":"")).c_str(),m_MobFamilyCollecter.getNumberOfCollectedMobs(cMonster::mfWater)); LOGD("Water mobs : %d %s", m_MobFamilyCollecter.GetNumberOfCollectedMobs(cMonster::mfWater), IsCapped(cMonster::mfWater) ? "(capped)" : "");
LOGD((std::string("Passive mobs : %d") + (isCaped(cMonster::mfPassive)?"(capped)":"")).c_str(),m_MobFamilyCollecter.getNumberOfCollectedMobs(cMonster::mfPassive)); LOGD("Passive mobs : %d %s", m_MobFamilyCollecter.GetNumberOfCollectedMobs(cMonster::mfPassive), IsCapped(cMonster::mfPassive) ? "(capped)" : "");
} }

View File

@ -4,55 +4,63 @@
#include "MobProximityCounter.h" #include "MobProximityCounter.h"
#include "MobFamilyCollecter.h" #include "MobFamilyCollecter.h"
// fwd:
class cChunk; class cChunk;
class cMonster; 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 /** This class is used to collect information, for each Mob, what is the distance of the closest player
// as side effect 2 : it also know the caps for mobs number and can compare census to this numbers 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 class cMobCensus
{ {
public : public:
cMobCensus(); typedef const std::map<cMonster::eFamily,int> tMobSpawnRate;
static tMobSpawnRate & m_SpawnRate(void);
/// Returns the nested proximity counter
cMobProximityCounter & GetProximityCounter(void);
// 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);
/// Collect a mob - it's distance to player, it's family ...
void CollectMob(cMonster& a_Monster, cChunk& a_Chunk, double a_Distance);
/// 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 : protected :
cMobProximityCounter m_ProximityCounter; cMobProximityCounter m_ProximityCounter;
cMobFamilyCollecter m_MobFamilyCollecter; cMobFamilyCollecter m_MobFamilyCollecter;
typedef const std::map<cMonster::eFamily,int> tCapMultipliersMap; typedef const std::map<cMonster::eFamily,int> tCapMultipliersMap;
static tCapMultipliersMap& m_CapMultipliers();
std::set<cChunk*> m_EligibleForSpawnChunks; static tCapMultipliersMap & m_CapMultipliers(void);
// count the chunks that are elligible to spawn (for now, the loaded valide not null chunks) std::set<cChunk *> m_EligibleForSpawnChunks;
int getChunkNb();
public: /// Returns the number of chunks that are elligible for spawning (for now, the loaded, valid chunks)
typedef const std::map<cMonster::eFamily,int> tMobSpawnRate; int GetNumChunks();
static tMobSpawnRate& m_SpawnRate();
// return the nested proximity counter static tCapMultipliersMap CapMultiplierInitializerBeforeCx11(void);
cMobProximityCounter& getProximityCounter(); static tCapMultipliersMap MobSpawnRateInitializerBeforeCx11(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);
// 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);
// log the results of census
void logd();
protected :
static tCapMultipliersMap CapMultiplierInitializerBeforeCx11();
static tCapMultipliersMap MobSpawnRateInitializerBeforeCx11();
};

View File

@ -6,7 +6,7 @@
cMobFamilyCollecter::tMobFamilyList cMobFamilyCollecter::initMobFamilyBeforeCx11() cMobFamilyCollecter::tMobFamilyList cMobFamilyCollecter::InitMobFamilyBeforeCx11(void)
{ {
std::set<cMonster::eFamily> toReturn; std::set<cMonster::eFamily> toReturn;
toReturn.insert(cMonster::mfHostile); toReturn.insert(cMonster::mfHostile);
@ -15,19 +15,37 @@ cMobFamilyCollecter::tMobFamilyList cMobFamilyCollecter::initMobFamilyBeforeCx11
toReturn.insert(cMonster::mfWater); toReturn.insert(cMonster::mfWater);
return toReturn; 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; return *AllFamilies;
} }
void cMobFamilyCollecter::CollectMob(cMonster& a_Monster) void cMobFamilyCollecter::CollectMob(cMonster& a_Monster)
{ {
cMonster::eFamily MobFamily = a_Monster.GetMobFamily(); cMonster::eFamily MobFamily = a_Monster.GetMobFamily();
m_Mobs[MobFamily].insert(&a_Monster); 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(); return m_Mobs[a_Family].size();
} }

View File

@ -6,35 +6,44 @@
#include "BlockID.h" #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 #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; 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 class cMobFamilyCollecter
{ {
protected :
std::map<cMonster::eFamily,std::set<cMonster*> > m_Mobs;
public :
// collect a mob
void CollectMob(cMonster& a_Monster);
// return the number of mobs for this family
int getNumberOfCollectedMobs(cMonster::eFamily a_Family);
public : public :
typedef const std::set<cMonster::eFamily> tMobFamilyList; typedef const std::set<cMonster::eFamily> tMobFamilyList;
static tMobFamilyList& m_AllFamilies();
public :
typedef const std::map<cMonster::eFamily,int> tMobSpawRate; typedef const std::map<cMonster::eFamily,int> tMobSpawRate;
static tMobSpawRate& m_SpawnRate();
// collect a mob
void CollectMob(cMonster & a_Monster);
// 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 : protected :
static tMobFamilyList initMobFamilyBeforeCx11(); std::map<cMonster::eFamily, std::set<cMonster*> > m_Mobs;
static tMobSpawRate initMobSpawnRateBeforeCx11();
}; static tMobFamilyList InitMobFamilyBeforeCx11(void);
static tMobSpawRate InitMobSpawnRateBeforeCx11(void);
} ;

View File

@ -732,21 +732,19 @@ void cWorld::TickMobs(float a_Dt)
{ {
cMobCensus::tMobSpawnRate::const_iterator spawnrate = cMobCensus::m_SpawnRate().find(*itr); cMobCensus::tMobSpawnRate::const_iterator spawnrate = cMobCensus::m_SpawnRate().find(*itr);
// hostile mobs are spawned more often // 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; m_LastSpawnMonster[*itr] = m_WorldAge;
// each megatype of mob has it's own cap // each megatype of mob has it's own cap
if (!(MobCensus.isCaped(*itr))) if (!(MobCensus.IsCapped(*itr)))
{ {
if (m_bAnimals) if (m_bAnimals)
{ {
cMobSpawner Spawner(*itr,m_AllowedMobs); cMobSpawner Spawner(*itr,m_AllowedMobs);
if (Spawner.CanSpawnSomething()) if (Spawner.CanSpawnSomething())
{ {
m_ChunkMap->SpawnMobs(Spawner); m_ChunkMap->SpawnMobs(Spawner);
// do the spawn // 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); SpawnMobFinalize(*itr2);
@ -758,14 +756,14 @@ void cWorld::TickMobs(float a_Dt)
} }
// move close mobs // 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++) 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 // 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++) for(cMobProximityCounter::tDistanceToMonster::const_iterator itr = allTooFarMobs.m_Begin; itr != allTooFarMobs.m_End; itr++)
{ {
itr->second.m_Monster.Destroy(true); itr->second.m_Monster.Destroy(true);