Cuberite
A lightweight, fast and extensible game server for Minecraft
MobProximityCounter.cpp
Go to the documentation of this file.
1 
2 #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
3 
4 #include "MobProximityCounter.h"
5 
6 #include "Entities/Entity.h"
7 #include "Chunk.h"
8 
9 
10 
11 
12 
13 void cMobProximityCounter::CollectMob(cEntity & a_Monster, cChunk & a_Chunk, double a_Distance)
14 {
15  // LOGD("Collecting monster %s, with distance %f", a_Monster->GetClass(), a_Distance);
16  tMonsterToDistance::iterator it = m_MonsterToDistance.find(&a_Monster);
17  if (it == m_MonsterToDistance.end())
18  {
19  sDistanceAndChunk newDistanceAndChunk(a_Distance, a_Chunk);
20  std::pair<tMonsterToDistance::iterator, bool> result = m_MonsterToDistance.insert(tMonsterToDistance::value_type(&a_Monster, newDistanceAndChunk));
21  if (!result.second)
22  {
23  ASSERT(!"A collected Monster was not found inside distance map using find(), but insert() said there already is a key for it");
24  }
25  }
26  else
27  {
28  if (a_Distance < it->second.m_Distance)
29  {
30  it->second.m_Distance = a_Distance;
31  it->second.m_Chunk = &a_Chunk;
32  }
33  }
34 
35  m_EligibleForSpawnChunks.insert(&a_Chunk);
36 
37 }
38 
39 
40 
41 
42 
44 {
45  for (tMonsterToDistance::const_iterator itr = m_MonsterToDistance.begin(); itr != m_MonsterToDistance.end(); ++itr)
46  {
47  m_DistanceToMonster.insert(tDistanceToMonster::value_type(itr->second.m_Distance, sMonsterAndChunk(*itr->first, *itr->second.m_Chunk)));
48  }
49 }
50 
51 
52 
53 
54 
56 {
57  sIterablePair toReturn;
58  toReturn.m_Count = 0;
59  toReturn.m_Begin = m_DistanceToMonster.end();
60  toReturn.m_End = m_DistanceToMonster.end();
61 
62  a_DistanceMin *= a_DistanceMin;// this is because is use square distance
63  a_DistanceMax *= a_DistanceMax;
64 
65  if (m_DistanceToMonster.empty())
66  {
67  convertMaps();
68  }
69 
70  for (tDistanceToMonster::const_iterator itr = m_DistanceToMonster.begin(); itr != m_DistanceToMonster.end(); ++itr)
71  {
72  if (toReturn.m_Begin == m_DistanceToMonster.end())
73  {
74  if ((a_DistanceMin == 1.0) || (itr->first > a_DistanceMin))
75  {
76  toReturn.m_Begin = itr; // This is the first one with distance > a_DistanceMin;
77  }
78  }
79 
80  if (toReturn.m_Begin != m_DistanceToMonster.end())
81  {
82  if ((a_DistanceMax != 1.0) && (itr->first > a_DistanceMax))
83  {
84  toReturn.m_End = itr; // This is just after the last one with distance < a_DistanceMax
85  // Note : if we are not going through this, it's ok, toReturn.m_End will be end();
86  break;
87  }
88  else
89  {
90  toReturn.m_Count ++;
91  }
92  }
93  }
94  return toReturn;
95 }
#define ASSERT(x)
Definition: Globals.h:276
Definition: Chunk.h:36
Definition: Entity.h:76
void CollectMob(cEntity &a_Monster, cChunk &a_Chunk, double a_Distance)
tDistanceToMonster m_DistanceToMonster
sIterablePair getMobWithinThosesDistances(double a_DistanceMin, double a_DistanceMax)
std::set< cChunk * > m_EligibleForSpawnChunks
tMonsterToDistance m_MonsterToDistance
tDistanceToMonster::const_iterator m_Begin
tDistanceToMonster::const_iterator m_End