crandom.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/Combat/crandom.h $*
  25. * *
  26. * $Author:: Byon_g $*
  27. * *
  28. * $Modtime:: 5/09/01 3:48p $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #ifndef CRANDOM_H
  36. #define CRANDOM_H
  37. #ifndef ALWAYS_H
  38. #include "always.h"
  39. #endif
  40. #ifndef RANDOM_H
  41. #include "random.h"
  42. #endif
  43. #ifndef WWDEBUG_H
  44. #include "wwdebug.h"
  45. #endif
  46. #define CRANDOM_FLOAT_RANGE 0x1000
  47. /*
  48. ** Commando Random Numbers Manager
  49. */
  50. class CRandom {
  51. public:
  52. CRandom( void ) {}
  53. ~CRandom( void ) {}
  54. // Get a random 32 bit long integer
  55. inline int Get_Int( void ) { return Generator(); }
  56. // Get a random 32 bit long integer less than max
  57. inline int Get_Int( int max ) { WWASSERT( max > 0 ); return (Generator() & 0x7FFFFFFF) % max; }
  58. // Get a random 32 bit long between min and max (both inclusive)
  59. inline int Get_Int( int min, int max );
  60. // Get a random float between 0 and 1 (both inclusive)
  61. inline float Get_Float( void ) { return (float)(Get_Int( CRANDOM_FLOAT_RANGE+1 )) / (float)CRANDOM_FLOAT_RANGE; }
  62. // Get a random float between 0 and max (both inclusive)
  63. inline float Get_Float( float max ) { return Get_Float() * max; }
  64. // Get a random float between min and max (both inclusive)
  65. inline float Get_Float( float min, float max );
  66. private:
  67. Random2Class Generator;
  68. };
  69. // Get a random 32 bit long between min and max (both inclusive)
  70. inline int CRandom::Get_Int( int min, int max )
  71. {
  72. // make sure we have a valid range
  73. if ( min > max ) {
  74. int temp = min;
  75. min = max;
  76. max = temp;
  77. }
  78. // Get one
  79. return Get_Int( max - min ) + min;
  80. }
  81. // Get a random float between min and max (both inclusive)
  82. inline float CRandom::Get_Float( float min, float max )
  83. {
  84. // make sure we have a valid range
  85. if ( min > max ) {
  86. float temp = min;
  87. min = max;
  88. max = temp;
  89. }
  90. // Get one
  91. return Get_Float() * ( max - min ) + min;
  92. }
  93. /*
  94. ** A free random number generator. This can be used for any numbers not required to
  95. ** be synced between other computers. Good for simple visual and sound effects.
  96. */
  97. extern CRandom FreeRandom;
  98. #endif