Added LUA Function to adjust hate position up or down: AdjustHatePosition(Caster, NPC, Increase) increase is boolean, true to increase, false to decrease
This commit is contained in:
parent
6a6573e339
commit
baf74d0bdd
@ -14860,4 +14860,30 @@ int EQ2Emu_lua_GetRaid(lua_State* state) {
|
||||
}
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
|
||||
int EQ2Emu_lua_AdjustHatePosition(lua_State* state) {
|
||||
Spawn* entity = lua_interface->GetSpawn(state);
|
||||
Spawn* npc = lua_interface->GetSpawn(state, 2);
|
||||
bool increase = lua_interface->GetBooleanValue(state, 3);
|
||||
|
||||
LuaSpell* luaspell = lua_interface->GetCurrentSpell(state);
|
||||
if(luaspell && luaspell->resisted) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (luaspell) {
|
||||
for (int32 id : luaspell->GetTargets()) {
|
||||
Spawn* spawn = luaspell->zone->GetSpawnByID(id);
|
||||
if (spawn && npc->IsNPC() && spawn->Alive()) {
|
||||
((NPC*)npc)->Brain()->AdjustHatePosition(entity->GetID(), increase);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (npc && npc->IsNPC() && entity->Alive()) {
|
||||
((NPC*)npc)->Brain()->AdjustHatePosition(entity->GetID(), increase);
|
||||
}
|
||||
lua_interface->ResetFunctionStack(state);
|
||||
return 0;
|
||||
}
|
@ -697,4 +697,5 @@ int EQ2Emu_lua_AttackAllowed(lua_State* state);
|
||||
int EQ2Emu_lua_IsInRaid(lua_State* state);
|
||||
int EQ2Emu_lua_InSameRaid(lua_State* state);
|
||||
int EQ2Emu_lua_GetRaid(lua_State* state);
|
||||
int EQ2Emu_lua_AdjustHatePosition(lua_State* state);
|
||||
#endif
|
@ -1837,6 +1837,7 @@ void LuaInterface::RegisterFunctions(lua_State* state) {
|
||||
lua_register(state,"IsInRaid", EQ2Emu_lua_IsInRaid);
|
||||
lua_register(state,"InSameRaid", EQ2Emu_lua_InSameRaid);
|
||||
lua_register(state,"GetRaid", EQ2Emu_lua_GetRaid);
|
||||
lua_register(state,"AdjustHatePosition", EQ2Emu_lua_AdjustHatePosition);
|
||||
}
|
||||
|
||||
void LuaInterface::LogError(const char* error, ...) {
|
||||
|
@ -271,6 +271,50 @@ void Brain::AddHate(Entity* entity, sint32 hate) {
|
||||
}
|
||||
}
|
||||
|
||||
bool Brain::AdjustHatePosition(int32 id, bool increase) {
|
||||
// Lock the hate list, we are altering the list so use a write lock
|
||||
MHateList.writelock(__FUNCTION__, __LINE__);
|
||||
|
||||
auto it = m_hatelist.find(id);
|
||||
if (it == m_hatelist.end()) {
|
||||
MHateList.releasewritelock(__FUNCTION__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Build a vector of (id, hate), sorted highest→lowest hate
|
||||
std::vector<std::pair<int32,sint32>> sorted;
|
||||
sorted.reserve(m_hatelist.size());
|
||||
for (auto &kv : m_hatelist)
|
||||
sorted.emplace_back(kv.first, kv.second);
|
||||
std::sort(sorted.begin(), sorted.end(),
|
||||
[](auto &a, auto &b){ return a.second > b.second; });
|
||||
|
||||
// Locate our position
|
||||
auto posIt = std::find_if(sorted.begin(), sorted.end(),
|
||||
[&](auto &p){ return p.first == id; });
|
||||
size_t idx = std::distance(sorted.begin(), posIt);
|
||||
|
||||
if (increase) {
|
||||
if (idx == 0) {
|
||||
MHateList.releasewritelock(__FUNCTION__, __LINE__);
|
||||
return false; // no higher to go
|
||||
}
|
||||
sint32 aboveHate = sorted[idx - 1].second;
|
||||
it->second = aboveHate + 1; // move up
|
||||
}
|
||||
else {
|
||||
if (idx + 1 >= sorted.size()) {
|
||||
MHateList.releasewritelock(__FUNCTION__, __LINE__);
|
||||
return false; // already at bottom
|
||||
}
|
||||
sint32 belowHate = sorted[idx + 1].second;
|
||||
it->second = belowHate - 1; // move lower
|
||||
}
|
||||
|
||||
MHateList.releasewritelock(__FUNCTION__, __LINE__);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Brain::ClearHate(bool lockSpawnList) {
|
||||
// Lock the hate list, we are altering the list so use a write lock
|
||||
MHateList.writelock(__FUNCTION__, __LINE__);
|
||||
|
@ -59,6 +59,8 @@ public:
|
||||
/// <param name="entity">The entity we are adding to this NPC's hate list</param>
|
||||
/// <param name="hate">The amount of hate to add</param>
|
||||
virtual void AddHate(Entity* entity, sint32 hate);
|
||||
virtual bool AdjustHatePosition(int32 id, bool increase);
|
||||
|
||||
/// <summary>Completely clears the hate list for this npc</summary>
|
||||
void ClearHate(bool lockSpawnList = false);
|
||||
/// <summary>Removes the given entity from this NPC's hate list</summary>
|
||||
|
@ -5170,7 +5170,7 @@ void ZoneServer::KillSpawn(bool spawnListLocked, Spawn* dead, Spawn* killer, boo
|
||||
//Check if caster is alive after death proc called, incase of deathsave
|
||||
if (dead->Alive())
|
||||
return;
|
||||
|
||||
|
||||
RemoveSpellTimersFromSpawn(dead, true, !dead->IsPlayer(), true, !isSpell);
|
||||
((Entity*)dead)->IsCasting(false);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user