Use GUIDs instead of "global" pointers in ~all boss/instance scripts.

GUIDs are most safer than pointers and in some cases can avoid segmentation faults.

== - ==

Let me show a simple example:

We have a pointer to a creature: Creature *pCreature;

and try to search a creature and store it in the pointer:
pCreature = SearchCreature(ID);

We can check simply if the creature found or not:
if(pCreature)
- if this give true: creature found and we have a memory address which points to the creature
- if this give false: creature not found and this is a NULL pointer

Suppose that we have a valid pointer, so creature found.
We wanna use that pointer somewhere, for example in script phase 3.
But we delete the creature(from world) in phase 1.
In phase 3:
if(pCreature)
    pCreature->DoSomeShit();
The if condition gives true, but pCreature points to an _invalid_ address.
In that address maybe have "nothing" or another creature's object or a player or anything else, dont know that.

~~ - ~~

Let me show the correct way:
Creature's GUID: uint64 CreatureGUID;

search the creature and set the GUID:
Creature *pCreature = SearchCreature(ID);
CreatureGUID = pCreature ? pCreature->GetGUID() : 0; // if pCreature is NULL set the GUID to 0

So we have a GUID and not a pointer.
We can get the creature from that GUID:
Creature *pCreature = GetCreature(CreatureGUID);
and we can simply check it is valid or not(found or not):
if(pCreature)

So we deleted the creature in phase 1 and try to use it in phase 3:
Creature *pCreature = GetCreature(CreatureGUID);
And this gives NULL because GetCreature() can't find the creature which GUID is CreatureGUID.

if(pCreature) // pCreature is NULL
    pCreature->DoSomeShit(); // DoSomeShit() not called

== - ==

Remove some not used variables.
Some clean.

TODO: Should search/fix these in zones scripts.

--HG--
branch : trunk
This commit is contained in:
Anubisss
2010-03-27 13:54:38 +01:00
parent 099ef2a9b6
commit be739e9900
35 changed files with 517 additions and 444 deletions
@@ -89,6 +89,7 @@ struct boss_svalaAI : public ScriptedAI
IntroPhase Phase;
TempSummon* pArthas;
uint64 ArthasGUID;
ScriptedInstance* pInstance;
@@ -97,7 +98,7 @@ struct boss_svalaAI : public ScriptedAI
Phase = IDLE;
uiIntroTimer = 1000;
uiIntroPhase = 0;
pArthas = NULL;
ArthasGUID = 0;
if (pInstance)
pInstance->SetData(DATA_SVALA_SORROWGRAVE_EVENT, NOT_STARTED);
@@ -113,10 +114,11 @@ struct boss_svalaAI : public ScriptedAI
Phase = INTRO;
m_creature->SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_NON_ATTACKABLE);
if (pArthas = m_creature->SummonCreature(CREATURE_ARTHAS, 295.81, -366.16, 92.57, 1.58, TEMPSUMMON_MANUAL_DESPAWN))
if (Creature *pArthas = m_creature->SummonCreature(CREATURE_ARTHAS, 295.81, -366.16, 92.57, 1.58, TEMPSUMMON_MANUAL_DESPAWN))
{
pArthas->SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_NON_ATTACKABLE|UNIT_FLAG_NOT_SELECTABLE);
pArthas->SetFloatValue(OBJECT_FIELD_SCALE_X, 5);
ArthasGUID = pArthas->GetGUID();
}
}
}
@@ -130,6 +132,7 @@ struct boss_svalaAI : public ScriptedAI
if (uiIntroTimer <= diff)
{
Creature *pArthas = Unit::GetCreature(*m_creature, ArthasGUID);
if (!pArthas)
return;
@@ -171,8 +174,8 @@ struct boss_svalaAI : public ScriptedAI
{
m_creature->SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_NON_ATTACKABLE|UNIT_FLAG_NOT_SELECTABLE);
m_creature->SetDisplayId(DATA_SVALA_DISPLAY_ID);
pArthas->UnSummon();
pArthas = NULL;
((TempSummon*)pArthas)->UnSummon(); // TODO: should need a ToTempSummon()
ArthasGUID = 0;
Phase = FINISHED;
}
else