Skip to content

Commit

Permalink
onChangeZone event and small fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MillhioreBT committed May 24, 2023
1 parent c60191b commit d5e8739
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions data/events/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<event class="Creature" method="onAreaCombat" enabled="0" />
<event class="Creature" method="onTargetCombat" enabled="0" />
<event class="Creature" method="onHear" enabled="0" />
<event class="Creature" method="onChangeZone" enabled="0" />

<!-- Party methods -->
<event class="Party" method="onJoin" enabled="0" />
Expand Down
4 changes: 4 additions & 0 deletions data/events/scripts/creature.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ end
function Creature:onHear(speaker, words, type)
if hasEvent.onHear then Event.onHear(self, speaker, words, type) end
end

function Creature:onChangeZone(fromZone, toZone)
if hasEvent.onChangeZone then Event.onChangeZone(self, fromZone, toZone) end
end
2 changes: 1 addition & 1 deletion data/scripts/eventcallbacks/account_manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ end
---@param days integer
---@param reason string
---@return boolean
function banIp(player, days, reason)
local function banIp(player, days, reason)
local ip = player:getIp()
local resultId = db.storeQuery("SELECT 1 FROM `ip_bans` WHERE `ip` = " .. ip)
if resultId then
Expand Down
1 change: 1 addition & 0 deletions data/scripts/lib/event_callbacks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ec.onChangeMount = {}
ec.onAreaCombat = {returnValue = true}
ec.onTargetCombat = {returnValue = true}
ec.onHear = {}
ec.onChangeZone = {}
-- Party
ec.onJoin = {}
ec.onLeave = {}
Expand Down
5 changes: 4 additions & 1 deletion src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
#include "creature.h"

#include "configmanager.h"
#include "events.h"
#include "game.h"
#include "monster.h"
#include "scheduler.h"

#include <fmt/format.h>

extern Game g_game;
extern ConfigManager g_config;
extern CreatureEvents* g_creatureEvents;
extern Events* g_events;
extern Game g_game;

Creature::Creature() { onIdleStatus(); }

Expand Down Expand Up @@ -504,6 +506,7 @@ void Creature::onCreatureMove(Creature* creature, const Tile* newTile, const Pos
}

if (newTile->getZone() != oldTile->getZone()) {
g_events->eventCreatureOnChangeZone(this, oldTile->getZone(), newTile->getZone());
onChangeZone(getZone());
}

Expand Down
29 changes: 29 additions & 0 deletions src/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ bool Events::load()
info.creatureOnTargetCombat = event;
} else if (methodName == "onHear") {
info.creatureOnHear = event;
} else if (methodName == "onChangeZone") {
info.creatureOnChangeZone = event;
} else {
std::cout << "[Warning - Events::load] Unknown creature method: " << methodName << std::endl;
}
Expand Down Expand Up @@ -297,6 +299,33 @@ void Events::eventCreatureOnHear(Creature* creature, Creature* speaker, std::str
scriptInterface.callVoidFunction(4);
}

void Events::eventCreatureOnChangeZone(Creature* creature, ZoneType_t fromZone, ZoneType_t toZone)
{
// Creature:onChangeZone(fromZone, toZone)
if (info.creatureOnChangeZone == -1) {
return;
}

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

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

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

LuaScriptInterface::pushUserdata<Creature>(L, creature);
LuaScriptInterface::setCreatureMetatable(L, -1, creature);

lua_pushinteger(L, fromZone);
lua_pushinteger(L, toZone);

scriptInterface.callVoidFunction(3);
}

// Party
bool Events::eventPartyOnJoin(Party* party, Player* player)
{
Expand Down
2 changes: 2 additions & 0 deletions src/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Events
int32_t creatureOnAreaCombat = -1;
int32_t creatureOnTargetCombat = -1;
int32_t creatureOnHear = -1;
int32_t creatureOnChangeZone = -1;

// Party
int32_t partyOnJoin = -1;
Expand Down Expand Up @@ -74,6 +75,7 @@ class Events
ReturnValue eventCreatureOnAreaCombat(Creature* creature, Tile* tile, bool aggressive);
ReturnValue eventCreatureOnTargetCombat(Creature* creature, Creature* target);
void eventCreatureOnHear(Creature* creature, Creature* speaker, std::string_view words, SpeakClasses type);
void eventCreatureOnChangeZone(Creature* creature, ZoneType_t fromZone, ZoneType_t toZone);

// Party
bool eventPartyOnJoin(Party* party, Player* player);
Expand Down

0 comments on commit d5e8739

Please sign in to comment.