int NpcScriptInterface::luaCreatureGetPos(lua_State *L) { /*/ //creatureGetPosition(cid) popNumber(L); reportErrorFunc("Deprecated function. Use getCreaturePosition"); /*/ uint32_t uid = popNumber(L); ScriptEnviroment* env = getScriptEnv(); Creature* creature = env->getCreatureByUID(uid); if(creature){ Position pos = creature->getPosition(); lua_pushnumber(L, pos.x); lua_pushnumber(L, pos.y); lua_pushnumber(L, pos.z); } else{ reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); } return 3; }
bool TalkAction::executeSay(Creature* creature, const std::string& words, const std::string& param) { // onSay(cid, words, param) if(m_scriptInterface->reserveScriptEnv()){ ScriptEnviroment* env = m_scriptInterface->getScriptEnv(); #ifdef __DEBUG_LUASCRIPTS__ std::stringstream desc; desc << creature->getName() << " - " << words << " " << param; env->setEventDesc(desc.str()); #endif env->setScriptId(m_scriptId, m_scriptInterface); env->setRealPos(creature->getPosition()); uint32_t cid = env->addThing(creature); lua_State* L = m_scriptInterface->getLuaState(); m_scriptInterface->pushFunction(m_scriptId); lua_pushnumber(L, cid); lua_pushstring(L, words.c_str()); lua_pushstring(L, param.c_str()); bool result = m_scriptInterface->callFunction(3); m_scriptInterface->releaseScriptEnv(); return result; } else{ std::cout << "[Error] Call stack overflow. TalkAction::executeSay" << std::endl; return 0; } }
void TileCallback::onTileCombat(Creature* creature, Tile* tile) const { //"onTileCombat"(cid, pos) if(m_scriptInterface->reserveScriptEnv()) { ScriptEnviroment* env = m_scriptInterface->getScriptEnv(); lua_State* L = m_scriptInterface->getLuaState(); if(!env->setCallbackId(m_scriptId, m_scriptInterface)) return; uint32_t cid = 0; if(creature) cid = env->addThing(creature); m_scriptInterface->pushFunction(m_scriptId); lua_pushnumber(L, cid); m_scriptInterface->pushPosition(L, tile->getPosition(), 0); m_scriptInterface->callFunction(2); env->resetCallback(); m_scriptInterface->releaseScriptEnv(); } else { std::cout << "[Error] Call stack overflow. TileCallback::onTileCombat" << std::endl; return; } }
int NpcScriptInterface::luagetDistanceTo(lua_State *L) { //getDistanceTo(uid) uint32_t uid = popNumber(L); ScriptEnviroment* env = getScriptEnv(); Npc* npc = env->getNpc(); Thing* thing = env->getThingByUID(uid); if(thing && npc){ Position thing_pos = thing->getPosition(); Position npc_pos = npc->getPosition(); if(npc_pos.z != thing_pos.z){ lua_pushnumber(L, -1); } else{ int32_t dist = std::max(std::abs(npc_pos.x - thing_pos.x), std::abs(npc_pos.y - thing_pos.y)); lua_pushnumber(L, dist); } } else{ reportErrorFunc(getErrorDesc(LUA_ERROR_THING_NOT_FOUND)); lua_pushnil(L); } return 1; }
int NpcScriptInterface::luaActionSay(lua_State* L) { //selfSay(words [[, target], send_to_all]) // send_to_all defaults to true if there is no target, false otherwise uint32_t parameters = lua_gettop(L); uint32_t target = 0; bool send_to_all = true; if(parameters == 3) { send_to_all = (popNumber(L) == LUA_TRUE); target = popNumber(L); } else if(parameters == 2) { target = popNumber(L); send_to_all = false; } std::string msg(popString(L)); ScriptEnviroment* env = getScriptEnv(); Npc* npc = env->getNpc(); Player* focus = env->getPlayerByUID(target); if(npc){ npc->doSay(msg, focus, send_to_all); } return 0; }
void NpcScript::onThink() { if(m_onThink == -1){ return; } //onThink() if(m_scriptInterface->reserveScriptEnv()){ ScriptEnviroment* env = m_scriptInterface->getScriptEnv(); #ifdef __DEBUG_LUASCRIPTS__ std::stringstream desc; desc << "npc " << m_npc->getName(); env->setEventDesc(desc.str()); #endif env->setScriptId(m_onThink, m_scriptInterface); env->setRealPos(m_npc->getPosition()); env->setNpc(m_npc); m_scriptInterface->pushFunction(m_onThink); m_scriptInterface->callFunction(0); m_scriptInterface->releaseScriptEnv(); } else{ std::cout << "[Error] Call stack overflow. NpcScript::onThink" << std::endl; } }
int NpcScriptInterface::luaCloseShop(lua_State *L) { //closeShopWindow(cid) ScriptEnviroment* env = getScriptEnv(); Npc* npc = env->getNpc(); Player* player = env->getPlayerByUID(popNumber(L)); int32_t buyCallback; int32_t sellCallback; if(player == NULL) { reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND)); lua_pushnumber(L, LUA_ERROR); return 1; } player->sendCloseShop(); player->getShopOwner(buyCallback, sellCallback); if(buyCallback != -1) luaL_unref(L, LUA_REGISTRYINDEX, buyCallback); if(sellCallback != -1) luaL_unref(L, LUA_REGISTRYINDEX, sellCallback); player->setShopOwner(NULL, -1, -1); return 1; }
bool ScriptEvent::executeEvent() { // onRaid() if(m_scriptInterface.reserveScriptEnv()){ ScriptEnviroment* env = m_scriptInterface.getScriptEnv(); #ifdef __DEBUG_LUASCRIPTS__ std::stringstream desc; desc << "Raid event" << std::endl; env->setEventDesc(desc.str()); #endif env->setScriptId(m_scriptId, &m_scriptInterface); m_scriptInterface.pushFunction(m_scriptId); bool result = m_scriptInterface.callFunction(0); m_scriptInterface.releaseScriptEnv(); return result; } else{ std::cout << "[Error] Call stack overflow. ScriptEvent::executeEvent" << std::endl; return 0; } }
void NpcScript::onPlayerTrade(const Player* player, int32_t callback, uint16_t itemid, uint8_t count, uint8_t amount) { if(callback == -1){ return; } //"onBuy"(cid, itemid, count, amount) if(m_scriptInterface->reserveScriptEnv()){ ScriptEnviroment* env = m_scriptInterface->getScriptEnv(); env->setScriptId(-1, m_scriptInterface); env->setRealPos(m_npc->getPosition()); env->setNpc(m_npc); uint32_t cid = env->addThing(const_cast<Player*>(player)); lua_State* L = m_scriptInterface->getLuaState(); LuaScriptInterface::pushCallback(L, callback); lua_pushnumber(L, cid); lua_pushnumber(L, itemid); lua_pushnumber(L, count); lua_pushnumber(L, amount); m_scriptInterface->callFunction(4); m_scriptInterface->releaseScriptEnv(); } else{ std::cout << "[Error] Call stack overflow. NpcScript::onPlayerTrade" << std::endl; } }
void TargetCallback::onTargetCombat(const CreatureP& creature, const CreatureP& target) const { //"onTargetCombat"(cid, target) if(m_interface->reserveEnv()) { ScriptEnviroment* env = m_interface->getEnv(); if(!env->setCallbackId(m_scriptId, m_interface)) return; uint32_t cid = 0; if(creature) cid = env->addThing(creature); m_interface->pushFunction(m_scriptId); lua_State* L = m_interface->getState(); lua_pushnumber(L, cid); lua_pushnumber(L, env->addThing(target)); int32_t size = lua_gettop(L); if(lua_pcall(L, 2, 0 /*nReturnValues*/, 0) != 0) LuaScriptInterface::error(nullptr, std::string(LuaScriptInterface::popString(L))); if((lua_gettop(L) + 2 /*nParams*/ + 1) != size) LuaScriptInterface::error(__FUNCTION__, "Stack size changed!"); env->resetCallback(); m_interface->releaseEnv(); } else { LOGe("[TargetCallback::onTargetCombat] Call stack overflow."); return; } }
bool CreatureEvent::executeOnLogout(Player* player) { //onLogout(cid) if(m_scriptInterface->reserveScriptEnv()) { ScriptEnviroment* env = m_scriptInterface->getScriptEnv(); #ifdef __DEBUG_LUASCRIPTS__ std::stringstream desc; desc << player->getName(); env->setEventDesc(desc.str()); #endif env->setScriptId(m_scriptId, m_scriptInterface); env->setRealPos(player->getPosition()); uint32_t cid = env->addThing(player); lua_State* L = m_scriptInterface->getLuaState(); m_scriptInterface->pushFunction(m_scriptId); lua_pushnumber(L, cid); bool result = m_scriptInterface->callFunction(1); m_scriptInterface->releaseScriptEnv(); return result; } else { std::cout << "[Error] Call stack overflow. CreatureEvent::executeOnLogout" << std::endl; return 0; } }
void CreatureEvent::executeOnAdvance(Player* player, levelTypes_t type, uint32_t oldLevel, uint32_t newLevel) { //onAdvance(cid, type, oldlevel, newlevel) if(m_scriptInterface->reserveScriptEnv()) { ScriptEnviroment* env = m_scriptInterface->getScriptEnv(); #ifdef __DEBUG_LUASCRIPTS__ std::stringstream desc; desc << player->getName(); env->setEventDesc(desc.str()); #endif env->setScriptId(m_scriptId, m_scriptInterface); env->setRealPos(player->getPosition()); uint32_t cid = env->addThing(player); lua_State* L = m_scriptInterface->getLuaState(); m_scriptInterface->pushFunction(m_scriptId); lua_pushnumber(L, cid); lua_pushnumber(L, (uint32_t)type); lua_pushnumber(L, oldLevel); lua_pushnumber(L, newLevel); m_scriptInterface->callFunction(4); m_scriptInterface->releaseScriptEnv(); } else { std::cout << "[Error] Call stack overflow. CreatureEvent::executeOnAdvance" << std::endl; } }
int NpcScriptInterface::luaActionSayCommand(lua_State* L) { //commandSay(words) std::string msg(popString(L)); ScriptEnviroment* env = getScriptEnv(); Npc* mynpc = env->getNpc(); if(mynpc) if(commands.exeCommand(mynpc, msg)) return 0; }
int NpcScriptInterface::luaUpdateNpcIdle(lua_State *L) { // updateNpcIdle() ScriptEnviroment* env = getScriptEnv(); Npc* npc = env->getNpc(); if (npc) { npc->updateIdle(); } return 1; }
int NpcScriptInterface::luaIsNpcIdle(lua_State *L) { // isNpcIdle() ScriptEnviroment* env = getScriptEnv(); Npc* npc = env->getNpc(); if (npc) { lua_pushboolean(L, npc->isIdle()); } return 1; }
int NpcScriptInterface::luaGetNpcFocus(lua_State* L) { //getNpcFocus() ScriptEnviroment* env = getScriptEnv(); Npc* npc = env->getNpc(); if (npc) { lua_pushnumber(L, npc->focusCreature); } return 1; }
int NpcScriptInterface::luaActionMove(lua_State* L) { //selfMove(direction) Direction dir = (Direction)popNumber(L); ScriptEnviroment* env = getScriptEnv(); Npc* npc = env->getNpc(); if(npc){ npc->doMove(dir); } return 0; }
int NpcScriptInterface::luaSetNpcFocus(lua_State *L) { //doNpcSetCreatureFocus(cid) uint32_t cid = popNumber(L); ScriptEnviroment* env = getScriptEnv(); Npc* npc = env->getNpc(); if(npc){ Creature* creature = env->getCreatureByUID(cid); npc->setCreatureFocus(creature); } return 0; }
int NpcScriptInterface::luaGetNpcName(lua_State* L) { //getNpcName() ScriptEnviroment* env = getScriptEnv(); Npc* npc = env->getNpc(); if(npc){ lua_pushstring(L, npc->getName().c_str()); } else{ lua_pushstring(L, ""); } return 1; }
int NpcScriptInterface::luaGetNpcCid(lua_State* L) { //getNpcCid() ScriptEnviroment* env = getScriptEnv(); Npc* npc = env->getNpc(); if(npc){ uint32_t cid = env->addThing(npc); lua_pushnumber(L, cid); } else{ lua_pushnil(L); } return 1; }
int NpcScriptInterface::luaActionMoveTo(lua_State* L) { //selfMoveTo(x,y,z) Position target; target.z = (int)popNumber(L); target.y = (int)popNumber(L); target.x = (int)popNumber(L); ScriptEnviroment* env = getScriptEnv(); Npc* npc = env->getNpc(); if(npc){ npc->doMoveTo(target); } return 0; }
int NpcScriptInterface::luaUnqueuePlayer(lua_State *L) { // unqueuePlayer(cid) uint32_t cid = popNumber(L); ScriptEnviroment* env = getScriptEnv(); Npc* npc = env->getNpc(); if (npc) { Creature* creature = env->getCreatureByUID(cid); if (creature) { npc->queueList.remove(cid); } } return 1; }
int NpcScriptInterface::luaFaceCreature(lua_State *L) { // facePlayer(cid) uint32_t cid = popNumber(L); ScriptEnviroment* env = getScriptEnv(); Npc* npc = env->getNpc(); if (npc) { Creature* creature = env->getCreatureByUID(cid); if (creature) { g_game.internalCreatureTurn(npc, npc->getDir(creature)); } } return 1; }
int NpcScriptInterface::luaGetNpcPos(lua_State* L) { //getNpcPos() ScriptEnviroment* env = getScriptEnv(); Position pos(0, 0, 0); uint32_t stackpos = 0; Npc* npc = env->getNpc(); if(npc){ pos = npc->getPosition(); stackpos = npc->getParent()->__getIndexOfThing(npc); } pushPosition(L, pos, stackpos); return 1; }
void TargetCallback::onTargetCombat(Creature* creature, Creature* target) const { //"onTargetCombat"(cid, target) if (m_scriptInterface->reserveScriptEnv()) { ScriptEnviroment* env = m_scriptInterface->getScriptEnv(); lua_State* L = m_scriptInterface->getLuaState(); if (!env->setCallbackId(m_scriptId, m_scriptInterface)) { return; } uint32_t cid = 0; if (creature) { cid = env->addThing(creature); } uint32_t targetCid = env->addThing(target); m_scriptInterface->pushFunction(m_scriptId); lua_pushnumber(L, cid); lua_pushnumber(L, targetCid); int size0 = lua_gettop(L); if (lua_pcall(L, 2, 0 /*nReturnValues*/, 0) != 0) { LuaScriptInterface::reportError(NULL, LuaScriptInterface::popString(L)); } if ((lua_gettop(L) + 2 /*nParams*/ + 1) != size0) { LuaScriptInterface::reportError(NULL, "Stack size changed!"); } env->resetCallback(); m_scriptInterface->releaseScriptEnv(); } else { std::cout << "[Error] Call stack overflow. TargetCallback::onTargetCombat" << std::endl; return; } }
int NpcScriptInterface::luaSelfGetPos(lua_State *L) { //selfGetPosition() ScriptEnviroment* env = getScriptEnv(); Npc* npc = env->getNpc(); if(npc){ Position pos = npc->getPosition(); lua_pushnumber(L, pos.x); lua_pushnumber(L, pos.y); lua_pushnumber(L, pos.z); } else{ lua_pushnil(L); lua_pushnil(L); lua_pushnil(L); } return 3; }
int NpcScriptInterface::luaSetNpcFocus(lua_State* L) { //setNpcFocus(cid) uint32_t cid = popNumber(L); ScriptEnviroment* env = getScriptEnv(); Npc* npc = env->getNpc(); if (npc) { Creature* creature = env->getCreatureByUID(cid); if (creature) { npc->setCreatureFocus(creature); } else { npc->focusCreature = 0; npc->setWalkDelay(std::time(unsigned(NULL)) + 5); } } return 1; }
int NpcScriptInterface::luaGetQueuedPlayer(lua_State *L) { // getQueuedPlayer() ScriptEnviroment* env = getScriptEnv(); Npc* npc = env->getNpc(); if (npc) { if (npc->queueList.empty()) { lua_pushnil(L); return 0; } Creature* creature = env->getCreatureByUID(npc->queueList.front()); if (creature) { lua_pushnumber(L, creature->getID()); npc->queueList.pop_front(); } } return 1; }
int NpcScriptInterface::luaActionSay(lua_State* L) { //selfSay(words, <optional> delay) int32_t parameters = lua_gettop(L); uint32_t delay = SCHEDULER_MINTICKS; if (parameters > 1) { delay = std::max(delay, popNumber(L)); } std::string msg(popString(L)); ScriptEnviroment* env = getScriptEnv(); Npc* npc = env->getNpc(); if(npc){ npc->doSay(msg, delay); } return 0; }
uint32_t MoveEvent::executeEquip(Player* player, Item* item, slots_t slot) { //onEquip(cid, item, slot) //onDeEquip(cid, item, slot) if (m_scriptInterface->reserveScriptEnv()) { ScriptEnviroment* env = m_scriptInterface->getScriptEnv(); #ifdef __DEBUG_LUASCRIPTS__ std::stringstream desc; desc << player->getName() << " itemid:" << item->getID() << " slot:" << slot; env->setEventDesc(desc.str()); #endif env->setScriptId(m_scriptId, m_scriptInterface); env->setRealPos(player->getPosition()); uint32_t cid = env->addThing(player); uint32_t itemid = env->addThing(item); lua_State* L = m_scriptInterface->getLuaState(); m_scriptInterface->pushFunction(m_scriptId); lua_pushnumber(L, cid); LuaScriptInterface::pushThing(L, item, itemid); lua_pushnumber(L, slot); bool result = m_scriptInterface->callFunction(3); m_scriptInterface->releaseScriptEnv(); return result; } else { std::cout << "[Error] Call stack overflow. MoveEvent::executeEquip" << std::endl; return 0; } }