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