Advertisement
Guest User

luascript.cpp

a guest
Dec 3rd, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 277.61 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////
  2. // OpenTibia - an opensource roleplaying game
  3. ////////////////////////////////////////////////////////////////////////
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16. ////////////////////////////////////////////////////////////////////////
  17. #include "otpch.h"
  18. #include "luascript.h"
  19. #include "scriptmanager.h"
  20.  
  21. #include <boost/filesystem.hpp>
  22. #include <boost/any.hpp>
  23. #include <iostream>
  24. #include <iomanip>
  25.  
  26. #include "player.h"
  27. #include "item.h"
  28. #include "teleport.h"
  29. #include "beds.h"
  30.  
  31. #include "town.h"
  32. #include "house.h"
  33. #include "housetile.h"
  34.  
  35. #include "database.h"
  36. #include "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.  
  2091.  
  2092.  
  2093.     // /ping 1
  2094.  
  2095.     //doSetItemOutfit(cid, item[, time = -1])
  2096.     lua_register(m_luaState, "doSetItemOutfit", LuaInterface::luaSetItemOutfit);
  2097.  
  2098.     //doSetCreatureOutfit(cid, outfit[, time = -1])
  2099.     lua_register(m_luaState, "doSetCreatureOutfit", LuaInterface::luaSetCreatureOutfit);
  2100.  
  2101.     //getCreatureOutfit(cid)
  2102.     lua_register(m_luaState, "getCreatureOutfit", LuaInterface::luaGetCreatureOutfit);
  2103.  
  2104.     //getCreatureLastPosition(cid)
  2105.     lua_register(m_luaState, "getCreatureLastPosition", LuaInterface::luaGetCreatureLastPosition);
  2106.  
  2107.     //getCreatureName(cid)
  2108.     lua_register(m_luaState, "getCreatureName", LuaInterface::luaGetCreatureName);
  2109.    
  2110.     //getCreaturePathTo(cid, pos, maxSearchDist)
  2111.     lua_register(m_luaState, "getCreaturePathTo", LuaInterface::luaGetCreaturePathTo);
  2112.  
  2113.     //getCreatureSpeed(cid)
  2114.     lua_register(m_luaState, "getCreatureSpeed", LuaInterface::luaGetCreatureSpeed);
  2115.  
  2116.     //getCreatureBaseSpeed(cid)
  2117.     lua_register(m_luaState, "getCreatureBaseSpeed", LuaInterface::luaGetCreatureBaseSpeed);
  2118.  
  2119.     //getCreatureTarget(cid)
  2120.     lua_register(m_luaState, "getCreatureTarget", LuaInterface::luaGetCreatureTarget);
  2121.  
  2122.     //isSightClear(fromPos, toPos, floorCheck)
  2123.     lua_register(m_luaState, "isSightClear", LuaInterface::luaIsSightClear);
  2124.  
  2125.     //isInArray(array, value[, caseSensitive = false])
  2126.     lua_register(m_luaState, "isInArray", LuaInterface::luaIsInArray);
  2127.  
  2128.     //addEvent(callback, delay, ...)
  2129.     lua_register(m_luaState, "addEvent", LuaInterface::luaAddEvent);
  2130.  
  2131.     //stopEvent(eventid)
  2132.     lua_register(m_luaState, "stopEvent", LuaInterface::luaStopEvent);
  2133.  
  2134.     //getPlayersByAccountId(accId)
  2135.     lua_register(m_luaState, "getPlayersByAccountId", LuaInterface::luaGetPlayersByAccountId);
  2136.  
  2137.     //getAccountIdByName(name)
  2138.     lua_register(m_luaState, "getAccountIdByName", LuaInterface::luaGetAccountIdByName);
  2139.  
  2140.     //getAccountByName(name)
  2141.     lua_register(m_luaState, "getAccountByName", LuaInterface::luaGetAccountByName);
  2142.  
  2143.     //getAccountIdByAccount(accName)
  2144.     lua_register(m_luaState, "getAccountIdByAccount", LuaInterface::luaGetAccountIdByAccount);
  2145.  
  2146.     //getAccountByAccountId(accId)
  2147.     lua_register(m_luaState, "getAccountByAccountId", LuaInterface::luaGetAccountByAccountId);
  2148.  
  2149.     //getIpByName(name)
  2150.     lua_register(m_luaState, "getIpByName", LuaInterface::luaGetIpByName);
  2151.  
  2152.     //getPlayersByIp(ip[, mask = 0xFFFFFFFF])
  2153.     lua_register(m_luaState, "getPlayersByIp", LuaInterface::luaGetPlayersByIp);
  2154.  
  2155.     //doPlayerPopupFYI(cid, message)
  2156.     lua_register(m_luaState, "doPlayerPopupFYI", LuaInterface::luaDoPlayerPopupFYI);
  2157.  
  2158.     //doPlayerSendTutorial(cid, id)
  2159.     lua_register(m_luaState, "doPlayerSendTutorial", LuaInterface::luaDoPlayerSendTutorial);
  2160.  
  2161.     //doPlayerSendMailByName(name, item[, town[, actor]])
  2162.     lua_register(m_luaState, "doPlayerSendMailByName", LuaInterface::luaDoPlayerSendMailByName);
  2163.  
  2164.     //doPlayerAddMapMark(cid, pos, type[, description])
  2165.     lua_register(m_luaState, "doPlayerAddMapMark", LuaInterface::luaDoPlayerAddMapMark);
  2166.  
  2167.     //doPlayerAddPremiumDays(cid, days)
  2168.     lua_register(m_luaState, "doPlayerAddPremiumDays", LuaInterface::luaDoPlayerAddPremiumDays);
  2169.  
  2170.     //getPlayerPremiumDays(cid)
  2171.     lua_register(m_luaState, "getPlayerPremiumDays", LuaInterface::luaGetPlayerPremiumDays);
  2172.  
  2173.     //doCreatureSetLookDirection(cid, dir)
  2174.     lua_register(m_luaState, "doCreatureSetLookDirection", LuaInterface::luaDoCreatureSetLookDir);
  2175.  
  2176.     //getCreatureGuildEmblem(cid[, target])
  2177.     lua_register(m_luaState, "getCreatureGuildEmblem", LuaInterface::luaGetCreatureGuildEmblem);
  2178.  
  2179.     //doCreatureSetGuildEmblem(cid, emblem)
  2180.     lua_register(m_luaState, "doCreatureSetGuildEmblem", LuaInterface::luaDoCreatureSetGuildEmblem);
  2181.  
  2182.     //getCreaturePartyShield(cid[, target])
  2183.     lua_register(m_luaState, "getCreaturePartyShield", LuaInterface::luaGetCreaturePartyShield);
  2184.  
  2185.     //doCreatureSetPartyShield(cid, shield)
  2186.     lua_register(m_luaState, "doCreatureSetPartyShield", LuaInterface::luaDoCreatureSetPartyShield);
  2187.  
  2188.     //getCreatureSkullType(cid[, target])
  2189.     lua_register(m_luaState, "getCreatureSkullType", LuaInterface::luaGetCreatureSkullType);
  2190.  
  2191.     //doCreatureSetSkullType(cid, skull)
  2192.     lua_register(m_luaState, "doCreatureSetSkullType", LuaInterface::luaDoCreatureSetSkullType);
  2193.  
  2194.     //getPlayerSkullEnd(cid)
  2195.     lua_register(m_luaState, "getPlayerSkullEnd", LuaInterface::luaGetPlayerSkullEnd);
  2196.  
  2197.     //doPlayerSetSkullEnd(cid, time, type)
  2198.     lua_register(m_luaState, "doPlayerSetSkullEnd", LuaInterface::luaDoPlayerSetSkullEnd);
  2199.  
  2200.     //getPlayerBlessing(cid, blessing)
  2201.     lua_register(m_luaState, "getPlayerBlessing", LuaInterface::luaGetPlayerBlessing);
  2202.  
  2203.     //doPlayerAddBlessing(cid, blessing)
  2204.     lua_register(m_luaState, "doPlayerAddBlessing", LuaInterface::luaDoPlayerAddBlessing);
  2205.  
  2206.     //getPlayerStamina(cid)
  2207.     lua_register(m_luaState, "getPlayerStamina", LuaInterface::luaGetPlayerStamina);
  2208.  
  2209.     //doPlayerSetStamina(cid, minutes)
  2210.     lua_register(m_luaState, "doPlayerSetStamina", LuaInterface::luaDoPlayerSetStamina);
  2211.  
  2212.     //getPlayerBalance(cid)
  2213.     lua_register(m_luaState, "getPlayerBalance", LuaInterface::luaGetPlayerBalance);
  2214.  
  2215.     //doPlayerSetBalance(cid, balance)
  2216.     lua_register(m_luaState, "doPlayerSetBalance", LuaInterface::luaDoPlayerSetBalance);
  2217.  
  2218.     //getCreatureNoMove(cid)
  2219.     lua_register(m_luaState, "getCreatureNoMove", LuaInterface::luaGetCreatureNoMove);
  2220.  
  2221.     //doCreatureSetNoMove(cid, block)
  2222.     lua_register(m_luaState, "doCreatureSetNoMove", LuaInterface::luaDoCreatureSetNoMove);
  2223.  
  2224.     //getPlayerIdleTime(cid)
  2225.     lua_register(m_luaState, "getPlayerIdleTime", LuaInterface::luaGetPlayerIdleTime);
  2226.  
  2227.     //doPlayerSetIdleTime(cid, amount)
  2228.     lua_register(m_luaState, "doPlayerSetIdleTime", LuaInterface::luaDoPlayerSetIdleTime);
  2229.  
  2230.     //getPlayerLastLoad(cid)
  2231.     lua_register(m_luaState, "getPlayerLastLoad", LuaInterface::luaGetPlayerLastLoad);
  2232.  
  2233.     //getPlayerLastLogin(cid)
  2234.     lua_register(m_luaState, "getPlayerLastLogin", LuaInterface::luaGetPlayerLastLogin);
  2235.  
  2236.     //getPlayerAccountManager(cid)
  2237.     lua_register(m_luaState, "getPlayerAccountManager", LuaInterface::luaGetPlayerAccountManager);
  2238.  
  2239.     //getPlayerTradeState(cid)
  2240.     lua_register(m_luaState, "getPlayerTradeState", LuaInterface::luaGetPlayerTradeState);
  2241.  
  2242.     //getPlayerModes(cid)
  2243.     lua_register(m_luaState, "getPlayerModes", LuaInterface::luaGetPlayerModes);
  2244.  
  2245.     //getPlayerRates(cid)
  2246.     lua_register(m_luaState, "getPlayerRates", LuaInterface::luaGetPlayerRates);
  2247.  
  2248.     //doPlayerSetRate(cid, type, value)
  2249.     lua_register(m_luaState, "doPlayerSetRate", LuaInterface::luaDoPlayerSetRate);
  2250.  
  2251.     // change loot rates per player 2
  2252.     //doPlayerSetSpecialRateLoot(cid, newRate)
  2253.     lua_register(m_luaState, "doPlayerSetSpecialRateLoot", LuaInterface::luaDoPlayerSetSpecialRateLoot);
  2254.  
  2255.     //doPlayerGetSpecialRateLoot(cid)
  2256.     lua_register(m_luaState, "doPlayerGetSpecialRateLoot", LuaInterface::luaDoPlayerGetSpecialRateLoot);
  2257.  
  2258.     //getPlayerPartner(cid)
  2259.     lua_register(m_luaState, "getPlayerPartner", LuaInterface::luaGetPlayerPartner);
  2260.  
  2261.     //doPlayerSetPartner(cid, guid)
  2262.     lua_register(m_luaState, "doPlayerSetPartner", LuaInterface::luaDoPlayerSetPartner);
  2263.  
  2264.     //doPlayerFollowCreature(cid, target)
  2265.     lua_register(m_luaState, "doPlayerFollowCreature", LuaInterface::luaDoPlayerFollowCreature);
  2266.  
  2267.     //getPlayerParty(cid)
  2268.     lua_register(m_luaState, "getPlayerParty", LuaInterface::luaGetPlayerParty);
  2269.  
  2270.     //doPlayerJoinParty(cid, lid)
  2271.     lua_register(m_luaState, "doPlayerJoinParty", LuaInterface::luaDoPlayerJoinParty);
  2272.  
  2273.     //doPlayerLeaveParty(cid[, forced = false])
  2274.     lua_register(m_luaState, "doPlayerLeaveParty", LuaInterface::luaDoPlayerLeaveParty);
  2275.  
  2276.     //getPartyMembers(lid)
  2277.     lua_register(m_luaState, "getPartyMembers", LuaInterface::luaGetPartyMembers);
  2278.  
  2279.     //getCreatureMaster(cid)
  2280.     lua_register(m_luaState, "getCreatureMaster", LuaInterface::luaGetCreatureMaster);
  2281.  
  2282.     //getCreatureSummons(cid)
  2283.     lua_register(m_luaState, "getCreatureSummons", LuaInterface::luaGetCreatureSummons);
  2284.  
  2285.     //getTownId(townName)
  2286.     lua_register(m_luaState, "getTownId", LuaInterface::luaGetTownId);
  2287.  
  2288.     //getTownName(townId)
  2289.     lua_register(m_luaState, "getTownName", LuaInterface::luaGetTownName);
  2290.  
  2291.     //getTownTemplePosition(townId)
  2292.     lua_register(m_luaState, "getTownTemplePosition", LuaInterface::luaGetTownTemplePosition);
  2293.  
  2294.     //getTownHouses(townId)
  2295.     lua_register(m_luaState, "getTownHouses", LuaInterface::luaGetTownHouses);
  2296.  
  2297.     //getSpectators(centerPos, rangex, rangey[, multifloor = false])
  2298.     lua_register(m_luaState, "getSpectators", LuaInterface::luaGetSpectators);
  2299.  
  2300.     //getVocationInfo(id)
  2301.     lua_register(m_luaState, "getVocationInfo", LuaInterface::luaGetVocationInfo);
  2302.  
  2303.     //getGroupInfo(id[, premium = false])
  2304.     lua_register(m_luaState, "getGroupInfo", LuaInterface::luaGetGroupInfo);
  2305.  
  2306.     //getTownList()
  2307.     lua_register(m_luaState, "getTownList", LuaInterface::luaGetTownList);
  2308.  
  2309.     //getWaypointList()
  2310.     lua_register(m_luaState, "getWaypointList", LuaInterface::luaGetWaypointList);
  2311.  
  2312.     //getTalkActionList()
  2313.     lua_register(m_luaState, "getTalkActionList", LuaInterface::luaGetTalkActionList);
  2314.  
  2315.     //getExperienceStageList()
  2316.     lua_register(m_luaState, "getExperienceStageList", LuaInterface::luaGetExperienceStageList);
  2317.  
  2318.     //getItemIdByName(name[, displayError = true])
  2319.     lua_register(m_luaState, "getItemIdByName", LuaInterface::luaGetItemIdByName);
  2320.  
  2321.     //getItemInfo(itemid)
  2322.     lua_register(m_luaState, "getItemInfo", LuaInterface::luaGetItemInfo);
  2323.  
  2324.     //getItemAttribute(uid, key)
  2325.     lua_register(m_luaState, "getItemAttribute", LuaInterface::luaGetItemAttribute);
  2326.  
  2327.     //doItemSetAttribute(uid, key, value)
  2328.     lua_register(m_luaState, "doItemSetAttribute", LuaInterface::luaDoItemSetAttribute);
  2329.  
  2330.     //doItemEraseAttribute(uid, key)
  2331.     lua_register(m_luaState, "doItemEraseAttribute", LuaInterface::luaDoItemEraseAttribute);
  2332.  
  2333.     //getItemWeight(uid[, precise = true])
  2334.     lua_register(m_luaState, "getItemWeight", LuaInterface::luaGetItemWeight);
  2335.  
  2336.     //getItemParent(uid)
  2337.     lua_register(m_luaState, "getItemParent", LuaInterface::luaGetItemParent);
  2338.  
  2339.     //hasItemProperty(uid, prop)
  2340.     lua_register(m_luaState, "hasItemProperty", LuaInterface::luaHasItemProperty);
  2341.  
  2342.     //hasPlayerClient(cid)
  2343.     lua_register(m_luaState, "hasPlayerClient", LuaInterface::luaHasPlayerClient);
  2344.  
  2345.     //isIpBanished(ip[, mask])
  2346.     lua_register(m_luaState, "isIpBanished", LuaInterface::luaIsIpBanished);
  2347.  
  2348.     //isPlayerBanished(name/guid, type)
  2349.     lua_register(m_luaState, "isPlayerBanished", LuaInterface::luaIsPlayerBanished);
  2350.  
  2351.     //isAccountBanished(accountId[, playerId])
  2352.     lua_register(m_luaState, "isAccountBanished", LuaInterface::luaIsAccountBanished);
  2353.  
  2354.     //doAddIpBanishment(...)
  2355.     lua_register(m_luaState, "doAddIpBanishment", LuaInterface::luaDoAddIpBanishment);
  2356.  
  2357.     //doAddPlayerBanishment(...)
  2358.     lua_register(m_luaState, "doAddPlayerBanishment", LuaInterface::luaDoAddPlayerBanishment);
  2359.  
  2360.     //doAddAccountBanishment(...)
  2361.     lua_register(m_luaState, "doAddAccountBanishment", LuaInterface::luaDoAddAccountBanishment);
  2362.  
  2363.     //doAddNotation(...)
  2364.     lua_register(m_luaState, "doAddNotation", LuaInterface::luaDoAddNotation);
  2365.  
  2366.     //doAddStatement(...)
  2367.     lua_register(m_luaState, "doAddStatement", LuaInterface::luaDoAddStatement);
  2368.  
  2369.     //doRemoveIpBanishment(ip[, mask])
  2370.     lua_register(m_luaState, "doRemoveIpBanishment", LuaInterface::luaDoRemoveIpBanishment);
  2371.  
  2372.     //doRemovePlayerBanishment(name/guid, type)
  2373.     lua_register(m_luaState, "doRemovePlayerBanishment", LuaInterface::luaDoRemovePlayerBanishment);
  2374.  
  2375.     //doRemoveAccountBanishment(accountId[, playerId])
  2376.     lua_register(m_luaState, "doRemoveAccountBanishment", LuaInterface::luaDoRemoveAccountBanishment);
  2377.  
  2378.     //doRemoveNotations(accountId[, playerId])
  2379.     lua_register(m_luaState, "doRemoveNotations", LuaInterface::luaDoRemoveNotations);
  2380.  
  2381.     //doRemoveStatements(name/guid[, channelId])
  2382.     lua_register(m_luaState, "doRemoveStatements", LuaInterface::luaDoRemoveStatements);
  2383.  
  2384.     //getNotationsCount(accountId[, playerId])
  2385.     lua_register(m_luaState, "getNotationsCount", LuaInterface::luaGetNotationsCount);
  2386.  
  2387.     //getStatementsCount(name/guid[, channelId])
  2388.     lua_register(m_luaState, "getStatementsCount", LuaInterface::luaGetStatementsCount);
  2389.  
  2390.     //getBanData(value[, type[, param]])
  2391.     lua_register(m_luaState, "getBanData", LuaInterface::luaGetBanData);
  2392.  
  2393.     //getBanReason(id)
  2394.     lua_register(m_luaState, "getBanReason", LuaInterface::luaGetBanReason);
  2395.  
  2396.     //getBanAction(id[, ipBanishment = false])
  2397.     lua_register(m_luaState, "getBanAction", LuaInterface::luaGetBanAction);
  2398.  
  2399.     //getBanList(type[, value[, param]])
  2400.     lua_register(m_luaState, "getBanList", LuaInterface::luaGetBanList);
  2401.  
  2402.     //getExperienceStage(level)
  2403.     lua_register(m_luaState, "getExperienceStage", LuaInterface::luaGetExperienceStage);
  2404.  
  2405.     //getDataDir()
  2406.     lua_register(m_luaState, "getDataDir", LuaInterface::luaGetDataDir);
  2407.  
  2408.     //getLogsDir()
  2409.     lua_register(m_luaState, "getLogsDir", LuaInterface::luaGetLogsDir);
  2410.  
  2411.     //getConfigFile()
  2412.     lua_register(m_luaState, "getConfigFile", LuaInterface::luaGetConfigFile);
  2413.  
  2414.     //isPlayerUsingOtclient(cid)
  2415.     lua_register(m_luaState, "isPlayerUsingOtclient", LuaInterface::luaIsPlayerUsingOtclient);
  2416.  
  2417.     //doPlayerSendExtendedOpcode(cid, opcode, buffer)
  2418.     lua_register(m_luaState, "doPlayerSendExtendedOpcode", LuaInterface::luaDoPlayerSendExtendedOpcode);
  2419.  
  2420.     //getConfigValue(key)
  2421.     lua_register(m_luaState, "getConfigValue", LuaInterface::luaGetConfigValue);
  2422.  
  2423.     //getModList()
  2424.     lua_register(m_luaState, "getModList", LuaInterface::luaGetModList);
  2425.  
  2426.     //getHighscoreString(skillId)
  2427.     lua_register(m_luaState, "getHighscoreString", LuaInterface::luaGetHighscoreString);
  2428.  
  2429.     //getWaypointPosition(name)
  2430.     lua_register(m_luaState, "getWaypointPosition", LuaInterface::luaGetWaypointPosition);
  2431.  
  2432.     //doWaypointAddTemporial(name, pos)
  2433.     lua_register(m_luaState, "doWaypointAddTemporial", LuaInterface::luaDoWaypointAddTemporial);
  2434.  
  2435.     //getGameState()
  2436.     lua_register(m_luaState, "getGameState", LuaInterface::luaGetGameState);
  2437.  
  2438.     //doSetGameState(id)
  2439.     lua_register(m_luaState, "doSetGameState", LuaInterface::luaDoSetGameState);
  2440.  
  2441.     //doExecuteRaid(name)
  2442.     lua_register(m_luaState, "doExecuteRaid", LuaInterface::luaDoExecuteRaid);
  2443.  
  2444.     //doCreatureExecuteTalkAction(cid, text[, ignoreAccess = false[, channelId = CHANNEL_DEFAULT]])
  2445.     lua_register(m_luaState, "doCreatureExecuteTalkAction", LuaInterface::luaDoCreatureExecuteTalkAction);
  2446.  
  2447.     //doReloadInfo(id[, cid])
  2448.     lua_register(m_luaState, "doReloadInfo", LuaInterface::luaDoReloadInfo);
  2449.  
  2450.     //doSaveServer([shallow = false])
  2451.     lua_register(m_luaState, "doSaveServer", LuaInterface::luaDoSaveServer);
  2452.  
  2453.     //doCleanHouse(houseId)
  2454.     lua_register(m_luaState, "doCleanHouse", LuaInterface::luaDoCleanHouse);
  2455.  
  2456.     //doCleanMap()
  2457.     lua_register(m_luaState, "doCleanMap", LuaInterface::luaDoCleanMap);
  2458.  
  2459.     //doRefreshMap()
  2460.     lua_register(m_luaState, "doRefreshMap", LuaInterface::luaDoRefreshMap);
  2461.  
  2462.     //doGuildAddEnemy(guild, enemy, war, type)
  2463.     lua_register(m_luaState, "doGuildAddEnemy", LuaInterface::luaDoGuildAddEnemy);
  2464.  
  2465.     //doGuildRemoveEnemy(guild, enemy)
  2466.     lua_register(m_luaState, "doGuildRemoveEnemy", LuaInterface::luaDoGuildRemoveEnemy);
  2467.  
  2468.     //doUpdateHouseAuctions()
  2469.     lua_register(m_luaState, "doUpdateHouseAuctions", LuaInterface::luaDoUpdateHouseAuctions);
  2470.  
  2471.     //loadmodlib(lib)
  2472.     lua_register(m_luaState, "loadmodlib", LuaInterface::luaL_loadmodlib);
  2473.  
  2474.     //domodlib(lib)
  2475.     lua_register(m_luaState, "domodlib", LuaInterface::luaL_domodlib);
  2476.  
  2477.     //dodirectory(dir)
  2478.     lua_register(m_luaState, "dodirectory", LuaInterface::luaL_dodirectory);
  2479.  
  2480.     //errors(var)
  2481.     lua_register(m_luaState, "errors", LuaInterface::luaL_errors);
  2482.  
  2483.     //os table
  2484.     luaL_register(m_luaState, "os", LuaInterface::luaSystemTable);
  2485.  
  2486.     //db table
  2487.     luaL_register(m_luaState, "db", LuaInterface::luaDatabaseTable);
  2488.  
  2489.     //result table
  2490.     luaL_register(m_luaState, "result", LuaInterface::luaResultTable);
  2491.  
  2492.     //bit table
  2493.     luaL_register(m_luaState, "bit", LuaInterface::luaBitTable);
  2494.  
  2495.     //std table
  2496.     luaL_register(m_luaState, "std", LuaInterface::luaStdTable);
  2497. }
  2498.  
  2499. const luaL_Reg LuaInterface::luaSystemTable[] =
  2500. {
  2501.     //os.mtime()
  2502.     {"mtime", LuaInterface::luaSystemTime},
  2503.  
  2504.     {NULL, NULL}
  2505. };
  2506.  
  2507. const luaL_Reg LuaInterface::luaDatabaseTable[] =
  2508. {
  2509.     //db.query(query)
  2510.     {"query", LuaInterface::luaDatabaseExecute},
  2511.  
  2512.     //db.storeQuery(query)
  2513.     {"storeQuery", LuaInterface::luaDatabaseStoreQuery},
  2514.  
  2515.     //db.escapeString(str)
  2516.     {"escapeString", LuaInterface::luaDatabaseEscapeString},
  2517.  
  2518.     //db.escapeBlob(s, length)
  2519.     {"escapeBlob", LuaInterface::luaDatabaseEscapeBlob},
  2520.  
  2521.     //db.lastInsertId()
  2522.     {"lastInsertId", LuaInterface::luaDatabaseLastInsertId},
  2523.  
  2524.     //db.stringComparer()
  2525.     {"stringComparer", LuaInterface::luaDatabaseStringComparer},
  2526.  
  2527.     //db.updateLimiter()
  2528.     {"updateLimiter", LuaInterface::luaDatabaseUpdateLimiter},
  2529.  
  2530.     {NULL, NULL}
  2531. };
  2532.  
  2533. const luaL_Reg LuaInterface::luaResultTable[] =
  2534. {
  2535.     //result.getDataInt(resId, s)
  2536.     {"getDataInt", LuaInterface::luaResultGetDataInt},
  2537.  
  2538.     //result.getDataLong(resId, s)
  2539.     {"getDataLong", LuaInterface::luaResultGetDataLong},
  2540.  
  2541.     //result.getDataString(resId, s)
  2542.     {"getDataString", LuaInterface::luaResultGetDataString},
  2543.  
  2544.     //result.getDataStream(resId, s, length)
  2545.     {"getDataStream", LuaInterface::luaResultGetDataStream},
  2546.  
  2547.     //result.next(resId)
  2548.     {"next", LuaInterface::luaResultNext},
  2549.  
  2550.     //result.free(resId)
  2551.     {"free", LuaInterface::luaResultFree},
  2552.  
  2553.     {NULL, NULL}
  2554. };
  2555.  
  2556. const luaL_Reg LuaInterface::luaBitTable[] =
  2557. {
  2558.     //{"cast", LuaInterface::luaBitCast},
  2559.     {"bnot", LuaInterface::luaBitNot},
  2560.     {"band", LuaInterface::luaBitAnd},
  2561.     {"bor", LuaInterface::luaBitOr},
  2562.     {"bxor", LuaInterface::luaBitXor},
  2563.     {"lshift", LuaInterface::luaBitLeftShift},
  2564.     {"rshift", LuaInterface::luaBitRightShift},
  2565.     //{"arshift", LuaInterface::luaBitArithmeticalRightShift},
  2566.  
  2567.     //{"ucast", LuaInterface::luaBitUCast},
  2568.     {"ubnot", LuaInterface::luaBitUNot},
  2569.     {"uband", LuaInterface::luaBitUAnd},
  2570.     {"ubor", LuaInterface::luaBitUOr},
  2571.     {"ubxor", LuaInterface::luaBitUXor},
  2572.     {"ulshift", LuaInterface::luaBitULeftShift},
  2573.     {"urshift", LuaInterface::luaBitURightShift},
  2574.     //{"uarshift", LuaInterface::luaBitUArithmeticalRightShift},
  2575.  
  2576.     {NULL, NULL}
  2577. };
  2578.  
  2579. const luaL_Reg LuaInterface::luaStdTable[] =
  2580. {
  2581.     {"cout", LuaInterface::luaStdCout},
  2582.     {"clog", LuaInterface::luaStdClog},
  2583.     {"cerr", LuaInterface::luaStdCerr},
  2584.  
  2585.     {"md5", LuaInterface::luaStdMD5},
  2586.     {"sha1", LuaInterface::luaStdSHA1},
  2587.     {"sha256", LuaInterface::luaStdSHA256},
  2588.     {"sha512", LuaInterface::luaStdSHA512},
  2589.  
  2590.     {NULL, NULL}
  2591. };
  2592.  
  2593. int32_t LuaInterface::internalGetPlayerInfo(lua_State* L, PlayerInfo_t info)
  2594. {
  2595.     ScriptEnviroment* env = getEnv();
  2596.     const Player* player = env->getPlayerByUID(popNumber(L));
  2597.     if(!player)
  2598.     {
  2599.         std::stringstream s;
  2600.         s << getError(LUA_ERROR_PLAYER_NOT_FOUND) << " when requesting player info #" << info;
  2601.         errorEx(s.str());
  2602.  
  2603.         lua_pushboolean(L, false);
  2604.         return 1;
  2605.     }
  2606.  
  2607.     int64_t value = 0;
  2608.     Position pos;
  2609.     switch(info)
  2610.     {
  2611.         case PlayerInfoNameDescription:
  2612.             lua_pushstring(L, player->getNameDescription().c_str());
  2613.             return 1;
  2614.         case PlayerInfoSpecialDescription:
  2615.             lua_pushstring(L, player->getSpecialDescription().c_str());
  2616.             return 1;
  2617.         case PlayerInfoAccess:
  2618.             value = player->getAccess();
  2619.             break;
  2620.         case PlayerInfoGhostAccess:
  2621.             value = player->getGhostAccess();
  2622.             break;
  2623.         case PlayerInfoLevel:
  2624.             value = player->getLevel();
  2625.             break;
  2626.         case PlayerInfoExperience:
  2627.             value = player->getExperience();
  2628.             break;
  2629.         case PlayerInfoManaSpent:
  2630.             value = player->getSpentMana();
  2631.             break;
  2632.         case PlayerInfoTown:
  2633.             value = player->getTown();
  2634.             break;
  2635.         case PlayerInfoPromotionLevel:
  2636.             value = player->getPromotionLevel();
  2637.             break;
  2638.         case PlayerInfoGUID:
  2639.             value = player->getGUID();
  2640.             break;
  2641.         case PlayerInfoAccountId:
  2642.             value = player->getAccount();
  2643.             break;
  2644.         case PlayerInfoAccount:
  2645.             lua_pushstring(L, player->getAccountName().c_str());
  2646.             return 1;
  2647.         case PlayerInfoPremiumDays:
  2648.             value = player->getPremiumDays();
  2649.             break;
  2650.         case PlayerInfoFood:
  2651.         {
  2652.             if(Condition* condition = player->getCondition(CONDITION_REGENERATION, CONDITIONID_DEFAULT))
  2653.                 value = condition->getTicks() / 1000;
  2654.  
  2655.             break;
  2656.         }
  2657.         case PlayerInfoVocation:
  2658.             value = player->getVocationId();
  2659.             break;
  2660.         case PlayerInfoSoul:
  2661.             value = player->getSoul();
  2662.             break;
  2663.         case PlayerInfoFreeCap:
  2664.             value = (int64_t)player->getFreeCapacity();
  2665.             break;
  2666.         case PlayerInfoGuildId:
  2667.             value = player->getGuildId();
  2668.             break;
  2669.         case PlayerInfoGuildName:
  2670.             lua_pushstring(L, player->getGuildName().c_str());
  2671.             return 1;
  2672.         case PlayerInfoGuildRankId:
  2673.             value = player->getRankId();
  2674.             break;
  2675.         case PlayerInfoGuildRank:
  2676.             lua_pushstring(L, player->getRankName().c_str());
  2677.             return 1;
  2678.         case PlayerInfoGuildLevel:
  2679.             value = player->getGuildLevel();
  2680.             break;
  2681.         case PlayerInfoGuildNick:
  2682.             lua_pushstring(L, player->getGuildNick().c_str());
  2683.             return 1;
  2684.         case PlayerInfoGroupId:
  2685.             value = player->getGroupId();
  2686.             break;
  2687.         case PlayerInfoBalance:
  2688.             if(g_config.getBool(ConfigManager::BANK_SYSTEM))
  2689.                 lua_pushnumber(L, player->balance);
  2690.             else
  2691.                 lua_pushnumber(L, 0);
  2692.  
  2693.             return 1;
  2694.         case PlayerInfoStamina:
  2695.             value = player->getStaminaMinutes();
  2696.             break;
  2697.         case PlayerInfoLossSkill:
  2698.             lua_pushboolean(L, player->getLossSkill());
  2699.             return 1;
  2700.         case PlayerInfoMarriage:
  2701.             value = player->marriage;
  2702.             break;
  2703.         case PlayerInfoPzLock:
  2704.             lua_pushboolean(L, player->isPzLocked());
  2705.             return 1;
  2706.         case PlayerInfoSaving:
  2707.             lua_pushboolean(L, player->isSaving());
  2708.             return 1;
  2709.         case PlayerInfoIp:
  2710.             value = player->getIP();
  2711.             break;
  2712.         case PlayerInfoSkullEnd:
  2713.             value = player->getSkullEnd();
  2714.             break;
  2715.         case PlayerInfoOutfitWindow:
  2716.             player->sendOutfitWindow();
  2717.             lua_pushboolean(L, true);
  2718.             return 1;
  2719.         case PlayerInfoIdleTime:
  2720.             value = player->getIdleTime();
  2721.             break;
  2722.         case PlayerInfoClient:
  2723.             lua_pushboolean(L, player->hasClient());
  2724.             return 1;
  2725.         case PlayerInfoLastLoad:
  2726.             value = player->getLastLoad();
  2727.             break;
  2728.         case PlayerInfoLastLogin:
  2729.             value = player->getLastLogin();
  2730.             break;
  2731.         case PlayerInfoAccountManager:
  2732.             value = player->accountManager;
  2733.             break;
  2734.         case PlayerInfoTradeState:
  2735.             value = player->tradeState;
  2736.             break;
  2737.         default:
  2738.             errorEx("Unknown player info #" + info);
  2739.             value = 0;
  2740.             break;
  2741.     }
  2742.  
  2743.     lua_pushnumber(L, value);
  2744.     return 1;
  2745. }
  2746.  
  2747. //getPlayer[Info](uid)
  2748. int32_t LuaInterface::luaGetPlayerNameDescription(lua_State* L)
  2749. {
  2750.     return internalGetPlayerInfo(L, PlayerInfoNameDescription);
  2751. }
  2752.  
  2753. int32_t LuaInterface::luaGetPlayerSpecialDescription(lua_State* L)
  2754. {
  2755.     return internalGetPlayerInfo(L, PlayerInfoSpecialDescription);
  2756. }
  2757.  
  2758. int32_t LuaInterface::luaGetPlayerFood(lua_State* L)
  2759. {
  2760.     return internalGetPlayerInfo(L, PlayerInfoFood);
  2761. }
  2762.  
  2763. int32_t LuaInterface::luaGetPlayerAccess(lua_State* L)
  2764. {
  2765.     return internalGetPlayerInfo(L, PlayerInfoAccess);
  2766. }
  2767.  
  2768. int32_t LuaInterface::luaGetPlayerGhostAccess(lua_State* L)
  2769. {
  2770.     return internalGetPlayerInfo(L, PlayerInfoGhostAccess);
  2771. }
  2772.  
  2773. int32_t LuaInterface::luaGetPlayerLevel(lua_State* L)
  2774. {
  2775.     return internalGetPlayerInfo(L, PlayerInfoLevel);
  2776. }
  2777.  
  2778. int32_t LuaInterface::luaGetPlayerExperience(lua_State* L)
  2779. {
  2780.     return internalGetPlayerInfo(L, PlayerInfoExperience);
  2781. }
  2782.  
  2783. int32_t LuaInterface::luaGetPlayerSpentMana(lua_State* L)
  2784. {
  2785.     return internalGetPlayerInfo(L, PlayerInfoManaSpent);
  2786. }
  2787.  
  2788. int32_t LuaInterface::luaGetPlayerVocation(lua_State* L)
  2789. {
  2790.     return internalGetPlayerInfo(L, PlayerInfoVocation);
  2791. }
  2792.  
  2793. int32_t LuaInterface::luaGetPlayerSoul(lua_State* L)
  2794. {
  2795.     return internalGetPlayerInfo(L, PlayerInfoSoul);
  2796. }
  2797.  
  2798. int32_t LuaInterface::luaGetPlayerFreeCap(lua_State* L)
  2799. {
  2800.     return internalGetPlayerInfo(L, PlayerInfoFreeCap);
  2801. }
  2802.  
  2803. int32_t LuaInterface::luaGetPlayerGuildId(lua_State* L)
  2804. {
  2805.     return internalGetPlayerInfo(L, PlayerInfoGuildId);
  2806. }
  2807.  
  2808. int32_t LuaInterface::luaGetPlayerGuildName(lua_State* L)
  2809. {
  2810.     return internalGetPlayerInfo(L, PlayerInfoGuildName);
  2811. }
  2812.  
  2813. int32_t LuaInterface::luaGetPlayerGuildRankId(lua_State* L)
  2814. {
  2815.     return internalGetPlayerInfo(L, PlayerInfoGuildRankId);
  2816. }
  2817.  
  2818. int32_t LuaInterface::luaGetPlayerGuildRank(lua_State* L)
  2819. {
  2820.     return internalGetPlayerInfo(L, PlayerInfoGuildRank);
  2821. }
  2822.  
  2823. int32_t LuaInterface::luaGetPlayerGuildLevel(lua_State* L)
  2824. {
  2825.     return internalGetPlayerInfo(L, PlayerInfoGuildLevel);
  2826. }
  2827.  
  2828. int32_t LuaInterface::luaGetPlayerGuildNick(lua_State* L)
  2829. {
  2830.     return internalGetPlayerInfo(L, PlayerInfoGuildNick);
  2831. }
  2832.  
  2833. int32_t LuaInterface::luaGetPlayerTown(lua_State* L)
  2834. {
  2835.     return internalGetPlayerInfo(L, PlayerInfoTown);
  2836. }
  2837.  
  2838. int32_t LuaInterface::luaGetPlayerPromotionLevel(lua_State* L)
  2839. {
  2840.     return internalGetPlayerInfo(L, PlayerInfoPromotionLevel);
  2841. }
  2842.  
  2843. int32_t LuaInterface::luaGetPlayerGroupId(lua_State* L)
  2844. {
  2845.     return internalGetPlayerInfo(L, PlayerInfoGroupId);
  2846. }
  2847.  
  2848. int32_t LuaInterface::luaGetPlayerGUID(lua_State* L)
  2849. {
  2850.     return internalGetPlayerInfo(L, PlayerInfoGUID);
  2851. }
  2852.  
  2853. int32_t LuaInterface::luaGetPlayerAccountId(lua_State* L)
  2854. {
  2855.     return internalGetPlayerInfo(L, PlayerInfoAccountId);
  2856. }
  2857.  
  2858. int32_t LuaInterface::luaGetPlayerAccount(lua_State* L)
  2859. {
  2860.     return internalGetPlayerInfo(L, PlayerInfoAccount);
  2861. }
  2862.  
  2863. int32_t LuaInterface::luaGetPlayerPremiumDays(lua_State* L)
  2864. {
  2865.     return internalGetPlayerInfo(L, PlayerInfoPremiumDays);
  2866. }
  2867.  
  2868. int32_t LuaInterface::luaGetPlayerBalance(lua_State* L)
  2869. {
  2870.     return internalGetPlayerInfo(L, PlayerInfoBalance);
  2871. }
  2872.  
  2873. int32_t LuaInterface::luaGetPlayerStamina(lua_State* L)
  2874. {
  2875.     return internalGetPlayerInfo(L, PlayerInfoStamina);
  2876. }
  2877.  
  2878. int32_t LuaInterface::luaGetPlayerLossSkill(lua_State* L)
  2879. {
  2880.     return internalGetPlayerInfo(L, PlayerInfoLossSkill);
  2881. }
  2882.  
  2883. int32_t LuaInterface::luaGetPlayerPartner(lua_State* L)
  2884. {
  2885.     return internalGetPlayerInfo(L, PlayerInfoMarriage);
  2886. }
  2887.  
  2888. int32_t LuaInterface::luaIsPlayerPzLocked(lua_State* L)
  2889. {
  2890.     return internalGetPlayerInfo(L, PlayerInfoPzLock);
  2891. }
  2892.  
  2893. int32_t LuaInterface::luaIsPlayerSaving(lua_State* L)
  2894. {
  2895.     return internalGetPlayerInfo(L, PlayerInfoSaving);
  2896. }
  2897.  
  2898. int32_t LuaInterface::luaGetPlayerIp(lua_State* L)
  2899. {
  2900.     return internalGetPlayerInfo(L, PlayerInfoIp);
  2901. }
  2902.  
  2903. int32_t LuaInterface::luaGetPlayerSkullEnd(lua_State* L)
  2904. {
  2905.     return internalGetPlayerInfo(L, PlayerInfoSkullEnd);
  2906. }
  2907.  
  2908. int32_t LuaInterface::luaDoPlayerSendOutfitWindow(lua_State* L)
  2909. {
  2910.     return internalGetPlayerInfo(L, PlayerInfoOutfitWindow);
  2911. }
  2912.  
  2913. int32_t LuaInterface::luaGetPlayerIdleTime(lua_State* L)
  2914. {
  2915.     return internalGetPlayerInfo(L, PlayerInfoIdleTime);
  2916. }
  2917.  
  2918. int32_t LuaInterface::luaHasPlayerClient(lua_State* L)
  2919. {
  2920.     return internalGetPlayerInfo(L, PlayerInfoClient);
  2921. }
  2922.  
  2923. int32_t LuaInterface::luaGetPlayerLastLoad(lua_State* L)
  2924. {
  2925.     return internalGetPlayerInfo(L, PlayerInfoLastLoad);
  2926. }
  2927.  
  2928. int32_t LuaInterface::luaGetPlayerLastLogin(lua_State* L)
  2929. {
  2930.     return internalGetPlayerInfo(L, PlayerInfoLastLogin);
  2931. }
  2932.  
  2933. int32_t LuaInterface::luaGetPlayerAccountManager(lua_State* L)
  2934. {
  2935.     return internalGetPlayerInfo(L, PlayerInfoAccountManager);
  2936. }
  2937.  
  2938. int32_t LuaInterface::luaGetPlayerTradeState(lua_State* L)
  2939. {
  2940.     return internalGetPlayerInfo(L, PlayerInfoTradeState);
  2941. }
  2942. //
  2943.  
  2944. int32_t LuaInterface::luaGetPlayerSex(lua_State* L)
  2945. {
  2946.     //getPlayerSex(cid[, full = false])
  2947.     bool full = false;
  2948.     if(lua_gettop(L) > 1)
  2949.         full = popNumber(L);
  2950.  
  2951.     ScriptEnviroment* env = getEnv();
  2952.     Player* player = env->getPlayerByUID((uint32_t)popNumber(L));
  2953.     if(!player)
  2954.     {
  2955.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  2956.         lua_pushboolean(L, false);
  2957.     }
  2958.     else
  2959.         lua_pushnumber(L, player->getSex(full));
  2960.  
  2961.     return 1;
  2962. }
  2963.  
  2964. int32_t LuaInterface::luaDoPlayerSetNameDescription(lua_State* L)
  2965. {
  2966.     //doPlayerSetNameDescription(cid, description)
  2967.     std::string description = popString(L);
  2968.  
  2969.     ScriptEnviroment* env = getEnv();
  2970.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  2971.     {
  2972.         player->nameDescription += description;
  2973.         lua_pushboolean(L, true);
  2974.     }
  2975.     else
  2976.     {
  2977.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  2978.         lua_pushboolean(L, false);
  2979.     }
  2980.  
  2981.     return 1;
  2982. }
  2983.  
  2984. int32_t LuaInterface::luaDoPlayerSetSpecialDescription(lua_State* L)
  2985. {
  2986.     //doPlayerSetSpecialDescription(cid, description)
  2987.     std::string description = popString(L);
  2988.  
  2989.     ScriptEnviroment* env = getEnv();
  2990.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  2991.     {
  2992.         player->setSpecialDescription(description);
  2993.         lua_pushboolean(L, true);
  2994.     }
  2995.     else
  2996.     {
  2997.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  2998.         lua_pushboolean(L, false);
  2999.     }
  3000.  
  3001.     return 1;
  3002. }
  3003.  
  3004. int32_t LuaInterface::luaGetPlayerMagLevel(lua_State* L)
  3005. {
  3006.     //getPlayerMagLevel(cid[, ignoreBuffs = false])
  3007.     bool ignoreBuffs = false;
  3008.     if(lua_gettop(L) > 1)
  3009.         ignoreBuffs = popNumber(L);
  3010.  
  3011.     ScriptEnviroment* env = getEnv();
  3012.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  3013.         lua_pushnumber(L, (ignoreBuffs ? player->magLevel : player->getMagicLevel()));
  3014.     else
  3015.     {
  3016.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3017.         lua_pushboolean(L, false);
  3018.     }
  3019.  
  3020.     return 1;
  3021. }
  3022.  
  3023. int32_t LuaInterface::luaGetPlayerRequiredMana(lua_State* L)
  3024. {
  3025.     //getPlayerRequiredMana(cid, magicLevel)
  3026.     uint32_t magLevel = popNumber(L);
  3027.  
  3028.     ScriptEnviroment* env = getEnv();
  3029.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3030.         lua_pushnumber(L, player->vocation->getReqMana(magLevel));
  3031.     else
  3032.     {
  3033.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3034.         lua_pushboolean(L, false);
  3035.     }
  3036.  
  3037.     return 1;
  3038. }
  3039.  
  3040. int32_t LuaInterface::luaGetPlayerRequiredSkillTries(lua_State* L)
  3041. {
  3042.     //getPlayerRequiredSkillTries(cid, skillId, skillLevel)
  3043.     int32_t sLevel = popNumber(L), sId = popNumber(L);
  3044.  
  3045.     ScriptEnviroment* env = getEnv();
  3046.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3047.         lua_pushnumber(L, player->vocation->getReqSkillTries(sId, sLevel));
  3048.     else
  3049.     {
  3050.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3051.         lua_pushboolean(L, false);
  3052.     }
  3053.  
  3054.     return 1;
  3055. }
  3056.  
  3057. int32_t LuaInterface::luaGetPlayerFlagValue(lua_State* L)
  3058. {
  3059.     //getPlayerFlagValue(cid, flag)
  3060.     uint32_t index = popNumber(L);
  3061.  
  3062.     ScriptEnviroment* env = getEnv();
  3063.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3064.     {
  3065.         if(index < PlayerFlag_LastFlag)
  3066.             lua_pushboolean(L, player->hasFlag((PlayerFlags)index));
  3067.         else
  3068.         {
  3069.             std::stringstream ss;
  3070.             ss << index;
  3071.             errorEx("No valid flag index - " + ss.str());
  3072.             lua_pushboolean(L, false);
  3073.         }
  3074.     }
  3075.     else
  3076.     {
  3077.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3078.         lua_pushboolean(L, false);
  3079.     }
  3080.  
  3081.     return 1;
  3082. }
  3083.  
  3084. int32_t LuaInterface::luaGetPlayerCustomFlagValue(lua_State* L)
  3085. {
  3086.     //getPlayerCustomFlagValue(cid, flag)
  3087.     uint32_t index = popNumber(L);
  3088.  
  3089.     ScriptEnviroment* env = getEnv();
  3090.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3091.     {
  3092.         if(index < PlayerCustomFlag_LastFlag)
  3093.             lua_pushboolean(L, player->hasCustomFlag((PlayerCustomFlags)index));
  3094.         else
  3095.         {
  3096.             std::stringstream ss;
  3097.             ss << index;
  3098.             errorEx("No valid flag index - " + ss.str());
  3099.             lua_pushboolean(L, false);
  3100.         }
  3101.     }
  3102.     else
  3103.     {
  3104.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3105.         lua_pushboolean(L, false);
  3106.     }
  3107.  
  3108.     return 1;
  3109. }
  3110.  
  3111. int32_t LuaInterface::luaDoPlayerLearnInstantSpell(lua_State* L)
  3112. {
  3113.     //doPlayerLearnInstantSpell(cid, name)
  3114.     std::string spellName = popString(L);
  3115.  
  3116.     ScriptEnviroment* env = getEnv();
  3117.     Player* player = env->getPlayerByUID(popNumber(L));
  3118.     if(!player)
  3119.     {
  3120.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3121.         lua_pushboolean(L, false);
  3122.         return 1;
  3123.     }
  3124.  
  3125.     InstantSpell* spell = g_spells->getInstantSpellByName(spellName);
  3126.     if(!spell)
  3127.     {
  3128.         lua_pushboolean(L, false);
  3129.         return 1;
  3130.     }
  3131.  
  3132.     player->learnInstantSpell(spell->getName());
  3133.     lua_pushboolean(L, true);
  3134.     return 1;
  3135. }
  3136.  
  3137. int32_t LuaInterface::luaDoPlayerUnlearnInstantSpell(lua_State* L)
  3138. {
  3139.     //doPlayerUnlearnInstantSpell(cid, name)
  3140.     std::string spellName = popString(L);
  3141.  
  3142.     ScriptEnviroment* env = getEnv();
  3143.     Player* player = env->getPlayerByUID(popNumber(L));
  3144.     if(!player)
  3145.     {
  3146.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3147.         lua_pushboolean(L, false);
  3148.         return 1;
  3149.     }
  3150.  
  3151.     InstantSpell* spell = g_spells->getInstantSpellByName(spellName);
  3152.     if(!spell)
  3153.     {
  3154.         lua_pushboolean(L, false);
  3155.         return 1;
  3156.     }
  3157.  
  3158.     player->unlearnInstantSpell(spell->getName());
  3159.     lua_pushboolean(L, true);
  3160.     return 1;
  3161. }
  3162.  
  3163. int32_t LuaInterface::luaGetPlayerLearnedInstantSpell(lua_State* L)
  3164. {
  3165.     //getPlayerLearnedInstantSpell(cid, name)
  3166.     std::string spellName = popString(L);
  3167.  
  3168.     ScriptEnviroment* env = getEnv();
  3169.     Player* player = env->getPlayerByUID(popNumber(L));
  3170.     if(!player)
  3171.     {
  3172.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3173.         lua_pushboolean(L, false);
  3174.         return 1;
  3175.     }
  3176.  
  3177.     InstantSpell* spell = g_spells->getInstantSpellByName(spellName);
  3178.     if(!spell)
  3179.     {
  3180.         lua_pushboolean(L, false);
  3181.         return 1;
  3182.     }
  3183.  
  3184.     lua_pushboolean(L, player->hasLearnedInstantSpell(spellName));
  3185.     return 1;
  3186. }
  3187.  
  3188. int32_t LuaInterface::luaGetPlayerInstantSpellCount(lua_State* L)
  3189. {
  3190.     //getPlayerInstantSpellCount(cid)
  3191.     ScriptEnviroment* env = getEnv();
  3192.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3193.         lua_pushnumber(L, g_spells->getInstantSpellCount(player));
  3194.     else
  3195.     {
  3196.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3197.         lua_pushboolean(L, false);
  3198.     }
  3199.  
  3200.     return 1;
  3201. }
  3202.  
  3203. int32_t LuaInterface::luaGetPlayerInstantSpellInfo(lua_State* L)
  3204. {
  3205.     //getPlayerInstantSpellInfo(cid, index)
  3206.     uint32_t index = popNumber(L);
  3207.  
  3208.     ScriptEnviroment* env = getEnv();
  3209.     Player* player = env->getPlayerByUID(popNumber(L));
  3210.     if(!player)
  3211.     {
  3212.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3213.         lua_pushboolean(L, false);
  3214.         return 1;
  3215.     }
  3216.  
  3217.     InstantSpell* spell = g_spells->getInstantSpellByIndex(player, index);
  3218.     if(!spell)
  3219.     {
  3220.         errorEx(getError(LUA_ERROR_SPELL_NOT_FOUND));
  3221.         lua_pushboolean(L, false);
  3222.         return 1;
  3223.     }
  3224.  
  3225.     lua_newtable(L);
  3226.     setField(L, "name", spell->getName());
  3227.     setField(L, "words", spell->getWords());
  3228.     setField(L, "level", spell->getLevel());
  3229.     setField(L, "mlevel", spell->getMagicLevel());
  3230.     setField(L, "mana", spell->getManaCost(player));
  3231.     setField(L, "manapercent", spell->getManaPercent());
  3232.     return 1;
  3233. }
  3234.  
  3235. int32_t LuaInterface::luaGetInstantSpellInfo(lua_State* L)
  3236. {
  3237.     //getInstantSpellInfo(name)
  3238.     InstantSpell* spell = g_spells->getInstantSpellByName(popString(L));
  3239.     if(!spell)
  3240.     {
  3241.         errorEx(getError(LUA_ERROR_SPELL_NOT_FOUND));
  3242.         lua_pushboolean(L, false);
  3243.         return 1;
  3244.     }
  3245.  
  3246.     lua_newtable(L);
  3247.     setField(L, "name", spell->getName());
  3248.     setField(L, "words", spell->getWords());
  3249.     setField(L, "level", spell->getLevel());
  3250.     setField(L, "mlevel", spell->getMagicLevel());
  3251.     setField(L, "mana", spell->getManaCost(NULL));
  3252.     setField(L, "manapercent", spell->getManaPercent());
  3253.     return 1;
  3254. }
  3255.  
  3256. int32_t LuaInterface::luaDoRemoveItem(lua_State* L)
  3257. {
  3258.     //doRemoveItem(uid[, count = -1])
  3259.     int32_t count = -1;
  3260.     if(lua_gettop(L) > 1)
  3261.         count = popNumber(L);
  3262.  
  3263.     ScriptEnviroment* env = getEnv();
  3264.     Item* item = env->getItemByUID(popNumber(L));
  3265.     if(!item)
  3266.     {
  3267.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  3268.         lua_pushboolean(L, false);
  3269.         return 1;
  3270.     }
  3271.  
  3272.     if(g_game.internalRemoveItem(NULL, item, count) != RET_NOERROR)
  3273.     {
  3274.         lua_pushboolean(L, false);
  3275.         return 1;
  3276.     }
  3277.  
  3278.     lua_pushboolean(L, true);
  3279.     return 1;
  3280. }
  3281.  
  3282. int32_t LuaInterface::luaDoPlayerRemoveItem(lua_State* L)
  3283. {
  3284.     //doPlayerRemoveItem(cid, itemid, count[, subType = -1])
  3285.     int32_t subType = -1;
  3286.     if(lua_gettop(L) > 3)
  3287.         subType = popNumber(L);
  3288.  
  3289.     uint32_t count = popNumber(L);
  3290.     uint16_t itemId = (uint16_t)popNumber(L);
  3291.  
  3292.     ScriptEnviroment* env = getEnv();
  3293.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3294.         lua_pushboolean(L, g_game.removeItemOfType(player, itemId, count, subType));
  3295.     else
  3296.     {
  3297.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3298.         lua_pushboolean(L, false);
  3299.     }
  3300.  
  3301.     return 1;
  3302. }
  3303.  
  3304. int32_t LuaInterface::luaDoPlayerFeed(lua_State* L)
  3305. {
  3306.     //doPlayerFeed(cid, food)
  3307.     int32_t food = (int32_t)popNumber(L);
  3308.  
  3309.     ScriptEnviroment* env = getEnv();
  3310.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3311.     {
  3312.         player->addDefaultRegeneration((food * 1000) * 3);
  3313.         lua_pushboolean(L, true);
  3314.     }
  3315.     else
  3316.     {
  3317.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3318.         lua_pushboolean(L, false);
  3319.     }
  3320.  
  3321.     return 1;
  3322. }
  3323.  
  3324.  
  3325. int32_t LuaInterface::luaGetPlayerCastBans(lua_State* L)
  3326. {
  3327.     //getPlayerCastBan(cid)
  3328.     ScriptEnviroment* env = getEnv();
  3329.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3330.     {
  3331.         PlayerCast pc = player->getCast();
  3332.         lua_newtable(L);
  3333.         for(std::list<CastBan>::iterator it = pc.bans.begin(); it != pc.bans.end(); ++it)
  3334.         {
  3335.             createTable(L, it->ip);
  3336.             setField(L, "name", it->name);
  3337.             pushTable(L);
  3338.         }
  3339.     }
  3340.     else
  3341.     {
  3342.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3343.         lua_pushboolean(L, false);
  3344.     }
  3345.  
  3346.     return 1;
  3347. }
  3348.  
  3349.  
  3350. int32_t LuaInterface::luaGetPlayerCastMutes(lua_State* L)
  3351. {
  3352.     //getPlayerCastMutes(cid)
  3353.     ScriptEnviroment* env = getEnv();
  3354.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3355.     {
  3356.         PlayerCast pc = player->getCast();
  3357.         lua_newtable(L);
  3358.         for(std::list<CastBan>::iterator it = pc.muted.begin(); it != pc.muted.end(); ++it)
  3359.         {
  3360.             createTable(L, it->ip);
  3361.             setField(L, "name", it->name);
  3362.             pushTable(L);
  3363.         }
  3364.     }
  3365.     else
  3366.     {
  3367.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3368.         lua_pushboolean(L, false);
  3369.     }
  3370.  
  3371.     return 1;
  3372. }
  3373.  
  3374. int32_t LuaInterface::luaDoPlayerRemoveCastMute(lua_State* L)
  3375. {
  3376.     //doPlayerRemoveCastMute(cid, ip)
  3377.     std::string name = popString(L);
  3378.     ScriptEnviroment* env = getEnv();
  3379.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3380.     {
  3381.         if(player->removeCastMute(name))
  3382.             lua_pushboolean(L, true);
  3383.         else
  3384.             lua_pushboolean(L, false);
  3385.     }
  3386.     else
  3387.     {
  3388.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3389.         lua_pushboolean(L, false);
  3390.     }
  3391.  
  3392.     return 1;
  3393. }
  3394.  
  3395. int32_t LuaInterface::luaDoPlayerAddCastMute(lua_State* L)
  3396. {
  3397.     //doPlayerAddCastMute(cid, ip)
  3398.     std::string name = popString(L);
  3399.     ScriptEnviroment* env = getEnv();
  3400.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3401.     {
  3402.         if(player->addCastMute(name))
  3403.             lua_pushboolean(L, true);
  3404.         else
  3405.             lua_pushboolean(L, false);
  3406.     }
  3407.     else
  3408.     {
  3409.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3410.         lua_pushboolean(L, false);
  3411.     }
  3412.  
  3413.     return 1;
  3414. }
  3415.  
  3416. int32_t LuaInterface::luaGetPlayerCastViewers(lua_State* L)
  3417. {
  3418.     //getPlayerCastBan(cid)
  3419.     ScriptEnviroment* env = getEnv();
  3420.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3421.     {
  3422.         PlayerCast pc = player->getCast();
  3423.         lua_newtable(L);
  3424.         for(AutoList<ProtocolGame>::iterator it = Player::cSpectators.begin(); it != Player::cSpectators.end(); ++it)
  3425.         {
  3426.             if(it->second->getPlayer() != player)
  3427.                 continue;
  3428.  
  3429.             createTable(L, it->first);
  3430.             setField(L, "name", it->second->getViewerName());
  3431.             setField(L, "ip", it->second->getIP());
  3432.             pushTable(L);
  3433.         }
  3434.     }
  3435.     else
  3436.     {
  3437.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3438.         lua_pushboolean(L, false);
  3439.     }
  3440.  
  3441.     return 1;
  3442. }
  3443.  
  3444.  
  3445. int32_t LuaInterface::luaDoPlayerRemoveCastBan(lua_State* L)
  3446. {
  3447.     //doPlayerRemoveCastBan(cid, ip)
  3448.     std::string name = popString(L);
  3449.     ScriptEnviroment* env = getEnv();
  3450.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3451.     {
  3452.         if(player->removeCastBan(name))
  3453.             lua_pushboolean(L, true);
  3454.         else
  3455.             lua_pushboolean(L, false);
  3456.     }
  3457.     else
  3458.     {
  3459.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3460.         lua_pushboolean(L, false);
  3461.     }
  3462.  
  3463.     return 1;
  3464. }
  3465.  
  3466. int32_t LuaInterface::luaDoPlayerAddCastBan(lua_State* L)
  3467. {
  3468.     //doPlayerAddCastBan(cid, ip)
  3469.     std::string name = popString(L);
  3470.     ScriptEnviroment* env = getEnv();
  3471.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3472.     {
  3473.         if(player->addCastBan(name))
  3474.             lua_pushboolean(L, true);
  3475.         else
  3476.             lua_pushboolean(L, false);
  3477.     }
  3478.     else
  3479.     {
  3480.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3481.         lua_pushboolean(L, false);
  3482.     }
  3483.  
  3484.     return 1;
  3485. }
  3486.  
  3487.  
  3488. int32_t LuaInterface::luaDoPlayerSetCastPassword(lua_State* L)
  3489. {
  3490.     //doPlayerSetCastPassword(cid, password)
  3491.     std::string str = popString(L);
  3492.     ScriptEnviroment* env = getEnv();
  3493.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3494.     {
  3495.         player->setCastPassword(str);
  3496.         lua_pushboolean(L, true);
  3497.     }
  3498.     else
  3499.     {
  3500.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3501.         lua_pushboolean(L, false);
  3502.     }
  3503.  
  3504.     return 1;
  3505. }
  3506.  
  3507. int32_t LuaInterface::luaDoPlayerSetCastDescription(lua_State* L)
  3508. {
  3509.     //doPlayerSetCastPassword(cid, password)
  3510.     std::string str = popString(L);
  3511.     ScriptEnviroment* env = getEnv();
  3512.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3513.     {
  3514.         player->setCastDescription(str);
  3515.         lua_pushboolean(L, true);
  3516.     }
  3517.     else
  3518.     {
  3519.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3520.         lua_pushboolean(L, false);
  3521.     }
  3522.  
  3523.     return 1;
  3524. }
  3525.  
  3526. int32_t LuaInterface::luaDoPlayerSetCastState(lua_State* L)
  3527. {
  3528.     //doPlayerSetCastState(cid, bool)
  3529.     bool state = popNumber(L);
  3530.     ScriptEnviroment* env = getEnv();
  3531.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3532.     {
  3533.         player->setCasting(state);
  3534.         lua_pushboolean(L, true);
  3535.     }
  3536.     else
  3537.     {
  3538.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3539.         lua_pushboolean(L, false);
  3540.     }
  3541.  
  3542.     return 1;
  3543. }
  3544.  
  3545. int32_t LuaInterface::luaGetPlayerCast(lua_State* L)
  3546. {
  3547.     //getPlayerCast(cid)
  3548.     ScriptEnviroment* env = getEnv();
  3549.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  3550.     {
  3551.         lua_newtable(L);
  3552.         setFieldBool(L, "status", player->getCastingState());
  3553.         setField(L, "password", player->getCastingPassword());
  3554.         setField(L, "description", player->getCastDescription());
  3555.     }
  3556.     else
  3557.     {
  3558.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3559.         lua_pushboolean(L, false);
  3560.     }
  3561.  
  3562.     return 1;
  3563. }
  3564.  
  3565. int32_t LuaInterface::luaGetCastsOnline(lua_State* L)
  3566. {
  3567.     //
  3568.     ScriptEnviroment* env = getEnv();
  3569.     AutoList<Player>::iterator it = Player::castAutoList.begin();
  3570.  
  3571.     lua_newtable(L);
  3572.     for(int32_t i = 1; it != Player::castAutoList.end(); ++it, ++i)
  3573.     {
  3574.         lua_pushnumber(L, i);
  3575.         lua_pushnumber(L, env->addThing(it->second));
  3576.         pushTable(L);
  3577.     }
  3578.  
  3579.     return 1;
  3580. }
  3581.  
  3582.  
  3583. int32_t LuaInterface::luaDoPlayerSendCancel(lua_State* L)
  3584. {
  3585.     //doPlayerSendCancel(cid, text)
  3586.     std::string text = popString(L);
  3587.     ScriptEnviroment* env = getEnv();
  3588.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  3589.     {
  3590.         player->sendCancel(text);
  3591.         lua_pushboolean(L, true);
  3592.     }
  3593.     else
  3594.     {
  3595.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3596.         lua_pushboolean(L, false);
  3597.     }
  3598.  
  3599.     return 1;
  3600. }
  3601.  
  3602. int32_t LuaInterface::luaDoSendDefaultCancel(lua_State* L)
  3603. {
  3604.     //doPlayerSendDefaultCancel(cid, ReturnValue)
  3605.     ReturnValue ret = (ReturnValue)popNumber(L);
  3606.     ScriptEnviroment* env = getEnv();
  3607.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  3608.     {
  3609.         player->sendCancelMessage(ret);
  3610.         lua_pushboolean(L, true);
  3611.     }
  3612.     else
  3613.     {
  3614.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3615.         lua_pushboolean(L, false);
  3616.     }
  3617.  
  3618.     return 1;
  3619. }
  3620.  
  3621. int32_t LuaInterface::luaGetSearchString(lua_State* L)
  3622. {
  3623.     //getSearchString(fromPosition, toPosition[, fromIsCreature = false[, toIsCreature = false]])
  3624.     PositionEx toPos, fromPos;
  3625.     bool toIsCreature = false, fromIsCreature = false;
  3626.  
  3627.     int32_t params = lua_gettop(L);
  3628.     if(params > 3)
  3629.         toIsCreature = popNumber(L);
  3630.  
  3631.     if(params > 2)
  3632.         fromIsCreature = popNumber(L);
  3633.  
  3634.     popPosition(L, toPos);
  3635.     popPosition(L, fromPos);
  3636.     if(!toPos.x || !toPos.y || !fromPos.x || !fromPos.y)
  3637.     {
  3638.         errorEx("wrong position(s) specified.");
  3639.         lua_pushboolean(L, false);
  3640.     }
  3641.     else
  3642.         lua_pushstring(L, g_game.getSearchString(fromPos, toPos, fromIsCreature, toIsCreature).c_str());
  3643.  
  3644.     return 1;
  3645. }
  3646.  
  3647. int32_t LuaInterface::luaGetClosestFreeTile(lua_State* L)
  3648. {
  3649.     //getClosestFreeTile(cid, targetPos[, extended = false[, ignoreHouse = true]])
  3650.     uint32_t params = lua_gettop(L);
  3651.     bool ignoreHouse = true, extended = false;
  3652.     if(params > 3)
  3653.         ignoreHouse = popNumber(L);
  3654.  
  3655.     if(params > 2)
  3656.         extended = popNumber(L);
  3657.  
  3658.     PositionEx pos;
  3659.     popPosition(L, pos);
  3660.  
  3661.     ScriptEnviroment* env = getEnv();
  3662.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3663.     {
  3664.         Position newPos = g_game.getClosestFreeTile(creature, pos, extended, ignoreHouse);
  3665.         if(newPos.x != 0)
  3666.             pushPosition(L, newPos, 0);
  3667.         else
  3668.             lua_pushboolean(L, false);
  3669.     }
  3670.     else
  3671.     {
  3672.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3673.         lua_pushboolean(L, false);
  3674.     }
  3675.  
  3676.     return 1;
  3677. }
  3678.  
  3679. int32_t LuaInterface::luaDoTeleportThing(lua_State* L)
  3680. {
  3681.     //doTeleportThing(cid, newpos[, pushMove = true[, fullTeleport = true]])
  3682.     bool fullTeleport = true, pushMove = true;
  3683.     int32_t params = lua_gettop(L);
  3684.     if(params > 3)
  3685.         fullTeleport = popNumber(L);
  3686.  
  3687.     if(params > 2)
  3688.         pushMove = popNumber(L);
  3689.  
  3690.     PositionEx pos;
  3691.     popPosition(L, pos);
  3692.  
  3693.     ScriptEnviroment* env = getEnv();
  3694.     if(Thing* tmp = env->getThingByUID(popNumber(L)))
  3695.         lua_pushboolean(L, g_game.internalTeleport(tmp, pos, !pushMove, FLAG_NOLIMIT, fullTeleport) == RET_NOERROR);
  3696.     else
  3697.     {
  3698.         errorEx(getError(LUA_ERROR_THING_NOT_FOUND));
  3699.         lua_pushboolean(L, false);
  3700.     }
  3701.  
  3702.     return 1;
  3703. }
  3704.  
  3705. int32_t LuaInterface::luaDoTransformItem(lua_State* L)
  3706. {
  3707.     //doTransformItem(uid, newId[, count/subType])
  3708.     int32_t count = -1;
  3709.     if(lua_gettop(L) > 2)
  3710.         count = popNumber(L);
  3711.  
  3712.     uint16_t newId = popNumber(L);
  3713.     uint32_t uid = popNumber(L);
  3714.     ScriptEnviroment* env = getEnv();
  3715.  
  3716.     Item* item = env->getItemByUID(uid);
  3717.     if(!item)
  3718.     {
  3719.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  3720.         lua_pushboolean(L, false);
  3721.         return 1;
  3722.     }
  3723.  
  3724.     const ItemType& it = Item::items[newId];
  3725.     if(it.stackable && count > 100)
  3726.         count = 100;
  3727.  
  3728.     Item* newItem = g_game.transformItem(item, newId, count);
  3729.     if(item->isRemoved())
  3730.         env->removeThing(uid);
  3731.  
  3732.     if(newItem && newItem != item)
  3733.         env->insertThing(uid, newItem);
  3734.  
  3735.     lua_pushboolean(L, true);
  3736.     return 1;
  3737. }
  3738.  
  3739. int32_t LuaInterface::luaDoCreatureSay(lua_State* L)
  3740. {
  3741.     //doCreatureSay(uid, text[, type = SPEAK_SAY[, ghost = false[, cid = 0[, pos]]]])
  3742.     uint32_t params = lua_gettop(L), cid = 0, uid = 0;
  3743.     PositionEx pos;
  3744.     if(params > 5)
  3745.         popPosition(L, pos);
  3746.  
  3747.     if(params > 4)
  3748.         cid = popNumber(L);
  3749.  
  3750.     bool ghost = false;
  3751.     if(params > 3)
  3752.         ghost = popNumber(L);
  3753.  
  3754.     SpeakClasses type = SPEAK_SAY;
  3755.     if(params > 2)
  3756.         type = (SpeakClasses)popNumber(L);
  3757.  
  3758.     std::string text = popString(L);
  3759.  
  3760.     uid = popNumber(L);
  3761.     if(params > 5 && (!pos.x || !pos.y))
  3762.     {
  3763.         errorEx("Invalid position specified.");
  3764.         lua_pushboolean(L, false);
  3765.         return 1;
  3766.     }
  3767.  
  3768.     ScriptEnviroment* env = getEnv();
  3769.     Creature* creature = env->getCreatureByUID(uid);
  3770.     if(!creature)
  3771.     {
  3772.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3773.         lua_pushboolean(L, false);
  3774.         return 1;
  3775.     }
  3776.  
  3777.     SpectatorVec list;
  3778.     if(cid)
  3779.     {
  3780.         Creature* target = env->getCreatureByUID(cid);
  3781.         if(!target)
  3782.         {
  3783.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3784.             lua_pushboolean(L, false);
  3785.             return 1;
  3786.         }
  3787.  
  3788.         list.push_back(target);
  3789.     }
  3790.  
  3791.     if(params > 5)
  3792.         lua_pushboolean(L, g_game.internalCreatureSay(creature, type, text, ghost, &list, &pos));
  3793.     else
  3794.         lua_pushboolean(L, g_game.internalCreatureSay(creature, type, text, ghost, &list));
  3795.  
  3796.     return 1;
  3797. }
  3798.  
  3799. int32_t LuaInterface::luaDoSendMagicEffect(lua_State* L)
  3800. {
  3801.     //doSendMagicEffect(pos, type[, player])
  3802.     ScriptEnviroment* env = getEnv();
  3803.     SpectatorVec list;
  3804.     if(lua_gettop(L) > 2)
  3805.     {
  3806.         if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3807.             list.push_back(creature);
  3808.     }
  3809.  
  3810.     uint32_t type = popNumber(L);
  3811.     PositionEx pos;
  3812.  
  3813.     popPosition(L, pos);
  3814.     if(pos.x == 0xFFFF)
  3815.         pos = env->getRealPos();
  3816.  
  3817.     if(!list.empty())
  3818.         g_game.addMagicEffect(list, pos, type);
  3819.     else
  3820.         g_game.addMagicEffect(pos, type);
  3821.  
  3822.     lua_pushboolean(L, true);
  3823.     return 1;
  3824. }
  3825.  
  3826. int32_t LuaInterface::luaDoSendDistanceShoot(lua_State* L)
  3827. {
  3828.     //doSendDistanceShoot(fromPos, toPos, type[, player])
  3829.     ScriptEnviroment* env = getEnv();
  3830.     SpectatorVec list;
  3831.     if(lua_gettop(L) > 3)
  3832.     {
  3833.         if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3834.             list.push_back(creature);
  3835.     }
  3836.  
  3837.     uint32_t type = popNumber(L);
  3838.     PositionEx toPos, fromPos;
  3839.  
  3840.     popPosition(L, toPos);
  3841.     popPosition(L, fromPos);
  3842.     if(fromPos.x == 0xFFFF)
  3843.         fromPos = env->getRealPos();
  3844.  
  3845.     if(toPos.x == 0xFFFF)
  3846.         toPos = env->getRealPos();
  3847.  
  3848.     if(!list.empty())
  3849.         g_game.addDistanceEffect(list, fromPos, toPos, type);
  3850.     else
  3851.         g_game.addDistanceEffect(fromPos, toPos, type);
  3852.  
  3853.     lua_pushboolean(L, true);
  3854.     return 1;
  3855. }
  3856.  
  3857. int32_t LuaInterface::luaDoPlayerAddSkillTry(lua_State* L)
  3858. {
  3859.     //doPlayerAddSkillTry(uid, skillid, n[, useMultiplier = true])
  3860.     bool multiplier = true;
  3861.     if(lua_gettop(L) > 3)
  3862.         multiplier = popNumber(L);
  3863.  
  3864.     uint32_t n = popNumber(L), skillid = popNumber(L);
  3865.     ScriptEnviroment* env = getEnv();
  3866.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  3867.     {
  3868.         player->addSkillAdvance((skills_t)skillid, n, multiplier);
  3869.         lua_pushboolean(L, true);
  3870.     }
  3871.     else
  3872.     {
  3873.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  3874.         lua_pushboolean(L, false);
  3875.     }
  3876.  
  3877.     return 1;
  3878. }
  3879.  
  3880. int32_t LuaInterface::luaGetCreatureSpeakType(lua_State* L)
  3881. {
  3882.     //getCreatureSpeakType(uid)
  3883.     ScriptEnviroment* env = getEnv();
  3884.     if(const Creature* creature = env->getCreatureByUID(popNumber(L)))
  3885.         lua_pushnumber(L, (SpeakClasses)creature->getSpeakType());
  3886.     else
  3887.     {
  3888.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3889.         lua_pushboolean(L, false);
  3890.     }
  3891.  
  3892.     return 1;
  3893. }
  3894.  
  3895. int32_t LuaInterface::luaDoCreatureSetSpeakType(lua_State* L)
  3896. {
  3897.     //doCreatureSetSpeakType(uid, type)
  3898.     SpeakClasses type = (SpeakClasses)popNumber(L);
  3899.  
  3900.     ScriptEnviroment* env = getEnv();
  3901.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3902.     {
  3903.         if(type < SPEAK_CLASS_FIRST || type > SPEAK_CLASS_LAST)
  3904.         {
  3905.             errorEx("Invalid speak type!");
  3906.             lua_pushboolean(L, false);
  3907.             return 1;
  3908.         }
  3909.  
  3910.         creature->setSpeakType(type);
  3911.         lua_pushboolean(L, true);
  3912.     }
  3913.     else
  3914.     {
  3915.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3916.         lua_pushboolean(L, false);
  3917.     }
  3918.  
  3919.     return 1;
  3920. }
  3921.  
  3922. int32_t LuaInterface::luaGetCreatureHideHealth(lua_State* L)
  3923. {
  3924.     //getCreatureHideHealth(cid)
  3925.     ScriptEnviroment* env = getEnv();
  3926.  
  3927.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3928.         lua_pushboolean(L, creature->getHideHealth());
  3929.     else
  3930.     {
  3931.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3932.         lua_pushboolean(L, false);
  3933.     }
  3934.  
  3935.     return 1;
  3936. }
  3937.  
  3938. int32_t LuaInterface::luaDoCreatureSetHideHealth(lua_State* L)
  3939. {
  3940.     //doCreatureSetHideHealth(cid, hide)
  3941.     bool hide = popNumber(L);
  3942.  
  3943.     ScriptEnviroment* env = getEnv();
  3944.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3945.     {
  3946.         creature->setHideHealth(hide);
  3947.         g_game.addCreatureHealth(creature);
  3948.         lua_pushboolean(L, true);
  3949.     }
  3950.     else
  3951.     {
  3952.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3953.         lua_pushboolean(L, false);
  3954.     }
  3955.  
  3956.     return 1;
  3957. }
  3958.  
  3959. int32_t LuaInterface::luaDoCreatureAddHealth(lua_State* L)
  3960. {
  3961.     //doCreatureAddHealth(uid, health[, hitEffect[, hitColor[, force]]])
  3962.     int32_t params = lua_gettop(L);
  3963.     bool force = false;
  3964.     if(params > 4)
  3965.         force = popNumber(L);
  3966.  
  3967.     Color_t hitColor = COLOR_UNKNOWN;
  3968.     if(params > 3)
  3969.         hitColor = (Color_t)popNumber(L);
  3970.  
  3971.     MagicEffect_t hitEffect = MAGIC_EFFECT_UNKNOWN;
  3972.     if(params > 2)
  3973.         hitEffect = (MagicEffect_t)popNumber(L);
  3974.  
  3975.     int32_t healthChange = popNumber(L);
  3976.     ScriptEnviroment* env = getEnv();
  3977.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  3978.     {
  3979.         if(healthChange) //do not post with 0 value
  3980.             g_game.combatChangeHealth(healthChange < 1 ? COMBAT_UNDEFINEDDAMAGE : COMBAT_HEALING,
  3981.                 NULL, creature, healthChange, hitEffect, hitColor, force);
  3982.  
  3983.         lua_pushboolean(L, true);
  3984.     }
  3985.     else
  3986.     {
  3987.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  3988.         lua_pushboolean(L, false);
  3989.     }
  3990.  
  3991.     return 1;
  3992. }
  3993.  
  3994. int32_t LuaInterface::luaDoCreatureAddMana(lua_State* L)
  3995. {
  3996.     //doCreatureAddMana(uid, mana[, aggressive])
  3997.     bool aggressive = true;
  3998.     if(lua_gettop(L) > 2)
  3999.         aggressive = popNumber(L);
  4000.  
  4001.     int32_t manaChange = popNumber(L);
  4002.     ScriptEnviroment* env = getEnv();
  4003.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  4004.     {
  4005.         if(aggressive)
  4006.             g_game.combatChangeMana(NULL, creature, manaChange);
  4007.         else
  4008.             creature->changeMana(manaChange);
  4009.  
  4010.         lua_pushboolean(L, true);
  4011.     }
  4012.     else
  4013.     {
  4014.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  4015.         lua_pushboolean(L, false);
  4016.     }
  4017.  
  4018.     return 1;
  4019. }
  4020.  
  4021. int32_t LuaInterface::luaDoPlayerAddSpentMana(lua_State* L)
  4022. {
  4023.     //doPlayerAddSpentMana(cid, amount[, useMultiplier = true])
  4024.     bool multiplier = true;
  4025.     if(lua_gettop(L) > 2)
  4026.         multiplier = popNumber(L);
  4027.  
  4028.     uint32_t amount = popNumber(L);
  4029.     ScriptEnviroment* env = getEnv();
  4030.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  4031.     {
  4032.         player->addManaSpent(amount, multiplier);
  4033.         lua_pushboolean(L, true);
  4034.     }
  4035.     else
  4036.     {
  4037.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4038.         lua_pushboolean(L, false);
  4039.     }
  4040.  
  4041.     return 1;
  4042. }
  4043.  
  4044. int32_t LuaInterface::luaDoPlayerAddItem(lua_State* L)
  4045. {
  4046.     //doPlayerAddItem(cid, itemid[, count/subtype = 1[, canDropOnMap = true[, slot = 0]]])
  4047.     //doPlayerAddItem(cid, itemid[, count = 1[, canDropOnMap = true[, subtype = 1[, slot = 0]]]])
  4048.     int32_t params = lua_gettop(L), subType = 1, slot = SLOT_WHEREEVER;
  4049.     if(params > 5)
  4050.         slot = popNumber(L);
  4051.  
  4052.     if(params > 4)
  4053.     {
  4054.         if(params > 5)
  4055.             subType = popNumber(L);
  4056.         else
  4057.             slot = popNumber(L);
  4058.     }
  4059.  
  4060.     bool canDropOnMap = true;
  4061.     if(params > 3)
  4062.         canDropOnMap = popNumber(L);
  4063.  
  4064.     uint32_t count = 1;
  4065.     if(params > 2)
  4066.         count = popNumber(L);
  4067.  
  4068.     uint32_t itemId = popNumber(L);
  4069.     if(slot > SLOT_AMMO)
  4070.     {
  4071.         errorEx("Invalid slot.");
  4072.         lua_pushboolean(L, false);
  4073.         return 1;
  4074.     }
  4075.  
  4076.     ScriptEnviroment* env = getEnv();
  4077.     Player* player = env->getPlayerByUID((uint32_t)popNumber(L));
  4078.     if(!player)
  4079.     {
  4080.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4081.         lua_pushboolean(L, false);
  4082.         return 1;
  4083.     }
  4084.  
  4085.     const ItemType& it = Item::items[itemId];
  4086.     int32_t itemCount = 1;
  4087.     if(params > 4)
  4088.         itemCount = std::max((uint32_t)1, count);
  4089.     else if(it.hasSubType())
  4090.     {
  4091.         if(it.stackable)
  4092.             itemCount = (int32_t)std::ceil((float)count / 100);
  4093.  
  4094.         subType = count;
  4095.     }
  4096.  
  4097.     while(itemCount > 0)
  4098.     {
  4099.         int32_t stackCount = std::min(100, subType);
  4100.         Item* newItem = Item::CreateItem(itemId, stackCount);
  4101.         if(!newItem)
  4102.         {
  4103.             errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  4104.             lua_pushboolean(L, false);
  4105.             return 1;
  4106.         }
  4107.  
  4108.         if(it.stackable)
  4109.             subType -= stackCount;
  4110.  
  4111.         ReturnValue ret = g_game.internalPlayerAddItem(NULL, player, newItem, canDropOnMap, (slots_t)slot);
  4112.         if(ret != RET_NOERROR)
  4113.         {
  4114.             delete newItem;
  4115.             lua_pushboolean(L, false);
  4116.             return 1;
  4117.         }
  4118.  
  4119.         --itemCount;
  4120.         if(itemCount)
  4121.             continue;
  4122.  
  4123.         if(newItem->getParent())
  4124.             lua_pushnumber(L, env->addThing(newItem));
  4125.         else //stackable item stacked with existing object, newItem will be released
  4126.             lua_pushnil(L);
  4127.  
  4128.         return 1;
  4129.     }
  4130.  
  4131.     lua_pushnil(L);
  4132.     return 1;
  4133. }
  4134.  
  4135. int32_t LuaInterface::luaDoPlayerAddItemEx(lua_State* L)
  4136. {
  4137.     //doPlayerAddItemEx(cid, uid[, canDropOnMap = false[, slot = 0]])
  4138.     int32_t params = lua_gettop(L), slot = SLOT_WHEREEVER;
  4139.     if(params > 3)
  4140.         slot = popNumber(L);
  4141.  
  4142.     bool canDropOnMap = false;
  4143.     if(params > 2)
  4144.         canDropOnMap = popNumber(L);
  4145.  
  4146.     uint32_t uid = (uint32_t)popNumber(L);
  4147.     if(slot > SLOT_AMMO)
  4148.     {
  4149.         errorEx("Invalid slot.");
  4150.         lua_pushboolean(L, false);
  4151.         return 1;
  4152.     }
  4153.  
  4154.     ScriptEnviroment* env = getEnv();
  4155.     Player* player = env->getPlayerByUID(popNumber(L));
  4156.     if(!player)
  4157.     {
  4158.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4159.         lua_pushboolean(L, false);
  4160.         return 1;
  4161.     }
  4162.  
  4163.     Item* item = env->getItemByUID(uid);
  4164.     if(!item)
  4165.     {
  4166.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  4167.         lua_pushboolean(L, false);
  4168.         return 1;
  4169.     }
  4170.  
  4171.     if(item->getParent() == VirtualCylinder::virtualCylinder)
  4172.         lua_pushnumber(L, g_game.internalPlayerAddItem(NULL, player, item, canDropOnMap, (slots_t)slot));
  4173.     else
  4174.         lua_pushboolean(L, false);
  4175.  
  4176.     return 1;
  4177. }
  4178.  
  4179. int32_t LuaInterface::luaDoTileAddItemEx(lua_State* L)
  4180. {
  4181.     //doTileAddItemEx(pos, uid)
  4182.     uint32_t uid = (uint32_t)popNumber(L);
  4183.     PositionEx pos;
  4184.     popPosition(L, pos);
  4185.  
  4186.     ScriptEnviroment* env = getEnv();
  4187.     Tile* tile = g_game.getTile(pos);
  4188.     if(!tile)
  4189.     {
  4190.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  4191.         lua_pushboolean(L, false);
  4192.         return 1;
  4193.     }
  4194.  
  4195.     Item* item = env->getItemByUID(uid);
  4196.     if(!item)
  4197.     {
  4198.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  4199.         lua_pushboolean(L, false);
  4200.         return 1;
  4201.     }
  4202.  
  4203.     if(item->getParent() == VirtualCylinder::virtualCylinder)
  4204.         lua_pushnumber(L, g_game.internalAddItem(NULL, tile, item));
  4205.     else
  4206.         lua_pushboolean(L, false);
  4207.  
  4208.     return 1;
  4209. }
  4210.  
  4211. int32_t LuaInterface::luaDoRelocate(lua_State* L)
  4212. {
  4213.     //doRelocate(pos, posTo[, creatures = true[, unmovable = true]])
  4214.     //Moves all moveable objects from pos to posTo
  4215.     bool unmovable = true, creatures = true;
  4216.     int32_t params = lua_gettop(L);
  4217.     if(params > 3)
  4218.         unmovable = popNumber(L);
  4219.  
  4220.     if(params > 2)
  4221.         creatures = popNumber(L);
  4222.  
  4223.     PositionEx toPos;
  4224.     popPosition(L, toPos);
  4225.  
  4226.     PositionEx fromPos;
  4227.     popPosition(L, fromPos);
  4228.  
  4229.     Tile* fromTile = g_game.getTile(fromPos.x, fromPos.y, fromPos.z);
  4230.     if(!fromTile)
  4231.     {
  4232.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  4233.         lua_pushboolean(L, false);
  4234.         return 1;
  4235.     }
  4236.  
  4237.     Tile* toTile = g_game.getTile(toPos.x, toPos.y, toPos.z);
  4238.     if(!toTile)
  4239.     {
  4240.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  4241.         lua_pushboolean(L, false);
  4242.         return 1;
  4243.     }
  4244.  
  4245.     if(fromTile != toTile)
  4246.     {
  4247.         for(int32_t i = fromTile->getThingCount() - 1; i >= 0; --i)
  4248.         {
  4249.             if(Thing* thing = fromTile->__getThing(i))
  4250.             {
  4251.                 if(Item* item = thing->getItem())
  4252.                 {
  4253.                     const ItemType& it = Item::items[item->getID()];
  4254.                     if(!it.isGroundTile() && !it.alwaysOnTop && !it.isMagicField())
  4255.                         g_game.internalTeleport(item, toPos, true, unmovable ? FLAG_IGNORENOTMOVEABLE : 0);
  4256.                 }
  4257.                 else if(creatures)
  4258.                 {
  4259.                     if(Creature* creature = thing->getCreature())
  4260.                         g_game.internalTeleport(creature, toPos, false);
  4261.                 }
  4262.             }
  4263.         }
  4264.     }
  4265.  
  4266.     lua_pushboolean(L, true);
  4267.     return 1;
  4268. }
  4269.  
  4270. int32_t LuaInterface::luaDoCleanTile(lua_State* L)
  4271. {
  4272.     //doCleanTile(pos, forceMapLoaded = false)
  4273.     //Remove all items from tile, ignore creatures
  4274.     bool forceMapLoaded = false;
  4275.     if(lua_gettop(L) > 1)
  4276.         forceMapLoaded = popNumber(L);
  4277.  
  4278.     PositionEx pos;
  4279.     popPosition(L, pos);
  4280.  
  4281.     Tile* tile = g_game.getTile(pos);
  4282.     if(!tile)
  4283.     {
  4284.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  4285.         lua_pushboolean(L, false);
  4286.         return 1;
  4287.     }
  4288.  
  4289.     for(int32_t i = tile->getThingCount() - 1; i >= 1; --i) //ignore ground
  4290.     {
  4291.         if(Thing* thing = tile->__getThing(i))
  4292.         {
  4293.             if(Item* item = thing->getItem())
  4294.             {
  4295.                 if(!item->isLoadedFromMap() || forceMapLoaded)
  4296.                     g_game.internalRemoveItem(NULL, item);
  4297.             }
  4298.         }
  4299.     }
  4300.  
  4301.     lua_pushboolean(L, true);
  4302.     return 1;
  4303. }
  4304.  
  4305. int32_t LuaInterface::luaDoPlayerSendTextMessage(lua_State* L)
  4306. {
  4307.     //doPlayerSendTextMessage(cid, MessageClasses, message)
  4308.     std::string text = popString(L);
  4309.     uint32_t messageClass = popNumber(L);
  4310.  
  4311.     ScriptEnviroment* env = getEnv();
  4312.     Player* player = env->getPlayerByUID(popNumber(L));
  4313.     if(!player)
  4314.     {
  4315.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4316.         lua_pushboolean(L, false);
  4317.         return 1;
  4318.     }
  4319.  
  4320.     player->sendTextMessage((MessageClasses)messageClass, text);
  4321.     lua_pushboolean(L, true);
  4322.     return 1;
  4323. }
  4324.  
  4325. int32_t LuaInterface::luaDoPlayerSendChannelMessage(lua_State* L)
  4326. {
  4327.     //doPlayerSendChannelMessage(cid, author, message, SpeakClasses, channel)
  4328.     uint16_t channelId = popNumber(L);
  4329.     uint32_t speakClass = popNumber(L);
  4330.     std::string text = popString(L), name = popString(L);
  4331.  
  4332.     ScriptEnviroment* env = getEnv();
  4333.     Player* player = env->getPlayerByUID(popNumber(L));
  4334.     if(!player)
  4335.     {
  4336.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4337.         lua_pushboolean(L, false);
  4338.         return 1;
  4339.     }
  4340.  
  4341.     player->sendChannelMessage(name, text, (SpeakClasses)speakClass, channelId);
  4342.     lua_pushboolean(L, true);
  4343.     return 1;
  4344. }
  4345.  
  4346. int32_t LuaInterface::luaDoPlayerSendToChannel(lua_State* L)
  4347. {
  4348.     //doPlayerSendToChannel(cid, targetId, SpeakClasses, message, channel[, time])
  4349.     ScriptEnviroment* env = getEnv();
  4350.     uint32_t time = 0;
  4351.     if(lua_gettop(L) > 5)
  4352.         time = popNumber(L);
  4353.  
  4354.     uint16_t channelId = popNumber(L);
  4355.     std::string text = popString(L);
  4356.     uint32_t speakClass = popNumber(L), targetId = popNumber(L);
  4357.  
  4358.     Player* player = env->getPlayerByUID(popNumber(L));
  4359.     if(!player)
  4360.     {
  4361.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4362.         lua_pushboolean(L, false);
  4363.         return 1;
  4364.     }
  4365.  
  4366.     Creature* creature = env->getCreatureByUID(targetId);
  4367.     if(!creature)
  4368.     {
  4369.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  4370.         lua_pushboolean(L, false);
  4371.         return 1;
  4372.     }
  4373.  
  4374.     player->sendToChannel(creature, (SpeakClasses)speakClass, text, channelId, time);
  4375.     lua_pushboolean(L, true);
  4376.     return 1;
  4377. }
  4378.  
  4379. int32_t LuaInterface::luaDoPlayerOpenChannel(lua_State* L)
  4380. {
  4381.     //doPlayerOpenChannel(cid, channelId)
  4382.     uint16_t channelId = popNumber(L);
  4383.     uint32_t cid = popNumber(L);
  4384.  
  4385.     ScriptEnviroment* env = getEnv();
  4386.     if(env->getPlayerByUID(cid))
  4387.     {
  4388.         lua_pushboolean(L, g_game.playerOpenChannel(cid, channelId));
  4389.         return 1;
  4390.     }
  4391.  
  4392.     errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4393.     lua_pushboolean(L, false);
  4394.     return 1;
  4395. }
  4396.  
  4397. int32_t LuaInterface::luaDoSendCreatureSquare(lua_State* L)
  4398. {
  4399.     //doSendCreatureSquare(cid, color[, player])
  4400.     ScriptEnviroment* env = getEnv();
  4401.     SpectatorVec list;
  4402.     if(lua_gettop(L) > 2)
  4403.     {
  4404.         if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  4405.             list.push_back(creature);
  4406.     }
  4407.  
  4408.         uint8_t color = popNumber(L);
  4409.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  4410.     {
  4411.             if(!list.empty())
  4412.                     g_game.addCreatureSquare(list, creature, color);
  4413.             else
  4414.                     g_game.addCreatureSquare(creature, color);
  4415.  
  4416.             lua_pushboolean(L, true);
  4417.     }
  4418.     else
  4419.     {
  4420.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  4421.         lua_pushboolean(L, false);
  4422.     }
  4423.  
  4424.         return 1;
  4425. }
  4426.  
  4427. int32_t LuaInterface::luaDoSendAnimatedText(lua_State* L)
  4428. {
  4429.     //doSendAnimatedText(pos, text, color[, player])
  4430.     ScriptEnviroment* env = getEnv();
  4431.     SpectatorVec list;
  4432.     if(lua_gettop(L) > 3)
  4433.     {
  4434.         if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  4435.             list.push_back(creature);
  4436.     }
  4437.  
  4438.     uint8_t color = popNumber(L);
  4439.     std::string text = popString(L);
  4440.  
  4441.     PositionEx pos;
  4442.     popPosition(L, pos);
  4443.     if(pos.x == 0xFFFF)
  4444.         pos = env->getRealPos();
  4445.  
  4446.     if(!list.empty())
  4447.         g_game.addAnimatedText(list, pos, color, text);
  4448.     else
  4449.         g_game.addAnimatedText(pos, color, text);
  4450.  
  4451.     lua_pushboolean(L, true);
  4452.     return 1;
  4453. }
  4454.  
  4455. int32_t LuaInterface::luaGetPlayerSkillLevel(lua_State* L)
  4456. {
  4457.     //getPlayerSkillLevel(cid, skillid)
  4458.     uint32_t skillId = popNumber(L);
  4459.  
  4460.     ScriptEnviroment* env = getEnv();
  4461.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  4462.     {
  4463.         if(skillId <= SKILL_LAST)
  4464.             lua_pushnumber(L, player->skills[skillId][SKILL_LEVEL]);
  4465.         else
  4466.             lua_pushboolean(L, false);
  4467.     }
  4468.     else
  4469.     {
  4470.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4471.         lua_pushboolean(L, false);
  4472.     }
  4473.  
  4474.     return 1;
  4475. }
  4476.  
  4477. int32_t LuaInterface::luaGetPlayerSkillTries(lua_State* L)
  4478. {
  4479.     //getPlayerSkillTries(cid, skillid)
  4480.     uint32_t skillid = popNumber(L);
  4481.  
  4482.     ScriptEnviroment* env = getEnv();
  4483.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  4484.     {
  4485.         if(skillid <= SKILL_LAST)
  4486.             lua_pushnumber(L, player->skills[skillid][SKILL_TRIES]);
  4487.         else
  4488.             lua_pushboolean(L, false);
  4489.     }
  4490.     else
  4491.     {
  4492.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4493.         lua_pushboolean(L, false);
  4494.     }
  4495.  
  4496.     return 1;
  4497. }
  4498.  
  4499. int32_t LuaInterface::luaDoCreatureSetDropLoot(lua_State* L)
  4500. {
  4501.     //doCreatureSetDropLoot(cid, doDrop)
  4502.     bool doDrop = popNumber(L);
  4503.  
  4504.     ScriptEnviroment* env = getEnv();
  4505.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  4506.     {
  4507.         creature->setDropLoot(doDrop ? LOOT_DROP_FULL : LOOT_DROP_NONE);
  4508.         lua_pushboolean(L, true);
  4509.     }
  4510.     else
  4511.     {
  4512.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  4513.         lua_pushboolean(L, false);
  4514.     }
  4515.  
  4516.     return 1;
  4517. }
  4518.  
  4519. int32_t LuaInterface::luaGetPlayerLossPercent(lua_State* L)
  4520. {
  4521.     //getPlayerLossPercent(cid, lossType)
  4522.     uint8_t lossType = (uint8_t)popNumber(L);
  4523.  
  4524.     ScriptEnviroment* env = getEnv();
  4525.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  4526.     {
  4527.         if(lossType <= LOSS_LAST)
  4528.         {
  4529.             uint32_t value = player->getLossPercent((lossTypes_t)lossType);
  4530.             lua_pushnumber(L, value);
  4531.         }
  4532.         else
  4533.             lua_pushboolean(L, false);
  4534.     }
  4535.     else
  4536.     {
  4537.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4538.         lua_pushboolean(L, false);
  4539.     }
  4540.  
  4541.     return 1;
  4542. }
  4543.  
  4544. int32_t LuaInterface::luaDoPlayerSetLossPercent(lua_State* L)
  4545. {
  4546.     //doPlayerSetLossPercent(cid, lossType, newPercent)
  4547.     uint32_t newPercent = popNumber(L);
  4548.     uint8_t lossType = (uint8_t)popNumber(L);
  4549.  
  4550.     ScriptEnviroment* env = getEnv();
  4551.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  4552.     {
  4553.         if(lossType <= LOSS_LAST)
  4554.         {
  4555.             player->setLossPercent((lossTypes_t)lossType, newPercent);
  4556.             lua_pushboolean(L, true);
  4557.         }
  4558.         else
  4559.             lua_pushboolean(L, false);
  4560.     }
  4561.     else
  4562.     {
  4563.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4564.         lua_pushboolean(L, false);
  4565.     }
  4566.  
  4567.     return 1;
  4568. }
  4569.  
  4570. int32_t LuaInterface::luaDoPlayerSetLossSkill(lua_State* L)
  4571. {
  4572.     //doPlayerSetLossSkill(cid, doLose)
  4573.     bool doLose = popNumber(L);
  4574.  
  4575.     ScriptEnviroment* env = getEnv();
  4576.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  4577.     {
  4578.         player->setLossSkill(doLose);
  4579.         lua_pushboolean(L, true);
  4580.     }
  4581.     else
  4582.     {
  4583.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4584.         lua_pushboolean(L, false);
  4585.     }
  4586.  
  4587.     return 1;
  4588. }
  4589.  
  4590. int32_t LuaInterface::luaDoShowTextDialog(lua_State* L)
  4591. {
  4592.     //doShowTextDialog(cid, itemid, text)
  4593.     std::string text = popString(L);
  4594.     uint32_t itemId = popNumber(L);
  4595.  
  4596.     ScriptEnviroment* env = getEnv();
  4597.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  4598.     {
  4599.         player->setWriteItem(NULL, 0);
  4600.         player->sendTextWindow(itemId, text);
  4601.         lua_pushboolean(L, true);
  4602.     }
  4603.     else
  4604.     {
  4605.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  4606.         lua_pushboolean(L, false);
  4607.     }
  4608.  
  4609.     return 1;
  4610. }
  4611.  
  4612. int32_t LuaInterface::luaDoDecayItem(lua_State* L)
  4613. {
  4614.     //doDecayItem(uid)
  4615.     //Note: to stop decay set decayTo = 0 in items.xml
  4616.     ScriptEnviroment* env = getEnv();
  4617.     if(Item* item = env->getItemByUID(popNumber(L)))
  4618.     {
  4619.         g_game.startDecay(item);
  4620.         lua_pushboolean(L, true);
  4621.     }
  4622.     else
  4623.     {
  4624.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  4625.         lua_pushboolean(L, false);
  4626.     }
  4627.  
  4628.     return 1;
  4629. }
  4630.  
  4631. int32_t LuaInterface::luaGetThingFromPos(lua_State* L)
  4632. {
  4633.     //getThingFromPos(pos[, displayError = true])
  4634.     //Note:
  4635.     //  stackpos = 255- top thing (movable item or creature)
  4636.     //  stackpos = 254- magic field
  4637.     //  stackpos = 253- top creature
  4638.  
  4639.     bool displayError = true;
  4640.     if(lua_gettop(L) > 1)
  4641.         displayError = popNumber(L);
  4642.  
  4643.     PositionEx pos;
  4644.     popPosition(L, pos);
  4645.  
  4646.     ScriptEnviroment* env = getEnv();
  4647.     Thing* thing = NULL;
  4648.     if(Tile* tile = g_game.getMap()->getTile(pos))
  4649.     {
  4650.         if(pos.stackpos == 255)
  4651.         {
  4652.             if(!(thing = tile->getTopCreature()))
  4653.             {
  4654.                 Item* item = tile->getTopDownItem();
  4655.                 if(item && item->isMoveable())
  4656.                     thing = item;
  4657.             }
  4658.         }
  4659.         else if(pos.stackpos == 254)
  4660.             thing = tile->getFieldItem();
  4661.         else if(pos.stackpos == 253)
  4662.             thing = tile->getTopCreature();
  4663.         else
  4664.             thing = tile->__getThing(pos.stackpos);
  4665.  
  4666.         if(thing)
  4667.             pushThing(L, thing, env->addThing(thing));
  4668.         else
  4669.             pushThing(L, NULL, 0);
  4670.  
  4671.         return 1;
  4672.     }
  4673.  
  4674.     if(displayError)
  4675.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  4676.  
  4677.     pushThing(L, NULL, 0);
  4678.     return 1;
  4679. }
  4680.  
  4681. int32_t LuaInterface::luaGetTileItemById(lua_State* L)
  4682. {
  4683.     //getTileItemById(pos, itemId[, subType = -1])
  4684.     ScriptEnviroment* env = getEnv();
  4685.  
  4686.     int32_t subType = -1;
  4687.     if(lua_gettop(L) > 2)
  4688.         subType = (int32_t)popNumber(L);
  4689.  
  4690.     int32_t itemId = (int32_t)popNumber(L);
  4691.     PositionEx pos;
  4692.     popPosition(L, pos);
  4693.  
  4694.     Tile* tile = g_game.getTile(pos);
  4695.     if(!tile)
  4696.     {
  4697.         pushThing(L, NULL, 0);
  4698.         return 1;
  4699.     }
  4700.  
  4701.     Item* item = g_game.findItemOfType(tile, itemId, false, subType);
  4702.     if(!item)
  4703.     {
  4704.         pushThing(L, NULL, 0);
  4705.         return 1;
  4706.     }
  4707.  
  4708.     pushThing(L, item, env->addThing(item));
  4709.     return 1;
  4710. }
  4711.  
  4712. int32_t LuaInterface::luaGetTileItemByType(lua_State* L)
  4713. {
  4714.     //getTileItemByType(pos, type)
  4715.     uint32_t rType = (uint32_t)popNumber(L);
  4716.     if(rType >= ITEM_TYPE_LAST)
  4717.     {
  4718.         errorEx("Not a valid item type");
  4719.         pushThing(L, NULL, 0);
  4720.         return 1;
  4721.     }
  4722.  
  4723.     PositionEx pos;
  4724.     popPosition(L, pos);
  4725.  
  4726.     Tile* tile = g_game.getTile(pos);
  4727.     if(!tile)
  4728.     {
  4729.         pushThing(L, NULL, 0);
  4730.         return 1;
  4731.     }
  4732.  
  4733.     bool found = true;
  4734.     switch((ItemTypes_t)rType)
  4735.     {
  4736.         case ITEM_TYPE_TELEPORT:
  4737.         {
  4738.             if(!tile->hasFlag(TILESTATE_TELEPORT))
  4739.                 found = false;
  4740.  
  4741.             break;
  4742.         }
  4743.         case ITEM_TYPE_MAGICFIELD:
  4744.         {
  4745.             if(!tile->hasFlag(TILESTATE_MAGICFIELD))
  4746.                 found = false;
  4747.  
  4748.             break;
  4749.         }
  4750.         case ITEM_TYPE_MAILBOX:
  4751.         {
  4752.             if(!tile->hasFlag(TILESTATE_MAILBOX))
  4753.                 found = false;
  4754.  
  4755.             break;
  4756.         }
  4757.         case ITEM_TYPE_TRASHHOLDER:
  4758.         {
  4759.             if(!tile->hasFlag(TILESTATE_TRASHHOLDER))
  4760.                 found = false;
  4761.  
  4762.             break;
  4763.         }
  4764.         case ITEM_TYPE_BED:
  4765.         {
  4766.             if(!tile->hasFlag(TILESTATE_BED))
  4767.                 found = false;
  4768.  
  4769.             break;
  4770.         }
  4771.         case ITEM_TYPE_DEPOT:
  4772.         {
  4773.             if(!tile->hasFlag(TILESTATE_DEPOT))
  4774.                 found = false;
  4775.  
  4776.             break;
  4777.         }
  4778.         default:
  4779.             break;
  4780.     }
  4781.  
  4782.     if(!found)
  4783.     {
  4784.         pushThing(L, NULL, 0);
  4785.         return 1;
  4786.     }
  4787.  
  4788.     ScriptEnviroment* env = getEnv();
  4789.     Item* item = NULL;
  4790.     for(uint32_t i = 0; i < tile->getThingCount(); ++i)
  4791.     {
  4792.         if(!(item = tile->__getThing(i)->getItem()))
  4793.             continue;
  4794.  
  4795.         if(Item::items[item->getID()].type != (ItemTypes_t)rType)
  4796.             continue;
  4797.  
  4798.         pushThing(L, item, env->addThing(item));
  4799.         return 1;
  4800.     }
  4801.  
  4802.     pushThing(L, NULL, 0);
  4803.     return 1;
  4804. }
  4805.  
  4806. int32_t LuaInterface::luaGetTileThingByPos(lua_State* L)
  4807. {
  4808.     //getTileThingByPos(pos)
  4809.     PositionEx pos;
  4810.     popPosition(L, pos);
  4811.  
  4812.     ScriptEnviroment* env = getEnv();
  4813.  
  4814.     Tile* tile = g_game.getTile(pos.x, pos.y, pos.z);
  4815.     if(!tile)
  4816.     {
  4817.         if(pos.stackpos == -1)
  4818.         {
  4819.             lua_pushnumber(L, -1);
  4820.             return 1;
  4821.         }
  4822.         else
  4823.         {
  4824.             pushThing(L, NULL, 0);
  4825.             return 1;
  4826.         }
  4827.     }
  4828.  
  4829.     if(pos.stackpos == -1)
  4830.     {
  4831.         lua_pushnumber(L, tile->getThingCount());
  4832.         return 1;
  4833.     }
  4834.  
  4835.     Thing* thing = tile->__getThing(pos.stackpos);
  4836.     if(!thing)
  4837.     {
  4838.         pushThing(L, NULL, 0);
  4839.         return 1;
  4840.     }
  4841.  
  4842.     pushThing(L, thing, env->addThing(thing));
  4843.     return 1;
  4844. }
  4845.  
  4846. int32_t LuaInterface::luaGetTopCreature(lua_State* L)
  4847. {
  4848.     //getTopCreature(pos)
  4849.     PositionEx pos;
  4850.     popPosition(L, pos);
  4851.  
  4852.     ScriptEnviroment* env = getEnv();
  4853.     Tile* tile = g_game.getTile(pos);
  4854.     if(!tile)
  4855.     {
  4856.         pushThing(L, NULL, 0);
  4857.         return 1;
  4858.     }
  4859.  
  4860.     Thing* thing = tile->getTopCreature();
  4861.     if(!thing || !thing->getCreature())
  4862.     {
  4863.         pushThing(L, NULL, 0);
  4864.         return 1;
  4865.     }
  4866.  
  4867.     pushThing(L, thing, env->addThing(thing));
  4868.     return 1;
  4869. }
  4870.  
  4871. int32_t LuaInterface::luaDoCreateItem(lua_State* L)
  4872. {
  4873.     //doCreateItem(itemid[, type/count = 1], pos)
  4874.     //Returns uid of the created item, only works on tiles.
  4875.     PositionEx pos;
  4876.     popPosition(L, pos);
  4877.  
  4878.     uint32_t count = 1;
  4879.     if(lua_gettop(L) > 1)
  4880.         count = popNumber(L);
  4881.  
  4882.     uint32_t itemId = popNumber(L);
  4883.     ScriptEnviroment* env = getEnv();
  4884.     const ItemType& it = Item::items[itemId];
  4885.  
  4886.     Tile* tile = g_game.getTile(pos);
  4887.     if(!tile)
  4888.     {
  4889.         if(it.group == ITEM_GROUP_GROUND)
  4890.         {
  4891.             Item* item = Item::CreateItem(itemId);
  4892.             tile = IOMap::createTile(item, NULL, pos.x, pos.y, pos.z);
  4893.  
  4894.             g_game.setTile(tile);
  4895.             lua_pushnumber(L, env->addThing(item));
  4896.             return 1;
  4897.         }
  4898.         else
  4899.         {
  4900.             errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  4901.             lua_pushboolean(L, false);
  4902.             return 1;
  4903.         }
  4904.     }
  4905.  
  4906.     int32_t itemCount = 1, subType = 1;
  4907.     if(it.hasSubType())
  4908.     {
  4909.         if(it.stackable)
  4910.             itemCount = (int32_t)std::ceil((float)count / 100);
  4911.  
  4912.         subType = count;
  4913.     }
  4914.     else
  4915.         itemCount = std::max((uint32_t)1, count);
  4916.  
  4917.     while(itemCount > 0)
  4918.     {
  4919.         int32_t stackCount = std::min(100, subType);
  4920.         Item* newItem = Item::CreateItem(itemId, stackCount);
  4921.         if(!newItem)
  4922.         {
  4923.             errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  4924.             lua_pushboolean(L, false);
  4925.             return 1;
  4926.         }
  4927.  
  4928.         if(it.stackable)
  4929.             subType -= stackCount;
  4930.  
  4931.         ReturnValue ret = g_game.internalAddItem(NULL, tile, newItem, INDEX_WHEREEVER, FLAG_NOLIMIT);
  4932.         if(ret != RET_NOERROR)
  4933.         {
  4934.             delete newItem;
  4935.             lua_pushboolean(L, false);
  4936.             return 1;
  4937.         }
  4938.  
  4939.         --itemCount;
  4940.         if(itemCount)
  4941.             continue;
  4942.  
  4943.         if(newItem->getParent())
  4944.             lua_pushnumber(L, env->addThing(newItem));
  4945.         else //stackable item stacked with existing object, newItem will be released
  4946.             lua_pushnil(L);
  4947.  
  4948.         return 1;
  4949.     }
  4950.  
  4951.     lua_pushnil(L);
  4952.     return 1;
  4953. }
  4954.  
  4955. int32_t LuaInterface::luaDoCreateItemEx(lua_State* L)
  4956. {
  4957.     //doCreateItemEx(itemid[, count/subType])
  4958.     uint32_t count = 0;
  4959.     if(lua_gettop(L) > 1)
  4960.         count = popNumber(L);
  4961.  
  4962.     ScriptEnviroment* env = getEnv();
  4963.     const ItemType& it = Item::items[(uint32_t)popNumber(L)];
  4964.     if(it.stackable && count > 100)
  4965.         count = 100;
  4966.  
  4967.     Item* newItem = Item::CreateItem(it.id, count);
  4968.     if(!newItem)
  4969.     {
  4970.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  4971.         lua_pushboolean(L, false);
  4972.         return 1;
  4973.     }
  4974.  
  4975.     newItem->setParent(VirtualCylinder::virtualCylinder);
  4976.     env->addTempItem(env, newItem);
  4977.  
  4978.     lua_pushnumber(L, env->addThing(newItem));
  4979.     return 1;
  4980. }
  4981.  
  4982. int32_t LuaInterface::luaDoCreateTeleport(lua_State* L)
  4983. {
  4984.     //doCreateTeleport(itemid, toPosition, fromPosition)
  4985.     PositionEx createPos;
  4986.     popPosition(L, createPos);
  4987.     PositionEx toPos;
  4988.     popPosition(L, toPos);
  4989.  
  4990.     uint32_t itemId = (uint32_t)popNumber(L);
  4991.     ScriptEnviroment* env = getEnv();
  4992.  
  4993.     Tile* tile = g_game.getMap()->getTile(createPos);
  4994.     if(!tile)
  4995.     {
  4996.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  4997.         lua_pushboolean(L, false);
  4998.         return 1;
  4999.     }
  5000.  
  5001.     Item* newItem = Item::CreateItem(itemId);
  5002.     Teleport* newTeleport = newItem->getTeleport();
  5003.     if(!newTeleport)
  5004.     {
  5005.         delete newItem;
  5006.         lua_pushboolean(L, false);
  5007.         return 1;
  5008.     }
  5009.  
  5010.     newTeleport->setDestination(toPos);
  5011.     if(g_game.internalAddItem(NULL, tile, newTeleport, INDEX_WHEREEVER, FLAG_NOLIMIT) != RET_NOERROR)
  5012.     {
  5013.         delete newItem;
  5014.         lua_pushboolean(L, false);
  5015.         return 1;
  5016.     }
  5017.  
  5018.     if(newItem->getParent())
  5019.         lua_pushnumber(L, env->addThing(newItem));
  5020.     else //stackable item stacked with existing object, newItem will be released
  5021.         lua_pushnil(L);
  5022.  
  5023.     return 1;
  5024. }
  5025.  
  5026. int32_t LuaInterface::luaGetCreatureStorage(lua_State* L)
  5027. {
  5028.     //getCreatureStorage(cid, key)
  5029.     uint32_t key = popNumber(L);
  5030.     ScriptEnviroment* env = getEnv();
  5031.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  5032.     {
  5033.         std::string strValue;
  5034.         if(creature->getStorage(key, strValue))
  5035.         {
  5036.             int32_t intValue = atoi(strValue.c_str());
  5037.             if(intValue || strValue == "0")
  5038.                 lua_pushnumber(L, intValue);
  5039.             else
  5040.                 lua_pushstring(L, strValue.c_str());
  5041.         }
  5042.         else
  5043.             lua_pushnumber(L, -1);
  5044.     }
  5045.     else
  5046.     {
  5047.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  5048.         lua_pushboolean(L, false);
  5049.     }
  5050.  
  5051.     return 1;
  5052. }
  5053.  
  5054. int32_t LuaInterface::luaDoCreatureSetStorage(lua_State* L)
  5055. {
  5056.     //doCreatureSetStorage(cid, key[, value])
  5057.     std::string value;
  5058.     bool nil = true;
  5059.     if(lua_gettop(L) > 2)
  5060.     {
  5061.         if(!lua_isnil(L, -1))
  5062.         {
  5063.             value = popString(L);
  5064.             nil = false;
  5065.         }
  5066.         else
  5067.             lua_pop(L, 1);
  5068.     }
  5069.  
  5070.     uint32_t key = popNumber(L);
  5071.     ScriptEnviroment* env = getEnv();
  5072.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  5073.     {
  5074.         if(!nil)
  5075.             nil = creature->setStorage(key, value);
  5076.         else
  5077.             creature->eraseStorage(key);
  5078.  
  5079.         lua_pushboolean(L, nil);
  5080.     }
  5081.     else
  5082.     {
  5083.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  5084.         lua_pushboolean(L, false);
  5085.     }
  5086.  
  5087.     return 1;
  5088. }
  5089.  
  5090. int32_t LuaInterface::luaGetTileInfo(lua_State* L)
  5091. {
  5092.     //getTileInfo(pos)
  5093.     PositionEx pos;
  5094.     popPosition(L, pos);
  5095.     if(Tile* tile = g_game.getMap()->getTile(pos))
  5096.     {
  5097.         ScriptEnviroment* env = getEnv();
  5098.         pushThing(L, tile->ground, env->addThing(tile->ground));
  5099.  
  5100.         setFieldBool(L, "protection", tile->hasFlag(TILESTATE_PROTECTIONZONE));
  5101.         setFieldBool(L, "optional", tile->hasFlag(TILESTATE_OPTIONALZONE));
  5102.         setFieldBool(L, "nologout", tile->hasFlag(TILESTATE_NOLOGOUT));
  5103.         setFieldBool(L, "hardcore", tile->hasFlag(TILESTATE_HARDCOREZONE));
  5104.         setFieldBool(L, "refresh", tile->hasFlag(TILESTATE_REFRESH));
  5105.         setFieldBool(L, "trashed", tile->hasFlag(TILESTATE_TRASHED));
  5106.         setFieldBool(L, "house", tile->hasFlag(TILESTATE_HOUSE));
  5107.         setFieldBool(L, "bed", tile->hasFlag(TILESTATE_BED));
  5108.         setFieldBool(L, "depot", tile->hasFlag(TILESTATE_DEPOT));
  5109.  
  5110.         setField(L, "things", tile->getThingCount());
  5111.         setField(L, "creatures", tile->getCreatureCount());
  5112.         setField(L, "items", tile->getItemCount());
  5113.         setField(L, "topItems", tile->getTopItemCount());
  5114.         setField(L, "downItems", tile->getDownItemCount());
  5115.     }
  5116.     else
  5117.     {
  5118.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  5119.         lua_pushboolean(L, false);
  5120.     }
  5121.  
  5122.     return 1;
  5123. }
  5124.  
  5125. int32_t LuaInterface::luaGetHouseFromPos(lua_State* L)
  5126. {
  5127.     //getHouseFromPos(pos)
  5128.     PositionEx pos;
  5129.     popPosition(L, pos);
  5130.  
  5131.     Tile* tile = g_game.getMap()->getTile(pos);
  5132.     if(!tile)
  5133.     {
  5134.         errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  5135.         lua_pushboolean(L, false);
  5136.         return 1;
  5137.     }
  5138.  
  5139.     HouseTile* houseTile = tile->getHouseTile();
  5140.     if(!houseTile)
  5141.     {
  5142.         lua_pushboolean(L, false);
  5143.         return 1;
  5144.     }
  5145.  
  5146.     House* house = houseTile->getHouse();
  5147.     if(!house)
  5148.     {
  5149.         lua_pushboolean(L, false);
  5150.         return 1;
  5151.     }
  5152.  
  5153.     lua_pushnumber(L, house->getId());
  5154.     return 1;
  5155. }
  5156.  
  5157. int32_t LuaInterface::luaDoCreateMonster(lua_State* L)
  5158. {
  5159.     //doCreateMonster(name, pos[, extend = false[, force = false[, displayError = true]]])
  5160.     bool displayError = true, force = false, extend = false;
  5161.     int32_t params = lua_gettop(L);
  5162.     if(params > 4)
  5163.         displayError = popNumber(L);
  5164.  
  5165.     if(params > 3)
  5166.         force = popNumber(L);
  5167.  
  5168.     if(params > 2)
  5169.         extend = popNumber(L);
  5170.  
  5171.     PositionEx pos;
  5172.     popPosition(L, pos);
  5173.  
  5174.     std::string name = popString(L);
  5175.     Monster* monster = Monster::createMonster(name.c_str());
  5176.     if(!monster)
  5177.     {
  5178.         if(displayError)
  5179.             errorEx("Monster with name '" + name + "' not found");
  5180.  
  5181.         lua_pushboolean(L, false);
  5182.         return 1;
  5183.     }
  5184.  
  5185.     if(!g_game.placeCreature(monster, pos, extend, force))
  5186.     {
  5187.         delete monster;
  5188.         if(displayError)
  5189.             errorEx("Cannot create monster: " + name);
  5190.  
  5191.         lua_pushboolean(L, true);
  5192.         return 1;
  5193.     }
  5194.  
  5195.     ScriptEnviroment* env = getEnv();
  5196.     lua_pushnumber(L, env->addThing((Thing*)monster));
  5197.     return 1;
  5198. }
  5199.  
  5200. int32_t LuaInterface::luaDoCreateNpc(lua_State* L)
  5201. {
  5202.     //doCreateNpc(name, pos[, displayError = true])
  5203.     bool displayError = true;
  5204.     if(lua_gettop(L) > 2)
  5205.         displayError = popNumber(L);
  5206.  
  5207.     PositionEx pos;
  5208.     popPosition(L, pos);
  5209.  
  5210.     std::string name = popString(L);
  5211.     Npc* npc = Npc::createNpc(name.c_str());
  5212.     if(!npc)
  5213.     {
  5214.         if(displayError)
  5215.             errorEx("Npc with name '" + name + "' not found");
  5216.  
  5217.         lua_pushboolean(L, false);
  5218.         return 1;
  5219.     }
  5220.  
  5221.     if(!g_game.placeCreature(npc, pos))
  5222.     {
  5223.         delete npc;
  5224.         if(displayError)
  5225.             errorEx("Cannot create npc: " + name);
  5226.  
  5227.         lua_pushboolean(L, true); //for scripting compatibility
  5228.         return 1;
  5229.     }
  5230.  
  5231.     ScriptEnviroment* env = getEnv();
  5232.     lua_pushnumber(L, env->addThing((Thing*)npc));
  5233.     return 1;
  5234. }
  5235.  
  5236. int32_t LuaInterface::luaDoRemoveCreature(lua_State* L)
  5237. {
  5238.     //doRemoveCreature(cid[, forceLogout = true])
  5239.     bool forceLogout = true;
  5240.     if(lua_gettop(L) > 1)
  5241.         forceLogout = popNumber(L);
  5242.  
  5243.     ScriptEnviroment* env = getEnv();
  5244.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  5245.     {
  5246.         if(Player* player = creature->getPlayer())
  5247.             player->kickPlayer(true, forceLogout); //Players will get kicked without restrictions
  5248.         else
  5249.             g_game.removeCreature(creature); //Monsters/NPCs will get removed
  5250.  
  5251.         lua_pushboolean(L, true);
  5252.     }
  5253.     else
  5254.     {
  5255.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  5256.         lua_pushboolean(L, false);
  5257.     }
  5258.  
  5259.     return 1;
  5260. }
  5261.  
  5262. int32_t LuaInterface::luaDoPlayerAddMoney(lua_State* L)
  5263. {
  5264.     //doPlayerAddMoney(cid, money)
  5265.     uint64_t money = popNumber(L);
  5266.  
  5267.     ScriptEnviroment* env = getEnv();
  5268.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5269.     {
  5270.         g_game.addMoney(player, money);
  5271.         lua_pushboolean(L, true);
  5272.     }
  5273.     else
  5274.     {
  5275.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5276.         lua_pushboolean(L, false);
  5277.     }
  5278.  
  5279.     return 1;
  5280. }
  5281. // check bankBalance onBuy 1
  5282. int32_t LuaInterface::luaDoPlayerRemoveMoney(lua_State* L)
  5283. {
  5284.     //doPlayerRemoveMoney(cid,money[, includeBank = true])
  5285.  
  5286.     bool includeBank = true;
  5287.     if(lua_gettop(L) > 2)
  5288.         includeBank = popBoolean(L);
  5289.  
  5290.     uint64_t money = popNumber(L);
  5291.  
  5292.     ScriptEnviroment* env = getEnv();
  5293.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5294.         lua_pushboolean(L, g_game.removeMoney(player, money, 0, includeBank));
  5295.     else
  5296.     {
  5297.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5298.         lua_pushboolean(L, false);
  5299.     }
  5300.     return 1;
  5301. }
  5302.  
  5303. int32_t LuaInterface::luaDoPlayerTransferMoneyTo(lua_State* L)
  5304. {
  5305.     //doPlayerTransferMoneyTo(cid, target, money)
  5306.     uint64_t money = popNumber(L);
  5307.     std::string target = popString(L);
  5308.  
  5309.     ScriptEnviroment* env = getEnv();
  5310.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5311.         lua_pushboolean(L, player->transferMoneyTo(target, money));
  5312.     else
  5313.     {
  5314.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5315.         lua_pushboolean(L, false);
  5316.     }
  5317.  
  5318.     return 1;
  5319. }
  5320.  
  5321. int32_t LuaInterface::luaDoPlayerSetPzLocked(lua_State* L)
  5322. {
  5323.     //doPlayerSetPzLocked(cid, locked)
  5324.     bool locked = popNumber(L);
  5325.  
  5326.     ScriptEnviroment* env = getEnv();
  5327.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5328.     {
  5329.         if(player->isPzLocked() != locked)
  5330.         {
  5331.             player->setPzLocked(locked);
  5332.             player->sendIcons();
  5333.         }
  5334.  
  5335.         lua_pushboolean(L, true);
  5336.     }
  5337.     else
  5338.     {
  5339.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5340.         lua_pushboolean(L, false);
  5341.     }
  5342.  
  5343.     return 1;
  5344. }
  5345.  
  5346. int32_t LuaInterface::luaDoPlayerSetTown(lua_State* L)
  5347. {
  5348.     //doPlayerSetTown(cid, townid)
  5349.     uint32_t townid = (uint32_t)popNumber(L);
  5350.  
  5351.     ScriptEnviroment* env = getEnv();
  5352.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5353.     {
  5354.         if(Town* town = Towns::getInstance()->getTown(townid))
  5355.         {
  5356.             player->setMasterPosition(town->getPosition());
  5357.             player->setTown(townid);
  5358.             lua_pushboolean(L, true);
  5359.         }
  5360.         else
  5361.             lua_pushboolean(L, false);
  5362.     }
  5363.     else
  5364.     {
  5365.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5366.         lua_pushboolean(L, false);
  5367.     }
  5368.  
  5369.     return 1;
  5370. }
  5371.  
  5372. int32_t LuaInterface::luaDoPlayerSetVocation(lua_State* L)
  5373. {
  5374.     //doPlayerSetVocation(cid, voc)
  5375.     uint32_t voc = popNumber(L);
  5376.  
  5377.     ScriptEnviroment* env = getEnv();
  5378.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5379.     {
  5380.         player->setVocation(voc);
  5381.         lua_pushboolean(L, true);
  5382.     }
  5383.     else
  5384.     {
  5385.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5386.         lua_pushboolean(L, false);
  5387.     }
  5388.  
  5389.     return 1;
  5390. }
  5391.  
  5392. int32_t LuaInterface::luaDoPlayerSetSex(lua_State* L)
  5393. {
  5394.     //doPlayerSetSex(cid, sex)
  5395.     uint32_t newSex = popNumber(L);
  5396.  
  5397.     ScriptEnviroment* env = getEnv();
  5398.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5399.     {
  5400.         player->setSex(newSex);
  5401.         lua_pushboolean(L, true);
  5402.     }
  5403.     else
  5404.     {
  5405.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5406.         lua_pushboolean(L, false);
  5407.     }
  5408.  
  5409.     return 1;
  5410. }
  5411.  
  5412. int32_t LuaInterface::luaDoPlayerAddSoul(lua_State* L)
  5413. {
  5414.     //doPlayerAddSoul(cid, soul)
  5415.     int32_t soul = popNumber(L);
  5416.  
  5417.     ScriptEnviroment* env = getEnv();
  5418.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5419.     {
  5420.         player->changeSoul(soul);
  5421.         lua_pushboolean(L, true);
  5422.     }
  5423.     else
  5424.     {
  5425.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5426.         lua_pushboolean(L, false);
  5427.     }
  5428.  
  5429.     return 1;
  5430. }
  5431.  
  5432. int32_t LuaInterface::luaGetPlayerItemCount(lua_State* L)
  5433. {
  5434.     //getPlayerItemCount(cid, itemid[, subType = -1])
  5435.     int32_t subType = -1;
  5436.     if(lua_gettop(L) > 2)
  5437.         subType = popNumber(L);
  5438.  
  5439.     uint32_t itemId = popNumber(L);
  5440.     ScriptEnviroment* env = getEnv();
  5441.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  5442.         lua_pushnumber(L, player->__getItemTypeCount(itemId, subType));
  5443.     else
  5444.     {
  5445.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5446.         lua_pushboolean(L, false);
  5447.     }
  5448.  
  5449.     return 1;
  5450. }
  5451. // check bankBalance onBuy 2
  5452. int32_t LuaInterface::luaGetPlayerMoney(lua_State* L)
  5453. {
  5454.     //getPlayerMoney(cid[, includeBank = true])
  5455.  
  5456.     bool includeBank = true;
  5457.     if(lua_gettop(L) > 1)
  5458.         includeBank = popBoolean(L);
  5459.  
  5460.     ScriptEnviroment* env = getEnv();
  5461.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5462.         lua_pushnumber(L, g_game.getMoney(player, includeBank));
  5463.     else
  5464.     {
  5465.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5466.         lua_pushboolean(L, false);
  5467.     }
  5468.     return 1;
  5469. }
  5470.  
  5471. int32_t LuaInterface::luaGetHouseInfo(lua_State* L)
  5472. {
  5473.     //getHouseInfo(houseId[, displayError = true])
  5474.     bool displayError = true;
  5475.     if(lua_gettop(L) > 1)
  5476.         displayError = popNumber(L);
  5477.  
  5478.     House* house = Houses::getInstance()->getHouse(popNumber(L));
  5479.     if(!house)
  5480.     {
  5481.         if(displayError)
  5482.             errorEx(getError(LUA_ERROR_HOUSE_NOT_FOUND));
  5483.  
  5484.         lua_pushboolean(L, false);
  5485.         return 1;
  5486.     }
  5487.  
  5488.     lua_newtable(L);
  5489.     setField(L, "id", house->getId());
  5490.     setField(L, "name", house->getName().c_str());
  5491.     setField(L, "owner", house->getOwner());
  5492.  
  5493.     lua_pushstring(L, "entry");
  5494.     pushPosition(L, house->getEntry(), 0);
  5495.     pushTable(L);
  5496.  
  5497.     setField(L, "rent", house->getRent());
  5498.     setField(L, "price", house->getPrice());
  5499.     setField(L, "town", house->getTownId());
  5500.     setField(L, "paidUntil", house->getPaidUntil());
  5501.     setField(L, "warnings", house->getRentWarnings());
  5502.     setField(L, "lastWarning", house->getLastWarning());
  5503.  
  5504.     setFieldBool(L, "guildHall", house->isGuild());
  5505.     setField(L, "size", house->getSize());
  5506.     createTable(L, "doors");
  5507.  
  5508.     HouseDoorList::iterator dit = house->getHouseDoorBegin();
  5509.     for(uint32_t i = 1; dit != house->getHouseDoorEnd(); ++dit, ++i)
  5510.     {
  5511.         lua_pushnumber(L, i);
  5512.         pushPosition(L, (*dit)->getPosition(), 0);
  5513.         pushTable(L);
  5514.     }
  5515.  
  5516.     pushTable(L);
  5517.     createTable(L, "beds");
  5518.  
  5519.     HouseBedList::iterator bit = house->getHouseBedsBegin();
  5520.     for(uint32_t i = 1; bit != house->getHouseBedsEnd(); ++bit, ++i)
  5521.     {
  5522.         lua_pushnumber(L, i);
  5523.         pushPosition(L, (*bit)->getPosition(), 0);
  5524.         pushTable(L);
  5525.     }
  5526.  
  5527.     pushTable(L);
  5528.     createTable(L, "tiles");
  5529.  
  5530.     HouseTileList::iterator tit = house->getHouseTileBegin();
  5531.     for(uint32_t i = 1; tit != house->getHouseTileEnd(); ++tit, ++i)
  5532.     {
  5533.         lua_pushnumber(L, i);
  5534.         pushPosition(L, (*tit)->getPosition(), 0);
  5535.         pushTable(L);
  5536.     }
  5537.  
  5538.     pushTable(L);
  5539.     return 1;
  5540. }
  5541.  
  5542. int32_t LuaInterface::luaGetHouseAccessList(lua_State* L)
  5543. {
  5544.     //getHouseAccessList(houseid, listid)
  5545.     uint32_t listid = popNumber(L);
  5546.     if(House* house = Houses::getInstance()->getHouse(popNumber(L)))
  5547.     {
  5548.         std::string list;
  5549.         if(house->getAccessList(listid, list))
  5550.             lua_pushstring(L, list.c_str());
  5551.         else
  5552.             lua_pushnil(L);
  5553.     }
  5554.     else
  5555.     {
  5556.         errorEx(getError(LUA_ERROR_HOUSE_NOT_FOUND));
  5557.         lua_pushnil(L);
  5558.     }
  5559.  
  5560.     return 1;
  5561. }
  5562.  
  5563. int32_t LuaInterface::luaGetHouseByPlayerGUID(lua_State* L)
  5564. {
  5565.     //getHouseByPlayerGUID(guid)
  5566.     if(House* house = Houses::getInstance()->getHouseByPlayerId(popNumber(L)))
  5567.         lua_pushnumber(L, house->getId());
  5568.     else
  5569.         lua_pushnil(L);
  5570.     return 1;
  5571. }
  5572.  
  5573. int32_t LuaInterface::luaSetHouseAccessList(lua_State* L)
  5574. {
  5575.     //setHouseAccessList(houseid, listid, listtext)
  5576.     std::string list = popString(L);
  5577.     uint32_t listid = popNumber(L);
  5578.  
  5579.     if(House* house = Houses::getInstance()->getHouse(popNumber(L)))
  5580.     {
  5581.         house->setAccessList(listid, list);
  5582.         lua_pushboolean(L, true);
  5583.     }
  5584.     else
  5585.     {
  5586.         errorEx(getError(LUA_ERROR_HOUSE_NOT_FOUND));
  5587.         lua_pushboolean(L, false);
  5588.     }
  5589.  
  5590.     return 1;
  5591. }
  5592.  
  5593. int32_t LuaInterface::luaSetHouseOwner(lua_State* L)
  5594. {
  5595.     //setHouseOwner(houseId, owner[, clean])
  5596.     bool clean = true;
  5597.     if(lua_gettop(L) > 2)
  5598.         clean = popNumber(L);
  5599.  
  5600.     uint32_t owner = popNumber(L);
  5601.     if(House* house = Houses::getInstance()->getHouse(popNumber(L)))
  5602.         lua_pushboolean(L, house->setOwnerEx(owner, clean));
  5603.     else
  5604.     {
  5605.         errorEx(getError(LUA_ERROR_HOUSE_NOT_FOUND));
  5606.         lua_pushboolean(L, false);
  5607.     }
  5608.  
  5609.     return 1;
  5610. }
  5611.  
  5612. int32_t LuaInterface::luaGetWorldType(lua_State* L)
  5613. {
  5614.     lua_pushnumber(L, (uint32_t)g_game.getWorldType());
  5615.     return 1;
  5616. }
  5617.  
  5618. int32_t LuaInterface::luaSetWorldType(lua_State* L)
  5619. {
  5620.     //setWorldType(type)
  5621.     WorldType_t type = (WorldType_t)popNumber(L);
  5622.     if(type >= WORLDTYPE_FIRST && type <= WORLDTYPE_LAST)
  5623.     {
  5624.         g_game.setWorldType(type);
  5625.         lua_pushboolean(L, true);
  5626.     }
  5627.     else
  5628.         lua_pushboolean(L, false);
  5629.  
  5630.     return 1;
  5631. }
  5632.  
  5633. int32_t LuaInterface::luaGetWorldTime(lua_State* L)
  5634. {
  5635.     //getWorldTime()
  5636.     lua_pushnumber(L, g_game.getLightHour());
  5637.     return 1;
  5638. }
  5639.  
  5640. int32_t LuaInterface::luaGetWorldLight(lua_State* L)
  5641. {
  5642.     //getWorldLight()
  5643.     LightInfo lightInfo;
  5644.     g_game.getWorldLightInfo(lightInfo);
  5645.     lua_pushnumber(L, lightInfo.level);
  5646.     lua_pushnumber(L, lightInfo.color);
  5647.     return 2;
  5648. }
  5649.  
  5650. int32_t LuaInterface::luaGetWorldCreatures(lua_State* L)
  5651. {
  5652.     //getWorldCreatures(type)
  5653.     //0 players, 1 monsters, 2 npcs, 3 all
  5654.     uint32_t type = popNumber(L), value;
  5655.     switch(type)
  5656.     {
  5657.         case 0:
  5658.             value = g_game.getPlayersOnline();
  5659.             break;
  5660.         case 1:
  5661.             value = g_game.getMonstersOnline();
  5662.             break;
  5663.         case 2:
  5664.             value = g_game.getNpcsOnline();
  5665.             break;
  5666.         case 3:
  5667.             value = g_game.getCreaturesOnline();
  5668.             break;
  5669.         default:
  5670.             lua_pushboolean(L, false);
  5671.             return 1;
  5672.     }
  5673.  
  5674.     lua_pushnumber(L, value);
  5675.     return 1;
  5676. }
  5677.  
  5678. int32_t LuaInterface::luaGetWorldUpTime(lua_State* L)
  5679. {
  5680.     //getWorldUpTime()
  5681.     uint32_t uptime = 0;
  5682.     if(Status* status = Status::getInstance())
  5683.         uptime = status->getUptime();
  5684.  
  5685.     lua_pushnumber(L, uptime);
  5686.     return 1;
  5687. }
  5688.  
  5689. int32_t LuaInterface::luaGetPlayerLight(lua_State* L)
  5690. {
  5691.     //getPlayerLight(cid)
  5692.     ScriptEnviroment* env = getEnv();
  5693.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  5694.     {
  5695.         LightInfo lightInfo;
  5696.         player->getCreatureLight(lightInfo);
  5697.         lua_pushnumber(L, lightInfo.level);
  5698.         lua_pushnumber(L, lightInfo.color);
  5699.         return 2;
  5700.     }
  5701.     else
  5702.     {
  5703.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5704.         lua_pushboolean(L, false);
  5705.         return 1;
  5706.     }
  5707. }
  5708.  
  5709. int32_t LuaInterface::luaDoPlayerAddExperience(lua_State* L)
  5710. {
  5711.     //doPlayerAddExperience(cid, amount)
  5712.     int64_t amount = popNumber(L);
  5713.  
  5714.     ScriptEnviroment* env = getEnv();
  5715.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5716.     {
  5717.         if(amount > 0)
  5718.             player->addExperience(amount);
  5719.         else if(amount < 0)
  5720.             player->removeExperience(std::abs(amount));
  5721.         else
  5722.         {
  5723.             lua_pushboolean(L, false);
  5724.             return 1;
  5725.         }
  5726.  
  5727.         lua_pushboolean(L, true);
  5728.     }
  5729.     else
  5730.     {
  5731.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5732.         lua_pushboolean(L, false);
  5733.     }
  5734.  
  5735.     return 1;
  5736. }
  5737.  
  5738. int32_t LuaInterface::luaGetPlayerSlotItem(lua_State* L)
  5739. {
  5740.     //getPlayerSlotItem(cid, slot)
  5741.     uint32_t slot = popNumber(L);
  5742.  
  5743.     ScriptEnviroment* env = getEnv();
  5744.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  5745.     {
  5746.         if(Thing* thing = player->__getThing(slot))
  5747.             pushThing(L, thing, env->addThing(thing));
  5748.         else
  5749.             pushThing(L, NULL, 0);
  5750.     }
  5751.     else
  5752.     {
  5753.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5754.         pushThing(L, NULL, 0);
  5755.     }
  5756.  
  5757.     return 1;
  5758. }
  5759.  
  5760. int32_t LuaInterface::luaGetPlayerWeapon(lua_State* L)
  5761. {
  5762.     //getPlayerWeapon(cid[, ignoreAmmo = false])
  5763.     bool ignoreAmmo = false;
  5764.     if(lua_gettop(L) > 1)
  5765.         ignoreAmmo = popNumber(L);
  5766.  
  5767.     ScriptEnviroment* env = getEnv();
  5768.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  5769.     {
  5770.         if(Item* weapon = player->getWeapon(ignoreAmmo))
  5771.             pushThing(L, weapon, env->addThing(weapon));
  5772.         else
  5773.             pushThing(L, NULL, 0);
  5774.     }
  5775.     else
  5776.     {
  5777.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5778.         lua_pushnil(L);
  5779.     }
  5780.  
  5781.     return 1;
  5782. }
  5783.  
  5784. int32_t LuaInterface::luaGetPlayerItemById(lua_State* L)
  5785. {
  5786.     //getPlayerItemById(cid, deepSearch, itemId[, subType = -1])
  5787.     ScriptEnviroment* env = getEnv();
  5788.  
  5789.     int32_t subType = -1;
  5790.     if(lua_gettop(L) > 3)
  5791.         subType = (int32_t)popNumber(L);
  5792.  
  5793.     int32_t itemId = (int32_t)popNumber(L);
  5794.     bool deepSearch = popNumber(L);
  5795.  
  5796.     Player* player = env->getPlayerByUID(popNumber(L));
  5797.     if(!player)
  5798.     {
  5799.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  5800.         pushThing(L, NULL, 0);
  5801.         return 1;
  5802.     }
  5803.  
  5804.     Item* item = g_game.findItemOfType(player, itemId, deepSearch, subType);
  5805.     if(!item)
  5806.     {
  5807.         pushThing(L, NULL, 0);
  5808.         return 1;
  5809.     }
  5810.  
  5811.     pushThing(L, item, env->addThing(item));
  5812.     return 1;
  5813. }
  5814.  
  5815. int32_t LuaInterface::luaGetThing(lua_State* L)
  5816. {
  5817.     //getThing(uid)
  5818.     uint32_t uid = popNumber(L);
  5819.  
  5820.     ScriptEnviroment* env = getEnv();
  5821.     if(Thing* thing = env->getThingByUID(uid))
  5822.         pushThing(L, thing, uid);
  5823.     else
  5824.     {
  5825.         errorEx(getError(LUA_ERROR_THING_NOT_FOUND));
  5826.         pushThing(L, NULL, 0);
  5827.     }
  5828.  
  5829.     return 1;
  5830. }
  5831.  
  5832. int32_t LuaInterface::luaDoTileQueryAdd(lua_State* L)
  5833. {
  5834.     //doTileQueryAdd(uid, pos[, flags[, displayError = true]])
  5835.     uint32_t flags = 0, params = lua_gettop(L);
  5836.     bool displayError = true;
  5837.     if(params > 3)
  5838.         displayError = popNumber(L);
  5839.  
  5840.     if(params > 2)
  5841.         flags = popNumber(L);
  5842.  
  5843.     PositionEx pos;
  5844.     popPosition(L, pos);
  5845.     uint32_t uid = popNumber(L);
  5846.  
  5847.     ScriptEnviroment* env = getEnv();
  5848.     Tile* tile = g_game.getTile(pos);
  5849.     if(!tile)
  5850.     {
  5851.         if(displayError)
  5852.             errorEx(getError(LUA_ERROR_TILE_NOT_FOUND));
  5853.  
  5854.         lua_pushnumber(L, (uint32_t)RET_NOTPOSSIBLE);
  5855.         return 1;
  5856.     }
  5857.  
  5858.     Thing* thing = env->getThingByUID(uid);
  5859.     if(!thing)
  5860.     {
  5861.         if(displayError)
  5862.             errorEx(getError(LUA_ERROR_THING_NOT_FOUND));
  5863.  
  5864.         lua_pushnumber(L, (uint32_t)RET_NOTPOSSIBLE);
  5865.         return 1;
  5866.     }
  5867.  
  5868.     lua_pushnumber(L, (uint32_t)tile->__queryAdd(0, thing, 1, flags));
  5869.     return 1;
  5870. }
  5871.  
  5872. int32_t LuaInterface::luaDoItemRaidUnref(lua_State* L)
  5873. {
  5874.     //doItemRaidUnref(uid)
  5875.     ScriptEnviroment* env = getEnv();
  5876.     if(Item* item = env->getItemByUID(popNumber(L)))
  5877.     {
  5878.         if(Raid* raid = item->getRaid())
  5879.         {
  5880.             raid->unRef();
  5881.             item->setRaid(NULL);
  5882.             lua_pushboolean(L, true);
  5883.         }
  5884.         else
  5885.             lua_pushboolean(L, false);
  5886.     }
  5887.     else
  5888.     {
  5889.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  5890.         lua_pushboolean(L, false);
  5891.     }
  5892.  
  5893.     return 1;
  5894. }
  5895.  
  5896. int32_t LuaInterface::luaGetThingPosition(lua_State* L)
  5897. {
  5898.     //getThingPosition(uid)
  5899.     ScriptEnviroment* env = getEnv();
  5900.     if(Thing* thing = env->getThingByUID(popNumber(L)))
  5901.     {
  5902.         Position pos = thing->getPosition();
  5903.         uint32_t stackpos = 0;
  5904.         if(Tile* tile = thing->getTile())
  5905.             stackpos = tile->__getIndexOfThing(thing);
  5906.  
  5907.         pushPosition(L, pos, stackpos);
  5908.     }
  5909.     else
  5910.     {
  5911.         errorEx(getError(LUA_ERROR_THING_NOT_FOUND));
  5912.         lua_pushboolean(L, false);
  5913.     }
  5914.  
  5915.     return 1;
  5916. }
  5917.  
  5918. int32_t LuaInterface::luaCreateCombatObject(lua_State* L)
  5919. {
  5920.     //createCombatObject()
  5921.     ScriptEnviroment* env = getEnv();
  5922.     if(env->getScriptId() != EVENT_ID_LOADING)
  5923.     {
  5924.         errorEx("This function can only be used while loading the script.");
  5925.         lua_pushboolean(L, false);
  5926.         return 1;
  5927.     }
  5928.  
  5929.     Combat* combat = new Combat;
  5930.     if(!combat)
  5931.     {
  5932.         errorEx(getError(LUA_ERROR_COMBAT_NOT_FOUND));
  5933.         lua_pushboolean(L, false);
  5934.         return 1;
  5935.     }
  5936.  
  5937.     lua_pushnumber(L, env->addCombatObject(combat));
  5938.     return 1;
  5939. }
  5940.  
  5941. bool LuaInterface::getArea(lua_State* L, std::list<uint32_t>& list, uint32_t& rows)
  5942. {
  5943.     rows = 0;
  5944.     uint32_t i = 0;
  5945.  
  5946.     lua_pushnil(L);
  5947.     while(lua_next(L, -2))
  5948.     {
  5949.         lua_pushnil(L);
  5950.         while(lua_next(L, -2))
  5951.         {
  5952.             list.push_back((uint32_t)lua_tonumber(L, -1));
  5953.             lua_pop(L, 1); //removes value, keeps key for next iteration
  5954.             ++i;
  5955.         }
  5956.  
  5957.         lua_pop(L, 1); //removes value, keeps key for next iteration
  5958.         ++rows;
  5959.         i = 0;
  5960.     }
  5961.  
  5962.     lua_pop(L, 1);
  5963.     return rows;
  5964. }
  5965.  
  5966. int32_t LuaInterface::luaCreateCombatArea(lua_State* L)
  5967. {
  5968.     //createCombatArea( {area}[, {extArea}])
  5969.     ScriptEnviroment* env = getEnv();
  5970.     if(env->getScriptId() != EVENT_ID_LOADING)
  5971.     {
  5972.         errorEx("This function can only be used while loading the script.");
  5973.         lua_pushboolean(L, false);
  5974.         return 1;
  5975.     }
  5976.  
  5977.     CombatArea* area = new CombatArea;
  5978.     if(lua_gettop(L) > 1)
  5979.     {
  5980.         //has extra parameter with diagonal area information
  5981.         uint32_t rowsExtArea;
  5982.         std::list<uint32_t> listExtArea;
  5983.  
  5984.         getArea(L, listExtArea, rowsExtArea);
  5985.         /*setup all possible rotations*/
  5986.         area->setupExtArea(listExtArea, rowsExtArea);
  5987.     }
  5988.  
  5989.     if(lua_isnoneornil(L, -1)) //prevent crash
  5990.     {
  5991.         lua_pop(L, 2);
  5992.         lua_pushboolean(L, false);
  5993.         return 1;
  5994.     }
  5995.  
  5996.     uint32_t rowsArea = 0;
  5997.     std::list<uint32_t> listArea;
  5998.     getArea(L, listArea, rowsArea);
  5999.  
  6000.     area->setupArea(listArea, rowsArea);
  6001.     lua_pushnumber(L, env->addCombatArea(area));
  6002.     return 1;
  6003. }
  6004.  
  6005. int32_t LuaInterface::luaCreateConditionObject(lua_State* L)
  6006. {
  6007.     //createConditionObject(type[, ticks[, buff[, subId]]])
  6008.     uint32_t params = lua_gettop(L), subId = 0;
  6009.     if(params > 3)
  6010.         subId = popNumber(L);
  6011.  
  6012.     bool buff = false;
  6013.     if(params > 2)
  6014.         buff = popNumber(L);
  6015.  
  6016.     int32_t ticks = 0;
  6017.     if(params > 1)
  6018.         ticks = popNumber(L);
  6019.  
  6020.     ScriptEnviroment* env = getEnv();
  6021.     if(Condition* condition = Condition::createCondition(CONDITIONID_COMBAT, (ConditionType_t)popNumber(L), ticks, 0, buff, subId))
  6022.     {
  6023.         if(env->getScriptId() != EVENT_ID_LOADING)
  6024.             lua_pushnumber(L, env->addTempConditionObject(condition));
  6025.         else
  6026.             lua_pushnumber(L, env->addConditionObject(condition));
  6027.     }
  6028.     else
  6029.     {
  6030.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6031.         lua_pushboolean(L, false);
  6032.     }
  6033.  
  6034.     return 1;
  6035. }
  6036.  
  6037. int32_t LuaInterface::luaSetCombatArea(lua_State* L)
  6038. {
  6039.     //setCombatArea(combat, area)
  6040.     uint32_t areaId = popNumber(L);
  6041.     ScriptEnviroment* env = getEnv();
  6042.     if(env->getScriptId() != EVENT_ID_LOADING)
  6043.     {
  6044.         errorEx("This function can only be used while loading the script.");
  6045.         lua_pushboolean(L, false);
  6046.         return 1;
  6047.     }
  6048.  
  6049.     Combat* combat = env->getCombatObject(popNumber(L));
  6050.     if(!combat)
  6051.     {
  6052.         errorEx(getError(LUA_ERROR_COMBAT_NOT_FOUND));
  6053.         lua_pushboolean(L, false);
  6054.         return 1;
  6055.     }
  6056.  
  6057.     const CombatArea* area = env->getCombatArea(areaId);
  6058.     if(!area)
  6059.     {
  6060.         errorEx(getError(LUA_ERROR_AREA_NOT_FOUND));
  6061.         lua_pushboolean(L, false);
  6062.         return 1;
  6063.     }
  6064.  
  6065.     combat->setArea(new CombatArea(*area));
  6066.     lua_pushboolean(L, true);
  6067.     return 1;
  6068. }
  6069.  
  6070. int32_t LuaInterface::luaSetCombatCondition(lua_State* L)
  6071. {
  6072.     //setCombatCondition(combat, condition)
  6073.     uint32_t conditionId = popNumber(L);
  6074.     ScriptEnviroment* env = getEnv();
  6075.  
  6076.     Combat* combat = env->getCombatObject(popNumber(L));
  6077.     if(!combat)
  6078.     {
  6079.         errorEx(getError(LUA_ERROR_COMBAT_NOT_FOUND));
  6080.         lua_pushboolean(L, false);
  6081.         return 1;
  6082.     }
  6083.  
  6084.     const Condition* condition = env->getConditionObject(conditionId);
  6085.     if(!condition)
  6086.     {
  6087.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6088.         lua_pushboolean(L, false);
  6089.         return 1;
  6090.     }
  6091.  
  6092.     combat->setCondition(condition->clone());
  6093.     lua_pushboolean(L, true);
  6094.     return 1;
  6095. }
  6096.  
  6097. int32_t LuaInterface::luaSetCombatParam(lua_State* L)
  6098. {
  6099.     //setCombatParam(combat, key, value)
  6100.     uint32_t value = popNumber(L);
  6101.     CombatParam_t key = (CombatParam_t)popNumber(L);
  6102.  
  6103.     ScriptEnviroment* env = getEnv();
  6104.     if(env->getScriptId() != EVENT_ID_LOADING)
  6105.     {
  6106.         errorEx("This function can only be used while loading the script.");
  6107.         lua_pushboolean(L, false);
  6108.         return 1;
  6109.     }
  6110.  
  6111.     Combat* combat = env->getCombatObject(popNumber(L));
  6112.     if(!combat)
  6113.     {
  6114.         errorEx(getError(LUA_ERROR_COMBAT_NOT_FOUND));
  6115.         lua_pushboolean(L, false);
  6116.     }
  6117.     else
  6118.     {
  6119.         combat->setParam(key, value);
  6120.         lua_pushboolean(L, true);
  6121.     }
  6122.  
  6123.     return 1;
  6124. }
  6125.  
  6126. int32_t LuaInterface::luaSetConditionParam(lua_State* L)
  6127. {
  6128.     //setConditionParam(condition, key, value)
  6129.     int32_t value = popNumber(L);
  6130.     ScriptEnviroment* env = getEnv();
  6131.  
  6132.     ConditionParam_t key = (ConditionParam_t)popNumber(L);
  6133.     if(Condition* condition = env->getConditionObject(popNumber(L)))
  6134.     {
  6135.         condition->setParam(key, value);
  6136.         lua_pushboolean(L, true);
  6137.     }
  6138.     else
  6139.     {
  6140.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6141.         lua_pushboolean(L, false);
  6142.     }
  6143.  
  6144.     return 1;
  6145. }
  6146.  
  6147. int32_t LuaInterface::luaAddDamageCondition(lua_State* L)
  6148. {
  6149.     //addDamageCondition(condition, rounds, time, value)
  6150.     int32_t value = popNumber(L), time = popNumber(L), rounds = popNumber(L);
  6151.     ScriptEnviroment* env = getEnv();
  6152.     if(ConditionDamage* condition = dynamic_cast<ConditionDamage*>(env->getConditionObject(popNumber(L))))
  6153.     {
  6154.         condition->addDamage(rounds, time, value);
  6155.         lua_pushboolean(L, true);
  6156.     }
  6157.     else
  6158.     {
  6159.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6160.         lua_pushboolean(L, false);
  6161.     }
  6162.  
  6163.     return 1;
  6164. }
  6165.  
  6166. int32_t LuaInterface::luaAddOutfitCondition(lua_State* L)
  6167. {
  6168.     //addOutfitCondition(condition, outfit)
  6169.     Outfit_t outfit = popOutfit(L);
  6170.     ScriptEnviroment* env = getEnv();
  6171.     if(ConditionOutfit* condition = dynamic_cast<ConditionOutfit*>(env->getConditionObject(popNumber(L))))
  6172.     {
  6173.         condition->addOutfit(outfit);
  6174.         lua_pushboolean(L, true);
  6175.     }
  6176.     else
  6177.     {
  6178.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6179.         lua_pushboolean(L, false);
  6180.     }
  6181.  
  6182.     return 1;
  6183. }
  6184.  
  6185. int32_t LuaInterface::luaSetCombatCallBack(lua_State* L)
  6186. {
  6187.     //setCombatCallBack(combat, key, functionName)
  6188.     std::string function = popString(L);
  6189.     CallBackParam_t key = (CallBackParam_t)popNumber(L);
  6190.  
  6191.     ScriptEnviroment* env = getEnv();
  6192.     if(env->getScriptId() != EVENT_ID_LOADING)
  6193.     {
  6194.         errorEx("This function can only be used while loading the script.");
  6195.         lua_pushboolean(L, false);
  6196.         return 1;
  6197.     }
  6198.  
  6199.     Combat* combat = env->getCombatObject(popNumber(L));
  6200.     if(!combat)
  6201.     {
  6202.         errorEx(getError(LUA_ERROR_COMBAT_NOT_FOUND));
  6203.         lua_pushboolean(L, false);
  6204.         return 1;
  6205.     }
  6206.  
  6207.     LuaInterface* interface = env->getInterface();
  6208.     combat->setCallback(key);
  6209.  
  6210.     CallBack* callback = combat->getCallback(key);
  6211.     if(!callback)
  6212.     {
  6213.         std::stringstream ss;
  6214.         ss << key;
  6215.  
  6216.         errorEx(ss.str() + " is not a valid callback key.");
  6217.         lua_pushboolean(L, false);
  6218.         return 1;
  6219.     }
  6220.  
  6221.     if(!callback->loadCallBack(interface, function))
  6222.     {
  6223.         errorEx("Cannot load callback");
  6224.         lua_pushboolean(L, false);
  6225.     }
  6226.     else
  6227.         lua_pushboolean(L, true);
  6228.  
  6229.     return 1;
  6230. }
  6231.  
  6232. int32_t LuaInterface::luaSetCombatFormula(lua_State* L)
  6233. {
  6234.     //setCombatFormula(combat, type, mina, minb, maxa, maxb[, minl, maxl[, minm, maxm[, minc[, maxc]]]])
  6235.     ScriptEnviroment* env = getEnv();
  6236.     if(env->getScriptId() != EVENT_ID_LOADING)
  6237.     {
  6238.         errorEx("This function can only be used while loading the script.");
  6239.         lua_pushboolean(L, false);
  6240.         return 1;
  6241.     }
  6242.  
  6243.     int32_t params = lua_gettop(L), minc = 0, maxc = 0;
  6244.     if(params > 11)
  6245.         maxc = popNumber(L);
  6246.  
  6247.     if(params > 10)
  6248.         minc = popNumber(L);
  6249.  
  6250.     double minm = g_config.getDouble(ConfigManager::FORMULA_MAGIC), maxm = minm,
  6251.         minl = g_config.getDouble(ConfigManager::FORMULA_LEVEL), maxl = minl;
  6252.     if(params > 8)
  6253.     {
  6254.         maxm = popFloatNumber(L);
  6255.         minm = popFloatNumber(L);
  6256.     }
  6257.  
  6258.     if(params > 6)
  6259.     {
  6260.         maxl = popFloatNumber(L);
  6261.         minl = popFloatNumber(L);
  6262.     }
  6263.  
  6264.     double maxb = popFloatNumber(L), maxa = popFloatNumber(L),
  6265.         minb = popFloatNumber(L), mina = popFloatNumber(L);
  6266.     formulaType_t type = (formulaType_t)popNumber(L);
  6267.     if(Combat* combat = env->getCombatObject(popNumber(L)))
  6268.     {
  6269.         combat->setPlayerCombatValues(type, mina, minb, maxa, maxb, minl, maxl, minm, maxm, minc, maxc);
  6270.         lua_pushboolean(L, true);
  6271.     }
  6272.     else
  6273.     {
  6274.         errorEx(getError(LUA_ERROR_COMBAT_NOT_FOUND));
  6275.         lua_pushboolean(L, false);
  6276.     }
  6277.  
  6278.     return 1;
  6279. }
  6280.  
  6281. int32_t LuaInterface::luaSetConditionFormula(lua_State* L)
  6282. {
  6283.     //setConditionFormula(condition, mina, minb, maxa, maxb)
  6284.     double maxb = popFloatNumber(L), maxa = popFloatNumber(L),
  6285.         minb = popFloatNumber(L), mina = popFloatNumber(L);
  6286.     ScriptEnviroment* env = getEnv();
  6287.     if(ConditionSpeed* condition = dynamic_cast<ConditionSpeed*>(env->getConditionObject(popNumber(L))))
  6288.     {
  6289.         condition->setFormulaVars(mina, minb, maxa, maxb);
  6290.         lua_pushboolean(L, true);
  6291.     }
  6292.     else
  6293.     {
  6294.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6295.         lua_pushboolean(L, false);
  6296.     }
  6297.  
  6298.     return 1;
  6299. }
  6300.  
  6301. int32_t LuaInterface::luaDoCombat(lua_State* L)
  6302. {
  6303.     //doCombat(cid, combat, param)
  6304.     ScriptEnviroment* env = getEnv();
  6305.  
  6306.     LuaVariant var = popVariant(L);
  6307.     uint32_t combatId = popNumber(L), cid = popNumber(L);
  6308.  
  6309.     Creature* creature = NULL;
  6310.     if(cid != 0)
  6311.     {
  6312.         creature = env->getCreatureByUID(cid);
  6313.         if(!creature)
  6314.         {
  6315.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6316.             lua_pushboolean(L, false);
  6317.             return 1;
  6318.         }
  6319.     }
  6320.  
  6321.     const Combat* combat = env->getCombatObject(combatId);
  6322.     if(!combat)
  6323.     {
  6324.         errorEx(getError(LUA_ERROR_COMBAT_NOT_FOUND));
  6325.         lua_pushboolean(L, false);
  6326.         return 1;
  6327.     }
  6328.  
  6329.     if(var.type == VARIANT_NONE)
  6330.     {
  6331.         errorEx(getError(LUA_ERROR_VARIANT_NOT_FOUND));
  6332.         lua_pushboolean(L, false);
  6333.         return 1;
  6334.     }
  6335.  
  6336.     switch(var.type)
  6337.     {
  6338.         case VARIANT_NUMBER:
  6339.         {
  6340.             Creature* target = g_game.getCreatureByID(var.number);
  6341.             if(!target || !creature || !creature->canSeeCreature(target))
  6342.             {
  6343.                 lua_pushboolean(L, false);
  6344.                 return 1;
  6345.             }
  6346.  
  6347.             if(combat->hasArea())
  6348.                 combat->doCombat(creature, target->getPosition());
  6349.             else
  6350.                 combat->doCombat(creature, target);
  6351.  
  6352.             break;
  6353.         }
  6354.  
  6355.         case VARIANT_POSITION:
  6356.         {
  6357.             combat->doCombat(creature, var.pos);
  6358.             break;
  6359.         }
  6360.  
  6361.         case VARIANT_TARGETPOSITION:
  6362.         {
  6363.             if(!combat->hasArea())
  6364.             {
  6365.                 combat->postCombatEffects(creature, var.pos);
  6366.                 g_game.addMagicEffect(var.pos, MAGIC_EFFECT_POFF);
  6367.             }
  6368.             else
  6369.                 combat->doCombat(creature, var.pos);
  6370.  
  6371.             break;
  6372.         }
  6373.  
  6374.         case VARIANT_STRING:
  6375.         {
  6376.             Player* target = g_game.getPlayerByName(var.text);
  6377.             if(!target || !creature || !creature->canSeeCreature(target))
  6378.             {
  6379.                 lua_pushboolean(L, false);
  6380.                 return 1;
  6381.             }
  6382.  
  6383.             combat->doCombat(creature, target);
  6384.             break;
  6385.         }
  6386.  
  6387.         default:
  6388.         {
  6389.             errorEx(getError(LUA_ERROR_VARIANT_UNKNOWN));
  6390.             lua_pushboolean(L, false);
  6391.             return 1;
  6392.         }
  6393.     }
  6394.  
  6395.     lua_pushboolean(L, true);
  6396.     return 1;
  6397. }
  6398.  
  6399. int32_t LuaInterface::luaDoCombatAreaHealth(lua_State* L)
  6400. {
  6401.     //doCombatAreaHealth(cid, type, pos, area, min, max, effect)
  6402.     MagicEffect_t effect = (MagicEffect_t)popNumber(L);
  6403.     int32_t maxChange = (int32_t)popNumber(L), minChange = (int32_t)popNumber(L);
  6404.     uint32_t areaId = popNumber(L);
  6405.  
  6406.     PositionEx pos;
  6407.     popPosition(L, pos);
  6408.  
  6409.     CombatType_t combatType = (CombatType_t)popNumber(L);
  6410.     uint32_t cid = popNumber(L);
  6411.  
  6412.     ScriptEnviroment* env = getEnv();
  6413.     Creature* creature = NULL;
  6414.     if(cid)
  6415.     {
  6416.         if(!(creature = env->getCreatureByUID(cid)))
  6417.         {
  6418.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6419.             lua_pushboolean(L, false);
  6420.             return 1;
  6421.         }
  6422.     }
  6423.  
  6424.     const CombatArea* area = env->getCombatArea(areaId);
  6425.     if(area || !areaId)
  6426.     {
  6427.         CombatParams params;
  6428.         params.combatType = combatType;
  6429.         params.effects.impact = effect;
  6430.  
  6431.         Combat::doCombatHealth(creature, pos, area, minChange, maxChange, params);
  6432.         lua_pushboolean(L, true);
  6433.     }
  6434.     else
  6435.     {
  6436.         errorEx(getError(LUA_ERROR_AREA_NOT_FOUND));
  6437.         lua_pushboolean(L, false);
  6438.     }
  6439.  
  6440.     return 1;
  6441. }
  6442.  
  6443. int32_t LuaInterface::luaDoTargetCombatHealth(lua_State* L)
  6444. {
  6445.     //doTargetCombatHealth(cid, target, type, min, max, effect)
  6446.     MagicEffect_t effect = (MagicEffect_t)popNumber(L);
  6447.     int32_t maxChange = (int32_t)popNumber(L), minChange = (int32_t)popNumber(L);
  6448.  
  6449.     CombatType_t combatType = (CombatType_t)popNumber(L);
  6450.     uint32_t targetCid = popNumber(L), cid = popNumber(L);
  6451.  
  6452.     ScriptEnviroment* env = getEnv();
  6453.     Creature* creature = NULL;
  6454.     if(cid)
  6455.     {
  6456.         if(!(creature = env->getCreatureByUID(cid)))
  6457.         {
  6458.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6459.             lua_pushboolean(L, false);
  6460.             return 1;
  6461.         }
  6462.     }
  6463.  
  6464.     Creature* target = env->getCreatureByUID(targetCid);
  6465.     if(target)
  6466.     {
  6467.         CombatParams params;
  6468.         params.combatType = combatType;
  6469.         params.effects.impact = effect;
  6470.  
  6471.         Combat::doCombatHealth(creature, target, minChange, maxChange, params);
  6472.         lua_pushboolean(L, true);
  6473.     }
  6474.     else
  6475.     {
  6476.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6477.         lua_pushboolean(L, false);
  6478.     }
  6479.  
  6480.     return 1;
  6481. }
  6482.  
  6483. int32_t LuaInterface::luaDoCombatAreaMana(lua_State* L)
  6484. {
  6485.     //doCombatAreaMana(cid, pos, area, min, max, effect)
  6486.     MagicEffect_t effect = (MagicEffect_t)popNumber(L);
  6487.     int32_t maxChange = (int32_t)popNumber(L), minChange = (int32_t)popNumber(L);
  6488.     uint32_t areaId = popNumber(L);
  6489.  
  6490.     PositionEx pos;
  6491.     popPosition(L, pos);
  6492.     uint32_t cid = popNumber(L);
  6493.  
  6494.     ScriptEnviroment* env = getEnv();
  6495.     Creature* creature = NULL;
  6496.     if(cid)
  6497.     {
  6498.         if(!(creature = env->getCreatureByUID(cid)))
  6499.         {
  6500.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6501.             lua_pushboolean(L, false);
  6502.             return 1;
  6503.         }
  6504.     }
  6505.  
  6506.     const CombatArea* area = env->getCombatArea(areaId);
  6507.     if(area || !areaId)
  6508.     {
  6509.         CombatParams params;
  6510.         params.effects.impact = effect;
  6511.  
  6512.         Combat::doCombatMana(creature, pos, area, minChange, maxChange, params);
  6513.         lua_pushboolean(L, true);
  6514.     }
  6515.     else
  6516.     {
  6517.         errorEx(getError(LUA_ERROR_AREA_NOT_FOUND));
  6518.         lua_pushboolean(L, false);
  6519.     }
  6520.  
  6521.     return 1;
  6522. }
  6523.  
  6524. int32_t LuaInterface::luaDoTargetCombatMana(lua_State* L)
  6525. {
  6526.     //doTargetCombatMana(cid, target, min, max, effect)
  6527.     MagicEffect_t effect = (MagicEffect_t)popNumber(L);
  6528.     int32_t maxChange = (int32_t)popNumber(L), minChange = (int32_t)popNumber(L);
  6529.     uint32_t targetCid = popNumber(L), cid = popNumber(L);
  6530.  
  6531.     ScriptEnviroment* env = getEnv();
  6532.     Creature* creature = NULL;
  6533.     if(cid)
  6534.     {
  6535.         if(!(creature = env->getCreatureByUID(cid)))
  6536.         {
  6537.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6538.             lua_pushboolean(L, false);
  6539.             return 1;
  6540.         }
  6541.     }
  6542.  
  6543.     if(Creature* target = env->getCreatureByUID(targetCid))
  6544.     {
  6545.         CombatParams params;
  6546.         params.effects.impact = effect;
  6547.  
  6548.         Combat::doCombatMana(creature, target, minChange, maxChange, params);
  6549.         lua_pushboolean(L, true);
  6550.     }
  6551.     else
  6552.     {
  6553.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6554.         lua_pushboolean(L, false);
  6555.     }
  6556.  
  6557.     return 1;
  6558. }
  6559.  
  6560. int32_t LuaInterface::luaDoCombatAreaCondition(lua_State* L)
  6561. {
  6562.     //doCombatAreaCondition(cid, pos, area, condition, effect)
  6563.     MagicEffect_t effect = (MagicEffect_t)popNumber(L);
  6564.     uint32_t conditionId = popNumber(L), areaId = popNumber(L);
  6565.  
  6566.     PositionEx pos;
  6567.     popPosition(L, pos);
  6568.     uint32_t cid = popNumber(L);
  6569.  
  6570.     ScriptEnviroment* env = getEnv();
  6571.     Creature* creature = NULL;
  6572.     if(cid)
  6573.     {
  6574.         if(!(creature = env->getCreatureByUID(cid)))
  6575.         {
  6576.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6577.             lua_pushboolean(L, false);
  6578.             return 1;
  6579.         }
  6580.     }
  6581.  
  6582.     if(const Condition* condition = env->getConditionObject(conditionId))
  6583.     {
  6584.         const CombatArea* area = env->getCombatArea(areaId);
  6585.         if(area || !areaId)
  6586.         {
  6587.             CombatParams params;
  6588.             params.effects.impact = effect;
  6589.             params.conditionList.push_back(condition);
  6590.  
  6591.             Combat::doCombatCondition(creature, pos, area, params);
  6592.             lua_pushboolean(L, true);
  6593.         }
  6594.         else
  6595.         {
  6596.             errorEx(getError(LUA_ERROR_AREA_NOT_FOUND));
  6597.             lua_pushboolean(L, false);
  6598.         }
  6599.     }
  6600.     else
  6601.     {
  6602.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6603.         lua_pushboolean(L, false);
  6604.     }
  6605.  
  6606.     return 1;
  6607. }
  6608.  
  6609. int32_t LuaInterface::luaDoTargetCombatCondition(lua_State* L)
  6610. {
  6611.     //doTargetCombatCondition(cid, target, condition, effect)
  6612.     MagicEffect_t effect = (MagicEffect_t)popNumber(L);
  6613.     uint32_t conditionId = popNumber(L), targetCid = popNumber(L), cid = popNumber(L);
  6614.  
  6615.     ScriptEnviroment* env = getEnv();
  6616.     Creature* creature = NULL;
  6617.     if(cid)
  6618.     {
  6619.         if(!(creature = env->getCreatureByUID(cid)))
  6620.         {
  6621.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6622.             lua_pushboolean(L, false);
  6623.             return 1;
  6624.         }
  6625.     }
  6626.  
  6627.     if(Creature* target = env->getCreatureByUID(targetCid))
  6628.     {
  6629.         if(const Condition* condition = env->getConditionObject(conditionId))
  6630.         {
  6631.             CombatParams params;
  6632.             params.effects.impact = effect;
  6633.             params.conditionList.push_back(condition);
  6634.  
  6635.             Combat::doCombatCondition(creature, target, params);
  6636.             lua_pushboolean(L, true);
  6637.         }
  6638.         else
  6639.         {
  6640.             errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  6641.             lua_pushboolean(L, false);
  6642.         }
  6643.     }
  6644.     else
  6645.     {
  6646.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6647.         lua_pushboolean(L, false);
  6648.     }
  6649.  
  6650.     return 1;
  6651. }
  6652.  
  6653. int32_t LuaInterface::luaDoCombatAreaDispel(lua_State* L)
  6654. {
  6655.     //doCombatAreaDispel(cid, pos, area, type, effect)
  6656.     MagicEffect_t effect = (MagicEffect_t)popNumber(L);
  6657.     ConditionType_t dispelType = (ConditionType_t)popNumber(L);
  6658.     uint32_t areaId = popNumber(L);
  6659.  
  6660.     PositionEx pos;
  6661.     popPosition(L, pos);
  6662.     uint32_t cid = popNumber(L);
  6663.  
  6664.     ScriptEnviroment* env = getEnv();
  6665.     Creature* creature = NULL;
  6666.     if(cid)
  6667.     {
  6668.         if(!(creature = env->getCreatureByUID(cid)))
  6669.         {
  6670.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6671.             lua_pushboolean(L, false);
  6672.             return 1;
  6673.         }
  6674.     }
  6675.  
  6676.     const CombatArea* area = env->getCombatArea(areaId);
  6677.     if(area || !areaId)
  6678.     {
  6679.         CombatParams params;
  6680.         params.effects.impact = effect;
  6681.         params.dispelType = dispelType;
  6682.  
  6683.         Combat::doCombatDispel(creature, pos, area, params);
  6684.         lua_pushboolean(L, true);
  6685.     }
  6686.     else
  6687.     {
  6688.         errorEx(getError(LUA_ERROR_AREA_NOT_FOUND));
  6689.         lua_pushboolean(L, false);
  6690.     }
  6691.  
  6692.     return 1;
  6693. }
  6694.  
  6695. int32_t LuaInterface::luaDoTargetCombatDispel(lua_State* L)
  6696. {
  6697.     //doTargetCombatDispel(cid, target, type, effect)
  6698.     MagicEffect_t effect = (MagicEffect_t)popNumber(L);
  6699.     ConditionType_t dispelType = (ConditionType_t)popNumber(L);
  6700.     uint32_t targetCid = popNumber(L), cid = popNumber(L);
  6701.  
  6702.     ScriptEnviroment* env = getEnv();
  6703.     Creature* creature = NULL;
  6704.     if(cid)
  6705.     {
  6706.         if(!(creature = env->getCreatureByUID(cid)))
  6707.         {
  6708.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6709.             lua_pushboolean(L, false);
  6710.             return 1;
  6711.         }
  6712.     }
  6713.  
  6714.     if(Creature* target = env->getCreatureByUID(targetCid))
  6715.     {
  6716.         CombatParams params;
  6717.         params.effects.impact = effect;
  6718.         params.dispelType = dispelType;
  6719.  
  6720.         Combat::doCombatDispel(creature, target, params);
  6721.         lua_pushboolean(L, true);
  6722.     }
  6723.     else
  6724.     {
  6725.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6726.         lua_pushboolean(L, false);
  6727.     }
  6728.  
  6729.     return 1;
  6730. }
  6731.  
  6732. int32_t LuaInterface::luaDoChallengeCreature(lua_State* L)
  6733. {
  6734.     //doChallengeCreature(cid, target)
  6735.     ScriptEnviroment* env = getEnv();
  6736.     uint32_t targetCid = popNumber(L);
  6737.  
  6738.     Creature* creature = env->getCreatureByUID(popNumber(L));
  6739.     if(!creature)
  6740.     {
  6741.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6742.         lua_pushboolean(L, false);
  6743.         return 1;
  6744.     }
  6745.  
  6746.     Creature* target = env->getCreatureByUID(targetCid);
  6747.     if(!target)
  6748.     {
  6749.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6750.         lua_pushboolean(L, false);
  6751.         return 1;
  6752.     }
  6753.  
  6754.     target->challengeCreature(creature);
  6755.     lua_pushboolean(L, true);
  6756.     return 1;
  6757. }
  6758.  
  6759. int32_t LuaInterface::luaDoSummonMonster(lua_State* L)
  6760. {
  6761.     //doSummonMonster(cid, name)
  6762.     std::string name = popString(L);
  6763.  
  6764.     ScriptEnviroment* env = getEnv();
  6765.     Creature* creature = env->getCreatureByUID(popNumber(L));
  6766.     if(!creature)
  6767.     {
  6768.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6769.         lua_pushboolean(L, false);
  6770.         return 1;
  6771.     }
  6772.  
  6773.     lua_pushnumber(L, g_game.placeSummon(creature, name));
  6774.     return 1;
  6775. }
  6776.  
  6777. int32_t LuaInterface::luaDoConvinceCreature(lua_State* L)
  6778. {
  6779.     //doConvinceCreature(cid, target)
  6780.     uint32_t cid = popNumber(L);
  6781.  
  6782.     ScriptEnviroment* env = getEnv();
  6783.     Creature* creature = env->getCreatureByUID(popNumber(L));
  6784.     if(!creature)
  6785.     {
  6786.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6787.         lua_pushboolean(L, false);
  6788.         return 1;
  6789.     }
  6790.  
  6791.     Creature* target = env->getCreatureByUID(cid);
  6792.     if(!target)
  6793.     {
  6794.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6795.         lua_pushboolean(L, false);
  6796.         return 1;
  6797.     }
  6798.  
  6799.     target->convinceCreature(creature);
  6800.     lua_pushboolean(L, true);
  6801.     return 1;
  6802. }
  6803.  
  6804. int32_t LuaInterface::luaGetMonsterTargetList(lua_State* L)
  6805. {
  6806.     //getMonsterTargetList(cid)
  6807.     ScriptEnviroment* env = getEnv();
  6808.     Creature* creature = env->getCreatureByUID(popNumber(L));
  6809.     if(!creature)
  6810.     {
  6811.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6812.         lua_pushboolean(L, false);
  6813.         return 1;
  6814.     }
  6815.  
  6816.     Monster* monster = creature->getMonster();
  6817.     if(!monster)
  6818.     {
  6819.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6820.         lua_pushboolean(L, false);
  6821.         return 1;
  6822.     }
  6823.  
  6824.     const CreatureList& targetList = monster->getTargetList();
  6825.     CreatureList::const_iterator it = targetList.begin();
  6826.  
  6827.     lua_newtable(L);
  6828.     for(uint32_t i = 1; it != targetList.end(); ++it, ++i)
  6829.     {
  6830.         if(monster->isTarget(*it))
  6831.         {
  6832.             lua_pushnumber(L, i);
  6833.             lua_pushnumber(L, env->addThing(*it));
  6834.             pushTable(L);
  6835.         }
  6836.     }
  6837.  
  6838.     return 1;
  6839. }
  6840.  
  6841. int32_t LuaInterface::luaGetMonsterFriendList(lua_State* L)
  6842. {
  6843.     //getMonsterFriendList(cid)
  6844.     ScriptEnviroment* env = getEnv();
  6845.     Creature* creature = env->getCreatureByUID(popNumber(L));
  6846.     if(!creature)
  6847.     {
  6848.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6849.         lua_pushboolean(L, false);
  6850.         return 1;
  6851.     }
  6852.  
  6853.     Monster* monster = creature->getMonster();
  6854.     if(!monster)
  6855.     {
  6856.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6857.         lua_pushboolean(L, false);
  6858.         return 1;
  6859.     }
  6860.  
  6861.     Creature* friendCreature;
  6862.     const CreatureList& friendList = monster->getFriendList();
  6863.     CreatureList::const_iterator it = friendList.begin();
  6864.  
  6865.     lua_newtable(L);
  6866.     for(uint32_t i = 1; it != friendList.end(); ++it, ++i)
  6867.     {
  6868.         friendCreature = (*it);
  6869.         if(!friendCreature->isRemoved() && friendCreature->getPosition().z == monster->getPosition().z)
  6870.         {
  6871.             lua_pushnumber(L, i);
  6872.             lua_pushnumber(L, env->addThing(*it));
  6873.             pushTable(L);
  6874.         }
  6875.     }
  6876.  
  6877.     return 1;
  6878. }
  6879.  
  6880. int32_t LuaInterface::luaDoMonsterSetTarget(lua_State* L)
  6881. {
  6882.     //doMonsterSetTarget(cid, target)
  6883.     uint32_t targetId = popNumber(L);
  6884.     ScriptEnviroment* env = getEnv();
  6885.  
  6886.     Creature* creature = env->getCreatureByUID(popNumber(L));
  6887.     if(!creature)
  6888.     {
  6889.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6890.         lua_pushboolean(L, false);
  6891.         return 1;
  6892.     }
  6893.  
  6894.     Monster* monster = creature->getMonster();
  6895.     if(!monster)
  6896.     {
  6897.         errorEx(getError(LUA_ERROR_MONSTER_NOT_FOUND));
  6898.         lua_pushboolean(L, false);
  6899.         return 1;
  6900.     }
  6901.  
  6902.     Creature* target = env->getCreatureByUID(targetId);
  6903.     if(!target)
  6904.     {
  6905.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6906.         lua_pushboolean(L, false);
  6907.         return 1;
  6908.     }
  6909.  
  6910.     if(!monster->isSummon())
  6911.         lua_pushboolean(L, monster->selectTarget(target));
  6912.     else
  6913.         lua_pushboolean(L, false);
  6914.  
  6915.     return 1;
  6916. }
  6917.  
  6918. int32_t LuaInterface::luaDoMonsterChangeTarget(lua_State* L)
  6919. {
  6920.     //doMonsterChangeTarget(cid)
  6921.     ScriptEnviroment* env = getEnv();
  6922.     Creature* creature = env->getCreatureByUID(popNumber(L));
  6923.     if(!creature)
  6924.     {
  6925.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6926.         lua_pushboolean(L, false);
  6927.         return 1;
  6928.     }
  6929.  
  6930.     Monster* monster = creature->getMonster();
  6931.     if(!monster)
  6932.     {
  6933.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  6934.         lua_pushboolean(L, false);
  6935.         return 1;
  6936.     }
  6937.  
  6938.     if(!monster->isSummon())
  6939.         monster->searchTarget(TARGETSEARCH_RANDOM);
  6940.  
  6941.     lua_pushboolean(L, true);
  6942.     return 1;
  6943. }
  6944.  
  6945. int32_t LuaInterface::luaGetMonsterInfo(lua_State* L)
  6946. {
  6947.     //getMonsterInfo(name)
  6948.     const MonsterType* mType = g_monsters.getMonsterType(popString(L));
  6949.     if(!mType)
  6950.     {
  6951.         errorEx(getError(LUA_ERROR_MONSTER_NOT_FOUND));
  6952.         lua_pushboolean(L, false);
  6953.         return 1;
  6954.     }
  6955.  
  6956.     lua_newtable(L);
  6957.     setField(L, "name", mType->name.c_str());
  6958.     setField(L, "description", mType->nameDescription.c_str());
  6959.     setField(L, "experience", mType->experience);
  6960.     setField(L, "health", mType->health);
  6961.     setField(L, "healthMax", mType->healthMax);
  6962.     setField(L, "manaCost", mType->manaCost);
  6963.     setField(L, "defense", mType->defense);
  6964.     setField(L, "armor", mType->armor);
  6965.     setField(L, "baseSpeed", mType->baseSpeed);
  6966.     setField(L, "lookCorpse", mType->lookCorpse);
  6967.     setField(L, "race", mType->race);
  6968.     setField(L, "skull", mType->skull);
  6969.     setField(L, "partyShield", mType->partyShield);
  6970.     setField(L, "guildEmblem", mType->guildEmblem);
  6971.     setFieldBool(L, "summonable", mType->isSummonable);
  6972.     setFieldBool(L, "illusionable", mType->isIllusionable);
  6973.     setFieldBool(L, "convinceable", mType->isConvinceable);
  6974.     setFieldBool(L, "attackable", mType->isAttackable);
  6975.     setFieldBool(L, "hostile", mType->isHostile);
  6976.  
  6977.     lua_pushstring(L, "outfit"); // name the table created by pushOutfit
  6978.     pushOutfit(L, mType->outfit);
  6979.     pushTable(L);
  6980.     createTable(L, "defenses");
  6981.  
  6982.     SpellList::const_iterator it = mType->spellDefenseList.begin();
  6983.     for(uint32_t i = 1; it != mType->spellDefenseList.end(); ++it, ++i)
  6984.     {
  6985.         createTable(L, i);
  6986.         setField(L, "speed", it->speed);
  6987.         setField(L, "chance", it->chance);
  6988.         setField(L, "range", it->range);
  6989.  
  6990.         setField(L, "minCombatValue", it->minCombatValue);
  6991.         setField(L, "maxCombatValue", it->maxCombatValue);
  6992.         setFieldBool(L, "isMelee", it->isMelee);
  6993.         pushTable(L);
  6994.     }
  6995.  
  6996.     pushTable(L);
  6997.     createTable(L, "attacks");
  6998.  
  6999.     it = mType->spellAttackList.begin();
  7000.     for(uint32_t i = 1; it != mType->spellAttackList.end(); ++it, ++i)
  7001.     {
  7002.         createTable(L, i);
  7003.         setField(L, "speed", it->speed);
  7004.         setField(L, "chance", it->chance);
  7005.         setField(L, "range", it->range);
  7006.  
  7007.         setField(L, "minCombatValue", it->minCombatValue);
  7008.         setField(L, "maxCombatValue", it->maxCombatValue);
  7009.         setFieldBool(L, "isMelee", it->isMelee);
  7010.         pushTable(L);
  7011.     }
  7012.  
  7013.     pushTable(L);
  7014.     createTable(L, "loot");
  7015.  
  7016.     LootItems::const_iterator lit = mType->lootItems.begin();
  7017.     for(uint32_t i = 1; lit != mType->lootItems.end(); ++lit, ++i)
  7018.     {
  7019.         createTable(L, i);
  7020.         if(lit->ids.size() > 1)
  7021.         {
  7022.             createTable(L, "ids");
  7023.             std::vector<uint16_t>::const_iterator iit = lit->ids.begin();
  7024.             for(uint32_t j = 1; iit != lit->ids.end(); ++iit, ++j)
  7025.             {
  7026.                 lua_pushnumber(L, j);
  7027.                 lua_pushnumber(L, (*iit));
  7028.                 pushTable(L);
  7029.             }
  7030.  
  7031.             pushTable(L);
  7032.         }
  7033.         else
  7034.             setField(L, "id", lit->ids[0]);
  7035.  
  7036.         setField(L, "count", lit->count);
  7037.         setField(L, "chance", lit->chance);
  7038.         setField(L, "subType", lit->subType);
  7039.         setField(L, "actionId", lit->actionId);
  7040.         setField(L, "uniqueId", lit->uniqueId);
  7041.         setField(L, "text", lit->text);
  7042.  
  7043.         if(lit->childLoot.size() > 0)
  7044.         {
  7045.             createTable(L, "child");
  7046.             LootItems::const_iterator cit = lit->childLoot.begin();
  7047.             for(uint32_t j = 1; cit != lit->childLoot.end(); ++cit, ++j)
  7048.             {
  7049.                 createTable(L, j);
  7050.                 if(cit->ids.size() > 1)
  7051.                 {
  7052.                     createTable(L, "ids");
  7053.                     std::vector<uint16_t>::const_iterator iit = cit->ids.begin();
  7054.                     for(uint32_t k = 1; iit != cit->ids.end(); ++iit, ++k)
  7055.                     {
  7056.                         lua_pushnumber(L, k);
  7057.                         lua_pushnumber(L, (*iit));
  7058.                         pushTable(L);
  7059.                     }
  7060.  
  7061.                     pushTable(L);
  7062.                 }
  7063.                 else
  7064.                     setField(L, "id", cit->ids[0]);
  7065.  
  7066.                 setField(L, "count", cit->count);
  7067.                 setField(L, "chance", cit->chance);
  7068.                 setField(L, "subType", cit->subType);
  7069.                 setField(L, "actionId", cit->actionId);
  7070.                 setField(L, "uniqueId", cit->uniqueId);
  7071.                 setField(L, "text", cit->text);
  7072.  
  7073.                 pushTable(L);
  7074.             }
  7075.  
  7076.             pushTable(L);
  7077.         }
  7078.  
  7079.         pushTable(L);
  7080.     }
  7081.  
  7082.     pushTable(L);
  7083.     createTable(L, "summons");
  7084.  
  7085.     SummonList::const_iterator sit = mType->summonList.begin();
  7086.     for(uint32_t i = 1; sit != mType->summonList.end(); ++sit, ++i)
  7087.     {
  7088.         createTable(L, i);
  7089.         setField(L, "name", sit->name);
  7090.         setField(L, "chance", sit->chance);
  7091.  
  7092.         setField(L, "interval", sit->interval);
  7093.         setField(L, "amount", sit->amount);
  7094.         pushTable(L);
  7095.     }
  7096.  
  7097.     pushTable(L);
  7098.     return 1;
  7099. }
  7100.  
  7101. int32_t LuaInterface::luaGetTalkActionList(lua_State* L)
  7102. {
  7103.     //getTalkactionList()
  7104.     lua_newtable(L);
  7105.  
  7106.     TalkActionsMap::const_iterator it = g_talkActions->getFirstTalk();
  7107.     for(uint32_t i = 1; it != g_talkActions->getLastTalk(); ++it, ++i)
  7108.     {
  7109.         createTable(L, i);
  7110.         setField(L, "words", it->first);
  7111.         setField(L, "access", it->second->getAccess());
  7112.  
  7113.         setFieldBool(L, "log", it->second->isLogged());
  7114.         setFieldBool(L, "logged", it->second->isLogged());
  7115.         setFieldBool(L, "hide", it->second->isHidden());
  7116.         setFieldBool(L, "hidden", it->second->isHidden());
  7117.  
  7118.         setField(L, "functionName", it->second->getFunctionName());
  7119.         setField(L, "channel", it->second->getChannel());
  7120.         pushTable(L);
  7121.     }
  7122.  
  7123.     return 1;
  7124. }
  7125.  
  7126. int32_t LuaInterface::luaGetExperienceStageList(lua_State* L)
  7127. {
  7128.     //getExperienceStageList()
  7129.     if(!g_config.getBool(ConfigManager::EXPERIENCE_STAGES))
  7130.     {
  7131.         lua_pushboolean(L, false);
  7132.         return true;
  7133.     }
  7134.  
  7135.     StageList::const_iterator it = g_game.getFirstStage();
  7136.     lua_newtable(L);
  7137.     for(uint32_t i = 1; it != g_game.getLastStage(); ++it, ++i)
  7138.     {
  7139.         createTable(L, i);
  7140.         setField(L, "level", it->first);
  7141.         setFieldFloat(L, "multiplier", it->second);
  7142.         pushTable(L);
  7143.     }
  7144.  
  7145.     return 1;
  7146. }
  7147.  
  7148. int32_t LuaInterface::luaDoAddCondition(lua_State* L)
  7149. {
  7150.     //doAddCondition(cid, condition)
  7151.     uint32_t conditionId = popNumber(L);
  7152.  
  7153.     ScriptEnviroment* env = getEnv();
  7154.     Creature* creature = env->getCreatureByUID(popNumber(L));
  7155.     if(!creature)
  7156.     {
  7157.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7158.         lua_pushboolean(L, false);
  7159.         return 1;
  7160.     }
  7161.  
  7162.     Condition* condition = env->getConditionObject(conditionId);
  7163.     if(!condition)
  7164.     {
  7165.         errorEx(getError(LUA_ERROR_CONDITION_NOT_FOUND));
  7166.         lua_pushboolean(L, false);
  7167.         return 1;
  7168.     }
  7169.  
  7170.     creature->addCondition(condition->clone());
  7171.     lua_pushboolean(L, true);
  7172.     return 1;
  7173. }
  7174.  
  7175. int32_t LuaInterface::luaDoRemoveCondition(lua_State* L)
  7176. {
  7177.     //doRemoveCondition(cid, type[, subId])
  7178.     uint32_t subId = 0;
  7179.     if(lua_gettop(L) > 2)
  7180.         subId = popNumber(L);
  7181.  
  7182.     ConditionType_t conditionType = (ConditionType_t)popNumber(L);
  7183.  
  7184.     ScriptEnviroment* env = getEnv();
  7185.     Creature* creature = env->getCreatureByUID(popNumber(L));
  7186.     if(!creature)
  7187.     {
  7188.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7189.         lua_pushboolean(L, false);
  7190.         return 1;
  7191.     }
  7192.  
  7193.     Condition* condition = NULL;
  7194.     while((condition = creature->getCondition(conditionType, CONDITIONID_COMBAT, subId)))
  7195.         creature->removeCondition(condition);
  7196.  
  7197.     while((condition = creature->getCondition(conditionType, CONDITIONID_DEFAULT, subId)))
  7198.         creature->removeCondition(condition);
  7199.  
  7200.     lua_pushboolean(L, true);
  7201.     return 1;
  7202. }
  7203.  
  7204. int32_t LuaInterface::luaDoRemoveConditions(lua_State* L)
  7205. {
  7206.     //doRemoveConditions(cid[, onlyPersistent])
  7207.     bool onlyPersistent = true;
  7208.     if(lua_gettop(L) > 1)
  7209.         onlyPersistent = popNumber(L);
  7210.  
  7211.     ScriptEnviroment* env = getEnv();
  7212.     Creature* creature = env->getCreatureByUID(popNumber(L));
  7213.     if(!creature)
  7214.     {
  7215.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7216.         lua_pushboolean(L, false);
  7217.         return 1;
  7218.     }
  7219.  
  7220.     creature->removeConditions(CONDITIONEND_ABORT, onlyPersistent);
  7221.     lua_pushboolean(L, true);
  7222.     return 1;
  7223. }
  7224.  
  7225. int32_t LuaInterface::luaNumberToVariant(lua_State* L)
  7226. {
  7227.     //numberToVariant(number)
  7228.     LuaVariant var;
  7229.     var.type = VARIANT_NUMBER;
  7230.     var.number = popNumber(L);
  7231.  
  7232.     LuaInterface::pushVariant(L, var);
  7233.     return 1;
  7234. }
  7235.  
  7236. int32_t LuaInterface::luaStringToVariant(lua_State* L)
  7237. {
  7238.     //stringToVariant(string)
  7239.     LuaVariant var;
  7240.     var.type = VARIANT_STRING;
  7241.     var.text = popString(L);
  7242.  
  7243.     LuaInterface::pushVariant(L, var);
  7244.     return 1;
  7245. }
  7246.  
  7247. int32_t LuaInterface::luaPositionToVariant(lua_State* L)
  7248. {
  7249.     //positionToVariant(pos)
  7250.     LuaVariant var;
  7251.     var.type = VARIANT_POSITION;
  7252.     popPosition(L, var.pos);
  7253.  
  7254.     LuaInterface::pushVariant(L, var);
  7255.     return 1;
  7256. }
  7257.  
  7258. int32_t LuaInterface::luaTargetPositionToVariant(lua_State* L)
  7259. {
  7260.     //targetPositionToVariant(pos)
  7261.     LuaVariant var;
  7262.     var.type = VARIANT_TARGETPOSITION;
  7263.     popPosition(L, var.pos);
  7264.  
  7265.     LuaInterface::pushVariant(L, var);
  7266.     return 1;
  7267. }
  7268.  
  7269. int32_t LuaInterface::luaVariantToNumber(lua_State* L)
  7270. {
  7271.     //variantToNumber(var)
  7272.     LuaVariant var = popVariant(L);
  7273.  
  7274.     uint32_t number = 0;
  7275.     if(var.type == VARIANT_NUMBER)
  7276.         number = var.number;
  7277.  
  7278.     lua_pushnumber(L, number);
  7279.     return 1;
  7280. }
  7281.  
  7282. int32_t LuaInterface::luaVariantToString(lua_State* L)
  7283. {
  7284.     //variantToString(var)
  7285.     LuaVariant var = popVariant(L);
  7286.  
  7287.     std::string text = "";
  7288.     if(var.type == VARIANT_STRING)
  7289.         text = var.text;
  7290.  
  7291.     lua_pushstring(L, text.c_str());
  7292.     return 1;
  7293. }
  7294.  
  7295. int32_t LuaInterface::luaVariantToPosition(lua_State* L)
  7296. {
  7297.     //luaVariantToPosition(var)
  7298.     LuaVariant var = popVariant(L);
  7299.  
  7300.     PositionEx pos(0, 0, 0, 0);
  7301.     if(var.type == VARIANT_POSITION || var.type == VARIANT_TARGETPOSITION)
  7302.         pos = var.pos;
  7303.  
  7304.     pushPosition(L, pos, pos.stackpos);
  7305.     return 1;
  7306. }
  7307.  
  7308. int32_t LuaInterface::luaDoChangeSpeed(lua_State* L)
  7309. {
  7310.     //doChangeSpeed(cid, delta)
  7311.     int32_t delta = (int32_t)popNumber(L);
  7312.  
  7313.     ScriptEnviroment* env = getEnv();
  7314.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  7315.     {
  7316.         g_game.changeSpeed(creature, delta);
  7317.         lua_pushboolean(L, true);
  7318.     }
  7319.     else
  7320.     {
  7321.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7322.         lua_pushboolean(L, false);
  7323.     }
  7324.  
  7325.     return 1;
  7326. }
  7327.  
  7328. int32_t LuaInterface::luaSetCreatureOutfit(lua_State* L)
  7329. {
  7330.     //doSetCreatureOutfit(cid, outfit[, time = -1])
  7331.     int32_t time = -1;
  7332.     if(lua_gettop(L) > 2)
  7333.         time = (int32_t)popNumber(L);
  7334.  
  7335.     Outfit_t outfit = popOutfit(L);
  7336.     ScriptEnviroment* env = getEnv();
  7337.  
  7338.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  7339.         lua_pushboolean(L, Spell::CreateIllusion(creature, outfit, time) == RET_NOERROR);
  7340.     else
  7341.     {
  7342.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7343.         lua_pushboolean(L, false);
  7344.     }
  7345.  
  7346.     return 1;
  7347. }
  7348.  
  7349. int32_t LuaInterface::luaGetCreatureOutfit(lua_State* L)
  7350. {
  7351.     //getCreatureOutfit(cid)
  7352.     ScriptEnviroment* env = getEnv();
  7353.     if(const Creature* creature = env->getCreatureByUID(popNumber(L)))
  7354.         pushOutfit(L, creature->getCurrentOutfit());
  7355.     else
  7356.     {
  7357.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7358.         lua_pushboolean(L, false);
  7359.     }
  7360.  
  7361.     return 1;
  7362. }
  7363.  
  7364. int32_t LuaInterface::luaSetMonsterOutfit(lua_State* L)
  7365. {
  7366.     //doSetMonsterOutfit(cid, name[, time = -1])
  7367.     int32_t time = -1;
  7368.     if(lua_gettop(L) > 2)
  7369.         time = (int32_t)popNumber(L);
  7370.  
  7371.     std::string name = popString(L);
  7372.     ScriptEnviroment* env = getEnv();
  7373.  
  7374.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  7375.         lua_pushboolean(L, Spell::CreateIllusion(creature, name, time) == RET_NOERROR);
  7376.     else
  7377.     {
  7378.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7379.         lua_pushboolean(L, false);
  7380.     }
  7381.  
  7382.     return 1;
  7383. }
  7384.  
  7385. // ping 2
  7386.  
  7387.  
  7388.  
  7389. // /ping 2
  7390.  
  7391. int32_t LuaInterface::luaSetItemOutfit(lua_State* L)
  7392. {
  7393.     //doSetItemOutfit(cid, item[, time = -1])
  7394.     int32_t time = -1;
  7395.     if(lua_gettop(L) > 2)
  7396.         time = (int32_t)popNumber(L);
  7397.  
  7398.     uint32_t item = (uint32_t)popNumber(L);
  7399.     ScriptEnviroment* env = getEnv();
  7400.  
  7401.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  7402.         lua_pushboolean(L, Spell::CreateIllusion(creature, item, time) == RET_NOERROR);
  7403.     else
  7404.     {
  7405.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7406.         lua_pushboolean(L, false);
  7407.     }
  7408.  
  7409.     return 1;
  7410. }
  7411.  
  7412. int32_t LuaInterface::luaGetStorage(lua_State* L)
  7413. {
  7414.     //getStorage(key)
  7415.     ScriptEnviroment* env = getEnv();
  7416.     std::string strValue;
  7417.     if(env->getStorage(popNumber(L), strValue))
  7418.     {
  7419.         int32_t intValue = atoi(strValue.c_str());
  7420.         if(intValue || strValue == "0")
  7421.             lua_pushnumber(L, intValue);
  7422.         else
  7423.             lua_pushstring(L, strValue.c_str());
  7424.     }
  7425.     else
  7426.         lua_pushnumber(L, -1);
  7427.  
  7428.     return 1;
  7429. }
  7430.  
  7431. int32_t LuaInterface::luaDoSetStorage(lua_State* L)
  7432. {
  7433.     //doSetStorage(key, value)
  7434.     std::string value;
  7435.     bool nil = false;
  7436.     if(lua_isnil(L, -1))
  7437.     {
  7438.         nil = true;
  7439.         lua_pop(L, 1);
  7440.     }
  7441.     else
  7442.         value = popString(L);
  7443.  
  7444.     ScriptEnviroment* env = getEnv();
  7445.     if(!nil)
  7446.         env->setStorage(popNumber(L), value);
  7447.     else
  7448.         env->eraseStorage(popNumber(L));
  7449.  
  7450.     lua_pushboolean(L, true);
  7451.     return 1;
  7452. }
  7453.  
  7454. int32_t LuaInterface::luaGetPlayerDepotItems(lua_State* L)
  7455. {
  7456.     //getPlayerDepotItems(cid, depotid)
  7457.     uint32_t depotid = popNumber(L);
  7458.  
  7459.     ScriptEnviroment* env = getEnv();
  7460.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  7461.     {
  7462.         if(const Depot* depot = player->getDepot(depotid, true))
  7463.             lua_pushnumber(L, depot->getItemHoldingCount());
  7464.         else
  7465.             lua_pushboolean(L, false);
  7466.     }
  7467.     else
  7468.     {
  7469.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  7470.         lua_pushboolean(L, false);
  7471.     }
  7472.  
  7473.     return 1;
  7474. }
  7475.  
  7476. int32_t LuaInterface::luaDoPlayerSetGuildId(lua_State* L)
  7477. {
  7478.     //doPlayerSetGuildId(cid, id)
  7479.     uint32_t id = popNumber(L);
  7480.  
  7481.     ScriptEnviroment* env = getEnv();
  7482.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  7483.     {
  7484.         if(player->getGuildId())
  7485.         {
  7486.             player->leaveGuild();
  7487.             if(!id)
  7488.                 lua_pushboolean(L, true);
  7489.             else if(IOGuild::getInstance()->guildExists(id))
  7490.                 lua_pushboolean(L, IOGuild::getInstance()->joinGuild(player, id));
  7491.             else
  7492.                 lua_pushboolean(L, false);
  7493.         }
  7494.         else if(id && IOGuild::getInstance()->guildExists(id))
  7495.             lua_pushboolean(L, IOGuild::getInstance()->joinGuild(player, id));
  7496.         else
  7497.             lua_pushboolean(L, false);
  7498.     }
  7499.     else
  7500.     {
  7501.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  7502.         lua_pushboolean(L, false);
  7503.     }
  7504.  
  7505.     return 1;
  7506. }
  7507.  
  7508. int32_t LuaInterface::luaDoPlayerSetGuildLevel(lua_State* L)
  7509. {
  7510.     //doPlayerSetGuildLevel(cid, level[, rank])
  7511.     uint32_t rank = 0;
  7512.     if(lua_gettop(L) > 2)
  7513.         rank = popNumber(L);
  7514.  
  7515.     GuildLevel_t level = (GuildLevel_t)popNumber(L);
  7516.     ScriptEnviroment* env = getEnv();
  7517.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  7518.         lua_pushboolean(L, player->setGuildLevel(level, rank));
  7519.     else
  7520.     {
  7521.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  7522.         lua_pushboolean(L, false);
  7523.     }
  7524.  
  7525.     return 1;
  7526. }
  7527.  
  7528. int32_t LuaInterface::luaDoPlayerSetGuildNick(lua_State* L)
  7529. {
  7530.     //doPlayerSetGuildNick(cid, nick)
  7531.     std::string nick = popString(L);
  7532.  
  7533.     ScriptEnviroment* env = getEnv();
  7534.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  7535.     {
  7536.         player->setGuildNick(nick);
  7537.         lua_pushboolean(L, true);
  7538.     }
  7539.     else
  7540.     {
  7541.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  7542.         lua_pushboolean(L, false);
  7543.     }
  7544.  
  7545.     return 1;
  7546. }
  7547.  
  7548. int32_t LuaInterface::luaGetGuildId(lua_State* L)
  7549. {
  7550.     //getGuildId(guildName)
  7551.     uint32_t guildId;
  7552.     if(IOGuild::getInstance()->getGuildId(guildId, popString(L)))
  7553.         lua_pushnumber(L, guildId);
  7554.     else
  7555.         lua_pushboolean(L, false);
  7556.  
  7557.     return 1;
  7558. }
  7559.  
  7560. int32_t LuaInterface::luaGetGuildMotd(lua_State* L)
  7561. {
  7562.     //getGuildMotd(guildId)
  7563.     uint32_t guildId = popNumber(L);
  7564.     if(IOGuild::getInstance()->guildExists(guildId))
  7565.         lua_pushstring(L, IOGuild::getInstance()->getMotd(guildId).c_str());
  7566.     else
  7567.         lua_pushboolean(L, false);
  7568.  
  7569.     return 1;
  7570. }
  7571.  
  7572. int32_t LuaInterface::luaDoMoveCreature(lua_State* L)
  7573. {
  7574.     //doMoveCreature(cid, direction[, flag = FLAG_NOLIMIT])
  7575.     uint32_t flags = FLAG_NOLIMIT;
  7576.     if(lua_gettop(L) > 2)
  7577.         flags = popNumber(L);
  7578.  
  7579.     int32_t direction = popNumber(L);
  7580.     if(direction < NORTH || direction > NORTHEAST)
  7581.     {
  7582.         lua_pushboolean(L, false);
  7583.         return 1;
  7584.     }
  7585.  
  7586.     ScriptEnviroment* env = getEnv();
  7587.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  7588.         lua_pushnumber(L, g_game.internalMoveCreature(creature, (Direction)direction, flags));
  7589.     else
  7590.     {
  7591.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7592.         lua_pushboolean(L, false);
  7593.     }
  7594.  
  7595.     return 1;
  7596. }
  7597.  
  7598. int32_t LuaInterface::luaIsCreature(lua_State* L)
  7599. {
  7600.     //isCreature(cid)
  7601.     ScriptEnviroment* env = getEnv();
  7602.     lua_pushboolean(L, env->getCreatureByUID(popNumber(L)) ? true : false);
  7603.     return 1;
  7604. }
  7605.  
  7606. int32_t LuaInterface::luaIsContainer(lua_State* L)
  7607. {
  7608.     //isContainer(uid)
  7609.     ScriptEnviroment* env = getEnv();
  7610.     lua_pushboolean(L, env->getContainerByUID(popNumber(L)) ? true : false);
  7611.     return 1;
  7612. }
  7613.  
  7614. int32_t LuaInterface::luaIsMovable(lua_State* L)
  7615. {
  7616.     //isMovable(uid)
  7617.     ScriptEnviroment* env = getEnv();
  7618.     Thing* thing = env->getThingByUID(popNumber(L));
  7619.     if(thing && thing->isPushable())
  7620.         lua_pushboolean(L, true);
  7621.     else
  7622.         lua_pushboolean(L, false);
  7623.  
  7624.     return 1;
  7625. }
  7626.  
  7627. int32_t LuaInterface::luaGetCreatureByName(lua_State* L)
  7628. {
  7629.     //getCreatureByName(name)
  7630.     ScriptEnviroment* env = getEnv();
  7631.     if(Creature* creature = g_game.getCreatureByName(popString(L)))
  7632.         lua_pushnumber(L, env->addThing(creature));
  7633.     else
  7634.         lua_pushnil(L);
  7635.  
  7636.     return 1;
  7637. }
  7638.  
  7639. int32_t LuaInterface::luaGetPlayerByGUID(lua_State* L)
  7640. {
  7641.     //getPlayerByGUID(guid)
  7642.     ScriptEnviroment* env = getEnv();
  7643.     if(Player* player = g_game.getPlayerByGuid(popNumber(L)))
  7644.         lua_pushnumber(L, env->addThing(player));
  7645.     else
  7646.         lua_pushnil(L);
  7647.  
  7648.     return 1;
  7649. }
  7650.  
  7651. int32_t LuaInterface::luaGetPlayerByNameWildcard(lua_State* L)
  7652. {
  7653.     //getPlayerByNameWildcard(name~[, ret = false])
  7654.     Player* player = NULL;
  7655.     bool pushRet = false;
  7656.     if(lua_gettop(L) > 1)
  7657.         pushRet = popNumber(L);
  7658.  
  7659.     ScriptEnviroment* env = getEnv();
  7660.     ReturnValue ret = g_game.getPlayerByNameWildcard(popString(L), player);
  7661.     if(ret == RET_NOERROR)
  7662.         lua_pushnumber(L, env->addThing(player));
  7663.     else if(pushRet)
  7664.         lua_pushnumber(L, ret);
  7665.     else
  7666.         lua_pushnil(L);
  7667.  
  7668.     return 1;
  7669. }
  7670.  
  7671. int32_t LuaInterface::luaGetPlayerGUIDByName(lua_State* L)
  7672. {
  7673.     //getPlayerGUIDByName(name[, multiworld = false])
  7674.     bool multiworld = false;
  7675.     if(lua_gettop(L) > 1)
  7676.         multiworld = popNumber(L);
  7677.  
  7678.     std::string name = popString(L);
  7679.     uint32_t guid;
  7680.     if(Player* player = g_game.getPlayerByName(name.c_str()))
  7681.         lua_pushnumber(L, player->getGUID());
  7682.     else if(IOLoginData::getInstance()->getGuidByName(guid, name, multiworld))
  7683.         lua_pushnumber(L, guid);
  7684.     else
  7685.         lua_pushnil(L);
  7686.  
  7687.     return 1;
  7688. }
  7689.  
  7690. int32_t LuaInterface::luaGetPlayerNameByGUID(lua_State* L)
  7691. {
  7692.     //getPlayerNameByGUID(guid[, multiworld = false[, displayError = true]])
  7693.     int32_t parameters = lua_gettop(L);
  7694.     bool multiworld = false, displayError = true;
  7695.  
  7696.     if(parameters > 2)
  7697.         displayError = popNumber(L);
  7698.  
  7699.     if(parameters > 1)
  7700.         multiworld = popNumber(L);
  7701.  
  7702.     uint32_t guid = popNumber(L);
  7703.     std::string name;
  7704.     if(!IOLoginData::getInstance()->getNameByGuid(guid, name, multiworld))
  7705.     {
  7706.         if(displayError)
  7707.             errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  7708.  
  7709.         lua_pushnil(L);
  7710.         return 1;
  7711.     }
  7712.  
  7713.     lua_pushstring(L, name.c_str());
  7714.     return 1;
  7715. }
  7716.  
  7717. int32_t LuaInterface::luaGetPlayersByAccountId(lua_State* L)
  7718. {
  7719.     //getPlayersByAccountId(accId)
  7720.     PlayerVector players = g_game.getPlayersByAccount(popNumber(L));
  7721.  
  7722.     ScriptEnviroment* env = getEnv();
  7723.     PlayerVector::iterator it = players.begin();
  7724.  
  7725.     lua_newtable(L);
  7726.     for(uint32_t i = 1; it != players.end(); ++it, ++i)
  7727.     {
  7728.         lua_pushnumber(L, i);
  7729.         lua_pushnumber(L, env->addThing(*it));
  7730.         pushTable(L);
  7731.     }
  7732.  
  7733.     return 1;
  7734. }
  7735.  
  7736. int32_t LuaInterface::luaGetIpByName(lua_State* L)
  7737. {
  7738.     //getIpByName(name)
  7739.     std::string name = popString(L);
  7740.  
  7741.     if(Player* player = g_game.getPlayerByName(name))
  7742.         lua_pushnumber(L, player->getIP());
  7743.     else
  7744.         lua_pushnumber(L, IOLoginData::getInstance()->getLastIPByName(name));
  7745.  
  7746.     return 1;
  7747. }
  7748.  
  7749. int32_t LuaInterface::luaGetPlayersByIp(lua_State* L)
  7750. {
  7751.     //getPlayersByIp(ip[, mask])
  7752.     uint32_t mask = 0xFFFFFFFF;
  7753.     if(lua_gettop(L) > 1)
  7754.         mask = (uint32_t)popNumber(L);
  7755.  
  7756.     PlayerVector players = g_game.getPlayersByIP(popNumber(L), mask);
  7757.  
  7758.     ScriptEnviroment* env = getEnv();
  7759.     PlayerVector::iterator it = players.begin();
  7760.  
  7761.     lua_newtable(L);
  7762.     for(uint32_t i = 1; it != players.end(); ++it, ++i)
  7763.     {
  7764.         lua_pushnumber(L, i);
  7765.         lua_pushnumber(L, env->addThing(*it));
  7766.         pushTable(L);
  7767.     }
  7768.  
  7769.     return 1;
  7770. }
  7771.  
  7772. int32_t LuaInterface::luaGetAccountIdByName(lua_State* L)
  7773. {
  7774.     //getAccountIdByName(name)
  7775.     std::string name = popString(L);
  7776.  
  7777.     if(Player* player = g_game.getPlayerByName(name))
  7778.         lua_pushnumber(L, player->getAccount());
  7779.     else
  7780.         lua_pushnumber(L, IOLoginData::getInstance()->getAccountIdByName(name));
  7781.  
  7782.     return 1;
  7783. }
  7784.  
  7785. int32_t LuaInterface::luaGetAccountByName(lua_State* L)
  7786. {
  7787.     //getAccountByName(name)
  7788.     std::string name = popString(L);
  7789.  
  7790.     if(Player* player = g_game.getPlayerByName(name))
  7791.         lua_pushstring(L, player->getAccountName().c_str());
  7792.     else
  7793.     {
  7794.         std::string tmp;
  7795.         IOLoginData::getInstance()->getAccountName(IOLoginData::getInstance()->getAccountIdByName(name), tmp);
  7796.         lua_pushstring(L, tmp.c_str());
  7797.     }
  7798.  
  7799.     return 1;
  7800. }
  7801.  
  7802. int32_t LuaInterface::luaGetAccountIdByAccount(lua_State* L)
  7803. {
  7804.     //getAccountIdByAccount(accName)
  7805.     uint32_t value = 0;
  7806.     IOLoginData::getInstance()->getAccountId(popString(L), value);
  7807.     lua_pushnumber(L, value);
  7808.     return 1;
  7809. }
  7810.  
  7811. int32_t LuaInterface::luaGetAccountByAccountId(lua_State* L)
  7812. {
  7813.     //getAccountByAccountId(accId)
  7814.     std::string value = 0;
  7815.     IOLoginData::getInstance()->getAccountName(popNumber(L), value);
  7816.     lua_pushstring(L, value.c_str());
  7817.     return 1;
  7818. }
  7819.  
  7820. int32_t LuaInterface::luaRegisterCreatureEvent(lua_State* L)
  7821. {
  7822.     //registerCreatureEvent(cid, name)
  7823.     std::string name = popString(L);
  7824.  
  7825.     ScriptEnviroment* env = getEnv();
  7826.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  7827.         lua_pushboolean(L, creature->registerCreatureEvent(name));
  7828.     else
  7829.     {
  7830.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7831.         lua_pushboolean(L, false);
  7832.     }
  7833.  
  7834.     return 1;
  7835. }
  7836.  
  7837. int32_t LuaInterface::luaUnregisterCreatureEvent(lua_State* L)
  7838. {
  7839.     //unregisterCreatureEvent(cid, name)
  7840.     std::string name = popString(L);
  7841.  
  7842.     ScriptEnviroment* env = getEnv();
  7843.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  7844.         lua_pushboolean(L, creature->unregisterCreatureEvent(name));
  7845.     else
  7846.     {
  7847.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  7848.         lua_pushboolean(L, false);
  7849.     }
  7850.  
  7851.     return 1;
  7852. }
  7853.  
  7854. int32_t LuaInterface::luaGetContainerSize(lua_State* L)
  7855. {
  7856.     //getContainerSize(uid)
  7857.     ScriptEnviroment* env = getEnv();
  7858.     if(Container* container = env->getContainerByUID(popNumber(L)))
  7859.         lua_pushnumber(L, container->size());
  7860.     else
  7861.     {
  7862.         errorEx(getError(LUA_ERROR_CONTAINER_NOT_FOUND));
  7863.         lua_pushboolean(L, false);
  7864.     }
  7865.  
  7866.     return 1;
  7867. }
  7868.  
  7869. int32_t LuaInterface::luaGetContainerCap(lua_State* L)
  7870. {
  7871.     //getContainerCap(uid)
  7872.     ScriptEnviroment* env = getEnv();
  7873.     if(Container* container = env->getContainerByUID(popNumber(L)))
  7874.         lua_pushnumber(L, container->capacity());
  7875.     else
  7876.     {
  7877.         errorEx(getError(LUA_ERROR_CONTAINER_NOT_FOUND));
  7878.         lua_pushboolean(L, false);
  7879.     }
  7880.  
  7881.     return 1;
  7882. }
  7883.  
  7884. int32_t LuaInterface::luaGetContainerItem(lua_State* L)
  7885. {
  7886.     //getContainerItem(uid, slot)
  7887.     uint32_t slot = popNumber(L);
  7888.  
  7889.     ScriptEnviroment* env = getEnv();
  7890.     if(Container* container = env->getContainerByUID(popNumber(L)))
  7891.     {
  7892.         if(Item* item = container->getItem(slot))
  7893.             pushThing(L, item, env->addThing(item));
  7894.         else
  7895.             pushThing(L, NULL, 0);
  7896.     }
  7897.     else
  7898.     {
  7899.         errorEx(getError(LUA_ERROR_CONTAINER_NOT_FOUND));
  7900.         pushThing(L, NULL, 0);
  7901.     }
  7902.  
  7903.     return 1;
  7904.  
  7905. }
  7906.  
  7907. int32_t LuaInterface::luaDoAddContainerItemEx(lua_State* L)
  7908. {
  7909.     //doAddContainerItemEx(uid, virtuid)
  7910.     uint32_t virtuid = popNumber(L);
  7911.     ScriptEnviroment* env = getEnv();
  7912.     if(Container* container = env->getContainerByUID(popNumber(L)))
  7913.     {
  7914.         Item* item = env->getItemByUID(virtuid);
  7915.         if(!item)
  7916.         {
  7917.             errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  7918.             lua_pushboolean(L, false);
  7919.             return 1;
  7920.         }
  7921.  
  7922.         if(item->getParent() != VirtualCylinder::virtualCylinder)
  7923.         {
  7924.             lua_pushboolean(L, false);
  7925.             return 1;
  7926.         }
  7927.  
  7928.         ReturnValue ret = g_game.internalAddItem(NULL, container, item);
  7929.         if(ret == RET_NOERROR)
  7930.             env->removeTempItem(item);
  7931.  
  7932.         lua_pushnumber(L, ret);
  7933.         return 1;
  7934.     }
  7935.     else
  7936.     {
  7937.         errorEx(getError(LUA_ERROR_CONTAINER_NOT_FOUND));
  7938.         lua_pushboolean(L, false);
  7939.         return 1;
  7940.     }
  7941. }
  7942.  
  7943. int32_t LuaInterface::luaDoAddContainerItem(lua_State* L)
  7944. {
  7945.     //doAddContainerItem(uid, itemid[, count/subType = 1])
  7946.     uint32_t count = 1;
  7947.     if(lua_gettop(L) > 2)
  7948.         count = popNumber(L);
  7949.  
  7950.     uint16_t itemId = popNumber(L);
  7951.     ScriptEnviroment* env = getEnv();
  7952.  
  7953.     Container* container = env->getContainerByUID((uint32_t)popNumber(L));
  7954.     if(!container)
  7955.     {
  7956.         errorEx(getError(LUA_ERROR_CONTAINER_NOT_FOUND));
  7957.         lua_pushboolean(L, false);
  7958.         return 1;
  7959.     }
  7960.  
  7961.     const ItemType& it = Item::items[itemId];
  7962.     int32_t itemCount = 1, subType = 1;
  7963.     if(it.hasSubType())
  7964.     {
  7965.         if(it.stackable)
  7966.             itemCount = (int32_t)std::ceil((float)count / 100);
  7967.  
  7968.         subType = count;
  7969.     }
  7970.     else
  7971.         itemCount = std::max((uint32_t)1, count);
  7972.  
  7973.     while(itemCount > 0)
  7974.     {
  7975.         int32_t stackCount = std::min(100, subType);
  7976.         Item* newItem = Item::CreateItem(itemId, stackCount);
  7977.         if(!newItem)
  7978.         {
  7979.             errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  7980.             lua_pushboolean(L, false);
  7981.             return 1;
  7982.         }
  7983.  
  7984.         if(it.stackable)
  7985.             subType -= stackCount;
  7986.  
  7987.         ReturnValue ret = g_game.internalAddItem(NULL, container, newItem);
  7988.         if(ret != RET_NOERROR)
  7989.         {
  7990.             delete newItem;
  7991.             lua_pushboolean(L, false);
  7992.             return 1;
  7993.         }
  7994.  
  7995.         --itemCount;
  7996.         if(itemCount)
  7997.             continue;
  7998.  
  7999.         if(newItem->getParent())
  8000.             lua_pushnumber(L, env->addThing(newItem));
  8001.         else //stackable item stacked with existing object, newItem will be released
  8002.             lua_pushnil(L);
  8003.  
  8004.         return 1;
  8005.     }
  8006.  
  8007.     lua_pushnil(L);
  8008.     return 1;
  8009. }
  8010.  
  8011. int32_t LuaInterface::luaDoPlayerAddOutfit(lua_State *L)
  8012. {
  8013.     //Consider using doPlayerAddOutfitId instead
  8014.     //doPlayerAddOutfit(cid, looktype, addon)
  8015.     uint32_t addon = popNumber(L), lookType = popNumber(L);
  8016.     ScriptEnviroment* env = getEnv();
  8017.  
  8018.     Player* player = env->getPlayerByUID((uint32_t)popNumber(L));
  8019.     if(!player)
  8020.     {
  8021.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8022.         lua_pushboolean(L, false);
  8023.         return 1;
  8024.     }
  8025.  
  8026.     Outfit outfit;
  8027.     if(Outfits::getInstance()->getOutfit(lookType, outfit))
  8028.     {
  8029.         lua_pushboolean(L, player->addOutfit(outfit.outfitId, addon));
  8030.         return 1;
  8031.     }
  8032.  
  8033.     lua_pushboolean(L, false);
  8034.     return 1;
  8035. }
  8036.  
  8037. int32_t LuaInterface::luaDoPlayerRemoveOutfit(lua_State *L)
  8038. {
  8039.     //Consider using doPlayerRemoveOutfitId instead
  8040.     //doPlayerRemoveOutfit(cid, looktype[, addon = 0])
  8041.     uint32_t addon = 0xFF;
  8042.     if(lua_gettop(L) > 2)
  8043.         addon = popNumber(L);
  8044.  
  8045.     uint32_t lookType = popNumber(L);
  8046.     ScriptEnviroment* env = getEnv();
  8047.  
  8048.     Player* player = env->getPlayerByUID((uint32_t)popNumber(L));
  8049.     if(!player)
  8050.     {
  8051.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8052.         lua_pushboolean(L, false);
  8053.         return 1;
  8054.     }
  8055.  
  8056.     Outfit outfit;
  8057.     if(Outfits::getInstance()->getOutfit(lookType, outfit))
  8058.     {
  8059.         lua_pushboolean(L, player->removeOutfit(outfit.outfitId, addon));
  8060.         return 1;
  8061.     }
  8062.  
  8063.     lua_pushboolean(L, false);
  8064.     return 1;
  8065. }
  8066.  
  8067. int32_t LuaInterface::luaDoPlayerAddOutfitId(lua_State *L)
  8068. {
  8069.     //doPlayerAddOutfitId(cid, outfitId, addon)
  8070.     uint32_t addon = popNumber(L), outfitId = popNumber(L);
  8071.     ScriptEnviroment* env = getEnv();
  8072.  
  8073.     Player* player = env->getPlayerByUID((uint32_t)popNumber(L));
  8074.     if(!player)
  8075.     {
  8076.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8077.         lua_pushboolean(L, false);
  8078.         return 1;
  8079.     }
  8080.  
  8081.     lua_pushboolean(L, player->addOutfit(outfitId, addon));
  8082.     return 1;
  8083. }
  8084.  
  8085. int32_t LuaInterface::luaDoPlayerRemoveOutfitId(lua_State *L)
  8086. {
  8087.     //doPlayerRemoveOutfitId(cid, outfitId[, addon = 0])
  8088.     uint32_t addon = 0xFF;
  8089.     if(lua_gettop(L) > 2)
  8090.         addon = popNumber(L);
  8091.  
  8092.     uint32_t outfitId = popNumber(L);
  8093.     ScriptEnviroment* env = getEnv();
  8094.  
  8095.     Player* player = env->getPlayerByUID((uint32_t)popNumber(L));
  8096.     if(!player)
  8097.     {
  8098.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8099.         lua_pushboolean(L, false);
  8100.         return 1;
  8101.     }
  8102.  
  8103.     lua_pushboolean(L, player->removeOutfit(outfitId, addon));
  8104.     return 1;
  8105. }
  8106.  
  8107. int32_t LuaInterface::luaCanPlayerWearOutfit(lua_State* L)
  8108. {
  8109.     //canPlayerWearOutfit(cid, looktype[, addon = 0])
  8110.     uint32_t addon = 0;
  8111.     if(lua_gettop(L) > 2)
  8112.         addon = popNumber(L);
  8113.  
  8114.     uint32_t lookType = popNumber(L);
  8115.     ScriptEnviroment* env = getEnv();
  8116.  
  8117.     Player* player = env->getPlayerByUID(popNumber(L));
  8118.     if(!player)
  8119.     {
  8120.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8121.         lua_pushboolean(L, false);
  8122.         return 1;
  8123.     }
  8124.  
  8125.     Outfit outfit;
  8126.     if(Outfits::getInstance()->getOutfit(lookType, outfit))
  8127.     {
  8128.         lua_pushboolean(L, player->canWearOutfit(outfit.outfitId, addon));
  8129.         return 1;
  8130.     }
  8131.  
  8132.     lua_pushboolean(L, false);
  8133.     return 1;
  8134. }
  8135.  
  8136. int32_t LuaInterface::luaCanPlayerWearOutfitId(lua_State* L)
  8137. {
  8138.     //canPlayerWearOutfitId(cid, outfitId[, addon = 0])
  8139.     uint32_t addon = 0;
  8140.     if(lua_gettop(L) > 2)
  8141.         addon = popNumber(L);
  8142.  
  8143.     uint32_t outfitId = popNumber(L);
  8144.     ScriptEnviroment* env = getEnv();
  8145.  
  8146.     Player* player = env->getPlayerByUID(popNumber(L));
  8147.     if(!player)
  8148.     {
  8149.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8150.         lua_pushboolean(L, false);
  8151.         return 1;
  8152.     }
  8153.  
  8154.     lua_pushboolean(L, player->canWearOutfit(outfitId, addon));
  8155.     return 1;
  8156. }
  8157.  
  8158. int32_t LuaInterface::luaDoCreatureChangeOutfit(lua_State* L)
  8159. {
  8160.     //doCreatureChangeOutfit(cid, outfit)
  8161.     Outfit_t outfit = popOutfit(L);
  8162.     ScriptEnviroment* env = getEnv();
  8163.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8164.     {
  8165.         if(Player* player = creature->getPlayer())
  8166.             player->changeOutfit(outfit, false);
  8167.         else
  8168.             creature->defaultOutfit = outfit;
  8169.  
  8170.         if(!creature->hasCondition(CONDITION_OUTFIT, 1))
  8171.             g_game.internalCreatureChangeOutfit(creature, outfit);
  8172.  
  8173.         lua_pushboolean(L, true);
  8174.     }
  8175.     else
  8176.     {
  8177.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8178.         lua_pushboolean(L, false);
  8179.     }
  8180.  
  8181.     return 1;
  8182. }
  8183.  
  8184. int32_t LuaInterface::luaDoPlayerPopupFYI(lua_State* L)
  8185. {
  8186.     //doPlayerPopupFYI(cid, message)
  8187.     std::string message = popString(L);
  8188.  
  8189.     ScriptEnviroment* env = getEnv();
  8190.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  8191.     {
  8192.         player->sendFYIBox(message);
  8193.         lua_pushboolean(L, true);
  8194.     }
  8195.     else
  8196.     {
  8197.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8198.         lua_pushboolean(L, false);
  8199.     }
  8200.  
  8201.     return 1;
  8202. }
  8203.  
  8204. int32_t LuaInterface::luaDoPlayerSendTutorial(lua_State* L)
  8205. {
  8206.     //doPlayerSendTutorial(cid, id)
  8207.     uint8_t id = (uint8_t)popNumber(L);
  8208.  
  8209.     ScriptEnviroment* env = getEnv();
  8210.  
  8211.     Player* player = env->getPlayerByUID(popNumber(L));
  8212.     if(!player)
  8213.     {
  8214.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8215.         lua_pushboolean(L, false);
  8216.         return 1;
  8217.     }
  8218.  
  8219.     player->sendTutorial(id);
  8220.     lua_pushboolean(L, true);
  8221.     return 1;
  8222. }
  8223.  
  8224. int32_t LuaInterface::luaDoPlayerSendMailByName(lua_State* L)
  8225. {
  8226.     //doPlayerSendMailByName(name, item[, town[, actor]])
  8227.     ScriptEnviroment* env = getEnv();
  8228.     int32_t params = lua_gettop(L);
  8229.  
  8230.     Creature* actor = NULL;
  8231.     if(params > 3)
  8232.         actor = env->getCreatureByUID(popNumber(L));
  8233.  
  8234.     uint32_t town = 0;
  8235.     if(params > 2)
  8236.         town = popNumber(L);
  8237.  
  8238.     Item* item = env->getItemByUID(popNumber(L));
  8239.     if(!item)
  8240.     {
  8241.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  8242.         lua_pushboolean(L, false);
  8243.         return 1;
  8244.     }
  8245.  
  8246.     if(item->getParent() != VirtualCylinder::virtualCylinder)
  8247.     {
  8248.         lua_pushboolean(L, false);
  8249.         return 1;
  8250.     }
  8251.  
  8252.     lua_pushboolean(L, IOLoginData::getInstance()->playerMail(actor, popString(L), town, item));
  8253.     return 1;
  8254. }
  8255.  
  8256. int32_t LuaInterface::luaDoPlayerAddMapMark(lua_State* L)
  8257. {
  8258.     //doPlayerAddMapMark(cid, pos, type[, description])
  8259.     std::string description;
  8260.     if(lua_gettop(L) > 3)
  8261.         description = popString(L);
  8262.  
  8263.     MapMarks_t type = (MapMarks_t)popNumber(L);
  8264.     PositionEx pos;
  8265.     popPosition(L, pos);
  8266.  
  8267.     ScriptEnviroment* env = getEnv();
  8268.     Player* player = env->getPlayerByUID(popNumber(L));
  8269.     if(!player)
  8270.     {
  8271.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8272.         lua_pushboolean(L, false);
  8273.         return 1;
  8274.     }
  8275.  
  8276.     player->sendAddMarker(pos, type, description);
  8277.     lua_pushboolean(L, true);
  8278.     return 1;
  8279. }
  8280.  
  8281. int32_t LuaInterface::luaDoPlayerAddPremiumDays(lua_State* L)
  8282. {
  8283.     //doPlayerAddPremiumDays(cid, days)
  8284.     int32_t days = popNumber(L);
  8285.     ScriptEnviroment* env = getEnv();
  8286.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  8287.     {
  8288.         if(player->premiumDays < 65535)
  8289.         {
  8290.             Account account = IOLoginData::getInstance()->loadAccount(player->getAccount());
  8291.             if(days < 0)
  8292.             {
  8293.                 account.premiumDays = std::max((uint32_t)0, uint32_t(account.premiumDays + (int32_t)days));
  8294.                 player->premiumDays = std::max((uint32_t)0, uint32_t(player->premiumDays + (int32_t)days));
  8295.             }
  8296.             else
  8297.             {
  8298.                 account.premiumDays = std::min((uint32_t)65534, uint32_t(account.premiumDays + (uint32_t)days));
  8299.                 player->premiumDays = std::min((uint32_t)65534, uint32_t(player->premiumDays + (uint32_t)days));
  8300.             }
  8301.             IOLoginData::getInstance()->saveAccount(account);
  8302.         }
  8303.         lua_pushboolean(L, true);
  8304.     }
  8305.     else
  8306.     {
  8307.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8308.         lua_pushboolean(L, false);
  8309.     }
  8310.  
  8311.     return 1;
  8312. }
  8313.  
  8314. int32_t LuaInterface::luaGetCreatureLastPosition(lua_State* L)
  8315. {
  8316.     //getCreatureLastPosition(cid)
  8317.     ScriptEnviroment* env = getEnv();
  8318.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8319.         pushPosition(L, creature->getLastPosition(), 0);
  8320.     else
  8321.     {
  8322.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8323.         lua_pushboolean(L, false);
  8324.     }
  8325.  
  8326.     return 1;
  8327. }
  8328.  
  8329. int32_t LuaInterface::luaGetCreatureName(lua_State* L)
  8330. {
  8331.     //getCreatureName(cid)
  8332.     ScriptEnviroment* env = getEnv();
  8333.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8334.         lua_pushstring(L, creature->getName().c_str());
  8335.     else
  8336.     {
  8337.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8338.         lua_pushboolean(L, false);
  8339.     }
  8340.  
  8341.     return 1;
  8342. }
  8343.  
  8344. int32_t LuaInterface::luaGetCreaturePathTo(lua_State* L)
  8345. {
  8346.     //getCreaturePathTo(cid, pos, maxSearchDist)
  8347.     ScriptEnviroment* env = getEnv();
  8348.     int32_t maxSearchDist = popNumber(L);
  8349.     PositionEx position;
  8350.     popPosition(L, position);
  8351.     Creature* creature = env->getCreatureByUID(popNumber(L));
  8352.     if (!creature) {
  8353.         lua_pushnil(L);
  8354.         return 1;
  8355.     }
  8356.     std::list<Direction> dirList;
  8357.     lua_newtable(L);
  8358.     if (g_game.getPathTo(creature, position, dirList, maxSearchDist)) {
  8359.         std::list<Direction>::const_iterator it = dirList.begin();
  8360.         for (int32_t index = 1; it != dirList.end(); ++it, ++index) {
  8361.             lua_pushnumber(L, index);
  8362.             lua_pushnumber(L, (*it));
  8363.             pushTable(L);
  8364.         }
  8365.     } else {
  8366.         lua_pushboolean(L, false);
  8367.     }
  8368.     return 1;
  8369. }
  8370.  
  8371. int32_t LuaInterface::luaGetCreatureNoMove(lua_State* L)
  8372. {
  8373.     //getCreatureNoMove(cid)
  8374.     ScriptEnviroment* env = getEnv();
  8375.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8376.         lua_pushboolean(L, creature->getNoMove());
  8377.     else
  8378.     {
  8379.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8380.         lua_pushboolean(L, false);
  8381.     }
  8382.  
  8383.     return 1;
  8384. }
  8385.  
  8386. int32_t LuaInterface::luaGetCreatureGuildEmblem(lua_State* L)
  8387. {
  8388.     //getCreatureGuildEmblem(cid[, target])
  8389.     uint32_t tid = 0;
  8390.     if(lua_gettop(L) > 1)
  8391.         tid = popNumber(L);
  8392.  
  8393.     ScriptEnviroment* env = getEnv();
  8394.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8395.     {
  8396.         if(!tid)
  8397.             lua_pushnumber(L, creature->getEmblem());
  8398.         else if(Creature* target = env->getCreatureByUID(tid))
  8399.             lua_pushnumber(L, creature->getGuildEmblem(target));
  8400.         else
  8401.         {
  8402.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8403.             lua_pushboolean(L, false);
  8404.         }
  8405.     }
  8406.     else
  8407.     {
  8408.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8409.         lua_pushboolean(L, false);
  8410.     }
  8411.  
  8412.     return 1;
  8413. }
  8414.  
  8415. int32_t LuaInterface::luaDoCreatureSetGuildEmblem(lua_State* L)
  8416. {
  8417.     //doCreatureSetGuildEmblem(cid, emblem)
  8418.     GuildEmblems_t emblem = (GuildEmblems_t)popNumber(L);
  8419.     ScriptEnviroment* env = getEnv();
  8420.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8421.     {
  8422.         creature->setEmblem(emblem);
  8423.         g_game.updateCreatureEmblem(creature);
  8424.         lua_pushboolean(L, true);
  8425.     }
  8426.     else
  8427.     {
  8428.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8429.         lua_pushboolean(L, false);
  8430.     }
  8431.  
  8432.     return 1;
  8433. }
  8434.  
  8435. int32_t LuaInterface::luaGetCreaturePartyShield(lua_State* L)
  8436. {
  8437.     //getCreaturePartyShield(cid[, target])
  8438.     uint32_t tid = 0;
  8439.     if(lua_gettop(L) > 1)
  8440.         tid = popNumber(L);
  8441.  
  8442.     ScriptEnviroment* env = getEnv();
  8443.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8444.     {
  8445.         if(!tid)
  8446.             lua_pushnumber(L, creature->getShield());
  8447.         else if(Creature* target = env->getCreatureByUID(tid))
  8448.             lua_pushnumber(L, creature->getPartyShield(target));
  8449.         else
  8450.         {
  8451.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8452.             lua_pushboolean(L, false);
  8453.         }
  8454.     }
  8455.     else
  8456.     {
  8457.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8458.         lua_pushboolean(L, false);
  8459.     }
  8460.  
  8461.     return 1;
  8462. }
  8463.  
  8464. int32_t LuaInterface::luaDoCreatureSetPartyShield(lua_State* L)
  8465. {
  8466.     //doCreatureSetPartyShield(cid, shield)
  8467.     PartyShields_t shield = (PartyShields_t)popNumber(L);
  8468.     ScriptEnviroment* env = getEnv();
  8469.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8470.     {
  8471.         creature->setShield(shield);
  8472.         g_game.updateCreatureShield(creature);
  8473.         lua_pushboolean(L, true);
  8474.     }
  8475.     else
  8476.     {
  8477.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8478.         lua_pushboolean(L, false);
  8479.     }
  8480.  
  8481.     return 1;
  8482. }
  8483.  
  8484. int32_t LuaInterface::luaGetCreatureSkullType(lua_State* L)
  8485. {
  8486.     //getCreatureSkullType(cid[, target])
  8487.     uint32_t tid = 0;
  8488.     if(lua_gettop(L) > 1)
  8489.         tid = popNumber(L);
  8490.  
  8491.     ScriptEnviroment* env = getEnv();
  8492.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8493.     {
  8494.         if(!tid)
  8495.             lua_pushnumber(L, creature->getSkull());
  8496.         else if(Creature* target = env->getCreatureByUID(tid))
  8497.             lua_pushnumber(L, creature->getSkullType(target));
  8498.         else
  8499.         {
  8500.             errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8501.             lua_pushboolean(L, false);
  8502.         }
  8503.     }
  8504.     else
  8505.     {
  8506.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8507.         lua_pushboolean(L, false);
  8508.     }
  8509.  
  8510.     return 1;
  8511. }
  8512.  
  8513. int32_t LuaInterface::luaDoCreatureSetLookDir(lua_State* L)
  8514. {
  8515.     //doCreatureSetLookDirection(cid, dir)
  8516.     Direction dir = (Direction)popNumber(L);
  8517.     ScriptEnviroment* env = getEnv();
  8518.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8519.     {
  8520.         if(dir < NORTH || dir > WEST)
  8521.         {
  8522.             lua_pushboolean(L, false);
  8523.             return 1;
  8524.         }
  8525.  
  8526.         g_game.internalCreatureTurn(creature, dir);
  8527.         lua_pushboolean(L, true);
  8528.     }
  8529.     else
  8530.     {
  8531.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8532.         lua_pushboolean(L, false);
  8533.     }
  8534.  
  8535.     return 1;
  8536. }
  8537.  
  8538. int32_t LuaInterface::luaDoCreatureSetSkullType(lua_State* L)
  8539. {
  8540.     //doCreatureSetSkullType(cid, skull)
  8541.     Skulls_t skull = (Skulls_t)popNumber(L);
  8542.     ScriptEnviroment* env = getEnv();
  8543.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8544.     {
  8545.         creature->setSkull(skull);
  8546.         g_game.updateCreatureSkull(creature);
  8547.         lua_pushboolean(L, true);
  8548.     }
  8549.     else
  8550.     {
  8551.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8552.         lua_pushboolean(L, false);
  8553.     }
  8554.  
  8555.     return 1;
  8556. }
  8557.  
  8558. int32_t LuaInterface::luaDoPlayerSetSkullEnd(lua_State* L)
  8559. {
  8560.     //doPlayerSetSkullEnd(cid, time, type)
  8561.     Skulls_t _skull = (Skulls_t)popNumber(L);
  8562.     time_t _time = (time_t)std::max((int64_t)0, popNumber(L));
  8563.  
  8564.     ScriptEnviroment* env = getEnv();
  8565.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  8566.     {
  8567.         player->setSkullEnd(_time, false, _skull);
  8568.         lua_pushboolean(L, true);
  8569.     }
  8570.     else
  8571.     {
  8572.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8573.         lua_pushboolean(L, false);
  8574.     }
  8575.  
  8576.     return 1;
  8577. }
  8578.  
  8579. int32_t LuaInterface::luaGetCreatureSpeed(lua_State* L)
  8580. {
  8581.     //getCreatureSpeed(cid)
  8582.     ScriptEnviroment* env = getEnv();
  8583.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8584.         lua_pushnumber(L, creature->getSpeed());
  8585.     else
  8586.     {
  8587.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8588.         lua_pushboolean(L, false);
  8589.     }
  8590.  
  8591.     return 1;
  8592. }
  8593.  
  8594. int32_t LuaInterface::luaGetCreatureBaseSpeed(lua_State* L)
  8595. {
  8596.     //getCreatureBaseSpeed(cid)
  8597.     ScriptEnviroment* env = getEnv();
  8598.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8599.         lua_pushnumber(L, creature->getBaseSpeed());
  8600.     else
  8601.     {
  8602.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8603.         lua_pushboolean(L, false);
  8604.     }
  8605.  
  8606.     return 1;
  8607. }
  8608.  
  8609. int32_t LuaInterface::luaGetCreatureTarget(lua_State* L)
  8610. {
  8611.     //getCreatureTarget(cid)
  8612.     ScriptEnviroment* env = getEnv();
  8613.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8614.     {
  8615.         Creature* target = creature->getAttackedCreature();
  8616.         lua_pushnumber(L, target ? env->addThing(target) : 0);
  8617.     }
  8618.     else
  8619.     {
  8620.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8621.         lua_pushboolean(L, false);
  8622.     }
  8623.  
  8624.     return 1;
  8625. }
  8626.  
  8627. int32_t LuaInterface::luaIsSightClear(lua_State* L)
  8628. {
  8629.     //isSightClear(fromPos, toPos, floorCheck)
  8630.     PositionEx fromPos, toPos;
  8631.     bool floorCheck = popNumber(L);
  8632.  
  8633.     popPosition(L, toPos);
  8634.     popPosition(L, fromPos);
  8635.  
  8636.     lua_pushboolean(L, g_game.isSightClear(fromPos, toPos, floorCheck));
  8637.     return 1;
  8638. }
  8639.  
  8640. int32_t LuaInterface::luaIsInArray(lua_State* L)
  8641. {
  8642.     //isInArray(array, value[, caseSensitive = false])
  8643.     bool caseSensitive = false;
  8644.     if(lua_gettop(L) > 2)
  8645.         caseSensitive = popNumber(L);
  8646.  
  8647.     boost::any value;
  8648.     if(lua_isnumber(L, -1))
  8649.         value = popFloatNumber(L);
  8650.     else if(lua_isboolean(L, -1))
  8651.         value = popBoolean(L);
  8652.     else if(lua_isstring(L, -1))
  8653.         value = popString(L);
  8654.     else
  8655.     {
  8656.         lua_pop(L, 1);
  8657.         lua_pushboolean(L, false);
  8658.         return 1;
  8659.     }
  8660.  
  8661.     const std::type_info& type = value.type();
  8662.     if(!caseSensitive && type == typeid(std::string))
  8663.         value = asLowerCaseString(boost::any_cast<std::string>(value));
  8664.  
  8665.     if(!lua_istable(L, -1))
  8666.     {
  8667.         boost::any data;
  8668.         if(lua_isnumber(L, -1))
  8669.             data = popFloatNumber(L);
  8670.         else if(lua_isboolean(L, -1))
  8671.             data = popBoolean(L);
  8672.         else if(lua_isstring(L, -1))
  8673.             data = popString(L);
  8674.         else
  8675.         {
  8676.             lua_pop(L, 1);
  8677.             lua_pushboolean(L, false);
  8678.             return 1;
  8679.         }
  8680.  
  8681.         if(type != data.type()) // check is it even same data type before searching deeper
  8682.             lua_pushboolean(L, false);
  8683.         else if(type == typeid(bool))
  8684.             lua_pushboolean(L, boost::any_cast<bool>(value) == boost::any_cast<bool>(data));
  8685.         else if(type == typeid(double))
  8686.             lua_pushboolean(L, boost::any_cast<double>(value) == boost::any_cast<double>(data));
  8687.         else if(caseSensitive)
  8688.             lua_pushboolean(L, boost::any_cast<std::string>(value) == boost::any_cast<std::string>(data));
  8689.         else
  8690.             lua_pushboolean(L, boost::any_cast<std::string>(value) == asLowerCaseString(boost::any_cast<std::string>(data)));
  8691.  
  8692.         return 1;
  8693.     }
  8694.  
  8695.     lua_pushnil(L);
  8696.     while(lua_next(L, -2))
  8697.     {
  8698.         boost::any data;
  8699.         if(lua_isnumber(L, -1))
  8700.             data = popFloatNumber(L);
  8701.         else if(lua_isboolean(L, -1))
  8702.             data = popBoolean(L);
  8703.         else if(lua_isstring(L, -1))
  8704.             data = popString(L);
  8705.         else
  8706.         {
  8707.             lua_pop(L, 1);
  8708.             break;
  8709.         }
  8710.  
  8711.         if(type != data.type()) // check is it same data type before searching deeper
  8712.             continue;
  8713.  
  8714.         if(type == typeid(bool))
  8715.         {
  8716.             if(boost::any_cast<bool>(value) != boost::any_cast<bool>(data))
  8717.                 continue;
  8718.  
  8719.             lua_pushboolean(L, true);
  8720.             return 1;
  8721.         }
  8722.         else if(type == typeid(double))
  8723.         {
  8724.             if(boost::any_cast<double>(value) != boost::any_cast<double>(data))
  8725.                 continue;
  8726.  
  8727.             lua_pushboolean(L, true);
  8728.             return 1;
  8729.         }
  8730.         else if(caseSensitive)
  8731.         {
  8732.             if(boost::any_cast<std::string>(value) != boost::any_cast<std::string>(data))
  8733.                 continue;
  8734.  
  8735.             lua_pushboolean(L, true);
  8736.             return 1;
  8737.         }
  8738.         else if(boost::any_cast<std::string>(value) == asLowerCaseString(boost::any_cast<std::string>(data)))
  8739.         {
  8740.             lua_pushboolean(L, true);
  8741.             return 1;
  8742.         }
  8743.     }
  8744.  
  8745.     lua_pop(L, 2);
  8746.     lua_pushboolean(L, false);
  8747.     return 1;
  8748. }
  8749.  
  8750. int32_t LuaInterface::luaAddEvent(lua_State* L)
  8751. {
  8752.     //addEvent(callback, delay, ...)
  8753.     ScriptEnviroment* env = getEnv();
  8754.     LuaInterface* interface = env->getInterface();
  8755.     if(!interface)
  8756.     {
  8757.         errorEx("No valid script interface!");
  8758.         lua_pushboolean(L, false);
  8759.         return 1;
  8760.     }
  8761.  
  8762.     int32_t parameters = lua_gettop(L);
  8763.     if(!lua_isfunction(L, -parameters)) //-parameters means the first parameter from left to right
  8764.     {
  8765.         errorEx("Callback parameter should be a function.");
  8766.         lua_pushboolean(L, false);
  8767.         return 1;
  8768.     }
  8769.  
  8770.     std::list<int32_t> params;
  8771.     for(int32_t i = 0; i < parameters - 2; ++i) //-2 because addEvent needs at least two parameters
  8772.         params.push_back(luaL_ref(L, LUA_REGISTRYINDEX));
  8773.  
  8774.     LuaTimerEvent event;
  8775.     event.eventId = Scheduler::getInstance().addEvent(createSchedulerTask(std::max((int64_t)SCHEDULER_MINTICKS, popNumber(L)),
  8776.         boost::bind(&LuaInterface::executeTimer, interface, ++interface->m_lastTimer)));
  8777.  
  8778.     event.parameters = params;
  8779.     event.function = luaL_ref(L, LUA_REGISTRYINDEX);
  8780.     event.scriptId = env->getScriptId();
  8781.  
  8782.     interface->m_timerEvents[interface->m_lastTimer] = event;
  8783.     lua_pushnumber(L, interface->m_lastTimer);
  8784.     return 1;
  8785. }
  8786.  
  8787. int32_t LuaInterface::luaStopEvent(lua_State* L)
  8788. {
  8789.     //stopEvent(eventid)
  8790.     uint32_t eventId = popNumber(L);
  8791.     ScriptEnviroment* env = getEnv();
  8792.  
  8793.     LuaInterface* interface = env->getInterface();
  8794.     if(!interface)
  8795.     {
  8796.         errorEx("No valid script interface!");
  8797.         lua_pushboolean(L, false);
  8798.         return 1;
  8799.     }
  8800.  
  8801.     LuaTimerEvents::iterator it = interface->m_timerEvents.find(eventId);
  8802.     if(it != interface->m_timerEvents.end())
  8803.     {
  8804.         Scheduler::getInstance().stopEvent(it->second.eventId);
  8805.         for(std::list<int32_t>::iterator lt = it->second.parameters.begin(); lt != it->second.parameters.end(); ++lt)
  8806.             luaL_unref(interface->m_luaState, LUA_REGISTRYINDEX, *lt);
  8807.  
  8808.         it->second.parameters.clear();
  8809.         luaL_unref(interface->m_luaState, LUA_REGISTRYINDEX, it->second.function);
  8810.  
  8811.         interface->m_timerEvents.erase(it);
  8812.         lua_pushboolean(L, true);
  8813.     }
  8814.     else
  8815.         lua_pushboolean(L, false);
  8816.  
  8817.     return 1;
  8818. }
  8819.  
  8820. int32_t LuaInterface::luaGetCreatureCondition(lua_State* L)
  8821. {
  8822.     //getCreatureCondition(cid, condition[, subId = 0])
  8823.     uint32_t subId = 0, condition = 0;
  8824.     if(lua_gettop(L) > 2)
  8825.         subId = popNumber(L);
  8826.  
  8827.     condition = popNumber(L);
  8828.     ScriptEnviroment* env = getEnv();
  8829.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8830.         lua_pushboolean(L, creature->hasCondition((ConditionType_t)condition, subId));
  8831.     else
  8832.     {
  8833.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8834.         lua_pushboolean(L, false);
  8835.     }
  8836.  
  8837.     return 1;
  8838. }
  8839.  
  8840. int32_t LuaInterface::luaGetPlayerBlessing(lua_State* L)
  8841. {
  8842.     //getPlayerBlessings(cid, blessing)
  8843.     int16_t blessing = popNumber(L) - 1;
  8844.  
  8845.     ScriptEnviroment* env = getEnv();
  8846.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  8847.         lua_pushboolean(L, player->hasBlessing(blessing));
  8848.     else
  8849.     {
  8850.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8851.         lua_pushboolean(L, false);
  8852.     }
  8853.  
  8854.     return 1;
  8855. }
  8856.  
  8857. int32_t LuaInterface::luaDoPlayerAddBlessing(lua_State* L)
  8858. {
  8859.     //doPlayerAddBlessing(cid, blessing)
  8860.     int16_t blessing = popNumber(L) - 1;
  8861.     ScriptEnviroment* env = getEnv();
  8862.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  8863.     {
  8864.         if(!player->hasBlessing(blessing))
  8865.         {
  8866.             player->addBlessing(1 << blessing);
  8867.             lua_pushboolean(L, true);
  8868.         }
  8869.         else
  8870.             lua_pushboolean(L, false);
  8871.     }
  8872.     else
  8873.     {
  8874.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8875.         lua_pushboolean(L, false);
  8876.     }
  8877.  
  8878.     return 1;
  8879. }
  8880.  
  8881. int32_t LuaInterface::luaDoPlayerSetPromotionLevel(lua_State* L)
  8882. {
  8883.     //doPlayerSetPromotionLevel(cid, level)
  8884.     uint32_t level = popNumber(L);
  8885.     ScriptEnviroment* env = getEnv();
  8886.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  8887.     {
  8888.         player->setPromotionLevel(level);
  8889.         lua_pushboolean(L, true);
  8890.     }
  8891.     else
  8892.     {
  8893.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8894.         lua_pushboolean(L, false);
  8895.     }
  8896.  
  8897.     return 1;
  8898. }
  8899.  
  8900. int32_t LuaInterface::luaDoPlayerSetMagicLevel(lua_State* L)
  8901. {
  8902.     //doPlayerSetMagicLevel(uid, value)
  8903.     uint64_t value = popNumber(L);
  8904.  
  8905.     ScriptEnviroment* env = getEnv();
  8906.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  8907.     {
  8908.         player->setMagicLevel(value);
  8909.         lua_pushboolean(L, true);
  8910.     }
  8911.     else
  8912.     {
  8913.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8914.         lua_pushboolean(L, false);
  8915.     }
  8916.     return 1;
  8917. }
  8918.  
  8919. int32_t LuaInterface::luaDoPlayerSetSkillLevel(lua_State* L)
  8920. {
  8921.     //doPlayerSetSkillLevel(uid, skill, value)
  8922.     uint32_t value = popNumber(L);
  8923.     int32_t skill = popNumber(L);
  8924.  
  8925.     ScriptEnviroment* env = getEnv();
  8926.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  8927.     {
  8928.         player->setSkillLevel((skills_t) skill, value);
  8929.         lua_pushboolean(L, true);
  8930.     }
  8931.     else
  8932.     {
  8933.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8934.         lua_pushboolean(L, false);
  8935.     }
  8936.     return 1;
  8937. }
  8938.  
  8939. int32_t LuaInterface::luaDoPlayerSetGroupId(lua_State* L)
  8940. {
  8941.     //doPlayerSetGroupId(cid, groupId)
  8942.     uint32_t groupId = popNumber(L);
  8943.     ScriptEnviroment* env = getEnv();
  8944.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  8945.     {
  8946.         if(Group* group = Groups::getInstance()->getGroup(groupId))
  8947.         {
  8948.             player->setGroup(group);
  8949.             lua_pushboolean(L, true);
  8950.         }
  8951.         else
  8952.             lua_pushboolean(L, false);
  8953.     }
  8954.     else
  8955.     {
  8956.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  8957.         lua_pushboolean(L, false);
  8958.     }
  8959.  
  8960.     return 1;
  8961. }
  8962.  
  8963. int32_t LuaInterface::luaGetCreatureMana(lua_State* L)
  8964. {
  8965.     //getCreatureMana(cid)
  8966.     ScriptEnviroment* env = getEnv();
  8967.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8968.         lua_pushnumber(L, creature->getMana());
  8969.     else
  8970.     {
  8971.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8972.         lua_pushboolean(L, false);
  8973.     }
  8974.  
  8975.     return 1;
  8976. }
  8977.  
  8978. int32_t LuaInterface::luaGetCreatureMaxMana(lua_State* L)
  8979. {
  8980.     //getCreatureMaxMana(cid)
  8981.     ScriptEnviroment* env = getEnv();
  8982.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8983.         lua_pushnumber(L, creature->getMaxMana());
  8984.     else
  8985.     {
  8986.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  8987.         lua_pushboolean(L, false);
  8988.     }
  8989.  
  8990.     return 1;
  8991. }
  8992.  
  8993. int32_t LuaInterface::luaGetCreatureHealth(lua_State* L)
  8994. {
  8995.     //getCreatureHealth(cid)
  8996.     ScriptEnviroment* env = getEnv();
  8997.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  8998.         lua_pushnumber(L, creature->getHealth());
  8999.     else
  9000.     {
  9001.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9002.         lua_pushboolean(L, false);
  9003.     }
  9004.  
  9005.     return 1;
  9006. }
  9007.  
  9008. int32_t LuaInterface::luaGetCreatureLookDirection(lua_State* L)
  9009. {
  9010.     //getCreatureLookDirection(cid)
  9011.     ScriptEnviroment* env = getEnv();
  9012.  
  9013.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  9014.         lua_pushnumber(L, creature->getDirection());
  9015.     else
  9016.     {
  9017.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9018.         lua_pushboolean(L, false);
  9019.     }
  9020.  
  9021.     return 1;
  9022. }
  9023.  
  9024. int32_t LuaInterface::luaGetCreatureMaxHealth(lua_State* L)
  9025. {
  9026.     //getCreatureMaxHealth(cid)
  9027.     ScriptEnviroment* env = getEnv();
  9028.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  9029.         lua_pushnumber(L, creature->getMaxHealth());
  9030.     else
  9031.     {
  9032.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9033.         lua_pushboolean(L, false);
  9034.     }
  9035.  
  9036.     return 1;
  9037. }
  9038.  
  9039. int32_t LuaInterface::luaDoPlayerSetStamina(lua_State* L)
  9040. {
  9041.     //doPlayerSetStamina(cid, minutes)
  9042.     uint32_t minutes = popNumber(L);
  9043.  
  9044.     ScriptEnviroment* env = getEnv();
  9045.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9046.     {
  9047.         player->setStaminaMinutes(minutes);
  9048.         lua_pushboolean(L, true);
  9049.     }
  9050.     else
  9051.     {
  9052.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9053.         lua_pushboolean(L, false);
  9054.     }
  9055.  
  9056.     return 1;
  9057. }
  9058.  
  9059. int32_t LuaInterface::luaDoPlayerSetBalance(lua_State* L)
  9060. {
  9061.     //doPlayerSetBalance(cid, balance)
  9062.     uint64_t balance = popNumber(L);
  9063.  
  9064.     ScriptEnviroment* env = getEnv();
  9065.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9066.     {
  9067.         player->balance = balance;
  9068.         lua_pushboolean(L, true);
  9069.     }
  9070.     else
  9071.     {
  9072.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9073.         lua_pushboolean(L, false);
  9074.     }
  9075.  
  9076.     return 1;
  9077. }
  9078.  
  9079. int32_t LuaInterface::luaDoPlayerSetPartner(lua_State* L)
  9080. {
  9081.     //doPlayerSetPartner(cid, guid)
  9082.     uint32_t guid = popNumber(L);
  9083.  
  9084.     ScriptEnviroment* env = getEnv();
  9085.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9086.     {
  9087.         player->marriage = guid;
  9088.         lua_pushboolean(L, true);
  9089.     }
  9090.     else
  9091.     {
  9092.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9093.         lua_pushboolean(L, false);
  9094.     }
  9095.  
  9096.     return 1;
  9097. }
  9098.  
  9099. int32_t LuaInterface::luaDoPlayerFollowCreature(lua_State* L)
  9100. {
  9101.     //doPlayerFollowCreature(cid, target)
  9102.     ScriptEnviroment* env = getEnv();
  9103.  
  9104.     Creature* creature = env->getCreatureByUID(popNumber(L));
  9105.     if(!creature)
  9106.     {
  9107.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9108.         lua_pushboolean(L, false);
  9109.         return 1;
  9110.     }
  9111.  
  9112.     Player* player = env->getPlayerByUID(popNumber(L));
  9113.     if(!player)
  9114.     {
  9115.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9116.         lua_pushboolean(L, false);
  9117.         return 1;
  9118.     }
  9119.  
  9120.     lua_pushboolean(L, g_game.playerFollowCreature(player->getID(), creature->getID()));
  9121.     return 1;
  9122. }
  9123.  
  9124. int32_t LuaInterface::luaGetPlayerParty(lua_State* L)
  9125. {
  9126.     //getPlayerParty(cid)
  9127.     uint32_t cid = popNumber(L);
  9128.  
  9129.     ScriptEnviroment* env = getEnv();
  9130.     if(Player* player = env->getPlayerByUID(cid))
  9131.     {
  9132.         if(Party* party = player->getParty())
  9133.             lua_pushnumber(L, env->addThing(party->getLeader()));
  9134.         else
  9135.             lua_pushnil(L);
  9136.     }
  9137.     else
  9138.     {
  9139.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9140.         lua_pushboolean(L, false);
  9141.     }
  9142.  
  9143.     return 1;
  9144. }
  9145.  
  9146. int32_t LuaInterface::luaDoPlayerJoinParty(lua_State* L)
  9147. {
  9148.     //doPlayerJoinParty(cid, lid)
  9149.     ScriptEnviroment* env = getEnv();
  9150.  
  9151.     Player* leader = env->getPlayerByUID(popNumber(L));
  9152.     if(!leader)
  9153.     {
  9154.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9155.         lua_pushboolean(L, false);
  9156.     }
  9157.  
  9158.     Player* player = env->getPlayerByUID(popNumber(L));
  9159.     if(!player)
  9160.     {
  9161.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9162.         lua_pushboolean(L, false);
  9163.     }
  9164.  
  9165.     g_game.playerJoinParty(player->getID(), leader->getID());
  9166.     lua_pushboolean(L, true);
  9167.     return 1;
  9168. }
  9169.  
  9170. int32_t LuaInterface::luaDoPlayerLeaveParty(lua_State* L)
  9171. {
  9172.     //doPlayerLeaveParty(cid[, forced = false])
  9173.     bool forced = false;
  9174.     if(lua_gettop(L) > 1)
  9175.         forced = popNumber(L);
  9176.  
  9177.     ScriptEnviroment* env = getEnv();
  9178.     Player* player = env->getPlayerByUID(popNumber(L));
  9179.     if(!player)
  9180.     {
  9181.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9182.         lua_pushboolean(L, false);
  9183.     }
  9184.  
  9185.     g_game.playerLeaveParty(player->getID(), forced);
  9186.     lua_pushboolean(L, true);
  9187.     return 1;
  9188. }
  9189.  
  9190. int32_t LuaInterface::luaGetPartyMembers(lua_State* L)
  9191. {
  9192.     //getPartyMembers(cid)
  9193.     ScriptEnviroment* env = getEnv();
  9194.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9195.     {
  9196.         if(Party* party = player->getParty())
  9197.         {
  9198.             PlayerVector list = party->getMembers();
  9199.             list.push_back(party->getLeader());
  9200.  
  9201.             PlayerVector::const_iterator it = list.begin();
  9202.             lua_newtable(L);
  9203.             for(uint32_t i = 1; it != list.end(); ++it, ++i)
  9204.             {
  9205.                 lua_pushnumber(L, i);
  9206.                 lua_pushnumber(L, (*it)->getID());
  9207.                 pushTable(L);
  9208.             }
  9209.  
  9210.             return 1;
  9211.         }
  9212.     }
  9213.  
  9214.     lua_pushboolean(L, false);
  9215.     return 1;
  9216. }
  9217.  
  9218. int32_t LuaInterface::luaGetVocationInfo(lua_State* L)
  9219. {
  9220.     //getVocationInfo(id)
  9221.     uint32_t id = popNumber(L);
  9222.     Vocation* voc = Vocations::getInstance()->getVocation(id);
  9223.     if(!voc)
  9224.     {
  9225.         lua_pushboolean(L, false);
  9226.         return 1;
  9227.     }
  9228.  
  9229.     lua_newtable(L);
  9230.     setField(L, "id", voc->getId());
  9231.     setField(L, "name", voc->getName().c_str());
  9232.     setField(L, "description", voc->getDescription().c_str());
  9233.     setField(L, "healthGain", voc->getGain(GAIN_HEALTH));
  9234.     setField(L, "healthGainTicks", voc->getGainTicks(GAIN_HEALTH));
  9235.     setField(L, "healthGainAmount", voc->getGainAmount(GAIN_HEALTH));
  9236.     setField(L, "manaGain", voc->getGain(GAIN_MANA));
  9237.     setField(L, "manaGainTicks", voc->getGainTicks(GAIN_MANA));
  9238.     setField(L, "manaGainAmount", voc->getGainAmount(GAIN_MANA));
  9239.     setField(L, "attackSpeed", voc->getAttackSpeed());
  9240.     setField(L, "baseSpeed", voc->getBaseSpeed());
  9241.     setField(L, "fromVocation", voc->getFromVocation());
  9242.     setField(L, "promotedVocation", Vocations::getInstance()->getPromotedVocation(id));
  9243.     setField(L, "soul", voc->getGain(GAIN_SOUL));
  9244.     setField(L, "soulAmount", voc->getGainAmount(GAIN_SOUL));
  9245.     setField(L, "soulTicks", voc->getGainTicks(GAIN_SOUL));
  9246.     setField(L, "capacity", voc->getGainCap());
  9247.     setFieldBool(L, "attackable", voc->isAttackable());
  9248.     setFieldBool(L, "needPremium", voc->isPremiumNeeded());
  9249.     setFieldFloat(L, "experienceMultiplier", voc->getExperienceMultiplier());
  9250.     return 1;
  9251. }
  9252.  
  9253. int32_t LuaInterface::luaGetGroupInfo(lua_State* L)
  9254. {
  9255.     //getGroupInfo(id[, premium = false])
  9256.     bool premium = false;
  9257.     if(lua_gettop(L) > 1)
  9258.         premium = popNumber(L);
  9259.  
  9260.     Group* group = Groups::getInstance()->getGroup(popNumber(L));
  9261.     if(!group)
  9262.     {
  9263.         lua_pushboolean(L, false);
  9264.         return 1;
  9265.     }
  9266.  
  9267.     lua_newtable(L);
  9268.     setField(L, "id", group->getId());
  9269.     setField(L, "name", group->getName().c_str());
  9270.     setField(L, "access", group->getAccess());
  9271.     setField(L, "ghostAccess", group->getGhostAccess());
  9272.     setField(L, "violationReasons", group->getViolationReasons());
  9273.     setField(L, "statementViolationFlags", group->getStatementViolationFlags());
  9274.     setField(L, "nameViolationFlags", group->getNameViolationFlags());
  9275.     setField(L, "flags", group->getFlags());
  9276.     setField(L, "customFlags", group->getCustomFlags());
  9277.     setField(L, "depotLimit", group->getDepotLimit(premium));
  9278.     setField(L, "maxVips", group->getMaxVips(premium));
  9279.     setField(L, "outfit", group->getOutfit());
  9280.     return 1;
  9281. }
  9282.  
  9283. int32_t LuaInterface::luaGetChannelUsers(lua_State* L)
  9284. {
  9285.     //getChannelUsers(channelId)
  9286.     ScriptEnviroment* env = getEnv();
  9287.     uint16_t channelId = popNumber(L);
  9288.  
  9289.     if(ChatChannel* channel = g_chat.getChannelById(channelId))
  9290.     {
  9291.         UsersMap usersMap = channel->getUsers();
  9292.         UsersMap::iterator it = usersMap.begin();
  9293.  
  9294.         lua_newtable(L);
  9295.         for(int32_t i = 1; it != usersMap.end(); ++it, ++i)
  9296.         {
  9297.             lua_pushnumber(L, i);
  9298.             lua_pushnumber(L, env->addThing(it->second));
  9299.             pushTable(L);
  9300.         }
  9301.     }
  9302.     else
  9303.         lua_pushboolean(L, false);
  9304.  
  9305.     return 1;
  9306. }
  9307.  
  9308. int32_t LuaInterface::luaGetPlayersOnline(lua_State* L)
  9309. {
  9310.     //getPlayersOnline()
  9311.     ScriptEnviroment* env = getEnv();
  9312.     AutoList<Player>::iterator it = Player::autoList.begin();
  9313.  
  9314.     lua_newtable(L);
  9315.     for(int32_t i = 1; it != Player::autoList.end(); ++it, ++i)
  9316.     {
  9317.         lua_pushnumber(L, i);
  9318.         lua_pushnumber(L, env->addThing(it->second));
  9319.         pushTable(L);
  9320.     }
  9321.  
  9322.     return 1;
  9323. }
  9324.  
  9325. int32_t LuaInterface::luaSetCreatureMaxHealth(lua_State* L)
  9326. {
  9327.     //setCreatureMaxHealth(uid, health)
  9328.     uint32_t maxHealth = (uint32_t)popNumber(L);
  9329.  
  9330.     ScriptEnviroment* env = getEnv();
  9331.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  9332.     {
  9333.         creature->changeMaxHealth(maxHealth);
  9334.         lua_pushboolean(L, true);
  9335.     }
  9336.     else
  9337.     {
  9338.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9339.         lua_pushboolean(L, false);
  9340.     }
  9341.  
  9342.     return 1;
  9343. }
  9344.  
  9345. int32_t LuaInterface::luaSetCreatureMaxMana(lua_State* L)
  9346. {
  9347.     //setCreatureMaxMana(uid, mana)
  9348.     uint32_t maxMana = (uint32_t)popNumber(L);
  9349.  
  9350.     ScriptEnviroment* env = getEnv();
  9351.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  9352.     {
  9353.         creature->changeMaxMana(maxMana);
  9354.         lua_pushboolean(L, true);
  9355.     }
  9356.     else
  9357.     {
  9358.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9359.         lua_pushboolean(L, false);
  9360.     }
  9361.  
  9362.     return 1;
  9363. }
  9364.  
  9365. int32_t LuaInterface::luaDoPlayerSetMaxCapacity(lua_State* L)
  9366. {
  9367.     //doPlayerSetMaxCapacity(uid, cap)
  9368.     double cap = popFloatNumber(L);
  9369.  
  9370.     ScriptEnviroment* env = getEnv();
  9371.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9372.     {
  9373.         player->setCapacity(cap);
  9374.         lua_pushboolean(L, true);
  9375.     }
  9376.     else
  9377.     {
  9378.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9379.         lua_pushboolean(L, false);
  9380.     }
  9381.  
  9382.     return 1;
  9383. }
  9384.  
  9385. int32_t LuaInterface::luaGetCreatureMaster(lua_State* L)
  9386. {
  9387.     //getCreatureMaster(cid)
  9388.     uint32_t cid = popNumber(L);
  9389.     ScriptEnviroment* env = getEnv();
  9390.  
  9391.     Creature* creature = env->getCreatureByUID(cid);
  9392.     if(!creature)
  9393.     {
  9394.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9395.         lua_pushboolean(L, false);
  9396.         return 1;
  9397.     }
  9398.  
  9399.     if(Creature* master = creature->getMaster())
  9400.         lua_pushnumber(L, env->addThing(master));
  9401.     else
  9402.         lua_pushnil(L);
  9403.  
  9404.     return 1;
  9405. }
  9406.  
  9407. int32_t LuaInterface::luaGetCreatureSummons(lua_State* L)
  9408. {
  9409.     //getCreatureSummons(cid)
  9410.     ScriptEnviroment* env = getEnv();
  9411.  
  9412.     Creature* creature = env->getCreatureByUID(popNumber(L));
  9413.     if(!creature)
  9414.     {
  9415.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9416.         lua_pushboolean(L, false);
  9417.         return 1;
  9418.     }
  9419.  
  9420.     const std::list<Creature*>& summons = creature->getSummons();
  9421.     CreatureList::const_iterator it = summons.begin();
  9422.  
  9423.     lua_newtable(L);
  9424.     for(uint32_t i = 1; it != summons.end(); ++it, ++i)
  9425.     {
  9426.         lua_pushnumber(L, i);
  9427.         lua_pushnumber(L, env->addThing(*it));
  9428.         pushTable(L);
  9429.     }
  9430.  
  9431.     return 1;
  9432. }
  9433.  
  9434. int32_t LuaInterface::luaDoPlayerSetIdleTime(lua_State* L)
  9435. {
  9436.     //doPlayerSetIdleTime(cid, amount)
  9437.     int64_t amount = popNumber(L);
  9438.     ScriptEnviroment* env = getEnv();
  9439.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9440.     {
  9441.         player->setIdleTime(amount);
  9442.         lua_pushboolean(L, true);
  9443.     }
  9444.     else
  9445.     {
  9446.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9447.         lua_pushboolean(L, false);
  9448.     }
  9449.  
  9450.     return 1;
  9451. }
  9452.  
  9453. int32_t LuaInterface::luaDoCreatureSetNoMove(lua_State* L)
  9454. {
  9455.     //doCreatureSetNoMove(cid, block)
  9456.     bool block = popNumber(L);
  9457.  
  9458.     ScriptEnviroment* env = getEnv();
  9459.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  9460.     {
  9461.         creature->setNoMove(block);
  9462.         lua_pushboolean(L, true);
  9463.     }
  9464.     else
  9465.     {
  9466.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9467.         lua_pushboolean(L, false);
  9468.     }
  9469.  
  9470.     return 1;
  9471. }
  9472.  
  9473. int32_t LuaInterface::luaGetPlayerModes(lua_State* L)
  9474. {
  9475.     //getPlayerModes(cid)
  9476.     ScriptEnviroment* env = getEnv();
  9477.  
  9478.     Player* player = env->getPlayerByUID(popNumber(L));
  9479.     if(!player)
  9480.     {
  9481.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9482.         lua_pushboolean(L, false);
  9483.         return 1;
  9484.     }
  9485.  
  9486.     lua_newtable(L);
  9487.     setField(L, "chase", player->getChaseMode());
  9488.     setField(L, "fight", player->getFightMode());
  9489.     setField(L, "secure", player->getSecureMode());
  9490.     return 1;
  9491. }
  9492.  
  9493. int32_t LuaInterface::luaGetPlayerRates(lua_State* L)
  9494. {
  9495.     //getPlayerRates(cid)
  9496.     ScriptEnviroment* env = getEnv();
  9497.  
  9498.     Player* player = env->getPlayerByUID(popNumber(L));
  9499.     if(!player)
  9500.     {
  9501.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9502.         lua_pushboolean(L, false);
  9503.         return 1;
  9504.     }
  9505.  
  9506.     lua_newtable(L);
  9507.     for(uint32_t i = SKILL_FIRST; i <= SKILL__LAST; ++i)
  9508.     {
  9509.         lua_pushnumber(L, i);
  9510.         lua_pushnumber(L, player->rates[(skills_t)i]);
  9511.         pushTable(L);
  9512.     }
  9513.  
  9514.     return 1;
  9515. }
  9516.  
  9517. int32_t LuaInterface::luaDoPlayerSetRate(lua_State* L)
  9518. {
  9519.     //doPlayerSetRate(cid, type, value)
  9520.     float value = popFloatNumber(L);
  9521.     uint32_t type = popNumber(L);
  9522.  
  9523.     ScriptEnviroment* env = getEnv();
  9524.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9525.     {
  9526.         if(type <= SKILL__LAST)
  9527.         {
  9528.             player->rates[(skills_t)type] = value;
  9529.             lua_pushboolean(L, true);
  9530.         }
  9531.         else
  9532.             lua_pushboolean(L, false);
  9533.     }
  9534.     else
  9535.     {
  9536.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9537.         lua_pushboolean(L, false);
  9538.     }
  9539.  
  9540.     return 1;
  9541. }
  9542.  
  9543. // change loot rates per player 1
  9544. int32_t LuaInterface::luaDoPlayerSetSpecialRateLoot(lua_State* L)
  9545. {
  9546.     //doPlayerSetSpecialRateLoot(cid, newRate)
  9547.     uint16_t newRate = (uint16_t)popNumber(L);
  9548.     ScriptEnviroment* env = getEnv();
  9549.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9550.     {
  9551.         player->setSpecialLootRate(newRate);
  9552.         lua_pushboolean(L, true);
  9553.     }
  9554.     else
  9555.     {
  9556.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9557.         lua_pushboolean(L, false);
  9558.     }
  9559.  
  9560.     return 1;
  9561. }
  9562. int32_t LuaInterface::luaDoPlayerGetSpecialRateLoot(lua_State* L)
  9563. {
  9564.     //doPlayerGetSpecialRateLoot(cid, newRate)
  9565.     ScriptEnviroment* env = getEnv();
  9566.     if(const Player* player = env->getPlayerByUID(popNumber(L)))
  9567.         lua_pushnumber(L, player->getSpecialLootRate());
  9568.     else
  9569.     {
  9570.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9571.         lua_pushboolean(L, false);
  9572.     }
  9573.     return 1;
  9574. }
  9575.  
  9576. int32_t LuaInterface::luaDoPlayerSwitchSaving(lua_State* L)
  9577. {
  9578.     //doPlayerSwitchSaving(cid)
  9579.     ScriptEnviroment* env = getEnv();
  9580.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9581.     {
  9582.         player->switchSaving();
  9583.         lua_pushboolean(L, true);
  9584.     }
  9585.     else
  9586.     {
  9587.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9588.         lua_pushboolean(L, false);
  9589.     }
  9590.  
  9591.     return 1;
  9592. }
  9593.  
  9594. int32_t LuaInterface::luaDoPlayerSave(lua_State* L)
  9595. {
  9596.     //doPlayerSave(cid[, shallow = false])
  9597.     bool shallow = false;
  9598.     if(lua_gettop(L) > 1)
  9599.         shallow = popNumber(L);
  9600.  
  9601.     ScriptEnviroment* env = getEnv();
  9602.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  9603.     {
  9604.         player->loginPosition = player->getPosition();
  9605.         lua_pushboolean(L, IOLoginData::getInstance()->savePlayer(player, false, shallow));
  9606.     }
  9607.     else
  9608.     {
  9609.         errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
  9610.         lua_pushboolean(L, false);
  9611.     }
  9612.  
  9613.     return 1;
  9614. }
  9615.  
  9616. int32_t LuaInterface::luaGetTownId(lua_State* L)
  9617. {
  9618.     //getTownId(townName)
  9619.     std::string townName = popString(L);
  9620.     if(Town* town = Towns::getInstance()->getTown(townName))
  9621.         lua_pushnumber(L, town->getID());
  9622.     else
  9623.         lua_pushboolean(L, false);
  9624.  
  9625.     return 1;
  9626. }
  9627.  
  9628. int32_t LuaInterface::luaGetTownName(lua_State* L)
  9629. {
  9630.     //getTownName(townId)
  9631.     uint32_t townId = popNumber(L);
  9632.     if(Town* town = Towns::getInstance()->getTown(townId))
  9633.         lua_pushstring(L, town->getName().c_str());
  9634.     else
  9635.         lua_pushboolean(L, false);
  9636.  
  9637.     return 1;
  9638. }
  9639.  
  9640. int32_t LuaInterface::luaGetTownTemplePosition(lua_State* L)
  9641. {
  9642.     //getTownTemplePosition(townId)
  9643.     uint32_t townId = popNumber(L);
  9644.     if(Town* town = Towns::getInstance()->getTown(townId))
  9645.         pushPosition(L, town->getPosition(), 255);
  9646.     else
  9647.         lua_pushboolean(L, false);
  9648.  
  9649.     return 1;
  9650. }
  9651.  
  9652. int32_t LuaInterface::luaGetTownHouses(lua_State* L)
  9653. {
  9654.     //getTownHouses(townId)
  9655.     uint32_t townId = 0;
  9656.     if(lua_gettop(L) > 0)
  9657.         townId = popNumber(L);
  9658.  
  9659.     HouseMap::iterator it = Houses::getInstance()->getHouseBegin();
  9660.     lua_newtable(L);
  9661.     for(uint32_t i = 1; it != Houses::getInstance()->getHouseEnd(); ++i, ++it)
  9662.     {
  9663.         if(townId != 0 && it->second->getTownId() != townId)
  9664.             continue;
  9665.  
  9666.         lua_pushnumber(L, i);
  9667.         lua_pushnumber(L, it->second->getId());
  9668.         pushTable(L);
  9669.     }
  9670.  
  9671.     return 1;
  9672. }
  9673.  
  9674. int32_t LuaInterface::luaGetSpectators(lua_State* L)
  9675. {
  9676.     //getSpectators(centerPos, rangex, rangey[, multifloor = false])
  9677.     bool multifloor = false;
  9678.     if(lua_gettop(L) > 3)
  9679.         multifloor = popNumber(L);
  9680.  
  9681.     uint32_t rangey = popNumber(L), rangex = popNumber(L);
  9682.     PositionEx centerPos;
  9683.     popPosition(L, centerPos);
  9684.  
  9685.     SpectatorVec list;
  9686.     g_game.getSpectators(list, centerPos, false, multifloor, rangex, rangex, rangey, rangey);
  9687.     if(list.empty())
  9688.     {
  9689.         lua_pushnil(L);
  9690.         return 1;
  9691.     }
  9692.  
  9693.     ScriptEnviroment* env = getEnv();
  9694.     SpectatorVec::const_iterator it = list.begin();
  9695.  
  9696.     lua_newtable(L);
  9697.     for(uint32_t i = 1; it != list.end(); ++it, ++i)
  9698.     {
  9699.         lua_pushnumber(L, i);
  9700.         lua_pushnumber(L, env->addThing(*it));
  9701.         pushTable(L);
  9702.     }
  9703.  
  9704.     return 1;
  9705. }
  9706.  
  9707. int32_t LuaInterface::luaGetHighscoreString(lua_State* L)
  9708. {
  9709.     //getHighscoreString(skillId)
  9710.     uint16_t skillId = popNumber(L);
  9711.     if(skillId <= SKILL__LAST)
  9712.         lua_pushstring(L, g_game.getHighscoreString(skillId).c_str());
  9713.     else
  9714.         lua_pushboolean(L, false);
  9715.  
  9716.     return 1;
  9717. }
  9718.  
  9719. int32_t LuaInterface::luaGetTownList(lua_State* L)
  9720. {
  9721.     //getTownList()
  9722.     lua_newtable(L);
  9723.     TownMap::const_iterator it = Towns::getInstance()->getFirstTown();
  9724.     for(uint32_t i = 1; it != Towns::getInstance()->getLastTown(); ++it, ++i)
  9725.     {
  9726.         createTable(L, i);
  9727.         setField(L, "id", it->first);
  9728.         setField(L, "name", it->second->getName());
  9729.         pushTable(L);
  9730.     }
  9731.  
  9732.     return 1;
  9733. }
  9734.  
  9735. int32_t LuaInterface::luaGetWaypointList(lua_State* L)
  9736. {
  9737.     //getWaypointList()
  9738.     WaypointMap waypointsMap = g_game.getMap()->waypoints.getWaypointsMap();
  9739.     WaypointMap::iterator it = waypointsMap.begin();
  9740.  
  9741.     lua_newtable(L);
  9742.     for(uint32_t i = 1; it != waypointsMap.end(); ++it, ++i)
  9743.     {
  9744.         createTable(L, i);
  9745.         setField(L, "name", it->first);
  9746.         setField(L, "pos", it->second->pos.x);
  9747.         pushTable(L);
  9748.     }
  9749.  
  9750.     return 1;
  9751. }
  9752.  
  9753. int32_t LuaInterface::luaGetWaypointPosition(lua_State* L)
  9754. {
  9755.     //getWaypointPosition(name)
  9756.     if(WaypointPtr waypoint = g_game.getMap()->waypoints.getWaypointByName(popString(L)))
  9757.         pushPosition(L, waypoint->pos, 0);
  9758.     else
  9759.         lua_pushboolean(L, false);
  9760.  
  9761.     return 1;
  9762. }
  9763.  
  9764. int32_t LuaInterface::luaDoWaypointAddTemporial(lua_State* L)
  9765. {
  9766.     //doWaypointAddTemporial(name, pos)
  9767.     PositionEx pos;
  9768.     popPosition(L, pos);
  9769.  
  9770.     g_game.getMap()->waypoints.addWaypoint(WaypointPtr(new Waypoint(popString(L), pos)));
  9771.     lua_pushboolean(L, true);
  9772.     return 1;
  9773. }
  9774.  
  9775. int32_t LuaInterface::luaGetGameState(lua_State* L)
  9776. {
  9777.     //getGameState()
  9778.     lua_pushnumber(L, g_game.getGameState());
  9779.     return 1;
  9780. }
  9781.  
  9782. int32_t LuaInterface::luaDoSetGameState(lua_State* L)
  9783. {
  9784.     //doSetGameState(id)
  9785.     uint32_t id = popNumber(L);
  9786.     if(id >= GAMESTATE_FIRST && id <= GAMESTATE_LAST)
  9787.     {
  9788.         Dispatcher::getInstance().addTask(createTask(
  9789.             boost::bind(&Game::setGameState, &g_game, (GameState_t)id)));
  9790.         lua_pushboolean(L, true);
  9791.     }
  9792.     else
  9793.         lua_pushboolean(L, false);
  9794.  
  9795.     return 1;
  9796. }
  9797.  
  9798. int32_t LuaInterface::luaDoCreatureExecuteTalkAction(lua_State* L)
  9799. {
  9800.     //doCreatureExecuteTalkAction(cid, text[, ignoreAccess = false[, channelId = CHANNEL_DEFAULT]])
  9801.     uint32_t params = lua_gettop(L), channelId = CHANNEL_DEFAULT;
  9802.     if(params > 3)
  9803.         channelId = popNumber(L);
  9804.  
  9805.     bool ignoreAccess = false;
  9806.     if(params > 2)
  9807.         ignoreAccess = popNumber(L);
  9808.  
  9809.     std::string text = popString(L);
  9810.     ScriptEnviroment* env = getEnv();
  9811.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
  9812.         lua_pushboolean(L, g_talkActions->onPlayerSay(creature, channelId, text, ignoreAccess));
  9813.     else
  9814.     {
  9815.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
  9816.         lua_pushboolean(L, false);
  9817.     }
  9818.  
  9819.     return 1;
  9820. }
  9821.  
  9822. int32_t LuaInterface::luaDoExecuteRaid(lua_State* L)
  9823. {
  9824.     //doExecuteRaid(name)
  9825.     std::string raidName = popString(L);
  9826.     if(Raids::getInstance()->getRunning())
  9827.     {
  9828.         lua_pushboolean(L, false);
  9829.         return 1;
  9830.     }
  9831.  
  9832.     Raid* raid = Raids::getInstance()->getRaidByName(raidName);
  9833.     if(!raid || !raid->isLoaded())
  9834.     {
  9835.         errorEx("Raid with name " + raidName + " does not exists.");
  9836.         lua_pushboolean(L, false);
  9837.         return 1;
  9838.     }
  9839.  
  9840.     lua_pushboolean(L, raid->startRaid());
  9841.     return 1;
  9842. }
  9843.  
  9844. int32_t LuaInterface::luaDoReloadInfo(lua_State* L)
  9845. {
  9846.     //doReloadInfo(id[, cid])
  9847.     uint32_t cid = 0;
  9848.     if(lua_gettop(L) > 1)
  9849.         cid = popNumber(L);
  9850.  
  9851.     uint32_t id = popNumber(L);
  9852.     if(id >= RELOAD_FIRST && id <= RELOAD_LAST)
  9853.     {
  9854.         // we're passing it to scheduler since talkactions reload will
  9855.         // re-init our lua state and crash due to unfinished call
  9856.         Scheduler::getInstance().addEvent(createSchedulerTask(SCHEDULER_MINTICKS,
  9857.             boost::bind(&Game::reloadInfo, &g_game, (ReloadInfo_t)id, cid)));
  9858.         lua_pushboolean(L, true);
  9859.     }
  9860.     else
  9861.         lua_pushboolean(L, false);
  9862.  
  9863.     return 1;
  9864. }
  9865.  
  9866. int32_t LuaInterface::luaDoSaveServer(lua_State* L)
  9867. {
  9868.     //doSaveServer([shallow = false])
  9869.     bool shallow = false;
  9870.     if(lua_gettop(L) > 0)
  9871.         shallow = popNumber(L);
  9872.  
  9873.     Dispatcher::getInstance().addTask(createTask(boost::bind(&Game::saveGameState, &g_game, shallow)));
  9874.     lua_pushnil(L);
  9875.     return 1;
  9876. }
  9877.  
  9878. int32_t LuaInterface::luaDoCleanHouse(lua_State* L)
  9879. {
  9880.     //doCleanHouse(houseId)
  9881.     uint32_t houseId = popNumber(L);
  9882.     if(House* house = Houses::getInstance()->getHouse(houseId))
  9883.     {
  9884.         house->clean();
  9885.         lua_pushboolean(L, true);
  9886.     }
  9887.     else
  9888.         lua_pushboolean(L, false);
  9889.  
  9890.     return 1;
  9891. }
  9892.  
  9893. int32_t LuaInterface::luaDoCleanMap(lua_State* L)
  9894. {
  9895.     //doCleanMap()
  9896.     uint32_t count = 0;
  9897.     g_game.cleanMapEx(count);
  9898.     lua_pushnumber(L, count);
  9899.     return 1;
  9900. }
  9901.  
  9902. int32_t LuaInterface::luaDoRefreshMap(lua_State* L)
  9903. {
  9904.     //doRefreshMap()
  9905.     g_game.proceduralRefresh();
  9906.     lua_pushnil(L);
  9907.     return 1;
  9908. }
  9909.  
  9910. int32_t LuaInterface::luaDoUpdateHouseAuctions(lua_State* L)
  9911. {
  9912.     //doUpdateHouseAuctions()
  9913.     lua_pushboolean(L, IOMapSerialize::getInstance()->updateAuctions());
  9914.     return 1;
  9915. }
  9916.  
  9917. int32_t LuaInterface::luaGetItemIdByName(lua_State* L)
  9918. {
  9919.     //getItemIdByName(name[, displayError = true])
  9920.     bool displayError = true;
  9921.     if(lua_gettop(L) >= 2)
  9922.         displayError = popNumber(L);
  9923.  
  9924.     int32_t itemId = Item::items.getItemIdByName(popString(L));
  9925.     if(itemId == -1)
  9926.     {
  9927.         if(displayError)
  9928.             errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  9929.  
  9930.         lua_pushboolean(L, false);
  9931.     }
  9932.     else
  9933.         lua_pushnumber(L, itemId);
  9934.  
  9935.     return 1;
  9936. }
  9937.  
  9938. int32_t LuaInterface::luaGetItemInfo(lua_State* L)
  9939. {
  9940.     //getItemInfo(itemid)
  9941.     const ItemType* item;
  9942.     if(!(item = Item::items.getElement(popNumber(L))))
  9943.     {
  9944.         lua_pushboolean(L, false);
  9945.         return 1;
  9946.     }
  9947.  
  9948.     lua_newtable(L);
  9949.     setFieldBool(L, "stopTime", item->stopTime);
  9950.     setFieldBool(L, "showCount", item->showCount);
  9951.     setFieldBool(L, "stackable", item->stackable);
  9952.     setFieldBool(L, "showDuration", item->showDuration);
  9953.     setFieldBool(L, "showCharges", item->showCharges);
  9954.     setFieldBool(L, "showAttributes", item->showAttributes);
  9955.     setFieldBool(L, "distRead", item->allowDistRead);
  9956.     setFieldBool(L, "readable", item->canReadText);
  9957.     setFieldBool(L, "writable", item->canWriteText);
  9958.     setFieldBool(L, "forceSerialize", item->forceSerialize);
  9959.     setFieldBool(L, "vertical", item->isVertical);
  9960.     setFieldBool(L, "horizontal", item->isHorizontal);
  9961.     setFieldBool(L, "hangable", item->isHangable);
  9962.     setFieldBool(L, "usable", item->useable);
  9963.     setFieldBool(L, "movable", item->moveable);
  9964.     setFieldBool(L, "pickupable", item->pickupable);
  9965.     setFieldBool(L, "rotable", item->rotable);
  9966.     setFieldBool(L, "replacable", item->replaceable);
  9967.     setFieldBool(L, "hasHeight", item->hasHeight);
  9968.     setFieldBool(L, "blockSolid", item->blockSolid);
  9969.     setFieldBool(L, "blockPickupable", item->blockPickupable);
  9970.     setFieldBool(L, "blockProjectile", item->blockProjectile);
  9971.     setFieldBool(L, "blockPathing", item->blockPathFind);
  9972.     setFieldBool(L, "allowPickupable", item->allowPickupable);
  9973.     setFieldBool(L, "alwaysOnTop", item->alwaysOnTop);
  9974.  
  9975.     createTable(L, "floorChange");
  9976.     for(int32_t i = CHANGE_FIRST; i <= CHANGE_LAST; ++i)
  9977.     {
  9978.         lua_pushnumber(L, i);
  9979.         lua_pushboolean(L, item->floorChange[i - 1]);
  9980.         pushTable(L);
  9981.     }
  9982.  
  9983.     pushTable(L);
  9984.     setField(L, "magicEffect", (int32_t)item->magicEffect);
  9985.     setField(L, "fluidSource", (int32_t)item->fluidSource);
  9986.     setField(L, "weaponType", (int32_t)item->weaponType);
  9987.     setField(L, "bedPartnerDirection", (int32_t)item->bedPartnerDir);
  9988.     setField(L, "ammoAction", (int32_t)item->ammoAction);
  9989.     setField(L, "combatType", (int32_t)item->combatType);
  9990.     setField(L, "corpseType", (int32_t)item->corpseType);
  9991.     setField(L, "shootType", (int32_t)item->shootType);
  9992.     setField(L, "ammoType", (int32_t)item->ammoType);
  9993.  
  9994.     createTable(L, "transformUseTo");
  9995.     setField(L, "female", item->transformUseTo[PLAYERSEX_FEMALE]);
  9996.     setField(L, "male", item->transformUseTo[PLAYERSEX_MALE]);
  9997.  
  9998.     pushTable(L);
  9999.     setField(L, "transformToFree", item->transformToFree);
  10000.     setField(L, "transformEquipTo", item->transformEquipTo);
  10001.     setField(L, "transformDeEquipTo", item->transformDeEquipTo);
  10002.     setField(L, "clientId", item->clientId);
  10003.     setField(L, "maxItems", item->maxItems);
  10004.     setField(L, "slotPosition", item->slotPosition);
  10005.     setField(L, "wieldPosition", item->wieldPosition);
  10006.     setField(L, "speed", item->speed);
  10007.     setField(L, "maxTextLength", item->maxTextLen);
  10008.     setField(L, "writeOnceItemId", item->writeOnceItemId);
  10009.     setField(L, "attack", item->attack);
  10010.     setField(L, "extraAttack", item->extraAttack);
  10011.     setField(L, "defense", item->defense);
  10012.     setField(L, "extraDefense", item->extraDefense);
  10013.     setField(L, "armor", item->armor);
  10014.     setField(L, "breakChance", item->breakChance);
  10015.     setField(L, "hitChance", item->hitChance);
  10016.     setField(L, "maxHitChance", item->maxHitChance);
  10017.     setField(L, "runeLevel", item->runeLevel);
  10018.     setField(L, "runeMagicLevel", item->runeMagLevel);
  10019.     setField(L, "lightLevel", item->lightLevel);
  10020.     setField(L, "lightColor", item->lightColor);
  10021.     setField(L, "decayTo", item->decayTo);
  10022.     setField(L, "rotateTo", item->rotateTo);
  10023.     setField(L, "alwaysOnTopOrder", item->alwaysOnTopOrder);
  10024.     setField(L, "shootRange", item->shootRange);
  10025.     setField(L, "charges", item->charges);
  10026.     setField(L, "decayTime", item->decayTime);
  10027.     setField(L, "attackSpeed", item->attackSpeed);
  10028.     setField(L, "wieldInfo", item->wieldInfo);
  10029.     setField(L, "minRequiredLevel", item->minReqLevel);
  10030.     setField(L, "minRequiredMagicLevel", item->minReqMagicLevel);
  10031.     setField(L, "worth", item->worth);
  10032.     setField(L, "levelDoor", item->levelDoor);
  10033.     setField(L, "name", item->name.c_str());
  10034.     setField(L, "plural", item->pluralName.c_str());
  10035.     setField(L, "article", item->article.c_str());
  10036.     setField(L, "description", item->description.c_str());
  10037.     setField(L, "runeSpellName", item->runeSpellName.c_str());
  10038.     setField(L, "vocationString", item->vocationString.c_str());
  10039.  
  10040.     createTable(L, "abilities");
  10041.     setFieldBool(L, "manaShield", item->abilities.manaShield);
  10042.     setFieldBool(L, "invisible", item->abilities.invisible);
  10043.     setFieldBool(L, "regeneration", item->abilities.regeneration);
  10044.     setFieldBool(L, "preventLoss", item->abilities.preventLoss);
  10045.     setFieldBool(L, "preventDrop", item->abilities.preventDrop);
  10046.     setField(L, "elementType", (int32_t)item->abilities.elementType);
  10047.     setField(L, "elementDamage", item->abilities.elementDamage);
  10048.     setField(L, "speed", item->abilities.speed);
  10049.     setField(L, "healthGain", item->abilities.healthGain);
  10050.     setField(L, "healthTicks", item->abilities.healthTicks);
  10051.     setField(L, "manaGain", item->abilities.manaGain);
  10052.     setField(L, "manaTicks", item->abilities.manaTicks);
  10053.     setField(L, "conditionSuppressions", item->abilities.conditionSuppressions);
  10054.  
  10055.     //TODO: absorb, increment, reflect, skills, skillsPercent, stats, statsPercent
  10056.  
  10057.     pushTable(L);
  10058.     setField(L, "group", (int32_t)item->group);
  10059.     setField(L, "type", (int32_t)item->type);
  10060.     setFieldFloat(L, "weight", item->weight);
  10061.     return 1;
  10062. }
  10063.  
  10064. int32_t LuaInterface::luaIsPlayerUsingOtclient(lua_State* L)
  10065. {
  10066.     //isPlayerUsingOtclient(cid)
  10067.     ScriptEnviroment* env = getEnv();
  10068.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  10069.     {
  10070.         lua_pushboolean(L, player->isUsingOtclient());
  10071.     }
  10072.  
  10073.     lua_pushboolean(L, false);
  10074.     return 1;
  10075. }
  10076.  
  10077. int32_t LuaInterface::luaDoPlayerSendExtendedOpcode(lua_State* L)
  10078. {
  10079.     //doPlayerSendExtendedOpcode(cid, opcode, buffer)
  10080.     std::string buffer = popString(L);
  10081.     int32_t opcode = popNumber(L);
  10082.  
  10083.     ScriptEnviroment* env = getEnv();
  10084.     if(Player* player = env->getPlayerByUID(popNumber(L)))
  10085.     {
  10086.         player->sendExtendedOpcode(opcode, buffer);
  10087.         lua_pushboolean(L, true);
  10088.     }
  10089.  
  10090.     lua_pushboolean(L, false);
  10091.     return 1;
  10092. }
  10093.  
  10094. int32_t LuaInterface::luaGetItemAttribute(lua_State* L)
  10095. {
  10096.     //getItemAttribute(uid, key)
  10097.     std::string key = popString(L);
  10098.     ScriptEnviroment* env = getEnv();
  10099.  
  10100.     Item* item = env->getItemByUID(popNumber(L));
  10101.     if(!item)
  10102.     {
  10103.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  10104.         lua_pushnil(L);
  10105.         return 1;
  10106.     }
  10107.  
  10108.     boost::any value = item->getAttribute(key);
  10109.     if(value.empty())
  10110.         lua_pushnil(L);
  10111.     else if(value.type() == typeid(std::string))
  10112.         lua_pushstring(L, boost::any_cast<std::string>(value).c_str());
  10113.     else if(value.type() == typeid(int32_t))
  10114.         lua_pushnumber(L, boost::any_cast<int32_t>(value));
  10115.     else if(value.type() == typeid(float))
  10116.         lua_pushnumber(L, boost::any_cast<float>(value));
  10117.     else if(value.type() == typeid(bool))
  10118.         lua_pushboolean(L, boost::any_cast<bool>(value));
  10119.     else
  10120.         lua_pushnil(L);
  10121.  
  10122.     return 1;
  10123. }
  10124.  
  10125. int32_t LuaInterface::luaDoItemSetAttribute(lua_State* L)
  10126. {
  10127.     //doItemSetAttribute(uid, key, value)
  10128.     boost::any value;
  10129.     if(lua_isnumber(L, -1))
  10130.     {
  10131.         float tmp = popFloatNumber(L);
  10132.         if(std::floor(tmp) < tmp)
  10133.             value = tmp;
  10134.         else
  10135.             value = (int32_t)tmp;
  10136.     }
  10137.     else if(lua_isboolean(L, -1))
  10138.         value = popBoolean(L);
  10139.     else if(lua_isstring(L, -1))
  10140.         value = popString(L);
  10141.     else
  10142.     {
  10143.         lua_pop(L, 1);
  10144.         errorEx("Invalid data type");
  10145.  
  10146.         lua_pushboolean(L, false);
  10147.         return 1;
  10148.     }
  10149.  
  10150.     std::string key = popString(L);
  10151.     ScriptEnviroment* env = getEnv();
  10152.  
  10153.     Item* item = env->getItemByUID(popNumber(L));
  10154.     if(!item)
  10155.     {
  10156.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  10157.         lua_pushboolean(L, false);
  10158.         return 1;
  10159.     }
  10160.  
  10161.     if(value.type() == typeid(int32_t))
  10162.     {
  10163.         if(key == "uid")
  10164.         {
  10165.             int32_t tmp = boost::any_cast<int32_t>(value);
  10166.             if(tmp < 1000 || tmp > 0xFFFF)
  10167.             {
  10168.                 errorEx("Value for protected key \"uid\" must be in range of 1000 to 65535");
  10169.                 lua_pushboolean(L, false);
  10170.                 return 1;
  10171.             }
  10172.  
  10173.             item->setUniqueId(tmp);
  10174.         }
  10175.         else if(key == "aid")
  10176.             item->setActionId(boost::any_cast<int32_t>(value));
  10177.         else
  10178.             item->setAttribute(key, boost::any_cast<int32_t>(value));
  10179.     }
  10180.     else
  10181.         item->setAttribute(key, value);
  10182.  
  10183.     lua_pushboolean(L, true);
  10184.     return 1;
  10185. }
  10186.  
  10187. int32_t LuaInterface::luaDoItemEraseAttribute(lua_State* L)
  10188. {
  10189.     //doItemEraseAttribute(uid, key)
  10190.     std::string key = popString(L);
  10191.     ScriptEnviroment* env = getEnv();
  10192.  
  10193.     Item* item = env->getItemByUID(popNumber(L));
  10194.     if(!item)
  10195.     {
  10196.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  10197.         lua_pushboolean(L, false);
  10198.         return 1;
  10199.     }
  10200.  
  10201.     bool ret = true;
  10202.     if(key == "uid")
  10203.     {
  10204.         errorEx("Attempt to erase protected key \"uid\".");
  10205.         ret = false;
  10206.     }
  10207.     else if(key != "aid")
  10208.         item->eraseAttribute(key);
  10209.     else
  10210.         item->resetActionId();
  10211.  
  10212.     lua_pushboolean(L, ret);
  10213.     return 1;
  10214. }
  10215.  
  10216. int32_t LuaInterface::luaGetItemWeight(lua_State* L)
  10217. {
  10218.     //getItemWeight(itemid[, precise = true])
  10219.     bool precise = true;
  10220.     if(lua_gettop(L) > 2)
  10221.         precise = popNumber(L);
  10222.  
  10223.     ScriptEnviroment* env = getEnv();
  10224.     Item* item = env->getItemByUID(popNumber(L));
  10225.     if(!item)
  10226.     {
  10227.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  10228.         lua_pushboolean(L, false);
  10229.         return 1;
  10230.     }
  10231.  
  10232.     double weight = item->getWeight();
  10233.     if(precise)
  10234.     {
  10235.         std::stringstream ws;
  10236.         ws << std::fixed << std::setprecision(2) << weight;
  10237.         weight = atof(ws.str().c_str());
  10238.     }
  10239.  
  10240.     lua_pushnumber(L, weight);
  10241.     return 1;
  10242. }
  10243.  
  10244. int32_t LuaInterface::luaGetItemParent(lua_State* L)
  10245. {
  10246.     //getItemParent(uid)
  10247.     ScriptEnviroment* env = getEnv();
  10248.  
  10249.     Item* item = env->getItemByUID(popNumber(L));
  10250.     if(!item)
  10251.     {
  10252.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  10253.         lua_pushnil(L);
  10254.         return 1;
  10255.     }
  10256.  
  10257.     Item* container = item->getParent()->getItem();
  10258.     pushThing(L, container, env->addThing(container));
  10259.     return 1;
  10260. }
  10261.  
  10262. int32_t LuaInterface::luaHasItemProperty(lua_State* L)
  10263. {
  10264.     //hasItemProperty(uid, prop)
  10265.     uint32_t prop = popNumber(L);
  10266.     ScriptEnviroment* env = getEnv();
  10267.  
  10268.     Item* item = env->getItemByUID(popNumber(L));
  10269.     if(!item)
  10270.     {
  10271.         errorEx(getError(LUA_ERROR_ITEM_NOT_FOUND));
  10272.         lua_pushboolean(L, false);
  10273.         return 1;
  10274.     }
  10275.  
  10276.     //Check if the item is a tile, so we can get more accurate properties
  10277.     bool tmp = item->hasProperty((ITEMPROPERTY)prop);
  10278.     if(item->getTile() && item->getTile()->ground == item)
  10279.         tmp = item->getTile()->hasProperty((ITEMPROPERTY)prop);
  10280.  
  10281.     lua_pushboolean(L, tmp);
  10282.     return 1;
  10283. }
  10284.  
  10285. int32_t LuaInterface::luaIsIpBanished(lua_State* L)
  10286. {
  10287.     //isIpBanished(ip[, mask])
  10288.     uint32_t mask = 0xFFFFFFFF;
  10289.     if(lua_gettop(L) > 1)
  10290.         mask = popNumber(L);
  10291.  
  10292.     lua_pushboolean(L, IOBan::getInstance()->isIpBanished((uint32_t)popNumber(L), mask));
  10293.     return 1;
  10294. }
  10295.  
  10296. int32_t LuaInterface::luaIsPlayerBanished(lua_State* L)
  10297. {
  10298.     //isPlayerBanished(name/guid, type)
  10299.     PlayerBan_t type = (PlayerBan_t)popNumber(L);
  10300.     if(lua_isnumber(L, -1))
  10301.         lua_pushboolean(L, IOBan::getInstance()->isPlayerBanished((uint32_t)popNumber(L), type));
  10302.     else
  10303.         lua_pushboolean(L, IOBan::getInstance()->isPlayerBanished(popString(L), type));
  10304.  
  10305.     return 1;
  10306. }
  10307.  
  10308. int32_t LuaInterface::luaIsAccountBanished(lua_State* L)
  10309. {
  10310.     //isAccountBanished(accountId[, playerId])
  10311.     uint32_t playerId = 0;
  10312.     if(lua_gettop(L) > 1)
  10313.         playerId = popNumber(L);
  10314.  
  10315.     lua_pushboolean(L, IOBan::getInstance()->isAccountBanished((uint32_t)popNumber(L), playerId));
  10316.     return 1;
  10317. }
  10318.  
  10319. int32_t LuaInterface::luaDoAddIpBanishment(lua_State* L)
  10320. {
  10321.     //doAddIpBanishment(ip[, mask[, length[, reason[, comment[, admin[, statement]]]]]])
  10322.     uint32_t admin = 0, reason = 21, mask = 0xFFFFFFFF, params = lua_gettop(L);
  10323.     int64_t length = time(NULL) + g_config.getNumber(ConfigManager::IPBANISHMENT_LENGTH);
  10324.     std::string statement, comment;
  10325.  
  10326.     if(params > 6)
  10327.         statement = popString(L);
  10328.  
  10329.     if(params > 5)
  10330.         admin = popNumber(L);
  10331.  
  10332.     if(params > 4)
  10333.         comment = popString(L);
  10334.  
  10335.     if(params > 3)
  10336.         reason = popNumber(L);
  10337.  
  10338.     if(params > 2)
  10339.         length = popNumber(L);
  10340.  
  10341.     if(params > 1)
  10342.         mask = popNumber(L);
  10343.  
  10344.     lua_pushboolean(L, IOBan::getInstance()->addIpBanishment((uint32_t)popNumber(L),
  10345.         length, reason, comment, admin, mask, statement));
  10346.     return 1;
  10347. }
  10348.  
  10349. int32_t LuaInterface::luaDoAddPlayerBanishment(lua_State* L)
  10350. {
  10351.     //doAddPlayerBanishment(name/guid[, type[, length[, reason[, action[, comment[, admin[, statement]]]]]]])
  10352.     uint32_t admin = 0, reason = 21, params = lua_gettop(L);
  10353.     int64_t length = -1;
  10354.     std::string statement, comment;
  10355.  
  10356.     ViolationAction_t action = ACTION_NAMELOCK;
  10357.     PlayerBan_t type = PLAYERBAN_LOCK;
  10358.     if(params > 7)
  10359.         statement = popString(L);
  10360.  
  10361.     if(params > 6)
  10362.         admin = popNumber(L);
  10363.  
  10364.     if(params > 5)
  10365.         comment = popString(L);
  10366.  
  10367.     if(params > 4)
  10368.         action = (ViolationAction_t)popNumber(L);
  10369.  
  10370.     if(params > 3)
  10371.         reason = popNumber(L);
  10372.  
  10373.     if(params > 2)
  10374.         length = popNumber(L);
  10375.  
  10376.     if(params > 1)
  10377.         type = (PlayerBan_t)popNumber(L);
  10378.  
  10379.     if(lua_isnumber(L, -1))
  10380.         lua_pushboolean(L, IOBan::getInstance()->addPlayerBanishment((uint32_t)popNumber(L),
  10381.             length, reason, action, comment, admin, type, statement));
  10382.     else
  10383.         lua_pushboolean(L, IOBan::getInstance()->addPlayerBanishment(popString(L),
  10384.             length, reason, action, comment, admin, type, statement));
  10385.  
  10386.     return 1;
  10387. }
  10388.  
  10389. int32_t LuaInterface::luaDoAddAccountBanishment(lua_State* L)
  10390. {
  10391.     //doAddAccountBanishment(accountId[, playerId[, length[, reason[, action[, comment[, admin[, statement]]]]]]])
  10392.     uint32_t admin = 0, reason = 21, playerId = 0, params = lua_gettop(L);
  10393.     int64_t length = time(NULL) + g_config.getNumber(ConfigManager::BAN_LENGTH);
  10394.     std::string statement, comment;
  10395.  
  10396.     ViolationAction_t action = ACTION_BANISHMENT;
  10397.     if(params > 7)
  10398.         statement = popString(L);
  10399.  
  10400.     if(params > 6)
  10401.         admin = popNumber(L);
  10402.  
  10403.     if(params > 5)
  10404.         comment = popString(L);
  10405.  
  10406.     if(params > 4)
  10407.         action = (ViolationAction_t)popNumber(L);
  10408.  
  10409.     if(params > 3)
  10410.  
  10411.         reason = popNumber(L);
  10412.  
  10413.     if(params > 2)
  10414.         length = popNumber(L);
  10415.  
  10416.     if(params > 1)
  10417.         playerId = popNumber(L);
  10418.  
  10419.     lua_pushboolean(L, IOBan::getInstance()->addAccountBanishment((uint32_t)popNumber(L),
  10420.         length, reason, action, comment, admin, playerId, statement));
  10421.     return 1;
  10422. }
  10423.  
  10424. int32_t LuaInterface::luaDoAddNotation(lua_State* L)
  10425. {
  10426.     //doAddNotation(accountId[, playerId[, reason[, comment[, admin[, statement]]]]]])
  10427.     uint32_t admin = 0, reason = 21, playerId = 0, params = lua_gettop(L);
  10428.     std::string statement, comment;
  10429.  
  10430.     if(params > 5)
  10431.         statement = popString(L);
  10432.  
  10433.     if(params > 4)
  10434.         admin = popNumber(L);
  10435.  
  10436.     if(params > 3)
  10437.         comment = popString(L);
  10438.  
  10439.     if(params > 2)
  10440.         reason = popNumber(L);
  10441.  
  10442.     if(params > 1)
  10443.         playerId = popNumber(L);
  10444.  
  10445.     lua_pushboolean(L, IOBan::getInstance()->addNotation((uint32_t)popNumber(L),
  10446.         reason, comment, admin, playerId, statement));
  10447.     return 1;
  10448. }
  10449.  
  10450. int32_t LuaInterface::luaDoAddStatement(lua_State* L)
  10451. {
  10452.     //doAddStatement(name/guid[, channelId[, reason[, comment[, admin[, statement]]]]]])
  10453.     uint32_t admin = 0, reason = 21, params = lua_gettop(L);
  10454.     int16_t channelId = -1;
  10455.     std::string statement, comment;
  10456.  
  10457.     if(params > 5)
  10458.         statement = popString(L);
  10459.  
  10460.     if(params > 4)
  10461.         admin = popNumber(L);
  10462.  
  10463.     if(params > 3)
  10464.         comment = popString(L);
  10465.  
  10466.     if(params > 2)
  10467.         reason = popNumber(L);
  10468.  
  10469.     if(params > 1)
  10470.         channelId = popNumber(L);
  10471.  
  10472.     if(lua_isnumber(L, -1))
  10473.         lua_pushboolean(L, IOBan::getInstance()->addStatement((uint32_t)popNumber(L),
  10474.             reason, comment, admin, channelId, statement));
  10475.     else
  10476.         lua_pushboolean(L, IOBan::getInstance()->addStatement(popString(L),
  10477.             reason, comment, admin, channelId, statement));
  10478.  
  10479.     return 1;
  10480. }
  10481.  
  10482. int32_t LuaInterface::luaDoRemoveIpBanishment(lua_State* L)
  10483. {
  10484.     //doRemoveIpBanishment(ip[, mask])
  10485.     uint32_t mask = 0xFFFFFFFF;
  10486.     if(lua_gettop(L) > 1)
  10487.         mask = popNumber(L);
  10488.  
  10489.     lua_pushboolean(L, IOBan::getInstance()->removeIpBanishment(
  10490.         (uint32_t)popNumber(L), mask));
  10491.     return 1;
  10492. }
  10493.  
  10494. int32_t LuaInterface::luaDoRemovePlayerBanishment(lua_State* L)
  10495. {
  10496.     //doRemovePlayerBanishment(name/guid, type)
  10497.     PlayerBan_t type = (PlayerBan_t)popNumber(L);
  10498.     if(lua_isnumber(L, -1))
  10499.         lua_pushboolean(L, IOBan::getInstance()->removePlayerBanishment((uint32_t)popNumber(L), type));
  10500.     else
  10501.         lua_pushboolean(L, IOBan::getInstance()->removePlayerBanishment(popString(L), type));
  10502.  
  10503.     return 1;
  10504. }
  10505.  
  10506. int32_t LuaInterface::luaDoRemoveAccountBanishment(lua_State* L)
  10507. {
  10508.     //doRemoveAccountBanishment(accountId[, playerId])
  10509.     uint32_t playerId = 0;
  10510.     if(lua_gettop(L) > 1)
  10511.         playerId = popNumber(L);
  10512.  
  10513.     lua_pushboolean(L, IOBan::getInstance()->removeAccountBanishment((uint32_t)popNumber(L), playerId));
  10514.     return 1;
  10515. }
  10516.  
  10517. int32_t LuaInterface::luaDoRemoveNotations(lua_State* L)
  10518. {
  10519.     //doRemoveNotations(accountId[, playerId])
  10520.     uint32_t playerId = 0;
  10521.     if(lua_gettop(L) > 1)
  10522.         playerId = popNumber(L);
  10523.  
  10524.     lua_pushboolean(L, IOBan::getInstance()->removeNotations((uint32_t)popNumber(L), playerId));
  10525.     return 1;
  10526. }
  10527.  
  10528. int32_t LuaInterface::luaDoRemoveStatements(lua_State* L)
  10529. {
  10530.     //doRemoveStatements(name/guid[, channelId])
  10531.     int16_t channelId = -1;
  10532.     if(lua_gettop(L) > 1)
  10533.         channelId = popNumber(L);
  10534.  
  10535.     if(lua_isnumber(L, -1))
  10536.         lua_pushboolean(L, IOBan::getInstance()->removeStatements((uint32_t)popNumber(L), channelId));
  10537.     else
  10538.         lua_pushboolean(L, IOBan::getInstance()->removeStatements(popString(L), channelId));
  10539.  
  10540.     return 1;
  10541. }
  10542.  
  10543. int32_t LuaInterface::luaGetNotationsCount(lua_State* L)
  10544. {
  10545.     //getNotationsCount(accountId[, playerId])
  10546.     uint32_t playerId = 0;
  10547.     if(lua_gettop(L) > 1)
  10548.         playerId = popNumber(L);
  10549.  
  10550.     lua_pushnumber(L, IOBan::getInstance()->getNotationsCount((uint32_t)popNumber(L), playerId));
  10551.     return 1;
  10552. }
  10553.  
  10554. int32_t LuaInterface::luaGetStatementsCount(lua_State* L)
  10555. {
  10556.     //getStatementsCount(name/guid[, channelId])
  10557.     int16_t channelId = -1;
  10558.     if(lua_gettop(L) > 1)
  10559.         channelId = popNumber(L);
  10560.  
  10561.     if(lua_isnumber(L, -1))
  10562.         lua_pushnumber(L, IOBan::getInstance()->getStatementsCount((uint32_t)popNumber(L), channelId));
  10563.     else
  10564.         lua_pushnumber(L, IOBan::getInstance()->getStatementsCount(popString(L), channelId));
  10565.  
  10566.     return 1;
  10567. }
  10568.  
  10569. int32_t LuaInterface::luaGetBanData(lua_State* L)
  10570. {
  10571.     //getBanData(value[, type[, param]])
  10572.     Ban tmp;
  10573.     uint32_t params = lua_gettop(L);
  10574.     if(params > 2)
  10575.         tmp.param = popNumber(L);
  10576.  
  10577.     if(params > 1)
  10578.         tmp.type = (Ban_t)popNumber(L);
  10579.  
  10580.     tmp.value = popNumber(L);
  10581.     if(!IOBan::getInstance()->getData(tmp))
  10582.     {
  10583.         lua_pushboolean(L, false);
  10584.         return 1;
  10585.     }
  10586.  
  10587.     lua_newtable(L);
  10588.     setField(L, "id", tmp.id);
  10589.     setField(L, "type", tmp.type);
  10590.     setField(L, "value", tmp.value);
  10591.     setField(L, "param", tmp.param);
  10592.     setField(L, "added", tmp.added);
  10593.     setField(L, "expires", tmp.expires);
  10594.     setField(L, "adminId", tmp.adminId);
  10595.     setField(L, "reason", tmp.reason);
  10596.     setField(L, "action", tmp.action);
  10597.     setField(L, "comment", tmp.comment);
  10598.     setField(L, "statement", tmp.statement);
  10599.     return 1;
  10600. }
  10601.  
  10602. int32_t LuaInterface::luaGetBanReason(lua_State* L)
  10603. {
  10604.     //getBanReason(id)
  10605.     lua_pushstring(L, getReason((ViolationAction_t)popNumber(L)).c_str());
  10606.     return 1;
  10607. }
  10608.  
  10609. int32_t LuaInterface::luaGetBanAction(lua_State* L)
  10610. {
  10611.     //getBanAction(id[, ipBanishment = false])
  10612.     bool ipBanishment = false;
  10613.     if(lua_gettop(L) > 1)
  10614.         ipBanishment = popNumber(L);
  10615.  
  10616.     lua_pushstring(L, getAction((ViolationAction_t)popNumber(L), ipBanishment).c_str());
  10617.     return 1;
  10618. }
  10619.  
  10620. int32_t LuaInterface::luaGetBanList(lua_State* L)
  10621. {
  10622.     //getBanList(type[, value[, param]])
  10623.     int32_t param = 0, params = lua_gettop(L);
  10624.     if(params > 2)
  10625.         param = popNumber(L);
  10626.  
  10627.     uint32_t value = 0;
  10628.     if(params > 1)
  10629.         value = popNumber(L);
  10630.  
  10631.     BansVec bans = IOBan::getInstance()->getList((Ban_t)popNumber(L), value, param);
  10632.     BansVec::const_iterator it = bans.begin();
  10633.  
  10634.     lua_newtable(L);
  10635.     for(uint32_t i = 1; it != bans.end(); ++it, ++i)
  10636.     {
  10637.         createTable(L, i);
  10638.         setField(L, "id", it->id);
  10639.         setField(L, "type", it->type);
  10640.         setField(L, "value", it->value);
  10641.         setField(L, "param", it->param);
  10642.         setField(L, "added", it->added);
  10643.         setField(L, "expires", it->expires);
  10644.         setField(L, "adminId", it->adminId);
  10645.         setField(L, "reason", it->reason);
  10646.         setField(L, "action", it->action);
  10647.         setField(L, "comment", it->comment);
  10648.         setField(L, "statement", it->statement);
  10649.         pushTable(L);
  10650.     }
  10651.  
  10652.     return 1;
  10653. }
  10654.  
  10655. int32_t LuaInterface::luaGetExperienceStage(lua_State* L)
  10656. {
  10657.     //getExperienceStage(level[, divider])
  10658.     double divider = 1.0f;
  10659.     if(lua_gettop(L) > 1)
  10660.         divider = popFloatNumber(L);
  10661.  
  10662.     lua_pushnumber(L, g_game.getExperienceStage(popNumber(L), divider));
  10663.     return 1;
  10664. }
  10665.  
  10666. int32_t LuaInterface::luaGetDataDir(lua_State* L)
  10667. {
  10668.     //getDataDir()
  10669.     lua_pushstring(L, getFilePath(FILE_TYPE_OTHER, "").c_str());
  10670.     return 1;
  10671. }
  10672.  
  10673. int32_t LuaInterface::luaGetLogsDir(lua_State* L)
  10674. {
  10675.     //getLogsDir()
  10676.     lua_pushstring(L, getFilePath(FILE_TYPE_LOG, "").c_str());
  10677.     return 1;
  10678. }
  10679.  
  10680. int32_t LuaInterface::luaGetConfigFile(lua_State* L)
  10681. {
  10682.     //getConfigFile()
  10683.     lua_pushstring(L, g_config.getString(ConfigManager::CONFIG_FILE).c_str());
  10684.     return 1;
  10685. }
  10686.  
  10687. int32_t LuaInterface::luaDoGuildAddEnemy(lua_State* L)
  10688. {
  10689.     //doGuildAddEnemy(guild, enemy, war, type)
  10690.     War_t war;
  10691.     war.type = (WarType_t)popNumber(L);
  10692.     war.war = popNumber(L);
  10693.  
  10694.     uint32_t enemy = popNumber(L), guild = popNumber(L), count = 0;
  10695.     for(AutoList<Player>::iterator it = Player::autoList.begin(); it != Player::autoList.end(); ++it)
  10696.     {
  10697.         if(it->second->isRemoved() || it->second->getGuildId() != guild)
  10698.             continue;
  10699.  
  10700.         ++count;
  10701.         it->second->addEnemy(enemy, war);
  10702.         g_game.updateCreatureEmblem(it->second);
  10703.     }
  10704.  
  10705.     lua_pushnumber(L, count);
  10706.     return 1;
  10707. }
  10708.  
  10709. int32_t LuaInterface::luaDoGuildRemoveEnemy(lua_State* L)
  10710. {
  10711.     //doGuildRemoveEnemy(guild, enemy)
  10712.     uint32_t enemy = popNumber(L), guild = popNumber(L), count = 0;
  10713.     for(AutoList<Player>::iterator it = Player::autoList.begin(); it != Player::autoList.end(); ++it)
  10714.     {
  10715.         if(it->second->isRemoved() || it->second->getGuildId() != guild)
  10716.             continue;
  10717.  
  10718.         ++count;
  10719.         it->second->removeEnemy(enemy);
  10720.         g_game.updateCreatureEmblem(it->second);
  10721.     }
  10722.  
  10723.     lua_pushnumber(L, count);
  10724.     return 1;
  10725. }
  10726.  
  10727. int32_t LuaInterface::luaGetConfigValue(lua_State* L)
  10728. {
  10729.     //getConfigValue(key)
  10730.     g_config.getValue(popString(L), L);
  10731.     return 1;
  10732. }
  10733.  
  10734. int32_t LuaInterface::luaGetModList(lua_State* L)
  10735. {
  10736.     //getModList()
  10737.     ModMap::iterator it = ScriptManager::getInstance()->getFirstMod();
  10738.     lua_newtable(L);
  10739.     for(uint32_t i = 1; it != ScriptManager::getInstance()->getLastMod(); ++it, ++i)
  10740.     {
  10741.         createTable(L, i);
  10742.         setField(L, "name", it->first);
  10743.         setField(L, "description", it->second.description);
  10744.         setField(L, "file", it->second.file);
  10745.  
  10746.         setField(L, "version", it->second.version);
  10747.         setField(L, "author", it->second.author);
  10748.         setField(L, "contact", it->second.contact);
  10749.  
  10750.         setFieldBool(L, "enabled", it->second.enabled);
  10751.         pushTable(L);
  10752.     }
  10753.  
  10754.     return 1;
  10755. }
  10756.  
  10757. int32_t LuaInterface::luaL_loadmodlib(lua_State* L)
  10758. {
  10759.     //loadmodlib(lib)
  10760.     std::string name = asLowerCaseString(popString(L));
  10761.     for(LibMap::iterator it = ScriptManager::getInstance()->getFirstLib();
  10762.         it != ScriptManager::getInstance()->getLastLib(); ++it)
  10763.     {
  10764.         if(asLowerCaseString(it->first) != name)
  10765.             continue;
  10766.  
  10767.         luaL_loadstring(L, it->second.second.c_str());
  10768.         lua_pushvalue(L, -1);
  10769.         break;
  10770.     }
  10771.  
  10772.     return 1;
  10773. }
  10774.  
  10775. int32_t LuaInterface::luaL_domodlib(lua_State* L)
  10776. {
  10777.     //domodlib(lib)
  10778.     std::string name = asLowerCaseString(popString(L));
  10779.     for(LibMap::iterator it = ScriptManager::getInstance()->getFirstLib();
  10780.         it != ScriptManager::getInstance()->getLastLib(); ++it)
  10781.     {
  10782.         if(asLowerCaseString(it->first) != name)
  10783.             continue;
  10784.  
  10785.         bool ret = luaL_dostring(L, it->second.second.c_str());
  10786.         if(ret)
  10787.             error(NULL, popString(L));
  10788.  
  10789.         lua_pushboolean(L, !ret);
  10790.         break;
  10791.     }
  10792.  
  10793.     return 1;
  10794. }
  10795.  
  10796. int32_t LuaInterface::luaL_dodirectory(lua_State* L)
  10797. {
  10798.     //dodirectory(dir)
  10799.     std::string dir = popString(L);
  10800.     if(!getEnv()->getInterface()->loadDirectory(dir, NULL))
  10801.     {
  10802.         errorEx("Failed to load directory " + dir + ".");
  10803.         lua_pushboolean(L, false);
  10804.     }
  10805.     else
  10806.         lua_pushboolean(L, true);
  10807.  
  10808.     return 1;
  10809. }
  10810.  
  10811. int32_t LuaInterface::luaL_errors(lua_State* L)
  10812. {
  10813.     //errors(var)
  10814.     lua_pushboolean(L, getEnv()->getInterface()->m_errors);
  10815.     getEnv()->getInterface()->m_errors = popNumber(L);
  10816.     return 1;
  10817. }
  10818.  
  10819. #define EXPOSE_LOG(Name, Stream)\
  10820.     int32_t LuaInterface::luaStd##Name(lua_State* L)\
  10821.     {\
  10822.         StringVec data;\
  10823.         for(int32_t i = 0, params = lua_gettop(L); i < params; ++i)\
  10824.             data.push_back(popString(L));\
  10825. \
  10826.         for(StringVec::reverse_iterator it = data.rbegin(); it != data.rend(); ++it)\
  10827.             Stream << (*it) << std::endl;\
  10828. \
  10829.         lua_pushnumber(L, data.size());\
  10830.         return 1;\
  10831.     }
  10832.  
  10833. EXPOSE_LOG(Cout, std::cout)
  10834. EXPOSE_LOG(Clog, std::clog)
  10835. EXPOSE_LOG(Cerr, std::cerr)
  10836.  
  10837. #undef EXPOSE_LOG
  10838.  
  10839. int32_t LuaInterface::luaStdMD5(lua_State* L)
  10840. {
  10841.     //std.md5(string[, upperCase = false])
  10842.     bool upperCase = false;
  10843.     if(lua_gettop(L) > 1)
  10844.         upperCase = popNumber(L);
  10845.  
  10846.     lua_pushstring(L, transformToMD5(popString(L), upperCase).c_str());
  10847.     return 1;
  10848. }
  10849.  
  10850. int32_t LuaInterface::luaStdSHA1(lua_State* L)
  10851. {
  10852.     //std.sha1(string[, upperCase = false])
  10853.     bool upperCase = false;
  10854.     if(lua_gettop(L) > 1)
  10855.         upperCase = popNumber(L);
  10856.  
  10857.     lua_pushstring(L, transformToSHA1(popString(L), upperCase).c_str());
  10858.     return 1;
  10859. }
  10860.  
  10861. int32_t LuaInterface::luaStdSHA256(lua_State* L)
  10862. {
  10863.     //std.sha256(string[, upperCase = false])
  10864.     bool upperCase = false;
  10865.     if(lua_gettop(L) > 1)
  10866.         upperCase = popNumber(L);
  10867.  
  10868.     lua_pushstring(L, transformToSHA256(popString(L), upperCase).c_str());
  10869.     return 1;
  10870. }
  10871.  
  10872. int32_t LuaInterface::luaStdSHA512(lua_State* L)
  10873. {
  10874.     //std.sha512(string[, upperCase = false])
  10875.     bool upperCase = false;
  10876.     if(lua_gettop(L) > 1)
  10877.         upperCase = popNumber(L);
  10878.  
  10879.     lua_pushstring(L, transformToSHA512(popString(L), upperCase).c_str());
  10880.     return 1;
  10881. }
  10882.  
  10883. int32_t LuaInterface::luaSystemTime(lua_State* L)
  10884. {
  10885.     //os.mtime()
  10886.     lua_pushnumber(L, OTSYS_TIME());
  10887.     return 1;
  10888. }
  10889.  
  10890. int32_t LuaInterface::luaDatabaseExecute(lua_State* L)
  10891. {
  10892.     //db.query(query)
  10893.     DBQuery query; //lock mutex
  10894.     lua_pushboolean(L, Database::getInstance()->query(popString(L)));
  10895.     return 1;
  10896. }
  10897.  
  10898. int32_t LuaInterface::luaDatabaseStoreQuery(lua_State* L)
  10899. {
  10900.     //db.storeQuery(query)
  10901.     ScriptEnviroment* env = getEnv();
  10902.  
  10903.     DBQuery query; //lock mutex
  10904.     if(DBResult* res = Database::getInstance()->storeQuery(popString(L)))
  10905.         lua_pushnumber(L, env->addResult(res));
  10906.     else
  10907.         lua_pushboolean(L, false);
  10908.  
  10909.     return 1;
  10910. }
  10911.  
  10912. int32_t LuaInterface::luaDatabaseEscapeString(lua_State* L)
  10913. {
  10914.     //db.escapeString(str)
  10915.     DBQuery query; //lock mutex
  10916.     lua_pushstring(L, Database::getInstance()->escapeString(popString(L)).c_str());
  10917.     return 1;
  10918. }
  10919.  
  10920. int32_t LuaInterface::luaDatabaseEscapeBlob(lua_State* L)
  10921. {
  10922.     //db.escapeBlob(s, length)
  10923.     uint32_t length = popNumber(L);
  10924.     DBQuery query; //lock mutex
  10925.  
  10926.     lua_pushstring(L, Database::getInstance()->escapeBlob(popString(L).c_str(), length).c_str());
  10927.     return 1;
  10928. }
  10929.  
  10930. int32_t LuaInterface::luaDatabaseLastInsertId(lua_State* L)
  10931. {
  10932.     //db.lastInsertId()
  10933.     DBQuery query; //lock mutex
  10934.     lua_pushnumber(L, Database::getInstance()->getLastInsertId());
  10935.     return 1;
  10936. }
  10937.  
  10938. int32_t LuaInterface::luaDatabaseStringComparer(lua_State* L)
  10939. {
  10940.     //db.stringComparer()
  10941.     lua_pushstring(L, Database::getInstance()->getStringComparer().c_str());
  10942.     return 1;
  10943. }
  10944.  
  10945. int32_t LuaInterface::luaDatabaseUpdateLimiter(lua_State* L)
  10946. {
  10947.     //db.updateLimiter()
  10948.     lua_pushstring(L, Database::getInstance()->getUpdateLimiter().c_str());
  10949.     return 1;
  10950. }
  10951.  
  10952. #define CHECK_RESULT()\
  10953.     if(!res)\
  10954.     {\
  10955.         lua_pushboolean(L, false);\
  10956.         return 1;\
  10957.     }
  10958.  
  10959. int32_t LuaInterface::luaResultGetDataInt(lua_State* L)
  10960. {
  10961.     //result.getDataInt(res, s)
  10962.     const std::string& s = popString(L);
  10963.     ScriptEnviroment* env = getEnv();
  10964.  
  10965.     DBResult* res = env->getResultByID(popNumber(L));
  10966.     CHECK_RESULT()
  10967.  
  10968.     lua_pushnumber(L, res->getDataInt(s));
  10969.     return 1;
  10970. }
  10971.  
  10972. int32_t LuaInterface::luaResultGetDataLong(lua_State* L)
  10973. {
  10974.     //result.getDataLong(res, s)
  10975.     const std::string& s = popString(L);
  10976.     ScriptEnviroment* env = getEnv();
  10977.  
  10978.     DBResult* res = env->getResultByID(popNumber(L));
  10979.     CHECK_RESULT()
  10980.  
  10981.     lua_pushnumber(L, res->getDataLong(s));
  10982.     return 1;
  10983. }
  10984.  
  10985. int32_t LuaInterface::luaResultGetDataString(lua_State* L)
  10986. {
  10987.     //result.getDataString(res, s)
  10988.     const std::string& s = popString(L);
  10989.     ScriptEnviroment* env = getEnv();
  10990.  
  10991.     DBResult* res = env->getResultByID(popNumber(L));
  10992.     CHECK_RESULT()
  10993.  
  10994.     lua_pushstring(L, res->getDataString(s).c_str());
  10995.     return 1;
  10996. }
  10997.  
  10998. int32_t LuaInterface::luaResultGetDataStream(lua_State* L)
  10999. {
  11000.     //result.getDataStream(res, s)
  11001.     const std::string s = popString(L);
  11002.     ScriptEnviroment* env = getEnv();
  11003.  
  11004.     DBResult* res = env->getResultByID(popNumber(L));
  11005.     CHECK_RESULT()
  11006.  
  11007.     uint64_t length = 0;
  11008.     lua_pushstring(L, res->getDataStream(s, length));
  11009.  
  11010.     lua_pushnumber(L, length);
  11011.     return 2;
  11012. }
  11013.  
  11014. int32_t LuaInterface::luaResultNext(lua_State* L)
  11015. {
  11016.     //result.next(res)
  11017.     ScriptEnviroment* env = getEnv();
  11018.  
  11019.     DBResult* res = env->getResultByID(popNumber(L));
  11020.     CHECK_RESULT()
  11021.  
  11022.     lua_pushboolean(L, res->next());
  11023.     return 1;
  11024. }
  11025.  
  11026. int32_t LuaInterface::luaResultFree(lua_State* L)
  11027. {
  11028.     //result.free(res)
  11029.     uint32_t rid = popNumber(L);
  11030.     ScriptEnviroment* env = getEnv();
  11031.  
  11032.     DBResult* res = env->getResultByID(rid);
  11033.     CHECK_RESULT()
  11034.  
  11035.     lua_pushboolean(L, env->removeResult(rid));
  11036.     return 1;
  11037. }
  11038.  
  11039. #undef CHECK_RESULT
  11040.  
  11041. int32_t LuaInterface::luaBitNot(lua_State* L)
  11042. {
  11043.     int32_t number = (int32_t)popNumber(L);
  11044.     lua_pushnumber(L, ~number);
  11045.     return 1;
  11046. }
  11047.  
  11048. int32_t LuaInterface::luaBitUNot(lua_State* L)
  11049. {
  11050.     uint32_t number = (uint32_t)popNumber(L);
  11051.     lua_pushnumber(L, ~number);
  11052.     return 1;
  11053. }
  11054.  
  11055. #define MULTI_OPERATOR(type, name, op)\
  11056.     int32_t LuaInterface::luaBit##name(lua_State* L)\
  11057.     {\
  11058.         int32_t params = lua_gettop(L);\
  11059.         type value = (type)popNumber(L);\
  11060.         for(int32_t i = 2; i <= params; ++i)\
  11061.             value op popNumber(L);\
  11062. \
  11063.         lua_pushnumber(L, value);\
  11064.         return 1;\
  11065.     }
  11066.  
  11067. MULTI_OPERATOR(int32_t, And, &=)
  11068. MULTI_OPERATOR(int32_t, Or, |=)
  11069. MULTI_OPERATOR(int32_t, Xor, ^=)
  11070. MULTI_OPERATOR(uint32_t, UAnd, &=)
  11071. MULTI_OPERATOR(uint32_t, UOr, |=)
  11072. MULTI_OPERATOR(uint32_t, UXor, ^=)
  11073.  
  11074. #undef MULTI_OPERATOR
  11075.  
  11076. #define SHIFT_OPERATOR(type, name, op)\
  11077.     int32_t LuaInterface::luaBit##name(lua_State* L)\
  11078.     {\
  11079.         type v2 = (type)popNumber(L), v1 = (type)popNumber(L);\
  11080.         lua_pushnumber(L, (v1 op v2));\
  11081.         return 1;\
  11082.     }
  11083.  
  11084. SHIFT_OPERATOR(int32_t, LeftShift, <<)
  11085. SHIFT_OPERATOR(int32_t, RightShift, >>)
  11086. SHIFT_OPERATOR(uint32_t, ULeftShift, <<)
  11087. SHIFT_OPERATOR(uint32_t, URightShift, >>)
  11088.  
  11089. #undef SHIFT_OPERATOR
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement