Skip to content

Commit

Permalink
Merge pull request #966 from ReyAleman/Creature_onOutfit
Browse files Browse the repository at this point in the history
Creature.onOutfit event.
  • Loading branch information
marksamman committed Nov 2, 2014
2 parents 7253585 + 6626585 commit 56749a6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
3 changes: 3 additions & 0 deletions data/events/events.xml
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<events>
<!-- Creature methods -->
<event class="Creature" method="onChangeOutfit" enabled="0" />

<!-- Party methods -->
<event class="Party" method="onJoin" enabled="0" />
<event class="Party" method="onLeave" enabled="0" />
Expand Down
3 changes: 3 additions & 0 deletions data/events/scripts/creature.lua
@@ -0,0 +1,3 @@
function Creature:onChangeOutfit(outfit)
return true
end
38 changes: 37 additions & 1 deletion src/events.cpp
Expand Up @@ -35,6 +35,9 @@ Events::Events() :

void Events::clear()
{
// Creature
creatureOnChangeOutfit = -1;

// Party
partyOnJoin = -1;
partyOnLeave = -1;
Expand Down Expand Up @@ -81,7 +84,13 @@ bool Events::load()

const std::string& methodName = eventNode.attribute("method").as_string();
const int32_t event = scriptInterface.getMetaEvent(className, methodName);
if (className == "Party") {
if (className == "Creature") {
if (methodName == "onChangeOutfit") {
creatureOnChangeOutfit = event;
} else {
std::cout << "[Warning - Events::load] Unknown creature method: " << methodName << std::endl;
}
} else if (className == "Party") {
if (methodName == "onJoin") {
partyOnJoin = event;
} else if (methodName == "onLeave") {
Expand Down Expand Up @@ -128,6 +137,33 @@ bool Events::load()
return true;
}

// Creature
bool Events::eventCreatureOnChangeOutfit(Creature* creature, const Outfit_t& outfit)
{
// Creature:onChangeOutfit(outfit) or Creature.onChangeOutfit(self, outfit)
if (creatureOnChangeOutfit == -1) {
return true;
}

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

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

lua_State* L = scriptInterface.getLuaState();
scriptInterface.pushFunction(creatureOnChangeOutfit);

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

LuaScriptInterface::pushOutfit(L, outfit);

return scriptInterface.callFunction(2);
}

// Party
bool Events::eventPartyOnJoin(Party* party, Player* player)
{
Expand Down
6 changes: 6 additions & 0 deletions src/events.h
Expand Up @@ -34,6 +34,9 @@ class Events
void clear();
bool load();

// Creature
bool eventCreatureOnChangeOutfit(Creature* creature, const Outfit_t& outfit);

// Party
bool eventPartyOnJoin(Party* party, Player* player);
bool eventPartyOnLeave(Party* party, Player* player);
Expand All @@ -56,6 +59,9 @@ class Events
private:
LuaScriptInterface scriptInterface;

// Creature
int32_t creatureOnChangeOutfit;

// Party
int32_t partyOnJoin;
int32_t partyOnLeave;
Expand Down
4 changes: 4 additions & 0 deletions src/game.cpp
Expand Up @@ -3763,6 +3763,10 @@ void Game::changeSpeed(Creature* creature, int32_t varSpeedDelta)

void Game::internalCreatureChangeOutfit(Creature* creature, const Outfit_t& outfit)
{
if (!g_events->eventCreatureOnChangeOutfit(creature, outfit)) {
return;
}

creature->setCurrentOutfit(outfit);

if (creature->isInvisible()) {
Expand Down

0 comments on commit 56749a6

Please sign in to comment.