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