Advertisement
Guest User

cpp

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