Skip to content

Commit

Permalink
[11323] Avoid explicit use HIGHGUID_UNIT as creature high guid in gui…
Browse files Browse the repository at this point in the history
…ds or creature creating.

This helper change for allow have in future static spawned vehicles as `creature` table data.

Added CreatureInfo::GetHighGuid() high guid selector, and wrapper CreatureData::GetHighGuid()
for most real cases of usage. Also easy get expected guid form by CreatureData::GetObjectGuid(lowguid).

Also fixed some memory lost cases at creature spawn fail.
  • Loading branch information
VladimirMangos committed Apr 7, 2011
1 parent 4ed7d25 commit d03b17c
Show file tree
Hide file tree
Showing 16 changed files with 151 additions and 76 deletions.
2 changes: 1 addition & 1 deletion src/game/Chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2909,7 +2909,7 @@ ObjectGuid ChatHandler::ExtractGuidFromLink(char** text)
return ObjectGuid();

if (CreatureData const* data = sObjectMgr.GetCreatureData(lowguid))
return ObjectGuid(HIGHGUID_UNIT, data->id, lowguid);
return data->GetObjectGuid(lowguid);
else
return ObjectGuid();
}
Expand Down
38 changes: 23 additions & 15 deletions src/game/Creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@
// apply implementation of the singletons
#include "Policies/SingletonImp.h"


HighGuid CreatureData::GetHighGuid() const
{
// info existence checked at loading
return ObjectMgr::GetCreatureTemplate(id)->GetHighGuid();
}

TrainerSpell const* TrainerSpellData::Find(uint32 spell_id) const
{
TrainerSpellMap::const_iterator itr = spellList.find(spell_id);
Expand Down Expand Up @@ -728,12 +735,12 @@ bool Creature::AIM_Initialize()
return true;
}

bool Creature::Create(uint32 guidlow, CreatureCreatePos& cPos, uint32 Entry, Team team /*= TEAM_NONE*/, const CreatureData *data /*= NULL*/, GameEventCreatureData const* eventData /*= NULL*/)
bool Creature::Create(uint32 guidlow, CreatureCreatePos& cPos, CreatureInfo const* cinfo, Team team /*= TEAM_NONE*/, const CreatureData *data /*= NULL*/, GameEventCreatureData const* eventData /*= NULL*/)
{
SetMap(cPos.GetMap());
SetPhaseMask(cPos.GetPhaseMask(), false);

if (!CreateFromProto(guidlow, Entry, team, data, eventData))
if (!CreateFromProto(guidlow, cinfo, team, data, eventData))
return false;

cPos.SelectFinalPoint(this);
Expand Down Expand Up @@ -1234,19 +1241,13 @@ float Creature::GetSpellDamageMod(int32 Rank)
}
}

bool Creature::CreateFromProto(uint32 guidlow, uint32 Entry, Team team, const CreatureData *data /*=NULL*/, GameEventCreatureData const* eventData /*=NULL*/)
bool Creature::CreateFromProto(uint32 guidlow, CreatureInfo const* cinfo, Team team, const CreatureData *data /*=NULL*/, GameEventCreatureData const* eventData /*=NULL*/)
{
CreatureInfo const *cinfo = ObjectMgr::GetCreatureTemplate(Entry);
if(!cinfo)
{
sLog.outErrorDb("Creature entry %u does not exist.", Entry);
return false;
}
m_originalEntry = Entry;
m_originalEntry = cinfo->Entry;

Object::_Create(guidlow, Entry, HIGHGUID_UNIT);
Object::_Create(guidlow, cinfo->Entry, cinfo->GetHighGuid());

if (!UpdateEntry(Entry, team, data, eventData, false))
if (!UpdateEntry(cinfo->Entry, team, data, eventData, false))
return false;

return true;
Expand All @@ -1262,15 +1263,22 @@ bool Creature::LoadFromDB(uint32 guidlow, Map *map)
return false;
}

CreatureInfo const *cinfo = ObjectMgr::GetCreatureTemplate(data->id);
if(!cinfo)
{
sLog.outErrorDb("Creature (Entry: %u) not found in table `creature_template`, can't load. ", data->id);
return false;
}

GameEventCreatureData const* eventData = sGameEventMgr.GetCreatureUpdateDataForActiveEvent(guidlow);

// Creature can be loaded already in map if grid has been unloaded while creature walk to another grid
if (map->GetCreature(ObjectGuid(HIGHGUID_UNIT, data->id, guidlow)))
if (map->GetCreature(data->GetObjectGuid(guidlow)))
return false;

CreatureCreatePos pos(map, data->posX, data->posY, data->posZ, data->orientation, data->phaseMask);

if (!Create(guidlow, pos, data->id, TEAM_NONE, data, eventData))
if (!Create(guidlow, pos, cinfo, TEAM_NONE, data, eventData))
return false;

m_respawnradius = data->spawndist;
Expand Down Expand Up @@ -2427,7 +2435,7 @@ struct AddCreatureToRemoveListInMapsWorker

void Creature::AddToRemoveListInMaps(uint32 db_guid, CreatureData const* data)
{
AddCreatureToRemoveListInMapsWorker worker(ObjectGuid(HIGHGUID_UNIT, data->id, db_guid));
AddCreatureToRemoveListInMapsWorker worker(data->GetObjectGuid(db_guid));
sMapMgr.DoForAllMapsWithMapId(data->mapid, worker);
}

Expand Down
14 changes: 12 additions & 2 deletions src/game/Creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ struct CreatureInfo
uint32 ScriptID;

// helpers
// TODO: return HIGHGUID_UNIT/HIGHGUID_VEHICLE base at currently missing creature template data
HighGuid GetHighGuid() const
{
return HIGHGUID_UNIT;
}

SkillType GetRequiredLootSkill() const
{
if(type_flags & CREATURE_TYPEFLAGS_HERBLOOT)
Expand Down Expand Up @@ -193,6 +199,10 @@ struct CreatureData
bool is_dead;
uint8 movementType;
uint8 spawnMask;

// helper function
HighGuid GetHighGuid() const;
ObjectGuid GetObjectGuid(uint32 lowguid) const { return ObjectGuid(GetHighGuid(), id, lowguid); }
};

struct CreatureDataAddonAura
Expand Down Expand Up @@ -443,7 +453,7 @@ class MANGOS_DLL_SPEC Creature : public Unit
void AddToWorld();
void RemoveFromWorld();

bool Create(uint32 guidlow, CreatureCreatePos& cPos, uint32 Entry, Team team = TEAM_NONE, const CreatureData *data = NULL, GameEventCreatureData const* eventData = NULL);
bool Create(uint32 guidlow, CreatureCreatePos& cPos, CreatureInfo const* cinfo, Team team = TEAM_NONE, const CreatureData *data = NULL, GameEventCreatureData const* eventData = NULL);
bool LoadCreatureAddon(bool reload = false);
void SelectLevel(const CreatureInfo *cinfo, float percentHealth = 100.0f, float percentMana = 100.0f);
void LoadEquipment(uint32 equip_entry, bool force=false);
Expand Down Expand Up @@ -699,7 +709,7 @@ class MANGOS_DLL_SPEC Creature : public Unit
void SetVirtualItem(VirtualItemSlot slot, uint32 item_id) { SetUInt32Value(UNIT_VIRTUAL_ITEM_SLOT_ID + slot, item_id); }

protected:
bool CreateFromProto(uint32 guidlow,uint32 Entry, Team team, const CreatureData *data = NULL, GameEventCreatureData const* eventData =NULL);
bool CreateFromProto(uint32 guidlow, CreatureInfo const* cinfo, Team team, const CreatureData *data = NULL, GameEventCreatureData const* eventData =NULL);
bool InitEntry(uint32 entry, const CreatureData* data = NULL, GameEventCreatureData const* eventData = NULL);

uint32 m_groupLootTimer; // (msecs)timer used for group loot
Expand Down
2 changes: 1 addition & 1 deletion src/game/GameEventMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ void GameEventMgr::UpdateCreatureData(int16 event_id, bool activate)
continue;

// Update if spawned
GameEventUpdateCreatureDataInMapsWorker worker(ObjectGuid(HIGHGUID_UNIT, data->id, itr->first), data, &itr->second, activate);
GameEventUpdateCreatureDataInMapsWorker worker(data->GetObjectGuid(itr->first), data, &itr->second, activate);
sMapMgr.DoForAllMapsWithMapId(data->mapid, worker);
}
}
Expand Down
51 changes: 34 additions & 17 deletions src/game/Level2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,15 @@ bool ChatHandler::HandleNpcAddCommand(char* args)
if (!ExtractUint32KeyFromLink(&args, "Hcreature_entry", id))
return false;

CreatureInfo const *cinfo = ObjectMgr::GetCreatureTemplate(id);
//FIXME: need vehicle support like GenerateStaticCreatureLowGuid when its will allowed static spawns
if (!cinfo || cinfo->GetHighGuid() != HIGHGUID_UNIT)
{
PSendSysMessage(LANG_COMMAND_INVALIDCREATUREID, id);
SetSentErrorMessage(true);
return false;
}

Player *chr = m_session->GetPlayer();
CreatureCreatePos pos(chr, chr->GetOrientation());
Map *map = chr->GetMap();
Expand All @@ -1573,7 +1582,7 @@ bool ChatHandler::HandleNpcAddCommand(char* args)
return false;
}

if (!pCreature->Create(lowguid, pos, id))
if (!pCreature->Create(lowguid, pos, cinfo))
{
delete pCreature;
return false;
Expand Down Expand Up @@ -1695,7 +1704,7 @@ bool ChatHandler::HandleNpcAddMoveCommand(char* args)
return false;
}

Creature* pCreature = player->GetMap()->GetCreature(ObjectGuid(HIGHGUID_UNIT, data->id, lowguid));
Creature* pCreature = player->GetMap()->GetCreature(data->GetObjectGuid(lowguid));

sWaypointMgr.AddLastNode(lowguid, player->GetPositionX(), player->GetPositionY(), player->GetPositionZ(), player->GetOrientation(), wait, 0);

Expand Down Expand Up @@ -1796,7 +1805,7 @@ bool ChatHandler::HandleNpcDeleteCommand(char* args)
return false;

if (CreatureData const* data = sObjectMgr.GetCreatureData(lowguid))
unit = m_session->GetPlayer()->GetMap()->GetCreature(ObjectGuid(HIGHGUID_UNIT, data->id, lowguid));
unit = m_session->GetPlayer()->GetMap()->GetCreature(data->GetObjectGuid(lowguid));
}
else
unit = getSelectedCreature();
Expand Down Expand Up @@ -1848,7 +1857,7 @@ bool ChatHandler::HandleNpcMoveCommand(char* args)
return false;
}

pCreature = player->GetMap()->GetCreature(ObjectGuid(HIGHGUID_UNIT, data->id, lowguid));
pCreature = player->GetMap()->GetCreature(data->GetObjectGuid(lowguid));
}
else
lowguid = pCreature->GetGUIDLow();
Expand Down Expand Up @@ -1931,7 +1940,7 @@ bool ChatHandler::HandleNpcSetMoveTypeCommand(char* args)
return false;
}

pCreature = player->GetMap()->GetCreature(ObjectGuid(HIGHGUID_UNIT, data->id, lowguid));
pCreature = player->GetMap()->GetCreature(data->GetObjectGuid(lowguid));
}

MovementGeneratorType move_type;
Expand Down Expand Up @@ -2867,7 +2876,7 @@ bool ChatHandler::HandleWpAddCommand(char* args)
return false;
}

target = m_session->GetPlayer()->GetMap()->GetCreature(ObjectGuid(HIGHGUID_UNIT, data->id, lowguid));
target = m_session->GetPlayer()->GetMap()->GetCreature(data->GetObjectGuid(lowguid));
if (!target)
{
PSendSysMessage(LANG_WAYPOINT_NOTFOUNDDBPROBLEM, lowguid);
Expand Down Expand Up @@ -2901,7 +2910,7 @@ bool ChatHandler::HandleWpAddCommand(char* args)
return false;
}

target = m_session->GetPlayer()->GetMap()->GetCreature(ObjectGuid(HIGHGUID_UNIT, data->id, lowguid));
target = m_session->GetPlayer()->GetMap()->GetCreature(data->GetObjectGuid(lowguid));
if (!target || target->IsPet())
{
PSendSysMessage(LANG_WAYPOINT_CREATNOTFOUND, lowguid);
Expand Down Expand Up @@ -2965,6 +2974,10 @@ bool ChatHandler::HandleWpModifyCommand(char* args)
if (!*args)
return false;

CreatureInfo const* waypointInfo = ObjectMgr::GetCreatureTemplate(VISUAL_WAYPOINT);
if (!waypointInfo || waypointInfo->GetHighGuid() != HIGHGUID_UNIT)
return false; // must exist as normal creature in mangos.sql 'creature_template'

// first arg: add del text emote spell waittime move
char* show_str = strtok(args, " ");
if (!show_str)
Expand Down Expand Up @@ -3116,7 +3129,7 @@ bool ChatHandler::HandleWpModifyCommand(char* args)
return false;
}

Creature* npcCreature = m_session->GetPlayer()->GetMap()->GetCreature(ObjectGuid(HIGHGUID_UNIT, data->id, lowguid));
Creature* npcCreature = m_session->GetPlayer()->GetMap()->GetCreature(data->GetObjectGuid(lowguid));

if (!npcCreature)
{
Expand Down Expand Up @@ -3152,7 +3165,7 @@ bool ChatHandler::HandleWpModifyCommand(char* args)

CreatureCreatePos pos(chr, chr->GetOrientation());

if (!wpCreature->Create(map->GenerateLocalLowGuid(HIGHGUID_UNIT), pos, VISUAL_WAYPOINT))
if (!wpCreature->Create(map->GenerateLocalLowGuid(HIGHGUID_UNIT), pos, waypointInfo))
{
PSendSysMessage(LANG_WAYPOINT_VP_NOTCREATED, VISUAL_WAYPOINT);
delete wpCreature;
Expand Down Expand Up @@ -3187,7 +3200,7 @@ bool ChatHandler::HandleWpModifyCommand(char* args)
return false;
}

Creature* npcCreature = m_session->GetPlayer()->GetMap()->GetCreature(ObjectGuid(HIGHGUID_UNIT, data->id, lowguid));
Creature* npcCreature = m_session->GetPlayer()->GetMap()->GetCreature(data->GetObjectGuid(lowguid));

// wpCreature
Creature* wpCreature = NULL;
Expand Down Expand Up @@ -3247,7 +3260,7 @@ bool ChatHandler::HandleWpModifyCommand(char* args)
return false;
}

Creature* npcCreature = m_session->GetPlayer()->GetMap()->GetCreature(ObjectGuid(HIGHGUID_UNIT, data->id, lowguid));
Creature* npcCreature = m_session->GetPlayer()->GetMap()->GetCreature(data->GetObjectGuid(lowguid));

// wpCreature
Creature* wpCreature = NULL;
Expand All @@ -3264,7 +3277,7 @@ bool ChatHandler::HandleWpModifyCommand(char* args)

CreatureCreatePos pos(chr, chr->GetOrientation());

if (!wpCreature2->Create(map->GenerateLocalLowGuid(HIGHGUID_UNIT), pos, VISUAL_WAYPOINT))
if (!wpCreature2->Create(map->GenerateLocalLowGuid(HIGHGUID_UNIT), pos, waypointInfo))
{
PSendSysMessage(LANG_WAYPOINT_VP_NOTCREATED, VISUAL_WAYPOINT);
delete wpCreature2;
Expand Down Expand Up @@ -3312,7 +3325,7 @@ bool ChatHandler::HandleWpModifyCommand(char* args)

sWaypointMgr.SetNodeText(lowguid, point, show_str, arg_str);

Creature* npcCreature = m_session->GetPlayer()->GetMap()->GetCreature(ObjectGuid(HIGHGUID_UNIT, data->id, lowguid));
Creature* npcCreature = m_session->GetPlayer()->GetMap()->GetCreature(data->GetObjectGuid(lowguid));
if (npcCreature)
{
npcCreature->SetDefaultMovementType(WAYPOINT_MOTION_TYPE);
Expand Down Expand Up @@ -3360,6 +3373,10 @@ bool ChatHandler::HandleWpShowCommand(char* args)
if (!*args)
return false;

CreatureInfo const* waypointInfo = ObjectMgr::GetCreatureTemplate(VISUAL_WAYPOINT);
if (!waypointInfo || waypointInfo->GetHighGuid() != HIGHGUID_UNIT)
return false; // must exist as normal creature in mangos.sql 'creature_template'

// first arg: on, off, first, last
char* show_str = strtok(args, " ");
if (!show_str)
Expand Down Expand Up @@ -3412,7 +3429,7 @@ bool ChatHandler::HandleWpShowCommand(char* args)
return false;
}

target = m_session->GetPlayer()->GetMap()->GetCreature(ObjectGuid(HIGHGUID_UNIT, data->id, lowguid));
target = m_session->GetPlayer()->GetMap()->GetCreature(data->GetObjectGuid(lowguid));

if (!target)
{
Expand Down Expand Up @@ -3555,7 +3572,7 @@ bool ChatHandler::HandleWpShowCommand(char* args)

Creature* wpCreature = new Creature;

if (!wpCreature->Create(map->GenerateLocalLowGuid(HIGHGUID_UNIT), pos, VISUAL_WAYPOINT))
if (!wpCreature->Create(map->GenerateLocalLowGuid(HIGHGUID_UNIT), pos, waypointInfo))
{
PSendSysMessage(LANG_WAYPOINT_VP_NOTCREATED, VISUAL_WAYPOINT);
delete wpCreature;
Expand Down Expand Up @@ -3600,7 +3617,7 @@ bool ChatHandler::HandleWpShowCommand(char* args)

Creature* pCreature = new Creature;

if (!pCreature->Create(map->GenerateLocalLowGuid(HIGHGUID_UNIT), pos, VISUAL_WAYPOINT))
if (!pCreature->Create(map->GenerateLocalLowGuid(HIGHGUID_UNIT), pos, waypointInfo))
{
PSendSysMessage(LANG_WAYPOINT_VP_NOTCREATED, VISUAL_WAYPOINT);
delete pCreature;
Expand Down Expand Up @@ -3648,7 +3665,7 @@ bool ChatHandler::HandleWpShowCommand(char* args)

Creature* pCreature = new Creature;

if (!pCreature->Create(map->GenerateLocalLowGuid(HIGHGUID_UNIT), pos, VISUAL_WAYPOINT))
if (!pCreature->Create(map->GenerateLocalLowGuid(HIGHGUID_UNIT), pos, waypointInfo))
{
PSendSysMessage(LANG_WAYPOINT_NOTCREATED, VISUAL_WAYPOINT);
delete pCreature;
Expand Down
9 changes: 8 additions & 1 deletion src/game/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,13 @@ void WorldObject::AddObjectToRemoveList()

Creature* WorldObject::SummonCreature(uint32 id, float x, float y, float z, float ang,TempSummonType spwtype,uint32 despwtime, bool asActiveObject)
{
CreatureInfo const *cinfo = ObjectMgr::GetCreatureTemplate(id);
if(!cinfo)
{
sLog.outErrorDb("WorldObject::SummonCreature: Creature (Entry: %u) not existed for summoner: %s. ", id, GetGuidStr().c_str());
return NULL;
}

TemporarySummon* pCreature = new TemporarySummon(GetObjectGuid());

Team team = TEAM_NONE;
Expand All @@ -1658,7 +1665,7 @@ Creature* WorldObject::SummonCreature(uint32 id, float x, float y, float z, floa
if (x == 0.0f && y == 0.0f && z == 0.0f)
pos = CreatureCreatePos(this, GetOrientation(), CONTACT_DISTANCE, ang);

if (!pCreature->Create(GetMap()->GenerateLocalLowGuid(HIGHGUID_UNIT), pos, id, team))
if (!pCreature->Create(GetMap()->GenerateLocalLowGuid(HIGHGUID_UNIT), pos, cinfo, team))
{
delete pCreature;
return NULL;
Expand Down

0 comments on commit d03b17c

Please sign in to comment.