Skip to content

Commit

Permalink
New event onSpellCheck (#4619)
Browse files Browse the repository at this point in the history
* new event spell check
  • Loading branch information
MillhioreBT authored Jan 12, 2024
1 parent 6579f12 commit 85e3cc8
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions data/events/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<event class="Player" method="onWrapItem" enabled="1" />
<event class="Player" method="onInventoryUpdate" enabled="1" />
<event class="Player" method="onNetworkMessage" enabled="1" />
<event class="Player" method="onSpellCheck" enabled="0" />

<!-- Monster methods -->
<event class="Monster" method="onDropLoot" enabled="1" />
Expand Down
8 changes: 8 additions & 0 deletions data/events/scripts/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,11 @@ function Player:onNetworkMessage(recvByte, msg)

handler(self, msg)
end

function Player:onSpellCheck(spell)
local onSpellCheck = EventCallback.onSpellCheck
if onSpellCheck then
return onSpellCheck(self, spell)
end
return true
end
1 change: 1 addition & 0 deletions data/scripts/lib/event_callbacks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ ec.onGainSkillTries = {[3] = 1}
ec.onWrapItem = {}
ec.onInventoryUpdate = {}
ec.onUpdateStorage = {}
ec.onSpellCheck = {}
-- Monster
ec.onDropLoot = {}
ec.onSpawn = {}
Expand Down
28 changes: 28 additions & 0 deletions src/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ bool Events::load()
info.playerOnInventoryUpdate = event;
} else if (methodName == "onNetworkMessage") {
info.playerOnNetworkMessage = event;
} else if (methodName == "onSpellCheck") {
info.playerOnSpellCheck = event;
} else {
std::cout << "[Warning - Events::load] Unknown player method: " << methodName << std::endl;
}
Expand Down Expand Up @@ -1338,6 +1340,32 @@ void Events::eventPlayerOnNetworkMessage(Player* player, uint8_t recvByte, Netwo
scriptInterface.callVoidFunction(3);
}

bool Events::eventPlayerOnSpellCheck(Player* player, const Spell* spell)
{
// Player:onSpellCheck(spell)
if (info.playerOnSpellCheck == -1) {
return true;
}

if (!scriptInterface.reserveScriptEnv()) {
std::cout << "[Error - Events::eventPlayerOnSpellCheck] Call stack overflow" << std::endl;
return false;
}

ScriptEnvironment* env = scriptInterface.getScriptEnv();
env->setScriptId(info.playerOnSpellCheck, &scriptInterface);

lua_State* L = scriptInterface.getLuaState();
scriptInterface.pushFunction(info.playerOnSpellCheck);

LuaScriptInterface::pushUserdata<Player>(L, player);
LuaScriptInterface::setMetatable(L, -1, "Player");

LuaScriptInterface::pushSpell(L, *spell);

return scriptInterface.callFunction(2);
}

void Events::eventMonsterOnDropLoot(Monster* monster, Container* corpse)
{
// Monster:onDropLoot(corpse)
Expand Down
3 changes: 3 additions & 0 deletions src/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
class ItemType;
class NetworkMessage;
class Party;
class Spell;
class Tile;

enum class EventInfoId
Expand Down Expand Up @@ -68,6 +69,7 @@ class Events
int32_t playerOnWrapItem = -1;
int32_t playerOnInventoryUpdate = -1;
int32_t playerOnNetworkMessage = -1;
int32_t playerOnSpellCheck = -1;

// Monster
int32_t monsterOnDropLoot = -1;
Expand Down Expand Up @@ -129,6 +131,7 @@ class Events
void eventPlayerOnWrapItem(Player* player, Item* item);
void eventPlayerOnInventoryUpdate(Player* player, Item* item, slots_t slot, bool equip);
void eventPlayerOnNetworkMessage(Player* player, uint8_t recvByte, NetworkMessage* msg);
bool eventPlayerOnSpellCheck(Player* player, const Spell* spell);

// Monster
void eventMonsterOnDropLoot(Monster* monster, Container* corpse);
Expand Down
13 changes: 13 additions & 0 deletions src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,19 @@ void LuaScriptInterface::pushInstantSpell(lua_State* L, const InstantSpell& spel
setMetatable(L, -1, "Spell");
}

void LuaScriptInterface::pushSpell(lua_State* L, const Spell& spell)
{
lua_createtable(L, 0, 5);

setField(L, "name", spell.getName());
setField(L, "level", spell.getLevel());
setField(L, "mlevel", spell.getMagicLevel());
setField(L, "mana", spell.getMana());
setField(L, "manapercent", spell.getManaPercent());

setMetatable(L, -1, "Spell");
}

void LuaScriptInterface::pushPosition(lua_State* L, const Position& position, int32_t stackpos /* = 0*/)
{
lua_createtable(L, 0, 4);
Expand Down
2 changes: 2 additions & 0 deletions src/luascript.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Combat;
class Container;
class Creature;
class Cylinder;
class Spell;
class InstantSpell;
class Item;
class LuaScriptInterface;
Expand Down Expand Up @@ -354,6 +355,7 @@ class LuaScriptInterface
static void pushBoolean(lua_State* L, bool value);
static void pushCombatDamage(lua_State* L, const CombatDamage& damage);
static void pushInstantSpell(lua_State* L, const InstantSpell& spell);
static void pushSpell(lua_State* L, const Spell& spell);
static void pushPosition(lua_State* L, const Position& position, int32_t stackpos = 0);
static void pushOutfit(lua_State* L, const Outfit_t& outfit);
static void pushOutfit(lua_State* L, const Outfit* outfit);
Expand Down
6 changes: 6 additions & 0 deletions src/spells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

#include "combat.h"
#include "configmanager.h"
#include "events.h"
#include "game.h"
#include "luavariant.h"
#include "monsters.h"
#include "pugicast.h"

extern Game g_game;
extern Spells* g_spells;
extern Events* g_events;
extern Monsters g_monsters;
extern ConfigManager g_config;
extern LuaEnvironment g_luaEnvironment;
Expand Down Expand Up @@ -529,6 +531,10 @@ bool Spell::playerSpellCheck(Player* player) const
return false;
}

if (!g_events->eventPlayerOnSpellCheck(player, this)) {
return false;
}

if ((aggressive || pzLock) && (range < 1 || (range > 0 && !player->getAttackedCreature())) &&
player->getSkull() == SKULL_BLACK) {
player->sendCancelMessage(RETURNVALUE_NOTPOSSIBLE);
Expand Down

0 comments on commit 85e3cc8

Please sign in to comment.