Advertisement
holmbergfan

Untitled

Nov 28th, 2022
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.14 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////
  2. // OpenTibia - an opensource roleplaying game
  3. //////////////////////////////////////////////////////////////////////
  4. //
  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. #ifndef __OTSERV_NPC_H__
  22. #define __OTSERV_NPC_H__
  23.  
  24. #include "creature.h"
  25. #include "luascript.h"
  26. #include "templates.h"
  27.  
  28. //////////////////////////////////////////////////////////////////////
  29. // Defines an NPC...
  30. // Defines an NPC...
  31. class Npc;
  32. class Player;
  33. class NpcResponse;
  34. struct NpcState;
  35.  
  36. typedef std::list<Npc*> NpcList;
  37. class Npcs{
  38. public:
  39.     Npcs() {};
  40.     ~Npcs() {};
  41.     void reload();
  42. };
  43.  
  44. class NpcScriptInterface : public LuaScriptInterface
  45. {
  46. public:
  47.     NpcScriptInterface();
  48.     virtual ~NpcScriptInterface();
  49.  
  50.     bool loadNpcLib(std::string file);
  51.  
  52.     static void pushState(lua_State *L, NpcState* state);
  53.     static void popState(lua_State *L, NpcState* &state);
  54.  
  55. protected:
  56.  
  57.     virtual void registerFunctions();
  58.  
  59.     static int luaActionSay(lua_State *L);
  60.     static int luaActionMove(lua_State *L);
  61.     static int luaActionMoveTo(lua_State *L);
  62.     static int luaActionTurn(lua_State* L);
  63.     static int luaActionFollow(lua_State* L);
  64.     static int luaCreatureGetName(lua_State *L);
  65.     static int luaCreatureGetName2(lua_State *L);
  66.     static int luaCreatureGetPos(lua_State *L);
  67.     static int luaSelfGetPos(lua_State *L);
  68.     static int luagetDistanceTo(lua_State *L);
  69.     static int luaSetNpcFocus(lua_State *L);
  70.     static int luaGetNpcCid(lua_State *L);
  71.     static int luaGetNpcPos(lua_State *L);
  72.     static int luaGetNpcState(lua_State *L);
  73.     static int luaSetNpcState(lua_State *L);
  74.     static int luaGetNpcName(lua_State *L);
  75.     static int luaGetNpcParameter(lua_State *L);
  76.    
  77. private:
  78.     virtual bool initState();
  79.     virtual bool closeState();
  80.  
  81.     bool m_libLoaded;
  82. };
  83.  
  84. class NpcEventsHandler
  85. {
  86. public:
  87.     NpcEventsHandler(Npc* npc);
  88.     virtual ~NpcEventsHandler();
  89.  
  90.     virtual void onCreatureAppear(const Creature* creature){};
  91.     virtual void onCreatureDisappear(const Creature* creature){};
  92.     virtual void onCreatureMove(const Creature* creature, const Position& oldPos, const Position& newPos){};
  93.     virtual void onCreatureSay(const Creature* creature, SpeakClasses, const std::string& text){};
  94.     virtual void onThink(){};
  95.  
  96.     bool isLoaded();
  97.  
  98. protected:
  99.     Npc* m_npc;
  100.     bool m_loaded;
  101. };
  102.  
  103. class NpcScript : public NpcEventsHandler
  104. {
  105. public:
  106.     NpcScript(std::string file, Npc* npc);
  107.     virtual ~NpcScript();
  108.  
  109.     virtual void onCreatureAppear(const Creature* creature);
  110.     virtual void onCreatureDisappear(const Creature* creature);
  111.     virtual void onCreatureMove(const Creature* creature, const Position& oldPos, const Position& newPos);
  112.     virtual void onCreatureSay(const Creature* creature, SpeakClasses, const std::string& text);
  113.     virtual void onThink();
  114.  
  115. private:
  116.     NpcScriptInterface* m_scriptInterface;
  117.  
  118.     int32_t m_onCreatureAppear;
  119.     int32_t m_onCreatureDisappear;
  120.     int32_t m_onCreatureMove;
  121.     int32_t m_onCreatureSay;
  122. //  int32_t m_onPlayerCloseChannel;
  123. //  int32_t m_onPlayerEndTrade;
  124.     int32_t m_onThink;
  125. };
  126.  
  127. enum RespondParam_t{
  128.     RESPOND_DEFAULT = 0,
  129.     RESPOND_MALE = 1,
  130.     RESPOND_FEMALE = 2,
  131.     RESPOND_PZBLOCK = 4,
  132.     RESPOND_LOWMONEY = 8,
  133.     RESPOND_NOAMOUNT = 16,
  134.     RESPOND_LOWAMOUNT = 32,
  135.     RESPOND_PREMIUM = 64,
  136.     RESPOND_DRUID = 128,
  137.     RESPOND_KNIGHT = 256,
  138.     RESPOND_PALADIN = 512,
  139.     RESPOND_SORCERER = 1024,
  140.     RESPOND_LOWLEVEL = 2048
  141. };
  142.  
  143. enum ResponseType_t{
  144.     RESPONSE_DEFAULT,
  145.     RESPONSE_SCRIPT,
  146. };
  147.  
  148. enum InteractType_t{
  149.     INTERACT_TEXT,
  150.     INTERACT_EVENT
  151. };
  152.  
  153. enum ReponseActionParam_t{
  154.     ACTION_NONE,
  155.     ACTION_SETTOPIC,
  156.     ACTION_SETLEVEL,
  157.     ACTION_SETPRICE,
  158.     ACTION_SETBUYPRICE,
  159.     ACTION_SETSELLPRICE,
  160.     ACTION_TAKEMONEY,
  161.     ACTION_GIVEMONEY,
  162.     ACTION_SELLITEM,
  163.     ACTION_BUYITEM,
  164.     ACTION_GIVEITEM,
  165.     ACTION_TAKEITEM,
  166.     ACTION_SETAMOUNT,
  167.     ACTION_SETITEM,
  168.     ACTION_SETSUBTYPE,
  169.     ACTION_SETEFFECT,
  170.     ACTION_SETSPELL,
  171.     ACTION_SETLISTNAME,
  172.     ACTION_SETLISTPNAME,
  173.     ACTION_TEACHSPELL,
  174.     ACTION_SETSTORAGE,
  175.     ACTION_SETTELEPORT,
  176.     ACTION_SCRIPT,
  177.     ACTION_SCRIPTPARAM,
  178.     ACTION_ADDQUEUE,
  179.     ACTION_SETIDLE
  180. };
  181.  
  182. enum StorageComparision_t{
  183.     STORAGE_LESS,
  184.     STORAGE_LESSOREQUAL,
  185.     STORAGE_EQUAL,
  186.     STORAGE_GREATEROREQUAL,
  187.     STORAGE_GREATER
  188. };
  189.  
  190. enum NpcEvent_t{
  191.     EVENT_NONE,
  192.     EVENT_BUSY,
  193.     EVENT_THINK,
  194.     EVENT_IDLE,
  195.     EVENT_PLAYER_ENTER,
  196.     EVENT_PLAYER_MOVE,
  197.     EVENT_PLAYER_LEAVE
  198.  
  199.     /*
  200.     EVENT_CREATURE_ENTER,
  201.     EVENT_CREATURE_MOVE,
  202.     EVENT_CREATURE_LEAVE,
  203.     */
  204. };
  205.  
  206. struct ResponseAction{
  207. public:
  208.     ResponseAction()
  209.     {
  210.         actionType = ACTION_NONE;
  211.         key = 0;
  212.         intValue = 0;
  213.         strValue = "";
  214.         pos.x = 0;
  215.         pos.y = 0;
  216.         pos.z = 0;
  217.     }
  218.  
  219.     ReponseActionParam_t actionType;
  220.     int32_t key;
  221.     int32_t intValue;
  222.     std::string strValue;
  223.     Position pos;
  224. };
  225.  
  226. struct ScriptVars{
  227.     ScriptVars()
  228.     {
  229.         n1 = -1;
  230.         n2 = -1;
  231.         n3 = -1;
  232.         b1 = false;
  233.         b2 = false;
  234.         b3 = false;
  235.         s1 = "";
  236.         s2 = "";
  237.         s3 = "";
  238.     }
  239.  
  240.     int32_t n1;
  241.     int32_t n2;
  242.     int32_t n3;
  243.     bool b1;
  244.     bool b2;
  245.     bool b3;
  246.     std::string s1;
  247.     std::string s2;
  248.     std::string s3;
  249. };
  250.  
  251. struct ListItem{
  252.     ListItem()
  253.     {
  254.         itemId = 0;
  255.         subType = 1;
  256.         sellPrice = 0;
  257.         buyPrice = 0;
  258.         keywords = "";
  259.         name = "";
  260.         pluralName = "";
  261.     }
  262.  
  263.     int32_t sellPrice;
  264.     int32_t buyPrice;
  265.     int32_t itemId;
  266.     int32_t subType;
  267.     std::string keywords;
  268.     std::string name;
  269.     std::string pluralName;
  270. };
  271.  
  272.  
  273. typedef std::list<ResponseAction> ActionList;
  274. typedef std::map<std::string, int32_t> ResponseScriptMap;
  275. typedef std::list<NpcResponse*> ResponseList;
  276.  
  277. class NpcResponse
  278. {
  279. public:
  280.     struct ResponseProperties{
  281.         ResponseProperties()
  282.         {
  283.             topic = -1;
  284.             amount = -1;
  285.             focusStatus = -1;
  286.             output = "";
  287.             interactType = INTERACT_TEXT;
  288.             responseType = RESPONSE_DEFAULT;
  289.             params = 0;
  290.             storageId = -1;
  291.             storageValue = -1;
  292.             storageComp = STORAGE_EQUAL;
  293.             knowSpell = "";
  294.         }
  295.  
  296.         int32_t topic;
  297.         int32_t amount;
  298.         int32_t focusStatus;
  299.         std::list<std::string> inputList;
  300.         std::string output;
  301.         InteractType_t interactType;
  302.         ResponseType_t responseType;
  303.         uint32_t params;
  304.         int32_t storageId;
  305.         int32_t storageValue;
  306.         StorageComparision_t storageComp;
  307.         std::string knowSpell;
  308.         ActionList actionList;
  309.         std::list<ListItem> itemList;
  310.     };
  311.  
  312.     NpcResponse(const ResponseProperties& _prop,
  313.         ResponseList _subResponseList,
  314.         ScriptVars _scriptVars)
  315.     {
  316.         prop = _prop;
  317.         subResponseList = _subResponseList;
  318.         scriptVars = _scriptVars;
  319.     };
  320.  
  321.     NpcResponse(NpcResponse& rhs)
  322.     {
  323.         prop = rhs.prop;
  324.         scriptVars = rhs.scriptVars;
  325.         for(ResponseList::iterator it = rhs.subResponseList.begin(); it != rhs.subResponseList.end(); ++it){
  326.             NpcResponse* response = new NpcResponse(*(*it));
  327.             subResponseList.push_back(response);
  328.         }
  329.     }
  330.  
  331.     ~NpcResponse()
  332.     {
  333.         for(ResponseList::iterator it = subResponseList.begin(); it != subResponseList.end(); ++it){
  334.             delete *it;
  335.         }
  336.  
  337.         subResponseList.clear();
  338.     }
  339.  
  340.     uint32_t getParams() const {return prop.params;}
  341.     std::string getInputText() const {return (prop.inputList.empty() ? "" : *prop.inputList.begin());}
  342.     int32_t getTopic() const {return prop.topic;}
  343.     int32_t getFocusState() const {return prop.focusStatus;}
  344.     int32_t getStorageId() const {return prop.storageId;}
  345.     int32_t getStorageValue() const {return prop.storageValue;}
  346.     ResponseType_t getResponseType() const {return prop.responseType;}
  347.     InteractType_t getInteractType() const {return prop.interactType;}
  348.     StorageComparision_t getStorageComp() const {return prop.storageComp;}
  349.     const std::string& getKnowSpell() const {return prop.knowSpell;}
  350.     const std::string& getText() const {return prop.output;}
  351.     int32_t getAmount() const {return prop.amount;}
  352.     void setAmount(int32_t _amount) { prop.amount = _amount;}
  353.  
  354.     std::string formatResponseString(Creature* creature) const;
  355.     void addAction(ResponseAction action) {prop.actionList.push_back(action);}
  356.     const std::list<std::string>& getInputList() const { return prop.inputList;}
  357.  
  358.     void setResponseList(ResponseList _list) { subResponseList.insert(subResponseList.end(),_list.begin(),_list.end());}
  359.     const ResponseList& getResponseList() const { return subResponseList;}
  360.  
  361.     ActionList::const_iterator getFirstAction() const {return prop.actionList.begin();}
  362.     ActionList::const_iterator getEndAction() const {return prop.actionList.end();}
  363.  
  364.     ResponseProperties prop;
  365.     ResponseList subResponseList;
  366.     ScriptVars scriptVars;
  367. };
  368.  
  369. struct NpcState{
  370.     int32_t topic;
  371.     bool isIdle;
  372.     bool isQueued;
  373.     int32_t price;
  374.     int32_t sellPrice;
  375.     int32_t buyPrice;
  376.     int32_t amount;
  377.     int32_t itemId;
  378.     int32_t subType;
  379.     std::string spellName;
  380.     std::string listName;
  381.     std::string listPluralName;
  382.     int32_t level;
  383.     uint64_t prevInteraction;
  384.     std::string respondToText;
  385.     uint32_t respondToCreature;
  386.     std::string prevRespondToText;
  387.     const NpcResponse* lastResponse;
  388.  
  389.     //script variables
  390.     ScriptVars scriptVars;
  391.  
  392.     //Do not forget to update pushState/popState if you add more variables
  393. };
  394.  
  395. class Npc : public Creature
  396. {
  397. public:
  398. #ifdef __ENABLE_SERVER_DIAGNOSTIC__
  399.     static uint32_t npcCount;
  400. #endif
  401.  
  402.     virtual ~Npc();
  403.  
  404.     virtual Npc* getNpc() {return this;};
  405.     virtual const Npc* getNpc() const {return this;};
  406.  
  407.     virtual bool isPushable() const { return false;};
  408.  
  409.     virtual uint32_t idRange(){ return 0x80000000;}
  410.     static AutoList<Npc> listNpc;
  411.     void removeList() {listNpc.removeList(getID());}
  412.     void addList() {listNpc.addList(this);}
  413.  
  414.     static Npc* createNpc(const std::string& name);
  415.  
  416.     virtual bool canSee(const Position& pos) const;
  417.  
  418.     bool load();
  419.     void reload();
  420.  
  421.     virtual const std::string& getName() const {return name;};
  422.     virtual const std::string& getNameDescription() const {return name;};
  423.  
  424.     void doSay(std::string msg);
  425.     void doMove(Direction dir);
  426.     void doTurn(Direction dir);
  427.     void doMoveTo(Position pos);
  428.     bool isLoaded(){return loaded;}
  429.  
  430.     void setCreatureFocus(Creature* creature);
  431.  
  432.     NpcScriptInterface* getScriptInterface();
  433.  
  434. protected:
  435.     Npc(const std::string& _name);
  436.  
  437.     virtual void onAddTileItem(const Tile* tile, const Position& pos, const Item* item);
  438.     virtual void onUpdateTileItem(const Tile* tile, const Position& pos, uint32_t stackpos,
  439.         const Item* oldItem, const ItemType& oldType, const Item* newItem, const ItemType& newType);
  440.     virtual void onRemoveTileItem(const Tile* tile, const Position& pos, uint32_t stackpos,
  441.         const ItemType& iType, const Item* item);
  442.     virtual void onUpdateTile(const Tile* tile, const Position& pos);
  443.  
  444.     virtual void onCreatureAppear(const Creature* creature, bool isLogin);
  445.     virtual void onCreatureDisappear(const Creature* creature, uint32_t stackpos, bool isLogout);
  446.     virtual void onCreatureMove(const Creature* creature, const Tile* newTile, const Position& newPos,
  447.         const Tile* oldTile, const Position& oldPos, uint32_t oldStackPos, bool teleport);
  448.  
  449.     virtual void onCreatureTurn(const Creature* creature, uint32_t stackpos);
  450.     virtual void onCreatureSay(const Creature* creature, SpeakClasses type, const std::string& text);
  451.     virtual void onCreatureChangeOutfit(const Creature* creature, const Outfit_t& outfit);
  452.     virtual void onThink(uint32_t interval);
  453.     virtual std::string getDescription(int32_t lookDistance) const;
  454.  
  455.     bool isImmune(CombatType_t type) const {return true;}
  456.     bool isImmune(ConditionType_t type) const {return true;}
  457.     virtual bool isAttackable() const { return attackable; }
  458.     virtual bool getNextStep(Direction& dir);
  459.  
  460.     bool canWalkTo(const Position& fromPos, Direction dir);
  461.     bool getRandomStep(Direction& dir);
  462.  
  463.     void reset();
  464.     bool loadFromXml(const std::string& name);
  465.  
  466.     const NpcResponse* getResponse(const ResponseList& list, const Player* player,
  467.         NpcState* npcState, const std::string& text, bool exactMatch = false);
  468.     const NpcResponse* getResponse(const Player* player, NpcState* npcState, const std::string& text);
  469.     const NpcResponse* getResponse(const Player* player, NpcEvent_t eventType);
  470.     const NpcResponse* getResponse(const Player* player, NpcState* npcState, NpcEvent_t eventType);
  471.     std::string getEventResponseName(NpcEvent_t eventType);
  472.  
  473.     uint32_t getMatchCount(NpcResponse* response, std::vector<std::string> wordList,
  474.         bool exactMatch, int32_t& matchAllCount, int32_t& totalKeywordCount);
  475.  
  476.     void executeResponse(Player* player, NpcState* npcState, const NpcResponse* response);
  477.  
  478.     std::string formatResponse(Creature* creature, const NpcState* npcState, const NpcResponse* response) const;
  479.  
  480.     void onPlayerEnter(Player* player, NpcState* state);
  481.     void onPlayerLeave(Player* player, NpcState* state);
  482.  
  483.     typedef std::map<std::string, std::string> ParametersMap;
  484.     ParametersMap m_parameters;
  485.  
  486.     uint32_t loadParams(xmlNodePtr node);
  487.     ResponseList loadInteraction(xmlNodePtr node);
  488.  
  489.     NpcState* getState(const Player* player, bool makeNew = true);
  490.  
  491.     std::string name;
  492.     std::string m_datadir;
  493.     std::string m_scriptdir;
  494.     std::string m_filename;
  495.     uint32_t walkTicks;
  496.     bool floorChange;
  497.     bool attackable;
  498.     bool isIdle;
  499.     bool hasBusyReply;
  500.     bool hasScriptedFocus;
  501.     int32_t talkRadius;
  502.     int32_t idleTime;
  503.     int32_t idleInterval;
  504.     bool defaultPublic;
  505.     int32_t focusCreature;
  506.  
  507.     typedef std::map<std::string, std::list<ListItem> > ItemListMap;
  508.     ItemListMap itemListMap;
  509.  
  510.     ResponseScriptMap responseScriptMap;
  511.     ResponseList responseList;
  512.  
  513.     typedef std::list<NpcState*> StateList;
  514.     StateList stateList;
  515.     NpcEventsHandler* m_npcEventHandler;
  516.  
  517.     typedef std::list<uint32_t> QueueList;
  518.     QueueList queueList;
  519.     bool loaded;
  520.  
  521.     static NpcScriptInterface* m_scriptInterface;
  522.  
  523.     friend class Npcs;
  524.     friend class NpcScriptInterface;
  525. };
  526.  
  527. #endif
  528.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement