Cuberite
A lightweight, fast and extensible game server for Minecraft
Statistics.cpp
Go to the documentation of this file.
1 
2 // Statistics.cpp
3 
4 #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
5 
6 #include "Statistics.h"
7 
8 
9 
11 {
12  // The order must match the order of enum eStatistic
13 
14  // https://minecraft.gamepedia.com/Achievements
15 
16  /* Type | Name | Prerequisite */
17  cStatInfo(achOpenInv, "achievement.openInventory"),
18  cStatInfo(achMineWood, "achievement.mineWood", achOpenInv),
19  cStatInfo(achCraftWorkbench, "achievement.buildWorkBench", achMineWood),
20  cStatInfo(achCraftPickaxe, "achievement.buildPickaxe", achCraftWorkbench),
21  cStatInfo(achCraftFurnace, "achievement.buildFurnace", achCraftPickaxe),
22  cStatInfo(achAcquireIron, "achievement.acquireIron", achCraftFurnace),
23  cStatInfo(achCraftHoe, "achievement.buildHoe", achCraftWorkbench),
24  cStatInfo(achMakeBread, "achievement.makeBread", achCraftHoe),
25  cStatInfo(achBakeCake, "achievement.bakeCake", achCraftHoe),
26  cStatInfo(achCraftBetterPick, "achievement.buildBetterPickaxe", achCraftPickaxe),
27  cStatInfo(achCookFish, "achievement.cookFish", achAcquireIron),
28  cStatInfo(achOnARail, "achievement.onARail", achAcquireIron),
29  cStatInfo(achCraftSword, "achievement.buildSword", achCraftWorkbench),
30  cStatInfo(achKillMonster, "achievement.killEnemy", achCraftSword),
31  cStatInfo(achKillCow, "achievement.killCow", achCraftSword),
32  cStatInfo(achFlyPig, "achievement.flyPig", achKillCow),
33  cStatInfo(achSnipeSkeleton, "achievement.snipeSkeleton", achKillMonster),
34  cStatInfo(achDiamonds, "achievement.diamonds", achAcquireIron),
35  cStatInfo(achEnterPortal, "achievement.portal", achDiamonds),
36  cStatInfo(achReturnToSender, "achievement.ghast", achEnterPortal),
37  cStatInfo(achBlazeRod, "achievement.blazeRod", achEnterPortal),
38  cStatInfo(achBrewPotion, "achievement.potion", achBlazeRod),
39  cStatInfo(achEnterTheEnd, "achievement.theEnd", achBlazeRod),
40  cStatInfo(achDefeatDragon, "achievement.theEnd2", achEnterTheEnd),
41  cStatInfo(achCraftEnchantTable, "achievement.enchantments", achDiamonds),
42  cStatInfo(achOverkill, "achievement.overkill", achCraftEnchantTable),
43  cStatInfo(achBookshelf, "achievement.bookcase", achCraftEnchantTable),
44  cStatInfo(achExploreAllBiomes, "achievement.exploreAllBiomes", achEnterTheEnd),
45  cStatInfo(achSpawnWither, "achievement.spawnWither", achDefeatDragon),
46  cStatInfo(achKillWither, "achievement.killWither", achSpawnWither),
47  cStatInfo(achFullBeacon, "achievement.fullBeacon", achKillWither),
48  cStatInfo(achBreedCow, "achievement.breedCow", achKillCow),
49  cStatInfo(achThrowDiamonds, "achievement.diamondsToYou", achDiamonds),
50 
51  // https://minecraft.gamepedia.com/Statistics
52 
53  /* Type | Name */
54  cStatInfo(statGamesQuit, "stat.leaveGame"),
55  cStatInfo(statMinutesPlayed, "stat.playOneMinute"),
56  cStatInfo(statDistWalked, "stat.walkOneCm"),
57  cStatInfo(statDistSwum, "stat.swimOneCm"),
58  cStatInfo(statDistFallen, "stat.fallOneCm"),
59  cStatInfo(statDistClimbed, "stat.climbOneCm"),
60  cStatInfo(statDistFlown, "stat.flyOneCm"),
61  cStatInfo(statDistDove, "stat.diveOneCm"),
62  cStatInfo(statDistMinecart, "stat.minecartOneCm"),
63  cStatInfo(statDistBoat, "stat.boatOneCm"),
64  cStatInfo(statDistPig, "stat.pigOneCm"),
65  cStatInfo(statDistHorse, "stat.horseOneCm"),
66  cStatInfo(statJumps, "stat.jump"),
67  cStatInfo(statItemsDropped, "stat.drop"),
68  cStatInfo(statDamageDealt, "stat.damageDealt"),
69  cStatInfo(statDamageTaken, "stat.damageTaken"),
70  cStatInfo(statDeaths, "stat.deaths"),
71  cStatInfo(statMobKills, "stat.mobKills"),
72  cStatInfo(statAnimalsBred, "stat.animalsBred"),
73  cStatInfo(statPlayerKills, "stat.playerKills"),
74  cStatInfo(statFishCaught, "stat.fishCaught"),
75  cStatInfo(statJunkFished, "stat.junkFished"),
76  cStatInfo(statTreasureFished, "stat.treasureFished")
77 };
78 
79 
80 
81 
82 
85  , m_Depends(statInvalid)
86 {}
87 
88 
89 
90 
91 
92 cStatInfo::cStatInfo(const eStatistic a_Type, const AString & a_Name, const eStatistic a_Depends)
93  : m_Type(a_Type)
94  , m_Name(a_Name)
95  , m_Depends(a_Depends)
96 {}
97 
98 
99 
100 
101 
102 const AString & cStatInfo::GetName(const eStatistic a_Type)
103 {
104  ASSERT((a_Type > statInvalid) && (a_Type < statCount));
105 
106  return ms_Info[a_Type].m_Name;
107 }
108 
109 
110 
111 
112 
114 {
115  for (unsigned int i = 0; i < ARRAYCOUNT(ms_Info); ++i)
116  {
117  if (NoCaseCompare(ms_Info[i].m_Name, a_Name) == 0)
118  {
119  return ms_Info[i].m_Type;
120  }
121  }
122 
123  return statInvalid;
124 }
125 
126 
127 
128 
129 
131 {
132  ASSERT((a_Type > statInvalid) && (a_Type < statCount));
133 
134  return ms_Info[a_Type].m_Depends;
135 }
136 
137 
138 
139 
140 
142 {
143  Reset();
144 }
145 
146 
147 
148 
149 
151 {
152  ASSERT((a_Stat > statInvalid) && (a_Stat < statCount));
153 
154  return m_MainStats[a_Stat];
155 }
156 
157 
158 
159 
160 
161 void cStatManager::SetValue(const eStatistic a_Stat, const StatValue a_Value)
162 {
163  ASSERT((a_Stat > statInvalid) && (a_Stat < statCount));
164 
165  m_MainStats[a_Stat] = a_Value;
166 }
167 
168 
169 
170 
171 
173 {
174  ASSERT((a_Stat > statInvalid) && (a_Stat < statCount));
175 
176  m_MainStats[a_Stat] += a_Delta;
177 
178  return m_MainStats[a_Stat];
179 }
180 
181 
182 
183 
184 
186 {
187  for (unsigned int i = 0; i < static_cast<unsigned int>(statCount); ++i)
188  {
189  m_MainStats[i] = 0;
190  }
191 }
192 
193 
194 
195 
196 
StatValue GetValue(const eStatistic a_Stat) const
Return the value of the specified stat.
Definition: Statistics.cpp:150
static eStatistic GetPrerequisite(const eStatistic a_Type)
Returns stat prerequisite.
Definition: Statistics.cpp:130
static cStatInfo ms_Info[statCount]
Definition: Statistics.h:113
StatValue AddValue(const eStatistic a_Stat, const StatValue a_Delta=1)
Increments the specified stat.
Definition: Statistics.cpp:172
eStatistic m_Type
Definition: Statistics.h:107
Class used to store and query statistic-related information.
Definition: Statistics.h:88
int NoCaseCompare(const AString &s1, const AString &s2)
Case-insensitive string comparison.
#define ASSERT(x)
Definition: Globals.h:335
static eStatistic GetType(const AString &a_Name)
Name -> Type.
Definition: Statistics.cpp:113
std::string AString
Definition: StringUtils.h:13
eStatistic
Definition: Statistics.h:13
AString m_Name
Definition: Statistics.h:109
eStatistic m_Depends
Definition: Statistics.h:111
eMonsterType m_Type
Definition: Monster.cpp:33
void SetValue(const eStatistic a_Stat, const StatValue a_Value)
Set the value of the specified stat.
Definition: Statistics.cpp:161
int StatValue
Definition: Statistics.h:120
#define ARRAYCOUNT(X)
Evaluates to the number of elements in an array (compile-time!)
Definition: Globals.h:290
void Reset()
Reset everything.
Definition: Statistics.cpp:185
static const AString & GetName(const eStatistic a_Type)
Type -> Name.
Definition: Statistics.cpp:102