TrinityCore
Loading...
Searching...
No Matches
RestMgr.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "RestMgr.h"
19#include "GameTime.h"
20#include "Log.h"
21#include "Player.h"
22#include "Random.h"
23#include "World.h"
24#include "WorldSession.h"
25
26RestMgr::RestMgr(Player* player) : _player(player), _restTime(0), _restFlagMask(0)
27{
28 for (uint8 i = REST_TYPE_XP; i < REST_TYPE_MAX; i++)
29 _restBonus[i] = 0;
30}
31
32void RestMgr::SetRestBonus(RestTypes restType, float restBonus)
33{
34 int32 next_level_xp;
35 bool affectedByRaF = false;
36
37 switch (restType)
38 {
39 case REST_TYPE_XP:
40 // Reset restBonus (XP only) for max level players
41 if (_player->GetLevel() >= sWorld->getIntConfig(CONFIG_MAX_PLAYER_LEVEL))
42 restBonus = 0;
43
44 next_level_xp = _player->m_activePlayerData->NextLevelXP;
45 affectedByRaF = true;
46 break;
47 case REST_TYPE_HONOR:
48 // Reset restBonus (Honor only) for players with max honor level.
50 restBonus = 0;
51
52 next_level_xp = _player->m_activePlayerData->HonorNextLevel;
53 break;
54 default:
55 return;
56 }
57
58 float rest_bonus_max = float(next_level_xp) * 1.5f / 2;
59
60 if (restBonus < 0)
61 restBonus = 0;
62
63 if (restBonus > rest_bonus_max)
64 restBonus = rest_bonus_max;
65
66 uint32 oldBonus = uint32(_restBonus[restType]);
67 _restBonus[restType] = restBonus;
68
69 PlayerRestState oldRestState = static_cast<PlayerRestState>(*_player->m_activePlayerData->RestInfo[restType].StateID);
71
72 if (affectedByRaF && _player->GetsRecruitAFriendBonus(true) && (_player->GetSession()->IsARecruiter() || _player->GetSession()->GetRecruiterId() != 0))
73 newRestState = REST_STATE_RAF_LINKED;
74 else if (_restBonus[restType] >= 1)
75 newRestState = REST_STATE_RESTED;
76
77 if (oldBonus == uint32(restBonus) && oldRestState == newRestState)
78 return;
79
80 // update data for client
81 _player->SetRestThreshold(restType, uint32(_restBonus[restType]));
82 _player->SetRestState(restType, newRestState);
83}
84
85void RestMgr::AddRestBonus(RestTypes restType, float restBonus)
86{
87 // Don't add extra rest bonus to max level players. Note: Might need different condition in next expansion for honor XP (PLAYER_LEVEL_MIN_HONOR perhaps).
88 if (_player->GetLevel() >= sWorld->getIntConfig(CONFIG_MAX_PLAYER_LEVEL))
89 restBonus = 0;
90
91 float totalRestBonus = GetRestBonus(restType) + restBonus;
92 SetRestBonus(restType, totalRestBonus);
93}
94
96{
97 uint32 oldRestMask = _restFlagMask;
98 _restFlagMask |= restFlag;
99
100 if (!oldRestMask && _restFlagMask) // only set flag/time on the first rest state
101 {
104 }
105}
106
108{
109 uint32 oldRestMask = _restFlagMask;
110 _restFlagMask &= ~restFlag;
111
112 if (oldRestMask && !_restFlagMask) // only remove flag/time on the last rest state remove
113 {
114 _restTime = 0;
116 }
117}
118
120{
121 uint32 rested_bonus = uint32(GetRestBonus(restType)); // xp for each rested bonus
122
123 if (rested_bonus > xp) // max rested_bonus == xp or (r+x) = 200% xp
124 rested_bonus = xp;
125
126 uint32 rested_loss = rested_bonus;
127 if (restType == REST_TYPE_XP)
129
130 SetRestBonus(restType, GetRestBonus(restType) - rested_loss);
131
132 TC_LOG_DEBUG("entities.player", "RestMgr::GetRestBonus: Player '{}' ({}) gain {} xp (+{} Rested Bonus). Rested points={}", _player->GetGUID().ToString(), _player->GetName(), xp + rested_bonus, rested_bonus, GetRestBonus(restType));
133 return rested_bonus;
134}
135
136void RestMgr::Update(time_t now)
137{
138 if (roll_chance(3) && _restTime > 0) // freeze update
139 {
140 time_t timeDiff = now - _restTime;
141 if (timeDiff >= 10)
142 {
143 _restTime = now;
144
145 float bubble = 0.125f * sWorld->getRate(RATE_REST_INGAME);
147 }
148 }
149}
150
151void RestMgr::LoadRestBonus(RestTypes restType, PlayerRestState state, float restBonus)
152{
153 _restBonus[restType] = restBonus;
154 _player->SetRestState(restType, state);
155 _player->SetRestThreshold(restType, uint32(restBonus));
156}
157
158float RestMgr::CalcExtraPerSec(RestTypes restType, float bubble) const
159{
160 switch (restType)
161 {
162 case REST_TYPE_HONOR:
163 return float(_player->m_activePlayerData->HonorNextLevel) / 72000.0f * bubble;
164 case REST_TYPE_XP:
165 return float(_player->m_activePlayerData->NextLevelXP) / 72000.0f * bubble;
166 default:
167 return 0.0f;
168 }
169}
uint8_t uint8
Definition Define.h:156
int32_t int32
Definition Define.h:150
uint32_t uint32
Definition Define.h:154
#define TC_LOG_DEBUG(filterType__, message__,...)
Definition Log.h:181
@ PLAYER_FLAGS_RESTING
Definition Player.h:525
bool roll_chance(T chance)
Definition Random.h:55
PlayerRestState
Definition RestMgr.h:46
@ REST_STATE_NORMAL
Definition RestMgr.h:48
@ REST_STATE_RESTED
Definition RestMgr.h:47
@ REST_STATE_RAF_LINKED
Definition RestMgr.h:49
RestFlag
Definition RestMgr.h:53
RestTypes
Definition RestMgr.h:28
@ REST_TYPE_MAX
Definition RestMgr.h:31
@ REST_TYPE_XP
Definition RestMgr.h:29
@ REST_TYPE_HONOR
Definition RestMgr.h:30
@ SPELL_AURA_MOD_RESTED_XP_CONSUMPTION
T AddPct(T &base, U pct)
Definition Util.h:85
ObjectGuid const & GetGUID() const
Definition BaseEntity.h:163
std::string ToString() const
void SetPlayerFlag(PlayerFlags flags)
Definition Player.h:2911
void RemovePlayerFlag(PlayerFlags flags)
Definition Player.h:2912
void SetRestThreshold(RestTypes type, uint32 threshold)
Definition Player.h:2898
UF::UpdateField< UF::ActivePlayerData, int32(WowCS::EntityFragment::CGObject), TYPEID_ACTIVE_PLAYER > m_activePlayerData
Definition Player.h:3062
WorldSession * GetSession() const
Definition Player.h:2272
bool GetsRecruitAFriendBonus(bool forXP)
Definition Player.cpp:26371
bool IsMaxHonorLevel() const
Definition Player.h:2470
void SetRestState(RestTypes type, PlayerRestState state)
Definition Player.h:2891
float _restBonus[REST_TYPE_MAX]
Definition RestMgr.h:96
uint32 GetRestBonusFor(RestTypes restType, uint32 xp)
Definition RestMgr.cpp:119
time_t _restTime
Definition RestMgr.h:94
void Update(time_t now)
Definition RestMgr.cpp:136
Player * _player
Definition RestMgr.h:93
void AddRestBonus(RestTypes restType, float restBonus)
Definition RestMgr.cpp:85
void SetRestFlag(RestFlag restFlag)
Definition RestMgr.cpp:95
void SetRestBonus(RestTypes restType, float restBonus)
Definition RestMgr.cpp:32
void LoadRestBonus(RestTypes restType, PlayerRestState state, float restBonus)
Definition RestMgr.cpp:151
uint32 _restFlagMask
Definition RestMgr.h:97
float GetRestBonus(RestTypes restType) const
Definition RestMgr.h:72
RestMgr(Player *player)
Definition RestMgr.cpp:26
float CalcExtraPerSec(RestTypes restType, float bubble) const
Definition RestMgr.cpp:158
void RemoveRestFlag(RestFlag restFlag)
Definition RestMgr.cpp:107
float GetTotalAuraModifier(AuraType auraType) const
Definition Unit.cpp:5069
uint8 GetLevel() const
Definition Unit.h:757
std::string const & GetName() const
Definition Object.h:342
uint32 GetRecruiterId() const
bool IsARecruiter() const
#define sWorld
Definition World.h:916
@ CONFIG_MAX_PLAYER_LEVEL
Definition World.h:262
@ RATE_REST_INGAME
Definition World.h:508
time_t GetGameTime()
Definition GameTime.cpp:52