CLua.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #pragma once
  2. #include "global.h"
  3. #include "client/FunctionList.h"
  4. #ifndef _MSC_VER
  5. extern "C" {
  6. #endif
  7. //#include "lstate.h"
  8. #ifndef _MSC_VER
  9. }
  10. #endif
  11. #include <set>
  12. #include <map>
  13. class CLua;
  14. struct SDL_Surface;
  15. class CGObjectInstance;
  16. class CGameInfo;
  17. class CGHeroInstance;
  18. class CScriptCallback;
  19. class SComponent;
  20. class CSelectableComponent;
  21. class CGameState;
  22. struct Mapa;
  23. struct lua_State;
  24. enum ESLan{UNDEF=-1,CPP,ERM,LUA};
  25. class CObjectScript
  26. {
  27. public:
  28. int owner, language;
  29. std::string filename;
  30. int getOwner(){return owner;} //255 - neutral / 254 - not flaggable
  31. CObjectScript();
  32. virtual ~CObjectScript();
  33. //functions to be called in script
  34. //virtual void init(){};
  35. virtual void newObject(int objid){};
  36. virtual void onHeroVisit(int objid, int heroID){};
  37. virtual void onHeroLeave(int objid, int heroID){};
  38. virtual std::string hoverText(int objid){return "";};
  39. virtual void newTurn (){};
  40. //TODO: implement functions below:
  41. virtual void equipArtefact(int HID, int AID, int slot, bool putOn){}; //putOn==0 means that artifact is taken off
  42. virtual void battleStart(int phase){}; //phase==0 - very start, before initialization of battle; phase==1 - just before battle starts
  43. virtual void battleNewTurn (int turn){}; //turn==-1 is for tactic stage
  44. //virtual void battleAction (int type,int destination, int stack, int owner, int){};
  45. //virtual void mouseClick (down,left,screen?, pos??){};
  46. virtual void heroLevelUp (int HID){}; //add possibility of changing available sec. skills
  47. };
  48. class CScript
  49. {
  50. public:
  51. CScript();
  52. virtual ~CScript();
  53. };
  54. class CLua :public CScript
  55. {
  56. protected:
  57. lua_State * is; /// tez niebezpieczne!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! (ale chwilowo okielznane)
  58. bool opened;
  59. public:
  60. CLua(std::string initpath);
  61. void open(std::string initpath);
  62. void registerCLuaCallback();
  63. CLua();
  64. virtual ~CLua();
  65. void findF(std::string fname);
  66. void findF2(std::string fname);
  67. void findFS(std::string fname);
  68. friend class CGameState;
  69. };
  70. class CLuaObjectScript : public CLua, public CObjectScript
  71. {
  72. public:
  73. CLuaObjectScript(std::string filename);
  74. virtual ~CLuaObjectScript();
  75. static std::string genFN(std::string base, int ID);
  76. void init();
  77. void newObject(int objid);
  78. void onHeroVisit(int objid, int heroID);
  79. };
  80. class CCPPObjectScript: public CObjectScript
  81. {
  82. public:
  83. CScriptCallback * cb;
  84. CCPPObjectScript(CScriptCallback * CB){cb=CB;};
  85. virtual std::vector<int> yourObjects()=0; //returns IDs of objects which are handled by script
  86. };
  87. class CVisitableOPH : public CCPPObjectScript //once per hero
  88. {
  89. public:
  90. CVisitableOPH(CScriptCallback * CB):CCPPObjectScript(CB){};
  91. std::map<int, int> typeOfTree; //0 - level for free; 1 - 2000 gold; 2 - 10 gems
  92. std::map<int,std::set<int> > visitors;
  93. void onNAHeroVisit(int objid, int heroID, bool alreadyVisited);
  94. void newObject(int objid);
  95. void onHeroVisit(int objid, int heroID);
  96. void treeSelected(int objid, int heroID, int resType, int resVal, int expVal, ui32 result);
  97. std::vector<int> yourObjects(); //returns IDs of objects which are handled by script
  98. };
  99. class CVisitableOPW : public CCPPObjectScript //once per week
  100. {
  101. public:
  102. CVisitableOPW(CScriptCallback * CB):CCPPObjectScript(CB){};
  103. std::map<int,bool> visited;
  104. void onNAHeroVisit(int objid, int heroID, bool alreadyVisited);
  105. void newObject(int objid);
  106. void onHeroVisit(int objid, int heroID);
  107. std::vector<int> yourObjects(); //returns IDs of objects which are handled by script
  108. void newTurn ();
  109. };
  110. class CMines : public CCPPObjectScript //flaggable, and giving resource at each day
  111. {
  112. public:
  113. CMines(CScriptCallback * CB):CCPPObjectScript(CB){};
  114. std::vector<int> ourObjs;
  115. void newObject(int objid);
  116. void onHeroVisit(int objid, int heroID);
  117. std::vector<int> yourObjects(); //returns IDs of objects which are handled by script
  118. void newTurn ();
  119. };
  120. class CPickable : public CCPPObjectScript //pickable - resources, artifacts, etc
  121. {
  122. public:
  123. CPickable(CScriptCallback * CB):CCPPObjectScript(CB){};
  124. void chosen(ui32 which, int heroid, int val); //val - value of treasure in gold
  125. void newObject(int objid);
  126. void onHeroVisit(int objid, int heroID);
  127. std::vector<int> yourObjects(); //returns IDs of objects which are handled by script
  128. };
  129. class CTownScript : public CCPPObjectScript //pickable - resources, artifacts, etc
  130. {
  131. public:
  132. CTownScript(CScriptCallback * CB):CCPPObjectScript(CB){};
  133. void onHeroVisit(int objid, int heroID);
  134. void onHeroLeave(int objid, int heroID);
  135. void newObject(int objid);
  136. std::vector<int> yourObjects(); //returns IDs of objects which are handled by script
  137. };
  138. class CHeroScript : public CCPPObjectScript
  139. {
  140. public:
  141. CHeroScript(CScriptCallback * CB):CCPPObjectScript(CB){};
  142. void newObject(int objid);
  143. void onHeroVisit(int objid, int heroID);
  144. std::vector<int> yourObjects(); //returns IDs of objects which are handled by script
  145. };
  146. class CMonsterS : public CCPPObjectScript
  147. {
  148. public:
  149. std::map<int, int> amounts; //monster id -> stack quantity
  150. CMonsterS(CScriptCallback * CB):CCPPObjectScript(CB){};
  151. void newObject(int objid);
  152. void onHeroVisit(int objid, int heroID);
  153. std::vector<int> yourObjects(); //returns IDs of objects which are handled by script
  154. };
  155. class CCreatureGen : public CCPPObjectScript
  156. {
  157. public:
  158. std::map<int, int> amount; //amount of creatures in each dwelling
  159. CCreatureGen(CScriptCallback * CB):CCPPObjectScript(CB){};
  160. void newObject(int objid);
  161. void onHeroVisit(int objid, int heroID);
  162. std::vector<int> yourObjects(); //returns IDs of objects which are handled by script
  163. };