Advertisement
Guest User

luascript.cpp

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