Module.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. ** Command & Conquer Generals(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. struct FieldParse;
  46. // TYPES //////////////////////////////////////////////////////////////////////////////////////////
  47. //-------------------------------------------------------------------------------------------------
  48. //-------------------------------------------------------------------------------------------------
  49. enum ModuleType
  50. {
  51. MODULETYPE_BEHAVIOR = 0,
  52. //
  53. // drawable module types - you should *NOT* remove drawable module types, we write
  54. // modules into save game files in buckets of module types ... if you do remove one
  55. // here you will have to update the xfer code for a drawable.
  56. //
  57. // ALSO note that new drawable module types should go at the end of the existing drawable modules
  58. //
  59. MODULETYPE_DRAW = 1,
  60. MODULETYPE_CLIENT_UPDATE = 2,
  61. // put new drawable module types here
  62. NUM_MODULE_TYPES, // keep this last!
  63. FIRST_DRAWABLE_MODULE_TYPE = MODULETYPE_DRAW,
  64. LAST_DRAWABLE_MODULE_TYPE = MODULETYPE_CLIENT_UPDATE,
  65. NUM_DRAWABLE_MODULE_TYPES = (LAST_DRAWABLE_MODULE_TYPE - FIRST_DRAWABLE_MODULE_TYPE + 1)
  66. };
  67. //-------------------------------------------------------------------------------------------------
  68. //-------------------------------------------------------------------------------------------------
  69. enum ModuleInterfaceType
  70. {
  71. MODULEINTERFACE_UPDATE = 0x00000001,
  72. MODULEINTERFACE_DIE = 0x00000002,
  73. MODULEINTERFACE_DAMAGE = 0x00000004,
  74. MODULEINTERFACE_CREATE = 0x00000008,
  75. MODULEINTERFACE_COLLIDE = 0x00000010,
  76. MODULEINTERFACE_BODY = 0x00000020,
  77. MODULEINTERFACE_CONTAIN = 0x00000040,
  78. MODULEINTERFACE_UPGRADE = 0x00000080,
  79. MODULEINTERFACE_SPECIAL_POWER = 0x00000100,
  80. MODULEINTERFACE_DESTROY = 0x00000200,
  81. MODULEINTERFACE_DRAW = 0x00000400,
  82. MODULEINTERFACE_CLIENT_UPDATE = 0x00000800
  83. };
  84. //-------------------------------------------------------------------------------------------------
  85. /** Base class for data-read-from-INI for modules. */
  86. //-------------------------------------------------------------------------------------------------
  87. /// @todo srj -- make ModuleData be MemoryPool based
  88. class ModuleData : public Snapshot
  89. {
  90. public:
  91. ModuleData() { }
  92. virtual ~ModuleData() { }
  93. void setModuleTagNameKey( NameKeyType key ) { m_moduleTagNameKey = key; }
  94. NameKeyType getModuleTagNameKey() const { return m_moduleTagNameKey; }
  95. virtual Bool isAiModuleData() const { return false; }
  96. // ugh, hack
  97. virtual const W3DModelDrawModuleData* getAsW3DModelDrawModuleData() const { return NULL; }
  98. virtual StaticGameLODLevel getMinimumRequiredGameLOD() const { return (StaticGameLODLevel)0;}
  99. static void buildFieldParse(MultiIniFieldParse& p)
  100. {
  101. // nothing
  102. }
  103. public:
  104. virtual void crc( Xfer *xfer ) {}
  105. virtual void xfer( Xfer *xfer ) {}
  106. virtual void loadPostProcess( void ) {}
  107. private:
  108. NameKeyType m_moduleTagNameKey; ///< module tag key, unique among all modules for an object instance
  109. };
  110. //-------------------------------------------------------------------------------------------------
  111. // This macro is to assist in the creation of new modules, contains the common
  112. // things that all module definitions must have in order to work with
  113. // the module creation factory
  114. //-------------------------------------------------------------------------------------------------
  115. #define MAKE_STANDARD_MODULE_MACRO( cls ) \
  116. public: \
  117. static Module* friend_newModuleInstance( Thing *thing, const ModuleData* moduleData ) { return newInstance( cls )( thing, moduleData ); } \
  118. virtual NameKeyType getModuleNameKey() const { static NameKeyType nk = NAMEKEY(#cls); return nk; } \
  119. protected: \
  120. virtual void crc( Xfer *xfer ); \
  121. virtual void xfer( Xfer *xfer ); \
  122. virtual void loadPostProcess( void );
  123. // ------------------------------------------------------------------------------------------------
  124. // For the creation of abstract module classes
  125. // ------------------------------------------------------------------------------------------------
  126. #define MAKE_STANDARD_MODULE_MACRO_ABC( cls ) \
  127. protected: \
  128. virtual void crc( Xfer *xfer ); \
  129. virtual void xfer( Xfer *xfer ); \
  130. virtual void loadPostProcess( void );
  131. //-------------------------------------------------------------------------------------------------
  132. // only use this macro for an ABC. for a real class, use MAKE_STANDARD_MODULE_MACRO_WITH_MODULE_DATA.
  133. #define MAKE_STANDARD_MODULE_DATA_MACRO_ABC( cls, clsmd ) \
  134. private: \
  135. const clsmd* get##clsmd() const { return (clsmd*)getModuleData(); } \
  136. public: \
  137. static ModuleData* friend_newModuleData(INI* ini) \
  138. { \
  139. clsmd* data = MSGNEW( "AllModuleData" ) clsmd; \
  140. if (ini) ini->initFromINIMultiProc(data, clsmd::buildFieldParse); \
  141. return data; \
  142. }
  143. //-------------------------------------------------------------------------------------------------
  144. #define MAKE_STANDARD_MODULE_MACRO_WITH_MODULE_DATA( cls, clsmd ) \
  145. MAKE_STANDARD_MODULE_MACRO(cls) \
  146. MAKE_STANDARD_MODULE_DATA_MACRO_ABC(cls, clsmd)
  147. //-------------------------------------------------------------------------------------------------
  148. /** Common interface for thing modules, we want a single common base class
  149. * for all the modules (either object or drawable) so that we can use
  150. * a single module factory to handle instancing them ... it's just
  151. * convenient this way */
  152. //-------------------------------------------------------------------------------------------------
  153. class Module : public MemoryPoolObject,
  154. public Snapshot
  155. {
  156. MEMORY_POOL_GLUE_ABC( Module ) ///< this abstract class needs memory pool hooks
  157. public:
  158. Module(const ModuleData* moduleData) : m_moduleData(moduleData) { }
  159. // virtual destructor prototype defined by MemoryPoolObject
  160. // this method should NEVER be overridden by user code, only via the MAKE_STANDARD_MODULE_xxx macros!
  161. // it should also NEVER be called directly; it's only for use by ModuleFactory!
  162. static ModuleData* friend_newModuleData(INI* ini);
  163. virtual NameKeyType getModuleNameKey() const = 0;
  164. inline NameKeyType getModuleTagNameKey() const { return getModuleData()->getModuleTagNameKey(); }
  165. /** this is called after all the Modules for a given Thing are created; it
  166. allows Modules to resolve any inter-Module dependencies.
  167. */
  168. virtual void onObjectCreated() { }
  169. /**
  170. this is called whenever a drawable is bound to the object.
  171. drawable is NOT guaranteed to be non-null.
  172. */
  173. virtual void onDrawableBoundToObject() { }
  174. /// preload any assets we might have for this time of day
  175. virtual void preloadAssets( TimeOfDay timeOfDay ) { }
  176. /** onDelete() will be called on all modules contained by an object or drawable before
  177. the actual deletion of each of those modules happens */
  178. virtual void onDelete( void ) { }
  179. protected:
  180. inline const ModuleData* getModuleData() const { return m_moduleData; }
  181. virtual void crc( Xfer *xfer );
  182. virtual void xfer( Xfer *xfer );
  183. virtual void loadPostProcess( void );
  184. private:
  185. const ModuleData* m_moduleData;
  186. }; // end Module
  187. //-------------------------------------------------------------------------------------------------
  188. //=================================================================================================
  189. // OBJECT Module interface and modules
  190. //=================================================================================================
  191. //-------------------------------------------------------------------------------------------------
  192. /** Module interface specific for Objects, this is really just to make a clear distinction
  193. * between modules intended for use in objects and modules intended for use
  194. * in drawables */
  195. //-------------------------------------------------------------------------------------------------
  196. class ObjectModule : public Module
  197. {
  198. MEMORY_POOL_GLUE_ABC( ObjectModule ) ///< this abstract class needs memory pool hooks
  199. public:
  200. ObjectModule( Thing *thing, const ModuleData* moduleData );
  201. // virtual destructor prototype defined by MemoryPoolObject
  202. virtual void onCapture( Player *oldOwner, Player *newOwner ) { }
  203. protected:
  204. inline Object *getObject() { return m_object; }
  205. inline const Object *getObject() const { return m_object; }
  206. virtual void crc( Xfer *xfer );
  207. virtual void xfer( Xfer *xfer );
  208. virtual void loadPostProcess( void );
  209. private:
  210. // it shouldn't be legal for subclasses to ever modify this, only to look at it;
  211. // so, we'll enforce this by making it private and providing a protected access method.
  212. Object *m_object; ///< the object this module is a part of
  213. };
  214. //-------------------------------------------------------------------------------------------------
  215. //-------------------------------------------------------------------------------------------------
  216. //=================================================================================================
  217. // DRAWABLE module interface and modules
  218. //=================================================================================================
  219. //-------------------------------------------------------------------------------------------------
  220. /** Module interface specific for Drawbles, this is really just to make a clear distinction
  221. * between modules intended for use in objects and modules intended for use
  222. * in drawables */
  223. //-------------------------------------------------------------------------------------------------
  224. class DrawableModule : public Module
  225. {
  226. MEMORY_POOL_GLUE_ABC( DrawableModule ) ///< this abstract class needs memory pool hooks
  227. public:
  228. DrawableModule( Thing *thing, const ModuleData* moduleData );
  229. // virtual destructor prototype defined by MemoryPoolObject
  230. protected:
  231. inline Drawable *getDrawable() { return m_drawable; }
  232. inline const Drawable *getDrawable() const { return m_drawable; }
  233. virtual void crc( Xfer *xfer );
  234. virtual void xfer( Xfer *xfer );
  235. virtual void loadPostProcess( void );
  236. private:
  237. // it shouldn't be legal for subclasses to ever modify this, only to look at it;
  238. // so, we'll enforce this by making it private and providing a protected access method.
  239. Drawable *m_drawable; ///< the drawble this module is a part of
  240. };
  241. //-------------------------------------------------------------------------------------------------
  242. //-------------------------------------------------------------------------------------------------
  243. /** VARIOUS MODULE INTERFACES */
  244. //-------------------------------------------------------------------------------------------------
  245. #endif // __MODULE_H_