Advertisement
Guest User

Untitled

a guest
Jul 10th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 134.14 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////
  2. // OpenTibia - an opensource roleplaying game
  3. //////////////////////////////////////////////////////////////////////
  4. // Lua script interface
  5. //////////////////////////////////////////////////////////////////////
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU General Public License
  8. // as published by the Free Software Foundation; either version 2
  9. // of the License, or (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software Foundation,
  18. // Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19. //////////////////////////////////////////////////////////////////////
  20. #include "otpch.h"
  21.  
  22. #include <string>
  23. #include <iostream>
  24. #include <sstream>
  25.  
  26. #include "luascript.h"
  27. #include "player.h"
  28. #include "item.h"
  29. #include "game.h"
  30. #include "house.h"
  31. #include "housetile.h"
  32. #include "status.h"
  33. #include "combat.h"
  34. #include "spells.h"
  35. #include "condition.h"
  36. #include "monsters.h"
  37. #include "baseevents.h"
  38. #include "town.h"
  39. #include "ioplayer.h"
  40. #include "configmanager.h"
  41.  
  42. extern Game g_game;
  43. extern Monsters g_monsters;
  44. extern ConfigManager g_config;
  45.  
  46. enum{
  47.     EVENT_ID_LOADING = 1,
  48.     EVENT_ID_USER = 1000,
  49. };
  50.  
  51. ScriptEnviroment::ThingMap ScriptEnviroment::m_globalMap;
  52. ScriptEnviroment::AreaMap ScriptEnviroment::m_areaMap;
  53. ScriptEnviroment::CombatMap ScriptEnviroment::m_combatMap;
  54. ScriptEnviroment::ConditionMap ScriptEnviroment::m_conditionMap;
  55. ScriptEnviroment::StorageMap ScriptEnviroment::m_globalStorageMap;
  56.  
  57. ScriptEnviroment::ScriptEnviroment()
  58. {
  59.     resetEnv();
  60.     m_lastUID = 70000;
  61.     m_lastAreaId = 0;
  62.     m_lastCombatId = 0;
  63.     m_lastConditionId = 0;
  64. }
  65.  
  66. ScriptEnviroment::~ScriptEnviroment()
  67. {
  68.     //
  69. }
  70.  
  71. void ScriptEnviroment::resetEnv()
  72. {
  73.     m_scriptId = 0;
  74.     m_callbackId = 0;
  75.     m_timerEvent = false;
  76.     m_interface = NULL;
  77.     m_localMap.clear();
  78.  
  79.     m_realPos.x = 0;
  80.     m_realPos.y = 0;
  81.     m_realPos.z = 0;
  82. }
  83.  
  84. bool ScriptEnviroment::setCallbackId(int32_t callbackId, LuaScriptInterface* scriptInterface)
  85. {
  86.     if(m_callbackId == 0){
  87.         m_callbackId = callbackId;
  88.         m_interface = scriptInterface;
  89.         return true;
  90.     }
  91.     else{
  92.         //nested callbacks are not allowed
  93.         if(m_interface){
  94.             m_interface->reportError(__FUNCTION__, "Nested callbacks!");
  95.         }
  96.         return false;
  97.     }
  98. }
  99.  
  100. void ScriptEnviroment::getEventInfo(int32_t& scriptId, std::string& desc, LuaScriptInterface*& scriptInterface, int32_t& callbackId, bool& timerEvent)
  101. {
  102.     scriptId = m_scriptId;
  103.     desc = m_eventdesc;
  104.     scriptInterface = m_interface;
  105.     callbackId = m_callbackId;
  106.     timerEvent = m_timerEvent;
  107. }
  108.  
  109. void ScriptEnviroment::addUniqueThing(Thing* thing)
  110. {
  111.     Item* item = thing->getItem();
  112.     if(item && item->getUniqueId() != 0 ){
  113.         int32_t uid = item->getUniqueId();
  114.        
  115.         Thing* tmp = m_globalMap[uid];
  116.         if(!tmp){
  117.             m_globalMap[uid] = thing;
  118.         }
  119.         else{
  120.             std::cout << "Duplicate uniqueId " <<  uid << std::endl;
  121.         }
  122.     }
  123. }
  124.  
  125. uint32_t ScriptEnviroment::addThing(Thing* thing)
  126. {
  127.     if(thing){
  128.         ThingMap::iterator it;
  129.         for(it = m_localMap.begin(); it != m_localMap.end(); ++it){
  130.             if(it->second == thing){
  131.                 return it->first;
  132.             }
  133.         }
  134.    
  135.         uint32_t newUid;
  136.         if(Creature* creature = thing->getCreature()){
  137.             newUid = creature->getID();
  138.         }
  139.         else{
  140.             if(Item* item = thing->getItem()){
  141.                 uint32_t uid = item->getUniqueId();
  142.                 if(uid && item->getTile() == item->getParent()){
  143.                     m_localMap[uid] = thing;
  144.                     return uid;
  145.                 }
  146.             }
  147.        
  148.             ++m_lastUID;
  149.             if(m_lastUID > 0xFFFFFF)
  150.                 m_lastUID = 70000;
  151.        
  152.             while(m_localMap[m_lastUID]){
  153.                 ++m_lastUID;
  154.             }
  155.             newUid = m_lastUID;
  156.         }
  157.    
  158.         m_localMap[newUid] = thing;
  159.         return newUid;
  160.     }
  161.     else{
  162.         return 0;
  163.     }
  164. }
  165.    
  166. Thing* ScriptEnviroment::getThingByUID(uint32_t uid)
  167. {
  168.     Thing* tmp = m_localMap[uid];
  169.     if(tmp && !tmp->isRemoved()){
  170.         return tmp;
  171.     }
  172.     tmp = m_globalMap[uid];
  173.     if(tmp && !tmp->isRemoved()){
  174.         return tmp;
  175.     }
  176.     if(uid >= 0x10000000){ //is a creature id
  177.         tmp = g_game.getCreatureByID(uid);
  178.         if(tmp && !tmp->isRemoved()){
  179.             m_localMap[uid] = tmp;
  180.             return tmp;
  181.         }
  182.     }
  183.     return NULL;
  184. }
  185.  
  186. Item* ScriptEnviroment::getItemByUID(uint32_t uid)
  187. {
  188.     Thing* tmp = getThingByUID(uid);
  189.     if(tmp){
  190.         if(Item* item = tmp->getItem())
  191.             return item;
  192.     }
  193.     return NULL;
  194. }
  195.  
  196. Container* ScriptEnviroment::getContainerByUID(uint32_t uid)
  197. {
  198.     Item* tmp = getItemByUID(uid);
  199.     if(tmp){
  200.         if(Container* container = tmp->getContainer())
  201.             return container;
  202.     }
  203.     return NULL;
  204. }
  205.  
  206. Creature* ScriptEnviroment::getCreatureByUID(uint32_t uid)
  207. {
  208.     Thing* tmp = getThingByUID(uid);
  209.     if(tmp){
  210.         if(Creature* creature = tmp->getCreature())
  211.             return creature;
  212.     }
  213.     return NULL;
  214. }
  215.  
  216. Player* ScriptEnviroment::getPlayerByUID(uint32_t uid)
  217. {
  218.     Thing* tmp = getThingByUID(uid);
  219.     if(tmp){
  220.         if(Creature* creature = tmp->getCreature())
  221.             if(Player* player = creature->getPlayer())
  222.                 return player;
  223.     }
  224.     return NULL;
  225. }
  226.  
  227. uint32_t ScriptEnviroment::addCombatArea(AreaCombat* area)
  228. {
  229.     uint32_t newAreaId = m_lastAreaId + 1;
  230.     m_areaMap[newAreaId] = area;
  231.    
  232.     m_lastAreaId++;
  233.     return newAreaId;
  234. }
  235.  
  236. AreaCombat* ScriptEnviroment::getCombatArea(uint32_t areaId) const
  237. {
  238.     AreaMap::const_iterator it = m_areaMap.find(areaId);
  239.     if(it != m_areaMap.end()){
  240.         return it->second;
  241.     }
  242.  
  243.     return NULL;
  244. }
  245.  
  246. uint32_t ScriptEnviroment::addCombatObject(Combat* combat)
  247. {
  248.     uint32_t newCombatId = m_lastCombatId + 1;
  249.     m_combatMap[newCombatId] = combat;
  250.    
  251.     m_lastCombatId++;
  252.     return newCombatId;
  253. }
  254.  
  255. const Combat* ScriptEnviroment::getCombatObject(uint32_t combatId) const
  256. {
  257.     CombatMap::iterator it = m_combatMap.find(combatId);
  258.     if(it != m_combatMap.end()){
  259.         return it->second;
  260.     }
  261.  
  262.     return NULL;
  263. }
  264.  
  265. Combat* ScriptEnviroment::getCombatObject(uint32_t combatId)
  266. {
  267.     CombatMap::iterator it = m_combatMap.find(combatId);
  268.     if(it != m_combatMap.end()){
  269.         return it->second;
  270.     }
  271.  
  272.     return NULL;
  273. }
  274.  
  275. uint32_t ScriptEnviroment::addConditionObject(Condition* condition)
  276. {
  277.     uint32_t newConditionId = m_lastConditionId + 1;
  278.     m_conditionMap[newConditionId] = condition;
  279.    
  280.     m_lastConditionId++;
  281.     return m_lastConditionId;
  282. }
  283.  
  284. const Condition* ScriptEnviroment::getConditionObject(uint32_t conditionId) const
  285. {
  286.     ConditionMap::iterator it = m_conditionMap.find(conditionId);
  287.     if(it != m_conditionMap.end()){
  288.         return it->second;
  289.     }
  290.  
  291.     return NULL;
  292. }
  293.  
  294. Condition* ScriptEnviroment::getConditionObject(uint32_t conditionId)
  295. {
  296.     ConditionMap::iterator it = m_conditionMap.find(conditionId);
  297.     if(it != m_conditionMap.end()){
  298.         return it->second;
  299.     }
  300.  
  301.     return NULL;
  302. }
  303.  
  304. void ScriptEnviroment::addGlobalStorageValue(const uint32_t key, const int32_t value)
  305. {
  306.     m_globalStorageMap[key] = value;
  307. }
  308.  
  309. bool ScriptEnviroment::getGlobalStorageValue(const uint32_t key, int32_t& value) const
  310. {
  311.     StorageMap::const_iterator it;
  312.     it = m_globalStorageMap.find(key);
  313.     if(it != m_globalStorageMap.end()){
  314.         value = it->second;
  315.         return true;
  316.     }
  317.     else{
  318.         value = 0;
  319.         return false;
  320.     }
  321. }
  322.  
  323. std::string LuaScriptInterface::getErrorDesc(ErrorCode_t code){
  324.     switch(code){
  325.     case LUA_ERROR_PLAYER_NOT_FOUND:
  326.         return "Player not found";
  327.         break;
  328.     case LUA_ERROR_CREATURE_NOT_FOUND:
  329.         return "Creature not found";
  330.         break;
  331.     case LUA_ERROR_ITEM_NOT_FOUND:
  332.         return "Item not found";
  333.         break;
  334.     case LUA_ERROR_THING_NOT_FOUND:
  335.         return "Thing not found";
  336.         break;
  337.     case LUA_ERROR_TILE_NOT_FOUND:
  338.         return "Tile not found";
  339.         break;
  340.     case LUA_ERROR_HOUSE_NOT_FOUND:
  341.         return "House not found";
  342.         break;
  343.     case LUA_ERROR_COMBAT_NOT_FOUND:
  344.         return "Combat not found";
  345.         break;
  346.     case LUA_ERROR_CONDITION_NOT_FOUND:
  347.         return "Condition not found";
  348.         break;
  349.     case LUA_ERROR_AREA_NOT_FOUND:
  350.         return "Area not found";
  351.         break;
  352.     case LUA_ERROR_CONTAINER_NOT_FOUND:
  353.         return "Container not found";
  354.         break;
  355.     case LUA_ERROR_VARIANT_NOT_FOUND:
  356.         return "Variant not found";
  357.     case LUA_ERROR_VARIANT_UNKNOWN:
  358.         return "Unknown variant type";
  359.         break;
  360.     default:
  361.         return "Wrong error code!";
  362.         break;
  363.     };
  364. }
  365.  
  366. ScriptEnviroment LuaScriptInterface::m_scriptEnv[16];
  367. int32_t LuaScriptInterface::m_scriptEnvIndex = -1;
  368.  
  369. LuaScriptInterface::LuaScriptInterface(std::string interfaceName)
  370. {
  371.     m_luaState = NULL;
  372.     m_interfaceName = interfaceName;
  373.     m_lastEventTimerId = 1000;
  374. }
  375.  
  376. LuaScriptInterface::~LuaScriptInterface()
  377. {
  378.     closeState();
  379. }
  380.    
  381. bool LuaScriptInterface::reInitState()
  382. {
  383.     closeState();
  384.     return initState();
  385. }
  386.  
  387. void LuaScriptInterface::dumpLuaStack()
  388. {
  389.     int a = lua_gettop(m_luaState);
  390.     std::cout <<  "stack size: " << a << std::endl;
  391.     for(int i = 1; i <= a ; ++i){
  392.         std::cout << lua_typename(m_luaState, lua_type(m_luaState,-i)) << " " << lua_topointer(m_luaState, -i) << std::endl;
  393.     }
  394. }
  395.  
  396. int32_t LuaScriptInterface::loadFile(const std::string& file, Npc* npc /* = NULL*/)
  397. {
  398.     //loads file as a chunk at stack top
  399.     int ret = luaL_loadfile(m_luaState, file.c_str());
  400.     if(ret != 0){
  401.         m_lastLuaError = popString(m_luaState);
  402.         return -1;
  403.     }
  404.     //check that it is loaded as a function
  405.     if(lua_isfunction(m_luaState, -1) == 0){
  406.         return -1;
  407.     }
  408.    
  409.     m_loadingFile = file;
  410.     this->reserveScriptEnv();
  411.     ScriptEnviroment* env = this->getScriptEnv();
  412.     env->setScriptId(EVENT_ID_LOADING, this);
  413.     env->setNpc(npc);
  414.    
  415.     //execute it
  416.     ret = lua_pcall(m_luaState, 0, 0, 0);
  417.     if(ret != 0){
  418.         reportError(NULL, std::string(popString(m_luaState)));
  419.         this->releaseScriptEnv();
  420.         return -1;
  421.     }
  422.    
  423.     this->releaseScriptEnv();
  424.     return 0;
  425. }
  426.  
  427. int32_t LuaScriptInterface::getEvent(const std::string& eventName)
  428. {
  429.     //get our events table
  430.     lua_getfield(m_luaState, LUA_REGISTRYINDEX, "EVENTS");
  431.     if(lua_istable(m_luaState, -1) == 0){
  432.         lua_pop(m_luaState, 1);
  433.         return -1;
  434.     }
  435.     //get current event function pointer
  436.     lua_getglobal(m_luaState, eventName.c_str());
  437.     if(lua_isfunction(m_luaState, -1) == 0){
  438.         lua_pop(m_luaState, 1);
  439.         return -1;
  440.     }
  441.     //save in our events table
  442.     lua_pushnumber(m_luaState, m_runningEventId);
  443.     lua_pushvalue(m_luaState, -2);
  444.     lua_rawset(m_luaState, -4);
  445.     lua_pop(m_luaState, 2);
  446.     //reset global value of this event
  447.     lua_pushnil(m_luaState);
  448.     lua_setglobal(m_luaState, eventName.c_str());
  449.  
  450.     m_cacheFiles[m_runningEventId] = m_loadingFile + ":" + eventName;
  451.     ++m_runningEventId;
  452.     return m_runningEventId - 1;
  453. }
  454.  
  455. const std::string& LuaScriptInterface::getFileById(int32_t scriptId)
  456. {
  457.     static std::string unk = "(Unknown scriptfile)";
  458.     if(scriptId != EVENT_ID_LOADING){
  459.         ScriptsCache::iterator it = m_cacheFiles.find(scriptId);
  460.         if(it != m_cacheFiles.end()){
  461.             return it->second;
  462.         }
  463.         else{
  464.             return unk;
  465.         }
  466.     }
  467.     else{
  468.         return m_loadingFile;
  469.     }
  470. }
  471.  
  472. void LuaScriptInterface::reportError(const char* function, const std::string& error_desc)
  473. {
  474.     ScriptEnviroment* env = getScriptEnv();
  475.     int32_t scriptId;
  476.     int32_t callbackId;
  477.     bool timerEvent;
  478.     std::string event_desc;
  479.     LuaScriptInterface* scriptInterface;
  480.     env->getEventInfo(scriptId, event_desc, scriptInterface, callbackId, timerEvent);
  481.    
  482.     std::cout << std::endl << "Lua Script Error: ";
  483.     if(scriptInterface){
  484.         std::cout << "[" << scriptInterface->getInterfaceName() << "] " << std::endl;
  485.         if(timerEvent){
  486.             std::cout << "in a timer event called from: " << std::endl;
  487.         }
  488.         if(callbackId){
  489.             std::cout << "in callback: " << scriptInterface->getFileById(callbackId) << std::endl;
  490.         }
  491.         std::cout << scriptInterface->getFileById(scriptId) << std::endl;
  492.     }
  493.     std::cout << event_desc << std::endl;
  494.     if(function)
  495.         std::cout << function << "(). ";
  496.     std::cout << error_desc << std::endl;
  497. }
  498.  
  499. bool LuaScriptInterface::pushFunction(int32_t functionId)
  500. {
  501.     lua_getfield(m_luaState, LUA_REGISTRYINDEX, "EVENTS");
  502.     if(lua_istable(m_luaState, -1) != 0){
  503.         lua_pushnumber(m_luaState, functionId);
  504.         lua_rawget(m_luaState, -2);
  505.         lua_remove(m_luaState, -2);
  506.         if(lua_isfunction(m_luaState, -1) != 0){
  507.             return true;
  508.         }
  509.     }
  510.     return false;
  511. }
  512.  
  513. bool LuaScriptInterface::initState()
  514. {
  515.     m_luaState = luaL_newstate();
  516.     if(!m_luaState){
  517.         return false;
  518.     }
  519.     luaopen_base(m_luaState);
  520.     luaopen_table(m_luaState);
  521.     luaopen_os(m_luaState);
  522.     luaopen_string(m_luaState);
  523.     luaopen_math(m_luaState);
  524.     //luaL_openlibs(m_luaState);
  525.    
  526.     std::string datadir = g_config.getString(ConfigManager::DATA_DIRECTORY);
  527.    
  528.     if(loadFile(std::string(datadir + "global.lua")) == -1){
  529.         std::cout << "Warning: [LuaScriptInterface::initState] Can not load " << datadir << "global.lua." << std::endl;
  530.     }
  531.  
  532.     registerFunctions();
  533.    
  534.     lua_newtable(m_luaState);
  535.     lua_setfield(m_luaState, LUA_REGISTRYINDEX, "EVENTS");
  536.    
  537.     m_runningEventId = EVENT_ID_USER;
  538.     return true;
  539. }
  540.  
  541. bool LuaScriptInterface::closeState()
  542. {
  543.     m_cacheFiles.clear();
  544.    
  545.     LuaTimerEvents::iterator it;
  546.     for(it = m_timerEvents.begin(); it != m_timerEvents.end(); ++it){
  547.         luaL_unref(m_luaState, LUA_REGISTRYINDEX, it->second.parameter);
  548.         luaL_unref(m_luaState, LUA_REGISTRYINDEX, it->second.function);
  549.     }
  550.     m_timerEvents.clear();
  551.    
  552.     lua_close(m_luaState);
  553.     return true;
  554. }
  555.  
  556. void LuaScriptInterface::executeTimerEvent(uint32_t eventIndex)
  557. {
  558.     OTSYS_THREAD_LOCK_CLASS lockClass(g_game.gameLock, "LuaScriptInterface::executeTimerEvent()");
  559.     LuaTimerEvents::iterator it = m_timerEvents.find(eventIndex);
  560.     if(it != m_timerEvents.end()){
  561.         //push function
  562.         lua_rawgeti(m_luaState, LUA_REGISTRYINDEX, it->second.function);
  563.        
  564.         //push parameters
  565.         lua_rawgeti(m_luaState, LUA_REGISTRYINDEX, it->second.parameter);
  566.        
  567.         //call the function
  568.         if(reserveScriptEnv()){
  569.             ScriptEnviroment* env = getScriptEnv();
  570.             env->setTimerEvent();
  571.             env->setScriptId(it->second.scriptId, this);
  572.             callFunction(1);
  573.             releaseScriptEnv();
  574.         }
  575.         else{
  576.             std::cout << "[Error] Call stack overflow. LuaScriptInterface::executeTimerEvent" << std::endl;
  577.         }
  578.        
  579.         //free resources
  580.         luaL_unref(m_luaState, LUA_REGISTRYINDEX, it->second.parameter);
  581.         luaL_unref(m_luaState, LUA_REGISTRYINDEX, it->second.function);
  582.         m_timerEvents.erase(it);
  583.     }    
  584. }
  585.  
  586. int32_t LuaScriptInterface::callFunction(uint32_t nParams)
  587. {
  588.     int32_t result = LUA_NO_ERROR;
  589.  
  590.     int size0 = lua_gettop(m_luaState);
  591.     if(lua_pcall(m_luaState, nParams, 1, 0) != 0){
  592.         LuaScriptInterface::reportError(NULL, std::string(LuaScriptInterface::popString(m_luaState)));
  593.         result = LUA_ERROR;
  594.     }
  595.     else{
  596.         result = (int32_t)LuaScriptInterface::popNumber(m_luaState);
  597.     }
  598.  
  599.     if((lua_gettop(m_luaState) + (int)nParams  + 1) != size0){
  600.         LuaScriptInterface::reportError(NULL, "Stack size changed!");
  601.     }
  602.  
  603.     return result;
  604. }
  605.  
  606. void LuaScriptInterface::pushVariant(lua_State *L, const LuaVariant& var)
  607. {
  608.     lua_newtable(L);
  609.     setField(L, "type", var.type);
  610.  
  611.     switch(var.type){
  612.         case VARIANT_NUMBER: setField(L, "number", var.number); break;
  613.         case VARIANT_STRING: setField(L, "string", var.text); break;
  614.         case VARIANT_TARGETPOSITION:
  615.         case VARIANT_POSITION:
  616.         {
  617.                 lua_pushstring(L, "pos");
  618.                 pushPosition(L, var.pos);
  619.                 lua_settable(L, -3);
  620.                 break;
  621.         }
  622.         case VARIANT_NONE:
  623.             break;
  624.      }
  625. }
  626.  
  627. void LuaScriptInterface::pushThing(lua_State *L, Thing* thing, uint32_t thingid)
  628. {
  629.     lua_newtable(L);
  630.     if(thing && thing->getItem()){    
  631.         const Item* item = thing->getItem();
  632.         setField(L, "uid", thingid);
  633.         setField(L, "itemid", item->getID());
  634.  
  635.         if(item->hasSubType())
  636.             setField(L, "type", item->getItemCountOrSubtype());
  637.         else
  638.             setField(L, "type", 0);
  639.  
  640.         setField(L, "actionid", item->getActionId());
  641.     }
  642.     else if(thing && thing->getCreature()){
  643.         const Creature* creature = thing->getCreature();
  644.         setField(L, "uid", thingid);
  645.         setField(L, "itemid", 1);
  646.         char type;
  647.         if(creature->getPlayer()){
  648.             type = 1;
  649.         }
  650.         else if(creature->getMonster()){
  651.             type = 2;
  652.         }
  653.         else{//npc
  654.             type = 3;
  655.         }    
  656.         setField(L, "type", type);
  657.         setField(L, "actionid", 0);
  658.     }    
  659.     else{
  660.         setField(L, "uid", 0);
  661.         setField(L, "itemid", 0);
  662.         setField(L, "type", 0);
  663.         setField(L, "actionid", 0);
  664.     }
  665. }
  666.  
  667. void LuaScriptInterface::pushPosition(lua_State *L, const PositionEx& position)
  668. {
  669.     lua_newtable(L);
  670.     setField(L, "z", position.z);
  671.     setField(L, "y", position.y);
  672.     setField(L, "x", position.x);
  673.     setField(L, "stackpos", position.stackpos);
  674. }
  675.  
  676. void LuaScriptInterface::pushPosition(lua_State *L, const Position& position, uint32_t stackpos)
  677. {
  678.     lua_newtable(L);
  679.     setField(L, "z", position.z);
  680.     setField(L, "y", position.y);
  681.     setField(L, "x", position.x);
  682.     setField(L, "stackpos", stackpos);
  683. }
  684.  
  685. LuaVariant LuaScriptInterface::popVariant(lua_State *L)
  686. {
  687.     uint32_t type = getField(L, "type");
  688.  
  689.     LuaVariant var;
  690.     var.type = (LuaVariantType_t)type;
  691.  
  692.     switch(type){
  693.         case VARIANT_NUMBER:
  694.         {
  695.             var.number = getField(L, "number");
  696.             break;
  697.         }
  698.  
  699.         case VARIANT_STRING:
  700.         {
  701.             var.text = getField(L, "string");
  702.             break;
  703.         }
  704.  
  705.         case VARIANT_POSITION:
  706.         case VARIANT_TARGETPOSITION:
  707.         {
  708.             lua_pushstring(L, "pos");
  709.             lua_gettable(L, -2);
  710.             popPosition(L, var.pos);
  711.             break;
  712.         }
  713.  
  714.         default:
  715.         {
  716.             var.type = VARIANT_NONE;
  717.             break;
  718.         }
  719.     }
  720.    
  721.     lua_pop(L, 1); //table
  722.    
  723.     return var;
  724. }
  725.  
  726. void LuaScriptInterface::popPosition(lua_State *L, PositionEx& position)
  727. {
  728.     position.z = getField(L, "z");
  729.     position.y = getField(L, "y");
  730.     position.x = getField(L, "x");
  731.     position.stackpos = getField(L, "stackpos");
  732.  
  733.     lua_pop(L, 1); //table
  734. }
  735.  
  736. void LuaScriptInterface::popPosition(lua_State *L, Position& position, uint32_t& stackpos)
  737. {
  738.     position.z = getField(L, "z");
  739.     position.y = getField(L, "y");
  740.     position.x = getField(L, "x");
  741.     stackpos = getField(L, "stackpos");
  742.     lua_pop(L, 1); //table
  743. }
  744.  
  745. uint32_t LuaScriptInterface::popNumber(lua_State *L)
  746. {
  747.     lua_pop(L,1);
  748.     return (uint32_t)lua_tonumber(L, 0);
  749. }
  750.  
  751. double LuaScriptInterface::popFloatNumber(lua_State *L)
  752. {
  753.     lua_pop(L,1);
  754.     return (double)lua_tonumber(L, 0);
  755. }
  756.  
  757. const char* LuaScriptInterface::popString(lua_State *L)
  758. {
  759.     lua_pop(L,1);
  760.     return lua_tostring(L, 0);
  761. }
  762.    
  763. int32_t LuaScriptInterface::getField(lua_State *L, const char *key)
  764. {
  765.     int32_t result;
  766.     lua_pushstring(L, key);
  767.     lua_gettable(L, -2);  // get table[key]
  768.     result = (int32_t)lua_tonumber(L, -1);
  769.     lua_pop(L, 1);  // remove number and key
  770.     return result;
  771. }
  772.  
  773. void LuaScriptInterface::setField(lua_State *L, const char* index, uint32_t val)
  774. {
  775.     lua_pushstring(L, index);
  776.     lua_pushnumber(L, (double)val);
  777.     lua_settable(L, -3);
  778. }
  779.    
  780. void LuaScriptInterface::setField(lua_State *L, const char* index, const std::string& val)
  781. {
  782.     lua_pushstring(L, index);
  783.     lua_pushstring(L, val.c_str());
  784.     lua_settable(L, -3);
  785. }
  786.  
  787. void LuaScriptInterface::registerFunctions()
  788. {
  789.     //lua_register(L, "name", C_function);
  790.    
  791.     //getPlayerFood(uid)
  792.     lua_register(m_luaState, "getPlayerFood", LuaScriptInterface::luaGetPlayerFood);
  793.     //getPlayerHealth(uid)    
  794.     lua_register(m_luaState, "getPlayerHealth", LuaScriptInterface::luaGetPlayerHealth);
  795.     //getPlayerMana(uid)
  796.     lua_register(m_luaState, "getPlayerMana", LuaScriptInterface::luaGetPlayerMana);
  797.     //getPlayerLevel(uid)
  798.     lua_register(m_luaState, "getPlayerLevel", LuaScriptInterface::luaGetPlayerLevel);
  799.     //getPlayerMagLevel(uid)
  800.     lua_register(m_luaState, "getPlayerMagLevel", LuaScriptInterface::luaGetPlayerMagLevel);
  801.     //getPlayerName(uid)
  802.     lua_register(m_luaState, "getPlayerName", LuaScriptInterface::luaGetPlayerName);
  803.     //getPlayerAccess(uid)
  804.     lua_register(m_luaState, "getPlayerAccess", LuaScriptInterface::luaGetPlayerAccess);
  805.     //getPlayerPosition(uid)
  806.     lua_register(m_luaState, "getPlayerPosition", LuaScriptInterface::luaGetPlayerPosition);
  807.     //getPlayerSkill(uid,skillid)
  808.     lua_register(m_luaState, "getPlayerSkill", LuaScriptInterface::luaGetPlayerSkill);
  809.     //getPlayerMasterPos(cid)
  810.     lua_register(m_luaState, "getPlayerMasterPos", LuaScriptInterface::luaGetPlayerMasterPos);
  811.     //getPlayerTown(cid)
  812.     lua_register(m_luaState, "getPlayerTown", LuaScriptInterface::luaGetPlayerTown);
  813.     //getPlayerVocation(cid)
  814.     lua_register(m_luaState, "getPlayerVocation", LuaScriptInterface::luaGetPlayerVocation);
  815.     //getPlayerItemCount(cid,itemid)
  816.     lua_register(m_luaState, "getPlayerItemCount", LuaScriptInterface::luaGetPlayerItemCount);
  817.     //getPlayerSoul(cid)
  818.     lua_register(m_luaState, "getPlayerSoul", LuaScriptInterface::luaGetPlayerSoul);
  819.     //getPlayerFreeCap(cid)
  820.     lua_register(m_luaState, "getPlayerFreeCap", LuaScriptInterface::luaGetPlayerFreeCap);
  821.     //getPlayerLight(cid)
  822.     lua_register(m_luaState, "getPlayerLight", LuaScriptInterface::luaGetPlayerLight);
  823.     //getPlayerSlotItem(cid, slot)
  824.     lua_register(m_luaState, "getPlayerSlotItem", LuaScriptInterface::luaGetPlayerSlotItem);
  825.     //getPlayerDepotItems(uid, depotid)
  826.     lua_register(m_luaState, "getPlayerDepotItems", LuaScriptInterface::luaGetPlayerDepotItems);    
  827.     //getPlayerSex(cid)
  828.     lua_register(m_luaState, "getPlayerSex", LuaScriptInterface::luaGetPlayerSex);
  829.     //getPlayerLookDir(cid)
  830.     lua_register(m_luaState, "getPlayerLookDir", LuaScriptInterface::luaGetPlayerLookDir);
  831.    
  832.     //getPlayerStorageValue(uid,valueid)
  833.     lua_register(m_luaState, "getPlayerStorageValue", LuaScriptInterface::luaGetPlayerStorageValue);
  834.     //setPlayerStorageValue(uid,valueid, newvalue)
  835.     lua_register(m_luaState, "setPlayerStorageValue", LuaScriptInterface::luaSetPlayerStorageValue);
  836.    
  837.     //getGlobalStorageValue(valueid)
  838.     lua_register(m_luaState, "getGlobalStorageValue", LuaScriptInterface::luaGetGlobalStorageValue);
  839.     //setGlobalStorageValue(valueid, newvalue)
  840.     lua_register(m_luaState, "setGlobalStorageValue", LuaScriptInterface::luaSetGlobalStorageValue);
  841.    
  842.     //getTilePzInfo(pos) 1 is pz. 0 no pz.
  843.     lua_register(m_luaState, "getTilePzInfo", LuaScriptInterface::luaGetTilePzInfo);
  844.     //getTileHouseInfo(pos). 0 no house. != 0 house id
  845.     lua_register(m_luaState, "getTileHouseInfo", LuaScriptInterface::luaGetTileHouseInfo);
  846.    
  847.     //getItemRWInfo(uid)
  848.     lua_register(m_luaState, "getItemRWInfo", LuaScriptInterface::luaGetItemRWInfo);
  849.     //getThingfromPos(pos)
  850.     lua_register(m_luaState, "getThingfromPos", LuaScriptInterface::luaGetThingfromPos);
  851.     //getThing(uid)
  852.     lua_register(m_luaState, "getThing", LuaScriptInterface::luaGetThing);
  853.     //getThingPos(uid)
  854.     lua_register(m_luaState, "getThingPos", LuaScriptInterface::luaGetThingPos);
  855.    
  856.     //doRemoveItem(uid,n)
  857.     lua_register(m_luaState, "doRemoveItem", LuaScriptInterface::luaDoRemoveItem);
  858.     //doPlayerFeed(uid,food)
  859.     lua_register(m_luaState, "doPlayerFeed", LuaScriptInterface::luaDoFeedPlayer);    
  860.     //doPlayerSendCancel(uid,text)
  861.     lua_register(m_luaState, "doPlayerSendCancel", LuaScriptInterface::luaDoSendCancel);
  862.     //doPlayerSendDefaultCancel(uid, ReturnValue)
  863.     lua_register(m_luaState, "doPlayerSendDefaultCancel", LuaScriptInterface::luaDoSendDefaultCancel);
  864.     //doTeleportThing(uid,newpos)
  865.     lua_register(m_luaState, "doTeleportThing", LuaScriptInterface::luaDoTeleportThing);
  866.     //doTransformItem(uid,toitemid)
  867.     lua_register(m_luaState, "doTransformItem", LuaScriptInterface::luaDoTransformItem);
  868.     //doPlayerSay(uid,text,type)
  869.     lua_register(m_luaState, "doPlayerSay", LuaScriptInterface::luaDoPlayerSay);
  870.     //doSendMagicEffect(position,type)
  871.     lua_register(m_luaState, "doSendMagicEffect", LuaScriptInterface::luaDoSendMagicEffect);
  872.     //doChangeTypeItem(uid,new_type)
  873.     lua_register(m_luaState, "doChangeTypeItem", LuaScriptInterface::luaDoChangeTypeItem);
  874.     //doSetItemActionId(uid,actionid)
  875.     lua_register(m_luaState, "doSetItemActionId", LuaScriptInterface::luaDoSetItemActionId);
  876.     //doSetItemText(uid,text)
  877.     lua_register(m_luaState, "doSetItemText", LuaScriptInterface::luaDoSetItemText);
  878.     //doSetItemSpecialDescription(uid,desc)
  879.     lua_register(m_luaState, "doSetItemSpecialDescription", LuaScriptInterface::luaDoSetItemSpecialDescription);
  880.     //doSendAnimatedText(position,text,color)
  881.     lua_register(m_luaState, "doSendAnimatedText", LuaScriptInterface::luaDoSendAnimatedText);
  882.     //doPlayerAddSkillTry(cid,skillid,n)
  883.     lua_register(m_luaState, "doPlayerAddSkillTry", LuaScriptInterface::luaDoPlayerAddSkillTry);
  884.     //doPlayerAddHealth(cid,health)
  885.     lua_register(m_luaState, "doPlayerAddHealth", LuaScriptInterface::luaDoPlayerAddHealth);
  886.     //doCreatureAddHealth(cid,health)
  887.     lua_register(m_luaState, "doCreatureAddHealth", LuaScriptInterface::luaDoPlayerAddHealth);
  888.     //doPlayerAddMana(cid,mana)
  889.     lua_register(m_luaState, "doPlayerAddMana", LuaScriptInterface::luaDoPlayerAddMana);
  890.     //doPlayerAddSoul(cid,soul)
  891.     lua_register(m_luaState, "doPlayerAddSoul", LuaScriptInterface::luaDoPlayerAddSoul);
  892.     //doPlayerAddItem(cid,itemid,count or type) . returns uid of the created item
  893.     lua_register(m_luaState, "doPlayerAddItem", LuaScriptInterface::luaDoPlayerAddItem);
  894.     //doPlayerSendTextMessage(cid,MessageClasses,message)
  895.     lua_register(m_luaState, "doPlayerSendTextMessage", LuaScriptInterface::luaDoPlayerSendTextMessage);
  896.     //doPlayerRemoveMoney(cid,money)
  897.     lua_register(m_luaState, "doPlayerRemoveMoney", LuaScriptInterface::luaDoPlayerRemoveMoney);
  898.     //doShowTextWindow(cid,maxlen,canWrite)    
  899.     lua_register(m_luaState, "doShowTextWindow", LuaScriptInterface::luaDoShowTextWindow);    
  900.     //doShowTextDialog(cid,itemid,text)    
  901.     lua_register(m_luaState, "doShowTextDialog", LuaScriptInterface::luaDoShowTextDialog);    
  902.     //doDecayItem(uid)
  903.     lua_register(m_luaState, "doDecayItem", LuaScriptInterface::luaDoDecayItem);
  904.     //doCreateItem(itemid,type or count,position) .only working on ground. returns uid of the created item
  905.     lua_register(m_luaState, "doCreateItem", LuaScriptInterface::luaDoCreateItem);
  906.     //doSummonCreature(name, position)
  907.     lua_register(m_luaState, "doSummonCreature", LuaScriptInterface::luaDoSummonCreature);
  908.     //doMoveCreature(cid, direction)
  909.     lua_register(m_luaState, "doMoveCreature", LuaScriptInterface::luaDoMoveCreature);
  910.     //doPlayerSetMasterPos(cid,pos)
  911.     lua_register(m_luaState, "doPlayerSetMasterPos", LuaScriptInterface::luaDoPlayerSetMasterPos);
  912.     //doPlayerSetTown(cid,townid)
  913.     lua_register(m_luaState, "doPlayerSetTown", LuaScriptInterface::luaDoPlayerSetTown);
  914.     //doPlayerSetVocation(cid,voc)
  915.     lua_register(m_luaState, "doPlayerSetVocation", LuaScriptInterface::luaDoPlayerSetVocation);
  916.     //doPlayerRemoveItem(cid,itemid,count)
  917.     lua_register(m_luaState, "doPlayerRemoveItem", LuaScriptInterface::luaDoPlayerRemoveItem);
  918.     //doPlayerAddExp(cid,exp)
  919.     lua_register(m_luaState, "doPlayerAddExp", LuaScriptInterface::luaDoPlayerAddExp);
  920.     //doPlayerSetGuildId(cid, id)
  921.     //lua_register(m_luaState, "doPlayerSetGuildId", LuaScriptInterface::luaDoPlayerSetGuildId);
  922.     //doPlayerSetGuildRank(cid, rank)
  923.     lua_register(m_luaState, "doPlayerSetGuildRank", LuaScriptInterface::luaDoPlayerSetGuildRank);
  924.     //doPlayerSetGuildNick(cid, nick)
  925.     lua_register(m_luaState, "doPlayerSetGuildNick", LuaScriptInterface::luaDoPlayerSetGuildNick);
  926.     //doPlayerAddOutfit(cid,looktype,addons)
  927.     lua_register(m_luaState, "doPlayerAddOutfit", LuaScriptInterface::luaDoPlayerAddOutfit);
  928.     //doPlayerRemOutfit(cid,looktype,addons)
  929.     lua_register(m_luaState, "doPlayerRemOutfit", LuaScriptInterface::luaDoPlayerRemOutfit);    
  930.     //doSetCreatureLight(cid, lightLevel, lightColor, time)
  931.     lua_register(m_luaState, "doSetCreatureLight", LuaScriptInterface::luaDoSetCreatureLight);
  932.    
  933.     //isPlayer(cid)
  934.     lua_register(m_luaState, "isPlayer", LuaScriptInterface::luaIsPlayer);
  935.     //isCreature(cid)
  936.     lua_register(m_luaState, "isCreature", LuaScriptInterface::luaIsCreature);
  937.     //isContainer(uid)
  938.     lua_register(m_luaState, "isContainer", LuaScriptInterface::luaIsContainer);
  939.     //isMoveable(uid)
  940.     lua_register(m_luaState, "isMoveable", LuaScriptInterface::luaIsMoveable);
  941.  
  942.     //getPlayerByName(name)
  943.     lua_register(m_luaState, "getPlayerByName", LuaScriptInterface::luaGetPlayerByName);
  944.     //registerCreature(cid)
  945.     lua_register(m_luaState, "registerCreature", LuaScriptInterface::luaRegisterCreature);
  946.  
  947.     //getContainerSize(uid)
  948.     lua_register(m_luaState, "getContainerSize", LuaScriptInterface::luaGetContainerSize);
  949.     //getContainerCap(uid)
  950.     lua_register(m_luaState, "getContainerCap", LuaScriptInterface::luaGetContainerCap);
  951.     //getContainerItem(uid, slot)
  952.     lua_register(m_luaState, "getContainerItem", LuaScriptInterface::luaGetContainerItem);
  953.     //doAddContainerItem(uid, itemid, count or subtype)
  954.     lua_register(m_luaState, "doAddContainerItem", LuaScriptInterface::luaDoAddContainerItem);
  955.    
  956.     //getHouseOwner(houseid)
  957.     lua_register(m_luaState, "getHouseOwner", LuaScriptInterface::luaGetHouseOwner);
  958.     //getHouseName(houseid)
  959.     lua_register(m_luaState, "getHouseName", LuaScriptInterface::luaGetHouseName);
  960.     //getHouseEntry(houseid)
  961.     lua_register(m_luaState, "getHouseEntry", LuaScriptInterface::luaGetHouseEntry);
  962.     //getHouseRent(houseid)
  963.     lua_register(m_luaState, "getHouseRent", LuaScriptInterface::luaGetHouseRent);
  964.     //getHouseTown(houseid)
  965.     lua_register(m_luaState, "getHouseTown", LuaScriptInterface::luaGetHouseTown);
  966.     //getHouseAccessList(houseod, listid)
  967.     lua_register(m_luaState, "getHouseAccessList", LuaScriptInterface::luaGetHouseAccessList);
  968.     //getHouseByPlayerName(playername)
  969.     lua_register(m_luaState, "getHouseByPlayerName", LuaScriptInterface::luaGetHouseByPlayerGUID);
  970.     //setHouseAccessList(houseid, listid, listtext)
  971.     lua_register(m_luaState, "setHouseAccessList", LuaScriptInterface::luaSetHouseAccessList);
  972.     //setHouseOwner(houseid, ownerGUID)
  973.     lua_register(m_luaState, "setHouseOwner", LuaScriptInterface::luaSetHouseOwner);
  974.    
  975.     //getWorldType()
  976.     lua_register(m_luaState, "getWorldType", LuaScriptInterface::luaGetWorldType);
  977.     //getWorldTime()
  978.     lua_register(m_luaState, "getWorldTime", LuaScriptInterface::luaGetWorldTime);
  979.     //getWorldLight()
  980.     lua_register(m_luaState, "getWorldLight", LuaScriptInterface::luaGetWorldLight);
  981.     //getWorldCreatures(type) 0 players, 1 monsters, 2 npcs, 3 all
  982.     lua_register(m_luaState, "getWorldCreatures", LuaScriptInterface::luaGetWorldCreatures);
  983.     //getWorldUpTime()
  984.     lua_register(m_luaState, "getWorldUpTime", LuaScriptInterface::luaGetWorldUpTime);
  985.    
  986.     //createCombatArea( {area}, {extArea} )
  987.     lua_register(m_luaState, "createCombatArea", LuaScriptInterface::luaCreateCombatArea);
  988.  
  989.     //createConditionObject(type)
  990.     lua_register(m_luaState, "createConditionObject", LuaScriptInterface::luaCreateConditionObject);
  991.  
  992.     //setCombatArea(combat, area)
  993.     lua_register(m_luaState, "setCombatArea", LuaScriptInterface::luaSetCombatArea);
  994.  
  995.     //setCombatCondition(combat, condition)
  996.     lua_register(m_luaState, "setCombatCondition", LuaScriptInterface::luaSetCombatCondition);
  997.  
  998.     //setCombatParam(combat, key, value)
  999.     lua_register(m_luaState, "setCombatParam", LuaScriptInterface::luaSetCombatParam);
  1000.    
  1001.     //setConditionParam(condition, key, value)
  1002.     lua_register(m_luaState, "setConditionParam", LuaScriptInterface::luaSetConditionParam);
  1003.  
  1004.     //addDamageCondition(condition, key, rounds, time, value)
  1005.     lua_register(m_luaState, "addDamageCondition", LuaScriptInterface::luaAddDamageCondition);
  1006.  
  1007.     //addOutfitCondition(condition, lookTypeEx, lookType, lookHead, lookBody, lookLegs, lookFeet)
  1008.     lua_register(m_luaState, "addOutfitCondition", LuaScriptInterface::luaAddOutfitCondition);
  1009.  
  1010.     //setCombatCallBack(combat, key, function_name)
  1011.     lua_register(m_luaState, "setCombatCallback", LuaScriptInterface::luaSetCombatCallBack);
  1012.  
  1013.     //setCombatFormula(combat, type, mina, minb, maxa, maxb)
  1014.     lua_register(m_luaState, "setCombatFormula", LuaScriptInterface::luaSetCombatFormula);
  1015.  
  1016.     //setConditionFormula(combat, mina, minb, maxa, maxb)
  1017.     lua_register(m_luaState, "setConditionFormula", LuaScriptInterface::luaSetConditionFormula);
  1018.  
  1019.     //doCombat(cid, combat, param)
  1020.     lua_register(m_luaState, "doCombat", LuaScriptInterface::luaDoCombat);
  1021.  
  1022.     //createCombatObject()
  1023.     lua_register(m_luaState, "createCombatObject", LuaScriptInterface::luaCreateCombatObject);
  1024.  
  1025.     //doAreaCombatHealth(cid, type, pos, area, min, max, effect)
  1026.     lua_register(m_luaState, "doAreaCombatHealth", LuaScriptInterface::luaDoAreaCombatHealth);
  1027.  
  1028.     //doTargetCombatHealth(cid, target, type, min, max, effect)
  1029.     lua_register(m_luaState, "doTargetCombatHealth", LuaScriptInterface::luaDoTargetCombatHealth);
  1030.  
  1031.     //doAreaCombatMana(cid, pos, area, min, max, effect)
  1032.     lua_register(m_luaState, "doAreaCombatMana", LuaScriptInterface::luaDoAreaCombatMana);
  1033.  
  1034.     //doTargetCombatMana(cid, target, min, max, effect)
  1035.     lua_register(m_luaState, "doTargetCombatMana", LuaScriptInterface::luaDoTargetCombatMana);
  1036.  
  1037.     //doAreaCombatCondition(cid, pos, area, condition, effect)
  1038.     lua_register(m_luaState, "doAreaCombatCondition", LuaScriptInterface::luaDoAreaCombatCondition);
  1039.  
  1040.     //doTargetCombatCondition(cid, target, condition, effect)
  1041.     lua_register(m_luaState, "doTargetCombatCondition", LuaScriptInterface::luaDoTargetCombatCondition);
  1042.  
  1043.     //doAreaCombatDispel(cid, pos, area, type, effect)
  1044.     lua_register(m_luaState, "doAreaCombatDispel", LuaScriptInterface::luaDoAreaCombatDispel);
  1045.  
  1046.     //doTargetCombatDispel(cid, target, type, effect)
  1047.     lua_register(m_luaState, "doTargetCombatDispel", LuaScriptInterface::luaDoTargetCombatDispel);
  1048.  
  1049.     //doChallengeCreature(cid, target)
  1050.     lua_register(m_luaState, "doChallengeCreature", LuaScriptInterface::luaDoChallengeCreature);
  1051.  
  1052.     //doConvinceCreature(cid, target)
  1053.     lua_register(m_luaState, "doConvinceCreature", LuaScriptInterface::luaDoConvinceCreature);
  1054.  
  1055.     //numberToVariant(number)
  1056.     lua_register(m_luaState, "numberToVariant", LuaScriptInterface::luaNumberToVariant);
  1057.  
  1058.     //stringToVariant(string)
  1059.     lua_register(m_luaState, "stringToVariant", LuaScriptInterface::luaStringToVariant);
  1060.  
  1061.     //positionToVariant(pos)
  1062.     lua_register(m_luaState, "positionToVariant", LuaScriptInterface::luaPositionToVariant);
  1063.  
  1064.     //targetPositionToVariant(pos)
  1065.     lua_register(m_luaState, "targetPositionToVariant", LuaScriptInterface::luaTargetPositionToVariant);
  1066.  
  1067.     //variantToNumber(var)
  1068.     lua_register(m_luaState, "variantToNumber", LuaScriptInterface::luaVariantToNumber);
  1069.  
  1070.     //variantToString(var)
  1071.     lua_register(m_luaState, "variantToString", LuaScriptInterface::luaVariantToString);
  1072.  
  1073.     //variantToPosition(var)
  1074.     lua_register(m_luaState, "variantToPosition", LuaScriptInterface::luaVariantToPosition);
  1075.  
  1076.     //doChangeSpeed(cid, delta)
  1077.     lua_register(m_luaState, "doChangeSpeed", LuaScriptInterface::luaDoChangeSpeed);
  1078.  
  1079.     //doSetMonsterOutfit(cid, name, time)
  1080.     lua_register(m_luaState, "doSetMonsterOutfit", LuaScriptInterface::luaSetMonsterOutfit);
  1081.     //doSetItemOutfit(cid, item, time)
  1082.     lua_register(m_luaState, "doSetItemOutfit", LuaScriptInterface::luaSetItemOutfit);
  1083.     //doSetCreatureOutfit(cid, outfit, time)
  1084.     lua_register(m_luaState, "doSetCreatureOutfit", LuaScriptInterface::luaSetCreatureOutfit);
  1085.     //getCreatureOutfit(cid)
  1086.     lua_register(m_luaState, "getCreatureOutfit", LuaScriptInterface::luaGetCreatureOutfit);
  1087.     //getCreaturePosition(cid)
  1088.     lua_register(m_luaState, "getCreaturePosition", LuaScriptInterface::luaGetCreaturePosition);
  1089.     //getCreatureName(cid)
  1090.     lua_register(m_luaState, "getCreatureName", LuaScriptInterface::luaGetCreatureName);
  1091.  
  1092.     //isItemStackable(itemid)
  1093.     lua_register(m_luaState, "isItemStackable", LuaScriptInterface::luaIsItemStackable);
  1094.     //isItemRune(itemid)
  1095.     lua_register(m_luaState, "isItemRune", LuaScriptInterface::luaIsItemRune);
  1096.     //isItemDoor(itemid)
  1097.     lua_register(m_luaState, "isItemDoor", LuaScriptInterface::luaIsItemDoor);
  1098.     //isItemContainer(itemid)
  1099.     lua_register(m_luaState, "isItemContainer", LuaScriptInterface::luaIsItemContainer);
  1100.     //isItemFluidContainer(itemid)
  1101.     lua_register(m_luaState, "isItemFluidContainer", LuaScriptInterface::luaIsItemFluidContainer);
  1102.     //getItemName(itemid)
  1103.     lua_register(m_luaState, "getItemName", LuaScriptInterface::luaGetItemName);
  1104.  
  1105.     #ifdef __XID_CVS_MODS__
  1106.     //getPlayerSkull(cid)
  1107.     lua_register(m_luaState, "getPlayerSkull", LuaScriptInterface::luaGetPlayerSkull);
  1108.     //getPlayerConditionTicks(cid, conditionid)
  1109.     lua_register(m_luaState, "getPlayerConditionTicks", LuaScriptInterface::luaGetPlayerConditionTicks);
  1110.     #endif
  1111.  
  1112.     #ifdef __XID_SEPERATE_ADDONS__
  1113.     //doPlayerAddAddon(cid, looktype, addon)
  1114.     lua_register(m_luaState, "doPlayerAddAddon", LuaScriptInterface::luaDoPlayerAddAddon);
  1115.     //getPlayerOutfitAddon(cid, looktype)
  1116.     lua_register(m_luaState, "getPlayerOutfitAddon", LuaScriptInterface::luaGetPlayerOutfitAddon);
  1117.     #endif
  1118.  
  1119.     #ifdef __XID_BUY_SELL__
  1120.     //getItemStackable(itemid)
  1121.     lua_register(m_luaState, "getItemStackable", LuaScriptInterface::luaGetItemStackable);
  1122.     #endif
  1123.  
  1124.     #ifdef __XID_PREMIUM_SYSTEM__
  1125.     //isPremium(cid)
  1126.     lua_register(m_luaState, "isPremium", LuaScriptInterface::luaIsPremium);
  1127.     //buyPrem(cid, days)
  1128.     lua_register(m_luaState, "addPremium", LuaScriptInterface::luaBuyPremium);
  1129.     #endif
  1130.  
  1131.     #ifdef __YUR_GUILD_SYSTEM__
  1132.     //foundNewGuild(guildname)
  1133.     lua_register(m_luaState, "foundNewGuild", LuaScriptInterface::luaFoundNewGuild);
  1134.     //getPlayerGuildStatus(name)
  1135.     lua_register(m_luaState, "getPlayerGuildStatus", LuaScriptInterface::luaGetPlayerGuildStatus);
  1136.     //setPlayerGuildStatus(guildstatus, name)
  1137.     lua_register(m_luaState, "setPlayerGuildStatus", LuaScriptInterface::luaSetPlayerGuildStatus);
  1138.     //getPlayerGuildName(name)
  1139.     lua_register(m_luaState, "getPlayerGuildName", LuaScriptInterface::luaGetPlayerGuildName);
  1140.     //setPlayerGuild(name, guildstatus, guilrank, guildname)
  1141.     lua_register(m_luaState, "setPlayerGuild", LuaScriptInterface::luaSetPlayerGuild);
  1142.     //clearPlayerGuild(name)
  1143.     lua_register(m_luaState, "clearPlayerGuild", LuaScriptInterface::luaClearPlayerGuild);
  1144.     //setPlayerGuildNick(name, guildnick)
  1145.     lua_register(m_luaState, "setPlayerGuildNick", LuaScriptInterface::luaSetPlayerGuildNick);
  1146.     //setPlayerGuildTitle(name, guildnick)
  1147.     lua_register(m_luaState, "setPlayerGuildTitle", LuaScriptInterface::luaSetPlayerGuildNick);
  1148.     #endif
  1149.  
  1150.     #ifdef __XID_LEARN_SPELLS__
  1151.     //doPlayerLearnSpell(cid, spellname)
  1152.     lua_register(m_luaState, "doPlayerLearnSpell", LuaScriptInterface::luaDoPlayerLearnSpell);
  1153.     #endif
  1154.    
  1155.     #ifdef __XID_BLESS_SYSTEM__
  1156.     //doPlayerAddBlesing(cid, blessid)
  1157.     lua_register(m_luaState, "doPlayerAddBlessing", LuaScriptInterface::luaDoPlayerAddBlessing);
  1158.     //getPlayerBlessing(cid, blessid)
  1159.     lua_register(m_luaState, "getPlayerBlessing", LuaScriptInterface::luaGetPlayerBlessing);    
  1160.     #endif
  1161.  
  1162.     //debugPrint(text)
  1163.     lua_register(m_luaState, "debugPrint", LuaScriptInterface::luaDebugPrint);
  1164.     //isInArray(array, value)
  1165.     lua_register(m_luaState, "isInArray", LuaScriptInterface::luaIsInArray);
  1166.    
  1167.     //addEvent(callback, delay, parameter)
  1168.     lua_register(m_luaState, "addEvent", LuaScriptInterface::luaAddEvent);
  1169.     //stopEvent(eventid)
  1170.     lua_register(m_luaState, "stopEvent", LuaScriptInterface::luaStopEvent);
  1171.  
  1172.     //getDataDir()
  1173.     lua_register(m_luaState, "getDataDir", LuaScriptInterface::luaGetDataDirectory);
  1174.    
  1175.     //getCreatureTarget(cid)
  1176.     lua_register(m_luaState, "getCreatureTarget", LuaScriptInterface::getCreatureTarget);
  1177. }
  1178.  
  1179.  
  1180. int LuaScriptInterface::internalGetPlayerInfo(lua_State *L, PlayerInfo_t info)
  1181. {
  1182.     uint32_t cid = popNumber(L);
  1183.     ScriptEnviroment* env = getScriptEnv();
  1184.     int32_t value;
  1185.    
  1186.     const Player* player = env->getPlayerByUID(cid);
  1187.     if(player){
  1188.         const Tile *tile;
  1189.         Position pos;
  1190.         uint32_t stackpos;
  1191.         switch(info){
  1192.         case PlayerInfoAccess:
  1193.             value = player->getAccessLevel();
  1194.             break;        
  1195.         case PlayerInfoLevel:
  1196.             value = player->level;
  1197.             break;
  1198.         case PlayerInfoMagLevel:
  1199.             value = player->magLevel;
  1200.             break;
  1201.         case PlayerInfoMana:
  1202.             value = player->mana;
  1203.             break;
  1204.         case PlayerInfoHealth:
  1205.             value = player->health;
  1206.             break;
  1207.         case PlayerInfoName:
  1208.             lua_pushstring(L, player->name.c_str());
  1209.             return 1;
  1210.             break;
  1211.         case PlayerInfoPosition:            
  1212.             pos = player->getPosition();
  1213.             tile = player->getTile();
  1214.             if(tile){
  1215.                 stackpos = player->getParent()->__getIndexOfThing(player);
  1216.             }
  1217.             else{
  1218.                 stackpos = 0;
  1219.             }
  1220.             pushPosition(L, pos, stackpos);
  1221.             return 1;
  1222.             break;
  1223.         case PlayerInfoMasterPos:
  1224.             pos = player->masterPos;
  1225.             pushPosition(L, pos, 0);
  1226.             return 1;
  1227.             break;
  1228.         case PlayerInfoFood:
  1229.         {
  1230.             //value = player->food/1000;
  1231.             value = 0;
  1232.  
  1233.             Condition* condition = player->getCondition(CONDITION_REGENERATION, CONDITIONID_DEFAULT);
  1234.             if(condition){
  1235.                 value = condition->getTicks() / 1000;
  1236.             }
  1237.             else{
  1238.                 value = 0;
  1239.             }
  1240.             break;
  1241.         }
  1242.         case PlayerInfoVocation:
  1243.             value = player->getVocationId();
  1244.             break;
  1245.         case PlayerInfoSoul:
  1246.             value = player->getPlayerInfo(PLAYERINFO_SOUL);
  1247.             break;
  1248.         case PlayerInfoFreeCap:
  1249.             value = (int)player->getFreeCapacity();
  1250.             break;
  1251.         case PlayerInfoSex:
  1252.             value = player->getSex();
  1253.             break;
  1254.         case PlayerInfoLookDirection:
  1255.             value = player->getDirection();
  1256.             break;
  1257.         case PlayerInfoTown:
  1258.             value = player->getTown();
  1259.             break;
  1260.         case PlayerInfoGUID:
  1261.             value = player->getGUID();
  1262.             break;
  1263.         default:
  1264.             std::string error_str = "Unknown player info. info = " + info;
  1265.             reportErrorFunc(error_str);
  1266.             value = 0;
  1267.             break;        
  1268.         }
  1269.         lua_pushnumber(L,value);
  1270.         return 1;
  1271.     }
  1272.     else{
  1273.         std::stringstream error_str;
  1274.         error_str << "Player not found. info = " << info;
  1275.         reportErrorFunc(error_str.str());
  1276.         lua_pushnumber(L, LUA_ERROR);
  1277.         return 1;
  1278.     }        
  1279.    
  1280.     lua_pushnumber(L, LUA_NO_ERROR);
  1281.     return 1;
  1282. }
  1283. //getPlayer[Info](uid)
  1284. int LuaScriptInterface::luaGetPlayerFood(lua_State *L){    
  1285.     return internalGetPlayerInfo(L, PlayerInfoFood);}
  1286.    
  1287. int LuaScriptInterface::luaGetPlayerAccess(lua_State *L){
  1288.     return internalGetPlayerInfo(L, PlayerInfoAccess);}
  1289.    
  1290. int LuaScriptInterface::luaGetPlayerLevel(lua_State *L){
  1291.     return internalGetPlayerInfo(L, PlayerInfoLevel);}
  1292.    
  1293. int LuaScriptInterface::luaGetPlayerMagLevel(lua_State *L){
  1294.     return internalGetPlayerInfo(L, PlayerInfoMagLevel);}
  1295.    
  1296. int LuaScriptInterface::luaGetPlayerMana(lua_State *L){
  1297.     return internalGetPlayerInfo(L, PlayerInfoMana);}
  1298.  
  1299. int LuaScriptInterface::luaGetPlayerHealth(lua_State *L){
  1300.     return internalGetPlayerInfo(L, PlayerInfoHealth);}
  1301.    
  1302. int LuaScriptInterface::luaGetPlayerName(lua_State *L){
  1303.     return internalGetPlayerInfo(L, PlayerInfoName);}
  1304.    
  1305. int LuaScriptInterface::luaGetPlayerPosition(lua_State *L){    
  1306.     return internalGetPlayerInfo(L, PlayerInfoPosition);}
  1307.    
  1308. int LuaScriptInterface::luaGetPlayerVocation(lua_State *L){
  1309.     return internalGetPlayerInfo(L, PlayerInfoVocation);}
  1310.  
  1311. int LuaScriptInterface::luaGetPlayerMasterPos(lua_State *L){
  1312.     return internalGetPlayerInfo(L, PlayerInfoMasterPos);}
  1313.  
  1314. int LuaScriptInterface::luaGetPlayerSoul(lua_State *L){
  1315.     return internalGetPlayerInfo(L, PlayerInfoSoul);}
  1316.  
  1317. int LuaScriptInterface::luaGetPlayerFreeCap(lua_State *L){
  1318.     return internalGetPlayerInfo(L, PlayerInfoFreeCap);}
  1319.  
  1320. int LuaScriptInterface::luaGetPlayerSex(lua_State *L){
  1321.     return internalGetPlayerInfo(L, PlayerInfoSex);}
  1322.  
  1323. int LuaScriptInterface::luaGetPlayerLookDir(lua_State *L){
  1324.     return internalGetPlayerInfo(L, PlayerInfoLookDirection);}
  1325.  
  1326. int LuaScriptInterface::luaGetPlayerTown(lua_State *L){
  1327.     return internalGetPlayerInfo(L, PlayerInfoTown);}
  1328. //
  1329.  
  1330. int LuaScriptInterface::luaDoRemoveItem(lua_State *L)
  1331. {    
  1332.     //doRemoveItem(uid,n)
  1333.     char n = (char)popNumber(L);    
  1334.     uint32_t uid = popNumber(L);
  1335.    
  1336.     ScriptEnviroment* env = getScriptEnv();
  1337.    
  1338.     Item* item = env->getItemByUID(uid);
  1339.     if(item){
  1340.         g_game.internalRemoveItem(item, n);
  1341.         lua_pushnumber(L, LUA_NO_ERROR);
  1342.     }
  1343.     else{
  1344.         reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND));
  1345.         lua_pushnumber(L, LUA_ERROR);
  1346.     }
  1347.     return 1;
  1348. }
  1349.  
  1350. int LuaScriptInterface::luaDoPlayerRemoveItem(lua_State *L)
  1351. {
  1352.     //doPlayerRemoveItem(cid,itemid,count)
  1353.     uint32_t count = popNumber(L);
  1354.     uint16_t itemId = (uint16_t)popNumber(L);
  1355.     uint32_t cid = popNumber(L);
  1356.    
  1357.     ScriptEnviroment* env = getScriptEnv();
  1358.    
  1359.     Player* player = env->getPlayerByUID(cid);
  1360.     if(player){
  1361.         if(g_game.removeItemOfType(player, itemId, count)){
  1362.             lua_pushnumber(L, LUA_TRUE);
  1363.         }
  1364.         else{
  1365.             lua_pushnumber(L, LUA_FALSE);
  1366.         }
  1367.     }
  1368.     else{
  1369.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  1370.         lua_pushnumber(L, LUA_ERROR);
  1371.     }
  1372.     return 1;
  1373. }
  1374.  
  1375. int LuaScriptInterface::luaDoFeedPlayer(lua_State *L)
  1376. {    
  1377.     //doFeedPlayer(uid,food)
  1378.     int32_t food = (int32_t)popNumber(L);
  1379.     uint32_t cid = popNumber(L);
  1380.    
  1381.     ScriptEnviroment* env = getScriptEnv();
  1382.    
  1383.     Player* player = env->getPlayerByUID(cid);
  1384.     if(player){
  1385.         player->addDefaultRegeneration(food * 1000);
  1386.         lua_pushnumber(L, LUA_NO_ERROR);
  1387.     }
  1388.     else{
  1389.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  1390.         lua_pushnumber(L, LUA_ERROR);
  1391.     }
  1392.     return 1;
  1393. }
  1394.  
  1395. int LuaScriptInterface::luaDoSendCancel(lua_State *L)
  1396. {    
  1397.     //doSendCancel(uid,text)
  1398.     const char * text = popString(L);
  1399.     uint32_t cid = popNumber(L);    
  1400.    
  1401.     ScriptEnviroment* env = getScriptEnv();
  1402.    
  1403.     const Player* player = env->getPlayerByUID(cid);
  1404.     if(player){
  1405.         player->sendCancel(text);
  1406.         lua_pushnumber(L, LUA_NO_ERROR);
  1407.     }
  1408.     else{
  1409.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  1410.         lua_pushnumber(L, LUA_ERROR);
  1411.     }
  1412.     return 1;
  1413. }
  1414.  
  1415. int LuaScriptInterface::luaDoSendDefaultCancel(lua_State *L)
  1416. {    
  1417.     //doPlayerSendDefaultCancel(uid, ReturnValue)
  1418.     ReturnValue ret = (ReturnValue)popNumber(L);    
  1419.     uint32_t cid = popNumber(L);
  1420.    
  1421.     ScriptEnviroment* env = getScriptEnv();
  1422.    
  1423.     const Player* player = env->getPlayerByUID(cid);
  1424.     if(player){
  1425.         player->sendCancelMessage(ret);
  1426.         lua_pushnumber(L, LUA_NO_ERROR);
  1427.     }
  1428.     else{
  1429.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  1430.         lua_pushnumber(L, LUA_ERROR);
  1431.     }
  1432.  
  1433.     return 1;
  1434. }
  1435.  
  1436. int LuaScriptInterface::luaDoTeleportThing(lua_State *L)
  1437. {
  1438.     //doTeleportThing(uid,newpos)
  1439.     Position pos;
  1440.     uint32_t stackpos;
  1441.     popPosition(L, pos, stackpos);
  1442.     uint32_t uid = popNumber(L);    
  1443.    
  1444.     ScriptEnviroment* env = getScriptEnv();
  1445.    
  1446.     Thing* tmp = env->getThingByUID(uid);
  1447.     if(tmp){
  1448.         if(g_game.internalTeleport(tmp,(Position&)pos) == RET_NOERROR){
  1449.             lua_pushnumber(L, LUA_NO_ERROR);
  1450.         }
  1451.         else{
  1452.             reportErrorFunc("Can not teleport thing.");
  1453.             lua_pushnumber(L, LUA_ERROR);
  1454.         }
  1455.     }
  1456.     else{
  1457.         reportErrorFunc(getErrorDesc(LUA_ERROR_THING_NOT_FOUND));
  1458.         lua_pushnumber(L, LUA_ERROR);
  1459.     }
  1460.     return 1;
  1461. }
  1462.  
  1463.    
  1464. int LuaScriptInterface::luaDoTransformItem(lua_State *L)
  1465. {
  1466.     //doTransformItem(uid,toitemid)    
  1467.     uint16_t toId = (uint16_t)popNumber(L);    
  1468.     uint32_t uid = popNumber(L);    
  1469.    
  1470.     ScriptEnviroment* env = getScriptEnv();
  1471.    
  1472.     Item* item = env->getItemByUID(uid);
  1473.     if(item){
  1474.         g_game.transformItem(item, toId);
  1475.         lua_pushnumber(L, LUA_NO_ERROR);
  1476.     }
  1477.     else{
  1478.         reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND));
  1479.         lua_pushnumber(L, LUA_ERROR);
  1480.     }
  1481.     return 1;
  1482. }
  1483.  
  1484. int LuaScriptInterface::luaDoPlayerSay(lua_State *L)
  1485. {
  1486.     //doPlayerSay(uid,text,type)
  1487.     uint32_t type = popNumber(L);    
  1488.     const char * text = popString(L);
  1489.     uint32_t cid = popNumber(L);
  1490.                    
  1491.     ScriptEnviroment* env = getScriptEnv();
  1492.    
  1493.     Player* player = env->getPlayerByUID(cid);
  1494.     if(player){
  1495.         g_game.internalCreatureSay(player,(SpeakClasses)type,std::string(text));
  1496.         lua_pushnumber(L, LUA_NO_ERROR);
  1497.     }
  1498.     else{
  1499.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  1500.         lua_pushnumber(L, LUA_ERROR);
  1501.     }
  1502.     return 1;
  1503. }
  1504.  
  1505. int LuaScriptInterface::luaDoSendMagicEffect(lua_State *L)
  1506. {
  1507.     //doSendMagicEffect(position,type)
  1508.     uint32_t type = popNumber(L);    
  1509.     Position pos;
  1510.     uint32_t stackpos;
  1511.     popPosition(L, pos, stackpos);
  1512.    
  1513.     ScriptEnviroment* env = getScriptEnv();
  1514.    
  1515.     SpectatorVec list;
  1516.     SpectatorVec::iterator it;
  1517.  
  1518.     if(pos.x == 0xFFFF){
  1519.         pos = env->getRealPos();
  1520.     }
  1521.    
  1522.     g_game.addMagicEffect(pos, type);
  1523.     lua_pushnumber(L, LUA_NO_ERROR);
  1524.     return 1;
  1525. }
  1526.  
  1527. int LuaScriptInterface::luaDoChangeTypeItem(lua_State *L)
  1528. {
  1529.     //doChangeTypeItem(uid,new_type)
  1530.     int32_t subtype = (int32_t)popNumber(L);    
  1531.     uint32_t uid = popNumber(L);
  1532.    
  1533.     ScriptEnviroment* env = getScriptEnv();
  1534.    
  1535.     Item* item = env->getItemByUID(uid);
  1536.     if(item){
  1537.         g_game.transformItem(item, item->getID(), subtype);
  1538.         lua_pushnumber(L, LUA_NO_ERROR);
  1539.     }
  1540.     else{
  1541.         reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND));
  1542.         lua_pushnumber(L, LUA_ERROR);
  1543.     }
  1544.     return 1;
  1545. }
  1546.  
  1547.  
  1548. int LuaScriptInterface::luaDoPlayerAddSkillTry(lua_State *L)
  1549. {
  1550.     //doPlayerAddSkillTry(uid,skillid,n)
  1551.     uint32_t n = popNumber(L);
  1552.     uint32_t skillid = popNumber(L);
  1553.     uint32_t cid = popNumber(L);
  1554.    
  1555.     ScriptEnviroment* env = getScriptEnv();
  1556.    
  1557.     Player* player = env->getPlayerByUID(cid);
  1558.     if(player){
  1559.         player->addSkillAdvance((skills_t)skillid, n);
  1560.         lua_pushnumber(L, LUA_NO_ERROR);
  1561.     }
  1562.     else{
  1563.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  1564.         lua_pushnumber(L, LUA_ERROR);
  1565.     }
  1566.     return 1;
  1567. }
  1568.  
  1569.  
  1570. int LuaScriptInterface::luaDoPlayerAddHealth(lua_State *L)
  1571. {
  1572.     //doPlayerAddHealth(uid,health)
  1573.     //doCreatureAddHealth(uid,health)
  1574.     int32_t healthChange = (int32_t)popNumber(L);
  1575.     uint32_t cid = popNumber(L);
  1576.    
  1577.     ScriptEnviroment* env = getScriptEnv();
  1578.    
  1579.     Creature* creature = env->getCreatureByUID(cid);
  1580.     if(creature){
  1581.         if(healthChange >= 0){
  1582.             g_game.combatChangeHealth(COMBAT_HEALING, NULL, creature, healthChange);
  1583.         }
  1584.         else{
  1585.             g_game.combatChangeHealth(COMBAT_UNDEFINEDDAMAGE, NULL, creature, healthChange);
  1586.         }
  1587.  
  1588.         lua_pushnumber(L, LUA_NO_ERROR);
  1589.     }
  1590.     else{
  1591.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  1592.         lua_pushnumber(L, LUA_ERROR);
  1593.     }
  1594.     return 1;
  1595. }
  1596.  
  1597. int LuaScriptInterface::luaDoPlayerAddMana(lua_State *L)
  1598. {
  1599.     //doPlayerAddMana(uid,mana)
  1600.     int32_t manaChange = (int32_t)popNumber(L);
  1601.     uint32_t cid = popNumber(L);
  1602.    
  1603.     ScriptEnviroment* env = getScriptEnv();
  1604.    
  1605.     Player* player = env->getPlayerByUID(cid);
  1606.     if(player){
  1607.         g_game.combatChangeMana(NULL, player, manaChange);
  1608.         lua_pushnumber(L, LUA_NO_ERROR);
  1609.     }
  1610.     else{
  1611.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  1612.         lua_pushnumber(L, LUA_ERROR);
  1613.     }
  1614.     return 1;
  1615. }
  1616.  
  1617. int LuaScriptInterface::luaDoPlayerAddItem(lua_State *L)
  1618. {
  1619.     //doPlayerAddItem(uid,itemid,count or type)
  1620.     uint32_t count = popNumber(L);
  1621.     uint32_t itemId = (uint32_t)popNumber(L);
  1622.     uint32_t cid = popNumber(L);
  1623.    
  1624.     ScriptEnviroment* env = getScriptEnv();
  1625.     Player* player = env->getPlayerByUID(cid);
  1626.     if(!player){
  1627.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  1628.         lua_pushnumber(L, LUA_ERROR);
  1629.         return 1;
  1630.     }
  1631.  
  1632.     const ItemType& it = Item::items[itemId];
  1633.     if(it.stackable && count > 100){
  1634.         count = 100;
  1635.     }
  1636.  
  1637.     Item* newItem = Item::CreateItem(itemId, count);
  1638.  
  1639.     if(!newItem){
  1640.         reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND));
  1641.         lua_pushnumber(L, LUA_ERROR);
  1642.         return 1;
  1643.     }
  1644.  
  1645.     //internalPlayerAddItem(L, player, itemId, count);
  1646.     ReturnValue ret = g_game.internalPlayerAddItem(player, newItem);
  1647.  
  1648.     if(ret != RET_NOERROR){
  1649.         delete newItem;
  1650.         reportErrorFunc("Could not add item");
  1651.         lua_pushnumber(L, LUA_ERROR);
  1652.         return 1;
  1653.     }
  1654.    
  1655.     if(newItem->getParent()){
  1656.         uint32_t uid = env->addThing((Thing*)newItem);
  1657.         lua_pushnumber(L, uid);
  1658.     }
  1659.     else{
  1660.         //stackable item stacked with existing object, newItem will be released
  1661.         lua_pushnumber(L, LUA_NULL);
  1662.     }
  1663.  
  1664.     return 1;
  1665. }
  1666.  
  1667. int LuaScriptInterface::luaDoPlayerSendTextMessage(lua_State *L)
  1668. {    
  1669.     //doPlayerSendTextMessage(uid,MessageClasses,message)
  1670.     const char * text = popString(L);
  1671.     uint32_t messageClass = popNumber(L);
  1672.     uint32_t cid = popNumber(L);
  1673.    
  1674.     ScriptEnviroment* env = getScriptEnv();
  1675.    
  1676.     const Player* player = env->getPlayerByUID(cid);
  1677.     if(player){
  1678.         player->sendTextMessage((MessageClasses)messageClass,text);
  1679.         lua_pushnumber(L, LUA_NO_ERROR);
  1680.     }
  1681.     else{
  1682.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  1683.         lua_pushnumber(L, LUA_ERROR);
  1684.     }        
  1685.     return 1;
  1686. }
  1687.  
  1688. int LuaScriptInterface::luaDoSendAnimatedText(lua_State *L)
  1689. {    
  1690.     //doSendAnimatedText(position,text,color)
  1691.     uint32_t color = popNumber(L);
  1692.     const char * text = popString(L);
  1693.     Position pos;
  1694.     uint32_t stackpos;
  1695.     popPosition(L, pos, stackpos);
  1696.    
  1697.     ScriptEnviroment* env = getScriptEnv();
  1698.    
  1699.     SpectatorVec list;
  1700.     SpectatorVec::iterator it;
  1701.  
  1702.     if(pos.x == 0xFFFF){
  1703.         pos = env->getRealPos();
  1704.     }
  1705.    
  1706.     g_game.addAnimatedText(pos, color, text);
  1707.     return 1;
  1708. }
  1709.  
  1710. int LuaScriptInterface::luaGetPlayerSkill(lua_State *L)
  1711. {
  1712.     //getPlayerSkill(uid,skillid)
  1713.     uint32_t skillid = popNumber(L);
  1714.     uint32_t cid = popNumber(L);
  1715.    
  1716.     ScriptEnviroment* env = getScriptEnv();
  1717.    
  1718.     const Player* player = env->getPlayerByUID(cid);
  1719.     if(player){
  1720.         if(skillid <= 6){
  1721.             uint32_t value = player->skills[skillid][SKILL_LEVEL];
  1722.             lua_pushnumber(L, value);
  1723.         }
  1724.         else{
  1725.             reportErrorFunc("No valid skillId");
  1726.             lua_pushnumber(L, LUA_ERROR);
  1727.         }
  1728.     }
  1729.     else{
  1730.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  1731.         lua_pushnumber(L, LUA_ERROR);
  1732.     }
  1733.     return 1;
  1734. }
  1735.  
  1736. int LuaScriptInterface::luaDoShowTextWindow(lua_State *L)
  1737. {
  1738.     //doShowTextWindow(uid,maxlen,canWrite)
  1739.     popNumber(L);
  1740.     popNumber(L);
  1741.     popNumber(L);
  1742.     reportErrorFunc("Deprecated function.");
  1743.     lua_pushnumber(L, LUA_ERROR);
  1744.     return 1;
  1745. }
  1746.  
  1747. int LuaScriptInterface::luaDoShowTextDialog(lua_State *L)
  1748. {
  1749.     //doShowTextDialog(cid, itemid, text)
  1750.     const char * text = popString(L);
  1751.     uint32_t itemid = popNumber(L);
  1752.     uint32_t cid = popNumber(L);
  1753.    
  1754.     ScriptEnviroment* env = getScriptEnv();
  1755.    
  1756.     Player* player = env->getPlayerByUID(cid);
  1757.     if(player){
  1758.         player->sendTextWindow(itemid, text);
  1759.         lua_pushnumber(L, LUA_NO_ERROR);
  1760.     }
  1761.     else{
  1762.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  1763.         lua_pushnumber(L, LUA_ERROR);
  1764.     }
  1765.     return 1;
  1766. }
  1767.  
  1768. int LuaScriptInterface::luaGetItemRWInfo(lua_State *L)
  1769. {
  1770.     //getItemRWInfo(uid)
  1771.     uint32_t uid = popNumber(L);
  1772.    
  1773.     ScriptEnviroment* env = getScriptEnv();
  1774.    
  1775.     const Item* item = env->getItemByUID(uid);
  1776.     if(item){
  1777.         uint32_t rwflags = 0;
  1778.         if(item->isReadable())
  1779.             rwflags |= 1;
  1780.  
  1781.         if(item->canWriteText()){
  1782.             rwflags |= 2;
  1783.         }
  1784.  
  1785.         lua_pushnumber(L, rwflags);
  1786.     }
  1787.     else{
  1788.         reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND));
  1789.         lua_pushnumber(L, LUA_ERROR);
  1790.     }
  1791.     return 1;
  1792. }
  1793.  
  1794. int LuaScriptInterface::luaDoDecayItem(lua_State *L)
  1795. {
  1796.     //doDecayItem(uid)
  1797.     //Note: to stop decay set decayTo = 0 in items.otb
  1798.     uint32_t uid = popNumber(L);    
  1799.    
  1800.     ScriptEnviroment* env = getScriptEnv();
  1801.    
  1802.     Item* item = env->getItemByUID(uid);
  1803.     if(item){
  1804.         g_game.startDecay(item);
  1805.         lua_pushnumber(L, LUA_NO_ERROR);
  1806.     }
  1807.     else{
  1808.         reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND));
  1809.         lua_pushnumber(L, LUA_ERROR);
  1810.     }
  1811.     return 1;
  1812. }
  1813.  
  1814. int LuaScriptInterface::luaGetThingfromPos(lua_State *L)
  1815. {
  1816.     //getThingfromPos(pos)
  1817.     //Note:
  1818.     //    stackpos = 255. Get the top thing(item moveable or creature).
  1819.     //    stackpos = 254. Get MagicFieldtItem
  1820.     //    stackpos = 253. Get Creature
  1821.    
  1822.     Position pos;
  1823.     uint32_t stackpos;
  1824.     popPosition(L, pos, stackpos);
  1825.    
  1826.     ScriptEnviroment* env = getScriptEnv();
  1827.    
  1828.     Tile* tile = g_game.map->getTile(pos);    
  1829.     Thing *thing = NULL;
  1830.    
  1831.     if(tile){
  1832.         if(stackpos == 255){
  1833.             thing = tile->getTopCreature();
  1834.            
  1835.             if(thing == NULL){
  1836.                 Item* item = tile->getTopDownItem();
  1837.                 if(item && !item->isNotMoveable())
  1838.                     thing = item;
  1839.             }
  1840.         }
  1841.         else if(stackpos == 254){
  1842.             thing = tile->getFieldItem();
  1843.         }
  1844.         else if(stackpos == 253){
  1845.             thing = tile->getTopCreature();
  1846.         }
  1847.         else{
  1848.             thing = tile->__getThing(stackpos);
  1849.         }
  1850.        
  1851.         if(thing){
  1852.             uint32_t thingid = env->addThing(thing);
  1853.             pushThing(L, thing, thingid);
  1854.         }
  1855.         else{
  1856.             pushThing(L, NULL, 0);
  1857.         }
  1858.         return 1;
  1859.        
  1860.     }//if(tile)
  1861.     else{
  1862.         reportErrorFunc(getErrorDesc(LUA_ERROR_TILE_NOT_FOUND));
  1863.         pushThing(L, NULL, 0);
  1864.         return 1;
  1865.     }
  1866. }
  1867.  
  1868. int LuaScriptInterface::luaDoCreateItem(lua_State *L)
  1869. {
  1870.     //doCreateItem(itemid,type or count,position) only working on ground. returns uid of the created item
  1871.     Position pos;
  1872.     uint32_t stackpos;
  1873.     popPosition(L, pos, stackpos);
  1874.     uint32_t count = popNumber(L);
  1875.     uint32_t itemId = (uint32_t)popNumber(L);
  1876.    
  1877.     ScriptEnviroment* env = getScriptEnv();
  1878.    
  1879.     Tile* tile = g_game.map->getTile(pos);
  1880.     if(!tile){
  1881.         reportErrorFunc(getErrorDesc(LUA_ERROR_TILE_NOT_FOUND));
  1882.         lua_pushnumber(L, LUA_ERROR);
  1883.         return 1;
  1884.     }
  1885.    
  1886.     const ItemType& it = Item::items[itemId];
  1887.     if(it.stackable && count > 100){
  1888.         count = 100;
  1889.     }
  1890.  
  1891.     Item* newItem = Item::CreateItem(itemId, count);
  1892.    
  1893.     ReturnValue ret = g_game.internalAddItem(tile, newItem, INDEX_WHEREEVER, FLAG_NOLIMIT);
  1894.     if(ret != RET_NOERROR){
  1895.         delete newItem;
  1896.         reportErrorFunc("Can not add Item");
  1897.         lua_pushnumber(L, LUA_ERROR);
  1898.         return 1;
  1899.     }
  1900.    
  1901.     if(newItem->getParent()){
  1902.         uint32_t uid = env->addThing(newItem);
  1903.         lua_pushnumber(L, uid);
  1904.     }
  1905.     else{
  1906.         //stackable item stacked with existing object, newItem will be released
  1907.         lua_pushnumber(L, LUA_NULL);
  1908.     }
  1909.     return 1;
  1910. }
  1911.  
  1912. int LuaScriptInterface::luaGetPlayerStorageValue(lua_State *L)
  1913. {
  1914.     //getPlayerStorageValue(cid,valueid)
  1915.     uint32_t key = popNumber(L);
  1916.     uint32_t cid = popNumber(L);
  1917.    
  1918.     ScriptEnviroment* env = getScriptEnv();
  1919.    
  1920.     const Player* player = env->getPlayerByUID(cid);
  1921.     if(player){
  1922.         int32_t value;
  1923.         if(player->getStorageValue(key, value)){
  1924.             lua_pushnumber(L, value);
  1925.         }
  1926.         else{
  1927.             lua_pushnumber(L, -1);
  1928.         }
  1929.     }
  1930.     else{
  1931.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  1932.         lua_pushnumber(L, LUA_ERROR);
  1933.     }
  1934.     return 1;
  1935. }
  1936.  
  1937. int LuaScriptInterface::luaSetPlayerStorageValue(lua_State *L)
  1938. {
  1939.     //setPlayerStorageValue(cid,valueid, newvalue)
  1940.     int32_t value = (int32_t)popNumber(L);
  1941.     uint32_t key = popNumber(L);
  1942.     uint32_t cid = popNumber(L);
  1943.     if(IS_IN_KEYRANGE(key, RESERVED_RANGE)){
  1944.         std::stringstream error_str;
  1945.         error_str << "Accesing to reserverd range: "  << key;
  1946.         reportErrorFunc(error_str.str());
  1947.         lua_pushnumber(L, LUA_ERROR);
  1948.         return 1;
  1949.     }
  1950.    
  1951.     ScriptEnviroment* env = getScriptEnv();
  1952.    
  1953.     Player* player = env->getPlayerByUID(cid);
  1954.     if(player){
  1955.         player->addStorageValue(key,value);
  1956.         lua_pushnumber(L, 0);
  1957.     }
  1958.     else{
  1959.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  1960.         lua_pushnumber(L, LUA_ERROR);
  1961.     }
  1962.     return 1;
  1963. }
  1964.  
  1965. int LuaScriptInterface::luaDoSetItemActionId(lua_State *L)
  1966. {
  1967.     //doSetItemActionId(uid,actionid)
  1968.     uint32_t actionid = popNumber(L);    
  1969.     uint32_t uid = popNumber(L);
  1970.    
  1971.     ScriptEnviroment* env = getScriptEnv();
  1972.    
  1973.     Item* item = env->getItemByUID(uid);    
  1974.     if(item){
  1975.         item->setActionId(actionid);
  1976.         lua_pushnumber(L, LUA_NO_ERROR);
  1977.     }
  1978.     else{
  1979.         reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND));
  1980.         lua_pushnumber(L, LUA_ERROR);
  1981.     }
  1982.     return 1;
  1983. }
  1984.  
  1985. int LuaScriptInterface::luaDoSetItemText(lua_State *L)
  1986. {
  1987.     //doSetItemText(uid,text)
  1988.     const char *text = popString(L);
  1989.     uint32_t uid = popNumber(L);    
  1990.    
  1991.     ScriptEnviroment* env = getScriptEnv();
  1992.    
  1993.     Item* item = env->getItemByUID(uid);
  1994.     if(item){
  1995.         std::string str(text);
  1996.         item->setText(str);
  1997.         lua_pushnumber(L, LUA_NO_ERROR);
  1998.     }
  1999.     else{
  2000.         reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND));
  2001.         lua_pushnumber(L, LUA_ERROR);
  2002.     }
  2003.     return 1;
  2004. }
  2005.  
  2006. int LuaScriptInterface::luaDoSetItemSpecialDescription(lua_State *L)
  2007. {
  2008.     //doSetItemSpecialDescription(uid,desc)
  2009.     const char *desc = popString(L);
  2010.     uint32_t uid = popNumber(L);
  2011.    
  2012.     ScriptEnviroment* env = getScriptEnv();
  2013.    
  2014.     Item* item = env->getItemByUID(uid);
  2015.     if(item){
  2016.         std::string str(desc);
  2017.         item->setSpecialDescription(str);
  2018.         lua_pushnumber(L, LUA_NO_ERROR);
  2019.     }
  2020.     else{
  2021.         reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND));
  2022.         lua_pushnumber(L, LUA_ERROR);
  2023.     }
  2024.     return 1;
  2025. }
  2026.  
  2027. int LuaScriptInterface::luaGetTilePzInfo(lua_State *L)
  2028. {
  2029.     //getTilePzInfo(pos)
  2030.     Position pos;
  2031.     uint32_t stackpos;
  2032.     popPosition(L, pos, stackpos);
  2033.    
  2034.     Tile *tile = g_game.map->getTile(pos);
  2035.    
  2036.     if(tile){
  2037.         if(tile->isPz()){
  2038.             lua_pushnumber(L, LUA_TRUE);
  2039.         }
  2040.         else{
  2041.             lua_pushnumber(L, LUA_FALSE);
  2042.         }
  2043.     }
  2044.     else{
  2045.         reportErrorFunc(getErrorDesc(LUA_ERROR_TILE_NOT_FOUND));
  2046.         lua_pushnumber(L, LUA_ERROR);
  2047.     }
  2048.     return 1;
  2049. }
  2050.  
  2051. int LuaScriptInterface::luaGetTileHouseInfo(lua_State *L)
  2052. {
  2053.     //getTileHouseInfo(pos)
  2054.     Position pos;
  2055.     uint32_t stackpos;
  2056.     popPosition(L, pos, stackpos);
  2057.    
  2058.     Tile *tile = g_game.map->getTile(pos);
  2059.     if(tile){
  2060.         if(HouseTile* houseTile = dynamic_cast<HouseTile*>(tile)){
  2061.             House* house = houseTile->getHouse();
  2062.             if(house){
  2063.                 lua_pushnumber(L, house->getHouseId());
  2064.             }
  2065.             else{
  2066.                 lua_pushnumber(L, LUA_NULL);
  2067.             }
  2068.         }
  2069.         else{
  2070.             lua_pushnumber(L, LUA_NULL);
  2071.         }
  2072.     }
  2073.     else{
  2074.         reportErrorFunc(getErrorDesc(LUA_ERROR_TILE_NOT_FOUND));
  2075.         lua_pushnumber(L, LUA_ERROR);
  2076.     }
  2077.     return 1;
  2078. }
  2079.  
  2080.  
  2081. int LuaScriptInterface::luaDoSummonCreature(lua_State *L){
  2082.     //doSummonCreature(name, position)
  2083.     Position pos;
  2084.     uint32_t stackpos;
  2085.     popPosition(L, pos, stackpos);
  2086.     const char *name = popString(L);
  2087.    
  2088.     ScriptEnviroment* env = getScriptEnv();
  2089.    
  2090.     Monster* monster = Monster::createMonster(name);
  2091.  
  2092.     if(!monster){
  2093.         std::string error_str = (std::string)"Monster name(" + name + (std::string)") not found";
  2094.         reportErrorFunc(error_str);
  2095.         lua_pushnumber(L, LUA_ERROR);
  2096.         return 1;
  2097.     }
  2098.    
  2099.     if(!g_game.placeCreature((Position&)pos, monster)){
  2100.         delete monster;
  2101.         std::string error_str = (std::string)"Can not summon monster: " + name;
  2102.         reportErrorFunc(error_str);
  2103.         lua_pushnumber(L, LUA_ERROR);
  2104.         return 1;
  2105.     }
  2106.    
  2107.     uint32_t cid = env->addThing((Thing*)monster);
  2108.    
  2109.     lua_pushnumber(L, cid);
  2110.     return 1;    
  2111. }
  2112.  
  2113.  
  2114. int LuaScriptInterface::luaDoPlayerRemoveMoney(lua_State *L)
  2115. {
  2116.     //doPlayerRemoveMoney(cid,money)
  2117.     uint32_t money = popNumber(L);
  2118.     uint32_t cid = popNumber(L);    
  2119.                    
  2120.     ScriptEnviroment* env = getScriptEnv();
  2121.    
  2122.     Player* player = env->getPlayerByUID(cid);
  2123.     if(player){
  2124.         if(g_game.removeMoney(player, money)){
  2125.             lua_pushnumber(L, LUA_TRUE);
  2126.         }
  2127.         else{
  2128.             lua_pushnumber(L, LUA_FALSE);
  2129.         }
  2130.     }
  2131.     else{
  2132.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  2133.         lua_pushnumber(L, LUA_ERROR);
  2134.     }
  2135.     return 1;
  2136. }
  2137.  
  2138. int LuaScriptInterface::luaDoPlayerSetMasterPos(lua_State *L)
  2139. {
  2140.     //doPlayerSetMasterPos(cid,pos)
  2141.     Position pos;
  2142.     uint32_t stackpos;
  2143.     popPosition(L, pos, stackpos);
  2144.     popNumber(L);
  2145.    
  2146.     reportErrorFunc("Deprecated function. Use doPlayerSetTown");
  2147.     lua_pushnumber(L, LUA_ERROR);
  2148.     return 1;
  2149. }
  2150.  
  2151. int LuaScriptInterface::luaDoPlayerSetTown(lua_State *L)
  2152. {
  2153.     //doPlayerSetTown(cid,towind)
  2154.     uint32_t townid = popNumber(L);
  2155.     uint32_t cid = popNumber(L);
  2156.    
  2157.     ScriptEnviroment* env = getScriptEnv();
  2158.    
  2159.     Player* player = env->getPlayerByUID(cid);
  2160.     if(player){
  2161.         Town* town = Towns::getInstance().getTown(townid);
  2162.         if(town){
  2163.             player->masterPos = town->getTemplePosition();
  2164.             player->setTown(townid);
  2165.             lua_pushnumber(L, LUA_NO_ERROR);
  2166.         }
  2167.         else{
  2168.             reportErrorFunc("Not found townid");
  2169.             lua_pushnumber(L, LUA_ERROR);
  2170.         }
  2171.     }
  2172.     else{
  2173.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  2174.         lua_pushnumber(L, LUA_ERROR);
  2175.     }
  2176.     return 1;
  2177. }
  2178.  
  2179. int LuaScriptInterface::luaDoPlayerSetVocation(lua_State *L)
  2180. {
  2181.     //doPlayerSetVocation(cid,voc)
  2182.     uint32_t voc = popNumber(L);
  2183.     uint32_t cid = popNumber(L);
  2184.    
  2185.     ScriptEnviroment* env = getScriptEnv();
  2186.    
  2187.     Player* player = env->getPlayerByUID(cid);
  2188.     if(player){
  2189.         player->setVocation(voc);
  2190.         lua_pushnumber(L, LUA_NO_ERROR);
  2191.     }
  2192.     else{
  2193.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  2194.         lua_pushnumber(L, LUA_ERROR);
  2195.     }
  2196.     return 1;
  2197. }
  2198.  
  2199.  
  2200. int LuaScriptInterface::luaDebugPrint(lua_State *L)
  2201. {
  2202.     //debugPrint(text)
  2203.     const char * text = popString(L);
  2204.     reportErrorFunc(std::string(text));
  2205.     return 0;
  2206. }
  2207.  
  2208. int LuaScriptInterface::luaDoPlayerAddSoul(lua_State *L)
  2209. {
  2210.     //doPlayerAddSoul(cid,soul)
  2211.     int32_t addsoul = popNumber(L);
  2212.     uint32_t cid = popNumber(L);
  2213.    
  2214.     ScriptEnviroment* env = getScriptEnv();
  2215.    
  2216.     Player* player = env->getPlayerByUID(cid);
  2217.     if(player){
  2218.         player->changeSoul(addsoul);
  2219.         lua_pushnumber(L, LUA_NO_ERROR);
  2220.     }
  2221.     else{
  2222.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  2223.         lua_pushnumber(L, LUA_ERROR);
  2224.     }        
  2225.     return 1;
  2226. }
  2227.  
  2228. int LuaScriptInterface::luaGetPlayerItemCount(lua_State *L)
  2229. {
  2230.     //getPlayerItemCount(cid,itemid)
  2231.     uint32_t itemId = (uint32_t)popNumber(L);
  2232.     uint32_t cid = popNumber(L);
  2233.    
  2234.     ScriptEnviroment* env = getScriptEnv();
  2235.    
  2236.     const Player* player = env->getPlayerByUID(cid);
  2237.     if(player){
  2238.         uint32_t n = player->__getItemTypeCount(itemId);
  2239.         lua_pushnumber(L, n);
  2240.     }
  2241.     else{
  2242.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  2243.         lua_pushnumber(L, LUA_ERROR);
  2244.     }
  2245.     return 1;
  2246. }
  2247.  
  2248.  
  2249. int LuaScriptInterface::luaGetHouseOwner(lua_State *L)
  2250. {
  2251.     //getHouseOwner(houseid)
  2252.     uint32_t houseid = popNumber(L);
  2253.    
  2254.     House* house = Houses::getInstance().getHouse(houseid);
  2255.     if(house){
  2256.         std::string owner = house->getHouseOwner();
  2257.         lua_pushstring(L, owner.c_str());
  2258.     }
  2259.     else{
  2260.         reportErrorFunc(getErrorDesc(LUA_ERROR_HOUSE_NOT_FOUND));
  2261.         lua_pushnumber(L, LUA_ERROR);
  2262.     }
  2263.     return 1;
  2264. }
  2265.  
  2266. int LuaScriptInterface::luaGetHouseName(lua_State *L)
  2267. {
  2268.     //getHouseName(houseid)
  2269.     uint32_t houseid = popNumber(L);
  2270.    
  2271.     House* house = Houses::getInstance().getHouse(houseid);
  2272.     if(house){
  2273.         lua_pushstring(L, house->getName().c_str());
  2274.     }
  2275.     else{
  2276.         reportErrorFunc(getErrorDesc(LUA_ERROR_HOUSE_NOT_FOUND));
  2277.         lua_pushnil(L);
  2278.     }
  2279.     return 1;
  2280. }
  2281.  
  2282. int LuaScriptInterface::luaGetHouseEntry(lua_State *L)
  2283. {
  2284.     //getHouseEntry(houseid)
  2285.     uint32_t houseid = popNumber(L);
  2286.    
  2287.     House* house = Houses::getInstance().getHouse(houseid);
  2288.     if(house){
  2289.         pushPosition(L, house->getEntryPosition(), 0);
  2290.     }
  2291.     else{
  2292.         reportErrorFunc(getErrorDesc(LUA_ERROR_HOUSE_NOT_FOUND));
  2293.         lua_pushnil(L);
  2294.     }
  2295.     return 1;
  2296. }
  2297.  
  2298. int LuaScriptInterface::luaGetHouseRent(lua_State *L)
  2299. {
  2300.     //getHouseRent(houseid)
  2301.     uint32_t houseid = popNumber(L);
  2302.    
  2303.     House* house = Houses::getInstance().getHouse(houseid);
  2304.     if(house){
  2305.         lua_pushnumber(L, house->getRent());
  2306.     }
  2307.     else{
  2308.         reportErrorFunc(getErrorDesc(LUA_ERROR_HOUSE_NOT_FOUND));
  2309.         lua_pushnil(L);
  2310.     }
  2311.     return 1;
  2312. }
  2313.  
  2314. int LuaScriptInterface::luaGetHouseTown(lua_State *L)
  2315. {
  2316.     //getHouseTown(houseid)
  2317.     uint32_t houseid = popNumber(L);
  2318.    
  2319.     House* house = Houses::getInstance().getHouse(houseid);
  2320.     if(house){
  2321.         lua_pushnumber(L, house->getTownId());
  2322.     }
  2323.     else{
  2324.         reportErrorFunc(getErrorDesc(LUA_ERROR_HOUSE_NOT_FOUND));
  2325.         lua_pushnil(L);
  2326.     }
  2327.     return 1;
  2328. }
  2329.  
  2330. int LuaScriptInterface::luaGetHouseAccessList(lua_State *L)
  2331. {
  2332.     //getHouseAccessList(houseid, listid)
  2333.     uint32_t listid = popNumber(L);
  2334.     uint32_t houseid = popNumber(L);
  2335.    
  2336.     House* house = Houses::getInstance().getHouse(houseid);
  2337.     if(house){
  2338.         std::string list;
  2339.         if(house->getAccessList(listid, list)){
  2340.             lua_pushstring(L, list.c_str());
  2341.         }
  2342.         else{
  2343.             reportErrorFunc("No valid listid.");
  2344.             lua_pushnil(L);
  2345.         }
  2346.     }
  2347.     else{
  2348.         reportErrorFunc(getErrorDesc(LUA_ERROR_HOUSE_NOT_FOUND));
  2349.         lua_pushnil(L);
  2350.     }
  2351.     return 1;
  2352. }
  2353.  
  2354. int  LuaScriptInterface::luaGetHouseByPlayerGUID(lua_State *L)
  2355. {
  2356.     //getHouseByPlayerName(playername)
  2357.     std::string name = popString(L);
  2358.    
  2359.     House* house = Houses::getInstance().getHouseByPlayerName(name);
  2360.     if(house){
  2361.         lua_pushnumber(L, house->getHouseId());
  2362.     }
  2363.     else{
  2364.         lua_pushnil(L);
  2365.     }
  2366.    
  2367.     return 1;
  2368. }
  2369.  
  2370. int LuaScriptInterface::luaSetHouseAccessList(lua_State *L)
  2371. {
  2372.     //setHouseAccessList(houseid, listid, listtext)
  2373.     std::string list = popString(L);
  2374.     uint32_t listid = popNumber(L);
  2375.     uint32_t houseid = popNumber(L);
  2376.    
  2377.     House* house = Houses::getInstance().getHouse(houseid);
  2378.     if(house){
  2379.         house->setAccessList(listid, list);
  2380.         lua_pushnumber(L, LUA_NO_ERROR);
  2381.     }
  2382.     else{
  2383.         reportErrorFunc(getErrorDesc(LUA_ERROR_HOUSE_NOT_FOUND));
  2384.         lua_pushnumber(L, LUA_ERROR);
  2385.     }
  2386.     return 1;
  2387. }
  2388.  
  2389. int LuaScriptInterface::luaSetHouseOwner(lua_State *L)
  2390. {
  2391.     //setHouseOwner(houseid, owner)
  2392.     std::string owner = popString(L);
  2393.     uint32_t houseid = popNumber(L);
  2394.    
  2395.     House* house = Houses::getInstance().getHouse(houseid);
  2396.     if(house){
  2397.         house->setHouseOwner(owner);
  2398.         lua_pushnumber(L, LUA_TRUE);
  2399.     }
  2400.     else{
  2401.         reportErrorFunc(getErrorDesc(LUA_ERROR_HOUSE_NOT_FOUND));
  2402.         lua_pushnumber(L, LUA_ERROR);
  2403.     }
  2404.     return 1;
  2405. }
  2406.  
  2407.  
  2408. int LuaScriptInterface::luaGetWorldType(lua_State *L)
  2409. {
  2410.     //getWorldType()
  2411.     switch(g_game.getWorldType()){
  2412.     case WORLD_TYPE_NO_PVP:
  2413.         lua_pushnumber(L, 1);
  2414.         break;
  2415.     case WORLD_TYPE_PVP:
  2416.         lua_pushnumber(L, 2);
  2417.         break;
  2418.     case WORLD_TYPE_PVP_ENFORCED:
  2419.         lua_pushnumber(L, 3);
  2420.         break;
  2421.     default:
  2422.         lua_pushnumber(L, LUA_ERROR);
  2423.         break;
  2424.     }
  2425.     return 1;
  2426. }
  2427.  
  2428. int LuaScriptInterface::luaGetWorldTime(lua_State *L)
  2429. {
  2430.     //getWorldTime()
  2431.     uint32_t time = g_game.light_hour;
  2432.     lua_pushnumber(L, time);
  2433.     return 1;
  2434. }
  2435.  
  2436. int LuaScriptInterface::luaGetWorldLight(lua_State *L)
  2437. {
  2438.     //getWorldLight()
  2439.     uint32_t level = g_game.lightlevel;
  2440.     lua_pushnumber(L, level);
  2441.     lua_pushnumber(L, 0xD7);//color
  2442.     return 2;
  2443. }
  2444.  
  2445. int LuaScriptInterface::luaGetWorldCreatures(lua_State *L)
  2446. {
  2447.     //getWorldCreatures(type) 0 players, 1 monsters, 2 npcs, 3 all
  2448.     uint32_t type = popNumber(L);
  2449.     uint32_t value;
  2450.     switch(type){
  2451.     case 0:
  2452.         value = g_game.getPlayersOnline();
  2453.         break;
  2454.     case 1:
  2455.         value = g_game.getMonstersOnline();
  2456.         break;
  2457.     case 2:
  2458.         value = g_game.getNpcsOnline();
  2459.         break;
  2460.     case 3:
  2461.         value = g_game.getCreaturesOnline();
  2462.         break;
  2463.     default:
  2464.         reportErrorFunc("Wrong creature type.");
  2465.         lua_pushnumber(L, LUA_ERROR);
  2466.         return 1;
  2467.         break;
  2468.     }
  2469.     lua_pushnumber(L, value);
  2470.     return 1;
  2471. }
  2472.  
  2473. int LuaScriptInterface::luaGetWorldUpTime(lua_State *L)
  2474. {
  2475.     //getWorldUpTime()
  2476.     uint32_t uptime = 0;
  2477.  
  2478.     Status* status = Status::instance();
  2479.     if(status){
  2480.         uptime = (OTSYS_TIME() - status->start)/1000;
  2481.     }
  2482.  
  2483.     lua_pushnumber(L, uptime);
  2484.     return 1;
  2485. }
  2486.  
  2487. int LuaScriptInterface::luaGetPlayerLight(lua_State *L)
  2488. {
  2489.     //getPlayerLight(cid)
  2490.     uint32_t cid = popNumber(L);
  2491.    
  2492.     ScriptEnviroment* env = getScriptEnv();
  2493.     const Player* player = env->getPlayerByUID(cid);
  2494.     if(player){
  2495.         LightInfo lightInfo;
  2496.         player->getCreatureLight(lightInfo);
  2497.         lua_pushnumber(L, lightInfo.level);
  2498.         lua_pushnumber(L, lightInfo.color);//color
  2499.     }
  2500.     else{
  2501.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  2502.         lua_pushnumber(L, LUA_ERROR);
  2503.         return 1;
  2504.     }
  2505.     return 2;
  2506. }
  2507.  
  2508. int LuaScriptInterface::luaDoPlayerAddExp(lua_State *L)
  2509. {
  2510.     //doPlayerAddExp(cid,exp)
  2511.     int32_t exp = (int32_t)popNumber(L);
  2512.     uint32_t cid = popNumber(L);
  2513.    
  2514.     ScriptEnviroment* env = getScriptEnv();
  2515.     Player* player = env->getPlayerByUID(cid);
  2516.     if(player){
  2517.         if(exp > 0){
  2518.             player->addExperience(exp);
  2519.             lua_pushnumber(L, LUA_TRUE);
  2520.         }
  2521.         else{
  2522.             lua_pushnumber(L, LUA_FALSE);
  2523.         }
  2524.     }
  2525.     else{
  2526.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  2527.         lua_pushnumber(L, LUA_ERROR);
  2528.     }
  2529.     return 1;
  2530. }
  2531.  
  2532. int LuaScriptInterface::luaGetPlayerSlotItem(lua_State *L)
  2533. {
  2534.     //getPlayerSlotItem(cid, slot)
  2535.     uint32_t slot = popNumber(L);
  2536.     uint32_t cid = popNumber(L);
  2537.    
  2538.     ScriptEnviroment* env = getScriptEnv();
  2539.     const Player* player = env->getPlayerByUID(cid);
  2540.     if(player){
  2541.         Thing* thing = player->__getThing(slot);
  2542.         if(thing){
  2543.             uint32_t uid = env->addThing(thing);
  2544.             pushThing(L, thing, uid);
  2545.         }
  2546.         else{
  2547.             pushThing(L, NULL, 0);
  2548.         }
  2549.     }
  2550.     else{
  2551.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  2552.         pushThing(L, NULL, 0);
  2553.     }
  2554.     return 1;
  2555. }
  2556.  
  2557. int LuaScriptInterface::luaGetThing(lua_State *L)
  2558. {
  2559.     //getThing(uid)
  2560.     uint32_t uid = popNumber(L);
  2561.    
  2562.     ScriptEnviroment* env = getScriptEnv();
  2563.    
  2564.     Thing* thing = env->getThingByUID(uid);
  2565.     if(thing){
  2566.         pushThing(L, thing, uid);
  2567.     }
  2568.     else{
  2569.         reportErrorFunc(getErrorDesc(LUA_ERROR_THING_NOT_FOUND));
  2570.         pushThing(L, NULL, 0);
  2571.     }
  2572.     return 1;
  2573. }
  2574.  
  2575. int LuaScriptInterface::luaGetThingPos(lua_State *L)
  2576. {
  2577.     //getThingPos(uid)
  2578.     uint32_t uid = popNumber(L);
  2579.    
  2580.     ScriptEnviroment* env = getScriptEnv();
  2581.    
  2582.     Thing* thing = env->getThingByUID(uid);
  2583.     Position pos(0, 0, 0);
  2584.     if(thing){
  2585.         pos = thing->getPosition();
  2586.     }
  2587.     else{
  2588.         reportErrorFunc(getErrorDesc(LUA_ERROR_THING_NOT_FOUND));
  2589.     }
  2590.    
  2591.     pushPosition(L, pos, 0);
  2592.     return 1;
  2593. }
  2594.  
  2595. int LuaScriptInterface::luaCreateCombatObject(lua_State *L)
  2596. {
  2597.     //createCombatObject()
  2598.  
  2599.     ScriptEnviroment* env = getScriptEnv();
  2600.  
  2601.     if(env->getScriptId() != EVENT_ID_LOADING){
  2602.         reportError(__FUNCTION__, "This function can only be used while loading the script.");
  2603.         lua_pushnumber(L, LUA_ERROR);
  2604.         return 1;
  2605.     }
  2606.  
  2607.     Combat* combat = new Combat;
  2608.  
  2609.     if(combat){
  2610.         uint32_t newCombatId = env->addCombatObject(combat);
  2611.         lua_pushnumber(L, newCombatId);
  2612.     }
  2613.     else{
  2614.         reportErrorFunc(getErrorDesc(LUA_ERROR_COMBAT_NOT_FOUND));
  2615.         lua_pushnumber(L, LUA_ERROR);
  2616.     }
  2617.     return 1;
  2618. }
  2619.  
  2620. bool LuaScriptInterface::getArea(lua_State *L, std::list<uint32_t>& list, uint32_t& rows)
  2621. {
  2622.     rows = 0;
  2623.     uint32_t i = 0, j = 0;
  2624.     lua_pushnil(L);  // first key //
  2625.  
  2626.     while(lua_next(L, -2) != 0){
  2627.         lua_pushnil(L);
  2628.         while(lua_next(L, -2) != 0){
  2629.             list.push_back((uint32_t)lua_tonumber(L, -1));
  2630.            
  2631.             lua_pop(L, 1);  // removes `value'; keeps `key' for next iteration //
  2632.             j++;
  2633.         }
  2634.  
  2635.         ++rows;
  2636.        
  2637.         j = 0;
  2638.         lua_pop(L, 1);  // removes `value'; keeps `key' for next iteration //
  2639.         i++;
  2640.     }
  2641.    
  2642.     lua_pop(L, 1);
  2643.     return (rows != 0);
  2644. }
  2645.  
  2646. int LuaScriptInterface::luaCreateCombatArea(lua_State *L)
  2647. {
  2648.     //createCombatArea( {area}, {extArea} )
  2649.  
  2650.     ScriptEnviroment* env = getScriptEnv();
  2651.  
  2652.     if(env->getScriptId() != EVENT_ID_LOADING){
  2653.         reportError(__FUNCTION__, "This function can only be used while loading the script.");
  2654.         lua_pushnumber(L, LUA_ERROR);
  2655.         return 1;
  2656.     }
  2657.  
  2658.     int32_t parameters = lua_gettop(L);
  2659.  
  2660.     AreaCombat* area = new AreaCombat;
  2661.  
  2662.     if(parameters > 1){
  2663.         //has extra parameter with diagonal area information
  2664.  
  2665.         uint32_t rowsExtArea;
  2666.         std::list<uint32_t> listExtArea;
  2667.         getArea(L, listExtArea, rowsExtArea);
  2668.        
  2669.         /*setup all possible rotations*/
  2670.         area->setupExtArea(listExtArea, rowsExtArea);
  2671.     }
  2672.  
  2673.     uint32_t rowsArea = 0;
  2674.     std::list<uint32_t> listArea;
  2675.     getArea(L, listArea, rowsArea);
  2676.  
  2677.     /*setup all possible rotations*/
  2678.     area->setupArea(listArea, rowsArea);
  2679.  
  2680.     uint32_t newAreaId = env->addCombatArea(area);
  2681.  
  2682.     lua_pushnumber(L, newAreaId);
  2683.     return 1;
  2684. }
  2685.  
  2686. int LuaScriptInterface::luaCreateConditionObject(lua_State *L)
  2687. {
  2688.     //createConditionObject(type)
  2689.  
  2690.     ScriptEnviroment* env = getScriptEnv();
  2691.  
  2692.     if(env->getScriptId() != EVENT_ID_LOADING){
  2693.         reportError(__FUNCTION__, "This function can only be used while loading the script.");
  2694.         lua_pushnumber(L, LUA_ERROR);
  2695.         return 1;
  2696.     }
  2697.  
  2698.     ConditionType_t type = (ConditionType_t)popNumber(L);
  2699.  
  2700.     Condition* condition = Condition::createCondition(CONDITIONID_COMBAT, type, 0, 0);
  2701.     if(condition){
  2702.         uint32_t newConditionId = env->addConditionObject(condition);
  2703.         lua_pushnumber(L, newConditionId);
  2704.     }
  2705.     else{
  2706.         reportErrorFunc(getErrorDesc(LUA_ERROR_CONDITION_NOT_FOUND));
  2707.         lua_pushnumber(L, LUA_ERROR);
  2708.     }
  2709.  
  2710.     return 1;
  2711. }
  2712.  
  2713. int LuaScriptInterface::luaSetCombatArea(lua_State *L)
  2714. {
  2715.     //setCombatArea(combat, area)
  2716.  
  2717.     uint32_t areaId = popNumber(L);
  2718.     uint32_t combatId = popNumber(L);
  2719.  
  2720.     ScriptEnviroment* env = getScriptEnv();
  2721.  
  2722.     if(env->getScriptId() != EVENT_ID_LOADING){
  2723.         reportError(__FUNCTION__, "This function can only be used while loading the script.");
  2724.         lua_pushnumber(L, LUA_ERROR);
  2725.         return 1;
  2726.     }
  2727.  
  2728.     Combat* combat = env->getCombatObject(combatId);
  2729.  
  2730.     if(!combat){
  2731.         reportErrorFunc(getErrorDesc(LUA_ERROR_COMBAT_NOT_FOUND));
  2732.         lua_pushnumber(L, LUA_ERROR);
  2733.         return 1;
  2734.     }
  2735.  
  2736.     const AreaCombat* area = env->getCombatArea(areaId);
  2737.     if(!area){
  2738.         reportErrorFunc(getErrorDesc(LUA_ERROR_AREA_NOT_FOUND));
  2739.         lua_pushnumber(L, LUA_ERROR);
  2740.         return 1;
  2741.     }
  2742.  
  2743.     combat->setArea(area);
  2744.  
  2745.     lua_pushnumber(L, LUA_NO_ERROR);
  2746.     return 1;
  2747. }
  2748.  
  2749. int LuaScriptInterface::luaSetCombatCondition(lua_State *L)
  2750. {
  2751.     //setCombatCondition(combat, condition)
  2752.  
  2753.     uint32_t conditionId = popNumber(L);
  2754.     uint32_t combatId = popNumber(L);
  2755.  
  2756.     ScriptEnviroment* env = getScriptEnv();
  2757.  
  2758.     if(env->getScriptId() != EVENT_ID_LOADING){
  2759.         reportError(__FUNCTION__, "This function can only be used while loading the script.");
  2760.         lua_pushnumber(L, LUA_ERROR);
  2761.         return 1;
  2762.     }
  2763.  
  2764.     Combat* combat = env->getCombatObject(combatId);
  2765.  
  2766.     if(!combat){
  2767.         reportErrorFunc(getErrorDesc(LUA_ERROR_COMBAT_NOT_FOUND));
  2768.         lua_pushnumber(L, LUA_ERROR);
  2769.         return 1;
  2770.     }
  2771.  
  2772.     const Condition* condition = env->getConditionObject(conditionId);
  2773.     if(!condition){
  2774.         reportErrorFunc(getErrorDesc(LUA_ERROR_CONDITION_NOT_FOUND));
  2775.         lua_pushnumber(L, LUA_ERROR);
  2776.         return 1;
  2777.     }
  2778.  
  2779.     combat->setCondition(condition);
  2780.  
  2781.     lua_pushnumber(L, LUA_NO_ERROR);
  2782.     return 1;
  2783. }
  2784.  
  2785. int LuaScriptInterface::luaSetCombatParam(lua_State *L)
  2786. {
  2787.     //setCombatParam(combat, key, value)
  2788.    
  2789.     uint32_t value = popNumber(L);
  2790.     CombatParam_t key = (CombatParam_t)popNumber(L);
  2791.     uint32_t combatId = popNumber(L);
  2792.  
  2793.     ScriptEnviroment* env = getScriptEnv();
  2794.  
  2795.     if(env->getScriptId() != EVENT_ID_LOADING){
  2796.         reportError(__FUNCTION__, "This function can only be used while loading the script.");
  2797.         lua_pushnumber(L, LUA_ERROR);
  2798.         return 1;
  2799.     }
  2800.  
  2801.     Combat* combat = env->getCombatObject(combatId);
  2802.  
  2803.     if(combat){
  2804.         combat->setParam(key, value);
  2805.         lua_pushnumber(L, LUA_NO_ERROR);
  2806.     }
  2807.     else{
  2808.         reportErrorFunc(getErrorDesc(LUA_ERROR_COMBAT_NOT_FOUND));
  2809.         lua_pushnumber(L, LUA_ERROR);
  2810.     }
  2811.     return 1;
  2812. }
  2813.  
  2814. int LuaScriptInterface::luaSetConditionParam(lua_State *L)
  2815. {
  2816.     //setConditionParam(condition, key, value)
  2817.  
  2818.     int32_t value = (int32_t)popNumber(L);
  2819.     ConditionParam_t key = (ConditionParam_t)popNumber(L);
  2820.     uint32_t conditionId = popNumber(L);
  2821.  
  2822.     ScriptEnviroment* env = getScriptEnv();
  2823.  
  2824.     if(env->getScriptId() != EVENT_ID_LOADING){
  2825.         reportError(__FUNCTION__, "This function can only be used while loading the script.");
  2826.         lua_pushnumber(L, LUA_ERROR);
  2827.         return 1;
  2828.     }
  2829.  
  2830.     Condition* condition = env->getConditionObject(conditionId);
  2831.  
  2832.     if(condition){
  2833.         condition->setParam(key, value);
  2834.         lua_pushnumber(L, LUA_NO_ERROR);        
  2835.     }
  2836.     else{
  2837.         reportErrorFunc(getErrorDesc(LUA_ERROR_CONDITION_NOT_FOUND));
  2838.         lua_pushnumber(L, LUA_ERROR);
  2839.     }
  2840.     return 1;
  2841. }
  2842.  
  2843. int LuaScriptInterface::luaAddDamageCondition(lua_State *L)
  2844. {
  2845.     //addDamageCondition(condition, rounds, time, value)
  2846.  
  2847.     int32_t value = (int32_t)popNumber(L);
  2848.     int32_t time = (int32_t)popNumber(L);
  2849.     int32_t rounds  = (int32_t)popNumber(L);
  2850.     uint32_t conditionId = popNumber(L);
  2851.  
  2852.     ScriptEnviroment* env = getScriptEnv();
  2853.  
  2854.     if(env->getScriptId() != EVENT_ID_LOADING){
  2855.         reportError(__FUNCTION__, "This function can only be used while loading the script.");
  2856.         lua_pushnumber(L, LUA_ERROR);
  2857.         return 1;
  2858.     }
  2859.  
  2860.     ConditionDamage* condition = dynamic_cast<ConditionDamage*>(env->getConditionObject(conditionId));
  2861.  
  2862.     if(condition){
  2863.         condition->addDamage(rounds, time, value);
  2864.         lua_pushnumber(L, LUA_NO_ERROR);
  2865.     }
  2866.     else{
  2867.         reportErrorFunc(getErrorDesc(LUA_ERROR_CONDITION_NOT_FOUND));
  2868.         lua_pushnumber(L, LUA_ERROR);
  2869.     }
  2870.     return 1;
  2871. }
  2872.  
  2873. int LuaScriptInterface::luaAddOutfitCondition(lua_State *L)
  2874. {
  2875.     //addOutfitCondition(condition, lookTypeEx, lookType, lookHead, lookBody, lookLegs, lookFeet)
  2876.  
  2877.     Outfit_t outfit;
  2878.     outfit.lookFeet = popNumber(L);
  2879.     outfit.lookLegs = popNumber(L);
  2880.     outfit.lookBody = popNumber(L);
  2881.     outfit.lookHead = popNumber(L);
  2882.     outfit.lookType = popNumber(L);
  2883.     outfit.lookTypeEx = popNumber(L);
  2884.     uint32_t conditionId = popNumber(L);
  2885.  
  2886.     ScriptEnviroment* env = getScriptEnv();
  2887.  
  2888.     if(env->getScriptId() != EVENT_ID_LOADING){
  2889.         reportError(__FUNCTION__, "This function can only be used while loading the script.");
  2890.         lua_pushnumber(L, LUA_ERROR);
  2891.         return 1;
  2892.     }
  2893.  
  2894.     ConditionOutfit* condition = dynamic_cast<ConditionOutfit*>(env->getConditionObject(conditionId));
  2895.  
  2896.     if(condition){
  2897.         condition->addOutfit(outfit);
  2898.         lua_pushnumber(L, LUA_NO_ERROR);
  2899.     }
  2900.     else{
  2901.         reportErrorFunc(getErrorDesc(LUA_ERROR_CONDITION_NOT_FOUND));
  2902.         lua_pushnumber(L, LUA_ERROR);
  2903.     }
  2904.     return 1;
  2905. }
  2906.  
  2907. int LuaScriptInterface::luaSetCombatCallBack(lua_State *L)
  2908. {
  2909.     //setCombatCallBack(combat, key, function_name)
  2910.  
  2911.     const char* function = popString(L);
  2912.     std::string function_str(function);
  2913.     CallBackParam_t key = (CallBackParam_t)popNumber(L);
  2914.     uint32_t combatId = popNumber(L);
  2915.  
  2916.     ScriptEnviroment* env = getScriptEnv();
  2917.    
  2918.     if(env->getScriptId() != EVENT_ID_LOADING){
  2919.         reportError(__FUNCTION__, "This function can only be used while loading the script.");
  2920.         lua_pushnumber(L, LUA_ERROR);
  2921.         return 1;
  2922.     }
  2923.    
  2924.     Combat* combat = env->getCombatObject(combatId);
  2925.  
  2926.     if(!combat){
  2927.         reportErrorFunc(getErrorDesc(LUA_ERROR_COMBAT_NOT_FOUND));
  2928.         lua_pushnumber(L, LUA_ERROR);
  2929.         return 1;
  2930.     }
  2931.    
  2932.     LuaScriptInterface* scriptInterface = env->getScriptInterface();
  2933.    
  2934.     combat->setCallback(key);
  2935.     CallBack* callback = combat->getCallback(key);
  2936.     if(!callback){
  2937.         std::stringstream ss;
  2938.         ss << (uint32_t)key << " is not a valid callback key";
  2939.         reportError(__FUNCTION__, ss.str());
  2940.         lua_pushnumber(L, LUA_ERROR);
  2941.         return 1;
  2942.     }
  2943.    
  2944.     if(!callback->loadCallBack(scriptInterface, function_str)){
  2945.         reportError(__FUNCTION__, "Can not load callback");
  2946.         lua_pushnumber(L, LUA_ERROR);
  2947.         return 1;
  2948.     }
  2949.  
  2950.     lua_pushnumber(L, LUA_NO_ERROR);
  2951.     return 1;
  2952. }
  2953.  
  2954. int LuaScriptInterface::luaSetCombatFormula(lua_State *L)
  2955. {
  2956.     //setCombatFormula(combat, type, mina, minb, maxa, maxb)
  2957.  
  2958.     ScriptEnviroment* env = getScriptEnv();
  2959.  
  2960.     if(env->getScriptId() != EVENT_ID_LOADING){
  2961.         reportError(__FUNCTION__, "This function can only be used while loading the script.");
  2962.         lua_pushnumber(L, LUA_ERROR);
  2963.         return 1;
  2964.     }
  2965.  
  2966.     double maxb = popFloatNumber(L);
  2967.     double maxa = popFloatNumber(L);
  2968.     double minb = popFloatNumber(L);
  2969.     double mina = popFloatNumber(L);
  2970.  
  2971.     formulaType_t type = (formulaType_t)popNumber(L);
  2972.     uint32_t combatId = popNumber(L);
  2973.  
  2974.     Combat* combat = env->getCombatObject(combatId);
  2975.  
  2976.     if(combat){
  2977.         combat->setPlayerCombatValues(type, mina, minb, maxa, maxb);
  2978.        
  2979.         lua_pushnumber(L, LUA_NO_ERROR);
  2980.     }
  2981.     else{
  2982.         reportErrorFunc(getErrorDesc(LUA_ERROR_COMBAT_NOT_FOUND));
  2983.         lua_pushnumber(L, LUA_ERROR);
  2984.     }
  2985.     return 1;
  2986. }
  2987.  
  2988. int LuaScriptInterface::luaSetConditionFormula(lua_State *L)
  2989. {
  2990.     //setConditionFormula(condition, mina, minb, maxa, maxb)
  2991.  
  2992.     ScriptEnviroment* env = getScriptEnv();
  2993.  
  2994.     if(env->getScriptId() != EVENT_ID_LOADING){
  2995.         reportError(__FUNCTION__, "This function can only be used while loading the script.");
  2996.         lua_pushnumber(L, LUA_ERROR);
  2997.         return 1;
  2998.     }
  2999.  
  3000.     double maxb = popFloatNumber(L);
  3001.     double maxa = popFloatNumber(L);
  3002.     double minb = popFloatNumber(L);
  3003.     double mina = popFloatNumber(L);
  3004.  
  3005.     uint32_t conditionId = popNumber(L);
  3006.  
  3007.     ConditionSpeed* condition = dynamic_cast<ConditionSpeed*>(env->getConditionObject(conditionId));
  3008.    
  3009.     if(condition){
  3010.         condition->setFormulaVars(mina, minb, maxa, maxb);
  3011.  
  3012.         lua_pushnumber(L, LUA_NO_ERROR);    
  3013.     }
  3014.     else{
  3015.         reportErrorFunc(getErrorDesc(LUA_ERROR_CONDITION_NOT_FOUND));
  3016.         lua_pushnumber(L, LUA_ERROR);
  3017.     }
  3018.     return 1;
  3019. }
  3020.  
  3021. int LuaScriptInterface::luaDoCombat(lua_State *L)
  3022. {
  3023.     //doCombat(cid, combat, param)
  3024.  
  3025.     ScriptEnviroment* env = getScriptEnv();
  3026.    
  3027.     LuaVariant var = popVariant(L);
  3028.     uint32_t combatId = (uint32_t)popNumber(L);
  3029.     uint32_t cid = (uint32_t)popNumber(L);
  3030.    
  3031.     Creature* creature = NULL;
  3032.    
  3033.     if(cid != 0){
  3034.         creature = env->getCreatureByUID(cid);
  3035.  
  3036.         if(!creature){
  3037.             reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3038.             lua_pushnumber(L, LUA_ERROR);
  3039.             return 1;
  3040.         }
  3041.     }
  3042.  
  3043.     const Combat* combat = env->getCombatObject(combatId);
  3044.  
  3045.     if(!combat){
  3046.         reportErrorFunc(getErrorDesc(LUA_ERROR_COMBAT_NOT_FOUND));
  3047.         lua_pushnumber(L, LUA_ERROR);
  3048.         return 1;
  3049.     }
  3050.  
  3051.     if(var.type == VARIANT_NONE){
  3052.         reportErrorFunc(getErrorDesc(LUA_ERROR_VARIANT_NOT_FOUND));
  3053.         lua_pushnumber(L, LUA_ERROR);
  3054.         return 1;
  3055.     }
  3056.  
  3057.     switch(var.type){
  3058.         case VARIANT_NUMBER:
  3059.         {
  3060.             Creature* target = g_game.getCreatureByID(var.number);
  3061.  
  3062.             if(!target){
  3063.                 lua_pushnumber(L, LUA_ERROR);
  3064.                 return 1;
  3065.             }
  3066.  
  3067.             if(combat->hasArea()){
  3068.                 combat->doCombat(creature, target->getPosition());
  3069.                 //std::cout << "Combat->hasArea()" << std::endl;
  3070.             }
  3071.             else{
  3072.                 combat->doCombat(creature, target);
  3073.             }
  3074.             break;
  3075.         }
  3076.  
  3077.         case VARIANT_POSITION:
  3078.         {
  3079.             combat->doCombat(creature, var.pos);
  3080.             break;
  3081.         }
  3082.  
  3083.         case VARIANT_TARGETPOSITION:
  3084.         {
  3085.             if(combat->hasArea()){
  3086.                 combat->doCombat(creature, var.pos);
  3087.             }
  3088.             else{
  3089.                 combat->postCombatEffects(creature, var.pos);
  3090.                 g_game.addMagicEffect(var.pos, NM_ME_PUFF);
  3091.             }
  3092.             break;
  3093.         }
  3094.  
  3095.         case VARIANT_STRING:
  3096.         {
  3097.             Player* target = g_game.getPlayerByName(var.text);
  3098.             if(!target){
  3099.                 lua_pushnumber(L, LUA_ERROR);
  3100.                 return 1;
  3101.             }
  3102.  
  3103.             combat->doCombat(creature, target);
  3104.             break;
  3105.         }
  3106.  
  3107.         default:
  3108.         {
  3109.             reportErrorFunc(getErrorDesc(LUA_ERROR_VARIANT_UNKNOWN));
  3110.             lua_pushnumber(L, LUA_ERROR);
  3111.             return 1;
  3112.             break;
  3113.         }
  3114.     }
  3115.  
  3116.     lua_pushnumber(L, LUA_NO_ERROR);
  3117.     return 1;
  3118. }
  3119.  
  3120. int LuaScriptInterface::luaDoAreaCombatHealth(lua_State *L)
  3121. {
  3122.     //doAreaCombatHealth(cid, type, pos, area, min, max, effect)
  3123.  
  3124.     uint8_t effect = (uint8_t)popNumber(L);
  3125.     int32_t maxChange = (int32_t)popNumber(L);
  3126.     int32_t minChange = (int32_t)popNumber(L);
  3127.     uint32_t areaId = popNumber(L);
  3128.    
  3129.     Position pos;
  3130.     uint32_t stackpos;
  3131.     popPosition(L, pos, stackpos);
  3132.  
  3133.     CombatType_t combatType = (CombatType_t)popNumber(L);
  3134.     uint32_t cid = (uint32_t)popNumber(L);
  3135.  
  3136.     ScriptEnviroment* env = getScriptEnv();
  3137.  
  3138.     Creature* creature = NULL;
  3139.    
  3140.     if(cid != 0){
  3141.         creature = env->getCreatureByUID(cid);
  3142.  
  3143.         if(!creature){
  3144.             reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3145.             lua_pushnumber(L, LUA_ERROR);
  3146.             return 1;
  3147.         }
  3148.     }
  3149.  
  3150.     const AreaCombat* area = env->getCombatArea(areaId);
  3151.     if(area || areaId == 0){
  3152.         CombatParams params;
  3153.         params.combatType = combatType;
  3154.         params.impactEffect = effect;
  3155.         Combat::doCombatHealth(creature, pos, area, minChange, maxChange, params);
  3156.  
  3157.         lua_pushnumber(L, LUA_NO_ERROR);
  3158.     }
  3159.     else{
  3160.         reportErrorFunc(getErrorDesc(LUA_ERROR_AREA_NOT_FOUND));
  3161.         lua_pushnumber(L, LUA_ERROR);
  3162.     }
  3163.  
  3164.     return 1;
  3165. }
  3166.  
  3167. int LuaScriptInterface::luaDoTargetCombatHealth(lua_State *L)
  3168. {
  3169.     //doTargetCombatHealth(cid, target, type, min, max, effect)
  3170.  
  3171.     uint8_t effect = (uint8_t)popNumber(L);
  3172.     int32_t maxChange = (int32_t)popNumber(L);
  3173.     int32_t minChange = (int32_t)popNumber(L);
  3174.     CombatType_t combatType = (CombatType_t)popNumber(L);
  3175.     uint32_t targetCid = popNumber(L);
  3176.     uint32_t cid = popNumber(L);
  3177.  
  3178.     ScriptEnviroment* env = getScriptEnv();
  3179.  
  3180.     Creature* creature = NULL;
  3181.    
  3182.     if(cid != 0){
  3183.         creature = env->getCreatureByUID(cid);
  3184.  
  3185.         if(!creature){
  3186.             reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3187.             lua_pushnumber(L, LUA_ERROR);
  3188.             return 1;
  3189.         }
  3190.     }
  3191.  
  3192.     Creature* target = env->getCreatureByUID(targetCid);
  3193.     if(target){
  3194.         CombatParams params;
  3195.         params.combatType = combatType;
  3196.         params.impactEffect = effect;
  3197.         Combat::doCombatHealth(creature, target, minChange, maxChange, params);
  3198.  
  3199.         lua_pushnumber(L, LUA_NO_ERROR);
  3200.     }
  3201.     else{
  3202.         reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3203.         lua_pushnumber(L, LUA_ERROR);
  3204.     }
  3205.     return 1;
  3206. }
  3207.  
  3208. int LuaScriptInterface::luaDoAreaCombatMana(lua_State *L)
  3209. {
  3210.     //doAreaCombatMana(cid, pos, area, min, max, effect)
  3211.  
  3212.     uint8_t effect = (uint8_t)popNumber(L);
  3213.     int32_t maxChange = (int32_t)popNumber(L);
  3214.     int32_t minChange = (int32_t)popNumber(L);
  3215.     uint32_t areaId = popNumber(L);
  3216.  
  3217.     Position pos;
  3218.     uint32_t stackpos;
  3219.     popPosition(L, pos, stackpos);
  3220.  
  3221.     uint32_t cid = (uint32_t)popNumber(L);
  3222.  
  3223.     ScriptEnviroment* env = getScriptEnv();
  3224.  
  3225.     Creature* creature = NULL;
  3226.    
  3227.     if(cid != 0){
  3228.         creature = env->getCreatureByUID(cid);
  3229.  
  3230.         if(!creature){
  3231.             reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3232.             lua_pushnumber(L, LUA_ERROR);
  3233.             return 1;
  3234.         }
  3235.     }
  3236.  
  3237.     const AreaCombat* area = env->getCombatArea(areaId);
  3238.     if(area || areaId == 0){
  3239.         CombatParams params;
  3240.         params.impactEffect = effect;
  3241.         Combat::doCombatMana(creature, pos, area, minChange, maxChange, params);
  3242.        
  3243.         lua_pushnumber(L, LUA_NO_ERROR);
  3244.     }
  3245.     else{
  3246.         reportErrorFunc(getErrorDesc(LUA_ERROR_AREA_NOT_FOUND));
  3247.         lua_pushnumber(L, LUA_ERROR);
  3248.     }
  3249.     return 1;
  3250. }
  3251.  
  3252. int LuaScriptInterface::luaDoTargetCombatMana(lua_State *L)
  3253. {
  3254.     //doTargetCombatMana(cid, target, min, max, effect)
  3255.  
  3256.     uint8_t effect = (uint8_t)popNumber(L);
  3257.     int32_t maxChange = (int32_t)popNumber(L);
  3258.     int32_t minChange = (int32_t)popNumber(L);
  3259.     uint32_t targetCid = popNumber(L);
  3260.     uint32_t cid = popNumber(L);
  3261.  
  3262.     ScriptEnviroment* env = getScriptEnv();
  3263.  
  3264.     Creature* creature = NULL;
  3265.    
  3266.     if(cid != 0){
  3267.         creature = env->getCreatureByUID(cid);
  3268.  
  3269.         if(!creature){
  3270.             reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3271.             lua_pushnumber(L, LUA_ERROR);
  3272.             return 1;
  3273.         }
  3274.     }
  3275.  
  3276.     Creature* target = env->getCreatureByUID(targetCid);
  3277.     if(target){
  3278.         CombatParams params;
  3279.         params.impactEffect = effect;
  3280.         Combat::doCombatMana(creature, target, minChange, maxChange, params);
  3281.        
  3282.         lua_pushnumber(L, LUA_NO_ERROR);
  3283.     }
  3284.     else{
  3285.         reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3286.         lua_pushnumber(L, LUA_ERROR);
  3287.     }
  3288.     return 1;
  3289. }
  3290.  
  3291. int LuaScriptInterface::luaDoAreaCombatCondition(lua_State *L)
  3292. {
  3293.     //doAreaCombatCondition(cid, pos, area, condition, effect)
  3294.  
  3295.     uint8_t effect = (uint8_t)popNumber(L);
  3296.     uint32_t conditionId = popNumber(L);
  3297.     uint32_t areaId = popNumber(L);
  3298.     Position pos;
  3299.     uint32_t stackpos;
  3300.     popPosition(L, pos, stackpos);
  3301.     uint32_t cid = (uint32_t)popNumber(L);
  3302.  
  3303.     ScriptEnviroment* env = getScriptEnv();
  3304.  
  3305.     Creature* creature = NULL;
  3306.    
  3307.     if(cid != 0){
  3308.         creature = env->getCreatureByUID(cid);
  3309.         if(!creature){
  3310.             reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3311.             lua_pushnumber(L, LUA_ERROR);
  3312.             return 1;
  3313.         }
  3314.     }
  3315.  
  3316.     const Condition* condition = env->getConditionObject(conditionId);
  3317.     if(condition){
  3318.        
  3319.         const AreaCombat* area = env->getCombatArea(areaId);
  3320.        
  3321.         if(area || areaId == 0){
  3322.             CombatParams params;
  3323.             params.impactEffect = effect;
  3324.             params.condition = condition;
  3325.             Combat::doCombatCondition(creature, pos, area, params);
  3326.            
  3327.             lua_pushnumber(L, LUA_NO_ERROR);
  3328.         }
  3329.         else{
  3330.             reportErrorFunc(getErrorDesc(LUA_ERROR_AREA_NOT_FOUND));
  3331.             lua_pushnumber(L, LUA_ERROR);
  3332.         }
  3333.     }
  3334.     else{
  3335.         reportErrorFunc(getErrorDesc(LUA_ERROR_CONDITION_NOT_FOUND));
  3336.         lua_pushnumber(L, LUA_ERROR);
  3337.     }
  3338.     return 1;
  3339. }
  3340.  
  3341. int LuaScriptInterface::luaDoTargetCombatCondition(lua_State *L)
  3342. {
  3343.     //doTargetCombatCondition(cid, target, condition, effect)
  3344.  
  3345.     uint8_t effect = (uint8_t)popNumber(L);
  3346.     uint32_t conditionId = popNumber(L);
  3347.     uint32_t targetCid = popNumber(L);
  3348.     uint32_t cid = popNumber(L);
  3349.  
  3350.     ScriptEnviroment* env = getScriptEnv();
  3351.  
  3352.     Creature* creature = NULL;
  3353.  
  3354.     if(cid != 0){
  3355.         creature = env->getCreatureByUID(cid);
  3356.  
  3357.         if(!creature){
  3358.             reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3359.             lua_pushnumber(L, LUA_ERROR);
  3360.             return 1;
  3361.         }
  3362.     }
  3363.  
  3364.     Creature* target = env->getCreatureByUID(targetCid);
  3365.     if(target){
  3366.         const Condition* condition = env->getConditionObject(conditionId);
  3367.         if(condition){
  3368.             CombatParams params;
  3369.             params.impactEffect = effect;
  3370.             params.condition = condition;
  3371.             Combat::doCombatCondition(creature, target, params);
  3372.            
  3373.             lua_pushnumber(L, LUA_NO_ERROR);
  3374.         }
  3375.         else{
  3376.             reportErrorFunc(getErrorDesc(LUA_ERROR_CONDITION_NOT_FOUND));
  3377.             lua_pushnumber(L, LUA_ERROR);
  3378.         }
  3379.     }
  3380.     else{
  3381.         reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3382.         lua_pushnumber(L, LUA_ERROR);
  3383.     }
  3384.     return 1;
  3385. }
  3386.  
  3387. int LuaScriptInterface::luaDoAreaCombatDispel(lua_State *L)
  3388. {
  3389.     //doAreaCombatDispel(cid, pos, area, type, effect)
  3390.  
  3391.     uint8_t effect = (uint8_t)popNumber(L);
  3392.     ConditionType_t dispelType = (ConditionType_t)popNumber(L);
  3393.     uint32_t areaId = popNumber(L);
  3394.     Position pos;
  3395.     uint32_t stackpos;
  3396.     popPosition(L, pos, stackpos);
  3397.     uint32_t cid = (uint32_t)popNumber(L);
  3398.  
  3399.     ScriptEnviroment* env = getScriptEnv();
  3400.  
  3401.     Creature* creature = NULL;
  3402.    
  3403.     if(cid != 0){
  3404.         creature = env->getCreatureByUID(cid);
  3405.  
  3406.         if(!creature){
  3407.             reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3408.             lua_pushnumber(L, LUA_ERROR);
  3409.             return 1;
  3410.         }
  3411.     }
  3412.  
  3413.     const AreaCombat* area = env->getCombatArea(areaId);
  3414.     if(area || areaId == 0){
  3415.         CombatParams params;
  3416.         params.impactEffect = effect;
  3417.         params.dispelType = dispelType;
  3418.         Combat::doCombatDispel(creature, pos, area, params);
  3419.  
  3420.         lua_pushnumber(L, LUA_NO_ERROR);
  3421.     }
  3422.     else{
  3423.         reportErrorFunc(getErrorDesc(LUA_ERROR_AREA_NOT_FOUND));
  3424.         lua_pushnumber(L, LUA_ERROR);
  3425.     }
  3426.     return 1;
  3427. }
  3428.  
  3429. int LuaScriptInterface::luaDoTargetCombatDispel(lua_State *L)
  3430. {
  3431.     //doTargetCombatDispel(cid, target, type, effect)
  3432.  
  3433.     uint8_t effect = (uint8_t)popNumber(L);
  3434.     ConditionType_t dispelType = (ConditionType_t)popNumber(L);
  3435.     uint32_t targetCid = popNumber(L);
  3436.     uint32_t cid = popNumber(L);
  3437.  
  3438.     ScriptEnviroment* env = getScriptEnv();
  3439.  
  3440.     Creature* creature = NULL;
  3441.  
  3442.     if(cid != 0){
  3443.         creature = env->getCreatureByUID(cid);
  3444.  
  3445.         if(!creature){
  3446.             reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3447.             lua_pushnumber(L, LUA_ERROR);
  3448.             return 1;
  3449.         }
  3450.     }
  3451.  
  3452.     Creature* target = env->getCreatureByUID(targetCid);
  3453.     if(target){
  3454.         CombatParams params;
  3455.         params.impactEffect = effect;
  3456.         params.dispelType = dispelType;
  3457.         Combat::doCombatDispel(creature, target, params);
  3458.  
  3459.         lua_pushnumber(L, LUA_NO_ERROR);
  3460.     }
  3461.     else{
  3462.         reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3463.         lua_pushnumber(L, LUA_ERROR);
  3464.     }
  3465.  
  3466.     return 1;
  3467. }
  3468.  
  3469. int LuaScriptInterface::luaDoChallengeCreature(lua_State *L)
  3470. {
  3471.     //doChallengeCreature(cid, target)
  3472.  
  3473.     uint32_t targetCid = popNumber(L);
  3474.     uint32_t cid = popNumber(L);
  3475.  
  3476.     ScriptEnviroment* env = getScriptEnv();
  3477.  
  3478.     Creature* creature = env->getCreatureByUID(cid);
  3479.  
  3480.     if(!creature){
  3481.         reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3482.         lua_pushnumber(L, LUA_ERROR);
  3483.         return 1;
  3484.     }
  3485.  
  3486.     Creature* target = env->getCreatureByUID(targetCid);
  3487.     if(target){
  3488.         target->challengeCreature(creature);
  3489.         lua_pushnumber(L, LUA_NO_ERROR);
  3490.     }
  3491.     else{
  3492.         reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3493.         lua_pushnumber(L, LUA_ERROR);
  3494.     }
  3495.  
  3496.     return 1;
  3497. }
  3498.  
  3499. int LuaScriptInterface::luaDoConvinceCreature(lua_State *L)
  3500. {
  3501.     //doConvinceCreature(cid, target)
  3502.  
  3503.     uint32_t targetCid = popNumber(L);
  3504.     uint32_t cid = popNumber(L);
  3505.  
  3506.     ScriptEnviroment* env = getScriptEnv();
  3507.  
  3508.     Creature* creature = env->getCreatureByUID(cid);
  3509.  
  3510.     if(!creature){
  3511.         reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3512.         lua_pushnumber(L, LUA_ERROR);
  3513.         return 1;
  3514.     }
  3515.  
  3516.     Creature* target = env->getCreatureByUID(targetCid);
  3517.     if(target){
  3518.         target->convinceCreature(creature);
  3519.         lua_pushnumber(L, LUA_NO_ERROR);
  3520.     }
  3521.     else{
  3522.         reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3523.         lua_pushnumber(L, LUA_ERROR);
  3524.     }
  3525.  
  3526.     return 1;
  3527. }
  3528.  
  3529. int LuaScriptInterface::luaNumberToVariant(lua_State *L)
  3530. {
  3531.     //numberToVariant(number)
  3532.  
  3533.     LuaVariant var;
  3534.     var.type = VARIANT_NUMBER;
  3535.     var.number = popNumber(L);
  3536.  
  3537.     LuaScriptInterface::pushVariant(L, var);
  3538.     return 1;
  3539. }
  3540.  
  3541. int LuaScriptInterface::luaStringToVariant(lua_State *L)
  3542. {
  3543.     //stringToVariant(string)
  3544.     LuaVariant var;
  3545.     var.type = VARIANT_STRING;
  3546.     var.text = popString(L);
  3547.  
  3548.     LuaScriptInterface::pushVariant(L, var);
  3549.     return 1;
  3550. }
  3551.  
  3552. int LuaScriptInterface::luaPositionToVariant(lua_State *L)
  3553. {
  3554.     //positionToVariant(pos)
  3555.  
  3556.     LuaVariant var;
  3557.     var.type = VARIANT_POSITION;
  3558.     popPosition(L, var.pos);
  3559.  
  3560.     LuaScriptInterface::pushVariant(L, var);
  3561.     return 1;
  3562. }
  3563.  
  3564. int LuaScriptInterface::luaTargetPositionToVariant(lua_State *L)
  3565. {
  3566.     //targetPositionToVariant(pos)
  3567.  
  3568.     LuaVariant var;
  3569.     var.type = VARIANT_TARGETPOSITION;
  3570.     popPosition(L, var.pos);
  3571.  
  3572.     LuaScriptInterface::pushVariant(L, var);
  3573.     return 1;
  3574. }
  3575.  
  3576. int LuaScriptInterface::luaVariantToNumber(lua_State *L)
  3577. {
  3578.     //variantToNumber(var)
  3579.  
  3580.     LuaVariant var = popVariant(L);
  3581.  
  3582.     uint32_t number = 0;
  3583.     if(var.type == VARIANT_NUMBER){
  3584.         number = var.number;
  3585.     }
  3586.  
  3587.     lua_pushnumber(L, number);
  3588.     return 1;
  3589. }
  3590.  
  3591. int LuaScriptInterface::luaVariantToString(lua_State *L)
  3592. {
  3593.     //variantToString(var)
  3594.  
  3595.     LuaVariant var = popVariant(L);
  3596.  
  3597.     std::string text = "";
  3598.     if(var.type == VARIANT_STRING){
  3599.         text = var.text;
  3600.     }
  3601.  
  3602.     lua_pushstring(L, text.c_str());
  3603.     return 1;
  3604. }
  3605.  
  3606. int LuaScriptInterface::luaVariantToPosition(lua_State *L)
  3607. {
  3608.     //luaVariantToPosition(var)
  3609.  
  3610.     LuaVariant var = popVariant(L);
  3611.  
  3612.     PositionEx pos(0, 0, 0, 0);
  3613.     if(var.type == VARIANT_POSITION || var.type == VARIANT_TARGETPOSITION){
  3614.         pos = var.pos;
  3615.     }
  3616.  
  3617.     pushPosition(L, pos, pos.stackpos);
  3618.     return 1;
  3619. }
  3620.  
  3621. int LuaScriptInterface::luaDoChangeSpeed(lua_State *L)
  3622. {
  3623.     //doChangeSpeed(cid, delta)
  3624.  
  3625.     int32_t delta = (int32_t)popNumber(L);
  3626.     uint32_t cid = popNumber(L);
  3627.  
  3628.     ScriptEnviroment* env = getScriptEnv();
  3629.  
  3630.     Creature* creature = env->getCreatureByUID(cid);
  3631.  
  3632.     if(creature){
  3633.         g_game.changeSpeed(creature, delta);
  3634.         lua_pushnumber(L, LUA_NO_ERROR);
  3635.     }
  3636.     else{
  3637.         reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3638.         lua_pushnumber(L, LUA_ERROR);
  3639.     }
  3640.     return 1;
  3641. }
  3642.  
  3643. int LuaScriptInterface::luaSetCreatureOutfit(lua_State *L)
  3644. {
  3645.     //doSetCreatureOutfit(cid, outfit, time)
  3646.  
  3647.     int32_t time = (int32_t)popNumber(L);
  3648.     Outfit_t outfit;
  3649.     outfit.lookType = getField(L, "lookType");
  3650.     outfit.lookHead = getField(L, "lookHead");
  3651.     outfit.lookBody = getField(L, "lookBody");
  3652.     outfit.lookLegs = getField(L, "lookLegs");
  3653.     outfit.lookFeet = getField(L, "lookFeet");
  3654.     outfit.lookAddons = getField(L, "lookAddons");
  3655.     lua_pop(L, 1);
  3656.  
  3657.     uint32_t cid = popNumber(L);
  3658.  
  3659.     ScriptEnviroment* env = getScriptEnv();
  3660.  
  3661.     Creature* creature = env->getCreatureByUID(cid);
  3662.  
  3663.     if(creature){
  3664.         ReturnValue ret = Spell::CreateIllusion(creature, outfit, time);
  3665.         if(ret == RET_NOERROR){
  3666.             lua_pushnumber(L, LUA_NO_ERROR);
  3667.         }
  3668.         else{
  3669.             lua_pushnumber(L, LUA_ERROR);
  3670.         }
  3671.     }
  3672.     else{
  3673.         reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3674.         lua_pushnumber(L, LUA_ERROR);
  3675.     }
  3676.     return 1;
  3677. }
  3678.  
  3679. int LuaScriptInterface::luaGetCreatureOutfit(lua_State *L)
  3680. {
  3681.     //getCreatureOutfit(cid)
  3682.  
  3683.     uint32_t cid = popNumber(L);
  3684.  
  3685.     ScriptEnviroment* env = getScriptEnv();
  3686.  
  3687.     Creature* creature = env->getCreatureByUID(cid);
  3688.  
  3689.     if(creature){
  3690.         const Outfit_t outfit = creature->getCurrentOutfit();
  3691.  
  3692.         lua_newtable(L);
  3693.         setField(L, "lookType", outfit.lookType);
  3694.         setField(L, "lookHead", outfit.lookHead);
  3695.         setField(L, "lookBody", outfit.lookBody);
  3696.         setField(L, "lookLegs", outfit.lookLegs);
  3697.         setField(L, "lookFeet", outfit.lookFeet);
  3698.         setField(L, "lookAddons", outfit.lookAddons);
  3699.  
  3700.         return 1;
  3701.     }
  3702.     else{
  3703.         reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3704.         lua_pushnumber(L, LUA_ERROR);
  3705.     }
  3706.  
  3707.     return 1;
  3708. }
  3709.  
  3710. int LuaScriptInterface::luaSetMonsterOutfit(lua_State *L)
  3711. {
  3712.     //doSetMonsterOutfit(cid, name, time)
  3713.  
  3714.     int32_t time = (int32_t)popNumber(L);
  3715.     std::string name = popString(L);
  3716.     uint32_t cid = popNumber(L);
  3717.  
  3718.     ScriptEnviroment* env = getScriptEnv();
  3719.  
  3720.     Creature* creature = env->getCreatureByUID(cid);
  3721.  
  3722.     if(creature){
  3723.         ReturnValue ret = Spell::CreateIllusion(creature, name, time);
  3724.         if(ret == RET_NOERROR){
  3725.             lua_pushnumber(L, LUA_NO_ERROR);
  3726.         }
  3727.         else{
  3728.             lua_pushnumber(L, LUA_ERROR);
  3729.         }
  3730.     }
  3731.     else{
  3732.         reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3733.         lua_pushnumber(L, LUA_ERROR);
  3734.     }
  3735.     return 1;
  3736. }
  3737.  
  3738. int LuaScriptInterface::luaSetItemOutfit(lua_State *L)
  3739. {
  3740.     //doSetItemOutfit(cid, item, time)
  3741.  
  3742.     int32_t time = (int32_t)popNumber(L);
  3743.     uint32_t item = (uint32_t)popNumber(L);
  3744.     uint32_t cid = popNumber(L);
  3745.  
  3746.     ScriptEnviroment* env = getScriptEnv();
  3747.  
  3748.     Creature* creature = env->getCreatureByUID(cid);
  3749.  
  3750.     if(creature){
  3751.         ReturnValue ret = Spell::CreateIllusion(creature, item, time);
  3752.         if(ret == RET_NOERROR){
  3753.             lua_pushnumber(L, LUA_NO_ERROR);
  3754.         }
  3755.         else{
  3756.             lua_pushnumber(L, LUA_ERROR);
  3757.         }
  3758.     }
  3759.     else{
  3760.         reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3761.         lua_pushnumber(L, LUA_ERROR);
  3762.     }
  3763.     return 1;
  3764. }
  3765.  
  3766. int LuaScriptInterface::luaGetGlobalStorageValue(lua_State *L)
  3767. {
  3768.     //getGlobalStorageValue(valueid)
  3769.     uint32_t key = popNumber(L);
  3770.    
  3771.     ScriptEnviroment* env = getScriptEnv();
  3772.    
  3773.     int32_t value;
  3774.     if(env->getGlobalStorageValue(key, value)){
  3775.         lua_pushnumber(L, value);
  3776.     }
  3777.     else{
  3778.         lua_pushnumber(L, -1);
  3779.     }
  3780.     return 1;
  3781. }
  3782.  
  3783. int LuaScriptInterface::luaSetGlobalStorageValue(lua_State *L)
  3784. {
  3785.     //setGlobalStorageValue(valueid, newvalue)
  3786.     int32_t value = (int32_t)popNumber(L);
  3787.     uint32_t key = popNumber(L);
  3788.    
  3789.     ScriptEnviroment* env = getScriptEnv();
  3790.     env->addGlobalStorageValue(key,value);
  3791.     lua_pushnumber(L,0);
  3792.     return 1;
  3793. }
  3794.  
  3795. int LuaScriptInterface::luaGetPlayerDepotItems(lua_State *L)
  3796. {
  3797.     //getPlayerDepotItems(cid, depotid)
  3798.     uint32_t depotid = popNumber(L);
  3799.     uint32_t cid = popNumber(L);
  3800.    
  3801.     ScriptEnviroment* env = getScriptEnv();
  3802.     Player* player = env->getPlayerByUID(cid);
  3803.     if(player){
  3804.         const Depot* depot = player->getDepot(depotid, true);
  3805.         if(depot){
  3806.             lua_pushnumber(L, depot->getItemHoldingCount());
  3807.         }
  3808.         else{
  3809.             reportErrorFunc("Depot not found");
  3810.             lua_pushnumber(L, LUA_ERROR);
  3811.         }
  3812.     }
  3813.     else{
  3814.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  3815.         lua_pushnumber(L, LUA_ERROR);
  3816.     }
  3817.     return 1;
  3818. }
  3819.  
  3820. int LuaScriptInterface::luaDoPlayerSetGuildRank(lua_State *L)
  3821. {
  3822.     //doPlayerSetGuildRank(cid, rank)
  3823.     const char* rank = popString(L);
  3824.     uint32_t cid = popNumber(L);
  3825.    
  3826.     ScriptEnviroment* env = getScriptEnv();
  3827.     Player* player = env->getPlayerByUID(cid);
  3828.     if(player){
  3829.         player->setGuildRank(std::string(rank));
  3830.         lua_pushnumber(L, LUA_NO_ERROR);
  3831.     }
  3832.     else{
  3833.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  3834.         lua_pushnumber(L, LUA_ERROR);
  3835.     }
  3836.     return 1;
  3837. }
  3838.  
  3839. int LuaScriptInterface::luaDoPlayerSetGuildNick(lua_State *L)
  3840. {
  3841.     //doPlayerSetGuildNick(cid, nick)
  3842.     const char* nick = popString(L);
  3843.     uint32_t cid = popNumber(L);
  3844.    
  3845.     ScriptEnviroment* env = getScriptEnv();
  3846.     Player* player = env->getPlayerByUID(cid);
  3847.     if(player){
  3848.         player->setGuildNick(std::string(nick));
  3849.         lua_pushnumber(L, LUA_NO_ERROR);
  3850.     }
  3851.     else{
  3852.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  3853.         lua_pushnumber(L, LUA_ERROR);
  3854.     }
  3855.     return 1;
  3856. }
  3857.  
  3858. int LuaScriptInterface::luaDoMoveCreature(lua_State *L)
  3859. {
  3860.     //doMoveCreature(cid, direction)
  3861.     uint32_t direction = popNumber(L);
  3862.     uint32_t cid = popNumber(L);
  3863.    
  3864.     switch(direction){
  3865.         case NORTH:
  3866.         case SOUTH:
  3867.         case WEST:
  3868.         case EAST:
  3869.         case SOUTHWEST:
  3870.         case NORTHWEST:
  3871.         case NORTHEAST:
  3872.         case SOUTHEAST:
  3873.             break;
  3874.         default:
  3875.             reportErrorFunc("No valid direction");
  3876.             lua_pushnumber(L, LUA_ERROR);
  3877.             return 1;
  3878.     }
  3879.    
  3880.     ScriptEnviroment* env = getScriptEnv();
  3881.    
  3882.     Creature* creature = env->getCreatureByUID(cid);
  3883.     if(creature){
  3884.         ReturnValue ret = g_game.internalMoveCreature(creature, (Direction)direction, true);
  3885.         lua_pushnumber(L, ret);
  3886.     }
  3887.     else{
  3888.         reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  3889.         lua_pushnumber(L, LUA_ERROR);
  3890.     }
  3891.     return 1;
  3892. }
  3893.  
  3894. int LuaScriptInterface::luaIsPlayer(lua_State *L)
  3895. {
  3896.     //isPlayer(cid)
  3897.     uint32_t cid = popNumber(L);
  3898.    
  3899.     ScriptEnviroment* env = getScriptEnv();
  3900.    
  3901.     if(env->getPlayerByUID(cid)){
  3902.         lua_pushnumber(L, LUA_TRUE);
  3903.     }
  3904.     else{
  3905.         lua_pushnumber(L, LUA_FALSE);
  3906.     }
  3907.     return 1;
  3908. }
  3909.  
  3910. int LuaScriptInterface::luaIsCreature(lua_State *L)
  3911. {
  3912.     //isCreature(cid)
  3913.     uint32_t cid = popNumber(L);
  3914.    
  3915.     ScriptEnviroment* env = getScriptEnv();
  3916.    
  3917.     if(env->getCreatureByUID(cid)){
  3918.         lua_pushnumber(L, LUA_TRUE);
  3919.     }
  3920.     else{
  3921.         lua_pushnumber(L, LUA_FALSE);
  3922.     }
  3923.     return 1;
  3924. }
  3925.  
  3926. int LuaScriptInterface::luaIsContainer(lua_State *L)
  3927. {
  3928.     //isContainer(uid)
  3929.     uint32_t uid = popNumber(L);
  3930.    
  3931.     ScriptEnviroment* env = getScriptEnv();
  3932.    
  3933.     if(env->getContainerByUID(uid)){
  3934.         lua_pushnumber(L, LUA_TRUE);
  3935.     }
  3936.     else{
  3937.         lua_pushnumber(L, LUA_FALSE);
  3938.     }
  3939.     return 1;
  3940. }
  3941.    
  3942. int LuaScriptInterface::luaIsMoveable(lua_State *L)
  3943. {
  3944.     //isMoveable(uid)
  3945.     uint32_t uid = popNumber(L);
  3946.    
  3947.     ScriptEnviroment* env = getScriptEnv();
  3948.    
  3949.     Thing* thing = env->getThingByUID(uid);
  3950.    
  3951.     if(thing && thing->isPushable()){
  3952.         lua_pushnumber(L, LUA_TRUE);
  3953.     }
  3954.     else{
  3955.         lua_pushnumber(L, LUA_FALSE);
  3956.     }
  3957.  
  3958.     return 1;
  3959. }
  3960.  
  3961. int LuaScriptInterface::luaGetPlayerByName(lua_State *L)
  3962. {
  3963.     //getPlayerByName(name)
  3964.     const char* name = popString(L);
  3965.    
  3966.     ScriptEnviroment* env = getScriptEnv();
  3967.    
  3968.     if(Player* player = g_game.getPlayerByName(name)){
  3969.         uint32_t cid = env->addThing(player);
  3970.         lua_pushnumber(L, cid);
  3971.     }
  3972.     else{
  3973.         lua_pushnumber(L, LUA_NULL);
  3974.     }
  3975.     return 1;
  3976.    
  3977. }
  3978.  
  3979. int LuaScriptInterface::luaRegisterCreature(lua_State *L)
  3980. {
  3981.     //registerCreature(cid)
  3982.     uint32_t cid = popNumber(L);
  3983.    
  3984.     ScriptEnviroment* env = getScriptEnv();
  3985.    
  3986.     Creature* creature = g_game.getCreatureByID(cid);
  3987.     uint32_t newcid;
  3988.     if(creature){
  3989.         newcid = env->addThing(creature);
  3990.     }
  3991.     else{
  3992.         newcid = 0;
  3993.     }
  3994.  
  3995.     lua_pushnumber(L, newcid);
  3996.     return 1;
  3997. }
  3998.  
  3999. int LuaScriptInterface::luaGetContainerSize(lua_State *L)
  4000. {
  4001.     //getContainerSize(uid)
  4002.     uint32_t uid = popNumber(L);
  4003.    
  4004.     ScriptEnviroment* env = getScriptEnv();
  4005.    
  4006.     if(Container* container = env->getContainerByUID(uid)){
  4007.         lua_pushnumber(L, container->size());
  4008.     }
  4009.     else{
  4010.         reportErrorFunc(getErrorDesc(LUA_ERROR_CONTAINER_NOT_FOUND));
  4011.         lua_pushnumber(L, LUA_ERROR);
  4012.     }
  4013.     return 1;
  4014. }
  4015.  
  4016. int LuaScriptInterface::luaGetContainerCap(lua_State *L)
  4017. {
  4018.     //getContainerCap(uid)
  4019.     uint32_t uid = popNumber(L);
  4020.    
  4021.     ScriptEnviroment* env = getScriptEnv();
  4022.    
  4023.     if(Container* container = env->getContainerByUID(uid)){
  4024.         lua_pushnumber(L, container->capacity());
  4025.     }
  4026.     else{
  4027.         reportErrorFunc(getErrorDesc(LUA_ERROR_CONTAINER_NOT_FOUND));
  4028.         lua_pushnumber(L, LUA_ERROR);
  4029.     }
  4030.     return 1;
  4031. }
  4032.  
  4033. int LuaScriptInterface::luaGetContainerItem(lua_State *L)
  4034. {
  4035.     //getContainerItem(uid, slot)
  4036.     uint32_t slot = popNumber(L);
  4037.     uint32_t uid = popNumber(L);
  4038.    
  4039.     ScriptEnviroment* env = getScriptEnv();
  4040.    
  4041.     if(Container* container = env->getContainerByUID(uid)){
  4042.         Item* item = container->getItem(slot);
  4043.         if(item){
  4044.             uint32_t uid = env->addThing(item);
  4045.             pushThing(L, item, uid);
  4046.         }
  4047.         else{
  4048.             pushThing(L, NULL, 0);
  4049.         }
  4050.     }
  4051.     else{
  4052.         reportErrorFunc(getErrorDesc(LUA_ERROR_CONTAINER_NOT_FOUND));
  4053.         pushThing(L, NULL, 0);
  4054.     }
  4055.     return 1;
  4056.    
  4057. }
  4058.  
  4059. int LuaScriptInterface::luaDoAddContainerItem(lua_State *L)
  4060. {
  4061.     //doAddContainerItem(uid, itemid, count or subtype)
  4062.     uint32_t count = popNumber(L);
  4063.     uint16_t itemId = (uint16_t)popNumber(L);
  4064.     uint32_t uid = popNumber(L);
  4065.    
  4066.     ScriptEnviroment* env = getScriptEnv();
  4067.     Container* container = env->getContainerByUID(uid);
  4068.         if(container){
  4069.         const ItemType& it = Item::items[itemId];
  4070.         if(it.stackable && count > 100){
  4071.             count = 100;
  4072.         }
  4073.  
  4074.         Item* newItem = Item::CreateItem(itemId, count);
  4075.  
  4076.         ReturnValue ret = g_game.internalAddItem(container, newItem);
  4077.         if(ret != RET_NOERROR){
  4078.             delete newItem;
  4079.             reportErrorFunc("Could not add item");
  4080.             lua_pushnumber(L, LUA_ERROR);
  4081.             return 1;
  4082.         }
  4083.        
  4084.         if(newItem->getParent()){
  4085.             uint32_t new_uid = env->addThing((Thing*)newItem);
  4086.             lua_pushnumber(L, new_uid);
  4087.             return 1;
  4088.         }
  4089.         else{
  4090.             //stackable item stacked with existing object, newItem will be released
  4091.             lua_pushnumber(L, LUA_NULL);
  4092.             return 1;
  4093.         }
  4094.     }
  4095.     else{
  4096.         reportErrorFunc(getErrorDesc(LUA_ERROR_CONTAINER_NOT_FOUND));
  4097.         lua_pushnumber(L, LUA_ERROR);
  4098.         return 1;
  4099.     }
  4100. }
  4101.  
  4102. int LuaScriptInterface::luaIsInArray(lua_State *L)
  4103. {
  4104.     //isInArray(array, value)
  4105.     int32_t value = (int32_t)popNumber(L);
  4106.     if(lua_istable(L, -1) == 0){
  4107.         lua_pop(L, 1);
  4108.         lua_pushnumber(L, LUA_ERROR);
  4109.         return 1;
  4110.     }
  4111.    
  4112.     int i = 1;
  4113.     while(1){
  4114.         lua_pushnumber(L, i);
  4115.         lua_gettable(L, -2);
  4116.         if(lua_isnil(L, -1) == 1){
  4117.             lua_pop(L, 2);
  4118.             lua_pushnumber(L, LUA_FALSE);
  4119.             return 1;
  4120.         }
  4121.         else if(lua_isnumber(L, -1) == 1){
  4122.             int32_t array_value = (int32_t)popNumber(L);
  4123.             if(array_value == value){
  4124.                 lua_pop(L, 1);
  4125.                 lua_pushnumber(L, LUA_TRUE);
  4126.                 return 1;
  4127.             }
  4128.         }
  4129.         else{
  4130.             lua_pop(L, 2);
  4131.             lua_pushnumber(L, LUA_ERROR);
  4132.             return 1;
  4133.         }
  4134.         ++i;
  4135.     }
  4136. }
  4137.  
  4138. int LuaScriptInterface::luaDoPlayerAddOutfit(lua_State *L)
  4139. {
  4140.     //doPlayerAddOutfit(cid, looktype, addon)
  4141.     int addon = (int)popNumber(L);
  4142.     int looktype = (int)popNumber(L);
  4143.     uint32_t cid = popNumber(L);
  4144.  
  4145.     ScriptEnviroment* env = getScriptEnv();
  4146.     Player* player = env->getPlayerByUID(cid);
  4147.     if(player){
  4148.         player->addOutfit(looktype, addon);
  4149.         lua_pushnumber(L, LUA_NO_ERROR);
  4150.     }
  4151.     else{
  4152.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  4153.         lua_pushnumber(L, LUA_ERROR);
  4154.     }
  4155.     return 1;
  4156. }
  4157.  
  4158. int LuaScriptInterface::luaDoPlayerRemOutfit(lua_State *L)
  4159. {
  4160.     //doPlayerRemOutfit(cid, looktype, addon)
  4161.     int addon = (int)popNumber(L);
  4162.     int looktype = (int)popNumber(L);
  4163.     uint32_t cid = popNumber(L);
  4164.    
  4165.     ScriptEnviroment* env = getScriptEnv();
  4166.     Player* player = env->getPlayerByUID(cid);
  4167.     if(player){
  4168.         player->remOutfit(looktype, addon);
  4169.         lua_pushnumber(L, LUA_NO_ERROR);
  4170.     }
  4171.     else{
  4172.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  4173.         lua_pushnumber(L, LUA_ERROR);
  4174.     }
  4175.     return 1;
  4176. }
  4177.  
  4178. int LuaScriptInterface::luaDoSetCreatureLight(lua_State *L)
  4179. {
  4180.     //doSetCreatureLight(cid, lightLevel, lightColor, time)
  4181.     uint32_t time = popNumber(L);
  4182.     uint8_t color = (uint8_t)popNumber(L);
  4183.     uint8_t level = (uint8_t)popNumber(L);
  4184.     uint32_t cid = popNumber(L);
  4185.    
  4186.     ScriptEnviroment* env = getScriptEnv();
  4187.     Creature* creature = env->getCreatureByUID(cid);
  4188.     if(creature){
  4189.         Condition* condition = Condition::createCondition(CONDITIONID_COMBAT, CONDITION_LIGHT, time, level | (color << 8));
  4190.        
  4191.         creature->addCondition(condition);
  4192.         lua_pushnumber(L, LUA_NO_ERROR);
  4193.     }
  4194.     else{
  4195.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  4196.         lua_pushnumber(L, LUA_ERROR);
  4197.     }
  4198.     return 1;
  4199. }
  4200.  
  4201. int LuaScriptInterface::luaGetCreaturePosition(lua_State *L)
  4202. {
  4203.     //getCreaturePosition(cid)
  4204.     uint32_t cid = popNumber(L);
  4205.    
  4206.     ScriptEnviroment* env = getScriptEnv();
  4207.    
  4208.     Creature* creature = env->getCreatureByUID(cid);
  4209.     if(creature){
  4210.         Position pos = creature->getPosition();
  4211.         pushPosition(L, pos, 0);
  4212.     }
  4213.     else{
  4214.         reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  4215.         lua_pushnumber(L, LUA_ERROR);
  4216.     }
  4217.     return 1;
  4218. }
  4219.  
  4220. int LuaScriptInterface::luaGetCreatureName(lua_State *L)
  4221. {
  4222.     //getCreatureName(cid)
  4223.     uint32_t cid = popNumber(L);
  4224.    
  4225.     ScriptEnviroment* env = getScriptEnv();
  4226.    
  4227.     Creature* creature = env->getCreatureByUID(cid);
  4228.     if(creature){
  4229.         lua_pushstring(L, creature->getName().c_str());
  4230.     }
  4231.     else{
  4232.         reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  4233.         lua_pushnumber(L, LUA_ERROR);
  4234.     }
  4235.     return 1;
  4236. }
  4237.  
  4238. int LuaScriptInterface::luaIsItemStackable(lua_State *L)
  4239. {
  4240.     //isItemStackable(itemid)
  4241.     uint32_t itemid = popNumber(L);
  4242.     const ItemType& it = Item::items[itemid];
  4243.     if(it.stackable){
  4244.         lua_pushnumber(L, LUA_TRUE);
  4245.     }
  4246.     else{
  4247.         lua_pushnumber(L, LUA_FALSE);
  4248.     }
  4249.     return 1;
  4250. }
  4251.  
  4252. int LuaScriptInterface::luaIsItemRune(lua_State *L)
  4253. {
  4254.     //isItemRune(itemid)
  4255.     uint32_t itemid = popNumber(L);
  4256.     const ItemType& it = Item::items[itemid];
  4257.     if(it.isRune()){
  4258.         lua_pushnumber(L, LUA_TRUE);
  4259.     }
  4260.     else{
  4261.         lua_pushnumber(L, LUA_FALSE);
  4262.     }
  4263.     return 1;
  4264. }
  4265.  
  4266. int LuaScriptInterface::luaIsItemDoor(lua_State *L)
  4267. {
  4268.     //isItemDoor(itemid)
  4269.     uint32_t itemid = popNumber(L);
  4270.     const ItemType& it = Item::items[itemid];
  4271.     if(it.isDoor()){
  4272.         lua_pushnumber(L, LUA_TRUE);
  4273.     }
  4274.     else{
  4275.         lua_pushnumber(L, LUA_FALSE);
  4276.     }
  4277.     return 1;
  4278. }
  4279.  
  4280. int LuaScriptInterface::luaIsItemContainer(lua_State *L)
  4281. {
  4282.     //isItemContainer(itemid)
  4283.     uint32_t itemid = popNumber(L);
  4284.     const ItemType& it = Item::items[itemid];
  4285.     if(it.isContainer()){
  4286.         lua_pushnumber(L, LUA_TRUE);
  4287.     }
  4288.     else{
  4289.         lua_pushnumber(L, LUA_FALSE);
  4290.     }
  4291.     return 1;
  4292. }
  4293.  
  4294. int LuaScriptInterface::luaIsItemFluidContainer(lua_State *L)
  4295. {
  4296.     //isItemFluidContainer(itemid)
  4297.     uint32_t itemid = popNumber(L);
  4298.     const ItemType& it = Item::items[itemid];
  4299.     if(it.isFluidContainer()){
  4300.         lua_pushnumber(L, LUA_TRUE);
  4301.     }
  4302.     else{
  4303.         lua_pushnumber(L, LUA_FALSE);
  4304.     }
  4305.     return 1;
  4306. }
  4307.  
  4308. int LuaScriptInterface::luaGetItemName(lua_State *L)
  4309. {
  4310.     //getItemName(itemid)
  4311.     uint32_t itemid = popNumber(L);
  4312.     const ItemType& it = Item::items[itemid];
  4313.     lua_pushstring(L, it.name.c_str());
  4314.     return 1;
  4315. }
  4316.  
  4317. int LuaScriptInterface::getCreatureTarget(lua_State *L)
  4318. {
  4319.     //getCreatureTarget(cid)
  4320.     ScriptEnviroment* env = getScriptEnv();
  4321.     if( Creature* creature = env->getCreatureByUID(popNumber(L)) )
  4322.     {
  4323.         Creature* target = creature->getAttackedCreature();
  4324.         lua_pushnumber(L, target ? env->addThing(target) : 0);
  4325.     }
  4326.     else
  4327.     {
  4328.         reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
  4329.         lua_pushboolean(L, false);
  4330.     }
  4331.     return 1;
  4332. }
  4333.  
  4334. int LuaScriptInterface::luaAddEvent(lua_State *L)
  4335. {
  4336.     //addEvent(callback, delay, parameter)    
  4337.     ScriptEnviroment* env = getScriptEnv();
  4338.    
  4339.     LuaScriptInterface* script_interface = env->getScriptInterface();
  4340.     if(!script_interface){
  4341.         reportError(__FUNCTION__, "No valid script interface!");
  4342.         lua_pushnumber(L, LUA_ERROR);
  4343.         return 1;
  4344.     }
  4345.    
  4346.     if(lua_isfunction(L, -3) == 0){
  4347.         reportError(__FUNCTION__, "callback parameter should be a function.");
  4348.         lua_pushnumber(L, LUA_ERROR);
  4349.         return 1;
  4350.     }
  4351.  
  4352.     LuaTimerEventDesc eventDesc;
  4353.     eventDesc.parameter = luaL_ref(L, LUA_REGISTRYINDEX);
  4354.     uint32_t delay = popNumber(L);
  4355.     if(delay < 100){
  4356.         delay = 100;
  4357.     }
  4358.     eventDesc.function = luaL_ref(L, LUA_REGISTRYINDEX);
  4359.    
  4360.     eventDesc.scriptId = env->getScriptId();
  4361.    
  4362.     script_interface->m_lastEventTimerId++;
  4363.     script_interface->m_timerEvents[script_interface->m_lastEventTimerId] = eventDesc;
  4364.    
  4365.     g_game.addEvent(makeTask(delay, boost::bind(&LuaScriptInterface::executeTimerEvent, script_interface, script_interface->m_lastEventTimerId)));
  4366.    
  4367.     lua_pushnumber(L, script_interface->m_lastEventTimerId);
  4368.     return 1;
  4369. }
  4370.  
  4371. int LuaScriptInterface::luaStopEvent(lua_State *L)
  4372. {
  4373.     //stopEvent(eventid)
  4374.     uint32_t eventId = popNumber(L);
  4375.     ScriptEnviroment* env = getScriptEnv();
  4376.    
  4377.     LuaScriptInterface* script_interface = env->getScriptInterface();
  4378.     if(!script_interface){
  4379.         reportError(__FUNCTION__, "No valid script interface!");
  4380.         lua_pushnumber(L, LUA_ERROR);
  4381.         return 1;
  4382.     }
  4383.  
  4384.     LuaTimerEvents::iterator it = script_interface->m_timerEvents.find(eventId);
  4385.     if(it != script_interface->m_timerEvents.end()){
  4386.         luaL_unref(script_interface->m_luaState, LUA_REGISTRYINDEX, it->second.parameter);
  4387.         luaL_unref(script_interface->m_luaState, LUA_REGISTRYINDEX, it->second.function);
  4388.         script_interface->m_timerEvents.erase(it);
  4389.         lua_pushnumber(L, LUA_NO_ERROR);
  4390.     }
  4391.     else{
  4392.         lua_pushnumber(L, LUA_ERROR);
  4393.     }
  4394.    
  4395.     return 1;
  4396. }
  4397.  
  4398. int LuaScriptInterface::luaGetDataDirectory(lua_State *L)
  4399. {
  4400.     //getDataDir()
  4401.     std::string datadir = g_config.getString(ConfigManager::DATA_DIRECTORY);
  4402.     lua_pushstring(L, datadir.c_str());
  4403.     return 1;
  4404. }
  4405.  
  4406. #ifdef __XID_CVS_MODS__
  4407. int LuaScriptInterface::luaGetPlayerSkull(lua_State *L)
  4408. {
  4409.     //getPlayerSkull(cid)
  4410.     uint32_t cid = popNumber(L);
  4411.    
  4412.     ScriptEnviroment* env = getScriptEnv();
  4413.     Player* player = env->getPlayerByUID(cid);
  4414.    
  4415.     if(player){
  4416.         lua_pushnumber(L, (int)player->getSkull());
  4417.     }
  4418.     else{
  4419.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  4420.         lua_pushnumber(L, LUA_ERROR);
  4421.     }
  4422.    
  4423.     return 1;
  4424. }
  4425.  
  4426. int LuaScriptInterface::luaGetPlayerConditionTicks(lua_State *L)
  4427. {
  4428.     //getPlayerConditionTicks(cid, conditionid)
  4429.     ConditionType_t conditionType;
  4430.     uint32_t conditionTicks = 0;
  4431.    
  4432.     conditionType = (ConditionType_t)popNumber(L);
  4433.     uint32_t cid = popNumber(L);
  4434.    
  4435.     ScriptEnviroment* env = getScriptEnv();
  4436.     Player* player = env->getPlayerByUID(cid);
  4437.    
  4438.     if(player){
  4439.         if(Condition* condition = player->getCondition(conditionType, CONDITIONID_DEFAULT)){
  4440.             conditionTicks = condition->getTicks() / 1000;
  4441.         }
  4442.        
  4443.         lua_pushnumber(L, conditionTicks);
  4444.     }
  4445.     else{
  4446.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  4447.         lua_pushnumber(L, LUA_ERROR);
  4448.     }
  4449.    
  4450.     return 1;
  4451. }
  4452. #endif
  4453.  
  4454. #ifdef __XID_SEPERATE_ADDONS__
  4455. int LuaScriptInterface::luaDoPlayerAddAddon(lua_State* L)
  4456. {
  4457.     uint32_t addon = popNumber(L);
  4458.     uint32_t lookType = popNumber(L);
  4459.     uint32_t cid = popNumber(L);
  4460.  
  4461.     ScriptEnviroment* env = getScriptEnv();
  4462.     Player* player = env->getPlayerByUID(cid);
  4463.    
  4464.     if(player){
  4465.         player->addAddon(lookType, addon);
  4466.         lua_pushnumber(L, LUA_NO_ERROR);
  4467.     }          
  4468.     else{
  4469.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  4470.         lua_pushnumber(L, LUA_ERROR);
  4471.     }
  4472.  
  4473.     return 1;
  4474. }
  4475.  
  4476. int LuaScriptInterface::luaGetPlayerOutfitAddon(lua_State* L)
  4477. {
  4478.     uint32_t lookType = popNumber(L);
  4479.     uint32_t cid = popNumber(L);
  4480.  
  4481.     ScriptEnviroment* env = getScriptEnv();
  4482.     Player* player = env->getPlayerByUID(cid);
  4483.    
  4484.     if(player){
  4485.         lua_pushnumber(L, player->getOutfitAddon(lookType));
  4486.     }          
  4487.     else{
  4488.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  4489.         lua_pushnumber(L, LUA_ERROR);
  4490.     }
  4491.  
  4492.     return 1;
  4493. }
  4494. #endif
  4495.  
  4496. #ifdef __XID_BUY_SELL__
  4497. int LuaScriptInterface::luaGetItemStackable(lua_State *L)
  4498. {
  4499.     //getItemStackable(id)
  4500.     int id = popNumber(L);
  4501.     if(Item::items[id].stackable || Item::items[id].isRune())
  4502.         lua_pushboolean(L, 1);
  4503.     else
  4504.         lua_pushboolean(L, 0);
  4505.    
  4506.     return 1;
  4507. }
  4508. #endif
  4509.  
  4510. #ifdef __XID_PREMIUM_SYSTEM__
  4511. int LuaScriptInterface::luaIsPremium(lua_State *L)
  4512. {
  4513.     //isPremium(cid)
  4514.     uint32_t cid = popNumber(L);
  4515.    
  4516.     ScriptEnviroment* env = getScriptEnv();
  4517.     Player* player = env->getPlayerByUID(cid);
  4518.    
  4519.     if(player){
  4520.         lua_pushboolean(L, player->isPremium());
  4521.     }
  4522.     else{
  4523.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  4524.         lua_pushnumber(L, LUA_ERROR);
  4525.     }
  4526.    
  4527.     return 1;
  4528. }
  4529.  
  4530. int LuaScriptInterface::luaBuyPremium(lua_State *L)
  4531. {
  4532.     int days = popNumber(L);
  4533.     uint32_t cid = popNumber(L);
  4534.  
  4535.     int premiumTime = days * 86400;
  4536.     ScriptEnviroment* env = getScriptEnv();
  4537.     Player* player = env->getPlayerByUID(cid);
  4538.    
  4539.     if(player){
  4540.         g_game.savePremium(player, premiumTime, false);
  4541.         lua_pushnumber(L, LUA_NO_ERROR);
  4542.     }          
  4543.     else{
  4544.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  4545.         lua_pushnumber(L, LUA_ERROR);
  4546.     }
  4547.  
  4548.     return 1;
  4549. }
  4550. #endif
  4551.  
  4552. #ifdef __YUR_GUILD_SYSTEM__
  4553. int LuaScriptInterface::luaFoundNewGuild(lua_State* L)
  4554. {
  4555.     const char* gname = popString(L);
  4556.     if(gname)
  4557.         lua_pushnumber(L, Guilds::AddNewGuild(gname));
  4558.     else
  4559.         lua_pushnumber(L, -1);
  4560.  
  4561.     return 1;
  4562. }
  4563.  
  4564. int LuaScriptInterface::luaGetPlayerGuildStatus(lua_State* L)
  4565. {
  4566.     const char* name = popString(L);
  4567.     lua_pushnumber(L, Guilds::GetGuildStatus(name));
  4568.    
  4569.     return 1;
  4570. }
  4571.  
  4572. int LuaScriptInterface::luaSetPlayerGuildStatus(lua_State* L)
  4573. {
  4574.     int gstat = popNumber(L);
  4575.     const char* name = popString(L);
  4576.  
  4577.     Guilds::SetGuildStatus(std::string(name), (gstat_t)gstat);
  4578.     if(Player* player = g_game.getPlayerByName(name)){
  4579.         Guilds::ReloadGuildInfo(player);
  4580.     }
  4581.  
  4582.     return 0;
  4583. }
  4584.  
  4585. int LuaScriptInterface::luaGetPlayerGuildName(lua_State* L)
  4586. {
  4587.     const char* name = popString(L);
  4588.     lua_pushstring(L, Guilds::GetGuildName(name).c_str());
  4589.    
  4590.     return 1;
  4591. }
  4592.  
  4593. int LuaScriptInterface::luaSetPlayerGuild(lua_State* L)
  4594. {
  4595.     const char* gname = popString(L);
  4596.     const char* grank = popString(L);
  4597.     int gstat = popNumber(L);
  4598.     const char* name = popString(L);
  4599.  
  4600.     Guilds::SetGuildInfo(name, (gstat_t)gstat, grank, gname);
  4601.     if(Player* player = g_game.getPlayerByName(name))
  4602.         Guilds::ReloadGuildInfo(player);
  4603.            
  4604.     return 0;
  4605. }
  4606.  
  4607. int LuaScriptInterface::luaClearPlayerGuild(lua_State* L)
  4608. {
  4609.     const char* name = popString(L);
  4610.  
  4611.     Guilds::ClearGuildInfo(name);
  4612.     if(Player* player = g_game.getPlayerByName(name))
  4613.         Guilds::ReloadGuildInfo(player);
  4614.  
  4615.     return 0;
  4616. }
  4617.  
  4618. int LuaScriptInterface::luaSetPlayerGuildNick(lua_State* L)
  4619. {
  4620.     const char* nick = popString(L);
  4621.     const char* name = popString(L);
  4622.    
  4623.     Guilds::SetGuildNick(name, nick);
  4624.     if(Player* player = g_game.getPlayerByName(name))
  4625.         Guilds::ReloadGuildInfo(player);
  4626.  
  4627.     return 0;
  4628. }
  4629. #endif
  4630.  
  4631. #ifdef __XID_LEARN_SPELLS__
  4632. int LuaScriptInterface::luaDoPlayerLearnSpell(lua_State* L)
  4633. {
  4634.     const char* spell = popString(L);
  4635.     uint32_t cid = popNumber(L);
  4636.  
  4637.     ScriptEnviroment* env = getScriptEnv();
  4638.     Player* player = env->getPlayerByUID(cid);
  4639.    
  4640.     if(player){
  4641.         if(!player->knowsSpell(spell)){
  4642.             player->learnSpell(spell);
  4643.             lua_pushnumber(L, LUA_TRUE);
  4644.         }
  4645.         else{
  4646.             lua_pushnumber(L, LUA_TRUE);
  4647.         }
  4648.     }          
  4649.     else{
  4650.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  4651.         lua_pushnumber(L, LUA_ERROR);
  4652.     }
  4653.  
  4654.     return 1;
  4655. }
  4656. #endif
  4657.  
  4658. #ifdef __XID_BLESS_SYSTEM__
  4659. int LuaScriptInterface::luaDoPlayerAddBlessing(lua_State* L)
  4660. {
  4661.     //doPlayerAddBlessing(cid, blessid)
  4662.     int32_t blessId = popNumber(L);
  4663.     uint32_t cid = popNumber(L);
  4664.  
  4665.     ScriptEnviroment* env = getScriptEnv();
  4666.     Player* player = env->getPlayerByUID(cid);
  4667.    
  4668.     if(player){
  4669.         player->addBlessing(blessId);
  4670.         g_game.addMagicEffect(player->getPosition(), NM_ME_MAGIC_POISON);
  4671.        
  4672.         lua_pushnumber(L, LUA_NO_ERROR);
  4673.     }          
  4674.     else{
  4675.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  4676.         lua_pushnumber(L, LUA_FALSE);
  4677.     }
  4678.  
  4679.     return 1;
  4680. }
  4681.  
  4682. int LuaScriptInterface::luaGetPlayerBlessing(lua_State *L)
  4683. {
  4684.     //getPlayerBlessing(cid, blessid)
  4685.     uint32_t blessId = popNumber(L);
  4686.     uint32_t cid = popNumber(L);
  4687.    
  4688.     ScriptEnviroment* env = getScriptEnv();
  4689.     Player* player = env->getPlayerByUID(cid);
  4690.    
  4691.     if(player){
  4692.         lua_pushboolean(L, player->getBlessing(blessId));
  4693.     }
  4694.     else{
  4695.         reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
  4696.         lua_pushnumber(L, LUA_ERROR);
  4697.     }
  4698.    
  4699.     return 1;
  4700. }
  4701. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement