CLua.h 5.4 KB

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