WeatherMgr.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. ** Command & Conquer Renegade(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. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/Combat/WeatherMgr.h $*
  25. * *
  26. * Author:: Ian Leslie *
  27. * *
  28. * $Modtime:: 1/15/02 7:26p $*
  29. * *
  30. * $Revision:: 16 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #ifndef WEATHERMGR_H
  36. #define WEATHERMGR_H
  37. // Code controlling defines.
  38. #define WEATHER_PARTICLE_SORT 0
  39. // Includes.
  40. #include "combatchunkid.h"
  41. #include "pointgr.h"
  42. #include "mempool.h"
  43. #include "networkobject.h"
  44. #include "random.h"
  45. #include "rendobj.h"
  46. #include "saveloadsubsystem.h"
  47. #include "soundenvironment.h"
  48. #include "texture.h"
  49. #include "vector.h"
  50. #include "vector2.h"
  51. #include "vector3.h"
  52. #include "wwdebug.h"
  53. // Class declarations.
  54. class ChunkLoadClass;
  55. class ChunkSaveClass;
  56. class PhysicsSceneClass;
  57. #if WEATHER_PARTICLE_SORT
  58. class SortingIndexBufferClass;
  59. #else
  60. class DX8IndexBufferClass;
  61. #endif
  62. class AudibleSoundClass;
  63. class WindClass
  64. {
  65. public:
  66. WindClass (float heading, float speed, float variability, SoundEnvironmentClass *soundenvironment);
  67. ~WindClass();
  68. Vector2 Get_Velocity() {return (Velocity);}
  69. void Set (float heading, float speed, float variability);
  70. bool Update();
  71. protected:
  72. enum {
  73. OCTAVE_COUNT = 2
  74. };
  75. SoundEnvironmentClass *SoundEnvironment;
  76. float Heading;
  77. float Speed;
  78. float Variability;
  79. double Theta [OCTAVE_COUNT];
  80. Vector2 Velocity;
  81. AudibleSoundClass *Sound;
  82. };
  83. class WeatherSystemClass : public RenderObjClass
  84. {
  85. public:
  86. enum {
  87. GROWTH_STEP = 256
  88. };
  89. enum RenderModeEnum {
  90. RENDER_MODE_AXIS_ALIGNED, // Render particle oriented about the particle's velocity vector (suitable for rain, for example).
  91. RENDER_MODE_CAMERA_ALIGNED, // Render particle aligned with the camera direction (suitable for snow, for example).
  92. RENDER_MODE_SURFACE_ALIGNED // Render particle such that it looks attached to the surface of an object.
  93. };
  94. WeatherSystemClass (PhysicsSceneClass *scene,
  95. float emittersize,
  96. float emitterheight,
  97. float particledensity,
  98. float particlesperunitlength,
  99. float particlewidth,
  100. float particleheight,
  101. float particlespeed,
  102. const Vector2 &pageoffset,
  103. const Vector2 &pagesize,
  104. unsigned pagecount,
  105. bool staticpageexists,
  106. float minstatictime,
  107. float maxstatictime,
  108. RenderModeEnum rendermode,
  109. bool decayaftercollision,
  110. bool prime);
  111. ~WeatherSystemClass();
  112. RenderObjClass *Clone() const
  113. {
  114. WWASSERT (false);
  115. return (0);
  116. }
  117. void Set_Density (float density);
  118. void Render (RenderInfoClass &rinfo);
  119. void Get_Obj_Space_Bounding_Sphere (SphereClass &sphere) const;
  120. void Get_Obj_Space_Bounding_Box (AABoxClass &box) const;
  121. virtual bool Update (WindClass *wind, const Vector3 &cameraposition);
  122. protected:
  123. enum {
  124. VERTICES_PER_TRIANGLE = 3,
  125. MAX_IB_PARTICLE_COUNT = 2048,
  126. MAX_AGE = 1000000
  127. };
  128. struct RayStruct : public AutoPoolClass <RayStruct, GROWTH_STEP>
  129. {
  130. public:
  131. RayStruct *Next; // Next ray in list.
  132. bool Initialized; // Has the ray been defined?
  133. bool RayCast; // Does this ray need to be relocated in the emitter (and therefore needs to be raycast).
  134. Vector3 ParticleVelocity; // Velocity of all particles spawned by this ray.
  135. Vector2 StartPosition; // Start position of ray inside emitter.
  136. Vector3 EndPosition; // Point of collision of ray with environment.
  137. bool ValidSurfaceNormal; // Does the ray intersect a phyical object?
  138. Vector3 SurfaceNormal; // Normal of surface ray intersects (if any).
  139. };
  140. struct ParticleStruct : public AutoPoolClass <ParticleStruct, GROWTH_STEP>
  141. {
  142. public:
  143. ParticleStruct *Prev; // Previous particle in list.
  144. ParticleStruct *Next; // Next particle in list.
  145. float CollisionTime; // Time when particle hits something (in seconds).
  146. float LifeTime; // Lifetime of particle (in seconds).
  147. float ElapsedTime; // Time elapsed since birth of particle (in seconds).
  148. Vector3 Velocity; // Velocity of particle (in metres per second).
  149. Vector2 UnitZVelocity; // Velocity / Velocity.Z (precomputed for optimization purposes).
  150. Vector3 CollisionPosition; // Position of collision.
  151. Vector3 CurrentPosition; // Current position of particle (in world space).
  152. Vector3 SurfaceNormal; // Normal of surface particle collides with (if any).
  153. unsigned char Page; // Texture page for this particle.
  154. unsigned char RenderMode;
  155. unsigned char Pad [2]; // Pad structure to 4-byte multiple.
  156. };
  157. // Utility functions.
  158. float Spawn_Count (float time) {return (ParticleDensity * EmitterSize * EmitterSize * time);}
  159. bool Can_Spawn (const RayStruct *rayptr) {return (rayptr->EndPosition.Z < EmitterPosition.Z);}
  160. bool Spawn (RayStruct *suppliedrayptr = NULL);
  161. void Kill (ParticleStruct *particleptr);
  162. PhysicsSceneClass *Scene; // The scene that contains the weather system.
  163. float Age; // Age of this weather system (in seconds).
  164. float EmitterSize; // Size of square emitter (area of emitter = size * size).
  165. float EmitterHeight; // Height of emitter (relative to camera).
  166. Vector3 EmitterPosition; // Centroid of emitter (in world space).
  167. float ParticleDensity; // Density of particles (over area of emitter).
  168. float ParticlesPerUnitLength;
  169. float ParticleSpeed;
  170. Vector3 ParticleVelocity; // Dominant velocity vector of particles.
  171. float HalfParticleWidth;
  172. float HalfParticleHeight;
  173. RayStruct *RayHead;
  174. unsigned RayCount;
  175. RayStruct *RaySpawnPtr;
  176. RayStruct *RayUpdatePtr;
  177. float MinRayEndZ; // Current lowest Z-value of end of any ray (used to determine a bounding box around this render object).
  178. float SpawnCountFraction; // Cumulative fractional spawn counts (for improved accuracy).
  179. Vector3 SceneMin, SceneMax; // Bounding box around the scene that the particle system lives in.
  180. Vector3 ObjectMin, ObjectMax; // Bounding box around this render object (for culling purposes).
  181. ParticleStruct *ParticleHead; // Head of list of particles in system.
  182. unsigned ParticleCount; // No. of particles in system.
  183. #if WEATHER_PARTICLE_SORT
  184. SortingIndexBufferClass *IndexBuffer;
  185. #else
  186. DX8IndexBufferClass *IndexBuffer;
  187. #endif
  188. VertexMaterialClass *Material;
  189. ShaderClass Shader;
  190. TextureClass *Texture;
  191. Vector2 *TextureArray;
  192. RenderModeEnum RenderMode;
  193. bool DecayAfterCollision;
  194. unsigned PageCount;
  195. bool StaticPageExists;
  196. float MinStaticTime;
  197. float MaxStaticTime;
  198. Vector3 CameraPosition;
  199. bool CameraPositionValid;
  200. static Random2Class _RandomNumber; // Random no. generator.
  201. static unsigned _GlobalParticleCount; // Total no. of particles over all weather systems.
  202. };
  203. class RainSystemClass : public WeatherSystemClass
  204. {
  205. public:
  206. RainSystemClass (PhysicsSceneClass *scene, float particledensity, WindClass *wind, SoundEnvironmentClass *soundenvironment, bool prime);
  207. virtual ~RainSystemClass();
  208. bool Update (WindClass *wind, const Vector3 &cameraposition);
  209. protected:
  210. enum {
  211. PAGE_COUNT = 4
  212. };
  213. AudibleSoundClass *Sound; // Sound effect for rainfall.
  214. SoundEnvironmentClass *SoundEnvironment;
  215. };
  216. class SnowSystemClass : public WeatherSystemClass
  217. {
  218. public:
  219. SnowSystemClass (PhysicsSceneClass *scene, float particledensity, WindClass *wind, bool prime);
  220. bool Update (WindClass *wind, const Vector3 &cameraposition);
  221. protected:
  222. enum {
  223. PAGE_COUNT = 4
  224. };
  225. };
  226. class AshSystemClass : public WeatherSystemClass
  227. {
  228. public:
  229. AshSystemClass (PhysicsSceneClass *scene, float particledensity, WindClass *wind, bool prime);
  230. bool Update (WindClass *wind, const Vector3 &cameraposition);
  231. protected:
  232. enum {
  233. PAGE_COUNT = 4
  234. };
  235. };
  236. class WeatherParameterClass
  237. {
  238. public:
  239. void Initialize();
  240. void Set (float targetvalue, float ramptime, bool override);
  241. void Set (float overrideramptime) {OverrideDuration = overrideramptime;}
  242. float Value() {return (CurrentValue);}
  243. bool Update (float time, bool override);
  244. void Update (float &value, float &target, float &duration, float time);
  245. private:
  246. float CurrentValue;
  247. float NormalValue;
  248. float NormalTarget;
  249. float NormalDuration;
  250. float OverrideTarget;
  251. float OverrideDuration;
  252. friend class WeatherMgrClass;
  253. };
  254. class WeatherMgrClass : public SaveLoadSubSystemClass, public NetworkObjectClass
  255. {
  256. public:
  257. enum PrecipitationEnum {
  258. PRECIPITATION_FIRST,
  259. PRECIPITATION_RAIN = PRECIPITATION_FIRST,
  260. PRECIPITATION_SNOW,
  261. PRECIPITATION_ASH,
  262. PRECIPITATION_COUNT
  263. };
  264. WeatherMgrClass();
  265. ~WeatherMgrClass() {}
  266. uint32 Chunk_ID() const {return (CHUNKID_WEATHER_MGR);}
  267. const char *Name() const {return ("WeatherMgrClass");}
  268. void Delete (void) {}
  269. virtual void Set_Delete_Pending (void) {};
  270. bool Save (ChunkSaveClass &csave);
  271. bool Load (ChunkLoadClass &cload);
  272. bool Load_Micro_Chunks (ChunkLoadClass &cload);
  273. void Export_Rare (BitStreamClass &packet);
  274. void Import_Rare (BitStreamClass &packet);
  275. static void Init (SoundEnvironmentClass *soundenvironment);
  276. static void Reset();
  277. static void Shutdown();
  278. static bool Save_Dynamic (ChunkSaveClass &csave);
  279. static bool Load_Dynamic (ChunkLoadClass &cload);
  280. static bool Load_Dynamic_Micro_Chunks (ChunkLoadClass &cload);
  281. static bool Set_Wind (float heading, float speed, float variability, float ramptime = 0.0f);
  282. static bool Override_Wind (float heading, float speed, float variability, float ramptime = 0.0f);
  283. static void Get_Wind (float &heading, float &speed, float &variability);
  284. static void Restore_Wind (float ramptime);
  285. static bool Set_Precipitation (PrecipitationEnum precipitation, float density, float ramptime = 0.0f);
  286. static bool Override_Precipitation (PrecipitationEnum precipitation, float density, float ramptime = 0.0f);
  287. static void Get_Precipitation (PrecipitationEnum precipitation, float &density);
  288. static void Restore_Precipitation (float ramptime);
  289. static void Set_Fog_Enable (bool enabled)
  290. {
  291. _FogEnabled = enabled;
  292. Set_Dirty();
  293. }
  294. static bool Get_Fog_Enable()
  295. {
  296. return (_FogEnabled);
  297. }
  298. static bool Set_Fog_Range (float startdistance, float enddistance, float ramptime = 0.0f);
  299. static void Get_Fog_Range (float &startdistance, float &enddistance);
  300. static void Update (PhysicsSceneClass *scene, CameraClass *camera);
  301. static void Render (const CameraClass *camera);
  302. private:
  303. #define VARID_PARAMETER(varname) \
  304. VARID_ ## varname ## _CURRENT_VALUE, \
  305. VARID_ ## varname ## _NORMAL_VALUE, \
  306. VARID_ ## varname ## _NORMAL_TARGET, \
  307. VARID_ ## varname ## _NORMAL_DURATION, \
  308. VARID_ ## varname ## _OVERRIDE_TARGET, \
  309. VARID_ ## varname ## _OVERRIDE_DURATION
  310. // Constants.
  311. enum {
  312. CHUNKID_MICRO_CHUNKS = 0x03020113,
  313. CHUNKID_DYNAMIC_MICRO_CHUNKS = 0x11020245
  314. };
  315. enum {
  316. VARID_DUMMY = 0x09,
  317. VARID_PARAMETER (WIND_HEADING),
  318. VARID_PARAMETER (WIND_SPEED),
  319. VARID_PARAMETER (WIND_VARIABILITY),
  320. VARID_PARAMETER (RAIN_DENSITY),
  321. VARID_PARAMETER (SNOW_DENSITY),
  322. VARID_PARAMETER (ASH_DENSITY),
  323. VARID_WIND_OVERRIDE_COUNT,
  324. VARID_PRECIPITATION_OVERRIDE_COUNT,
  325. VARID_FOG_ENABLED,
  326. VARID_PARAMETER (FOG_START_DISTANCE),
  327. VARID_PARAMETER (FOG_END_DISTANCE)
  328. };
  329. #undef VARID_PARAMETER
  330. enum {
  331. PARAMETER_WIND_HEADING,
  332. PARAMETER_WIND_SPEED,
  333. PARAMETER_WIND_VARIABILITY,
  334. PARAMETER_RAIN_DENSITY,
  335. PARAMETER_SNOW_DENSITY,
  336. PARAMETER_ASH_DENSITY,
  337. PARAMETER_FOG_START_DISTANCE,
  338. PARAMETER_FOG_END_DISTANCE,
  339. PARAMETER_COUNT
  340. };
  341. static bool Set_Wind (float heading, float speed, float variability, float ramptime, bool override);
  342. static bool Set_Precipitation (PrecipitationEnum precipitation, float density, float ramptime, bool override);
  343. static bool Is_Dirty() {return (_Dirty);}
  344. static void Set_Dirty (bool dirty = true) {_Dirty = dirty;}
  345. static SoundEnvironmentClass *_SoundEnvironment;
  346. static WeatherParameterClass _Parameters [PARAMETER_COUNT];
  347. static bool _Prime;
  348. static bool _Imported;
  349. static unsigned _WindOverrideCount;
  350. static unsigned _PrecipitationOverrideCount;
  351. static WindClass *_Wind;
  352. static WeatherSystemClass *_Precipitation [PRECIPITATION_COUNT];
  353. static bool _FogEnabled;
  354. static bool _Dirty;
  355. };
  356. // Externals.
  357. extern WeatherMgrClass _TheWeatherMgr;
  358. #endif // WEATHERMGR_H