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