CLua.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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){return "";};
  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. };
  88. class CVisitableOPH : public CCPPObjectScript //once per hero
  89. {
  90. public:
  91. CVisitableOPH(CScriptCallback * CB):CCPPObjectScript(CB){};
  92. std::map<int, int> typeOfTree; //0 - level for free; 1 - 2000 gold; 2 - 10 gems
  93. std::map<int,std::set<int> > visitors;
  94. void onNAHeroVisit(int objid, int heroID, bool alreadyVisited);
  95. void newObject(int objid);
  96. void onHeroVisit(int objid, int heroID);
  97. void treeSelected(int objid, int heroID, int resType, int resVal, int expVal, ui32 result);
  98. std::vector<int> yourObjects(); //returns IDs of objects which are handled by script
  99. };
  100. class CVisitableOPW : public CCPPObjectScript //once per week
  101. {
  102. public:
  103. CVisitableOPW(CScriptCallback * CB):CCPPObjectScript(CB){};
  104. std::map<int,bool> visited;
  105. void onNAHeroVisit(int objid, int heroID, bool alreadyVisited);
  106. void newObject(int objid);
  107. void onHeroVisit(int objid, int heroID);
  108. std::vector<int> yourObjects(); //returns IDs of objects which are handled by script
  109. void newTurn ();
  110. };
  111. class CMines : public CCPPObjectScript //flaggable, and giving resource at each day
  112. {
  113. public:
  114. CMines(CScriptCallback * CB):CCPPObjectScript(CB){};
  115. std::vector<int> ourObjs;
  116. void newObject(int objid);
  117. void onHeroVisit(int objid, int heroID);
  118. std::vector<int> yourObjects(); //returns IDs of objects which are handled by script
  119. void newTurn ();
  120. };
  121. class CPickable : public CCPPObjectScript //pickable - resources, artifacts, etc
  122. {
  123. public:
  124. CPickable(CScriptCallback * CB):CCPPObjectScript(CB){};
  125. void chosen(ui32 which, int heroid, int val); //val - value of treasure in gold
  126. void newObject(int objid);
  127. void onHeroVisit(int objid, int heroID);
  128. std::vector<int> yourObjects(); //returns IDs of objects which are handled by script
  129. };
  130. class CTownScript : public CCPPObjectScript //pickable - resources, artifacts, etc
  131. {
  132. public:
  133. CTownScript(CScriptCallback * CB):CCPPObjectScript(CB){};
  134. void onHeroVisit(int objid, int heroID);
  135. void onHeroLeave(int objid, int heroID);
  136. void newObject(int objid);
  137. std::vector<int> yourObjects(); //returns IDs of objects which are handled by script
  138. };
  139. class CHeroScript : public CCPPObjectScript
  140. {
  141. public:
  142. CHeroScript(CScriptCallback * CB):CCPPObjectScript(CB){};
  143. void newObject(int objid);
  144. void onHeroVisit(int objid, int heroID);
  145. std::vector<int> yourObjects(); //returns IDs of objects which are handled by script
  146. };
  147. class CMonsterS : public CCPPObjectScript
  148. {
  149. public:
  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. void endBattleWith(const CGObjectInstance *monster, BattleResult *result);
  155. };
  156. class CCreatureGen : public CCPPObjectScript
  157. {
  158. public:
  159. std::map<int, int> amount; //amount of creatures in each dwelling
  160. CCreatureGen(CScriptCallback * CB):CCPPObjectScript(CB){};
  161. void newObject(int objid);
  162. void onHeroVisit(int objid, int heroID);
  163. std::vector<int> yourObjects(); //returns IDs of objects which are handled by script
  164. };
  165. class CTeleports : public CCPPObjectScript
  166. {
  167. public:
  168. std::map<int,std::map<int, std::vector<int> > > objs; //map[ID][subID] => vector of ids
  169. void newObject(int objid);
  170. void onHeroVisit(int objid, int heroID);
  171. std::vector<int> yourObjects(); //returns IDs of objects which are handled by script
  172. CTeleports(CScriptCallback * CB):CCPPObjectScript(CB){};
  173. };