Advertisement
Guest User

luascript.cpp

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