Skip to content

fix build, codestyle and add enabled hook list for performance improv… #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 11 additions & 36 deletions src/npc_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ This code and content is released under the [GNU AGPL v3](https://github.com/aze

*/

#include "Config.h"
#include "ScriptMgr.h"
#include "Chat.h"
#include "Config.h"
#include "Log.h"
#include "Player.h"
#include "ScriptedCreature.h"
#include "ScriptedGossip.h"
#include "ScriptMgr.h"
#include "SpellMgr.h"
#include "Log.h"

static bool BFEnableModule;
static bool BFAnnounceModule;
Expand All @@ -87,7 +87,9 @@ static uint32 MaxLevel = 80;
class BufferConfig : public WorldScript
{
public:
BufferConfig() : WorldScript("BufferConfig_conf") {}
BufferConfig() : WorldScript("BufferConfig_conf", {
WORLDHOOK_ON_BEFORE_CONFIG_LOAD
}) {}

void OnBeforeConfigLoad(bool /*reload*/) override
{
Expand All @@ -107,25 +109,23 @@ class BufferConfig : public WorldScript
if (BuffMessageTimer != 0)
{
if (BuffMessageTimer < 60000 || BuffMessageTimer > 300000)
{
BuffMessageTimer = 60000;
}
}
}
};

class BufferAnnounce : public PlayerScript
{
public:
BufferAnnounce() : PlayerScript("BufferAnnounce") {}
BufferAnnounce() : PlayerScript("BufferAnnounce", {
PLAYERHOOK_ON_LOGIN
}) {}

void OnLogin(Player *player)
void OnPlayerLogin(Player *player)
{
// Announce Module
if (BFEnableModule && BFAnnounceModule)
{
ChatHandler(player->GetSession()).SendSysMessage("This server is running the |cff4CFF00BufferNPC |rmodule.");
}
}
};

Expand All @@ -143,17 +143,13 @@ class buff_npc : public CreatureScript

// if the character is level max level or higher, return the last spell in the chain
if (level >= MaxLevel)
{
return sSpellMgr->GetLastSpellInChain(spell_id);
}

uint32 first_spell = sSpellMgr->GetFirstSpellInChain(spell_id);
uint32 next_spell = first_spell;
uint32 number_of_spells_in_chain = 0;
for (; next_spell; next_spell = sSpellMgr->GetNextSpellInChain(next_spell))
{
number_of_spells_in_chain++;
}

// if the chain is empty, return the first spell
if (number_of_spells_in_chain == 0)
Expand All @@ -164,9 +160,7 @@ class buff_npc : public CreatureScript

// if the chain has only one spell, return that spell
if (number_of_spells_in_chain == 1)
{
return first_spell;
}

// if the chain has more than one spell, calculate the level-appropriate spell
uint32 spell_index = (level * number_of_spells_in_chain) / MaxLevel;
Expand Down Expand Up @@ -200,9 +194,7 @@ class buff_npc : public CreatureScript

// Sanitize
if (whisper == "")
{
whisper = "ERROR! NPC Emote Text Not Found! Check the npc_buffer.conf!";
}

std::string randMsg = sConfigMgr->GetOption<std::string>(whisper.c_str(), "");
replace(randMsg, "%s", Name);
Expand All @@ -219,9 +211,7 @@ class buff_npc : public CreatureScript

// Sanitize
if (phrase == "")
{
phrase = "ERROR! NPC Emote Text Not Found! Check the npc_buffer.conf!";
}

std::string randMsg = sConfigMgr->GetOption<std::string>(phrase.c_str(), "");
return randMsg.c_str();
Expand All @@ -231,9 +221,7 @@ class buff_npc : public CreatureScript
bool OnGossipHello(Player* player, Creature* creature)
{
if (!BFEnableModule)
{
return false;
}

// Who are we dealing with?
std::string CreatureWhisper = "Init";
Expand All @@ -243,9 +231,7 @@ class buff_npc : public CreatureScript
std::vector<uint32> vecBuffs = {};
std::stringstream ss(sConfigMgr->GetOption<std::string>("Buff.Spells", ""));
for (std::string buff; std::getline(ss, buff, ';');)
{
vecBuffs.push_back(stoul(buff));
}

// Cure Resurrection Sickness
if (BuffCureRes && player->HasAura(15007))
Expand All @@ -270,17 +256,13 @@ class buff_npc : public CreatureScript
{
// No level requirement, so buff with max level default buffs
for (std::vector<uint32>::const_iterator itr = vecBuffs.begin(); itr != vecBuffs.end(); itr++)
{
player->CastSpell(player, *itr, true);
}
}

// Choose and speak a random phrase to the player
// Phrases are stored in the config file
if (BuffNumWhispers > 0)
{
creature->Whisper(PickWhisper(PlayerName).c_str(), LANG_UNIVERSAL, player);
}

// Emote and Close
creature->HandleEmoteCommand(EMOTE_ONESHOT_FLEX);
Expand All @@ -298,9 +280,8 @@ class buff_npc : public CreatureScript
// Called once when client is loaded
void Reset()
{
if (BuffMessageTimer != 0) {
if (BuffMessageTimer != 0)
MessageTimer = urand(BuffMessageTimer, 300000); // 1-5 minutes
}
}

// Called at World update tick
Expand All @@ -318,24 +299,18 @@ class buff_npc : public CreatureScript

// Use gesture?
if (BuffEmoteCommand != 0)
{
me->HandleEmoteCommand(BuffEmoteCommand);
}

// Alert players?
if (BuffEmoteSpell != 0)
{
me->CastSpell(me, BuffEmoteSpell);
}

MessageTimer = urand(BuffMessageTimer, 300000);
}
else { MessageTimer -= diff; }
}
else
{
MessageTimer -= diff;
}
}
};

Expand Down