Core/ObjectMgr: Refactor sCreatureModelStorage

This commit is contained in:
leak
2011-04-28 22:11:45 +02:00
parent 6a431f745c
commit 85a1e9e7ab
8 changed files with 112 additions and 106 deletions
+12 -16
View File
@@ -299,18 +299,16 @@ bool Creature::InitEntry(uint32 Entry, uint32 /*team*/, const CreatureData *data
return false;
}
uint32 display_id = sObjectMgr->ChooseDisplayId(0, GetCreatureInfo(), data);
CreatureModelInfo const *minfo = sObjectMgr->GetCreatureModelRandomGender(display_id);
uint32 displayID = sObjectMgr->ChooseDisplayId(0, GetCreatureInfo(), data);
CreatureModelInfo const *minfo = sObjectMgr->GetCreatureModelRandomGender(displayID);
if (!minfo) // Cancel load if no model defined
{
sLog->outErrorDb("Creature (Entry: %u) has no model defined in table `creature_template`, can't load. ",Entry);
return false;
}
display_id = minfo->modelid; // it can be different (for another gender)
SetDisplayId(display_id);
SetNativeDisplayId(display_id);
SetDisplayId(displayID);
SetNativeDisplayId(displayID);
SetByteValue(UNIT_FIELD_BYTES_0, 2, minfo->gender);
// Load creature equipment
@@ -780,13 +778,12 @@ bool Creature::Create(uint32 guidlow, Map *map, uint32 phaseMask, uint32 Entry,
}
LoadCreaturesAddon();
CreatureModelInfo const *minfo = sObjectMgr->GetCreatureModelRandomGender(GetNativeDisplayId());
uint32 displayID = 0;
CreatureModelInfo const *minfo = sObjectMgr->GetCreatureModelRandomGender(displayID = GetNativeDisplayId());
if (minfo && !isTotem()) // Cancel load if no model defined or if totem
{
uint32 display_id = minfo->modelid; // it can be different (for another gender)
SetDisplayId(display_id);
SetNativeDisplayId(display_id);
SetDisplayId(displayID);
SetNativeDisplayId(displayID);
SetByteValue(UNIT_FIELD_BYTES_0, 2, minfo->gender);
}
@@ -1620,13 +1617,12 @@ void Creature::Respawn(bool force)
else
setDeathState(JUST_ALIVED);
CreatureModelInfo const *minfo = sObjectMgr->GetCreatureModelRandomGender(GetNativeDisplayId());
uint32 displayID = 0;
CreatureModelInfo const *minfo = sObjectMgr->GetCreatureModelRandomGender(displayID = GetNativeDisplayId());
if (minfo) // Cancel load if no model defined
{
uint32 display_id = minfo->modelid; // it can be different (for another gender)
SetDisplayId(display_id);
SetNativeDisplayId(display_id);
SetDisplayId(displayID);
SetNativeDisplayId(displayID);
SetByteValue(UNIT_FIELD_BYTES_0, 2, minfo->gender);
}
+3 -1
View File
@@ -281,13 +281,15 @@ struct CreatureDataAddon
struct CreatureModelInfo
{
uint32 modelid;
float bounding_radius;
float combat_reach;
uint8 gender;
uint32 modelid_other_gender;
};
// Benchmarked: Faster than std::map (insert/find)
typedef UNORDERED_MAP<uint16, CreatureModelInfo> CreatureModelContainer;
enum InhabitTypeValues
{
INHABIT_GROUND = 1,
+10 -12
View File
@@ -57,34 +57,32 @@ void Totem::InitStats(uint32 duration)
CreatureInfo const *cinfo = GetCreatureInfo();
if (m_owner->GetTypeId() == TYPEID_PLAYER && cinfo)
{
uint32 display_id = sObjectMgr->ChooseDisplayId(m_owner->ToPlayer()->GetTeam(), cinfo);
CreatureModelInfo const *minfo = sObjectMgr->GetCreatureModelRandomGender(display_id);
if (minfo)
display_id = minfo->modelid;
uint32 displayID = sObjectMgr->ChooseDisplayId(m_owner->ToPlayer()->GetTeam(), cinfo);
CreatureModelInfo const *minfo = sObjectMgr->GetCreatureModelRandomGender(displayID);
switch (m_owner->ToPlayer()->GetTeam())
{
case ALLIANCE:
display_id = cinfo->Modelid1;
displayID = cinfo->Modelid1;
break;
case HORDE:
if (cinfo->Modelid3)
display_id = cinfo->Modelid3;
displayID = cinfo->Modelid3;
else
display_id = cinfo->Modelid1;
displayID = cinfo->Modelid1;
switch (((Player*)m_owner)->getRace())
{
case RACE_ORC:
if (cinfo->Modelid2)
display_id = cinfo->Modelid2;
displayID = cinfo->Modelid2;
else
display_id = cinfo->Modelid1;
displayID = cinfo->Modelid1;
break;
case RACE_TROLL:
if (cinfo->Modelid4)
display_id = cinfo->Modelid4;
displayID = cinfo->Modelid4;
else
display_id = cinfo->Modelid1;
displayID = cinfo->Modelid1;
break;
default:
break;
@@ -93,7 +91,7 @@ void Totem::InitStats(uint32 duration)
default:
break;
}
SetDisplayId(display_id);
SetDisplayId(displayID);
}
// Get spell casted by totem
+5 -7
View File
@@ -1299,7 +1299,7 @@ void GameEventMgr::ChangeEquipOrModel(int16 event_id, bool activate)
pCreature->LoadEquipment(itr->second.equipment_id, true);
if (itr->second.modelid >0 && itr->second.modelid_prev != itr->second.modelid)
{
CreatureModelInfo const *minfo = sObjectMgr->GetCreatureModelInfo(itr->second.modelid);
CreatureModelInfo const* minfo = sObjectMgr->GetCreatureModelInfo(itr->second.modelid);
if (minfo)
{
pCreature->SetDisplayId(itr->second.modelid);
@@ -1314,7 +1314,7 @@ void GameEventMgr::ChangeEquipOrModel(int16 event_id, bool activate)
pCreature->LoadEquipment(itr->second.equipement_id_prev, true);
if (itr->second.modelid_prev >0 && itr->second.modelid_prev != itr->second.modelid)
{
CreatureModelInfo const *minfo = sObjectMgr->GetCreatureModelInfo(itr->second.modelid_prev);
CreatureModelInfo const* minfo = sObjectMgr->GetCreatureModelInfo(itr->second.modelid_prev);
if (minfo)
{
pCreature->SetDisplayId(itr->second.modelid_prev);
@@ -1331,16 +1331,14 @@ void GameEventMgr::ChangeEquipOrModel(int16 event_id, bool activate)
if (data2 && activate)
{
CreatureInfo const *cinfo = ObjectMgr::GetCreatureTemplate(data2->id);
uint32 display_id = sObjectMgr->ChooseDisplayId(0,cinfo,data2);
CreatureModelInfo const *minfo = sObjectMgr->GetCreatureModelRandomGender(display_id);
if (minfo)
display_id = minfo->modelid;
uint32 displayID = sObjectMgr->ChooseDisplayId(0,cinfo,data2);
CreatureModelInfo const *minfo = sObjectMgr->GetCreatureModelRandomGender(displayID);
if (data2->equipmentId == 0)
itr->second.equipement_id_prev = cinfo->equipmentId;
else if (data2->equipmentId != -1)
itr->second.equipement_id_prev = data->equipmentId;
itr->second.modelid_prev = display_id;
itr->second.modelid_prev = displayID;
}
}
// now last step: put in data
+68 -49
View File
@@ -687,8 +687,8 @@ void ObjectMgr::CheckCreatureTemplate(CreatureInfo const* cInfo)
else if (!displayScaleEntry)
displayScaleEntry = displayEntry;
CreatureModelInfo const* minfo = sCreatureModelStorage.LookupEntry<CreatureModelInfo>(cInfo->Modelid1);
if (!minfo)
CreatureModelInfo const* modelInfo = GetCreatureModelInfo(cInfo->Modelid1);
if (!modelInfo)
sLog->outErrorDb("No model data exist for `Modelid1` = %u listed by creature (Entry: %u).", cInfo->Modelid1, cInfo->Entry);
}
@@ -703,8 +703,8 @@ void ObjectMgr::CheckCreatureTemplate(CreatureInfo const* cInfo)
else if (!displayScaleEntry)
displayScaleEntry = displayEntry;
CreatureModelInfo const* minfo = sCreatureModelStorage.LookupEntry<CreatureModelInfo>(cInfo->Modelid2);
if (!minfo)
CreatureModelInfo const* modelInfo = GetCreatureModelInfo(cInfo->Modelid2);;
if (!modelInfo)
sLog->outErrorDb("No model data exist for `Modelid2` = %u listed by creature (Entry: %u).", cInfo->Modelid2, cInfo->Entry);
}
@@ -719,8 +719,8 @@ void ObjectMgr::CheckCreatureTemplate(CreatureInfo const* cInfo)
else if (!displayScaleEntry)
displayScaleEntry = displayEntry;
CreatureModelInfo const* minfo = sCreatureModelStorage.LookupEntry<CreatureModelInfo>(cInfo->Modelid3);
if (!minfo)
CreatureModelInfo const* modelInfo = GetCreatureModelInfo(cInfo->Modelid3);
if (!modelInfo)
sLog->outErrorDb("No model data exist for `Modelid3` = %u listed by creature (Entry: %u).", cInfo->Modelid3, cInfo->Entry);
}
@@ -735,8 +735,8 @@ void ObjectMgr::CheckCreatureTemplate(CreatureInfo const* cInfo)
else if (!displayScaleEntry)
displayScaleEntry = displayEntry;
CreatureModelInfo const* minfo = sCreatureModelStorage.LookupEntry<CreatureModelInfo>(cInfo->Modelid4);
if (!minfo)
CreatureModelInfo const* modelInfo = GetCreatureModelInfo(cInfo->Modelid4);;
if (!modelInfo)
sLog->outErrorDb("No model data exist for `Modelid4` = %u listed by creature (Entry: %u).", cInfo->Modelid4, cInfo->Entry);
}
@@ -1073,9 +1073,13 @@ void ObjectMgr::LoadEquipmentTemplates()
sLog->outString();
}
CreatureModelInfo const* ObjectMgr::GetCreatureModelInfo(uint32 modelid)
CreatureModelInfo const* ObjectMgr::GetCreatureModelInfo(uint32 modelId)
{
return sCreatureModelStorage.LookupEntry<CreatureModelInfo>(modelid);
CreatureModelContainer::const_iterator itr = CreatureModelStore.find(modelId);
if (itr != CreatureModelStore.end())
return &(itr->second);
return NULL;
}
uint32 ObjectMgr::ChooseDisplayId(uint32 /*team*/, const CreatureInfo *cinfo, const CreatureData *data /*= NULL*/)
@@ -1112,9 +1116,9 @@ void ObjectMgr::ChooseCreatureFlags(const CreatureInfo *cinfo, uint32& npcflag,
}
}
CreatureModelInfo const* ObjectMgr::GetCreatureModelRandomGender(uint32 display_id)
CreatureModelInfo const* ObjectMgr::GetCreatureModelRandomGender(uint32 &displayID)
{
CreatureModelInfo const *minfo = GetCreatureModelInfo(display_id);
CreatureModelInfo const* minfo = GetCreatureModelInfo(displayID);
if (!minfo)
return NULL;
@@ -1124,60 +1128,76 @@ CreatureModelInfo const* ObjectMgr::GetCreatureModelRandomGender(uint32 display_
CreatureModelInfo const *minfo_tmp = GetCreatureModelInfo(minfo->modelid_other_gender);
if (!minfo_tmp)
{
sLog->outErrorDb("Model (Entry: %u) has modelid_other_gender %u not found in table `creature_model_info`. ", minfo->modelid, minfo->modelid_other_gender);
return minfo; // not fatal, just use the previous one
sLog->outErrorDb("Model (Entry: %u) has modelid_other_gender %u not found in table `creature_model_info`. ", displayID, minfo->modelid_other_gender);
}
else
{
// Model ID changed
displayID = minfo->modelid_other_gender;
return minfo_tmp;
}
}
else
return minfo;
return minfo;
}
void ObjectMgr::LoadCreatureModelInfo()
{
uint32 oldMSTime = getMSTime();
sCreatureModelStorage.Load();
QueryResult result = WorldDatabase.Query("SELECT modelid, bounding_radius, combat_reach, gender, modelid_other_gender FROM creature_model_info");
// post processing
for (uint32 i = 1; i < sCreatureModelStorage.MaxEntry; ++i)
if (!result)
{
CreatureModelInfo const *minfo = sCreatureModelStorage.LookupEntry<CreatureModelInfo>(i);
if (!minfo)
continue;
if (!sCreatureDisplayInfoStore.LookupEntry(minfo->modelid))
sLog->outErrorDb("Table `creature_model_info` has model for not existed display id (%u).", minfo->modelid);
if (minfo->gender > GENDER_NONE)
{
sLog->outErrorDb("Table `creature_model_info` has wrong gender (%u) for display id (%u).", uint32(minfo->gender), minfo->modelid);
const_cast<CreatureModelInfo*>(minfo)->gender = GENDER_MALE;
}
if (minfo->modelid_other_gender && !sCreatureDisplayInfoStore.LookupEntry(minfo->modelid_other_gender))
{
sLog->outErrorDb("Table `creature_model_info` has not existed alt.gender model (%u) for existed display id (%u).", minfo->modelid_other_gender, minfo->modelid);
const_cast<CreatureModelInfo*>(minfo)->modelid_other_gender = 0;
}
sLog->outString(">> Loaded 0 creature model definitions. DB table `creature_model_info` is empty.");
sLog->outString();
return;
}
// check if combat_reach is valid
for (uint32 i = 1; i < sCreatureModelStorage.MaxEntry; ++i)
uint32 count = 0;
do
{
CreatureModelInfo const* mInfo = sCreatureModelStorage.LookupEntry<CreatureModelInfo>(i);
if (!mInfo)
continue;
Field *fields = result->Fetch();
if (mInfo->combat_reach < 0.1f)
uint32 modelId = fields[0].GetUInt32();
CreatureModelInfo modelInfo;
modelInfo.bounding_radius = fields[1].GetFloat();
modelInfo.combat_reach = fields[2].GetFloat();
modelInfo.gender = fields[3].GetUInt8();
modelInfo.modelid_other_gender = fields[4].GetUInt32();
// Checks
if (!sCreatureDisplayInfoStore.LookupEntry(modelId))
sLog->outErrorDb("Table `creature_model_info` has model for not existed display id (%u).", modelId);
if (modelInfo.gender > GENDER_NONE)
{
//sLog->outErrorDb("Creature model (Entry: %u) has invalid combat reach (%f), setting it to 0.5", mInfo->modelid, mInfo->combat_reach);
const_cast<CreatureModelInfo*>(mInfo)->combat_reach = DEFAULT_COMBAT_REACH;
sLog->outErrorDb("Table `creature_model_info` has wrong gender (%u) for display id (%u).", uint32(modelInfo.gender), modelId);
modelInfo.gender = GENDER_MALE;
}
}
sLog->outString(">> Loaded %u creature model based info in %u ms", sCreatureModelStorage.RecordCount, GetMSTimeDiffToNow(oldMSTime));
if (modelInfo.modelid_other_gender && !sCreatureDisplayInfoStore.LookupEntry(modelInfo.modelid_other_gender))
{
sLog->outErrorDb("Table `creature_model_info` has not existed alt.gender model (%u) for existed display id (%u).", modelInfo.modelid_other_gender, modelId);
modelInfo.modelid_other_gender = 0;
}
if (modelInfo.combat_reach < 0.1f)
{
modelInfo.combat_reach = DEFAULT_COMBAT_REACH;
}
CreatureModelStore[modelId] = modelInfo;
++count;
}
while (result->NextRow());
sLog->outString(">> Loaded %u creature model based info in %u ms", count, GetMSTimeDiffToNow(oldMSTime));
sLog->outString();
}
@@ -6141,9 +6161,8 @@ uint32 ObjectMgr::GetTaxiMountDisplayId(uint32 id, uint32 team, bool allowed_alt
}
}
// minfo is not actually used but the mount_id was updated
CreatureModelInfo const *minfo = sObjectMgr->GetCreatureModelRandomGender(mount_id);
if (minfo)
mount_id = minfo->modelid;
return mount_id;
}
+3 -3
View File
@@ -46,7 +46,6 @@
extern SQLStorage sCreatureStorage;
extern SQLStorage sCreatureDataAddonStorage;
extern SQLStorage sCreatureInfoAddonStorage;
extern SQLStorage sCreatureModelStorage;
extern SQLStorage sEquipmentStorage;
extern SQLStorage sGOStorage;
extern SQLStorage sPageTextStore;
@@ -652,8 +651,8 @@ class ObjectMgr
ArenaTeamMap::iterator GetArenaTeamMapEnd() { return mArenaTeamMap.end(); }
static CreatureInfo const *GetCreatureTemplate(uint32 id) { return sCreatureStorage.LookupEntry<CreatureInfo>(id); }
CreatureModelInfo const *GetCreatureModelInfo(uint32 modelid);
CreatureModelInfo const* GetCreatureModelRandomGender(uint32 display_id);
CreatureModelInfo const* GetCreatureModelInfo(uint32 modelId);
CreatureModelInfo const* GetCreatureModelRandomGender(uint32 &displayID);
uint32 ChooseDisplayId(uint32 team, const CreatureInfo *cinfo, const CreatureData *data = NULL);
static void ChooseCreatureFlags(const CreatureInfo *cinfo, uint32& npcflag, uint32& unit_flags, uint32& dynamicflags, const CreatureData *data = NULL);
EquipmentInfo const *GetEquipmentInfo(uint32 entry);
@@ -1383,6 +1382,7 @@ class ObjectMgr
MapObjectGuids mMapObjectGuids;
CreatureDataMap mCreatureDataMap;
CreatureModelContainer CreatureModelStore;
LinkedRespawnMap mLinkedRespawnMap;
CreatureLocaleMap mCreatureLocaleMap;
GameObjectDataMap mGameObjectDataMap;
@@ -3416,12 +3416,11 @@ void AuraEffect::HandleAuraTransform(AuraApplication const * aurApp, uint8 mode,
if (target->GetTypeId() == TYPEID_PLAYER)
team = target->ToPlayer()->GetTeam();
uint32 display_id = sObjectMgr->ChooseDisplayId(team,ci);
CreatureModelInfo const *minfo = sObjectMgr->GetCreatureModelRandomGender(display_id);
if (minfo)
display_id = minfo->modelid;
uint32 displayID = sObjectMgr->ChooseDisplayId(team,ci);
target->SetUInt32Value(UNIT_FIELD_MOUNTDISPLAYID,display_id);
CreatureModelInfo const *minfo = sObjectMgr->GetCreatureModelRandomGender(displayID);
target->SetUInt32Value(UNIT_FIELD_MOUNTDISPLAYID,displayID);
}
}
}
@@ -3878,18 +3877,16 @@ void AuraEffect::HandleAuraMounted(AuraApplication const * aurApp, uint8 mode, b
if (target->GetTypeId() == TYPEID_PLAYER)
team = target->ToPlayer()->GetTeam();
uint32 display_id = sObjectMgr->ChooseDisplayId(team,ci);
CreatureModelInfo const *minfo = sObjectMgr->GetCreatureModelRandomGender(display_id);
if (minfo)
display_id = minfo->modelid;
uint32 displayID = sObjectMgr->ChooseDisplayId(team,ci);
CreatureModelInfo const *minfo = sObjectMgr->GetCreatureModelRandomGender(displayID);
//some spell has one aura of mount and one of vehicle
for (uint32 i = 0; i < MAX_SPELL_EFFECTS; ++i)
if (GetSpellProto()->Effect[i] == SPELL_EFFECT_SUMMON
&& GetSpellProto()->EffectMiscValue[i] == GetMiscValue())
display_id = 0;
displayID = 0;
target->Mount(display_id, ci->VehicleId, GetMiscValue());
target->Mount(displayID, ci->VehicleId, GetMiscValue());
}
else
{
@@ -6156,12 +6153,10 @@ void AuraEffect::HandleAuraDummy(AuraApplication const * aurApp, uint8 mode, boo
if (target->GetTypeId() == TYPEID_PLAYER)
team = target->ToPlayer()->GetTeam();
uint32 display_id = sObjectMgr->ChooseDisplayId(team, creatureInfo);
CreatureModelInfo const *minfo = sObjectMgr->GetCreatureModelRandomGender(display_id);
if (minfo)
display_id = minfo->modelid;
uint32 displayID = sObjectMgr->ChooseDisplayId(team, creatureInfo);
CreatureModelInfo const *minfo = sObjectMgr->GetCreatureModelRandomGender(displayID);
target->SetUInt32Value(UNIT_FIELD_MOUNTDISPLAYID, display_id);
target->SetUInt32Value(UNIT_FIELD_MOUNTDISPLAYID, displayID);
}
}
break;