TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Formulas.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef TRINITY_FORMULAS_H
20 #define TRINITY_FORMULAS_H
21 
22 #include "World.h"
23 #include "SharedDefines.h"
24 #include "ScriptMgr.h"
25 #include "Player.h"
26 
27 namespace Trinity
28 {
29  namespace Honor
30  {
31  inline float hk_honor_at_level_f(uint8 level, float multiplier = 1.0f)
32  {
33  float honor = multiplier * level * 1.55f;
34  sScriptMgr->OnHonorCalculation(honor, level, multiplier);
35  return honor;
36  }
37 
38  inline uint32 hk_honor_at_level(uint8 level, float multiplier = 1.0f)
39  {
40  return uint32(ceil(hk_honor_at_level_f(level, multiplier)));
41  }
42  } // namespace Trinity::Honor
43 
44  namespace XP
45  {
46  inline uint8 GetGrayLevel(uint8 pl_level)
47  {
48  uint8 level;
49 
50  if (pl_level < 7)
51  level = 0;
52  else if (pl_level < 35)
53  {
54  uint8 count = 0;
55  for (int i = 15; i <= pl_level; ++i)
56  if (i % 5 == 0) ++count;
57 
58  level = (pl_level - 7) - (count - 1);
59  }
60  else
61  level = pl_level - 10;
62 
63  sScriptMgr->OnGrayLevelCalculation(level, pl_level);
64  return level;
65  }
66 
67  inline XPColorChar GetColorCode(uint8 pl_level, uint8 mob_level)
68  {
69  XPColorChar color;
70 
71  if (mob_level >= pl_level + 5)
72  color = XP_RED;
73  else if (mob_level >= pl_level + 3)
74  color = XP_ORANGE;
75  else if (mob_level >= pl_level - 2)
76  color = XP_YELLOW;
77  else if (mob_level > GetGrayLevel(pl_level))
78  color = XP_GREEN;
79  else
80  color = XP_GRAY;
81 
82  sScriptMgr->OnColorCodeCalculation(color, pl_level, mob_level);
83  return color;
84  }
85 
86  inline uint8 GetZeroDifference(uint8 pl_level)
87  {
88  uint8 diff;
89 
90  if (pl_level < 4)
91  diff = 5;
92  else if (pl_level < 10)
93  diff = 6;
94  else if (pl_level < 12)
95  diff = 7;
96  else if (pl_level < 16)
97  diff = 8;
98  else if (pl_level < 20)
99  diff = 9;
100  else if (pl_level < 30)
101  diff = 11;
102  else if (pl_level < 40)
103  diff = 12;
104  else if (pl_level < 45)
105  diff = 13;
106  else if (pl_level < 50)
107  diff = 14;
108  else if (pl_level < 55)
109  diff = 15;
110  else if (pl_level < 60)
111  diff = 16;
112  else
113  diff = 17;
114 
115  sScriptMgr->OnZeroDifferenceCalculation(diff, pl_level);
116  return diff;
117  }
118 
119  inline uint32 BaseGain(uint8 pl_level, uint8 mob_level)
120  {
121  uint32 baseGain;
122 
123  GtOCTLevelExperienceEntry const* BaseExpPlayer = sGtOCTLevelExperienceStore.EvaluateTable(pl_level - 1, 1);
124  GtOCTLevelExperienceEntry const* BaseExpMob = sGtOCTLevelExperienceStore.EvaluateTable(mob_level - 1, 1);
125 
126  GtOCTLevelExperienceEntry const* CoefPlayer = sGtOCTLevelExperienceStore.EvaluateTable(pl_level - 1, 4);
127  GtOCTLevelExperienceEntry const* CoefMob = sGtOCTLevelExperienceStore.EvaluateTable(mob_level - 1, 4);
128 
129  if (mob_level >= pl_level)
130  {
131  uint8 nLevelDiff = mob_level - pl_level;
132  if (nLevelDiff > 4)
133  nLevelDiff = 4;
134 
135  baseGain = uint32(round(BaseExpPlayer->Data * (1 + 0.05f * nLevelDiff)));
136  }
137  else
138  {
139  uint8 gray_level = GetGrayLevel(pl_level);
140  if (mob_level > gray_level)
141  {
142  uint8 ZD = GetZeroDifference(pl_level);
143  baseGain = uint32(round(BaseExpMob->Data * ((1 - ((pl_level - mob_level) / float(ZD))) * (CoefMob->Data / CoefPlayer->Data))));
144  }
145  else
146  baseGain = 0;
147  }
148 
149  sScriptMgr->OnBaseGainCalculation(baseGain, pl_level, mob_level);
150  return baseGain;
151  }
152 
153  inline uint32 Gain(Player* player, Unit* u, bool isBattleGround = false)
154  {
155  Creature* creature = u->ToCreature();
156  uint32 gain = 0;
157 
158  if (!creature || (!creature->IsTotem() && !creature->IsPet() && !creature->IsCritter() &&
160  {
161  float xpMod = 1.0f;
162 
163  gain = BaseGain(player->getLevel(), u->getLevel());
164 
165  if (gain && creature)
166  {
167  // Players get only 10% xp for killing creatures of lower expansion levels than himself
168  if ((uint32(creature->GetCreatureTemplate()->expansion) < GetExpansionForLevel(player->getLevel())))
169  gain = uint32(round(gain / 10.0f));
170 
171  if (creature->isElite())
172  {
173  // Elites in instances have a 2.75x XP bonus instead of the regular 2x world bonus.
174  if (u->GetMap()->IsDungeon())
175  xpMod *= 2.75f;
176  else
177  xpMod *= 2.0f;
178  }
179 
180  xpMod *= creature->GetCreatureTemplate()->ModExperience;
181  }
182 
183  xpMod *= isBattleGround ? sWorld->getRate(RATE_XP_BG_KILL) : sWorld->getRate(RATE_XP_KILL);
184  if (creature && creature->m_PlayerDamageReq) // if players dealt less than 50% of the damage and were credited anyway (due to CREATURE_FLAG_EXTRA_NO_PLAYER_DAMAGE_REQ), scale XP gained appropriately (linear scaling)
185  xpMod *= 1.0f - 2.0f*creature->m_PlayerDamageReq / creature->GetMaxHealth();
186 
187  gain = uint32(gain * xpMod);
188  }
189 
190  sScriptMgr->OnGainCalculation(gain, player, u);
191  return gain;
192  }
193 
194  inline float xp_in_group_rate(uint32 count, bool isRaid)
195  {
196  float rate;
197 
198  if (isRaid)
199  {
200  // FIXME: Must apply decrease modifiers depending on raid size.
201  // set to < 1 to, so client will display raid related strings
202  rate = 0.99f;
203  }
204  else
205  {
206  switch (count)
207  {
208  case 0:
209  case 1:
210  case 2:
211  rate = 1.0f;
212  break;
213  case 3:
214  rate = 1.166f;
215  break;
216  case 4:
217  rate = 1.3f;
218  break;
219  case 5:
220  default:
221  rate = 1.4f;
222  }
223  }
224 
225  sScriptMgr->OnGroupRateCalculation(rate, count, isRaid);
226  return rate;
227  }
228  } // namespace Trinity::XP
229 
230  namespace Currency
231  {
233  {
234  if (rate <= 1500)
235  return 1350; // Default conquest points
236  else if (rate > 3000)
237  rate = 3000;
238 
239  // http://www.arenajunkies.com/topic/179536-conquest-point-cap-vs-personal-rating-chart/page__st__60#entry3085246
240  return uint32(1.4326 * ((1511.26 / (1 + 1639.28 / exp(0.00412 * rate))) + 850.15));
241  }
242 
244  {
245  // WowWiki: Battleground ratings receive a bonus of 22.2% to the cap they generate
246  return uint32((ConquestRatingCalculator(rate) * 1.222f) + 0.5f);
247  }
248  } // namespace Trinity::Currency
249 } // namespace Trinity
250 
251 #endif
Definition: SharedDefines.h:4682
uint32 GetExpansionForLevel(uint32 level)
Definition: DBCStores.cpp:632
Quat exp(const Quat &q)
Definition: Quat.h:729
int32 expansion
Definition: Creature.h:95
Definition: SharedDefines.h:4679
uint32 GetMaxHealth() const
Definition: Unit.h:1427
uint32 Gain(Player *player, Unit *u, bool isBattleGround=false)
Definition: Formulas.h:153
Map * GetMap() const
Definition: Object.h:543
double round(double f)
Definition: g3dmath.h:214
float ModExperience
Definition: Creature.h:137
Definition: SharedDefines.h:4681
Definition: World.h:403
XPColorChar
Definition: SharedDefines.h:4676
float Data
Definition: DBCStructure.h:594
uint32 ConquestRatingCalculator(uint32 rate)
Definition: Formulas.h:232
bool IsDungeon() const
Definition: Map.h:395
uint8 GetGrayLevel(uint8 pl_level)
Definition: Formulas.h:46
Definition: Creature.h:467
uint32 flags_extra
Definition: Creature.h:142
#define sWorld
Definition: World.h:887
XPColorChar GetColorCode(uint8 pl_level, uint8 mob_level)
Definition: Formulas.h:67
uint8 getLevel() const
Definition: Unit.h:1408
uint32 m_PlayerDamageReq
Definition: Creature.h:697
CreatureTemplate const * GetCreatureTemplate() const
Definition: Creature.h:558
Definition: SharedDefines.h:4678
Definition: World.h:402
uint32_t uint32
Definition: Define.h:150
Definition: SharedDefines.h:4680
bool IsPet() const
Definition: Unit.h:1403
uint32 BgConquestRatingCalculator(uint32 rate)
Definition: Formulas.h:243
Definition: Creature.h:48
GameTable< GtOCTLevelExperienceEntry > sGtOCTLevelExperienceStore(GtOCTLevelExperiencefmt)
bool IsTotem() const
Definition: Unit.h:1405
bool IsCritter() const
Definition: Unit.h:1580
Creature * ToCreature()
Definition: Object.h:194
float hk_honor_at_level_f(uint8 level, float multiplier=1.0f)
Definition: Formulas.h:31
uint8 GetZeroDifference(uint8 pl_level)
Definition: Formulas.h:86
bool isElite() const
Definition: Creature.cpp:1824
uint32 hk_honor_at_level(uint8 level, float multiplier=1.0f)
Definition: Formulas.h:38
uint8_t uint8
Definition: Define.h:152
#define sScriptMgr
Definition: ScriptMgr.h:837
uint32_t uint32
Definition: g3dmath.h:168
Definition: Common.h:172
static Vector3int16 ceil(const Vector3 &v)
Definition: Vector3int16.cpp:55
Definition: DBCStructure.h:592
uint32 BaseGain(uint8 pl_level, uint8 mob_level)
Definition: Formulas.h:119
Definition: Unit.h:1305
float xp_in_group_rate(uint32 count, bool isRaid)
Definition: Formulas.h:194