CLua.h 6.5 KB

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