Advertisement
Guest User

Untitled

a guest
Jul 10th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.69 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////
  2. // OpenTibia - an opensource roleplaying game
  3. //////////////////////////////////////////////////////////////////////
  4. // Lua script interface
  5. //////////////////////////////////////////////////////////////////////
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU General Public License
  8. // as published by the Free Software Foundation; either version 2
  9. // of the License, or (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software Foundation,
  18. // Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19. //////////////////////////////////////////////////////////////////////
  20.  
  21.  
  22. #ifndef __OTSERV_LUASCRIPT_H__
  23. #define __OTSERV_LUASCRIPT_H__
  24.  
  25. #include <string>
  26. #include <map>
  27. #include <list>
  28.  
  29. extern "C"
  30. {
  31. #include <lua.h>
  32. #include <lauxlib.h>
  33. #include <lualib.h>
  34. }
  35.  
  36. #include "position.h"
  37. #include "definitions.h"
  38.  
  39. class Thing;
  40. class Creature;
  41. class Player;
  42. class Item;
  43. class Container;
  44. class AreaCombat;
  45. class Combat;
  46. class Condition;
  47. class Npc;
  48.  
  49. enum LUA_RET_CODE{
  50.     LUA_NO_ERROR = 0,
  51.     LUA_ERROR = -1,
  52.     LUA_TRUE = 1,
  53.     LUA_FALSE = 0,
  54.     LUA_NULL = 0,
  55. };
  56.  
  57. enum LuaVariantType_t{    
  58.     VARIANT_NONE = 0,
  59.     VARIANT_NUMBER,
  60.     VARIANT_POSITION,
  61.     VARIANT_TARGETPOSITION,
  62.     VARIANT_STRING,
  63. };
  64.  
  65. struct LuaVariant{
  66.     LuaVariant()
  67.     {
  68.         type = VARIANT_NONE;
  69.         text = "";
  70.         pos.x = 0;
  71.         pos.y = 0;
  72.         pos.z = 0;
  73.         pos.stackpos = 0;
  74.         number = 0;
  75.     }
  76.  
  77.     LuaVariantType_t type;
  78.     std::string text;
  79.     PositionEx pos;
  80.     uint32_t number;
  81. };
  82.  
  83. class LuaScriptInterface;
  84. class Game;
  85. class Npc;
  86.  
  87. class ScriptEnviroment
  88. {
  89. public:
  90.     ScriptEnviroment();
  91.     ~ScriptEnviroment();
  92.  
  93.     void resetEnv();
  94.     void resetCallback() {m_callbackId = 0;}
  95.  
  96.     void setScriptId(int32_t scriptId, LuaScriptInterface* scriptInterface)
  97.         {m_scriptId = scriptId; m_interface = scriptInterface;}
  98.     bool setCallbackId(int32_t callbackId, LuaScriptInterface* scriptInterface);
  99.     void setEventDesc(const std::string& desc) {m_eventdesc = desc;}
  100.    
  101.     std::string getEventDesc() {return m_eventdesc;}
  102.     int32_t getScriptId() {return m_scriptId;}
  103.     int32_t getCallbackId() {return m_callbackId;}
  104.     LuaScriptInterface* getScriptInterface() {return m_interface;}
  105.    
  106.     void setTimerEvent() {m_timerEvent = true;}
  107.     void resetTimerEvent() {m_timerEvent = false;}
  108.  
  109.     void getEventInfo(int32_t& scriptId, std::string& desc, LuaScriptInterface*& scriptInterface, int32_t& callbackId, bool& timerEvent);
  110.  
  111.     static void addUniqueThing(Thing* thing);
  112.     uint32_t addThing(Thing* thing);
  113.    
  114.     void addGlobalStorageValue(const uint32_t key, const int32_t value);
  115.     bool getGlobalStorageValue(const uint32_t key, int32_t& value) const;
  116.  
  117.     void setRealPos(const Position& realPos) {m_realPos = realPos;}
  118.     Position getRealPos() {return m_realPos;}
  119.  
  120.     void setNpc(Npc* npc) {m_curNpc = npc;}
  121.     Npc* getNpc() const {return m_curNpc;}
  122.  
  123.     Thing* getThingByUID(uint32_t uid);
  124.     Item* getItemByUID(uint32_t uid);
  125.     Container* getContainerByUID(uint32_t uid);
  126.     Creature* getCreatureByUID(uint32_t uid);
  127.     Player* getPlayerByUID(uint32_t uid);
  128.  
  129.     uint32_t addCombatArea(AreaCombat* area);
  130.     AreaCombat* getCombatArea(uint32_t areaId) const;
  131.  
  132.     uint32_t addCombatObject(Combat* combat);
  133.     const Combat* getCombatObject(uint32_t combatId) const;
  134.     Combat* getCombatObject(uint32_t combatId);
  135.  
  136.     uint32_t addConditionObject(Condition* condition);
  137.     const Condition* getConditionObject(uint32_t conditionId) const;
  138.     Condition* getConditionObject(uint32_t conditionId);
  139.  
  140. private:
  141.     typedef std::map<int32_t, Thing*> ThingMap;
  142.     typedef std::vector<const LuaVariant*> VariantVector;
  143.     typedef std::map<uint32_t, int32_t> StorageMap;
  144.     typedef std::map<uint32_t, AreaCombat*> AreaMap;
  145.     typedef std::map<uint32_t, Combat*> CombatMap;
  146.     typedef std::map<uint32_t, Condition*> ConditionMap;
  147.  
  148.     //script file id
  149.     int32_t m_scriptId;
  150.     int32_t m_callbackId;
  151.     bool m_timerEvent;
  152.     LuaScriptInterface* m_interface;
  153.     //script event desc
  154.     std::string m_eventdesc;
  155.  
  156.     static StorageMap m_globalStorageMap;
  157.     //unique id map
  158.     static ThingMap m_globalMap;
  159.    
  160.     Position m_realPos;
  161.    
  162.     //item/creature map
  163.     int32_t m_lastUID;
  164.     ThingMap m_localMap;
  165.    
  166.     //area map
  167.     uint32_t m_lastAreaId;
  168.     static AreaMap m_areaMap;
  169.  
  170.     //combat map
  171.     uint32_t m_lastCombatId;
  172.     static CombatMap m_combatMap;
  173.  
  174.     //condition map
  175.     uint32_t m_lastConditionId;
  176.     static ConditionMap m_conditionMap;
  177.  
  178.     //for npc scripts
  179.     Npc* m_curNpc;
  180. };
  181.  
  182. class Position;
  183.  
  184. enum PlayerInfo_t{
  185.     PlayerInfoFood,
  186.     PlayerInfoAccess,
  187.     PlayerInfoLevel,
  188.     PlayerInfoMagLevel,
  189.     PlayerInfoMana,
  190.     PlayerInfoHealth,
  191.     PlayerInfoName,
  192.     PlayerInfoPosition,
  193.     PlayerInfoVocation,
  194.     PlayerInfoMasterPos,
  195.     PlayerInfoSoul,
  196.     PlayerInfoFreeCap,
  197.     PlayerInfoGuildId,
  198.     PlayerInfoGuildName,
  199.     PlayerInfoGuildRank,
  200.     PlayerInfoGuildNick,
  201.     PlayerInfoSex,
  202.     PlayerInfoLookDirection,
  203.     PlayerInfoTown,
  204.     PlayerInfoGUID
  205. };
  206.  
  207. #define reportErrorFunc(a)  reportError(__FUNCTION__, a)
  208.  
  209. enum ErrorCode_t{
  210.     LUA_ERROR_PLAYER_NOT_FOUND,
  211.     LUA_ERROR_CREATURE_NOT_FOUND,
  212.     LUA_ERROR_ITEM_NOT_FOUND,
  213.     LUA_ERROR_THING_NOT_FOUND,
  214.     LUA_ERROR_TILE_NOT_FOUND,
  215.     LUA_ERROR_HOUSE_NOT_FOUND,
  216.     LUA_ERROR_COMBAT_NOT_FOUND,
  217.     LUA_ERROR_CONDITION_NOT_FOUND,
  218.     LUA_ERROR_AREA_NOT_FOUND,
  219.     LUA_ERROR_CONTAINER_NOT_FOUND,
  220.     LUA_ERROR_VARIANT_NOT_FOUND,
  221.     LUA_ERROR_VARIANT_UNKNOWN
  222. };
  223.  
  224.  
  225. class LuaScriptInterface
  226. {
  227. public:
  228.     LuaScriptInterface(std::string interfaceName);
  229.     virtual ~LuaScriptInterface();
  230.  
  231.     virtual bool initState();
  232.     bool reInitState();
  233.  
  234.     int32_t loadFile(const std::string& file, Npc* npc = NULL);
  235.     const std::string& getFileById(int32_t scriptId);
  236.  
  237.     int32_t getEvent(const std::string& eventName);
  238.  
  239.     static ScriptEnviroment* getScriptEnv(){
  240.         assert(m_scriptEnvIndex >= 0 && m_scriptEnvIndex < 16);
  241.         return &m_scriptEnv[m_scriptEnvIndex];
  242.     }
  243.    
  244.     static bool reserveScriptEnv(){
  245.         ++m_scriptEnvIndex;
  246.         if(m_scriptEnvIndex < 15){
  247.             return true;
  248.         }
  249.         else{
  250.             --m_scriptEnvIndex;
  251.             return false;
  252.         }
  253.     }
  254.    
  255.     static void releaseScriptEnv(){
  256.         if(m_scriptEnvIndex >= 0){
  257.             m_scriptEnv[m_scriptEnvIndex].resetEnv();
  258.             --m_scriptEnvIndex;
  259.         }
  260.     }
  261.  
  262.     static void reportError(const char* function, const std::string& error_desc);
  263.  
  264.     std::string getInterfaceName() {return m_interfaceName;}
  265.     const std::string& getLastLuaError() const {return m_lastLuaError;}
  266.     void dumpLuaStack();
  267.  
  268.     lua_State* getLuaState() {return m_luaState;}
  269.  
  270.     bool pushFunction(int32_t functionId);
  271.  
  272.     int32_t callFunction(uint32_t nParams);
  273.  
  274.     //push/pop common structures
  275.     static void pushThing(lua_State *L, Thing* thing, uint32_t thingid);
  276.     static void pushVariant(lua_State *L, const LuaVariant& var);
  277.     static void pushPosition(lua_State *L, const PositionEx& position);
  278.     static void pushPosition(lua_State *L, const Position& position, uint32_t stackpos);
  279.        
  280.     static LuaVariant popVariant(lua_State *L);
  281.     static void popPosition(lua_State *L, PositionEx& position);
  282.     static void popPosition(lua_State *L, Position& position, uint32_t& stackpos);
  283.     static uint32_t popNumber(lua_State *L);
  284.     static double popFloatNumber(lua_State *L);
  285.     static const char* popString(lua_State *L);
  286.  
  287.     static int32_t getField(lua_State *L, const char *key);
  288.     static void setField(lua_State *L, const char* index, uint32_t val);
  289.     static void setField(lua_State *L, const char* index, const std::string& val);
  290.  
  291. protected:
  292.     virtual bool closeState();
  293.  
  294.     virtual void registerFunctions();
  295.  
  296.     static std::string getErrorDesc(ErrorCode_t code);
  297.     static bool getArea(lua_State *L, std::list<uint32_t>& list, uint32_t& rows);
  298.  
  299.     //lua functions
  300.     static int luaDoRemoveItem(lua_State *L);
  301.     static int luaDoFeedPlayer(lua_State *L);
  302.     static int luaDoSendCancel(lua_State *L);
  303.     static int luaDoSendDefaultCancel(lua_State *L);
  304.     static int luaDoTeleportThing(lua_State *L);
  305.     static int luaDoTransformItem(lua_State *L);
  306.     static int luaDoSendMagicEffect(lua_State *L);
  307.     static int luaDoChangeTypeItem(lua_State *L);
  308.     static int luaDoSendAnimatedText(lua_State *L);
  309.     static int luaDoShowTextWindow(lua_State *L);
  310.     static int luaDoShowTextDialog(lua_State *L);
  311.     static int luaDoDecayItem(lua_State *L);
  312.     static int luaDoCreateItem(lua_State *L);
  313.     static int luaDoSummonCreature(lua_State *L);
  314.     static int luaDoMoveCreature(lua_State *L);
  315.  
  316.     static int luaDoPlayerSay(lua_State *L);
  317.     static int luaDoPlayerAddSkillTry(lua_State *L);
  318.     static int luaDoPlayerAddHealth(lua_State *L);
  319.     static int luaDoPlayerAddMana(lua_State *L);
  320.     static int luaDoPlayerSoul(lua_State *L);
  321.     static int luaDoPlayerAddItem(lua_State *L);
  322.     static int luaDoPlayerSendTextMessage(lua_State *L);
  323.     static int luaDoPlayerRemoveMoney(lua_State *L);
  324.     static int luaDoPlayerSetMasterPos(lua_State *L);
  325.     static int luaDoPlayerSetTown(lua_State *L);
  326.     static int luaDoPlayerSetVocation(lua_State *L);
  327.     static int luaDoPlayerRemoveItem(lua_State *L);
  328.     static int luaDoPlayerAddSoul(lua_State *L);
  329.     static int luaDoPlayerAddExp(lua_State *L);
  330.     //static int luaDoPlayerSetGuildId(lua_State *L);
  331.     static int luaDoPlayerSetGuildRank(lua_State *L);
  332.     static int luaDoPlayerSetGuildNick(lua_State *L);
  333.     static int luaDoSetCreatureLight(lua_State *L);
  334.  
  335.     //get item info
  336.     static int luaGetItemRWInfo(lua_State *L);
  337.     static int luaGetThingfromPos(lua_State *L);
  338.     static int luaGetThing(lua_State *L);
  339.     static int luaGetThingPos(lua_State *L);
  340.     //set item
  341.     static int luaDoSetItemActionId(lua_State *L);
  342.     static int luaDoSetItemText(lua_State *L);
  343.     static int luaDoSetItemSpecialDescription(lua_State *L);
  344.  
  345.     //get tile info
  346.     static int luaGetTilePzInfo(lua_State *L);
  347.     static int luaGetTileHouseInfo(lua_State *L);
  348.     //houses
  349.     static int luaGetHouseOwner(lua_State *L);
  350.     static int luaGetHouseName(lua_State *L);
  351.     static int luaGetHouseEntry(lua_State *L);
  352.     static int luaGetHouseRent(lua_State *L);
  353.     static int luaGetHouseTown(lua_State *L);
  354.     static int luaGetHouseAccessList(lua_State *L);
  355.     static int luaGetHouseByPlayerGUID(lua_State *L);
  356.     static int luaSetHouseOwner(lua_State *L);
  357.     static int luaSetHouseAccessList(lua_State *L);
  358.  
  359.     //get player info functions
  360.     static int luaGetPlayerFood(lua_State *L);
  361.     static int luaGetPlayerAccess(lua_State *L);
  362.     static int luaGetPlayerLevel(lua_State *L);
  363.     static int luaGetPlayerMagLevel(lua_State *L);
  364.     static int luaGetPlayerMana(lua_State *L);
  365.     static int luaGetPlayerHealth(lua_State *L);
  366.     static int luaGetPlayerName(lua_State *L);
  367.     static int luaGetPlayerPosition(lua_State *L);
  368.     static int luaGetPlayerSkill(lua_State *L);
  369.     static int luaGetPlayerVocation(lua_State *L);
  370.     static int luaGetPlayerMasterPos(lua_State *L);
  371.     static int luaGetPlayerTown(lua_State *L);
  372.     static int luaGetPlayerItemCount(lua_State *L);
  373.     static int luaGetPlayerSoul(lua_State *L);
  374.     static int luaGetPlayerFreeCap(lua_State *L);
  375.     static int luaGetPlayerLight(lua_State *L);
  376.     static int luaGetPlayerSlotItem(lua_State *L);
  377.     static int luaGetPlayerDepotItems(lua_State *L);
  378.     static int luaGetPlayerSex(lua_State *L);
  379.     static int luaGetPlayerLookDir(lua_State *L);
  380.  
  381.     static int luaGetPlayerStorageValue(lua_State *L);
  382.     static int luaSetPlayerStorageValue(lua_State *L);
  383.    
  384.     static int luaGetGlobalStorageValue(lua_State *L);
  385.     static int luaSetGlobalStorageValue(lua_State *L);
  386.    
  387.     static int luaDoPlayerAddOutfit(lua_State *L);
  388.     static int luaDoPlayerRemOutfit(lua_State *L);
  389.  
  390.     static int luaGetWorldType(lua_State *L);
  391.     static int luaGetWorldTime(lua_State *L);
  392.     static int luaGetWorldLight(lua_State *L);
  393.     static int luaGetWorldCreatures(lua_State *L);
  394.     static int luaGetWorldUpTime(lua_State *L);
  395.  
  396.     //type validation
  397.     static int luaIsPlayer(lua_State *L);
  398.     static int luaIsCreature(lua_State *L);
  399.     static int luaIsContainer(lua_State *L);
  400.     static int luaIsMoveable(lua_State *L);
  401.    
  402.     static int luaGetPlayerByName(lua_State *L);
  403.     static int luaRegisterCreature(lua_State *L);
  404.    
  405.     //container
  406.     static int luaGetContainerSize(lua_State *L);
  407.     static int luaGetContainerCap(lua_State *L);
  408.     static int luaGetContainerItem(lua_State *L);
  409.     static int luaDoAddContainerItem(lua_State *L);
  410.  
  411.     //
  412.     static int luaCreateCombatObject(lua_State *L);
  413.     static int luaCreateCombatArea(lua_State *L);
  414.     static int luaSetCombatArea(lua_State *L);
  415.     static int luaSetCombatCondition(lua_State *L);
  416.     static int luaSetCombatParam(lua_State *L);
  417.     static int luaCreateConditionObject(lua_State *L);
  418.     static int luaSetConditionParam(lua_State *L);
  419.     static int luaAddDamageCondition(lua_State *L);
  420.     static int luaAddOutfitCondition(lua_State *L);
  421.  
  422.     static int luaSetCombatCallBack(lua_State *L);
  423.     static int luaSetCombatFormula(lua_State *L);
  424.     static int luaSetConditionFormula(lua_State *L);
  425.     static int luaDoCombat(lua_State *L);
  426.  
  427.     static int luaDoAreaCombatHealth(lua_State *L);
  428.     static int luaDoTargetCombatHealth(lua_State *L);
  429.  
  430.     //
  431.     static int luaDoAreaCombatMana(lua_State *L);
  432.     static int luaDoTargetCombatMana(lua_State *L);
  433.  
  434.     static int luaDoAreaCombatCondition(lua_State *L);
  435.     static int luaDoTargetCombatCondition(lua_State *L);
  436.  
  437.     static int luaDoAreaCombatDispel(lua_State *L);
  438.     static int luaDoTargetCombatDispel(lua_State *L);
  439.  
  440.     static int luaDoChallengeCreature(lua_State *L);
  441.     static int luaDoConvinceCreature(lua_State *L);
  442.  
  443.     static int luaNumberToVariant(lua_State *L);
  444.     static int luaStringToVariant(lua_State *L);
  445.     static int luaPositionToVariant(lua_State *L);
  446.     static int luaTargetPositionToVariant(lua_State *L);
  447.  
  448.     static int luaVariantToNumber(lua_State *L);
  449.     static int luaVariantToString(lua_State *L);
  450.     static int luaVariantToPosition(lua_State *L);
  451.  
  452.     static int luaDoChangeSpeed(lua_State *L);
  453.  
  454.     static int luaSetCreatureOutfit(lua_State *L);
  455.     static int luaGetCreatureOutfit(lua_State *L);
  456.     static int luaSetMonsterOutfit(lua_State *L);
  457.     static int luaSetItemOutfit(lua_State *L);
  458.     static int luaGetCreaturePosition(lua_State *L);
  459.     static int luaGetCreatureName(lua_State *L);
  460.  
  461.     static int luaIsItemStackable(lua_State *L);
  462.     static int luaIsItemRune(lua_State *L);
  463.     static int luaIsItemDoor(lua_State *L);
  464.     static int luaIsItemContainer(lua_State *L);
  465.     static int luaIsItemFluidContainer(lua_State *L);
  466.     static int luaGetItemName(lua_State *L);
  467.  
  468.     static int getCreatureTarget(lua_State *L);
  469.  
  470.     #ifdef __XID_CVS_MODS__
  471.     static int luaGetPlayerSkull(lua_State *L);
  472.     static int luaGetPlayerConditionTicks(lua_State *L);
  473.     #endif
  474.  
  475.     #ifdef __XID_SEPERATE_ADDONS__
  476.     static int luaDoPlayerAddAddon(lua_State* L);
  477.     static int luaGetPlayerOutfitAddon(lua_State* L);
  478.     #endif
  479.  
  480.     #ifdef __XID_BUY_SELL__
  481.     static int luaGetItemStackable(lua_State *L);
  482.     #endif
  483.  
  484.     #ifdef __XID_PREMIUM_SYSTEM__
  485.     static int luaIsPremium(lua_State *L);
  486.     static int luaBuyPremium(lua_State *L);
  487.     #endif
  488.  
  489.     #ifdef __YUR_GUILD_SYSTEM__
  490.     static int luaFoundNewGuild(lua_State* L);
  491.     static int luaGetPlayerGuildStatus(lua_State* L);
  492.     static int luaSetPlayerGuildStatus(lua_State* L);
  493.     static int luaGetPlayerGuildName(lua_State* L);
  494.     static int luaSetPlayerGuild(lua_State* L);
  495.     static int luaClearPlayerGuild(lua_State* L);
  496.     static int luaSetPlayerGuildNick(lua_State* L);
  497.     #endif
  498.  
  499.     #ifdef __XID_LEARN_SPELLS__
  500.     static int luaDoPlayerLearnSpell(lua_State* L);
  501.     #endif
  502.  
  503.     #ifdef __XID_BLESS_SYSTEM__
  504.     static int luaDoPlayerAddBlessing(lua_State* L);
  505.     static int luaGetPlayerBlessing(lua_State* L);
  506.     #endif
  507.  
  508.     static int luaDebugPrint(lua_State *L);
  509.     static int luaIsInArray(lua_State *L);
  510.     static int luaAddEvent(lua_State *L);
  511.     static int luaStopEvent(lua_State *L);
  512.  
  513.     static int luaGetDataDirectory(lua_State *L);
  514.     //
  515.  
  516.     static int internalGetPlayerInfo(lua_State *L, PlayerInfo_t info);
  517.  
  518.     lua_State* m_luaState;
  519.     std::string m_lastLuaError;
  520. private:
  521.  
  522.     static ScriptEnviroment m_scriptEnv[16];
  523.     static int32_t m_scriptEnvIndex;
  524.  
  525.     int32_t m_runningEventId;
  526.     std::string m_loadingFile;
  527.  
  528.     //script file cache
  529.     typedef std::map<int32_t , std::string> ScriptsCache;
  530.     ScriptsCache m_cacheFiles;
  531.  
  532.     //events information
  533.     struct LuaTimerEventDesc{
  534.         int32_t scriptId;
  535.         int function;
  536.         int parameter;
  537.     };
  538.     uint32_t m_lastEventTimerId;
  539.     typedef std::map<uint32_t , LuaTimerEventDesc > LuaTimerEvents;
  540.     LuaTimerEvents m_timerEvents;
  541.    
  542.     void executeTimerEvent(uint32_t eventIndex);
  543.  
  544.     std::string m_interfaceName;
  545. };
  546.  
  547. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement