Skip to content

Commit

Permalink
Scripts: Implement targeting using stringId for spell_script_target a…
Browse files Browse the repository at this point in the history
…nd dbscripts
  • Loading branch information
killerwife committed Mar 5, 2023
1 parent 9687751 commit 03d2904
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 70 deletions.
52 changes: 46 additions & 6 deletions src/game/DBScripts/ScriptMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void ScriptMgr::LoadScripts(ScriptMapType scriptType)
}

// generic command args check
if (tmp.buddyEntry && !(tmp.data_flags & SCRIPT_FLAG_BUDDY_BY_GUID))
if (tmp.buddyEntry && !(tmp.data_flags & SCRIPT_FLAG_BUDDY_BY_GUID) && (tmp.data_flags & SCRIPT_FLAG_BUDDY_BY_STRING_ID) == 0)
{
if (tmp.IsCreatureBuddy() && !ObjectMgr::GetCreatureTemplate(tmp.buddyEntry))
{
Expand Down Expand Up @@ -207,7 +207,7 @@ void ScriptMgr::LoadScripts(ScriptMapType scriptType)
CreatureData const* data = sObjectMgr.GetCreatureData(tmp.searchRadiusOrGuid);
if (!data)
{
sLog.outErrorDb("Table `%s` has buddy defined by guid (SCRIPT_FLAG_BUDDY_BY_GUID %u set) but no npc spawned with guid %u, skipping.", tablename, SCRIPT_FLAG_BUDDY_BY_GUID, tmp.searchRadiusOrGuid);
sLog.outErrorDb("Table `%s` for script id %u has buddy defined by guid (SCRIPT_FLAG_BUDDY_BY_GUID %u set) but no npc spawned with guid %u, skipping.", tablename, tmp.id, SCRIPT_FLAG_BUDDY_BY_GUID, tmp.searchRadiusOrGuid);
continue;
}
}
Expand All @@ -216,7 +216,7 @@ void ScriptMgr::LoadScripts(ScriptMapType scriptType)
GameObjectData const* data = sObjectMgr.GetGOData(tmp.searchRadiusOrGuid);
if (!data)
{
sLog.outErrorDb("Table `%s` has go-buddy defined by guid (SCRIPT_FLAG_BUDDY_BY_GUID %u set) but no go spawned with guid %u, skipping.", tablename, SCRIPT_FLAG_BUDDY_BY_GUID, tmp.searchRadiusOrGuid);
sLog.outErrorDb("Table `%s` for script id %u has go-buddy defined by guid (SCRIPT_FLAG_BUDDY_BY_GUID %u set) but no go spawned with guid %u, skipping.", tablename, tmp.id, SCRIPT_FLAG_BUDDY_BY_GUID, tmp.searchRadiusOrGuid);
continue;
}
}
Expand All @@ -228,7 +228,7 @@ void ScriptMgr::LoadScripts(ScriptMapType scriptType)
auto pool = sPoolMgr.GetPoolCreatures(tmp.searchRadiusOrGuid);
if (pool.isEmpty())
{
sLog.outErrorDb("Table `%s` has go-buddy defined by pool (SCRIPT_FLAG_BUDDY_BY_POOL %u set) but pool %u is empty, skipping.", tablename, tmp.data_flags, tmp.searchRadiusOrGuid);
sLog.outErrorDb("Table `%s` for script id %u has go-buddy defined by pool (SCRIPT_FLAG_BUDDY_BY_POOL %u set) but pool %u is empty, skipping.", tablename, tmp.id, tmp.data_flags, tmp.searchRadiusOrGuid);
continue;
}
}
Expand All @@ -237,7 +237,7 @@ void ScriptMgr::LoadScripts(ScriptMapType scriptType)
auto pool = sPoolMgr.GetPoolGameObjects(tmp.searchRadiusOrGuid);
if (pool.isEmpty())
{
sLog.outErrorDb("Table `%s` has go-buddy defined by pool (SCRIPT_FLAG_BUDDY_BY_POOL %u set) but pool %u is empty, skipping.", tablename, tmp.data_flags, tmp.searchRadiusOrGuid);
sLog.outErrorDb("Table `%s` for script id %u has go-buddy defined by pool (SCRIPT_FLAG_BUDDY_BY_POOL %u set) but pool %u is empty, skipping.", tablename, tmp.id, tmp.data_flags, tmp.searchRadiusOrGuid);
continue;
}
}
Expand All @@ -247,7 +247,16 @@ void ScriptMgr::LoadScripts(ScriptMapType scriptType)
uint32 groupEntry = tmp.searchRadiusOrGuid;
if (sObjectMgr.GetSpawnGroupContainer()->spawnGroupMap.find(groupEntry) == sObjectMgr.GetSpawnGroupContainer()->spawnGroupMap.end())
{
sLog.outErrorDb("Table `%s` has go-buddy defined by group (SCRIPT_FLAG_BUDDY_BY_SPAWN_GROUP %u set) but group %u is empty, skipping.", tablename, tmp.data_flags, tmp.searchRadiusOrGuid);
sLog.outErrorDb("Table `%s` for script id %u has go-buddy defined by group (SCRIPT_FLAG_BUDDY_BY_SPAWN_GROUP %u set) but group %u is empty, skipping.", tablename, tmp.id, tmp.data_flags, tmp.searchRadiusOrGuid);
continue;
}
}
else if (tmp.data_flags & SCRIPT_FLAG_BUDDY_BY_STRING_ID)
{
uint32 stringId = tmp.buddyEntry;
if (stringId && !sScriptMgr.ExistsStringId(stringId))
{
sLog.outErrorDb("Table `%s` has buddy defined by stringId (SCRIPT_FLAG_BUDDY_BY_STRING_ID) but stringId does not exist, skipping.", tablename, tmp.id, stringId);
continue;
}
}
Expand Down Expand Up @@ -1430,6 +1439,37 @@ bool ScriptAction::GetScriptProcessTargets(WorldObject* originalSource, WorldObj
buddies.push_back(closest);
}
}
else if (m_script->data_flags & SCRIPT_FLAG_BUDDY_BY_STRING_ID)
{
WorldObject* origin = originalSource ? originalSource : originalTarget;
if (origin->GetTypeId() == TYPEID_PLAYER && originalSource && originalSource->GetTypeId() != TYPEID_PLAYER)
origin = originalTarget;
auto worldObjects = m_map->GetWorldObjects(m_script->buddyEntry);
if (worldObjects == nullptr)
return false;
if ((m_script->data_flags & SCRIPT_FLAG_ALL_ELIGIBLE_BUDDIES) != 0)
{
for (WorldObject* wo : *worldObjects)
buddies.push_back(wo);
}
else
{
WorldObject* closest = nullptr;
for (WorldObject* wo : *worldObjects)
{
if (!closest)
closest = wo;
else if (origin->GetDistance(wo, true, DIST_CALC_NONE) < origin->GetDistance(closest, true, DIST_CALC_NONE))
closest = wo;
}
if (closest)
buddies.push_back(closest);
}
if (m_script->searchRadiusOrGuid > 0)
for (WorldObject* buddy : buddies)
if (buddy->IsWithinDist(origin, m_script->searchRadiusOrGuid))
buddies.erase(std::remove(buddies.begin(), buddies.end(), buddy), buddies.end());
}
else // Buddy by entry
{
if (!originalSource && !originalTarget)
Expand Down
5 changes: 4 additions & 1 deletion src/game/DBScripts/ScriptMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ enum ScriptInfoDataFlags
SCRIPT_FLAG_BUDDY_BY_SPAWN_GROUP = 0x100, // buddy is from spawn group
SCRIPT_FLAG_ALL_ELIGIBLE_BUDDIES = 0x200, // multisource/multitarget - will execute for each eligible
SCRIPT_FLAG_BUDDY_BY_GO = 0x400, // take the buddy by GO (for commands which can target both creature and GO)
SCRIPT_FLAG_BUDDY_BY_STRING_ID = 0x800, // takes buddy from string id - creature or go
};
#define MAX_SCRIPT_FLAG_VALID (2 * SCRIPT_FLAG_BUDDY_BY_GO - 1)
#define MAX_SCRIPT_FLAG_VALID (2 * SCRIPT_FLAG_BUDDY_BY_STRING_ID - 1)

struct ScriptInfo
{
Expand Down Expand Up @@ -669,6 +670,8 @@ class ScriptMgr
std::shared_ptr<ScriptMapMapName> GetScriptMap(ScriptMapType scriptMapType);

bool ExistsStringId(uint32 stringId);
std::shared_ptr<StringIdMap> GetStringIdMap() { return m_stringIds; }
std::shared_ptr<StringIdMapByString> GetStringIdByStringMap() { return m_stringIdsByString; }
private:
void LoadScripts(ScriptMapType scriptType);
void CheckScriptTexts(ScriptMapType scriptType);
Expand Down
6 changes: 4 additions & 2 deletions src/game/Entities/Creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1627,10 +1627,12 @@ bool Creature::LoadFromDB(uint32 dbGuid, Map* map, uint32 newGuid, uint32 forced
return false;

if (groupEntry)
{
SetCreatureGroup(group);

if (groupEntry->StringId)
SetStringId(groupEntry->StringId, true);
if (groupEntry->StringId)
SetStringId(groupEntry->StringId, true);
}

SetRespawnCoord(pos);
m_respawnradius = data->spawndist;
Expand Down
2 changes: 1 addition & 1 deletion src/game/Entities/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ bool GameObject::LoadFromDB(uint32 dbGuid, Map* map, uint32 newGuid, uint32 forc
if (group)
SetGameObjectGroup(group);

if (groupEntry->StringId)
if (groupEntry && groupEntry->StringId)
SetStringId(groupEntry->StringId, true);

if (!GetGOInfo()->GetDespawnPossibility() && !GetGOInfo()->IsDespawnAtAction() && data->spawntimesecsmin >= 0)
Expand Down
5 changes: 5 additions & 0 deletions src/game/Entities/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,11 @@ void WorldObject::RemoveStringId(std::string& stringId)
SetStringId(stringIdId, false);
}

bool WorldObject::HasStringId(uint32 stringId)
{
return m_stringIds.find(stringId) != m_stringIds.end();
}

WorldObject::WorldObject() :
m_transport(nullptr), m_transportInfo(nullptr), m_isOnEventNotified(false),
m_visibilityData(this), m_currMap(nullptr),
Expand Down
1 change: 1 addition & 0 deletions src/game/Entities/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,7 @@ class WorldObject : public Object

void AddStringId(std::string& stringId);
void RemoveStringId(std::string& stringId);
bool HasStringId(uint32 stringId);

protected:
explicit WorldObject();
Expand Down
10 changes: 5 additions & 5 deletions src/game/Globals/ObjectMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1157,9 +1157,9 @@ void ObjectMgr::LoadSpawnGroups()
entry.Flags = fields[5].GetUInt32();
entry.StringId = fields[6].GetUInt32();

if (!sScriptMgr.ExistsStringId(entry.StringId))
if (entry.StringId && !sScriptMgr.ExistsStringId(entry.StringId))
{
sLog.outErrorDb("Table spawn_group entry %u stringId %u does not exist. Setting to 0.", entry, entry.StringId);
sLog.outErrorDb("Table spawn_group entry %u stringId %u does not exist. Setting to 0.", entry.Id, entry.StringId);
entry.StringId = 0;
}

Expand Down Expand Up @@ -1861,7 +1861,7 @@ void ObjectMgr::LoadCreatureSpawnDataTemplates()
uint32 relayId = fields[9].GetUInt32();
uint32 stringId = fields[10].GetUInt32();

if (!sScriptMgr.ExistsStringId(stringId))
if (stringId && !sScriptMgr.ExistsStringId(stringId))
{
stringId = 0;
sLog.outErrorDb("Table creature_spawn_data_template entry %u stringId %u does not exist. Setting to 0.", entry, stringId);
Expand Down Expand Up @@ -2339,7 +2339,7 @@ void ObjectMgr::LoadGameObjects()
sLog.outString(">> Loaded " SIZEFMTD " gameobjects", mGameObjectDataMap.size());
sLog.outString();

result.reset(WorldDatabase.PQuery("SELECT guid, animprogress, state FROM gameobject_addon"));
result.reset(WorldDatabase.PQuery("SELECT guid, animprogress, state, stringId FROM gameobject_addon"));
do
{
Field* fields = result->Fetch();
Expand All @@ -2360,7 +2360,7 @@ void ObjectMgr::LoadGameObjects()
continue;
}

if (!sScriptMgr.ExistsStringId(stringId))
if (stringId && !sScriptMgr.ExistsStringId(stringId))
{
stringId = 0;
sLog.outErrorDb("Table gameobject_addon guid %u stringId %u does not exist. Setting to 0.", guid, stringId);
Expand Down
6 changes: 3 additions & 3 deletions src/game/Maps/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2218,17 +2218,17 @@ GameObject* Map::GetGameObject(uint32 dbguid) const
return static_cast<GameObject*>((*itr).second.front());
}

std::vector<WorldObject*> const* Map::GetWorldObjects(std::string& stringId) const
std::vector<WorldObject*> const* Map::GetWorldObjects(std::string stringId) const
{
return GetWorldObjects(GetMapDataContainer().GetStringId(stringId));
}

std::vector<Creature*> const* Map::GetCreatures(std::string& stringId) const
std::vector<Creature*> const* Map::GetCreatures(std::string stringId) const
{
return GetCreatures(GetMapDataContainer().GetStringId(stringId));
}

std::vector<GameObject*> const* Map::GetGameObjects(std::string& stringId) const
std::vector<GameObject*> const* Map::GetGameObjects(std::string stringId) const
{
return GetGameObjects(GetMapDataContainer().GetStringId(stringId));
}
Expand Down
6 changes: 3 additions & 3 deletions src/game/Maps/Map.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ class Map : public GridRefManager<NGridType>
// dbguid methods
Creature* GetCreature(uint32 dbguid) const;
GameObject* GetGameObject(uint32 dbguid) const;
std::vector<WorldObject*> const* GetWorldObjects(std::string& stringId) const;
std::vector<Creature*> const* GetCreatures(std::string& stringId) const;
std::vector<GameObject*> const* GetGameObjects(std::string& stringId) const;
std::vector<WorldObject*> const* GetWorldObjects(std::string stringId) const;
std::vector<Creature*> const* GetCreatures(std::string stringId) const;
std::vector<GameObject*> const* GetGameObjects(std::string stringId) const;
std::vector<WorldObject*> const* GetWorldObjects(uint32 stringId) const;
std::vector<Creature*> const* GetCreatures(uint32 stringId) const;
std::vector<GameObject*> const* GetGameObjects(uint32 stringId) const;
Expand Down
3 changes: 2 additions & 1 deletion src/game/Maps/MapDataContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

MapDataContainer::MapDataContainer() : m_spellListContainer(sObjectMgr.GetCreatureSpellListContainer()),
m_spawnGroupContainer(sObjectMgr.GetSpawnGroupContainer()), m_CreatureEventAIEventEntryMap(sEventAIMgr.GetCreatureEventEntryAIMap()),
m_CreatureEventAIEventGuidMap(sEventAIMgr.GetCreatureEventGuidAIMap()), m_creatureEventAIComputedDataMap(sEventAIMgr.GetEAIComputedDataMap())
m_CreatureEventAIEventGuidMap(sEventAIMgr.GetCreatureEventGuidAIMap()), m_creatureEventAIComputedDataMap(sEventAIMgr.GetEAIComputedDataMap()),
m_stringIds(sScriptMgr.GetStringIdMap()), m_stringIdsByString(sScriptMgr.GetStringIdByStringMap())
{
for (uint32 i = 0; i < SCRIPT_TYPE_MAX; ++i)
SetScriptMap(ScriptMapType(i), sScriptMgr.GetScriptMap(ScriptMapType(i)));
Expand Down
Loading

0 comments on commit 03d2904

Please sign in to comment.