Skip to content

Commit

Permalink
Core/Misc: Add a bunch of examples for 67418a1
Browse files Browse the repository at this point in the history
  • Loading branch information
Shauren committed Feb 23, 2025
1 parent 67418a1 commit 00223f3
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 53 deletions.
3 changes: 2 additions & 1 deletion src/server/game/AI/SmartScripts/SmartScriptMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "Util.h"
#include "WaypointDefines.h"
#include "WaypointManager.h"
#include "advstd.h"
#include <algorithm>

#define TC_SAI_IS_BOOLEAN_VALID(e, value) \
Expand Down Expand Up @@ -2036,7 +2037,7 @@ bool SmartAIMgr::IsEventValid(SmartScriptHolder& e)
return false;
}

if (std::ranges::none_of(InventoryTypesEquipable, [dbcItem](InventoryType inventoryType) { return inventoryType == dbcItem->InventoryType; }))
if (!advstd::ranges::contains(InventoryTypesEquipable, dbcItem->InventoryType))
{
TC_LOG_ERROR("sql.sql", "SmartScript: SMART_ACTION_EQUIP uses item {} (slot {}) not equipable in a hand for creature {}, skipped.", itemEntry, slot, e.entryOrGuid);
return false;
Expand Down
7 changes: 2 additions & 5 deletions src/server/game/Achievements/AchievementMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,8 @@ uint32 AchievementMgr::GetAchievementPoints() const

std::vector<uint32> AchievementMgr::GetCompletedAchievementIds() const
{
std::vector<uint32> achievementIds;
std::transform(_completedAchievements.begin(), _completedAchievements.end(), std::back_inserter(achievementIds), [](std::pair<uint32 const, CompletedAchievementData> const& achievement)
{
return achievement.first;
});
std::vector<uint32> achievementIds(_completedAchievements.size());
std::ranges::transform(_completedAchievements, achievementIds.begin(), Trinity::Containers::MapKey);
return achievementIds;
}

Expand Down
4 changes: 2 additions & 2 deletions src/server/game/AuctionHouse/AuctionHouseMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -910,10 +910,10 @@ void AuctionHouseObject::AddAuction(CharacterDatabaseTransaction trans, AuctionP

if (ItemModifiedAppearanceEntry const* itemModifiedAppearance = auction.Items[0]->GetItemModifiedAppearance())
{
auto itr = std::ranges::find(bucket->ItemModifiedAppearanceId, itemModifiedAppearance->ID, &std::pair<uint32, uint32>::first);
auto itr = std::ranges::find(bucket->ItemModifiedAppearanceId, itemModifiedAppearance->ID, Trinity::TupleElement<0>);

if (itr == bucket->ItemModifiedAppearanceId.end())
itr = std::ranges::find(bucket->ItemModifiedAppearanceId, 0u, &std::pair<uint32, uint32>::first);
itr = std::ranges::find(bucket->ItemModifiedAppearanceId, 0u, Trinity::TupleElement<0>);

if (itr != bucket->ItemModifiedAppearanceId.end())
{
Expand Down
36 changes: 16 additions & 20 deletions src/server/game/BattlePets/BattlePetMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ namespace BattlePets
{
namespace
{
std::unordered_map<uint16 /*BreedID*/, std::unordered_map<BattlePetState /*state*/, int32 /*value*/, std::hash<std::underlying_type<BattlePetState>::type> >> _battlePetBreedStates;
std::unordered_map<uint32 /*SpeciesID*/, std::unordered_map<BattlePetState /*state*/, int32 /*value*/, std::hash<std::underlying_type<BattlePetState>::type> >> _battlePetSpeciesStates;
std::unordered_map<uint16 /*BreedID*/, std::unordered_map<BattlePetState /*state*/, int32 /*value*/>> _battlePetBreedStates;
std::unordered_map<uint32 /*SpeciesID*/, std::unordered_map<BattlePetState /*state*/, int32 /*value*/>> _battlePetSpeciesStates;
std::unordered_map<uint32 /*CreatureID*/, BattlePetSpeciesEntry const*> _battlePetSpeciesByCreature;
std::unordered_map<uint32 /*SpellID*/, BattlePetSpeciesEntry const*> _battlePetSpeciesBySpell;
std::unordered_map<uint32 /*SpeciesID*/, std::unordered_set<uint8 /*breed*/>> _availableBreedsPerSpecies;
Expand All @@ -47,18 +47,14 @@ std::unordered_map<uint32 /*SpeciesID*/, uint8 /*quality*/> _defaultQualityPerSp

void BattlePet::CalculateStats()
{
float health = 0.0f;
float power = 0.0f;
float speed = 0.0f;

// get base breed stats
auto breedState = _battlePetBreedStates.find(PacketInfo.Breed);
if (breedState == _battlePetBreedStates.end()) // non existing breed id
return;

health = breedState->second[STATE_STAT_STAMINA];
power = breedState->second[STATE_STAT_POWER];
speed = breedState->second[STATE_STAT_SPEED];
float health = breedState->second[STATE_STAT_STAMINA];
float power = breedState->second[STATE_STAT_POWER];
float speed = breedState->second[STATE_STAT_SPEED];

// modify stats depending on species - not all pets have this
auto speciesState = _battlePetSpeciesStates.find(PacketInfo.Species);
Expand Down Expand Up @@ -538,7 +534,7 @@ void BattlePetMgr::ModifyName(ObjectGuid guid, std::string const& name, std::uni
summonedBattlePet->SetBattlePetCompanionNameTimestamp(pet->NameTimestamp);
}

bool BattlePetMgr::IsPetInSlot(ObjectGuid guid)
bool BattlePetMgr::IsPetInSlot(ObjectGuid guid) const
{
for (WorldPackets::BattlePet::BattlePetSlot const& slot : _slots)
if (slot.Pet.Guid == guid)
Expand All @@ -549,21 +545,21 @@ bool BattlePetMgr::IsPetInSlot(ObjectGuid guid)

uint8 BattlePetMgr::GetPetCount(BattlePetSpeciesEntry const* battlePetSpecies, ObjectGuid ownerGuid) const
{
return uint8(std::count_if(_pets.begin(), _pets.end(), [battlePetSpecies, ownerGuid](std::pair<uint64 const, BattlePet> const& pet)
return uint8(std::ranges::count_if(_pets, [battlePetSpecies, ownerGuid](BattlePet const& pet)
{
if (pet.second.PacketInfo.Species != battlePetSpecies->ID)
if (pet.PacketInfo.Species != battlePetSpecies->ID)
return false;

if (pet.second.SaveInfo == BATTLE_PET_REMOVED)
if (pet.SaveInfo == BATTLE_PET_REMOVED)
return false;

if (battlePetSpecies->GetFlags().HasFlag(BattlePetSpeciesFlags::NotAccountWide))
if (!ownerGuid.IsEmpty() && pet.second.PacketInfo.OwnerInfo)
if (pet.second.PacketInfo.OwnerInfo->Guid != ownerGuid)
if (!ownerGuid.IsEmpty() && pet.PacketInfo.OwnerInfo)
if (pet.PacketInfo.OwnerInfo->Guid != ownerGuid)
return false;

return true;
}));
}, Trinity::Containers::MapValue));
}

bool BattlePetMgr::HasMaxPetCount(BattlePetSpeciesEntry const* battlePetSpecies, ObjectGuid ownerGuid) const
Expand All @@ -576,10 +572,10 @@ bool BattlePetMgr::HasMaxPetCount(BattlePetSpeciesEntry const* battlePetSpecies,
uint32 BattlePetMgr::GetPetUniqueSpeciesCount() const
{
std::set<uint32> speciesIds;
std::transform(_pets.begin(), _pets.end(), std::inserter(speciesIds, speciesIds.end()), [](std::pair<uint64 const, BattlePet> const& pet)
std::ranges::transform(_pets, std::inserter(speciesIds, speciesIds.end()), [](BattlePet const& pet)
{
return pet.second.PacketInfo.Species;
});
return pet.PacketInfo.Species;
}, Trinity::Containers::MapValue);
return speciesIds.size();
}

Expand Down Expand Up @@ -885,7 +881,7 @@ void BattlePetMgr::SendJournal()
battlePetJournal.Pets.push_back(std::ref(pet.second.PacketInfo));

battlePetJournal.Slots.reserve(_slots.size());
std::transform(_slots.begin(), _slots.end(), std::back_inserter(battlePetJournal.Slots), [](WorldPackets::BattlePet::BattlePetSlot& slot) { return std::ref(slot); });
std::ranges::transform(_slots, std::back_inserter(battlePetJournal.Slots), [](WorldPackets::BattlePet::BattlePetSlot& slot) { return std::ref(slot); });
_owner->SendPacket(battlePetJournal.Write());
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/game/BattlePets/BattlePetMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class TC_GAME_API BattlePetMgr
void RemovePet(ObjectGuid guid);
void ClearFanfare(ObjectGuid guid);
void ModifyName(ObjectGuid guid, std::string const& name, std::unique_ptr<DeclinedName> declinedName);
bool IsPetInSlot(ObjectGuid guid);
bool IsPetInSlot(ObjectGuid guid) const;

uint8 GetPetCount(BattlePetSpeciesEntry const* battlePetSpecies, ObjectGuid ownerGuid) const;
bool HasMaxPetCount(BattlePetSpeciesEntry const* battlePetSpecies, ObjectGuid ownerGuid) const;
Expand Down
3 changes: 2 additions & 1 deletion src/server/game/DataStores/DB2Stores.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2721,7 +2721,8 @@ MapDifficultyEntry const* DB2Manager::GetDefaultMapDifficulty(uint32 mapId, Diff

// first find any valid difficulty
auto foundDifficulty = std::ranges::find_if(difficultiesForMap->begin(), difficultyEnd,
[](std::pair<uint32 const, MapDifficultyEntry const*> const& p) { return sDifficultyStore.HasRecord(p.first); });
[](uint32 difficultyId) { return sDifficultyStore.HasRecord(difficultyId); },
Trinity::Containers::MapKey);

if (foundDifficulty == difficultyEnd)
return nullptr; // nothing valid was found
Expand Down
2 changes: 1 addition & 1 deletion src/server/game/Entities/Creature/CreatureGroups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ bool CreatureGroup::CanLeaderStartMoving() const

bool CreatureGroup::HasAliveMembers() const
{
return std::ranges::any_of(_members, [](Creature const* member) { return member->IsAlive(); }, &MembersMap::value_type::first);
return std::ranges::any_of(_members, [](Creature const* member) { return member->IsAlive(); }, Trinity::Containers::MapKey);
}

bool CreatureGroup::LeaderHasStringId(std::string_view id) const
Expand Down
4 changes: 2 additions & 2 deletions src/server/game/Entities/Player/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3402,7 +3402,7 @@ void Player::RemoveSpell(uint32 spell_id, bool disabled /*= false*/, bool learn_
if (!learnNode->Active)
return false;
return HasSpell(learnNode->SourceSpell);
}, &SpellLearnedBySpellMap::value_type::second);
}, Trinity::Containers::MapValue);

if (hasOtherSpellTeachingThis)
continue;
Expand Down Expand Up @@ -17087,7 +17087,7 @@ bool Player::HasQuestForCurrency(uint32 currencyId) const
auto hasObjectiveTypeForCurrency = [&](QuestObjectiveType type)
{
return std::ranges::any_of(Trinity::Containers::MapEqualRange(m_questObjectiveStatus, { type, currencyId }),
isCompletableObjective, &QuestObjectiveStatusMap::value_type::second);
isCompletableObjective, Trinity::Containers::MapValue);
};

if (hasObjectiveTypeForCurrency(QUEST_OBJECTIVE_CURRENCY))
Expand Down
13 changes: 5 additions & 8 deletions src/server/game/Globals/ObjectMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
#include "VMapManager2.h"
#include "Vehicle.h"
#include "World.h"
#include "advstd.h"
#include <G3D/g3dmath.h>
#include <limits>
#include <numeric>
Expand Down Expand Up @@ -1528,7 +1529,7 @@ void ObjectMgr::LoadEquipmentTemplates()
continue;
}

if (std::ranges::none_of(InventoryTypesEquipable, [dbcItem](InventoryType inventoryType) { return inventoryType == dbcItem->InventoryType; }))
if (!advstd::ranges::contains(InventoryTypesEquipable, dbcItem->InventoryType))
{
TC_LOG_ERROR("sql.sql", "Item (ID={}) in creature_equip_template.ItemID{} for CreatureID = {} and ID = {} is not equipable in a hand, forced to 0.",
equipmentInfo.Items[i].ItemId, i + 1, entry, id);
Expand Down Expand Up @@ -11776,13 +11777,9 @@ SpawnTrackingTemplateData const* ObjectMgr::GetSpawnTrackingData(uint32 spawnTra

bool ObjectMgr::IsQuestObjectiveForSpawnTracking(uint32 spawnTrackingId, uint32 questObjectiveId) const
{
auto itr = _spawnTrackingQuestObjectiveStore.find(spawnTrackingId);
if (itr != _spawnTrackingQuestObjectiveStore.end())
{
std::vector<QuestObjective const*> const* questObjectiveList = &itr->second;
if (std::ranges::find(*questObjectiveList, questObjectiveId, &QuestObjective::ID) != (*questObjectiveList).end())
return true;
}
if (std::vector<QuestObjective const*> const* questObjectiveList = Trinity::Containers::MapGetValuePtr(_spawnTrackingQuestObjectiveStore, spawnTrackingId))
return advstd::ranges::contains(*questObjectiveList, questObjectiveId, &QuestObjective::ID);

return false;
}

Expand Down
7 changes: 2 additions & 5 deletions src/server/game/Loot/Loot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,7 @@ Optional<LootSlotType> LootItem::GetUiTypeForPlayer(Player const* player, Loot c
{
if (NotNormalLootItemList const* ffaItems = Trinity::Containers::MapGetValuePtr(loot.GetPlayerFFAItems(), player->GetGUID()))
{
auto ffaItemItr = std::ranges::find_if(*ffaItems, [&](NotNormalLootItem const& ffaItem)
{
return ffaItem.LootListId == LootListId;
});
auto ffaItemItr = std::ranges::find(*ffaItems, LootListId, &NotNormalLootItem::LootListId);
if (ffaItemItr != ffaItems->end() && !ffaItemItr->is_looted)
return loot.GetLootMethod() == FREE_FOR_ALL ? LOOT_SLOT_TYPE_OWNER : LOOT_SLOT_TYPE_ALLOW_LOOT;
}
Expand Down Expand Up @@ -1128,7 +1125,7 @@ bool Loot::hasItemFor(Player const* player) const
return true;

if (NotNormalLootItemList const* ffaItems = Trinity::Containers::MapGetValuePtr(GetPlayerFFAItems(), player->GetGUID()))
if (std::ranges::any_of(*ffaItems, std::identity(), &NotNormalLootItem::is_looted))
if (std::ranges::any_of(*ffaItems, &NotNormalLootItem::is_looted))
return true;

return false;
Expand Down
2 changes: 1 addition & 1 deletion src/server/game/Loot/LootMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ uint32 LootStore::LoadAndCollectLootIds(LootIdSet& lootIdSet)
{
uint32 count = LoadLootTable();

std::ranges::transform(m_LootTemplates, std::inserter(lootIdSet, lootIdSet.end()), &LootTemplateMap::value_type::first);
std::ranges::transform(m_LootTemplates, std::inserter(lootIdSet, lootIdSet.end()), Trinity::Containers::MapKey);

return count;
}
Expand Down
7 changes: 2 additions & 5 deletions src/server/game/Spells/Spell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9609,10 +9609,7 @@ void SelectRandomInjuredTargets(std::list<WorldObject*>& targets, size_t maxTarg
return std::make_pair(target, negativePoints);
});

std::sort(tempTargets.begin(), tempTargets.end(), [](std::pair<WorldObject*, int32> const& left, std::pair<WorldObject*, int32> const& right)
{
return left.second < right.second;
});
std::ranges::sort(tempTargets, {}, Trinity::TupleElement<1>);

std::size_t foundTargets = 0;
for (std::ptrdiff_t countForPriority : countsByPriority)
Expand All @@ -9630,7 +9627,7 @@ void SelectRandomInjuredTargets(std::list<WorldObject*>& targets, size_t maxTarg
}

targets.resize(maxTargets);
std::transform(tempTargets.begin(), tempTargets.begin() + maxTargets, targets.begin(), std::mem_fn(&std::pair<WorldObject*, int32>::first));
std::ranges::transform(tempTargets.begin(), tempTargets.begin() + maxTargets, targets.begin(), Trinity::TupleElement<0>);
}
} //namespace Trinity

Expand Down
3 changes: 2 additions & 1 deletion src/server/scripts/Commands/cs_npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ EndScriptData */
#include "Language.h"
#include "Loot.h"
#include "Map.h"
#include "MapUtils.h"
#include "MotionMaster.h"
#include "MovementDefines.h"
#include "ObjectAccessor.h"
Expand Down Expand Up @@ -1313,7 +1314,7 @@ class npc_commandscript : public CommandScript

Loot const* loot = creatureTarget->m_loot.get();
if ((!loot || loot->isLooted())
&& !std::ranges::count_if(creatureTarget->m_personalLoot, std::not_fn(&Loot::isLooted), &std::unordered_map<ObjectGuid, std::unique_ptr<Loot>>::value_type::second))
&& !std::ranges::count_if(creatureTarget->m_personalLoot, std::not_fn(&Loot::isLooted), Trinity::Containers::MapValue))
{
handler->PSendSysMessage(LANG_COMMAND_NOT_DEAD_OR_NO_LOOT, creatureTarget->GetName().c_str());
handler->SetSentErrorMessage(true);
Expand Down

0 comments on commit 00223f3

Please sign in to comment.