Skip to content
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

Metatables for Lua #27

Merged
merged 2 commits into from
Aug 7, 2013
Merged
Show file tree
Hide file tree
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
31 changes: 31 additions & 0 deletions src/creatureevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ bool CreatureEvent::configureEvent(xmlNodePtr p)
m_type = CREATURE_EVENT_KILL;
} else if (tmpStr == "advance") {
m_type = CREATURE_EVENT_ADVANCE;
} else if (tmpStr == "modalwindow") {
m_type = CREATURE_EVENT_MODALWINDOW;
} else {
std::cout << "[Error - CreatureEvent::configureEvent] No valid type for creature event." << str << std::endl;
return false;
Expand Down Expand Up @@ -226,6 +228,9 @@ std::string CreatureEvent::getScriptEventName()

case CREATURE_EVENT_ADVANCE:
return "onAdvance";

case CREATURE_EVENT_MODALWINDOW:
return "onModalWindow";

case CREATURE_EVENT_NONE:
default:
Expand Down Expand Up @@ -448,3 +453,29 @@ uint32_t CreatureEvent::executeOnKill(Creature* creature, Creature* target)
return 0;
}
}

uint32_t CreatureEvent::executeModalWindow(Player* player, uint32_t modalWindowId, uint8_t buttonId, uint8_t choiceId)
{
//onModalWindow(cid, modalWindowId, buttonId, choiceId)
if (!m_scriptInterface->reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeModalWindow] Call stack overflow." << std::endl;
return 0;
}

ScriptEnvironment* env = m_scriptInterface->getScriptEnv();

env->setScriptId(m_scriptId, m_scriptInterface);
env->setRealPos(player->getPosition());

lua_State* L = m_scriptInterface->getLuaState();

m_scriptInterface->pushFunction(m_scriptId);
LuaScriptInterface::pushNumber(L, player->getID());
LuaScriptInterface::pushNumber(L, modalWindowId);
LuaScriptInterface::pushNumber(L, buttonId);
LuaScriptInterface::pushNumber(L, choiceId);

bool result = m_scriptInterface->callFunction(4);
m_scriptInterface->releaseScriptEnv();
return result;
}
4 changes: 3 additions & 1 deletion src/creatureevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ enum CreatureEventType_t {
CREATURE_EVENT_PREPAREDEATH,
CREATURE_EVENT_DEATH,
CREATURE_EVENT_KILL,
CREATURE_EVENT_ADVANCE
CREATURE_EVENT_ADVANCE,
CREATURE_EVENT_MODALWINDOW
};

class CreatureEvent;
Expand Down Expand Up @@ -93,6 +94,7 @@ class CreatureEvent : public Event
uint32_t executeOnDeath(Creature* creature, Item* corpse, Creature* killer, Creature* mostDamageKiller, bool lastHitUnjustified, bool mostDamageUnjustified);
uint32_t executeOnKill(Creature* creature, Creature* target);
uint32_t executeAdvance(Player* player, skills_t, uint32_t, uint32_t);
uint32_t executeModalWindow(Player* player, uint32_t modalWindowId, uint8_t buttonId, uint8_t choiceId);
//

protected:
Expand Down
6 changes: 6 additions & 0 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6543,6 +6543,12 @@ bool Game::playerAnswerModalWindow(uint32_t playerId, uint32_t modalWindowId, ui
}

player->setBedItem(NULL);
} else {
const CreatureEventList& modalWindowEvents = player->getCreatureEvents(CREATURE_EVENT_MODALWINDOW);

for (auto it = modalWindowEvents.begin(), end = modalWindowEvents.end(); it != end; ++it) {
(*it)->executeModalWindow(player, modalWindowId, button, choice);
}
}

return true;
Expand Down
Loading