Skip to content

Commit

Permalink
Add custom npc commands for creature_template_addon
Browse files Browse the repository at this point in the history
  • Loading branch information
KalopsiaTwilight committed Apr 15, 2023
1 parent 5106d9b commit 410ac3e
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 0 deletions.
29 changes: 29 additions & 0 deletions sql/custom/auth/2023_04_15_add_customnpc_addon_commands.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* PERMISSION GROUP STRUCTURE:
* NOTE: Lesser ID PERM GROUP inherits all permissions from higher ID PERM GROUP.
* e.g. PERM GROUP 198 (mod) has all permissions of PERM GROUP 199 (player)
* 196 - Role: [GM3] Administrator
* 197 - Role: [GM2] Gamemaster
* 198 - Role: [GM1] Moderator, ST
* 199 - Role: [GM0] Player
*/
START TRANSACTION;
SET @PLAYER := 195;
SET @MODERATOR := 194;
SET @GM := 193;
SET @ADMIN := 192;

-- [RECREATION: CUSTOM PERMS]
REPLACE INTO rbac_permissions (`id`, `name`) VALUES
(1368, 'Command: .cnpc set aura'),
(1369, 'Command: .cnpc set emote'),
(1370, 'Command: .cnpc set mount')
;

REPLACE INTO rbac_linked_permissions (`id`, `linkedId`) VALUES
(@MODERATOR, 1368),
(@MODERATOR, 1369),
(@MODERATOR, 1370)
;
COMMIT;

10 changes: 10 additions & 0 deletions sql/custom/world/2023_04_15_add_customnpc_addon_commands.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- FREEDOM CUSTOM COMMANDS
REPLACE INTO `command` (name, help) VALUES
('customnpc set aura', 'Syntax: .customnpc set aura $key $auraId [0|1]\nToggles the aura for $auraId for the custom npc template with key $key. Optionally use 1 for toggle on (default), or 0 for toggle off.'),
('customnpc set emote', 'Syntax: .customnpc set emote $key $emoteId\nSets the default emote state for the custom NPC template with key $key to $emoteId.'),
('customnpc set mount', 'Syntax: .customnpc set mount $key $mountId\nSets the mount for the custom NPC template with key $key to the creature with id $mountId.'),

('cnpc set aura', 'Syntax: .cnpc set aura $key $auraId [0|1]\nToggles the aura for $auraId for the custom npc template with key $key. Optionally use 1 for toggle on (default), or 0 for toggle off.'),
('cnpc set emote', 'Syntax: .cnpc set emote $key $emoteId\nSets the default emote state for the custom NPC template with key $key to $emoteId.'),
('cnpc set mount', 'Syntax: .cnpc set mount $key $mountId\nSets the mount for the custom NPC template with key $key to the creature with id $mountId.')
;
10 changes: 10 additions & 0 deletions sql/updates/freedom/2023_04_15_add_customnpc_addon_commands.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- FREEDOM CUSTOM COMMANDS
REPLACE INTO `commands` (command, syntax, description, gmlevel) VALUES
('customnpc set aura', 'Syntax: .customnpc set aura $key $auraId [0|1]','Toggles the aura for $auraId for the custom npc template with key $key. Optionally use 1 for toggle on (default), or 0 for toggle off.', 1),
('customnpc set emote', 'Syntax: .customnpc set emote $key $emoteId','Sets the default emote state for the custom NPC template with key $key to $emoteId.', 1),
('customnpc set mount', 'Syntax: .customnpc set mount $key $mountId','Sets the mount for the custom NPC template with key $key to the creature with id $mountId.', 1),

('cnpc set aura', 'Syntax: .cnpc set aura $key $auraId [0|1]','Toggles the aura for $auraId for the custom npc template with key $key. Optionally use 1 for toggle on (default), or 0 for toggle off.', 1),
('cnpc set emote', 'Syntax: .cnpc set emote $key $emoteId','Sets the default emote state for the custom NPC template with key $key to $emoteId.', 1),
('cnpc set mount', 'Syntax: .cnpc set mount $key $mountId','Sets the mount for the custom NPC template with key $key to the creature with id $mountId.', 1)
;
2 changes: 2 additions & 0 deletions src/server/database/Database/Implementation/WorldDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ void WorldDatabaseConnection::DoPrepareStatements()
PrepareStatement(WORLD_DEL_ALL_CREATURE_TEMPLATE_MODEL, "DELETE FROM creature_template_model WHERE CreatureID = ?", CONNECTION_ASYNC);
PrepareStatement(WORLD_DEL_CREATURE_EQUIP_TEMPLATE, "DELETE FROM creature_equip_template WHERE CreatureID = ?", CONNECTION_ASYNC);
PrepareStatement(WORLD_DEL_DRESSNPC_OUTFIT, "DELETE FROM creature_template_outfits WHERE entry = ?", CONNECTION_ASYNC);
PrepareStatement(WORLD_REP_CREATURE_TEMPLATE_ADDON, "REPLACE INTO creature_template_addon (entry, path_id, mount, bytes1, bytes2, emote, aiAnimKit, movementAnimKit, meleeAnimkit, visibilityDistanceType, auras) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", CONNECTION_ASYNC);
PrepareStatement(WORLD_DEL_CREATURE_TEMPLATE_ADDON, "DELETE FROM creature_template_addon WHERE entry = ?", CONNECTION_ASYNC);
}

WorldDatabaseConnection::WorldDatabaseConnection(MySQLConnectionInfo& connInfo) : MySQLConnection(connInfo)
Expand Down
2 changes: 2 additions & 0 deletions src/server/database/Database/Implementation/WorldDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ enum WorldDatabaseStatements : uint32
WORLD_DEL_CREATURE_TEMPLATE_MODEL,
WORLD_DEL_ALL_CREATURE_TEMPLATE_MODEL,
WORLD_DEL_DRESSNPC_OUTFIT,
WORLD_REP_CREATURE_TEMPLATE_ADDON,
WORLD_DEL_CREATURE_TEMPLATE_ADDON,

WORLD_DEL_CREATURE_FORMATION,

Expand Down
4 changes: 4 additions & 0 deletions src/server/game/Accounts/RBAC.h
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,10 @@ enum RBACPermissions
RBAC_FPERM_COMMAND_NPCCAST = 1366,
RBAC_FPERM_COMMAND_NPCCAST_REMOVE = 1367,

RBAC_FPERM_COMMAND_CUSTOMNPC_SET_AURA = 1368,
RBAC_FPERM_COMMAND_CUSTOMNPC_SET_EMOTE = 1369,
RBAC_FPERM_COMMAND_CUSTOMNPC_SET_MOUNT = 1370,

// custom .reload permissions 9000-9099
RBAC_FPERM_COMMAND_RELOAD_PLAYERCREATEINFO_SPELL_CUSTOM = 9000,
RBAC_FPERM_COMMAND_RELOAD_PLAYERCREATEINFO_SKILL_CUSTOM = 9001,
Expand Down
107 changes: 107 additions & 0 deletions src/server/game/Freedom/FreedomMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2147,6 +2147,59 @@ void FreedomMgr::SetCustomNpcCustomizations(std::string const& key, uint8 variat
ReloadSpawnedCustomNpcs(key);
}

void FreedomMgr::ToggleCustomNpcAura(std::string const& key, uint32 auraId, bool active)
{
uint32 templateId = _customNpcStore[key].templateId;
CreatureAddon cAddon = sObjectMgr->_creatureTemplateAddonStore[templateId];

auto it = cAddon.auras.begin();
for (; it != cAddon.auras.end(); it++)
{
if (*it == auraId && active) // Nothing to do aura already on
{
return;
}

if (*it == auraId && !active) // we found auraId we want to remove
break;
}
if (active)
{
cAddon.auras.push_back(auraId);
}
else if (it != cAddon.auras.end())
{
cAddon.auras.erase(it);
}

sObjectMgr->_creatureTemplateAddonStore[templateId] = cAddon;
SaveNpcCreatureTemplateAddonToDb(templateId, cAddon);
ReloadSpawnedCustomNpcs(key);
}

void FreedomMgr::SetCustomNpcMount(std::string const& key, uint32 mount)
{
uint32 templateId = _customNpcStore[key].templateId;
CreatureAddon cAddon = sObjectMgr->_creatureTemplateAddonStore[templateId];

cAddon.mount = mount;

sObjectMgr->_creatureTemplateAddonStore[templateId] = cAddon;
SaveNpcCreatureTemplateAddonToDb(templateId, cAddon);
ReloadSpawnedCustomNpcs(key);
}

void FreedomMgr::SetCustomNpcEmote(std::string const& key, uint32 emote)
{
uint32 templateId = _customNpcStore[key].templateId;
CreatureAddon cAddon = sObjectMgr->_creatureTemplateAddonStore[templateId];

cAddon.emote = emote;

sObjectMgr->_creatureTemplateAddonStore[templateId] = cAddon;
SaveNpcCreatureTemplateAddonToDb(templateId, cAddon);
ReloadSpawnedCustomNpcs(key);
}

void FreedomMgr::RemoveCustomNpcVariation(std::string const& key, uint8 variationId)
{
Expand Down Expand Up @@ -2283,6 +2336,42 @@ void FreedomMgr::SaveNpcEquipmentInfoToDb(uint32 templateId, uint8 variationId)
WorldDatabase.Execute(stmt);
}

void FreedomMgr::SaveNpcCreatureTemplateAddonToDb(uint32 templateId, CreatureAddon cAddon)
{
TC_LOG_DEBUG("freedom", "FREEDOMMGR: Saving creature addon template for creature template id '%u' to DB...", templateId);
WorldDatabasePreparedStatement* stmt = WorldDatabase.GetPreparedStatement(WORLD_REP_CREATURE_TEMPLATE_ADDON);

// REPLACE INTO creature_template_addon (entry, path_id, mount, bytes1, bytes2, emote, aiAnimKit, movementAnimKit, meleeAnimkit, visibilityDistanceType, auras) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
int index = 0;
stmt->setUInt32(index++, templateId);
stmt->setUInt32(index++, cAddon.path_id);
stmt->setUInt32(index++, cAddon.mount);
stmt->setUInt32(index++, cAddon.bytes1);
stmt->setUInt32(index++, cAddon.bytes2);
stmt->setUInt32(index++, cAddon.emote);
stmt->setUInt16(index++, cAddon.aiAnimKit);
stmt->setUInt16(index++, cAddon.movementAnimKit);
stmt->setUInt16(index++, cAddon.meleeAnimKit);
stmt->setUInt8(index++, uint8(cAddon.visibilityDistanceType));
if (!cAddon.auras.empty())
{
std::string auraList = "";
for (auto aura : cAddon.auras)
{
if (auraList.empty())
auraList = std::to_string(aura);
else
auraList += " " + std::to_string(aura);
}
stmt->setString(index, auraList);
}
else
{
stmt->setNull(index);
}
WorldDatabase.Execute(stmt);
}

void FreedomMgr::ReloadSpawnedCustomNpcs(std::string const& key)
{
TC_LOG_DEBUG("freedom", "FREEDOMMGR: Reloading custom npc '%s'", key.c_str());
Expand All @@ -2297,9 +2386,22 @@ void FreedomMgr::ReloadSpawnedCustomNpcs(std::string const& key)
TC_LOG_DEBUG("freedom", "FREEDOMMGR: Reloading creature id '%lu'", spawn);
uint8 modelId = urand(0u, sObjectMgr->_creatureTemplateStore[data.templateId].Models.size() - 1);
CreatureModel model = sObjectMgr->_creatureTemplateStore[data.templateId].Models[modelId];
CreatureAddon cAddon = sObjectMgr->_creatureTemplateAddonStore[data.templateId];
creature->SetDisplayId(model.CreatureDisplayID, model.DisplayScale);
creature->SetName(sObjectMgr->_creatureTemplateStore[data.templateId].Name);
creature->LoadEquipment(urand(1u, sObjectMgr->_equipmentInfoStore[data.templateId].size()));

creature->RemoveAllAuras();
for (auto auraId : cAddon.auras) {
creature->AddAura(auraId, creature);
}

creature->SetEmoteState(Emote(cAddon.emote));
if (cAddon.mount)
creature->Mount(cAddon.mount);
else
creature->Dismount();

TC_LOG_DEBUG("freedom", "FREEDOMMGR: Reloaded creature id '%lu'", spawn);
}
}
Expand All @@ -2326,6 +2428,10 @@ void FreedomMgr::DeleteCustomNpc(std::string const& key)
stmt->setUInt32(0, data.templateId);
trans->Append(stmt);

stmt = WorldDatabase.GetPreparedStatement(WORLD_DEL_CREATURE_TEMPLATE_ADDON);
stmt->setUInt32(0, data.templateId);
trans->Append(stmt);

stmt = WorldDatabase.GetPreparedStatement(WORLD_DEL_CREATURE_TEMPLATE);
stmt->setUInt32(0, data.templateId);
trans->Append(stmt);
Expand All @@ -2351,6 +2457,7 @@ void FreedomMgr::DeleteCustomNpc(std::string const& key)
sObjectMgr->_creatureOutfitStore.erase(outfitId);
}
sObjectMgr->_equipmentInfoStore.erase(data.templateId);
sObjectMgr->_creatureTemplateAddonStore.erase(data.templateId);
sObjectMgr->_creatureTemplateStore.erase(data.templateId);
}

Expand Down
4 changes: 4 additions & 0 deletions src/server/game/Freedom/FreedomMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ class TC_GAME_API FreedomMgr
void SetCustomNpcRank(std::string const& key, uint32 rank);
void SetCustomNpcGuild(std::string const& key, uint8 variationId, uint64 guildId);
void SetCustomNpcTameable(std::string const& key, bool tameable);
void ToggleCustomNpcAura(std::string const& key, uint32 auraId, bool active);
void SetCustomNpcMount(std::string const& key, uint32 mount);
void SetCustomNpcEmote(std::string const& key, uint32 emote);
void LoadCustomNpcSpawn(uint32 templateId, ObjectGuid::LowType spawn);
void RemoveCustomNpcVariation(std::string const& key, uint8 variationId);
void DeleteCustomNpc(std::string const& key);
Expand Down Expand Up @@ -398,6 +401,7 @@ class TC_GAME_API FreedomMgr
void SaveNpcOutfitToDb(uint32 templateId, uint8 variationId);
void SaveCustomNpcDataToDb(CustomNpcData outfitData);
void SaveNpcCreatureTemplateToDb(CreatureTemplate cTemplate);
void SaveNpcCreatureTemplateAddonToDb(uint32 templateId, CreatureAddon cAddon);
void SaveNpcEquipmentInfoToDb(uint32 templateId, uint8 variationId);
void SaveNpcModelInfo(CreatureModel model, uint32 creatureTemplateId, uint8 variationId);
void ReloadSpawnedCustomNpcs(std::string const& key);
Expand Down
42 changes: 42 additions & 0 deletions src/server/scripts/Freedom/fcs_customnpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ class F_customnpc_commandscript : public CommandScript
{
static ChatCommandTable customNpcSetCommandTable =
{
{ "aura", HandleCustomNpcSetAuraCommand, rbac::RBAC_FPERM_COMMAND_CUSTOMNPC_SET_AURA, Console::No },
{ "displayid", HandleCustomNpcSetDisplayIdCommand, rbac::RBAC_FPERM_COMMAND_CUSTOMNPC_SET_DISPLAYID, Console::No },
{ "emote", HandleCustomNpcSetEmoteCommand, rbac::RBAC_FPERM_COMMAND_CUSTOMNPC_SET_EMOTE, Console::No },
{ "face", HandleCustomNpcSetFaceCommand, rbac::RBAC_FPERM_COMMAND_CUSTOMNPC_SET_FACE, Console::No },
{ "gender", HandleCustomNpcSetGenderCommand, rbac::RBAC_FPERM_COMMAND_CUSTOMNPC_SET_GENDER, Console::No },
{ "guild", HandleCustomNpcSetGuildCommand, rbac::RBAC_FPERM_COMMAND_CUSTOMNPC_SET_GUILD, Console::No },
{ "mount", HandleCustomNpcSetMountCommand, rbac::RBAC_FPERM_COMMAND_CUSTOMNPC_SET_MOUNT, Console::No },
{ "name", HandleCustomNpcSetDisplayNameCommand, rbac::RBAC_FPERM_COMMAND_CUSTOMNPC_SET_DISPLAYNAME, Console::No },
{ "race", HandleCustomNpcSetRaceCommand, rbac::RBAC_FPERM_COMMAND_CUSTOMNPC_SET_RACE, Console::No },
{ "rank", HandleCustomNpcSetRankCommand, rbac::RBAC_FPERM_COMMAND_CUSTOMNPC_SET_RANK, Console::No },
Expand Down Expand Up @@ -694,6 +697,45 @@ class F_customnpc_commandscript : public CommandScript
return true;
}

static bool HandleCustomNpcSetAuraCommand(ChatHandler* handler, std::string const& name, uint32 auraId, Optional<uint8> activeInt)
{
if (!sFreedomMgr->CustomNpcNameExists(name)) {
handler->PSendSysMessage("There is no Custom NPC with the name: %s", name);
handler->SetSentErrorMessage(true);
return false;
}

bool active = activeInt.has_value() ? activeInt.value() == 1 : true;
sFreedomMgr->ToggleCustomNpcAura(name, auraId,active);
handler->PSendSysMessage("Custom NPC %s now has aura %u toggled: %s!", name, auraId, active ? "on" : "off");
return true;
}

static bool HandleCustomNpcSetEmoteCommand(ChatHandler* handler, std::string const& name, uint32 emoteId)
{
if (!sFreedomMgr->CustomNpcNameExists(name)) {
handler->PSendSysMessage("There is no Custom NPC with the name: %s", name);
handler->SetSentErrorMessage(true);
return false;
}

sFreedomMgr->SetCustomNpcEmote(name, emoteId);
handler->PSendSysMessage("Custom NPC %s default emote state set to %u!", name, emoteId);
return true;
}

static bool HandleCustomNpcSetMountCommand(ChatHandler* handler, std::string const& name, uint32 mountId)
{
if (!sFreedomMgr->CustomNpcNameExists(name)) {
handler->PSendSysMessage("There is no Custom NPC with the name: %s", name);
handler->SetSentErrorMessage(true);
return false;
}

sFreedomMgr->SetCustomNpcMount(name, mountId);
handler->PSendSysMessage("Custom NPC %s mount set to %u!", name, mountId);
return true;
}
};

void AddSC_F_customnpc_commandscript()
Expand Down

0 comments on commit 410ac3e

Please sign in to comment.