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