Advertisement
Guest User

Untitled

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