Module.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: Module.h /////////////////////////////////////////////////////////////////////////////////
  24. // Author: Colin Day, September 2001
  25. // Desc: Object and drawable modules and actions. These are simply just class
  26. // instances that we can assign to objects, drawables, and things to contain
  27. // data and code for specific events, or just to hold data
  28. ///////////////////////////////////////////////////////////////////////////////////////////////////
  29. #pragma once
  30. #ifndef __MODULE_H_
  31. #define __MODULE_H_
  32. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  33. #include "Common/INI.h"
  34. #include "Common/GameMemory.h"
  35. #include "Common/NameKeyGenerator.h"
  36. #include "Common/Snapshot.h"
  37. // FORWARD REFERENCES /////////////////////////////////////////////////////////////////////////////
  38. enum TimeOfDay;
  39. enum StaticGameLODLevel;
  40. class Drawable;
  41. class Object;
  42. class Player;
  43. class Thing;
  44. class W3DModelDrawModuleData; // ugh, hack (srj)
  45. class W3DTreeDrawModuleData; // ugh, hack (srj)
  46. struct FieldParse;
  47. // TYPES //////////////////////////////////////////////////////////////////////////////////////////
  48. //-------------------------------------------------------------------------------------------------
  49. //-------------------------------------------------------------------------------------------------
  50. enum ModuleType
  51. {
  52. MODULETYPE_BEHAVIOR = 0,
  53. //
  54. // drawable module types - you should *NOT* remove drawable module types, we write
  55. // modules into save game files in buckets of module types ... if you do remove one
  56. // here you will have to update the xfer code for a drawable.
  57. //
  58. // ALSO note that new drawable module types should go at the end of the existing drawable modules
  59. //
  60. MODULETYPE_DRAW = 1,
  61. MODULETYPE_CLIENT_UPDATE = 2,
  62. // put new drawable module types here
  63. NUM_MODULE_TYPES, // keep this last!
  64. FIRST_DRAWABLE_MODULE_TYPE = MODULETYPE_DRAW,
  65. LAST_DRAWABLE_MODULE_TYPE = MODULETYPE_CLIENT_UPDATE,
  66. NUM_DRAWABLE_MODULE_TYPES = (LAST_DRAWABLE_MODULE_TYPE - FIRST_DRAWABLE_MODULE_TYPE + 1)
  67. };
  68. //-------------------------------------------------------------------------------------------------
  69. //-------------------------------------------------------------------------------------------------
  70. enum ModuleInterfaceType
  71. {
  72. MODULEINTERFACE_UPDATE = 0x00000001,
  73. MODULEINTERFACE_DIE = 0x00000002,
  74. MODULEINTERFACE_DAMAGE = 0x00000004,
  75. MODULEINTERFACE_CREATE = 0x00000008,
  76. MODULEINTERFACE_COLLIDE = 0x00000010,
  77. MODULEINTERFACE_BODY = 0x00000020,
  78. MODULEINTERFACE_CONTAIN = 0x00000040,
  79. MODULEINTERFACE_UPGRADE = 0x00000080,
  80. MODULEINTERFACE_SPECIAL_POWER = 0x00000100,
  81. MODULEINTERFACE_DESTROY = 0x00000200,
  82. MODULEINTERFACE_DRAW = 0x00000400,
  83. MODULEINTERFACE_CLIENT_UPDATE = 0x00000800
  84. };
  85. //-------------------------------------------------------------------------------------------------
  86. /** Base class for data-read-from-INI for modules. */
  87. //-------------------------------------------------------------------------------------------------
  88. /// @todo srj -- make ModuleData be MemoryPool based
  89. class ModuleData : public Snapshot
  90. {
  91. public:
  92. ModuleData() { }
  93. virtual ~ModuleData() { }
  94. void setModuleTagNameKey( NameKeyType key ) { m_moduleTagNameKey = key; }
  95. NameKeyType getModuleTagNameKey() const { return m_moduleTagNameKey; }
  96. virtual Bool isAiModuleData() const { return false; }
  97. // ugh, hack
  98. virtual const W3DModelDrawModuleData* getAsW3DModelDrawModuleData() const { return NULL; }
  99. // ugh, hack
  100. virtual const W3DTreeDrawModuleData* getAsW3DTreeDrawModuleData() const { return NULL; }
  101. virtual StaticGameLODLevel getMinimumRequiredGameLOD() const { return (StaticGameLODLevel)0;}
  102. static void buildFieldParse(MultiIniFieldParse& p)
  103. {
  104. // nothing
  105. }
  106. public:
  107. virtual void crc( Xfer *xfer ) {}
  108. virtual void xfer( Xfer *xfer ) {}
  109. virtual void loadPostProcess( void ) {}
  110. private:
  111. NameKeyType m_moduleTagNameKey; ///< module tag key, unique among all modules for an object instance
  112. };
  113. //-------------------------------------------------------------------------------------------------
  114. // This macro is to assist in the creation of new modules, contains the common
  115. // things that all module definitions must have in order to work with
  116. // the module creation factory
  117. //-------------------------------------------------------------------------------------------------
  118. #define MAKE_STANDARD_MODULE_MACRO( cls ) \
  119. public: \
  120. static Module* friend_newModuleInstance( Thing *thing, const ModuleData* moduleData ) { return newInstance( cls )( thing, moduleData ); } \
  121. virtual NameKeyType getModuleNameKey() const { static NameKeyType nk = NAMEKEY(#cls); return nk; } \
  122. protected: \
  123. virtual void crc( Xfer *xfer ); \
  124. virtual void xfer( Xfer *xfer ); \
  125. virtual void loadPostProcess( void );
  126. // ------------------------------------------------------------------------------------------------
  127. // For the creation of abstract module classes
  128. // ------------------------------------------------------------------------------------------------
  129. #define MAKE_STANDARD_MODULE_MACRO_ABC( cls ) \
  130. protected: \
  131. virtual void crc( Xfer *xfer ); \
  132. virtual void xfer( Xfer *xfer ); \
  133. virtual void loadPostProcess( void );
  134. //-------------------------------------------------------------------------------------------------
  135. // only use this macro for an ABC. for a real class, use MAKE_STANDARD_MODULE_MACRO_WITH_MODULE_DATA.
  136. #define MAKE_STANDARD_MODULE_DATA_MACRO_ABC( cls, clsmd ) \
  137. private: \
  138. const clsmd* get##clsmd() const { return (clsmd*)getModuleData(); } \
  139. public: \
  140. static ModuleData* friend_newModuleData(INI* ini) \
  141. { \
  142. clsmd* data = MSGNEW( "AllModuleData" ) clsmd; \
  143. if (ini) ini->initFromINIMultiProc(data, clsmd::buildFieldParse); \
  144. return data; \
  145. }
  146. //-------------------------------------------------------------------------------------------------
  147. #define MAKE_STANDARD_MODULE_MACRO_WITH_MODULE_DATA( cls, clsmd ) \
  148. MAKE_STANDARD_MODULE_MACRO(cls) \
  149. MAKE_STANDARD_MODULE_DATA_MACRO_ABC(cls, clsmd)
  150. //-------------------------------------------------------------------------------------------------
  151. /** Common interface for thing modules, we want a single common base class
  152. * for all the modules (either object or drawable) so that we can use
  153. * a single module factory to handle instancing them ... it's just
  154. * convenient this way */
  155. //-------------------------------------------------------------------------------------------------
  156. class Module : public MemoryPoolObject,
  157. public Snapshot
  158. {
  159. MEMORY_POOL_GLUE_ABC( Module ) ///< this abstract class needs memory pool hooks
  160. public:
  161. Module(const ModuleData* moduleData) : m_moduleData(moduleData) { }
  162. // virtual destructor prototype defined by MemoryPoolObject
  163. // this method should NEVER be overridden by user code, only via the MAKE_STANDARD_MODULE_xxx macros!
  164. // it should also NEVER be called directly; it's only for use by ModuleFactory!
  165. static ModuleData* friend_newModuleData(INI* ini);
  166. virtual NameKeyType getModuleNameKey() const = 0;
  167. inline NameKeyType getModuleTagNameKey() const { return getModuleData()->getModuleTagNameKey(); }
  168. /** this is called after all the Modules for a given Thing are created; it
  169. allows Modules to resolve any inter-Module dependencies.
  170. */
  171. virtual void onObjectCreated() { }
  172. /**
  173. this is called whenever a drawable is bound to the object.
  174. drawable is NOT guaranteed to be non-null.
  175. */
  176. virtual void onDrawableBoundToObject() { }
  177. /// preload any assets we might have for this time of day
  178. virtual void preloadAssets( TimeOfDay timeOfDay ) { }
  179. /** onDelete() will be called on all modules contained by an object or drawable before
  180. the actual deletion of each of those modules happens */
  181. virtual void onDelete( void ) { }
  182. protected:
  183. inline const ModuleData* getModuleData() const { return m_moduleData; }
  184. virtual void crc( Xfer *xfer );
  185. virtual void xfer( Xfer *xfer );
  186. virtual void loadPostProcess( void );
  187. private:
  188. const ModuleData* m_moduleData;
  189. }; // end Module
  190. //-------------------------------------------------------------------------------------------------
  191. //=================================================================================================
  192. // OBJECT Module interface and modules
  193. //=================================================================================================
  194. //-------------------------------------------------------------------------------------------------
  195. /** Module interface specific for Objects, this is really just to make a clear distinction
  196. * between modules intended for use in objects and modules intended for use
  197. * in drawables */
  198. //-------------------------------------------------------------------------------------------------
  199. class ObjectModule : public Module
  200. {
  201. MEMORY_POOL_GLUE_ABC( ObjectModule ) ///< this abstract class needs memory pool hooks
  202. public:
  203. ObjectModule( Thing *thing, const ModuleData* moduleData );
  204. // virtual destructor prototype defined by MemoryPoolObject
  205. virtual void onCapture( Player *oldOwner, Player *newOwner ) { }
  206. virtual void onDisabledEdge( Bool nowDisabled ) { }
  207. protected:
  208. inline Object *getObject() { return m_object; }
  209. inline const Object *getObject() const { return m_object; }
  210. virtual void crc( Xfer *xfer );
  211. virtual void xfer( Xfer *xfer );
  212. virtual void loadPostProcess( void );
  213. private:
  214. // it shouldn't be legal for subclasses to ever modify this, only to look at it;
  215. // so, we'll enforce this by making it private and providing a protected access method.
  216. Object *m_object; ///< the object this module is a part of
  217. };
  218. //-------------------------------------------------------------------------------------------------
  219. //-------------------------------------------------------------------------------------------------
  220. //=================================================================================================
  221. // DRAWABLE module interface and modules
  222. //=================================================================================================
  223. //-------------------------------------------------------------------------------------------------
  224. /** Module interface specific for Drawbles, this is really just to make a clear distinction
  225. * between modules intended for use in objects and modules intended for use
  226. * in drawables */
  227. //-------------------------------------------------------------------------------------------------
  228. class DrawableModule : public Module
  229. {
  230. MEMORY_POOL_GLUE_ABC( DrawableModule ) ///< this abstract class needs memory pool hooks
  231. public:
  232. DrawableModule( Thing *thing, const ModuleData* moduleData );
  233. // virtual destructor prototype defined by MemoryPoolObject
  234. protected:
  235. inline Drawable *getDrawable() { return m_drawable; }
  236. inline const Drawable *getDrawable() const { return m_drawable; }
  237. virtual void crc( Xfer *xfer );
  238. virtual void xfer( Xfer *xfer );
  239. virtual void loadPostProcess( void );
  240. private:
  241. // it shouldn't be legal for subclasses to ever modify this, only to look at it;
  242. // so, we'll enforce this by making it private and providing a protected access method.
  243. Drawable *m_drawable; ///< the drawble this module is a part of
  244. };
  245. //-------------------------------------------------------------------------------------------------
  246. //-------------------------------------------------------------------------------------------------
  247. /** VARIOUS MODULE INTERFACES */
  248. //-------------------------------------------------------------------------------------------------
  249. #endif // __MODULE_H_