Advertisement
Guest User

luascript

a guest
Apr 7th, 2021
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 294.89 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////
  2. // OpenTibia - an opensource roleplaying game
  3. ////////////////////////////////////////////////////////////////////////
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16. ////////////////////////////////////////////////////////////////////////
  17. #include "otpch.h"
  18. #include "luascript.h"
  19. #include "scriptmanager.h"
  20.  
  21. #include <boost/filesystem.hpp>
  22. #include <boost/any.hpp>
  23. #include <iostream>
  24. #include <iomanip>
  25.  
  26. #include "player.h"
  27. #include "item.h"
  28. #include "teleport.h"
  29. #include "beds.h"
  30.  
  31. #include "town.h"
  32. #include "house.h"
  33. #include "housetile.h"
  34.  
  35. #include "database.h"
  36. #include "databasemanager.h"
  37. #include "iologindata.h"
  38. #include "ioban.h"
  39. #include "iomap.h"
  40. #include "iomapserialize.h"
  41.  
  42. #include "monsters.h"
  43. #include "movement.h"
  44. #include "spells.h"
  45. #include "talkaction.h"
  46. #include "creatureevent.h"
  47.  
  48. #include "combat.h"
  49. #include "condition.h"
  50.  
  51. #include "baseevents.h"
  52. #include "raids.h"
  53.  
  54. #include "configmanager.h"
  55. #include "vocation.h"
  56. #include "status.h"
  57. #include "game.h"
  58. #include "chat.h"
  59. #include "tools.h"
  60.  
  61. extern ConfigManager g_config;
  62. extern Game g_game;
  63. extern Chat g_chat;
  64. extern Monsters g_monsters;
  65. extern MoveEvents* g_moveEvents;
  66. extern Spells* g_spells;
  67. extern TalkActions* g_talkActions;
  68. extern CreatureEvents* g_creatureEvents;
  69.  
  70. enum
  71. {
  72.     EVENT_ID_LOADING = 1,
  73.     EVENT_ID_USER = 1000,
  74. };
  75.  
  76. enum STR2INT_ERROR
  77. {
  78.     SUCCESS,
  79.     OVERFLOWS,
  80.     UNDERFLOWS,
  81.     INCONVERTIBLE
  82. };
  83.  
  84. STR2INT_ERROR str2int(int32_t& i, char const* s, int32_t base = 0)
  85. {
  86.     char* end;
  87.     long  l;
  88.     errno = 0;
  89.     l = strtol(s, &end, base);
  90.     if((errno == ERANGE && l == LONG_MAX) || l > INT_MAX) {
  91.         return OVERFLOWS;
  92.     }
  93.     if((errno == ERANGE && l == LONG_MIN) || l < INT_MIN) {
  94.         return UNDERFLOWS;
  95.     }
  96.     if(*s == '\0' || *end != '\0') {
  97.         return INCONVERTIBLE;
  98.     }
  99.     i = l;
  100.     return SUCCESS;
  101. }
  102.  
  103. ScriptEnviroment::AreaMap ScriptEnviroment::m_areaMap;
  104. uint32_t ScriptEnviroment::m_lastAreaId = 0;
  105. ScriptEnviroment::CombatMap ScriptEnviroment::m_combatMap;
  106. uint32_t ScriptEnviroment::m_lastCombatId = 0;
  107. ScriptEnviroment::ConditionMap ScriptEnviroment::m_conditionMap;
  108. uint32_t ScriptEnviroment::m_lastConditionId = 0;
  109. ScriptEnviroment::ConditionMap ScriptEnviroment::m_tempConditionMap;
  110. uint32_t ScriptEnviroment::m_lastTempConditionId = 0;
  111.  
  112. ScriptEnviroment::ThingMap ScriptEnviroment::m_globalMap;
  113. ScriptEnviroment::TempItemListMap ScriptEnviroment::m_tempItems;
  114. StorageMap ScriptEnviroment::m_storageMap;
  115.  
  116. ScriptEnviroment::ScriptEnviroment()
  117. {
  118.     m_lastUID = 70000;
  119.     m_loaded = true;
  120.     reset();
  121. }
  122.  
  123. ScriptEnviroment::~ScriptEnviroment()
  124. {
  125.     for(CombatMap::iterator it = m_combatMap.begin(); it != m_combatMap.end(); ++it)
  126.         delete it->second;
  127.  
  128.     m_combatMap.clear();
  129.     for(AreaMap::iterator it = m_areaMap.begin(); it != m_areaMap.end(); ++it)
  130.         delete it->second;
  131.  
  132.     m_areaMap.clear();
  133.     for(ConditionMap::iterator it = m_conditionMap.begin(); it != m_conditionMap.end(); ++it)
  134.         delete it->second;
  135.  
  136.     m_conditionMap.clear();
  137.     reset();
  138. }
  139.  
  140. void ScriptEnviroment::reset()
  141. {
  142.     m_scriptId = m_callbackId = 0;
  143.     m_realPos = Position();
  144.     m_timerEvent = false;
  145.  
  146.     m_interface = NULL;
  147.     for(TempItemListMap::iterator mit = m_tempItems.begin(); mit != m_tempItems.end(); ++mit)
  148.     {
  149.         for(ItemList::iterator it = mit->second.begin(); it != mit->second.end(); ++it)
  150.         {
  151.             if((*it)->getParent() == VirtualCylinder::virtualCylinder)
  152.                 g_game.freeThing(*it);
  153.         }
  154.     }
  155.  
  156.     m_tempItems.clear();
  157.     for(DBResultMap::iterator it = m_tempResults.begin(); it != m_tempResults.end(); ++it)
  158.     {
  159.         if(it->second)
  160.             it->second->free();
  161.     }
  162.  
  163.     m_tempResults.clear();
  164.     for(ConditionMap::iterator it = m_tempConditionMap.begin(); it != m_tempConditionMap.end(); ++it)
  165.         delete it->second;
  166.  
  167.     m_tempConditionMap.clear();
  168.     m_localMap.clear();
  169. }
  170.  
  171. bool ScriptEnviroment::saveGameState()
  172. {
  173.     if(!g_config.getBool(ConfigManager::SAVE_GLOBAL_STORAGE))
  174.         return true;
  175.  
  176.     Database* db = Database::getInstance();
  177.     std::ostringstream query;
  178.  
  179.     query << "DELETE FROM `global_storage` WHERE `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << ";";
  180.     if(!db->query(query.str()))
  181.         return false;
  182.  
  183.     DBInsert stmt(db);
  184.     stmt.setQuery("INSERT INTO `global_storage` (`key`, `world_id`, `value`) VALUES ");
  185.     for(StorageMap::const_iterator it = m_storageMap.begin(); it != m_storageMap.end(); ++it)
  186.     {
  187.         query.str("");
  188.         query << db->escapeString(it->first) << "," << g_config.getNumber(ConfigManager::WORLD_ID) << "," << db->escapeString(it->second);
  189.         if(!stmt.addRow(query))
  190.             return false;
  191.     }
  192.  
  193.     return stmt.execute();
  194. }
  195.  
  196. bool ScriptEnviroment::loadGameState()
  197. {
  198.     Database* db = Database::getInstance();
  199.     DBResult* result;
  200.  
  201.     std::ostringstream query;
  202.     query << "SELECT `key`, `value` FROM `global_storage` WHERE `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << ";";
  203.     if((result = db->storeQuery(query.str())))
  204.     {
  205.         do
  206.             m_storageMap[result->getDataString("key")] = result->getDataString("value");
  207.         while(result->next());
  208.         result->free();
  209.     }
  210.  
  211.     query.str("");
  212.     return true;
  213. }
  214.  
  215. bool ScriptEnviroment::setCallbackId(int32_t callbackId, LuaInterface* interface)
  216. {
  217.     if(!m_callbackId)
  218.     {
  219.         m_callbackId = callbackId;
  220.         m_interface = interface;
  221.         return true;
  222.     }
  223.  
  224.     //nested callbacks are not allowed
  225.     if(m_interface)
  226.         m_interface->errorEx("Nested callbacks!");
  227.  
  228.     return false;
  229. }
  230.  
  231. void ScriptEnviroment::getInfo(int32_t& scriptId, std::string& desc, LuaInterface*& interface, int32_t& callbackId, bool& timerEvent)
  232. {
  233.     scriptId = m_scriptId;
  234.     desc = m_event;
  235.     interface = m_interface;
  236.     callbackId = m_callbackId;
  237.     timerEvent = m_timerEvent;
  238. }
  239.  
  240. void ScriptEnviroment::addUniqueThing(Thing* thing)
  241. {
  242.     Item* item = thing->getItem();
  243.     if(!item || !item->getUniqueId())
  244.         return;
  245.  
  246.     if(m_globalMap[item->getUniqueId()])
  247.     {
  248.         if(item->getActionId() != 2000) //scripted quest system
  249.             std::clog << "Duplicate uniqueId " << item->getUniqueId() << std::endl;
  250.     }
  251.     else
  252.         m_globalMap[item->getUniqueId()] = thing;
  253. }
  254.  
  255. void ScriptEnviroment::removeUniqueThing(Thing* thing)
  256. {
  257.     Item* item = thing->getItem();
  258.     if(!item || !item->getUniqueId())
  259.         return;
  260.  
  261.     ThingMap::iterator it = m_globalMap.find(item->getUniqueId());
  262.     if(it != m_globalMap.end())
  263.         m_globalMap.erase(it);
  264. }
  265.  
  266. uint32_t ScriptEnviroment::addThing(Thing* thing)
  267. {
  268.     if(!thing || thing->isRemoved())
  269.         return 0;
  270.  
  271.     for(ThingMap::iterator it = m_localMap.begin(); it != m_localMap.end(); ++it)
  272.     {
  273.         if(it->second == thing)
  274.             return it->first;
  275.     }
  276.  
  277.     if(Creature* creature = thing->getCreature())
  278.     {
  279.         m_localMap[creature->getID()] = thing;
  280.         return creature->getID();
  281.     }
  282.  
  283.     if(Item* item = thing->getItem())
  284.     {
  285.         uint32_t tmp = item->getUniqueId();
  286.         if(tmp)
  287.         {
  288.             m_localMap[tmp] = thing;
  289.             return tmp;
  290.         }
  291.     }
  292.  
  293.     while(m_localMap.find(m_lastUID) != m_localMap.end())
  294.         ++m_lastUID;
  295.  
  296.     m_localMap[m_lastUID] = thing;
  297.     return m_lastUID;
  298. }
  299.  
  300. void ScriptEnviroment::insertThing(uint32_t uid, Thing* thing)
  301. {
  302.     if(m_localMap[uid])
  303.         std::clog << "[Error - ScriptEnviroment::insertThing] Thing uid already taken" << std::endl;
  304.     else
  305.         m_localMap[uid] = thing;
  306. }
  307.  
  308. Thing* ScriptEnviroment::getThingByUID(uint32_t uid)
  309. {
  310.     Thing* tmp = m_localMap[uid];
  311.     if(tmp && !tmp->isRemoved())
  312.         return tmp;
  313.  
  314.     tmp = m_globalMap[uid];
  315.     if(tmp && !tmp->isRemoved())
  316.         return tmp;
  317.  
  318.     if(uid < PLAYER_ID_RANGE)
  319.         return NULL;
  320.  
  321.     if(!(tmp = g_game.getCreatureByID(uid)) || tmp->isRemoved())
  322.         return NULL;
  323.  
  324.     m_localMap[uid] = tmp;
  325.     return tmp;
  326. }
  327.  
  328. Item* ScriptEnviroment::getItemByUID(uint32_t uid)
  329. {
  330.     if(Thing* tmp = getThingByUID(uid))
  331.     {
  332.         if(Item* item = tmp->getItem())
  333.             return item;
  334.     }
  335.  
  336.     return NULL;
  337. }
  338.  
  339. Container* ScriptEnviroment::getContainerByUID(uint32_t uid)
  340. {
  341.     if(Item* tmp = getItemByUID(uid))
  342.     {
  343.         if(Container* container = tmp->getContainer())
  344.             return container;
  345.     }
  346.  
  347.     return NULL;
  348. }
  349.  
  350. Creature* ScriptEnviroment::getCreatureByUID(uint32_t uid)
  351. {
  352.     if(Thing* tmp = getThingByUID(uid))
  353.     {
  354.         if(Creature* creature = tmp->getCreature())
  355.             return creature;
  356.     }
  357.  
  358.     return NULL;
  359. }
  360.  
  361. Player* ScriptEnviroment::getPlayerByUID(uint32_t uid)
  362. {
  363.     if(Thing* tmp = getThingByUID(uid))
  364.     {
  365.         if(Creature* creature = tmp->getCreature())
  366.         {
  367.             if(Player* player = creature->getPlayer())
  368.                 return player;
  369.         }
  370.     }
  371.  
  372.     return NULL;
  373. }
  374.  
  375. void ScriptEnviroment::removeThing(uint32_t uid)
  376. {
  377.     ThingMap::iterator it;
  378.     it = m_localMap.find(uid);
  379.     if(it != m_localMap.end())
  380.         m_localMap.erase(it);
  381.  
  382.     it = m_globalMap.find(uid);
  383.     if(it != m_globalMap.end())
  384.         m_globalMap.erase(it);
  385. }
  386.  
  387. uint32_t ScriptEnviroment::addCombatArea(CombatArea* area)
  388. {
  389.     uint32_t newAreaId = m_lastAreaId + 1;
  390.     m_areaMap[newAreaId] = area;
  391.  
  392.     m_lastAreaId++;
  393.     return newAreaId;
  394. }
  395.  
  396. CombatArea* ScriptEnviroment::getCombatArea(uint32_t areaId)
  397. {
  398.     AreaMap::const_iterator it = m_areaMap.find(areaId);
  399.     if(it != m_areaMap.end())
  400.         return it->second;
  401.  
  402.     return NULL;
  403. }
  404.  
  405. uint32_t ScriptEnviroment::addCombatObject(Combat* combat)
  406. {
  407.     uint32_t newCombatId = m_lastCombatId + 1;
  408.     m_combatMap[newCombatId] = combat;
  409.  
  410.     m_lastCombatId++;
  411.     return newCombatId;
  412. }
  413.  
  414. Combat* ScriptEnviroment::getCombatObject(uint32_t combatId)
  415. {
  416.     CombatMap::iterator it = m_combatMap.find(combatId);
  417.     if(it != m_combatMap.end())
  418.         return it->second;
  419.  
  420.     return NULL;
  421. }
  422.  
  423. uint32_t ScriptEnviroment::addConditionObject(Condition* condition)
  424. {
  425.     m_conditionMap[++m_lastConditionId] = condition;
  426.     return m_lastConditionId;
  427. }
  428.  
  429. uint32_t ScriptEnviroment::addTempConditionObject(Condition* condition)
  430. {
  431.     m_tempConditionMap[++m_lastTempConditionId] = condition;
  432.     return m_lastTempConditionId;
  433. }
  434.  
  435. Condition* ScriptEnviroment::getConditionObject(uint32_t conditionId, bool loaded)
  436. {
  437.     ConditionMap::iterator it;
  438.     if(loaded)
  439.     {
  440.         it = m_conditionMap.find(conditionId);
  441.         if(it != m_conditionMap.end())
  442.             return it->second;
  443.     }
  444.     else
  445.     {
  446.         it = m_tempConditionMap.find(conditionId);
  447.         if(it != m_tempConditionMap.end())
  448.             return it->second;
  449.     }
  450.  
  451.     return NULL;
  452. }
  453.  
  454. void ScriptEnviroment::addTempItem(ScriptEnviroment* env, Item* item)
  455. {
  456.     m_tempItems[env].push_back(item);
  457. }
  458.  
  459. void ScriptEnviroment::removeTempItem(ScriptEnviroment* env, Item* item)
  460. {
  461.     ItemList::iterator it = std::find(m_tempItems[env].begin(), m_tempItems[env].end(), item);
  462.     if(it != m_tempItems[env].end())
  463.         m_tempItems[env].erase(it);
  464. }
  465.  
  466. void ScriptEnviroment::removeTempItem(Item* item)
  467. {
  468.     ItemList::iterator it;
  469.     for(TempItemListMap::iterator mit = m_tempItems.begin(); mit != m_tempItems.end(); ++mit)
  470.     {
  471.         it = std::find(mit->second.begin(), mit->second.end(), item);
  472.         if(it != mit->second.end())
  473.             mit->second.erase(it);
  474.     }
  475. }
  476.  
  477. uint32_t ScriptEnviroment::addResult(DBResult* res)
  478. {
  479.     uint32_t lastId = 0;
  480.     while(m_tempResults.find(lastId) != m_tempResults.end())
  481.         lastId++;
  482.  
  483.     m_tempResults[lastId] = res;
  484.     return lastId;
  485. }
  486.  
  487. bool ScriptEnviroment::removeResult(uint32_t id)
  488. {
  489.     DBResultMap::iterator it = m_tempResults.find(id);
  490.     if(it == m_tempResults.end())
  491.         return false;
  492.  
  493.     if(it->second)
  494.         it->second->free();
  495.  
  496.     m_tempResults.erase(it);
  497.     return true;
  498. }
  499.  
  500. DBResult* ScriptEnviroment::getResultByID(uint32_t id)
  501. {
  502.     DBResultMap::iterator it = m_tempResults.find(id);
  503.     if(it != m_tempResults.end())
  504.         return it->second;
  505.  
  506.     return NULL;
  507. }
  508.  
  509. bool ScriptEnviroment::getStorage(const std::string& key, std::string& value) const
  510. {
  511.     StorageMap::const_iterator it = m_storageMap.find(key);
  512.     if(it != m_storageMap.end())
  513.     {
  514.         value = it->second;
  515.         return true;
  516.     }
  517.  
  518.     value = "-1";
  519.     return false;
  520. }
  521.  
  522. void ScriptEnviroment::streamVariant(std::ostringstream& stream, const std::string& local, const LuaVariant& var)
  523. {
  524.     if(!local.empty())
  525.         stream << "local " << local << " = {" << std::endl;
  526.  
  527.     stream << "type = " << var.type;
  528.     switch(var.type)
  529.     {
  530.         case VARIANT_NUMBER:
  531.             stream << "," << std::endl << "number = " << var.number;
  532.             break;
  533.         case VARIANT_STRING:
  534.             stream << "," << std::endl << "string = \"" << var.text << "\"";
  535.             break;
  536.         case VARIANT_TARGETPOSITION:
  537.         case VARIANT_POSITION:
  538.         {
  539.             stream << "," << std::endl;
  540.             streamPosition(stream, "pos", var.pos);
  541.             break;
  542.         }
  543.         case VARIANT_NONE:
  544.         default:
  545.             break;
  546.     }
  547.  
  548.     if(!local.empty())
  549.         stream << std::endl << "}" << std::endl;
  550. }
  551.  
  552. void ScriptEnviroment::streamThing(std::ostringstream& stream, const std::string& local, Thing* thing, uint32_t id/* = 0*/)
  553. {
  554.     if(!local.empty())
  555.         stream << "local " << local << " = {" << std::endl;
  556.  
  557.     if(thing && thing->getItem())
  558.     {
  559.         const Item* item = thing->getItem();
  560.         if(!id)
  561.             id = addThing(thing);
  562.  
  563.         stream << "uniqueid = " << id << "," << std::endl;
  564.         stream << "uid = " << id << "," << std::endl;
  565.         stream << "itemid = " << item->getID() << "," << std::endl;
  566.         stream << "id = " << item->getID() << "," << std::endl;
  567.         if(item->hasSubType())
  568.             stream << "type = " << item->getSubType() << "," << std::endl;
  569.         else
  570.             stream << "type = 0," << std::endl;
  571.  
  572.         stream << "actionid = " << item->getActionId() << "," << std::endl;
  573.         stream << "aid = " << item->getActionId() << std::endl;
  574.     }
  575.     else if(thing && thing->getCreature())
  576.     {
  577.         const Creature* creature = thing->getCreature();
  578.         if(!id)
  579.             id = creature->getID();
  580.  
  581.         stream << "uniqueid = " << id << "," << std::endl;
  582.         stream << "uid = " << id << "," << std::endl;
  583.         stream << "itemid = 1," << std::endl;
  584.         stream << "id = 1," << std::endl;
  585.         if(creature->getPlayer())
  586.             stream << "type = 1," << std::endl;
  587.         else if(creature->getMonster())
  588.             stream << "type = 2," << std::endl;
  589.         else
  590.             stream << "type = 3," << std::endl;
  591.  
  592.         if(const Player* player = creature->getPlayer())
  593.         {
  594.             stream << "actionid = " << player->getGUID() << "," << std::endl;
  595.             stream << "aid = " << player->getGUID() << std::endl;
  596.         }
  597.         else
  598.         {
  599.             stream << "actionid = 0," << std::endl;
  600.             stream << "aid = 0" << std::endl;
  601.         }
  602.     }
  603.     else
  604.     {
  605.         stream << "uniqueid = 0," << std::endl;
  606.         stream << "uid = 0," << std::endl;
  607.         stream << "itemid = 0," << std::endl;
  608.         stream << "id = 0," << std::endl;
  609.         stream << "type = 0," << std::endl;
  610.         stream << "aid = 0," << std::endl;
  611.         stream << "actionid = 0" << std::endl;
  612.     }
  613.  
  614.     if(!local.empty())
  615.         stream << "}" << std::endl;
  616. }
  617.  
  618. void ScriptEnviroment::streamPosition(std::ostringstream& stream, const std::string& local, const Position& position, uint32_t stackpos)
  619. {
  620.     if(!local.empty())
  621.         stream << "local " << local << " = {" << std::endl;
  622.  
  623.     stream << "x = " << position.x << "," << std::endl;
  624.     stream << "y = " << position.y << "," << std::endl;
  625.     stream << "z = " << position.z << "," << std::endl;
  626.  
  627.     stream << "stackpos = " << stackpos << std::endl;
  628.     if(!local.empty())
  629.         stream << "}" << std::endl;
  630. }
  631.  
  632. void ScriptEnviroment::streamOutfit(std::ostringstream& stream, const std::string& local, const Outfit_t& outfit)
  633. {
  634.     if(!local.empty())
  635.         stream << "local " << local << " = {" << std::endl;
  636.  
  637.     stream << "lookType = " << outfit.lookType << "," << std::endl;
  638.     stream << "lookTypeEx = " << outfit.lookTypeEx << "," << std::endl;
  639.  
  640.     stream << "lookHead = " << outfit.lookHead << "," << std::endl;
  641.     stream << "lookBody = " << outfit.lookBody << "," << std::endl;
  642.     stream << "lookLegs = " << outfit.lookLegs << "," << std::endl;
  643.     stream << "lookFeet = " << outfit.lookFeet << "," << std::endl;
  644.  
  645.     stream << "lookAddons = " << outfit.lookAddons << std::endl;
  646.     if(!local.empty())
  647.         stream << "}" << std::endl;
  648. }
  649.  
  650. std::string LuaInterface::getError(ErrorCode_t code)
  651. {
  652.     switch(code)
  653.     {
  654.         case LUA_ERROR_PLAYER_NOT_FOUND:
  655.             return "Player not found";
  656.         case LUA_ERROR_MONSTER_NOT_FOUND:
  657.             return "Monster not found";
  658.         case LUA_ERROR_NPC_NOT_FOUND:
  659.             return "NPC not found";
  660.         case LUA_ERROR_CREATURE_NOT_FOUND:
  661.             return "Creature not found";
  662.         case LUA_ERROR_ITEM_NOT_FOUND:
  663.             return "Item not found";
  664.         case LUA_ERROR_THING_NOT_FOUND:
  665.             return "Thing not found";
  666.         case LUA_ERROR_TILE_NOT_FOUND:
  667.             return "Tile not found";
  668.         case LUA_ERROR_HOUSE_NOT_FOUND:
  669.             return "House not found";
  670.         case LUA_ERROR_COMBAT_NOT_FOUND:
  671.             return "Combat not found";
  672.         case LUA_ERROR_CONDITION_NOT_FOUND:
  673.             return "Condition not found";
  674.         case LUA_ERROR_AREA_NOT_FOUND:
  675.             return "Area not found";
  676.         case LUA_ERROR_CONTAINER_NOT_FOUND:
  677.             return "Container not found";
  678.         case LUA_ERROR_VARIANT_NOT_FOUND:
  679.             return "Variant not found";
  680.         case LUA_ERROR_VARIANT_UNKNOWN:
  681.             return "Unknown variant type";
  682.         case LUA_ERROR_SPELL_NOT_FOUND:
  683.             return "Spell not found";
  684.         default:
  685.             break;
  686.     }
  687.  
  688.     return "Invalid error code!";
  689. }
  690.  
  691. ScriptEnviroment LuaInterface::m_scriptEnv[21];
  692. int32_t LuaInterface::m_scriptEnvIndex = -1;
  693.  
  694. LuaInterface::LuaInterface(std::string interfaceName)
  695. {
  696.     m_luaState = NULL;
  697.     m_interfaceName = interfaceName;
  698.     m_lastTimer = 1000;
  699.     m_errors = true;
  700. }
  701.  
  702. LuaInterface::~LuaInterface()
  703. {
  704.     for(LuaTimerEvents::iterator it = m_timerEvents.begin(); it != m_timerEvents.end(); ++it)
  705.         Scheduler::getInstance().stopEvent(it->second.eventId);
  706.  
  707.     closeState();
  708. }
  709.  
  710. bool LuaInterface::reInitState()
  711. {
  712.     closeState();
  713.     return initState();
  714. }
  715.  
  716. bool LuaInterface::loadBuffer(const std::string& text, Npc* npc/* = NULL*/)
  717. {
  718.     //loads buffer as a chunk at stack top
  719.     int32_t ret = luaL_loadbuffer(m_luaState, text.c_str(), text.length(), "LuaInterface::loadBuffer");
  720.     if(ret)
  721.     {
  722.         m_lastError = popString(m_luaState);
  723.         std::clog << "[Error - LuaInterface::loadBuffer] " << m_lastError << std::endl;
  724.         return false;
  725.     }
  726.  
  727.     //check that it is loaded as a function
  728.     if(!lua_isfunction(m_luaState, -1))
  729.         return false;
  730.  
  731.     m_loadingFile = text;
  732.     reserveEnv();
  733.  
  734.     ScriptEnviroment* env = getEnv();
  735.     env->setScriptId(EVENT_ID_LOADING, this);
  736.     env->setNpc(npc);
  737.  
  738.     //execute it
  739.     ret = lua_pcall(m_luaState, 0, 0, 0);
  740.     if(ret)
  741.     {
  742.         error(NULL, popString(m_luaState));
  743.         releaseEnv();
  744.         return false;
  745.     }
  746.  
  747.     releaseEnv();
  748.     return true;
  749. }
  750.  
  751. bool LuaInterface::loadFile(const std::string& file, Npc* npc/* = NULL*/)
  752. {
  753.     //loads file as a chunk at stack top
  754.     int32_t ret = luaL_loadfile(m_luaState, file.c_str());
  755.     if(ret)
  756.     {
  757.         m_lastError = popString(m_luaState);
  758.         std::clog << "[Error - LuaInterface::loadFile] " << m_lastError << std::endl;
  759.         return false;
  760.     }
  761.  
  762.     //check that it is loaded as a function
  763.     if(!lua_isfunction(m_luaState, -1))
  764.         return false;
  765.  
  766.     m_loadingFile = file;
  767.     reserveEnv();
  768.  
  769.     ScriptEnviroment* env = getEnv();
  770.     env->setScriptId(EVENT_ID_LOADING, this);
  771.     env->setNpc(npc);
  772.  
  773.     //execute it
  774.     ret = lua_pcall(m_luaState, 0, 0, 0);
  775.     if(ret)
  776.     {
  777.         error(NULL, popString(m_luaState));
  778.         releaseEnv();
  779.         return false;
  780.     }
  781.  
  782.     releaseEnv();
  783.     return true;
  784. }
  785.  
  786. bool LuaInterface::loadDirectory(std::string dir, bool recursively, bool loadSystems, Npc* npc/* = NULL*/)
  787. {
  788.     if(dir[dir.size() - 1] != '/')
  789.         dir += '/';
  790.  
  791.     StringVec files;
  792.     for(boost::filesystem::directory_iterator it(dir), end; it != end; ++it)
  793.     {
  794.         std::string s = BOOST_DIR_ITER_FILENAME(it);
  795.         if(!loadSystems && s[0] == '_')
  796.             continue;
  797.  
  798.         if(boost::filesystem::is_directory(it->status()))
  799.         {
  800.             if(recursively && !loadDirectory(dir + s, recursively, loadSystems, npc))
  801.                 return false;
  802.         }
  803.         else if((s.size() > 4 ? s.substr(s.size() - 4) : "") == ".lua")
  804.             files.push_back(s);
  805.     }
  806.  
  807.     std::sort(files.begin(), files.end());
  808.     for(StringVec::iterator it = files.begin(); it != files.end(); ++it)
  809.     {
  810.         if(!loadFile(dir + (*it), npc))
  811.             return false;
  812.     }
  813.  
  814.     return true;
  815. }
  816.  
  817. int32_t LuaInterface::getEvent(const std::string& eventName)
  818. {
  819.     //get our events table
  820.     lua_getfield(m_luaState, LUA_REGISTRYINDEX, "EVENTS");
  821.     if(!lua_istable(m_luaState, -1))
  822.     {
  823.         lua_pop(m_luaState, 1);
  824.         return -1;
  825.     }
  826.  
  827.     //get current event function pointer
  828.     lua_getglobal(m_luaState, eventName.c_str());
  829.     if(!lua_isfunction(m_luaState, -1))
  830.     {
  831.         lua_pop(m_luaState, 1);
  832.         return -1;
  833.     }
  834.  
  835.     //save in our events table
  836.     lua_pushnumber(m_luaState, m_runningEvent);
  837.     lua_pushvalue(m_luaState, -2);
  838.  
  839.     lua_rawset(m_luaState, -4);
  840.     lua_pop(m_luaState, 2);
  841.  
  842.     //reset global value of this event
  843.     lua_pushnil(m_luaState);
  844.     lua_setglobal(m_luaState, eventName.c_str());
  845.  
  846.     m_cacheFiles[m_runningEvent] = m_loadingFile + ":" + eventName;
  847.     ++m_runningEvent;
  848.     return m_runningEvent - 1;
  849. }
  850.  
  851. std::string LuaInterface::getScript(int32_t scriptId)
  852. {
  853.     if(scriptId == EVENT_ID_LOADING)
  854.         return m_loadingFile;
  855.  
  856.     ScriptsCache::iterator it = m_cacheFiles.find(scriptId);
  857.     if(it != m_cacheFiles.end())
  858.         return it->second;
  859.  
  860.     return "(Unknown script file)";
  861. }
  862.  
  863. void LuaInterface::error(const char* function, const std::string& desc)
  864. {
  865.     if(g_config.getBool(ConfigManager::SILENT_LUA))
  866.         return;
  867.  
  868.     int32_t script, callback;
  869.     bool timer;
  870.     std::string event;
  871.  
  872.     LuaInterface* interface;
  873.     getEnv()->getInfo(script, event, interface, callback, timer);
  874.     if(interface)
  875.     {
  876.         if(!interface->m_errors)
  877.             return;
  878.  
  879.         std::clog << std::endl << "[Error - " << interface->getName() << "] " << std::endl;
  880.         if(callback)
  881.             std::clog << "In a callback: " << interface->getScript(callback) << std::endl;
  882.  
  883.         if(timer)
  884.             std::clog << (callback ? "from" : "In") << " a timer event called from: " << std::endl;
  885.  
  886.         std::clog << interface->getScript(script) << std::endl << "Description: ";
  887.     }
  888.     else
  889.         std::clog << std::endl << "[Lua Error] ";
  890.  
  891.     std::clog << event << std::endl;
  892.     if(function)
  893.         std::clog << "(" << function << ") ";
  894.  
  895.     std::clog << desc << std::endl;
  896. }
  897.  
  898. bool LuaInterface::pushFunction(int32_t function)
  899. {
  900.     lua_getfield(m_luaState, LUA_REGISTRYINDEX, "EVENTS");
  901.     if(!lua_istable(m_luaState, -1))
  902.         return false;
  903.  
  904.     lua_pushnumber(m_luaState, function);
  905.     lua_rawget(m_luaState, -2);
  906.  
  907.     lua_remove(m_luaState, -2);
  908.     return lua_isfunction(m_luaState, -1);
  909. }
  910.  
  911. bool LuaInterface::initState()
  912. {
  913.     m_luaState = luaL_newstate();
  914.     if(!m_luaState)
  915.         return false;
  916.  
  917.     luaL_openlibs(m_luaState);
  918. #ifdef __LUAJIT__
  919.     luaJIT_setmode(m_luaState, 0, LUAJIT_MODE_ENGINE | LUAJIT_MODE_ON);
  920. #endif
  921.  
  922.     registerFunctions();
  923.     std::string path = getFilePath(FILE_TYPE_OTHER, "lib/");
  924.     if(!fileExists(path) || !loadDirectory(path, false, true))
  925.         std::clog << "[Warning - LuaInterface::initState] Cannot load " << path << std::endl;
  926.  
  927.     lua_newtable(m_luaState);
  928.     lua_setfield(m_luaState, LUA_REGISTRYINDEX, "EVENTS");
  929.     m_runningEvent = EVENT_ID_USER;
  930.     return true;
  931. }
  932.  
  933. bool LuaInterface::closeState()
  934. {
  935.     if(!m_luaState)
  936.         return false;
  937.  
  938.     m_cacheFiles.clear();
  939.     for(LuaTimerEvents::iterator it = m_timerEvents.begin(); it != m_timerEvents.end(); ++it)
  940.     {
  941.         for(std::list<int32_t>::iterator lt = it->second.parameters.begin(); lt != it->second.parameters.end(); ++lt)
  942.             luaL_unref(m_luaState, LUA_REGISTRYINDEX, *lt);
  943.  
  944.         it->second.parameters.clear();
  945.         luaL_unref(m_luaState, LUA_REGISTRYINDEX, it->second.function);
  946.     }
  947.  
  948.     m_timerEvents.clear();
  949.     lua_close(m_luaState);
  950.     return true;
  951. }
  952.  
  953. void LuaInterface::executeTimer(uint32_t eventIndex)
  954. {
  955.     LuaTimerEvents::iterator it = m_timerEvents.find(eventIndex);
  956.     if(it == m_timerEvents.end())
  957.         return;
  958.  
  959.     //push function
  960.     lua_rawgeti(m_luaState, LUA_REGISTRYINDEX, it->second.function);
  961.     //push parameters
  962.     for(std::list<int32_t>::reverse_iterator rt = it->second.parameters.rbegin(); rt != it->second.parameters.rend(); ++rt)
  963.         lua_rawgeti(m_luaState, LUA_REGISTRYINDEX, *rt);
  964.  
  965.     //call the function
  966.     if(reserveEnv())
  967.     {
  968.         ScriptEnviroment* env = getEnv();
  969.         env->setTimerEvent();
  970.  
  971.         env->setScriptId(it->second.scriptId, this);
  972.         env->setNpc(it->second.npc);
  973.  
  974.         callFunction(it->second.parameters.size());
  975.         releaseEnv();
  976.     }
  977.     else
  978.         std::clog << "[Error - LuaInterface::executeTimer] Call stack overflow." << std::endl;
  979.  
  980.     //free resources
  981.     for(std::list<int32_t>::iterator lt = it->second.parameters.begin(); lt != it->second.parameters.end(); ++lt)
  982.         luaL_unref(m_luaState, LUA_REGISTRYINDEX, *lt);
  983.  
  984.     it->second.parameters.clear();
  985.     luaL_unref(m_luaState, LUA_REGISTRYINDEX, it->second.function);
  986.     m_timerEvents.erase(it);
  987. }
  988.  
  989. int32_t LuaInterface::handleFunction(lua_State* L)
  990. {
  991.     lua_getfield(L, LUA_GLOBALSINDEX, "debug");
  992.     if(!lua_istable(L, -1))
  993.     {
  994.         lua_pop(L, 1);
  995.         return 1;
  996.     }
  997.  
  998.     lua_getfield(L, -1, "traceback");
  999.     if(!lua_isfunction(L, -1))
  1000.     {
  1001.         lua_pop(L, 2);
  1002.         return 1;
  1003.     }
  1004.  
  1005.     lua_pushvalue(L, 1);
  1006.     lua_pushinteger(L, 2);
  1007.  
  1008.     lua_call(L, 2, 1);
  1009.     return 1;
  1010. }
  1011.  
  1012. bool LuaInterface::callFunction(uint32_t params)
  1013. {
  1014.     int32_t size = lua_gettop(m_luaState), handler = lua_gettop(m_luaState) - params;
  1015.     lua_pushcfunction(m_luaState, handleFunction);
  1016.  
  1017.     bool result = false;
  1018.     lua_insert(m_luaState, handler);
  1019.     if(lua_pcall(m_luaState, params, 1, handler))
  1020.         LuaInterface::error(NULL, LuaInterface::popString(m_luaState));
  1021.     else
  1022.         result = (int32_t)LuaInterface::popBoolean(m_luaState);
  1023.  
  1024.     lua_remove(m_luaState, handler);
  1025.     if((lua_gettop(m_luaState) + (int32_t)params + 1) != size)
  1026.         LuaInterface::error(NULL, "Stack size changed!");
  1027.  
  1028.     return result;
  1029. }
  1030.  
  1031. void LuaInterface::dumpStack(lua_State* L/* = NULL*/)
  1032. {
  1033.     if(!L)
  1034.         L = m_luaState;
  1035.  
  1036.     int32_t stack = lua_gettop(L);
  1037.     if(!stack)
  1038.         return;
  1039.  
  1040.     std::clog << "Stack size: " << stack << std::endl;
  1041.     for(int32_t i = 1; i <= stack ; ++i)
  1042.         std::clog << lua_typename(m_luaState, lua_type(m_luaState, -i)) << " " << lua_topointer(m_luaState, -i) << std::endl;
  1043. }
  1044.  
  1045. void LuaInterface::pushVariant(lua_State* L, const LuaVariant& var)
  1046. {
  1047.     lua_newtable(L);
  1048.     setField(L, "type", var.type);
  1049.     switch(var.type)
  1050.     {
  1051.         case VARIANT_NUMBER:
  1052.             setField(L, "number", var.number);
  1053.             break;
  1054.         case VARIANT_STRING:
  1055.             setField(L, "string", var.text);
  1056.             break;
  1057.         case VARIANT_TARGETPOSITION:
  1058.         case VARIANT_POSITION:
  1059.         {
  1060.             lua_pushstring(L, "pos");
  1061.             pushPosition(L, var.pos);
  1062.             pushTable(L);
  1063.             break;
  1064.         }
  1065.         case VARIANT_NONE:
  1066.             break;
  1067.     }
  1068. }
  1069.  
  1070. void LuaInterface::pushThing(lua_State* L, Thing* thing, uint32_t id/* = 0*/, Recursive_t recursive/* = RECURSE_FIRST*/)
  1071. {
  1072.     lua_newtable(L);
  1073.     if(thing && thing->getItem())
  1074.     {
  1075.         const Item* item = thing->getItem();
  1076.         if(!id)
  1077.             id = getEnv()->addThing(thing);
  1078.  
  1079.         setField(L, "uniqueid", id);
  1080.         setField(L, "uid", id);
  1081.         setField(L, "itemid", item->getID());
  1082.         setField(L, "id", item->getID());
  1083.         if(item->hasSubType())
  1084.             setField(L, "type", item->getSubType());
  1085.         else
  1086.             setField(L, "type", 0);
  1087.  
  1088.         setField(L, "actionid", item->getActionId());
  1089.         setField(L, "aid", item->getActionId());
  1090.         if(recursive != RECURSE_NONE)
  1091.         {
  1092.             if(const Container* container = item->getContainer())
  1093.             {
  1094.                 if(recursive == RECURSE_FIRST)
  1095.                     recursive = RECURSE_NONE;
  1096.  
  1097.                 ItemList::const_iterator it = container->getItems();
  1098.                 createTable(L, "items");
  1099.                 for(int32_t i = 1; it != container->getEnd(); ++it, ++i)
  1100.                 {
  1101.                     lua_pushnumber(L, i);
  1102.                     pushThing(L, *it, getEnv()->addThing(*it), recursive);
  1103.                     pushTable(L);
  1104.                 }
  1105.  
  1106.                 pushTable(L);
  1107.             }
  1108.         }
  1109.     }
  1110.     else if(thing && thing->getCreature())
  1111.     {
  1112.         const Creature* creature = thing->getCreature();
  1113.         if(!id)
  1114.             id = creature->getID();
  1115.  
  1116.         setField(L, "uniqueid", id);
  1117.         setField(L, "uid", id);
  1118.         setField(L, "itemid", 1);
  1119.         setField(L, "id", 1);
  1120.         if(creature->getPlayer())
  1121.             setField(L, "type", 1);
  1122.         else if(creature->getMonster())
  1123.             setField(L, "type", 2);
  1124.         else
  1125.             setField(L, "type", 3);
  1126.  
  1127.         if(const Player* player = creature->getPlayer())
  1128.         {
  1129.             setField(L, "actionid", player->getGUID());
  1130.             setField(L, "aid", player->getGUID());
  1131.         }
  1132.         else
  1133.         {
  1134.             setField(L, "actionid", 0);
  1135.             setField(L, "aid", 0);
  1136.         }
  1137.     }
  1138.     else
  1139.     {
  1140.         setField(L, "uid", 0);
  1141.         setField(L, "uniqueid", 0);
  1142.         setField(L, "itemid", 0);
  1143.         setField(L, "id", 0);
  1144.         setField(L, "type", 0);
  1145.         setField(L, "actionid", 0);
  1146.         setField(L, "aid", 0);
  1147.     }
  1148. }
  1149.  
  1150. void LuaInterface::pushPosition(lua_State* L, const Position& position, uint32_t stackpos)
  1151. {
  1152.     lua_newtable(L);
  1153.     setField(L, "x", position.x);
  1154.     setField(L, "y", position.y);
  1155.     setField(L, "z", position.z);
  1156.     setField(L, "stackpos", stackpos);
  1157. }
  1158.  
  1159. void LuaInterface::pushOutfit(lua_State* L, const Outfit_t& outfit)
  1160. {
  1161.     lua_newtable(L);
  1162.     setField(L, "lookType", outfit.lookType);
  1163.     setField(L, "lookTypeEx", outfit.lookTypeEx);
  1164.     setField(L, "lookHead", outfit.lookHead);
  1165.     setField(L, "lookBody", outfit.lookBody);
  1166.     setField(L, "lookLegs", outfit.lookLegs);
  1167.     setField(L, "lookFeet", outfit.lookFeet);
  1168.     setField(L, "lookAddons", outfit.lookAddons);
  1169. }
  1170.  
  1171. void LuaInterface::pushCallback(lua_State* L, int32_t callback)
  1172. {
  1173.     lua_rawgeti(L, LUA_REGISTRYINDEX, callback);
  1174. }
  1175.  
  1176. LuaVariant LuaInterface::popVariant(lua_State* L)
  1177. {
  1178.     LuaVariant var;
  1179.     var.type = (LuaVariantType_t)getField(L, "type");
  1180.     switch(var.type)
  1181.     {
  1182.         case VARIANT_NUMBER:
  1183.             var.number = getFieldUnsigned(L, "number");
  1184.             break;
  1185.         case VARIANT_STRING:
  1186.             var.text = getField(L, "string");
  1187.             break;
  1188.         case VARIANT_POSITION:
  1189.         case VARIANT_TARGETPOSITION:
  1190.         {
  1191.             lua_pushstring(L, "pos");
  1192.             lua_gettable(L, -2);
  1193.             popPosition(L, var.pos);
  1194.             break;
  1195.         }
  1196.         default:
  1197.             var.type = VARIANT_NONE;
  1198.             break;
  1199.     }
  1200.  
  1201.     lua_pop(L, 1); //table
  1202.     return var;
  1203. }
  1204.  
  1205. void LuaInterface::popPosition(lua_State* L, PositionEx& position)
  1206. {
  1207.     if(!lua_isboolean(L, -1))
  1208.     {
  1209.         position.x = getField(L, "x");
  1210.         position.y = getField(L, "y");
  1211.         position.z = getField(L, "z");
  1212.         position.stackpos = getField(L, "stackpos");
  1213.     }
  1214.     else
  1215.         position = PositionEx();
  1216.  
  1217.     lua_pop(L, 1); //table
  1218. }
  1219.  
  1220. void LuaInterface::popPosition(lua_State* L, Position& position, uint32_t& stackpos)
  1221. {
  1222.     stackpos = 0;
  1223.     if(!lua_isboolean(L, -1))
  1224.     {
  1225.         position.x = getField(L, "x");
  1226.         position.y = getField(L, "y");
  1227.         position.z = getField(L, "z");
  1228.         stackpos = getField(L, "stackpos");
  1229.     }
  1230.     else
  1231.         position = Position();
  1232.  
  1233.     lua_pop(L, 1); //table
  1234. }
  1235.  
  1236. bool LuaInterface::popBoolean(lua_State* L)
  1237. {
  1238.     lua_pop(L, 1);
  1239.     return (lua_toboolean(L, 0) != 0);
  1240. }
  1241.  
  1242. int64_t LuaInterface::popNumber(lua_State* L)
  1243. {
  1244.     lua_pop(L, 1);
  1245.     if(lua_isboolean(L, 0))
  1246.         return (int64_t)lua_toboolean(L, 0);
  1247.  
  1248.     return (int64_t)lua_tonumber(L, 0);
  1249. }
  1250.  
  1251. double LuaInterface::popFloatNumber(lua_State* L)
  1252. {
  1253.     lua_pop(L, 1);
  1254.     return lua_tonumber(L, 0);
  1255. }
  1256.  
  1257. std::string LuaInterface::popString(lua_State* L)
  1258. {
  1259.     lua_pop(L, 1);
  1260.     if(!lua_isstring(L, 0) && !lua_isnumber(L, 0))
  1261.         return std::string();
  1262.  
  1263.     const char* str = lua_tostring(L, 0);
  1264.     if(*str == '\0')
  1265.         return std::string();
  1266.  
  1267.     return str;
  1268. }
  1269.  
  1270. int32_t LuaInterface::popCallback(lua_State* L)
  1271. {
  1272.     return luaL_ref(L, LUA_REGISTRYINDEX);
  1273. }
  1274.  
  1275. Outfit_t LuaInterface::popOutfit(lua_State* L)
  1276. {
  1277.     Outfit_t outfit;
  1278.     outfit.lookAddons = getField(L, "lookAddons");
  1279.  
  1280.     outfit.lookFeet = getField(L, "lookFeet");
  1281.     outfit.lookLegs = getField(L, "lookLegs");
  1282.     outfit.lookBody = getField(L, "lookBody");
  1283.     outfit.lookHead = getField(L, "lookHead");
  1284.  
  1285.     outfit.lookTypeEx = getField(L, "lookTypeEx");
  1286.     outfit.lookType = getField(L, "lookType");
  1287.  
  1288.     lua_pop(L, 1); //table
  1289.     return outfit;
  1290. }
  1291.  
  1292. void LuaInterface::setField(lua_State* L, const char* index, int32_t val)
  1293. {
  1294.     lua_pushstring(L, index);
  1295.     lua_pushnumber(L, val);
  1296.     pushTable(L);
  1297. }
  1298.  
  1299. void LuaInterface::setField(lua_State* L, const char* index, const std::string& val)
  1300. {
  1301.     lua_pushstring(L, index);
  1302.     lua_pushstring(L, val.c_str());
  1303.     pushTable(L);
  1304. }
  1305.  
  1306. void LuaInterface::setFieldBool(lua_State* L, const char* index, bool val)
  1307. {
  1308.     lua_pushstring(L, index);
  1309.     lua_pushboolean(L, val);
  1310.     pushTable(L);
  1311. }
  1312.  
  1313. void LuaInterface::setFieldFloat(lua_State* L, const char* index, double val)
  1314. {
  1315.     lua_pushstring(L, index);
  1316.     lua_pushnumber(L, val);
  1317.     pushTable(L);
  1318. }
  1319.  
  1320. void LuaInterface::createTable(lua_State* L, const char* index)
  1321. {
  1322.     lua_pushstring(L, index);
  1323.     lua_newtable(L);
  1324. }
  1325.  
  1326. void LuaInterface::createTable(lua_State* L, const char* index, int32_t narr, int32_t nrec)
  1327. {
  1328.     lua_pushstring(L, index);
  1329.     lua_createtable(L, narr, nrec);
  1330. }
  1331.  
  1332. void LuaInterface::createTable(lua_State* L, int32_t index)
  1333. {
  1334.     lua_pushnumber(L, index);
  1335.     lua_newtable(L);
  1336. }
  1337.  
  1338. void LuaInterface::createTable(lua_State* L, int32_t index, int32_t narr, int32_t nrec)
  1339. {
  1340.     lua_pushnumber(L, index);
  1341.     lua_createtable(L, narr, nrec);
  1342. }
  1343.  
  1344. void LuaInterface::pushTable(lua_State* L)
  1345. {
  1346.     lua_settable(L, -3);
  1347. }
  1348.  
  1349. int64_t LuaInterface::getField(lua_State* L, const char* key)
  1350. {
  1351.     lua_pushstring(L, key);
  1352.     lua_gettable(L, -2); // get table[key]
  1353.  
  1354.     int64_t result = (int64_t)lua_tonumber(L, -1);
  1355.     lua_pop(L, 1); // remove number and key
  1356.     return result;
  1357. }
  1358.  
  1359. uint64_t LuaInterface::getFieldUnsigned(lua_State* L, const char* key)
  1360. {
  1361.     lua_pushstring(L, key);
  1362.     lua_gettable(L, -2); // get table[key]
  1363.  
  1364.     uint64_t result = (uint64_t)lua_tonumber(L, -1);
  1365.     lua_pop(L, 1); // remove number and key
  1366.     return result;
  1367. }
  1368.  
  1369. bool LuaInterface::getFieldBool(lua_State* L, const char* key)
  1370. {
  1371.     lua_pushstring(L, key);
  1372.     lua_gettable(L, -2); // get table[key]
  1373.  
  1374.     bool result = (lua_toboolean(L, -1) != 0);
  1375.     lua_pop(L, 1); // remove number and key
  1376.     return result;
  1377. }
  1378.  
  1379. std::string LuaInterface::getFieldString(lua_State* L, const char* key)
  1380. {
  1381.     lua_pushstring(L, key);
  1382.     lua_gettable(L, -2); // get table[key]
  1383.  
  1384.     std::string result = lua_tostring(L, -1);
  1385.     lua_pop(L, 1); // remove number and key
  1386.     return result;
  1387. }
  1388.  
  1389. std::string LuaInterface::getGlobalString(lua_State* L, const std::string& _identifier, const std::string& _default/* = ""*/)
  1390. {
  1391.     lua_getglobal(L, _identifier.c_str());
  1392.     if(!lua_isstring(L, -1))
  1393.     {
  1394.         lua_pop(L, 1);
  1395.         return _default;
  1396.     }
  1397.  
  1398.     int32_t len = (int32_t)lua_strlen(L, -1);
  1399.     std::string ret(lua_tostring(L, -1), len);
  1400.  
  1401.     lua_pop(L, 1);
  1402.     return ret;
  1403. }
  1404.  
  1405. bool LuaInterface::getGlobalBool(lua_State* L, const std::string& _identifier, bool _default/* = false*/)
  1406. {
  1407.     lua_getglobal(L, _identifier.c_str());
  1408.     if(!lua_isboolean(L, -1))
  1409.     {
  1410.         lua_pop(L, 1);
  1411.         return booleanString(LuaInterface::getGlobalString(L, _identifier, _default ? "yes" : "no"));
  1412.     }
  1413.  
  1414.     bool val = (lua_toboolean(L, -1) != 0);
  1415.     lua_pop(L, 1);
  1416.     return val;
  1417. }
  1418.  
  1419. int64_t LuaInterface::getGlobalNumber(lua_State* L, const std::string& _identifier, const int64_t _default/* = 0*/)
  1420. {
  1421.     return (int64_t)LuaInterface::getGlobalDouble(L, _identifier, _default);
  1422. }
  1423.  
  1424. double LuaInterface::getGlobalDouble(lua_State* L, const std::string& _identifier, const double _default/* = 0*/)
  1425. {
  1426.     lua_getglobal(L, _identifier.c_str());
  1427.     if(!lua_isnumber(L, -1))
  1428.     {
  1429.         lua_pop(L, 1);
  1430.         return _default;
  1431.     }
  1432.  
  1433.     double val = lua_tonumber(L, -1);
  1434.     lua_pop(L, 1);
  1435.     return val;
  1436. }
  1437.  
  1438. void LuaInterface::getValue(const std::string& key, lua_State* L, lua_State* _L)
  1439. {
  1440.     lua_getglobal(L, key.c_str());
  1441.     moveValue(L, _L);
  1442. }
  1443.  
  1444. void LuaInterface::moveValue(lua_State* from, lua_State* to)
  1445. {
  1446.     switch(lua_type(from, -1))
  1447.     {
  1448.         case LUA_TNIL:
  1449.             lua_pushnil(to);
  1450.             break;
  1451.         case LUA_TBOOLEAN:
  1452.             lua_pushboolean(to, lua_toboolean(from, -1));
  1453.             break;
  1454.         case LUA_TNUMBER:
  1455.             lua_pushnumber(to, lua_tonumber(from, -1));
  1456.             break;
  1457.         case LUA_TSTRING:
  1458.         {
  1459.             size_t len;
  1460.             const char* str = lua_tolstring(from, -1, &len);
  1461.  
  1462.             lua_pushlstring(to, str, len);
  1463.             break;
  1464.         }
  1465.         case LUA_TTABLE:
  1466.         {
  1467.             lua_newtable(to);
  1468.             lua_pushnil(from); // First key
  1469.             while(lua_next(from, -2))
  1470.             {
  1471.                 // Move value to the other state
  1472.                 moveValue(from, to); // Value is popped, key is left
  1473.                 // Move key to the other state
  1474.                 lua_pushvalue(from, -1); // Make a copy of the key to use for the next iteration
  1475.                 moveValue(from, to); // Key is in other state.
  1476.                 // We still have the key in the 'from' state ontop of the stack
  1477.  
  1478.                 lua_insert(to, -2); // Move key above value
  1479.                 pushTable(to); // Set the key
  1480.             }
  1481.  
  1482.             break;
  1483.         }
  1484.         default:
  1485.             break;
  1486.     }
  1487.  
  1488.     lua_pop(from, 1); // Pop the value we just read
  1489. }
  1490.  
  1491. void LuaInterface::registerFunctions()
  1492. {
  1493.     //example(...)
  1494.     //lua_register(L, "name", C_function);
  1495.  
  1496.     //getSecureMode(cid)
  1497.     lua_register(m_luaState, "getSecureMode", LuaInterface::luaGetPlayerSecureMode);
  1498.  
  1499.     //getCreatureHealth(cid)
  1500.     lua_register(m_luaState, "getCreatureHealth", LuaInterface::luaGetCreatureHealth);
  1501.  
  1502.     //getCreatureMaxHealth(cid[, ignoreModifiers = false])
  1503.     lua_register(m_luaState, "getCreatureMaxHealth", LuaInterface::luaGetCreatureMaxHealth);
  1504.  
  1505.     //getCreatureMana(cid)
  1506.     lua_register(m_luaState, "getCreatureMana", LuaInterface::luaGetCreatureMana);
  1507.  
  1508.     //getCreatureMaxMana(cid[, ignoreModifiers = false])
  1509.     lua_register(m_luaState, "getCreatureMaxMana", LuaInterface::luaGetCreatureMaxMana);
  1510.  
  1511.     //getCreatureHideHealth(cid)
  1512.     lua_register(m_luaState, "getCreatureHideHealth", LuaInterface::luaGetCreatureHideHealth);
  1513.  
  1514.     //doCreatureSetHideHealth(cid, hide)
  1515.     lua_register(m_luaState, "doCreatureSetHideHealth", LuaInterface::luaDoCreatureSetHideHealth);
  1516.  
  1517.     //getCreatureSpeakType(cid)
  1518.     lua_register(m_luaState, "getCreatureSpeakType", LuaInterface::luaGetCreatureSpeakType);
  1519.  
  1520.     //doCreatureSetSpeakType(cid, type)
  1521.     lua_register(m_luaState, "doCreatureSetSpeakType", LuaInterface::luaDoCreatureSetSpeakType);
  1522.  
  1523.     //getCreatureLookDirection(cid)
  1524.     lua_register(m_luaState, "getCreatureLookDirection", LuaInterface::luaGetCreatureLookDirection);
  1525.  
  1526.     //getPlayerLevel(cid)
  1527.     lua_register(m_luaState, "getPlayerLevel", LuaInterface::luaGetPlayerLevel);
  1528.  
  1529.     //getPlayerExperience(cid)
  1530.     lua_register(m_luaState, "getPlayerExperience", LuaInterface::luaGetPlayerExperience);
  1531.  
  1532.     //getPlayerMagLevel(cid[, ignoreModifiers = false])
  1533.     lua_register(m_luaState, "getPlayerMagLevel", LuaInterface::luaGetPlayerMagLevel);
  1534.  
  1535.     //getPlayerSpentMana(cid)
  1536.     lua_register(m_luaState, "getPlayerSpentMana", LuaInterface::luaGetPlayerSpentMana);
  1537.  
  1538.     //getPlayerFood(cid)
  1539.     lua_register(m_luaState, "getPlayerFood", LuaInterface::luaGetPlayerFood);
  1540.  
  1541.     //getPlayerAccess(cid)
  1542.     lua_register(m_luaState, "getPlayerAccess", LuaInterface::luaGetPlayerAccess);
  1543.  
  1544.     //getPlayerGhostAccess(cid)
  1545.     lua_register(m_luaState, "getPlayerGhostAccess", LuaInterface::luaGetPlayerGhostAccess);
  1546.  
  1547.     //getPlayerSkillLevel(cid, skill[, ignoreModifiers = false])
  1548.     lua_register(m_luaState, "getPlayerSkillLevel", LuaInterface::luaGetPlayerSkillLevel);
  1549.  
  1550.     //getPlayerSkillTries(cid, skill)
  1551.     lua_register(m_luaState, "getPlayerSkillTries", LuaInterface::luaGetPlayerSkillTries);
  1552.  
  1553.     //doPlayerSetOfflineTrainingSkill(cid, skill)
  1554.     lua_register(m_luaState, "doPlayerSetOfflineTrainingSkill", LuaInterface::luaDoPlayerSetOfflineTrainingSkill);
  1555.  
  1556.     //getPlayerTown(cid)
  1557.     lua_register(m_luaState, "getPlayerTown", LuaInterface::luaGetPlayerTown);
  1558.  
  1559.     //getPlayerVocation(cid)
  1560.     lua_register(m_luaState, "getPlayerVocation", LuaInterface::luaGetPlayerVocation);
  1561.  
  1562.     //getPlayerIp(cid)
  1563.     lua_register(m_luaState, "getPlayerIp", LuaInterface::luaGetPlayerIp);
  1564.  
  1565.     //getPlayerRequiredMana(cid, magicLevel)
  1566.     lua_register(m_luaState, "getPlayerRequiredMana", LuaInterface::luaGetPlayerRequiredMana);
  1567.  
  1568.     //getPlayerRequiredSkillTries(cid, skillId, skillLevel)
  1569.     lua_register(m_luaState, "getPlayerRequiredSkillTries", LuaInterface::luaGetPlayerRequiredSkillTries);
  1570.  
  1571.     //getPlayerItemCount(cid, itemid[, subType = -1])
  1572.     lua_register(m_luaState, "getPlayerItemCount", LuaInterface::luaGetPlayerItemCount);
  1573.  
  1574.     //getPlayerMoney(cid)
  1575.     lua_register(m_luaState, "getPlayerMoney", LuaInterface::luaGetPlayerMoney);
  1576.  
  1577.     //getPlayerSoul(cid[, ignoreModifiers = false])
  1578.     lua_register(m_luaState, "getPlayerSoul", LuaInterface::luaGetPlayerSoul);
  1579.  
  1580.     //getPlayerFreeCap(cid)
  1581.     lua_register(m_luaState, "getPlayerFreeCap", LuaInterface::luaGetPlayerFreeCap);
  1582.  
  1583.     //getPlayerLight(cid)
  1584.     lua_register(m_luaState, "getPlayerLight", LuaInterface::luaGetPlayerLight);
  1585.  
  1586.     //getPlayerSlotItem(cid, slot)
  1587.     lua_register(m_luaState, "getPlayerSlotItem", LuaInterface::luaGetPlayerSlotItem);
  1588.  
  1589.     //getPlayerWeapon(cid[, ignoreAmmo = false])
  1590.     lua_register(m_luaState, "getPlayerWeapon", LuaInterface::luaGetPlayerWeapon);
  1591.  
  1592.     //getPlayerItemById(cid, deepSearch, itemId[, subType = -1])
  1593.     lua_register(m_luaState, "getPlayerItemById", LuaInterface::luaGetPlayerItemById);
  1594.  
  1595.     //getPlayerDepotItems(cid, depotid)
  1596.     lua_register(m_luaState, "getPlayerDepotItems", LuaInterface::luaGetPlayerDepotItems);
  1597.  
  1598.     //getPlayerGuildId(cid)
  1599.     lua_register(m_luaState, "getPlayerGuildId", LuaInterface::luaGetPlayerGuildId);
  1600.  
  1601.     //getPlayerGuildName(cid)
  1602.     lua_register(m_luaState, "getPlayerGuildName", LuaInterface::luaGetPlayerGuildName);
  1603.  
  1604.     //getPlayerGuildRankId(cid)
  1605.     lua_register(m_luaState, "getPlayerGuildRankId", LuaInterface::luaGetPlayerGuildRankId);
  1606.  
  1607.     //getPlayerGuildRank(cid)
  1608.     lua_register(m_luaState, "getPlayerGuildRank", LuaInterface::luaGetPlayerGuildRank);
  1609.  
  1610.     //getPlayerGuildNick(cid)
  1611.     lua_register(m_luaState, "getPlayerGuildNick", LuaInterface::luaGetPlayerGuildNick);
  1612.  
  1613.     //getPlayerGuildLevel(cid)
  1614.     lua_register(m_luaState, "getPlayerGuildLevel", LuaInterface::luaGetPlayerGuildLevel);
  1615.  
  1616.     //getPlayerGUID(cid)
  1617.     lua_register(m_luaState, "getPlayerGUID", LuaInterface::luaGetPlayerGUID);
  1618.  
  1619.     //getPlayerNameDescription(cid)
  1620.     lua_register(m_luaState, "getPlayerNameDescription", LuaInterface::luaGetPlayerNameDescription);
  1621.  
  1622.     //doPlayerSetNameDescription(cid, desc)
  1623.     lua_register(m_luaState, "doPlayerSetNameDescription", LuaInterface::luaDoPlayerSetNameDescription);
  1624.  
  1625.     //getPlayerSpecialDescription(cid)
  1626.     lua_register(m_luaState, "getPlayerSpecialDescription", LuaInterface::luaGetPlayerSpecialDescription);
  1627.  
  1628.     //doPlayerSetSpecialDescription(cid, desc)
  1629.     lua_register(m_luaState, "doPlayerSetSpecialDescription", LuaInterface::luaDoPlayerSetSpecialDescription);
  1630.  
  1631.     //getPlayerAccountId(cid)
  1632.     lua_register(m_luaState, "getPlayerAccountId", LuaInterface::luaGetPlayerAccountId);
  1633.  
  1634.     //getPlayerAccount(cid)
  1635.     lua_register(m_luaState, "getPlayerAccount", LuaInterface::luaGetPlayerAccount);
  1636.  
  1637.     //getPlayerFlagValue(cid, flag)
  1638.     lua_register(m_luaState, "getPlayerFlagValue", LuaInterface::luaGetPlayerFlagValue);
  1639.  
  1640.     //getPlayerCustomFlagValue(cid, flag)
  1641.     lua_register(m_luaState, "getPlayerCustomFlagValue", LuaInterface::luaGetPlayerCustomFlagValue);
  1642.  
  1643.     //getPlayerPromotionLevel(cid)
  1644.     lua_register(m_luaState, "getPlayerPromotionLevel", LuaInterface::luaGetPlayerPromotionLevel);
  1645.  
  1646.     //doPlayerSetPromotionLevel(cid, level)
  1647.     lua_register(m_luaState, "doPlayerSetPromotionLevel", LuaInterface::luaDoPlayerSetPromotionLevel);
  1648.  
  1649.     //getPlayerGroupId(cid)
  1650.     lua_register(m_luaState, "getPlayerGroupId", LuaInterface::luaGetPlayerGroupId);
  1651.  
  1652.     //doPlayerSetGroupId(cid, newGroupId)
  1653.     lua_register(m_luaState, "doPlayerSetGroupId", LuaInterface::luaDoPlayerSetGroupId);
  1654.  
  1655.     //doPlayerSendOutfitWindow(cid)
  1656.     lua_register(m_luaState, "doPlayerSendOutfitWindow", LuaInterface::luaDoPlayerSendOutfitWindow);
  1657.  
  1658.     //doPlayerLearnInstantSpell(cid, name)
  1659.     lua_register(m_luaState, "doPlayerLearnInstantSpell", LuaInterface::luaDoPlayerLearnInstantSpell);
  1660.  
  1661.     //doPlayerUnlearnInstantSpell(cid, name)
  1662.     lua_register(m_luaState, "doPlayerUnlearnInstantSpell", LuaInterface::luaDoPlayerUnlearnInstantSpell);
  1663.  
  1664.     //getPlayerLearnedInstantSpell(cid, name)
  1665.     lua_register(m_luaState, "getPlayerLearnedInstantSpell", LuaInterface::luaGetPlayerLearnedInstantSpell);
  1666.  
  1667.     //getPlayerInstantSpellCount(cid)
  1668.     lua_register(m_luaState, "getPlayerInstantSpellCount", LuaInterface::luaGetPlayerInstantSpellCount);
  1669.  
  1670.     //getPlayerInstantSpellInfo(cid, index)
  1671.     lua_register(m_luaState, "getPlayerInstantSpellInfo", LuaInterface::luaGetPlayerInstantSpellInfo);
  1672.  
  1673.     //getInstantSpellInfo(cid, name)
  1674.     lua_register(m_luaState, "getInstantSpellInfo", LuaInterface::luaGetInstantSpellInfo);
  1675.  
  1676.     //doCreatureCastSpell(uid, spell)
  1677.     lua_register(m_luaState, "doCreatureCastSpell", LuaInterface::luaDoCreatureCastSpell);
  1678.  
  1679.     //getCreatureStorageList(cid)
  1680.     lua_register(m_luaState, "getCreatureStorageList", LuaInterface::luaGetCreatureStorageList);
  1681.  
  1682.     //getCreatureStorage(uid, key)
  1683.     lua_register(m_luaState, "getCreatureStorage", LuaInterface::luaGetCreatureStorage);
  1684.  
  1685.     //doCreatureSetStorage(uid, key, value)
  1686.     lua_register(m_luaState, "doCreatureSetStorage", LuaInterface::luaDoCreatureSetStorage);
  1687.  
  1688.     //getPlayerSpectators(cid)
  1689.     lua_register(m_luaState, "getPlayerSpectators", LuaInterface::luaGetPlayerSpectators);
  1690.  
  1691.     //doPlayerSetSpectators(cid, data)
  1692.     lua_register(m_luaState, "doPlayerSetSpectators", LuaInterface::luaDoPlayerSetSpectators);
  1693.  
  1694.     //getStorageList()
  1695.     lua_register(m_luaState, "getStorageList", LuaInterface::luaGetStorageList);
  1696.  
  1697.     //getStorage(key)
  1698.     lua_register(m_luaState, "getStorage", LuaInterface::luaGetStorage);
  1699.  
  1700.     //doSetStorage(key, value)
  1701.     lua_register(m_luaState, "doSetStorage", LuaInterface::luaDoSetStorage);
  1702.  
  1703.     //getChannelUsers(channelId)
  1704.     lua_register(m_luaState, "getChannelUsers", LuaInterface::luaGetChannelUsers);
  1705.  
  1706.     //getPlayersOnline()
  1707.     lua_register(m_luaState, "getPlayersOnline", LuaInterface::luaGetPlayersOnline);
  1708.  
  1709.     //getTileInfo(pos)
  1710.     lua_register(m_luaState, "getTileInfo", LuaInterface::luaGetTileInfo);
  1711.  
  1712.     //getThingFromPosition(pos)
  1713.     lua_register(m_luaState, "getThingFromPosition", LuaInterface::luaGetThingFromPosition);
  1714.  
  1715.     //getThing(uid[, recursive = RECURSE_FIRST])
  1716.     lua_register(m_luaState, "getThing", LuaInterface::luaGetThing);
  1717.  
  1718.     //doTileQueryAdd(uid, pos[, flags])
  1719.     lua_register(m_luaState, "doTileQueryAdd", LuaInterface::luaDoTileQueryAdd);
  1720.  
  1721.     //doItemRaidUnref(uid)
  1722.     lua_register(m_luaState, "doItemRaidUnref", LuaInterface::luaDoItemRaidUnref);
  1723.  
  1724.     //getThingPosition(uid)
  1725.     lua_register(m_luaState, "getThingPosition", LuaInterface::luaGetThingPosition);
  1726.  
  1727.     //getTileItemById(pos, itemId[, subType = -1])
  1728.     lua_register(m_luaState, "getTileItemById", LuaInterface::luaGetTileItemById);
  1729.  
  1730.     //getTileItemByType(pos, type)
  1731.     lua_register(m_luaState, "getTileItemByType", LuaInterface::luaGetTileItemByType);
  1732.  
  1733.     //getTopCreature(pos)
  1734.     lua_register(m_luaState, "getTopCreature", LuaInterface::luaGetTopCreature);
  1735.  
  1736.     //doRemoveItem(uid[, count = -1])
  1737.     lua_register(m_luaState, "doRemoveItem", LuaInterface::luaDoRemoveItem);
  1738.  
  1739.     //doPlayerFeed(cid, food)
  1740.     lua_register(m_luaState, "doPlayerFeed", LuaInterface::luaDoPlayerFeed);
  1741.  
  1742.     //doPlayerSendCancel(cid, text)
  1743.     lua_register(m_luaState, "doPlayerSendCancel", LuaInterface::luaDoPlayerSendCancel);
  1744.  
  1745.     //doPlayerSendDefaultCancel(cid, ReturnValue)
  1746.     lua_register(m_luaState, "doPlayerSendDefaultCancel", LuaInterface::luaDoSendDefaultCancel);
  1747.  
  1748.     //getSearchString(fromPosition, toPosition[, fromIsCreature = false[, toIsCreature = false]])
  1749.     lua_register(m_luaState, "getSearchString", LuaInterface::luaGetSearchString);
  1750.  
  1751.     //getClosestFreeTile(cid, targetpos[, extended = false[, ignoreHouse = true]])
  1752.     lua_register(m_luaState, "getClosestFreeTile", LuaInterface::luaGetClosestFreeTile);
  1753.  
  1754.     //doTeleportThing(cid, destination[, pushmove = true[, fullTeleport = true]])
  1755.     lua_register(m_luaState, "doTeleportThing", LuaInterface::luaDoTeleportThing);
  1756.  
  1757.     //doItemSetDestination(uid, destination)
  1758.     lua_register(m_luaState, "doItemSetDestination", LuaInterface::luaDoItemSetDestination);
  1759.  
  1760.     //doTransformItem(uid, newId[, count/subType])
  1761.     lua_register(m_luaState, "doTransformItem", LuaInterface::luaDoTransformItem);
  1762.  
  1763.     //doCreatureSay(uid, text[, type = SPEAK_SAY[, ghost = false[, cid = 0[, pos]]]])
  1764.     lua_register(m_luaState, "doCreatureSay", LuaInterface::luaDoCreatureSay);
  1765.  
  1766.     //doSendCreatureSquare(cid, color[, player])
  1767.     lua_register(m_luaState, "doSendCreatureSquare", LuaInterface::luaDoSendCreatureSquare);
  1768.  
  1769.     //doSendMagicEffect(pos, type[, player])
  1770.     lua_register(m_luaState, "doSendMagicEffect", LuaInterface::luaDoSendMagicEffect);
  1771.  
  1772.     //doSendDistanceShoot(fromPos, toPos, type[, player])
  1773.     lua_register(m_luaState, "doSendDistanceShoot", LuaInterface::luaDoSendDistanceShoot);
  1774.  
  1775.     //doSendAnimatedText(pos, text, color[, player])
  1776.     lua_register(m_luaState, "doSendAnimatedText", LuaInterface::luaDoSendAnimatedText);
  1777.  
  1778.     //doPlayerAddSkillTry(cid, skillid, n[, useMultiplier = true])
  1779.     lua_register(m_luaState, "doPlayerAddSkillTry", LuaInterface::luaDoPlayerAddSkillTry);
  1780.  
  1781.     //doCreatureAddHealth(cid, health[, hitEffect[, hitColor[, force]]])
  1782.     lua_register(m_luaState, "doCreatureAddHealth", LuaInterface::luaDoCreatureAddHealth);
  1783.  
  1784.     //doCreatureAddMana(cid, mana)
  1785.     lua_register(m_luaState, "doCreatureAddMana", LuaInterface::luaDoCreatureAddMana);
  1786.  
  1787.     //setCreatureMaxHealth(cid, health)
  1788.     lua_register(m_luaState, "setCreatureMaxHealth", LuaInterface::luaSetCreatureMaxHealth);
  1789.  
  1790.     //setCreatureMaxMana(cid, mana)
  1791.     lua_register(m_luaState, "setCreatureMaxMana", LuaInterface::luaSetCreatureMaxMana);
  1792.  
  1793.     //doPlayerSetMaxCapacity(cid, cap)
  1794.     lua_register(m_luaState, "doPlayerSetMaxCapacity", LuaInterface::luaDoPlayerSetMaxCapacity);
  1795.  
  1796.     //doPlayerAddSpentMana(cid, amount[, useMultiplier = true])
  1797.     lua_register(m_luaState, "doPlayerAddSpentMana", LuaInterface::luaDoPlayerAddSpentMana);
  1798.  
  1799.     //doPlayerAddSoul(cid, amount)
  1800.     lua_register(m_luaState, "doPlayerAddSoul", LuaInterface::luaDoPlayerAddSoul);
  1801.  
  1802.     //doPlayerSetExtraAttackSpeed(cid, speed)
  1803.     lua_register(m_luaState, "doPlayerSetExtraAttackSpeed", LuaInterface::luaDoPlayerSetExtraAttackSpeed);
  1804.  
  1805.     //doPlayerAddItem(cid, itemid[, count/subtype = 1[, canDropOnMap = true[, slot = 0]]])
  1806.     //doPlayerAddItem(cid, itemid[, count = 1[, canDropOnMap = true[, subtype = 1[, slot = 0]]]])
  1807.     //Returns uid of the created item
  1808.     lua_register(m_luaState, "doPlayerAddItem", LuaInterface::luaDoPlayerAddItem);
  1809.  
  1810.     //doPlayerAddItemEx(cid, uid[, canDropOnMap = false[, slot = 0]])
  1811.     lua_register(m_luaState, "doPlayerAddItemEx", LuaInterface::luaDoPlayerAddItemEx);
  1812.  
  1813.     //doPlayerSendTextMessage(cid, MessageClasses, message[, value[, color[, position]]])
  1814.     lua_register(m_luaState, "doPlayerSendTextMessage", LuaInterface::luaDoPlayerSendTextMessage);
  1815.  
  1816.     //doPlayerSendChannelMessage(cid, author, message, MessageClasses, channel)
  1817.     lua_register(m_luaState, "doPlayerSendChannelMessage", LuaInterface::luaDoPlayerSendChannelMessage);
  1818.  
  1819.     //doCreatureChannelSay(cid, targetId, MessageClasses, message, channel[, time])
  1820.     lua_register(m_luaState, "doCreatureChannelSay", LuaInterface::luaDoCreatureChannelSay);
  1821.  
  1822.     //doPlayerOpenChannel(cid, channelId)
  1823.     lua_register(m_luaState, "doPlayerOpenChannel", LuaInterface::luaDoPlayerOpenChannel);
  1824.  
  1825.     //doPlayerSendChannels(cid[, list])
  1826.     lua_register(m_luaState, "doPlayerSendChannels", LuaInterface::luaDoPlayerSendChannels);
  1827.  
  1828.     //doPlayerAddMoney(cid, money)
  1829.     lua_register(m_luaState, "doPlayerAddMoney", LuaInterface::luaDoPlayerAddMoney);
  1830.  
  1831.     //doPlayerRemoveMoney(cid, money [, canDrop = true])
  1832.     lua_register(m_luaState, "doPlayerRemoveMoney", LuaInterface::luaDoPlayerRemoveMoney);
  1833.  
  1834.     //doPlayerTransferMoneyTo(cid, target, money)
  1835.     lua_register(m_luaState, "doPlayerTransferMoneyTo", LuaInterface::luaDoPlayerTransferMoneyTo);
  1836.  
  1837.     //doShowTextDialog(cid, itemid[, (text/canWrite)[, (canWrite/length)[, length]]])
  1838.     lua_register(m_luaState, "doShowTextDialog", LuaInterface::luaDoShowTextDialog);
  1839.  
  1840.     //doDecayItem(uid)
  1841.     lua_register(m_luaState, "doDecayItem", LuaInterface::luaDoDecayItem);
  1842.  
  1843.     //doCreateItem(itemid[, type/count], pos)
  1844.     //Returns uid of the created item, only works on tiles.
  1845.     lua_register(m_luaState, "doCreateItem", LuaInterface::luaDoCreateItem);
  1846.  
  1847.     //doCreateItemEx(itemid[, count/subType = -1])
  1848.     lua_register(m_luaState, "doCreateItemEx", LuaInterface::luaDoCreateItemEx);
  1849.  
  1850.     //doTileAddItemEx(pos, uid)
  1851.     lua_register(m_luaState, "doTileAddItemEx", LuaInterface::luaDoTileAddItemEx);
  1852.  
  1853.     //doAddContainerItemEx(uid, virtuid)
  1854.     lua_register(m_luaState, "doAddContainerItemEx", LuaInterface::luaDoAddContainerItemEx);
  1855.  
  1856.     //doRelocate(pos, posTo[, creatures = true[, unmovable = true]])
  1857.     //Moves all movable objects from pos to posTo
  1858.     lua_register(m_luaState, "doRelocate", LuaInterface::luaDoRelocate);
  1859.  
  1860.     //doCleanTile(pos[, forceMapLoaded = false])
  1861.     lua_register(m_luaState, "doCleanTile", LuaInterface::luaDoCleanTile);
  1862.  
  1863.     //doCreateTeleport(itemId, destination, position)
  1864.     lua_register(m_luaState, "doCreateTeleport", LuaInterface::luaDoCreateTeleport);
  1865.  
  1866.     //doCreateMonster(name, pos[, extend = false[, force = false]])
  1867.     lua_register(m_luaState, "doCreateMonster", LuaInterface::luaDoCreateMonster);
  1868.  
  1869.     //doCreateNpc(name, pos)
  1870.     lua_register(m_luaState, "doCreateNpc", LuaInterface::luaDoCreateNpc);
  1871.  
  1872.     //doSummonMonster(cid, name)
  1873.     lua_register(m_luaState, "doSummonMonster", LuaInterface::luaDoSummonMonster);
  1874.  
  1875.     //doConvinceCreature(cid, target)
  1876.     lua_register(m_luaState, "doConvinceCreature", LuaInterface::luaDoConvinceCreature);
  1877.  
  1878.     //getMonsterTargetList(cid)
  1879.     lua_register(m_luaState, "getMonsterTargetList", LuaInterface::luaGetMonsterTargetList);
  1880.  
  1881.     //getMonsterFriendList(cid)
  1882.     lua_register(m_luaState, "getMonsterFriendList", LuaInterface::luaGetMonsterFriendList);
  1883.  
  1884.     //doMonsterSetTarget(cid, target)
  1885.     lua_register(m_luaState, "doMonsterSetTarget", LuaInterface::luaDoMonsterSetTarget);
  1886.  
  1887.     //doMonsterChangeTarget(cid)
  1888.     lua_register(m_luaState, "doMonsterChangeTarget", LuaInterface::luaDoMonsterChangeTarget);
  1889.  
  1890.     //getMonsterInfo(name)
  1891.     lua_register(m_luaState, "getMonsterInfo", LuaInterface::luaGetMonsterInfo);
  1892.  
  1893.     //doAddCondition(cid, condition)
  1894.     lua_register(m_luaState, "doAddCondition", LuaInterface::luaDoAddCondition);
  1895.  
  1896.     //doRemoveCondition(cid, type[, subId = 0])
  1897.     lua_register(m_luaState, "doRemoveCondition", LuaInterface::luaDoRemoveCondition);
  1898.  
  1899.     //doRemoveConditions(cid[, onlyPersistent])
  1900.     lua_register(m_luaState, "doRemoveConditions", LuaInterface::luaDoRemoveConditions);
  1901.  
  1902.     //doRemoveCreature(cid[, forceLogout = true])
  1903.     lua_register(m_luaState, "doRemoveCreature", LuaInterface::luaDoRemoveCreature);
  1904.  
  1905.     //doMoveCreature(cid, direction[, flag = FLAG_NOLIMIT])
  1906.     lua_register(m_luaState, "doMoveCreature", LuaInterface::luaDoMoveCreature);
  1907.  
  1908.     //doSteerCreature(cid, position[, maxNodes])
  1909.     lua_register(m_luaState, "doSteerCreature", LuaInterface::luaDoSteerCreature);
  1910.  
  1911.     //doPlayerSetPzLocked(cid, locked)
  1912.     lua_register(m_luaState, "doPlayerSetPzLocked", LuaInterface::luaDoPlayerSetPzLocked);
  1913.  
  1914.     //doPlayerSetTown(cid, townid)
  1915.     lua_register(m_luaState, "doPlayerSetTown", LuaInterface::luaDoPlayerSetTown);
  1916.  
  1917.     //doPlayerSetVocation(cid,voc)
  1918.     lua_register(m_luaState, "doPlayerSetVocation", LuaInterface::luaDoPlayerSetVocation);
  1919.  
  1920.     //doPlayerRemoveItem(cid, itemid[, count[, subType = -1[, ignoreEquipped = false]]])
  1921.     lua_register(m_luaState, "doPlayerRemoveItem", LuaInterface::luaDoPlayerRemoveItem);
  1922.  
  1923.     //doPlayerAddExperience(cid, amount)
  1924.     lua_register(m_luaState, "doPlayerAddExperience", LuaInterface::luaDoPlayerAddExperience);
  1925.  
  1926.     //doPlayerSetGuildId(cid, id)
  1927.     lua_register(m_luaState, "doPlayerSetGuildId", LuaInterface::luaDoPlayerSetGuildId);
  1928.  
  1929.     //doPlayerSetGuildLevel(cid, level[, rank])
  1930.     lua_register(m_luaState, "doPlayerSetGuildLevel", LuaInterface::luaDoPlayerSetGuildLevel);
  1931.  
  1932.     //doPlayerSetGuildNick(cid, nick)
  1933.     lua_register(m_luaState, "doPlayerSetGuildNick", LuaInterface::luaDoPlayerSetGuildNick);
  1934.  
  1935.     //doPlayerAddOutfit(cid, looktype, addon)
  1936.     lua_register(m_luaState, "doPlayerAddOutfit", LuaInterface::luaDoPlayerAddOutfit);
  1937.  
  1938.     //doPlayerRemoveOutfit(cid, looktype[, addon = 0])
  1939.     lua_register(m_luaState, "doPlayerRemoveOutfit", LuaInterface::luaDoPlayerRemoveOutfit);
  1940.  
  1941.     //doPlayerAddOutfitId(cid, outfitId, addon)
  1942.     lua_register(m_luaState, "doPlayerAddOutfitId", LuaInterface::luaDoPlayerAddOutfitId);
  1943.  
  1944.     //doPlayerRemoveOutfitId(cid, outfitId[, addon = 0])
  1945.     lua_register(m_luaState, "doPlayerRemoveOutfitId", LuaInterface::luaDoPlayerRemoveOutfitId);
  1946.  
  1947.     //canPlayerWearOutfit(cid, looktype[, addon = 0])
  1948.     lua_register(m_luaState, "canPlayerWearOutfit", LuaInterface::luaCanPlayerWearOutfit);
  1949.  
  1950.     //canPlayerWearOutfitId(cid, outfitId[, addon = 0])
  1951.     lua_register(m_luaState, "canPlayerWearOutfitId", LuaInterface::luaCanPlayerWearOutfitId);
  1952.  
  1953.     //hasCreatureCondition(cid, condition[, subId = 0[, conditionId = (both)]])
  1954.     lua_register(m_luaState, "hasCreatureCondition", LuaInterface::luaHasCreatureCondition);
  1955.  
  1956.     //getCreatureConditionInfo(cid, condition[, subId = 0[, conditionId = CONDITIONID_COMBAT]])
  1957.     lua_register(m_luaState, "getCreatureConditionInfo", LuaInterface::luaGetCreatureConditionInfo);
  1958.  
  1959.     //doCreatureSetDropLoot(cid, doDrop)
  1960.     lua_register(m_luaState, "doCreatureSetDropLoot", LuaInterface::luaDoCreatureSetDropLoot);
  1961.  
  1962.     //getPlayerLossPercent(cid, lossType)
  1963.     lua_register(m_luaState, "getPlayerLossPercent", LuaInterface::luaGetPlayerLossPercent);
  1964.  
  1965.     //doPlayerSetLossPercent(cid, lossType, newPercent)
  1966.     lua_register(m_luaState, "doPlayerSetLossPercent", LuaInterface::luaDoPlayerSetLossPercent);
  1967.  
  1968.     //doPlayerSetLossSkill(cid, doLose)
  1969.     lua_register(m_luaState, "doPlayerSetLossSkill", LuaInterface::luaDoPlayerSetLossSkill);
  1970.  
  1971.     //getPlayerLossSkill(cid)
  1972.     lua_register(m_luaState, "getPlayerLossSkill", LuaInterface::luaGetPlayerLossSkill);
  1973.  
  1974.     //doPlayerSwitchSaving(cid)
  1975.     lua_register(m_luaState, "doPlayerSwitchSaving", LuaInterface::luaDoPlayerSwitchSaving);
  1976.  
  1977.     //doPlayerSave(cid[, shallow = false])
  1978.     lua_register(m_luaState, "doPlayerSave", LuaInterface::luaDoPlayerSave);
  1979.  
  1980.     //doPlayerSaveItems(cid)
  1981.     lua_register(m_luaState, "doPlayerSaveItems", LuaInterface::luaDoPlayerSaveItems);
  1982.  
  1983.     //isPlayerPzLocked(cid)
  1984.     lua_register(m_luaState, "isPlayerPzLocked", LuaInterface::luaIsPlayerPzLocked);
  1985.  
  1986.     //isPlayerSaving(cid)
  1987.     lua_register(m_luaState, "isPlayerSaving", LuaInterface::luaIsPlayerSaving);
  1988.  
  1989.     //isPlayerProtected(cid)
  1990.     lua_register(m_luaState, "isPlayerProtected", LuaInterface::luaIsPlayerProtected);
  1991.  
  1992.     //isCreature(cid)
  1993.     lua_register(m_luaState, "isCreature", LuaInterface::luaIsCreature);
  1994.  
  1995.     //isMovable(uid)
  1996.     lua_register(m_luaState, "isMovable", LuaInterface::luaIsMovable);
  1997.  
  1998.     //getCreatureByName(name)
  1999.     lua_register(m_luaState, "getCreatureByName", LuaInterface::luaGetCreatureByName);
  2000.  
  2001.     //getPlayerByGUID(guid)
  2002.     lua_register(m_luaState, "getPlayerByGUID", LuaInterface::luaGetPlayerByGUID);
  2003.  
  2004.     //getPlayerByNameWildcard(name~[, ret = false])
  2005.     lua_register(m_luaState, "getPlayerByNameWildcard", LuaInterface::luaGetPlayerByNameWildcard);
  2006.  
  2007.     //getPlayerGUIDByName(name[, multiworld = false])
  2008.     lua_register(m_luaState, "getPlayerGUIDByName", LuaInterface::luaGetPlayerGUIDByName);
  2009.  
  2010.     //getPlayerNameByGUID(guid[, multiworld = false])
  2011.     lua_register(m_luaState, "getPlayerNameByGUID", LuaInterface::luaGetPlayerNameByGUID);
  2012.  
  2013.     //doPlayerChangeName(guid, oldName, newName)
  2014.     lua_register(m_luaState, "doPlayerChangeName", LuaInterface::luaDoPlayerChangeName);
  2015.  
  2016.     //registerCreatureEvent(uid, name)
  2017.     lua_register(m_luaState, "registerCreatureEvent", LuaInterface::luaRegisterCreatureEvent);
  2018.  
  2019.     //unregisterCreatureEvent(uid, name)
  2020.     lua_register(m_luaState, "unregisterCreatureEvent", LuaInterface::luaUnregisterCreatureEvent);
  2021.  
  2022.     //unregisterCreatureEventType(uid, type)
  2023.     lua_register(m_luaState, "unregisterCreatureEventType", LuaInterface::luaUnregisterCreatureEventType);
  2024.  
  2025.     //getContainerSize(uid)
  2026.     lua_register(m_luaState, "getContainerSize", LuaInterface::luaGetContainerSize);
  2027.  
  2028.     //getContainerCap(uid)
  2029.     lua_register(m_luaState, "getContainerCap", LuaInterface::luaGetContainerCap);
  2030.  
  2031.     //getContainerItem(uid, slot)
  2032.     lua_register(m_luaState, "getContainerItem", LuaInterface::luaGetContainerItem);
  2033.  
  2034.     //doAddContainerItem(uid, itemid[, count/subType = 1])
  2035.     lua_register(m_luaState, "doAddContainerItem", LuaInterface::luaDoAddContainerItem);
  2036.  
  2037.     //getHouseInfo(houseId[, full = true])
  2038.     lua_register(m_luaState, "getHouseInfo", LuaInterface::luaGetHouseInfo);
  2039.  
  2040.     //getHouseAccessList(houseid, listId)
  2041.     lua_register(m_luaState, "getHouseAccessList", LuaInterface::luaGetHouseAccessList);
  2042.  
  2043.     //getHouseByPlayerGUID(playerGUID)
  2044.     lua_register(m_luaState, "getHouseByPlayerGUID", LuaInterface::luaGetHouseByPlayerGUID);
  2045.  
  2046.     //getHouseFromPosition(pos)
  2047.     lua_register(m_luaState, "getHouseFromPosition", LuaInterface::luaGetHouseFromPosition);
  2048.  
  2049.     //setHouseAccessList(houseid, listid, listtext)
  2050.     lua_register(m_luaState, "setHouseAccessList", LuaInterface::luaSetHouseAccessList);
  2051.  
  2052.     //setHouseOwner(houseId, owner[, clean = true])
  2053.     lua_register(m_luaState, "setHouseOwner", LuaInterface::luaSetHouseOwner);
  2054.  
  2055.     //getWorldType()
  2056.     lua_register(m_luaState, "getWorldType", LuaInterface::luaGetWorldType);
  2057.  
  2058.     //setWorldType(type)
  2059.     lua_register(m_luaState, "setWorldType", LuaInterface::luaSetWorldType);
  2060.  
  2061.     //getWorldTime()
  2062.     lua_register(m_luaState, "getWorldTime", LuaInterface::luaGetWorldTime);
  2063.  
  2064.     //getWorldLight()
  2065.     lua_register(m_luaState, "getWorldLight", LuaInterface::luaGetWorldLight);
  2066.  
  2067.     //getWorldCreatures(type)
  2068.     //0 players, 1 monsters, 2 npcs, 3 all
  2069.     lua_register(m_luaState, "getWorldCreatures", LuaInterface::luaGetWorldCreatures);
  2070.  
  2071.     //getWorldUpTime()
  2072.     lua_register(m_luaState, "getWorldUpTime", LuaInterface::luaGetWorldUpTime);
  2073.  
  2074.     //getGuildId(guildName)
  2075.     lua_register(m_luaState, "getGuildId", LuaInterface::luaGetGuildId);
  2076.  
  2077.     //getGuildMotd(guildId)
  2078.     lua_register(m_luaState, "getGuildMotd", LuaInterface::luaGetGuildMotd);
  2079.  
  2080.     //getPlayerSex(cid[, full = false])
  2081.     lua_register(m_luaState, "getPlayerSex", LuaInterface::luaGetPlayerSex);
  2082.  
  2083.     //doPlayerSetSex(cid, newSex)
  2084.     lua_register(m_luaState, "doPlayerSetSex", LuaInterface::luaDoPlayerSetSex);
  2085.  
  2086.     //createCombatArea({area}[, {extArea}])
  2087.     lua_register(m_luaState, "createCombatArea", LuaInterface::luaCreateCombatArea);
  2088.  
  2089.     //createConditionObject(type[, ticks = 0[, buff = false[, subId = 0[, conditionId = CONDITIONID_COMBAT]]]])
  2090.     lua_register(m_luaState, "createConditionObject", LuaInterface::luaCreateConditionObject);
  2091.  
  2092.     //setCombatArea(combat, area)
  2093.     lua_register(m_luaState, "setCombatArea", LuaInterface::luaSetCombatArea);
  2094.  
  2095.     //setCombatCondition(combat, condition)
  2096.     lua_register(m_luaState, "setCombatCondition", LuaInterface::luaSetCombatCondition);
  2097.  
  2098.     //setCombatParam(combat, key, value)
  2099.     lua_register(m_luaState, "setCombatParam", LuaInterface::luaSetCombatParam);
  2100.  
  2101.     //setConditionParam(condition, key, value)
  2102.     lua_register(m_luaState, "setConditionParam", LuaInterface::luaSetConditionParam);
  2103.  
  2104.     //addDamageCondition(condition, rounds, time, value)
  2105.     lua_register(m_luaState, "addDamageCondition", LuaInterface::luaAddDamageCondition);
  2106.  
  2107.     //addOutfitCondition(condition, outfit)
  2108.     lua_register(m_luaState, "addOutfitCondition", LuaInterface::luaAddOutfitCondition);
  2109.  
  2110.     //setCombatCallBack(combat, key, function_name)
  2111.     lua_register(m_luaState, "setCombatCallback", LuaInterface::luaSetCombatCallBack);
  2112.  
  2113.     //setCombatFormula(combat, type, mina, minb, maxa, maxb[, minl, maxl[, minm, maxm[, minc[, maxc]]]])
  2114.     lua_register(m_luaState, "setCombatFormula", LuaInterface::luaSetCombatFormula);
  2115.  
  2116.     //setConditionFormula(combat, mina, minb, maxa, maxb)
  2117.     lua_register(m_luaState, "setConditionFormula", LuaInterface::luaSetConditionFormula);
  2118.  
  2119.     //doCombat(cid, combat, param)
  2120.     lua_register(m_luaState, "doCombat", LuaInterface::luaDoCombat);
  2121.  
  2122.     //createCombatObject()
  2123.     lua_register(m_luaState, "createCombatObject", LuaInterface::luaCreateCombatObject);
  2124.  
  2125.     //doCombatAreaHealth(cid, type, pos, area, min, max, effect)
  2126.     lua_register(m_luaState, "doCombatAreaHealth", LuaInterface::luaDoCombatAreaHealth);
  2127.  
  2128.     //doTargetCombatHealth(cid, target, type, min, max, effect)
  2129.     lua_register(m_luaState, "doTargetCombatHealth", LuaInterface::luaDoTargetCombatHealth);
  2130.  
  2131.     //doCombatAreaMana(cid, pos, area, min, max, effect)
  2132.     lua_register(m_luaState, "doCombatAreaMana", LuaInterface::luaDoCombatAreaMana);
  2133.  
  2134.     //doTargetCombatMana(cid, target, min, max, effect)
  2135.     lua_register(m_luaState, "doTargetCombatMana", LuaInterface::luaDoTargetCombatMana);
  2136.  
  2137.     //doCombatAreaCondition(cid, pos, area, condition, effect)
  2138.     lua_register(m_luaState, "doCombatAreaCondition", LuaInterface::luaDoCombatAreaCondition);
  2139.  
  2140.     //doTargetCombatCondition(cid, target, condition, effect)
  2141.     lua_register(m_luaState, "doTargetCombatCondition", LuaInterface::luaDoTargetCombatCondition);
  2142.  
  2143.     //doCombatAreaDispel(cid, pos, area, type, effect)
  2144.     lua_register(m_luaState, "doCombatAreaDispel", LuaInterface::luaDoCombatAreaDispel);
  2145.  
  2146.     //doTargetCombatDispel(cid, target, type, effect)
  2147.     lua_register(m_luaState, "doTargetCombatDispel", LuaInterface::luaDoTargetCombatDispel);
  2148.  
  2149.     //doChallengeCreature(cid, target)
  2150.     lua_register(m_luaState, "doChallengeCreature", LuaInterface::luaDoChallengeCreature);
  2151.  
  2152.     //numberToVariant(number)
  2153.     lua_register(m_luaState, "numberToVariant", LuaInterface::luaNumberToVariant);
  2154.  
  2155.     //stringToVariant(string)
  2156.     lua_register(m_luaState, "stringToVariant", LuaInterface::luaStringToVariant);
  2157.  
  2158.     //positionToVariant(pos)
  2159.     lua_register(m_luaState, "positionToVariant", LuaInterface::luaPositionToVariant);
  2160.  
  2161.     //targetPositionToVariant(pos)
  2162.     lua_register(m_luaState, "targetPositionToVariant", LuaInterface::luaTargetPositionToVariant);
  2163.  
  2164.     //variantToNumber(var)
  2165.     lua_register(m_luaState, "variantToNumber", LuaInterface::luaVariantToNumber);
  2166.  
  2167.     //variantToString(var)
  2168.     lua_register(m_luaState, "variantToString", LuaInterface::luaVariantToString);
  2169.  
  2170.     //variantToPosition(var)
  2171.     lua_register(m_luaState, "variantToPosition", LuaInterface::luaVariantToPosition);
  2172.  
  2173.     //doChangeSpeed(cid, delta)
  2174.     lua_register(m_luaState, "doChangeSpeed", LuaInterface::luaDoChangeSpeed);
  2175.  
  2176.     //doCreatureChangeOutfit(cid, outfit)
  2177.     lua_register(m_luaState, "doCreatureChangeOutfit", LuaInterface::luaDoCreatureChangeOutfit);
  2178.  
  2179.     //doSetMonsterOutfit(cid, name[, time = -1])
  2180.     lua_register(m_luaState, "doSetMonsterOutfit", LuaInterface::luaSetMonsterOutfit);
  2181.  
  2182.     //doSetItemOutfit(cid, item[, time = -1])
  2183.     lua_register(m_luaState, "doSetItemOutfit", LuaInterface::luaSetItemOutfit);
  2184.  
  2185.     //doSetCreatureOutfit(cid, outfit[, time = -1])
  2186.     lua_register(m_luaState, "doSetCreatureOutfit", LuaInterface::luaSetCreatureOutfit);
  2187.  
  2188.     //getCreatureOutfit(cid)
  2189.     lua_register(m_luaState, "getCreatureOutfit", LuaInterface::luaGetCreatureOutfit);
  2190.  
  2191.     //getCreatureLastPosition(cid)
  2192.     lua_register(m_luaState, "getCreatureLastPosition", LuaInterface::luaGetCreatureLastPosition);
  2193.  
  2194.     //getCreatureName(cid)
  2195.     lua_register(m_luaState, "getCreatureName", LuaInterface::luaGetCreatureName);
  2196.  
  2197.     //getCreatureSpeed(cid)
  2198.     lua_register(m_luaState, "getCreatureSpeed", LuaInterface::luaGetCreatureSpeed);
  2199.  
  2200.     //getCreatureBaseSpeed(cid)
  2201.     lua_register(m_luaState, "getCreatureBaseSpeed", LuaInterface::luaGetCreatureBaseSpeed);
  2202.  
  2203.     //getCreatureTarget(cid)
  2204.     lua_register(m_luaState, "getCreatureTarget", LuaInterface::luaGetCreatureTarget);
  2205.  
  2206.     //isSightClear(fromPos, toPos, floorCheck)
  2207.     lua_register(m_luaState, "isSightClear", LuaInterface::luaIsSightClear);
  2208.  
  2209.     //addEvent(callback, delay, ...)
  2210.     lua_register(m_luaState, "addEvent", LuaInterface::luaAddEvent);
  2211.  
  2212.     //stopEvent(eventid)
  2213.     lua_register(m_luaState, "stopEvent", LuaInterface::luaStopEvent);
  2214.  
  2215.     //getPlayersByAccountId(accId)
  2216.     lua_register(m_luaState, "getPlayersByAccountId", LuaInterface::luaGetPlayersByAccountId);
  2217.  
  2218.     //getAccountIdByName(name)
  2219.     lua_register(m_luaState, "getAccountIdByName", LuaInterface::luaGetAccountIdByName);
  2220.  
  2221.     //getAccountByName(name)
  2222.     lua_register(m_luaState, "getAccountByName", LuaInterface::luaGetAccountByName);
  2223.  
  2224.     //getAccountIdByAccount(accName)
  2225.     lua_register(m_luaState, "getAccountIdByAccount", LuaInterface::luaGetAccountIdByAccount);
  2226.  
  2227.     //getAccountByAccountId(accId)
  2228.     lua_register(m_luaState, "getAccountByAccountId", LuaInterface::luaGetAccountByAccountId);
  2229.  
  2230.     //getAccountFlagValue(name/id)
  2231.     lua_register(m_luaState, "getAccountFlagValue", LuaInterface::luaGetAccountFlagValue);
  2232.  
  2233.     //getAccountCustomFlagValue(name/id)
  2234.     lua_register(m_luaState, "getAccountCustomFlagValue", LuaInterface::luaGetAccountCustomFlagValue);
  2235.  
  2236.     //getIpByName(name)
  2237.     lua_register(m_luaState, "getIpByName", LuaInterface::luaGetIpByName);
  2238.  
  2239.     //getPlayersByIp(ip[, mask = 0xFFFFFFFF])
  2240.     lua_register(m_luaState, "getPlayersByIp", LuaInterface::luaGetPlayersByIp);
  2241.  
  2242.     //doPlayerPopupFYI(cid, message)
  2243.     lua_register(m_luaState, "doPlayerPopupFYI", LuaInterface::luaDoPlayerPopupFYI);
  2244.  
  2245.     //doPlayerSendTutorial(cid, id)
  2246.     lua_register(m_luaState, "doPlayerSendTutorial", LuaInterface::luaDoPlayerSendTutorial);
  2247.  
  2248.     //doPlayerSendMailByName(name, item[, town[, actor]])
  2249.     lua_register(m_luaState, "doPlayerSendMailByName", LuaInterface::luaDoPlayerSendMailByName);
  2250.  
  2251.     //doPlayerAddMapMark(cid, pos, type[, description])
  2252.     lua_register(m_luaState, "doPlayerAddMapMark", LuaInterface::luaDoPlayerAddMapMark);
  2253.  
  2254.     //doPlayerAddPremiumDays(cid, days)
  2255.     lua_register(m_luaState, "doPlayerAddPremiumDays", LuaInterface::luaDoPlayerAddPremiumDays);
  2256.  
  2257.     //getPlayerPremiumDays(cid)
  2258.     lua_register(m_luaState, "getPlayerPremiumDays", LuaInterface::luaGetPlayerPremiumDays);
  2259.  
  2260.     //doCreatureSetLookDirection(cid, dir)
  2261.     lua_register(m_luaState, "doCreatureSetLookDirection", LuaInterface::luaDoCreatureSetLookDir);
  2262.  
  2263.     //getCreatureGuildEmblem(cid[, target])
  2264.     lua_register(m_luaState, "getCreatureGuildEmblem", LuaInterface::luaGetCreatureGuildEmblem);
  2265.  
  2266.     //doCreatureSetGuildEmblem(cid, emblem)
  2267.     lua_register(m_luaState, "doCreatureSetGuildEmblem", LuaInterface::luaDoCreatureSetGuildEmblem);
  2268.  
  2269.     //getCreaturePartyShield(cid[, target])
  2270.     lua_register(m_luaState, "getCreaturePartyShield", LuaInterface::luaGetCreaturePartyShield);
  2271.  
  2272.     //doCreatureSetPartyShield(cid, shield)
  2273.     lua_register(m_luaState, "doCreatureSetPartyShield", LuaInterface::luaDoCreatureSetPartyShield);
  2274.  
  2275.     //getCreatureSkullType(cid[, target])
  2276.     lua_register(m_luaState, "getCreatureSkullType", LuaInterface::luaGetCreatureSkullType);
  2277.  
  2278.     //doCreatureSetSkullType(cid, skull)
  2279.     lua_register(m_luaState, "doCreatureSetSkullType", LuaInterface::luaDoCreatureSetSkullType);
  2280.  
  2281.     //getPlayerSkullEnd(cid)
  2282.     lua_register(m_luaState, "getPlayerSkullEnd", LuaInterface::luaGetPlayerSkullEnd);
  2283.  
  2284.     //doPlayerSetSkullEnd(cid, time, type)
  2285.     lua_register(m_luaState, "doPlayerSetSkullEnd", LuaInterface::luaDoPlayerSetSkullEnd);
  2286.  
  2287.     //getPlayerBlessing(cid, blessing)
  2288.     lua_register(m_luaState, "getPlayerBlessing", LuaInterface::luaGetPlayerBlessing);
  2289.  
  2290.     //doPlayerAddBlessing(cid, blessing)
  2291.     lua_register(m_luaState, "doPlayerAddBlessing", LuaInterface::luaDoPlayerAddBlessing);
  2292.  
  2293.     //getPlayerPVPBlessing(cid)
  2294.     lua_register(m_luaState, "getPlayerPVPBlessing", LuaInterface::luaGetPlayerPVPBlessing);
  2295.  
  2296.     //doPlayerSetPVPBlessing(cid[, value])
  2297.     lua_register(m_luaState, "doPlayerSetPVPBlessing", LuaInterface::luaDoPlayerSetPVPBlessing);
  2298.  
  2299.     //getPlayerStamina(cid)
  2300.     lua_register(m_luaState, "getPlayerStamina", LuaInterface::luaGetPlayerStamina);
  2301.  
  2302.     //doPlayerSetStamina(cid, minutes)
  2303.     lua_register(m_luaState, "doPlayerSetStamina", LuaInterface::luaDoPlayerSetStamina);
  2304.  
  2305.     //getPlayerBalance(cid)
  2306.     lua_register(m_luaState, "getPlayerBalance", LuaInterface::luaGetPlayerBalance);
  2307.  
  2308.     //doPlayerSetBalance(cid, balance)
  2309.     lua_register(m_luaState, "doPlayerSetBalance", LuaInterface::luaDoPlayerSetBalance);
  2310.  
  2311.     //getCreatureNoMove(cid)
  2312.     lua_register(m_luaState, "getCreatureNoMove", LuaInterface::luaGetCreatureNoMove);
  2313.  
  2314.     //doCreatureSetNoMove(cid, block)
  2315.     lua_register(m_luaState, "doCreatureSetNoMove", LuaInterface::luaDoCreatureSetNoMove);
  2316.  
  2317.     //getPlayerIdleTime(cid)
  2318.     lua_register(m_luaState, "getPlayerIdleTime", LuaInterface::luaGetPlayerIdleTime);
  2319.  
  2320.     //doPlayerSetIdleTime(cid, amount)
  2321.     lua_register(m_luaState, "doPlayerSetIdleTime", LuaInterface::luaDoPlayerSetIdleTime);
  2322.  
  2323.     //getPlayerLastLoad(cid)
  2324.     lua_register(m_luaState, "getPlayerLastLoad", LuaInterface::luaGetPlayerLastLoad);
  2325.  
  2326.     //getPlayerLastLogin(cid)
  2327.     lua_register(m_luaState, "getPlayerLastLogin", LuaInterface::luaGetPlayerLastLogin);
  2328.  
  2329.     //getPlayerAccountManager(cid)
  2330.     lua_register(m_luaState, "getPlayerAccountManager", LuaInterface::luaGetPlayerAccountManager);
  2331.  
  2332.     //getPlayerTradeState(cid)
  2333.     lua_register(m_luaState, "getPlayerTradeState", LuaInterface::luaGetPlayerTradeState);
  2334.  
  2335.     //getPlayerOperatingSystem(cid)
  2336.     lua_register(m_luaState, "getPlayerOperatingSystem", LuaInterface::luaGetPlayerOperatingSystem);
  2337.  
  2338.     //getPlayerClientVersion(cid)
  2339.     lua_register(m_luaState, "getPlayerClientVersion", LuaInterface::luaGetPlayerClientVersion);
  2340.  
  2341.     //getPlayerModes(cid)
  2342.     lua_register(m_luaState, "getPlayerModes", LuaInterface::luaGetPlayerModes);
  2343.  
  2344.     //getPlayerRates(cid)
  2345.     lua_register(m_luaState, "getPlayerRates", LuaInterface::luaGetPlayerRates);
  2346.  
  2347.     //doPlayerSetRate(cid, type, value)
  2348.     lua_register(m_luaState, "doPlayerSetRate", LuaInterface::luaDoPlayerSetRate);
  2349.  
  2350.     //getPlayerPartner(cid)
  2351.     lua_register(m_luaState, "getPlayerPartner", LuaInterface::luaGetPlayerPartner);
  2352.  
  2353.     //doPlayerSetPartner(cid, guid)
  2354.     lua_register(m_luaState, "doPlayerSetPartner", LuaInterface::luaDoPlayerSetPartner);
  2355.  
  2356.     //doPlayerFollowCreature(cid, target)
  2357.     lua_register(m_luaState, "doPlayerFollowCreature", LuaInterface::luaDoPlayerFollowCreature);
  2358.  
  2359.     //getPlayerParty(cid)
  2360.     lua_register(m_luaState, "getPlayerParty", LuaInterface::luaGetPlayerParty);
  2361.  
  2362.     //doPlayerJoinParty(cid, lid)
  2363.     lua_register(m_luaState, "doPlayerJoinParty", LuaInterface::luaDoPlayerJoinParty);
  2364.  
  2365.     //doPlayerLeaveParty(cid[, forced = false])
  2366.     lua_register(m_luaState, "doPlayerLeaveParty", LuaInterface::luaDoPlayerLeaveParty);
  2367.  
  2368.     //getPartyMembers(lid)
  2369.     lua_register(m_luaState, "getPartyMembers", LuaInterface::luaGetPartyMembers);
  2370.  
  2371.     //getCreatureMaster(cid)
  2372.     lua_register(m_luaState, "getCreatureMaster", LuaInterface::luaGetCreatureMaster);
  2373.  
  2374.     //getCreatureSummons(cid)
  2375.     lua_register(m_luaState, "getCreatureSummons", LuaInterface::luaGetCreatureSummons);
  2376.  
  2377.     //getTownId(townName)
  2378.     lua_register(m_luaState, "getTownId", LuaInterface::luaGetTownId);
  2379.  
  2380.     //getTownName(townId)
  2381.     lua_register(m_luaState, "getTownName", LuaInterface::luaGetTownName);
  2382.  
  2383.     //getTownTemplePosition(townId)
  2384.     lua_register(m_luaState, "getTownTemplePosition", LuaInterface::luaGetTownTemplePosition);
  2385.  
  2386.     //getTownHouses(townId)
  2387.     lua_register(m_luaState, "getTownHouses", LuaInterface::luaGetTownHouses);
  2388.  
  2389.     //getSpectators(centerPos, rangex, rangey[, multifloor = false])
  2390.     lua_register(m_luaState, "getSpectators", LuaInterface::luaGetSpectators);
  2391.  
  2392.     //getVocationInfo(id)
  2393.     lua_register(m_luaState, "getVocationInfo", LuaInterface::luaGetVocationInfo);
  2394.  
  2395.     //getGroupInfo(id[, premium = false])
  2396.     lua_register(m_luaState, "getGroupInfo", LuaInterface::luaGetGroupInfo);
  2397.  
  2398.     //getVocationList()
  2399.     lua_register(m_luaState, "getVocationList", LuaInterface::luaGetVocationList);
  2400.  
  2401.     //getGroupList()
  2402.     lua_register(m_luaState, "getGroupList", LuaInterface::luaGetGroupList);
  2403.  
  2404.     //getChannelList()
  2405.     lua_register(m_luaState, "getChannelList", LuaInterface::luaGetChannelList);
  2406.  
  2407.     //getTownList()
  2408.     lua_register(m_luaState, "getTownList", LuaInterface::luaGetTownList);
  2409.  
  2410.     //getWaypointList()
  2411.     lua_register(m_luaState, "getWaypointList", LuaInterface::luaGetWaypointList);
  2412.  
  2413.     //getTalkActionList()
  2414.     lua_register(m_luaState, "getTalkActionList", LuaInterface::luaGetTalkActionList);
  2415.  
  2416.     //getExperienceStageList()
  2417.     lua_register(m_luaState, "getExperienceStageList", LuaInterface::luaGetExperienceStageList);
  2418.  
  2419.     //getItemIdByName(name)
  2420.     lua_register(m_luaState, "getItemIdByName", LuaInterface::luaGetItemIdByName);
  2421.  
  2422.     //getItemInfo(itemid)
  2423.     lua_register(m_luaState, "getItemInfo", LuaInterface::luaGetItemInfo);
  2424.  
  2425.     //getItemAttribute(uid, key)
  2426.     lua_register(m_luaState, "getItemAttribute", LuaInterface::luaGetItemAttribute);
  2427.  
  2428.     //doItemSetAttribute(uid, key, value)
  2429.     lua_register(m_luaState, "doItemSetAttribute", LuaInterface::luaDoItemSetAttribute);
  2430.  
  2431.     //doItemEraseAttribute(uid, key)
  2432.     lua_register(m_luaState, "doItemEraseAttribute", LuaInterface::luaDoItemEraseAttribute);
  2433.  
  2434.     //getItemWeight(uid[, precise = true])
  2435.     lua_register(m_luaState, "getItemWeight", LuaInterface::luaGetItemWeight);
  2436.  
  2437.     //getItemParent(uid)
  2438.     lua_register(m_luaState, "getItemParent", LuaInterface::luaGetItemParent);
  2439.  
  2440.     //hasItemProperty(uid, prop)
  2441.     lua_register(m_luaState, "hasItemProperty", LuaInterface::luaHasItemProperty);
  2442.  
  2443.     //hasPlayerClient(cid)
  2444.     lua_register(m_luaState, "hasPlayerClient", LuaInterface::luaHasPlayerClient);
  2445.  
  2446.     //hasMonsterRaid(cid)
  2447.     lua_register(m_luaState, "hasMonsterRaid", LuaInterface::luaHasMonsterRaid);
  2448.  
  2449.     //isIpBanished(ip[, mask])
  2450.     lua_register(m_luaState, "isIpBanished", LuaInterface::luaIsIpBanished);
  2451.  
  2452.     //isPlayerBanished(name/guid, type)
  2453.     lua_register(m_luaState, "isPlayerBanished", LuaInterface::luaIsPlayerBanished);
  2454.  
  2455.     //isAccountBanished(accountId[, playerId])
  2456.     lua_register(m_luaState, "isAccountBanished", LuaInterface::luaIsAccountBanished);
  2457.  
  2458.     //doAddIpBanishment(...)
  2459.     lua_register(m_luaState, "doAddIpBanishment", LuaInterface::luaDoAddIpBanishment);
  2460.  
  2461.     //doAddPlayerBanishment(...)
  2462.     lua_register(m_luaState, "doAddPlayerBanishment", LuaInterface::luaDoAddPlayerBanishment);
  2463.  
  2464.     //doAddAccountBanishment(...)
  2465.     lua_register(m_luaState, "doAddAccountBanishment", LuaInterface::luaDoAddAccountBanishment);
  2466.  
  2467.     //doAddAccountWarnings(...)
  2468.     lua_register(m_luaState, "doAddAccountWarnings", LuaInterface::luaDoAddAccountWarnings);
  2469.  
  2470.     //getAccountWarnings(accountId)
  2471.     lua_register(m_luaState, "getAccountWarnings", LuaInterface::luaGetAccountWarnings);
  2472.  
  2473.     //doAddNotation(...)
  2474.     lua_register(m_luaState, "doAddNotation", LuaInterface::luaDoAddNotation);
  2475.  
  2476.     //doAddStatement(...)
  2477.     lua_register(m_luaState, "doAddStatement", LuaInterface::luaDoAddStatement);
  2478.  
  2479.     //doRemoveIpBanishment(ip[, mask])
  2480.     lua_register(m_luaState, "doRemoveIpBanishment", LuaInterface::luaDoRemoveIpBanishment);
  2481.  
  2482.     //doRemovePlayerBanishment(name/guid, type)
  2483.     lua_register(m_luaState, "doRemovePlayerBanishment", LuaInterface::luaDoRemovePlayerBanishment);
  2484.  
  2485.     //doRemoveAccountBanishment(accountId[, playerId])
  2486.     lua_register(m_luaState, "doRemoveAccountBanishment", LuaInterface::luaDoRemoveAccountBanishment);
  2487.  
  2488.     //doRemoveNotations(accountId[, playerId])
  2489.     lua_register(m_luaState, "doRemoveNotations", LuaInterface::luaDoRemoveNotations);
  2490.  
  2491.     //getNotationsCount(accountId[, playerId])
  2492.     lua_register(m_luaState, "getNotationsCount", LuaInterface::luaGetNotationsCount);
  2493.  
  2494.     //getBanData(value[, type[, param]])
  2495.     lua_register(m_luaState, "getBanData", LuaInterface::luaGetBanData);
  2496.  
  2497.     //getBanList(type[, value[, param]])
  2498.     lua_register(m_luaState, "getBanList", LuaInterface::luaGetBanList);
  2499.  
  2500.     //getExperienceStage(level)
  2501.     lua_register(m_luaState, "getExperienceStage", LuaInterface::luaGetExperienceStage);
  2502.  
  2503.     //getDataDir()
  2504.     lua_register(m_luaState, "getDataDir", LuaInterface::luaGetDataDir);
  2505.  
  2506.     //getLogsDir()
  2507.     lua_register(m_luaState, "getLogsDir", LuaInterface::luaGetLogsDir);
  2508.  
  2509.     //getConfigFile()
  2510.     lua_register(m_luaState, "getConfigFile", LuaInterface::luaGetConfigFile);
  2511.  
  2512.     //isPlayerUsingOtclient(cid)
  2513.     lua_register(m_luaState, "isPlayerUsingOtclient", LuaInterface::luaIsPlayerUsingOtclient);
  2514.  
  2515.     //doSendPlayerExtendedOpcode(cid, opcode, buffer)
  2516.     lua_register(m_luaState, "doSendPlayerExtendedOpcode", LuaInterface::luaDoSendPlayerExtendedOpcode);
  2517.  
  2518.     //getConfigValue(key)
  2519.     lua_register(m_luaState, "getConfigValue", LuaInterface::luaGetConfigValue);
  2520.  
  2521.     //getModList()
  2522.     lua_register(m_luaState, "getModList", LuaInterface::luaGetModList);
  2523.  
  2524.     //getHighscoreString(skillId)
  2525.     lua_register(m_luaState, "getHighscoreString", LuaInterface::luaGetHighscoreString);
  2526.  
  2527.     //getWaypointPosition(name)
  2528.     lua_register(m_luaState, "getWaypointPosition", LuaInterface::luaGetWaypointPosition);
  2529.  
  2530.     //doWaypointAddTemporial(name, pos)
  2531.     lua_register(m_luaState, "doWaypointAddTemporial", LuaInterface::luaDoWaypointAddTemporial);
  2532.  
  2533.     //getGameState()
  2534.     lua_register(m_luaState, "getGameState", LuaInterface::luaGetGameState);
  2535.  
  2536.     //doSetGameState(id)
  2537.     lua_register(m_luaState, "doSetGameState", LuaInterface::luaDoSetGameState);
  2538.  
  2539.     //doExecuteRaid(name)
  2540.     lua_register(m_luaState, "doExecuteRaid", LuaInterface::luaDoExecuteRaid);
  2541.  
  2542.     //doCreatureExecuteTalkAction(cid, text[, ignoreAccess = false[, channelId = CHANNEL_DEFAULT]])
  2543.     lua_register(m_luaState, "doCreatureExecuteTalkAction", LuaInterface::luaDoCreatureExecuteTalkAction);
  2544.  
  2545.     //doReloadInfo(id[, cid])
  2546.     lua_register(m_luaState, "doReloadInfo", LuaInterface::luaDoReloadInfo);
  2547.  
  2548.     //doSaveServer([flags = 13])
  2549.     lua_register(m_luaState, "doSaveServer", LuaInterface::luaDoSaveServer);
  2550.  
  2551.     //doSaveHouse({list})
  2552.     lua_register(m_luaState, "doSaveHouse", LuaInterface::luaDoSaveHouse);
  2553.  
  2554.     //doCleanHouse(houseId)
  2555.     lua_register(m_luaState, "doCleanHouse", LuaInterface::luaDoCleanHouse);
  2556.  
  2557.     //doCleanMap()
  2558.     lua_register(m_luaState, "doCleanMap", LuaInterface::luaDoCleanMap);
  2559.  
  2560.     //doRefreshMap()
  2561.     lua_register(m_luaState, "doRefreshMap", LuaInterface::luaDoRefreshMap);
  2562.  
  2563.     //doPlayerSetWalkthrough(cid, uid, walkthrough)
  2564.     lua_register(m_luaState, "doPlayerSetWalkthrough", LuaInterface::luaDoPlayerSetWalkthrough);
  2565.  
  2566.     //doGuildAddEnemy(guild, enemy, war, type)
  2567.     lua_register(m_luaState, "doGuildAddEnemy", LuaInterface::luaDoGuildAddEnemy);
  2568.  
  2569.     //doGuildRemoveEnemy(guild, enemy)
  2570.     lua_register(m_luaState, "doGuildRemoveEnemy", LuaInterface::luaDoGuildRemoveEnemy);
  2571.  
  2572.     //doUpdateHouseAuctions()
  2573.     lua_register(m_luaState, "doUpdateHouseAuctions", LuaInterface::luaDoUpdateHouseAuctions);
  2574.  
  2575.     //loadmodlib(lib)
  2576.     lua_register(m_luaState, "loadmodlib", LuaInterface::luaL_loadmodlib);
  2577.  
  2578.     //domodlib(lib)
  2579.     lua_register(m_luaState, "domodlib", LuaInterface::luaL_domodlib);
  2580.  
  2581.     //dodirectory(dir[, recursively = false])
  2582.     lua_register(m_luaState, "dodirectory", LuaInterface::luaL_dodirectory);
  2583.  
  2584.     //errors(var)
  2585.     lua_register(m_luaState, "errors", LuaInterface::luaL_errors);
  2586.  
  2587.     //os table
  2588.     luaL_register(m_luaState, "os", LuaInterface::luaSystemTable);
  2589.  
  2590.     //db table
  2591.     luaL_register(m_luaState, "db", LuaInterface::luaDatabaseTable);
  2592.  
  2593.     //result table
  2594.     luaL_register(m_luaState, "result", LuaInterface::luaResultTable);
  2595.  
  2596.     //bit table
  2597.     luaL_register(m_luaState, "bit", LuaInterface::luaBitTable);
  2598.  
  2599.     //std table
  2600.     luaL_register(m_luaState, "std", LuaInterface::luaStdTable);
  2601. }
  2602.  
  2603. const luaL_Reg LuaInterface::luaSystemTable[] =
  2604. {
  2605.     //os.mtime()
  2606.     {"mtime", LuaInterface::luaSystemTime},
  2607.  
  2608.     {NULL, NULL}
  2609. };
  2610.  
  2611. const luaL_Reg LuaInterface::luaDatabaseTable[] =
  2612. {
  2613.     //db.query(query)
  2614.     {"query", LuaInterface::luaDatabaseExecute},
  2615.  
  2616.     //db.storeQuery(query)
  2617.     {"storeQuery", LuaInterface::luaDatabaseStoreQuery},
  2618.  
  2619.     //db.escapeString(str)
  2620.     {"escapeString", LuaInterface::luaDatabaseEscapeString},
  2621.  
  2622.     //db.escapeBlob(s, length)
  2623.     {"escapeBlob", LuaInterface::luaDatabaseEscapeBlob},
  2624.  
  2625.     //db.lastInsertId()
  2626.     {"lastInsertId", LuaInterface::luaDatabaseLastInsertId},
  2627.  
  2628.     //db.stringComparer()
  2629.     {"stringComparer", LuaInterface::luaDatabaseStringComparer},
  2630.  
  2631.     //db.updateLimiter()
  2632.     {"updateLimiter", LuaInterface::luaDatabaseUpdateLimiter},
  2633.  
  2634.     //db.connected()
  2635.     {"connected", LuaInterface::luaDatabaseConnected},
  2636.  
  2637.     //db.tableExists(name)
  2638.     {"tableExists", LuaInterface::luaDatabaseTableExists},
  2639.  
  2640.     //db.transBegin()
  2641.     {"transBegin", LuaInterface::luaDatabaseTransBegin},
  2642.  
  2643.     //db.transRollback()
  2644.     {"transRollback", LuaInterface::luaDatabaseTransRollback},
  2645.  
  2646.     //db.transCommit()
  2647.     {"transCommit", LuaInterface::luaDatabaseTransCommit},
  2648.  
  2649.     {NULL, NULL}
  2650. };
  2651.  
  2652. const luaL_Reg LuaInterface::luaResultTable[] =
  2653. {
  2654.     //result.getDataInt(resId, s)
  2655.     {"getDataInt", LuaInterface::luaResultGetDataInt},
  2656.  
  2657.     //result.getDataLong(resId, s)
  2658.     {"getDataLong", LuaInterface::luaResultGetDataLong},
  2659.  
  2660.     //result.getDataString(resId, s)
  2661.     {"getDataString", LuaInterface::luaResultGetDataString},
  2662.  
  2663.     //result.getDataStream(resId, s, length)
  2664.     {"getDataStream", LuaInterface::luaResultGetDataStream},
  2665.  
  2666.     //result.next(resId)
  2667.     {"next", LuaInterface::luaResultNext},
  2668.  
  2669.     //result.free(resId)
  2670.     {"free", LuaInterface::luaResultFree},
  2671.  
  2672.     {NULL, NULL}
  2673. };
  2674.  
  2675. const luaL_Reg LuaInterface::luaBitTable[] =
  2676. {
  2677.     //{"cast", LuaInterface::luaBitCast},
  2678.     {"bnot", LuaInterface::luaBitNot},
  2679.     {"band", LuaInterface::luaBitAnd},
  2680.     {"bor", LuaInterface::luaBitOr},
  2681.     {"bxor", LuaInterface::luaBitXor},
  2682.     {"lshift", LuaInterface::luaBitLeftShift},
  2683.     {"rshift", LuaInterface::luaBitRightShift},
  2684.     //{"arshift", LuaInterface::luaBitArithmeticalRightShift},
  2685.  
  2686.     //{"ucast", LuaInterface::luaBitUCast},
  2687.     {"ubnot", LuaInterface::luaBitUNot},
  2688.     {"uband", LuaInterface::luaBitUAnd},
  2689.     {"ubor", LuaInterface::luaBitUOr},
  2690.     {"ubxor", LuaInterface::luaBitUXor},
  2691.     {"ulshift", LuaInterface::luaBitULeftShift},
  2692.     {"urshift", LuaInterface::luaBitURightShift},
  2693.     //{"uarshift", LuaInterface::luaBitUArithmeticalRightShift},
  2694.  
  2695.     {NULL, NULL}
  2696. };
  2697.  
  2698. const luaL_Reg LuaInterface::luaStdTable[] =
  2699. {
  2700.     {"cout", LuaInterface::luaStdCout},
  2701.     {"clog", LuaInterface::luaStdClog},
  2702.     {"cerr", LuaInterface::luaStdCerr},
  2703.  
  2704.     {"md5", LuaInterface::luaStdMD5},
  2705.     {"sha1", LuaInterface::luaStdSHA1},
  2706.     {"sha256", LuaInterface::luaStdSHA256},
  2707.     {"sha512", LuaInterface::luaStdSHA512},
  2708.  
  2709.     {"checkName", LuaInterface::luaStdCheckName},
  2710.     {NULL, NULL}
  2711. };
  2712.  
  2713. int32_t LuaInterface::internalGetPlayerInfo(lua_State* L, PlayerInfo_t info)
  2714. {
  2715.     ScriptEnviroment* env = getEnv();
  2716.     const Player* player = env->getPlayerByUID(popNumber(L));
  2717.     if(!player)
  2718.     {
  2719.         std::ostringstream s;
  2720.         s << getError(LUA_ERROR_PLAYER_NOT_FOUND) << " when requesting player info #" << info;
  2721.         errorEx(s.str());
  2722.  
  2723.         lua_pushboolean(L, false);
  2724.         return 1;
  2725.     }
  2726.  
  2727.     int64_t value = 0;
  2728.     Position pos;
  2729.     switch(info)
  2730.     {
  2731.         case PlayerInfoNameDescription:
  2732.             lua_pushstring(L, player->getNameDescription().c_str());
  2733.             return 1;
  2734.         case PlayerInfoSpecialDescription:
  2735.             lua_pushstring(L, player->getSpecialDescription().c_str());
  2736.             return 1;
  2737.         case PlayerInfoAccess:
  2738.             value = player->getAccess();
  2739.             break;
  2740.         case PlayerInfoGhostAccess:
  2741.             value = player->getGhostAccess();
  2742.             break;
  2743.         case PlayerInfoLevel:
  2744.             value = player->getLevel();
  2745.             break;
  2746.         case PlayerInfoExperience:
  2747.             value = player->getExperience();
  2748.             break;
  2749.         case PlayerInfoManaSpent:
  2750.             value = player->getSpentMana();
  2751.             break;
  2752.         case PlayerInfoTown:
  2753.             value = player->getTown();
  2754.             break;
  2755.         case PlayerInfoPromotionLevel:
  2756.             value = player->getPromotionLevel();
  2757.             break;
  2758.         case PlayerInfoGUID:
  2759.             value = player->getGUID();
  2760.             break;
  2761.         case PlayerInfoAccountId:
  2762.             value = player->getAccount();
  2763.             break;
  2764.         case PlayerInfoAccount:
  2765.             lua_pushstring(L, player->getAccountName().c_str());
  2766.             return 1;
  2767.         case PlayerInfoPremiumDays:
  2768.             value = player->getPremiumDays();
  2769.             break;
  2770.         case PlayerInfoFood:
  2771.         {
  2772.             if(Condition* condition = player->getCondition(CONDITION_REGENERATION, CONDITIONID_DEFAULT))
  2773.                 value = condition->getTicks() / 1000;
  2774.  
  2775.             break;
  2776.         }
  2777.         case PlayerInfoVocation:
  2778.             value = player->getVocationId();
  2779.             break;
  2780.         case PlayerInfoMoney:
  2781.             value = g_game.getMoney(player);
  2782.             break;
  2783.         case PlayerInfoFreeCap:
  2784.             value = (int64_t)player->getFreeCapacity();
  2785.             break;
  2786.         case PlayerInfoGuildId:
  2787.             value = player->getGuildId();
  2788.             break;
  2789.         case PlayerInfoGuildName:
  2790.             lua_pushstring(L, player->getGuildName().c_str());
  2791.             return 1;
  2792.         case PlayerInfoGuildRankId:
  2793.             value = player->getRankId();
  2794.             break;
  2795.         case PlayerInfoGuildRank:
  2796.             lua_pushstring(L, player->getRankName().c_str());
  2797.             return 1;
  2798.         case PlayerInfoGuildLevel:
  2799.             value = player->getGuildLevel();
  2800.             break;
  2801.         case PlayerInfoGuildNick:
  2802.             lua_pushstring(L, player->getGuildNick().c_str());
  2803.             return 1;
  2804.         case PlayerInfoGroupId:
  2805.             value = player->getGroupId();
  2806.             break;
  2807.         case PlayerInfoBalance:
  2808.             if(g_config.getBool(ConfigManager::BANK_SYSTEM))
  2809.                 lua_pushnumber(L, player->balance);
  2810.             else
  2811.                 lua_pushnumber(L, 0);
  2812.  
  2813.             return 1;
  2814.         case PlayerInfoStamina:
  2815.             value = player->getStaminaMinutes();
  2816.             break;
  2817.         case PlayerInfoLossSkill:
  2818.             lua_pushboolean(L, player->getLossSkill());
  2819.             return 1;
  2820.         case PlayerInfoMarriage:
  2821.             value = player->marriage;
  2822.             break;
  2823.         case PlayerInfoPzLock:
  2824.             lua_pushboolean(L, player->isPzLocked());
  2825.             return 1;
  2826.         case PlayerInfoSaving:
  2827.             lua_pushboolean(L, player->isSaving());
  2828.             return 1;
  2829.         case PlayerInfoProtected:
  2830.             lua_pushboolean(L, player->isProtected());
  2831.             return 1;
  2832.         case PlayerInfoIp:
  2833.             value = player->getIP();
  2834.             break;
  2835.         case PlayerInfoSkullEnd:
  2836.             value = player->getSkullEnd();
  2837.             break;
  2838.         case PlayerInfoOutfitWindow:
  2839.             player->sendOutfitWindow();
  2840.             lua_pushboolean(L, true);
  2841.             return 1;
  2842.         case PlayerInfoIdleTime:
  2843.             value = player->getIdleTime();
  2844.             break;
  2845.         case PlayerInfoClient:
  2846.             lua_pushboolean(L, player->hasClient());
  2847.             return 1;
  2848.         case PlayerInfoLastLoad:
  2849.             value = player->getLastLoad();
  2850.             break;
  2851.         case PlayerInfoLastLogin:
  2852.             value = player->getLastLogin();
  2853.             break;
  2854.         case PlayerInfoAccountManager:
  2855.             value = player->accountManager;
  2856.             break;
  2857.         case PlayerInfoTradeState:
  2858.             value = player->tradeState;
  2859.             break;
  2860.         case PlayerInfoOperatingSystem:
  2861.             value = player->getOperatingSystem();
  2862.             break;
  2863.         case PlayerInfoClientVersion:
  2864.             value = player->getClientVersion();
  2865.             break;
  2866.         default:
  2867.             errorEx("Unknown player info #" + info);
  2868.             value = 0;
  2869.             break;
  2870.     }
  2871.  
  2872.     lua_pushnumber(L, value);
  2873.     return 1;
  2874. }
  2875.  
  2876. //getPlayer[Info](uid)
  2877. int32_t LuaInterface::luaGetPlayerNameDescription(lua_State* L)
  2878. {
  2879.     return internalGetPlayerInfo(L, PlayerInfoNameDescription);
  2880. }
  2881.  
  2882. int32_t LuaInterface::luaGetPlayerSpecialDescription(lua_State* L)
  2883. {
  2884.     return internalGetPlayerInfo(L, PlayerInfoSpecialDescription);
  2885. }
  2886.  
  2887. int32_t LuaInterface::luaGetPlayerFood(lua_State* L)
  2888. {
  2889.     return internalGetPlayerInfo(L, PlayerInfoFood);
  2890. }
  2891.  
  2892. int32_t LuaInterface::luaGetPlayerAccess(lua_State* L)
  2893. {
  2894.     return internalGetPlayerInfo(L, PlayerInfoAccess);
  2895. }
  2896.  
  2897. int32_t LuaInterface::luaGetPlayerGhostAccess(lua_State* L)
  2898. {
  2899.     return internalGetPlayerInfo(L, PlayerInfoGhostAccess);
  2900. }
  2901.  
  2902. int32_t LuaInterface::luaGetPlayerLevel(lua_State* L)
  2903. {
  2904.     return internalGetPlayerInfo(L, PlayerInfoLevel);
  2905. }
  2906.  
  2907. int32_t LuaInterface::luaGetPlayerExperience(lua_State* L)
  2908. {
  2909.     return internalGetPlayerInfo(L, PlayerInfoExperience);
  2910. }
  2911.  
  2912. int32_t LuaInterface::luaGetPlayerSpentMana(lua_State* L)
  2913. {
  2914.     return internalGetPlayerInfo(L, PlayerInfoManaSpent);
  2915. }
  2916.  
  2917. int32_t LuaInterface::luaGetPlayerVocation(lua_State* L)
  2918. {
  2919.     return internalGetPlayerInfo(L, PlayerInfoVocation);
  2920. }
  2921.  
  2922. int32_t LuaInterface::luaGetPlayerMoney(lua_State* L)
  2923. {
  2924.     return internalGetPlayerInfo(L, PlayerInfoMoney);
  2925. }
  2926.  
  2927. int32_t LuaInterface::luaGetPlayerFreeCap(lua_State* L)
  2928. {
  2929.     return internalGetPlayerInfo(L, PlayerInfoFreeCap);
  2930. }
  2931.  
  2932. int32_t LuaInterface::luaGetPlayerGuildId(lua_State* L)
  2933. {
  2934.     return internalGetPlayerInfo(L, PlayerInfoGuildId);
  2935. }
  2936.  
  2937. int32_t LuaInterface::luaGetPlayerGuildName(lua_State* L)
  2938. {
  2939.     return internalGetPlayerInfo(L, PlayerInfoGuildName);
  2940. }
  2941.  
  2942. int32_t LuaInterface::luaGetPlayerGuildRankId(lua_State* L)
  2943. {
  2944.     return internalGetPlayerInfo(L, PlayerInfoGuildRankId);
  2945. }
  2946.  
  2947. int32_t LuaInterface::luaGetPlayerGuildRank(lua_State* L)
  2948. {
  2949.     return internalGetPlayerInfo(L, PlayerInfoGuildRank);
  2950. }
  2951.  
  2952. int32_t LuaInterface::luaGetPlayerGuildLevel(lua_State* L)
  2953. {
  2954.     return internalGetPlayerInfo(L, PlayerInfoGuildLevel);
  2955. }
  2956.  
  2957. int32_t LuaInterface::luaGetPlayerGuildNick(lua_State* L)
  2958. {
  2959.     return internalGetPlayerInfo(L, PlayerInfoGuildNick);
  2960. }
  2961.  
  2962. int32_t LuaInterface::luaGetPlayerTown(lua_State* L)
  2963. {
  2964.     return internalGetPlayerInfo(L, PlayerInfoTown);
  2965. }
  2966.  
  2967. int32_t LuaInterface::luaGetPlayerPromotionLevel(lua_State* L)
  2968. {
  2969.     return internalGetPlayerInfo(L, PlayerInfoPromotionLevel);
  2970. }
  2971.  
  2972. int32_t LuaInterface::luaGetPlayerGroupId(lua_State* L)
  2973. {
  2974.     return internalGetPlayerInfo(L, PlayerInfoGroupId);
  2975. }
  2976.  
  2977. int32_t LuaInterface::luaGetPlayerGUID(lua_State* L)
  2978. {
  2979.     return internalGetPlayerInfo(L, PlayerInfoGUID);
  2980. }
  2981.  
  2982. int32_t LuaInterface::luaGetPlayerAccountId(lua_State* L)
  2983. {
  2984.     return internalGetPlayerInfo(L, PlayerInfoAccountId);
  2985. }
  2986.  
  2987. int32_t LuaInterface::luaGetPlayerAccount(lua_State* L)
  2988. {
  2989.     return internalGetPlayerInfo(L, PlayerInfoAccount);
  2990. }
  2991.  
  2992. int32_t LuaInterface::luaGetPlayerPremiumDays(lua_State* L)
  2993. {
  2994.     return internalGetPlayerInfo(L, PlayerInfoPremiumDays);
  2995. }
  2996.  
  2997. int32_t LuaInterface::luaGetPlayerBalance(lua_State* L)
  2998. {
  2999.     return internalGetPlayerInfo(L, PlayerInfoBalance);
  3000. }
  3001.  
  3002. int32_t LuaInterface::luaGetPlayerStamina(lua_State* L)
  3003. {
  3004.     return internalGetPlayerInfo(L, PlayerInfoStamina);
  3005. }
  3006.  
  3007. int32_t LuaInterface::luaGetPlayerLossSkill(lua_State* L)
  3008. {
  3009.     return internalGetPlayerInfo(L, PlayerInfoLossSkill);
  3010. }
  3011.  
  3012. int32_t LuaInterface::luaGetPlayerPartner(lua_State* L)
  3013. {
  3014.     return internalGetPlayerInfo(L, PlayerInfoMarriage);
  3015. }
  3016.  
  3017. int32_t LuaInterface::luaIsPlayerPzLocked(lua_State* L)
  3018. {
  3019.     return internalGetPlayerInfo(L, PlayerInfoPzLock);
  3020. }
  3021.  
  3022. int32_t LuaInterface::luaIsPlayerSaving(lua_State* L)
  3023. {
  3024.     return internalGetPlayerInfo(L, PlayerInfoSaving);
  3025. }
  3026.  
  3027. int32_t LuaInterface::luaIsPlayerProtected(lua_State* L)
  3028. {
  3029.     return internalGetPlayerInfo(L, PlayerInfoProtected);
  3030. }
  3031.  
  3032. int32_t LuaInterface::luaGetPlayerIp(lua_State* L)
  3033. {
  3034.     return internalGetPlayerInfo(L, PlayerInfoIp);
  3035. }
  3036.  
  3037. int32_t LuaInterface::luaGetPlayerSkullEnd(lua_State* L)
  3038. {
  3039.     return internalGetPlayerInfo(L, PlayerInfoSkullEnd);
  3040. }
  3041.  
  3042. int32_t LuaInterface::luaDoPlayerSendOutfitWindow(lua_State* L)
  3043. {
  3044.     return internalGetPlayerInfo(L, PlayerInfoOutfitWindow);
  3045. }
  3046.  
  3047. int32_t LuaInterface::luaGetPlayerIdleTime(lua_State* L)
  3048. {
  3049.     return internalGetPlayerInfo(L, PlayerInfoIdleTime);
  3050. }
  3051.  
  3052. int32_t LuaInterface::luaHasPlayerClient(lua_State* L)
  3053. {
  3054.     return internalGetPlayerInfo(L, PlayerInfoClient);
  3055. }
  3056.  
  3057. int32_t LuaInterface::luaGetPlayerLastLoad(lua_State* L)
  3058. {
  3059.     return internalGetPlayerInfo(L, PlayerInfoLastLoad);
  3060. }
  3061.  
  3062. int32_t LuaInterface::luaGetPlayerLastLogin(lua_State* L)
  3063. {
  3064.     return internalGetPlayerInfo(L, PlayerInfoLastLogin);
  3065. }
  3066.  
  3067. int32_t LuaInterface::luaGetPlayerAccountManager(lua_State* L)
  3068. {
  3069.     return internalGetPlayerInfo(L, PlayerInfoAccountManager);
  3070. }
  3071.  
  3072. int32_t LuaInterface::luaGetPlayerTradeState(lua_State* L)
  3073. {
  3074.     return internalGetPlayerInfo(L, PlayerInfoTradeState);
  3075. }
  3076.  
  3077. int32_t LuaInterface::luaGetPlayerOperatingSystem(lua_State* L)
  3078. {
  3079.     return internalGetPlayerInfo(L, PlayerInfoOperatingSystem);
  3080. }
  3081.  
  3082. int32_t LuaInterface::luaGetPlayerClientVersion(lua_State* L)
  3083. {
  3084.     return internalGetPlayerInfo(L, PlayerInfoClientVersion);
  3085. }
  3086. //
  3087.  
  3088. int32_t LuaInterface::luaGetPlayerSex(lua_State* L)
  3089. {
  3090.     //getPlayerSex(cid[, full = false])
  3091.     bool full = false;
  3092.     if(lua_gettop(L) > 1)
  3093.         full = popBoolean(L);
  3094.  
  3095.     ScriptEnviroment* env = getEnv();
  3096.     Player* player = env->getPlayerByUID((uint32_t)popNumber(L));
  3097.     if(!player)
  3098.     {
  3099.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3100.         lua_pushboolean(L, false);
  3101.     }
  3102.     else
  3103.         lua_pushnumber(L, player->getSex(full));
  3104.  
  3105.     return 1;
  3106. }
  3107.  
  3108. int32_t LuaInterface::luaDoPlayerSetNameDescription(lua_State* L)
  3109. {
  3110.     //doPlayerSetNameDescription(cid, description)
  3111.     std::string description = popString(L);
  3112.  
  3113.     ScriptEnviroment* env = getEnv();
  3114.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3115.     {
  3116.         player->nameDescription += description;
  3117.         lua_pushboolean(L, true);
  3118.     }
  3119.     else
  3120.     {
  3121.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3122.         lua_pushboolean(L, false);
  3123.     }
  3124.  
  3125.     return 1;
  3126. }
  3127.  
  3128. int32_t LuaInterface::luaDoPlayerSetSpecialDescription(lua_State* L)
  3129. {
  3130.     //doPlayerSetSpecialDescription(cid, description)
  3131.     std::string description = popString(L);
  3132.  
  3133.     ScriptEnviroment* env = getEnv();
  3134.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3135.     {
  3136.         player->setSpecialDescription(description);
  3137.         lua_pushboolean(L, true);
  3138.     }
  3139.     else
  3140.     {
  3141.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3142.         lua_pushboolean(L, false);
  3143.     }
  3144.  
  3145.     return 1;
  3146. }
  3147.  
  3148. int32_t LuaInterface::luaGetPlayerMagLevel(lua_State* L)
  3149. {
  3150.     //getPlayerMagLevel(cid[, ignoreModifiers = false])
  3151.     bool ignoreModifiers = false;
  3152.     if(lua_gettop(L) > 1)
  3153.         ignoreModifiers = popBoolean(L);
  3154.  
  3155.     ScriptEnviroment* env = getEnv();
  3156.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  3157.         lua_pushnumber(L, ignoreModifiers ? player->magLevel : player->getMagicLevel());
  3158.     else
  3159.     {
  3160.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3161.         lua_pushboolean(L, false);
  3162.     }
  3163.  
  3164.     return 1;
  3165. }
  3166.  
  3167. int32_t LuaInterface::luaGetPlayerRequiredMana(lua_State* L)
  3168. {
  3169.     //getPlayerRequiredMana(cid, magicLevel)
  3170.     uint32_t magLevel = popNumber(L);
  3171.  
  3172.     ScriptEnviroment* env = getEnv();
  3173.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3174.         lua_pushnumber(L, player->vocation->getReqMana(magLevel));
  3175.     else
  3176.     {
  3177.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3178.         lua_pushboolean(L, false);
  3179.     }
  3180.  
  3181.     return 1;
  3182. }
  3183.  
  3184. int32_t LuaInterface::luaGetPlayerRequiredSkillTries(lua_State* L)
  3185. {
  3186.     //getPlayerRequiredSkillTries(cid, skill, level)
  3187.     int32_t level = popNumber(L), skill = popNumber(L);
  3188.  
  3189.     ScriptEnviroment* env = getEnv();
  3190.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3191.         lua_pushnumber(L, player->vocation->getReqSkillTries(skill, level));
  3192.     else
  3193.     {
  3194.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3195.         lua_pushboolean(L, false);
  3196.     }
  3197.  
  3198.     return 1;
  3199. }
  3200.  
  3201. int32_t LuaInterface::luaGetPlayerFlagValue(lua_State* L)
  3202. {
  3203.     //getPlayerFlagValue(cid, flag)
  3204.     uint32_t index = popNumber(L);
  3205.  
  3206.     ScriptEnviroment* env = getEnv();
  3207.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3208.     {
  3209.         if(index < PlayerFlag_LastFlag)
  3210.             lua_pushboolean(L, player->hasFlag((PlayerFlags)index));
  3211.         else
  3212.         {
  3213.             errorEx("No valid flag index - " + asString<uint32_t>(index));
  3214.             lua_pushboolean(L, false);
  3215.         }
  3216.     }
  3217.     else
  3218.     {
  3219.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3220.         lua_pushboolean(L, false);
  3221.     }
  3222.  
  3223.     return 1;
  3224. }
  3225.  
  3226. int32_t LuaInterface::luaGetPlayerCustomFlagValue(lua_State* L)
  3227. {
  3228.     //getPlayerCustomFlagValue(cid, flag)
  3229.     uint32_t index = popNumber(L);
  3230.  
  3231.     ScriptEnviroment* env = getEnv();
  3232.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3233.     {
  3234.         if(index < PlayerCustomFlag_LastFlag)
  3235.             lua_pushboolean(L, player->hasCustomFlag((PlayerCustomFlags)index));
  3236.         else
  3237.         {
  3238.             errorEx("No valid flag index - " + asString<uint32_t>(index));
  3239.             lua_pushboolean(L, false);
  3240.         }
  3241.     }
  3242.     else
  3243.     {
  3244.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3245.         lua_pushboolean(L, false);
  3246.     }
  3247.  
  3248.     return 1;
  3249. }
  3250.  
  3251. int32_t LuaInterface::luaDoPlayerLearnInstantSpell(lua_State* L)
  3252. {
  3253.     //doPlayerLearnInstantSpell(cid, name)
  3254.     std::string spellName = popString(L);
  3255.  
  3256.     ScriptEnviroment* env = getEnv();
  3257.     Player* player = env->getPlayerByUID(popNumber(L));
  3258.     if(!player)
  3259.     {
  3260.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3261.         lua_pushboolean(L, false);
  3262.         return 1;
  3263.     }
  3264.  
  3265.     InstantSpell* spell = g_spells->getInstantSpellByName(spellName);
  3266.     if(!spell)
  3267.     {
  3268.         lua_pushboolean(L, false);
  3269.         return 1;
  3270.     }
  3271.  
  3272.     player->learnInstantSpell(spell->getName());
  3273.     lua_pushboolean(L, true);
  3274.     return 1;
  3275. }
  3276.  
  3277. int32_t LuaInterface::luaDoPlayerUnlearnInstantSpell(lua_State* L)
  3278. {
  3279.     //doPlayerUnlearnInstantSpell(cid, name)
  3280.     std::string spellName = popString(L);
  3281.  
  3282.     ScriptEnviroment* env = getEnv();
  3283.     Player* player = env->getPlayerByUID(popNumber(L));
  3284.     if(!player)
  3285.     {
  3286.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3287.         lua_pushboolean(L, false);
  3288.         return 1;
  3289.     }
  3290.  
  3291.     InstantSpell* spell = g_spells->getInstantSpellByName(spellName);
  3292.     if(!spell)
  3293.     {
  3294.         lua_pushboolean(L, false);
  3295.         return 1;
  3296.     }
  3297.  
  3298.     player->unlearnInstantSpell(spell->getName());
  3299.     lua_pushboolean(L, true);
  3300.     return 1;
  3301. }
  3302.  
  3303. int32_t LuaInterface::luaGetPlayerLearnedInstantSpell(lua_State* L)
  3304. {
  3305.     //getPlayerLearnedInstantSpell(cid, name)
  3306.     std::string spellName = popString(L);
  3307.  
  3308.     ScriptEnviroment* env = getEnv();
  3309.     Player* player = env->getPlayerByUID(popNumber(L));
  3310.     if(!player)
  3311.     {
  3312.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3313.         lua_pushboolean(L, false);
  3314.         return 1;
  3315.     }
  3316.  
  3317.     InstantSpell* spell = g_spells->getInstantSpellByName(spellName);
  3318.     if(!spell)
  3319.     {
  3320.         lua_pushboolean(L, false);
  3321.         return 1;
  3322.     }
  3323.  
  3324.     lua_pushboolean(L, player->hasLearnedInstantSpell(spellName));
  3325.     return 1;
  3326. }
  3327.  
  3328. int32_t LuaInterface::luaGetPlayerInstantSpellCount(lua_State* L)
  3329. {
  3330.     //getPlayerInstantSpellCount(cid)
  3331.     ScriptEnviroment* env = getEnv();
  3332.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3333.         lua_pushnumber(L, g_spells->getInstantSpellCount(player));
  3334.     else
  3335.     {
  3336.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3337.         lua_pushboolean(L, false);
  3338.     }
  3339.  
  3340.     return 1;
  3341. }
  3342.  
  3343. int32_t LuaInterface::luaGetPlayerInstantSpellInfo(lua_State* L)
  3344. {
  3345.     //getPlayerInstantSpellInfo(cid, index)
  3346.     uint32_t index = popNumber(L);
  3347.  
  3348.     ScriptEnviroment* env = getEnv();
  3349.     Player* player = env->getPlayerByUID(popNumber(L));
  3350.     if(!player)
  3351.     {
  3352.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3353.         lua_pushboolean(L, false);
  3354.         return 1;
  3355.     }
  3356.  
  3357.     InstantSpell* spell = g_spells->getInstantSpellByIndex(player, index);
  3358.     if(!spell)
  3359.     {
  3360.         errorEx(getError(LUA_ERROR_SPELL_NOT_FOUND));
  3361.         lua_pushboolean(L, false);
  3362.         return 1;
  3363.     }
  3364.  
  3365.     lua_newtable(L);
  3366.     setField(L, "name", spell->getName());
  3367.     setField(L, "words", spell->getWords());
  3368.     setField(L, "level", spell->getLevel());
  3369.     setField(L, "mlevel", spell->getMagicLevel());
  3370.     setField(L, "mana", spell->getManaCost(player));
  3371.     setField(L, "manapercent", spell->getManaPercent());
  3372.     return 1;
  3373. }
  3374.  
  3375. int32_t LuaInterface::luaGetInstantSpellInfo(lua_State* L)
  3376. {
  3377.     //getInstantSpellInfo(name)
  3378.     InstantSpell* spell = g_spells->getInstantSpellByName(popString(L));
  3379.     if(!spell)
  3380.     {
  3381.         errorEx(getError(LUA_ERROR_SPELL_NOT_FOUND));
  3382.         lua_pushboolean(L, false);
  3383.         return 1;
  3384.     }
  3385.  
  3386.     lua_newtable(L);
  3387.     setField(L, "name", spell->getName());
  3388.     setField(L, "words", spell->getWords());
  3389.     setField(L, "level", spell->getLevel());
  3390.     setField(L, "mlevel", spell->getMagicLevel());
  3391.     setField(L, "mana", spell->getManaCost(NULL));
  3392.     setField(L, "manapercent", spell->getManaPercent());
  3393.     return 1;
  3394. }
  3395.  
  3396. int32_t LuaInterface::luaDoCreatureCastSpell(lua_State* L)
  3397. {
  3398.     //doCreatureCastSpell (uid, spell)
  3399.     std::string spellname = popString(L);
  3400.  
  3401.     ScriptEnviroment *env = getEnv();
  3402.     Creature *creature = env->getCreatureByUID(popNumber(L));
  3403.     if(!creature)
  3404.     {
  3405.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3406.         lua_pushboolean (L, false);
  3407.         return 1;
  3408.     }
  3409.  
  3410.     Spell *spell = g_spells->getInstantSpellByName(spellname);
  3411.     if(spell)
  3412.     {
  3413.         if(spell->castSpell(creature))
  3414.         {
  3415.             lua_pushboolean(L, true);
  3416.             return 1;
  3417.         }
  3418.     }
  3419.  
  3420.     errorEx(getError(LUA_ERROR_SPELL_NOT_FOUND));
  3421.     lua_pushboolean(L, false);
  3422.     return 1;
  3423. }
  3424.  
  3425. int32_t LuaInterface::luaDoRemoveItem(lua_State* L)
  3426. {
  3427.     //doRemoveItem(uid[, count = -1])
  3428.     int32_t count = -1;
  3429.     if(lua_gettop(L) > 1)
  3430.         count = popNumber(L);
  3431.  
  3432.     ScriptEnviroment* env = getEnv();
  3433.     Item* item = env->getItemByUID(popNumber(L));
  3434.     if(!item)
  3435.     {
  3436.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  3437.         lua_pushboolean(L, false);
  3438.         return 1;
  3439.     }
  3440.  
  3441.     if(g_game.internalRemoveItem(NULL, item, count) != RET_NOERROR)
  3442.     {
  3443.         lua_pushboolean(L, false);
  3444.         return 1;
  3445.     }
  3446.  
  3447.     lua_pushboolean(L, true);
  3448.     return 1;
  3449. }
  3450.  
  3451. int32_t LuaInterface::luaDoPlayerRemoveItem(lua_State* L)
  3452. {
  3453.     //doPlayerRemoveItem(cid, itemid, count[, subType = -1[, ignoreEquipped = false]])
  3454.     int32_t params = lua_gettop(L), subType = -1;
  3455.     bool ignoreEquipped = false;
  3456.     if(params > 4)
  3457.         ignoreEquipped = popBoolean(L);
  3458.  
  3459.     if(params > 3)
  3460.         subType = popNumber(L);
  3461.  
  3462.     uint32_t count = popNumber(L);
  3463.     uint16_t itemId = (uint16_t)popNumber(L);
  3464.  
  3465.     ScriptEnviroment* env = getEnv();
  3466.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3467.         lua_pushboolean(L, g_game.removeItemOfType(player, itemId, count, subType, ignoreEquipped));
  3468.     else
  3469.     {
  3470.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3471.         lua_pushboolean(L, false);
  3472.     }
  3473.  
  3474.     return 1;
  3475. }
  3476.  
  3477. int32_t LuaInterface::luaDoPlayerFeed(lua_State* L)
  3478. {
  3479.     //doPlayerFeed(cid, food)
  3480.     int32_t food = (int32_t)popNumber(L);
  3481.  
  3482.     ScriptEnviroment* env = getEnv();
  3483.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3484.     {
  3485.         player->addDefaultRegeneration((food * 1000) * 3);
  3486.         lua_pushboolean(L, true);
  3487.     }
  3488.     else
  3489.     {
  3490.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3491.         lua_pushboolean(L, false);
  3492.     }
  3493.  
  3494.     return 1;
  3495. }
  3496.  
  3497. int32_t LuaInterface::luaDoPlayerSendCancel(lua_State* L)
  3498. {
  3499.     //doPlayerSendCancel(cid, text)
  3500.     std::string text = popString(L);
  3501.     ScriptEnviroment* env = getEnv();
  3502.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  3503.     {
  3504.         player->sendCancel(text);
  3505.         lua_pushboolean(L, true);
  3506.     }
  3507.     else
  3508.     {
  3509.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3510.         lua_pushboolean(L, false);
  3511.     }
  3512.  
  3513.     return 1;
  3514. }
  3515.  
  3516. int32_t LuaInterface::luaDoSendDefaultCancel(lua_State* L)
  3517. {
  3518.     //doPlayerSendDefaultCancel(cid, ReturnValue)
  3519.     ReturnValue ret = (ReturnValue)popNumber(L);
  3520.     ScriptEnviroment* env = getEnv();
  3521.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  3522.     {
  3523.         player->sendCancelMessage(ret);
  3524.         lua_pushboolean(L, true);
  3525.     }
  3526.     else
  3527.     {
  3528.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3529.         lua_pushboolean(L, false);
  3530.     }
  3531.  
  3532.     return 1;
  3533. }
  3534.  
  3535. int32_t LuaInterface::luaGetSearchString(lua_State* L)
  3536. {
  3537.     //getSearchString(fromPosition, toPosition[, fromIsCreature = false[, toIsCreature = false]])
  3538.     PositionEx toPos, fromPos;
  3539.     bool toIsCreature = false, fromIsCreature = false;
  3540.  
  3541.     int32_t params = lua_gettop(L);
  3542.     if(params > 3)
  3543.         toIsCreature = popBoolean(L);
  3544.  
  3545.     if(params > 2)
  3546.         fromIsCreature = popBoolean(L);
  3547.  
  3548.     popPosition(L, toPos);
  3549.     popPosition(L, fromPos);
  3550.     if(!toPos.x || !toPos.y || !fromPos.x || !fromPos.y)
  3551.     {
  3552.         errorEx("Wrong position(s) specified");
  3553.         lua_pushboolean(L, false);
  3554.     }
  3555.     else
  3556.         lua_pushstring(L, g_game.getSearchString(fromPos, toPos, fromIsCreature, toIsCreature).c_str());
  3557.  
  3558.     return 1;
  3559. }
  3560.  
  3561. int32_t LuaInterface::luaGetClosestFreeTile(lua_State* L)
  3562. {
  3563.     //getClosestFreeTile(cid, targetPos[, extended = false[, ignoreHouse = true]])
  3564.     uint32_t params = lua_gettop(L);
  3565.     bool ignoreHouse = true, extended = false;
  3566.     if(params > 3)
  3567.         ignoreHouse = popBoolean(L);
  3568.  
  3569.     if(params > 2)
  3570.         extended = popBoolean(L);
  3571.  
  3572.     PositionEx pos;
  3573.     popPosition(L, pos);
  3574.  
  3575.     ScriptEnviroment* env = getEnv();
  3576.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3577.     {
  3578.         Position newPos = g_game.getClosestFreeTile(creature, pos, extended, ignoreHouse);
  3579.         if(newPos.x != 0)
  3580.             pushPosition(L, newPos, 0);
  3581.         else
  3582.             lua_pushboolean(L, false);
  3583.     }
  3584.     else
  3585.     {
  3586.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3587.         lua_pushboolean(L, false);
  3588.     }
  3589.  
  3590.     return 1;
  3591. }
  3592.  
  3593. int32_t LuaInterface::luaDoTeleportThing(lua_State* L)
  3594. {
  3595.     //doTeleportThing(cid, destination[, pushMove = true[, fullTeleport = true]])
  3596.     bool fullTeleport = true, pushMove = true;
  3597.     int32_t params = lua_gettop(L);
  3598.     if(params > 3)
  3599.         fullTeleport = popBoolean(L);
  3600.  
  3601.     if(params > 2)
  3602.         pushMove = popBoolean(L);
  3603.  
  3604.     PositionEx pos;
  3605.     popPosition(L, pos);
  3606.  
  3607.     ScriptEnviroment* env = getEnv();
  3608.     if(Thing* tmp = env->getThingByUID(popNumber(L)))
  3609.         lua_pushboolean(L, g_game.internalTeleport(tmp, pos, !pushMove, FLAG_NOLIMIT, fullTeleport) == RET_NOERROR);
  3610.     else
  3611.     {
  3612.         errorEx(getError(LUA_ERROR_THING_NOT_FOUND));
  3613.         lua_pushboolean(L, false);
  3614.     }
  3615.  
  3616.     return 1;
  3617. }
  3618.  
  3619. int32_t LuaInterface::luaDoItemSetDestination(lua_State* L)
  3620. {
  3621.     //doItemSetDestination(uid, destination)
  3622.     PositionEx destination;
  3623.     popPosition(L, destination);
  3624.  
  3625.     ScriptEnviroment* env = getEnv();
  3626.     Item* item = env->getItemByUID(popNumber(L));
  3627.     if(!item)
  3628.     {
  3629.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  3630.         lua_pushboolean(L, false);
  3631.         return 1;
  3632.     }
  3633.  
  3634.     if(Teleport* teleport = item->getTeleport())
  3635.     {
  3636.         teleport->setDestination(destination);
  3637.         lua_pushboolean(L, true);
  3638.         return 1;
  3639.     }
  3640.  
  3641.     errorEx("Target item is not a teleport.");
  3642.     lua_pushboolean(L, false);
  3643.     return 1;
  3644. }
  3645.  
  3646. int32_t LuaInterface::luaDoTransformItem(lua_State* L)
  3647. {
  3648.     //doTransformItem(uid, newId[, count/subType])
  3649.     int32_t count = -1;
  3650.     if(lua_gettop(L) > 2)
  3651.         count = popNumber(L);
  3652.  
  3653.     uint32_t newId = popNumber(L), uid = popNumber(L);
  3654.     ScriptEnviroment* env = getEnv();
  3655.  
  3656.     Item* item = env->getItemByUID(uid);
  3657.     if(!item)
  3658.     {
  3659.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  3660.         lua_pushboolean(L, false);
  3661.         return 1;
  3662.     }
  3663.  
  3664.     const ItemType& it = Item::items[newId];
  3665.     if(it.stackable && count > 100)
  3666.         count = 100;
  3667.  
  3668.     Item* newItem = g_game.transformItem(item, newId, count);
  3669.     if(newItem && newItem != item)
  3670.     {
  3671.         env->removeThing(uid);
  3672.         env->insertThing(uid, newItem);
  3673.  
  3674.         if(newItem->getUniqueId() != 0)
  3675.             env->addUniqueThing(newItem);
  3676.     }
  3677.  
  3678.     lua_pushboolean(L, true);
  3679.     return 1;
  3680. }
  3681.  
  3682. int32_t LuaInterface::luaDoCreatureSay(lua_State* L)
  3683. {
  3684.     //doCreatureSay(uid, text[, type = SPEAK_SAY[, ghost = false[, cid = 0[, pos]]]])
  3685.     uint32_t params = lua_gettop(L), cid = 0, uid = 0;
  3686.     PositionEx pos;
  3687.     if(params > 5)
  3688.         popPosition(L, pos);
  3689.  
  3690.     if(params > 4)
  3691.         cid = popNumber(L);
  3692.  
  3693.     bool ghost = false;
  3694.     if(params > 3)
  3695.         ghost = popBoolean(L);
  3696.  
  3697.     MessageClasses type = MSG_SPEAK_SAY;
  3698.     if(params > 2)
  3699.         type = (MessageClasses)popNumber(L);
  3700.  
  3701.     std::string text = popString(L);
  3702.  
  3703.     uid = popNumber(L);
  3704.     if(params > 5 && (!pos.x || !pos.y))
  3705.     {
  3706.         errorEx("Invalid position specified");
  3707.         lua_pushboolean(L, false);
  3708.         return 1;
  3709.     }
  3710.  
  3711.     ScriptEnviroment* env = getEnv();
  3712.     Creature* creature = env->getCreatureByUID(uid);
  3713.     if(!creature)
  3714.     {
  3715.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3716.         lua_pushboolean(L, false);
  3717.         return 1;
  3718.     }
  3719.  
  3720.     SpectatorVec list;
  3721.     if(cid)
  3722.     {
  3723.         Creature* target = env->getCreatureByUID(cid);
  3724.         if(!target)
  3725.         {
  3726.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3727.             lua_pushboolean(L, false);
  3728.             return 1;
  3729.         }
  3730.  
  3731.         list.insert(target);
  3732.     }
  3733.  
  3734.     if(params > 5)
  3735.         lua_pushboolean(L, g_game.internalCreatureSay(creature, type, text, ghost, &list, &pos));
  3736.     else
  3737.         lua_pushboolean(L, g_game.internalCreatureSay(creature, type, text, ghost, &list));
  3738.  
  3739.     return 1;
  3740. }
  3741.  
  3742. int32_t LuaInterface::luaDoCreatureChannelSay(lua_State* L)
  3743. {
  3744.     //doCreatureChannelSay(target, uid, message, type, channel)
  3745.     ScriptEnviroment* env = getEnv();
  3746.     uint16_t channelId = popNumber(L);
  3747.     std::string text = popString(L);
  3748.     uint32_t speakClass = popNumber(L), targetId = popNumber(L);
  3749.  
  3750.     Player* player = env->getPlayerByUID(popNumber(L));
  3751.     if(!player)
  3752.     {
  3753.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3754.         lua_pushboolean(L, false);
  3755.         return 1;
  3756.     }
  3757.  
  3758.     Creature* creature = env->getCreatureByUID(targetId);
  3759.     if(!creature)
  3760.     {
  3761.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3762.         lua_pushboolean(L, false);
  3763.         return 1;
  3764.     }
  3765.  
  3766.     player->sendCreatureChannelSay(creature, (MessageClasses)speakClass, text, channelId);
  3767.     lua_pushboolean(L, true);
  3768.     return 1;
  3769. }
  3770.  
  3771. int32_t LuaInterface::luaDoSendMagicEffect(lua_State* L)
  3772. {
  3773.     //doSendMagicEffect(pos, type[, player])
  3774.     ScriptEnviroment* env = getEnv();
  3775.     SpectatorVec list;
  3776.     if(lua_gettop(L) > 2)
  3777.     {
  3778.         if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3779.             list.insert(creature);
  3780.     }
  3781.  
  3782.     uint32_t type = popNumber(L);
  3783.     PositionEx pos;
  3784.  
  3785.     popPosition(L, pos);
  3786.     if(pos.x == 0xFFFF)
  3787.         pos = env->getRealPos();
  3788.  
  3789.     if(!list.empty())
  3790.         g_game.addMagicEffect(list, pos, type);
  3791.     else
  3792.         g_game.addMagicEffect(pos, type);
  3793.  
  3794.     lua_pushboolean(L, true);
  3795.     return 1;
  3796. }
  3797.  
  3798. int32_t LuaInterface::luaDoSendDistanceShoot(lua_State* L)
  3799. {
  3800.     //doSendDistanceShoot(fromPos, toPos, type[, player])
  3801.     ScriptEnviroment* env = getEnv();
  3802.     SpectatorVec list;
  3803.     if(lua_gettop(L) > 3)
  3804.     {
  3805.         if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3806.             list.insert(creature);
  3807.     }
  3808.  
  3809.     uint32_t type = popNumber(L);
  3810.     PositionEx toPos, fromPos;
  3811.  
  3812.     popPosition(L, toPos);
  3813.     popPosition(L, fromPos);
  3814.     if(fromPos.x == 0xFFFF)
  3815.         fromPos = env->getRealPos();
  3816.  
  3817.     if(toPos.x == 0xFFFF)
  3818.         toPos = env->getRealPos();
  3819.  
  3820.     if(!list.empty())
  3821.         g_game.addDistanceEffect(list, fromPos, toPos, type);
  3822.     else
  3823.         g_game.addDistanceEffect(fromPos, toPos, type);
  3824.  
  3825.     lua_pushboolean(L, true);
  3826.     return 1;
  3827. }
  3828.  
  3829. int32_t LuaInterface::luaDoPlayerAddSkillTry(lua_State* L)
  3830. {
  3831.     //doPlayerAddSkillTry(uid, skillid, n[, useMultiplier = true])
  3832.     bool multiplier = true;
  3833.     if(lua_gettop(L) > 3)
  3834.         multiplier = popBoolean(L);
  3835.  
  3836.     uint64_t n = popNumber(L);
  3837.     uint16_t skillid = popNumber(L);
  3838.  
  3839.     ScriptEnviroment* env = getEnv();
  3840.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3841.     {
  3842.         player->addSkillAdvance((skills_t)skillid, n, multiplier);
  3843.         lua_pushboolean(L, true);
  3844.     }
  3845.     else
  3846.     {
  3847.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3848.         lua_pushboolean(L, false);
  3849.     }
  3850.  
  3851.     return 1;
  3852. }
  3853.  
  3854. int32_t LuaInterface::luaGetCreatureSpeakType(lua_State* L)
  3855. {
  3856.     //getCreatureSpeakType(uid)
  3857.     ScriptEnviroment* env = getEnv();
  3858.     if(const Creature* creature = env->getCreatureByUID(popNumber(L)))
  3859.         lua_pushnumber(L, (MessageClasses)creature->getSpeakType());
  3860.     else
  3861.     {
  3862.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3863.         lua_pushboolean(L, false);
  3864.     }
  3865.  
  3866.     return 1;
  3867. }
  3868.  
  3869. int32_t LuaInterface::luaDoCreatureSetSpeakType(lua_State* L)
  3870. {
  3871.     //doCreatureSetSpeakType(uid, type)
  3872.     MessageClasses type = (MessageClasses)popNumber(L);
  3873.  
  3874.     ScriptEnviroment* env = getEnv();
  3875.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3876.     {
  3877.         if(!((type >= MSG_SPEAK_FIRST && type <= MSG_SPEAK_LAST) ||
  3878.             (type >= MSG_SPEAK_MONSTER_FIRST && type <= MSG_SPEAK_MONSTER_LAST)))
  3879.         {
  3880.             errorEx("Invalid speak type");
  3881.             lua_pushboolean(L, false);
  3882.             return 1;
  3883.         }
  3884.  
  3885.         creature->setSpeakType(type);
  3886.         lua_pushboolean(L, true);
  3887.     }
  3888.     else
  3889.     {
  3890.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3891.         lua_pushboolean(L, false);
  3892.     }
  3893.  
  3894.     return 1;
  3895. }
  3896.  
  3897. int32_t LuaInterface::luaGetCreatureHideHealth(lua_State* L)
  3898. {
  3899.     //getCreatureHideHealth(cid)
  3900.     ScriptEnviroment* env = getEnv();
  3901.  
  3902.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3903.         lua_pushboolean(L, creature->getHideHealth());
  3904.     else
  3905.     {
  3906.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3907.         lua_pushboolean(L, false);
  3908.     }
  3909.  
  3910.     return 1;
  3911. }
  3912.  
  3913. int32_t LuaInterface::luaDoCreatureSetHideHealth(lua_State* L)
  3914. {
  3915.     //doCreatureSetHideHealth(cid, hide)
  3916.     bool hide = popBoolean(L);
  3917.  
  3918.     ScriptEnviroment* env = getEnv();
  3919.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3920.     {
  3921.         creature->setHideHealth(hide);
  3922.         g_game.addCreatureHealth(creature);
  3923.         lua_pushboolean(L, true);
  3924.     }
  3925.     else
  3926.     {
  3927.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3928.         lua_pushboolean(L, false);
  3929.     }
  3930.  
  3931.     return 1;
  3932. }
  3933.  
  3934. int32_t LuaInterface::luaDoCreatureAddHealth(lua_State* L)
  3935. {
  3936.     //doCreatureAddHealth(uid, health[, hitEffect[, hitColor[, force]]])
  3937.     int32_t params = lua_gettop(L);
  3938.     bool force = false;
  3939.     if(params > 4)
  3940.         force = popBoolean(L);
  3941.  
  3942.     Color_t hitColor = COLOR_UNKNOWN;
  3943.     if(params > 3)
  3944.         hitColor = (Color_t)popNumber(L);
  3945.  
  3946.     MagicEffect_t hitEffect = MAGIC_EFFECT_UNKNOWN;
  3947.     if(params > 2)
  3948.         hitEffect = (MagicEffect_t)popNumber(L);
  3949.  
  3950.     int32_t healthChange = popNumber(L);
  3951.     ScriptEnviroment* env = getEnv();
  3952.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3953.     {
  3954.         if(healthChange) //do not post with 0 value
  3955.             g_game.combatChangeHealth(healthChange < 1 ? COMBAT_UNDEFINEDDAMAGE : COMBAT_HEALING,
  3956.                 NULL, creature, healthChange, hitEffect, hitColor, force);
  3957.  
  3958.         lua_pushboolean(L, true);
  3959.     }
  3960.     else
  3961.     {
  3962.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3963.         lua_pushboolean(L, false);
  3964.     }
  3965.  
  3966.     return 1;
  3967. }
  3968.  
  3969. int32_t LuaInterface::luaDoCreatureAddMana(lua_State* L)
  3970. {
  3971.     //doCreatureAddMana(uid, mana[, aggressive])
  3972.     bool aggressive = true;
  3973.     if(lua_gettop(L) > 2)
  3974.         aggressive = popBoolean(L);
  3975.  
  3976.     int32_t manaChange = popNumber(L);
  3977.     ScriptEnviroment* env = getEnv();
  3978.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3979.     {
  3980.         if(aggressive)
  3981.             g_game.combatChangeMana(NULL, creature, manaChange);
  3982.         else
  3983.             creature->changeMana(manaChange);
  3984.  
  3985.         lua_pushboolean(L, true);
  3986.     }
  3987.     else
  3988.     {
  3989.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3990.         lua_pushboolean(L, false);
  3991.     }
  3992.  
  3993.     return 1;
  3994. }
  3995.  
  3996. int32_t LuaInterface::luaDoPlayerAddSpentMana(lua_State* L)
  3997. {
  3998.     //doPlayerAddSpentMana(cid, amount[, useMultiplier = true])
  3999.     bool multiplier = true;
  4000.     if(lua_gettop(L) > 2)
  4001.         multiplier = popBoolean(L);
  4002.  
  4003.     uint32_t amount = popNumber(L);
  4004.     ScriptEnviroment* env = getEnv();
  4005.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  4006.     {
  4007.         player->addManaSpent(amount, multiplier);
  4008.         lua_pushboolean(L, true);
  4009.     }
  4010.     else
  4011.     {
  4012.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4013.         lua_pushboolean(L, false);
  4014.     }
  4015.  
  4016.     return 1;
  4017. }
  4018.  
  4019. int32_t LuaInterface::luaDoPlayerAddItem(lua_State* L)
  4020. {
  4021.     //doPlayerAddItem(cid, itemid[, count/subtype = 1[, canDropOnMap = true[, slot = 0]]])
  4022.     //doPlayerAddItem(cid, itemid[, count = 1[, canDropOnMap = true[, subtype = 1[, slot = 0]]]])
  4023.     int32_t params = lua_gettop(L), subType = 1, slot = SLOT_WHEREEVER;
  4024.     if(params > 5)
  4025.         slot = popNumber(L);
  4026.  
  4027.     if(params > 4)
  4028.     {
  4029.         if(params > 5)
  4030.             subType = popNumber(L);
  4031.         else
  4032.             slot = popNumber(L);
  4033.     }
  4034.  
  4035.     bool canDropOnMap = true;
  4036.     if(params > 3)
  4037.         canDropOnMap = popBoolean(L);
  4038.  
  4039.     uint32_t count = 1;
  4040.     if(params > 2)
  4041.         count = popNumber(L);
  4042.  
  4043.     uint32_t itemId = popNumber(L);
  4044.     if(slot > SLOT_AMMO)
  4045.     {
  4046.         errorEx("Invalid slot");
  4047.         lua_pushboolean(L, false);
  4048.         return 1;
  4049.     }
  4050.  
  4051.     ScriptEnviroment* env = getEnv();
  4052.     Player* player = env->getPlayerByUID((uint32_t)popNumber(L));
  4053.     if(!player)
  4054.     {
  4055.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4056.         lua_pushboolean(L, false);
  4057.         return 1;
  4058.     }
  4059.  
  4060.     const ItemType& it = Item::items[itemId];
  4061.     int32_t itemCount = 1;
  4062.     if(params > 4)
  4063.         itemCount = std::max((uint32_t)1, count);
  4064.     else if(it.hasSubType())
  4065.     {
  4066.         if(it.stackable)
  4067.             itemCount = (int32_t)std::ceil((float)count / 100);
  4068.  
  4069.         subType = count;
  4070.     }
  4071.  
  4072.     uint32_t ret = 0;
  4073.     Item* newItem = NULL;
  4074.     while(itemCount > 0)
  4075.     {
  4076.         int32_t stackCount = std::min(100, subType);
  4077.         if(!(newItem = Item::CreateItem(itemId, stackCount)))
  4078.         {
  4079.             errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  4080.             lua_pushboolean(L, false);
  4081.             return ++ret;
  4082.         }
  4083.  
  4084.         if(it.stackable)
  4085.             subType -= stackCount;
  4086.  
  4087.         Item* stackItem = NULL;
  4088.         if(g_game.internalPlayerAddItem(NULL, player, newItem, canDropOnMap, (slots_t)slot, &stackItem) != RET_NOERROR)
  4089.         {
  4090.             delete newItem;
  4091.             lua_pushboolean(L, false);
  4092.             return ++ret;
  4093.         }
  4094.  
  4095.         ++ret;
  4096.         if(newItem->getParent())
  4097.             lua_pushnumber(L, env->addThing(newItem));
  4098.         else if(stackItem)
  4099.             lua_pushnumber(L, env->addThing(stackItem));
  4100.         else
  4101.             lua_pushnil(L);
  4102.  
  4103.         --itemCount;
  4104.     }
  4105.    
  4106.     if (newItem)
  4107.     {
  4108.         if (newItem->isStackable())
  4109.         {
  4110.             player->updateInventoryWeight();
  4111.             player->sendStats();
  4112.         }
  4113.     }
  4114.  
  4115.     if(ret)
  4116.         return ret;
  4117.  
  4118.     lua_pushnil(L);
  4119.     return 1;
  4120. }
  4121.  
  4122. int32_t LuaInterface::luaDoPlayerAddItemEx(lua_State* L)
  4123. {
  4124.     //doPlayerAddItemEx(cid, uid[, canDropOnMap = false[, slot = 0]])
  4125.     int32_t params = lua_gettop(L), slot = SLOT_WHEREEVER;
  4126.     if(params > 3)
  4127.         slot = popNumber(L);
  4128.  
  4129.     bool canDropOnMap = false;
  4130.     if(params > 2)
  4131.         canDropOnMap = popBoolean(L);
  4132.  
  4133.     uint32_t uid = (uint32_t)popNumber(L);
  4134.     if(slot > SLOT_AMMO)
  4135.     {
  4136.         errorEx("Invalid slot");
  4137.         lua_pushboolean(L, false);
  4138.         return 1;
  4139.     }
  4140.  
  4141.     ScriptEnviroment* env = getEnv();
  4142.     Player* player = env->getPlayerByUID(popNumber(L));
  4143.     if(!player)
  4144.     {
  4145.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4146.         lua_pushboolean(L, false);
  4147.         return 1;
  4148.     }
  4149.  
  4150.     Item* item = env->getItemByUID(uid);
  4151.     if(!item)
  4152.     {
  4153.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  4154.         lua_pushboolean(L, false);
  4155.         return 1;
  4156.     }
  4157.  
  4158.     if(item->getParent() == VirtualCylinder::virtualCylinder)
  4159.     {
  4160.         env->removeTempItem(env, item);
  4161.         ReturnValue ret = g_game.internalPlayerAddItem(NULL, player, item, canDropOnMap, (slots_t)slot);
  4162.         if(ret != RET_NOERROR)
  4163.             env->addTempItem(env, item);
  4164.  
  4165.         lua_pushnumber(L, ret);
  4166.     }
  4167.     else
  4168.         lua_pushboolean(L, false);
  4169.  
  4170.     return 1;
  4171. }
  4172.  
  4173. int32_t LuaInterface::luaDoTileAddItemEx(lua_State* L)
  4174. {
  4175.     //doTileAddItemEx(pos, uid)
  4176.     uint32_t uid = (uint32_t)popNumber(L);
  4177.     PositionEx pos;
  4178.     popPosition(L, pos);
  4179.  
  4180.     ScriptEnviroment* env = getEnv();
  4181.     Item* item = env->getItemByUID(uid);
  4182.     if(!item)
  4183.     {
  4184.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  4185.         lua_pushboolean(L, false);
  4186.         return 1;
  4187.     }
  4188.  
  4189.     Tile* tile = g_game.getTile(pos);
  4190.     if(!tile)
  4191.     {
  4192.         if(item->isGroundTile())
  4193.         {
  4194.             if(item->getParent() == VirtualCylinder::virtualCylinder)
  4195.             {
  4196.                 tile = IOMap::createTile(item, NULL, pos.x, pos.y, pos.z);
  4197.                 g_game.setTile(tile);
  4198.  
  4199.                 env->removeTempItem(env, item);
  4200.                 lua_pushnumber(L, RET_NOERROR);
  4201.             }
  4202.             else
  4203.                 lua_pushboolean(L, false);
  4204.         }
  4205.         else
  4206.         {
  4207.             errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  4208.             lua_pushboolean(L, false);
  4209.         }
  4210.  
  4211.         return 1;
  4212.     }
  4213.  
  4214.     if(item->getParent() == VirtualCylinder::virtualCylinder)
  4215.     {
  4216.         ReturnValue ret = g_game.internalAddItem(NULL, tile, item);
  4217.         if(ret == RET_NOERROR)
  4218.             env->removeTempItem(env, item);
  4219.  
  4220.         lua_pushnumber(L, ret);
  4221.     }
  4222.     else
  4223.         lua_pushboolean(L, false);
  4224.  
  4225.     return 1;
  4226. }
  4227.  
  4228. int32_t LuaInterface::luaDoRelocate(lua_State* L)
  4229. {
  4230.     //doRelocate(pos, posTo[, creatures = true[, unmovable = true]])
  4231.     //Moves all[ movable] objects from pos to posTo
  4232.     //Uses protected methods for optimal speed
  4233.     bool unmovable = true, creatures = true;
  4234.     int32_t params = lua_gettop(L);
  4235.     if(params > 3)
  4236.         unmovable = popBoolean(L);
  4237.  
  4238.     if(params > 2)
  4239.         creatures = popBoolean(L);
  4240.  
  4241.     PositionEx toPos;
  4242.     popPosition(L, toPos);
  4243.  
  4244.     PositionEx fromPos;
  4245.     popPosition(L, fromPos);
  4246.  
  4247.     Tile* fromTile = g_game.getTile(fromPos);
  4248.     if(!fromTile)
  4249.     {
  4250.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  4251.         lua_pushboolean(L, false);
  4252.         return 1;
  4253.     }
  4254.  
  4255.     Tile* toTile = g_game.getTile(toPos);
  4256.     if(!toTile)
  4257.     {
  4258.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  4259.         lua_pushboolean(L, false);
  4260.         return 1;
  4261.     }
  4262.  
  4263.     if(fromTile == toTile)
  4264.     {
  4265.         lua_pushboolean(L, false);
  4266.         return 1;
  4267.     }
  4268.  
  4269.     TileItemVector *toItems = toTile->getItemList(),
  4270.         *fromItems = fromTile->getItemList();
  4271.     if(fromItems && toItems)
  4272.     {
  4273.         int32_t itemLimit = g_config.getNumber(toTile->hasFlag(TILESTATE_PROTECTIONZONE)
  4274.             ? ConfigManager::PROTECTION_TILE_LIMIT : ConfigManager::TILE_LIMIT), count = 0;
  4275.         for(ItemVector::iterator it = fromItems->getBeginDownItem(); it != fromItems->getEndDownItem(); )
  4276.         {
  4277.             if(itemLimit && (int32_t)toItems->size() > itemLimit)
  4278.                 break;
  4279.  
  4280.             const ItemType& iType = Item::items[(*it)->getID()];
  4281.             if(!iType.isGroundTile() && !iType.alwaysOnTop && !iType.isMagicField() && (unmovable || iType.movable))
  4282.             {
  4283.                 if(Item* item = (*it))
  4284.                 {
  4285.                     it = fromItems->erase(it);
  4286.                     fromItems->removeDownItem();
  4287.                     fromTile->updateTileFlags(item, true);
  4288.  
  4289.                     g_moveEvents->onItemMove(NULL, item, fromTile, false);
  4290.                     g_moveEvents->onRemoveTileItem(fromTile, item);
  4291.  
  4292.                     item->setParent(toTile);
  4293.                     ++count;
  4294.  
  4295.                     toItems->insert(toItems->getBeginDownItem(), item);
  4296.                     toItems->addDownItem();
  4297.                     toTile->updateTileFlags(item, false);
  4298.  
  4299.                     g_moveEvents->onAddTileItem(toTile, item);
  4300.                     g_moveEvents->onItemMove(NULL, item, toTile, true);
  4301.                 }
  4302.                 else
  4303.                     ++it;
  4304.             }
  4305.             else
  4306.                 ++it;
  4307.         }
  4308.  
  4309.         fromTile->updateThingCount(-count);
  4310.         toTile->updateThingCount(count);
  4311.  
  4312.         fromTile->onUpdateTile();
  4313.         toTile->onUpdateTile();
  4314.         if(g_config.getBool(ConfigManager::STORE_TRASH)
  4315.             && fromTile->hasFlag(TILESTATE_TRASHED))
  4316.         {
  4317.             g_game.addTrash(toPos);
  4318.             toTile->setFlag(TILESTATE_TRASHED);
  4319.         }
  4320.     }
  4321.  
  4322.     if(creatures)
  4323.     {
  4324.         CreatureVector* creatureVector = fromTile->getCreatures();
  4325.         Creature* creature = NULL;
  4326.         while(creatureVector && !creatureVector->empty())
  4327.         {
  4328.             if((creature = (*creatureVector->begin())))
  4329.                 g_game.internalMoveCreature(NULL, creature, fromTile, toTile, FLAG_NOLIMIT);
  4330.         }
  4331.     }
  4332.  
  4333.     lua_pushboolean(L, true);
  4334.     return 1;
  4335. }
  4336.  
  4337. int32_t LuaInterface::luaDoCleanTile(lua_State* L)
  4338. {
  4339.     //doCleanTile(pos, forceMapLoaded = false)
  4340.     //Remove all items from tile, ignore creatures
  4341.     bool forceMapLoaded = false;
  4342.     if(lua_gettop(L) > 1)
  4343.         forceMapLoaded = popBoolean(L);
  4344.  
  4345.     PositionEx pos;
  4346.     popPosition(L, pos);
  4347.  
  4348.     Tile* tile = g_game.getTile(pos);
  4349.     if(!tile)
  4350.     {
  4351.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  4352.         lua_pushboolean(L, false);
  4353.         return 1;
  4354.     }
  4355.  
  4356.     Thing* thing = NULL;
  4357.     Item* item = NULL;
  4358.  
  4359.     for(int32_t i = tile->getThingCount() - 1; i >= 1; --i) //ignore ground
  4360.     {
  4361.         if(!(thing = tile->__getThing(i)) || !(item = thing->getItem()))
  4362.             continue;
  4363.  
  4364.         if(!item->isLoadedFromMap() || forceMapLoaded)
  4365.             g_game.internalRemoveItem(NULL, item);
  4366.     }
  4367.  
  4368.     lua_pushboolean(L, true);
  4369.     return 1;
  4370. }
  4371.  
  4372. int32_t LuaInterface::luaDoPlayerSendTextMessage(lua_State* L)
  4373. {
  4374.     //doPlayerSendTextMessage(cid, MessageClasses, message[, value[, color[, position]]])
  4375.     int32_t args = lua_gettop(L), value = 0, color = COLOR_WHITE;
  4376.     PositionEx position;
  4377.     if(args > 5)
  4378.         popPosition(L, position);
  4379.  
  4380.     if(args > 4)
  4381.         color = popNumber(L);
  4382.  
  4383.     if(args > 3)
  4384.         value = popNumber(L);
  4385.  
  4386.     std::string text = popString(L);
  4387.     uint32_t messageClass = popNumber(L);
  4388.  
  4389.     ScriptEnviroment* env = getEnv();
  4390.     Player* player = env->getPlayerByUID(popNumber(L));
  4391.     if(!player)
  4392.     {
  4393.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4394.         lua_pushboolean(L, false);
  4395.         return 1;
  4396.     }
  4397.  
  4398.     if(args > 3)
  4399.     {
  4400.         if(!position.x || !position.y)
  4401.             position = player->getPosition();
  4402.  
  4403.         MessageDetails* details = new MessageDetails(value, (Color_t)color);
  4404.         player->sendStatsMessage((MessageClasses)messageClass, text, position, details);
  4405.         delete details;
  4406.     }
  4407.     else
  4408.         player->sendTextMessage((MessageClasses)messageClass, text);
  4409.  
  4410.     lua_pushboolean(L, true);
  4411.     return 1;
  4412. }
  4413.  
  4414. int32_t LuaInterface::luaDoPlayerSendChannelMessage(lua_State* L)
  4415. {
  4416.     //doPlayerSendChannelMessage(cid, author, message, MessageClasses, channel)
  4417.     uint16_t channelId = popNumber(L);
  4418.     uint32_t speakClass = popNumber(L);
  4419.     std::string text = popString(L), name = popString(L);
  4420.  
  4421.     ScriptEnviroment* env = getEnv();
  4422.     Player* player = env->getPlayerByUID(popNumber(L));
  4423.     if(!player)
  4424.     {
  4425.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4426.         lua_pushboolean(L, false);
  4427.         return 1;
  4428.     }
  4429.  
  4430.     player->sendChannelMessage(name, text, (MessageClasses)speakClass, channelId);
  4431.     lua_pushboolean(L, true);
  4432.     return 1;
  4433. }
  4434.  
  4435. int32_t LuaInterface::luaDoPlayerOpenChannel(lua_State* L)
  4436. {
  4437.     //doPlayerOpenChannel(cid, channelId)
  4438.     uint16_t channelId = popNumber(L);
  4439.     uint32_t cid = popNumber(L);
  4440.  
  4441.     ScriptEnviroment* env = getEnv();
  4442.     if(env->getPlayerByUID(cid))
  4443.     {
  4444.         lua_pushboolean(L, g_game.playerOpenChannel(cid, channelId));
  4445.         return 1;
  4446.     }
  4447.  
  4448.     errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4449.     lua_pushboolean(L, false);
  4450.     return 1;
  4451. }
  4452.  
  4453. int32_t LuaInterface::luaDoPlayerSendChannels(lua_State* L)
  4454. {
  4455.     //doPlayerSendChannels(cid[, list])
  4456.     ChannelsList channels;
  4457.     uint32_t params = lua_gettop(L);
  4458.     if(params > 1)
  4459.     {
  4460.         if(!lua_istable(L, -1))
  4461.         {
  4462.             errorEx("Channel list is not a table");
  4463.             lua_pushboolean(L, false);
  4464.             return 1;
  4465.         }
  4466.  
  4467.         lua_pushnil(L);
  4468.         while(lua_next(L, -2))
  4469.         {
  4470.             channels.push_back(std::make_pair((uint16_t)lua_tonumber(L, -2), lua_tostring(L, -1)));
  4471.             lua_pop(L, 1);
  4472.         }
  4473.  
  4474.         lua_pop(L, 1);
  4475.     }
  4476.  
  4477.     ScriptEnviroment* env = getEnv();
  4478.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  4479.     {
  4480.         if(params < 2)
  4481.             channels = g_chat.getChannelList(player);
  4482.  
  4483.         player->sendChannelsDialog(channels);
  4484.         player->setSentChat(params < 2);
  4485.         lua_pushboolean(L, true);
  4486.         return 1;
  4487.     }
  4488.  
  4489.     errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4490.     lua_pushboolean(L, false);
  4491.     return 1;
  4492. }
  4493.  
  4494. int32_t LuaInterface::luaDoSendCreatureSquare(lua_State* L)
  4495. {
  4496.     //doSendCreatureSquare(cid, color[, player])
  4497.     ScriptEnviroment* env = getEnv();
  4498.     SpectatorVec list;
  4499.     if(lua_gettop(L) > 2)
  4500.     {
  4501.         if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  4502.             list.insert(creature);
  4503.     }
  4504.  
  4505.     uint8_t color = popNumber(L);
  4506.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  4507.     {
  4508.         if(!list.empty())
  4509.             g_game.addCreatureSquare(list, creature, color);
  4510.         else
  4511.             g_game.addCreatureSquare(creature, color);
  4512.  
  4513.         lua_pushboolean(L, true);
  4514.     }
  4515.     else
  4516.     {
  4517.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  4518.         lua_pushboolean(L, false);
  4519.     }
  4520.  
  4521.     return 1;
  4522. }
  4523.  
  4524. int32_t LuaInterface::luaDoSendAnimatedText(lua_State* L)
  4525. {
  4526.     //doSendAnimatedText(pos, text, color[, player])
  4527.     ScriptEnviroment* env = getEnv();
  4528.     SpectatorVec list;
  4529.     if(lua_gettop(L) > 3)
  4530.     {
  4531.         if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  4532.             list.insert(creature);
  4533.     }
  4534.  
  4535.     uint8_t color = popNumber(L);
  4536.     std::string text = popString(L);
  4537.  
  4538.     PositionEx pos;
  4539.     popPosition(L, pos);
  4540.     if(pos.x == 0xFFFF)
  4541.         pos = env->getRealPos();
  4542.  
  4543.     if(!list.empty())
  4544.         g_game.addAnimatedText(list, pos, color, text);
  4545.     else
  4546.         g_game.addAnimatedText(pos, color, text);
  4547.  
  4548.     lua_pushboolean(L, true);
  4549.     return 1;
  4550. }
  4551.  
  4552. int32_t LuaInterface::luaGetPlayerSkillLevel(lua_State* L)
  4553. {
  4554.     //getPlayerSkillLevel(cid, skill[, ignoreModifiers = false])
  4555.     bool ignoreModifiers = false;
  4556.     if(lua_gettop(L) > 2)
  4557.         ignoreModifiers = popBoolean(L);
  4558.  
  4559.     uint32_t skill = popNumber(L);
  4560.  
  4561.     ScriptEnviroment* env = getEnv();
  4562.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  4563.     {
  4564.         if(skill <= SKILL_LAST)
  4565.             lua_pushnumber(L, ignoreModifiers ? player->skills[skill][SKILL_LEVEL] :
  4566.                 player->skills[skill][SKILL_LEVEL] + player->getVarSkill((skills_t)skill));
  4567.         else
  4568.             lua_pushboolean(L, false);
  4569.     }
  4570.     else
  4571.     {
  4572.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4573.         lua_pushboolean(L, false);
  4574.     }
  4575.  
  4576.     return 1;
  4577. }
  4578.  
  4579. int32_t LuaInterface::luaGetPlayerSkillTries(lua_State* L)
  4580. {
  4581.     //getPlayerSkillTries(cid, skill)
  4582.     uint32_t skill = popNumber(L);
  4583.  
  4584.     ScriptEnviroment* env = getEnv();
  4585.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  4586.     {
  4587.         if(skill <= SKILL_LAST)
  4588.             lua_pushnumber(L, player->skills[skill][SKILL_TRIES]);
  4589.         else
  4590.             lua_pushboolean(L, false);
  4591.     }
  4592.     else
  4593.     {
  4594.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4595.         lua_pushboolean(L, false);
  4596.     }
  4597.  
  4598.     return 1;
  4599. }
  4600.  
  4601. int32_t LuaInterface::luaDoPlayerSetOfflineTrainingSkill(lua_State* L)
  4602. {
  4603.     //doPlayerSetOfflineTrainingSkill(cid, skillid)
  4604.     uint32_t skillid = (uint32_t)popNumber(L);
  4605.     uint32_t cid = popNumber(L);
  4606.  
  4607.     ScriptEnviroment* env = getEnv();
  4608.  
  4609.     Player* player = env->getPlayerByUID(cid);
  4610.     if(player)
  4611.     {
  4612.         player->setOfflineTrainingSkill(skillid);
  4613.         lua_pushboolean(L, true);
  4614.     }
  4615.     else
  4616.     {
  4617.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4618.         lua_pushboolean(L, false);
  4619.     }
  4620.  
  4621.     return 1;
  4622. }
  4623.  
  4624. int32_t LuaInterface::luaDoCreatureSetDropLoot(lua_State* L)
  4625. {
  4626.     //doCreatureSetDropLoot(cid, doDrop)
  4627.     bool doDrop = popBoolean(L);
  4628.  
  4629.     ScriptEnviroment* env = getEnv();
  4630.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  4631.     {
  4632.         creature->setDropLoot(doDrop ? LOOT_DROP_FULL : LOOT_DROP_NONE);
  4633.         lua_pushboolean(L, true);
  4634.     }
  4635.     else
  4636.     {
  4637.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  4638.         lua_pushboolean(L, false);
  4639.     }
  4640.  
  4641.     return 1;
  4642. }
  4643.  
  4644. int32_t LuaInterface::luaGetPlayerLossPercent(lua_State* L)
  4645. {
  4646.     //getPlayerLossPercent(cid, lossType)
  4647.     uint8_t lossType = (uint8_t)popNumber(L);
  4648.  
  4649.     ScriptEnviroment* env = getEnv();
  4650.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  4651.     {
  4652.         if(lossType <= LOSS_LAST)
  4653.         {
  4654.             uint32_t value = player->getLossPercent((lossTypes_t)lossType);
  4655.             lua_pushnumber(L, value);
  4656.         }
  4657.         else
  4658.             lua_pushboolean(L, false);
  4659.     }
  4660.     else
  4661.     {
  4662.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4663.         lua_pushboolean(L, false);
  4664.     }
  4665.  
  4666.     return 1;
  4667. }
  4668.  
  4669. int32_t LuaInterface::luaDoPlayerSetLossPercent(lua_State* L)
  4670. {
  4671.     //doPlayerSetLossPercent(cid, lossType, newPercent)
  4672.     uint32_t newPercent = popNumber(L);
  4673.     uint8_t lossType = (uint8_t)popNumber(L);
  4674.  
  4675.     ScriptEnviroment* env = getEnv();
  4676.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  4677.     {
  4678.         if(lossType <= LOSS_LAST)
  4679.         {
  4680.             player->setLossPercent((lossTypes_t)lossType, newPercent);
  4681.             lua_pushboolean(L, true);
  4682.         }
  4683.         else
  4684.             lua_pushboolean(L, false);
  4685.     }
  4686.     else
  4687.     {
  4688.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4689.         lua_pushboolean(L, false);
  4690.     }
  4691.  
  4692.     return 1;
  4693. }
  4694.  
  4695. int32_t LuaInterface::luaDoPlayerSetLossSkill(lua_State* L)
  4696. {
  4697.     //doPlayerSetLossSkill(cid, doLose)
  4698.     bool doLose = popBoolean(L);
  4699.  
  4700.     ScriptEnviroment* env = getEnv();
  4701.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  4702.     {
  4703.         player->setLossSkill(doLose);
  4704.         lua_pushboolean(L, true);
  4705.     }
  4706.     else
  4707.     {
  4708.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4709.         lua_pushboolean(L, false);
  4710.     }
  4711.  
  4712.     return 1;
  4713. }
  4714.  
  4715. int32_t LuaInterface::luaDoShowTextDialog(lua_State* L)
  4716. {
  4717.     //doShowTextDialog(cid, itemid[, (text/canWrite)[, (canWrite/length)[, length]]])
  4718.     int32_t length = -1, params = lua_gettop(L);
  4719.     if(params > 4)
  4720.         length = std::abs(popNumber(L));
  4721.  
  4722.     bool canWrite = false;
  4723.     if(params > 3)
  4724.     {
  4725.         if(lua_isboolean(L, -1))
  4726.             canWrite = popBoolean(L);
  4727.         else
  4728.             length = popNumber(L);
  4729.     }
  4730.  
  4731.     std::string text;
  4732.     if(params > 2)
  4733.     {
  4734.         if(lua_isboolean(L, -1))
  4735.             canWrite = popBoolean(L);
  4736.         else
  4737.             text = popString(L);
  4738.     }
  4739.  
  4740.     uint32_t itemId = popNumber(L);
  4741.     ScriptEnviroment* env = getEnv();
  4742.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  4743.     {
  4744.         Item* item = Item::CreateItem(itemId);
  4745.         if(length < 0)
  4746.             length = item->getMaxWriteLength();
  4747.  
  4748.         player->transferContainer.__addThing(NULL, item);
  4749.         if(text.size())
  4750.         {
  4751.             item->setText(text);
  4752.             length = std::max((int32_t)text.size(), length);
  4753.         }
  4754.  
  4755.         player->setWriteItem(item, length);
  4756.         player->transferContainer.setParent(player);
  4757.  
  4758.         player->sendTextWindow(item, length, canWrite);
  4759.         lua_pushboolean(L, true);
  4760.     }
  4761.     else
  4762.     {
  4763.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4764.         lua_pushboolean(L, false);
  4765.     }
  4766.  
  4767.     return 1;
  4768. }
  4769.  
  4770. int32_t LuaInterface::luaDoDecayItem(lua_State* L)
  4771. {
  4772.     //doDecayItem(uid)
  4773.     //Note: to stop decay set decayTo = 0 in items.xml
  4774.     ScriptEnviroment* env = getEnv();
  4775.     if(Item* item = env->getItemByUID(popNumber(L)))
  4776.     {
  4777.         g_game.startDecay(item);
  4778.         lua_pushboolean(L, true);
  4779.     }
  4780.     else
  4781.     {
  4782.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  4783.         lua_pushboolean(L, false);
  4784.     }
  4785.  
  4786.     return 1;
  4787. }
  4788.  
  4789. int32_t LuaInterface::luaGetThingFromPosition(lua_State* L)
  4790. {
  4791.     //getThingFromPosition(pos)
  4792.     // Note:
  4793.     //  stackpos = 255- top thing (movable item or creature)
  4794.     //  stackpos = 254- magic field
  4795.     //  stackpos = 253- top creature
  4796.     PositionEx pos;
  4797.     popPosition(L, pos);
  4798.  
  4799.     ScriptEnviroment* env = getEnv();
  4800.     Thing* thing = NULL;
  4801.     if(Tile* tile = g_game.getMap()->getTile(pos))
  4802.     {
  4803.         if(pos.stackpos == 255)
  4804.         {
  4805.             if(!(thing = tile->getTopCreature()))
  4806.             {
  4807.                 Item* item = tile->getTopDownItem();
  4808.                 if(item && item->isMovable())
  4809.                     thing = item;
  4810.             }
  4811.         }
  4812.         else if(pos.stackpos == 254)
  4813.             thing = tile->getFieldItem();
  4814.         else if(pos.stackpos == 253)
  4815.             thing = tile->getTopCreature();
  4816.         else if(pos.stackpos == 0)
  4817.             thing = tile->ground;
  4818.         else if(pos.stackpos == -1)
  4819.         {
  4820.             lua_pushnumber(L, tile->getThingCount());
  4821.             return 1;
  4822.         }
  4823.         else
  4824.             thing = tile->__getThing(pos.stackpos);
  4825.  
  4826.         if(thing)
  4827.             pushThing(L, thing, env->addThing(thing));
  4828.         else
  4829.             pushThing(L, NULL, 0);
  4830.  
  4831.         return 1;
  4832.     }
  4833.  
  4834.     if(pos.stackpos != -1)
  4835.         pushThing(L, NULL, 0);
  4836.     else
  4837.         lua_pushnil(L);
  4838.  
  4839.     return 1;
  4840. }
  4841.  
  4842. int32_t LuaInterface::luaGetTileItemById(lua_State* L)
  4843. {
  4844.     //getTileItemById(pos, itemId[, subType = -1])
  4845.     ScriptEnviroment* env = getEnv();
  4846.  
  4847.     int32_t subType = -1;
  4848.     if(lua_gettop(L) > 2)
  4849.         subType = (int32_t)popNumber(L);
  4850.  
  4851.     int32_t itemId = (int32_t)popNumber(L);
  4852.     PositionEx pos;
  4853.     popPosition(L, pos);
  4854.  
  4855.     Tile* tile = g_game.getTile(pos);
  4856.     if(!tile)
  4857.     {
  4858.         pushThing(L, NULL, 0);
  4859.         return 1;
  4860.     }
  4861.  
  4862.     Item* item = g_game.findItemOfType(tile, itemId, false, subType);
  4863.     if(!item)
  4864.     {
  4865.         pushThing(L, NULL, 0);
  4866.         return 1;
  4867.     }
  4868.  
  4869.     pushThing(L, item, env->addThing(item));
  4870.     return 1;
  4871. }
  4872.  
  4873. int32_t LuaInterface::luaGetTileItemByType(lua_State* L)
  4874. {
  4875.     //getTileItemByType(pos, type)
  4876.     uint32_t rType = (uint32_t)popNumber(L);
  4877.     if(rType >= ITEM_TYPE_LAST)
  4878.     {
  4879.         errorEx("Not a valid item type");
  4880.         pushThing(L, NULL, 0);
  4881.         return 1;
  4882.     }
  4883.  
  4884.     PositionEx pos;
  4885.     popPosition(L, pos);
  4886.  
  4887.     Tile* tile = g_game.getTile(pos);
  4888.     if(!tile)
  4889.     {
  4890.         pushThing(L, NULL, 0);
  4891.         return 1;
  4892.     }
  4893.  
  4894.     bool found = true;
  4895.     switch((ItemTypes_t)rType)
  4896.     {
  4897.         case ITEM_TYPE_TELEPORT:
  4898.         {
  4899.             if(!tile->hasFlag(TILESTATE_TELEPORT))
  4900.                 found = false;
  4901.  
  4902.             break;
  4903.         }
  4904.         case ITEM_TYPE_MAGICFIELD:
  4905.         {
  4906.             if(!tile->hasFlag(TILESTATE_MAGICFIELD))
  4907.                 found = false;
  4908.  
  4909.             break;
  4910.         }
  4911.         case ITEM_TYPE_MAILBOX:
  4912.         {
  4913.             if(!tile->hasFlag(TILESTATE_MAILBOX))
  4914.                 found = false;
  4915.  
  4916.             break;
  4917.         }
  4918.         case ITEM_TYPE_TRASHHOLDER:
  4919.         {
  4920.             if(!tile->hasFlag(TILESTATE_TRASHHOLDER))
  4921.                 found = false;
  4922.  
  4923.             break;
  4924.         }
  4925.         case ITEM_TYPE_BED:
  4926.         {
  4927.             if(!tile->hasFlag(TILESTATE_BED))
  4928.                 found = false;
  4929.  
  4930.             break;
  4931.         }
  4932.         case ITEM_TYPE_DEPOT:
  4933.         {
  4934.             if(!tile->hasFlag(TILESTATE_DEPOT))
  4935.                 found = false;
  4936.  
  4937.             break;
  4938.         }
  4939.         default:
  4940.             break;
  4941.     }
  4942.  
  4943.     if(!found)
  4944.     {
  4945.         pushThing(L, NULL, 0);
  4946.         return 1;
  4947.     }
  4948.  
  4949.     ScriptEnviroment* env = getEnv();
  4950.     if(tile->ground)
  4951.     {
  4952.         const ItemType& it = Item::items[tile->ground->getID()];
  4953.         if(it.type == (ItemTypes_t)rType)
  4954.         {
  4955.             uint32_t uid = env->addThing(tile->ground);
  4956.             pushThing(L, tile->ground, uid);
  4957.             return 1;
  4958.         }
  4959.     }
  4960.  
  4961.     if(TileItemVector* items = tile->getItemList())
  4962.     {
  4963.         for(ItemVector::const_iterator it = items->begin(), end = items->end(); it != end; ++it)
  4964.         {
  4965.             if(Item::items[(*it)->getID()].type != (ItemTypes_t)rType)
  4966.                 continue;
  4967.  
  4968.             pushThing(L, *it, env->addThing(*it));
  4969.             return 1;
  4970.         }
  4971.     }
  4972.  
  4973.     pushThing(L, NULL, 0);
  4974.     return 1;
  4975. }
  4976.  
  4977. int32_t LuaInterface::luaGetTopCreature(lua_State* L)
  4978. {
  4979.     //getTopCreature(pos)
  4980.     PositionEx pos;
  4981.     popPosition(L, pos);
  4982.  
  4983.     ScriptEnviroment* env = getEnv();
  4984.     Tile* tile = g_game.getTile(pos);
  4985.     if(!tile)
  4986.     {
  4987.         pushThing(L, NULL, 0);
  4988.         return 1;
  4989.     }
  4990.  
  4991.     Thing* thing = tile->getTopCreature();
  4992.     if(!thing || !thing->getCreature())
  4993.     {
  4994.         pushThing(L, NULL, 0);
  4995.         return 1;
  4996.     }
  4997.  
  4998.     pushThing(L, thing, env->addThing(thing));
  4999.     return 1;
  5000. }
  5001.  
  5002. int32_t LuaInterface::luaDoCreateItem(lua_State* L)
  5003. {
  5004.     //doCreateItem(itemid[, type/count = 1], pos)
  5005.     //Returns uid of the created item, only works on tiles.
  5006.     PositionEx pos;
  5007.     popPosition(L, pos);
  5008.  
  5009.     uint32_t count = 1;
  5010.     if(lua_gettop(L) > 1)
  5011.         count = popNumber(L);
  5012.  
  5013.     const ItemType& it = Item::items[popNumber(L)];
  5014.     ScriptEnviroment* env = getEnv();
  5015.  
  5016.     Tile* tile = g_game.getTile(pos);
  5017.     if(!tile)
  5018.     {
  5019.         if(it.group == ITEM_GROUP_GROUND)
  5020.         {
  5021.             Item* item = Item::CreateItem(it.id);
  5022.             tile = IOMap::createTile(item, NULL, pos.x, pos.y, pos.z);
  5023.  
  5024.             g_game.setTile(tile);
  5025.             lua_pushnumber(L, env->addThing(item));
  5026.         }
  5027.         else
  5028.         {
  5029.             errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  5030.             lua_pushboolean(L, false);
  5031.         }
  5032.  
  5033.         return 1;
  5034.     }
  5035.  
  5036.     int32_t itemCount = 1, subType = 1;
  5037.     if(it.hasSubType())
  5038.     {
  5039.         if(it.stackable)
  5040.             itemCount = (int32_t)std::ceil(count / 100.);
  5041.  
  5042.         subType = count;
  5043.     }
  5044.     else
  5045.         itemCount = std::max(1U, count);
  5046.  
  5047.     uint32_t ret = 0;
  5048.     Item* newItem = NULL;
  5049.     while(itemCount > 0)
  5050.     {
  5051.         int32_t stackCount = std::min(100, subType);
  5052.         if(!(newItem = Item::CreateItem(it.id, stackCount)))
  5053.         {
  5054.             errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  5055.             lua_pushboolean(L, false);
  5056.             return ++ret;
  5057.         }
  5058.  
  5059.         if(it.stackable)
  5060.             subType -= stackCount;
  5061.  
  5062.         uint32_t dummy = 0;
  5063.         Item* stackItem = NULL;
  5064.         if(g_game.internalAddItem(NULL, tile, newItem, INDEX_WHEREEVER, FLAG_NOLIMIT, false, dummy, &stackItem) != RET_NOERROR)
  5065.         {
  5066.             delete newItem;
  5067.             lua_pushboolean(L, false);
  5068.             return ++ret;
  5069.         }
  5070.  
  5071.         ++ret;
  5072.         if(newItem->getParent())
  5073.             lua_pushnumber(L, env->addThing(newItem));
  5074.         else if(stackItem)
  5075.             lua_pushnumber(L, env->addThing(stackItem));
  5076.         else
  5077.             lua_pushnil(L);
  5078.  
  5079.         --itemCount;
  5080.     }
  5081.  
  5082.     if(ret)
  5083.         return ret;
  5084.  
  5085.     lua_pushnil(L);
  5086.     return 1;
  5087. }
  5088.  
  5089. int32_t LuaInterface::luaDoCreateItemEx(lua_State* L)
  5090. {
  5091.     //doCreateItemEx(itemid[, count/subType])
  5092.     uint32_t count = 0;
  5093.     if(lua_gettop(L) > 1)
  5094.         count = popNumber(L);
  5095.  
  5096.     ScriptEnviroment* env = getEnv();
  5097.     const ItemType& it = Item::items[(uint32_t)popNumber(L)];
  5098.     if(it.stackable && count > 100)
  5099.         count = 100;
  5100.  
  5101.     Item* newItem = Item::CreateItem(it.id, count);
  5102.     if(!newItem)
  5103.     {
  5104.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  5105.         lua_pushboolean(L, false);
  5106.         return 1;
  5107.     }
  5108.  
  5109.     newItem->setParent(VirtualCylinder::virtualCylinder);
  5110.     env->addTempItem(env, newItem);
  5111.  
  5112.     lua_pushnumber(L, env->addThing(newItem));
  5113.     return 1;
  5114. }
  5115.  
  5116. int32_t LuaInterface::luaDoCreateTeleport(lua_State* L)
  5117. {
  5118.     //doCreateTeleport(itemid, destination, position)
  5119.     PositionEx position;
  5120.     popPosition(L, position);
  5121.     PositionEx destination;
  5122.     popPosition(L, destination);
  5123.  
  5124.     uint32_t itemId = (uint32_t)popNumber(L);
  5125.     ScriptEnviroment* env = getEnv();
  5126.  
  5127.     Tile* tile = g_game.getMap()->getTile(position);
  5128.     if(!tile)
  5129.     {
  5130.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  5131.         lua_pushboolean(L, false);
  5132.         return 1;
  5133.     }
  5134.  
  5135.     Item* newItem = Item::CreateItem(itemId);
  5136.     Teleport* newTeleport = newItem->getTeleport();
  5137.     if(!newTeleport)
  5138.     {
  5139.         delete newItem;
  5140.         errorEx("Item " + asString(itemId) + " is not a teleport.");
  5141.         lua_pushboolean(L, false);
  5142.         return 1;
  5143.     }
  5144.  
  5145.     uint32_t dummy = 0;
  5146.     Item* stackItem = NULL;
  5147.     if(g_game.internalAddItem(NULL, tile, newItem, INDEX_WHEREEVER, FLAG_NOLIMIT, false, dummy, &stackItem) != RET_NOERROR)
  5148.     {
  5149.         delete newItem;
  5150.         lua_pushboolean(L, false);
  5151.         return 1;
  5152.     }
  5153.  
  5154.     newTeleport->setDestination(destination);
  5155.     if(newItem->getParent())
  5156.         lua_pushnumber(L, env->addThing(newItem));
  5157.     else if(stackItem)
  5158.         lua_pushnumber(L, env->addThing(stackItem));
  5159.     else
  5160.         lua_pushnil(L);
  5161.  
  5162.     return 1;
  5163. }
  5164.  
  5165. int32_t LuaInterface::luaGetCreatureStorageList(lua_State* L)
  5166. {
  5167.     //getCreatureStorageList(cid)
  5168.     ScriptEnviroment* env = getEnv();
  5169.  
  5170.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  5171.     {
  5172.         StorageMap::const_iterator it = creature->getStorageBegin();
  5173.         lua_newtable(L);
  5174.         for(uint32_t i = 1; it != creature->getStorageEnd(); ++i, ++it)
  5175.         {
  5176.             lua_pushnumber(L, i);
  5177.             lua_pushstring(L, it->first.c_str());
  5178.             pushTable(L);
  5179.         }
  5180.     }
  5181.     else
  5182.     {
  5183.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  5184.         lua_pushboolean(L, false);
  5185.     }
  5186.  
  5187.     return 1;
  5188. }
  5189.  
  5190. int32_t LuaInterface::luaGetCreatureStorage(lua_State* L)
  5191. {
  5192.     //getCreatureStorage(cid, key)
  5193.     std::string key = popString(L);
  5194.     ScriptEnviroment* env = getEnv();
  5195.     if(Creature * creature = env->getCreatureByUID(popNumber(L))) {
  5196.         std::string strValue;
  5197.         if(creature->getStorage(key, strValue)) {
  5198.  
  5199.             int32_t intValue;
  5200.             STR2INT_ERROR ret = str2int(intValue, strValue.c_str(), 10);
  5201.  
  5202.             if(ret == SUCCESS) { // The value was successfully converted to a number
  5203.                 lua_pushnumber(L, intValue);
  5204.             } else if(ret == OVERFLOWS) {// The value will overflow, so we'll return the maximum possible value.
  5205.                 std::clog << "[Warning - getCreatureStorage] The " << std::string(creature->getPlayer() ? " player [" : " creature [") << creature->getName() << "] has exceeded the MAXIMUM value for the storage [" << key << "]!" << std::endl;
  5206.                 lua_pushnumber(L, INT_MAX);
  5207.             } else if(ret == UNDERFLOWS) {// The value will underflow, so we'll return the minimum possible value.
  5208.                 std::clog << "[Warning - getCreatureStorage] The " << std::string(creature->getPlayer() ? " player [" : " creature [") << creature->getName() << "] has exceeded the MINIMUM value for the storage [" << key << "]!" << std::endl;
  5209.                 lua_pushnumber(L, INT_MIN);
  5210.             } else if(ret == INCONVERTIBLE) {// The value cannot be converted to a number, so we'll return a string.
  5211.                 lua_pushstring(L, strValue.c_str());
  5212.             }
  5213.         } else
  5214.             lua_pushnumber(L, -1);
  5215.     } else {
  5216.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  5217.         lua_pushboolean(L, false);
  5218.     }
  5219.     return 1;
  5220. }
  5221.  
  5222.  
  5223. int32_t LuaInterface::luaDoCreatureSetStorage(lua_State* L)
  5224. {
  5225.     //doCreatureSetStorage(cid, key[, value])
  5226.     std::string value;
  5227.     bool tmp = true;
  5228.     if(lua_gettop(L) > 2)
  5229.     {
  5230.         if(!lua_isnil(L, -1))
  5231.         {
  5232.             if(lua_isnumber(L, -1)) {
  5233.                 value = lua_tostring(L, -1);
  5234.                 popNumber(L);
  5235.             }else
  5236.                 value = popString(L);
  5237.  
  5238.             tmp = false;
  5239.         }
  5240.         else
  5241.             lua_pop(L, 1);
  5242.     }
  5243.  
  5244.     std::string key = popString(L);
  5245.     ScriptEnviroment* env = getEnv();
  5246.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  5247.     {
  5248.         if(!tmp)
  5249.             tmp = creature->setStorage(key, value);
  5250.         else
  5251.             creature->eraseStorage(key);
  5252.  
  5253.         lua_pushboolean(L, tmp);
  5254.     }
  5255.     else
  5256.     {
  5257.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  5258.         lua_pushboolean(L, false);
  5259.     }
  5260.  
  5261.     return 1;
  5262. }
  5263.  
  5264. int32_t LuaInterface::luaGetPlayerSpectators(lua_State* L)
  5265. {
  5266.     ScriptEnviroment* env = getEnv();
  5267.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5268.     {
  5269.         lua_newtable(L);
  5270.         setFieldBool(L, "broadcast", player->client->isBroadcasting());
  5271.         setField(L, "password", player->client->getPassword());
  5272.         setFieldBool(L, "auth", player->client->isAuth());
  5273.  
  5274.         createTable(L, "names");
  5275.         StringVec t = player->client->list();
  5276.  
  5277.         StringVec::const_iterator it = t.begin();
  5278.         for(uint32_t i = 1; it != t.end(); ++it, ++i)
  5279.         {
  5280.             lua_pushnumber(L, i);
  5281.             lua_pushstring(L, (*it).c_str());
  5282.             pushTable(L);
  5283.         }
  5284.  
  5285.         pushTable(L);
  5286.         createTable(L, "mutes");
  5287.         t = player->client->muteList();
  5288.  
  5289.         it = t.begin();
  5290.         for(uint32_t i = 1; it != t.end(); ++it, ++i)
  5291.         {
  5292.             lua_pushnumber(L, i);
  5293.             lua_pushstring(L, (*it).c_str());
  5294.             pushTable(L);
  5295.         }
  5296.  
  5297.         pushTable(L);
  5298.         createTable(L, "bans");
  5299.         std::map<std::string, uint32_t> _t = player->client->banList();
  5300.  
  5301.         std::map<std::string, uint32_t>::const_iterator _it = _t.begin();
  5302.         for(uint32_t i = 1; _it != _t.end(); ++_it, ++i)
  5303.         {
  5304.             lua_pushnumber(L, i);
  5305.             lua_pushstring(L, _it->first.c_str());
  5306.             pushTable(L);
  5307.         }
  5308.  
  5309.         pushTable(L);
  5310.         createTable(L, "kick");
  5311.         pushTable(L);
  5312.     }
  5313.     else
  5314.     {
  5315.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5316.         lua_pushboolean(L, false);
  5317.     }
  5318.  
  5319.     return 1;
  5320. }
  5321.  
  5322. int32_t LuaInterface::luaDoPlayerSetSpectators(lua_State* L)
  5323. {
  5324.     std::string password = getFieldString(L, "password");
  5325.     bool broadcast = getFieldBool(L, "broadcast"),
  5326.         auth = getFieldBool(L, "auth");
  5327.  
  5328.     StringVec m, b, k;
  5329.     lua_pushstring(L, "mutes");
  5330.     lua_gettable(L, -2);
  5331.  
  5332.     lua_pushnil(L);
  5333.     while(lua_next(L, -2))
  5334.     {
  5335.         m.push_back(asLowerCaseString(lua_tostring(L, -1)));
  5336.         lua_pop(L, 1);
  5337.     }
  5338.  
  5339.     lua_pop(L, 1);
  5340.     lua_pushstring(L, "bans");
  5341.     lua_gettable(L, -2);
  5342.  
  5343.     lua_pushnil(L);
  5344.     while(lua_next(L, -2))
  5345.     {
  5346.         b.push_back(asLowerCaseString(lua_tostring(L, -1)));
  5347.         lua_pop(L, 1);
  5348.     }
  5349.  
  5350.     lua_pop(L, 1);
  5351.     lua_pushstring(L, "kick");
  5352.     lua_gettable(L, -2);
  5353.  
  5354.     lua_pushnil(L);
  5355.     while(lua_next(L, -2))
  5356.     {
  5357.         k.push_back(asLowerCaseString(lua_tostring(L, -1)));
  5358.         lua_pop(L, 1);
  5359.     }
  5360.  
  5361.     lua_pop(L, 2);
  5362.     ScriptEnviroment* env = getEnv();
  5363.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5364.     {
  5365.         if(player->client->getPassword() != password && !password.empty())
  5366.             player->client->clear(false);
  5367.  
  5368.         player->client->setPassword(password);
  5369.         if(!broadcast && player->client->isBroadcasting())
  5370.             player->client->clear(false);
  5371.  
  5372.         player->client->kick(k);
  5373.         player->client->mute(m);
  5374.         player->client->ban(b);
  5375.  
  5376.         player->client->setBroadcast(broadcast);
  5377.         player->client->setAuth(auth);
  5378.         lua_pushboolean(L, true);
  5379.     }
  5380.     else
  5381.     {
  5382.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5383.         lua_pushboolean(L, false);
  5384.     }
  5385.  
  5386.     return 1;
  5387. }
  5388.  
  5389. int32_t LuaInterface::luaGetTileInfo(lua_State* L)
  5390. {
  5391.     //getTileInfo(pos)
  5392.     PositionEx pos;
  5393.     popPosition(L, pos);
  5394.     if(Tile* tile = g_game.getMap()->getTile(pos))
  5395.     {
  5396.         ScriptEnviroment* env = getEnv();
  5397.         pushThing(L, tile->ground, env->addThing(tile->ground));
  5398.  
  5399.         setFieldBool(L, "protection", tile->hasFlag(TILESTATE_PROTECTIONZONE));
  5400.         setFieldBool(L, "optional", tile->hasFlag(TILESTATE_OPTIONALZONE));
  5401.         setFieldBool(L, "hardcore", tile->hasFlag(TILESTATE_HARDCOREZONE));
  5402.         setFieldBool(L, "noLogout", tile->hasFlag(TILESTATE_NOLOGOUT));
  5403.         setFieldBool(L, "refresh", tile->hasFlag(TILESTATE_REFRESH));
  5404.         setFieldBool(L, "trashed", tile->hasFlag(TILESTATE_TRASHED));
  5405.         setFieldBool(L, "magicField", tile->hasFlag(TILESTATE_MAGICFIELD));
  5406.         setFieldBool(L, "trashHolder", tile->hasFlag(TILESTATE_TRASHHOLDER));
  5407.         setFieldBool(L, "mailbox", tile->hasFlag(TILESTATE_MAILBOX));
  5408.         setFieldBool(L, "depot", tile->hasFlag(TILESTATE_DEPOT));
  5409.         setFieldBool(L, "bed", tile->hasFlag(TILESTATE_BED));
  5410.  
  5411.         createTable(L, "floorChange");
  5412.         for(int32_t i = CHANGE_FIRST; i <= CHANGE_LAST; ++i)
  5413.         {
  5414.             lua_pushnumber(L, i);
  5415.             lua_pushboolean(L, tile->floorChange((FloorChange_t)i));
  5416.             pushTable(L);
  5417.         }
  5418.  
  5419.         pushTable(L);
  5420.         setFieldBool(L, "teleport", tile->hasFlag(TILESTATE_TELEPORT));
  5421.  
  5422.         setField(L, "things", tile->getThingCount());
  5423.         setField(L, "creatures", tile->getCreatureCount());
  5424.         setField(L, "items", tile->getItemCount());
  5425.         setField(L, "topItems", tile->getTopItemCount());
  5426.         setField(L, "downItems", tile->getDownItemCount());
  5427.         if(House* house = tile->getHouse())
  5428.             setField(L, "house", house->getId());
  5429.     }
  5430.     else
  5431.     {
  5432.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  5433.         lua_pushboolean(L, false);
  5434.     }
  5435.  
  5436.     return 1;
  5437. }
  5438.  
  5439. int32_t LuaInterface::luaGetHouseFromPosition(lua_State* L)
  5440. {
  5441.     //getHouseFromPosition(pos)
  5442.     PositionEx pos;
  5443.     popPosition(L, pos);
  5444.  
  5445.     Tile* tile = g_game.getMap()->getTile(pos);
  5446.     if(!tile)
  5447.     {
  5448.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  5449.         lua_pushboolean(L, false);
  5450.         return 1;
  5451.     }
  5452.  
  5453.     HouseTile* houseTile = tile->getHouseTile();
  5454.     if(!houseTile)
  5455.     {
  5456.         lua_pushboolean(L, false);
  5457.         return 1;
  5458.     }
  5459.  
  5460.     House* house = houseTile->getHouse();
  5461.     if(!house)
  5462.     {
  5463.         lua_pushboolean(L, false);
  5464.         return 1;
  5465.     }
  5466.  
  5467.     lua_pushnumber(L, house->getId());
  5468.     return 1;
  5469. }
  5470.  
  5471. int32_t LuaInterface::luaDoCreateMonster(lua_State* L)
  5472. {
  5473.     //doCreateMonster(name, pos[, extend = false[, force = false]])
  5474.     bool force = false, extend = false;
  5475.     int32_t params = lua_gettop(L);
  5476.     if(params > 3)
  5477.         force = popBoolean(L);
  5478.  
  5479.     if(params > 2)
  5480.         extend = popBoolean(L);
  5481.  
  5482.     PositionEx pos;
  5483.     popPosition(L, pos);
  5484.  
  5485.     std::string name = popString(L);
  5486.     Monster* monster = Monster::createMonster(name.c_str());
  5487.     if(!monster)
  5488.     {
  5489.         errorEx("Monster with name '" + name + "' not found");
  5490.         lua_pushboolean(L, false);
  5491.         return 1;
  5492.     }
  5493.  
  5494.     if(!g_game.placeCreature(monster, pos, extend, force))
  5495.     {
  5496.         delete monster;
  5497.         errorEx("Cannot create monster: " + name);
  5498.  
  5499.         lua_pushboolean(L, false);
  5500.         return 1;
  5501.     }
  5502.  
  5503.     ScriptEnviroment* env = getEnv();
  5504.     lua_pushnumber(L, env->addThing((Thing*)monster));
  5505.     return 1;
  5506. }
  5507.  
  5508. int32_t LuaInterface::luaDoCreateNpc(lua_State* L)
  5509. {
  5510.     //doCreateNpc(name, pos)
  5511.     PositionEx pos;
  5512.     popPosition(L, pos);
  5513.     std::string name = popString(L);
  5514.  
  5515.     Npc* npc = Npc::createNpc(name.c_str());
  5516.     if(!npc)
  5517.     {
  5518.         errorEx("Npc with name '" + name + "' not found");
  5519.         lua_pushboolean(L, false);
  5520.         return 1;
  5521.     }
  5522.  
  5523.     if(!g_game.placeCreature(npc, pos))
  5524.     {
  5525.         delete npc;
  5526.         errorEx("Cannot create npc: " + name);
  5527.  
  5528.         lua_pushboolean(L, true); //for scripting compatibility
  5529.         return 1;
  5530.     }
  5531.  
  5532.     ScriptEnviroment* env = getEnv();
  5533.     lua_pushnumber(L, env->addThing((Thing*)npc));
  5534.     return 1;
  5535. }
  5536.  
  5537. int32_t LuaInterface::luaDoRemoveCreature(lua_State* L)
  5538. {
  5539.     //doRemoveCreature(cid[, forceLogout = true])
  5540.     bool forceLogout = true;
  5541.     if(lua_gettop(L) > 1)
  5542.         forceLogout = popBoolean(L);
  5543.  
  5544.     ScriptEnviroment* env = getEnv();
  5545.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  5546.     {
  5547.         if(Player* player = creature->getPlayer())
  5548.             player->kick(true, forceLogout); //Players will get kicked without restrictions
  5549.         else
  5550.             g_game.removeCreature(creature); //Monsters/NPCs will get removed
  5551.  
  5552.         lua_pushboolean(L, true);
  5553.     }
  5554.     else
  5555.     {
  5556.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  5557.         lua_pushboolean(L, false);
  5558.     }
  5559.  
  5560.     return 1;
  5561. }
  5562.  
  5563. int32_t LuaInterface::luaDoPlayerAddMoney(lua_State* L)
  5564. {
  5565.     //doPlayerAddMoney(cid, money)
  5566.     uint64_t money = popNumber(L);
  5567.  
  5568.     ScriptEnviroment* env = getEnv();
  5569.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5570.     {
  5571.         g_game.addMoney(player, money);
  5572.         lua_pushboolean(L, true);
  5573.     }
  5574.     else
  5575.     {
  5576.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5577.         lua_pushboolean(L, false);
  5578.     }
  5579.  
  5580.     return 1;
  5581. }
  5582.  
  5583. int32_t LuaInterface::luaDoPlayerRemoveMoney(lua_State* L)
  5584. {
  5585.     //doPlayerRemoveMoney(cid, money [, canDrop = true])
  5586.     bool canDrop = true;
  5587.    
  5588.     if (lua_gettop(L) > 2)
  5589.         canDrop = popBoolean(L);
  5590.  
  5591.     uint64_t money = popNumber(L);
  5592.  
  5593.     ScriptEnviroment* env = getEnv();
  5594.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5595.         lua_pushboolean(L, g_game.removeMoney(player, money, 0, canDrop));
  5596.     else
  5597.     {
  5598.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5599.         lua_pushboolean(L, false);
  5600.     }
  5601.  
  5602.     return 1;
  5603. }
  5604.  
  5605. int32_t LuaInterface::luaDoPlayerTransferMoneyTo(lua_State* L)
  5606. {
  5607.     //doPlayerTransferMoneyTo(cid, target, money)
  5608.     uint64_t money = popNumber(L);
  5609.     std::string target = popString(L);
  5610.  
  5611.     ScriptEnviroment* env = getEnv();
  5612.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5613.         lua_pushboolean(L, player->transferMoneyTo(target, money));
  5614.     else
  5615.     {
  5616.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5617.         lua_pushboolean(L, false);
  5618.     }
  5619.  
  5620.     return 1;
  5621. }
  5622.  
  5623. int32_t LuaInterface::luaDoPlayerSetPzLocked(lua_State* L)
  5624. {
  5625.     //doPlayerSetPzLocked(cid, locked)
  5626.     bool locked = popBoolean(L);
  5627.  
  5628.     ScriptEnviroment* env = getEnv();
  5629.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5630.     {
  5631.         if(player->isPzLocked() != locked)
  5632.         {
  5633.             player->setPzLocked(locked);
  5634.             player->sendIcons();
  5635.         }
  5636.  
  5637.         lua_pushboolean(L, true);
  5638.     }
  5639.     else
  5640.     {
  5641.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5642.         lua_pushboolean(L, false);
  5643.     }
  5644.  
  5645.     return 1;
  5646. }
  5647.  
  5648. int32_t LuaInterface::luaDoPlayerSetTown(lua_State* L)
  5649. {
  5650.     //doPlayerSetTown(cid, townid)
  5651.     uint32_t townid = (uint32_t)popNumber(L);
  5652.  
  5653.     ScriptEnviroment* env = getEnv();
  5654.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5655.     {
  5656.         if(Town* town = Towns::getInstance()->getTown(townid))
  5657.         {
  5658.             player->setMasterPosition(town->getPosition());
  5659.             player->setTown(townid);
  5660.             lua_pushboolean(L, true);
  5661.         }
  5662.         else
  5663.             lua_pushboolean(L, false);
  5664.     }
  5665.     else
  5666.     {
  5667.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5668.         lua_pushboolean(L, false);
  5669.     }
  5670.  
  5671.     return 1;
  5672. }
  5673.  
  5674. int32_t LuaInterface::luaDoPlayerSetVocation(lua_State* L)
  5675. {
  5676.     //doPlayerSetVocation(cid, voc)
  5677.     uint32_t voc = popNumber(L);
  5678.  
  5679.     ScriptEnviroment* env = getEnv();
  5680.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5681.     {
  5682.         player->setVocation(voc);
  5683.         lua_pushboolean(L, true);
  5684.     }
  5685.     else
  5686.     {
  5687.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5688.         lua_pushboolean(L, false);
  5689.     }
  5690.  
  5691.     return 1;
  5692. }
  5693.  
  5694. int32_t LuaInterface::luaDoPlayerSetSex(lua_State* L)
  5695. {
  5696.     //doPlayerSetSex(cid, sex)
  5697.     uint32_t newSex = popNumber(L);
  5698.  
  5699.     ScriptEnviroment* env = getEnv();
  5700.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5701.     {
  5702.         player->setSex(newSex);
  5703.         lua_pushboolean(L, true);
  5704.     }
  5705.     else
  5706.     {
  5707.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5708.         lua_pushboolean(L, false);
  5709.     }
  5710.  
  5711.     return 1;
  5712. }
  5713.  
  5714. int32_t LuaInterface::luaDoPlayerAddSoul(lua_State* L)
  5715. {
  5716.     //doPlayerAddSoul(cid, amount)
  5717.     int32_t amount = popNumber(L);
  5718.  
  5719.     ScriptEnviroment* env = getEnv();
  5720.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5721.     {
  5722.         player->changeSoul(amount);
  5723.         lua_pushboolean(L, true);
  5724.     }
  5725.     else
  5726.     {
  5727.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5728.         lua_pushboolean(L, false);
  5729.     }
  5730.  
  5731.     return 1;
  5732. }
  5733.  
  5734. int32_t LuaInterface::luaDoPlayerSetExtraAttackSpeed(lua_State *L)
  5735. {
  5736.     uint32_t speed = popNumber(L);
  5737.  
  5738.     ScriptEnviroment* env = getEnv();
  5739.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5740.     {
  5741.         player->setPlayerExtraAttackSpeed(speed);
  5742.         lua_pushnumber(L, true);
  5743.     }
  5744.     else
  5745.     {
  5746.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5747.         lua_pushnumber(L, false);
  5748.     }
  5749.  
  5750.     return 1;
  5751. }
  5752.  
  5753. int32_t LuaInterface::luaGetPlayerItemCount(lua_State* L)
  5754. {
  5755.     //getPlayerItemCount(cid, itemid[, subType = -1])
  5756.     int32_t subType = -1;
  5757.     if(lua_gettop(L) > 2)
  5758.         subType = popNumber(L);
  5759.  
  5760.     uint32_t itemId = popNumber(L);
  5761.     ScriptEnviroment* env = getEnv();
  5762.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  5763.         lua_pushnumber(L, player->__getItemTypeCount(itemId, subType));
  5764.     else
  5765.     {
  5766.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5767.         lua_pushboolean(L, false);
  5768.     }
  5769.  
  5770.     return 1;
  5771. }
  5772.  
  5773. int32_t LuaInterface::luaGetHouseInfo(lua_State* L)
  5774. {
  5775.     //getHouseInfo(houseId[, full = true])
  5776.     bool full = true;
  5777.     if(lua_gettop(L) > 1)
  5778.         full = popBoolean(L);
  5779.  
  5780.     House* house = Houses::getInstance()->getHouse(popNumber(L));
  5781.     if(!house)
  5782.     {
  5783.         errorEx(getError(LUA_ERROR_HOUSE_NOT_FOUND));
  5784.         lua_pushboolean(L, false);
  5785.         return 1;
  5786.     }
  5787.  
  5788.     lua_newtable(L);
  5789.     setField(L, "id", house->getId());
  5790.     setField(L, "name", house->getName().c_str());
  5791.     setField(L, "owner", house->getOwner());
  5792.  
  5793.     lua_pushstring(L, "entry");
  5794.     pushPosition(L, house->getEntry(), 0);
  5795.     pushTable(L);
  5796.  
  5797.     setField(L, "rent", house->getRent());
  5798.     setField(L, "price", house->getPrice());
  5799.     setField(L, "town", house->getTownId());
  5800.     setField(L, "paidUntil", house->getPaidUntil());
  5801.     setField(L, "warnings", house->getRentWarnings());
  5802.     setField(L, "lastWarning", house->getLastWarning());
  5803.  
  5804.     setFieldBool(L, "guildHall", house->isGuild());
  5805.     setField(L, "size", house->getSize());
  5806.  
  5807.     if(full)
  5808.     {
  5809.         createTable(L, "doors");
  5810.  
  5811.         HouseDoorList::iterator dit = house->getHouseDoorBegin();
  5812.         for(uint32_t i = 1; dit != house->getHouseDoorEnd(); ++dit, ++i)
  5813.         {
  5814.             lua_pushnumber(L, i);
  5815.             pushPosition(L, (*dit)->getPosition(), 0);
  5816.             pushTable(L);
  5817.         }
  5818.  
  5819.         pushTable(L);
  5820.         createTable(L, "beds");
  5821.  
  5822.         HouseBedList::iterator bit = house->getHouseBedsBegin();
  5823.         for(uint32_t i = 1; bit != house->getHouseBedsEnd(); ++bit, ++i)
  5824.         {
  5825.             lua_pushnumber(L, i);
  5826.             pushPosition(L, (*bit)->getPosition(), 0);
  5827.             pushTable(L);
  5828.         }
  5829.  
  5830.         pushTable(L);
  5831.         createTable(L, "tiles");
  5832.  
  5833.         HouseTileList::iterator tit = house->getHouseTileBegin();
  5834.         for(uint32_t i = 1; tit != house->getHouseTileEnd(); ++tit, ++i)
  5835.         {
  5836.             lua_pushnumber(L, i);
  5837.             pushPosition(L, (*tit)->getPosition(), 0);
  5838.             pushTable(L);
  5839.         }
  5840.  
  5841.         pushTable(L);
  5842.     }
  5843.  
  5844.     return 1;
  5845. }
  5846.  
  5847. int32_t LuaInterface::luaGetHouseAccessList(lua_State* L)
  5848. {
  5849.     //getHouseAccessList(houseid, listid)
  5850.     uint32_t listid = popNumber(L);
  5851.     if(House* house = Houses::getInstance()->getHouse(popNumber(L)))
  5852.     {
  5853.         std::string list;
  5854.         if(house->getAccessList(listid, list))
  5855.             lua_pushstring(L, list.c_str());
  5856.         else
  5857.             lua_pushnil(L);
  5858.     }
  5859.     else
  5860.     {
  5861.         errorEx(getError(LUA_ERROR_HOUSE_NOT_FOUND));
  5862.         lua_pushnil(L);
  5863.     }
  5864.  
  5865.     return 1;
  5866. }
  5867.  
  5868. int32_t LuaInterface::luaGetHouseByPlayerGUID(lua_State* L)
  5869. {
  5870.     //getHouseByPlayerGUID(guid)
  5871.     if(House* house = Houses::getInstance()->getHouseByPlayerId(popNumber(L)))
  5872.         lua_pushnumber(L, house->getId());
  5873.     else
  5874.         lua_pushnil(L);
  5875.     return 1;
  5876. }
  5877.  
  5878. int32_t LuaInterface::luaSetHouseAccessList(lua_State* L)
  5879. {
  5880.     //setHouseAccessList(houseid, listid, listtext)
  5881.     std::string list = popString(L);
  5882.     uint32_t listid = popNumber(L);
  5883.  
  5884.     if(House* house = Houses::getInstance()->getHouse(popNumber(L)))
  5885.     {
  5886.         house->setAccessList(listid, list);
  5887.         lua_pushboolean(L, true);
  5888.     }
  5889.     else
  5890.     {
  5891.         errorEx(getError(LUA_ERROR_HOUSE_NOT_FOUND));
  5892.         lua_pushboolean(L, false);
  5893.     }
  5894.  
  5895.     return 1;
  5896. }
  5897.  
  5898. int32_t LuaInterface::luaSetHouseOwner(lua_State* L)
  5899. {
  5900.     //setHouseOwner(houseId, owner[, clean = true])
  5901.     bool clean = true;
  5902.     if(lua_gettop(L) > 2)
  5903.         clean = popBoolean(L);
  5904.  
  5905.     uint32_t owner = popNumber(L);
  5906.     if(House* house = Houses::getInstance()->getHouse(popNumber(L)))
  5907.         lua_pushboolean(L, house->setOwnerEx(owner, clean));
  5908.     else
  5909.     {
  5910.         errorEx(getError(LUA_ERROR_HOUSE_NOT_FOUND));
  5911.         lua_pushboolean(L, false);
  5912.     }
  5913.  
  5914.     return 1;
  5915. }
  5916.  
  5917. int32_t LuaInterface::luaGetWorldType(lua_State* L)
  5918. {
  5919.     lua_pushnumber(L, (uint32_t)g_game.getWorldType(NULL));
  5920.     return 1;
  5921. }
  5922.  
  5923. int32_t LuaInterface::luaSetWorldType(lua_State* L)
  5924. {
  5925.     //setWorldType(type)
  5926.     WorldType_t type = (WorldType_t)popNumber(L);
  5927.     if(type >= WORLDTYPE_FIRST && type <= WORLDTYPE_LAST)
  5928.     {
  5929.         g_game.setWorldType(type);
  5930.         lua_pushboolean(L, true);
  5931.     }
  5932.     else
  5933.         lua_pushboolean(L, false);
  5934.  
  5935.     return 1;
  5936. }
  5937.  
  5938. int32_t LuaInterface::luaGetWorldTime(lua_State* L)
  5939. {
  5940.     //getWorldTime()
  5941.     lua_pushnumber(L, g_game.getLightHour());
  5942.     return 1;
  5943. }
  5944.  
  5945. int32_t LuaInterface::luaGetWorldLight(lua_State* L)
  5946. {
  5947.     //getWorldLight()
  5948.     LightInfo lightInfo;
  5949.     g_game.getWorldLightInfo(lightInfo);
  5950.  
  5951.     lua_pushnumber(L, lightInfo.level);
  5952.     lua_pushnumber(L, lightInfo.color);
  5953.     return 2;
  5954. }
  5955.  
  5956. int32_t LuaInterface::luaGetWorldCreatures(lua_State* L)
  5957. {
  5958.     //getWorldCreatures(type)
  5959.     //0 players, 1 monsters, 2 npcs, 3 all
  5960.     uint32_t type = popNumber(L), value;
  5961.     switch(type)
  5962.     {
  5963.         case 0:
  5964.             value = g_game.getPlayersOnline();
  5965.             break;
  5966.         case 1:
  5967.             value = g_game.getMonstersOnline();
  5968.             break;
  5969.         case 2:
  5970.             value = g_game.getNpcsOnline();
  5971.             break;
  5972.         case 3:
  5973.             value = g_game.getCreaturesOnline();
  5974.             break;
  5975.         default:
  5976.             lua_pushboolean(L, false);
  5977.             return 1;
  5978.     }
  5979.  
  5980.     lua_pushnumber(L, value);
  5981.     return 1;
  5982. }
  5983.  
  5984. int32_t LuaInterface::luaGetWorldUpTime(lua_State* L)
  5985. {
  5986.     //getWorldUpTime()
  5987.     lua_pushnumber(L, (OTSYS_TIME() - ProtocolStatus::start) / 1000);
  5988.     return 1;
  5989. }
  5990.  
  5991. int32_t LuaInterface::luaGetPlayerLight(lua_State* L)
  5992. {
  5993.     //getPlayerLight(cid)
  5994.     ScriptEnviroment* env = getEnv();
  5995.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  5996.     {
  5997.         LightInfo lightInfo;
  5998.         player->getCreatureLight(lightInfo);
  5999.  
  6000.         lua_pushnumber(L, lightInfo.level);
  6001.         lua_pushnumber(L, lightInfo.color);
  6002.         return 2;
  6003.     }
  6004.     else
  6005.     {
  6006.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  6007.         lua_pushboolean(L, false);
  6008.         return 1;
  6009.     }
  6010. }
  6011.  
  6012. int32_t LuaInterface::luaGetPlayerSoul(lua_State* L)
  6013. {
  6014.     //getPlayerSoul(cid[, ignoreModifiers = false])
  6015.     bool ignoreModifiers = false;
  6016.     if(lua_gettop(L) > 1)
  6017.         ignoreModifiers = popBoolean(L);
  6018.  
  6019.     ScriptEnviroment* env = getEnv();
  6020.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  6021.         lua_pushnumber(L, ignoreModifiers ? player->soul : player->getSoul());
  6022.     else
  6023.     {
  6024.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  6025.         lua_pushboolean(L, false);
  6026.     }
  6027.  
  6028.     return 1;
  6029. }
  6030.  
  6031. int32_t LuaInterface::luaDoPlayerAddExperience(lua_State* L)
  6032. {
  6033.     //doPlayerAddExperience(cid, amount)
  6034.     int64_t amount = popNumber(L);
  6035.  
  6036.     ScriptEnviroment* env = getEnv();
  6037.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  6038.     {
  6039.         if(amount > 0)
  6040.             player->addExperience(amount);
  6041.         else if(amount < 0)
  6042.             player->removeExperience(std::abs(amount));
  6043.         else
  6044.         {
  6045.             lua_pushboolean(L, false);
  6046.             return 1;
  6047.         }
  6048.  
  6049.         lua_pushboolean(L, true);
  6050.     }
  6051.     else
  6052.     {
  6053.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  6054.         lua_pushboolean(L, false);
  6055.     }
  6056.  
  6057.     return 1;
  6058. }
  6059.  
  6060. int32_t LuaInterface::luaGetPlayerSlotItem(lua_State* L)
  6061. {
  6062.     //getPlayerSlotItem(cid, slot)
  6063.     uint32_t slot = popNumber(L);
  6064.  
  6065.     ScriptEnviroment* env = getEnv();
  6066.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  6067.     {
  6068.         if(Thing* thing = player->__getThing(slot))
  6069.             pushThing(L, thing, env->addThing(thing));
  6070.         else
  6071.             pushThing(L, NULL, 0);
  6072.     }
  6073.     else
  6074.     {
  6075.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  6076.         pushThing(L, NULL, 0);
  6077.     }
  6078.  
  6079.     return 1;
  6080. }
  6081.  
  6082. int32_t LuaInterface::luaGetPlayerWeapon(lua_State* L)
  6083. {
  6084.     //getPlayerWeapon(cid[, ignoreAmmo = false])
  6085.     bool ignoreAmmo = false;
  6086.     if(lua_gettop(L) > 1)
  6087.         ignoreAmmo = popBoolean(L);
  6088.  
  6089.     ScriptEnviroment* env = getEnv();
  6090.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  6091.     {
  6092.         if(Item* weapon = player->getWeapon(ignoreAmmo))
  6093.             pushThing(L, weapon, env->addThing(weapon));
  6094.         else
  6095.             pushThing(L, NULL, 0);
  6096.     }
  6097.     else
  6098.     {
  6099.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  6100.         lua_pushnil(L);
  6101.     }
  6102.  
  6103.     return 1;
  6104. }
  6105.  
  6106. int32_t LuaInterface::luaGetPlayerItemById(lua_State* L)
  6107. {
  6108.     //getPlayerItemById(cid, deepSearch, itemId[, subType = -1])
  6109.     ScriptEnviroment* env = getEnv();
  6110.  
  6111.     int32_t subType = -1;
  6112.     if(lua_gettop(L) > 3)
  6113.         subType = (int32_t)popNumber(L);
  6114.  
  6115.     int32_t itemId = (int32_t)popNumber(L);
  6116.     bool deepSearch = popBoolean(L);
  6117.  
  6118.     Player* player = env->getPlayerByUID(popNumber(L));
  6119.     if(!player)
  6120.     {
  6121.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  6122.         pushThing(L, NULL, 0);
  6123.         return 1;
  6124.     }
  6125.  
  6126.     Item* item = g_game.findItemOfType(player, itemId, deepSearch, subType);
  6127.     if(!item)
  6128.     {
  6129.         pushThing(L, NULL, 0);
  6130.         return 1;
  6131.     }
  6132.  
  6133.     pushThing(L, item, env->addThing(item));
  6134.     return 1;
  6135. }
  6136.  
  6137. int32_t LuaInterface::luaGetThing(lua_State* L)
  6138. {
  6139.     //getThing(uid[, recursive = RECURSE_FIRST])
  6140.     Recursive_t recursive = RECURSE_FIRST;
  6141.     if(lua_gettop(L) > 1)
  6142.         recursive = (Recursive_t)popNumber(L);
  6143.  
  6144.     uint32_t uid = popNumber(L);
  6145.     ScriptEnviroment* env = getEnv();
  6146.     if(Thing* thing = env->getThingByUID(uid))
  6147.         pushThing(L, thing, uid, recursive);
  6148.     else
  6149.     {
  6150.         errorEx(getError(LUA_ERROR_THING_NOT_FOUND));
  6151.         pushThing(L, NULL, 0);
  6152.     }
  6153.  
  6154.     return 1;
  6155. }
  6156.  
  6157. int32_t LuaInterface::luaDoTileQueryAdd(lua_State* L)
  6158. {
  6159.     //doTileQueryAdd(uid, pos[, flags])
  6160.     uint32_t flags = 0, params = lua_gettop(L);
  6161.     if(params > 2)
  6162.         flags = popNumber(L);
  6163.  
  6164.     PositionEx pos;
  6165.     popPosition(L, pos);
  6166.     uint32_t uid = popNumber(L);
  6167.  
  6168.     ScriptEnviroment* env = getEnv();
  6169.     Tile* tile = g_game.getTile(pos);
  6170.     if(!tile)
  6171.     {
  6172.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  6173.         lua_pushnumber(L, (uint32_t)RET_NOTPOSSIBLE);
  6174.         return 1;
  6175.     }
  6176.  
  6177.     Thing* thing = env->getThingByUID(uid);
  6178.     if(!thing)
  6179.     {
  6180.         errorEx(getError(LUA_ERROR_THING_NOT_FOUND));
  6181.         lua_pushnumber(L, (uint32_t)RET_NOTPOSSIBLE);
  6182.         return 1;
  6183.     }
  6184.  
  6185.     lua_pushnumber(L, (uint32_t)tile->__queryAdd(0, thing, 1, flags));
  6186.     return 1;
  6187. }
  6188.  
  6189. int32_t LuaInterface::luaDoItemRaidUnref(lua_State* L)
  6190. {
  6191.     //doItemRaidUnref(uid)
  6192.     ScriptEnviroment* env = getEnv();
  6193.     if(Item* item = env->getItemByUID(popNumber(L)))
  6194.     {
  6195.         if(Raid* raid = item->getRaid())
  6196.         {
  6197.             raid->unRef();
  6198.             item->setRaid(NULL);
  6199.             lua_pushboolean(L, true);
  6200.         }
  6201.         else
  6202.             lua_pushboolean(L, false);
  6203.     }
  6204.     else
  6205.     {
  6206.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  6207.         lua_pushboolean(L, false);
  6208.     }
  6209.  
  6210.     return 1;
  6211. }
  6212.  
  6213. int32_t LuaInterface::luaGetThingPosition(lua_State* L)
  6214. {
  6215.     //getThingPosition(uid)
  6216.     ScriptEnviroment* env = getEnv();
  6217.     if(Thing* thing = env->getThingByUID(popNumber(L)))
  6218.     {
  6219.         Position pos = thing->getPosition();
  6220.         uint32_t stackpos = 0;
  6221.         if(Tile* tile = thing->getTile())
  6222.             stackpos = tile->__getIndexOfThing(thing);
  6223.  
  6224.         pushPosition(L, pos, stackpos);
  6225.     }
  6226.     else
  6227.     {
  6228.         errorEx(getError(LUA_ERROR_THING_NOT_FOUND));
  6229.         lua_pushboolean(L, false);
  6230.     }
  6231.  
  6232.     return 1;
  6233. }
  6234.  
  6235. int32_t LuaInterface::luaCreateCombatObject(lua_State* L)
  6236. {
  6237.     //createCombatObject()
  6238.     ScriptEnviroment* env = getEnv();
  6239.     if(env->getScriptId() != EVENT_ID_LOADING)
  6240.     {
  6241.         errorEx("This function can only be used while loading the script");
  6242.         lua_pushboolean(L, false);
  6243.         return 1;
  6244.     }
  6245.  
  6246.     Combat* combat = new Combat;
  6247.     if(!combat)
  6248.     {
  6249.         errorEx(getError(LUA_ERROR_COMBAT_NOT_FOUND));
  6250.         lua_pushboolean(L, false);
  6251.         return 1;
  6252.     }
  6253.  
  6254.     lua_pushnumber(L, env->addCombatObject(combat));
  6255.     return 1;
  6256. }
  6257.  
  6258. bool LuaInterface::getArea(lua_State* L, std::list<uint32_t>& list, uint32_t& rows)
  6259. {
  6260.     rows = 0;
  6261.     if(!lua_istable(L, -1))
  6262.     {
  6263.         errorEx("Object on the stack is not a table");
  6264.         return false;
  6265.     }
  6266.  
  6267.     lua_pushnil(L);
  6268.     while(lua_next(L, -2))
  6269.     {
  6270.         lua_pushnil(L);
  6271.         while(lua_next(L, -2))
  6272.         {
  6273.             list.push_back((uint32_t)lua_tonumber(L, -1));
  6274.             lua_pop(L, 1);
  6275.         }
  6276.  
  6277.         lua_pop(L, 1);
  6278.         ++rows;
  6279.     }
  6280.  
  6281.     lua_pop(L, 1);
  6282.     return (rows != 0);
  6283. }
  6284.  
  6285. int32_t LuaInterface::luaCreateCombatArea(lua_State* L)
  6286. {
  6287.     //createCombatArea({area}[, {extArea}])
  6288.     ScriptEnviroment* env = getEnv();
  6289.     if(env->getScriptId() != EVENT_ID_LOADING)
  6290.     {
  6291.         errorEx("This function can only be used while loading the script");
  6292.         lua_pushboolean(L, false);
  6293.         return 1;
  6294.     }
  6295.  
  6296.     CombatArea* area = new CombatArea;
  6297.     if(lua_gettop(L) > 1) //has extra parameter with diagonal area information
  6298.     {
  6299.         uint32_t rowsExtArea = 0;
  6300.         std::list<uint32_t> listExtArea;
  6301.         if(getArea(L, listExtArea, rowsExtArea))
  6302.             area->setupExtArea(listExtArea, rowsExtArea);
  6303.     }
  6304.  
  6305.     uint32_t rowsArea = 0;
  6306.     std::list<uint32_t> listArea;
  6307.     if(getArea(L, listArea, rowsArea))
  6308.     {
  6309.         area->setupArea(listArea, rowsArea);
  6310.         lua_pushnumber(L, env->addCombatArea(area));
  6311.     }
  6312.     else
  6313.         lua_pushboolean(L, false);
  6314.  
  6315.     return 1;
  6316. }
  6317.  
  6318. int32_t LuaInterface::luaCreateConditionObject(lua_State* L)
  6319. {
  6320.     //createConditionObject(type[, ticks = 0[, buff = false[, subId = 0[, conditionId = CONDITIONID_COMBAT]]]])
  6321.     int32_t conditionId = CONDITIONID_COMBAT;
  6322.     uint32_t params = lua_gettop(L), subId = 0;
  6323.     if(params > 4)
  6324.         conditionId = popNumber(L);
  6325.  
  6326.     if(params > 3)
  6327.         subId = popNumber(L);
  6328.  
  6329.     bool buff = false;
  6330.     if(params > 2)
  6331.         buff = popBoolean(L);
  6332.  
  6333.     int32_t ticks = 0;
  6334.     if(params > 1)
  6335.         ticks = popNumber(L);
  6336.  
  6337.     ScriptEnviroment* env = getEnv();
  6338.     if(Condition* condition = Condition::createCondition((ConditionId_t)conditionId, (ConditionType_t)popNumber(L), ticks, 0, buff, subId))
  6339.     {
  6340.         if(env->getScriptId() != EVENT_ID_LOADING)
  6341.             lua_pushnumber(L, env->addTempConditionObject(condition));
  6342.         else
  6343.             lua_pushnumber(L, env->addConditionObject(condition));
  6344.     }
  6345.     else
  6346.     {
  6347.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6348.         lua_pushboolean(L, false);
  6349.     }
  6350.  
  6351.     return 1;
  6352. }
  6353.  
  6354. int32_t LuaInterface::luaSetCombatArea(lua_State* L)
  6355. {
  6356.     //setCombatArea(combat, area)
  6357.     uint32_t areaId = popNumber(L);
  6358.     ScriptEnviroment* env = getEnv();
  6359.     if(env->getScriptId() != EVENT_ID_LOADING)
  6360.     {
  6361.         errorEx("This function can only be used while loading the script");
  6362.         lua_pushboolean(L, false);
  6363.         return 1;
  6364.     }
  6365.  
  6366.     Combat* combat = env->getCombatObject(popNumber(L));
  6367.     if(!combat)
  6368.     {
  6369.         errorEx(getError(LUA_ERROR_COMBAT_NOT_FOUND));
  6370.         lua_pushboolean(L, false);
  6371.         return 1;
  6372.     }
  6373.  
  6374.     const CombatArea* area = env->getCombatArea(areaId);
  6375.     if(!area)
  6376.     {
  6377.         errorEx(getError(LUA_ERROR_AREA_NOT_FOUND));
  6378.         lua_pushboolean(L, false);
  6379.         return 1;
  6380.     }
  6381.  
  6382.     combat->setArea(new CombatArea(*area));
  6383.     lua_pushboolean(L, true);
  6384.     return 1;
  6385. }
  6386.  
  6387. int32_t LuaInterface::luaSetCombatCondition(lua_State* L)
  6388. {
  6389.     //setCombatCondition(combat, condition[, loaded])
  6390.     bool loaded = true;
  6391.     if(lua_gettop(L) > 2)
  6392.         loaded = popBoolean(L);
  6393.  
  6394.     uint32_t conditionId = popNumber(L);
  6395.     ScriptEnviroment* env = getEnv();
  6396.  
  6397.     Combat* combat = env->getCombatObject(popNumber(L));
  6398.     if(!combat)
  6399.     {
  6400.         errorEx(getError(LUA_ERROR_COMBAT_NOT_FOUND));
  6401.         lua_pushboolean(L, false);
  6402.         return 1;
  6403.     }
  6404.  
  6405.     const Condition* condition = env->getConditionObject(conditionId, loaded);
  6406.     if(!condition)
  6407.     {
  6408.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6409.         lua_pushboolean(L, false);
  6410.         return 1;
  6411.     }
  6412.  
  6413.     combat->setCondition(condition->clone());
  6414.     lua_pushboolean(L, true);
  6415.     return 1;
  6416. }
  6417.  
  6418. int32_t LuaInterface::luaSetCombatParam(lua_State* L)
  6419. {
  6420.     //setCombatParam(combat, key, value)
  6421.     uint32_t value = popNumber(L);
  6422.     CombatParam_t key = (CombatParam_t)popNumber(L);
  6423.  
  6424.     ScriptEnviroment* env = getEnv();
  6425.     if(env->getScriptId() != EVENT_ID_LOADING)
  6426.     {
  6427.         errorEx("This function can only be used while loading the script");
  6428.         lua_pushboolean(L, false);
  6429.         return 1;
  6430.     }
  6431.  
  6432.     Combat* combat = env->getCombatObject(popNumber(L));
  6433.     if(!combat)
  6434.     {
  6435.         errorEx(getError(LUA_ERROR_COMBAT_NOT_FOUND));
  6436.         lua_pushboolean(L, false);
  6437.     }
  6438.     else
  6439.     {
  6440.         combat->setParam(key, value);
  6441.         lua_pushboolean(L, true);
  6442.     }
  6443.  
  6444.     return 1;
  6445. }
  6446.  
  6447. int32_t LuaInterface::luaSetConditionParam(lua_State* L)
  6448. {
  6449.     //setConditionParam(condition, key, value[, loaded])
  6450.     bool loaded = true;
  6451.     if(lua_gettop(L) > 3)
  6452.         loaded = popBoolean(L);
  6453.  
  6454.     int32_t value = popNumber(L);
  6455.     ScriptEnviroment* env = getEnv();
  6456.  
  6457.     ConditionParam_t key = (ConditionParam_t)popNumber(L);
  6458.     if(Condition* condition = env->getConditionObject(popNumber(L), loaded))
  6459.     {
  6460.         condition->setParam(key, (int32_t)value);
  6461.         lua_pushboolean(L, true);
  6462.     }
  6463.     else
  6464.     {
  6465.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6466.         lua_pushboolean(L, false);
  6467.     }
  6468.  
  6469.     return 1;
  6470. }
  6471.  
  6472. int32_t LuaInterface::luaAddDamageCondition(lua_State* L)
  6473. {
  6474.     //addDamageCondition(condition, rounds, time, value[, loaded])
  6475.     bool loaded = true;
  6476.     if(lua_gettop(L) > 4)
  6477.         loaded = popBoolean(L);
  6478.  
  6479.     int32_t value = popNumber(L), time = popNumber(L), rounds = popNumber(L);
  6480.     ScriptEnviroment* env = getEnv();
  6481.     if(ConditionDamage* condition = dynamic_cast<ConditionDamage*>(env->getConditionObject(popNumber(L), loaded)))
  6482.     {
  6483.         condition->addDamage(rounds, time, value);
  6484.         lua_pushboolean(L, true);
  6485.     }
  6486.     else
  6487.     {
  6488.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6489.         lua_pushboolean(L, false);
  6490.     }
  6491.  
  6492.     return 1;
  6493. }
  6494.  
  6495. int32_t LuaInterface::luaAddOutfitCondition(lua_State* L)
  6496. {
  6497.     //addOutfitCondition(condition, outfit[, loaded])
  6498.     bool loaded = true;
  6499.     if(lua_gettop(L) > 2)
  6500.         loaded = popBoolean(L);
  6501.  
  6502.     Outfit_t outfit = popOutfit(L);
  6503.     ScriptEnviroment* env = getEnv();
  6504.     if(ConditionOutfit* condition = dynamic_cast<ConditionOutfit*>(env->getConditionObject(popNumber(L), loaded)))
  6505.     {
  6506.         condition->addOutfit(outfit);
  6507.         lua_pushboolean(L, true);
  6508.     }
  6509.     else
  6510.     {
  6511.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6512.         lua_pushboolean(L, false);
  6513.     }
  6514.  
  6515.     return 1;
  6516. }
  6517.  
  6518. int32_t LuaInterface::luaSetCombatCallBack(lua_State* L)
  6519. {
  6520.     //setCombatCallBack(combat, key, functionName)
  6521.     std::string function = popString(L);
  6522.     CallBackParam_t key = (CallBackParam_t)popNumber(L);
  6523.  
  6524.     ScriptEnviroment* env = getEnv();
  6525.     if(env->getScriptId() != EVENT_ID_LOADING)
  6526.     {
  6527.         errorEx("This function can only be used while loading the script");
  6528.         lua_pushboolean(L, false);
  6529.         return 1;
  6530.     }
  6531.  
  6532.     Combat* combat = env->getCombatObject(popNumber(L));
  6533.     if(!combat)
  6534.     {
  6535.         errorEx(getError(LUA_ERROR_COMBAT_NOT_FOUND));
  6536.         lua_pushboolean(L, false);
  6537.         return 1;
  6538.     }
  6539.  
  6540.     LuaInterface* interface = env->getInterface();
  6541.     combat->setCallback(key);
  6542.  
  6543.     CallBack* callback = combat->getCallback(key);
  6544.     if(!callback)
  6545.     {
  6546.         errorEx(asString<uint32_t>(key) + " is not a valid callback key");
  6547.         lua_pushboolean(L, false);
  6548.         return 1;
  6549.     }
  6550.  
  6551.     if(!callback->loadCallBack(interface, function))
  6552.     {
  6553.         errorEx("Cannot load callback");
  6554.         lua_pushboolean(L, false);
  6555.     }
  6556.     else
  6557.         lua_pushboolean(L, true);
  6558.  
  6559.     return 1;
  6560. }
  6561.  
  6562. int32_t LuaInterface::luaSetCombatFormula(lua_State* L)
  6563. {
  6564.     //setCombatFormula(combat, type, mina, minb, maxa, maxb[, minl, maxl[, minm, maxm[, minc[, maxc]]]])
  6565.     ScriptEnviroment* env = getEnv();
  6566.     if(env->getScriptId() != EVENT_ID_LOADING)
  6567.     {
  6568.         errorEx("This function can only be used while loading the script");
  6569.         lua_pushboolean(L, false);
  6570.         return 1;
  6571.     }
  6572.  
  6573.     int32_t params = lua_gettop(L), minc = 0, maxc = 0;
  6574.     if(params > 11)
  6575.         maxc = popNumber(L);
  6576.  
  6577.     if(params > 10)
  6578.         minc = popNumber(L);
  6579.  
  6580.     double minm = g_config.getDouble(ConfigManager::FORMULA_MAGIC), maxm = minm,
  6581.         minl = g_config.getDouble(ConfigManager::FORMULA_LEVEL), maxl = minl;
  6582.     if(params > 8)
  6583.     {
  6584.         maxm = popFloatNumber(L);
  6585.         minm = popFloatNumber(L);
  6586.     }
  6587.  
  6588.     if(params > 6)
  6589.     {
  6590.         maxl = popFloatNumber(L);
  6591.         minl = popFloatNumber(L);
  6592.     }
  6593.  
  6594.     double maxb = popFloatNumber(L), maxa = popFloatNumber(L),
  6595.         minb = popFloatNumber(L), mina = popFloatNumber(L);
  6596.     formulaType_t type = (formulaType_t)popNumber(L);
  6597.     if(Combat* combat = env->getCombatObject(popNumber(L)))
  6598.     {
  6599.         combat->setPlayerCombatValues(type, mina, minb, maxa, maxb, minl, maxl, minm, maxm, minc, maxc);
  6600.         lua_pushboolean(L, true);
  6601.     }
  6602.     else
  6603.     {
  6604.         errorEx(getError(LUA_ERROR_COMBAT_NOT_FOUND));
  6605.         lua_pushboolean(L, false);
  6606.     }
  6607.  
  6608.     return 1;
  6609. }
  6610.  
  6611. int32_t LuaInterface::luaSetConditionFormula(lua_State* L)
  6612. {
  6613.     //setConditionFormula(condition, mina, minb, maxa, maxb[, loaded])
  6614.     bool loaded = true;
  6615.     if(lua_gettop(L) > 5)
  6616.         loaded = popBoolean(L);
  6617.  
  6618.     double maxb = popFloatNumber(L), maxa = popFloatNumber(L),
  6619.         minb = popFloatNumber(L), mina = popFloatNumber(L);
  6620.     ScriptEnviroment* env = getEnv();
  6621.     if(ConditionSpeed* condition = dynamic_cast<ConditionSpeed*>(env->getConditionObject(popNumber(L), loaded)))
  6622.     {
  6623.         condition->setFormulaVars(mina, minb, maxa, maxb);
  6624.         lua_pushboolean(L, true);
  6625.     }
  6626.     else
  6627.     {
  6628.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6629.         lua_pushboolean(L, false);
  6630.     }
  6631.  
  6632.     return 1;
  6633. }
  6634.  
  6635. int32_t LuaInterface::luaDoCombat(lua_State* L)
  6636. {
  6637.     //doCombat(cid, combat, param)
  6638.     ScriptEnviroment* env = getEnv();
  6639.  
  6640.     LuaVariant var = popVariant(L);
  6641.     uint32_t combatId = popNumber(L), cid = popNumber(L);
  6642.  
  6643.     Creature* creature = NULL;
  6644.     if(cid != 0)
  6645.     {
  6646.         creature = env->getCreatureByUID(cid);
  6647.         if(!creature)
  6648.         {
  6649.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6650.             lua_pushboolean(L, false);
  6651.             return 1;
  6652.         }
  6653.     }
  6654.  
  6655.     const Combat* combat = env->getCombatObject(combatId);
  6656.     if(!combat)
  6657.     {
  6658.         errorEx(getError(LUA_ERROR_COMBAT_NOT_FOUND));
  6659.         lua_pushboolean(L, false);
  6660.         return 1;
  6661.     }
  6662.  
  6663.     if(var.type == VARIANT_NONE)
  6664.     {
  6665.         errorEx(getError(LUA_ERROR_VARIANT_NOT_FOUND));
  6666.         lua_pushboolean(L, false);
  6667.         return 1;
  6668.     }
  6669.  
  6670.     switch(var.type)
  6671.     {
  6672.         case VARIANT_NUMBER:
  6673.         {
  6674.             Creature* target = g_game.getCreatureByID(var.number);
  6675.             if(!target || !creature || !creature->canSeeCreature(target))
  6676.             {
  6677.                 lua_pushboolean(L, false);
  6678.                 return 1;
  6679.             }
  6680.  
  6681.             if(combat->hasArea())
  6682.                 combat->doCombat(creature, target->getPosition());
  6683.             else
  6684.                 combat->doCombat(creature, target);
  6685.  
  6686.             break;
  6687.         }
  6688.  
  6689.         case VARIANT_POSITION:
  6690.         {
  6691.             combat->doCombat(creature, var.pos);
  6692.             break;
  6693.         }
  6694.  
  6695.         case VARIANT_TARGETPOSITION:
  6696.         {
  6697.             if(!combat->hasArea())
  6698.             {
  6699.                 combat->postCombatEffects(creature, var.pos);
  6700.                 g_game.addMagicEffect(var.pos, MAGIC_EFFECT_POFF);
  6701.             }
  6702.             else
  6703.                 combat->doCombat(creature, var.pos);
  6704.  
  6705.             break;
  6706.         }
  6707.  
  6708.         case VARIANT_STRING:
  6709.         {
  6710.             Player* target = g_game.getPlayerByName(var.text);
  6711.             if(!target || !creature || !creature->canSeeCreature(target))
  6712.             {
  6713.                 lua_pushboolean(L, false);
  6714.                 return 1;
  6715.             }
  6716.  
  6717.             combat->doCombat(creature, target);
  6718.             break;
  6719.         }
  6720.  
  6721.         default:
  6722.         {
  6723.             errorEx(getError(LUA_ERROR_VARIANT_UNKNOWN));
  6724.             lua_pushboolean(L, false);
  6725.             break;
  6726.         }
  6727.     }
  6728.  
  6729.     lua_pushboolean(L, true);
  6730.     return 1;
  6731. }
  6732.  
  6733. int32_t LuaInterface::luaDoCombatAreaHealth(lua_State* L)
  6734. {
  6735.     //doCombatAreaHealth(cid, type, pos, area, min, max, effect[, aggressive])
  6736.     bool aggressive = true;
  6737.     if(lua_gettop(L) > 7) // shouldn't it be enough if we check only is conditionType == CONDITION_HEALING?
  6738.         aggressive = popBoolean(L);
  6739.  
  6740.     MagicEffect_t effect = (MagicEffect_t)popNumber(L);
  6741.     int32_t maxChange = (int32_t)popNumber(L), minChange = (int32_t)popNumber(L);
  6742.     uint32_t areaId = popNumber(L);
  6743.  
  6744.     PositionEx pos;
  6745.     popPosition(L, pos);
  6746.  
  6747.     CombatType_t combatType = (CombatType_t)popNumber(L);
  6748.     uint32_t cid = popNumber(L);
  6749.  
  6750.     ScriptEnviroment* env = getEnv();
  6751.     Creature* creature = NULL;
  6752.     if(cid)
  6753.     {
  6754.         if(!(creature = env->getCreatureByUID(cid)))
  6755.         {
  6756.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6757.             lua_pushboolean(L, false);
  6758.             return 1;
  6759.         }
  6760.     }
  6761.  
  6762.     const CombatArea* area = env->getCombatArea(areaId);
  6763.     if(area || !areaId)
  6764.     {
  6765.         CombatParams params;
  6766.         params.combatType = combatType;
  6767.         params.effects.impact = effect;
  6768.         params.isAggressive = aggressive;
  6769.  
  6770.         Combat::doCombatHealth(creature, pos, area, minChange, maxChange, params);
  6771.         lua_pushboolean(L, true);
  6772.     }
  6773.     else
  6774.     {
  6775.         errorEx(getError(LUA_ERROR_AREA_NOT_FOUND));
  6776.         lua_pushboolean(L, false);
  6777.     }
  6778.  
  6779.     return 1;
  6780. }
  6781.  
  6782. int32_t LuaInterface::luaDoTargetCombatHealth(lua_State* L)
  6783. {
  6784.     //doTargetCombatHealth(cid, target, type, min, max, effect[, aggressive])
  6785.     bool aggressive = true;
  6786.     if(lua_gettop(L) > 6) // shouldn't it be enough if we check only is conditionType == CONDITION_HEALING?
  6787.         aggressive = popBoolean(L);
  6788.  
  6789.     MagicEffect_t effect = (MagicEffect_t)popNumber(L);
  6790.     int32_t maxChange = (int32_t)popNumber(L), minChange = (int32_t)popNumber(L);
  6791.  
  6792.     CombatType_t combatType = (CombatType_t)popNumber(L);
  6793.     uint32_t targetCid = popNumber(L), cid = popNumber(L);
  6794.  
  6795.     ScriptEnviroment* env = getEnv();
  6796.     Creature* creature = NULL;
  6797.     if(cid)
  6798.     {
  6799.         if(!(creature = env->getCreatureByUID(cid)))
  6800.         {
  6801.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6802.             lua_pushboolean(L, false);
  6803.             return 1;
  6804.         }
  6805.     }
  6806.  
  6807.     Creature* target = env->getCreatureByUID(targetCid);
  6808.     if(target)
  6809.     {
  6810.         CombatParams params;
  6811.         params.combatType = combatType;
  6812.         params.effects.impact = effect;
  6813.         params.isAggressive = aggressive;
  6814.  
  6815.         Combat::doCombatHealth(creature, target, minChange, maxChange, params);
  6816.         lua_pushboolean(L, true);
  6817.     }
  6818.     else
  6819.     {
  6820.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6821.         lua_pushboolean(L, false);
  6822.     }
  6823.  
  6824.     return 1;
  6825. }
  6826.  
  6827. int32_t LuaInterface::luaDoCombatAreaMana(lua_State* L)
  6828. {
  6829.     //doCombatAreaMana(cid, pos, area, min, max, effect[, aggressive])
  6830.     bool aggressive = true;
  6831.     if(lua_gettop(L) > 6)
  6832.         aggressive = popBoolean(L);
  6833.  
  6834.     MagicEffect_t effect = (MagicEffect_t)popNumber(L);
  6835.     int32_t maxChange = (int32_t)popNumber(L), minChange = (int32_t)popNumber(L);
  6836.     uint32_t areaId = popNumber(L);
  6837.  
  6838.     PositionEx pos;
  6839.     popPosition(L, pos);
  6840.     uint32_t cid = popNumber(L);
  6841.  
  6842.     ScriptEnviroment* env = getEnv();
  6843.     Creature* creature = NULL;
  6844.     if(cid)
  6845.     {
  6846.         if(!(creature = env->getCreatureByUID(cid)))
  6847.         {
  6848.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6849.             lua_pushboolean(L, false);
  6850.             return 1;
  6851.         }
  6852.     }
  6853.  
  6854.     const CombatArea* area = env->getCombatArea(areaId);
  6855.     if(area || !areaId)
  6856.     {
  6857.         CombatParams params;
  6858.         params.effects.impact = effect;
  6859.         params.isAggressive = aggressive;
  6860.  
  6861.         Combat::doCombatMana(creature, pos, area, minChange, maxChange, params);
  6862.         lua_pushboolean(L, true);
  6863.     }
  6864.     else
  6865.     {
  6866.         errorEx(getError(LUA_ERROR_AREA_NOT_FOUND));
  6867.         lua_pushboolean(L, false);
  6868.     }
  6869.  
  6870.     return 1;
  6871. }
  6872.  
  6873. int32_t LuaInterface::luaDoTargetCombatMana(lua_State* L)
  6874. {
  6875.     //doTargetCombatMana(cid, target, min, max, effect[, aggressive])
  6876.     bool aggressive = true;
  6877.     if(lua_gettop(L) > 5)
  6878.         aggressive = popBoolean(L);
  6879.  
  6880.     MagicEffect_t effect = (MagicEffect_t)popNumber(L);
  6881.     int32_t maxChange = (int32_t)popNumber(L), minChange = (int32_t)popNumber(L);
  6882.     uint32_t targetCid = popNumber(L), cid = popNumber(L);
  6883.  
  6884.     ScriptEnviroment* env = getEnv();
  6885.     Creature* creature = NULL;
  6886.     if(cid)
  6887.     {
  6888.         if(!(creature = env->getCreatureByUID(cid)))
  6889.         {
  6890.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6891.             lua_pushboolean(L, false);
  6892.             return 1;
  6893.         }
  6894.     }
  6895.  
  6896.     if(Creature* target = env->getCreatureByUID(targetCid))
  6897.     {
  6898.         CombatParams params;
  6899.         params.effects.impact = effect;
  6900.         params.isAggressive = aggressive;
  6901.  
  6902.         Combat::doCombatMana(creature, target, minChange, maxChange, params);
  6903.         lua_pushboolean(L, true);
  6904.     }
  6905.     else
  6906.     {
  6907.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6908.         lua_pushboolean(L, false);
  6909.     }
  6910.  
  6911.     return 1;
  6912. }
  6913.  
  6914. int32_t LuaInterface::luaDoCombatAreaCondition(lua_State* L)
  6915. {
  6916.     //doCombatAreaCondition(cid, pos, area, condition, effect[, loaded])
  6917.     bool loaded = true;
  6918.     if(lua_gettop(L) > 5)
  6919.         loaded = popBoolean(L);
  6920.  
  6921.     MagicEffect_t effect = (MagicEffect_t)popNumber(L);
  6922.     uint32_t conditionId = popNumber(L), areaId = popNumber(L);
  6923.  
  6924.     PositionEx pos;
  6925.     popPosition(L, pos);
  6926.     uint32_t cid = popNumber(L);
  6927.  
  6928.     ScriptEnviroment* env = getEnv();
  6929.     Creature* creature = NULL;
  6930.     if(cid)
  6931.     {
  6932.         if(!(creature = env->getCreatureByUID(cid)))
  6933.         {
  6934.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6935.             lua_pushboolean(L, false);
  6936.             return 1;
  6937.         }
  6938.     }
  6939.  
  6940.     if(const Condition* condition = env->getConditionObject(conditionId, loaded))
  6941.     {
  6942.         const CombatArea* area = env->getCombatArea(areaId);
  6943.         if(area || !areaId)
  6944.         {
  6945.             CombatParams params;
  6946.             params.effects.impact = effect;
  6947.             params.conditionList.push_back(condition);
  6948.  
  6949.             Combat::doCombatCondition(creature, pos, area, params);
  6950.             lua_pushboolean(L, true);
  6951.         }
  6952.         else
  6953.         {
  6954.             errorEx(getError(LUA_ERROR_AREA_NOT_FOUND));
  6955.             lua_pushboolean(L, false);
  6956.         }
  6957.     }
  6958.     else
  6959.     {
  6960.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6961.         lua_pushboolean(L, false);
  6962.     }
  6963.  
  6964.     return 1;
  6965. }
  6966.  
  6967. int32_t LuaInterface::luaDoTargetCombatCondition(lua_State* L)
  6968. {
  6969.     //doTargetCombatCondition(cid, target, condition, effect[, loaded])
  6970.     bool loaded = true;
  6971.     if(lua_gettop(L) > 4)
  6972.         loaded = popBoolean(L);
  6973.  
  6974.     MagicEffect_t effect = (MagicEffect_t)popNumber(L);
  6975.     uint32_t conditionId = popNumber(L), targetCid = popNumber(L), cid = popNumber(L);
  6976.  
  6977.     ScriptEnviroment* env = getEnv();
  6978.     Creature* creature = NULL;
  6979.     if(cid)
  6980.     {
  6981.         if(!(creature = env->getCreatureByUID(cid)))
  6982.         {
  6983.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6984.             lua_pushboolean(L, false);
  6985.             return 1;
  6986.         }
  6987.     }
  6988.  
  6989.     if(Creature* target = env->getCreatureByUID(targetCid))
  6990.     {
  6991.         if(const Condition* condition = env->getConditionObject(conditionId, loaded))
  6992.         {
  6993.             CombatParams params;
  6994.             params.effects.impact = effect;
  6995.             params.conditionList.push_back(condition);
  6996.  
  6997.             Combat::doCombatCondition(creature, target, params);
  6998.             lua_pushboolean(L, true);
  6999.         }
  7000.         else
  7001.         {
  7002.             errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  7003.             lua_pushboolean(L, false);
  7004.         }
  7005.     }
  7006.     else
  7007.     {
  7008.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7009.         lua_pushboolean(L, false);
  7010.     }
  7011.  
  7012.     return 1;
  7013. }
  7014.  
  7015. int32_t LuaInterface::luaDoCombatAreaDispel(lua_State* L)
  7016. {
  7017.     //doCombatAreaDispel(cid, pos, area, type, effect)
  7018.     MagicEffect_t effect = (MagicEffect_t)popNumber(L);
  7019.     ConditionType_t dispelType = (ConditionType_t)popNumber(L);
  7020.     uint32_t areaId = popNumber(L);
  7021.  
  7022.     PositionEx pos;
  7023.     popPosition(L, pos);
  7024.     uint32_t cid = popNumber(L);
  7025.  
  7026.     ScriptEnviroment* env = getEnv();
  7027.     Creature* creature = NULL;
  7028.     if(cid)
  7029.     {
  7030.         if(!(creature = env->getCreatureByUID(cid)))
  7031.         {
  7032.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7033.             lua_pushboolean(L, false);
  7034.             return 1;
  7035.         }
  7036.     }
  7037.  
  7038.     const CombatArea* area = env->getCombatArea(areaId);
  7039.     if(area || !areaId)
  7040.     {
  7041.         CombatParams params;
  7042.         params.effects.impact = effect;
  7043.         params.dispelType = dispelType;
  7044.  
  7045.         Combat::doCombatDispel(creature, pos, area, params);
  7046.         lua_pushboolean(L, true);
  7047.     }
  7048.     else
  7049.     {
  7050.         errorEx(getError(LUA_ERROR_AREA_NOT_FOUND));
  7051.         lua_pushboolean(L, false);
  7052.     }
  7053.  
  7054.     return 1;
  7055. }
  7056.  
  7057. int32_t LuaInterface::luaDoTargetCombatDispel(lua_State* L)
  7058. {
  7059.     //doTargetCombatDispel(cid, target, type, effect)
  7060.     MagicEffect_t effect = (MagicEffect_t)popNumber(L);
  7061.     ConditionType_t dispelType = (ConditionType_t)popNumber(L);
  7062.     uint32_t targetCid = popNumber(L), cid = popNumber(L);
  7063.  
  7064.     ScriptEnviroment* env = getEnv();
  7065.     Creature* creature = NULL;
  7066.     if(cid)
  7067.     {
  7068.         if(!(creature = env->getCreatureByUID(cid)))
  7069.         {
  7070.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7071.             lua_pushboolean(L, false);
  7072.             return 1;
  7073.         }
  7074.     }
  7075.  
  7076.     if(Creature* target = env->getCreatureByUID(targetCid))
  7077.     {
  7078.         CombatParams params;
  7079.         params.effects.impact = effect;
  7080.         params.dispelType = dispelType;
  7081.  
  7082.         Combat::doCombatDispel(creature, target, params);
  7083.         lua_pushboolean(L, true);
  7084.     }
  7085.     else
  7086.     {
  7087.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7088.         lua_pushboolean(L, false);
  7089.     }
  7090.  
  7091.     return 1;
  7092. }
  7093.  
  7094. int32_t LuaInterface::luaDoChallengeCreature(lua_State* L)
  7095. {
  7096.     //doChallengeCreature(cid, target)
  7097.     ScriptEnviroment* env = getEnv();
  7098.     uint32_t targetCid = popNumber(L);
  7099.  
  7100.     Creature* creature = env->getCreatureByUID(popNumber(L));
  7101.     if(!creature)
  7102.     {
  7103.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7104.         lua_pushboolean(L, false);
  7105.         return 1;
  7106.     }
  7107.  
  7108.     Creature* target = env->getCreatureByUID(targetCid);
  7109.     if(!target)
  7110.     {
  7111.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7112.         lua_pushboolean(L, false);
  7113.         return 1;
  7114.     }
  7115.  
  7116.     target->challengeCreature(creature);
  7117.     lua_pushboolean(L, true);
  7118.     return 1;
  7119. }
  7120.  
  7121. int32_t LuaInterface::luaDoSummonMonster(lua_State* L)
  7122. {
  7123.     //doSummonMonster(cid, name)
  7124.     std::string name = popString(L);
  7125.  
  7126.     ScriptEnviroment* env = getEnv();
  7127.     Creature* creature = env->getCreatureByUID(popNumber(L));
  7128.     if(!creature)
  7129.     {
  7130.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7131.         lua_pushboolean(L, false);
  7132.         return 1;
  7133.     }
  7134.  
  7135.     lua_pushnumber(L, g_game.placeSummon(creature, name));
  7136.     return 1;
  7137. }
  7138.  
  7139. int32_t LuaInterface::luaDoConvinceCreature(lua_State* L)
  7140. {
  7141.     //doConvinceCreature(cid, target)
  7142.     uint32_t cid = popNumber(L);
  7143.  
  7144.     ScriptEnviroment* env = getEnv();
  7145.     Creature* creature = env->getCreatureByUID(popNumber(L));
  7146.     if(!creature)
  7147.     {
  7148.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7149.         lua_pushboolean(L, false);
  7150.         return 1;
  7151.     }
  7152.  
  7153.     Creature* target = env->getCreatureByUID(cid);
  7154.     if(!target)
  7155.     {
  7156.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7157.         lua_pushboolean(L, false);
  7158.         return 1;
  7159.     }
  7160.  
  7161.     target->convinceCreature(creature);
  7162.     lua_pushboolean(L, true);
  7163.     return 1;
  7164. }
  7165.  
  7166. int32_t LuaInterface::luaGetMonsterTargetList(lua_State* L)
  7167. {
  7168.     //getMonsterTargetList(cid)
  7169.     ScriptEnviroment* env = getEnv();
  7170.     Creature* creature = env->getCreatureByUID(popNumber(L));
  7171.     if(!creature)
  7172.     {
  7173.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7174.         lua_pushboolean(L, false);
  7175.         return 1;
  7176.     }
  7177.  
  7178.     Monster* monster = creature->getMonster();
  7179.     if(!monster)
  7180.     {
  7181.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7182.         lua_pushboolean(L, false);
  7183.         return 1;
  7184.     }
  7185.  
  7186.     const CreatureList& targetList = monster->getTargetList();
  7187.     CreatureList::const_iterator it = targetList.begin();
  7188.  
  7189.     lua_newtable(L);
  7190.     for(uint32_t i = 1; it != targetList.end(); ++it, ++i)
  7191.     {
  7192.         if(monster->isTarget(*it))
  7193.         {
  7194.             lua_pushnumber(L, i);
  7195.             lua_pushnumber(L, env->addThing(*it));
  7196.             pushTable(L);
  7197.         }
  7198.     }
  7199.  
  7200.     return 1;
  7201. }
  7202.  
  7203. int32_t LuaInterface::luaGetMonsterFriendList(lua_State* L)
  7204. {
  7205.     //getMonsterFriendList(cid)
  7206.     ScriptEnviroment* env = getEnv();
  7207.     Creature* creature = env->getCreatureByUID(popNumber(L));
  7208.     if(!creature)
  7209.     {
  7210.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7211.         lua_pushboolean(L, false);
  7212.         return 1;
  7213.     }
  7214.  
  7215.     Monster* monster = creature->getMonster();
  7216.     if(!monster)
  7217.     {
  7218.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7219.         lua_pushboolean(L, false);
  7220.         return 1;
  7221.     }
  7222.  
  7223.     Creature* friendCreature;
  7224.     const std::unordered_map<uint32_t, Creature*>& friendList = monster->getFriendList();
  7225.  
  7226.     lua_newtable(L);
  7227.     uint16_t i = 1;
  7228.     for (const auto& element : friendList)
  7229.     {
  7230.         friendCreature = (element.second);
  7231.         if (!friendCreature->isRemoved() && friendCreature->getPosition().z == monster->getPosition().z)
  7232.         {
  7233.             lua_pushnumber(L, i);
  7234.             lua_pushnumber(L, env->addThing(element.second));
  7235.             pushTable(L);
  7236.             i++;
  7237.         }
  7238.     }
  7239.  
  7240.     return 1;
  7241. }
  7242.  
  7243. int32_t LuaInterface::luaDoMonsterSetTarget(lua_State* L)
  7244. {
  7245.     //doMonsterSetTarget(cid, target)
  7246.     uint32_t targetId = popNumber(L);
  7247.     ScriptEnviroment* env = getEnv();
  7248.  
  7249.     Creature* creature = env->getCreatureByUID(popNumber(L));
  7250.     if(!creature)
  7251.     {
  7252.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7253.         lua_pushboolean(L, false);
  7254.         return 1;
  7255.     }
  7256.  
  7257.     Monster* monster = creature->getMonster();
  7258.     if(!monster)
  7259.     {
  7260.         errorEx(getError(LUA_ERROR_MONSTER_NOT_FOUND));
  7261.         lua_pushboolean(L, false);
  7262.         return 1;
  7263.     }
  7264.  
  7265.     Creature* target = env->getCreatureByUID(targetId);
  7266.     if(!target)
  7267.     {
  7268.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7269.         lua_pushboolean(L, false);
  7270.         return 1;
  7271.     }
  7272.  
  7273.     if(!monster->isSummon())
  7274.         lua_pushboolean(L, monster->selectTarget(target));
  7275.     else
  7276.         lua_pushboolean(L, false);
  7277.  
  7278.     return 1;
  7279. }
  7280.  
  7281. int32_t LuaInterface::luaDoMonsterChangeTarget(lua_State* L)
  7282. {
  7283.     //doMonsterChangeTarget(cid)
  7284.     ScriptEnviroment* env = getEnv();
  7285.     Creature* creature = env->getCreatureByUID(popNumber(L));
  7286.     if(!creature)
  7287.     {
  7288.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7289.         lua_pushboolean(L, false);
  7290.         return 1;
  7291.     }
  7292.  
  7293.     Monster* monster = creature->getMonster();
  7294.     if(!monster)
  7295.     {
  7296.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7297.         lua_pushboolean(L, false);
  7298.         return 1;
  7299.     }
  7300.  
  7301.     if(!monster->isSummon())
  7302.         monster->searchTarget(TARGETSEARCH_RANDOM);
  7303.  
  7304.     lua_pushboolean(L, true);
  7305.     return 1;
  7306. }
  7307.  
  7308. int32_t LuaInterface::luaGetMonsterInfo(lua_State* L)
  7309. {
  7310.     //getMonsterInfo(name)
  7311.     const MonsterType* mType = g_monsters.getMonsterType(popString(L));
  7312.     if(!mType)
  7313.     {
  7314.         errorEx(getError(LUA_ERROR_MONSTER_NOT_FOUND));
  7315.         lua_pushboolean(L, false);
  7316.         return 1;
  7317.     }
  7318.  
  7319.     lua_newtable(L);
  7320.     setField(L, "name", mType->name.c_str());
  7321.     setField(L, "description", mType->nameDescription.c_str());
  7322.     setField(L, "file", mType->file.c_str());
  7323.     setField(L, "experience", mType->experience);
  7324.     setField(L, "health", mType->health);
  7325.     setField(L, "healthMax", mType->healthMax);
  7326.     setField(L, "manaCost", mType->manaCost);
  7327.     setField(L, "defense", mType->defense);
  7328.     setField(L, "armor", mType->armor);
  7329.     setField(L, "baseSpeed", mType->baseSpeed);
  7330.     setField(L, "lookCorpse", mType->lookCorpse);
  7331.     setField(L, "corpseUnique", mType->corpseUnique);
  7332.     setField(L, "corpseAction", mType->corpseAction);
  7333.     setField(L, "race", mType->race);
  7334.     setField(L, "skull", mType->skull);
  7335.     setField(L, "partyShield", mType->partyShield);
  7336.     setField(L, "guildEmblem", mType->guildEmblem);
  7337.     setFieldBool(L, "summonable", mType->isSummonable);
  7338.     setFieldBool(L, "illusionable", mType->isIllusionable);
  7339.     setFieldBool(L, "convinceable", mType->isConvinceable);
  7340.     setFieldBool(L, "attackable", mType->isAttackable);
  7341.     setFieldBool(L, "hostile", mType->isHostile);
  7342.     setFieldBool(L, "passive", mType->isPassive);
  7343.  
  7344.     lua_pushstring(L, "outfit"); // name the table created by pushOutfit
  7345.     pushOutfit(L, mType->outfit);
  7346.     pushTable(L);
  7347.     createTable(L, "defenses");
  7348.  
  7349.     SpellList::const_iterator it = mType->spellDefenseList.begin();
  7350.     for(uint32_t i = 1; it != mType->spellDefenseList.end(); ++it, ++i)
  7351.     {
  7352.         createTable(L, i);
  7353.         setField(L, "speed", it->speed);
  7354.         setField(L, "chance", it->chance);
  7355.         setField(L, "range", it->range);
  7356.  
  7357.         setField(L, "minCombatValue", it->minCombatValue);
  7358.         setField(L, "maxCombatValue", it->maxCombatValue);
  7359.         setFieldBool(L, "isMelee", it->isMelee);
  7360.         pushTable(L);
  7361.     }
  7362.  
  7363.     pushTable(L);
  7364.     createTable(L, "attacks");
  7365.  
  7366.     it = mType->spellAttackList.begin();
  7367.     for(uint32_t i = 1; it != mType->spellAttackList.end(); ++it, ++i)
  7368.     {
  7369.         createTable(L, i);
  7370.         setField(L, "speed", it->speed);
  7371.         setField(L, "chance", it->chance);
  7372.         setField(L, "range", it->range);
  7373.  
  7374.         setField(L, "minCombatValue", it->minCombatValue);
  7375.         setField(L, "maxCombatValue", it->maxCombatValue);
  7376.         setFieldBool(L, "isMelee", it->isMelee);
  7377.         pushTable(L);
  7378.     }
  7379.  
  7380.     pushTable(L);
  7381.     createTable(L, "loot");
  7382.  
  7383.     LootItems::const_iterator lit = mType->lootItems.begin();
  7384.     for(uint32_t i = 1; lit != mType->lootItems.end(); ++lit, ++i)
  7385.     {
  7386.         createTable(L, i);
  7387.         if(lit->ids.size() > 1)
  7388.         {
  7389.             createTable(L, "ids");
  7390.             std::vector<uint16_t>::const_iterator iit = lit->ids.begin();
  7391.             for(uint32_t j = 1; iit != lit->ids.end(); ++iit, ++j)
  7392.             {
  7393.                 lua_pushnumber(L, j);
  7394.                 lua_pushnumber(L, (*iit));
  7395.                 pushTable(L);
  7396.             }
  7397.  
  7398.             pushTable(L);
  7399.         }
  7400.         else
  7401.             setField(L, "id", lit->ids[0]);
  7402.  
  7403.         setField(L, "count", lit->count);
  7404.         setField(L, "chance", lit->chance);
  7405.         setField(L, "subType", lit->subType);
  7406.         setField(L, "actionId", lit->actionId);
  7407.         setField(L, "uniqueId", lit->uniqueId);
  7408.         setField(L, "text", lit->text);
  7409.  
  7410.         if(lit->childLoot.size() > 0)
  7411.         {
  7412.             createTable(L, "child");
  7413.             LootItems::const_iterator cit = lit->childLoot.begin();
  7414.             for(uint32_t j = 1; cit != lit->childLoot.end(); ++cit, ++j)
  7415.             {
  7416.                 createTable(L, j);
  7417.                 if(cit->ids.size() > 1)
  7418.                 {
  7419.                     createTable(L, "ids");
  7420.                     std::vector<uint16_t>::const_iterator iit = cit->ids.begin();
  7421.                     for(uint32_t k = 1; iit != cit->ids.end(); ++iit, ++k)
  7422.                     {
  7423.                         lua_pushnumber(L, k);
  7424.                         lua_pushnumber(L, (*iit));
  7425.                         pushTable(L);
  7426.                     }
  7427.  
  7428.                     pushTable(L);
  7429.                 }
  7430.                 else
  7431.                     setField(L, "id", cit->ids[0]);
  7432.  
  7433.                 setField(L, "count", cit->count);
  7434.                 setField(L, "chance", cit->chance);
  7435.                 setField(L, "subType", cit->subType);
  7436.                 setField(L, "actionId", cit->actionId);
  7437.                 setField(L, "uniqueId", cit->uniqueId);
  7438.                 setField(L, "text", cit->text);
  7439.  
  7440.                 pushTable(L);
  7441.             }
  7442.  
  7443.             pushTable(L);
  7444.         }
  7445.  
  7446.         pushTable(L);
  7447.     }
  7448.  
  7449.     pushTable(L);
  7450.     createTable(L, "summons");
  7451.  
  7452.     SummonList::const_iterator sit = mType->summonList.begin();
  7453.     for(uint32_t i = 1; sit != mType->summonList.end(); ++sit, ++i)
  7454.     {
  7455.         createTable(L, i);
  7456.         setField(L, "name", sit->name);
  7457.         setField(L, "chance", sit->chance);
  7458.  
  7459.         setField(L, "interval", sit->interval);
  7460.         setField(L, "amount", sit->amount);
  7461.         pushTable(L);
  7462.     }
  7463.  
  7464.     pushTable(L);
  7465.     return 1;
  7466. }
  7467.  
  7468. int32_t LuaInterface::luaGetTalkActionList(lua_State* L)
  7469. {
  7470.     //getTalkactionList()
  7471.     lua_newtable(L);
  7472.  
  7473.     TalkActionsMap::const_iterator it = g_talkActions->getFirstTalk();
  7474.     for(uint32_t i = 1; it != g_talkActions->getLastTalk(); ++it, ++i)
  7475.     {
  7476.         createTable(L, i);
  7477.         setField(L, "words", it->first);
  7478.         setField(L, "access", it->second->getAccess());
  7479.  
  7480.         createTable(L, "groups");
  7481.         IntegerVec::const_iterator git = it->second->getGroupsBegin();
  7482.         for(uint32_t j = 1; git != it->second->getGroupsEnd(); ++git, ++j)
  7483.         {
  7484.             lua_pushnumber(L, j);
  7485.             lua_pushnumber(L, *git);
  7486.             pushTable(L);
  7487.         }
  7488.  
  7489.         pushTable(L);
  7490.         setFieldBool(L, "log", it->second->isLogged());
  7491.         setFieldBool(L, "logged", it->second->isLogged());
  7492.         setFieldBool(L, "hide", it->second->isHidden());
  7493.         setFieldBool(L, "hidden", it->second->isHidden());
  7494.  
  7495.         setField(L, "functionName", it->second->getFunctionName());
  7496.         setField(L, "channel", it->second->getChannel());
  7497.         pushTable(L);
  7498.     }
  7499.  
  7500.     return 1;
  7501. }
  7502.  
  7503. int32_t LuaInterface::luaGetExperienceStageList(lua_State* L)
  7504. {
  7505.     //getExperienceStageList()
  7506.     if(!g_config.getBool(ConfigManager::EXPERIENCE_STAGES))
  7507.     {
  7508.         lua_pushboolean(L, false);
  7509.         return true;
  7510.     }
  7511.  
  7512.     StageList::const_iterator it = g_game.getFirstStage();
  7513.     lua_newtable(L);
  7514.     for(uint32_t i = 1; it != g_game.getLastStage(); ++it, ++i)
  7515.     {
  7516.         createTable(L, i);
  7517.         setField(L, "level", it->first);
  7518.         setFieldFloat(L, "multiplier", it->second);
  7519.         pushTable(L);
  7520.     }
  7521.  
  7522.     return 1;
  7523. }
  7524.  
  7525. int32_t LuaInterface::luaDoAddCondition(lua_State* L)
  7526. {
  7527.     //doAddCondition(cid, condition[, loaded])
  7528.     bool loaded = true;
  7529.     if(lua_gettop(L) > 2)
  7530.         loaded = popBoolean(L);
  7531.  
  7532.     uint32_t conditionId = popNumber(L);
  7533.     ScriptEnviroment* env = getEnv();
  7534.  
  7535.     Creature* creature = env->getCreatureByUID(popNumber(L));
  7536.     if(!creature)
  7537.     {
  7538.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7539.         lua_pushboolean(L, false);
  7540.         return 1;
  7541.     }
  7542.  
  7543.     Condition* condition = env->getConditionObject(conditionId, loaded);
  7544.     if(!condition)
  7545.     {
  7546.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  7547.         lua_pushboolean(L, false);
  7548.         return 1;
  7549.     }
  7550.  
  7551.     creature->addCondition(condition->clone());
  7552.     lua_pushboolean(L, true);
  7553.     return 1;
  7554. }
  7555.  
  7556. int32_t LuaInterface::luaGetPlayerSecureMode(lua_State* L)
  7557. {
  7558.     //getSecureMode(cid)
  7559.     ScriptEnviroment* env = getEnv();
  7560.     Player* player = env->getPlayerByUID((uint32_t)popNumber(L));
  7561.     if(player)
  7562.         lua_pushnumber(L, player->getSecureMode());
  7563.     else {
  7564.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  7565.         lua_pushnil(L);
  7566.     }
  7567.     return 1;
  7568. }
  7569.  
  7570. int32_t LuaInterface::luaDoRemoveCondition(lua_State* L)
  7571. {
  7572.     //doRemoveCondition(cid, type[, subId = 0[, conditionId = CONDITIONID_COMBAT]])
  7573.     int32_t conditionId = CONDITIONID_COMBAT;
  7574.     uint32_t params = lua_gettop(L), subId = 0;
  7575.     if(params > 3)
  7576.         conditionId = popNumber(L);
  7577.  
  7578.     if(params > 2)
  7579.         subId = popNumber(L);
  7580.  
  7581.     ConditionType_t conditionType = (ConditionType_t)popNumber(L);
  7582.     ScriptEnviroment* env = getEnv();
  7583.  
  7584.     Creature* creature = env->getCreatureByUID(popNumber(L));
  7585.     if(!creature)
  7586.     {
  7587.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7588.         lua_pushboolean(L, false);
  7589.         return 1;
  7590.     }
  7591.  
  7592.     Condition* condition = NULL;
  7593.     while((condition = creature->getCondition(conditionType, (ConditionId_t)conditionId, subId)))
  7594.         creature->removeCondition(condition);
  7595.  
  7596.     lua_pushboolean(L, true);
  7597.     return 1;
  7598. }
  7599.  
  7600. int32_t LuaInterface::luaDoRemoveConditions(lua_State* L)
  7601. {
  7602.     //doRemoveConditions(cid[, onlyPersistent])
  7603.     bool onlyPersistent = true;
  7604.     if(lua_gettop(L) > 1)
  7605.         onlyPersistent = popBoolean(L);
  7606.  
  7607.     ScriptEnviroment* env = getEnv();
  7608.     Creature* creature = env->getCreatureByUID(popNumber(L));
  7609.     if(!creature)
  7610.     {
  7611.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7612.         lua_pushboolean(L, false);
  7613.         return 1;
  7614.     }
  7615.  
  7616.     creature->removeConditions(CONDITIONEND_ABORT, onlyPersistent);
  7617.     lua_pushboolean(L, true);
  7618.     return 1;
  7619. }
  7620.  
  7621. int32_t LuaInterface::luaNumberToVariant(lua_State* L)
  7622. {
  7623.     //numberToVariant(number)
  7624.     LuaVariant var;
  7625.     var.type = VARIANT_NUMBER;
  7626.     var.number = popNumber(L);
  7627.  
  7628.     LuaInterface::pushVariant(L, var);
  7629.     return 1;
  7630. }
  7631.  
  7632. int32_t LuaInterface::luaStringToVariant(lua_State* L)
  7633. {
  7634.     //stringToVariant(string)
  7635.     LuaVariant var;
  7636.     var.type = VARIANT_STRING;
  7637.     var.text = popString(L);
  7638.  
  7639.     LuaInterface::pushVariant(L, var);
  7640.     return 1;
  7641. }
  7642.  
  7643. int32_t LuaInterface::luaPositionToVariant(lua_State* L)
  7644. {
  7645.     //positionToVariant(pos)
  7646.     LuaVariant var;
  7647.     var.type = VARIANT_POSITION;
  7648.     popPosition(L, var.pos);
  7649.  
  7650.     LuaInterface::pushVariant(L, var);
  7651.     return 1;
  7652. }
  7653.  
  7654. int32_t LuaInterface::luaTargetPositionToVariant(lua_State* L)
  7655. {
  7656.     //targetPositionToVariant(pos)
  7657.     LuaVariant var;
  7658.     var.type = VARIANT_TARGETPOSITION;
  7659.     popPosition(L, var.pos);
  7660.  
  7661.     LuaInterface::pushVariant(L, var);
  7662.     return 1;
  7663. }
  7664.  
  7665. int32_t LuaInterface::luaVariantToNumber(lua_State* L)
  7666. {
  7667.     //variantToNumber(var)
  7668.     LuaVariant var = popVariant(L);
  7669.  
  7670.     uint32_t number = 0;
  7671.     if(var.type == VARIANT_NUMBER)
  7672.         number = var.number;
  7673.  
  7674.     lua_pushnumber(L, number);
  7675.     return 1;
  7676. }
  7677.  
  7678. int32_t LuaInterface::luaVariantToString(lua_State* L)
  7679. {
  7680.     //variantToString(var)
  7681.     LuaVariant var = popVariant(L);
  7682.  
  7683.     std::string text = "";
  7684.     if(var.type == VARIANT_STRING)
  7685.         text = var.text;
  7686.  
  7687.     lua_pushstring(L, text.c_str());
  7688.     return 1;
  7689. }
  7690.  
  7691. int32_t LuaInterface::luaVariantToPosition(lua_State* L)
  7692. {
  7693.     //luaVariantToPosition(var)
  7694.     LuaVariant var = popVariant(L);
  7695.  
  7696.     PositionEx pos(0, 0, 0, 0);
  7697.     if(var.type == VARIANT_POSITION || var.type == VARIANT_TARGETPOSITION)
  7698.         pos = var.pos;
  7699.  
  7700.     pushPosition(L, pos, pos.stackpos);
  7701.     return 1;
  7702. }
  7703.  
  7704. int32_t LuaInterface::luaDoChangeSpeed(lua_State* L)
  7705. {
  7706.     //doChangeSpeed(cid, delta)
  7707.     int32_t delta = (int32_t)popNumber(L);
  7708.  
  7709.     ScriptEnviroment* env = getEnv();
  7710.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  7711.     {
  7712.         g_game.changeSpeed(creature, delta);
  7713.         lua_pushboolean(L, true);
  7714.     }
  7715.     else
  7716.     {
  7717.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7718.         lua_pushboolean(L, false);
  7719.     }
  7720.  
  7721.     return 1;
  7722. }
  7723.  
  7724. int32_t LuaInterface::luaSetCreatureOutfit(lua_State* L)
  7725. {
  7726.     //doSetCreatureOutfit(cid, outfit[, time = -1])
  7727.     int32_t time = -1;
  7728.     if(lua_gettop(L) > 2)
  7729.         time = (int32_t)popNumber(L);
  7730.  
  7731.     Outfit_t outfit = popOutfit(L);
  7732.     ScriptEnviroment* env = getEnv();
  7733.  
  7734.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  7735.         lua_pushboolean(L, Spell::CreateIllusion(creature, outfit, time) == RET_NOERROR);
  7736.     else
  7737.     {
  7738.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7739.         lua_pushboolean(L, false);
  7740.     }
  7741.  
  7742.     return 1;
  7743. }
  7744.  
  7745. int32_t LuaInterface::luaGetCreatureOutfit(lua_State* L)
  7746. {
  7747.     //getCreatureOutfit(cid)
  7748.     ScriptEnviroment* env = getEnv();
  7749.     if(const Creature* creature = env->getCreatureByUID(popNumber(L)))
  7750.         pushOutfit(L, creature->getCurrentOutfit());
  7751.     else
  7752.     {
  7753.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7754.         lua_pushboolean(L, false);
  7755.     }
  7756.  
  7757.     return 1;
  7758. }
  7759.  
  7760. int32_t LuaInterface::luaSetMonsterOutfit(lua_State* L)
  7761. {
  7762.     //doSetMonsterOutfit(cid, name[, time = -1])
  7763.     int32_t time = -1;
  7764.     if(lua_gettop(L) > 2)
  7765.         time = (int32_t)popNumber(L);
  7766.  
  7767.     std::string name = popString(L);
  7768.     ScriptEnviroment* env = getEnv();
  7769.  
  7770.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  7771.         lua_pushboolean(L, Spell::CreateIllusion(creature, name, time) == RET_NOERROR);
  7772.     else
  7773.     {
  7774.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7775.         lua_pushboolean(L, false);
  7776.     }
  7777.  
  7778.     return 1;
  7779. }
  7780.  
  7781. int32_t LuaInterface::luaSetItemOutfit(lua_State* L)
  7782. {
  7783.     //doSetItemOutfit(cid, item[, time = -1])
  7784.     int32_t time = -1;
  7785.     if(lua_gettop(L) > 2)
  7786.         time = (int32_t)popNumber(L);
  7787.  
  7788.     uint32_t item = (uint32_t)popNumber(L);
  7789.     ScriptEnviroment* env = getEnv();
  7790.  
  7791.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  7792.         lua_pushboolean(L, Spell::CreateIllusion(creature, item, time) == RET_NOERROR);
  7793.     else
  7794.     {
  7795.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7796.         lua_pushboolean(L, false);
  7797.     }
  7798.  
  7799.     return 1;
  7800. }
  7801.  
  7802. int32_t LuaInterface::luaGetStorageList(lua_State* L)
  7803. {
  7804.     //getStorageList()
  7805.     ScriptEnviroment* env = getEnv();
  7806.  
  7807.     StorageMap::const_iterator it = env->getStorageBegin();
  7808.     lua_newtable(L);
  7809.     for(uint32_t i = 1; it != env->getStorageEnd(); ++i, ++it)
  7810.     {
  7811.         lua_pushnumber(L, i);
  7812.         lua_pushstring(L, it->first.c_str());
  7813.         pushTable(L);
  7814.     }
  7815.  
  7816.     return 1;
  7817. }
  7818.  
  7819. int32_t LuaInterface::luaGetStorage(lua_State* L)
  7820. {
  7821.     //getStorage(key)
  7822.     ScriptEnviroment* env = getEnv();
  7823.     std::string strValue;
  7824.     if(!env->getStorage(popString(L), strValue))
  7825.     {
  7826.         lua_pushnumber(L, -1);
  7827.         lua_pushnil(L);
  7828.         return 2;
  7829.     }
  7830.  
  7831.     int32_t intValue = atoi(strValue.c_str());
  7832.     if(intValue || strValue == "0")
  7833.         lua_pushnumber(L, intValue);
  7834.     else
  7835.         lua_pushstring(L, strValue.c_str());
  7836.  
  7837.     return 1;
  7838. }
  7839.  
  7840. int32_t LuaInterface::luaDoSetStorage(lua_State* L)
  7841. {
  7842.     //doSetStorage(key, value)
  7843.     std::string value;
  7844.     bool tmp = false;
  7845.     if(lua_isnil(L, -1))
  7846.     {
  7847.         tmp = true;
  7848.         lua_pop(L, 1);
  7849.     }
  7850.     else
  7851.         value = popString(L);
  7852.  
  7853.     ScriptEnviroment* env = getEnv();
  7854.     if(!tmp)
  7855.         env->setStorage(popString(L), value);
  7856.     else
  7857.         env->eraseStorage(popString(L));
  7858.  
  7859.     lua_pushboolean(L, true);
  7860.     return 1;
  7861. }
  7862.  
  7863. int32_t LuaInterface::luaGetPlayerDepotItems(lua_State* L)
  7864. {
  7865.     //getPlayerDepotItems(cid, depotid)
  7866.     uint32_t depotid = popNumber(L);
  7867.  
  7868.     ScriptEnviroment* env = getEnv();
  7869.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  7870.     {
  7871.         if(const Depot* depot = player->getDepot(depotid, true))
  7872.             lua_pushnumber(L, depot->getItemHoldingCount());
  7873.         else
  7874.             lua_pushboolean(L, false);
  7875.     }
  7876.     else
  7877.     {
  7878.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  7879.         lua_pushboolean(L, false);
  7880.     }
  7881.  
  7882.     return 1;
  7883. }
  7884.  
  7885. int32_t LuaInterface::luaDoPlayerSetGuildId(lua_State* L)
  7886. {
  7887.     //doPlayerSetGuildId(cid, id)
  7888.     uint32_t id = popNumber(L);
  7889.  
  7890.     ScriptEnviroment* env = getEnv();
  7891.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  7892.     {
  7893.         if(player->getGuildId())
  7894.         {
  7895.             player->leaveGuild();
  7896.  
  7897.             if(!id)
  7898.                 lua_pushboolean(L, true);
  7899.             else if(IOGuild::getInstance()->guildExists(id))
  7900.                 lua_pushboolean(L, IOGuild::getInstance()->joinGuild(player, id));
  7901.             else
  7902.                 lua_pushboolean(L, false);
  7903.         }
  7904.         else if(id && IOGuild::getInstance()->guildExists(id))
  7905.             lua_pushboolean(L, IOGuild::getInstance()->joinGuild(player, id));
  7906.         else
  7907.             lua_pushboolean(L, false);
  7908.     }
  7909.     else
  7910.     {
  7911.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  7912.         lua_pushboolean(L, false);
  7913.     }
  7914.  
  7915.     return 1;
  7916. }
  7917.  
  7918. int32_t LuaInterface::luaDoPlayerSetGuildLevel(lua_State* L)
  7919. {
  7920.     //doPlayerSetGuildLevel(cid, level[, rank])
  7921.     uint32_t rank = 0;
  7922.     if(lua_gettop(L) > 2)
  7923.         rank = popNumber(L);
  7924.  
  7925.     GuildLevel_t level = (GuildLevel_t)popNumber(L);
  7926.     ScriptEnviroment* env = getEnv();
  7927.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  7928.         lua_pushboolean(L, player->setGuildLevel(level, rank));
  7929.     else
  7930.     {
  7931.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  7932.         lua_pushboolean(L, false);
  7933.     }
  7934.  
  7935.     return 1;
  7936. }
  7937.  
  7938. int32_t LuaInterface::luaDoPlayerSetGuildNick(lua_State* L)
  7939. {
  7940.     //doPlayerSetGuildNick(cid, nick)
  7941.     std::string nick = popString(L);
  7942.  
  7943.     ScriptEnviroment* env = getEnv();
  7944.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  7945.     {
  7946.         player->setGuildNick(nick);
  7947.         lua_pushboolean(L, true);
  7948.     }
  7949.     else
  7950.     {
  7951.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  7952.         lua_pushboolean(L, false);
  7953.     }
  7954.  
  7955.     return 1;
  7956. }
  7957.  
  7958. int32_t LuaInterface::luaGetGuildId(lua_State* L)
  7959. {
  7960.     //getGuildId(guildName)
  7961.     uint32_t guildId;
  7962.     if(IOGuild::getInstance()->getGuildId(guildId, popString(L)))
  7963.         lua_pushnumber(L, guildId);
  7964.     else
  7965.         lua_pushboolean(L, false);
  7966.  
  7967.     return 1;
  7968. }
  7969.  
  7970. int32_t LuaInterface::luaGetGuildMotd(lua_State* L)
  7971. {
  7972.     //getGuildMotd(guildId)
  7973.     uint32_t guildId = popNumber(L);
  7974.     if(IOGuild::getInstance()->guildExists(guildId))
  7975.         lua_pushstring(L, IOGuild::getInstance()->getMotd(guildId).c_str());
  7976.     else
  7977.         lua_pushboolean(L, false);
  7978.  
  7979.     return 1;
  7980. }
  7981.  
  7982. int32_t LuaInterface::luaDoMoveCreature(lua_State* L)
  7983. {
  7984.     //doMoveCreature(cid, direction[, flag = FLAG_NOLIMIT])
  7985.     uint32_t flags = FLAG_NOLIMIT;
  7986.     if(lua_gettop(L) > 2)
  7987.         flags = popNumber(L);
  7988.  
  7989.     int32_t direction = popNumber(L);
  7990.     if(direction < NORTH || direction > NORTHEAST)
  7991.     {
  7992.         lua_pushboolean(L, false);
  7993.         return 1;
  7994.     }
  7995.  
  7996.     ScriptEnviroment* env = getEnv();
  7997.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  7998.         lua_pushnumber(L, g_game.internalMoveCreature(creature, (Direction)direction, flags));
  7999.     else
  8000.     {
  8001.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8002.         lua_pushboolean(L, false);
  8003.     }
  8004.  
  8005.     return 1;
  8006. }
  8007.  
  8008. int32_t LuaInterface::luaDoSteerCreature(lua_State* L)
  8009. {
  8010.     //doSteerCreature(cid, position[, maxNodes])
  8011.     uint16_t maxNodes = 100;
  8012.     if(lua_gettop(L) > 2)
  8013.         maxNodes = popNumber(L);
  8014.  
  8015.     PositionEx pos;
  8016.     popPosition(L, pos);
  8017.  
  8018.     ScriptEnviroment* env = getEnv();
  8019.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8020.         lua_pushboolean(L, g_game.steerCreature(creature, pos, maxNodes));
  8021.     else
  8022.     {
  8023.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8024.         lua_pushboolean(L, false);
  8025.     }
  8026.  
  8027.     return 1;
  8028. }
  8029.  
  8030. int32_t LuaInterface::luaIsCreature(lua_State* L)
  8031. {
  8032.     //isCreature(cid)
  8033.     ScriptEnviroment* env = getEnv();
  8034.     lua_pushboolean(L, env->getCreatureByUID(popNumber(L)) ? true : false);
  8035.     return 1;
  8036. }
  8037.  
  8038. int32_t LuaInterface::luaIsMovable(lua_State* L)
  8039. {
  8040.     //isMovable(uid)
  8041.     ScriptEnviroment* env = getEnv();
  8042.     Thing* thing = env->getThingByUID(popNumber(L));
  8043.     if(thing && thing->isPushable())
  8044.         lua_pushboolean(L, true);
  8045.     else
  8046.         lua_pushboolean(L, false);
  8047.  
  8048.     return 1;
  8049. }
  8050.  
  8051. int32_t LuaInterface::luaGetCreatureByName(lua_State* L)
  8052. {
  8053.     //getCreatureByName(name)
  8054.     ScriptEnviroment* env = getEnv();
  8055.     if(Creature* creature = g_game.getCreatureByName(popString(L)))
  8056.         lua_pushnumber(L, env->addThing(creature));
  8057.     else
  8058.         lua_pushnil(L);
  8059.  
  8060.     return 1;
  8061. }
  8062.  
  8063. int32_t LuaInterface::luaGetPlayerByGUID(lua_State* L)
  8064. {
  8065.     //getPlayerByGUID(guid)
  8066.     ScriptEnviroment* env = getEnv();
  8067.     if(Player* player = g_game.getPlayerByGuid(popNumber(L)))
  8068.         lua_pushnumber(L, env->addThing(player));
  8069.     else
  8070.         lua_pushnil(L);
  8071.  
  8072.     return 1;
  8073. }
  8074.  
  8075. int32_t LuaInterface::luaGetPlayerByNameWildcard(lua_State* L)
  8076. {
  8077.     //getPlayerByNameWildcard(name~[, ret = false])
  8078.     Player* player = NULL;
  8079.     bool pushValue = false;
  8080.     if(lua_gettop(L) > 1)
  8081.         pushValue = popBoolean(L);
  8082.  
  8083.     ScriptEnviroment* env = getEnv();
  8084.     ReturnValue ret = g_game.getPlayerByNameWildcard(popString(L), player);
  8085.     if(ret == RET_NOERROR)
  8086.         lua_pushnumber(L, env->addThing(player));
  8087.     else if(pushValue)
  8088.         lua_pushnumber(L, ret);
  8089.     else
  8090.         lua_pushnil(L);
  8091.  
  8092.     return 1;
  8093. }
  8094.  
  8095. int32_t LuaInterface::luaGetPlayerGUIDByName(lua_State* L)
  8096. {
  8097.     //getPlayerGUIDByName(name[, multiworld = false])
  8098.     bool multiworld = false;
  8099.     if(lua_gettop(L) > 1)
  8100.         multiworld = popBoolean(L);
  8101.  
  8102.     std::string name = popString(L);
  8103.     uint32_t guid;
  8104.     if(Player* player = g_game.getPlayerByName(name))
  8105.         lua_pushnumber(L, player->getGUID());
  8106.     else if(IOLoginData::getInstance()->getGuidByName(guid, name, multiworld))
  8107.         lua_pushnumber(L, guid);
  8108.     else
  8109.         lua_pushnil(L);
  8110.  
  8111.     return 1;
  8112. }
  8113.  
  8114. int32_t LuaInterface::luaGetPlayerNameByGUID(lua_State* L)
  8115. {
  8116.     //getPlayerNameByGUID(guid[, multiworld = false])
  8117.     bool multiworld = false;
  8118.     if(lua_gettop(L) > 1)
  8119.         multiworld = popBoolean(L);
  8120.  
  8121.     uint32_t guid = popNumber(L);
  8122.     std::string name;
  8123.     if(!IOLoginData::getInstance()->getNameByGuid(guid, name, multiworld))
  8124.     {
  8125.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8126.         lua_pushnil(L);
  8127.         return 1;
  8128.     }
  8129.  
  8130.     lua_pushstring(L, name.c_str());
  8131.     return 1;
  8132. }
  8133.  
  8134. int32_t LuaInterface::luaDoPlayerChangeName(lua_State* L)
  8135. {
  8136.     //doPlayerChangeName(guid, oldName, newName)
  8137.     std::string newName = popString(L), oldName = popString(L);
  8138.     uint32_t guid = popNumber(L);
  8139.     if(IOLoginData::getInstance()->changeName(guid, newName, oldName))
  8140.     {
  8141.         if(House* house = Houses::getInstance()->getHouseByPlayerId(guid))
  8142.             house->updateDoorDescription(newName);
  8143.  
  8144.         lua_pushboolean(L, true);
  8145.     }
  8146.     else
  8147.         lua_pushboolean(L, false);
  8148.  
  8149.     return 1;
  8150. }
  8151.  
  8152. int32_t LuaInterface::luaGetPlayersByAccountId(lua_State* L)
  8153. {
  8154.     //getPlayersByAccountId(accId)
  8155.     PlayerVector players = g_game.getPlayersByAccount(popNumber(L));
  8156.  
  8157.     ScriptEnviroment* env = getEnv();
  8158.     PlayerVector::iterator it = players.begin();
  8159.  
  8160.     lua_newtable(L);
  8161.     for(uint32_t i = 1; it != players.end(); ++it, ++i)
  8162.     {
  8163.         lua_pushnumber(L, i);
  8164.         lua_pushnumber(L, env->addThing(*it));
  8165.         pushTable(L);
  8166.     }
  8167.  
  8168.     return 1;
  8169. }
  8170.  
  8171. int32_t LuaInterface::luaGetIpByName(lua_State* L)
  8172. {
  8173.     //getIpByName(name)
  8174.     std::string name = popString(L);
  8175.     if(Player* player = g_game.getPlayerByName(name))
  8176.         lua_pushnumber(L, player->getIP());
  8177.     else if(IOLoginData::getInstance()->playerExists(name))
  8178.         lua_pushnumber(L, IOLoginData::getInstance()->getLastIPByName(name));
  8179.     else
  8180.         lua_pushnil(L);
  8181.  
  8182.     return 1;
  8183. }
  8184.  
  8185. int32_t LuaInterface::luaGetPlayersByIp(lua_State* L)
  8186. {
  8187.     //getPlayersByIp(ip[, mask])
  8188.     uint32_t mask = 0xFFFFFFFF;
  8189.     if(lua_gettop(L) > 1)
  8190.         mask = (uint32_t)popNumber(L);
  8191.  
  8192.     PlayerVector players = g_game.getPlayersByIP(popNumber(L), mask);
  8193.  
  8194.     ScriptEnviroment* env = getEnv();
  8195.     PlayerVector::iterator it = players.begin();
  8196.  
  8197.     lua_newtable(L);
  8198.     for(uint32_t i = 1; it != players.end(); ++it, ++i)
  8199.     {
  8200.         lua_pushnumber(L, i);
  8201.         lua_pushnumber(L, env->addThing(*it));
  8202.         pushTable(L);
  8203.     }
  8204.  
  8205.     return 1;
  8206. }
  8207.  
  8208. int32_t LuaInterface::luaGetAccountIdByName(lua_State* L)
  8209. {
  8210.     //getAccountIdByName(name)
  8211.     std::string name = popString(L);
  8212.  
  8213.     if(Player* player = g_game.getPlayerByName(name))
  8214.         lua_pushnumber(L, player->getAccount());
  8215.     else
  8216.         lua_pushnumber(L, IOLoginData::getInstance()->getAccountIdByName(name));
  8217.  
  8218.     return 1;
  8219. }
  8220.  
  8221. int32_t LuaInterface::luaGetAccountByName(lua_State* L)
  8222. {
  8223.     //getAccountByName(name)
  8224.     std::string name = popString(L);
  8225.  
  8226.     if(Player* player = g_game.getPlayerByName(name))
  8227.         lua_pushstring(L, player->getAccountName().c_str());
  8228.     else
  8229.     {
  8230.         std::string tmp;
  8231.         IOLoginData::getInstance()->getAccountName(IOLoginData::getInstance()->getAccountIdByName(name), tmp);
  8232.         lua_pushstring(L, tmp.c_str());
  8233.     }
  8234.  
  8235.     return 1;
  8236. }
  8237.  
  8238. int32_t LuaInterface::luaGetAccountIdByAccount(lua_State* L)
  8239. {
  8240.     //getAccountIdByAccount(accName)
  8241.     uint32_t value = 0;
  8242.     IOLoginData::getInstance()->getAccountId(popString(L), value);
  8243.     lua_pushnumber(L, value);
  8244.     return 1;
  8245. }
  8246.  
  8247. int32_t LuaInterface::luaGetAccountByAccountId(lua_State* L)
  8248. {
  8249.     //getAccountByAccountId(accId)
  8250.     std::string value = 0;
  8251.     IOLoginData::getInstance()->getAccountName(popNumber(L), value);
  8252.     lua_pushstring(L, value.c_str());
  8253.     return 1;
  8254. }
  8255.  
  8256. int32_t LuaInterface::luaGetAccountFlagValue(lua_State* L)
  8257. {
  8258.     //getAccountFlagValue(name/id)
  8259.     PlayerFlags flag = (PlayerFlags)popNumber(L);
  8260.     if(lua_isnumber(L, -1))
  8261.         lua_pushboolean(L, IOLoginData::getInstance()->hasFlag((uint32_t)popNumber(L), flag));
  8262.     else
  8263.         lua_pushboolean(L, IOLoginData::getInstance()->hasFlag(flag, popString(L)));
  8264.  
  8265.     return 1;
  8266. }
  8267.  
  8268. int32_t LuaInterface::luaGetAccountCustomFlagValue(lua_State* L)
  8269. {
  8270.     //getAccountCustomFlagValue(name/id)
  8271.     PlayerCustomFlags flag = (PlayerCustomFlags)popNumber(L);
  8272.     if(lua_isnumber(L, -1))
  8273.         lua_pushboolean(L, IOLoginData::getInstance()->hasCustomFlag((uint32_t)popNumber(L), flag));
  8274.     else
  8275.         lua_pushboolean(L, IOLoginData::getInstance()->hasCustomFlag(flag, popString(L)));
  8276.  
  8277.     return 1;
  8278. }
  8279.  
  8280. int32_t LuaInterface::luaRegisterCreatureEvent(lua_State* L)
  8281. {
  8282.     //registerCreatureEvent(cid, name)
  8283.     std::string name = popString(L);
  8284.  
  8285.     ScriptEnviroment* env = getEnv();
  8286.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8287.         lua_pushboolean(L, creature->registerCreatureEvent(name));
  8288.     else
  8289.     {
  8290.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8291.         lua_pushboolean(L, false);
  8292.     }
  8293.  
  8294.     return 1;
  8295. }
  8296.  
  8297. int32_t LuaInterface::luaUnregisterCreatureEvent(lua_State* L)
  8298. {
  8299.     //unregisterCreatureEvent(cid, name)
  8300.     std::string name = popString(L);
  8301.  
  8302.     ScriptEnviroment* env = getEnv();
  8303.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8304.         lua_pushboolean(L, creature->unregisterCreatureEvent(name));
  8305.     else
  8306.     {
  8307.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8308.         lua_pushboolean(L, false);
  8309.     }
  8310.  
  8311.     return 1;
  8312. }
  8313.  
  8314. int32_t LuaInterface::luaUnregisterCreatureEventType(lua_State* L)
  8315. {
  8316.     //unregisterCreatureEventType(cid, type)
  8317.     std::string type = popString(L);
  8318.  
  8319.     ScriptEnviroment* env = getEnv();
  8320.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8321.     {
  8322.         CreatureEventType_t _type = g_creatureEvents->getType(type);
  8323.         if(_type != CREATURE_EVENT_NONE)
  8324.         {
  8325.             creature->unregisterCreatureEvent(_type);
  8326.             lua_pushboolean(L, true);
  8327.         }
  8328.         else
  8329.         {
  8330.             errorEx("Invalid event type");
  8331.             lua_pushboolean(L, false);
  8332.         }
  8333.     }
  8334.     else
  8335.     {
  8336.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8337.         lua_pushboolean(L, false);
  8338.     }
  8339.  
  8340.     return 1;
  8341. }
  8342.  
  8343. int32_t LuaInterface::luaGetContainerSize(lua_State* L)
  8344. {
  8345.     //getContainerSize(uid)
  8346.     ScriptEnviroment* env = getEnv();
  8347.     if(Container* container = env->getContainerByUID(popNumber(L)))
  8348.         lua_pushnumber(L, container->size());
  8349.     else
  8350.     {
  8351.         errorEx(getError(LUA_ERROR_CONTAINER_NOT_FOUND));
  8352.         lua_pushboolean(L, false);
  8353.     }
  8354.  
  8355.     return 1;
  8356. }
  8357.  
  8358. int32_t LuaInterface::luaGetContainerCap(lua_State* L)
  8359. {
  8360.     //getContainerCap(uid)
  8361.     ScriptEnviroment* env = getEnv();
  8362.     if(Container* container = env->getContainerByUID(popNumber(L)))
  8363.         lua_pushnumber(L, container->capacity());
  8364.     else
  8365.     {
  8366.         errorEx(getError(LUA_ERROR_CONTAINER_NOT_FOUND));
  8367.         lua_pushboolean(L, false);
  8368.     }
  8369.  
  8370.     return 1;
  8371. }
  8372.  
  8373. int32_t LuaInterface::luaGetContainerItem(lua_State* L)
  8374. {
  8375.     //getContainerItem(uid, slot)
  8376.     uint32_t slot = popNumber(L);
  8377.     ScriptEnviroment* env = getEnv();
  8378.     if(Container* container = env->getContainerByUID(popNumber(L)))
  8379.     {
  8380.         if(Item* item = container->getItem(slot))
  8381.             pushThing(L, item, env->addThing(item));
  8382.         else
  8383.             pushThing(L, NULL, 0);
  8384.     }
  8385.     else
  8386.     {
  8387.         errorEx(getError(LUA_ERROR_CONTAINER_NOT_FOUND));
  8388.         pushThing(L, NULL, 0);
  8389.     }
  8390.  
  8391.     return 1;
  8392. }
  8393.  
  8394. int32_t LuaInterface::luaDoAddContainerItemEx(lua_State* L)
  8395. {
  8396.     //doAddContainerItemEx(uid, virtuid)
  8397.     uint32_t virtuid = popNumber(L);
  8398.     ScriptEnviroment* env = getEnv();
  8399.     if(Container* container = env->getContainerByUID(popNumber(L)))
  8400.     {
  8401.         Item* item = env->getItemByUID(virtuid);
  8402.         if(!item)
  8403.         {
  8404.             errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  8405.             lua_pushboolean(L, false);
  8406.             return 1;
  8407.         }
  8408.  
  8409.         if(item->getParent() != VirtualCylinder::virtualCylinder)
  8410.         {
  8411.             lua_pushboolean(L, false);
  8412.             return 1;
  8413.         }
  8414.  
  8415.         ReturnValue ret = g_game.internalAddItem(NULL, container, item);
  8416.         if(ret == RET_NOERROR)
  8417.             env->removeTempItem(env, item);
  8418.  
  8419.         lua_pushnumber(L, ret);
  8420.         return 1;
  8421.     }
  8422.     else
  8423.     {
  8424.         errorEx(getError(LUA_ERROR_CONTAINER_NOT_FOUND));
  8425.         lua_pushboolean(L, false);
  8426.         return 1;
  8427.     }
  8428. }
  8429.  
  8430. int32_t LuaInterface::luaDoAddContainerItem(lua_State* L)
  8431. {
  8432.     //doAddContainerItem(uid, itemid[, count/subType = 1])
  8433.     uint32_t count = 1;
  8434.     if(lua_gettop(L) > 2)
  8435.         count = popNumber(L);
  8436.  
  8437.     uint16_t itemId = popNumber(L);
  8438.     ScriptEnviroment* env = getEnv();
  8439.  
  8440.     Container* container = env->getContainerByUID((uint32_t)popNumber(L));
  8441.     if(!container)
  8442.     {
  8443.         errorEx(getError(LUA_ERROR_CONTAINER_NOT_FOUND));
  8444.         lua_pushboolean(L, false);
  8445.         return 1;
  8446.     }
  8447.  
  8448.     const ItemType& it = Item::items[itemId];
  8449.     int32_t itemCount = 1, subType = 1;
  8450.     if(it.hasSubType())
  8451.     {
  8452.         if(it.stackable)
  8453.             itemCount = (int32_t)std::ceil((float)count / 100);
  8454.  
  8455.         subType = count;
  8456.     }
  8457.     else
  8458.         itemCount = std::max((uint32_t)1, count);
  8459.  
  8460.     uint32_t ret = 0;
  8461.     Item* newItem = NULL;
  8462.     while(itemCount > 0)
  8463.     {
  8464.         int32_t stackCount = std::min(100, subType);
  8465.         if(!(newItem = Item::CreateItem(itemId, stackCount)))
  8466.         {
  8467.             errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  8468.             lua_pushboolean(L, false);
  8469.             return 1;
  8470.         }
  8471.  
  8472.         if(it.stackable)
  8473.             subType -= stackCount;
  8474.  
  8475.         uint32_t dummy = 0;
  8476.         Item* stackItem = NULL;
  8477.         if(g_game.internalAddItem(NULL, container, newItem, INDEX_WHEREEVER, FLAG_NOLIMIT, false, dummy, &stackItem) != RET_NOERROR)
  8478.         {
  8479.             delete newItem;
  8480.             lua_pushboolean(L, false);
  8481.             return ++ret;
  8482.         }
  8483.  
  8484.         ++ret;
  8485.         if(newItem->getParent())
  8486.             lua_pushnumber(L, env->addThing(newItem));
  8487.         else if(stackItem)
  8488.             lua_pushnumber(L, env->addThing(stackItem));
  8489.         else //stackable item stacked with existing object, newItem will be released
  8490.             lua_pushnil(L);
  8491.  
  8492.         --itemCount;
  8493.     }
  8494.  
  8495.     if(ret)
  8496.         return ret;
  8497.  
  8498.     lua_pushnil(L);
  8499.     return 1;
  8500. }
  8501.  
  8502. int32_t LuaInterface::luaDoPlayerAddOutfit(lua_State *L)
  8503. {
  8504.     //Consider using doPlayerAddOutfitId instead
  8505.     //doPlayerAddOutfit(cid, looktype, addon)
  8506.     uint32_t addon = popNumber(L), lookType = popNumber(L);
  8507.     ScriptEnviroment* env = getEnv();
  8508.  
  8509.     Player* player = env->getPlayerByUID((uint32_t)popNumber(L));
  8510.     if(!player)
  8511.     {
  8512.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8513.         lua_pushboolean(L, false);
  8514.         return 1;
  8515.     }
  8516.  
  8517.     Outfit outfit;
  8518.     if(Outfits::getInstance()->getOutfit(lookType, outfit))
  8519.     {
  8520.         lua_pushboolean(L, player->addOutfit(outfit.outfitId, addon));
  8521.         return 1;
  8522.     }
  8523.  
  8524.     lua_pushboolean(L, false);
  8525.     return 1;
  8526. }
  8527.  
  8528. int32_t LuaInterface::luaDoPlayerRemoveOutfit(lua_State *L)
  8529. {
  8530.     //Consider using doPlayerRemoveOutfitId instead
  8531.     //doPlayerRemoveOutfit(cid, looktype[, addon = 0])
  8532.     uint32_t addon = 0xFF;
  8533.     if(lua_gettop(L) > 2)
  8534.         addon = popNumber(L);
  8535.  
  8536.     uint32_t lookType = popNumber(L);
  8537.     ScriptEnviroment* env = getEnv();
  8538.  
  8539.     Player* player = env->getPlayerByUID((uint32_t)popNumber(L));
  8540.     if(!player)
  8541.     {
  8542.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8543.         lua_pushboolean(L, false);
  8544.         return 1;
  8545.     }
  8546.  
  8547.     Outfit outfit;
  8548.     if(Outfits::getInstance()->getOutfit(lookType, outfit))
  8549.     {
  8550.         lua_pushboolean(L, player->removeOutfit(outfit.outfitId, addon));
  8551.         return 1;
  8552.     }
  8553.  
  8554.     lua_pushboolean(L, false);
  8555.     return 1;
  8556. }
  8557.  
  8558. int32_t LuaInterface::luaDoPlayerAddOutfitId(lua_State *L)
  8559. {
  8560.     //doPlayerAddOutfitId(cid, outfitId, addon)
  8561.     uint32_t addon = popNumber(L), outfitId = popNumber(L);
  8562.     ScriptEnviroment* env = getEnv();
  8563.  
  8564.     Player* player = env->getPlayerByUID((uint32_t)popNumber(L));
  8565.     if(!player)
  8566.     {
  8567.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8568.         lua_pushboolean(L, false);
  8569.         return 1;
  8570.     }
  8571.  
  8572.     lua_pushboolean(L, player->addOutfit(outfitId, addon));
  8573.     return 1;
  8574. }
  8575.  
  8576. int32_t LuaInterface::luaDoPlayerRemoveOutfitId(lua_State *L)
  8577. {
  8578.     //doPlayerRemoveOutfitId(cid, outfitId[, addon = 0])
  8579.     uint32_t addon = 0xFF;
  8580.     if(lua_gettop(L) > 2)
  8581.         addon = popNumber(L);
  8582.  
  8583.     uint32_t outfitId = popNumber(L);
  8584.     ScriptEnviroment* env = getEnv();
  8585.  
  8586.     Player* player = env->getPlayerByUID((uint32_t)popNumber(L));
  8587.     if(!player)
  8588.     {
  8589.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8590.         lua_pushboolean(L, false);
  8591.         return 1;
  8592.     }
  8593.  
  8594.     lua_pushboolean(L, player->removeOutfit(outfitId, addon));
  8595.     return 1;
  8596. }
  8597.  
  8598. int32_t LuaInterface::luaCanPlayerWearOutfit(lua_State* L)
  8599. {
  8600.     //canPlayerWearOutfit(cid, looktype[, addon = 0])
  8601.     uint32_t addon = 0;
  8602.     if(lua_gettop(L) > 2)
  8603.         addon = popNumber(L);
  8604.  
  8605.     uint32_t lookType = popNumber(L);
  8606.     ScriptEnviroment* env = getEnv();
  8607.  
  8608.     Player* player = env->getPlayerByUID(popNumber(L));
  8609.     if(!player)
  8610.     {
  8611.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8612.         lua_pushboolean(L, false);
  8613.         return 1;
  8614.     }
  8615.  
  8616.     Outfit outfit;
  8617.     if(Outfits::getInstance()->getOutfit(lookType, outfit))
  8618.     {
  8619.         lua_pushboolean(L, player->canWearOutfit(outfit.outfitId, addon));
  8620.         return 1;
  8621.     }
  8622.  
  8623.     lua_pushboolean(L, false);
  8624.     return 1;
  8625. }
  8626.  
  8627. int32_t LuaInterface::luaCanPlayerWearOutfitId(lua_State* L)
  8628. {
  8629.     //canPlayerWearOutfitId(cid, outfitId[, addon = 0])
  8630.     uint32_t addon = 0;
  8631.     if(lua_gettop(L) > 2)
  8632.         addon = popNumber(L);
  8633.  
  8634.     uint32_t outfitId = popNumber(L);
  8635.     ScriptEnviroment* env = getEnv();
  8636.  
  8637.     Player* player = env->getPlayerByUID(popNumber(L));
  8638.     if(!player)
  8639.     {
  8640.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8641.         lua_pushboolean(L, false);
  8642.         return 1;
  8643.     }
  8644.  
  8645.     lua_pushboolean(L, player->canWearOutfit(outfitId, addon));
  8646.     return 1;
  8647. }
  8648.  
  8649. int32_t LuaInterface::luaDoCreatureChangeOutfit(lua_State* L)
  8650. {
  8651.     //doCreatureChangeOutfit(cid, outfit)
  8652.     Outfit_t outfit = popOutfit(L);
  8653.     ScriptEnviroment* env = getEnv();
  8654.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8655.     {
  8656.         if(Player* player = creature->getPlayer())
  8657.             player->changeOutfit(outfit, false);
  8658.         else
  8659.             creature->defaultOutfit = outfit;
  8660.  
  8661.         if(!creature->hasCondition(CONDITION_OUTFIT, 1))
  8662.             g_game.internalCreatureChangeOutfit(creature, outfit);
  8663.  
  8664.         lua_pushboolean(L, true);
  8665.     }
  8666.     else
  8667.     {
  8668.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8669.         lua_pushboolean(L, false);
  8670.     }
  8671.  
  8672.     return 1;
  8673. }
  8674.  
  8675. int32_t LuaInterface::luaDoPlayerPopupFYI(lua_State* L)
  8676. {
  8677.     //doPlayerPopupFYI(cid, message)
  8678.     std::string message = popString(L);
  8679.  
  8680.     ScriptEnviroment* env = getEnv();
  8681.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  8682.     {
  8683.         player->sendFYIBox(message);
  8684.         lua_pushboolean(L, true);
  8685.     }
  8686.     else
  8687.     {
  8688.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8689.         lua_pushboolean(L, false);
  8690.     }
  8691.  
  8692.     return 1;
  8693. }
  8694.  
  8695. int32_t LuaInterface::luaDoPlayerSendTutorial(lua_State* L)
  8696. {
  8697.     //doPlayerSendTutorial(cid, id)
  8698.     uint8_t id = (uint8_t)popNumber(L);
  8699.  
  8700.     ScriptEnviroment* env = getEnv();
  8701.  
  8702.     Player* player = env->getPlayerByUID(popNumber(L));
  8703.     if(!player)
  8704.     {
  8705.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8706.         lua_pushboolean(L, false);
  8707.         return 1;
  8708.     }
  8709.  
  8710.     player->sendTutorial(id);
  8711.     lua_pushboolean(L, true);
  8712.     return 1;
  8713. }
  8714.  
  8715. int32_t LuaInterface::luaDoPlayerSendMailByName(lua_State* L)
  8716. {
  8717.     //doPlayerSendMailByName(name, item[, town[, actor]])
  8718.     ScriptEnviroment* env = getEnv();
  8719.     int32_t params = lua_gettop(L);
  8720.  
  8721.     Creature* actor = NULL;
  8722.     if(params > 3)
  8723.         actor = env->getCreatureByUID(popNumber(L));
  8724.  
  8725.     uint32_t town = 0;
  8726.     if(params > 2)
  8727.         town = popNumber(L);
  8728.  
  8729.     Item* item = env->getItemByUID(popNumber(L));
  8730.     if(!item)
  8731.     {
  8732.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  8733.         lua_pushboolean(L, false);
  8734.         return 1;
  8735.     }
  8736.  
  8737.     if(item->getParent() != VirtualCylinder::virtualCylinder)
  8738.     {
  8739.         lua_pushboolean(L, false);
  8740.         return 1;
  8741.     }
  8742.  
  8743.     bool result = IOLoginData::getInstance()->playerMail(actor, popString(L), town, item);
  8744.     if(result)
  8745.         env->removeTempItem(env, item);
  8746.  
  8747.     lua_pushboolean(L, result);
  8748.     return 1;
  8749. }
  8750.  
  8751. int32_t LuaInterface::luaDoPlayerAddMapMark(lua_State* L)
  8752. {
  8753.     //doPlayerAddMapMark(cid, pos, type[, description])
  8754.     std::string description;
  8755.     if(lua_gettop(L) > 3)
  8756.         description = popString(L);
  8757.  
  8758.     MapMarks_t type = (MapMarks_t)popNumber(L);
  8759.     PositionEx pos;
  8760.     popPosition(L, pos);
  8761.  
  8762.     ScriptEnviroment* env = getEnv();
  8763.     Player* player = env->getPlayerByUID(popNumber(L));
  8764.     if(!player)
  8765.     {
  8766.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8767.         lua_pushboolean(L, false);
  8768.         return 1;
  8769.     }
  8770.  
  8771.     player->sendAddMarker(pos, type, description);
  8772.     lua_pushboolean(L, true);
  8773.     return 1;
  8774. }
  8775.  
  8776. int32_t LuaInterface::luaDoPlayerAddPremiumDays(lua_State* L)
  8777. {
  8778.     //doPlayerAddPremiumDays(cid, days)
  8779.     int32_t days = popNumber(L);
  8780.     ScriptEnviroment* env = getEnv();
  8781.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  8782.     {
  8783.         player->addPremiumDays(days);
  8784.         lua_pushboolean(L, true);
  8785.     }
  8786.     else
  8787.     {
  8788.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8789.         lua_pushboolean(L, false);
  8790.     }
  8791.  
  8792.     return 1;
  8793. }
  8794.  
  8795. int32_t LuaInterface::luaGetCreatureLastPosition(lua_State* L)
  8796. {
  8797.     //getCreatureLastPosition(cid)
  8798.     ScriptEnviroment* env = getEnv();
  8799.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8800.         pushPosition(L, creature->getLastPosition(), 0);
  8801.     else
  8802.     {
  8803.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8804.         lua_pushboolean(L, false);
  8805.     }
  8806.  
  8807.     return 1;
  8808. }
  8809.  
  8810. int32_t LuaInterface::luaGetCreatureName(lua_State* L)
  8811. {
  8812.     //getCreatureName(cid)
  8813.     ScriptEnviroment* env = getEnv();
  8814.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8815.         lua_pushstring(L, creature->getName().c_str());
  8816.     else
  8817.     {
  8818.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8819.         lua_pushboolean(L, false);
  8820.     }
  8821.  
  8822.     return 1;
  8823. }
  8824.  
  8825. int32_t LuaInterface::luaGetCreatureNoMove(lua_State* L)
  8826. {
  8827.     //getCreatureNoMove(cid)
  8828.     ScriptEnviroment* env = getEnv();
  8829.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8830.         lua_pushboolean(L, creature->getNoMove());
  8831.     else
  8832.     {
  8833.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8834.         lua_pushboolean(L, false);
  8835.     }
  8836.  
  8837.     return 1;
  8838. }
  8839.  
  8840. int32_t LuaInterface::luaGetCreatureGuildEmblem(lua_State* L)
  8841. {
  8842.     //getCreatureGuildEmblem(cid[, target])
  8843.     uint32_t tid = 0;
  8844.     if(lua_gettop(L) > 1)
  8845.         tid = popNumber(L);
  8846.  
  8847.     ScriptEnviroment* env = getEnv();
  8848.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8849.     {
  8850.         if(!tid)
  8851.             lua_pushnumber(L, creature->getEmblem());
  8852.         else if(Creature* target = env->getCreatureByUID(tid))
  8853.             lua_pushnumber(L, creature->getGuildEmblem(target));
  8854.         else
  8855.         {
  8856.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8857.             lua_pushboolean(L, false);
  8858.         }
  8859.     }
  8860.     else
  8861.     {
  8862.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8863.         lua_pushboolean(L, false);
  8864.     }
  8865.  
  8866.     return 1;
  8867. }
  8868.  
  8869. int32_t LuaInterface::luaDoCreatureSetGuildEmblem(lua_State* L)
  8870. {
  8871.     //doCreatureSetGuildEmblem(cid, emblem)
  8872.     GuildEmblems_t emblem = (GuildEmblems_t)popNumber(L);
  8873.     ScriptEnviroment* env = getEnv();
  8874.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8875.     {
  8876.         creature->setEmblem(emblem);
  8877.         g_game.updateCreatureEmblem(creature);
  8878.         lua_pushboolean(L, true);
  8879.     }
  8880.     else
  8881.     {
  8882.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8883.         lua_pushboolean(L, false);
  8884.     }
  8885.  
  8886.     return 1;
  8887. }
  8888.  
  8889. int32_t LuaInterface::luaGetCreaturePartyShield(lua_State* L)
  8890. {
  8891.     //getCreaturePartyShield(cid[, target])
  8892.     uint32_t tid = 0;
  8893.     if(lua_gettop(L) > 1)
  8894.         tid = popNumber(L);
  8895.  
  8896.     ScriptEnviroment* env = getEnv();
  8897.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8898.     {
  8899.         if(!tid)
  8900.             lua_pushnumber(L, creature->getShield());
  8901.         else if(Creature* target = env->getCreatureByUID(tid))
  8902.             lua_pushnumber(L, creature->getPartyShield(target));
  8903.         else
  8904.         {
  8905.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8906.             lua_pushboolean(L, false);
  8907.         }
  8908.     }
  8909.     else
  8910.     {
  8911.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8912.         lua_pushboolean(L, false);
  8913.     }
  8914.  
  8915.     return 1;
  8916. }
  8917.  
  8918. int32_t LuaInterface::luaDoCreatureSetPartyShield(lua_State* L)
  8919. {
  8920.     //doCreatureSetPartyShield(cid, shield)
  8921.     PartyShields_t shield = (PartyShields_t)popNumber(L);
  8922.     ScriptEnviroment* env = getEnv();
  8923.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8924.     {
  8925.         creature->setShield(shield);
  8926.         g_game.updateCreatureShield(creature);
  8927.         lua_pushboolean(L, true);
  8928.     }
  8929.     else
  8930.     {
  8931.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8932.         lua_pushboolean(L, false);
  8933.     }
  8934.  
  8935.     return 1;
  8936. }
  8937.  
  8938. int32_t LuaInterface::luaGetCreatureSkullType(lua_State* L)
  8939. {
  8940.     //getCreatureSkullType(cid[, target])
  8941.     uint32_t tid = 0;
  8942.     if(lua_gettop(L) > 1)
  8943.         tid = popNumber(L);
  8944.  
  8945.     ScriptEnviroment* env = getEnv();
  8946.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8947.     {
  8948.         if(!tid)
  8949.             lua_pushnumber(L, creature->getSkull());
  8950.         else if(Creature* target = env->getCreatureByUID(tid))
  8951.             lua_pushnumber(L, creature->getSkullType(target));
  8952.         else
  8953.         {
  8954.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8955.             lua_pushboolean(L, false);
  8956.         }
  8957.     }
  8958.     else
  8959.     {
  8960.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8961.         lua_pushboolean(L, false);
  8962.     }
  8963.  
  8964.     return 1;
  8965. }
  8966.  
  8967. int32_t LuaInterface::luaDoCreatureSetLookDir(lua_State* L)
  8968. {
  8969.     //doCreatureSetLookDirection(cid, dir)
  8970.     Direction dir = (Direction)popNumber(L);
  8971.     ScriptEnviroment* env = getEnv();
  8972.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8973.     {
  8974.         if(dir < NORTH || dir > WEST)
  8975.         {
  8976.             lua_pushboolean(L, false);
  8977.             return 1;
  8978.         }
  8979.  
  8980.         g_game.internalCreatureTurn(creature, dir);
  8981.         lua_pushboolean(L, true);
  8982.     }
  8983.     else
  8984.     {
  8985.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8986.         lua_pushboolean(L, false);
  8987.     }
  8988.  
  8989.     return 1;
  8990. }
  8991.  
  8992. int32_t LuaInterface::luaDoCreatureSetSkullType(lua_State* L)
  8993. {
  8994.     //doCreatureSetSkullType(cid, skull)
  8995.     Skulls_t skull = (Skulls_t)popNumber(L);
  8996.     ScriptEnviroment* env = getEnv();
  8997.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8998.     {
  8999.         creature->setSkull(skull);
  9000.         g_game.updateCreatureSkull(creature);
  9001.         lua_pushboolean(L, true);
  9002.     }
  9003.     else
  9004.     {
  9005.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9006.         lua_pushboolean(L, false);
  9007.     }
  9008.  
  9009.     return 1;
  9010. }
  9011.  
  9012. int32_t LuaInterface::luaDoPlayerSetSkullEnd(lua_State* L)
  9013. {
  9014.     //doPlayerSetSkullEnd(cid, time, type)
  9015.     Skulls_t _skull = (Skulls_t)popNumber(L);
  9016.     time_t _time = (time_t)std::max((int64_t)0, popNumber(L));
  9017.  
  9018.     ScriptEnviroment* env = getEnv();
  9019.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9020.     {
  9021.         player->setSkullEnd(_time, false, _skull);
  9022.         lua_pushboolean(L, true);
  9023.     }
  9024.     else
  9025.     {
  9026.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9027.         lua_pushboolean(L, false);
  9028.     }
  9029.  
  9030.     return 1;
  9031. }
  9032.  
  9033. int32_t LuaInterface::luaGetCreatureSpeed(lua_State* L)
  9034. {
  9035.     //getCreatureSpeed(cid)
  9036.     ScriptEnviroment* env = getEnv();
  9037.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  9038.         lua_pushnumber(L, creature->getSpeed());
  9039.     else
  9040.     {
  9041.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9042.         lua_pushboolean(L, false);
  9043.     }
  9044.  
  9045.     return 1;
  9046. }
  9047.  
  9048. int32_t LuaInterface::luaGetCreatureBaseSpeed(lua_State* L)
  9049. {
  9050.     //getCreatureBaseSpeed(cid)
  9051.     ScriptEnviroment* env = getEnv();
  9052.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  9053.         lua_pushnumber(L, creature->getBaseSpeed());
  9054.     else
  9055.     {
  9056.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9057.         lua_pushboolean(L, false);
  9058.     }
  9059.  
  9060.     return 1;
  9061. }
  9062.  
  9063. int32_t LuaInterface::luaGetCreatureTarget(lua_State* L)
  9064. {
  9065.     //getCreatureTarget(cid)
  9066.     ScriptEnviroment* env = getEnv();
  9067.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  9068.     {
  9069.         Creature* target = creature->getAttackedCreature();
  9070.         lua_pushnumber(L, target ? env->addThing(target) : 0);
  9071.     }
  9072.     else
  9073.     {
  9074.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9075.         lua_pushboolean(L, false);
  9076.     }
  9077.  
  9078.     return 1;
  9079. }
  9080.  
  9081. int32_t LuaInterface::luaIsSightClear(lua_State* L)
  9082. {
  9083.     //isSightClear(fromPos, toPos, floorCheck)
  9084.     PositionEx fromPos, toPos;
  9085.     bool floorCheck = popBoolean(L);
  9086.  
  9087.     popPosition(L, toPos);
  9088.     popPosition(L, fromPos);
  9089.  
  9090.     lua_pushboolean(L, g_game.isSightClear(fromPos, toPos, floorCheck));
  9091.     return 1;
  9092. }
  9093.  
  9094. int32_t LuaInterface::luaAddEvent(lua_State* L)
  9095. {
  9096.     //addEvent(callback, delay, ...)
  9097.     ScriptEnviroment* env = getEnv();
  9098.     LuaInterface* interface = env->getInterface();
  9099.     if(!interface)
  9100.     {
  9101.         errorEx("No valid script interface!");
  9102.         lua_pushboolean(L, false);
  9103.         return 1;
  9104.     }
  9105.  
  9106.     int32_t parameters = lua_gettop(L);
  9107.     if(!lua_isfunction(L, -parameters)) //-parameters means the first parameter from left to right
  9108.     {
  9109.         errorEx("Callback parameter should be a function");
  9110.         lua_pushboolean(L, false);
  9111.         return 1;
  9112.     }
  9113.  
  9114.     std::list<int32_t> params;
  9115.     for(int32_t i = 0; i < parameters - 2; ++i) //-2 because addEvent needs at least two parameters
  9116.         params.push_back(luaL_ref(L, LUA_REGISTRYINDEX));
  9117.  
  9118.     LuaTimerEvent event;
  9119.     event.eventId = Scheduler::getInstance().addEvent(createSchedulerTask(std::max((int64_t)SCHEDULER_MINTICKS, popNumber(L)),
  9120.         boost::bind(&LuaInterface::executeTimer, interface, ++interface->m_lastTimer)));
  9121.  
  9122.     event.parameters = params;
  9123.     event.function = luaL_ref(L, LUA_REGISTRYINDEX);
  9124.     event.scriptId = env->getScriptId();
  9125.     event.npc = env->getNpc();
  9126.  
  9127.     interface->m_timerEvents[interface->m_lastTimer] = event;
  9128.     lua_pushnumber(L, interface->m_lastTimer);
  9129.     return 1;
  9130. }
  9131.  
  9132. int32_t LuaInterface::luaStopEvent(lua_State* L)
  9133. {
  9134.     //stopEvent(eventid)
  9135.     uint32_t eventId = popNumber(L);
  9136.     ScriptEnviroment* env = getEnv();
  9137.  
  9138.     LuaInterface* interface = env->getInterface();
  9139.     if(!interface)
  9140.     {
  9141.         errorEx("No valid script interface!");
  9142.         lua_pushboolean(L, false);
  9143.         return 1;
  9144.     }
  9145.  
  9146.     LuaTimerEvents::iterator it = interface->m_timerEvents.find(eventId);
  9147.     if(it != interface->m_timerEvents.end())
  9148.     {
  9149.         Scheduler::getInstance().stopEvent(it->second.eventId);
  9150.         for(std::list<int32_t>::iterator lt = it->second.parameters.begin(); lt != it->second.parameters.end(); ++lt)
  9151.             luaL_unref(interface->m_luaState, LUA_REGISTRYINDEX, *lt);
  9152.  
  9153.         it->second.parameters.clear();
  9154.         luaL_unref(interface->m_luaState, LUA_REGISTRYINDEX, it->second.function);
  9155.  
  9156.         interface->m_timerEvents.erase(it);
  9157.         lua_pushboolean(L, true);
  9158.     }
  9159.     else
  9160.         lua_pushboolean(L, false);
  9161.  
  9162.     return 1;
  9163. }
  9164.  
  9165. int32_t LuaInterface::luaHasCreatureCondition(lua_State* L)
  9166. {
  9167.     //hasCreatureCondition(cid, conditionType[, subId = 0[, conditionId = (both)]])
  9168.     int32_t conditionId = CONDITIONID_COMBAT;
  9169.     uint32_t params = lua_gettop(L), subId = 0;
  9170.  
  9171.     bool both = true;
  9172.     if(params > 3)
  9173.     {
  9174.         conditionId = popNumber(L);
  9175.         both = false;
  9176.     }
  9177.  
  9178.     if(params > 2)
  9179.         subId = popNumber(L);
  9180.  
  9181.     ConditionType_t conditionType = (ConditionType_t)popNumber(L);
  9182.     ScriptEnviroment* env = getEnv();
  9183.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  9184.     {
  9185.         if(!both)
  9186.             lua_pushboolean(L, creature->getCondition(conditionType, (ConditionId_t)conditionId, subId) != NULL);
  9187.         else if(creature->getCondition(conditionType, CONDITIONID_DEFAULT, subId) != NULL)
  9188.         {
  9189.             lua_pushboolean(L, true);
  9190.             return 1;
  9191.         }
  9192.         else
  9193.             lua_pushboolean(L, creature->getCondition(conditionType, CONDITIONID_COMBAT, subId) != NULL);
  9194.     }
  9195.     else
  9196.     {
  9197.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9198.         lua_pushboolean(L, false);
  9199.     }
  9200.  
  9201.     return 1;
  9202. }
  9203.  
  9204. int32_t LuaInterface::luaGetCreatureConditionInfo(lua_State* L)
  9205. {
  9206.     //getCreatureConditionInfo(cid, conditionType[, subId = 0[, conditionId = CONDITIONID_COMBAT]])
  9207.     int32_t conditionId = CONDITIONID_COMBAT;
  9208.     uint32_t params = lua_gettop(L), subId = 0;
  9209.     if(params > 3)
  9210.         conditionId = popNumber(L);
  9211.  
  9212.     if(params > 2)
  9213.         subId = popNumber(L);
  9214.  
  9215.     ConditionType_t conditionType = (ConditionType_t)popNumber(L);
  9216.     ScriptEnviroment* env = getEnv();
  9217.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  9218.     {
  9219.         if(Condition* condition = creature->getCondition(conditionType, (ConditionId_t)conditionId, subId))
  9220.         {
  9221.             lua_newtable(L);
  9222.             setField(L, "icons", condition->getIcons());
  9223.             setField(L, "endTime", condition->getEndTime());
  9224.             setField(L, "ticks", condition->getTicks());
  9225.             setFieldBool(L, "persistent", condition->isPersistent());
  9226.             setField(L, "subId", condition->getSubId());
  9227.         }
  9228.         else
  9229.             lua_pushboolean(L, false);
  9230.     }
  9231.     else
  9232.     {
  9233.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9234.         lua_pushboolean(L, false);
  9235.     }
  9236.  
  9237.     return 1;
  9238. }
  9239.  
  9240. int32_t LuaInterface::luaGetPlayerBlessing(lua_State* L)
  9241. {
  9242.     //getPlayerBlessing(cid, blessing)
  9243.     int16_t blessing = popNumber(L) - 1;
  9244.  
  9245.     ScriptEnviroment* env = getEnv();
  9246.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9247.         lua_pushboolean(L, player->hasBlessing(blessing));
  9248.     else
  9249.     {
  9250.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9251.         lua_pushboolean(L, false);
  9252.     }
  9253.  
  9254.     return 1;
  9255. }
  9256.  
  9257. int32_t LuaInterface::luaDoPlayerAddBlessing(lua_State* L)
  9258. {
  9259.     //doPlayerAddBlessing(cid, blessing)
  9260.     int16_t blessing = popNumber(L) - 1;
  9261.     ScriptEnviroment* env = getEnv();
  9262.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9263.     {
  9264.         if(!player->hasBlessing(blessing))
  9265.         {
  9266.             player->addBlessing(1 << blessing);
  9267.             lua_pushboolean(L, true);
  9268.         }
  9269.         else
  9270.             lua_pushboolean(L, false);
  9271.     }
  9272.     else
  9273.     {
  9274.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9275.         lua_pushboolean(L, false);
  9276.     }
  9277.  
  9278.     return 1;
  9279. }
  9280.  
  9281. int32_t LuaInterface::luaGetPlayerPVPBlessing(lua_State* L)
  9282. {
  9283.     //getPlayerPVPBlessing(cid)
  9284.     ScriptEnviroment* env = getEnv();
  9285.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9286.         lua_pushboolean(L, player->hasPVPBlessing());
  9287.     else
  9288.     {
  9289.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9290.         lua_pushboolean(L, false);
  9291.     }
  9292.  
  9293.     return 1;
  9294. }
  9295.  
  9296. int32_t LuaInterface::luaDoPlayerSetPVPBlessing(lua_State* L)
  9297. {
  9298.     //doPlayerSetPVPBlessing(cid[, value])
  9299.     bool value = true;
  9300.     if(lua_gettop(L) > 1)
  9301.         value = popBoolean(L);
  9302.  
  9303.     ScriptEnviroment* env = getEnv();
  9304.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9305.     {
  9306.         player->setPVPBlessing(value);
  9307.         lua_pushboolean(L, true);
  9308.     }
  9309.     else
  9310.     {
  9311.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9312.         lua_pushboolean(L, false);
  9313.     }
  9314.  
  9315.     return 1;
  9316. }
  9317.  
  9318. int32_t LuaInterface::luaDoPlayerSetPromotionLevel(lua_State* L)
  9319. {
  9320.     //doPlayerSetPromotionLevel(cid, level)
  9321.     uint32_t level = popNumber(L);
  9322.     ScriptEnviroment* env = getEnv();
  9323.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9324.     {
  9325.         player->setPromotionLevel(level);
  9326.         lua_pushboolean(L, true);
  9327.     }
  9328.     else
  9329.     {
  9330.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9331.         lua_pushboolean(L, false);
  9332.     }
  9333.  
  9334.     return 1;
  9335. }
  9336.  
  9337. int32_t LuaInterface::luaDoPlayerSetGroupId(lua_State* L)
  9338. {
  9339.     //doPlayerSetGroupId(cid, groupId)
  9340.     uint32_t groupId = popNumber(L);
  9341.     ScriptEnviroment* env = getEnv();
  9342.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9343.     {
  9344.         if(Group* group = Groups::getInstance()->getGroup(groupId))
  9345.         {
  9346.             player->setGroup(group);
  9347.             lua_pushboolean(L, true);
  9348.         }
  9349.         else
  9350.             lua_pushboolean(L, false);
  9351.     }
  9352.     else
  9353.     {
  9354.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9355.         lua_pushboolean(L, false);
  9356.     }
  9357.  
  9358.     return 1;
  9359. }
  9360.  
  9361. int32_t LuaInterface::luaGetCreatureMana(lua_State* L)
  9362. {
  9363.     //getCreatureMana(cid)
  9364.     ScriptEnviroment* env = getEnv();
  9365.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  9366.         lua_pushnumber(L, creature->getMana());
  9367.     else
  9368.     {
  9369.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9370.         lua_pushboolean(L, false);
  9371.     }
  9372.  
  9373.     return 1;
  9374. }
  9375.  
  9376. int32_t LuaInterface::luaGetCreatureMaxMana(lua_State* L)
  9377. {
  9378.     //getCreatureMaxMana(cid[, ignoreModifiers = false])
  9379.     bool ignoreModifiers = false;
  9380.     if(lua_gettop(L) > 1)
  9381.         ignoreModifiers = popBoolean(L);
  9382.  
  9383.     ScriptEnviroment* env = getEnv();
  9384.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  9385.         lua_pushnumber(L, creature->getPlayer() && ignoreModifiers ? creature->manaMax : creature->getMaxMana());
  9386.     else
  9387.     {
  9388.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9389.         lua_pushboolean(L, false);
  9390.     }
  9391.  
  9392.     return 1;
  9393. }
  9394.  
  9395. int32_t LuaInterface::luaGetCreatureHealth(lua_State* L)
  9396. {
  9397.     //getCreatureHealth(cid)
  9398.     ScriptEnviroment* env = getEnv();
  9399.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  9400.         lua_pushnumber(L, creature->getHealth());
  9401.     else
  9402.     {
  9403.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9404.         lua_pushboolean(L, false);
  9405.     }
  9406.  
  9407.     return 1;
  9408. }
  9409.  
  9410. int32_t LuaInterface::luaGetCreatureLookDirection(lua_State* L)
  9411. {
  9412.     //getCreatureLookDirection(cid)
  9413.     ScriptEnviroment* env = getEnv();
  9414.  
  9415.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  9416.         lua_pushnumber(L, creature->getDirection());
  9417.     else
  9418.     {
  9419.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9420.         lua_pushboolean(L, false);
  9421.     }
  9422.  
  9423.     return 1;
  9424. }
  9425.  
  9426. int32_t LuaInterface::luaGetCreatureMaxHealth(lua_State* L)
  9427. {
  9428.     //getCreatureMaxHealth(cid[, ignoreModifiers = false])
  9429.     bool ignoreModifiers = false;
  9430.     if(lua_gettop(L) > 1)
  9431.         ignoreModifiers = popBoolean(L);
  9432.  
  9433.     ScriptEnviroment* env = getEnv();
  9434.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  9435.         lua_pushnumber(L, creature->getPlayer() && ignoreModifiers ? creature->healthMax : creature->getMaxHealth());
  9436.     else
  9437.     {
  9438.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9439.         lua_pushboolean(L, false);
  9440.     }
  9441.  
  9442.     return 1;
  9443. }
  9444.  
  9445. int32_t LuaInterface::luaDoPlayerSetStamina(lua_State* L)
  9446. {
  9447.     //doPlayerSetStamina(cid, minutes)
  9448.     uint32_t minutes = popNumber(L);
  9449.  
  9450.     ScriptEnviroment* env = getEnv();
  9451.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9452.     {
  9453.         player->setStaminaMinutes(minutes);
  9454.         player->sendStats();
  9455.         lua_pushboolean(L, true);
  9456.     }
  9457.     else
  9458.     {
  9459.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9460.         lua_pushboolean(L, false);
  9461.     }
  9462.  
  9463.     return 1;
  9464. }
  9465.  
  9466. int32_t LuaInterface::luaDoPlayerSetBalance(lua_State* L)
  9467. {
  9468.     //doPlayerSetBalance(cid, balance)
  9469.     uint64_t balance = popNumber(L);
  9470.  
  9471.     ScriptEnviroment* env = getEnv();
  9472.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9473.     {
  9474.         player->balance = balance;
  9475.         lua_pushboolean(L, true);
  9476.     }
  9477.     else
  9478.     {
  9479.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9480.         lua_pushboolean(L, false);
  9481.     }
  9482.  
  9483.     return 1;
  9484. }
  9485.  
  9486. int32_t LuaInterface::luaDoPlayerSetPartner(lua_State* L)
  9487. {
  9488.     //doPlayerSetPartner(cid, guid)
  9489.     uint32_t guid = popNumber(L);
  9490.  
  9491.     ScriptEnviroment* env = getEnv();
  9492.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9493.     {
  9494.         player->marriage = guid;
  9495.         lua_pushboolean(L, true);
  9496.     }
  9497.     else
  9498.     {
  9499.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9500.         lua_pushboolean(L, false);
  9501.     }
  9502.  
  9503.     return 1;
  9504. }
  9505.  
  9506. int32_t LuaInterface::luaDoPlayerFollowCreature(lua_State* L)
  9507. {
  9508.     //doPlayerFollowCreature(cid, target)
  9509.     ScriptEnviroment* env = getEnv();
  9510.  
  9511.     Creature* creature = env->getCreatureByUID(popNumber(L));
  9512.     if(!creature)
  9513.     {
  9514.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9515.         lua_pushboolean(L, false);
  9516.         return 1;
  9517.     }
  9518.  
  9519.     Player* player = env->getPlayerByUID(popNumber(L));
  9520.     if(!player)
  9521.     {
  9522.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9523.         lua_pushboolean(L, false);
  9524.         return 1;
  9525.     }
  9526.  
  9527.     lua_pushboolean(L, g_game.playerFollowCreature(player->getID(), creature->getID()));
  9528.     return 1;
  9529. }
  9530.  
  9531. int32_t LuaInterface::luaGetPlayerParty(lua_State* L)
  9532. {
  9533.     //getPlayerParty(cid)
  9534.     uint32_t cid = popNumber(L);
  9535.  
  9536.     ScriptEnviroment* env = getEnv();
  9537.     if(Player* player = env->getPlayerByUID(cid))
  9538.     {
  9539.         if(Party* party = player->getParty())
  9540.             lua_pushnumber(L, env->addThing(party->getLeader()));
  9541.         else
  9542.             lua_pushnil(L);
  9543.     }
  9544.     else
  9545.     {
  9546.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9547.         lua_pushboolean(L, false);
  9548.     }
  9549.  
  9550.     return 1;
  9551. }
  9552.  
  9553. int32_t LuaInterface::luaDoPlayerJoinParty(lua_State* L)
  9554. {
  9555.     //doPlayerJoinParty(cid, lid)
  9556.     ScriptEnviroment* env = getEnv();
  9557.  
  9558.     Player* leader = env->getPlayerByUID(popNumber(L));
  9559.     if(!leader)
  9560.     {
  9561.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9562.         lua_pushboolean(L, false);
  9563.     }
  9564.  
  9565.     Player* player = env->getPlayerByUID(popNumber(L));
  9566.     if(!player)
  9567.     {
  9568.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9569.         lua_pushboolean(L, false);
  9570.     }
  9571.  
  9572.     g_game.playerJoinParty(player->getID(), leader->getID());
  9573.     lua_pushboolean(L, true);
  9574.     return 1;
  9575. }
  9576.  
  9577. int32_t LuaInterface::luaDoPlayerLeaveParty(lua_State* L)
  9578. {
  9579.     //doPlayerLeaveParty(cid[, forced = false])
  9580.     bool forced = false;
  9581.     if(lua_gettop(L) > 1)
  9582.         forced = popBoolean(L);
  9583.  
  9584.     ScriptEnviroment* env = getEnv();
  9585.     Player* player = env->getPlayerByUID(popNumber(L));
  9586.     if(!player)
  9587.     {
  9588.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9589.         lua_pushboolean(L, false);
  9590.     }
  9591.  
  9592.     g_game.playerLeaveParty(player->getID(), forced);
  9593.     lua_pushboolean(L, true);
  9594.     return 1;
  9595. }
  9596.  
  9597. int32_t LuaInterface::luaIsPlayerUsingOtclient(lua_State* L)
  9598. {
  9599.     //isPlayerUsingOtclient(cid)
  9600.     ScriptEnviroment* env = getEnv();
  9601.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9602.     {
  9603.         lua_pushboolean(L, player->isUsingOtclient());
  9604.     }
  9605.  
  9606.     lua_pushboolean(L, false);
  9607.     return 1;
  9608. }
  9609.  
  9610. int32_t LuaInterface::luaDoSendPlayerExtendedOpcode(lua_State* L)
  9611. {
  9612.     //doSendPlayerExtendedOpcode(cid, opcode, buffer)
  9613.     std::string buffer = popString(L);
  9614.     int32_t opcode = popNumber(L);
  9615.  
  9616.     ScriptEnviroment* env = getEnv();
  9617.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9618.     {
  9619.         player->sendExtendedOpcode(opcode, buffer);
  9620.         lua_pushboolean(L, true);
  9621.     }
  9622.  
  9623.     lua_pushboolean(L, false);
  9624.     return 1;
  9625. }
  9626.  
  9627. int32_t LuaInterface::luaGetPartyMembers(lua_State* L)
  9628. {
  9629.     //getPartyMembers(cid)
  9630.     ScriptEnviroment* env = getEnv();
  9631.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9632.     {
  9633.         if(Party* party = player->getParty())
  9634.         {
  9635.             PlayerVector list = party->getMembers();
  9636.             list.push_back(party->getLeader());
  9637.  
  9638.             PlayerVector::const_iterator it = list.begin();
  9639.             lua_newtable(L);
  9640.             for(uint32_t i = 1; it != list.end(); ++it, ++i)
  9641.             {
  9642.                 lua_pushnumber(L, i);
  9643.                 lua_pushnumber(L, (*it)->getID());
  9644.                 pushTable(L);
  9645.             }
  9646.  
  9647.             return 1;
  9648.         }
  9649.     }
  9650.  
  9651.     lua_pushboolean(L, false);
  9652.     return 1;
  9653. }
  9654.  
  9655. int32_t LuaInterface::luaGetVocationInfo(lua_State* L)
  9656. {
  9657.     //getVocationInfo(id)
  9658.     uint32_t id = popNumber(L);
  9659.     Vocation* voc = Vocations::getInstance()->getVocation(id);
  9660.     if(!voc)
  9661.     {
  9662.         lua_pushboolean(L, false);
  9663.         return 1;
  9664.     }
  9665.  
  9666.     lua_newtable(L);
  9667.     setField(L, "id", voc->getId());
  9668.     setField(L, "name", voc->getName().c_str());
  9669.     setField(L, "description", voc->getDescription().c_str());
  9670.     setField(L, "healthGain", voc->getGain(GAIN_HEALTH));
  9671.     setField(L, "healthGainTicks", voc->getGainTicks(GAIN_HEALTH));
  9672.     setField(L, "healthGainAmount", voc->getGainAmount(GAIN_HEALTH));
  9673.     setField(L, "manaGain", voc->getGain(GAIN_MANA));
  9674.     setField(L, "manaGainTicks", voc->getGainTicks(GAIN_MANA));
  9675.     setField(L, "manaGainAmount", voc->getGainAmount(GAIN_MANA));
  9676.     setField(L, "attackSpeed", voc->getAttackSpeed());
  9677.     setField(L, "baseSpeed", voc->getBaseSpeed());
  9678.     setField(L, "fromVocation", voc->getFromVocation());
  9679.     setField(L, "promotedVocation", Vocations::getInstance()->getPromotedVocation(id));
  9680.     setField(L, "soul", voc->getGain(GAIN_SOUL));
  9681.     setField(L, "soulAmount", voc->getGainAmount(GAIN_SOUL));
  9682.     setField(L, "soulTicks", voc->getGainTicks(GAIN_SOUL));
  9683.     setField(L, "capacity", voc->getGainCap());
  9684.     setFieldBool(L, "attackable", voc->isAttackable());
  9685.     setFieldBool(L, "needPremium", voc->isPremiumNeeded());
  9686.     setFieldFloat(L, "experienceMultiplier", voc->getExperienceMultiplier());
  9687.     return 1;
  9688. }
  9689.  
  9690. int32_t LuaInterface::luaGetGroupInfo(lua_State* L)
  9691. {
  9692.     //getGroupInfo(id[, premium = false])
  9693.     bool premium = false;
  9694.     if(lua_gettop(L) > 1)
  9695.         premium = popBoolean(L);
  9696.  
  9697.     Group* group = Groups::getInstance()->getGroup(popNumber(L));
  9698.     if(!group)
  9699.     {
  9700.         lua_pushboolean(L, false);
  9701.         return 1;
  9702.     }
  9703.  
  9704.     lua_newtable(L);
  9705.     setField(L, "id", group->getId());
  9706.     setField(L, "name", group->getName().c_str());
  9707.     setField(L, "access", group->getAccess());
  9708.     setField(L, "ghostAccess", group->getGhostAccess());
  9709.     setField(L, "flags", group->getFlags());
  9710.     setField(L, "customFlags", group->getCustomFlags());
  9711.     setField(L, "depotLimit", group->getDepotLimit(premium));
  9712.     setField(L, "maxVips", group->getMaxVips(premium));
  9713.     setField(L, "outfit", group->getOutfit());
  9714.     return 1;
  9715. }
  9716.  
  9717. int32_t LuaInterface::luaGetChannelUsers(lua_State* L)
  9718. {
  9719.     //getChannelUsers(channelId)
  9720.     ScriptEnviroment* env = getEnv();
  9721.     uint16_t channelId = popNumber(L);
  9722.  
  9723.     if(ChatChannel* channel = g_chat.getChannelById(channelId))
  9724.     {
  9725.         UsersMap usersMap = channel->getUsers();
  9726.         UsersMap::iterator it = usersMap.begin();
  9727.  
  9728.         lua_newtable(L);
  9729.         for(int32_t i = 1; it != usersMap.end(); ++it, ++i)
  9730.         {
  9731.             lua_pushnumber(L, i);
  9732.             lua_pushnumber(L, env->addThing(it->second));
  9733.             pushTable(L);
  9734.         }
  9735.     }
  9736.     else
  9737.         lua_pushboolean(L, false);
  9738.  
  9739.     return 1;
  9740. }
  9741.  
  9742. int32_t LuaInterface::luaGetPlayersOnline(lua_State* L)
  9743. {
  9744.     //getPlayersOnline()
  9745.     ScriptEnviroment* env = getEnv();
  9746.     AutoList<Player>::iterator it = Player::autoList.begin();
  9747.  
  9748.     lua_newtable(L);
  9749.     for(int32_t i = 1; it != Player::autoList.end(); ++it, ++i)
  9750.     {
  9751.         lua_pushnumber(L, i);
  9752.         lua_pushnumber(L, env->addThing(it->second));
  9753.         pushTable(L);
  9754.     }
  9755.  
  9756.     return 1;
  9757. }
  9758.  
  9759. int32_t LuaInterface::luaSetCreatureMaxHealth(lua_State* L)
  9760. {
  9761.     //setCreatureMaxHealth(uid, health)
  9762.     uint32_t maxHealth = (uint32_t)popNumber(L);
  9763.  
  9764.     ScriptEnviroment* env = getEnv();
  9765.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  9766.     {
  9767.         creature->changeMaxHealth(maxHealth);
  9768.         lua_pushboolean(L, true);
  9769.     }
  9770.     else
  9771.     {
  9772.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9773.         lua_pushboolean(L, false);
  9774.     }
  9775.  
  9776.     return 1;
  9777. }
  9778.  
  9779. int32_t LuaInterface::luaSetCreatureMaxMana(lua_State* L)
  9780. {
  9781.     //setCreatureMaxMana(uid, mana)
  9782.     uint32_t maxMana = (uint32_t)popNumber(L);
  9783.  
  9784.     ScriptEnviroment* env = getEnv();
  9785.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  9786.     {
  9787.         creature->changeMaxMana(maxMana);
  9788.         lua_pushboolean(L, true);
  9789.     }
  9790.     else
  9791.     {
  9792.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9793.         lua_pushboolean(L, false);
  9794.     }
  9795.  
  9796.     return 1;
  9797. }
  9798.  
  9799. int32_t LuaInterface::luaDoPlayerSetMaxCapacity(lua_State* L)
  9800. {
  9801.     //doPlayerSetMaxCapacity(uid, cap)
  9802.     double cap = popFloatNumber(L);
  9803.  
  9804.     ScriptEnviroment* env = getEnv();
  9805.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9806.     {
  9807.         player->setCapacity(cap);
  9808.         lua_pushboolean(L, true);
  9809.     }
  9810.     else
  9811.     {
  9812.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9813.         lua_pushboolean(L, false);
  9814.     }
  9815.  
  9816.     return 1;
  9817. }
  9818.  
  9819. int32_t LuaInterface::luaGetCreatureMaster(lua_State* L)
  9820. {
  9821.     //getCreatureMaster(cid)
  9822.     uint32_t cid = popNumber(L);
  9823.     ScriptEnviroment* env = getEnv();
  9824.  
  9825.     Creature* creature = env->getCreatureByUID(cid);
  9826.     if(!creature)
  9827.     {
  9828.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9829.         lua_pushboolean(L, false);
  9830.         return 1;
  9831.     }
  9832.  
  9833.     if(Creature* master = creature->getMaster())
  9834.         lua_pushnumber(L, env->addThing(master));
  9835.     else
  9836.         lua_pushnil(L);
  9837.  
  9838.     return 1;
  9839. }
  9840.  
  9841. int32_t LuaInterface::luaGetCreatureSummons(lua_State* L)
  9842. {
  9843.     //getCreatureSummons(cid)
  9844.     ScriptEnviroment* env = getEnv();
  9845.  
  9846.     Creature* creature = env->getCreatureByUID(popNumber(L));
  9847.     if(!creature)
  9848.     {
  9849.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9850.         lua_pushboolean(L, false);
  9851.         return 1;
  9852.     }
  9853.  
  9854.     const std::list<Creature*>& summons = creature->getSummons();
  9855.     CreatureList::const_iterator it = summons.begin();
  9856.  
  9857.     lua_newtable(L);
  9858.     for(uint32_t i = 1; it != summons.end(); ++it, ++i)
  9859.     {
  9860.         lua_pushnumber(L, i);
  9861.         lua_pushnumber(L, env->addThing(*it));
  9862.         pushTable(L);
  9863.     }
  9864.  
  9865.     return 1;
  9866. }
  9867.  
  9868. int32_t LuaInterface::luaDoPlayerSetIdleTime(lua_State* L)
  9869. {
  9870.     //doPlayerSetIdleTime(cid, amount)
  9871.     int64_t amount = popNumber(L);
  9872.     ScriptEnviroment* env = getEnv();
  9873.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9874.     {
  9875.         player->setIdleTime(amount);
  9876.         lua_pushboolean(L, true);
  9877.     }
  9878.     else
  9879.     {
  9880.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9881.         lua_pushboolean(L, false);
  9882.     }
  9883.  
  9884.     return 1;
  9885. }
  9886.  
  9887. int32_t LuaInterface::luaDoCreatureSetNoMove(lua_State* L)
  9888. {
  9889.     //doCreatureSetNoMove(cid, block)
  9890.     bool block = popBoolean(L);
  9891.  
  9892.     ScriptEnviroment* env = getEnv();
  9893.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  9894.     {
  9895.         creature->setNoMove(block);
  9896.         creature->onWalkAborted();
  9897.         lua_pushboolean(L, true);
  9898.     }
  9899.     else
  9900.     {
  9901.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9902.         lua_pushboolean(L, false);
  9903.     }
  9904.  
  9905.     return 1;
  9906. }
  9907.  
  9908. int32_t LuaInterface::luaGetPlayerModes(lua_State* L)
  9909. {
  9910.     //getPlayerModes(cid)
  9911.     ScriptEnviroment* env = getEnv();
  9912.  
  9913.     Player* player = env->getPlayerByUID(popNumber(L));
  9914.     if(!player)
  9915.     {
  9916.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9917.         lua_pushboolean(L, false);
  9918.         return 1;
  9919.     }
  9920.  
  9921.     lua_newtable(L);
  9922.     setField(L, "chase", player->getChaseMode());
  9923.     setField(L, "fight", player->getFightMode());
  9924.     setField(L, "secure", player->getSecureMode());
  9925.     return 1;
  9926. }
  9927.  
  9928. int32_t LuaInterface::luaGetPlayerRates(lua_State* L)
  9929. {
  9930.     //getPlayerRates(cid)
  9931.     ScriptEnviroment* env = getEnv();
  9932.  
  9933.     Player* player = env->getPlayerByUID(popNumber(L));
  9934.     if(!player)
  9935.     {
  9936.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9937.         lua_pushboolean(L, false);
  9938.         return 1;
  9939.     }
  9940.  
  9941.     lua_newtable(L);
  9942.     for(uint32_t i = SKILL_FIRST; i <= SKILL__LAST; ++i)
  9943.     {
  9944.         lua_pushnumber(L, i);
  9945.         lua_pushnumber(L, player->rates[(skills_t)i]);
  9946.         pushTable(L);
  9947.     }
  9948.  
  9949.     return 1;
  9950. }
  9951.  
  9952. int32_t LuaInterface::luaDoPlayerSetRate(lua_State* L)
  9953. {
  9954.     //doPlayerSetRate(cid, type, value)
  9955.     double value = popFloatNumber(L);
  9956.     uint32_t type = popNumber(L);
  9957.  
  9958.     ScriptEnviroment* env = getEnv();
  9959.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9960.     {
  9961.         if(type <= SKILL__LAST)
  9962.         {
  9963.             player->rates[(skills_t)type] = value;
  9964.             lua_pushboolean(L, true);
  9965.         }
  9966.         else
  9967.             lua_pushboolean(L, false);
  9968.     }
  9969.     else
  9970.     {
  9971.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9972.         lua_pushboolean(L, false);
  9973.     }
  9974.  
  9975.     return 1;
  9976. }
  9977.  
  9978. int32_t LuaInterface::luaDoPlayerSwitchSaving(lua_State* L)
  9979. {
  9980.     //doPlayerSwitchSaving(cid)
  9981.     ScriptEnviroment* env = getEnv();
  9982.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9983.     {
  9984.         player->switchSaving();
  9985.         lua_pushboolean(L, true);
  9986.     }
  9987.     else
  9988.     {
  9989.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9990.         lua_pushboolean(L, false);
  9991.     }
  9992.  
  9993.     return 1;
  9994. }
  9995.  
  9996. int32_t LuaInterface::luaDoPlayerSave(lua_State* L)
  9997. {
  9998.     //doPlayerSave(cid[, shallow = false])
  9999.     bool shallow = false;
  10000.     if(lua_gettop(L) > 1)
  10001.         shallow = popBoolean(L);
  10002.  
  10003.     ScriptEnviroment* env = getEnv();
  10004.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  10005.     {
  10006.         player->loginPosition = player->getPosition();
  10007.         lua_pushboolean(L, IOLoginData::getInstance()->savePlayer(player, false, shallow));
  10008.     }
  10009.     else
  10010.     {
  10011.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  10012.         lua_pushboolean(L, false);
  10013.     }
  10014.  
  10015.     return 1;
  10016. }
  10017.  
  10018. int32_t LuaInterface::luaDoPlayerSaveItems(lua_State* L)
  10019. {
  10020.     //doPlayerSaveItems(cid)
  10021.     ScriptEnviroment* env = getEnv();
  10022.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  10023.     {
  10024.         player->loginPosition = player->getPosition();
  10025.         IOLoginData *p = IOLoginData::getInstance();
  10026.         boost::thread th(&IOLoginData::savePlayerItems, p, player);
  10027.         th.detach();
  10028.         lua_pushboolean(L, true);
  10029.     }
  10030.     else
  10031.     {
  10032.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  10033.         lua_pushboolean(L, false);
  10034.     }
  10035.  
  10036.     return 1;
  10037. }
  10038.  
  10039. int32_t LuaInterface::luaGetTownId(lua_State* L)
  10040. {
  10041.     //getTownId(townName)
  10042.     std::string townName = popString(L);
  10043.     if(Town* town = Towns::getInstance()->getTown(townName))
  10044.         lua_pushnumber(L, town->getID());
  10045.     else
  10046.         lua_pushboolean(L, false);
  10047.  
  10048.     return 1;
  10049. }
  10050.  
  10051. int32_t LuaInterface::luaGetTownName(lua_State* L)
  10052. {
  10053.     //getTownName(townId)
  10054.     uint32_t townId = popNumber(L);
  10055.     if(Town* town = Towns::getInstance()->getTown(townId))
  10056.         lua_pushstring(L, town->getName().c_str());
  10057.     else
  10058.         lua_pushboolean(L, false);
  10059.  
  10060.     return 1;
  10061. }
  10062.  
  10063. int32_t LuaInterface::luaGetTownTemplePosition(lua_State* L)
  10064. {
  10065.     //getTownTemplePosition(townId)
  10066.     uint32_t townId = popNumber(L);
  10067.     if(Town* town = Towns::getInstance()->getTown(townId))
  10068.         pushPosition(L, town->getPosition(), 255);
  10069.     else
  10070.         lua_pushboolean(L, false);
  10071.  
  10072.     return 1;
  10073. }
  10074.  
  10075. int32_t LuaInterface::luaGetTownHouses(lua_State* L)
  10076. {
  10077.     //getTownHouses([townId])
  10078.     uint32_t townId = 0;
  10079.     if(lua_gettop(L) > 0)
  10080.         townId = popNumber(L);
  10081.  
  10082.     HouseMap::iterator it = Houses::getInstance()->getHouseBegin();
  10083.     lua_newtable(L);
  10084.     for(uint32_t i = 1; it != Houses::getInstance()->getHouseEnd(); ++i, ++it)
  10085.     {
  10086.         if(townId && it->second->getTownId() != townId)
  10087.             continue;
  10088.  
  10089.         lua_pushnumber(L, i);
  10090.         lua_pushnumber(L, it->second->getId());
  10091.         pushTable(L);
  10092.     }
  10093.  
  10094.     return 1;
  10095. }
  10096.  
  10097. int32_t LuaInterface::luaGetSpectators(lua_State* L)
  10098. {
  10099.     //getSpectators(centerPos, rangex, rangey[, multifloor = false])
  10100.     bool multifloor = false;
  10101.     if(lua_gettop(L) > 3)
  10102.         multifloor = popBoolean(L);
  10103.  
  10104.     uint32_t rangey = popNumber(L), rangex = popNumber(L);
  10105.     PositionEx centerPos;
  10106.     popPosition(L, centerPos);
  10107.  
  10108.     SpectatorVec list;
  10109.     g_game.getSpectators(list, centerPos, multifloor, false, rangex, rangex, rangey, rangey);
  10110.     if(list.empty())
  10111.     {
  10112.         lua_pushnil(L);
  10113.         return 1;
  10114.     }
  10115.  
  10116.     ScriptEnviroment* env = getEnv();
  10117.     SpectatorVec::const_iterator it = list.begin();
  10118.  
  10119.     lua_newtable(L);
  10120.     for(uint32_t i = 1; it != list.end(); ++it, ++i)
  10121.     {
  10122.         lua_pushnumber(L, i);
  10123.         lua_pushnumber(L, env->addThing(*it));
  10124.         pushTable(L);
  10125.     }
  10126.  
  10127.     return 1;
  10128. }
  10129.  
  10130. int32_t LuaInterface::luaGetHighscoreString(lua_State* L)
  10131. {
  10132.     //getHighscoreString(skillId)
  10133.     uint16_t skillId = popNumber(L);
  10134.     if(skillId <= SKILL__LAST)
  10135.         lua_pushstring(L, g_game.getHighscoreString(skillId).c_str());
  10136.     else
  10137.         lua_pushboolean(L, false);
  10138.  
  10139.     return 1;
  10140. }
  10141.  
  10142. int32_t LuaInterface::luaGetVocationList(lua_State* L)
  10143. {
  10144.     //getVocationList()
  10145.     VocationsMap::iterator it = Vocations::getInstance()->getFirstVocation();
  10146.     lua_newtable(L);
  10147.     for(uint32_t i = 1; it != Vocations::getInstance()->getLastVocation(); ++i, ++it)
  10148.     {
  10149.         createTable(L, i);
  10150.         setField(L, "id", it->first);
  10151.         setField(L, "name", it->second->getName());
  10152.         pushTable(L);
  10153.     }
  10154.  
  10155.     return 1;
  10156. }
  10157.  
  10158. int32_t LuaInterface::luaGetGroupList(lua_State* L)
  10159. {
  10160.     //getGroupList()
  10161.     GroupsMap::iterator it = Groups::getInstance()->getFirstGroup();
  10162.     lua_newtable(L);
  10163.     for(uint32_t i = 1; it != Groups::getInstance()->getLastGroup(); ++i, ++it)
  10164.     {
  10165.         createTable(L, i);
  10166.         setField(L, "id", it->first);
  10167.         setField(L, "name", it->second->getName());
  10168.         pushTable(L);
  10169.     }
  10170.  
  10171.     return 1;
  10172. }
  10173.  
  10174. int32_t LuaInterface::luaGetChannelList(lua_State* L)
  10175. {
  10176.     //getChannelList()
  10177.     lua_newtable(L);
  10178.     ChannelList list = g_chat.getPublicChannels();
  10179.  
  10180.     ChannelList::const_iterator it = list.begin();
  10181.     for(uint32_t i = 1; it != list.end(); ++it, ++i)
  10182.     {
  10183.         createTable(L, i);
  10184.         setField(L, "id", (*it)->getId());
  10185.         setField(L, "name", (*it)->getName());
  10186.  
  10187.         setField(L, "flags", (*it)->getFlags());
  10188.         setField(L, "level", (*it)->getLevel());
  10189.         setField(L, "access", (*it)->getAccess());
  10190.         pushTable(L);
  10191.     }
  10192.  
  10193.     return 1;
  10194. }
  10195.  
  10196. int32_t LuaInterface::luaGetTownList(lua_State* L)
  10197. {
  10198.     //getTownList()
  10199.     TownMap::const_iterator it = Towns::getInstance()->getFirstTown();
  10200.     lua_newtable(L);
  10201.     for(uint32_t i = 1; it != Towns::getInstance()->getLastTown(); ++it, ++i)
  10202.     {
  10203.         createTable(L, i);
  10204.         setField(L, "id", it->first);
  10205.         setField(L, "name", it->second->getName());
  10206.         pushTable(L);
  10207.     }
  10208.  
  10209.     return 1;
  10210. }
  10211.  
  10212. int32_t LuaInterface::luaGetWaypointList(lua_State* L)
  10213. {
  10214.     //getWaypointList()
  10215.     WaypointMap waypointsMap = g_game.getMap()->waypoints.getWaypointsMap();
  10216.     WaypointMap::iterator it = waypointsMap.begin();
  10217.  
  10218.     lua_newtable(L);
  10219.     for(uint32_t i = 1; it != waypointsMap.end(); ++it, ++i)
  10220.     {
  10221.         createTable(L, i);
  10222.         setField(L, "name", it->first);
  10223.         setField(L, "pos", it->second->pos.x);
  10224.         pushTable(L);
  10225.     }
  10226.  
  10227.     return 1;
  10228. }
  10229.  
  10230. int32_t LuaInterface::luaGetWaypointPosition(lua_State* L)
  10231. {
  10232.     //getWaypointPosition(name)
  10233.     if(WaypointPtr waypoint = g_game.getMap()->waypoints.getWaypointByName(popString(L)))
  10234.         pushPosition(L, waypoint->pos, 0);
  10235.     else
  10236.         lua_pushboolean(L, false);
  10237.  
  10238.     return 1;
  10239. }
  10240.  
  10241. int32_t LuaInterface::luaDoWaypointAddTemporial(lua_State* L)
  10242. {
  10243.     //doWaypointAddTemporial(name, pos)
  10244.     PositionEx pos;
  10245.     popPosition(L, pos);
  10246.  
  10247.     g_game.getMap()->waypoints.addWaypoint(WaypointPtr(new Waypoint(popString(L), pos)));
  10248.     lua_pushboolean(L, true);
  10249.     return 1;
  10250. }
  10251.  
  10252. int32_t LuaInterface::luaGetGameState(lua_State* L)
  10253. {
  10254.     //getGameState()
  10255.     lua_pushnumber(L, g_game.getGameState());
  10256.     return 1;
  10257. }
  10258.  
  10259. int32_t LuaInterface::luaDoSetGameState(lua_State* L)
  10260. {
  10261.     //doSetGameState(id)
  10262.     uint32_t id = popNumber(L);
  10263.     if(id >= GAMESTATE_FIRST && id <= GAMESTATE_LAST)
  10264.     {
  10265.         Dispatcher::getInstance().addTask(createTask(
  10266.             boost::bind(&Game::setGameState, &g_game, (GameState_t)id)));
  10267.         lua_pushboolean(L, true);
  10268.     }
  10269.     else
  10270.         lua_pushboolean(L, false);
  10271.  
  10272.     return 1;
  10273. }
  10274.  
  10275. int32_t LuaInterface::luaDoCreatureExecuteTalkAction(lua_State* L)
  10276. {
  10277.     //doCreatureExecuteTalkAction(cid, text[, ignoreAccess = false[, channelId = CHANNEL_DEFAULT]])
  10278.     uint32_t params = lua_gettop(L), channelId = CHANNEL_DEFAULT;
  10279.     if(params > 3)
  10280.         channelId = popNumber(L);
  10281.  
  10282.     bool ignoreAccess = false;
  10283.     if(params > 2)
  10284.         ignoreAccess = popBoolean(L);
  10285.  
  10286.     std::string text = popString(L);
  10287.     ScriptEnviroment* env = getEnv();
  10288.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  10289.         lua_pushboolean(L, g_talkActions->onPlayerSay(creature, channelId, text, ignoreAccess));
  10290.     else
  10291.     {
  10292.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  10293.         lua_pushboolean(L, false);
  10294.     }
  10295.  
  10296.     return 1;
  10297. }
  10298.  
  10299. int32_t LuaInterface::luaDoExecuteRaid(lua_State* L)
  10300. {
  10301.     //doExecuteRaid(name)
  10302.     std::string raidName = popString(L);
  10303.     if(Raids::getInstance()->getRunning())
  10304.     {
  10305.         lua_pushboolean(L, false);
  10306.         return 1;
  10307.     }
  10308.  
  10309.     Raid* raid = Raids::getInstance()->getRaidByName(raidName);
  10310.     if(!raid || !raid->isLoaded())
  10311.     {
  10312.         errorEx("Raid with name " + raidName + " does not exists");
  10313.         lua_pushboolean(L, false);
  10314.         return 1;
  10315.     }
  10316.  
  10317.     lua_pushboolean(L, raid->startRaid());
  10318.     return 1;
  10319. }
  10320.  
  10321. int32_t LuaInterface::luaDoReloadInfo(lua_State* L)
  10322. {
  10323.     //doReloadInfo(id[, cid])
  10324.     uint32_t cid = 0;
  10325.     if(lua_gettop(L) > 1)
  10326.         cid = popNumber(L);
  10327.  
  10328.     uint32_t id = popNumber(L);
  10329.     if(id >= RELOAD_FIRST && id <= RELOAD_LAST)
  10330.     {
  10331.         // we're passing it to scheduler since talkactions reload will
  10332.         // re-init our lua state and crash due to unfinished call
  10333.         Scheduler::getInstance().addEvent(createSchedulerTask(SCHEDULER_MINTICKS,
  10334.             boost::bind(&Game::reloadInfo, &g_game, (ReloadInfo_t)id, cid, false)));
  10335.         lua_pushboolean(L, true);
  10336.     }
  10337.     else
  10338.         lua_pushboolean(L, false);
  10339.  
  10340.     return 1;
  10341. }
  10342.  
  10343. int32_t LuaInterface::luaDoSaveServer(lua_State* L)
  10344. {
  10345.     //doSaveServer([flags = 13])
  10346.     uint8_t flags = 13;
  10347.     if(lua_gettop(L) > 0)
  10348.         flags = popNumber(L);
  10349.  
  10350.     g_game.saveGameState(flags);
  10351.     lua_pushnil(L);
  10352.     return 1;
  10353. }
  10354.  
  10355. int32_t LuaInterface::luaDoSaveHouse(lua_State* L)
  10356. {
  10357.     //doSaveHouse({list})
  10358.     IntegerVec list;
  10359.     if(lua_istable(L, -1))
  10360.     {
  10361.         lua_pushnil(L);
  10362.         while(lua_next(L, -2))
  10363.             list.push_back(popNumber(L));
  10364.  
  10365.         lua_pop(L, 2);
  10366.     }
  10367.     else
  10368.         list.push_back(popNumber(L));
  10369.  
  10370.     House* house;
  10371.     std::vector<House*> houses;
  10372.     for(IntegerVec::const_iterator it = list.begin(); it != list.end(); ++it)
  10373.     {
  10374.         if(!(house = Houses::getInstance()->getHouse(*it)))
  10375.         {
  10376.             std::ostringstream s;
  10377.             s << "House not found, ID: " << (*it);
  10378.             errorEx(s.str());
  10379.  
  10380.             lua_pushboolean(L, false);
  10381.             return 1;
  10382.         }
  10383.  
  10384.         houses.push_back(house);
  10385.     }
  10386.  
  10387.     Database* db = Database::getInstance();
  10388.     DBTransaction trans(db);
  10389.     if(!trans.begin())
  10390.     {
  10391.         lua_pushboolean(L, false);
  10392.         return 1;
  10393.     }
  10394.  
  10395.     for(std::vector<House*>::iterator it = houses.begin(); it != houses.end(); ++it)
  10396.     {
  10397.         if(!IOMapSerialize::getInstance()->saveHouse(db, *it))
  10398.         {
  10399.             std::ostringstream s;
  10400.             s << "Unable to save house information, ID: " << (*it)->getId();
  10401.             errorEx(s.str());
  10402.         }
  10403.  
  10404.         if(!IOMapSerialize::getInstance()->saveHouseItems(db, *it))
  10405.         {
  10406.             std::ostringstream s;
  10407.             s << "Unable to save house items, ID: " << (*it)->getId();
  10408.             errorEx(s.str());
  10409.         }
  10410.     }
  10411.  
  10412.     lua_pushboolean(L, trans.commit());
  10413.     return 1;
  10414. }
  10415.  
  10416. int32_t LuaInterface::luaDoCleanHouse(lua_State* L)
  10417. {
  10418.     //doCleanHouse(houseId)
  10419.     uint32_t houseId = popNumber(L);
  10420.     if(House* house = Houses::getInstance()->getHouse(houseId))
  10421.     {
  10422.         house->clean();
  10423.         lua_pushboolean(L, true);
  10424.     }
  10425.     else
  10426.         lua_pushboolean(L, false);
  10427.  
  10428.     return 1;
  10429. }
  10430.  
  10431. int32_t LuaInterface::luaDoCleanMap(lua_State* L)
  10432. {
  10433.     //doCleanMap()
  10434.     uint32_t count = 0;
  10435.     g_game.cleanMapEx(count);
  10436.     lua_pushnumber(L, count);
  10437.     return 1;
  10438. }
  10439.  
  10440. int32_t LuaInterface::luaDoRefreshMap(lua_State* L)
  10441. {
  10442.     //doRefreshMap()
  10443.     g_game.proceduralRefresh();
  10444.     lua_pushnil(L);
  10445.     return 1;
  10446. }
  10447.  
  10448. int32_t LuaInterface::luaDoUpdateHouseAuctions(lua_State* L)
  10449. {
  10450.     //doUpdateHouseAuctions()
  10451.     lua_pushboolean(L, g_game.getMap()->updateAuctions());
  10452.     return 1;
  10453. }
  10454.  
  10455. int32_t LuaInterface::luaGetItemIdByName(lua_State* L)
  10456. {
  10457.     //getItemIdByName(name)
  10458.     int32_t itemId = Item::items.getItemIdByName(popString(L));
  10459.     if(itemId == -1)
  10460.     {
  10461.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  10462.         lua_pushboolean(L, false);
  10463.     }
  10464.     else
  10465.         lua_pushnumber(L, itemId);
  10466.  
  10467.     return 1;
  10468. }
  10469.  
  10470. int32_t LuaInterface::luaGetItemInfo(lua_State* L)
  10471. {
  10472.     //getItemInfo(itemid)
  10473.     const ItemType* item;
  10474.     if(!(item = Item::items.getElement(popNumber(L))))
  10475.     {
  10476.         lua_pushboolean(L, false);
  10477.         return 1;
  10478.     }
  10479.  
  10480.     lua_newtable(L);
  10481.     setFieldBool(L, "stopTime", item->stopTime);
  10482.     setFieldBool(L, "showCount", item->showCount);
  10483.     setFieldBool(L, "stackable", item->stackable);
  10484.     setFieldBool(L, "showDuration", item->showDuration);
  10485.     setFieldBool(L, "showCharges", item->showCharges);
  10486.     setFieldBool(L, "showAttributes", item->showAttributes);
  10487.     setFieldBool(L, "distRead", item->allowDistRead);
  10488.     setFieldBool(L, "readable", item->canReadText);
  10489.     setFieldBool(L, "writable", item->canWriteText);
  10490.     setFieldBool(L, "forceSerialize", item->forceSerialize);
  10491.     setFieldBool(L, "vertical", item->isVertical);
  10492.     setFieldBool(L, "horizontal", item->isHorizontal);
  10493.     setFieldBool(L, "hangable", item->isHangable);
  10494.     setFieldBool(L, "usable", item->usable);
  10495.     setFieldBool(L, "movable", item->movable);
  10496.     setFieldBool(L, "pickupable", item->pickupable);
  10497.     setFieldBool(L, "rotable", item->rotable);
  10498.     setFieldBool(L, "replacable", item->replacable);
  10499.     setFieldBool(L, "hasHeight", item->hasHeight);
  10500.     setFieldBool(L, "blockSolid", item->blockSolid);
  10501.     setFieldBool(L, "blockPickupable", item->blockPickupable);
  10502.     setFieldBool(L, "blockProjectile", item->blockProjectile);
  10503.     setFieldBool(L, "blockPathing", item->blockPathFind);
  10504.     setFieldBool(L, "allowPickupable", item->allowPickupable);
  10505.     setFieldBool(L, "alwaysOnTop", item->alwaysOnTop);
  10506.     setFieldBool(L, "dualWield", item->dualWield);
  10507.  
  10508.     createTable(L, "floorChange");
  10509.     for(int32_t i = CHANGE_FIRST; i <= CHANGE_LAST; ++i)
  10510.     {
  10511.         lua_pushnumber(L, i);
  10512.         lua_pushboolean(L, item->floorChange[i - 1]);
  10513.         pushTable(L);
  10514.     }
  10515.  
  10516.     pushTable(L);
  10517.     setField(L, "magicEffect", (int32_t)item->magicEffect);
  10518.     setField(L, "fluidSource", (int32_t)item->fluidSource);
  10519.     setField(L, "weaponType", (int32_t)item->weaponType);
  10520.     setField(L, "bedPartnerDirection", (int32_t)item->bedPartnerDir);
  10521.     setField(L, "ammoAction", (int32_t)item->ammoAction);
  10522.     setField(L, "combatType", (int32_t)item->combatType);
  10523.     setField(L, "corpseType", (int32_t)item->corpseType);
  10524.     setField(L, "shootType", (int32_t)item->shootType);
  10525.     setField(L, "ammoType", (int32_t)item->ammoType);
  10526.  
  10527.     createTable(L, "transformBed");
  10528.     setField(L, "female", item->transformBed[PLAYERSEX_FEMALE]);
  10529.     setField(L, "male", item->transformBed[PLAYERSEX_MALE]);
  10530.  
  10531.     pushTable(L);
  10532.     setField(L, "transformUseTo", item->transformUseTo);
  10533.     setField(L, "transformEquipTo", item->transformEquipTo);
  10534.     setField(L, "transformDeEquipTo", item->transformDeEquipTo);
  10535.     setField(L, "clientId", item->clientId);
  10536.     setField(L, "maxItems", item->maxItems);
  10537.     setField(L, "slotPosition", item->slotPosition);
  10538.     setField(L, "wieldPosition", item->wieldPosition);
  10539.     setField(L, "speed", item->speed);
  10540.     setField(L, "maxTextLength", item->maxTextLength);
  10541.     setField(L, "writeOnceItemId", item->writeOnceItemId);
  10542.     setField(L, "date", item->date);
  10543.     setField(L, "writer", item->writer);
  10544.     setField(L, "text", item->text);
  10545.     setField(L, "criticalHitChance", item->criticalHitChance);
  10546.     setField(L, "attack", item->attack);
  10547.     setField(L, "extraAttack", item->extraAttack);
  10548.     setField(L, "defense", item->defense);
  10549.     setField(L, "extraDefense", item->extraDefense);
  10550.     setField(L, "armor", item->armor);
  10551.     setField(L, "breakChance", item->breakChance);
  10552.     setField(L, "hitChance", item->hitChance);
  10553.     setField(L, "maxHitChance", item->maxHitChance);
  10554.     setField(L, "runeLevel", item->runeLevel);
  10555.     setField(L, "runeMagicLevel", item->runeMagLevel);
  10556.     setField(L, "lightLevel", item->lightLevel);
  10557.     setField(L, "lightColor", item->lightColor);
  10558.     setField(L, "decayTo", item->decayTo);
  10559.     setField(L, "rotateTo", item->rotateTo);
  10560.     setField(L, "alwaysOnTopOrder", item->alwaysOnTopOrder);
  10561.     setField(L, "shootRange", item->shootRange);
  10562.     setField(L, "charges", item->charges);
  10563.     setField(L, "decayTime", item->decayTime);
  10564.     setField(L, "attackSpeed", item->attackSpeed);
  10565.     setField(L, "wieldInfo", item->wieldInfo);
  10566.     setField(L, "minRequiredLevel", item->minReqLevel);
  10567.     setField(L, "minRequiredMagicLevel", item->minReqMagicLevel);
  10568.     setField(L, "worth", item->worth);
  10569.     setField(L, "levelDoor", item->levelDoor);
  10570.     setFieldBool(L, "specialDoor", item->specialDoor);
  10571.     setFieldBool(L, "closingDoor", item->closingDoor);
  10572.     setField(L, "name", item->name.c_str());
  10573.     setField(L, "plural", item->pluralName.c_str());
  10574.     setField(L, "article", item->article.c_str());
  10575.     setField(L, "description", item->description.c_str());
  10576.     setField(L, "runeSpellName", item->runeSpellName.c_str());
  10577.     setField(L, "vocationString", item->vocationString.c_str());
  10578.  
  10579.     createTable(L, "abilities");
  10580.     setFieldBool(L, "manaShield", item->hasAbilities() ? item->abilities->manaShield : false);
  10581.     setFieldBool(L, "invisible", item->hasAbilities() ? item->abilities->invisible : false);
  10582.     setFieldBool(L, "regeneration", item->hasAbilities() ? item->abilities->regeneration : false);
  10583.     setFieldBool(L, "preventLoss", item->hasAbilities() ? item->abilities->preventLoss : false);
  10584.     setFieldBool(L, "preventDrop", item->hasAbilities() ? item->abilities->preventDrop : false);
  10585.     setField(L, "elementType", (int32_t)item->hasAbilities() ? item->abilities->elementType : 0);
  10586.     setField(L, "elementDamage", item->hasAbilities() ? item->abilities->elementDamage : 0);
  10587.     setField(L, "speed", item->hasAbilities() ? item->abilities->speed : 0);
  10588.     setField(L, "healthGain", item->hasAbilities() ? item->abilities->healthGain : 0);
  10589.     setField(L, "healthTicks", item->hasAbilities() ? item->abilities->healthTicks : 0);
  10590.     setField(L, "manaGain", item->hasAbilities() ? item->abilities->manaGain : 0);
  10591.     setField(L, "manaTicks", item->hasAbilities() ? item->abilities->manaTicks : 0);
  10592.     setField(L, "conditionSuppressions", item->hasAbilities() ? item->abilities->conditionSuppressions : 0);
  10593.  
  10594.     //TODO: absorb, increment, reflect, skills, skillsPercent, stats, statsPercent
  10595.  
  10596.     pushTable(L);
  10597.     setField(L, "group", (int32_t)item->group);
  10598.     setField(L, "type", (int32_t)item->type);
  10599.     setFieldFloat(L, "weight", item->weight);
  10600.     return 1;
  10601. }
  10602.  
  10603. int32_t LuaInterface::luaGetItemAttribute(lua_State* L)
  10604. {
  10605.     //getItemAttribute(uid, key)
  10606.     std::string key = popString(L);
  10607.     ScriptEnviroment* env = getEnv();
  10608.  
  10609.     Item* item = env->getItemByUID(popNumber(L));
  10610.     if(!item)
  10611.     {
  10612.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  10613.         lua_pushnil(L);
  10614.         return 1;
  10615.     }
  10616.  
  10617.     boost::any value = item->getAttribute(key.c_str());
  10618.     if(value.empty())
  10619.         lua_pushnil(L);
  10620.     else if(value.type() == typeid(std::string))
  10621.         lua_pushstring(L, boost::any_cast<std::string>(value).c_str());
  10622.     else if(value.type() == typeid(int32_t))
  10623.         lua_pushnumber(L, boost::any_cast<int32_t>(value));
  10624.     else if(value.type() == typeid(float))
  10625.         lua_pushnumber(L, boost::any_cast<float>(value));
  10626.     else if(value.type() == typeid(bool))
  10627.         lua_pushboolean(L, boost::any_cast<bool>(value));
  10628.     else
  10629.         lua_pushnil(L);
  10630.  
  10631.     return 1;
  10632. }
  10633.  
  10634. int32_t LuaInterface::luaDoItemSetAttribute(lua_State* L)
  10635. {
  10636.     //doItemSetAttribute(uid, key, value)
  10637.     boost::any value;
  10638.     if(lua_isnumber(L, -1))
  10639.     {
  10640.         double tmp = popFloatNumber(L);
  10641.         if(std::floor(tmp) < tmp)
  10642.             value = tmp;
  10643.         else
  10644.             value = (int32_t)tmp;
  10645.     }
  10646.     else if(lua_isboolean(L, -1))
  10647.         value = popBoolean(L);
  10648.     else if(lua_isstring(L, -1))
  10649.         value = popString(L);
  10650.     else
  10651.     {
  10652.         lua_pop(L, 1);
  10653.         errorEx("Invalid data type");
  10654.  
  10655.         lua_pushboolean(L, false);
  10656.         return 1;
  10657.     }
  10658.  
  10659.     std::string key = popString(L);
  10660.     ScriptEnviroment* env = getEnv();
  10661.  
  10662.     Item* item = env->getItemByUID(popNumber(L));
  10663.     if(!item)
  10664.     {
  10665.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  10666.         lua_pushboolean(L, false);
  10667.         return 1;
  10668.     }
  10669.  
  10670.     if(value.type() == typeid(int32_t))
  10671.     {
  10672.         if(key == "uid")
  10673.         {
  10674.             int32_t tmp = boost::any_cast<int32_t>(value);
  10675.             if(tmp < 1000 || tmp > 0xFFFF)
  10676.             {
  10677.                 errorEx("Value for protected key \"uid\" must be in range of 1000 to 65535");
  10678.                 lua_pushboolean(L, false);
  10679.                 return 1;
  10680.             }
  10681.  
  10682.             item->setUniqueId(tmp);
  10683.         }
  10684.         else if(key == "aid")
  10685.             item->setActionId(boost::any_cast<int32_t>(value));
  10686.         else
  10687.             item->setAttribute(key.c_str(), boost::any_cast<int32_t>(value));
  10688.     }
  10689.     else
  10690.         item->setAttribute(key.c_str(), value);
  10691.  
  10692.     lua_pushboolean(L, true);
  10693.     return 1;
  10694. }
  10695.  
  10696. int32_t LuaInterface::luaDoItemEraseAttribute(lua_State* L)
  10697. {
  10698.     //doItemEraseAttribute(uid, key)
  10699.     std::string key = popString(L);
  10700.     ScriptEnviroment* env = getEnv();
  10701.  
  10702.     Item* item = env->getItemByUID(popNumber(L));
  10703.     if(!item)
  10704.     {
  10705.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  10706.         lua_pushboolean(L, false);
  10707.         return 1;
  10708.     }
  10709.  
  10710.     bool ret = true;
  10711.     if(key == "uid")
  10712.     {
  10713.         errorEx("Attempt to erase protected key \"uid\"");
  10714.         ret = false;
  10715.     }
  10716.     else if(key != "aid")
  10717.         item->eraseAttribute(key.c_str());
  10718.     else
  10719.         item->resetActionId();
  10720.  
  10721.     lua_pushboolean(L, ret);
  10722.     return 1;
  10723. }
  10724.  
  10725. int32_t LuaInterface::luaGetItemWeight(lua_State* L)
  10726. {
  10727.     //getItemWeight(itemid[, precise = true])
  10728.     bool precise = true;
  10729.     if(lua_gettop(L) > 2)
  10730.         precise = popBoolean(L);
  10731.  
  10732.     ScriptEnviroment* env = getEnv();
  10733.     Item* item = env->getItemByUID(popNumber(L));
  10734.     if(!item)
  10735.     {
  10736.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  10737.         lua_pushboolean(L, false);
  10738.         return 1;
  10739.     }
  10740.  
  10741.     double weight = item->getWeight();
  10742.     if(precise)
  10743.     {
  10744.         std::ostringstream ws;
  10745.         ws << std::fixed << std::setprecision(2) << weight;
  10746.         weight = atof(ws.str().c_str());
  10747.     }
  10748.  
  10749.     lua_pushnumber(L, weight);
  10750.     return 1;
  10751. }
  10752.  
  10753. int32_t LuaInterface::luaGetItemParent(lua_State* L)
  10754. {
  10755.     //getItemParent(uid)
  10756.     ScriptEnviroment* env = getEnv();
  10757.  
  10758.     Thing* thing = env->getThingByUID(popNumber(L));
  10759.     if(!thing)
  10760.     {
  10761.         errorEx(getError(LUA_ERROR_THING_NOT_FOUND));
  10762.         lua_pushboolean(L, false);
  10763.         return 1;
  10764.     }
  10765.  
  10766.     if(!thing->getParent())
  10767.     {
  10768.         pushThing(L, NULL, 0);
  10769.         return 1;
  10770.     }
  10771.  
  10772.     if(Item* container = thing->getParent()->getItem())
  10773.         pushThing(L, container, env->addThing(container));
  10774.     else if(Creature* creature = thing->getParent()->getCreature())
  10775.         pushThing(L, creature, env->addThing(creature));
  10776.     else if(Tile* tile = thing->getParent()->getTile())
  10777.     {
  10778.         if(tile->ground && tile->ground != thing)
  10779.             pushThing(L, tile->ground, env->addThing(tile->ground));
  10780.         else
  10781.             pushThing(L, NULL, 0);
  10782.     }
  10783.     else
  10784.         pushThing(L, NULL, 0);
  10785.  
  10786.     return 1;
  10787. }
  10788.  
  10789. int32_t LuaInterface::luaHasItemProperty(lua_State* L)
  10790. {
  10791.     //hasItemProperty(uid, prop)
  10792.     uint32_t prop = popNumber(L);
  10793.     ScriptEnviroment* env = getEnv();
  10794.  
  10795.     Item* item = env->getItemByUID(popNumber(L));
  10796.     if(!item)
  10797.     {
  10798.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  10799.         lua_pushboolean(L, false);
  10800.         return 1;
  10801.     }
  10802.  
  10803.     //Check if the item is a tile, so we can get more accurate properties
  10804.     bool tmp = item->hasProperty((ITEMPROPERTY)prop);
  10805.     if(item->getTile() && item->getTile()->ground == item)
  10806.         tmp = item->getTile()->hasProperty((ITEMPROPERTY)prop);
  10807.  
  10808.     lua_pushboolean(L, tmp);
  10809.     return 1;
  10810. }
  10811.  
  10812. int32_t LuaInterface::luaHasMonsterRaid(lua_State* L)
  10813. {
  10814.     //hasMonsterRaid(cid)
  10815.     ScriptEnviroment* env = getEnv();
  10816.  
  10817.     Creature* creature = env->getCreatureByUID(popNumber(L));
  10818.     if(!creature)
  10819.     {
  10820.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  10821.         lua_pushboolean(L, false);
  10822.         return 1;
  10823.     }
  10824.  
  10825.     Monster* monster = creature->getMonster();
  10826.     if(!monster)
  10827.     {
  10828.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  10829.         lua_pushboolean(L, false);
  10830.         return 1;
  10831.     }
  10832.  
  10833.     lua_pushboolean(L, monster->hasRaid());
  10834.     return 1;
  10835. }
  10836.  
  10837. int32_t LuaInterface::luaIsIpBanished(lua_State* L)
  10838. {
  10839.     //isIpBanished(ip[, mask])
  10840.     uint32_t mask = 0xFFFFFFFF;
  10841.     if(lua_gettop(L) > 1)
  10842.         mask = popNumber(L);
  10843.  
  10844.     lua_pushboolean(L, IOBan::getInstance()->isIpBanished((uint32_t)popNumber(L), mask));
  10845.     return 1;
  10846. }
  10847.  
  10848. int32_t LuaInterface::luaIsPlayerBanished(lua_State* L)
  10849. {
  10850.     //isPlayerBanished(name/guid, type)
  10851.     PlayerBan_t type = (PlayerBan_t)popNumber(L);
  10852.     if(lua_isnumber(L, -1))
  10853.         lua_pushboolean(L, IOBan::getInstance()->isPlayerBanished((uint32_t)popNumber(L), type));
  10854.     else
  10855.         lua_pushboolean(L, IOBan::getInstance()->isPlayerBanished(popString(L), type));
  10856.  
  10857.     return 1;
  10858. }
  10859.  
  10860. int32_t LuaInterface::luaIsAccountBanished(lua_State* L)
  10861. {
  10862.     //isAccountBanished(accountId[, playerId])
  10863.     uint32_t playerId = 0;
  10864.     if(lua_gettop(L) > 1)
  10865.         playerId = popNumber(L);
  10866.  
  10867.     lua_pushboolean(L, IOBan::getInstance()->isAccountBanished((uint32_t)popNumber(L), playerId));
  10868.     return 1;
  10869. }
  10870.  
  10871. int32_t LuaInterface::luaDoAddIpBanishment(lua_State* L)
  10872. {
  10873.     //doAddIpBanishment(ip[, mask[, length[, reason[, comment[, admin[, statement]]]]]])
  10874.     uint32_t admin = 0, reason = 21, mask = 0xFFFFFFFF, params = lua_gettop(L);
  10875.     int64_t length = time(NULL) + g_config.getNumber(ConfigManager::IPBAN_LENGTH);
  10876.     std::string statement, comment;
  10877.  
  10878.     if(params > 6)
  10879.         statement = popString(L);
  10880.  
  10881.     if(params > 5)
  10882.         admin = popNumber(L);
  10883.  
  10884.     if(params > 4)
  10885.         comment = popString(L);
  10886.  
  10887.     if(params > 3)
  10888.         reason = popNumber(L);
  10889.  
  10890.     if(params > 2)
  10891.         length = popNumber(L);
  10892.  
  10893.     if(params > 1)
  10894.         mask = popNumber(L);
  10895.  
  10896.     lua_pushboolean(L, IOBan::getInstance()->addIpBanishment((uint32_t)popNumber(L),
  10897.         length, reason, comment, admin, mask, statement));
  10898.     return 1;
  10899. }
  10900.  
  10901. int32_t LuaInterface::luaDoAddPlayerBanishment(lua_State* L)
  10902. {
  10903.     //doAddPlayerBanishment(name/guid[, type[, length[, reason[, action[, comment[, admin[, statement]]]]]]])
  10904.     uint32_t admin = 0, reason = 21, params = lua_gettop(L);
  10905.     int64_t length = -1;
  10906.     std::string statement, comment;
  10907.  
  10908.     ViolationAction_t action = ACTION_NAMELOCK;
  10909.     PlayerBan_t type = PLAYERBAN_LOCK;
  10910.     if(params > 7)
  10911.         statement = popString(L);
  10912.  
  10913.     if(params > 6)
  10914.         admin = popNumber(L);
  10915.  
  10916.     if(params > 5)
  10917.         comment = popString(L);
  10918.  
  10919.     if(params > 4)
  10920.         action = (ViolationAction_t)popNumber(L);
  10921.  
  10922.     if(params > 3)
  10923.         reason = popNumber(L);
  10924.  
  10925.     if(params > 2)
  10926.         length = popNumber(L);
  10927.  
  10928.     if(params > 1)
  10929.         type = (PlayerBan_t)popNumber(L);
  10930.  
  10931.     if(lua_isnumber(L, -1))
  10932.         lua_pushboolean(L, IOBan::getInstance()->addPlayerBanishment((uint32_t)popNumber(L),
  10933.             length, reason, action, comment, admin, type, statement));
  10934.     else
  10935.         lua_pushboolean(L, IOBan::getInstance()->addPlayerBanishment(popString(L),
  10936.             length, reason, action, comment, admin, type, statement));
  10937.  
  10938.     return 1;
  10939. }
  10940.  
  10941. int32_t LuaInterface::luaDoAddAccountBanishment(lua_State* L)
  10942. {
  10943.     //doAddAccountBanishment(accountId[, playerId[, length[, reason[, action[, comment[, admin[, statement]]]]]]])
  10944.     uint32_t admin = 0, reason = 21, playerId = 0, params = lua_gettop(L);
  10945.     int64_t length = time(NULL) + g_config.getNumber(ConfigManager::BAN_LENGTH);
  10946.     std::string statement, comment;
  10947.  
  10948.     ViolationAction_t action = ACTION_BANISHMENT;
  10949.     if(params > 7)
  10950.         statement = popString(L);
  10951.  
  10952.     if(params > 6)
  10953.         admin = popNumber(L);
  10954.  
  10955.     if(params > 5)
  10956.         comment = popString(L);
  10957.  
  10958.     if(params > 4)
  10959.         action = (ViolationAction_t)popNumber(L);
  10960.  
  10961.     if(params > 3)
  10962.  
  10963.         reason = popNumber(L);
  10964.  
  10965.     if(params > 2)
  10966.         length = popNumber(L);
  10967.  
  10968.     if(params > 1)
  10969.         playerId = popNumber(L);
  10970.  
  10971.     lua_pushboolean(L, IOBan::getInstance()->addAccountBanishment((uint32_t)popNumber(L),
  10972.         length, reason, action, comment, admin, playerId, statement));
  10973.     return 1;
  10974. }
  10975.  
  10976. int32_t LuaInterface::luaDoAddAccountWarnings(lua_State* L)
  10977. {
  10978.     //doAddAccountWarnings(accountId[, warnings])
  10979.     uint32_t warnings = 1;
  10980.     int32_t params = lua_gettop(L);
  10981.     if(params > 1)
  10982.         warnings = popNumber(L);
  10983.  
  10984.     Account account = IOLoginData::getInstance()->loadAccount(popNumber(L), true);
  10985.     account.warnings += warnings;
  10986.     IOLoginData::getInstance()->saveAccount(account);
  10987.     lua_pushboolean(L, true);
  10988.     return 1;
  10989. }
  10990.  
  10991. int32_t LuaInterface::luaDoAddNotation(lua_State* L)
  10992. {
  10993.     //doAddNotation(accountId[, playerId[, reason[, comment[, admin[, statement]]]]]])
  10994.     uint32_t admin = 0, reason = 21, playerId = 0, params = lua_gettop(L);
  10995.     std::string statement, comment;
  10996.  
  10997.     if(params > 5)
  10998.         statement = popString(L);
  10999.  
  11000.     if(params > 4)
  11001.         admin = popNumber(L);
  11002.  
  11003.     if(params > 3)
  11004.         comment = popString(L);
  11005.  
  11006.     if(params > 2)
  11007.         reason = popNumber(L);
  11008.  
  11009.     if(params > 1)
  11010.         playerId = popNumber(L);
  11011.  
  11012.     lua_pushboolean(L, IOBan::getInstance()->addNotation((uint32_t)popNumber(L),
  11013.         reason, comment, admin, playerId, statement));
  11014.     return 1;
  11015. }
  11016.  
  11017. int32_t LuaInterface::luaDoAddStatement(lua_State* L)
  11018. {
  11019.     //doAddStatement(name/guid[, channelId[, reason[, comment[, admin[, statement]]]]]])
  11020.     uint32_t admin = 0, reason = 21, params = lua_gettop(L);
  11021.     int16_t channelId = -1;
  11022.     std::string statement, comment;
  11023.  
  11024.     if(params > 5)
  11025.         statement = popString(L);
  11026.  
  11027.     if(params > 4)
  11028.         admin = popNumber(L);
  11029.  
  11030.     if(params > 3)
  11031.         comment = popString(L);
  11032.  
  11033.     if(params > 2)
  11034.         reason = popNumber(L);
  11035.  
  11036.     if(params > 1)
  11037.         channelId = popNumber(L);
  11038.  
  11039.     if(lua_isnumber(L, -1))
  11040.         lua_pushboolean(L, IOBan::getInstance()->addStatement((uint32_t)popNumber(L),
  11041.             reason, comment, admin, channelId, statement));
  11042.     else
  11043.         lua_pushboolean(L, IOBan::getInstance()->addStatement(popString(L),
  11044.             reason, comment, admin, channelId, statement));
  11045.  
  11046.     return 1;
  11047. }
  11048.  
  11049. int32_t LuaInterface::luaDoRemoveIpBanishment(lua_State* L)
  11050. {
  11051.     //doRemoveIpBanishment(ip[, mask])
  11052.     uint32_t mask = 0xFFFFFFFF;
  11053.     if(lua_gettop(L) > 1)
  11054.         mask = popNumber(L);
  11055.  
  11056.     lua_pushboolean(L, IOBan::getInstance()->removeIpBanishment(
  11057.         (uint32_t)popNumber(L), mask));
  11058.     return 1;
  11059. }
  11060.  
  11061. int32_t LuaInterface::luaDoRemovePlayerBanishment(lua_State* L)
  11062. {
  11063.     //doRemovePlayerBanishment(name/guid, type)
  11064.     PlayerBan_t type = (PlayerBan_t)popNumber(L);
  11065.     if(lua_isnumber(L, -1))
  11066.         lua_pushboolean(L, IOBan::getInstance()->removePlayerBanishment((uint32_t)popNumber(L), type));
  11067.     else
  11068.         lua_pushboolean(L, IOBan::getInstance()->removePlayerBanishment(popString(L), type));
  11069.  
  11070.     return 1;
  11071. }
  11072.  
  11073. int32_t LuaInterface::luaDoRemoveAccountBanishment(lua_State* L)
  11074. {
  11075.     //doRemoveAccountBanishment(accountId[, playerId])
  11076.     uint32_t playerId = 0;
  11077.     if(lua_gettop(L) > 1)
  11078.         playerId = popNumber(L);
  11079.  
  11080.     lua_pushboolean(L, IOBan::getInstance()->removeAccountBanishment((uint32_t)popNumber(L), playerId));
  11081.     return 1;
  11082. }
  11083.  
  11084. int32_t LuaInterface::luaDoRemoveNotations(lua_State* L)
  11085. {
  11086.     //doRemoveNotations(accountId[, playerId])
  11087.     uint32_t playerId = 0;
  11088.     if(lua_gettop(L) > 1)
  11089.         playerId = popNumber(L);
  11090.  
  11091.     lua_pushboolean(L, IOBan::getInstance()->removeNotations((uint32_t)popNumber(L), playerId));
  11092.     return 1;
  11093. }
  11094.  
  11095. int32_t LuaInterface::luaGetAccountWarnings(lua_State* L)
  11096. {
  11097.     //getAccountWarnings(accountId)
  11098.     Account account = IOLoginData::getInstance()->loadAccount(popNumber(L));
  11099.     lua_pushnumber(L, account.warnings);
  11100.     return 1;
  11101. }
  11102.  
  11103. int32_t LuaInterface::luaGetNotationsCount(lua_State* L)
  11104. {
  11105.     //getNotationsCount(accountId[, playerId])
  11106.     uint32_t playerId = 0;
  11107.     if(lua_gettop(L) > 1)
  11108.         playerId = popNumber(L);
  11109.  
  11110.     lua_pushnumber(L, IOBan::getInstance()->getNotationsCount((uint32_t)popNumber(L), playerId));
  11111.     return 1;
  11112. }
  11113.  
  11114. int32_t LuaInterface::luaGetBanData(lua_State* L)
  11115. {
  11116.     //getBanData(value[, type[, param]])
  11117.     Ban tmp;
  11118.     uint32_t params = lua_gettop(L);
  11119.     if(params > 2)
  11120.         tmp.param = popNumber(L);
  11121.  
  11122.     if(params > 1)
  11123.         tmp.type = (Ban_t)popNumber(L);
  11124.  
  11125.     tmp.value = popNumber(L);
  11126.     if(!IOBan::getInstance()->getData(tmp))
  11127.     {
  11128.         lua_pushboolean(L, false);
  11129.         return 1;
  11130.     }
  11131.  
  11132.     lua_newtable(L);
  11133.     setField(L, "id", tmp.id);
  11134.     setField(L, "type", tmp.type);
  11135.     setField(L, "value", tmp.value);
  11136.     setField(L, "param", tmp.param);
  11137.     setField(L, "added", tmp.added);
  11138.     setField(L, "expires", tmp.expires);
  11139.     setField(L, "adminId", tmp.adminId);
  11140.     setField(L, "comment", tmp.comment);
  11141.     return 1;
  11142. }
  11143.  
  11144. int32_t LuaInterface::luaGetBanList(lua_State* L)
  11145. {
  11146.     //getBanList(type[, value[, param]])
  11147.     int32_t param = 0, params = lua_gettop(L);
  11148.     if(params > 2)
  11149.         param = popNumber(L);
  11150.  
  11151.     uint32_t value = 0;
  11152.     if(params > 1)
  11153.         value = popNumber(L);
  11154.  
  11155.     BansVec bans = IOBan::getInstance()->getList((Ban_t)popNumber(L), value, param);
  11156.     BansVec::const_iterator it = bans.begin();
  11157.  
  11158.     lua_newtable(L);
  11159.     for(uint32_t i = 1; it != bans.end(); ++it, ++i)
  11160.     {
  11161.         createTable(L, i);
  11162.         setField(L, "id", it->id);
  11163.         setField(L, "type", it->type);
  11164.         setField(L, "value", it->value);
  11165.         setField(L, "param", it->param);
  11166.         setField(L, "added", it->added);
  11167.         setField(L, "expires", it->expires);
  11168.         setField(L, "adminId", it->adminId);
  11169.         setField(L, "comment", it->comment);
  11170.         pushTable(L);
  11171.     }
  11172.  
  11173.     return 1;
  11174. }
  11175.  
  11176. int32_t LuaInterface::luaGetExperienceStage(lua_State* L)
  11177. {
  11178.     //getExperienceStage(level[, divider])
  11179.     double divider = 1.0f;
  11180.     if(lua_gettop(L) > 1)
  11181.         divider = popFloatNumber(L);
  11182.  
  11183.     lua_pushnumber(L, g_game.getExperienceStage(popNumber(L), divider));
  11184.     return 1;
  11185. }
  11186.  
  11187. int32_t LuaInterface::luaGetDataDir(lua_State* L)
  11188. {
  11189.     //getDataDir()
  11190.     lua_pushstring(L, getFilePath(FILE_TYPE_OTHER, "").c_str());
  11191.     return 1;
  11192. }
  11193.  
  11194. int32_t LuaInterface::luaGetLogsDir(lua_State* L)
  11195. {
  11196.     //getLogsDir()
  11197.     lua_pushstring(L, getFilePath(FILE_TYPE_LOG, "").c_str());
  11198.     return 1;
  11199. }
  11200.  
  11201. int32_t LuaInterface::luaGetConfigFile(lua_State* L)
  11202. {
  11203.     //getConfigFile()
  11204.     lua_pushstring(L, g_config.getString(ConfigManager::CONFIG_FILE).c_str());
  11205.     return 1;
  11206. }
  11207.  
  11208. int32_t LuaInterface::luaDoPlayerSetWalkthrough(lua_State* L)
  11209. {
  11210.     //doPlayerSetWalkthrough(cid, uid, walkthrough)
  11211.     bool walkthrough = popBoolean(L);
  11212.     uint32_t uid = popNumber(L);
  11213.  
  11214.     ScriptEnviroment* env = getEnv();
  11215.     Player* player = env->getPlayerByUID(popNumber(L));
  11216.     if(!player)
  11217.     {
  11218.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  11219.         lua_pushboolean(L, false);
  11220.         return 1;
  11221.     }
  11222.  
  11223.     Creature* creature = env->getCreatureByUID(uid);
  11224.     if(!creature)
  11225.     {
  11226.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  11227.         lua_pushboolean(L, false);
  11228.         return 1;
  11229.     }
  11230.  
  11231.     if(player != creature)
  11232.     {
  11233.         player->setWalkthrough(creature, walkthrough);
  11234.         lua_pushboolean(L, true);
  11235.     }
  11236.     else
  11237.         lua_pushboolean(L, false);
  11238.  
  11239.     return 1;
  11240. }
  11241.  
  11242. int32_t LuaInterface::luaDoGuildAddEnemy(lua_State* L)
  11243. {
  11244.     //doGuildAddEnemy(guild, enemy, war, type)
  11245.     War_t war;
  11246.     war.type = (WarType_t)popNumber(L);
  11247.     war.war = popNumber(L);
  11248.  
  11249.     uint32_t enemy = popNumber(L), guild = popNumber(L), count = 0;
  11250.     for(AutoList<Player>::iterator it = Player::autoList.begin(); it != Player::autoList.end(); ++it)
  11251.     {
  11252.         if(it->second->isRemoved() || it->second->getGuildId() != guild)
  11253.             continue;
  11254.  
  11255.         ++count;
  11256.         it->second->addEnemy(enemy, war);
  11257.         g_game.updateCreatureEmblem(it->second);
  11258.     }
  11259.  
  11260.     lua_pushnumber(L, count);
  11261.     return 1;
  11262. }
  11263.  
  11264. int32_t LuaInterface::luaDoGuildRemoveEnemy(lua_State* L)
  11265. {
  11266.     //doGuildRemoveEnemy(guild, enemy)
  11267.     uint32_t enemy = popNumber(L), guild = popNumber(L), count = 0;
  11268.     for(AutoList<Player>::iterator it = Player::autoList.begin(); it != Player::autoList.end(); ++it)
  11269.     {
  11270.         if(it->second->isRemoved() || it->second->getGuildId() != guild)
  11271.             continue;
  11272.  
  11273.         ++count;
  11274.         it->second->removeEnemy(enemy);
  11275.         g_game.updateCreatureEmblem(it->second);
  11276.     }
  11277.  
  11278.     lua_pushnumber(L, count);
  11279.     return 1;
  11280. }
  11281.  
  11282. int32_t LuaInterface::luaGetConfigValue(lua_State* L)
  11283. {
  11284.     //getConfigValue(key)
  11285.     g_config.getValue(popString(L), L);
  11286.     return 1;
  11287. }
  11288.  
  11289. int32_t LuaInterface::luaGetModList(lua_State* L)
  11290. {
  11291.     //getModList()
  11292.     ModMap::iterator it = ScriptManager::getInstance()->getFirstMod();
  11293.     lua_newtable(L);
  11294.     for(uint32_t i = 1; it != ScriptManager::getInstance()->getLastMod(); ++it, ++i)
  11295.     {
  11296.         createTable(L, i);
  11297.         setField(L, "name", it->first);
  11298.         setField(L, "description", it->second.description);
  11299.         setField(L, "file", it->second.file);
  11300.  
  11301.         setField(L, "version", it->second.version);
  11302.         setField(L, "author", it->second.author);
  11303.         setField(L, "contact", it->second.contact);
  11304.  
  11305.         setFieldBool(L, "enabled", it->second.enabled);
  11306.         pushTable(L);
  11307.     }
  11308.  
  11309.     return 1;
  11310. }
  11311.  
  11312. int32_t LuaInterface::luaL_loadmodlib(lua_State* L)
  11313. {
  11314.     //loadmodlib(lib)
  11315.     std::string name = asLowerCaseString(popString(L));
  11316.     for(LibMap::iterator it = ScriptManager::getInstance()->getFirstLib();
  11317.         it != ScriptManager::getInstance()->getLastLib(); ++it)
  11318.     {
  11319.         if(asLowerCaseString(it->first) != name)
  11320.             continue;
  11321.  
  11322.         luaL_loadstring(L, it->second.second.c_str());
  11323.         lua_pushvalue(L, -1);
  11324.         break;
  11325.     }
  11326.  
  11327.     return 1;
  11328. }
  11329.  
  11330. int32_t LuaInterface::luaL_domodlib(lua_State* L)
  11331. {
  11332.     //domodlib(lib)
  11333.     std::string name = asLowerCaseString(popString(L));
  11334.     for(LibMap::iterator it = ScriptManager::getInstance()->getFirstLib();
  11335.         it != ScriptManager::getInstance()->getLastLib(); ++it)
  11336.     {
  11337.         if(asLowerCaseString(it->first) != name)
  11338.             continue;
  11339.  
  11340.         bool ret = luaL_dostring(L, it->second.second.c_str());
  11341.         if(ret)
  11342.             error(NULL, popString(L));
  11343.  
  11344.         lua_pushboolean(L, !ret);
  11345.         break;
  11346.     }
  11347.  
  11348.     return 1;
  11349. }
  11350.  
  11351. int32_t LuaInterface::luaL_dodirectory(lua_State* L)
  11352. {
  11353.     //dodirectory(dir[, recursively = false[, loadSystems = true]])
  11354.     bool recursively = false, loadSystems = true;
  11355.     int32_t params = lua_gettop(L);
  11356.     if(params > 2)
  11357.         loadSystems = popBoolean(L);
  11358.  
  11359.     if(params > 1)
  11360.         recursively = popBoolean(L);
  11361.  
  11362.     std::string dir = popString(L);
  11363.     if(!getEnv()->getInterface()->loadDirectory(dir, recursively, loadSystems, NULL))
  11364.     {
  11365.         errorEx("Failed to load directory " + dir);
  11366.         lua_pushboolean(L, false);
  11367.     }
  11368.     else
  11369.         lua_pushboolean(L, true);
  11370.  
  11371.     return 1;
  11372. }
  11373.  
  11374. int32_t LuaInterface::luaL_errors(lua_State* L)
  11375. {
  11376.     //errors(var)
  11377.     bool status = getEnv()->getInterface()->m_errors;
  11378.     getEnv()->getInterface()->m_errors = popBoolean(L);
  11379.     lua_pushboolean(L, status);
  11380.     return 1;
  11381. }
  11382.  
  11383. #define EXPOSE_LOG(Name, Stream)\
  11384.     int32_t LuaInterface::luaStd##Name(lua_State* L)\
  11385.     {\
  11386.         StringVec data;\
  11387.         for(int32_t i = 0, params = lua_gettop(L); i < params; ++i)\
  11388.         {\
  11389.             if(lua_isnil(L, -1))\
  11390.             {\
  11391.                 data.push_back("nil");\
  11392.                 lua_pop(L, 1);\
  11393.             }\
  11394.             else if(lua_isboolean(L, -1))\
  11395.                 data.push_back(popBoolean(L) ? "true" : "false");\
  11396.             else if(lua_istable(L, -1)) {/* "table: address" */}\
  11397.             else if(lua_isfunction(L, -1)) {/* "function: address" */}\
  11398.             else\
  11399.                 data.push_back(popString(L));\
  11400.         }\
  11401. \
  11402.         for(StringVec::reverse_iterator it = data.rbegin(); it != data.rend(); ++it)\
  11403.             Stream << (*it) << std::endl;\
  11404. \
  11405.         lua_pushnumber(L, data.size());\
  11406.         return 1;\
  11407.     }
  11408.  
  11409. EXPOSE_LOG(Cout, std::cout)
  11410. EXPOSE_LOG(Clog, std::clog)
  11411. EXPOSE_LOG(Cerr, std::cerr)
  11412.  
  11413. #undef EXPOSE_LOG
  11414.  
  11415. int32_t LuaInterface::luaStdMD5(lua_State* L)
  11416. {
  11417.     //std.md5(string[, upperCase = false])
  11418.     bool upperCase = false;
  11419.     if(lua_gettop(L) > 1)
  11420.         upperCase = popBoolean(L);
  11421.  
  11422.     lua_pushstring(L, transformToMD5(popString(L), upperCase).c_str());
  11423.     return 1;
  11424. }
  11425.  
  11426. int32_t LuaInterface::luaStdSHA1(lua_State* L)
  11427. {
  11428.     //std.sha1(string[, upperCase = false])
  11429.     bool upperCase = false;
  11430.     if(lua_gettop(L) > 1)
  11431.         upperCase = popBoolean(L);
  11432.  
  11433.     lua_pushstring(L, transformToSHA1(popString(L), upperCase).c_str());
  11434.     return 1;
  11435. }
  11436.  
  11437. int32_t LuaInterface::luaStdSHA256(lua_State* L)
  11438. {
  11439.     //std.sha256(string[, upperCase = false])
  11440.     bool upperCase = false;
  11441.     if(lua_gettop(L) > 1)
  11442.         upperCase = popBoolean(L);
  11443.  
  11444.     lua_pushstring(L, transformToSHA256(popString(L), upperCase).c_str());
  11445.     return 1;
  11446. }
  11447.  
  11448. int32_t LuaInterface::luaStdSHA512(lua_State* L)
  11449. {
  11450.     //std.sha512(string[, upperCase = false])
  11451.     bool upperCase = false;
  11452.     if(lua_gettop(L) > 1)
  11453.         upperCase = popBoolean(L);
  11454.  
  11455.     lua_pushstring(L, transformToSHA512(popString(L), upperCase).c_str());
  11456.     return 1;
  11457. }
  11458.  
  11459. int32_t LuaInterface::luaStdCheckName(lua_State* L)
  11460. {
  11461.     //std.checkName(string[, forceUppercaseOnFirstLetter = true])
  11462.     bool forceUppercaseOnFirstLetter = true;
  11463.     if(lua_gettop(L) > 1)
  11464.         forceUppercaseOnFirstLetter = popBoolean(L);
  11465.  
  11466.     lua_pushboolean(L, isValidName(popString(L), forceUppercaseOnFirstLetter));
  11467.     return 1;
  11468. }
  11469.  
  11470. int32_t LuaInterface::luaSystemTime(lua_State* L)
  11471. {
  11472.     //os.mtime()
  11473.     lua_pushnumber(L, OTSYS_TIME());
  11474.     return 1;
  11475. }
  11476.  
  11477. int32_t LuaInterface::luaDatabaseExecute(lua_State* L)
  11478. {
  11479.     //db.query(query)
  11480.     std::ostringstream query; //lock mutex
  11481.     query << popString(L);
  11482.  
  11483.     lua_pushboolean(L, Database::getInstance()->query(query.str()));
  11484.     return 1;
  11485. }
  11486.  
  11487. int32_t LuaInterface::luaDatabaseStoreQuery(lua_State* L)
  11488. {
  11489.     //db.storeQuery(query)
  11490.     ScriptEnviroment* env = getEnv();
  11491.     std::ostringstream query; //lock mutex
  11492.  
  11493.     query << popString(L);
  11494.     if(DBResult* res = Database::getInstance()->storeQuery(query.str()))
  11495.         lua_pushnumber(L, env->addResult(res));
  11496.     else
  11497.         lua_pushboolean(L, false);
  11498.  
  11499.     return 1;
  11500. }
  11501.  
  11502. int32_t LuaInterface::luaDatabaseEscapeString(lua_State* L)
  11503. {
  11504.     //db.escapeString(str)
  11505.     lua_pushstring(L, Database::getInstance()->escapeString(popString(L)).c_str());
  11506.     return 1;
  11507. }
  11508.  
  11509. int32_t LuaInterface::luaDatabaseEscapeBlob(lua_State* L)
  11510. {
  11511.     //db.escapeBlob(s, length)
  11512.     uint32_t length = popNumber(L);
  11513.     lua_pushstring(L, Database::getInstance()->escapeBlob(popString(L).c_str(), length).c_str());
  11514.     return 1;
  11515. }
  11516.  
  11517. int32_t LuaInterface::luaDatabaseLastInsertId(lua_State* L)
  11518. {
  11519.     //db.lastInsertId()
  11520.     lua_pushnumber(L, Database::getInstance()->getLastInsertId());
  11521.     return 1;
  11522. }
  11523.  
  11524. int32_t LuaInterface::luaDatabaseStringComparer(lua_State* L)
  11525. {
  11526.     //db.stringComparer()
  11527.     lua_pushstring(L, Database::getInstance()->getStringComparer().c_str());
  11528.     return 1;
  11529. }
  11530.  
  11531. int32_t LuaInterface::luaDatabaseUpdateLimiter(lua_State* L)
  11532. {
  11533.     //db.updateLimiter()
  11534.     lua_pushstring(L, Database::getInstance()->getUpdateLimiter().c_str());
  11535.     return 1;
  11536. }
  11537.  
  11538. int32_t LuaInterface::luaDatabaseConnected(lua_State* L)
  11539. {
  11540.     //db.connected()
  11541.     lua_pushboolean(L, Database::getInstance()->isConnected());
  11542.     return 1;
  11543. }
  11544.  
  11545. int32_t LuaInterface::luaDatabaseTableExists(lua_State* L)
  11546. {
  11547.     //db.tableExists(table)
  11548.     lua_pushboolean(L, DatabaseManager::getInstance()->tableExists(popString(L)));
  11549.     return 1;
  11550. }
  11551.  
  11552. int32_t LuaInterface::luaDatabaseTransBegin(lua_State* L)
  11553. {
  11554.     //db.transBegin()
  11555.     lua_pushboolean(L, Database::getInstance()->beginTransaction());
  11556.     return 1;
  11557. }
  11558.  
  11559. int32_t LuaInterface::luaDatabaseTransRollback(lua_State* L)
  11560. {
  11561.     //db.transRollback()
  11562.     lua_pushboolean(L, Database::getInstance()->rollback());
  11563.     return 1;
  11564. }
  11565.  
  11566. int32_t LuaInterface::luaDatabaseTransCommit(lua_State* L)
  11567. {
  11568.     //db.transCommit()
  11569.     lua_pushboolean(L, Database::getInstance()->commit());
  11570.     return 1;
  11571. }
  11572.  
  11573. #define CHECK_RESULT()\
  11574.     if(!res)\
  11575.     {\
  11576.         lua_pushboolean(L, false);\
  11577.         return 1;\
  11578.     }
  11579.  
  11580. int32_t LuaInterface::luaResultGetDataInt(lua_State* L)
  11581. {
  11582.     //result.getDataInt(res, s)
  11583.     const std::string& s = popString(L);
  11584.     ScriptEnviroment* env = getEnv();
  11585.  
  11586.     DBResult* res = env->getResultByID(popNumber(L));
  11587.     CHECK_RESULT()
  11588.  
  11589.     lua_pushnumber(L, res->getDataInt(s));
  11590.     return 1;
  11591. }
  11592.  
  11593. int32_t LuaInterface::luaResultGetDataLong(lua_State* L)
  11594. {
  11595.     //result.getDataLong(res, s)
  11596.     const std::string& s = popString(L);
  11597.     ScriptEnviroment* env = getEnv();
  11598.  
  11599.     DBResult* res = env->getResultByID(popNumber(L));
  11600.     CHECK_RESULT()
  11601.  
  11602.     lua_pushnumber(L, res->getDataLong(s));
  11603.     return 1;
  11604. }
  11605.  
  11606. int32_t LuaInterface::luaResultGetDataString(lua_State* L)
  11607. {
  11608.     //result.getDataString(res, s)
  11609.     const std::string& s = popString(L);
  11610.     ScriptEnviroment* env = getEnv();
  11611.  
  11612.     DBResult* res = env->getResultByID(popNumber(L));
  11613.     CHECK_RESULT()
  11614.  
  11615.     lua_pushstring(L, res->getDataString(s).c_str());
  11616.     return 1;
  11617. }
  11618.  
  11619. int32_t LuaInterface::luaResultGetDataStream(lua_State* L)
  11620. {
  11621.     //result.getDataStream(res, s)
  11622.     const std::string s = popString(L);
  11623.     ScriptEnviroment* env = getEnv();
  11624.  
  11625.     DBResult* res = env->getResultByID(popNumber(L));
  11626.     CHECK_RESULT()
  11627.  
  11628.     uint64_t length = 0;
  11629.     lua_pushstring(L, res->getDataStream(s, length));
  11630.  
  11631.     lua_pushnumber(L, length);
  11632.     return 2;
  11633. }
  11634.  
  11635. int32_t LuaInterface::luaResultNext(lua_State* L)
  11636. {
  11637.     //result.next(res)
  11638.     ScriptEnviroment* env = getEnv();
  11639.  
  11640.     DBResult* res = env->getResultByID(popNumber(L));
  11641.     CHECK_RESULT()
  11642.  
  11643.     lua_pushboolean(L, res->next());
  11644.     return 1;
  11645. }
  11646.  
  11647. int32_t LuaInterface::luaResultFree(lua_State* L)
  11648. {
  11649.     //result.free(res)
  11650.     uint32_t rid = popNumber(L);
  11651.     ScriptEnviroment* env = getEnv();
  11652.  
  11653.     DBResult* res = env->getResultByID(rid);
  11654.     CHECK_RESULT()
  11655.  
  11656.     lua_pushboolean(L, env->removeResult(rid));
  11657.     return 1;
  11658. }
  11659.  
  11660. #undef CHECK_RESULT
  11661.  
  11662. int32_t LuaInterface::luaBitNot(lua_State* L)
  11663. {
  11664.     int32_t number = (int32_t)popNumber(L);
  11665.     lua_pushnumber(L, ~number);
  11666.     return 1;
  11667. }
  11668.  
  11669. int32_t LuaInterface::luaBitUNot(lua_State* L)
  11670. {
  11671.     uint32_t number = (uint32_t)popNumber(L);
  11672.     lua_pushnumber(L, ~number);
  11673.     return 1;
  11674. }
  11675.  
  11676. #define MULTI_OPERATOR(type, name, op)\
  11677.     int32_t LuaInterface::luaBit##name(lua_State* L)\
  11678.     {\
  11679.         int32_t params = lua_gettop(L);\
  11680.         type value = (type)popNumber(L);\
  11681.         for(int32_t i = 2; i <= params; ++i)\
  11682.             value op popNumber(L);\
  11683. \
  11684.         lua_pushnumber(L, value);\
  11685.         return 1;\
  11686.     }
  11687.  
  11688. MULTI_OPERATOR(int32_t, And, &=)
  11689. MULTI_OPERATOR(int32_t, Or, |=)
  11690. MULTI_OPERATOR(int32_t, Xor, ^=)
  11691. MULTI_OPERATOR(uint32_t, UAnd, &=)
  11692. MULTI_OPERATOR(uint32_t, UOr, |=)
  11693. MULTI_OPERATOR(uint32_t, UXor, ^=)
  11694.  
  11695. #undef MULTI_OPERATOR
  11696.  
  11697. #define SHIFT_OPERATOR(type, name, op)\
  11698.     int32_t LuaInterface::luaBit##name(lua_State* L)\
  11699.     {\
  11700.         type v2 = (type)popNumber(L), v1 = (type)popNumber(L);\
  11701.         lua_pushnumber(L, (v1 op v2));\
  11702.         return 1;\
  11703.     }
  11704.  
  11705. SHIFT_OPERATOR(int32_t, LeftShift, <<)
  11706. SHIFT_OPERATOR(int32_t, RightShift, >>)
  11707. SHIFT_OPERATOR(uint32_t, ULeftShift, <<)
  11708. SHIFT_OPERATOR(uint32_t, URightShift, >>)
  11709.  
  11710. #undef SHIFT_OPERATOR
  11711.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement