TrinityCore
Loading...
Searching...
No Matches
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 "MapUtils.h"
25#include "ObjectMgr.h"
26#include "Player.h"
27#include "ReputationPackets.h"
28#include "ScriptMgr.h"
29#include "World.h"
30#include "WorldSession.h"
31
37
38std::set<int32> const ReputationMgr::ReputationRankThresholds =
39{
40 -42000,
41 // Hated
42 -6000,
43 // Hostile
44 -3000,
45 // Unfriendly
46 0,
47 // Neutral
48 3000,
49 // Friendly
50 9000,
51 // Honored
52 21000,
53 // Revered
54 42000
55 // Exalted
56};
57
60
61template<typename T, typename F, typename... Rest>
62static int32 ReputationToRankHelper(std::set<T, Rest...> const& thresholds, int32 standing, F thresholdExtractor)
63{
64 auto itr = thresholds.begin();
65 auto end = thresholds.end();
66 int32 rank = -1;
67 while (itr != end && standing >= thresholdExtractor(*itr))
68 {
69 ++rank;
70 ++itr;
71 }
72
73 return rank;
74}
75
77{
79 if (DB2Manager::FriendshipRepReactionSet const* friendshipReactions = sDB2Manager.GetFriendshipRepReactions(factionEntry->FriendshipRepID))
80 rank = ReputationToRankHelper(*friendshipReactions, standing, [](FriendshipRepReactionEntry const* frr) { return frr->ReactionThreshold; });
81 else
82 rank = ReputationToRankHelper(ReputationRankThresholds, standing, [](int32 threshold) { return threshold; });
83
84 return ReputationRank(rank);
85}
86
87FactionState const* ReputationMgr::GetState(FactionEntry const* factionEntry) const
88{
89 return factionEntry->CanHaveReputation() ? GetState(factionEntry->ReputationIndex) : nullptr;
90}
91
92bool ReputationMgr::IsAtWar(uint32 faction_id) const
93{
94 FactionEntry const* factionEntry = sFactionStore.LookupEntry(faction_id);
95
96 if (!factionEntry)
97 {
98 TC_LOG_ERROR("misc", "ReputationMgr::IsAtWar: Can't get AtWar flag of {} for unknown faction (faction id) #{}.", _player->GetName(), faction_id);
99 return false;
100 }
101
102 return IsAtWar(factionEntry);
103}
104
105bool ReputationMgr::IsAtWar(FactionEntry const* factionEntry) const
106{
107 if (!factionEntry)
108 return false;
109
110 if (FactionState const* factionState = GetState(factionEntry))
111 return factionState->Flags.HasFlag(ReputationFlags::AtWar);
112 return false;
113}
114
116{
117 FactionEntry const* factionEntry = sFactionStore.LookupEntry(faction_id);
118
119 if (!factionEntry)
120 {
121 TC_LOG_ERROR("misc", "ReputationMgr::GetReputation: Can't get reputation of {} for unknown faction (faction id) #{}.", _player->GetName(), faction_id);
122 return 0;
123 }
124
125 return GetReputation(factionEntry);
126}
127
129{
130 return GetBaseReputation(factionEntry, _player->GetRace(), _player->GetClass());
131}
132
133int32 ReputationMgr::GetBaseReputation(FactionEntry const* factionEntry, uint8 race, uint8 playerClass)
134{
135 int32 dataIndex = GetFactionDataIndexForRaceAndClass(factionEntry, race, playerClass);
136 if (dataIndex < 0)
137 return 0;
138
139 return factionEntry->ReputationBase[dataIndex];
140}
141
143{
144 if (DB2Manager::FriendshipRepReactionSet const* friendshipReactions = sDB2Manager.GetFriendshipRepReactions(factionEntry->FriendshipRepID))
145 return (*friendshipReactions->begin())->ReactionThreshold;
146
147 return *ReputationRankThresholds.begin();
148}
149
151{
152 if (ParagonReputationEntry const* paragonReputation = sDB2Manager.GetParagonReputation(factionEntry->ID))
153 {
154 // has reward quest, cap is just before threshold for another quest reward
155 // for example: if current reputation is 12345 and quests are given every 10000 and player has unclaimed reward
156 // then cap will be 19999
157
158 // otherwise cap is one theshold level larger
159 // if current reputation is 12345 and questa are given every 10000 and player does NOT have unclaimed reward
160 // then cap will be 29999
161
162 int32 reputation = GetReputation(factionEntry);
163 int32 cap = reputation + paragonReputation->LevelThreshold - reputation % paragonReputation->LevelThreshold - 1;
164
165 if (_player->GetQuestStatus(paragonReputation->QuestID) == QUEST_STATUS_NONE)
166 cap += paragonReputation->LevelThreshold;
167
168 return cap;
169 }
170
171 if (IsRenownReputation(factionEntry))
172 {
173 // Compared to a paragon reputation, DF renown reputations
174 // have a maximum value of 2500 which resets with each level of renown acquired.
175 // We calculate the total reputation necessary to raise the renown to the maximum
176 return GetRenownMaxLevel(factionEntry) * GetRenownLevelThreshold(factionEntry);
177 }
178
179 if (DB2Manager::FriendshipRepReactionSet const* friendshipReactions = sDB2Manager.GetFriendshipRepReactions(factionEntry->FriendshipRepID))
180 return (*friendshipReactions->rbegin())->ReactionThreshold;
181
182 if (int32 dataIndex = GetFactionDataIndexForRaceAndClass(factionEntry); dataIndex >= 0)
183 return factionEntry->ReputationMax[dataIndex];
184
185 return *ReputationRankThresholds.rbegin();
186}
187
189{
190 // Faction without recorded reputation. Just ignore.
191 if (!factionEntry)
192 return 0;
193
194 if (FactionState const* state = GetState(factionEntry))
195 return GetBaseReputation(factionEntry) + state->Standing;
196
197 return 0;
198}
199
201{
202 int32 reputation = GetReputation(factionEntry);
203 return ReputationToRank(factionEntry, reputation);
204}
205
207{
208 int32 reputation = GetBaseReputation(factionEntry);
209 return ReputationToRank(factionEntry, reputation);
210}
211
212std::string ReputationMgr::GetReputationRankName(FactionEntry const* factionEntry) const
213{
214 ReputationRank rank = GetRank(factionEntry);
215 if (!factionEntry->FriendshipRepID)
216 return sObjectMgr->GetTrinityString(ReputationRankStrIndex[rank], _player->GetSession()->GetSessionDbcLocale());
217
218 if (DB2Manager::FriendshipRepReactionSet const* friendshipReactions = sDB2Manager.GetFriendshipRepReactions(factionEntry->FriendshipRepID))
219 {
220 auto itr = friendshipReactions->begin();
221 std::advance(itr, uint32(rank));
222 return (*itr)->Reaction[_player->GetSession()->GetSessionDbcLocale()];
223 }
224
225 return "";
226}
227
229{
230 return GetForcedRankIfAny(factionTemplateEntry->Faction);
231}
232
237
239{
240 if (sDB2Manager.GetParagonReputation(factionEntry->ID))
241 return true;
242
243 return false;
244}
245
247{
248 return GetParagonLevel(sFactionStore.LookupEntry(paragonFactionId));
249}
250
251int32 ReputationMgr::GetParagonLevel(FactionEntry const* paragonFactionEntry) const
252{
253 if (!paragonFactionEntry)
254 return 0;
255
256 if (ParagonReputationEntry const* paragonReputation = sDB2Manager.GetParagonReputation(paragonFactionEntry->ID))
257 return GetReputation(paragonFactionEntry) / paragonReputation->LevelThreshold;
258
259 return 0;
260}
261
263{
264 if (!IsRenownReputation(factionEntry))
265 return false;
266
267 return GetRenownLevel(factionEntry) >= GetRenownMaxLevel(factionEntry);
268}
269
270bool ReputationMgr::IsRenownReputation(FactionEntry const* factionEntry) const
271{
272 return factionEntry->RenownCurrencyID > 0;
273}
274
275int32 ReputationMgr::GetRenownLevel(FactionEntry const* renownFactionEntry) const
276{
277 if (!renownFactionEntry)
278 return 0;
279
280 if (CurrencyTypesEntry const* currency = sCurrencyTypesStore.LookupEntry(renownFactionEntry->RenownCurrencyID))
281 return _player->GetCurrencyQuantity(currency->ID);
282
283 return 0;
284}
285
287{
288 if (!renownFactionEntry || !IsRenownReputation(renownFactionEntry))
289 return 0;
290
291 if (int32 dataIndex = GetFactionDataIndexForRaceAndClass(renownFactionEntry); dataIndex >= 0)
292 return renownFactionEntry->ReputationMax[dataIndex];
293
294 return 0;
295}
296
298{
299 if (!renownFactionEntry)
300 return 0;
301
302 if (CurrencyTypesEntry const* currency = sCurrencyTypesStore.LookupEntry(renownFactionEntry->RenownCurrencyID))
303 return _player->GetCurrencyMaxQuantity(currency);
304
305 return 0;
306}
307
309{
310 if (apply)
311 {
312 _forcedReactions[faction_id] = rank;
313 _player->SetVisibleForcedReaction(faction_id, rank);
314 }
315 else
316 {
317 _forcedReactions.erase(faction_id);
319 }
320}
321
323{
325
326 if (int32 dataIndex = GetFactionDataIndexForRaceAndClass(factionEntry); dataIndex >= 0)
327 flags |= static_cast<ReputationFlags>(factionEntry->ReputationFlags[dataIndex]);
328
329 if (sDB2Manager.GetParagonReputation(factionEntry->ID))
331
332 return flags;
333}
334
336{
338 setFactionStanding.BonusFromAchievementSystem = 0.0f;
339
340 auto getStandingForPacket = [](FactionState const* state)
341 {
342 return state->VisualStandingIncrease ? state->VisualStandingIncrease : state->Standing;
343 };
344
345 if (faction)
346 setFactionStanding.Faction.emplace_back(int32(faction->ReputationListID), getStandingForPacket(faction), faction->ID);
347
348 for (auto& [reputationIndex, state] : _factions)
349 {
350 if (state.needSend)
351 {
352 state.needSend = false;
353 if (!faction || state.ReputationListID != faction->ReputationListID)
354 setFactionStanding.Faction.emplace_back(int32(state.ReputationListID), getStandingForPacket(&state), state.ID);
355 }
356 }
357
358 setFactionStanding.ShowVisual = _sendFactionIncreased;
359 _player->SendDirectMessage(setFactionStanding.Write());
360
361 _sendFactionIncreased = false; // Reset
362}
363
365{
367
368 for (FactionStateList::iterator itr = _factions.begin(); itr != _factions.end(); ++itr)
369 {
370 WorldPackets::Reputation::FactionData& factionData = initFactions.Factions.emplace_back();
371 factionData.FactionID = itr->second.ID;
372 factionData.Flags = itr->second.Flags.AsUnderlyingType();
373 factionData.Standing = itr->second.Standing;
375 WorldPackets::Reputation::FactionBonusData& bonus = initFactions.Bonuses.emplace_back();
376 bonus.FactionID = itr->second.ID;
377 bonus.FactionHasBonus = false;
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 else if (newRank > oldRank)
552 SetAtWar(&itr->second, false);
553
554 if (newRank > oldRank)
556
557 if (!factionEntry->FriendshipRepID)
558 UpdateRankCounters(oldRank, newRank);
559 }
560 else
561 _sendFactionIncreased = true; // TODO: Check Paragon reputation
562
563 // Calculate new standing and reputation change
564 int32 newStanding = 0;
565 int32 reputationChange = standing - oldStanding;
566
567 if (!IsRenownReputation(factionEntry))
568 newStanding = standing - baseRep;
569 else
570 {
571 if (CurrencyTypesEntry const* currency = sCurrencyTypesStore.LookupEntry(factionEntry->RenownCurrencyID))
572 {
573 int32 renownLevelThreshold = GetRenownLevelThreshold(factionEntry);
574 int32 oldRenownLevel = GetRenownLevel(factionEntry);
575
576 int32 totalReputation = (oldRenownLevel * renownLevelThreshold) + (standing - baseRep);
577 int32 newRenownLevel = totalReputation / renownLevelThreshold;
578 newStanding = totalReputation % renownLevelThreshold;
579
580 if (newRenownLevel >= GetRenownMaxLevel(factionEntry))
581 {
582 newStanding = 0;
583 reputationChange += (GetRenownMaxLevel(factionEntry) * renownLevelThreshold) - totalReputation;
584 }
585
586 itr->second.VisualStandingIncrease = reputationChange;
587
588 // If the reputation is decreased by command, we will send CurrencyDestroyReason::Cheat
589 if (oldRenownLevel != newRenownLevel)
590 _player->ModifyCurrency(currency->ID, newRenownLevel - oldRenownLevel, CurrencyGainSource::RenownRepGain, CurrencyDestroyReason::Cheat);
591 }
592 }
593
594 _player->ReputationChanged(factionEntry, reputationChange);
595
596 itr->second.Standing = newStanding;
597 itr->second.needSend = true;
598 itr->second.needSave = true;
599
600 SetVisible(&itr->second);
601
602 ParagonReputationEntry const* paragonReputation = sDB2Manager.GetParagonReputation(factionEntry->ID);
603 if (paragonReputation)
604 {
605 int32 oldParagonLevel = oldStanding / paragonReputation->LevelThreshold;
606 int32 newParagonLevel = standing / paragonReputation->LevelThreshold;
607 if (oldParagonLevel != newParagonLevel)
608 if (Quest const* paragonRewardQuest = sObjectMgr->GetQuestTemplate(paragonReputation->QuestID))
609 _player->AddQuestAndCheckCompletion(paragonRewardQuest, nullptr);
610 }
611
617
618 return true;
619 }
620 return false;
621}
622
623void ReputationMgr::SetVisible(FactionTemplateEntry const* factionTemplateEntry)
624{
625 if (!factionTemplateEntry->Faction)
626 return;
627
628 if (FactionEntry const* factionEntry = sFactionStore.LookupEntry(factionTemplateEntry->Faction))
629 // Never show factions of the opposing team
630 if (!(factionEntry->ReputationRaceMask2.HasRace(_player->GetRace()) && factionEntry->ReputationBase[1] == Reputation_Bottom))
631 SetVisible(factionEntry);
632}
633
635{
636 if (!factionEntry->CanHaveReputation())
637 return;
638
639 FactionStateList::iterator itr = _factions.find(factionEntry->ReputationIndex);
640 if (itr == _factions.end())
641 return;
642
643 SetVisible(&itr->second);
644}
645
647{
648 // always invisible or hidden faction can't be make visible
650 return;
651
653 return;
654
655 if (sDB2Manager.GetParagonReputation(faction->ID))
656 return;
657
658 // already set
660 return;
661
663 faction->needSend = true;
664 faction->needSave = true;
665
667
668 SendVisible(faction);
669}
670
671void ReputationMgr::SetAtWar(RepListID repListID, bool on)
672{
673 FactionStateList::iterator itr = _factions.find(repListID);
674 if (itr == _factions.end())
675 return;
676
677 // always invisible or hidden faction can't change war state
678 if (itr->second.Flags.HasFlag(ReputationFlags::Hidden | ReputationFlags::Header))
679 return;
680
681 SetAtWar(&itr->second, on);
682}
683
684void ReputationMgr::SetAtWar(FactionState* faction, bool atWar) const
685{
686 // Do not allow to declare war to our own faction. But allow for rival factions (eg Aldor vs Scryer).
687 if (atWar && faction->Flags.HasFlag(ReputationFlags::Peaceful) && GetRank(sFactionStore.AssertEntry(faction->ID)) > REP_HATED)
688 return;
689
690 // already set
691 if (faction->Flags.HasFlag(ReputationFlags::AtWar) == atWar)
692 return;
693
694 if (atWar)
695 faction->Flags |= ReputationFlags::AtWar;
696 else
697 faction->Flags &= ~ReputationFlags::AtWar;
698
699 faction->needSend = true;
700 faction->needSave = true;
701}
702
703void ReputationMgr::SetInactive(RepListID repListID, bool on)
704{
705 FactionStateList::iterator itr = _factions.find(repListID);
706 if (itr == _factions.end())
707 return;
708
709 SetInactive(&itr->second, on);
710}
711
712void ReputationMgr::SetInactive(FactionState* faction, bool inactive) const
713{
714 // always invisible or hidden faction can't be inactive
716 return;
717
718 // already set
719 if (faction->Flags.HasFlag(ReputationFlags::Inactive) == inactive)
720 return;
721
722 if (inactive)
724 else
726
727 faction->needSend = true;
728 faction->needSave = true;
729}
730
732{
733 // Set initial reputations (so everything is nifty before DB data load)
734 Initialize();
735
736 //QueryResult* result = CharacterDatabase.PQuery("SELECT faction, standing, flags FROM character_reputation WHERE guid = '{}'", GetGUIDLow());
737
738 if (result)
739 {
740 do
741 {
742 Field* fields = result->Fetch();
743
744 FactionEntry const* factionEntry = sFactionStore.LookupEntry(fields[0].GetUInt16());
745 if (factionEntry && factionEntry->CanHaveReputation())
746 {
747 FactionState* faction = &_factions[factionEntry->ReputationIndex];
748
749 // update standing to current
750 faction->Standing = fields[1].GetInt32();
751
752 // update counters
753 if (!factionEntry->FriendshipRepID)
754 {
755 int32 BaseRep = GetBaseReputation(factionEntry);
756 ReputationRank old_rank = ReputationToRank(factionEntry, BaseRep);
757 ReputationRank new_rank = ReputationToRank(factionEntry, BaseRep + faction->Standing);
758 UpdateRankCounters(old_rank, new_rank);
759 }
760
761 EnumFlag<ReputationFlags> dbFactionFlags = static_cast<ReputationFlags>(fields[2].GetUInt16());
762
763 if (dbFactionFlags.HasFlag(ReputationFlags::Visible))
764 SetVisible(faction); // have internal checks for forced invisibility
765
766 if (dbFactionFlags.HasFlag(ReputationFlags::Inactive))
767 SetInactive(faction, true); // have internal checks for visibility requirement
768
769 if (dbFactionFlags.HasFlag(ReputationFlags::AtWar)) // DB at war
770 SetAtWar(faction, true); // have internal checks for ReputationFlags::Peaceful
771 else // DB not at war
772 {
773 // allow remove if visible (and then not FACTION_FLAG_INVISIBLE_FORCED or FACTION_FLAG_HIDDEN)
775 SetAtWar(faction, false); // have internal checks for ReputationFlags::Peaceful
776 }
777
778 // set atWar for hostile
779 if (GetRank(factionEntry) <= REP_HOSTILE)
780 SetAtWar(faction, true);
781
782 // reset changed flag if values similar to saved in DB
783 if (faction->Flags == dbFactionFlags)
784 {
785 faction->needSend = false;
786 faction->needSave = false;
787 }
788 }
789 }
790 while (result->NextRow());
791 }
792}
793
795{
796 for (FactionStateList::iterator itr = _factions.begin(); itr != _factions.end(); ++itr)
797 {
798 if (itr->second.needSave)
799 {
801 stmt->setUInt64(0, _player->GetGUID().GetCounter());
802 stmt->setUInt16(1, uint16(itr->second.ID));
803 trans->Append(stmt);
804
805 stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_CHAR_REPUTATION_BY_FACTION);
806 stmt->setUInt64(0, _player->GetGUID().GetCounter());
807 stmt->setUInt16(1, uint16(itr->second.ID));
808 stmt->setInt32(2, itr->second.Standing);
809 stmt->setUInt16(3, itr->second.Flags.AsUnderlyingType());
810 trans->Append(stmt);
811
812 itr->second.needSave = false;
813 }
814 }
815}
816
818{
819 if (old_rank >= REP_EXALTED)
821 if (old_rank >= REP_REVERED)
823 if (old_rank >= REP_HONORED)
825
826 if (new_rank >= REP_EXALTED)
828 if (new_rank >= REP_REVERED)
830 if (new_rank >= REP_HONORED)
832}
833
838
840{
841 if (!factionEntry)
842 return -1;
843
844 std::array<Trinity::RaceMask<int32, 2> const*, 4> reputationRaceMask =
845 {
846 &factionEntry->ReputationRaceMask1,
847 &factionEntry->ReputationRaceMask2,
848 &factionEntry->ReputationRaceMask3,
849 &factionEntry->ReputationRaceMask4
850 };
851
852 uint32 classMask = 1 << (playerClass - 1);
853 for (int32 i = 0; i < 4; ++i)
854 {
855 if ((reputationRaceMask[i]->IsEmpty() || reputationRaceMask[i]->HasRace(race))
856 && (factionEntry->ReputationClassMask[i] == 0 || factionEntry->ReputationClassMask[i] & classMask))
857 return i;
858 }
859
860 return -1;
861}
862
864{
865 if (!sFactionStore.LookupEntry(factionEntry->ParagonFactionID))
866 return false;
867
868 if (GetRank(factionEntry) != REP_EXALTED && !HasMaximumRenownReputation(factionEntry))
869 return false;
870
871 ParagonReputationEntry const* paragonReputation = sDB2Manager.GetParagonReputation(factionEntry->ParagonFactionID);
872 if (!paragonReputation)
873 return false;
874
875 Quest const* quest = sObjectMgr->GetQuestTemplate(paragonReputation->QuestID);
876 if (!quest)
877 return false;
878
879 return _player->GetLevel() >= _player->GetQuestMinLevel(quest);
880}
@ 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:579
@ TotalFactionsEncountered
SQLTransaction< CharacterDatabaseConnection > CharacterDatabaseTransaction
std::shared_ptr< PreparedResultSet > PreparedQueryResult
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
uint8_t uint8
Definition Define.h:156
int32_t int32
Definition Define.h:150
uint16_t uint16
Definition Define.h:155
uint32_t uint32
Definition Define.h:154
uint16 flags
@ LANG_REP_FRIENDLY
Definition Language.h:377
@ LANG_REP_HONORED
Definition Language.h:378
@ LANG_REP_NEUTRAL
Definition Language.h:376
@ LANG_REP_REVERED
Definition Language.h:379
@ LANG_REP_HOSTILE
Definition Language.h:374
@ LANG_REP_HATED
Definition Language.h:373
@ LANG_REP_EXALTED
Definition Language.h:380
@ LANG_REP_UNFRIENDLY
Definition Language.h:375
#define TC_LOG_ERROR(filterType__, message__,...)
Definition Log.h:182
#define sObjectMgr
Definition ObjectMgr.h:1873
@ QUEST_STATUS_NONE
Definition QuestDef.h:143
uint32 const ReputationRankStrIndex[MAX_REPUTATION_RANK]
static int32 ReputationToRankHelper(std::set< T, Rest... > const &thresholds, int32 standing, F thresholdExtractor)
uint32 RepListID
ReputationFlags
TC_GAME_API uint32 const ReputationRankStrIndex[MAX_REPUTATION_RANK]
#define sScriptMgr
Definition ScriptMgr.h:1449
#define MAX_REPUTATION_RANK
#define MIN_REPUTATION_RANK
ReputationRank
@ REP_HATED
@ REP_EXALTED
@ REP_HONORED
@ REP_REVERED
@ REP_HOSTILE
#define MAX_SPILLOVER_FACTIONS
ObjectGuid const & GetGUID() const
Definition BaseEntity.h:163
std::set< FriendshipRepReactionEntry const *, FriendshipRepReactionEntryComparator > FriendshipRepReactionSet
Definition DB2Stores.h:448
constexpr bool HasFlag(T flag) const
Definition EnumFlag.h:106
Class used to access individual fields of database query result.
Definition Field.h:94
uint16 GetUInt16() const noexcept
Definition Field.cpp:43
int32 GetInt32() const noexcept
Definition Field.cpp:64
LowType GetCounter() const
Definition ObjectGuid.h:336
void SendDirectMessage(WorldPacket const *data) const
Definition Player.cpp:6193
void ReputationChanged(FactionEntry const *factionEntry, int32 change)
Definition Player.cpp:16690
void SetVisibleForcedReaction(uint32 factionId, ReputationRank rank)
Definition Player.cpp:6510
WorldSession * GetSession() const
Definition Player.h:2256
void UpdateCriteria(CriteriaType type, uint64 miscValue1=0, uint64 miscValue2=0, uint64 miscValue3=0, WorldObject *ref=nullptr)
Definition Player.cpp:27532
void AddQuestAndCheckCompletion(Quest const *quest, Object *questGiver)
Definition Player.cpp:14633
QuestStatus GetQuestStatus(uint32 quest_id) const
Definition Player.cpp:15929
ReputationRank GetReputationRank(uint32 faction_id) const
Definition Player.cpp:6415
void RemoveVisibleForcedReaction(uint32 factionId)
Definition Player.cpp:6526
int32 GetQuestMinLevel(Quest const *quest) const
Definition Player.cpp:14288
void ModifyCurrency(uint32 id, int32 amount, CurrencyGainSource gainSource=CurrencyGainSource::Cheat, CurrencyDestroyReason destroyReason=CurrencyDestroyReason::Cheat)
Modify currency amount.
Definition Player.cpp:7036
uint32 GetCurrencyQuantity(uint32 id) const
Definition Player.cpp:7274
uint32 GetCurrencyMaxQuantity(CurrencyTypesEntry const *currency, bool onLoad=false, bool onUpdateVersion=false) const
Definition Player.cpp:7310
void setUInt16(uint8 index, uint16 value)
void setUInt64(uint8 index, uint64 value)
void setInt32(uint8 index, int32 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
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
void UpdateRankCounters(ReputationRank old_rank, ReputationRank new_rank)
static const int32 Reputation_Cap
void LoadFromDB(PreparedQueryResult result)
ReputationRank const * GetForcedRankIfAny(FactionTemplateEntry const *factionTemplateEntry) const
void SetInactive(RepListID repListID, bool on)
void SaveToDB(CharacterDatabaseTransaction trans)
uint8 GetClass() const
Definition Unit.h:776
uint8 GetLevel() const
Definition Unit.h:769
uint8 GetRace() const
Definition Unit.h:773
std::string const & GetName() const
Definition Object.h:342
std::vector< FactionBonusData > Bonuses
std::vector< FactionStandingData > Faction
LocaleConstant GetSessionDbcLocale() const
bool PlayerLoading() const
#define sWorld
Definition World.h:919
@ RATE_REPUTATION_GAIN
Definition World.h:487
auto MapGetValuePtr(M &map, typename M::key_type const &key)
Definition MapUtils.h:37
std::array< int32, 4 > ReputationBase
std::array< int32, 4 > ReputationMax
uint16 ParagonFactionID
uint32 FriendshipRepID
uint16 ParentFactionID
std::array< int16, 4 > ReputationClassMask
Trinity::RaceMask< int32, 2 > ReputationRaceMask2
Trinity::RaceMask< int32, 2 > ReputationRaceMask3
Trinity::RaceMask< int32, 2 > ReputationRaceMask1
bool CanHaveReputation() const
Trinity::RaceMask< int32, 2 > ReputationRaceMask4
std::array< float, 2 > ParentFactionMod
std::array< uint16, 4 > ReputationFlags
int32 RenownCurrencyID
int16 ReputationIndex
RepListID ReputationListID
EnumFlag< ReputationFlags > Flags
int32 VisualStandingIncrease