Advertisement
Guest User

Untitled

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