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
@@ -44,7 +44,6 @@ struct boss_ayamissAI : public ScriptedAI
pInstance = c->GetInstanceData();
}
Unit *pTarget;
uint32 STINGERSPRAY_Timer;
uint32 POISONSTINGER_Timer;
uint32 SUMMONSWARMER_Timer;
@@ -54,7 +53,6 @@ struct boss_ayamissAI : public ScriptedAI
void Reset()
{
pTarget = NULL;
STINGERSPRAY_Timer = 30000;
POISONSTINGER_Timer = 30000;
SUMMONSWARMER_Timer = 60000;
@@ -66,8 +64,6 @@ struct boss_ayamissAI : public ScriptedAI
void EnterCombat(Unit *who)
{
pTarget = who;
if (pInstance)
pInstance->SetData(DATA_AYAMISS_EVENT, IN_PROGRESS);
}
@@ -62,8 +62,6 @@ struct boss_skeramAI : public ScriptedAI
uint32 Blink_Timer;
uint32 Invisible_Timer;
Creature *Image1, *Image2;
bool Images75;
bool Images50;
bool Images25;
@@ -260,7 +258,7 @@ struct boss_skeramAI : public ScriptedAI
Unit *pTarget = SelectUnit(SELECT_TARGET_RANDOM,0);
Image1 = m_creature->SummonCreature(15263, i1->x, i1->y, i1->z, i1->r, TEMPSUMMON_CORPSE_DESPAWN, 30000);
Creature *Image1 = m_creature->SummonCreature(15263, i1->x, i1->y, i1->z, i1->r, TEMPSUMMON_CORPSE_DESPAWN, 30000);
if (Image1)
{
Image1->SetMaxHealth(m_creature->GetMaxHealth() / 5);
@@ -270,7 +268,7 @@ struct boss_skeramAI : public ScriptedAI
CAST_AI(boss_skeramAI, Image1->AI())->IsImage = true;
}
Image2 = m_creature->SummonCreature(15263,i2->x, i2->y, i2->z, i2->r, TEMPSUMMON_CORPSE_DESPAWN, 30000);
Creature *Image2 = m_creature->SummonCreature(15263,i2->x, i2->y, i2->z, i2->r, TEMPSUMMON_CORPSE_DESPAWN, 30000);
if (Image2)
{
Image2->SetMaxHealth(m_creature->GetMaxHealth() / 5);
@@ -80,24 +80,25 @@ struct aqsentinelAI : public ScriptedAI
abselected = 0; // just initialization of variable
}
Creature *nearby[3];
uint64 NearbyGUID[3];
void ClearBuddyList()
{
nearby[0] = nearby[1] = nearby[2] = NULL;
NearbyGUID[0] = NearbyGUID[1] = NearbyGUID[2] = 0;
}
void AddBuddyToList(Creature *c)
void AddBuddyToList(uint64 CreatureGUID)
{
if (c==m_creature)
if (CreatureGUID == m_creature->GetGUID())
return;
for (int i=0; i<3; ++i)
{
if (nearby[i] == c)
if (NearbyGUID[i] == CreatureGUID)
return;
if (!nearby[i])
if (!NearbyGUID[i])
{
nearby[i] = c;
NearbyGUID[i] = CreatureGUID;
return;
}
}
@@ -107,23 +108,23 @@ struct aqsentinelAI : public ScriptedAI
{
aqsentinelAI *cai = CAST_AI(aqsentinelAI, (c)->AI());
for (int i=0; i<3; ++i)
if (nearby[i] && nearby[i]!=c)
cai->AddBuddyToList(nearby[i]);
cai->AddBuddyToList(m_creature);
if (NearbyGUID[i] && NearbyGUID[i] != c->GetGUID())
cai->AddBuddyToList(NearbyGUID[i]);
cai->AddBuddyToList(m_creature->GetGUID());
}
void SendMyListToBuddies()
{
for (int i=0; i<3; ++i)
if (nearby[i])
GiveBuddyMyList(nearby[i]);
if (Creature *pNearby = Unit::GetCreature(*m_creature, NearbyGUID[i]))
GiveBuddyMyList(pNearby);
}
void CallBuddiesToAttack(Unit *who)
{
for (int i=0; i<3; ++i)
{
Creature *c = nearby[i];
Creature *c = Unit::GetCreature(*m_creature, NearbyGUID[i]);
if (c)
{
if (!c->isInCombat())
@@ -145,7 +146,7 @@ struct aqsentinelAI : public ScriptedAI
return;
for (std::list<Creature*>::iterator iter = assistList.begin(); iter != assistList.end(); ++iter)
AddBuddyToList((*iter));
AddBuddyToList((*iter)->GetGUID());
}
int pickAbilityRandom(bool *chosenAbilities)
@@ -175,11 +176,16 @@ struct aqsentinelAI : public ScriptedAI
int bli;
for (bli = 0; bli < 3; ++bli)
{
if (!nearby[bli])
if (!NearbyGUID[bli])
break;
AddSentinelsNear(nearby[bli]);
CAST_AI(aqsentinelAI, nearby[bli]->AI())->gatherOthersWhenAggro = false;
CAST_AI(aqsentinelAI, nearby[bli]->AI())->selectAbility(pickAbilityRandom(chosenAbilities));
Creature *pNearby = Unit::GetCreature(*m_creature, NearbyGUID[bli]);
if(!pNearby)
break;
AddSentinelsNear(pNearby);
CAST_AI(aqsentinelAI, pNearby->AI())->gatherOthersWhenAggro = false;
CAST_AI(aqsentinelAI, pNearby->AI())->selectAbility(pickAbilityRandom(chosenAbilities));
}
/*if (bli < 3)
DoYell("I dont have enough buddies.", LANG_NEUTRAL, 0);*/
@@ -197,10 +203,13 @@ struct aqsentinelAI : public ScriptedAI
{
for (int i=0; i<3; ++i)
{
if (!nearby[i])
if (!NearbyGUID[i])
continue;
if (nearby[i]->isDead())
nearby[i]->Respawn();
if (Creature *pNearby = Unit::GetCreature(*m_creature, NearbyGUID[i]))
{
if (pNearby->isDead())
pNearby->Respawn();
}
}
}
ClearBuddyList();
@@ -225,7 +234,7 @@ struct aqsentinelAI : public ScriptedAI
{
for (int ni=0; ni<3; ++ni)
{
Creature *sent = nearby[ni];
Creature *sent = Unit::GetCreature(*m_creature, NearbyGUID[ni]);
if (!sent)
continue;
if (sent->isDead())