Skip to content

Commit

Permalink
[11136] Add Creature::FillGuidsListFromThreatList for safe guid itera…
Browse files Browse the repository at this point in the history
…tion from threat list.

Often not 100%-safe iterate by live threat list because called code can modify threat list (reset for example).
  • Loading branch information
VladimirMangos committed Feb 11, 2011
1 parent c71c4e5 commit 66d1972
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/game/Creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2392,3 +2392,18 @@ void Creature::ApplyGameEventSpells(GameEventCreatureData const* eventData, bool
if (cast_spell)
CastSpell(this, cast_spell, true);
}

void Creature::FillGuidsListFromThreatList( std::vector<ObjectGuid>& guids, uint32 maxamount /*= 0*/ )
{
if (!CanHaveThreatList())
return;

ThreatList const& threats = getThreatManager().getThreatList();

maxamount = maxamount > 0 ? std::min(maxamount,uint32(threats.size())) : threats.size();

guids.reserve(guids.size() + maxamount);

for (ThreatList::const_iterator itr = threats.begin(); maxamount && itr != threats.end(); ++itr, --maxamount)
guids.push_back((*itr)->getUnitGuid());
}
3 changes: 3 additions & 0 deletions src/game/Creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,10 @@ class MANGOS_DLL_SPEC Creature : public Unit
bool IsTrainerOf(Player* player, bool msg) const;
bool CanInteractWithBattleMaster(Player* player, bool msg) const;
bool CanTrainAndResetTalentsOf(Player* pPlayer) const;

bool IsOutOfThreatArea(Unit* pVictim) const;
void FillGuidsListFromThreatList(std::vector<ObjectGuid>& guids, uint32 maxamount = 0);

bool IsImmuneToSpell(SpellEntry const* spellInfo);
// redefine Unit::IsImmuneToSpell
bool IsImmuneToSpellEffect(SpellEntry const* spellInfo, SpellEffectIndex index) const;
Expand Down
2 changes: 1 addition & 1 deletion src/shared/revision_nr.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifndef __REVISION_NR_H__
#define __REVISION_NR_H__
#define REVISION_NR "11135"
#define REVISION_NR "11136"
#endif // __REVISION_NR_H__

4 comments on commit 66d1972

@rsa
Copy link
Contributor

@rsa rsa commented on 66d1972 Feb 11, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long live the thread-safe code!
:)

@VladimirMangos
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in fact it can fail and in single thread

@VladimirMangos
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also in mangos this code mostly for scripts that executed for single map objects so not expected any other thread affects base at planned thread usage way.

@rsa
Copy link
Contributor

@rsa rsa commented on 66d1972 Feb 11, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may offer this:
https://github.com/rsa/mangos/commit/fdf656d23a114f5dd3fd17ec77b7ae766bade4a2
not need with current mangos code, but required for thread-safe. tested, worked fine.

Please sign in to comment.