Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. uint32_t CreatureEvent::executeDeath(Creature* creature, Item* corpse, DeathList deathList)
  2. {
  3.     //onDeath(cid, corpse, deathList)
  4.     if(m_interface->reserveEnv())
  5.     {
  6.         ScriptEnviroment* env = m_interface->getEnv();
  7.         if(m_scripted == EVENT_SCRIPT_BUFFER)
  8.         {
  9.             env->setRealPos(creature->getPosition());
  10.             std::stringstream scriptstream;
  11.             scriptstream << "local cid = " << env->addThing(creature) << std::endl;
  12.  
  13.             env->streamThing(scriptstream, "corpse", corpse, env->addThing(corpse));
  14.             scriptstream << "local deathList = {}" << std::endl;
  15.             for(DeathList::iterator it = deathList.begin(); it != deathList.end(); ++it)
  16.             {
  17.                 scriptstream << "deathList:insert(";
  18.                 if(it->isCreatureKill())
  19.                     scriptstream << env->addThing(it->getKillerCreature());
  20.                 else
  21.                     scriptstream << it->getKillerName();
  22.  
  23.                 scriptstream << ")" << std::endl;
  24.             }
  25.  
  26.             scriptstream << m_scriptData;
  27.             bool result = true;
  28.             if(m_interface->loadBuffer(scriptstream.str()))
  29.             {
  30.                 lua_State* L = m_interface->getState();
  31.                 result = m_interface->getGlobalBool(L, "_result", true);
  32.             }
  33.  
  34.             m_interface->releaseEnv();
  35.             return result;
  36.         }
  37.         else
  38.         {
  39.             #ifdef __DEBUG_LUASCRIPTS__
  40.             char desc[35];
  41.             sprintf(desc, "%s", creature->getName().c_str());
  42.             env->setEventDesc(desc);
  43.             #endif
  44.  
  45.             env->setScriptId(m_scriptId, m_interface);
  46.             env->setRealPos(creature->getPosition());
  47.  
  48.             lua_State* L = m_interface->getState();
  49.             m_interface->pushFunction(m_scriptId);
  50.  
  51.             lua_pushnumber(L, env->addThing(creature));
  52.             LuaScriptInterface::pushThing(L, corpse, env->addThing(corpse));
  53.  
  54.             lua_newtable(L);
  55.             DeathList::iterator it = deathList.begin();
  56.             for(int32_t i = 1; it != deathList.end(); ++it, ++i)
  57.             {
  58.                 lua_pushnumber(L, i);
  59.                 if(it->isCreatureKill())
  60.                     lua_pushnumber(L, env->addThing(it->getKillerCreature()));
  61.                 else
  62.                     lua_pushstring(L, it->getKillerName().c_str());
  63.  
  64.                 lua_settable(L, -3);
  65.             }
  66.  
  67.             bool result = m_interface->callFunction(3);
  68.             m_interface->releaseEnv();
  69.             return result;
  70.         }
  71.     }
  72.     else
  73.     {
  74.         std::cout << "[Error - CreatureEvent::executeDeath] Call stack overflow." << std::endl;
  75.         return 0;
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement