Guest User

luascript.cpp

a guest
Apr 13th, 2020
409
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 "iologindata.h"
  37. #include "ioban.h"
  38. #include "iomap.h"
  39. #include "iomapserialize.h"
  40.  
  41. #include "talkaction.h"
  42. #include "spells.h"
  43. #include "combat.h"
  44. #include "condition.h"
  45.  
  46. #include "baseevents.h"
  47. #include "monsters.h"
  48. #include "raids.h"
  49.  
  50. #include "configmanager.h"
  51. #include "vocation.h"
  52. #include "status.h"
  53. #include "game.h"
  54. #include "chat.h"
  55.  
  56. extern Game g_game;
  57. extern Monsters g_monsters;
  58. extern Chat g_chat;
  59. extern ConfigManager g_config;
  60. extern Spells* g_spells;
  61. extern TalkActions* g_talkActions;
  62.  
  63. enum
  64. {
  65.     EVENT_ID_LOADING = 1,
  66.     EVENT_ID_USER = 1000,
  67. };
  68.  
  69. ScriptEnviroment::AreaMap ScriptEnviroment::m_areaMap;
  70. uint32_t ScriptEnviroment::m_lastAreaId = 0;
  71. ScriptEnviroment::CombatMap ScriptEnviroment::m_combatMap;
  72. uint32_t ScriptEnviroment::m_lastCombatId = 0;
  73. ScriptEnviroment::ConditionMap ScriptEnviroment::m_conditionMap;
  74. uint32_t ScriptEnviroment::m_lastConditionId = 0;
  75. ScriptEnviroment::ConditionMap ScriptEnviroment::m_tempConditionMap;
  76.  
  77. ScriptEnviroment::ThingMap ScriptEnviroment::m_globalMap;
  78. ScriptEnviroment::StorageMap ScriptEnviroment::m_storageMap;
  79. ScriptEnviroment::TempItemListMap ScriptEnviroment::m_tempItems;
  80.  
  81. ScriptEnviroment::ScriptEnviroment()
  82. {
  83.     m_lastUID = 70000;
  84.     m_loaded = true;
  85.     reset();
  86. }
  87.  
  88. ScriptEnviroment::~ScriptEnviroment()
  89. {
  90.     for(CombatMap::iterator it = m_combatMap.begin(); it != m_combatMap.end(); ++it)
  91.         delete it->second;
  92.  
  93.     m_combatMap.clear();
  94.     for(AreaMap::iterator it = m_areaMap.begin(); it != m_areaMap.end(); ++it)
  95.         delete it->second;
  96.  
  97.     m_areaMap.clear();
  98.     for(ConditionMap::iterator it = m_conditionMap.begin(); it != m_conditionMap.end(); ++it)
  99.         delete it->second;
  100.  
  101.     m_conditionMap.clear();
  102.     reset();
  103. }
  104.  
  105. void ScriptEnviroment::reset()
  106. {
  107.     m_scriptId = m_callbackId = 0;
  108.     m_timerEvent = false;
  109.  
  110.     m_realPos = Position();
  111.     m_interface = NULL;
  112.     for(TempItemListMap::iterator mit = m_tempItems.begin(); mit != m_tempItems.end(); ++mit)
  113.     {
  114.         ItemList itemList = mit->second;
  115.         for(ItemList::iterator it = itemList.begin(); it != itemList.end(); ++it)
  116.         {
  117.             if((*it)->getParent() == VirtualCylinder::virtualCylinder)
  118.                 g_game.freeThing(*it);
  119.         }
  120.     }
  121.  
  122.     m_tempItems.clear();
  123.     for(DBResultMap::iterator it = m_tempResults.begin(); it != m_tempResults.end(); ++it)
  124.     {
  125.         if(it->second)
  126.             it->second->free();
  127.     }
  128.  
  129.     m_tempResults.clear();
  130.     for(ConditionMap::iterator it = m_tempConditionMap.begin(); it != m_tempConditionMap.end(); ++it)
  131.         delete it->second;
  132.  
  133.     m_tempConditionMap.clear();
  134.     m_localMap.clear();
  135. }
  136.  
  137. bool ScriptEnviroment::saveGameState()
  138. {
  139.     if(!g_config.getBool(ConfigManager::SAVE_GLOBAL_STORAGE))
  140.         return true;
  141.  
  142.     Database* db = Database::getInstance();
  143.     DBQuery query;
  144.  
  145.     query << "DELETE FROM `global_storage` WHERE `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << ";";
  146.     if(!db->query(query.str()))
  147.         return false;
  148.  
  149.     DBInsert query_insert(db);
  150.     query_insert.setQuery("INSERT INTO `global_storage` (`key`, `world_id`, `value`) VALUES ");
  151.     for(StorageMap::const_iterator it = m_storageMap.begin(); it != m_storageMap.end(); ++it)
  152.     {
  153.         std::stringstream ss;
  154.         ss << it->first << ", " << g_config.getNumber(ConfigManager::WORLD_ID) << ", " << db->escapeString(it->second).c_str();
  155.         if(!query_insert.addRow(ss))
  156.             return false;
  157.     }
  158.  
  159.     return query_insert.execute();
  160. }
  161.  
  162. bool ScriptEnviroment::loadGameState()
  163. {
  164.     Database* db = Database::getInstance();
  165.     DBResult* result;
  166.  
  167.     DBQuery query;
  168.     query << "SELECT `key`, `value` FROM `global_storage` WHERE `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << ";";
  169.     if((result = db->storeQuery(query.str())))
  170.     {
  171.         do
  172.             m_storageMap[result->getDataInt("key")] = result->getDataString("value");
  173.         while(result->next());
  174.         result->free();
  175.     }
  176.  
  177.     query.str("");
  178.     return true;
  179. }
  180.  
  181. bool ScriptEnviroment::setCallbackId(int32_t callbackId, LuaInterface* interface)
  182. {
  183.     if(!m_callbackId)
  184.     {
  185.         m_callbackId = callbackId;
  186.         m_interface = interface;
  187.         return true;
  188.     }
  189.  
  190.     //nested callbacks are not allowed
  191.     if(m_interface)
  192.         m_interface->errorEx("Nested callbacks!");
  193.  
  194.     return false;
  195. }
  196.  
  197. void ScriptEnviroment::getInfo(int32_t& scriptId, std::string& desc, LuaInterface*& interface, int32_t& callbackId, bool& timerEvent)
  198. {
  199.     scriptId = m_scriptId;
  200.     desc = m_event;
  201.     interface = m_interface;
  202.     callbackId = m_callbackId;
  203.     timerEvent = m_timerEvent;
  204. }
  205.  
  206. void ScriptEnviroment::addUniqueThing(Thing* thing)
  207. {
  208.     Item* item = thing->getItem();
  209.     if(!item || !item->getUniqueId())
  210.         return;
  211.  
  212.     if(m_globalMap[item->getUniqueId()])
  213.     {
  214.         if(item->getActionId() != 2000) //scripted quest system
  215.             std::clog << "Duplicate uniqueId " << item->getUniqueId() << std::endl;
  216.     }
  217.     else
  218.         m_globalMap[item->getUniqueId()] = thing;
  219. }
  220.  
  221. void ScriptEnviroment::removeUniqueThing(Thing* thing)
  222. {
  223.     Item* item = thing->getItem();
  224.     if(!item || !item->getUniqueId())
  225.         return;
  226.  
  227.     ThingMap::iterator it = m_globalMap.find(item->getUniqueId());
  228.     if(it != m_globalMap.end())
  229.         m_globalMap.erase(it);
  230. }
  231.  
  232. uint32_t ScriptEnviroment::addThing(Thing* thing)
  233. {
  234.     if(!thing || thing->isRemoved())
  235.         return 0;
  236.  
  237.     for(ThingMap::iterator it = m_localMap.begin(); it != m_localMap.end(); ++it)
  238.     {
  239.         if(it->second == thing)
  240.             return it->first;
  241.     }
  242.  
  243.     if(Creature* creature = thing->getCreature())
  244.     {
  245.         m_localMap[creature->getID()] = thing;
  246.         return creature->getID();
  247.     }
  248.  
  249.     if(Item* item = thing->getItem())
  250.     {
  251.         uint32_t tmp = item->getUniqueId();
  252.         if(tmp)
  253.         {
  254.             m_localMap[tmp] = thing;
  255.             return tmp;
  256.         }
  257.     }
  258.  
  259.     while(m_localMap.find(m_lastUID) != m_localMap.end())
  260.         ++m_lastUID;
  261.  
  262.     m_localMap[m_lastUID] = thing;
  263.     return m_lastUID;
  264. }
  265.  
  266. void ScriptEnviroment::insertThing(uint32_t uid, Thing* thing)
  267. {
  268.     if(!m_localMap[uid])
  269.         m_localMap[uid] = thing;
  270.     else
  271.         std::clog << "[Error - ScriptEnviroment::insertThing] Thing uid already taken" << std::endl;
  272. }
  273.  
  274. Thing* ScriptEnviroment::getThingByUID(uint32_t uid)
  275. {
  276.     Thing* tmp = m_localMap[uid];
  277.     if(tmp && !tmp->isRemoved())
  278.         return tmp;
  279.  
  280.     tmp = m_globalMap[uid];
  281.     if(tmp && !tmp->isRemoved())
  282.         return tmp;
  283.  
  284.     if(uid >= 0x10000000)
  285.     {
  286.         tmp = g_game.getCreatureByID(uid);
  287.         if(tmp && !tmp->isRemoved())
  288.         {
  289.             m_localMap[uid] = tmp;
  290.             return tmp;
  291.         }
  292.     }
  293.  
  294.     return NULL;
  295. }
  296.  
  297. Item* ScriptEnviroment::getItemByUID(uint32_t uid)
  298. {
  299.     if(Thing* tmp = getThingByUID(uid))
  300.     {
  301.         if(Item* item = tmp->getItem())
  302.             return item;
  303.     }
  304.  
  305.     return NULL;
  306. }
  307.  
  308. Container* ScriptEnviroment::getContainerByUID(uint32_t uid)
  309. {
  310.     if(Item* tmp = getItemByUID(uid))
  311.     {
  312.         if(Container* container = tmp->getContainer())
  313.             return container;
  314.     }
  315.  
  316.     return NULL;
  317. }
  318.  
  319. Creature* ScriptEnviroment::getCreatureByUID(uint32_t uid)
  320. {
  321.     if(Thing* tmp = getThingByUID(uid))
  322.     {
  323.         if(Creature* creature = tmp->getCreature())
  324.             return creature;
  325.     }
  326.  
  327.     return NULL;
  328. }
  329.  
  330. Player* ScriptEnviroment::getPlayerByUID(uint32_t uid)
  331. {
  332.     if(Thing* tmp = getThingByUID(uid))
  333.     {
  334.         if(Creature* creature = tmp->getCreature())
  335.         {
  336.             if(Player* player = creature->getPlayer())
  337.                 return player;
  338.         }
  339.     }
  340.  
  341.     return NULL;
  342. }
  343.  
  344. void ScriptEnviroment::removeThing(uint32_t uid)
  345. {
  346.     ThingMap::iterator it;
  347.     it = m_localMap.find(uid);
  348.     if(it != m_localMap.end())
  349.         m_localMap.erase(it);
  350.  
  351.     it = m_globalMap.find(uid);
  352.     if(it != m_globalMap.end())
  353.         m_globalMap.erase(it);
  354. }
  355.  
  356. uint32_t ScriptEnviroment::addCombatArea(CombatArea* area)
  357. {
  358.     uint32_t newAreaId = m_lastAreaId + 1;
  359.     m_areaMap[newAreaId] = area;
  360.  
  361.     m_lastAreaId++;
  362.     return newAreaId;
  363. }
  364.  
  365. CombatArea* ScriptEnviroment::getCombatArea(uint32_t areaId)
  366. {
  367.     AreaMap::const_iterator it = m_areaMap.find(areaId);
  368.     if(it != m_areaMap.end())
  369.         return it->second;
  370.  
  371.     return NULL;
  372. }
  373.  
  374. uint32_t ScriptEnviroment::addCombatObject(Combat* combat)
  375. {
  376.     uint32_t newCombatId = m_lastCombatId + 1;
  377.     m_combatMap[newCombatId] = combat;
  378.  
  379.     m_lastCombatId++;
  380.     return newCombatId;
  381. }
  382.  
  383. Combat* ScriptEnviroment::getCombatObject(uint32_t combatId)
  384. {
  385.     CombatMap::iterator it = m_combatMap.find(combatId);
  386.     if(it != m_combatMap.end())
  387.         return it->second;
  388.  
  389.     return NULL;
  390. }
  391.  
  392. uint32_t ScriptEnviroment::addConditionObject(Condition* condition)
  393. {
  394.     m_conditionMap[++m_lastConditionId] = condition;
  395.     return m_lastConditionId;
  396. }
  397.  
  398. uint32_t ScriptEnviroment::addTempConditionObject(Condition* condition)
  399. {
  400.     m_conditionMap[++m_lastConditionId] = condition;
  401.     return m_lastConditionId;
  402. }
  403.  
  404. Condition* ScriptEnviroment::getConditionObject(uint32_t conditionId)
  405. {
  406.     ConditionMap::iterator it = m_conditionMap.find(conditionId);
  407.     if(it != m_conditionMap.end())
  408.         return it->second;
  409.  
  410.     it = m_tempConditionMap.find(conditionId);
  411.     if(it != m_tempConditionMap.end())
  412.         return it->second;
  413.  
  414.     return NULL;
  415. }
  416.  
  417. void ScriptEnviroment::addTempItem(ScriptEnviroment* env, Item* item)
  418. {
  419.     m_tempItems[env].push_back(item);
  420. }
  421.  
  422. void ScriptEnviroment::removeTempItem(ScriptEnviroment* env, Item* item)
  423. {
  424.     ItemList itemList = m_tempItems[env];
  425.     ItemList::iterator it = std::find(itemList.begin(), itemList.end(), item);
  426.     if(it != itemList.end())
  427.         itemList.erase(it);
  428. }
  429.  
  430. void ScriptEnviroment::removeTempItem(Item* item)
  431. {
  432.     for(TempItemListMap::iterator mit = m_tempItems.begin(); mit != m_tempItems.end(); ++mit)
  433.     {
  434.         ItemList itemList = mit->second;
  435.         ItemList::iterator it = std::find(itemList.begin(), itemList.end(), item);
  436.         if(it != itemList.end())
  437.             itemList.erase(it);
  438.     }
  439. }
  440.  
  441. uint32_t ScriptEnviroment::addResult(DBResult* res)
  442. {
  443.     uint32_t lastId = 0;
  444.     while(m_tempResults.find(lastId) != m_tempResults.end())
  445.         lastId++;
  446.  
  447.     m_tempResults[lastId] = res;
  448.     return lastId;
  449. }
  450.  
  451. bool ScriptEnviroment::removeResult(uint32_t id)
  452. {
  453.     DBResultMap::iterator it = m_tempResults.find(id);
  454.     if(it == m_tempResults.end())
  455.         return false;
  456.  
  457.     if(it->second)
  458.         it->second->free();
  459.  
  460.     m_tempResults.erase(it);
  461.     return true;
  462. }
  463.  
  464. DBResult* ScriptEnviroment::getResultByID(uint32_t id)
  465. {
  466.     DBResultMap::iterator it = m_tempResults.find(id);
  467.     if(it != m_tempResults.end())
  468.         return it->second;
  469.  
  470.     return NULL;
  471. }
  472.  
  473. bool ScriptEnviroment::getStorage(const uint32_t key, std::string& value) const
  474. {
  475.     StorageMap::const_iterator it = m_storageMap.find(key);
  476.     if(it != m_storageMap.end())
  477.     {
  478.         value = it->second;
  479.         return true;
  480.     }
  481.  
  482.     value = "-1";
  483.     return false;
  484. }
  485.  
  486. void ScriptEnviroment::streamVariant(std::stringstream& stream, const std::string& local, const LuaVariant& var)
  487. {
  488.     if(!local.empty())
  489.         stream << "local " << local << " = {" << std::endl;
  490.  
  491.     stream << "type = " << var.type;
  492.     switch(var.type)
  493.     {
  494.         case VARIANT_NUMBER:
  495.             stream << "," << std::endl << "number = " << var.number;
  496.             break;
  497.         case VARIANT_STRING:
  498.             stream << "," << std::endl << "string = \"" << var.text << "\"";
  499.             break;
  500.         case VARIANT_TARGETPOSITION:
  501.         case VARIANT_POSITION:
  502.         {
  503.             stream << "," << std::endl;
  504.             streamPosition(stream, "pos", var.pos);
  505.             break;
  506.         }
  507.         case VARIANT_NONE:
  508.         default:
  509.             break;
  510.     }
  511.  
  512.     if(!local.empty())
  513.         stream << std::endl << "}" << std::endl;
  514. }
  515.  
  516. void ScriptEnviroment::streamThing(std::stringstream& stream, const std::string& local, Thing* thing, uint32_t id/* = 0*/)
  517. {
  518.     if(!local.empty())
  519.         stream << "local " << local << " = {" << std::endl;
  520.  
  521.     if(thing && thing->getItem())
  522.     {
  523.         const Item* item = thing->getItem();
  524.         if(!id)
  525.             id = addThing(thing);
  526.  
  527.         stream << "uid = " << id << "," << std::endl;
  528.         stream << "itemid = " << item->getID() << "," << std::endl;
  529.         if(item->hasSubType())
  530.             stream << "type = " << item->getSubType() << "," << std::endl;
  531.         else
  532.             stream << "type = 0," << std::endl;
  533.  
  534.         stream << "actionid = " << item->getActionId() << std::endl;
  535.     }
  536.     else if(thing && thing->getCreature())
  537.     {
  538.         const Creature* creature = thing->getCreature();
  539.         if(!id)
  540.             id = creature->getID();
  541.  
  542.         stream << "uid = " << id << "," << std::endl;
  543.         stream << "itemid = 1," << std::endl;
  544.         if(creature->getPlayer())
  545.             stream << "type = 1," << std::endl;
  546.         else if(creature->getMonster())
  547.             stream << "type = 2," << std::endl;
  548.         else
  549.             stream << "type = 3," << std::endl;
  550.  
  551.         if(const Player* player = creature->getPlayer())
  552.             stream << "actionid = " << player->getGUID() << "," << std::endl;
  553.         else
  554.             stream << "actionid = 0" << std::endl;
  555.     }
  556.     else
  557.     {
  558.         stream << "uid = 0," << std::endl;
  559.         stream << "itemid = 0," << std::endl;
  560.         stream << "type = 0," << std::endl;
  561.         stream << "actionid = 0" << std::endl;
  562.     }
  563.  
  564.     if(!local.empty())
  565.         stream << "}" << std::endl;
  566. }
  567.  
  568. void ScriptEnviroment::streamPosition(std::stringstream& stream, const std::string& local, const Position& position, uint32_t stackpos)
  569. {
  570.     if(!local.empty())
  571.         stream << "local " << local << " = {" << std::endl;
  572.  
  573.     stream << "x = " << position.x << "," << std::endl;
  574.     stream << "y = " << position.y << "," << std::endl;
  575.     stream << "z = " << position.z << "," << std::endl;
  576.  
  577.     stream << "stackpos = " << stackpos << std::endl;
  578.     if(!local.empty())
  579.         stream << "}" << std::endl;
  580. }
  581.  
  582. void ScriptEnviroment::streamOutfit(std::stringstream& stream, const std::string& local, const Outfit_t& outfit)
  583. {
  584.     if(!local.empty())
  585.         stream << "local " << local << " = {" << std::endl;
  586.  
  587.     stream << "lookType = " << outfit.lookType << "," << std::endl;
  588.     stream << "lookTypeEx = " << outfit.lookTypeEx << "," << std::endl;
  589.  
  590.     stream << "lookHead = " << outfit.lookHead << "," << std::endl;
  591.     stream << "lookBody = " << outfit.lookBody << "," << std::endl;
  592.     stream << "lookLegs = " << outfit.lookLegs << "," << std::endl;
  593.     stream << "lookFeet = " << outfit.lookFeet << "," << std::endl;
  594.  
  595.     stream << "lookAddons = " << outfit.lookAddons << std::endl;
  596.     if(!local.empty())
  597.         stream << "}" << std::endl;
  598. }
  599.  
  600. std::string LuaInterface::getError(ErrorCode_t code)
  601. {
  602.     switch(code)
  603.     {
  604.         case LUA_ERROR_PLAYER_NOT_FOUND:
  605.             return "Player not found";
  606.         case LUA_ERROR_MONSTER_NOT_FOUND:
  607.             return "Monster not found";
  608.         case LUA_ERROR_NPC_NOT_FOUND:
  609.             return "NPC not found";
  610.         case LUA_ERROR_CREATURE_NOT_FOUND:
  611.             return "Creature not found";
  612.         case LUA_ERROR_ITEM_NOT_FOUND:
  613.             return "Item not found";
  614.         case LUA_ERROR_THING_NOT_FOUND:
  615.             return "Thing not found";
  616.         case LUA_ERROR_TILE_NOT_FOUND:
  617.             return "Tile not found";
  618.         case LUA_ERROR_HOUSE_NOT_FOUND:
  619.             return "House not found";
  620.         case LUA_ERROR_COMBAT_NOT_FOUND:
  621.             return "Combat not found";
  622.         case LUA_ERROR_CONDITION_NOT_FOUND:
  623.             return "Condition not found";
  624.         case LUA_ERROR_AREA_NOT_FOUND:
  625.             return "Area not found";
  626.         case LUA_ERROR_CONTAINER_NOT_FOUND:
  627.             return "Container not found";
  628.         case LUA_ERROR_VARIANT_NOT_FOUND:
  629.             return "Variant not found";
  630.         case LUA_ERROR_VARIANT_UNKNOWN:
  631.             return "Unknown variant type";
  632.         case LUA_ERROR_SPELL_NOT_FOUND:
  633.             return "Spell not found";
  634.         default:
  635.             break;
  636.     }
  637.  
  638.     return "Invalid error code!";
  639. }
  640.  
  641. ScriptEnviroment LuaInterface::m_scriptEnv[21];
  642. int32_t LuaInterface::m_scriptEnvIndex = -1;
  643.  
  644. LuaInterface::LuaInterface(std::string interfaceName)
  645. {
  646.     m_luaState = NULL;
  647.     m_interfaceName = interfaceName;
  648.     m_lastTimer = 1000;
  649.     m_errors = true;
  650. }
  651.  
  652. LuaInterface::~LuaInterface()
  653. {
  654.     for(LuaTimerEvents::iterator it = m_timerEvents.begin(); it != m_timerEvents.end(); ++it)
  655.         Scheduler::getInstance().stopEvent(it->second.eventId);
  656.  
  657.     closeState();
  658. }
  659.  
  660. bool LuaInterface::reInitState()
  661. {
  662.     closeState();
  663.     return initState();
  664. }
  665.  
  666. bool LuaInterface::loadBuffer(const std::string& text, Npc* npc/* = NULL*/)
  667. {
  668.     //loads buffer as a chunk at stack top
  669.     int32_t ret = luaL_loadbuffer(m_luaState, text.c_str(), text.length(), "LuaInterface::loadBuffer");
  670.     if(ret)
  671.     {
  672.         m_lastError = popString(m_luaState);
  673.         error(NULL, m_lastError);
  674.         return false;
  675.     }
  676.  
  677.     //check that it is loaded as a function
  678.     if(!lua_isfunction(m_luaState, -1))
  679.         return false;
  680.  
  681.     m_loadingFile = text;
  682.     reserveEnv();
  683.  
  684.     ScriptEnviroment* env = getEnv();
  685.     env->setScriptId(EVENT_ID_LOADING, this);
  686.     env->setNpc(npc);
  687.  
  688.     //execute it
  689.     ret = lua_pcall(m_luaState, 0, 0, 0);
  690.     if(ret)
  691.     {
  692.         error(NULL, popString(m_luaState));
  693.         releaseEnv();
  694.         return false;
  695.     }
  696.  
  697.     releaseEnv();
  698.     return true;
  699. }
  700.  
  701. bool LuaInterface::loadFile(const std::string& file, Npc* npc/* = NULL*/)
  702. {
  703.     //loads file as a chunk at stack top
  704.     int32_t ret = luaL_loadfile(m_luaState, file.c_str());
  705.     if(ret)
  706.     {
  707.         m_lastError = popString(m_luaState);
  708.         std::clog << "[Error - LuaInterface::loadFile] " << m_lastError << std::endl;
  709.         return false;
  710.     }
  711.  
  712.     //check that it is loaded as a function
  713.     if(!lua_isfunction(m_luaState, -1))
  714.         return false;
  715.  
  716.     m_loadingFile = file;
  717.     reserveEnv();
  718.  
  719.     ScriptEnviroment* env = getEnv();
  720.     env->setScriptId(EVENT_ID_LOADING, this);
  721.     env->setNpc(npc);
  722.  
  723.     //execute it
  724.     ret = lua_pcall(m_luaState, 0, 0, 0);
  725.     if(ret)
  726.     {
  727.         error(NULL, popString(m_luaState));
  728.         releaseEnv();
  729.         return false;
  730.     }
  731.  
  732.     releaseEnv();
  733.     return true;
  734. }
  735.  
  736. bool LuaInterface::loadDirectory(const std::string& dir, Npc* npc/* = NULL*/)
  737. {
  738.     StringVec files;
  739.     for(boost::filesystem::directory_iterator it(dir), end; it != end; ++it)
  740.     {
  741.         std::string s = BOOST_DIR_ITER_FILENAME(it);
  742.         if(!boost::filesystem::is_directory(it->status()) && (s.size() > 4 ? s.substr(s.size() - 4) : "") == ".lua")
  743.             files.push_back(s);
  744.     }
  745.  
  746.     std::sort(files.begin(), files.end());
  747.     for(StringVec::iterator it = files.begin(); it != files.end(); ++it)
  748.     {
  749.         if(!loadFile(dir + (*it), npc))
  750.             return false;
  751.     }
  752.  
  753.     return true;
  754. }
  755.  
  756. int32_t LuaInterface::getEvent(const std::string& eventName)
  757. {
  758.     //get our events table
  759.     lua_getfield(m_luaState, LUA_REGISTRYINDEX, "EVENTS");
  760.     if(!lua_istable(m_luaState, -1))
  761.     {
  762.         lua_pop(m_luaState, 1);
  763.         return -1;
  764.     }
  765.  
  766.     //get current event function pointer
  767.     lua_getglobal(m_luaState, eventName.c_str());
  768.     if(!lua_isfunction(m_luaState, -1))
  769.     {
  770.         lua_pop(m_luaState, 1);
  771.         return -1;
  772.     }
  773.  
  774.     //save in our events table
  775.     lua_pushnumber(m_luaState, m_runningEvent);
  776.     lua_pushvalue(m_luaState, -2);
  777.  
  778.     lua_rawset(m_luaState, -4);
  779.     lua_pop(m_luaState, 2);
  780.  
  781.     //reset global value of this event
  782.     lua_pushnil(m_luaState);
  783.     lua_setglobal(m_luaState, eventName.c_str());
  784.  
  785.     m_cacheFiles[m_runningEvent] = m_loadingFile + ":" + eventName;
  786.     ++m_runningEvent;
  787.     return m_runningEvent - 1;
  788. }
  789.  
  790. std::string LuaInterface::getScript(int32_t scriptId)
  791. {
  792.     const static std::string tmp = "(Unknown script file)";
  793.     if(scriptId != EVENT_ID_LOADING)
  794.     {
  795.         ScriptsCache::iterator it = m_cacheFiles.find(scriptId);
  796.         if(it != m_cacheFiles.end())
  797.             return it->second;
  798.  
  799.         return tmp;
  800.     }
  801.  
  802.     return m_loadingFile;
  803. }
  804.  
  805. void LuaInterface::error(const char* function, const std::string& desc)
  806. {
  807.     int32_t script, callback;
  808.     bool timer;
  809.     std::string event;
  810.  
  811.     LuaInterface* interface;
  812.     getEnv()->getInfo(script, event, interface, callback, timer);
  813.     if(interface)
  814.     {
  815.         if(!interface->m_errors)
  816.             return;
  817.  
  818.         std::clog << std::endl << "[Error - " << interface->getName() << "] " << std::endl;
  819.         if(callback)
  820.             std::clog << "In a callback: " << interface->getScript(callback) << std::endl;
  821.  
  822.         if(timer)
  823.             std::clog << (callback ? "from" : "In") << " a timer event called from: " << std::endl;
  824.  
  825.         std::clog << interface->getScript(script) << std::endl << "Description: ";
  826.     }
  827.     else
  828.         std::clog << std::endl << "[Lua Error] ";
  829.  
  830.     std::clog << event << std::endl;
  831.     if(function)
  832.         std::clog << "(" << function << ") ";
  833.  
  834.     std::clog << desc << std::endl;
  835. }
  836.  
  837. bool LuaInterface::pushFunction(int32_t function)
  838. {
  839.     lua_getfield(m_luaState, LUA_REGISTRYINDEX, "EVENTS");
  840.     if(lua_istable(m_luaState, -1))
  841.     {
  842.         lua_pushnumber(m_luaState, function);
  843.         lua_rawget(m_luaState, -2);
  844.  
  845.         lua_remove(m_luaState, -2);
  846.         if(lua_isfunction(m_luaState, -1))
  847.             return true;
  848.     }
  849.  
  850.     return false;
  851. }
  852.  
  853. bool LuaInterface::initState()
  854. {
  855.     m_luaState = luaL_newstate();
  856.     if(!m_luaState)
  857.         return false;
  858.  
  859.     luaL_openlibs(m_luaState);
  860. #ifdef __LUAJIT__
  861.     luaJIT_setmode(m_luaState, 0, LUAJIT_MODE_ENGINE | LUAJIT_MODE_ON);
  862. #endif
  863.  
  864.     registerFunctions();
  865.     if(!loadDirectory(getFilePath(FILE_TYPE_OTHER, "lib/"), NULL))
  866.         std::clog << "[Warning - LuaInterface::initState] Cannot load " << getFilePath(FILE_TYPE_OTHER, "lib/") << std::endl;
  867.  
  868.     lua_newtable(m_luaState);
  869.     lua_setfield(m_luaState, LUA_REGISTRYINDEX, "EVENTS");
  870.     m_runningEvent = EVENT_ID_USER;
  871.     return true;
  872. }
  873.  
  874. bool LuaInterface::closeState()
  875. {
  876.     if(!m_luaState)
  877.         return false;
  878.  
  879.     m_cacheFiles.clear();
  880.     for(LuaTimerEvents::iterator it = m_timerEvents.begin(); it != m_timerEvents.end(); ++it)
  881.     {
  882.         for(std::list<int32_t>::iterator lt = it->second.parameters.begin(); lt != it->second.parameters.end(); ++lt)
  883.             luaL_unref(m_luaState, LUA_REGISTRYINDEX, *lt);
  884.  
  885.         it->second.parameters.clear();
  886.         luaL_unref(m_luaState, LUA_REGISTRYINDEX, it->second.function);
  887.     }
  888.  
  889.     m_timerEvents.clear();
  890.     lua_close(m_luaState);
  891.     return true;
  892. }
  893.  
  894. void LuaInterface::executeTimer(uint32_t eventIndex)
  895. {
  896.     LuaTimerEvents::iterator it = m_timerEvents.find(eventIndex);
  897.     if(it != m_timerEvents.end())
  898.     {
  899.         //push function
  900.         lua_rawgeti(m_luaState, LUA_REGISTRYINDEX, it->second.function);
  901.  
  902.         //push parameters
  903.         for(std::list<int32_t>::reverse_iterator rt = it->second.parameters.rbegin(); rt != it->second.parameters.rend(); ++rt)
  904.             lua_rawgeti(m_luaState, LUA_REGISTRYINDEX, *rt);
  905.  
  906.         //call the function
  907.         if(reserveEnv())
  908.         {
  909.             ScriptEnviroment* env = getEnv();
  910.             env->setTimerEvent();
  911.             env->setScriptId(it->second.scriptId, this);
  912.  
  913.             callFunction(it->second.parameters.size());
  914.             releaseEnv();
  915.         }
  916.         else
  917.             std::clog << "[Error - LuaInterface::executeTimer] Call stack overflow." << std::endl;
  918.  
  919.         //free resources
  920.         for(std::list<int32_t>::iterator lt = it->second.parameters.begin(); lt != it->second.parameters.end(); ++lt)
  921.             luaL_unref(m_luaState, LUA_REGISTRYINDEX, *lt);
  922.  
  923.         it->second.parameters.clear();
  924.         luaL_unref(m_luaState, LUA_REGISTRYINDEX, it->second.function);
  925.         m_timerEvents.erase(it);
  926.     }
  927. }
  928.  
  929. int32_t LuaInterface::handleFunction(lua_State* L)
  930. {
  931.     lua_getfield(L, LUA_GLOBALSINDEX, "debug");
  932.     if(!lua_istable(L, -1))
  933.     {
  934.         lua_pop(L, 1);
  935.         return 1;
  936.     }
  937.  
  938.     lua_getfield(L, -1, "traceback");
  939.     if(!lua_isfunction(L, -1))
  940.     {
  941.         lua_pop(L, 2);
  942.         return 1;
  943.     }
  944.  
  945.     lua_pushvalue(L, 1);
  946.     lua_pushinteger(L, 2);
  947.  
  948.     lua_call(L, 2, 1);
  949.     return 1;
  950. }
  951.  
  952. bool LuaInterface::callFunction(uint32_t params)
  953. {
  954.     int32_t size = lua_gettop(m_luaState), handler = lua_gettop(m_luaState) - params;
  955.     lua_pushcfunction(m_luaState, handleFunction);
  956.  
  957.     bool result = false;
  958.     lua_insert(m_luaState, handler);
  959.     if(lua_pcall(m_luaState, params, 1, handler))
  960.         LuaInterface::error(NULL, LuaInterface::popString(m_luaState));
  961.     else
  962.         result = (int32_t)LuaInterface::popBoolean(m_luaState);
  963.  
  964.     lua_remove(m_luaState, handler);
  965.     if((lua_gettop(m_luaState) + (int32_t)params + 1) != size)
  966.         LuaInterface::error(NULL, "Stack size changed!");
  967.  
  968.     return result;
  969. }
  970.  
  971. void LuaInterface::dumpStack(lua_State* L/* = NULL*/)
  972. {
  973.     if(!L)
  974.         L = m_luaState;
  975.  
  976.     int32_t stack = lua_gettop(L);
  977.     if(!stack)
  978.         return;
  979.  
  980.     std::clog << "Stack size: " << stack << std::endl;
  981.     for(int32_t i = 1; i <= stack ; ++i)
  982.         std::clog << lua_typename(m_luaState, lua_type(m_luaState, -i)) << " " << lua_topointer(m_luaState, -i) << std::endl;
  983. }
  984.  
  985. void LuaInterface::pushVariant(lua_State* L, const LuaVariant& var)
  986. {
  987.     lua_newtable(L);
  988.     setField(L, "type", var.type);
  989.     switch(var.type)
  990.     {
  991.         case VARIANT_NUMBER:
  992.             setField(L, "number", var.number);
  993.             break;
  994.         case VARIANT_STRING:
  995.             setField(L, "string", var.text);
  996.             break;
  997.         case VARIANT_TARGETPOSITION:
  998.         case VARIANT_POSITION:
  999.         {
  1000.             lua_pushstring(L, "pos");
  1001.             pushPosition(L, var.pos);
  1002.             pushTable(L);
  1003.             break;
  1004.         }
  1005.         case VARIANT_NONE:
  1006.             break;
  1007.     }
  1008. }
  1009.  
  1010. void LuaInterface::pushThing(lua_State* L, Thing* thing, uint32_t id/* = 0*/)
  1011. {
  1012.     lua_newtable(L);
  1013.     if(thing && thing->getItem())
  1014.     {
  1015.         const Item* item = thing->getItem();
  1016.         if(!id)
  1017.             id = getEnv()->addThing(thing);
  1018.  
  1019.         setField(L, "uid", id);
  1020.         setField(L, "itemid", item->getID());
  1021.         if(item->hasSubType())
  1022.             setField(L, "type", item->getSubType());
  1023.         else
  1024.             setField(L, "type", 0);
  1025.  
  1026.         setField(L, "actionid", item->getActionId());
  1027.     }
  1028.     else if(thing && thing->getCreature())
  1029.     {
  1030.         const Creature* creature = thing->getCreature();
  1031.         if(!id)
  1032.             id = creature->getID();
  1033.  
  1034.         setField(L, "uid", id);
  1035.         setField(L, "itemid", 1);
  1036.         if(creature->getPlayer())
  1037.             setField(L, "type", 1);
  1038.         else if(creature->getMonster())
  1039.             setField(L, "type", 2);
  1040.         else
  1041.             setField(L, "type", 3);
  1042.  
  1043.         if(const Player* player = creature->getPlayer())
  1044.             setField(L, "actionid", player->getGUID());
  1045.         else
  1046.             setField(L, "actionid", 0);
  1047.     }
  1048.     else
  1049.     {
  1050.         setField(L, "uid", 0);
  1051.         setField(L, "itemid", 0);
  1052.         setField(L, "type", 0);
  1053.         setField(L, "actionid", 0);
  1054.     }
  1055. }
  1056.  
  1057. void LuaInterface::pushPosition(lua_State* L, const Position& position, uint32_t stackpos)
  1058. {
  1059.     lua_newtable(L);
  1060.     setField(L, "x", position.x);
  1061.     setField(L, "y", position.y);
  1062.     setField(L, "z", position.z);
  1063.     setField(L, "stackpos", stackpos);
  1064. }
  1065.  
  1066. void LuaInterface::pushOutfit(lua_State* L, const Outfit_t& outfit)
  1067. {
  1068.     lua_newtable(L);
  1069.     setField(L, "lookType", outfit.lookType);
  1070.     setField(L, "lookTypeEx", outfit.lookTypeEx);
  1071.     setField(L, "lookHead", outfit.lookHead);
  1072.     setField(L, "lookBody", outfit.lookBody);
  1073.     setField(L, "lookLegs", outfit.lookLegs);
  1074.     setField(L, "lookFeet", outfit.lookFeet);
  1075.     setField(L, "lookAddons", outfit.lookAddons);
  1076. }
  1077.  
  1078. void LuaInterface::pushCallback(lua_State* L, int32_t callback)
  1079. {
  1080.     lua_rawgeti(L, LUA_REGISTRYINDEX, callback);
  1081. }
  1082.  
  1083. LuaVariant LuaInterface::popVariant(lua_State* L)
  1084. {
  1085.     LuaVariant var;
  1086.     var.type = (LuaVariantType_t)getField(L, "type");
  1087.     switch(var.type)
  1088.     {
  1089.         case VARIANT_NUMBER:
  1090.             var.number = getFieldUnsigned(L, "number");
  1091.             break;
  1092.         case VARIANT_STRING:
  1093.             var.text = getField(L, "string");
  1094.             break;
  1095.         case VARIANT_POSITION:
  1096.         case VARIANT_TARGETPOSITION:
  1097.         {
  1098.             lua_pushstring(L, "pos");
  1099.             lua_gettable(L, -2);
  1100.             popPosition(L, var.pos);
  1101.             break;
  1102.         }
  1103.         default:
  1104.             var.type = VARIANT_NONE;
  1105.             break;
  1106.     }
  1107.  
  1108.     lua_pop(L, 1); //table
  1109.     return var;
  1110. }
  1111.  
  1112. void LuaInterface::popPosition(lua_State* L, PositionEx& position)
  1113. {
  1114.     if(!lua_isboolean(L, -1))
  1115.     {
  1116.         position.x = getField(L, "x");
  1117.         position.y = getField(L, "y");
  1118.         position.z = getField(L, "z");
  1119.         position.stackpos = getField(L, "stackpos");
  1120.     }
  1121.     else
  1122.         position = PositionEx();
  1123.  
  1124.     lua_pop(L, 1); //table
  1125. }
  1126.  
  1127. void LuaInterface::popPosition(lua_State* L, Position& position, uint32_t& stackpos)
  1128. {
  1129.     stackpos = 0;
  1130.     if(!lua_isboolean(L, -1))
  1131.     {
  1132.         position.x = getField(L, "x");
  1133.         position.y = getField(L, "y");
  1134.         position.z = getField(L, "z");
  1135.         stackpos = getField(L, "stackpos");
  1136.     }
  1137.     else
  1138.         position = Position();
  1139.  
  1140.     lua_pop(L, 1); //table
  1141. }
  1142.  
  1143. bool LuaInterface::popBoolean(lua_State* L)
  1144. {
  1145.     lua_pop(L, 1);
  1146.     return lua_toboolean(L, 0);
  1147. }
  1148.  
  1149. int64_t LuaInterface::popNumber(lua_State* L)
  1150. {
  1151.     lua_pop(L, 1);
  1152.     if(lua_isboolean(L, 0))
  1153.         return (int64_t)lua_toboolean(L, 0);
  1154.  
  1155.     return (int64_t)lua_tonumber(L, 0);
  1156. }
  1157.  
  1158. double LuaInterface::popFloatNumber(lua_State* L)
  1159. {
  1160.     lua_pop(L, 1);
  1161.     return lua_tonumber(L, 0);
  1162. }
  1163.  
  1164. std::string LuaInterface::popString(lua_State* L)
  1165. {
  1166.     lua_pop(L, 1);
  1167.     if(!lua_isstring(L, 0) && !lua_isnumber(L, 0))
  1168.         return std::string();
  1169.  
  1170.     const char* str = lua_tostring(L, 0);
  1171.     if(!str || !strlen(str))
  1172.         return std::string();
  1173.  
  1174.     return str;
  1175. }
  1176.  
  1177. int32_t LuaInterface::popCallback(lua_State* L)
  1178. {
  1179.     return luaL_ref(L, LUA_REGISTRYINDEX);
  1180. }
  1181.  
  1182. Outfit_t LuaInterface::popOutfit(lua_State* L)
  1183. {
  1184.     Outfit_t outfit;
  1185.     outfit.lookAddons = getField(L, "lookAddons");
  1186.  
  1187.     outfit.lookFeet = getField(L, "lookFeet");
  1188.     outfit.lookLegs = getField(L, "lookLegs");
  1189.     outfit.lookBody = getField(L, "lookBody");
  1190.     outfit.lookHead = getField(L, "lookHead");
  1191.  
  1192.     outfit.lookTypeEx = getField(L, "lookTypeEx");
  1193.     outfit.lookType = getField(L, "lookType");
  1194.  
  1195.     lua_pop(L, 1); //table
  1196.     return outfit;
  1197. }
  1198.  
  1199. void LuaInterface::setField(lua_State* L, const char* index, int32_t val)
  1200. {
  1201.     lua_pushstring(L, index);
  1202.     lua_pushnumber(L, val);
  1203.     pushTable(L);
  1204. }
  1205.  
  1206. void LuaInterface::setField(lua_State* L, const char* index, const std::string& val)
  1207. {
  1208.     lua_pushstring(L, index);
  1209.     lua_pushstring(L, val.c_str());
  1210.     pushTable(L);
  1211. }
  1212.  
  1213. void LuaInterface::setFieldBool(lua_State* L, const char* index, bool val)
  1214. {
  1215.     lua_pushstring(L, index);
  1216.     lua_pushboolean(L, val);
  1217.     pushTable(L);
  1218. }
  1219.  
  1220. void LuaInterface::setFieldFloat(lua_State* L, const char* index, double val)
  1221. {
  1222.     lua_pushstring(L, index);
  1223.     lua_pushnumber(L, val);
  1224.     pushTable(L);
  1225. }
  1226.  
  1227. void LuaInterface::createTable(lua_State* L, const char* index)
  1228. {
  1229.     lua_pushstring(L, index);
  1230.     lua_newtable(L);
  1231. }
  1232.  
  1233. void LuaInterface::createTable(lua_State* L, const char* index, int32_t narr, int32_t nrec)
  1234. {
  1235.     lua_pushstring(L, index);
  1236.     lua_createtable(L, narr, nrec);
  1237. }
  1238.  
  1239. void LuaInterface::createTable(lua_State* L, int32_t index)
  1240. {
  1241.     lua_pushnumber(L, index);
  1242.     lua_newtable(L);
  1243. }
  1244.  
  1245. void LuaInterface::createTable(lua_State* L, int32_t index, int32_t narr, int32_t nrec)
  1246. {
  1247.     lua_pushnumber(L, index);
  1248.     lua_createtable(L, narr, nrec);
  1249. }
  1250.  
  1251. void LuaInterface::pushTable(lua_State* L)
  1252. {
  1253.     lua_settable(L, -3);
  1254. }
  1255.  
  1256. int64_t LuaInterface::getField(lua_State* L, const char* key)
  1257. {
  1258.     lua_pushstring(L, key);
  1259.     lua_gettable(L, -2); // get table[key]
  1260.  
  1261.     int64_t result = (int64_t)lua_tonumber(L, -1);
  1262.     lua_pop(L, 1); // remove number and key
  1263.     return result;
  1264. }
  1265.  
  1266. uint64_t LuaInterface::getFieldUnsigned(lua_State* L, const char* key)
  1267. {
  1268.     lua_pushstring(L, key);
  1269.     lua_gettable(L, -2); // get table[key]
  1270.  
  1271.     uint64_t result = (uint64_t)lua_tonumber(L, -1);
  1272.     lua_pop(L, 1); // remove number and key
  1273.     return result;
  1274. }
  1275.  
  1276. bool LuaInterface::getFieldBool(lua_State* L, const char* key)
  1277. {
  1278.     lua_pushstring(L, key);
  1279.     lua_gettable(L, -2); // get table[key]
  1280.  
  1281.     bool result = lua_toboolean(L, -1);
  1282.     lua_pop(L, 1); // remove number and key
  1283.     return result;
  1284. }
  1285.  
  1286. std::string LuaInterface::getFieldString(lua_State* L, const char* key)
  1287. {
  1288.     lua_pushstring(L, key);
  1289.     lua_gettable(L, -2); // get table[key]
  1290.  
  1291.     std::string result = lua_tostring(L, -1);
  1292.     lua_pop(L, 1); // remove number and key
  1293.     return result;
  1294. }
  1295.  
  1296. std::string LuaInterface::getGlobalString(lua_State* L, const std::string& _identifier, const std::string& _default/* = ""*/)
  1297. {
  1298.     lua_getglobal(L, _identifier.c_str());
  1299.     if(!lua_isstring(L, -1))
  1300.     {
  1301.         lua_pop(L, 1);
  1302.         return _default;
  1303.     }
  1304.  
  1305.     int32_t len = (int32_t)lua_strlen(L, -1);
  1306.     std::string ret(lua_tostring(L, -1), len);
  1307.  
  1308.     lua_pop(L, 1);
  1309.     return ret;
  1310. }
  1311.  
  1312. bool LuaInterface::getGlobalBool(lua_State* L, const std::string& _identifier, bool _default/* = false*/)
  1313. {
  1314.     lua_getglobal(L, _identifier.c_str());
  1315.     if(!lua_isboolean(L, -1))
  1316.     {
  1317.         lua_pop(L, 1);
  1318.         return booleanString(LuaInterface::getGlobalString(L, _identifier, _default ? "yes" : "no"));
  1319.     }
  1320.  
  1321.     bool val = lua_toboolean(L, -1);
  1322.     lua_pop(L, 1);
  1323.     return val;
  1324. }
  1325.  
  1326. int32_t LuaInterface::getGlobalNumber(lua_State* L, const std::string& _identifier, const int32_t _default/* = 0*/)
  1327. {
  1328.     return (int32_t)LuaInterface::getGlobalDouble(L, _identifier, _default);
  1329. }
  1330.  
  1331. double LuaInterface::getGlobalDouble(lua_State* L, const std::string& _identifier, const double _default/* = 0*/)
  1332. {
  1333.     lua_getglobal(L, _identifier.c_str());
  1334.     if(!lua_isnumber(L, -1))
  1335.     {
  1336.         lua_pop(L, 1);
  1337.         return _default;
  1338.     }
  1339.  
  1340.     double val = lua_tonumber(L, -1);
  1341.     lua_pop(L, 1);
  1342.     return val;
  1343. }
  1344.  
  1345. void LuaInterface::getValue(const std::string& key, lua_State* L, lua_State* _L)
  1346. {
  1347.     lua_getglobal(L, key.c_str());
  1348.     moveValue(L, _L);
  1349. }
  1350.  
  1351. void LuaInterface::moveValue(lua_State* from, lua_State* to)
  1352. {
  1353.     switch(lua_type(from, -1))
  1354.     {
  1355.         case LUA_TNIL:
  1356.             lua_pushnil(to);
  1357.             break;
  1358.         case LUA_TBOOLEAN:
  1359.             lua_pushboolean(to, lua_toboolean(from, -1));
  1360.             break;
  1361.         case LUA_TNUMBER:
  1362.             lua_pushnumber(to, lua_tonumber(from, -1));
  1363.             break;
  1364.         case LUA_TSTRING:
  1365.         {
  1366.             size_t len;
  1367.             const char* str = lua_tolstring(from, -1, &len);
  1368.  
  1369.             lua_pushlstring(to, str, len);
  1370.             break;
  1371.         }
  1372.         case LUA_TTABLE:
  1373.         {
  1374.             lua_newtable(to);
  1375.             lua_pushnil(from); // First key
  1376.             while(lua_next(from, -2))
  1377.             {
  1378.                 // Move value to the other state
  1379.                 moveValue(from, to); // Value is popped, key is left
  1380.                 // Move key to the other state
  1381.                 lua_pushvalue(from, -1); // Make a copy of the key to use for the next iteration
  1382.                 moveValue(from, to); // Key is in other state.
  1383.                 // We still have the key in the 'from' state ontop of the stack
  1384.  
  1385.                 lua_insert(to, -2); // Move key above value
  1386.                 pushTable(to); // Set the key
  1387.             }
  1388.  
  1389.             break;
  1390.         }
  1391.         default:
  1392.             break;
  1393.     }
  1394.  
  1395.     lua_pop(from, 1); // Pop the value we just read
  1396. }
  1397.  
  1398. void LuaInterface::registerFunctions()
  1399. {
  1400.     //example(...)
  1401.     //lua_register(L, "name", C_function);
  1402.    
  1403.     //getPlayersOnline()
  1404.     lua_register(m_luaState, "getCastsOnline", LuaInterface::luaGetCastsOnline);
  1405.        
  1406.     //doPlayerSetCastDescription(cid, desc)
  1407.     lua_register(m_luaState, "doPlayerSetCastDescription", LuaInterface::luaDoPlayerSetCastDescription);
  1408.  
  1409.     //doPlayerAddCastMute(cid, ip)
  1410.     lua_register(m_luaState, "doPlayerAddCastMute", LuaInterface::luaDoPlayerAddCastMute);
  1411.  
  1412.     //doPlayerRemoveCastMute(cidl, ip)
  1413.     lua_register(m_luaState, "doPlayerRemoveCastMute", LuaInterface::luaDoPlayerRemoveCastMute);
  1414.  
  1415.     //doPlayerGetCastMutes(cid)
  1416.     lua_register(m_luaState, "getCastMutes", LuaInterface::luaGetPlayerCastMutes);
  1417.  
  1418.     //doPlayerAddCastBan(cid, ip)
  1419.     lua_register(m_luaState, "doPlayerAddCastBan", LuaInterface::luaDoPlayerAddCastBan);
  1420.  
  1421.     //doPlayerRemoveCastBan(cidl, ip)
  1422.     lua_register(m_luaState, "doPlayerRemoveCastBan", LuaInterface::luaDoPlayerRemoveCastBan);
  1423.  
  1424.     //doPlayerGetCastBan(cid)
  1425.     lua_register(m_luaState, "getCastBans", LuaInterface::luaGetPlayerCastBans);
  1426.  
  1427.     //doPlayerAddCastBan(cid, ip)
  1428.     lua_register(m_luaState, "getCastViewers", LuaInterface::luaGetPlayerCastViewers);
  1429.  
  1430.     //doPlayerSetCastPassword(cid, password)
  1431.     lua_register(m_luaState, "doPlayerSetCastPassword", LuaInterface::luaDoPlayerSetCastPassword);
  1432.  
  1433.     //getPlayerCast(cid)
  1434.     lua_register(m_luaState, "doPlayerSetCastState", LuaInterface::luaDoPlayerSetCastState);
  1435.  
  1436.     //getPlayerCast(cid)
  1437.     lua_register(m_luaState, "getPlayerCast", LuaInterface::luaGetPlayerCast);
  1438.  
  1439.     //getCreatureHealth(cid)
  1440.     lua_register(m_luaState, "getCreatureHealth", LuaInterface::luaGetCreatureHealth);
  1441.  
  1442.     //getCreatureMaxHealth(cid)
  1443.     lua_register(m_luaState, "getCreatureMaxHealth", LuaInterface::luaGetCreatureMaxHealth);
  1444.  
  1445.     //getCreatureMana(cid)
  1446.     lua_register(m_luaState, "getCreatureMana", LuaInterface::luaGetCreatureMana);
  1447.  
  1448.     //getCreatureMaxMana(cid)
  1449.     lua_register(m_luaState, "getCreatureMaxMana", LuaInterface::luaGetCreatureMaxMana);
  1450.  
  1451.     //getCreatureHideHealth(cid)
  1452.     lua_register(m_luaState, "getCreatureHideHealth", LuaInterface::luaGetCreatureHideHealth);
  1453.  
  1454.     //doCreatureSetHideHealth(cid, hide)
  1455.     lua_register(m_luaState, "doCreatureSetHideHealth", LuaInterface::luaDoCreatureSetHideHealth);
  1456.  
  1457.     //getCreatureSpeakType(cid)
  1458.     lua_register(m_luaState, "getCreatureSpeakType", LuaInterface::luaGetCreatureSpeakType);
  1459.  
  1460.     //doCreatureSetSpeakType(cid, type)
  1461.     lua_register(m_luaState, "doCreatureSetSpeakType", LuaInterface::luaDoCreatureSetSpeakType);
  1462.  
  1463.     //getCreatureLookDirection(cid)
  1464.     lua_register(m_luaState, "getCreatureLookDirection", LuaInterface::luaGetCreatureLookDirection);
  1465.  
  1466.     //getPlayerLevel(cid)
  1467.     lua_register(m_luaState, "getPlayerLevel", LuaInterface::luaGetPlayerLevel);
  1468.  
  1469.     //getPlayerExperience(cid)
  1470.     lua_register(m_luaState, "getPlayerExperience", LuaInterface::luaGetPlayerExperience);
  1471.  
  1472.     //getPlayerMagLevel(cid[, ignoreBuffs = false])
  1473.     lua_register(m_luaState, "getPlayerMagLevel", LuaInterface::luaGetPlayerMagLevel);
  1474.  
  1475.     //getPlayerSpentMana(cid)
  1476.     lua_register(m_luaState, "getPlayerSpentMana", LuaInterface::luaGetPlayerSpentMana);
  1477.  
  1478.     //getPlayerFood(cid)
  1479.     lua_register(m_luaState, "getPlayerFood", LuaInterface::luaGetPlayerFood);
  1480.  
  1481.     //getPlayerAccess(cid)
  1482.     lua_register(m_luaState, "getPlayerAccess", LuaInterface::luaGetPlayerAccess);
  1483.  
  1484.     //getPlayerGhostAccess(cid)
  1485.     lua_register(m_luaState, "getPlayerGhostAccess", LuaInterface::luaGetPlayerGhostAccess);
  1486.  
  1487.     //getPlayerSkillLevel(cid, skillid)
  1488.     lua_register(m_luaState, "getPlayerSkillLevel", LuaInterface::luaGetPlayerSkillLevel);
  1489.  
  1490.     //getPlayerSkillTries(cid, skillid)
  1491.     lua_register(m_luaState, "getPlayerSkillTries", LuaInterface::luaGetPlayerSkillTries);
  1492.  
  1493.     //getPlayerTown(cid)
  1494.     lua_register(m_luaState, "getPlayerTown", LuaInterface::luaGetPlayerTown);
  1495.  
  1496.     //getPlayerVocation(cid)
  1497.     lua_register(m_luaState, "getPlayerVocation", LuaInterface::luaGetPlayerVocation);
  1498.  
  1499.     //getPlayerIp(cid)
  1500.     lua_register(m_luaState, "getPlayerIp", LuaInterface::luaGetPlayerIp);
  1501.  
  1502.     //getPlayerRequiredMana(cid, magicLevel)
  1503.     lua_register(m_luaState, "getPlayerRequiredMana", LuaInterface::luaGetPlayerRequiredMana);
  1504.  
  1505.     //getPlayerRequiredSkillTries(cid, skillId, skillLevel)
  1506.     lua_register(m_luaState, "getPlayerRequiredSkillTries", LuaInterface::luaGetPlayerRequiredSkillTries);
  1507.  
  1508.     //getPlayerItemCount(cid, itemid[, subType = -1])
  1509.     lua_register(m_luaState, "getPlayerItemCount", LuaInterface::luaGetPlayerItemCount);
  1510.  
  1511.     //getPlayerMoney(cid)
  1512.     lua_register(m_luaState, "getPlayerMoney", LuaInterface::luaGetPlayerMoney);
  1513.  
  1514.     //getPlayerSoul(cid)
  1515.     lua_register(m_luaState, "getPlayerSoul", LuaInterface::luaGetPlayerSoul);
  1516.  
  1517.     //getPlayerFreeCap(cid)
  1518.     lua_register(m_luaState, "getPlayerFreeCap", LuaInterface::luaGetPlayerFreeCap);
  1519.  
  1520.     //getPlayerLight(cid)
  1521.     lua_register(m_luaState, "getPlayerLight", LuaInterface::luaGetPlayerLight);
  1522.  
  1523.     //getPlayerSlotItem(cid, slot)
  1524.     lua_register(m_luaState, "getPlayerSlotItem", LuaInterface::luaGetPlayerSlotItem);
  1525.  
  1526.     //getPlayerWeapon(cid[, ignoreAmmo = false])
  1527.     lua_register(m_luaState, "getPlayerWeapon", LuaInterface::luaGetPlayerWeapon);
  1528.  
  1529.     //getPlayerItemById(cid, deepSearch, itemId[, subType = -1])
  1530.     lua_register(m_luaState, "getPlayerItemById", LuaInterface::luaGetPlayerItemById);
  1531.  
  1532.     //getPlayerDepotItems(cid, depotid)
  1533.     lua_register(m_luaState, "getPlayerDepotItems", LuaInterface::luaGetPlayerDepotItems);
  1534.  
  1535.     //getPlayerGuildId(cid)
  1536.     lua_register(m_luaState, "getPlayerGuildId", LuaInterface::luaGetPlayerGuildId);
  1537.  
  1538.     //getPlayerGuildName(cid)
  1539.     lua_register(m_luaState, "getPlayerGuildName", LuaInterface::luaGetPlayerGuildName);
  1540.  
  1541.     //getPlayerGuildRankId(cid)
  1542.     lua_register(m_luaState, "getPlayerGuildRankId", LuaInterface::luaGetPlayerGuildRankId);
  1543.  
  1544.     //getPlayerGuildRank(cid)
  1545.     lua_register(m_luaState, "getPlayerGuildRank", LuaInterface::luaGetPlayerGuildRank);
  1546.  
  1547.     //getPlayerGuildNick(cid)
  1548.     lua_register(m_luaState, "getPlayerGuildNick", LuaInterface::luaGetPlayerGuildNick);
  1549.  
  1550.     //getPlayerGuildLevel(cid)
  1551.     lua_register(m_luaState, "getPlayerGuildLevel", LuaInterface::luaGetPlayerGuildLevel);
  1552.  
  1553.     //getPlayerGUID(cid)
  1554.     lua_register(m_luaState, "getPlayerGUID", LuaInterface::luaGetPlayerGUID);
  1555.  
  1556.     //getPlayerNameDescription(cid)
  1557.     lua_register(m_luaState, "getPlayerNameDescription", LuaInterface::luaGetPlayerNameDescription);
  1558.  
  1559.     //doPlayerSetNameDescription(cid, desc)
  1560.     lua_register(m_luaState, "doPlayerSetNameDescription", LuaInterface::luaDoPlayerSetNameDescription);
  1561.  
  1562.     //getPlayerSpecialDescription(cid)
  1563.     lua_register(m_luaState, "getPlayerSpecialDescription", LuaInterface::luaGetPlayerSpecialDescription);
  1564.  
  1565.     //doPlayerSetSpecialDescription(cid, desc)
  1566.     lua_register(m_luaState, "doPlayerSetSpecialDescription", LuaInterface::luaDoPlayerSetSpecialDescription);
  1567.  
  1568.     //getPlayerAccountId(cid)
  1569.     lua_register(m_luaState, "getPlayerAccountId", LuaInterface::luaGetPlayerAccountId);
  1570.  
  1571.     //getPlayerAccount(cid)
  1572.     lua_register(m_luaState, "getPlayerAccount", LuaInterface::luaGetPlayerAccount);
  1573.  
  1574.     //getPlayerFlagValue(cid, flag)
  1575.     lua_register(m_luaState, "getPlayerFlagValue", LuaInterface::luaGetPlayerFlagValue);
  1576.  
  1577.     //getPlayerCustomFlagValue(cid, flag)
  1578.     lua_register(m_luaState, "getPlayerCustomFlagValue", LuaInterface::luaGetPlayerCustomFlagValue);
  1579.  
  1580.     //getPlayerPromotionLevel(cid)
  1581.     lua_register(m_luaState, "getPlayerPromotionLevel", LuaInterface::luaGetPlayerPromotionLevel);
  1582.  
  1583.     //doPlayerSetPromotionLevel(cid, level)
  1584.     lua_register(m_luaState, "doPlayerSetPromotionLevel", LuaInterface::luaDoPlayerSetPromotionLevel);
  1585.  
  1586.     //getPlayerGroupId(cid)
  1587.     lua_register(m_luaState, "getPlayerGroupId", LuaInterface::luaGetPlayerGroupId);
  1588.  
  1589.     //doPlayerSetGroupId(cid, newGroupId)
  1590.     lua_register(m_luaState, "doPlayerSetGroupId", LuaInterface::luaDoPlayerSetGroupId);
  1591.  
  1592.     //doPlayerSendOutfitWindow(cid)
  1593.     lua_register(m_luaState, "doPlayerSendOutfitWindow", LuaInterface::luaDoPlayerSendOutfitWindow);
  1594.  
  1595.     //doPlayerLearnInstantSpell(cid, name)
  1596.     lua_register(m_luaState, "doPlayerLearnInstantSpell", LuaInterface::luaDoPlayerLearnInstantSpell);
  1597.  
  1598.     //doPlayerUnlearnInstantSpell(cid, name)
  1599.     lua_register(m_luaState, "doPlayerUnlearnInstantSpell", LuaInterface::luaDoPlayerUnlearnInstantSpell);
  1600.  
  1601.     //getPlayerLearnedInstantSpell(cid, name)
  1602.     lua_register(m_luaState, "getPlayerLearnedInstantSpell", LuaInterface::luaGetPlayerLearnedInstantSpell);
  1603.  
  1604.     //getPlayerInstantSpellCount(cid)
  1605.     lua_register(m_luaState, "getPlayerInstantSpellCount", LuaInterface::luaGetPlayerInstantSpellCount);
  1606.  
  1607.     //getPlayerInstantSpellInfo(cid, index)
  1608.     lua_register(m_luaState, "getPlayerInstantSpellInfo", LuaInterface::luaGetPlayerInstantSpellInfo);
  1609.  
  1610.     //getInstantSpellInfo(cid, name)
  1611.     lua_register(m_luaState, "getInstantSpellInfo", LuaInterface::luaGetInstantSpellInfo);
  1612.  
  1613.     //getCreatureStorage(uid, key)
  1614.     lua_register(m_luaState, "getCreatureStorage", LuaInterface::luaGetCreatureStorage);
  1615.  
  1616.     //doCreatureSetStorage(uid, key, value)
  1617.     lua_register(m_luaState, "doCreatureSetStorage", LuaInterface::luaDoCreatureSetStorage);
  1618.  
  1619.     //getStorage(key)
  1620.     lua_register(m_luaState, "getStorage", LuaInterface::luaGetStorage);
  1621.  
  1622.     //doSetStorage(key, value)
  1623.     lua_register(m_luaState, "doSetStorage", LuaInterface::luaDoSetStorage);
  1624.  
  1625.     //getChannelUsers(channelId)
  1626.     lua_register(m_luaState, "getChannelUsers", LuaInterface::luaGetChannelUsers);
  1627.  
  1628.     //getPlayersOnline()
  1629.     lua_register(m_luaState, "getPlayersOnline", LuaInterface::luaGetPlayersOnline);
  1630.  
  1631.     //getTileInfo(pos)
  1632.     lua_register(m_luaState, "getTileInfo", LuaInterface::luaGetTileInfo);
  1633.  
  1634.     //getThingFromPos(pos[, displayError = true])
  1635.     lua_register(m_luaState, "getThingFromPos", LuaInterface::luaGetThingFromPos);
  1636.  
  1637.     //getThing(uid)
  1638.     lua_register(m_luaState, "getThing", LuaInterface::luaGetThing);
  1639.  
  1640.     //doTileQueryAdd(uid, pos[, flags[, displayError = true]])
  1641.     lua_register(m_luaState, "doTileQueryAdd", LuaInterface::luaDoTileQueryAdd);
  1642.  
  1643.     //doItemRaidUnref(uid)
  1644.     lua_register(m_luaState, "doItemRaidUnref", LuaInterface::luaDoItemRaidUnref);
  1645.  
  1646.     //getThingPosition(uid)
  1647.     lua_register(m_luaState, "getThingPosition", LuaInterface::luaGetThingPosition);
  1648.  
  1649.     //getTileItemById(pos, itemId[, subType = -1])
  1650.     lua_register(m_luaState, "getTileItemById", LuaInterface::luaGetTileItemById);
  1651.  
  1652.     //getTileItemByType(pos, type)
  1653.     lua_register(m_luaState, "getTileItemByType", LuaInterface::luaGetTileItemByType);
  1654.  
  1655.     //getTileThingByPos(pos)
  1656.     lua_register(m_luaState, "getTileThingByPos", LuaInterface::luaGetTileThingByPos);
  1657.  
  1658.     //getTopCreature(pos)
  1659.     lua_register(m_luaState, "getTopCreature", LuaInterface::luaGetTopCreature);
  1660.  
  1661.     //doRemoveItem(uid[, count = -1])
  1662.     lua_register(m_luaState, "doRemoveItem", LuaInterface::luaDoRemoveItem);
  1663.  
  1664.     //doPlayerFeed(cid, food)
  1665.     lua_register(m_luaState, "doPlayerFeed", LuaInterface::luaDoPlayerFeed);
  1666.  
  1667.     //doPlayerSendCancel(cid, text)
  1668.     lua_register(m_luaState, "doPlayerSendCancel", LuaInterface::luaDoPlayerSendCancel);
  1669.  
  1670.     //doPlayerSendDefaultCancel(cid, ReturnValue)
  1671.     lua_register(m_luaState, "doPlayerSendDefaultCancel", LuaInterface::luaDoSendDefaultCancel);
  1672.  
  1673.     //getSearchString(fromPosition, toPosition[, fromIsCreature = false[, toIsCreature = false]])
  1674.     lua_register(m_luaState, "getSearchString", LuaInterface::luaGetSearchString);
  1675.  
  1676.     //getClosestFreeTile(cid, targetpos[, extended = false[, ignoreHouse = true]])
  1677.     lua_register(m_luaState, "getClosestFreeTile", LuaInterface::luaGetClosestFreeTile);
  1678.  
  1679.     //doTeleportThing(cid, newpos[, pushmove = true[, fullTeleport = true]])
  1680.     lua_register(m_luaState, "doTeleportThing", LuaInterface::luaDoTeleportThing);
  1681.  
  1682.     //doTransformItem(uid, newId[, count/subType])
  1683.     lua_register(m_luaState, "doTransformItem", LuaInterface::luaDoTransformItem);
  1684.  
  1685.     //doCreatureSay(uid, text[, type = SPEAK_SAY[, ghost = false[, cid = 0[, pos]]]])
  1686.     lua_register(m_luaState, "doCreatureSay", LuaInterface::luaDoCreatureSay);
  1687.  
  1688.     //doSendCreatureSquare(cid, color[, player])
  1689.     lua_register(m_luaState, "doSendCreatureSquare", LuaInterface::luaDoSendCreatureSquare);
  1690.  
  1691.     //doSendMagicEffect(pos, type[, player])
  1692.     lua_register(m_luaState, "doSendMagicEffect", LuaInterface::luaDoSendMagicEffect);
  1693.  
  1694.     //doSendDistanceShoot(fromPos, toPos, type[, player])
  1695.     lua_register(m_luaState, "doSendDistanceShoot", LuaInterface::luaDoSendDistanceShoot);
  1696.  
  1697.     //doSendAnimatedText(pos, text, color[, player])
  1698.     lua_register(m_luaState, "doSendAnimatedText", LuaInterface::luaDoSendAnimatedText);
  1699.  
  1700.     //doPlayerAddSkillTry(cid, skillid, n[, useMultiplier = true])
  1701.     lua_register(m_luaState, "doPlayerAddSkillTry", LuaInterface::luaDoPlayerAddSkillTry);
  1702.  
  1703.     //doCreatureAddHealth(cid, health[, hitEffect[, hitColor[, force]]])
  1704.     lua_register(m_luaState, "doCreatureAddHealth", LuaInterface::luaDoCreatureAddHealth);
  1705.  
  1706.     //doCreatureAddMana(cid, mana)
  1707.     lua_register(m_luaState, "doCreatureAddMana", LuaInterface::luaDoCreatureAddMana);
  1708.  
  1709.     //setCreatureMaxHealth(cid, health)
  1710.     lua_register(m_luaState, "setCreatureMaxHealth", LuaInterface::luaSetCreatureMaxHealth);
  1711.  
  1712.     //setCreatureMaxMana(cid, mana)
  1713.     lua_register(m_luaState, "setCreatureMaxMana", LuaInterface::luaSetCreatureMaxMana);
  1714.  
  1715.     //doPlayerSetMaxCapacity(cid, cap)
  1716.     lua_register(m_luaState, "doPlayerSetMaxCapacity", LuaInterface::luaDoPlayerSetMaxCapacity);
  1717.  
  1718.     //doPlayerSetMagicLevel(cid, value)
  1719.     lua_register(m_luaState, "doPlayerSetMagicLevel", LuaInterface::luaDoPlayerSetMagicLevel);
  1720.  
  1721.     //doPlayerSetSkillLevel(cid, skill, value)
  1722.     lua_register(m_luaState, "doPlayerSetSkillLevel", LuaInterface::luaDoPlayerSetSkillLevel);
  1723.  
  1724.     //doPlayerAddSpentMana(cid, amount[, useMultiplier = true])
  1725.     lua_register(m_luaState, "doPlayerAddSpentMana", LuaInterface::luaDoPlayerAddSpentMana);
  1726.  
  1727.     //doPlayerAddSoul(cid, soul)
  1728.     lua_register(m_luaState, "doPlayerAddSoul", LuaInterface::luaDoPlayerAddSoul);
  1729.  
  1730.     //doPlayerAddItem(cid, itemid[, count/subtype = 1[, canDropOnMap = true[, slot = 0]]])
  1731.     //doPlayerAddItem(cid, itemid[, count = 1[, canDropOnMap = true[, subtype = 1[, slot = 0]]]])
  1732.     //Returns uid of the created item
  1733.     lua_register(m_luaState, "doPlayerAddItem", LuaInterface::luaDoPlayerAddItem);
  1734.  
  1735.     //doPlayerAddItemEx(cid, uid[, canDropOnMap = false[, slot = 0]])
  1736.     lua_register(m_luaState, "doPlayerAddItemEx", LuaInterface::luaDoPlayerAddItemEx);
  1737.  
  1738.     //doPlayerSendTextMessage(cid, MessageClasses, message)
  1739.     lua_register(m_luaState, "doPlayerSendTextMessage", LuaInterface::luaDoPlayerSendTextMessage);
  1740.  
  1741.     //doPlayerSendChannelMessage(cid, author, message, SpeakClasses, channel)
  1742.     lua_register(m_luaState, "doPlayerSendChannelMessage", LuaInterface::luaDoPlayerSendChannelMessage);
  1743.  
  1744.     //doPlayerSendToChannel(cid, targetId, SpeakClasses, message, channel[, time])
  1745.     lua_register(m_luaState, "doPlayerSendToChannel", LuaInterface::luaDoPlayerSendToChannel);
  1746.  
  1747.     //doPlayerOpenChannel(cid, channelId)
  1748.     lua_register(m_luaState, "doPlayerOpenChannel", LuaInterface::luaDoPlayerOpenChannel);
  1749.  
  1750.     //doPlayerAddMoney(cid, money)
  1751.     lua_register(m_luaState, "doPlayerAddMoney", LuaInterface::luaDoPlayerAddMoney);
  1752.  
  1753.     //doPlayerRemoveMoney(cid, money)
  1754.     lua_register(m_luaState, "doPlayerRemoveMoney", LuaInterface::luaDoPlayerRemoveMoney);
  1755.  
  1756.     //doPlayerTransferMoneyTo(cid, target, money)
  1757.     lua_register(m_luaState, "doPlayerTransferMoneyTo", LuaInterface::luaDoPlayerTransferMoneyTo);
  1758.  
  1759.     //doShowTextDialog(cid, itemid, text)
  1760.     lua_register(m_luaState, "doShowTextDialog", LuaInterface::luaDoShowTextDialog);
  1761.  
  1762.     //doDecayItem(uid)
  1763.     lua_register(m_luaState, "doDecayItem", LuaInterface::luaDoDecayItem);
  1764.  
  1765.     //doCreateItem(itemid[, type/count], pos)
  1766.     //Returns uid of the created item, only works on tiles.
  1767.     lua_register(m_luaState, "doCreateItem", LuaInterface::luaDoCreateItem);
  1768.  
  1769.     //doCreateItemEx(itemid[, count/subType = -1])
  1770.     lua_register(m_luaState, "doCreateItemEx", LuaInterface::luaDoCreateItemEx);
  1771.  
  1772.     //doTileAddItemEx(pos, uid)
  1773.     lua_register(m_luaState, "doTileAddItemEx", LuaInterface::luaDoTileAddItemEx);
  1774.  
  1775.     //doAddContainerItemEx(uid, virtuid)
  1776.     lua_register(m_luaState, "doAddContainerItemEx", LuaInterface::luaDoAddContainerItemEx);
  1777.  
  1778.     //doRelocate(pos, posTo[, creatures = true[, unmovable = true]])
  1779.     //Moves all moveable objects from pos to posTo
  1780.     lua_register(m_luaState, "doRelocate", LuaInterface::luaDoRelocate);
  1781.  
  1782.     //doCleanTile(pos[, forceMapLoaded = false])
  1783.     lua_register(m_luaState, "doCleanTile", LuaInterface::luaDoCleanTile);
  1784.  
  1785.     //doCreateTeleport(itemid, topos, createpos)
  1786.     lua_register(m_luaState, "doCreateTeleport", LuaInterface::luaDoCreateTeleport);
  1787.  
  1788.     //doCreateMonster(name, pos[, extend = false[, force = false[, displayError = true]]])
  1789.     lua_register(m_luaState, "doCreateMonster", LuaInterface::luaDoCreateMonster);
  1790.  
  1791.     //doCreateNpc(name, pos[, displayError = true])
  1792.     lua_register(m_luaState, "doCreateNpc", LuaInterface::luaDoCreateNpc);
  1793.  
  1794.     //doSummonMonster(cid, name)
  1795.     lua_register(m_luaState, "doSummonMonster", LuaInterface::luaDoSummonMonster);
  1796.  
  1797.     //doConvinceCreature(cid, target)
  1798.     lua_register(m_luaState, "doConvinceCreature", LuaInterface::luaDoConvinceCreature);
  1799.  
  1800.     //getMonsterTargetList(cid)
  1801.     lua_register(m_luaState, "getMonsterTargetList", LuaInterface::luaGetMonsterTargetList);
  1802.  
  1803.     //getMonsterFriendList(cid)
  1804.     lua_register(m_luaState, "getMonsterFriendList", LuaInterface::luaGetMonsterFriendList);
  1805.  
  1806.     //doMonsterSetTarget(cid, target)
  1807.     lua_register(m_luaState, "doMonsterSetTarget", LuaInterface::luaDoMonsterSetTarget);
  1808.  
  1809.     //doMonsterChangeTarget(cid)
  1810.     lua_register(m_luaState, "doMonsterChangeTarget", LuaInterface::luaDoMonsterChangeTarget);
  1811.  
  1812.     //getMonsterInfo(name)
  1813.     lua_register(m_luaState, "getMonsterInfo", LuaInterface::luaGetMonsterInfo);
  1814.  
  1815.     //doAddCondition(cid, condition)
  1816.     lua_register(m_luaState, "doAddCondition", LuaInterface::luaDoAddCondition);
  1817.  
  1818.     //doRemoveCondition(cid, type[, subId])
  1819.     lua_register(m_luaState, "doRemoveCondition", LuaInterface::luaDoRemoveCondition);
  1820.  
  1821.     //doRemoveConditions(cid[, onlyPersistent])
  1822.     lua_register(m_luaState, "doRemoveConditions", LuaInterface::luaDoRemoveConditions);
  1823.  
  1824.     //doRemoveCreature(cid[, forceLogout = true])
  1825.     lua_register(m_luaState, "doRemoveCreature", LuaInterface::luaDoRemoveCreature);
  1826.  
  1827.     //doMoveCreature(cid, direction[, flag = FLAG_NOLIMIT])
  1828.     lua_register(m_luaState, "doMoveCreature", LuaInterface::luaDoMoveCreature);
  1829.  
  1830.     //doPlayerSetPzLocked(cid, locked)
  1831.     lua_register(m_luaState, "doPlayerSetPzLocked", LuaInterface::luaDoPlayerSetPzLocked);
  1832.  
  1833.     //doPlayerSetTown(cid, townid)
  1834.     lua_register(m_luaState, "doPlayerSetTown", LuaInterface::luaDoPlayerSetTown);
  1835.  
  1836.     //doPlayerSetVocation(cid,voc)
  1837.     lua_register(m_luaState, "doPlayerSetVocation", LuaInterface::luaDoPlayerSetVocation);
  1838.  
  1839.     //doPlayerRemoveItem(cid, itemid[, count[, subType = -1]])
  1840.     lua_register(m_luaState, "doPlayerRemoveItem", LuaInterface::luaDoPlayerRemoveItem);
  1841.  
  1842.     //doPlayerAddExperience(cid, amount)
  1843.     lua_register(m_luaState, "doPlayerAddExperience", LuaInterface::luaDoPlayerAddExperience);
  1844.  
  1845.     //doPlayerSetGuildId(cid, id)
  1846.     lua_register(m_luaState, "doPlayerSetGuildId", LuaInterface::luaDoPlayerSetGuildId);
  1847.  
  1848.     //doPlayerSetGuildLevel(cid, level[, rank])
  1849.     lua_register(m_luaState, "doPlayerSetGuildLevel", LuaInterface::luaDoPlayerSetGuildLevel);
  1850.  
  1851.     //doPlayerSetGuildNick(cid, nick)
  1852.     lua_register(m_luaState, "doPlayerSetGuildNick", LuaInterface::luaDoPlayerSetGuildNick);
  1853.  
  1854.     //doPlayerAddOutfit(cid, looktype, addon)
  1855.     lua_register(m_luaState, "doPlayerAddOutfit", LuaInterface::luaDoPlayerAddOutfit);
  1856.  
  1857.     //doPlayerRemoveOutfit(cid, looktype[, addon = 0])
  1858.     lua_register(m_luaState, "doPlayerRemoveOutfit", LuaInterface::luaDoPlayerRemoveOutfit);
  1859.  
  1860.     //doPlayerAddOutfitId(cid, outfitId, addon)
  1861.     lua_register(m_luaState, "doPlayerAddOutfitId", LuaInterface::luaDoPlayerAddOutfitId);
  1862.  
  1863.     //doPlayerRemoveOutfitId(cid, outfitId[, addon = 0])
  1864.     lua_register(m_luaState, "doPlayerRemoveOutfitId", LuaInterface::luaDoPlayerRemoveOutfitId);
  1865.  
  1866.     //canPlayerWearOutfit(cid, looktype[, addon = 0])
  1867.     lua_register(m_luaState, "canPlayerWearOutfit", LuaInterface::luaCanPlayerWearOutfit);
  1868.  
  1869.     //canPlayerWearOutfitId(cid, outfitId[, addon = 0])
  1870.     lua_register(m_luaState, "canPlayerWearOutfitId", LuaInterface::luaCanPlayerWearOutfitId);
  1871.  
  1872.     //getCreatureCondition(cid, condition[, subId = 0])
  1873.     lua_register(m_luaState, "getCreatureCondition", LuaInterface::luaGetCreatureCondition);
  1874.  
  1875.     //doCreatureSetDropLoot(cid, doDrop)
  1876.     lua_register(m_luaState, "doCreatureSetDropLoot", LuaInterface::luaDoCreatureSetDropLoot);
  1877.  
  1878.     //getPlayerLossPercent(cid, lossType)
  1879.     lua_register(m_luaState, "getPlayerLossPercent", LuaInterface::luaGetPlayerLossPercent);
  1880.  
  1881.     //doPlayerSetLossPercent(cid, lossType, newPercent)
  1882.     lua_register(m_luaState, "doPlayerSetLossPercent", LuaInterface::luaDoPlayerSetLossPercent);
  1883.  
  1884.     //doPlayerSetLossSkill(cid, doLose)
  1885.     lua_register(m_luaState, "doPlayerSetLossSkill", LuaInterface::luaDoPlayerSetLossSkill);
  1886.  
  1887.     //getPlayerLossSkill(cid)
  1888.     lua_register(m_luaState, "getPlayerLossSkill", LuaInterface::luaGetPlayerLossSkill);
  1889.  
  1890.     //doPlayerSwitchSaving(cid)
  1891.     lua_register(m_luaState, "doPlayerSwitchSaving", LuaInterface::luaDoPlayerSwitchSaving);
  1892.  
  1893.     //doPlayerSave(cid[, shallow = false])
  1894.     lua_register(m_luaState, "doPlayerSave", LuaInterface::luaDoPlayerSave);
  1895.  
  1896.     //isPlayerPzLocked(cid)
  1897.     lua_register(m_luaState, "isPlayerPzLocked", LuaInterface::luaIsPlayerPzLocked);
  1898.  
  1899.     //isPlayerSaving(cid)
  1900.     lua_register(m_luaState, "isPlayerSaving", LuaInterface::luaIsPlayerSaving);
  1901.  
  1902.     //isCreature(cid)
  1903.     lua_register(m_luaState, "isCreature", LuaInterface::luaIsCreature);
  1904.  
  1905.     //isContainer(uid)
  1906.     lua_register(m_luaState, "isContainer", LuaInterface::luaIsContainer);
  1907.  
  1908.     //isMovable(uid)
  1909.     lua_register(m_luaState, "isMovable", LuaInterface::luaIsMovable);
  1910.  
  1911.     //getCreatureByName(name)
  1912.     lua_register(m_luaState, "getCreatureByName", LuaInterface::luaGetCreatureByName);
  1913.  
  1914.     //getPlayerByGUID(guid)
  1915.     lua_register(m_luaState, "getPlayerByGUID", LuaInterface::luaGetPlayerByGUID);
  1916.  
  1917.     //getPlayerByNameWildcard(name~[, ret = false])
  1918.     lua_register(m_luaState, "getPlayerByNameWildcard", LuaInterface::luaGetPlayerByNameWildcard);
  1919.  
  1920.     //getPlayerGUIDByName(name[, multiworld = false])
  1921.     lua_register(m_luaState, "getPlayerGUIDByName", LuaInterface::luaGetPlayerGUIDByName);
  1922.  
  1923.     //getPlayerNameByGUID(guid[, multiworld = false[, displayError = true]])
  1924.     lua_register(m_luaState, "getPlayerNameByGUID", LuaInterface::luaGetPlayerNameByGUID);
  1925.  
  1926.     //registerCreatureEvent(uid, eventName)
  1927.     lua_register(m_luaState, "registerCreatureEvent", LuaInterface::luaRegisterCreatureEvent);
  1928.  
  1929.     //unregisterCreatureEvent(uid, eventName)
  1930.     lua_register(m_luaState, "unregisterCreatureEvent", LuaInterface::luaUnregisterCreatureEvent);
  1931.  
  1932.     //getContainerSize(uid)
  1933.     lua_register(m_luaState, "getContainerSize", LuaInterface::luaGetContainerSize);
  1934.  
  1935.     //getContainerCap(uid)
  1936.     lua_register(m_luaState, "getContainerCap", LuaInterface::luaGetContainerCap);
  1937.  
  1938.     //getContainerItem(uid, slot)
  1939.     lua_register(m_luaState, "getContainerItem", LuaInterface::luaGetContainerItem);
  1940.  
  1941.     //doAddContainerItem(uid, itemid[, count/subType = 1])
  1942.     lua_register(m_luaState, "doAddContainerItem", LuaInterface::luaDoAddContainerItem);
  1943.  
  1944.     //getHouseInfo(houseId[, displayError = true])
  1945.     lua_register(m_luaState, "getHouseInfo", LuaInterface::luaGetHouseInfo);
  1946.  
  1947.     //getHouseAccessList(houseid, listId)
  1948.     lua_register(m_luaState, "getHouseAccessList", LuaInterface::luaGetHouseAccessList);
  1949.  
  1950.     //getHouseByPlayerGUID(playerGUID)
  1951.     lua_register(m_luaState, "getHouseByPlayerGUID", LuaInterface::luaGetHouseByPlayerGUID);
  1952.  
  1953.     //getHouseFromPos(pos)
  1954.     lua_register(m_luaState, "getHouseFromPos", LuaInterface::luaGetHouseFromPos);
  1955.  
  1956.     //setHouseAccessList(houseid, listid, listtext)
  1957.     lua_register(m_luaState, "setHouseAccessList", LuaInterface::luaSetHouseAccessList);
  1958.  
  1959.     //setHouseOwner(houseId, owner[, clean])
  1960.     lua_register(m_luaState, "setHouseOwner", LuaInterface::luaSetHouseOwner);
  1961.  
  1962.     //getWorldType()
  1963.     lua_register(m_luaState, "getWorldType", LuaInterface::luaGetWorldType);
  1964.  
  1965.     //setWorldType(type)
  1966.     lua_register(m_luaState, "setWorldType", LuaInterface::luaSetWorldType);
  1967.  
  1968.     //getWorldTime()
  1969.     lua_register(m_luaState, "getWorldTime", LuaInterface::luaGetWorldTime);
  1970.  
  1971.     //getWorldLight()
  1972.     lua_register(m_luaState, "getWorldLight", LuaInterface::luaGetWorldLight);
  1973.  
  1974.     //getWorldCreatures(type)
  1975.     //0 players, 1 monsters, 2 npcs, 3 all
  1976.     lua_register(m_luaState, "getWorldCreatures", LuaInterface::luaGetWorldCreatures);
  1977.  
  1978.     //getWorldUpTime()
  1979.     lua_register(m_luaState, "getWorldUpTime", LuaInterface::luaGetWorldUpTime);
  1980.  
  1981.     //getGuildId(guildName)
  1982.     lua_register(m_luaState, "getGuildId", LuaInterface::luaGetGuildId);
  1983.  
  1984.     //getGuildMotd(guildId)
  1985.     lua_register(m_luaState, "getGuildMotd", LuaInterface::luaGetGuildMotd);
  1986.  
  1987.     //getPlayerSex(cid[, full = false])
  1988.     lua_register(m_luaState, "getPlayerSex", LuaInterface::luaGetPlayerSex);
  1989.  
  1990.     //doPlayerSetSex(cid, newSex)
  1991.     lua_register(m_luaState, "doPlayerSetSex", LuaInterface::luaDoPlayerSetSex);
  1992.  
  1993.     //createCombatArea({area}[, {extArea}])
  1994.     lua_register(m_luaState, "createCombatArea", LuaInterface::luaCreateCombatArea);
  1995.  
  1996.     //createConditionObject(type[, ticks[, buff[, subId]]])
  1997.     lua_register(m_luaState, "createConditionObject", LuaInterface::luaCreateConditionObject);
  1998.  
  1999.     //setCombatArea(combat, area)
  2000.     lua_register(m_luaState, "setCombatArea", LuaInterface::luaSetCombatArea);
  2001.  
  2002.     //setCombatCondition(combat, condition)
  2003.     lua_register(m_luaState, "setCombatCondition", LuaInterface::luaSetCombatCondition);
  2004.  
  2005.     //setCombatParam(combat, key, value)
  2006.     lua_register(m_luaState, "setCombatParam", LuaInterface::luaSetCombatParam);
  2007.  
  2008.     //setConditionParam(condition, key, value)
  2009.     lua_register(m_luaState, "setConditionParam", LuaInterface::luaSetConditionParam);
  2010.  
  2011.     //addDamageCondition(condition, rounds, time, value)
  2012.     lua_register(m_luaState, "addDamageCondition", LuaInterface::luaAddDamageCondition);
  2013.  
  2014.     //addOutfitCondition(condition, outfit)
  2015.     lua_register(m_luaState, "addOutfitCondition", LuaInterface::luaAddOutfitCondition);
  2016.  
  2017.     //setCombatCallBack(combat, key, function_name)
  2018.     lua_register(m_luaState, "setCombatCallback", LuaInterface::luaSetCombatCallBack);
  2019.  
  2020.     //setCombatFormula(combat, type, mina, minb, maxa, maxb[, minl, maxl[, minm, maxm[, minc[, maxc]]]])
  2021.     lua_register(m_luaState, "setCombatFormula", LuaInterface::luaSetCombatFormula);
  2022.  
  2023.     //setConditionFormula(combat, mina, minb, maxa, maxb)
  2024.     lua_register(m_luaState, "setConditionFormula", LuaInterface::luaSetConditionFormula);
  2025.  
  2026.     //doCombat(cid, combat, param)
  2027.     lua_register(m_luaState, "doCombat", LuaInterface::luaDoCombat);
  2028.  
  2029.     //createCombatObject()
  2030.     lua_register(m_luaState, "createCombatObject", LuaInterface::luaCreateCombatObject);
  2031.  
  2032.     //doCombatAreaHealth(cid, type, pos, area, min, max, effect)
  2033.     lua_register(m_luaState, "doCombatAreaHealth", LuaInterface::luaDoCombatAreaHealth);
  2034.  
  2035.     //doTargetCombatHealth(cid, target, type, min, max, effect)
  2036.     lua_register(m_luaState, "doTargetCombatHealth", LuaInterface::luaDoTargetCombatHealth);
  2037.  
  2038.     //doCombatAreaMana(cid, pos, area, min, max, effect)
  2039.     lua_register(m_luaState, "doCombatAreaMana", LuaInterface::luaDoCombatAreaMana);
  2040.  
  2041.     //doTargetCombatMana(cid, target, min, max, effect)
  2042.     lua_register(m_luaState, "doTargetCombatMana", LuaInterface::luaDoTargetCombatMana);
  2043.  
  2044.     //doCombatAreaCondition(cid, pos, area, condition, effect)
  2045.     lua_register(m_luaState, "doCombatAreaCondition", LuaInterface::luaDoCombatAreaCondition);
  2046.  
  2047.     //doTargetCombatCondition(cid, target, condition, effect)
  2048.     lua_register(m_luaState, "doTargetCombatCondition", LuaInterface::luaDoTargetCombatCondition);
  2049.  
  2050.     //doCombatAreaDispel(cid, pos, area, type, effect)
  2051.     lua_register(m_luaState, "doCombatAreaDispel", LuaInterface::luaDoCombatAreaDispel);
  2052.  
  2053.     //doTargetCombatDispel(cid, target, type, effect)
  2054.     lua_register(m_luaState, "doTargetCombatDispel", LuaInterface::luaDoTargetCombatDispel);
  2055.  
  2056.     //doChallengeCreature(cid, target)
  2057.     lua_register(m_luaState, "doChallengeCreature", LuaInterface::luaDoChallengeCreature);
  2058.  
  2059.     //numberToVariant(number)
  2060.     lua_register(m_luaState, "numberToVariant", LuaInterface::luaNumberToVariant);
  2061.  
  2062.     //stringToVariant(string)
  2063.     lua_register(m_luaState, "stringToVariant", LuaInterface::luaStringToVariant);
  2064.  
  2065.     //positionToVariant(pos)
  2066.     lua_register(m_luaState, "positionToVariant", LuaInterface::luaPositionToVariant);
  2067.  
  2068.     //targetPositionToVariant(pos)
  2069.     lua_register(m_luaState, "targetPositionToVariant", LuaInterface::luaTargetPositionToVariant);
  2070.  
  2071.     //variantToNumber(var)
  2072.     lua_register(m_luaState, "variantToNumber", LuaInterface::luaVariantToNumber);
  2073.  
  2074.     //variantToString(var)
  2075.     lua_register(m_luaState, "variantToString", LuaInterface::luaVariantToString);
  2076.  
  2077.     //variantToPosition(var)
  2078.     lua_register(m_luaState, "variantToPosition", LuaInterface::luaVariantToPosition);
  2079.  
  2080.     //doChangeSpeed(cid, delta)
  2081.     lua_register(m_luaState, "doChangeSpeed", LuaInterface::luaDoChangeSpeed);
  2082.  
  2083.     //doCreatureChangeOutfit(cid, outfit)
  2084.     lua_register(m_luaState, "doCreatureChangeOutfit", LuaInterface::luaDoCreatureChangeOutfit);
  2085.  
  2086.     //doSetMonsterOutfit(cid, name[, time = -1])
  2087.     lua_register(m_luaState, "doSetMonsterOutfit", LuaInterface::luaSetMonsterOutfit);
  2088.  
  2089.     // ping 1
  2090.     //doPlayerSendPing(cid)
  2091.     lua_register(m_luaState, "doPlayerSendPing", LuaInterface::luaDoPlayerSendPing);
  2092.     //getPlayerLastPing(cid)
  2093.     lua_register(m_luaState, "getPlayerLastPing", LuaInterface::luaGetPlayerLastPing);
  2094.     //getPlayerLastPong(cid)
  2095.     lua_register(m_luaState, "getPlayerLastPong", LuaInterface::luaGetPlayerLastPong);
  2096.     //getOtsysTime(cid)
  2097.     lua_register(m_luaState, "getOtsysTime", LuaInterface::luaGetOtsysTime);
  2098.     // /ping 1
  2099.  
  2100.     //doSetItemOutfit(cid, item[, time = -1])
  2101.     lua_register(m_luaState, "doSetItemOutfit", LuaInterface::luaSetItemOutfit);
  2102.  
  2103.     //doSetCreatureOutfit(cid, outfit[, time = -1])
  2104.     lua_register(m_luaState, "doSetCreatureOutfit", LuaInterface::luaSetCreatureOutfit);
  2105.  
  2106.     //getCreatureOutfit(cid)
  2107.     lua_register(m_luaState, "getCreatureOutfit", LuaInterface::luaGetCreatureOutfit);
  2108.  
  2109.     //getCreatureLastPosition(cid)
  2110.     lua_register(m_luaState, "getCreatureLastPosition", LuaInterface::luaGetCreatureLastPosition);
  2111.  
  2112.     //getCreatureName(cid)
  2113.     lua_register(m_luaState, "getCreatureName", LuaInterface::luaGetCreatureName);
  2114.    
  2115.     //getCreaturePathTo(cid, pos, maxSearchDist)
  2116.     lua_register(m_luaState, "getCreaturePathTo", LuaInterface::luaGetCreaturePathTo);
  2117.  
  2118.     //getCreatureSpeed(cid)
  2119.     lua_register(m_luaState, "getCreatureSpeed", LuaInterface::luaGetCreatureSpeed);
  2120.  
  2121.     //getCreatureBaseSpeed(cid)
  2122.     lua_register(m_luaState, "getCreatureBaseSpeed", LuaInterface::luaGetCreatureBaseSpeed);
  2123.  
  2124.     //getCreatureTarget(cid)
  2125.     lua_register(m_luaState, "getCreatureTarget", LuaInterface::luaGetCreatureTarget);
  2126.  
  2127.     //isSightClear(fromPos, toPos, floorCheck)
  2128.     lua_register(m_luaState, "isSightClear", LuaInterface::luaIsSightClear);
  2129.  
  2130.     //isInArray(array, value[, caseSensitive = false])
  2131.     lua_register(m_luaState, "isInArray", LuaInterface::luaIsInArray);
  2132.  
  2133.     //addEvent(callback, delay, ...)
  2134.     lua_register(m_luaState, "addEvent", LuaInterface::luaAddEvent);
  2135.  
  2136.     //stopEvent(eventid)
  2137.     lua_register(m_luaState, "stopEvent", LuaInterface::luaStopEvent);
  2138.  
  2139.     //getPlayersByAccountId(accId)
  2140.     lua_register(m_luaState, "getPlayersByAccountId", LuaInterface::luaGetPlayersByAccountId);
  2141.  
  2142.     //getAccountIdByName(name)
  2143.     lua_register(m_luaState, "getAccountIdByName", LuaInterface::luaGetAccountIdByName);
  2144.  
  2145.     //getAccountByName(name)
  2146.     lua_register(m_luaState, "getAccountByName", LuaInterface::luaGetAccountByName);
  2147.  
  2148.     //getAccountIdByAccount(accName)
  2149.     lua_register(m_luaState, "getAccountIdByAccount", LuaInterface::luaGetAccountIdByAccount);
  2150.  
  2151.     //getAccountByAccountId(accId)
  2152.     lua_register(m_luaState, "getAccountByAccountId", LuaInterface::luaGetAccountByAccountId);
  2153.  
  2154.     //getIpByName(name)
  2155.     lua_register(m_luaState, "getIpByName", LuaInterface::luaGetIpByName);
  2156.  
  2157.     //getPlayersByIp(ip[, mask = 0xFFFFFFFF])
  2158.     lua_register(m_luaState, "getPlayersByIp", LuaInterface::luaGetPlayersByIp);
  2159.  
  2160.     //doPlayerPopupFYI(cid, message)
  2161.     lua_register(m_luaState, "doPlayerPopupFYI", LuaInterface::luaDoPlayerPopupFYI);
  2162.  
  2163.     //doPlayerSendTutorial(cid, id)
  2164.     lua_register(m_luaState, "doPlayerSendTutorial", LuaInterface::luaDoPlayerSendTutorial);
  2165.  
  2166.     //doPlayerSendMailByName(name, item[, town[, actor]])
  2167.     lua_register(m_luaState, "doPlayerSendMailByName", LuaInterface::luaDoPlayerSendMailByName);
  2168.  
  2169.     //doPlayerAddMapMark(cid, pos, type[, description])
  2170.     lua_register(m_luaState, "doPlayerAddMapMark", LuaInterface::luaDoPlayerAddMapMark);
  2171.  
  2172.     //doPlayerAddPremiumDays(cid, days)
  2173.     lua_register(m_luaState, "doPlayerAddPremiumDays", LuaInterface::luaDoPlayerAddPremiumDays);
  2174.  
  2175.     //getPlayerPremiumDays(cid)
  2176.     lua_register(m_luaState, "getPlayerPremiumDays", LuaInterface::luaGetPlayerPremiumDays);
  2177.  
  2178.     //doCreatureSetLookDirection(cid, dir)
  2179.     lua_register(m_luaState, "doCreatureSetLookDirection", LuaInterface::luaDoCreatureSetLookDir);
  2180.  
  2181.     //getCreatureGuildEmblem(cid[, target])
  2182.     lua_register(m_luaState, "getCreatureGuildEmblem", LuaInterface::luaGetCreatureGuildEmblem);
  2183.  
  2184.     //doCreatureSetGuildEmblem(cid, emblem)
  2185.     lua_register(m_luaState, "doCreatureSetGuildEmblem", LuaInterface::luaDoCreatureSetGuildEmblem);
  2186.  
  2187.     //getCreaturePartyShield(cid[, target])
  2188.     lua_register(m_luaState, "getCreaturePartyShield", LuaInterface::luaGetCreaturePartyShield);
  2189.  
  2190.     //doCreatureSetPartyShield(cid, shield)
  2191.     lua_register(m_luaState, "doCreatureSetPartyShield", LuaInterface::luaDoCreatureSetPartyShield);
  2192.  
  2193.     //getCreatureSkullType(cid[, target])
  2194.     lua_register(m_luaState, "getCreatureSkullType", LuaInterface::luaGetCreatureSkullType);
  2195.  
  2196.     //doCreatureSetSkullType(cid, skull)
  2197.     lua_register(m_luaState, "doCreatureSetSkullType", LuaInterface::luaDoCreatureSetSkullType);
  2198.  
  2199.     //getPlayerSkullEnd(cid)
  2200.     lua_register(m_luaState, "getPlayerSkullEnd", LuaInterface::luaGetPlayerSkullEnd);
  2201.  
  2202.     //doPlayerSetSkullEnd(cid, time, type)
  2203.     lua_register(m_luaState, "doPlayerSetSkullEnd", LuaInterface::luaDoPlayerSetSkullEnd);
  2204.  
  2205.     //getPlayerBlessing(cid, blessing)
  2206.     lua_register(m_luaState, "getPlayerBlessing", LuaInterface::luaGetPlayerBlessing);
  2207.  
  2208.     //doPlayerAddBlessing(cid, blessing)
  2209.     lua_register(m_luaState, "doPlayerAddBlessing", LuaInterface::luaDoPlayerAddBlessing);
  2210.  
  2211.     //getPlayerStamina(cid)
  2212.     lua_register(m_luaState, "getPlayerStamina", LuaInterface::luaGetPlayerStamina);
  2213.  
  2214.     //doPlayerSetStamina(cid, minutes)
  2215.     lua_register(m_luaState, "doPlayerSetStamina", LuaInterface::luaDoPlayerSetStamina);
  2216.  
  2217.     //getPlayerBalance(cid)
  2218.     lua_register(m_luaState, "getPlayerBalance", LuaInterface::luaGetPlayerBalance);
  2219.  
  2220.     //doPlayerSetBalance(cid, balance)
  2221.     lua_register(m_luaState, "doPlayerSetBalance", LuaInterface::luaDoPlayerSetBalance);
  2222.  
  2223.     //getCreatureNoMove(cid)
  2224.     lua_register(m_luaState, "getCreatureNoMove", LuaInterface::luaGetCreatureNoMove);
  2225.  
  2226.     //doCreatureSetNoMove(cid, block)
  2227.     lua_register(m_luaState, "doCreatureSetNoMove", LuaInterface::luaDoCreatureSetNoMove);
  2228.  
  2229.     //getPlayerIdleTime(cid)
  2230.     lua_register(m_luaState, "getPlayerIdleTime", LuaInterface::luaGetPlayerIdleTime);
  2231.  
  2232.     //doPlayerSetIdleTime(cid, amount)
  2233.     lua_register(m_luaState, "doPlayerSetIdleTime", LuaInterface::luaDoPlayerSetIdleTime);
  2234.  
  2235.     //getPlayerLastLoad(cid)
  2236.     lua_register(m_luaState, "getPlayerLastLoad", LuaInterface::luaGetPlayerLastLoad);
  2237.  
  2238.     //getPlayerLastLogin(cid)
  2239.     lua_register(m_luaState, "getPlayerLastLogin", LuaInterface::luaGetPlayerLastLogin);
  2240.  
  2241.     //getPlayerAccountManager(cid)
  2242.     lua_register(m_luaState, "getPlayerAccountManager", LuaInterface::luaGetPlayerAccountManager);
  2243.  
  2244.     //getPlayerTradeState(cid)
  2245.     lua_register(m_luaState, "getPlayerTradeState", LuaInterface::luaGetPlayerTradeState);
  2246.  
  2247.     //getPlayerModes(cid)
  2248.     lua_register(m_luaState, "getPlayerModes", LuaInterface::luaGetPlayerModes);
  2249.  
  2250.     //getPlayerRates(cid)
  2251.     lua_register(m_luaState, "getPlayerRates", LuaInterface::luaGetPlayerRates);
  2252.  
  2253.     //doPlayerSetRate(cid, type, value)
  2254.     lua_register(m_luaState, "doPlayerSetRate", LuaInterface::luaDoPlayerSetRate);
  2255.  
  2256.     // change loot rates per player 2
  2257.     //doPlayerSetSpecialRateLoot(cid, newRate)
  2258.     lua_register(m_luaState, "doPlayerSetSpecialRateLoot", LuaInterface::luaDoPlayerSetSpecialRateLoot);
  2259.  
  2260.     //doPlayerGetSpecialRateLoot(cid)
  2261.     lua_register(m_luaState, "doPlayerGetSpecialRateLoot", LuaInterface::luaDoPlayerGetSpecialRateLoot);
  2262.  
  2263.     //getPlayerPartner(cid)
  2264.     lua_register(m_luaState, "getPlayerPartner", LuaInterface::luaGetPlayerPartner);
  2265.  
  2266.     //doPlayerSetPartner(cid, guid)
  2267.     lua_register(m_luaState, "doPlayerSetPartner", LuaInterface::luaDoPlayerSetPartner);
  2268.  
  2269.     //doPlayerFollowCreature(cid, target)
  2270.     lua_register(m_luaState, "doPlayerFollowCreature", LuaInterface::luaDoPlayerFollowCreature);
  2271.  
  2272.     //getPlayerParty(cid)
  2273.     lua_register(m_luaState, "getPlayerParty", LuaInterface::luaGetPlayerParty);
  2274.  
  2275.     //doPlayerJoinParty(cid, lid)
  2276.     lua_register(m_luaState, "doPlayerJoinParty", LuaInterface::luaDoPlayerJoinParty);
  2277.  
  2278.     //doPlayerLeaveParty(cid[, forced = false])
  2279.     lua_register(m_luaState, "doPlayerLeaveParty", LuaInterface::luaDoPlayerLeaveParty);
  2280.  
  2281.     //getPartyMembers(lid)
  2282.     lua_register(m_luaState, "getPartyMembers", LuaInterface::luaGetPartyMembers);
  2283.  
  2284.     //getCreatureMaster(cid)
  2285.     lua_register(m_luaState, "getCreatureMaster", LuaInterface::luaGetCreatureMaster);
  2286.  
  2287.     //getCreatureSummons(cid)
  2288.     lua_register(m_luaState, "getCreatureSummons", LuaInterface::luaGetCreatureSummons);
  2289.  
  2290.     //getTownId(townName)
  2291.     lua_register(m_luaState, "getTownId", LuaInterface::luaGetTownId);
  2292.  
  2293.     //getTownName(townId)
  2294.     lua_register(m_luaState, "getTownName", LuaInterface::luaGetTownName);
  2295.  
  2296.     //getTownTemplePosition(townId)
  2297.     lua_register(m_luaState, "getTownTemplePosition", LuaInterface::luaGetTownTemplePosition);
  2298.  
  2299.     //getTownHouses(townId)
  2300.     lua_register(m_luaState, "getTownHouses", LuaInterface::luaGetTownHouses);
  2301.  
  2302.     //getSpectators(centerPos, rangex, rangey[, multifloor = false])
  2303.     lua_register(m_luaState, "getSpectators", LuaInterface::luaGetSpectators);
  2304.  
  2305.     //getVocationInfo(id)
  2306.     lua_register(m_luaState, "getVocationInfo", LuaInterface::luaGetVocationInfo);
  2307.  
  2308.     //getGroupInfo(id[, premium = false])
  2309.     lua_register(m_luaState, "getGroupInfo", LuaInterface::luaGetGroupInfo);
  2310.  
  2311.     //getTownList()
  2312.     lua_register(m_luaState, "getTownList", LuaInterface::luaGetTownList);
  2313.  
  2314.     //getWaypointList()
  2315.     lua_register(m_luaState, "getWaypointList", LuaInterface::luaGetWaypointList);
  2316.  
  2317.     //getTalkActionList()
  2318.     lua_register(m_luaState, "getTalkActionList", LuaInterface::luaGetTalkActionList);
  2319.  
  2320.     //getExperienceStageList()
  2321.     lua_register(m_luaState, "getExperienceStageList", LuaInterface::luaGetExperienceStageList);
  2322.  
  2323.     //getItemIdByName(name[, displayError = true])
  2324.     lua_register(m_luaState, "getItemIdByName", LuaInterface::luaGetItemIdByName);
  2325.  
  2326.     //getItemInfo(itemid)
  2327.     lua_register(m_luaState, "getItemInfo", LuaInterface::luaGetItemInfo);
  2328.  
  2329.     //getItemAttribute(uid, key)
  2330.     lua_register(m_luaState, "getItemAttribute", LuaInterface::luaGetItemAttribute);
  2331.  
  2332.     //doItemSetAttribute(uid, key, value)
  2333.     lua_register(m_luaState, "doItemSetAttribute", LuaInterface::luaDoItemSetAttribute);
  2334.  
  2335.     //doItemEraseAttribute(uid, key)
  2336.     lua_register(m_luaState, "doItemEraseAttribute", LuaInterface::luaDoItemEraseAttribute);
  2337.  
  2338.     //getItemWeight(uid[, precise = true])
  2339.     lua_register(m_luaState, "getItemWeight", LuaInterface::luaGetItemWeight);
  2340.  
  2341.     //getItemParent(uid)
  2342.     lua_register(m_luaState, "getItemParent", LuaInterface::luaGetItemParent);
  2343.  
  2344.     //hasItemProperty(uid, prop)
  2345.     lua_register(m_luaState, "hasItemProperty", LuaInterface::luaHasItemProperty);
  2346.  
  2347.     //hasPlayerClient(cid)
  2348.     lua_register(m_luaState, "hasPlayerClient", LuaInterface::luaHasPlayerClient);
  2349.  
  2350.     //isIpBanished(ip[, mask])
  2351.     lua_register(m_luaState, "isIpBanished", LuaInterface::luaIsIpBanished);
  2352.  
  2353.     //isPlayerBanished(name/guid, type)
  2354.     lua_register(m_luaState, "isPlayerBanished", LuaInterface::luaIsPlayerBanished);
  2355.  
  2356.     //isAccountBanished(accountId[, playerId])
  2357.     lua_register(m_luaState, "isAccountBanished", LuaInterface::luaIsAccountBanished);
  2358.  
  2359.     //doAddIpBanishment(...)
  2360.     lua_register(m_luaState, "doAddIpBanishment", LuaInterface::luaDoAddIpBanishment);
  2361.  
  2362.     //doAddPlayerBanishment(...)
  2363.     lua_register(m_luaState, "doAddPlayerBanishment", LuaInterface::luaDoAddPlayerBanishment);
  2364.  
  2365.     //doAddAccountBanishment(...)
  2366.     lua_register(m_luaState, "doAddAccountBanishment", LuaInterface::luaDoAddAccountBanishment);
  2367.  
  2368.     //doAddNotation(...)
  2369.     lua_register(m_luaState, "doAddNotation", LuaInterface::luaDoAddNotation);
  2370.  
  2371.     //doAddStatement(...)
  2372.     lua_register(m_luaState, "doAddStatement", LuaInterface::luaDoAddStatement);
  2373.  
  2374.     //doRemoveIpBanishment(ip[, mask])
  2375.     lua_register(m_luaState, "doRemoveIpBanishment", LuaInterface::luaDoRemoveIpBanishment);
  2376.  
  2377.     //doRemovePlayerBanishment(name/guid, type)
  2378.     lua_register(m_luaState, "doRemovePlayerBanishment", LuaInterface::luaDoRemovePlayerBanishment);
  2379.  
  2380.     //doRemoveAccountBanishment(accountId[, playerId])
  2381.     lua_register(m_luaState, "doRemoveAccountBanishment", LuaInterface::luaDoRemoveAccountBanishment);
  2382.  
  2383.     //doRemoveNotations(accountId[, playerId])
  2384.     lua_register(m_luaState, "doRemoveNotations", LuaInterface::luaDoRemoveNotations);
  2385.  
  2386.     //doRemoveStatements(name/guid[, channelId])
  2387.     lua_register(m_luaState, "doRemoveStatements", LuaInterface::luaDoRemoveStatements);
  2388.  
  2389.     //getNotationsCount(accountId[, playerId])
  2390.     lua_register(m_luaState, "getNotationsCount", LuaInterface::luaGetNotationsCount);
  2391.  
  2392.     //getStatementsCount(name/guid[, channelId])
  2393.     lua_register(m_luaState, "getStatementsCount", LuaInterface::luaGetStatementsCount);
  2394.  
  2395.     //getBanData(value[, type[, param]])
  2396.     lua_register(m_luaState, "getBanData", LuaInterface::luaGetBanData);
  2397.  
  2398.     //getBanReason(id)
  2399.     lua_register(m_luaState, "getBanReason", LuaInterface::luaGetBanReason);
  2400.  
  2401.     //getBanAction(id[, ipBanishment = false])
  2402.     lua_register(m_luaState, "getBanAction", LuaInterface::luaGetBanAction);
  2403.  
  2404.     //getBanList(type[, value[, param]])
  2405.     lua_register(m_luaState, "getBanList", LuaInterface::luaGetBanList);
  2406.  
  2407.     //getExperienceStage(level)
  2408.     lua_register(m_luaState, "getExperienceStage", LuaInterface::luaGetExperienceStage);
  2409.  
  2410.     //getDataDir()
  2411.     lua_register(m_luaState, "getDataDir", LuaInterface::luaGetDataDir);
  2412.  
  2413.     //getLogsDir()
  2414.     lua_register(m_luaState, "getLogsDir", LuaInterface::luaGetLogsDir);
  2415.  
  2416.     //getConfigFile()
  2417.     lua_register(m_luaState, "getConfigFile", LuaInterface::luaGetConfigFile);
  2418.  
  2419.     //isPlayerUsingOtclient(cid)
  2420.     lua_register(m_luaState, "isPlayerUsingOtclient", LuaInterface::luaIsPlayerUsingOtclient);
  2421.  
  2422.     //doPlayerSendExtendedOpcode(cid, opcode, buffer)
  2423.     lua_register(m_luaState, "doPlayerSendExtendedOpcode", LuaInterface::luaDoPlayerSendExtendedOpcode);
  2424.  
  2425.     //getConfigValue(key)
  2426.     lua_register(m_luaState, "getConfigValue", LuaInterface::luaGetConfigValue);
  2427.  
  2428.     //getModList()
  2429.     lua_register(m_luaState, "getModList", LuaInterface::luaGetModList);
  2430.  
  2431.     //getHighscoreString(skillId)
  2432.     lua_register(m_luaState, "getHighscoreString", LuaInterface::luaGetHighscoreString);
  2433.  
  2434.     //getWaypointPosition(name)
  2435.     lua_register(m_luaState, "getWaypointPosition", LuaInterface::luaGetWaypointPosition);
  2436.  
  2437.     //doWaypointAddTemporial(name, pos)
  2438.     lua_register(m_luaState, "doWaypointAddTemporial", LuaInterface::luaDoWaypointAddTemporial);
  2439.  
  2440.     //getGameState()
  2441.     lua_register(m_luaState, "getGameState", LuaInterface::luaGetGameState);
  2442.  
  2443.     //doSetGameState(id)
  2444.     lua_register(m_luaState, "doSetGameState", LuaInterface::luaDoSetGameState);
  2445.  
  2446.     //doExecuteRaid(name)
  2447.     lua_register(m_luaState, "doExecuteRaid", LuaInterface::luaDoExecuteRaid);
  2448.  
  2449.     //doCreatureExecuteTalkAction(cid, text[, ignoreAccess = false[, channelId = CHANNEL_DEFAULT]])
  2450.     lua_register(m_luaState, "doCreatureExecuteTalkAction", LuaInterface::luaDoCreatureExecuteTalkAction);
  2451.  
  2452.     //doReloadInfo(id[, cid])
  2453.     lua_register(m_luaState, "doReloadInfo", LuaInterface::luaDoReloadInfo);
  2454.  
  2455.     //doSaveServer([shallow = false])
  2456.     lua_register(m_luaState, "doSaveServer", LuaInterface::luaDoSaveServer);
  2457.  
  2458.     //doCleanHouse(houseId)
  2459.     lua_register(m_luaState, "doCleanHouse", LuaInterface::luaDoCleanHouse);
  2460.  
  2461.     //doCleanMap()
  2462.     lua_register(m_luaState, "doCleanMap", LuaInterface::luaDoCleanMap);
  2463.  
  2464.     //doRefreshMap()
  2465.     lua_register(m_luaState, "doRefreshMap", LuaInterface::luaDoRefreshMap);
  2466.  
  2467.     //doGuildAddEnemy(guild, enemy, war, type)
  2468.     lua_register(m_luaState, "doGuildAddEnemy", LuaInterface::luaDoGuildAddEnemy);
  2469.  
  2470.     //doGuildRemoveEnemy(guild, enemy)
  2471.     lua_register(m_luaState, "doGuildRemoveEnemy", LuaInterface::luaDoGuildRemoveEnemy);
  2472.  
  2473.     //doUpdateHouseAuctions()
  2474.     lua_register(m_luaState, "doUpdateHouseAuctions", LuaInterface::luaDoUpdateHouseAuctions);
  2475.  
  2476.     //loadmodlib(lib)
  2477.     lua_register(m_luaState, "loadmodlib", LuaInterface::luaL_loadmodlib);
  2478.  
  2479.     //domodlib(lib)
  2480.     lua_register(m_luaState, "domodlib", LuaInterface::luaL_domodlib);
  2481.  
  2482.     //dodirectory(dir)
  2483.     lua_register(m_luaState, "dodirectory", LuaInterface::luaL_dodirectory);
  2484.  
  2485.     //errors(var)
  2486.     lua_register(m_luaState, "errors", LuaInterface::luaL_errors);
  2487.  
  2488.     //os table
  2489.     luaL_register(m_luaState, "os", LuaInterface::luaSystemTable);
  2490.  
  2491.     //db table
  2492.     luaL_register(m_luaState, "db", LuaInterface::luaDatabaseTable);
  2493.  
  2494.     //result table
  2495.     luaL_register(m_luaState, "result", LuaInterface::luaResultTable);
  2496.  
  2497.     //bit table
  2498.     luaL_register(m_luaState, "bit", LuaInterface::luaBitTable);
  2499.  
  2500.     //std table
  2501.     luaL_register(m_luaState, "std", LuaInterface::luaStdTable);
  2502. }
  2503.  
  2504. const luaL_Reg LuaInterface::luaSystemTable[] =
  2505. {
  2506.     //os.mtime()
  2507.     {"mtime", LuaInterface::luaSystemTime},
  2508.  
  2509.     {NULL, NULL}
  2510. };
  2511.  
  2512. const luaL_Reg LuaInterface::luaDatabaseTable[] =
  2513. {
  2514.     //db.query(query)
  2515.     {"query", LuaInterface::luaDatabaseExecute},
  2516.  
  2517.     //db.storeQuery(query)
  2518.     {"storeQuery", LuaInterface::luaDatabaseStoreQuery},
  2519.  
  2520.     //db.escapeString(str)
  2521.     {"escapeString", LuaInterface::luaDatabaseEscapeString},
  2522.  
  2523.     //db.escapeBlob(s, length)
  2524.     {"escapeBlob", LuaInterface::luaDatabaseEscapeBlob},
  2525.  
  2526.     //db.lastInsertId()
  2527.     {"lastInsertId", LuaInterface::luaDatabaseLastInsertId},
  2528.  
  2529.     //db.stringComparer()
  2530.     {"stringComparer", LuaInterface::luaDatabaseStringComparer},
  2531.  
  2532.     //db.updateLimiter()
  2533.     {"updateLimiter", LuaInterface::luaDatabaseUpdateLimiter},
  2534.  
  2535.     {NULL, NULL}
  2536. };
  2537.  
  2538. const luaL_Reg LuaInterface::luaResultTable[] =
  2539. {
  2540.     //result.getDataInt(resId, s)
  2541.     {"getDataInt", LuaInterface::luaResultGetDataInt},
  2542.  
  2543.     //result.getDataLong(resId, s)
  2544.     {"getDataLong", LuaInterface::luaResultGetDataLong},
  2545.  
  2546.     //result.getDataString(resId, s)
  2547.     {"getDataString", LuaInterface::luaResultGetDataString},
  2548.  
  2549.     //result.getDataStream(resId, s, length)
  2550.     {"getDataStream", LuaInterface::luaResultGetDataStream},
  2551.  
  2552.     //result.next(resId)
  2553.     {"next", LuaInterface::luaResultNext},
  2554.  
  2555.     //result.free(resId)
  2556.     {"free", LuaInterface::luaResultFree},
  2557.  
  2558.     {NULL, NULL}
  2559. };
  2560.  
  2561. const luaL_Reg LuaInterface::luaBitTable[] =
  2562. {
  2563.     //{"cast", LuaInterface::luaBitCast},
  2564.     {"bnot", LuaInterface::luaBitNot},
  2565.     {"band", LuaInterface::luaBitAnd},
  2566.     {"bor", LuaInterface::luaBitOr},
  2567.     {"bxor", LuaInterface::luaBitXor},
  2568.     {"lshift", LuaInterface::luaBitLeftShift},
  2569.     {"rshift", LuaInterface::luaBitRightShift},
  2570.     //{"arshift", LuaInterface::luaBitArithmeticalRightShift},
  2571.  
  2572.     //{"ucast", LuaInterface::luaBitUCast},
  2573.     {"ubnot", LuaInterface::luaBitUNot},
  2574.     {"uband", LuaInterface::luaBitUAnd},
  2575.     {"ubor", LuaInterface::luaBitUOr},
  2576.     {"ubxor", LuaInterface::luaBitUXor},
  2577.     {"ulshift", LuaInterface::luaBitULeftShift},
  2578.     {"urshift", LuaInterface::luaBitURightShift},
  2579.     //{"uarshift", LuaInterface::luaBitUArithmeticalRightShift},
  2580.  
  2581.     {NULL, NULL}
  2582. };
  2583.  
  2584. const luaL_Reg LuaInterface::luaStdTable[] =
  2585. {
  2586.     {"cout", LuaInterface::luaStdCout},
  2587.     {"clog", LuaInterface::luaStdClog},
  2588.     {"cerr", LuaInterface::luaStdCerr},
  2589.  
  2590.     {"md5", LuaInterface::luaStdMD5},
  2591.     {"sha1", LuaInterface::luaStdSHA1},
  2592.     {"sha256", LuaInterface::luaStdSHA256},
  2593.     {"sha512", LuaInterface::luaStdSHA512},
  2594.  
  2595.     {NULL, NULL}
  2596. };
  2597.  
  2598. int32_t LuaInterface::internalGetPlayerInfo(lua_State* L, PlayerInfo_t info)
  2599. {
  2600.     ScriptEnviroment* env = getEnv();
  2601.     const Player* player = env->getPlayerByUID(popNumber(L));
  2602.     if(!player)
  2603.     {
  2604.         std::stringstream s;
  2605.         s << getError(LUA_ERROR_PLAYER_NOT_FOUND) << " when requesting player info #" << info;
  2606.         errorEx(s.str());
  2607.  
  2608.         lua_pushboolean(L, false);
  2609.         return 1;
  2610.     }
  2611.  
  2612.     int64_t value = 0;
  2613.     Position pos;
  2614.     switch(info)
  2615.     {
  2616.         case PlayerInfoNameDescription:
  2617.             lua_pushstring(L, player->getNameDescription().c_str());
  2618.             return 1;
  2619.         case PlayerInfoSpecialDescription:
  2620.             lua_pushstring(L, player->getSpecialDescription().c_str());
  2621.             return 1;
  2622.         case PlayerInfoAccess:
  2623.             value = player->getAccess();
  2624.             break;
  2625.         case PlayerInfoGhostAccess:
  2626.             value = player->getGhostAccess();
  2627.             break;
  2628.         case PlayerInfoLevel:
  2629.             value = player->getLevel();
  2630.             break;
  2631.         case PlayerInfoExperience:
  2632.             value = player->getExperience();
  2633.             break;
  2634.         case PlayerInfoManaSpent:
  2635.             value = player->getSpentMana();
  2636.             break;
  2637.         case PlayerInfoTown:
  2638.             value = player->getTown();
  2639.             break;
  2640.         case PlayerInfoPromotionLevel:
  2641.             value = player->getPromotionLevel();
  2642.             break;
  2643.         case PlayerInfoGUID:
  2644.             value = player->getGUID();
  2645.             break;
  2646.         case PlayerInfoAccountId:
  2647.             value = player->getAccount();
  2648.             break;
  2649.         case PlayerInfoAccount:
  2650.             lua_pushstring(L, player->getAccountName().c_str());
  2651.             return 1;
  2652.         case PlayerInfoPremiumDays:
  2653.             value = player->getPremiumDays();
  2654.             break;
  2655.         case PlayerInfoFood:
  2656.         {
  2657.             if(Condition* condition = player->getCondition(CONDITION_REGENERATION, CONDITIONID_DEFAULT))
  2658.                 value = condition->getTicks() / 1000;
  2659.  
  2660.             break;
  2661.         }
  2662.         case PlayerInfoVocation:
  2663.             value = player->getVocationId();
  2664.             break;
  2665.         case PlayerInfoSoul:
  2666.             value = player->getSoul();
  2667.             break;
  2668.         case PlayerInfoFreeCap:
  2669.             value = (int64_t)player->getFreeCapacity();
  2670.             break;
  2671.         case PlayerInfoGuildId:
  2672.             value = player->getGuildId();
  2673.             break;
  2674.         case PlayerInfoGuildName:
  2675.             lua_pushstring(L, player->getGuildName().c_str());
  2676.             return 1;
  2677.         case PlayerInfoGuildRankId:
  2678.             value = player->getRankId();
  2679.             break;
  2680.         case PlayerInfoGuildRank:
  2681.             lua_pushstring(L, player->getRankName().c_str());
  2682.             return 1;
  2683.         case PlayerInfoGuildLevel:
  2684.             value = player->getGuildLevel();
  2685.             break;
  2686.         case PlayerInfoGuildNick:
  2687.             lua_pushstring(L, player->getGuildNick().c_str());
  2688.             return 1;
  2689.         case PlayerInfoGroupId:
  2690.             value = player->getGroupId();
  2691.             break;
  2692.         case PlayerInfoBalance:
  2693.             if(g_config.getBool(ConfigManager::BANK_SYSTEM))
  2694.                 lua_pushnumber(L, player->balance);
  2695.             else
  2696.                 lua_pushnumber(L, 0);
  2697.  
  2698.             return 1;
  2699.         case PlayerInfoStamina:
  2700.             value = player->getStaminaMinutes();
  2701.             break;
  2702.         case PlayerInfoLossSkill:
  2703.             lua_pushboolean(L, player->getLossSkill());
  2704.             return 1;
  2705.         case PlayerInfoMarriage:
  2706.             value = player->marriage;
  2707.             break;
  2708.         case PlayerInfoPzLock:
  2709.             lua_pushboolean(L, player->isPzLocked());
  2710.             return 1;
  2711.         case PlayerInfoSaving:
  2712.             lua_pushboolean(L, player->isSaving());
  2713.             return 1;
  2714.         case PlayerInfoIp:
  2715.             value = player->getIP();
  2716.             break;
  2717.         case PlayerInfoSkullEnd:
  2718.             value = player->getSkullEnd();
  2719.             break;
  2720.         case PlayerInfoOutfitWindow:
  2721.             player->sendOutfitWindow();
  2722.             lua_pushboolean(L, true);
  2723.             return 1;
  2724.         case PlayerInfoIdleTime:
  2725.             value = player->getIdleTime();
  2726.             break;
  2727.         case PlayerInfoClient:
  2728.             lua_pushboolean(L, player->hasClient());
  2729.             return 1;
  2730.         case PlayerInfoLastLoad:
  2731.             value = player->getLastLoad();
  2732.             break;
  2733.         case PlayerInfoLastLogin:
  2734.             value = player->getLastLogin();
  2735.             break;
  2736.         case PlayerInfoAccountManager:
  2737.             value = player->accountManager;
  2738.             break;
  2739.         case PlayerInfoTradeState:
  2740.             value = player->tradeState;
  2741.             break;
  2742.         default:
  2743.             errorEx("Unknown player info #" + info);
  2744.             value = 0;
  2745.             break;
  2746.     }
  2747.  
  2748.     lua_pushnumber(L, value);
  2749.     return 1;
  2750. }
  2751.  
  2752. //getPlayer[Info](uid)
  2753. int32_t LuaInterface::luaGetPlayerNameDescription(lua_State* L)
  2754. {
  2755.     return internalGetPlayerInfo(L, PlayerInfoNameDescription);
  2756. }
  2757.  
  2758. int32_t LuaInterface::luaGetPlayerSpecialDescription(lua_State* L)
  2759. {
  2760.     return internalGetPlayerInfo(L, PlayerInfoSpecialDescription);
  2761. }
  2762.  
  2763. int32_t LuaInterface::luaGetPlayerFood(lua_State* L)
  2764. {
  2765.     return internalGetPlayerInfo(L, PlayerInfoFood);
  2766. }
  2767.  
  2768. int32_t LuaInterface::luaGetPlayerAccess(lua_State* L)
  2769. {
  2770.     return internalGetPlayerInfo(L, PlayerInfoAccess);
  2771. }
  2772.  
  2773. int32_t LuaInterface::luaGetPlayerGhostAccess(lua_State* L)
  2774. {
  2775.     return internalGetPlayerInfo(L, PlayerInfoGhostAccess);
  2776. }
  2777.  
  2778. int32_t LuaInterface::luaGetPlayerLevel(lua_State* L)
  2779. {
  2780.     return internalGetPlayerInfo(L, PlayerInfoLevel);
  2781. }
  2782.  
  2783. int32_t LuaInterface::luaGetPlayerExperience(lua_State* L)
  2784. {
  2785.     return internalGetPlayerInfo(L, PlayerInfoExperience);
  2786. }
  2787.  
  2788. int32_t LuaInterface::luaGetPlayerSpentMana(lua_State* L)
  2789. {
  2790.     return internalGetPlayerInfo(L, PlayerInfoManaSpent);
  2791. }
  2792.  
  2793. int32_t LuaInterface::luaGetPlayerVocation(lua_State* L)
  2794. {
  2795.     return internalGetPlayerInfo(L, PlayerInfoVocation);
  2796. }
  2797.  
  2798. int32_t LuaInterface::luaGetPlayerSoul(lua_State* L)
  2799. {
  2800.     return internalGetPlayerInfo(L, PlayerInfoSoul);
  2801. }
  2802.  
  2803. int32_t LuaInterface::luaGetPlayerFreeCap(lua_State* L)
  2804. {
  2805.     return internalGetPlayerInfo(L, PlayerInfoFreeCap);
  2806. }
  2807.  
  2808. int32_t LuaInterface::luaGetPlayerGuildId(lua_State* L)
  2809. {
  2810.     return internalGetPlayerInfo(L, PlayerInfoGuildId);
  2811. }
  2812.  
  2813. int32_t LuaInterface::luaGetPlayerGuildName(lua_State* L)
  2814. {
  2815.     return internalGetPlayerInfo(L, PlayerInfoGuildName);
  2816. }
  2817.  
  2818. int32_t LuaInterface::luaGetPlayerGuildRankId(lua_State* L)
  2819. {
  2820.     return internalGetPlayerInfo(L, PlayerInfoGuildRankId);
  2821. }
  2822.  
  2823. int32_t LuaInterface::luaGetPlayerGuildRank(lua_State* L)
  2824. {
  2825.     return internalGetPlayerInfo(L, PlayerInfoGuildRank);
  2826. }
  2827.  
  2828. int32_t LuaInterface::luaGetPlayerGuildLevel(lua_State* L)
  2829. {
  2830.     return internalGetPlayerInfo(L, PlayerInfoGuildLevel);
  2831. }
  2832.  
  2833. int32_t LuaInterface::luaGetPlayerGuildNick(lua_State* L)
  2834. {
  2835.     return internalGetPlayerInfo(L, PlayerInfoGuildNick);
  2836. }
  2837.  
  2838. int32_t LuaInterface::luaGetPlayerTown(lua_State* L)
  2839. {
  2840.     return internalGetPlayerInfo(L, PlayerInfoTown);
  2841. }
  2842.  
  2843. int32_t LuaInterface::luaGetPlayerPromotionLevel(lua_State* L)
  2844. {
  2845.     return internalGetPlayerInfo(L, PlayerInfoPromotionLevel);
  2846. }
  2847.  
  2848. int32_t LuaInterface::luaGetPlayerGroupId(lua_State* L)
  2849. {
  2850.     return internalGetPlayerInfo(L, PlayerInfoGroupId);
  2851. }
  2852.  
  2853. int32_t LuaInterface::luaGetPlayerGUID(lua_State* L)
  2854. {
  2855.     return internalGetPlayerInfo(L, PlayerInfoGUID);
  2856. }
  2857.  
  2858. int32_t LuaInterface::luaGetPlayerAccountId(lua_State* L)
  2859. {
  2860.     return internalGetPlayerInfo(L, PlayerInfoAccountId);
  2861. }
  2862.  
  2863. int32_t LuaInterface::luaGetPlayerAccount(lua_State* L)
  2864. {
  2865.     return internalGetPlayerInfo(L, PlayerInfoAccount);
  2866. }
  2867.  
  2868. int32_t LuaInterface::luaGetPlayerPremiumDays(lua_State* L)
  2869. {
  2870.     return internalGetPlayerInfo(L, PlayerInfoPremiumDays);
  2871. }
  2872.  
  2873. int32_t LuaInterface::luaGetPlayerBalance(lua_State* L)
  2874. {
  2875.     return internalGetPlayerInfo(L, PlayerInfoBalance);
  2876. }
  2877.  
  2878. int32_t LuaInterface::luaGetPlayerStamina(lua_State* L)
  2879. {
  2880.     return internalGetPlayerInfo(L, PlayerInfoStamina);
  2881. }
  2882.  
  2883. int32_t LuaInterface::luaGetPlayerLossSkill(lua_State* L)
  2884. {
  2885.     return internalGetPlayerInfo(L, PlayerInfoLossSkill);
  2886. }
  2887.  
  2888. int32_t LuaInterface::luaGetPlayerPartner(lua_State* L)
  2889. {
  2890.     return internalGetPlayerInfo(L, PlayerInfoMarriage);
  2891. }
  2892.  
  2893. int32_t LuaInterface::luaIsPlayerPzLocked(lua_State* L)
  2894. {
  2895.     return internalGetPlayerInfo(L, PlayerInfoPzLock);
  2896. }
  2897.  
  2898. int32_t LuaInterface::luaIsPlayerSaving(lua_State* L)
  2899. {
  2900.     return internalGetPlayerInfo(L, PlayerInfoSaving);
  2901. }
  2902.  
  2903. int32_t LuaInterface::luaGetPlayerIp(lua_State* L)
  2904. {
  2905.     return internalGetPlayerInfo(L, PlayerInfoIp);
  2906. }
  2907.  
  2908. int32_t LuaInterface::luaGetPlayerSkullEnd(lua_State* L)
  2909. {
  2910.     return internalGetPlayerInfo(L, PlayerInfoSkullEnd);
  2911. }
  2912.  
  2913. int32_t LuaInterface::luaDoPlayerSendOutfitWindow(lua_State* L)
  2914. {
  2915.     return internalGetPlayerInfo(L, PlayerInfoOutfitWindow);
  2916. }
  2917.  
  2918. int32_t LuaInterface::luaGetPlayerIdleTime(lua_State* L)
  2919. {
  2920.     return internalGetPlayerInfo(L, PlayerInfoIdleTime);
  2921. }
  2922.  
  2923. int32_t LuaInterface::luaHasPlayerClient(lua_State* L)
  2924. {
  2925.     return internalGetPlayerInfo(L, PlayerInfoClient);
  2926. }
  2927.  
  2928. int32_t LuaInterface::luaGetPlayerLastLoad(lua_State* L)
  2929. {
  2930.     return internalGetPlayerInfo(L, PlayerInfoLastLoad);
  2931. }
  2932.  
  2933. int32_t LuaInterface::luaGetPlayerLastLogin(lua_State* L)
  2934. {
  2935.     return internalGetPlayerInfo(L, PlayerInfoLastLogin);
  2936. }
  2937.  
  2938. int32_t LuaInterface::luaGetPlayerAccountManager(lua_State* L)
  2939. {
  2940.     return internalGetPlayerInfo(L, PlayerInfoAccountManager);
  2941. }
  2942.  
  2943. int32_t LuaInterface::luaGetPlayerTradeState(lua_State* L)
  2944. {
  2945.     return internalGetPlayerInfo(L, PlayerInfoTradeState);
  2946. }
  2947. //
  2948.  
  2949. int32_t LuaInterface::luaGetPlayerSex(lua_State* L)
  2950. {
  2951.     //getPlayerSex(cid[, full = false])
  2952.     bool full = false;
  2953.     if(lua_gettop(L) > 1)
  2954.         full = popNumber(L);
  2955.  
  2956.     ScriptEnviroment* env = getEnv();
  2957.     Player* player = env->getPlayerByUID((uint32_t)popNumber(L));
  2958.     if(!player)
  2959.     {
  2960.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  2961.         lua_pushboolean(L, false);
  2962.     }
  2963.     else
  2964.         lua_pushnumber(L, player->getSex(full));
  2965.  
  2966.     return 1;
  2967. }
  2968.  
  2969. int32_t LuaInterface::luaDoPlayerSetNameDescription(lua_State* L)
  2970. {
  2971.     //doPlayerSetNameDescription(cid, description)
  2972.     std::string description = popString(L);
  2973.  
  2974.     ScriptEnviroment* env = getEnv();
  2975.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  2976.     {
  2977.         player->nameDescription += description;
  2978.         lua_pushboolean(L, true);
  2979.     }
  2980.     else
  2981.     {
  2982.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  2983.         lua_pushboolean(L, false);
  2984.     }
  2985.  
  2986.     return 1;
  2987. }
  2988.  
  2989. int32_t LuaInterface::luaDoPlayerSetSpecialDescription(lua_State* L)
  2990. {
  2991.     //doPlayerSetSpecialDescription(cid, description)
  2992.     std::string description = popString(L);
  2993.  
  2994.     ScriptEnviroment* env = getEnv();
  2995.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  2996.     {
  2997.         player->setSpecialDescription(description);
  2998.         lua_pushboolean(L, true);
  2999.     }
  3000.     else
  3001.     {
  3002.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3003.         lua_pushboolean(L, false);
  3004.     }
  3005.  
  3006.     return 1;
  3007. }
  3008.  
  3009. int32_t LuaInterface::luaGetPlayerMagLevel(lua_State* L)
  3010. {
  3011.     //getPlayerMagLevel(cid[, ignoreBuffs = false])
  3012.     bool ignoreBuffs = false;
  3013.     if(lua_gettop(L) > 1)
  3014.         ignoreBuffs = popNumber(L);
  3015.  
  3016.     ScriptEnviroment* env = getEnv();
  3017.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  3018.         lua_pushnumber(L, (ignoreBuffs ? player->magLevel : player->getMagicLevel()));
  3019.     else
  3020.     {
  3021.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3022.         lua_pushboolean(L, false);
  3023.     }
  3024.  
  3025.     return 1;
  3026. }
  3027.  
  3028. int32_t LuaInterface::luaGetPlayerRequiredMana(lua_State* L)
  3029. {
  3030.     //getPlayerRequiredMana(cid, magicLevel)
  3031.     uint32_t magLevel = popNumber(L);
  3032.  
  3033.     ScriptEnviroment* env = getEnv();
  3034.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3035.         lua_pushnumber(L, player->vocation->getReqMana(magLevel));
  3036.     else
  3037.     {
  3038.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3039.         lua_pushboolean(L, false);
  3040.     }
  3041.  
  3042.     return 1;
  3043. }
  3044.  
  3045. int32_t LuaInterface::luaGetPlayerRequiredSkillTries(lua_State* L)
  3046. {
  3047.     //getPlayerRequiredSkillTries(cid, skillId, skillLevel)
  3048.     int32_t sLevel = popNumber(L), sId = popNumber(L);
  3049.  
  3050.     ScriptEnviroment* env = getEnv();
  3051.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3052.         lua_pushnumber(L, player->vocation->getReqSkillTries(sId, sLevel));
  3053.     else
  3054.     {
  3055.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3056.         lua_pushboolean(L, false);
  3057.     }
  3058.  
  3059.     return 1;
  3060. }
  3061.  
  3062. int32_t LuaInterface::luaGetPlayerFlagValue(lua_State* L)
  3063. {
  3064.     //getPlayerFlagValue(cid, flag)
  3065.     uint32_t index = popNumber(L);
  3066.  
  3067.     ScriptEnviroment* env = getEnv();
  3068.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3069.     {
  3070.         if(index < PlayerFlag_LastFlag)
  3071.             lua_pushboolean(L, player->hasFlag((PlayerFlags)index));
  3072.         else
  3073.         {
  3074.             std::stringstream ss;
  3075.             ss << index;
  3076.             errorEx("No valid flag index - " + ss.str());
  3077.             lua_pushboolean(L, false);
  3078.         }
  3079.     }
  3080.     else
  3081.     {
  3082.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3083.         lua_pushboolean(L, false);
  3084.     }
  3085.  
  3086.     return 1;
  3087. }
  3088.  
  3089. int32_t LuaInterface::luaGetPlayerCustomFlagValue(lua_State* L)
  3090. {
  3091.     //getPlayerCustomFlagValue(cid, flag)
  3092.     uint32_t index = popNumber(L);
  3093.  
  3094.     ScriptEnviroment* env = getEnv();
  3095.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3096.     {
  3097.         if(index < PlayerCustomFlag_LastFlag)
  3098.             lua_pushboolean(L, player->hasCustomFlag((PlayerCustomFlags)index));
  3099.         else
  3100.         {
  3101.             std::stringstream ss;
  3102.             ss << index;
  3103.             errorEx("No valid flag index - " + ss.str());
  3104.             lua_pushboolean(L, false);
  3105.         }
  3106.     }
  3107.     else
  3108.     {
  3109.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3110.         lua_pushboolean(L, false);
  3111.     }
  3112.  
  3113.     return 1;
  3114. }
  3115.  
  3116. int32_t LuaInterface::luaDoPlayerLearnInstantSpell(lua_State* L)
  3117. {
  3118.     //doPlayerLearnInstantSpell(cid, name)
  3119.     std::string spellName = popString(L);
  3120.  
  3121.     ScriptEnviroment* env = getEnv();
  3122.     Player* player = env->getPlayerByUID(popNumber(L));
  3123.     if(!player)
  3124.     {
  3125.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3126.         lua_pushboolean(L, false);
  3127.         return 1;
  3128.     }
  3129.  
  3130.     InstantSpell* spell = g_spells->getInstantSpellByName(spellName);
  3131.     if(!spell)
  3132.     {
  3133.         lua_pushboolean(L, false);
  3134.         return 1;
  3135.     }
  3136.  
  3137.     player->learnInstantSpell(spell->getName());
  3138.     lua_pushboolean(L, true);
  3139.     return 1;
  3140. }
  3141.  
  3142. int32_t LuaInterface::luaDoPlayerUnlearnInstantSpell(lua_State* L)
  3143. {
  3144.     //doPlayerUnlearnInstantSpell(cid, name)
  3145.     std::string spellName = popString(L);
  3146.  
  3147.     ScriptEnviroment* env = getEnv();
  3148.     Player* player = env->getPlayerByUID(popNumber(L));
  3149.     if(!player)
  3150.     {
  3151.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3152.         lua_pushboolean(L, false);
  3153.         return 1;
  3154.     }
  3155.  
  3156.     InstantSpell* spell = g_spells->getInstantSpellByName(spellName);
  3157.     if(!spell)
  3158.     {
  3159.         lua_pushboolean(L, false);
  3160.         return 1;
  3161.     }
  3162.  
  3163.     player->unlearnInstantSpell(spell->getName());
  3164.     lua_pushboolean(L, true);
  3165.     return 1;
  3166. }
  3167.  
  3168. int32_t LuaInterface::luaGetPlayerLearnedInstantSpell(lua_State* L)
  3169. {
  3170.     //getPlayerLearnedInstantSpell(cid, name)
  3171.     std::string spellName = popString(L);
  3172.  
  3173.     ScriptEnviroment* env = getEnv();
  3174.     Player* player = env->getPlayerByUID(popNumber(L));
  3175.     if(!player)
  3176.     {
  3177.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3178.         lua_pushboolean(L, false);
  3179.         return 1;
  3180.     }
  3181.  
  3182.     InstantSpell* spell = g_spells->getInstantSpellByName(spellName);
  3183.     if(!spell)
  3184.     {
  3185.         lua_pushboolean(L, false);
  3186.         return 1;
  3187.     }
  3188.  
  3189.     lua_pushboolean(L, player->hasLearnedInstantSpell(spellName));
  3190.     return 1;
  3191. }
  3192.  
  3193. int32_t LuaInterface::luaGetPlayerInstantSpellCount(lua_State* L)
  3194. {
  3195.     //getPlayerInstantSpellCount(cid)
  3196.     ScriptEnviroment* env = getEnv();
  3197.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3198.         lua_pushnumber(L, g_spells->getInstantSpellCount(player));
  3199.     else
  3200.     {
  3201.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3202.         lua_pushboolean(L, false);
  3203.     }
  3204.  
  3205.     return 1;
  3206. }
  3207.  
  3208. int32_t LuaInterface::luaGetPlayerInstantSpellInfo(lua_State* L)
  3209. {
  3210.     //getPlayerInstantSpellInfo(cid, index)
  3211.     uint32_t index = popNumber(L);
  3212.  
  3213.     ScriptEnviroment* env = getEnv();
  3214.     Player* player = env->getPlayerByUID(popNumber(L));
  3215.     if(!player)
  3216.     {
  3217.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3218.         lua_pushboolean(L, false);
  3219.         return 1;
  3220.     }
  3221.  
  3222.     InstantSpell* spell = g_spells->getInstantSpellByIndex(player, index);
  3223.     if(!spell)
  3224.     {
  3225.         errorEx(getError(LUA_ERROR_SPELL_NOT_FOUND));
  3226.         lua_pushboolean(L, false);
  3227.         return 1;
  3228.     }
  3229.  
  3230.     lua_newtable(L);
  3231.     setField(L, "name", spell->getName());
  3232.     setField(L, "words", spell->getWords());
  3233.     setField(L, "level", spell->getLevel());
  3234.     setField(L, "mlevel", spell->getMagicLevel());
  3235.     setField(L, "mana", spell->getManaCost(player));
  3236.     setField(L, "manapercent", spell->getManaPercent());
  3237.     return 1;
  3238. }
  3239.  
  3240. int32_t LuaInterface::luaGetInstantSpellInfo(lua_State* L)
  3241. {
  3242.     //getInstantSpellInfo(name)
  3243.     InstantSpell* spell = g_spells->getInstantSpellByName(popString(L));
  3244.     if(!spell)
  3245.     {
  3246.         errorEx(getError(LUA_ERROR_SPELL_NOT_FOUND));
  3247.         lua_pushboolean(L, false);
  3248.         return 1;
  3249.     }
  3250.  
  3251.     lua_newtable(L);
  3252.     setField(L, "name", spell->getName());
  3253.     setField(L, "words", spell->getWords());
  3254.     setField(L, "level", spell->getLevel());
  3255.     setField(L, "mlevel", spell->getMagicLevel());
  3256.     setField(L, "mana", spell->getManaCost(NULL));
  3257.     setField(L, "manapercent", spell->getManaPercent());
  3258.     return 1;
  3259. }
  3260.  
  3261. int32_t LuaInterface::luaDoRemoveItem(lua_State* L)
  3262. {
  3263.     //doRemoveItem(uid[, count = -1])
  3264.     int32_t count = -1;
  3265.     if(lua_gettop(L) > 1)
  3266.         count = popNumber(L);
  3267.  
  3268.     ScriptEnviroment* env = getEnv();
  3269.     Item* item = env->getItemByUID(popNumber(L));
  3270.     if(!item)
  3271.     {
  3272.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  3273.         lua_pushboolean(L, false);
  3274.         return 1;
  3275.     }
  3276.  
  3277.     if(g_game.internalRemoveItem(NULL, item, count) != RET_NOERROR)
  3278.     {
  3279.         lua_pushboolean(L, false);
  3280.         return 1;
  3281.     }
  3282.  
  3283.     lua_pushboolean(L, true);
  3284.     return 1;
  3285. }
  3286.  
  3287. int32_t LuaInterface::luaDoPlayerRemoveItem(lua_State* L)
  3288. {
  3289.     //doPlayerRemoveItem(cid, itemid, count[, subType = -1])
  3290.     int32_t subType = -1;
  3291.     if(lua_gettop(L) > 3)
  3292.         subType = popNumber(L);
  3293.  
  3294.     uint32_t count = popNumber(L);
  3295.     uint16_t itemId = (uint16_t)popNumber(L);
  3296.  
  3297.     ScriptEnviroment* env = getEnv();
  3298.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3299.         lua_pushboolean(L, g_game.removeItemOfType(player, itemId, count, subType));
  3300.     else
  3301.     {
  3302.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3303.         lua_pushboolean(L, false);
  3304.     }
  3305.  
  3306.     return 1;
  3307. }
  3308.  
  3309. int32_t LuaInterface::luaDoPlayerFeed(lua_State* L)
  3310. {
  3311.     //doPlayerFeed(cid, food)
  3312.     int32_t food = (int32_t)popNumber(L);
  3313.  
  3314.     ScriptEnviroment* env = getEnv();
  3315.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3316.     {
  3317.         player->addDefaultRegeneration((food * 1000) * 3);
  3318.         lua_pushboolean(L, true);
  3319.     }
  3320.     else
  3321.     {
  3322.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3323.         lua_pushboolean(L, false);
  3324.     }
  3325.  
  3326.     return 1;
  3327. }
  3328.  
  3329.  
  3330. int32_t LuaInterface::luaGetPlayerCastBans(lua_State* L)
  3331. {
  3332.     //getPlayerCastBan(cid)
  3333.     ScriptEnviroment* env = getEnv();
  3334.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3335.     {
  3336.         PlayerCast pc = player->getCast();
  3337.         lua_newtable(L);
  3338.         for(std::list<CastBan>::iterator it = pc.bans.begin(); it != pc.bans.end(); ++it)
  3339.         {
  3340.             createTable(L, it->ip);
  3341.             setField(L, "name", it->name);
  3342.             pushTable(L);
  3343.         }
  3344.     }
  3345.     else
  3346.     {
  3347.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3348.         lua_pushboolean(L, false);
  3349.     }
  3350.  
  3351.     return 1;
  3352. }
  3353.  
  3354.  
  3355. int32_t LuaInterface::luaGetPlayerCastMutes(lua_State* L)
  3356. {
  3357.     //getPlayerCastMutes(cid)
  3358.     ScriptEnviroment* env = getEnv();
  3359.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3360.     {
  3361.         PlayerCast pc = player->getCast();
  3362.         lua_newtable(L);
  3363.         for(std::list<CastBan>::iterator it = pc.muted.begin(); it != pc.muted.end(); ++it)
  3364.         {
  3365.             createTable(L, it->ip);
  3366.             setField(L, "name", it->name);
  3367.             pushTable(L);
  3368.         }
  3369.     }
  3370.     else
  3371.     {
  3372.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3373.         lua_pushboolean(L, false);
  3374.     }
  3375.  
  3376.     return 1;
  3377. }
  3378.  
  3379. int32_t LuaInterface::luaDoPlayerRemoveCastMute(lua_State* L)
  3380. {
  3381.     //doPlayerRemoveCastMute(cid, ip)
  3382.     std::string name = popString(L);
  3383.     ScriptEnviroment* env = getEnv();
  3384.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3385.     {
  3386.         if(player->removeCastMute(name))
  3387.             lua_pushboolean(L, true);
  3388.         else
  3389.             lua_pushboolean(L, false);
  3390.     }
  3391.     else
  3392.     {
  3393.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3394.         lua_pushboolean(L, false);
  3395.     }
  3396.  
  3397.     return 1;
  3398. }
  3399.  
  3400. int32_t LuaInterface::luaDoPlayerAddCastMute(lua_State* L)
  3401. {
  3402.     //doPlayerAddCastMute(cid, ip)
  3403.     std::string name = popString(L);
  3404.     ScriptEnviroment* env = getEnv();
  3405.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3406.     {
  3407.         if(player->addCastMute(name))
  3408.             lua_pushboolean(L, true);
  3409.         else
  3410.             lua_pushboolean(L, false);
  3411.     }
  3412.     else
  3413.     {
  3414.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3415.         lua_pushboolean(L, false);
  3416.     }
  3417.  
  3418.     return 1;
  3419. }
  3420.  
  3421. int32_t LuaInterface::luaGetPlayerCastViewers(lua_State* L)
  3422. {
  3423.     //getPlayerCastBan(cid)
  3424.     ScriptEnviroment* env = getEnv();
  3425.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3426.     {
  3427.         PlayerCast pc = player->getCast();
  3428.         lua_newtable(L);
  3429.         for(AutoList<ProtocolGame>::iterator it = Player::cSpectators.begin(); it != Player::cSpectators.end(); ++it)
  3430.         {
  3431.             if(it->second->getPlayer() != player)
  3432.                 continue;
  3433.  
  3434.             createTable(L, it->first);
  3435.             setField(L, "name", it->second->getViewerName());
  3436.             setField(L, "ip", it->second->getIP());
  3437.             pushTable(L);
  3438.         }
  3439.     }
  3440.     else
  3441.     {
  3442.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3443.         lua_pushboolean(L, false);
  3444.     }
  3445.  
  3446.     return 1;
  3447. }
  3448.  
  3449.  
  3450. int32_t LuaInterface::luaDoPlayerRemoveCastBan(lua_State* L)
  3451. {
  3452.     //doPlayerRemoveCastBan(cid, ip)
  3453.     std::string name = popString(L);
  3454.     ScriptEnviroment* env = getEnv();
  3455.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3456.     {
  3457.         if(player->removeCastBan(name))
  3458.             lua_pushboolean(L, true);
  3459.         else
  3460.             lua_pushboolean(L, false);
  3461.     }
  3462.     else
  3463.     {
  3464.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3465.         lua_pushboolean(L, false);
  3466.     }
  3467.  
  3468.     return 1;
  3469. }
  3470.  
  3471. int32_t LuaInterface::luaDoPlayerAddCastBan(lua_State* L)
  3472. {
  3473.     //doPlayerAddCastBan(cid, ip)
  3474.     std::string name = popString(L);
  3475.     ScriptEnviroment* env = getEnv();
  3476.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3477.     {
  3478.         if(player->addCastBan(name))
  3479.             lua_pushboolean(L, true);
  3480.         else
  3481.             lua_pushboolean(L, false);
  3482.     }
  3483.     else
  3484.     {
  3485.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3486.         lua_pushboolean(L, false);
  3487.     }
  3488.  
  3489.     return 1;
  3490. }
  3491.  
  3492.  
  3493. int32_t LuaInterface::luaDoPlayerSetCastPassword(lua_State* L)
  3494. {
  3495.     //doPlayerSetCastPassword(cid, password)
  3496.     std::string str = popString(L);
  3497.     ScriptEnviroment* env = getEnv();
  3498.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3499.     {
  3500.         player->setCastPassword(str);
  3501.         lua_pushboolean(L, true);
  3502.     }
  3503.     else
  3504.     {
  3505.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3506.         lua_pushboolean(L, false);
  3507.     }
  3508.  
  3509.     return 1;
  3510. }
  3511.  
  3512. int32_t LuaInterface::luaDoPlayerSetCastDescription(lua_State* L)
  3513. {
  3514.     //doPlayerSetCastPassword(cid, password)
  3515.     std::string str = popString(L);
  3516.     ScriptEnviroment* env = getEnv();
  3517.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3518.     {
  3519.         player->setCastDescription(str);
  3520.         lua_pushboolean(L, true);
  3521.     }
  3522.     else
  3523.     {
  3524.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3525.         lua_pushboolean(L, false);
  3526.     }
  3527.  
  3528.     return 1;
  3529. }
  3530.  
  3531. int32_t LuaInterface::luaDoPlayerSetCastState(lua_State* L)
  3532. {
  3533.     //doPlayerSetCastState(cid, bool)
  3534.     bool state = popNumber(L);
  3535.     ScriptEnviroment* env = getEnv();
  3536.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3537.     {
  3538.         player->setCasting(state);
  3539.         lua_pushboolean(L, true);
  3540.     }
  3541.     else
  3542.     {
  3543.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3544.         lua_pushboolean(L, false);
  3545.     }
  3546.  
  3547.     return 1;
  3548. }
  3549.  
  3550. int32_t LuaInterface::luaGetPlayerCast(lua_State* L)
  3551. {
  3552.     //getPlayerCast(cid)
  3553.     ScriptEnviroment* env = getEnv();
  3554.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  3555.     {
  3556.         lua_newtable(L);
  3557.         setFieldBool(L, "status", player->getCastingState());
  3558.         setField(L, "password", player->getCastingPassword());
  3559.         setField(L, "description", player->getCastDescription());
  3560.     }
  3561.     else
  3562.     {
  3563.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3564.         lua_pushboolean(L, false);
  3565.     }
  3566.  
  3567.     return 1;
  3568. }
  3569.  
  3570. int32_t LuaInterface::luaGetCastsOnline(lua_State* L)
  3571. {
  3572.     //
  3573.     ScriptEnviroment* env = getEnv();
  3574.     AutoList<Player>::iterator it = Player::castAutoList.begin();
  3575.  
  3576.     lua_newtable(L);
  3577.     for(int32_t i = 1; it != Player::castAutoList.end(); ++it, ++i)
  3578.     {
  3579.         lua_pushnumber(L, i);
  3580.         lua_pushnumber(L, env->addThing(it->second));
  3581.         pushTable(L);
  3582.     }
  3583.  
  3584.     return 1;
  3585. }
  3586.  
  3587.  
  3588. int32_t LuaInterface::luaDoPlayerSendCancel(lua_State* L)
  3589. {
  3590.     //doPlayerSendCancel(cid, text)
  3591.     std::string text = popString(L);
  3592.     ScriptEnviroment* env = getEnv();
  3593.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  3594.     {
  3595.         player->sendCancel(text);
  3596.         lua_pushboolean(L, true);
  3597.     }
  3598.     else
  3599.     {
  3600.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3601.         lua_pushboolean(L, false);
  3602.     }
  3603.  
  3604.     return 1;
  3605. }
  3606.  
  3607. int32_t LuaInterface::luaDoSendDefaultCancel(lua_State* L)
  3608. {
  3609.     //doPlayerSendDefaultCancel(cid, ReturnValue)
  3610.     ReturnValue ret = (ReturnValue)popNumber(L);
  3611.     ScriptEnviroment* env = getEnv();
  3612.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  3613.     {
  3614.         player->sendCancelMessage(ret);
  3615.         lua_pushboolean(L, true);
  3616.     }
  3617.     else
  3618.     {
  3619.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3620.         lua_pushboolean(L, false);
  3621.     }
  3622.  
  3623.     return 1;
  3624. }
  3625.  
  3626. int32_t LuaInterface::luaGetSearchString(lua_State* L)
  3627. {
  3628.     //getSearchString(fromPosition, toPosition[, fromIsCreature = false[, toIsCreature = false]])
  3629.     PositionEx toPos, fromPos;
  3630.     bool toIsCreature = false, fromIsCreature = false;
  3631.  
  3632.     int32_t params = lua_gettop(L);
  3633.     if(params > 3)
  3634.         toIsCreature = popNumber(L);
  3635.  
  3636.     if(params > 2)
  3637.         fromIsCreature = popNumber(L);
  3638.  
  3639.     popPosition(L, toPos);
  3640.     popPosition(L, fromPos);
  3641.     if(!toPos.x || !toPos.y || !fromPos.x || !fromPos.y)
  3642.     {
  3643.         errorEx("wrong position(s) specified.");
  3644.         lua_pushboolean(L, false);
  3645.     }
  3646.     else
  3647.         lua_pushstring(L, g_game.getSearchString(fromPos, toPos, fromIsCreature, toIsCreature).c_str());
  3648.  
  3649.     return 1;
  3650. }
  3651.  
  3652. int32_t LuaInterface::luaGetClosestFreeTile(lua_State* L)
  3653. {
  3654.     //getClosestFreeTile(cid, targetPos[, extended = false[, ignoreHouse = true]])
  3655.     uint32_t params = lua_gettop(L);
  3656.     bool ignoreHouse = true, extended = false;
  3657.     if(params > 3)
  3658.         ignoreHouse = popNumber(L);
  3659.  
  3660.     if(params > 2)
  3661.         extended = popNumber(L);
  3662.  
  3663.     PositionEx pos;
  3664.     popPosition(L, pos);
  3665.  
  3666.     ScriptEnviroment* env = getEnv();
  3667.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3668.     {
  3669.         Position newPos = g_game.getClosestFreeTile(creature, pos, extended, ignoreHouse);
  3670.         if(newPos.x != 0)
  3671.             pushPosition(L, newPos, 0);
  3672.         else
  3673.             lua_pushboolean(L, false);
  3674.     }
  3675.     else
  3676.     {
  3677.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3678.         lua_pushboolean(L, false);
  3679.     }
  3680.  
  3681.     return 1;
  3682. }
  3683.  
  3684. int32_t LuaInterface::luaDoTeleportThing(lua_State* L)
  3685. {
  3686.     //doTeleportThing(cid, newpos[, pushMove = true[, fullTeleport = true]])
  3687.     bool fullTeleport = true, pushMove = true;
  3688.     int32_t params = lua_gettop(L);
  3689.     if(params > 3)
  3690.         fullTeleport = popNumber(L);
  3691.  
  3692.     if(params > 2)
  3693.         pushMove = popNumber(L);
  3694.  
  3695.     PositionEx pos;
  3696.     popPosition(L, pos);
  3697.  
  3698.     ScriptEnviroment* env = getEnv();
  3699.     if(Thing* tmp = env->getThingByUID(popNumber(L)))
  3700.         lua_pushboolean(L, g_game.internalTeleport(tmp, pos, !pushMove, FLAG_NOLIMIT, fullTeleport) == RET_NOERROR);
  3701.     else
  3702.     {
  3703.         errorEx(getError(LUA_ERROR_THING_NOT_FOUND));
  3704.         lua_pushboolean(L, false);
  3705.     }
  3706.  
  3707.     return 1;
  3708. }
  3709.  
  3710. int32_t LuaInterface::luaDoTransformItem(lua_State* L)
  3711. {
  3712.     //doTransformItem(uid, newId[, count/subType])
  3713.     int32_t count = -1;
  3714.     if(lua_gettop(L) > 2)
  3715.         count = popNumber(L);
  3716.  
  3717.     uint16_t newId = popNumber(L);
  3718.     uint32_t uid = popNumber(L);
  3719.     ScriptEnviroment* env = getEnv();
  3720.  
  3721.     Item* item = env->getItemByUID(uid);
  3722.     if(!item)
  3723.     {
  3724.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  3725.         lua_pushboolean(L, false);
  3726.         return 1;
  3727.     }
  3728.  
  3729.     const ItemType& it = Item::items[newId];
  3730.     if(it.stackable && count > 100)
  3731.         count = 100;
  3732.  
  3733.     Item* newItem = g_game.transformItem(item, newId, count);
  3734.     if(item->isRemoved())
  3735.         env->removeThing(uid);
  3736.  
  3737.     if(newItem && newItem != item)
  3738.         env->insertThing(uid, newItem);
  3739.  
  3740.     lua_pushboolean(L, true);
  3741.     return 1;
  3742. }
  3743.  
  3744. int32_t LuaInterface::luaDoCreatureSay(lua_State* L)
  3745. {
  3746.     //doCreatureSay(uid, text[, type = SPEAK_SAY[, ghost = false[, cid = 0[, pos]]]])
  3747.     uint32_t params = lua_gettop(L), cid = 0, uid = 0;
  3748.     PositionEx pos;
  3749.     if(params > 5)
  3750.         popPosition(L, pos);
  3751.  
  3752.     if(params > 4)
  3753.         cid = popNumber(L);
  3754.  
  3755.     bool ghost = false;
  3756.     if(params > 3)
  3757.         ghost = popNumber(L);
  3758.  
  3759.     SpeakClasses type = SPEAK_SAY;
  3760.     if(params > 2)
  3761.         type = (SpeakClasses)popNumber(L);
  3762.  
  3763.     std::string text = popString(L);
  3764.  
  3765.     uid = popNumber(L);
  3766.     if(params > 5 && (!pos.x || !pos.y))
  3767.     {
  3768.         errorEx("Invalid position specified.");
  3769.         lua_pushboolean(L, false);
  3770.         return 1;
  3771.     }
  3772.  
  3773.     ScriptEnviroment* env = getEnv();
  3774.     Creature* creature = env->getCreatureByUID(uid);
  3775.     if(!creature)
  3776.     {
  3777.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3778.         lua_pushboolean(L, false);
  3779.         return 1;
  3780.     }
  3781.  
  3782.     SpectatorVec list;
  3783.     if(cid)
  3784.     {
  3785.         Creature* target = env->getCreatureByUID(cid);
  3786.         if(!target)
  3787.         {
  3788.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3789.             lua_pushboolean(L, false);
  3790.             return 1;
  3791.         }
  3792.  
  3793.         list.push_back(target);
  3794.     }
  3795.  
  3796.     if(params > 5)
  3797.         lua_pushboolean(L, g_game.internalCreatureSay(creature, type, text, ghost, &list, &pos));
  3798.     else
  3799.         lua_pushboolean(L, g_game.internalCreatureSay(creature, type, text, ghost, &list));
  3800.  
  3801.     return 1;
  3802. }
  3803.  
  3804. int32_t LuaInterface::luaDoSendMagicEffect(lua_State* L)
  3805. {
  3806.     //doSendMagicEffect(pos, type[, player])
  3807.     ScriptEnviroment* env = getEnv();
  3808.     SpectatorVec list;
  3809.     if(lua_gettop(L) > 2)
  3810.     {
  3811.         if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3812.             list.push_back(creature);
  3813.     }
  3814.  
  3815.     uint32_t type = popNumber(L);
  3816.     PositionEx pos;
  3817.  
  3818.     popPosition(L, pos);
  3819.     if(pos.x == 0xFFFF)
  3820.         pos = env->getRealPos();
  3821.  
  3822.     if(!list.empty())
  3823.         g_game.addMagicEffect(list, pos, type);
  3824.     else
  3825.         g_game.addMagicEffect(pos, type);
  3826.  
  3827.     lua_pushboolean(L, true);
  3828.     return 1;
  3829. }
  3830.  
  3831. int32_t LuaInterface::luaDoSendDistanceShoot(lua_State* L)
  3832. {
  3833.     //doSendDistanceShoot(fromPos, toPos, type[, player])
  3834.     ScriptEnviroment* env = getEnv();
  3835.     SpectatorVec list;
  3836.     if(lua_gettop(L) > 3)
  3837.     {
  3838.         if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3839.             list.push_back(creature);
  3840.     }
  3841.  
  3842.     uint32_t type = popNumber(L);
  3843.     PositionEx toPos, fromPos;
  3844.  
  3845.     popPosition(L, toPos);
  3846.     popPosition(L, fromPos);
  3847.     if(fromPos.x == 0xFFFF)
  3848.         fromPos = env->getRealPos();
  3849.  
  3850.     if(toPos.x == 0xFFFF)
  3851.         toPos = env->getRealPos();
  3852.  
  3853.     if(!list.empty())
  3854.         g_game.addDistanceEffect(list, fromPos, toPos, type);
  3855.     else
  3856.         g_game.addDistanceEffect(fromPos, toPos, type);
  3857.  
  3858.     lua_pushboolean(L, true);
  3859.     return 1;
  3860. }
  3861.  
  3862. int32_t LuaInterface::luaDoPlayerAddSkillTry(lua_State* L)
  3863. {
  3864.     //doPlayerAddSkillTry(uid, skillid, n[, useMultiplier = true])
  3865.     bool multiplier = true;
  3866.     if(lua_gettop(L) > 3)
  3867.         multiplier = popNumber(L);
  3868.  
  3869.     uint32_t n = popNumber(L), skillid = popNumber(L);
  3870.     ScriptEnviroment* env = getEnv();
  3871.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3872.     {
  3873.         player->addSkillAdvance((skills_t)skillid, n, multiplier);
  3874.         lua_pushboolean(L, true);
  3875.     }
  3876.     else
  3877.     {
  3878.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3879.         lua_pushboolean(L, false);
  3880.     }
  3881.  
  3882.     return 1;
  3883. }
  3884.  
  3885. int32_t LuaInterface::luaGetCreatureSpeakType(lua_State* L)
  3886. {
  3887.     //getCreatureSpeakType(uid)
  3888.     ScriptEnviroment* env = getEnv();
  3889.     if(const Creature* creature = env->getCreatureByUID(popNumber(L)))
  3890.         lua_pushnumber(L, (SpeakClasses)creature->getSpeakType());
  3891.     else
  3892.     {
  3893.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3894.         lua_pushboolean(L, false);
  3895.     }
  3896.  
  3897.     return 1;
  3898. }
  3899.  
  3900. int32_t LuaInterface::luaDoCreatureSetSpeakType(lua_State* L)
  3901. {
  3902.     //doCreatureSetSpeakType(uid, type)
  3903.     SpeakClasses type = (SpeakClasses)popNumber(L);
  3904.  
  3905.     ScriptEnviroment* env = getEnv();
  3906.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3907.     {
  3908.         if(type < SPEAK_CLASS_FIRST || type > SPEAK_CLASS_LAST)
  3909.         {
  3910.             errorEx("Invalid speak type!");
  3911.             lua_pushboolean(L, false);
  3912.             return 1;
  3913.         }
  3914.  
  3915.         creature->setSpeakType(type);
  3916.         lua_pushboolean(L, true);
  3917.     }
  3918.     else
  3919.     {
  3920.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3921.         lua_pushboolean(L, false);
  3922.     }
  3923.  
  3924.     return 1;
  3925. }
  3926.  
  3927. int32_t LuaInterface::luaGetCreatureHideHealth(lua_State* L)
  3928. {
  3929.     //getCreatureHideHealth(cid)
  3930.     ScriptEnviroment* env = getEnv();
  3931.  
  3932.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3933.         lua_pushboolean(L, creature->getHideHealth());
  3934.     else
  3935.     {
  3936.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3937.         lua_pushboolean(L, false);
  3938.     }
  3939.  
  3940.     return 1;
  3941. }
  3942.  
  3943. int32_t LuaInterface::luaDoCreatureSetHideHealth(lua_State* L)
  3944. {
  3945.     //doCreatureSetHideHealth(cid, hide)
  3946.     bool hide = popNumber(L);
  3947.  
  3948.     ScriptEnviroment* env = getEnv();
  3949.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3950.     {
  3951.         creature->setHideHealth(hide);
  3952.         g_game.addCreatureHealth(creature);
  3953.         lua_pushboolean(L, true);
  3954.     }
  3955.     else
  3956.     {
  3957.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3958.         lua_pushboolean(L, false);
  3959.     }
  3960.  
  3961.     return 1;
  3962. }
  3963.  
  3964. int32_t LuaInterface::luaDoCreatureAddHealth(lua_State* L)
  3965. {
  3966.     //doCreatureAddHealth(uid, health[, hitEffect[, hitColor[, force]]])
  3967.     int32_t params = lua_gettop(L);
  3968.     bool force = false;
  3969.     if(params > 4)
  3970.         force = popNumber(L);
  3971.  
  3972.     Color_t hitColor = COLOR_UNKNOWN;
  3973.     if(params > 3)
  3974.         hitColor = (Color_t)popNumber(L);
  3975.  
  3976.     MagicEffect_t hitEffect = MAGIC_EFFECT_UNKNOWN;
  3977.     if(params > 2)
  3978.         hitEffect = (MagicEffect_t)popNumber(L);
  3979.  
  3980.     int32_t healthChange = popNumber(L);
  3981.     ScriptEnviroment* env = getEnv();
  3982.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3983.     {
  3984.         if(healthChange) //do not post with 0 value
  3985.             g_game.combatChangeHealth(healthChange < 1 ? COMBAT_UNDEFINEDDAMAGE : COMBAT_HEALING,
  3986.                 NULL, creature, healthChange, hitEffect, hitColor, force);
  3987.  
  3988.         lua_pushboolean(L, true);
  3989.     }
  3990.     else
  3991.     {
  3992.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3993.         lua_pushboolean(L, false);
  3994.     }
  3995.  
  3996.     return 1;
  3997. }
  3998.  
  3999. int32_t LuaInterface::luaDoCreatureAddMana(lua_State* L)
  4000. {
  4001.     //doCreatureAddMana(uid, mana[, aggressive])
  4002.     bool aggressive = true;
  4003.     if(lua_gettop(L) > 2)
  4004.         aggressive = popNumber(L);
  4005.  
  4006.     int32_t manaChange = popNumber(L);
  4007.     ScriptEnviroment* env = getEnv();
  4008.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  4009.     {
  4010.         if(aggressive)
  4011.             g_game.combatChangeMana(NULL, creature, manaChange);
  4012.         else
  4013.             creature->changeMana(manaChange);
  4014.  
  4015.         lua_pushboolean(L, true);
  4016.     }
  4017.     else
  4018.     {
  4019.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  4020.         lua_pushboolean(L, false);
  4021.     }
  4022.  
  4023.     return 1;
  4024. }
  4025.  
  4026. int32_t LuaInterface::luaDoPlayerAddSpentMana(lua_State* L)
  4027. {
  4028.     //doPlayerAddSpentMana(cid, amount[, useMultiplier = true])
  4029.     bool multiplier = true;
  4030.     if(lua_gettop(L) > 2)
  4031.         multiplier = popNumber(L);
  4032.  
  4033.     uint32_t amount = popNumber(L);
  4034.     ScriptEnviroment* env = getEnv();
  4035.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  4036.     {
  4037.         player->addManaSpent(amount, multiplier);
  4038.         lua_pushboolean(L, true);
  4039.     }
  4040.     else
  4041.     {
  4042.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4043.         lua_pushboolean(L, false);
  4044.     }
  4045.  
  4046.     return 1;
  4047. }
  4048.  
  4049. int32_t LuaInterface::luaDoPlayerAddItem(lua_State* L)
  4050. {
  4051.     //doPlayerAddItem(cid, itemid[, count/subtype = 1[, canDropOnMap = true[, slot = 0]]])
  4052.     //doPlayerAddItem(cid, itemid[, count = 1[, canDropOnMap = true[, subtype = 1[, slot = 0]]]])
  4053.     int32_t params = lua_gettop(L), subType = 1, slot = SLOT_WHEREEVER;
  4054.     if(params > 5)
  4055.         slot = popNumber(L);
  4056.  
  4057.     if(params > 4)
  4058.     {
  4059.         if(params > 5)
  4060.             subType = popNumber(L);
  4061.         else
  4062.             slot = popNumber(L);
  4063.     }
  4064.  
  4065.     bool canDropOnMap = true;
  4066.     if(params > 3)
  4067.         canDropOnMap = popNumber(L);
  4068.  
  4069.     uint32_t count = 1;
  4070.     if(params > 2)
  4071.         count = popNumber(L);
  4072.  
  4073.     uint32_t itemId = popNumber(L);
  4074.     if(slot > SLOT_AMMO)
  4075.     {
  4076.         errorEx("Invalid slot.");
  4077.         lua_pushboolean(L, false);
  4078.         return 1;
  4079.     }
  4080.  
  4081.     ScriptEnviroment* env = getEnv();
  4082.     Player* player = env->getPlayerByUID((uint32_t)popNumber(L));
  4083.     if(!player)
  4084.     {
  4085.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4086.         lua_pushboolean(L, false);
  4087.         return 1;
  4088.     }
  4089.  
  4090.     const ItemType& it = Item::items[itemId];
  4091.     int32_t itemCount = 1;
  4092.     if(params > 4)
  4093.         itemCount = std::max((uint32_t)1, count);
  4094.     else if(it.hasSubType())
  4095.     {
  4096.         if(it.stackable)
  4097.             itemCount = (int32_t)std::ceil((float)count / 100);
  4098.  
  4099.         subType = count;
  4100.     }
  4101.  
  4102.     while(itemCount > 0)
  4103.     {
  4104.         int32_t stackCount = std::min(100, subType);
  4105.         Item* newItem = Item::CreateItem(itemId, stackCount);
  4106.         if(!newItem)
  4107.         {
  4108.             errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  4109.             lua_pushboolean(L, false);
  4110.             return 1;
  4111.         }
  4112.  
  4113.         if(it.stackable)
  4114.             subType -= stackCount;
  4115.  
  4116.         ReturnValue ret = g_game.internalPlayerAddItem(NULL, player, newItem, canDropOnMap, (slots_t)slot);
  4117.         if(ret != RET_NOERROR)
  4118.         {
  4119.             delete newItem;
  4120.             lua_pushboolean(L, false);
  4121.             return 1;
  4122.         }
  4123.  
  4124.         --itemCount;
  4125.         if(itemCount)
  4126.             continue;
  4127.  
  4128.         if(newItem->getParent())
  4129.             lua_pushnumber(L, env->addThing(newItem));
  4130.         else //stackable item stacked with existing object, newItem will be released
  4131.             lua_pushnil(L);
  4132.  
  4133.         return 1;
  4134.     }
  4135.  
  4136.     lua_pushnil(L);
  4137.     return 1;
  4138. }
  4139.  
  4140. int32_t LuaInterface::luaDoPlayerAddItemEx(lua_State* L)
  4141. {
  4142.     //doPlayerAddItemEx(cid, uid[, canDropOnMap = false[, slot = 0]])
  4143.     int32_t params = lua_gettop(L), slot = SLOT_WHEREEVER;
  4144.     if(params > 3)
  4145.         slot = popNumber(L);
  4146.  
  4147.     bool canDropOnMap = false;
  4148.     if(params > 2)
  4149.         canDropOnMap = popNumber(L);
  4150.  
  4151.     uint32_t uid = (uint32_t)popNumber(L);
  4152.     if(slot > SLOT_AMMO)
  4153.     {
  4154.         errorEx("Invalid slot.");
  4155.         lua_pushboolean(L, false);
  4156.         return 1;
  4157.     }
  4158.  
  4159.     ScriptEnviroment* env = getEnv();
  4160.     Player* player = env->getPlayerByUID(popNumber(L));
  4161.     if(!player)
  4162.     {
  4163.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4164.         lua_pushboolean(L, false);
  4165.         return 1;
  4166.     }
  4167.  
  4168.     Item* item = env->getItemByUID(uid);
  4169.     if(!item)
  4170.     {
  4171.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  4172.         lua_pushboolean(L, false);
  4173.         return 1;
  4174.     }
  4175.  
  4176.     if(item->getParent() == VirtualCylinder::virtualCylinder)
  4177.         lua_pushnumber(L, g_game.internalPlayerAddItem(NULL, player, item, canDropOnMap, (slots_t)slot));
  4178.     else
  4179.         lua_pushboolean(L, false);
  4180.  
  4181.     return 1;
  4182. }
  4183.  
  4184. int32_t LuaInterface::luaDoTileAddItemEx(lua_State* L)
  4185. {
  4186.     //doTileAddItemEx(pos, uid)
  4187.     uint32_t uid = (uint32_t)popNumber(L);
  4188.     PositionEx pos;
  4189.     popPosition(L, pos);
  4190.  
  4191.     ScriptEnviroment* env = getEnv();
  4192.     Tile* tile = g_game.getTile(pos);
  4193.     if(!tile)
  4194.     {
  4195.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  4196.         lua_pushboolean(L, false);
  4197.         return 1;
  4198.     }
  4199.  
  4200.     Item* item = env->getItemByUID(uid);
  4201.     if(!item)
  4202.     {
  4203.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  4204.         lua_pushboolean(L, false);
  4205.         return 1;
  4206.     }
  4207.  
  4208.     if(item->getParent() == VirtualCylinder::virtualCylinder)
  4209.         lua_pushnumber(L, g_game.internalAddItem(NULL, tile, item));
  4210.     else
  4211.         lua_pushboolean(L, false);
  4212.  
  4213.     return 1;
  4214. }
  4215.  
  4216. int32_t LuaInterface::luaDoRelocate(lua_State* L)
  4217. {
  4218.     //doRelocate(pos, posTo[, creatures = true[, unmovable = true]])
  4219.     //Moves all moveable objects from pos to posTo
  4220.     bool unmovable = true, creatures = true;
  4221.     int32_t params = lua_gettop(L);
  4222.     if(params > 3)
  4223.         unmovable = popNumber(L);
  4224.  
  4225.     if(params > 2)
  4226.         creatures = popNumber(L);
  4227.  
  4228.     PositionEx toPos;
  4229.     popPosition(L, toPos);
  4230.  
  4231.     PositionEx fromPos;
  4232.     popPosition(L, fromPos);
  4233.  
  4234.     Tile* fromTile = g_game.getTile(fromPos.x, fromPos.y, fromPos.z);
  4235.     if(!fromTile)
  4236.     {
  4237.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  4238.         lua_pushboolean(L, false);
  4239.         return 1;
  4240.     }
  4241.  
  4242.     Tile* toTile = g_game.getTile(toPos.x, toPos.y, toPos.z);
  4243.     if(!toTile)
  4244.     {
  4245.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  4246.         lua_pushboolean(L, false);
  4247.         return 1;
  4248.     }
  4249.  
  4250.     if(fromTile != toTile)
  4251.     {
  4252.         for(int32_t i = fromTile->getThingCount() - 1; i >= 0; --i)
  4253.         {
  4254.             if(Thing* thing = fromTile->__getThing(i))
  4255.             {
  4256.                 if(Item* item = thing->getItem())
  4257.                 {
  4258.                     const ItemType& it = Item::items[item->getID()];
  4259.                     if(!it.isGroundTile() && !it.alwaysOnTop && !it.isMagicField())
  4260.                         g_game.internalTeleport(item, toPos, true, unmovable ? FLAG_IGNORENOTMOVEABLE : 0);
  4261.                 }
  4262.                 else if(creatures)
  4263.                 {
  4264.                     if(Creature* creature = thing->getCreature())
  4265.                         g_game.internalTeleport(creature, toPos, false);
  4266.                 }
  4267.             }
  4268.         }
  4269.     }
  4270.  
  4271.     lua_pushboolean(L, true);
  4272.     return 1;
  4273. }
  4274.  
  4275. int32_t LuaInterface::luaDoCleanTile(lua_State* L)
  4276. {
  4277.     //doCleanTile(pos, forceMapLoaded = false)
  4278.     //Remove all items from tile, ignore creatures
  4279.     bool forceMapLoaded = false;
  4280.     if(lua_gettop(L) > 1)
  4281.         forceMapLoaded = popNumber(L);
  4282.  
  4283.     PositionEx pos;
  4284.     popPosition(L, pos);
  4285.  
  4286.     Tile* tile = g_game.getTile(pos);
  4287.     if(!tile)
  4288.     {
  4289.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  4290.         lua_pushboolean(L, false);
  4291.         return 1;
  4292.     }
  4293.  
  4294.     for(int32_t i = tile->getThingCount() - 1; i >= 1; --i) //ignore ground
  4295.     {
  4296.         if(Thing* thing = tile->__getThing(i))
  4297.         {
  4298.             if(Item* item = thing->getItem())
  4299.             {
  4300.                 if(!item->isLoadedFromMap() || forceMapLoaded)
  4301.                     g_game.internalRemoveItem(NULL, item);
  4302.             }
  4303.         }
  4304.     }
  4305.  
  4306.     lua_pushboolean(L, true);
  4307.     return 1;
  4308. }
  4309.  
  4310. int32_t LuaInterface::luaDoPlayerSendTextMessage(lua_State* L)
  4311. {
  4312.     //doPlayerSendTextMessage(cid, MessageClasses, message)
  4313.     std::string text = popString(L);
  4314.     uint32_t messageClass = popNumber(L);
  4315.  
  4316.     ScriptEnviroment* env = getEnv();
  4317.     Player* player = env->getPlayerByUID(popNumber(L));
  4318.     if(!player)
  4319.     {
  4320.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4321.         lua_pushboolean(L, false);
  4322.         return 1;
  4323.     }
  4324.  
  4325.     player->sendTextMessage((MessageClasses)messageClass, text);
  4326.     lua_pushboolean(L, true);
  4327.     return 1;
  4328. }
  4329.  
  4330. int32_t LuaInterface::luaDoPlayerSendChannelMessage(lua_State* L)
  4331. {
  4332.     //doPlayerSendChannelMessage(cid, author, message, SpeakClasses, channel)
  4333.     uint16_t channelId = popNumber(L);
  4334.     uint32_t speakClass = popNumber(L);
  4335.     std::string text = popString(L), name = popString(L);
  4336.  
  4337.     ScriptEnviroment* env = getEnv();
  4338.     Player* player = env->getPlayerByUID(popNumber(L));
  4339.     if(!player)
  4340.     {
  4341.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4342.         lua_pushboolean(L, false);
  4343.         return 1;
  4344.     }
  4345.  
  4346.     player->sendChannelMessage(name, text, (SpeakClasses)speakClass, channelId);
  4347.     lua_pushboolean(L, true);
  4348.     return 1;
  4349. }
  4350.  
  4351. int32_t LuaInterface::luaDoPlayerSendToChannel(lua_State* L)
  4352. {
  4353.     //doPlayerSendToChannel(cid, targetId, SpeakClasses, message, channel[, time])
  4354.     ScriptEnviroment* env = getEnv();
  4355.     uint32_t time = 0;
  4356.     if(lua_gettop(L) > 5)
  4357.         time = popNumber(L);
  4358.  
  4359.     uint16_t channelId = popNumber(L);
  4360.     std::string text = popString(L);
  4361.     uint32_t speakClass = popNumber(L), targetId = popNumber(L);
  4362.  
  4363.     Player* player = env->getPlayerByUID(popNumber(L));
  4364.     if(!player)
  4365.     {
  4366.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4367.         lua_pushboolean(L, false);
  4368.         return 1;
  4369.     }
  4370.  
  4371.     Creature* creature = env->getCreatureByUID(targetId);
  4372.     if(!creature)
  4373.     {
  4374.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  4375.         lua_pushboolean(L, false);
  4376.         return 1;
  4377.     }
  4378.  
  4379.     player->sendToChannel(creature, (SpeakClasses)speakClass, text, channelId, time);
  4380.     lua_pushboolean(L, true);
  4381.     return 1;
  4382. }
  4383.  
  4384. int32_t LuaInterface::luaDoPlayerOpenChannel(lua_State* L)
  4385. {
  4386.     //doPlayerOpenChannel(cid, channelId)
  4387.     uint16_t channelId = popNumber(L);
  4388.     uint32_t cid = popNumber(L);
  4389.  
  4390.     ScriptEnviroment* env = getEnv();
  4391.     if(env->getPlayerByUID(cid))
  4392.     {
  4393.         lua_pushboolean(L, g_game.playerOpenChannel(cid, channelId));
  4394.         return 1;
  4395.     }
  4396.  
  4397.     errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4398.     lua_pushboolean(L, false);
  4399.     return 1;
  4400. }
  4401.  
  4402. int32_t LuaInterface::luaDoSendCreatureSquare(lua_State* L)
  4403. {
  4404.     //doSendCreatureSquare(cid, color[, player])
  4405.     ScriptEnviroment* env = getEnv();
  4406.     SpectatorVec list;
  4407.     if(lua_gettop(L) > 2)
  4408.     {
  4409.         if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  4410.             list.push_back(creature);
  4411.     }
  4412.  
  4413.         uint8_t color = popNumber(L);
  4414.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  4415.     {
  4416.             if(!list.empty())
  4417.                     g_game.addCreatureSquare(list, creature, color);
  4418.             else
  4419.                     g_game.addCreatureSquare(creature, color);
  4420.  
  4421.             lua_pushboolean(L, true);
  4422.     }
  4423.     else
  4424.     {
  4425.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  4426.         lua_pushboolean(L, false);
  4427.     }
  4428.  
  4429.         return 1;
  4430. }
  4431.  
  4432. int32_t LuaInterface::luaDoSendAnimatedText(lua_State* L)
  4433. {
  4434.     //doSendAnimatedText(pos, text, color[, player])
  4435.     ScriptEnviroment* env = getEnv();
  4436.     SpectatorVec list;
  4437.     if(lua_gettop(L) > 3)
  4438.     {
  4439.         if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  4440.             list.push_back(creature);
  4441.     }
  4442.  
  4443.     uint8_t color = popNumber(L);
  4444.     std::string text = popString(L);
  4445.  
  4446.     PositionEx pos;
  4447.     popPosition(L, pos);
  4448.     if(pos.x == 0xFFFF)
  4449.         pos = env->getRealPos();
  4450.  
  4451.     if(!list.empty())
  4452.         g_game.addAnimatedText(list, pos, color, text);
  4453.     else
  4454.         g_game.addAnimatedText(pos, color, text);
  4455.  
  4456.     lua_pushboolean(L, true);
  4457.     return 1;
  4458. }
  4459.  
  4460. int32_t LuaInterface::luaGetPlayerSkillLevel(lua_State* L)
  4461. {
  4462.     //getPlayerSkillLevel(cid, skillid)
  4463.     uint32_t skillId = popNumber(L);
  4464.  
  4465.     ScriptEnviroment* env = getEnv();
  4466.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  4467.     {
  4468.         if(skillId <= SKILL_LAST)
  4469.             lua_pushnumber(L, player->skills[skillId][SKILL_LEVEL]);
  4470.         else
  4471.             lua_pushboolean(L, false);
  4472.     }
  4473.     else
  4474.     {
  4475.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4476.         lua_pushboolean(L, false);
  4477.     }
  4478.  
  4479.     return 1;
  4480. }
  4481.  
  4482. int32_t LuaInterface::luaGetPlayerSkillTries(lua_State* L)
  4483. {
  4484.     //getPlayerSkillTries(cid, skillid)
  4485.     uint32_t skillid = popNumber(L);
  4486.  
  4487.     ScriptEnviroment* env = getEnv();
  4488.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  4489.     {
  4490.         if(skillid <= SKILL_LAST)
  4491.             lua_pushnumber(L, player->skills[skillid][SKILL_TRIES]);
  4492.         else
  4493.             lua_pushboolean(L, false);
  4494.     }
  4495.     else
  4496.     {
  4497.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4498.         lua_pushboolean(L, false);
  4499.     }
  4500.  
  4501.     return 1;
  4502. }
  4503.  
  4504. int32_t LuaInterface::luaDoCreatureSetDropLoot(lua_State* L)
  4505. {
  4506.     //doCreatureSetDropLoot(cid, doDrop)
  4507.     bool doDrop = popNumber(L);
  4508.  
  4509.     ScriptEnviroment* env = getEnv();
  4510.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  4511.     {
  4512.         creature->setDropLoot(doDrop ? LOOT_DROP_FULL : LOOT_DROP_NONE);
  4513.         lua_pushboolean(L, true);
  4514.     }
  4515.     else
  4516.     {
  4517.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  4518.         lua_pushboolean(L, false);
  4519.     }
  4520.  
  4521.     return 1;
  4522. }
  4523.  
  4524. int32_t LuaInterface::luaGetPlayerLossPercent(lua_State* L)
  4525. {
  4526.     //getPlayerLossPercent(cid, lossType)
  4527.     uint8_t lossType = (uint8_t)popNumber(L);
  4528.  
  4529.     ScriptEnviroment* env = getEnv();
  4530.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  4531.     {
  4532.         if(lossType <= LOSS_LAST)
  4533.         {
  4534.             uint32_t value = player->getLossPercent((lossTypes_t)lossType);
  4535.             lua_pushnumber(L, value);
  4536.         }
  4537.         else
  4538.             lua_pushboolean(L, false);
  4539.     }
  4540.     else
  4541.     {
  4542.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4543.         lua_pushboolean(L, false);
  4544.     }
  4545.  
  4546.     return 1;
  4547. }
  4548.  
  4549. int32_t LuaInterface::luaDoPlayerSetLossPercent(lua_State* L)
  4550. {
  4551.     //doPlayerSetLossPercent(cid, lossType, newPercent)
  4552.     uint32_t newPercent = popNumber(L);
  4553.     uint8_t lossType = (uint8_t)popNumber(L);
  4554.  
  4555.     ScriptEnviroment* env = getEnv();
  4556.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  4557.     {
  4558.         if(lossType <= LOSS_LAST)
  4559.         {
  4560.             player->setLossPercent((lossTypes_t)lossType, newPercent);
  4561.             lua_pushboolean(L, true);
  4562.         }
  4563.         else
  4564.             lua_pushboolean(L, false);
  4565.     }
  4566.     else
  4567.     {
  4568.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4569.         lua_pushboolean(L, false);
  4570.     }
  4571.  
  4572.     return 1;
  4573. }
  4574.  
  4575. int32_t LuaInterface::luaDoPlayerSetLossSkill(lua_State* L)
  4576. {
  4577.     //doPlayerSetLossSkill(cid, doLose)
  4578.     bool doLose = popNumber(L);
  4579.  
  4580.     ScriptEnviroment* env = getEnv();
  4581.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  4582.     {
  4583.         player->setLossSkill(doLose);
  4584.         lua_pushboolean(L, true);
  4585.     }
  4586.     else
  4587.     {
  4588.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4589.         lua_pushboolean(L, false);
  4590.     }
  4591.  
  4592.     return 1;
  4593. }
  4594.  
  4595. int32_t LuaInterface::luaDoShowTextDialog(lua_State* L)
  4596. {
  4597.     //doShowTextDialog(cid, itemid, text)
  4598.     std::string text = popString(L);
  4599.     uint32_t itemId = popNumber(L);
  4600.  
  4601.     ScriptEnviroment* env = getEnv();
  4602.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  4603.     {
  4604.         player->setWriteItem(NULL, 0);
  4605.         player->sendTextWindow(itemId, text);
  4606.         lua_pushboolean(L, true);
  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::luaDoDecayItem(lua_State* L)
  4618. {
  4619.     //doDecayItem(uid)
  4620.     //Note: to stop decay set decayTo = 0 in items.xml
  4621.     ScriptEnviroment* env = getEnv();
  4622.     if(Item* item = env->getItemByUID(popNumber(L)))
  4623.     {
  4624.         g_game.startDecay(item);
  4625.         lua_pushboolean(L, true);
  4626.     }
  4627.     else
  4628.     {
  4629.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  4630.         lua_pushboolean(L, false);
  4631.     }
  4632.  
  4633.     return 1;
  4634. }
  4635.  
  4636. int32_t LuaInterface::luaGetThingFromPos(lua_State* L)
  4637. {
  4638.     //getThingFromPos(pos[, displayError = true])
  4639.     //Note:
  4640.     //  stackpos = 255- top thing (movable item or creature)
  4641.     //  stackpos = 254- magic field
  4642.     //  stackpos = 253- top creature
  4643.  
  4644.     bool displayError = true;
  4645.     if(lua_gettop(L) > 1)
  4646.         displayError = popNumber(L);
  4647.  
  4648.     PositionEx pos;
  4649.     popPosition(L, pos);
  4650.  
  4651.     ScriptEnviroment* env = getEnv();
  4652.     Thing* thing = NULL;
  4653.     if(Tile* tile = g_game.getMap()->getTile(pos))
  4654.     {
  4655.         if(pos.stackpos == 255)
  4656.         {
  4657.             if(!(thing = tile->getTopCreature()))
  4658.             {
  4659.                 Item* item = tile->getTopDownItem();
  4660.                 if(item && item->isMoveable())
  4661.                     thing = item;
  4662.             }
  4663.         }
  4664.         else if(pos.stackpos == 254)
  4665.             thing = tile->getFieldItem();
  4666.         else if(pos.stackpos == 253)
  4667.             thing = tile->getTopCreature();
  4668.         else
  4669.             thing = tile->__getThing(pos.stackpos);
  4670.  
  4671.         if(thing)
  4672.             pushThing(L, thing, env->addThing(thing));
  4673.         else
  4674.             pushThing(L, NULL, 0);
  4675.  
  4676.         return 1;
  4677.     }
  4678.  
  4679.     if(displayError)
  4680.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  4681.  
  4682.     pushThing(L, NULL, 0);
  4683.     return 1;
  4684. }
  4685.  
  4686. int32_t LuaInterface::luaGetTileItemById(lua_State* L)
  4687. {
  4688.     //getTileItemById(pos, itemId[, subType = -1])
  4689.     ScriptEnviroment* env = getEnv();
  4690.  
  4691.     int32_t subType = -1;
  4692.     if(lua_gettop(L) > 2)
  4693.         subType = (int32_t)popNumber(L);
  4694.  
  4695.     int32_t itemId = (int32_t)popNumber(L);
  4696.     PositionEx pos;
  4697.     popPosition(L, pos);
  4698.  
  4699.     Tile* tile = g_game.getTile(pos);
  4700.     if(!tile)
  4701.     {
  4702.         pushThing(L, NULL, 0);
  4703.         return 1;
  4704.     }
  4705.  
  4706.     Item* item = g_game.findItemOfType(tile, itemId, false, subType);
  4707.     if(!item)
  4708.     {
  4709.         pushThing(L, NULL, 0);
  4710.         return 1;
  4711.     }
  4712.  
  4713.     pushThing(L, item, env->addThing(item));
  4714.     return 1;
  4715. }
  4716.  
  4717. int32_t LuaInterface::luaGetTileItemByType(lua_State* L)
  4718. {
  4719.     //getTileItemByType(pos, type)
  4720.     uint32_t rType = (uint32_t)popNumber(L);
  4721.     if(rType >= ITEM_TYPE_LAST)
  4722.     {
  4723.         errorEx("Not a valid item type");
  4724.         pushThing(L, NULL, 0);
  4725.         return 1;
  4726.     }
  4727.  
  4728.     PositionEx pos;
  4729.     popPosition(L, pos);
  4730.  
  4731.     Tile* tile = g_game.getTile(pos);
  4732.     if(!tile)
  4733.     {
  4734.         pushThing(L, NULL, 0);
  4735.         return 1;
  4736.     }
  4737.  
  4738.     bool found = true;
  4739.     switch((ItemTypes_t)rType)
  4740.     {
  4741.         case ITEM_TYPE_TELEPORT:
  4742.         {
  4743.             if(!tile->hasFlag(TILESTATE_TELEPORT))
  4744.                 found = false;
  4745.  
  4746.             break;
  4747.         }
  4748.         case ITEM_TYPE_MAGICFIELD:
  4749.         {
  4750.             if(!tile->hasFlag(TILESTATE_MAGICFIELD))
  4751.                 found = false;
  4752.  
  4753.             break;
  4754.         }
  4755.         case ITEM_TYPE_MAILBOX:
  4756.         {
  4757.             if(!tile->hasFlag(TILESTATE_MAILBOX))
  4758.                 found = false;
  4759.  
  4760.             break;
  4761.         }
  4762.         case ITEM_TYPE_TRASHHOLDER:
  4763.         {
  4764.             if(!tile->hasFlag(TILESTATE_TRASHHOLDER))
  4765.                 found = false;
  4766.  
  4767.             break;
  4768.         }
  4769.         case ITEM_TYPE_BED:
  4770.         {
  4771.             if(!tile->hasFlag(TILESTATE_BED))
  4772.                 found = false;
  4773.  
  4774.             break;
  4775.         }
  4776.         case ITEM_TYPE_DEPOT:
  4777.         {
  4778.             if(!tile->hasFlag(TILESTATE_DEPOT))
  4779.                 found = false;
  4780.  
  4781.             break;
  4782.         }
  4783.         default:
  4784.             break;
  4785.     }
  4786.  
  4787.     if(!found)
  4788.     {
  4789.         pushThing(L, NULL, 0);
  4790.         return 1;
  4791.     }
  4792.  
  4793.     ScriptEnviroment* env = getEnv();
  4794.     Item* item = NULL;
  4795.     for(uint32_t i = 0; i < tile->getThingCount(); ++i)
  4796.     {
  4797.         if(!(item = tile->__getThing(i)->getItem()))
  4798.             continue;
  4799.  
  4800.         if(Item::items[item->getID()].type != (ItemTypes_t)rType)
  4801.             continue;
  4802.  
  4803.         pushThing(L, item, env->addThing(item));
  4804.         return 1;
  4805.     }
  4806.  
  4807.     pushThing(L, NULL, 0);
  4808.     return 1;
  4809. }
  4810.  
  4811. int32_t LuaInterface::luaGetTileThingByPos(lua_State* L)
  4812. {
  4813.     //getTileThingByPos(pos)
  4814.     PositionEx pos;
  4815.     popPosition(L, pos);
  4816.  
  4817.     ScriptEnviroment* env = getEnv();
  4818.  
  4819.     Tile* tile = g_game.getTile(pos.x, pos.y, pos.z);
  4820.     if(!tile)
  4821.     {
  4822.         if(pos.stackpos == -1)
  4823.         {
  4824.             lua_pushnumber(L, -1);
  4825.             return 1;
  4826.         }
  4827.         else
  4828.         {
  4829.             pushThing(L, NULL, 0);
  4830.             return 1;
  4831.         }
  4832.     }
  4833.  
  4834.     if(pos.stackpos == -1)
  4835.     {
  4836.         lua_pushnumber(L, tile->getThingCount());
  4837.         return 1;
  4838.     }
  4839.  
  4840.     Thing* thing = tile->__getThing(pos.stackpos);
  4841.     if(!thing)
  4842.     {
  4843.         pushThing(L, NULL, 0);
  4844.         return 1;
  4845.     }
  4846.  
  4847.     pushThing(L, thing, env->addThing(thing));
  4848.     return 1;
  4849. }
  4850.  
  4851. int32_t LuaInterface::luaGetTopCreature(lua_State* L)
  4852. {
  4853.     //getTopCreature(pos)
  4854.     PositionEx pos;
  4855.     popPosition(L, pos);
  4856.  
  4857.     ScriptEnviroment* env = getEnv();
  4858.     Tile* tile = g_game.getTile(pos);
  4859.     if(!tile)
  4860.     {
  4861.         pushThing(L, NULL, 0);
  4862.         return 1;
  4863.     }
  4864.  
  4865.     Thing* thing = tile->getTopCreature();
  4866.     if(!thing || !thing->getCreature())
  4867.     {
  4868.         pushThing(L, NULL, 0);
  4869.         return 1;
  4870.     }
  4871.  
  4872.     pushThing(L, thing, env->addThing(thing));
  4873.     return 1;
  4874. }
  4875.  
  4876. int32_t LuaInterface::luaDoCreateItem(lua_State* L)
  4877. {
  4878.     //doCreateItem(itemid[, type/count = 1], pos)
  4879.     //Returns uid of the created item, only works on tiles.
  4880.     PositionEx pos;
  4881.     popPosition(L, pos);
  4882.  
  4883.     uint32_t count = 1;
  4884.     if(lua_gettop(L) > 1)
  4885.         count = popNumber(L);
  4886.  
  4887.     uint32_t itemId = popNumber(L);
  4888.     ScriptEnviroment* env = getEnv();
  4889.     const ItemType& it = Item::items[itemId];
  4890.  
  4891.     Tile* tile = g_game.getTile(pos);
  4892.     if(!tile)
  4893.     {
  4894.         if(it.group == ITEM_GROUP_GROUND)
  4895.         {
  4896.             Item* item = Item::CreateItem(itemId);
  4897.             tile = IOMap::createTile(item, NULL, pos.x, pos.y, pos.z);
  4898.  
  4899.             g_game.setTile(tile);
  4900.             lua_pushnumber(L, env->addThing(item));
  4901.             return 1;
  4902.         }
  4903.         else
  4904.         {
  4905.             errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  4906.             lua_pushboolean(L, false);
  4907.             return 1;
  4908.         }
  4909.     }
  4910.  
  4911.     int32_t itemCount = 1, subType = 1;
  4912.     if(it.hasSubType())
  4913.     {
  4914.         if(it.stackable)
  4915.             itemCount = (int32_t)std::ceil((float)count / 100);
  4916.  
  4917.         subType = count;
  4918.     }
  4919.     else
  4920.         itemCount = std::max((uint32_t)1, count);
  4921.  
  4922.     while(itemCount > 0)
  4923.     {
  4924.         int32_t stackCount = std::min(100, subType);
  4925.         Item* newItem = Item::CreateItem(itemId, stackCount);
  4926.         if(!newItem)
  4927.         {
  4928.             errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  4929.             lua_pushboolean(L, false);
  4930.             return 1;
  4931.         }
  4932.  
  4933.         if(it.stackable)
  4934.             subType -= stackCount;
  4935.  
  4936.         ReturnValue ret = g_game.internalAddItem(NULL, tile, newItem, INDEX_WHEREEVER, FLAG_NOLIMIT);
  4937.         if(ret != RET_NOERROR)
  4938.         {
  4939.             delete newItem;
  4940.             lua_pushboolean(L, false);
  4941.             return 1;
  4942.         }
  4943.  
  4944.         --itemCount;
  4945.         if(itemCount)
  4946.             continue;
  4947.  
  4948.         if(newItem->getParent())
  4949.             lua_pushnumber(L, env->addThing(newItem));
  4950.         else //stackable item stacked with existing object, newItem will be released
  4951.             lua_pushnil(L);
  4952.  
  4953.         return 1;
  4954.     }
  4955.  
  4956.     lua_pushnil(L);
  4957.     return 1;
  4958. }
  4959.  
  4960. int32_t LuaInterface::luaDoCreateItemEx(lua_State* L)
  4961. {
  4962.     //doCreateItemEx(itemid[, count/subType])
  4963.     uint32_t count = 0;
  4964.     if(lua_gettop(L) > 1)
  4965.         count = popNumber(L);
  4966.  
  4967.     ScriptEnviroment* env = getEnv();
  4968.     const ItemType& it = Item::items[(uint32_t)popNumber(L)];
  4969.     if(it.stackable && count > 100)
  4970.         count = 100;
  4971.  
  4972.     Item* newItem = Item::CreateItem(it.id, count);
  4973.     if(!newItem)
  4974.     {
  4975.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  4976.         lua_pushboolean(L, false);
  4977.         return 1;
  4978.     }
  4979.  
  4980.     newItem->setParent(VirtualCylinder::virtualCylinder);
  4981.     env->addTempItem(env, newItem);
  4982.  
  4983.     lua_pushnumber(L, env->addThing(newItem));
  4984.     return 1;
  4985. }
  4986.  
  4987. int32_t LuaInterface::luaDoCreateTeleport(lua_State* L)
  4988. {
  4989.     //doCreateTeleport(itemid, toPosition, fromPosition)
  4990.     PositionEx createPos;
  4991.     popPosition(L, createPos);
  4992.     PositionEx toPos;
  4993.     popPosition(L, toPos);
  4994.  
  4995.     uint32_t itemId = (uint32_t)popNumber(L);
  4996.     ScriptEnviroment* env = getEnv();
  4997.  
  4998.     Tile* tile = g_game.getMap()->getTile(createPos);
  4999.     if(!tile)
  5000.     {
  5001.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  5002.         lua_pushboolean(L, false);
  5003.         return 1;
  5004.     }
  5005.  
  5006.     Item* newItem = Item::CreateItem(itemId);
  5007.     Teleport* newTeleport = newItem->getTeleport();
  5008.     if(!newTeleport)
  5009.     {
  5010.         delete newItem;
  5011.         lua_pushboolean(L, false);
  5012.         return 1;
  5013.     }
  5014.  
  5015.     newTeleport->setDestination(toPos);
  5016.     if(g_game.internalAddItem(NULL, tile, newTeleport, INDEX_WHEREEVER, FLAG_NOLIMIT) != RET_NOERROR)
  5017.     {
  5018.         delete newItem;
  5019.         lua_pushboolean(L, false);
  5020.         return 1;
  5021.     }
  5022.  
  5023.     if(newItem->getParent())
  5024.         lua_pushnumber(L, env->addThing(newItem));
  5025.     else //stackable item stacked with existing object, newItem will be released
  5026.         lua_pushnil(L);
  5027.  
  5028.     return 1;
  5029. }
  5030.  
  5031. int32_t LuaInterface::luaGetCreatureStorage(lua_State* L)
  5032. {
  5033.     //getCreatureStorage(cid, key)
  5034.     uint32_t key = popNumber(L);
  5035.     ScriptEnviroment* env = getEnv();
  5036.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  5037.     {
  5038.         std::string strValue;
  5039.         if(creature->getStorage(key, strValue))
  5040.         {
  5041.             int32_t intValue = atoi(strValue.c_str());
  5042.             if(intValue || strValue == "0")
  5043.                 lua_pushnumber(L, intValue);
  5044.             else
  5045.                 lua_pushstring(L, strValue.c_str());
  5046.         }
  5047.         else
  5048.             lua_pushnumber(L, -1);
  5049.     }
  5050.     else
  5051.     {
  5052.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  5053.         lua_pushboolean(L, false);
  5054.     }
  5055.  
  5056.     return 1;
  5057. }
  5058.  
  5059. int32_t LuaInterface::luaDoCreatureSetStorage(lua_State* L)
  5060. {
  5061.     //doCreatureSetStorage(cid, key[, value])
  5062.     std::string value;
  5063.     bool nil = true;
  5064.     if(lua_gettop(L) > 2)
  5065.     {
  5066.         if(!lua_isnil(L, -1))
  5067.         {
  5068.             value = popString(L);
  5069.             nil = false;
  5070.         }
  5071.         else
  5072.             lua_pop(L, 1);
  5073.     }
  5074.  
  5075.     uint32_t key = popNumber(L);
  5076.     ScriptEnviroment* env = getEnv();
  5077.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  5078.     {
  5079.         if(!nil)
  5080.             nil = creature->setStorage(key, value);
  5081.         else
  5082.             creature->eraseStorage(key);
  5083.  
  5084.         lua_pushboolean(L, nil);
  5085.     }
  5086.     else
  5087.     {
  5088.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  5089.         lua_pushboolean(L, false);
  5090.     }
  5091.  
  5092.     return 1;
  5093. }
  5094.  
  5095. int32_t LuaInterface::luaGetTileInfo(lua_State* L)
  5096. {
  5097.     //getTileInfo(pos)
  5098.     PositionEx pos;
  5099.     popPosition(L, pos);
  5100.     if(Tile* tile = g_game.getMap()->getTile(pos))
  5101.     {
  5102.         ScriptEnviroment* env = getEnv();
  5103.         pushThing(L, tile->ground, env->addThing(tile->ground));
  5104.  
  5105.         setFieldBool(L, "protection", tile->hasFlag(TILESTATE_PROTECTIONZONE));
  5106.         setFieldBool(L, "optional", tile->hasFlag(TILESTATE_OPTIONALZONE));
  5107.         setFieldBool(L, "nologout", tile->hasFlag(TILESTATE_NOLOGOUT));
  5108.         setFieldBool(L, "hardcore", tile->hasFlag(TILESTATE_HARDCOREZONE));
  5109.         setFieldBool(L, "refresh", tile->hasFlag(TILESTATE_REFRESH));
  5110.         setFieldBool(L, "trashed", tile->hasFlag(TILESTATE_TRASHED));
  5111.         setFieldBool(L, "house", tile->hasFlag(TILESTATE_HOUSE));
  5112.         setFieldBool(L, "bed", tile->hasFlag(TILESTATE_BED));
  5113.         setFieldBool(L, "depot", tile->hasFlag(TILESTATE_DEPOT));
  5114.  
  5115.         setField(L, "things", tile->getThingCount());
  5116.         setField(L, "creatures", tile->getCreatureCount());
  5117.         setField(L, "items", tile->getItemCount());
  5118.         setField(L, "topItems", tile->getTopItemCount());
  5119.         setField(L, "downItems", tile->getDownItemCount());
  5120.     }
  5121.     else
  5122.     {
  5123.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  5124.         lua_pushboolean(L, false);
  5125.     }
  5126.  
  5127.     return 1;
  5128. }
  5129.  
  5130. int32_t LuaInterface::luaGetHouseFromPos(lua_State* L)
  5131. {
  5132.     //getHouseFromPos(pos)
  5133.     PositionEx pos;
  5134.     popPosition(L, pos);
  5135.  
  5136.     Tile* tile = g_game.getMap()->getTile(pos);
  5137.     if(!tile)
  5138.     {
  5139.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  5140.         lua_pushboolean(L, false);
  5141.         return 1;
  5142.     }
  5143.  
  5144.     HouseTile* houseTile = tile->getHouseTile();
  5145.     if(!houseTile)
  5146.     {
  5147.         lua_pushboolean(L, false);
  5148.         return 1;
  5149.     }
  5150.  
  5151.     House* house = houseTile->getHouse();
  5152.     if(!house)
  5153.     {
  5154.         lua_pushboolean(L, false);
  5155.         return 1;
  5156.     }
  5157.  
  5158.     lua_pushnumber(L, house->getId());
  5159.     return 1;
  5160. }
  5161.  
  5162. int32_t LuaInterface::luaDoCreateMonster(lua_State* L)
  5163. {
  5164.     //doCreateMonster(name, pos[, extend = false[, force = false[, displayError = true]]])
  5165.     bool displayError = true, force = false, extend = false;
  5166.     int32_t params = lua_gettop(L);
  5167.     if(params > 4)
  5168.         displayError = popNumber(L);
  5169.  
  5170.     if(params > 3)
  5171.         force = popNumber(L);
  5172.  
  5173.     if(params > 2)
  5174.         extend = popNumber(L);
  5175.  
  5176.     PositionEx pos;
  5177.     popPosition(L, pos);
  5178.  
  5179.     std::string name = popString(L);
  5180.     Monster* monster = Monster::createMonster(name.c_str());
  5181.     if(!monster)
  5182.     {
  5183.         if(displayError)
  5184.             errorEx("Monster with name '" + name + "' not found");
  5185.  
  5186.         lua_pushboolean(L, false);
  5187.         return 1;
  5188.     }
  5189.  
  5190.     if(!g_game.placeCreature(monster, pos, extend, force))
  5191.     {
  5192.         delete monster;
  5193.         if(displayError)
  5194.             errorEx("Cannot create monster: " + name);
  5195.  
  5196.         lua_pushboolean(L, true);
  5197.         return 1;
  5198.     }
  5199.  
  5200.     ScriptEnviroment* env = getEnv();
  5201.     lua_pushnumber(L, env->addThing((Thing*)monster));
  5202.     return 1;
  5203. }
  5204.  
  5205. int32_t LuaInterface::luaDoCreateNpc(lua_State* L)
  5206. {
  5207.     //doCreateNpc(name, pos[, displayError = true])
  5208.     bool displayError = true;
  5209.     if(lua_gettop(L) > 2)
  5210.         displayError = popNumber(L);
  5211.  
  5212.     PositionEx pos;
  5213.     popPosition(L, pos);
  5214.  
  5215.     std::string name = popString(L);
  5216.     Npc* npc = Npc::createNpc(name.c_str());
  5217.     if(!npc)
  5218.     {
  5219.         if(displayError)
  5220.             errorEx("Npc with name '" + name + "' not found");
  5221.  
  5222.         lua_pushboolean(L, false);
  5223.         return 1;
  5224.     }
  5225.  
  5226.     if(!g_game.placeCreature(npc, pos))
  5227.     {
  5228.         delete npc;
  5229.         if(displayError)
  5230.             errorEx("Cannot create npc: " + name);
  5231.  
  5232.         lua_pushboolean(L, true); //for scripting compatibility
  5233.         return 1;
  5234.     }
  5235.  
  5236.     ScriptEnviroment* env = getEnv();
  5237.     lua_pushnumber(L, env->addThing((Thing*)npc));
  5238.     return 1;
  5239. }
  5240.  
  5241. int32_t LuaInterface::luaDoRemoveCreature(lua_State* L)
  5242. {
  5243.     //doRemoveCreature(cid[, forceLogout = true])
  5244.     bool forceLogout = true;
  5245.     if(lua_gettop(L) > 1)
  5246.         forceLogout = popNumber(L);
  5247.  
  5248.     ScriptEnviroment* env = getEnv();
  5249.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  5250.     {
  5251.         if(Player* player = creature->getPlayer())
  5252.             player->kickPlayer(true, forceLogout); //Players will get kicked without restrictions
  5253.         else
  5254.             g_game.removeCreature(creature); //Monsters/NPCs will get removed
  5255.  
  5256.         lua_pushboolean(L, true);
  5257.     }
  5258.     else
  5259.     {
  5260.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  5261.         lua_pushboolean(L, false);
  5262.     }
  5263.  
  5264.     return 1;
  5265. }
  5266.  
  5267. int32_t LuaInterface::luaDoPlayerAddMoney(lua_State* L)
  5268. {
  5269.     //doPlayerAddMoney(cid, money)
  5270.     uint64_t money = popNumber(L);
  5271.  
  5272.     ScriptEnviroment* env = getEnv();
  5273.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5274.     {
  5275.         g_game.addMoney(player, money);
  5276.         lua_pushboolean(L, true);
  5277.     }
  5278.     else
  5279.     {
  5280.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5281.         lua_pushboolean(L, false);
  5282.     }
  5283.  
  5284.     return 1;
  5285. }
  5286. // check bankBalance onBuy 1
  5287. int32_t LuaInterface::luaDoPlayerRemoveMoney(lua_State* L)
  5288. {
  5289.     //doPlayerRemoveMoney(cid,money[, includeBank = true])
  5290.  
  5291.     bool includeBank = true;
  5292.     if(lua_gettop(L) > 2)
  5293.         includeBank = popBoolean(L);
  5294.  
  5295.     uint64_t money = popNumber(L);
  5296.  
  5297.     ScriptEnviroment* env = getEnv();
  5298.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5299.         lua_pushboolean(L, g_game.removeMoney(player, money, 0, includeBank));
  5300.     else
  5301.     {
  5302.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5303.         lua_pushboolean(L, false);
  5304.     }
  5305.     return 1;
  5306. }
  5307.  
  5308. int32_t LuaInterface::luaDoPlayerTransferMoneyTo(lua_State* L)
  5309. {
  5310.     //doPlayerTransferMoneyTo(cid, target, money)
  5311.     uint64_t money = popNumber(L);
  5312.     std::string target = popString(L);
  5313.  
  5314.     ScriptEnviroment* env = getEnv();
  5315.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5316.         lua_pushboolean(L, player->transferMoneyTo(target, money));
  5317.     else
  5318.     {
  5319.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5320.         lua_pushboolean(L, false);
  5321.     }
  5322.  
  5323.     return 1;
  5324. }
  5325.  
  5326. int32_t LuaInterface::luaDoPlayerSetPzLocked(lua_State* L)
  5327. {
  5328.     //doPlayerSetPzLocked(cid, locked)
  5329.     bool locked = popNumber(L);
  5330.  
  5331.     ScriptEnviroment* env = getEnv();
  5332.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5333.     {
  5334.         if(player->isPzLocked() != locked)
  5335.         {
  5336.             player->setPzLocked(locked);
  5337.             player->sendIcons();
  5338.         }
  5339.  
  5340.         lua_pushboolean(L, true);
  5341.     }
  5342.     else
  5343.     {
  5344.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5345.         lua_pushboolean(L, false);
  5346.     }
  5347.  
  5348.     return 1;
  5349. }
  5350.  
  5351. int32_t LuaInterface::luaDoPlayerSetTown(lua_State* L)
  5352. {
  5353.     //doPlayerSetTown(cid, townid)
  5354.     uint32_t townid = (uint32_t)popNumber(L);
  5355.  
  5356.     ScriptEnviroment* env = getEnv();
  5357.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5358.     {
  5359.         if(Town* town = Towns::getInstance()->getTown(townid))
  5360.         {
  5361.             player->setMasterPosition(town->getPosition());
  5362.             player->setTown(townid);
  5363.             lua_pushboolean(L, true);
  5364.         }
  5365.         else
  5366.             lua_pushboolean(L, false);
  5367.     }
  5368.     else
  5369.     {
  5370.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5371.         lua_pushboolean(L, false);
  5372.     }
  5373.  
  5374.     return 1;
  5375. }
  5376.  
  5377. int32_t LuaInterface::luaDoPlayerSetVocation(lua_State* L)
  5378. {
  5379.     //doPlayerSetVocation(cid, voc)
  5380.     uint32_t voc = popNumber(L);
  5381.  
  5382.     ScriptEnviroment* env = getEnv();
  5383.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5384.     {
  5385.         player->setVocation(voc);
  5386.         lua_pushboolean(L, true);
  5387.     }
  5388.     else
  5389.     {
  5390.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5391.         lua_pushboolean(L, false);
  5392.     }
  5393.  
  5394.     return 1;
  5395. }
  5396.  
  5397. int32_t LuaInterface::luaDoPlayerSetSex(lua_State* L)
  5398. {
  5399.     //doPlayerSetSex(cid, sex)
  5400.     uint32_t newSex = popNumber(L);
  5401.  
  5402.     ScriptEnviroment* env = getEnv();
  5403.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5404.     {
  5405.         player->setSex(newSex);
  5406.         lua_pushboolean(L, true);
  5407.     }
  5408.     else
  5409.     {
  5410.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5411.         lua_pushboolean(L, false);
  5412.     }
  5413.  
  5414.     return 1;
  5415. }
  5416.  
  5417. int32_t LuaInterface::luaDoPlayerAddSoul(lua_State* L)
  5418. {
  5419.     //doPlayerAddSoul(cid, soul)
  5420.     int32_t soul = popNumber(L);
  5421.  
  5422.     ScriptEnviroment* env = getEnv();
  5423.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5424.     {
  5425.         player->changeSoul(soul);
  5426.         lua_pushboolean(L, true);
  5427.     }
  5428.     else
  5429.     {
  5430.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5431.         lua_pushboolean(L, false);
  5432.     }
  5433.  
  5434.     return 1;
  5435. }
  5436.  
  5437. int32_t LuaInterface::luaGetPlayerItemCount(lua_State* L)
  5438. {
  5439.     //getPlayerItemCount(cid, itemid[, subType = -1])
  5440.     int32_t subType = -1;
  5441.     if(lua_gettop(L) > 2)
  5442.         subType = popNumber(L);
  5443.  
  5444.     uint32_t itemId = popNumber(L);
  5445.     ScriptEnviroment* env = getEnv();
  5446.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  5447.         lua_pushnumber(L, player->__getItemTypeCount(itemId, subType));
  5448.     else
  5449.     {
  5450.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5451.         lua_pushboolean(L, false);
  5452.     }
  5453.  
  5454.     return 1;
  5455. }
  5456. // check bankBalance onBuy 2
  5457. int32_t LuaInterface::luaGetPlayerMoney(lua_State* L)
  5458. {
  5459.     //getPlayerMoney(cid[, includeBank = true])
  5460.  
  5461.     bool includeBank = true;
  5462.     if(lua_gettop(L) > 1)
  5463.         includeBank = popBoolean(L);
  5464.  
  5465.     ScriptEnviroment* env = getEnv();
  5466.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5467.         lua_pushnumber(L, g_game.getMoney(player, includeBank));
  5468.     else
  5469.     {
  5470.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5471.         lua_pushboolean(L, false);
  5472.     }
  5473.     return 1;
  5474. }
  5475.  
  5476. int32_t LuaInterface::luaGetHouseInfo(lua_State* L)
  5477. {
  5478.     //getHouseInfo(houseId[, displayError = true])
  5479.     bool displayError = true;
  5480.     if(lua_gettop(L) > 1)
  5481.         displayError = popNumber(L);
  5482.  
  5483.     House* house = Houses::getInstance()->getHouse(popNumber(L));
  5484.     if(!house)
  5485.     {
  5486.         if(displayError)
  5487.             errorEx(getError(LUA_ERROR_HOUSE_NOT_FOUND));
  5488.  
  5489.         lua_pushboolean(L, false);
  5490.         return 1;
  5491.     }
  5492.  
  5493.     lua_newtable(L);
  5494.     setField(L, "id", house->getId());
  5495.     setField(L, "name", house->getName().c_str());
  5496.     setField(L, "owner", house->getOwner());
  5497.  
  5498.     lua_pushstring(L, "entry");
  5499.     pushPosition(L, house->getEntry(), 0);
  5500.     pushTable(L);
  5501.  
  5502.     setField(L, "rent", house->getRent());
  5503.     setField(L, "price", house->getPrice());
  5504.     setField(L, "town", house->getTownId());
  5505.     setField(L, "paidUntil", house->getPaidUntil());
  5506.     setField(L, "warnings", house->getRentWarnings());
  5507.     setField(L, "lastWarning", house->getLastWarning());
  5508.  
  5509.     setFieldBool(L, "guildHall", house->isGuild());
  5510.     setField(L, "size", house->getSize());
  5511.     createTable(L, "doors");
  5512.  
  5513.     HouseDoorList::iterator dit = house->getHouseDoorBegin();
  5514.     for(uint32_t i = 1; dit != house->getHouseDoorEnd(); ++dit, ++i)
  5515.     {
  5516.         lua_pushnumber(L, i);
  5517.         pushPosition(L, (*dit)->getPosition(), 0);
  5518.         pushTable(L);
  5519.     }
  5520.  
  5521.     pushTable(L);
  5522.     createTable(L, "beds");
  5523.  
  5524.     HouseBedList::iterator bit = house->getHouseBedsBegin();
  5525.     for(uint32_t i = 1; bit != house->getHouseBedsEnd(); ++bit, ++i)
  5526.     {
  5527.         lua_pushnumber(L, i);
  5528.         pushPosition(L, (*bit)->getPosition(), 0);
  5529.         pushTable(L);
  5530.     }
  5531.  
  5532.     pushTable(L);
  5533.     createTable(L, "tiles");
  5534.  
  5535.     HouseTileList::iterator tit = house->getHouseTileBegin();
  5536.     for(uint32_t i = 1; tit != house->getHouseTileEnd(); ++tit, ++i)
  5537.     {
  5538.         lua_pushnumber(L, i);
  5539.         pushPosition(L, (*tit)->getPosition(), 0);
  5540.         pushTable(L);
  5541.     }
  5542.  
  5543.     pushTable(L);
  5544.     return 1;
  5545. }
  5546.  
  5547. int32_t LuaInterface::luaGetHouseAccessList(lua_State* L)
  5548. {
  5549.     //getHouseAccessList(houseid, listid)
  5550.     uint32_t listid = popNumber(L);
  5551.     if(House* house = Houses::getInstance()->getHouse(popNumber(L)))
  5552.     {
  5553.         std::string list;
  5554.         if(house->getAccessList(listid, list))
  5555.             lua_pushstring(L, list.c_str());
  5556.         else
  5557.             lua_pushnil(L);
  5558.     }
  5559.     else
  5560.     {
  5561.         errorEx(getError(LUA_ERROR_HOUSE_NOT_FOUND));
  5562.         lua_pushnil(L);
  5563.     }
  5564.  
  5565.     return 1;
  5566. }
  5567.  
  5568. int32_t LuaInterface::luaGetHouseByPlayerGUID(lua_State* L)
  5569. {
  5570.     //getHouseByPlayerGUID(guid)
  5571.     if(House* house = Houses::getInstance()->getHouseByPlayerId(popNumber(L)))
  5572.         lua_pushnumber(L, house->getId());
  5573.     else
  5574.         lua_pushnil(L);
  5575.     return 1;
  5576. }
  5577.  
  5578. int32_t LuaInterface::luaSetHouseAccessList(lua_State* L)
  5579. {
  5580.     //setHouseAccessList(houseid, listid, listtext)
  5581.     std::string list = popString(L);
  5582.     uint32_t listid = popNumber(L);
  5583.  
  5584.     if(House* house = Houses::getInstance()->getHouse(popNumber(L)))
  5585.     {
  5586.         house->setAccessList(listid, list);
  5587.         lua_pushboolean(L, true);
  5588.     }
  5589.     else
  5590.     {
  5591.         errorEx(getError(LUA_ERROR_HOUSE_NOT_FOUND));
  5592.         lua_pushboolean(L, false);
  5593.     }
  5594.  
  5595.     return 1;
  5596. }
  5597.  
  5598. int32_t LuaInterface::luaSetHouseOwner(lua_State* L)
  5599. {
  5600.     //setHouseOwner(houseId, owner[, clean])
  5601.     bool clean = true;
  5602.     if(lua_gettop(L) > 2)
  5603.         clean = popNumber(L);
  5604.  
  5605.     uint32_t owner = popNumber(L);
  5606.     if(House* house = Houses::getInstance()->getHouse(popNumber(L)))
  5607.         lua_pushboolean(L, house->setOwnerEx(owner, clean));
  5608.     else
  5609.     {
  5610.         errorEx(getError(LUA_ERROR_HOUSE_NOT_FOUND));
  5611.         lua_pushboolean(L, false);
  5612.     }
  5613.  
  5614.     return 1;
  5615. }
  5616.  
  5617. int32_t LuaInterface::luaGetWorldType(lua_State* L)
  5618. {
  5619.     lua_pushnumber(L, (uint32_t)g_game.getWorldType());
  5620.     return 1;
  5621. }
  5622.  
  5623. int32_t LuaInterface::luaSetWorldType(lua_State* L)
  5624. {
  5625.     //setWorldType(type)
  5626.     WorldType_t type = (WorldType_t)popNumber(L);
  5627.     if(type >= WORLDTYPE_FIRST && type <= WORLDTYPE_LAST)
  5628.     {
  5629.         g_game.setWorldType(type);
  5630.         lua_pushboolean(L, true);
  5631.     }
  5632.     else
  5633.         lua_pushboolean(L, false);
  5634.  
  5635.     return 1;
  5636. }
  5637.  
  5638. int32_t LuaInterface::luaGetWorldTime(lua_State* L)
  5639. {
  5640.     //getWorldTime()
  5641.     lua_pushnumber(L, g_game.getLightHour());
  5642.     return 1;
  5643. }
  5644.  
  5645. int32_t LuaInterface::luaGetWorldLight(lua_State* L)
  5646. {
  5647.     //getWorldLight()
  5648.     LightInfo lightInfo;
  5649.     g_game.getWorldLightInfo(lightInfo);
  5650.     lua_pushnumber(L, lightInfo.level);
  5651.     lua_pushnumber(L, lightInfo.color);
  5652.     return 2;
  5653. }
  5654.  
  5655. int32_t LuaInterface::luaGetWorldCreatures(lua_State* L)
  5656. {
  5657.     //getWorldCreatures(type)
  5658.     //0 players, 1 monsters, 2 npcs, 3 all
  5659.     uint32_t type = popNumber(L), value;
  5660.     switch(type)
  5661.     {
  5662.         case 0:
  5663.             value = g_game.getPlayersOnline();
  5664.             break;
  5665.         case 1:
  5666.             value = g_game.getMonstersOnline();
  5667.             break;
  5668.         case 2:
  5669.             value = g_game.getNpcsOnline();
  5670.             break;
  5671.         case 3:
  5672.             value = g_game.getCreaturesOnline();
  5673.             break;
  5674.         default:
  5675.             lua_pushboolean(L, false);
  5676.             return 1;
  5677.     }
  5678.  
  5679.     lua_pushnumber(L, value);
  5680.     return 1;
  5681. }
  5682.  
  5683. int32_t LuaInterface::luaGetWorldUpTime(lua_State* L)
  5684. {
  5685.     //getWorldUpTime()
  5686.     uint32_t uptime = 0;
  5687.     if(Status* status = Status::getInstance())
  5688.         uptime = status->getUptime();
  5689.  
  5690.     lua_pushnumber(L, uptime);
  5691.     return 1;
  5692. }
  5693.  
  5694. int32_t LuaInterface::luaGetPlayerLight(lua_State* L)
  5695. {
  5696.     //getPlayerLight(cid)
  5697.     ScriptEnviroment* env = getEnv();
  5698.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  5699.     {
  5700.         LightInfo lightInfo;
  5701.         player->getCreatureLight(lightInfo);
  5702.         lua_pushnumber(L, lightInfo.level);
  5703.         lua_pushnumber(L, lightInfo.color);
  5704.         return 2;
  5705.     }
  5706.     else
  5707.     {
  5708.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5709.         lua_pushboolean(L, false);
  5710.         return 1;
  5711.     }
  5712. }
  5713.  
  5714. int32_t LuaInterface::luaDoPlayerAddExperience(lua_State* L)
  5715. {
  5716.     //doPlayerAddExperience(cid, amount)
  5717.     int64_t amount = popNumber(L);
  5718.  
  5719.     ScriptEnviroment* env = getEnv();
  5720.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5721.     {
  5722.         if(amount > 0)
  5723.             player->addExperience(amount);
  5724.         else if(amount < 0)
  5725.             player->removeExperience(std::abs(amount));
  5726.         else
  5727.         {
  5728.             lua_pushboolean(L, false);
  5729.             return 1;
  5730.         }
  5731.  
  5732.         lua_pushboolean(L, true);
  5733.     }
  5734.     else
  5735.     {
  5736.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5737.         lua_pushboolean(L, false);
  5738.     }
  5739.  
  5740.     return 1;
  5741. }
  5742.  
  5743. int32_t LuaInterface::luaGetPlayerSlotItem(lua_State* L)
  5744. {
  5745.     //getPlayerSlotItem(cid, slot)
  5746.     uint32_t slot = popNumber(L);
  5747.  
  5748.     ScriptEnviroment* env = getEnv();
  5749.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  5750.     {
  5751.         if(Thing* thing = player->__getThing(slot))
  5752.             pushThing(L, thing, env->addThing(thing));
  5753.         else
  5754.             pushThing(L, NULL, 0);
  5755.     }
  5756.     else
  5757.     {
  5758.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5759.         pushThing(L, NULL, 0);
  5760.     }
  5761.  
  5762.     return 1;
  5763. }
  5764.  
  5765. int32_t LuaInterface::luaGetPlayerWeapon(lua_State* L)
  5766. {
  5767.     //getPlayerWeapon(cid[, ignoreAmmo = false])
  5768.     bool ignoreAmmo = false;
  5769.     if(lua_gettop(L) > 1)
  5770.         ignoreAmmo = popNumber(L);
  5771.  
  5772.     ScriptEnviroment* env = getEnv();
  5773.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5774.     {
  5775.         if(Item* weapon = player->getWeapon(ignoreAmmo))
  5776.             pushThing(L, weapon, env->addThing(weapon));
  5777.         else
  5778.             pushThing(L, NULL, 0);
  5779.     }
  5780.     else
  5781.     {
  5782.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5783.         lua_pushnil(L);
  5784.     }
  5785.  
  5786.     return 1;
  5787. }
  5788.  
  5789. int32_t LuaInterface::luaGetPlayerItemById(lua_State* L)
  5790. {
  5791.     //getPlayerItemById(cid, deepSearch, itemId[, subType = -1])
  5792.     ScriptEnviroment* env = getEnv();
  5793.  
  5794.     int32_t subType = -1;
  5795.     if(lua_gettop(L) > 3)
  5796.         subType = (int32_t)popNumber(L);
  5797.  
  5798.     int32_t itemId = (int32_t)popNumber(L);
  5799.     bool deepSearch = popNumber(L);
  5800.  
  5801.     Player* player = env->getPlayerByUID(popNumber(L));
  5802.     if(!player)
  5803.     {
  5804.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5805.         pushThing(L, NULL, 0);
  5806.         return 1;
  5807.     }
  5808.  
  5809.     Item* item = g_game.findItemOfType(player, itemId, deepSearch, subType);
  5810.     if(!item)
  5811.     {
  5812.         pushThing(L, NULL, 0);
  5813.         return 1;
  5814.     }
  5815.  
  5816.     pushThing(L, item, env->addThing(item));
  5817.     return 1;
  5818. }
  5819.  
  5820. int32_t LuaInterface::luaGetThing(lua_State* L)
  5821. {
  5822.     //getThing(uid)
  5823.     uint32_t uid = popNumber(L);
  5824.  
  5825.     ScriptEnviroment* env = getEnv();
  5826.     if(Thing* thing = env->getThingByUID(uid))
  5827.         pushThing(L, thing, uid);
  5828.     else
  5829.     {
  5830.         errorEx(getError(LUA_ERROR_THING_NOT_FOUND));
  5831.         pushThing(L, NULL, 0);
  5832.     }
  5833.  
  5834.     return 1;
  5835. }
  5836.  
  5837. int32_t LuaInterface::luaDoTileQueryAdd(lua_State* L)
  5838. {
  5839.     //doTileQueryAdd(uid, pos[, flags[, displayError = true]])
  5840.     uint32_t flags = 0, params = lua_gettop(L);
  5841.     bool displayError = true;
  5842.     if(params > 3)
  5843.         displayError = popNumber(L);
  5844.  
  5845.     if(params > 2)
  5846.         flags = popNumber(L);
  5847.  
  5848.     PositionEx pos;
  5849.     popPosition(L, pos);
  5850.     uint32_t uid = popNumber(L);
  5851.  
  5852.     ScriptEnviroment* env = getEnv();
  5853.     Tile* tile = g_game.getTile(pos);
  5854.     if(!tile)
  5855.     {
  5856.         if(displayError)
  5857.             errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  5858.  
  5859.         lua_pushnumber(L, (uint32_t)RET_NOTPOSSIBLE);
  5860.         return 1;
  5861.     }
  5862.  
  5863.     Thing* thing = env->getThingByUID(uid);
  5864.     if(!thing)
  5865.     {
  5866.         if(displayError)
  5867.             errorEx(getError(LUA_ERROR_THING_NOT_FOUND));
  5868.  
  5869.         lua_pushnumber(L, (uint32_t)RET_NOTPOSSIBLE);
  5870.         return 1;
  5871.     }
  5872.  
  5873.     lua_pushnumber(L, (uint32_t)tile->__queryAdd(0, thing, 1, flags));
  5874.     return 1;
  5875. }
  5876.  
  5877. int32_t LuaInterface::luaDoItemRaidUnref(lua_State* L)
  5878. {
  5879.     //doItemRaidUnref(uid)
  5880.     ScriptEnviroment* env = getEnv();
  5881.     if(Item* item = env->getItemByUID(popNumber(L)))
  5882.     {
  5883.         if(Raid* raid = item->getRaid())
  5884.         {
  5885.             raid->unRef();
  5886.             item->setRaid(NULL);
  5887.             lua_pushboolean(L, true);
  5888.         }
  5889.         else
  5890.             lua_pushboolean(L, false);
  5891.     }
  5892.     else
  5893.     {
  5894.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  5895.         lua_pushboolean(L, false);
  5896.     }
  5897.  
  5898.     return 1;
  5899. }
  5900.  
  5901. int32_t LuaInterface::luaGetThingPosition(lua_State* L)
  5902. {
  5903.     //getThingPosition(uid)
  5904.     ScriptEnviroment* env = getEnv();
  5905.     if(Thing* thing = env->getThingByUID(popNumber(L)))
  5906.     {
  5907.         Position pos = thing->getPosition();
  5908.         uint32_t stackpos = 0;
  5909.         if(Tile* tile = thing->getTile())
  5910.             stackpos = tile->__getIndexOfThing(thing);
  5911.  
  5912.         pushPosition(L, pos, stackpos);
  5913.     }
  5914.     else
  5915.     {
  5916.         errorEx(getError(LUA_ERROR_THING_NOT_FOUND));
  5917.         lua_pushboolean(L, false);
  5918.     }
  5919.  
  5920.     return 1;
  5921. }
  5922.  
  5923. int32_t LuaInterface::luaCreateCombatObject(lua_State* L)
  5924. {
  5925.     //createCombatObject()
  5926.     ScriptEnviroment* env = getEnv();
  5927.     if(env->getScriptId() != EVENT_ID_LOADING)
  5928.     {
  5929.         errorEx("This function can only be used while loading the script.");
  5930.         lua_pushboolean(L, false);
  5931.         return 1;
  5932.     }
  5933.  
  5934.     Combat* combat = new Combat;
  5935.     if(!combat)
  5936.     {
  5937.         errorEx(getError(LUA_ERROR_COMBAT_NOT_FOUND));
  5938.         lua_pushboolean(L, false);
  5939.         return 1;
  5940.     }
  5941.  
  5942.     lua_pushnumber(L, env->addCombatObject(combat));
  5943.     return 1;
  5944. }
  5945.  
  5946. bool LuaInterface::getArea(lua_State* L, std::list<uint32_t>& list, uint32_t& rows)
  5947. {
  5948.     rows = 0;
  5949.     uint32_t i = 0;
  5950.  
  5951.     lua_pushnil(L);
  5952.     while(lua_next(L, -2))
  5953.     {
  5954.         lua_pushnil(L);
  5955.         while(lua_next(L, -2))
  5956.         {
  5957.             list.push_back((uint32_t)lua_tonumber(L, -1));
  5958.             lua_pop(L, 1); //removes value, keeps key for next iteration
  5959.             ++i;
  5960.         }
  5961.  
  5962.         lua_pop(L, 1); //removes value, keeps key for next iteration
  5963.         ++rows;
  5964.         i = 0;
  5965.     }
  5966.  
  5967.     lua_pop(L, 1);
  5968.     return rows;
  5969. }
  5970.  
  5971. int32_t LuaInterface::luaCreateCombatArea(lua_State* L)
  5972. {
  5973.     //createCombatArea( {area}[, {extArea}])
  5974.     ScriptEnviroment* env = getEnv();
  5975.     if(env->getScriptId() != EVENT_ID_LOADING)
  5976.     {
  5977.         errorEx("This function can only be used while loading the script.");
  5978.         lua_pushboolean(L, false);
  5979.         return 1;
  5980.     }
  5981.  
  5982.     CombatArea* area = new CombatArea;
  5983.     if(lua_gettop(L) > 1)
  5984.     {
  5985.         //has extra parameter with diagonal area information
  5986.         uint32_t rowsExtArea;
  5987.         std::list<uint32_t> listExtArea;
  5988.  
  5989.         getArea(L, listExtArea, rowsExtArea);
  5990.         /*setup all possible rotations*/
  5991.         area->setupExtArea(listExtArea, rowsExtArea);
  5992.     }
  5993.  
  5994.     if(lua_isnoneornil(L, -1)) //prevent crash
  5995.     {
  5996.         lua_pop(L, 2);
  5997.         lua_pushboolean(L, false);
  5998.         return 1;
  5999.     }
  6000.  
  6001.     uint32_t rowsArea = 0;
  6002.     std::list<uint32_t> listArea;
  6003.     getArea(L, listArea, rowsArea);
  6004.  
  6005.     area->setupArea(listArea, rowsArea);
  6006.     lua_pushnumber(L, env->addCombatArea(area));
  6007.     return 1;
  6008. }
  6009.  
  6010. int32_t LuaInterface::luaCreateConditionObject(lua_State* L)
  6011. {
  6012.     //createConditionObject(type[, ticks[, buff[, subId]]])
  6013.     uint32_t params = lua_gettop(L), subId = 0;
  6014.     if(params > 3)
  6015.         subId = popNumber(L);
  6016.  
  6017.     bool buff = false;
  6018.     if(params > 2)
  6019.         buff = popNumber(L);
  6020.  
  6021.     int32_t ticks = 0;
  6022.     if(params > 1)
  6023.         ticks = popNumber(L);
  6024.  
  6025.     ScriptEnviroment* env = getEnv();
  6026.     if(Condition* condition = Condition::createCondition(CONDITIONID_COMBAT, (ConditionType_t)popNumber(L), ticks, 0, buff, subId))
  6027.     {
  6028.         if(env->getScriptId() != EVENT_ID_LOADING)
  6029.             lua_pushnumber(L, env->addTempConditionObject(condition));
  6030.         else
  6031.             lua_pushnumber(L, env->addConditionObject(condition));
  6032.     }
  6033.     else
  6034.     {
  6035.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6036.         lua_pushboolean(L, false);
  6037.     }
  6038.  
  6039.     return 1;
  6040. }
  6041.  
  6042. int32_t LuaInterface::luaSetCombatArea(lua_State* L)
  6043. {
  6044.     //setCombatArea(combat, area)
  6045.     uint32_t areaId = popNumber(L);
  6046.     ScriptEnviroment* env = getEnv();
  6047.     if(env->getScriptId() != EVENT_ID_LOADING)
  6048.     {
  6049.         errorEx("This function can only be used while loading the script.");
  6050.         lua_pushboolean(L, false);
  6051.         return 1;
  6052.     }
  6053.  
  6054.     Combat* combat = env->getCombatObject(popNumber(L));
  6055.     if(!combat)
  6056.     {
  6057.         errorEx(getError(LUA_ERROR_COMBAT_NOT_FOUND));
  6058.         lua_pushboolean(L, false);
  6059.         return 1;
  6060.     }
  6061.  
  6062.     const CombatArea* area = env->getCombatArea(areaId);
  6063.     if(!area)
  6064.     {
  6065.         errorEx(getError(LUA_ERROR_AREA_NOT_FOUND));
  6066.         lua_pushboolean(L, false);
  6067.         return 1;
  6068.     }
  6069.  
  6070.     combat->setArea(new CombatArea(*area));
  6071.     lua_pushboolean(L, true);
  6072.     return 1;
  6073. }
  6074.  
  6075. int32_t LuaInterface::luaSetCombatCondition(lua_State* L)
  6076. {
  6077.     //setCombatCondition(combat, condition)
  6078.     uint32_t conditionId = popNumber(L);
  6079.     ScriptEnviroment* env = getEnv();
  6080.  
  6081.     Combat* combat = env->getCombatObject(popNumber(L));
  6082.     if(!combat)
  6083.     {
  6084.         errorEx(getError(LUA_ERROR_COMBAT_NOT_FOUND));
  6085.         lua_pushboolean(L, false);
  6086.         return 1;
  6087.     }
  6088.  
  6089.     const Condition* condition = env->getConditionObject(conditionId);
  6090.     if(!condition)
  6091.     {
  6092.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6093.         lua_pushboolean(L, false);
  6094.         return 1;
  6095.     }
  6096.  
  6097.     combat->setCondition(condition->clone());
  6098.     lua_pushboolean(L, true);
  6099.     return 1;
  6100. }
  6101.  
  6102. int32_t LuaInterface::luaSetCombatParam(lua_State* L)
  6103. {
  6104.     //setCombatParam(combat, key, value)
  6105.     uint32_t value = popNumber(L);
  6106.     CombatParam_t key = (CombatParam_t)popNumber(L);
  6107.  
  6108.     ScriptEnviroment* env = getEnv();
  6109.     if(env->getScriptId() != EVENT_ID_LOADING)
  6110.     {
  6111.         errorEx("This function can only be used while loading the script.");
  6112.         lua_pushboolean(L, false);
  6113.         return 1;
  6114.     }
  6115.  
  6116.     Combat* combat = env->getCombatObject(popNumber(L));
  6117.     if(!combat)
  6118.     {
  6119.         errorEx(getError(LUA_ERROR_COMBAT_NOT_FOUND));
  6120.         lua_pushboolean(L, false);
  6121.     }
  6122.     else
  6123.     {
  6124.         combat->setParam(key, value);
  6125.         lua_pushboolean(L, true);
  6126.     }
  6127.  
  6128.     return 1;
  6129. }
  6130.  
  6131. int32_t LuaInterface::luaSetConditionParam(lua_State* L)
  6132. {
  6133.     //setConditionParam(condition, key, value)
  6134.     int32_t value = popNumber(L);
  6135.     ScriptEnviroment* env = getEnv();
  6136.  
  6137.     ConditionParam_t key = (ConditionParam_t)popNumber(L);
  6138.     if(Condition* condition = env->getConditionObject(popNumber(L)))
  6139.     {
  6140.         condition->setParam(key, value);
  6141.         lua_pushboolean(L, true);
  6142.     }
  6143.     else
  6144.     {
  6145.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6146.         lua_pushboolean(L, false);
  6147.     }
  6148.  
  6149.     return 1;
  6150. }
  6151.  
  6152. int32_t LuaInterface::luaAddDamageCondition(lua_State* L)
  6153. {
  6154.     //addDamageCondition(condition, rounds, time, value)
  6155.     int32_t value = popNumber(L), time = popNumber(L), rounds = popNumber(L);
  6156.     ScriptEnviroment* env = getEnv();
  6157.     if(ConditionDamage* condition = dynamic_cast<ConditionDamage*>(env->getConditionObject(popNumber(L))))
  6158.     {
  6159.         condition->addDamage(rounds, time, value);
  6160.         lua_pushboolean(L, true);
  6161.     }
  6162.     else
  6163.     {
  6164.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6165.         lua_pushboolean(L, false);
  6166.     }
  6167.  
  6168.     return 1;
  6169. }
  6170.  
  6171. int32_t LuaInterface::luaAddOutfitCondition(lua_State* L)
  6172. {
  6173.     //addOutfitCondition(condition, outfit)
  6174.     Outfit_t outfit = popOutfit(L);
  6175.     ScriptEnviroment* env = getEnv();
  6176.     if(ConditionOutfit* condition = dynamic_cast<ConditionOutfit*>(env->getConditionObject(popNumber(L))))
  6177.     {
  6178.         condition->addOutfit(outfit);
  6179.         lua_pushboolean(L, true);
  6180.     }
  6181.     else
  6182.     {
  6183.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6184.         lua_pushboolean(L, false);
  6185.     }
  6186.  
  6187.     return 1;
  6188. }
  6189.  
  6190. int32_t LuaInterface::luaSetCombatCallBack(lua_State* L)
  6191. {
  6192.     //setCombatCallBack(combat, key, functionName)
  6193.     std::string function = popString(L);
  6194.     CallBackParam_t key = (CallBackParam_t)popNumber(L);
  6195.  
  6196.     ScriptEnviroment* env = getEnv();
  6197.     if(env->getScriptId() != EVENT_ID_LOADING)
  6198.     {
  6199.         errorEx("This function can only be used while loading the script.");
  6200.         lua_pushboolean(L, false);
  6201.         return 1;
  6202.     }
  6203.  
  6204.     Combat* combat = env->getCombatObject(popNumber(L));
  6205.     if(!combat)
  6206.     {
  6207.         errorEx(getError(LUA_ERROR_COMBAT_NOT_FOUND));
  6208.         lua_pushboolean(L, false);
  6209.         return 1;
  6210.     }
  6211.  
  6212.     LuaInterface* interface = env->getInterface();
  6213.     combat