CLua.h 6.6 KB

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