CLua.h 5.2 KB

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