CLua.h 6.0 KB

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