TrinityCore
ReputationMgr.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 "ReputationMgr.h"
19#include "CharacterPackets.h"
20#include "DatabaseEnv.h"
21#include "DB2Stores.h"
22#include "Language.h"
23#include "Log.h"
24#include "ObjectMgr.h"
25#include "Player.h"
26#include "ReputationPackets.h"
27#include "ScriptMgr.h"
28#include "World.h"
29#include "WorldSession.h"
30
32{
35};
36
37std::set<int32> const ReputationMgr::ReputationRankThresholds =
38{
39 -42000,
40 // Hated
41 -6000,
42 // Hostile
43 -3000,
44 // Unfriendly
45 0,
46 // Neutral
47 3000,
48 // Friendly
49 9000,
50 // Honored
51 21000,
52 // Revered
53 42000
54 // Exalted
55};
56
59
60template<typename T, typename F, typename... Rest>
61static int32 ReputationToRankHelper(std::set<T, Rest...> const& thresholds, int32 standing, F thresholdExtractor)
62{
63 auto itr = thresholds.begin();
64 auto end = thresholds.end();
65 int32 rank = -1;
66 while (itr != end && standing >= thresholdExtractor(*itr))
67 {
68 ++rank;
69 ++itr;
70 }
71
72 return rank;
73}
74
76{
78 if (DB2Manager::FriendshipRepReactionSet const* friendshipReactions = sDB2Manager.GetFriendshipRepReactions(factionEntry->FriendshipRepID))
79 rank = ReputationToRankHelper(*friendshipReactions, standing, [](FriendshipRepReactionEntry const* frr) { return frr->ReactionThreshold; });
80 else
81 rank = ReputationToRankHelper(ReputationRankThresholds, standing, [](int32 threshold) { return threshold; });
82
83 return ReputationRank(rank);
84}
85
86FactionState const* ReputationMgr::GetState(FactionEntry const* factionEntry) const
87{
88 return factionEntry->CanHaveReputation() ? GetState(factionEntry->ReputationIndex) : nullptr;
89}
90
91bool ReputationMgr::IsAtWar(uint32 faction_id) const
92{
93 FactionEntry const* factionEntry = sFactionStore.LookupEntry(faction_id);
94
95 if (!factionEntry)
96 {
97 TC_LOG_ERROR("misc", "ReputationMgr::IsAtWar: Can't get AtWar flag of {} for unknown faction (faction id) #{}.", _player->GetName(), faction_id);
98 return false;
99 }
100
101 return IsAtWar(factionEntry);
102}
103
104bool ReputationMgr::IsAtWar(FactionEntry const* factionEntry) const
105{
106 if (!factionEntry)
107 return false;
108
109 if (FactionState const* factionState = GetState(factionEntry))
110 return factionState->Flags.HasFlag(ReputationFlags::AtWar);
111 return false;
112}
113
115{
116 FactionEntry const* factionEntry = sFactionStore.LookupEntry(faction_id);
117
118 if (!factionEntry)
119 {
120 TC_LOG_ERROR("misc", "ReputationMgr::GetReputation: Can't get reputation of {} for unknown faction (faction id) #{}.", _player->GetName(), faction_id);
121 return 0;
122 }
123
124 return GetReputation(factionEntry);
125}
126
128{
129 int32 dataIndex = GetFactionDataIndexForRaceAndClass(factionEntry);
130 if (dataIndex < 0)
131 return 0;
132
133 return factionEntry->ReputationBase[dataIndex];
134}
135
137{
138 if (DB2Manager::FriendshipRepReactionSet const* friendshipReactions = sDB2Manager.GetFriendshipRepReactions(factionEntry->FriendshipRepID))
139 return (*friendshipReactions->begin())->ReactionThreshold;
140
141 return *ReputationRankThresholds.begin();
142}
143
145{
146 if (ParagonReputationEntry const* paragonReputation = sDB2Manager.GetParagonReputation(factionEntry->ID))
147 {
148 // has reward quest, cap is just before threshold for another quest reward
149 // for example: if current reputation is 12345 and quests are given every 10000 and player has unclaimed reward
150 // then cap will be 19999
151
152 // otherwise cap is one theshold level larger
153 // if current reputation is 12345 and questa are given every 10000 and player does NOT have unclaimed reward
154 // then cap will be 29999
155
156 int32 reputation = GetReputation(factionEntry);
157 int32 cap = reputation + paragonReputation->LevelThreshold - reputation % paragonReputation->LevelThreshold - 1;
158
159 if (_player->GetQuestStatus(paragonReputation->QuestID) == QUEST_STATUS_NONE)
160 cap += paragonReputation->LevelThreshold;
161
162 return cap;
163 }
164
165 if (IsRenownReputation(factionEntry))
166 {
167 // Compared to a paragon reputation, DF renown reputations
168 // have a maximum value of 2500 which resets with each level of renown acquired.
169 // We calculate the total reputation necessary to raise the renown to the maximum
170 return GetRenownMaxLevel(factionEntry) * GetRenownLevelThreshold(factionEntry);
171 }
172
173 if (DB2Manager::FriendshipRepReactionSet const* friendshipReactions = sDB2Manager.GetFriendshipRepReactions(factionEntry->FriendshipRepID))
174 return (*friendshipReactions->rbegin())->ReactionThreshold;
175
176 int32 dataIndex = GetFactionDataIndexForRaceAndClass(factionEntry);
177 if (dataIndex >= 0)
178 return factionEntry->ReputationMax[dataIndex];
179
180 return *ReputationRankThresholds.rbegin();
181}
182
184{
185 // Faction without recorded reputation. Just ignore.
186 if (!factionEntry)
187 return 0;
188
189 if (FactionState const* state = GetState(factionEntry))
190 return GetBaseReputation(factionEntry) + state->Standing;
191
192 return 0;
193}
194
196{
197 int32 reputation = GetReputation(factionEntry);
198 return ReputationToRank(factionEntry, reputation);
199}
200
202{
203 int32 reputation = GetBaseReputation(factionEntry);
204 return ReputationToRank(factionEntry, reputation);
205}
206
207std::string ReputationMgr::GetReputationRankName(FactionEntry const* factionEntry) const
208{
209 ReputationRank rank = GetRank(factionEntry);
210 if (!factionEntry->FriendshipRepID)
211 return sObjectMgr->GetTrinityString(ReputationRankStrIndex[GetRank(factionEntry)], _player->GetSession()->GetSessionDbcLocale());
212
213 if (DB2Manager::FriendshipRepReactionSet const* friendshipReactions = sDB2Manager.GetFriendshipRepReactions(factionEntry->FriendshipRepID))
214 {
215 auto itr = friendshipReactions->begin();
216 std::advance(itr, uint32(rank));
217 return (*itr)->Reaction[_player->GetSession()->GetSessionDbcLocale()];
218 }
219
220 return "";
221}
222
224{
225 return GetForcedRankIfAny(factionTemplateEntry->Faction);
226}
227
229{
230 if (sDB2Manager.GetParagonReputation(factionEntry->ID))
231 return true;
232
233 return false;
234}
235
237{
238 return GetParagonLevel(sFactionStore.LookupEntry(paragonFactionId));
239}
240
241int32 ReputationMgr::GetParagonLevel(FactionEntry const* paragonFactionEntry) const
242{
243 if (!paragonFactionEntry)
244 return 0;
245
246 if (ParagonReputationEntry const* paragonReputation = sDB2Manager.GetParagonReputation(paragonFactionEntry->ID))
247 return GetReputation(paragonFactionEntry) / paragonReputation->LevelThreshold;
248
249 return 0;
250}
251
253{
254 if (!IsRenownReputation(factionEntry))
255 return false;
256
257 return GetRenownLevel(factionEntry) >= GetRenownMaxLevel(factionEntry);
258}
259
260bool ReputationMgr::IsRenownReputation(FactionEntry const* factionEntry) const
261{
262 return factionEntry->RenownCurrencyID > 0;
263}
264
265int32 ReputationMgr::GetRenownLevel(FactionEntry const* renownFactionEntry) const
266{
267 if (!renownFactionEntry)
268 return 0;
269
270 if (CurrencyTypesEntry const* currency = sCurrencyTypesStore.LookupEntry(renownFactionEntry->RenownCurrencyID))
271 return _player->GetCurrencyQuantity(currency->ID);
272
273 return 0;
274}
275
277{
278 if (!renownFactionEntry || !IsRenownReputation(renownFactionEntry))
279 return 0;
280
281 int32 dataIndex = GetFactionDataIndexForRaceAndClass(renownFactionEntry);
282 if (dataIndex >= 0)
283 return renownFactionEntry->ReputationMax[dataIndex];
284
285 return 0;
286}
287
289{
290 if (!renownFactionEntry)
291 return 0;
292
293 if (CurrencyTypesEntry const* currency = sCurrencyTypesStore.LookupEntry(renownFactionEntry->RenownCurrencyID))
294 return _player->GetCurrencyMaxQuantity(currency);
295
296 return 0;
297}
298
300{
301 if (apply)
302 _forcedReactions[faction_id] = rank;
303 else
304 _forcedReactions.erase(faction_id);
305}
306
308{
309 ReputationFlags flags = [&]()
310 {
311 int32 dataIndex = GetFactionDataIndexForRaceAndClass(factionEntry);
312 if (dataIndex < 0)
314
315 return static_cast<ReputationFlags>(factionEntry->ReputationFlags[dataIndex]);
316 }();
317
318 if (sDB2Manager.GetParagonReputation(factionEntry->ID))
320
321 return flags;
322}
323
325{
327 setForcedReactions.Reactions.resize(_forcedReactions.size());
328
329 std::size_t i = 0;
330 for (ForcedReactions::const_iterator itr = _forcedReactions.begin(); itr != _forcedReactions.end(); ++itr)
331 {
332 WorldPackets::Reputation::ForcedReaction& forcedReaction = setForcedReactions.Reactions[i++];
333 forcedReaction.Faction = int32(itr->first);
334 forcedReaction.Reaction = int32(itr->second);
335 }
336
337 _player->SendDirectMessage(setForcedReactions.Write());
338}
339
341{
343 setFactionStanding.BonusFromAchievementSystem = 0.0f;
344
345 auto getStandingForPacket = [](FactionState const* state)
346 {
347 return state->VisualStandingIncrease ? state->VisualStandingIncrease : state->Standing;
348 };
349
350 if (faction)
351 setFactionStanding.Faction.emplace_back(int32(faction->ReputationListID), getStandingForPacket(faction));
352
353 for (auto& [reputationIndex, state] : _factions)
354 {
355 if (state.needSend)
356 {
357 state.needSend = false;
358 if (!faction || state.ReputationListID != faction->ReputationListID)
359 setFactionStanding.Faction.emplace_back(int32(state.ReputationListID), getStandingForPacket(&state));
360 }
361 }
362
363 setFactionStanding.ShowVisual = _sendFactionIncreased;
364 _player->SendDirectMessage(setFactionStanding.Write());
365
366 _sendFactionIncreased = false; // Reset
367}
368
370{
372
373 for (FactionStateList::iterator itr = _factions.begin(); itr != _factions.end(); ++itr)
374 {
375 initFactions.FactionFlags[itr->first] = itr->second.Flags.AsUnderlyingType();
376 initFactions.FactionStandings[itr->first] = itr->second.Standing;
378 itr->second.needSend = false;
379 }
380
381 _player->SendDirectMessage(initFactions.Write());
382}
383
384void ReputationMgr::SendVisible(FactionState const* faction, bool visible) const
385{
387 return;
388
389 // make faction visible/not visible in reputation list at client
391 packet.FactionIndex = faction->ReputationListID;
393}
394
396{
397 _factions.clear();
402 _sendFactionIncreased = false;
403
404 for (FactionEntry const* factionEntry : sFactionStore)
405 {
406 if (factionEntry->CanHaveReputation())
407 {
408 FactionState newFaction;
409 newFaction.ID = factionEntry->ID;
410 newFaction.ReputationListID = factionEntry->ReputationIndex;
411 newFaction.Standing = 0;
412 newFaction.VisualStandingIncrease = 0;
413 newFaction.Flags = GetDefaultStateFlags(factionEntry);
414 newFaction.needSend = true;
415 newFaction.needSave = true;
416
417 if (newFaction.Flags.HasFlag(ReputationFlags::Visible))
419
420 if (!factionEntry->FriendshipRepID)
422
423 _factions[newFaction.ReputationListID] = newFaction;
424 }
425 }
426}
427
428bool ReputationMgr::SetReputation(FactionEntry const* factionEntry, int32 standing, bool incremental, bool spillOverOnly, bool noSpillover)
429{
430 sScriptMgr->OnPlayerReputationChange(_player, factionEntry->ID, standing, incremental);
431 bool res = false;
432 if (!noSpillover)
433 {
434 // if spillover definition exists in DB, override DBC
435 if (RepSpilloverTemplate const* repTemplate = sObjectMgr->GetRepSpilloverTemplate(factionEntry->ID))
436 {
437 for (uint32 i = 0; i < MAX_SPILLOVER_FACTIONS; ++i)
438 {
439 if (repTemplate->faction[i])
440 {
441 if (_player->GetReputationRank(repTemplate->faction[i]) <= ReputationRank(repTemplate->faction_rank[i]))
442 {
443 // bonuses are already given, so just modify standing by rate
444 int32 spilloverRep = int32(standing * repTemplate->faction_rate[i]);
445 SetOneFactionReputation(sFactionStore.AssertEntry(repTemplate->faction[i]), spilloverRep, incremental);
446 }
447 }
448 }
449 }
450 else
451 {
452 float spillOverRepOut = float(standing);
453 // check for sub-factions that receive spillover
454 std::vector<uint32> const* flist = sDB2Manager.GetFactionTeamList(factionEntry->ID);
455 // if has no sub-factions, check for factions with same parent
456 if (!flist && factionEntry->ParentFactionID && factionEntry->ParentFactionMod[1] != 0.0f)
457 {
458 spillOverRepOut *= factionEntry->ParentFactionMod[1];
459 if (FactionEntry const* parent = sFactionStore.LookupEntry(factionEntry->ParentFactionID))
460 {
461 FactionStateList::iterator parentState = _factions.find(parent->ReputationIndex);
462 // some team factions have own reputation standing, in this case do not spill to other sub-factions
463 if (parentState != _factions.end() && parentState->second.Flags.HasFlag(ReputationFlags::HeaderShowsBar))
464 {
465 SetOneFactionReputation(parent, int32(spillOverRepOut), incremental);
466 }
467 else // spill to "sister" factions
468 {
469 flist = sDB2Manager.GetFactionTeamList(factionEntry->ParentFactionID);
470 }
471 }
472 }
473 if (flist)
474 {
475 // Spillover to affiliated factions
476 for (std::vector<uint32>::const_iterator itr = flist->begin(); itr != flist->end(); ++itr)
477 {
478 if (FactionEntry const* factionEntryCalc = sFactionStore.LookupEntry(*itr))
479 {
480 if (factionEntryCalc == factionEntry || GetRank(factionEntryCalc) > ReputationRank(factionEntryCalc->ParentFactionCap[0]))
481 continue;
482 int32 spilloverRep = int32(spillOverRepOut * factionEntryCalc->ParentFactionMod[0]);
483 if (spilloverRep != 0 || !incremental)
484 res = SetOneFactionReputation(factionEntryCalc, spilloverRep, incremental);
485 }
486 }
487 }
488 }
489 }
490
491 // spillover done, update faction itself
492 FactionStateList::iterator faction = _factions.find(factionEntry->ReputationIndex);
493 if (faction != _factions.end())
494 {
495 FactionEntry const* primaryFactionToModify = factionEntry;
496 if (incremental && standing > 0 && CanGainParagonReputationForFaction(factionEntry))
497 {
498 primaryFactionToModify = sFactionStore.AssertEntry(factionEntry->ParagonFactionID);
499 faction = _factions.find(primaryFactionToModify->ReputationIndex);
500 }
501
502 if (faction != _factions.end())
503 {
504 // if we update spillover only, do not update main reputation (rank exceeds creature reward rate)
505 if (!spillOverOnly)
506 res = SetOneFactionReputation(primaryFactionToModify, standing, incremental);
507
508 // only this faction gets reported to client, even if it has no own visible standing
509 SendState(&faction->second);
510 }
511 }
512 return res;
513}
514
515bool ReputationMgr::SetOneFactionReputation(FactionEntry const* factionEntry, int32 standing, bool incremental)
516{
517 FactionStateList::iterator itr = _factions.find(factionEntry->ReputationIndex);
518 if (itr != _factions.end())
519 {
520 // Ignore renown reputation already raised to the maximum level
521 if (HasMaximumRenownReputation(factionEntry) && standing > 0)
522 {
523 itr->second.needSend = false;
524 itr->second.needSave = false;
525 return false;
526 }
527
528 int32 baseRep = GetBaseReputation(factionEntry);
529 int32 oldStanding = itr->second.Standing + baseRep;
530
531 if (incremental || IsRenownReputation(factionEntry))
532 {
533 // int32 *= float cause one point loss?
534 standing = int32(floor((float)standing * sWorld->getRate(RATE_REPUTATION_GAIN) + 0.5f));
535 standing += oldStanding;
536 }
537
538 if (standing > GetMaxReputation(factionEntry))
539 standing = GetMaxReputation(factionEntry);
540 else if (standing < GetMinReputation(factionEntry))
541 standing = GetMinReputation(factionEntry);
542
543 // Ignore rank for paragon or renown reputation
544 if (!IsParagonReputation(factionEntry) && !IsRenownReputation(factionEntry))
545 {
546 ReputationRank oldRank = ReputationToRank(factionEntry, oldStanding);
547 ReputationRank newRank = ReputationToRank(factionEntry, standing);
548
549 if (newRank <= REP_HOSTILE)
550 SetAtWar(&itr->second, true);
551
552 if (newRank > oldRank)
554
555 if (!factionEntry->FriendshipRepID)
556 UpdateRankCounters(oldRank, newRank);
557 }
558 else
559 _sendFactionIncreased = true; // TODO: Check Paragon reputation
560
561 // Calculate new standing and reputation change
562 int32 newStanding = 0;
563 int32 reputationChange = standing - oldStanding;
564
565 if (!IsRenownReputation(factionEntry))
566 newStanding = standing - baseRep;
567 else
568 {
569 if (CurrencyTypesEntry const* currency = sCurrencyTypesStore.LookupEntry(factionEntry->RenownCurrencyID))
570 {
571 int32 renownLevelThreshold = GetRenownLevelThreshold(factionEntry);
572 int32 oldRenownLevel = GetRenownLevel(factionEntry);
573
574 int32 totalReputation = (oldRenownLevel * renownLevelThreshold) + (standing - baseRep);
575 int32 newRenownLevel = totalReputation / renownLevelThreshold;
576 newStanding = totalReputation % renownLevelThreshold;
577
578 if (newRenownLevel >= GetRenownMaxLevel(factionEntry))
579 {
580 newStanding = 0;
581 reputationChange += (GetRenownMaxLevel(factionEntry) * renownLevelThreshold) - totalReputation;
582 }
583
584 itr->second.VisualStandingIncrease = reputationChange;
585
586 // If the reputation is decreased by command, we will send CurrencyDestroyReason::Cheat
587 if (oldRenownLevel != newRenownLevel)
588 _player->ModifyCurrency(currency->ID, newRenownLevel - oldRenownLevel, CurrencyGainSource::RenownRepGain, CurrencyDestroyReason::Cheat);
589 }
590 }
591
592 _player->ReputationChanged(factionEntry, reputationChange);
593
594 itr->second.Standing = newStanding;
595 itr->second.needSend = true;
596 itr->second.needSave = true;
597
598 SetVisible(&itr->second);
599
600 ParagonReputationEntry const* paragonReputation = sDB2Manager.GetParagonReputation(factionEntry->ID);
601 if (paragonReputation)
602 {
603 int32 oldParagonLevel = oldStanding / paragonReputation->LevelThreshold;
604 int32 newParagonLevel = standing / paragonReputation->LevelThreshold;
605 if (oldParagonLevel != newParagonLevel)
606 if (Quest const* paragonRewardQuest = sObjectMgr->GetQuestTemplate(paragonReputation->QuestID))
607 _player->AddQuestAndCheckCompletion(paragonRewardQuest, nullptr);
608 }
609
615
616 return true;
617 }
618 return false;
619}
620
621void ReputationMgr::SetVisible(FactionTemplateEntry const* factionTemplateEntry)
622{
623 if (!factionTemplateEntry->Faction)
624 return;
625
626 if (FactionEntry const* factionEntry = sFactionStore.LookupEntry(factionTemplateEntry->Faction))
627 // Never show factions of the opposing team
628 if (!(factionEntry->ReputationRaceMask[1].HasRace(_player->GetRace()) && factionEntry->ReputationBase[1] == Reputation_Bottom))
629 SetVisible(factionEntry);
630}
631
633{
634 if (!factionEntry->CanHaveReputation())
635 return;
636
637 FactionStateList::iterator itr = _factions.find(factionEntry->ReputationIndex);
638 if (itr == _factions.end())
639 return;
640
641 SetVisible(&itr->second);
642}
643
645{
646 // always invisible or hidden faction can't be make visible
648 return;
649
651 return;
652
653 if (sDB2Manager.GetParagonReputation(faction->ID))
654 return;
655
656 // already set
658 return;
659
661 faction->needSend = true;
662 faction->needSave = true;
663
665
666 SendVisible(faction);
667}
668
669void ReputationMgr::SetAtWar(RepListID repListID, bool on)
670{
671 FactionStateList::iterator itr = _factions.find(repListID);
672 if (itr == _factions.end())
673 return;
674
675 // always invisible or hidden faction can't change war state
676 if (itr->second.Flags.HasFlag(ReputationFlags::Hidden | ReputationFlags::Header))
677 return;
678
679 SetAtWar(&itr->second, on);
680}
681
682void ReputationMgr::SetAtWar(FactionState* faction, bool atWar) const
683{
684 // Do not allow to declare war to our own faction. But allow for rival factions (eg Aldor vs Scryer).
685 if (atWar && faction->Flags.HasFlag(ReputationFlags::Peaceful) && GetRank(sFactionStore.AssertEntry(faction->ID)) > REP_HATED)
686 return;
687
688 // already set
689 if (faction->Flags.HasFlag(ReputationFlags::AtWar) == atWar)
690 return;
691
692 if (atWar)
693 faction->Flags |= ReputationFlags::AtWar;
694 else
695 faction->Flags &= ~ReputationFlags::AtWar;
696
697 faction->needSend = true;
698 faction->needSave = true;
699}
700
701void ReputationMgr::SetInactive(RepListID repListID, bool on)
702{
703 FactionStateList::iterator itr = _factions.find(repListID);
704 if (itr == _factions.end())
705 return;
706
707 SetInactive(&itr->second, on);
708}
709
710void ReputationMgr::SetInactive(FactionState* faction, bool inactive) const
711{
712 // always invisible or hidden faction can't be inactive
714 return;
715
716 // already set
717 if (faction->Flags.HasFlag(ReputationFlags::Inactive) == inactive)
718 return;
719
720 if (inactive)
722 else
724
725 faction->needSend = true;
726 faction->needSave = true;
727}
728
730{
731 // Set initial reputations (so everything is nifty before DB data load)
732 Initialize();
733
734 //QueryResult* result = CharacterDatabase.PQuery("SELECT faction, standing, flags FROM character_reputation WHERE guid = '{}'", GetGUIDLow());
735
736 if (result)
737 {
738 do
739 {
740 Field* fields = result->Fetch();
741
742 FactionEntry const* factionEntry = sFactionStore.LookupEntry(fields[0].GetUInt16());
743 if (factionEntry && factionEntry->CanHaveReputation())
744 {
745 FactionState* faction = &_factions[factionEntry->ReputationIndex];
746
747 // update standing to current
748 faction->Standing = fields[1].GetInt32();
749
750 // update counters
751 if (!factionEntry->FriendshipRepID)
752 {
753 int32 BaseRep = GetBaseReputation(factionEntry);
754 ReputationRank old_rank = ReputationToRank(factionEntry, BaseRep);
755 ReputationRank new_rank = ReputationToRank(factionEntry, BaseRep + faction->Standing);
756 UpdateRankCounters(old_rank, new_rank);
757 }
758
759 EnumFlag<ReputationFlags> dbFactionFlags = static_cast<ReputationFlags>(fields[2].GetUInt16());
760
761 if (dbFactionFlags.HasFlag(ReputationFlags::Visible))
762 SetVisible(faction); // have internal checks for forced invisibility
763
764 if (dbFactionFlags.HasFlag(ReputationFlags::Inactive))
765 SetInactive(faction, true); // have internal checks for visibility requirement
766
767 if (dbFactionFlags.HasFlag(ReputationFlags::AtWar)) // DB at war
768 SetAtWar(faction, true); // have internal checks for ReputationFlags::Peaceful
769 else // DB not at war
770 {
771 // allow remove if visible (and then not FACTION_FLAG_INVISIBLE_FORCED or FACTION_FLAG_HIDDEN)
773 SetAtWar(faction, false); // have internal checks for ReputationFlags::Peaceful
774 }
775
776 // set atWar for hostile
777 if (GetRank(factionEntry) <= REP_HOSTILE)
778 SetAtWar(faction, true);
779
780 // reset changed flag if values similar to saved in DB
781 if (faction->Flags == dbFactionFlags)
782 {
783 faction->needSend = false;
784 faction->needSave = false;
785 }
786 }
787 }
788 while (result->NextRow());
789 }
790}
791
793{
794 for (FactionStateList::iterator itr = _factions.begin(); itr != _factions.end(); ++itr)
795 {
796 if (itr->second.needSave)
797 {
799 stmt->setUInt64(0, _player->GetGUID().GetCounter());
800 stmt->setUInt16(1, uint16(itr->second.ID));
801 trans->Append(stmt);
802
803 stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_CHAR_REPUTATION_BY_FACTION);
804 stmt->setUInt64(0, _player->GetGUID().GetCounter());
805 stmt->setUInt16(1, uint16(itr->second.ID));
806 stmt->setInt32(2, itr->second.Standing);
807 stmt->setUInt16(3, itr->second.Flags.AsUnderlyingType());
808 trans->Append(stmt);
809
810 itr->second.needSave = false;
811 }
812 }
813}
814
816{
817 if (old_rank >= REP_EXALTED)
819 if (old_rank >= REP_REVERED)
821 if (old_rank >= REP_HONORED)
823
824 if (new_rank >= REP_EXALTED)
826 if (new_rank >= REP_REVERED)
828 if (new_rank >= REP_HONORED)
830}
831
833{
834 if (!factionEntry)
835 return -1;
836
837 uint8 race = _player->GetRace();
838 uint32 classMask = _player->GetClassMask();
839 for (int32 i = 0; i < 4; i++)
840 {
841 if ((factionEntry->ReputationRaceMask[i].HasRace(race) || (factionEntry->ReputationRaceMask[i].IsEmpty() && factionEntry->ReputationClassMask[i] != 0))
842 && (factionEntry->ReputationClassMask[i] & classMask || factionEntry->ReputationClassMask[i] == 0))
843
844 return i;
845 }
846
847 return -1;
848}
849
851{
852 if (!sFactionStore.LookupEntry(factionEntry->ParagonFactionID))
853 return false;
854
855 if (GetRank(factionEntry) != REP_EXALTED && !HasMaximumRenownReputation(factionEntry))
856 return false;
857
858 ParagonReputationEntry const* paragonReputation = sDB2Manager.GetParagonReputation(factionEntry->ParagonFactionID);
859 if (!paragonReputation)
860 return false;
861
862 Quest const* quest = sObjectMgr->GetQuestTemplate(paragonReputation->QuestID);
863 if (!quest)
864 return false;
865
866 return _player->GetLevel() >= _player->GetQuestMinLevel(quest);
867}
@ CHAR_INS_CHAR_REPUTATION_BY_FACTION
@ CHAR_DEL_CHAR_REPUTATION_BY_FACTION
DB2Storage< CurrencyTypesEntry > sCurrencyTypesStore("CurrencyTypes.db2", &CurrencyTypesLoadInfo::Instance)
DB2Storage< FactionEntry > sFactionStore("Faction.db2", &FactionLoadInfo::Instance)
#define sDB2Manager
Definition: DB2Stores.h:538
@ TotalFactionsEncountered
SQLTransaction< CharacterDatabaseConnection > CharacterDatabaseTransaction
std::shared_ptr< PreparedResultSet > PreparedQueryResult
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
Definition: DatabaseEnv.cpp:21
uint8_t uint8
Definition: Define.h:144
int32_t int32
Definition: Define.h:138
uint16_t uint16
Definition: Define.h:143
uint32_t uint32
Definition: Define.h:142
uint16 flags
Definition: DisableMgr.cpp:49
@ LANG_REP_FRIENDLY
Definition: Language.h:378
@ LANG_REP_HONORED
Definition: Language.h:379
@ LANG_REP_NEUTRAL
Definition: Language.h:377
@ LANG_REP_REVERED
Definition: Language.h:380
@ LANG_REP_HOSTILE
Definition: Language.h:375
@ LANG_REP_HATED
Definition: Language.h:374
@ LANG_REP_EXALTED
Definition: Language.h:381
@ LANG_REP_UNFRIENDLY
Definition: Language.h:376
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:165
#define sObjectMgr
Definition: ObjectMgr.h:1946
@ QUEST_STATUS_NONE
Definition: QuestDef.h:142
uint32 const ReputationRankStrIndex[MAX_REPUTATION_RANK]
static int32 ReputationToRankHelper(std::set< T, Rest... > const &thresholds, int32 standing, F thresholdExtractor)
uint32 RepListID
Definition: ReputationMgr.h:51
ReputationFlags
Definition: ReputationMgr.h:34
#define sScriptMgr
Definition: ScriptMgr.h:1418
#define MAX_REPUTATION_RANK
#define MIN_REPUTATION_RANK
ReputationRank
@ REP_HATED
@ REP_EXALTED
@ REP_HONORED
@ REP_REVERED
@ REP_HOSTILE
#define MAX_SPILLOVER_FACTIONS
std::set< FriendshipRepReactionEntry const *, FriendshipRepReactionEntryComparator > FriendshipRepReactionSet
Definition: DB2Stores.h:406
constexpr bool HasFlag(T flag) const
Definition: EnumFlag.h:106
Class used to access individual fields of database query result.
Definition: Field.h:90
uint16 GetUInt16() const
Definition: Field.cpp:46
int32 GetInt32() const
Definition: Field.cpp:70
LowType GetCounter() const
Definition: ObjectGuid.h:293
static ObjectGuid GetGUID(Object const *o)
Definition: Object.h:159
void SendDirectMessage(WorldPacket const *data) const
Definition: Player.cpp:6324
void ReputationChanged(FactionEntry const *factionEntry, int32 change)
Definition: Player.cpp:16724
WorldSession * GetSession() const
Definition: Player.h:2101
void UpdateCriteria(CriteriaType type, uint64 miscValue1=0, uint64 miscValue2=0, uint64 miscValue3=0, WorldObject *ref=nullptr)
Definition: Player.cpp:26767
void AddQuestAndCheckCompletion(Quest const *quest, Object *questGiver)
Definition: Player.cpp:14774
QuestStatus GetQuestStatus(uint32 quest_id) const
Definition: Player.cpp:16050
ReputationRank GetReputationRank(uint32 faction_id) const
Definition: Player.cpp:6497
int32 GetQuestMinLevel(Quest const *quest) const
Definition: Player.cpp:14429
void ModifyCurrency(uint32 id, int32 amount, CurrencyGainSource gainSource=CurrencyGainSource::Cheat, CurrencyDestroyReason destroyReason=CurrencyDestroyReason::Cheat)
Modify currency amount.
Definition: Player.cpp:7110
uint32 GetCurrencyQuantity(uint32 id) const
Definition: Player.cpp:7346
uint32 GetCurrencyMaxQuantity(CurrencyTypesEntry const *currency, bool onLoad=false, bool onUpdateVersion=false) const
Definition: Player.cpp:7382
void setInt32(const uint8 index, const int32 value)
void setUInt16(const uint8 index, const uint16 value)
void setUInt64(const uint8 index, const uint64 value)
bool HasMaximumRenownReputation(FactionEntry const *factionEntry) const
bool IsParagonReputation(FactionEntry const *factionEntry) const
std::string GetReputationRankName(FactionEntry const *factionEntry) const
uint8 _honoredFactionCount
ReputationRank GetBaseRank(FactionEntry const *factionEntry) const
int32 GetFactionDataIndexForRaceAndClass(FactionEntry const *factionEntry) const
int32 GetRenownLevelThreshold(FactionEntry const *renownFactionEntry) const
void SetVisible(FactionTemplateEntry const *factionTemplateEntry)
int32 GetBaseReputation(FactionEntry const *factionEntry) const
int32 GetMaxReputation(FactionEntry const *factionEntry) const
uint8 _visibleFactionCount
ReputationFlags GetDefaultStateFlags(FactionEntry const *factionEntry) const
bool CanGainParagonReputationForFaction(FactionEntry const *factionEntry) const
void SetAtWar(RepListID repListID, bool on)
void SendState(FactionState const *faction)
int32 GetReputation(uint32 faction_id) const
bool SetReputation(FactionEntry const *factionEntry, int32 standing)
int32 GetRenownLevel(FactionEntry const *renownFactionEntry) const
void ApplyForceReaction(uint32 faction_id, ReputationRank rank, bool apply)
ReputationRank GetRank(FactionEntry const *factionEntry) const
int32 GetMinReputation(FactionEntry const *factionEntry) const
void SendInitialReputations()
static ReputationRank ReputationToRank(FactionEntry const *factionEntry, int32 standing)
uint8 _exaltedFactionCount
FactionState const * GetState(FactionEntry const *factionEntry) const
uint8 _reveredFactionCount
FactionStateList _factions
ForcedReactions _forcedReactions
static const int32 Reputation_Bottom
Definition: ReputationMgr.h:80
void SendVisible(FactionState const *faction, bool visible=true) const
bool IsAtWar(uint32 faction_id) const
bool SetOneFactionReputation(FactionEntry const *factionEntry, int32 standing, bool incremental)
Public for chat command needs.
bool _sendFactionIncreased
Play visual effect on next SMSG_SET_FACTION_STANDING sent.
int32 GetParagonLevel(uint32 paragonFactionId) const
int32 GetRenownMaxLevel(FactionEntry const *renownFactionEntry) const
bool IsRenownReputation(FactionEntry const *factionEntry) const
static std::set< int32 > const ReputationRankThresholds
Definition: ReputationMgr.h:78
void SendForceReactions()
void UpdateRankCounters(ReputationRank old_rank, ReputationRank new_rank)
static const int32 Reputation_Cap
Definition: ReputationMgr.h:79
Player * _player
void LoadFromDB(PreparedQueryResult result)
ReputationRank const * GetForcedRankIfAny(FactionTemplateEntry const *factionTemplateEntry) const
void SetInactive(RepListID repListID, bool on)
void SaveToDB(CharacterDatabaseTransaction trans)
uint32 GetClassMask() const
Definition: Unit.h:754
uint8 GetLevel() const
Definition: Unit.h:746
uint8 GetRace() const
Definition: Unit.h:749
std::string const & GetName() const
Definition: Object.h:555
std::array< int32, FactionCount > FactionStandings
std::array< uint16, FactionCount > FactionFlags
std::vector< FactionStandingData > Faction
LocaleConstant GetSessionDbcLocale() const
bool PlayerLoading() const
Definition: WorldSession.h:969
#define sWorld
Definition: World.h:931
@ RATE_REPUTATION_GAIN
Definition: World.h:489
void apply(T *val)
Definition: ByteConverter.h:41
std::array< int32, 4 > ReputationBase
std::array< int32, 4 > ReputationMax
uint16 ParagonFactionID
uint32 FriendshipRepID
uint16 ParentFactionID
std::array< int16, 4 > ReputationClassMask
bool CanHaveReputation() const
std::array< float, 2 > ParentFactionMod
std::array< uint16, 4 > ReputationFlags
int32 RenownCurrencyID
int16 ReputationIndex
std::array< Trinity::RaceMask< int64 >, 4 > ReputationRaceMask
RepListID ReputationListID
Definition: ReputationMgr.h:55
EnumFlag< ReputationFlags > Flags
Definition: ReputationMgr.h:58
int32 VisualStandingIncrease
Definition: ReputationMgr.h:57