Radar.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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: Radar.h //////////////////////////////////////////////////////////////////////////////////
  24. // Author: Colin Day, January 2002
  25. // Desc: Logical radar implementation
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #pragma once
  28. #ifndef __RADAR_H_
  29. #define __RADAR_H_
  30. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  31. #include "Lib/BaseType.h"
  32. #include "Common/SubsystemInterface.h"
  33. #include "Common/GameMemory.h"
  34. #include "GameClient/Display.h" // for ShroudLevel
  35. #include "GameClient/Color.h"
  36. // FORWARD REFERENCES /////////////////////////////////////////////////////////////////////////////
  37. class GameWindow;
  38. class Object;
  39. class Player;
  40. class TerrainLogic;
  41. // GLOBAL /////////////////////////////////////////////////////////////////////////////////////////
  42. //
  43. // the following is used for the resolution of the radar "cells" ... this is how accurate
  44. // the radar is and also reflects directly the size of the image we build ... which with
  45. // WW3D must be a square power of two as well
  46. //
  47. enum
  48. {
  49. RADAR_CELL_WIDTH = 128, // radar created at this horz resolution
  50. RADAR_CELL_HEIGHT = 128 // radar created at this vert resolution
  51. };
  52. //-------------------------------------------------------------------------------------------------
  53. /** These event types determine the colors radar events happen in to make it easier for us
  54. * to play events with a consistent color scheme */
  55. //-------------------------------------------------------------------------------------------------
  56. enum RadarEventType
  57. {
  58. RADAR_EVENT_INVALID = 0,
  59. RADAR_EVENT_CONSTRUCTION,
  60. RADAR_EVENT_UPGRADE,
  61. RADAR_EVENT_UNDER_ATTACK,
  62. RADAR_EVENT_INFORMATION,
  63. RADAR_EVENT_BEACON_PULSE,
  64. RADAR_EVENT_INFILTRATION, //for defection, hijacking, hacking, carbombing, and other sneaks
  65. RADAR_EVENT_BATTLE_PLAN,
  66. RADAR_EVENT_STEALTH_DISCOVERED, // we discovered a stealth unit
  67. RADAR_EVENT_STEALTH_NEUTRALIZED, // our stealth unit has been revealed
  68. RADAR_EVENT_FAKE, //Internally creates a radar event, but doesn't notify the player (unit lost
  69. //for example, so we can use the spacebar to jump to the event).
  70. RADAR_EVENT_NUM_EVENTS // keep this last
  71. };
  72. // PROTOTYPES /////////////////////////////////////////////////////////////////////////////////////
  73. //-------------------------------------------------------------------------------------------------
  74. /** Radar objects are objects that are on the radar, go figure :) */
  75. //-------------------------------------------------------------------------------------------------
  76. class RadarObject : public MemoryPoolObject,
  77. public Snapshot
  78. {
  79. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE( RadarObject, "RadarObject" )
  80. public:
  81. RadarObject( void );
  82. // destructor prototype defined by memory pool glue
  83. // color management
  84. void setColor( Color c ) { m_color = c; }
  85. inline Color getColor( void ) const { return m_color; }
  86. inline void friend_setObject( Object *obj ) { m_object = obj; }
  87. inline Object *friend_getObject( void ) { return m_object; }
  88. inline const Object *friend_getObject( void ) const { return m_object; }
  89. inline void friend_setNext( RadarObject *next ) { m_next = next; }
  90. inline RadarObject *friend_getNext( void ) { return m_next; }
  91. inline const RadarObject *friend_getNext( void ) const { return m_next; }
  92. Bool isTemporarilyHidden() const;
  93. protected:
  94. // snapshot methods
  95. virtual void crc( Xfer *xfer );
  96. virtual void xfer( Xfer *xfer );
  97. virtual void loadPostProcess( void );
  98. Object *m_object; ///< the object
  99. RadarObject *m_next; ///< next radar object
  100. Color m_color; ///< color to draw for this object on the radar
  101. };
  102. //-------------------------------------------------------------------------------------------------
  103. /** Radar priorities. Keep this in sync with the priority names list below */
  104. //-------------------------------------------------------------------------------------------------
  105. enum RadarPriorityType
  106. {
  107. RADAR_PRIORITY_INVALID, // a priority that has not been set (in general it won't show up on the radar)
  108. RADAR_PRIORITY_NOT_ON_RADAR, // object specifically forbidden from being on the radar
  109. RADAR_PRIORITY_STRUCTURE, // structure level drawing priority
  110. RADAR_PRIORITY_UNIT, // unit level drawing priority
  111. RADAR_PRIORITY_LOCAL_UNIT_ONLY, // unit priority, but only on the radar if controlled by the local player
  112. RADAR_PRIORITY_NUM_PRIORITIES // keep this last
  113. };
  114. #ifdef DEFINE_RADAR_PRIORITY_NAMES
  115. static const char *RadarPriorityNames[] =
  116. {
  117. "INVALID", // a priority that has not been set (in general it won't show up on the radar)
  118. "NOT_ON_RADAR", // object specifically forbidden from being on the radar
  119. "STRUCTURE", // structure level drawing priority
  120. "UNIT", // unit level drawing priority
  121. "LOCAL_UNIT_ONLY", // unit priority, but only on the radar if controlled by the local player
  122. NULL // keep this last
  123. };
  124. #endif // DEFINE_RADAR_PRIOTITY_NAMES
  125. //-------------------------------------------------------------------------------------------------
  126. /** Interface for the radar */
  127. //-------------------------------------------------------------------------------------------------
  128. class Radar : public Snapshot,
  129. public SubsystemInterface
  130. {
  131. public:
  132. Radar( void );
  133. virtual ~Radar( void );
  134. virtual void init( void ) { } ///< subsystem initialization
  135. virtual void reset( void ); ///< subsystem reset
  136. virtual void update( void ); ///< subsystem per frame update
  137. // is the game window parameter the radar window
  138. Bool isRadarWindow( GameWindow *window ) { return (m_radarWindow == window) && (m_radarWindow != NULL); }
  139. Bool radarToWorld( const ICoord2D *radar, Coord3D *world ); ///< radar point to world point on terrain
  140. Bool worldToRadar( const Coord3D *world, ICoord2D *radar ); ///< translate world point to radar (x,y)
  141. Bool localPixelToRadar( const ICoord2D *pixel, ICoord2D *radar ); ///< translate pixel (with UL of radar being (0,0)) to logical radar coords
  142. Bool screenPixelToWorld( const ICoord2D *pixel, Coord3D *world ); ///< translate pixel (with UL of the screen being (0,0)) to world position in the world
  143. Object *objectUnderRadarPixel( const ICoord2D *pixel ); ///< return the object (if any) represented by the pixel coords passed in
  144. void findDrawPositions( Int startX, Int startY, Int width, Int height,
  145. ICoord2D *ul, ICoord2D *lr ); ///< make translation for screen area of radar square to scaled aspect ratio preserving points inside the radar area
  146. // priority inquiry
  147. Bool isPriorityVisible( RadarPriorityType priority ) const; ///< is the priority passed in a "visible" one on the radar
  148. // radar events
  149. void createEvent( const Coord3D *world, RadarEventType type, Real secondsToLive = 4.0f ); ///< create radar event at location in world
  150. void createPlayerEvent( Player *player, const Coord3D *world, RadarEventType type, Real secondsToLive = 4.0f ); ///< create radar event using player colors
  151. Bool getLastEventLoc( Coord3D *eventPos ); ///< get last event loc (if any)
  152. void tryUnderAttackEvent( const Object *obj ); ///< try to make an "under attack" event if it's the proper time
  153. void tryInfiltrationEvent( const Object *obj ); ///< try to make an "infiltration" event if it's the proper time
  154. Bool tryEvent( RadarEventType event, const Coord3D *pos ); ///< try to make a "stealth" event
  155. // adding and removing objects from the radar
  156. void addObject( Object *obj ); ///< add object to radar
  157. void removeObject( Object *obj ); ///< remove object from radar
  158. void examineObject( Object *obj ); ///< re-examine object and resort if needed
  159. // radar options
  160. void hide( Bool hide ) { m_radarHidden = hide; } ///< hide/unhide the radar
  161. Bool isRadarHidden( void ) { return m_radarHidden; } ///< is radar hidden
  162. // other radar option methods here like the ability to show a certain
  163. // team, show buildings, show units at all, etc
  164. // forcing the radar on/off regardless of player situation
  165. void forceOn( Bool force ) { m_radarForceOn = force; } ///< force the radar to be on
  166. Bool isRadarForced( void ) { return m_radarForceOn; } ///< is radar forced on?
  167. /// refresh the water values for the radar
  168. virtual void refreshTerrain( TerrainLogic *terrain );
  169. /// queue a refresh of the terran at the next available time
  170. virtual void queueTerrainRefresh( void );
  171. virtual void newMap( TerrainLogic *terrain ); ///< reset radar for new map
  172. virtual void draw( Int pixelX, Int pixelY, Int width, Int height ) = 0; ///< draw the radar
  173. /// empty the entire shroud
  174. virtual void clearShroud() = 0;
  175. /// set the shroud level at shroud cell x,y
  176. virtual void setShroudLevel( Int x, Int y, CellShroudStatus setting ) = 0;
  177. protected:
  178. // snapshot methods
  179. virtual void crc( Xfer *xfer );
  180. virtual void xfer( Xfer *xfer );
  181. virtual void loadPostProcess( void );
  182. /// internal method for creating a radar event with specific colors
  183. void internalCreateEvent( const Coord3D *world, RadarEventType type, Real secondsToLive,
  184. const RGBAColorInt *color1, const RGBAColorInt *color2 );
  185. void deleteListResources( void ); ///< delete list radar resources used
  186. Bool deleteFromList( Object *obj, RadarObject **list ); ///< try to remove object from specific list
  187. inline Real getTerrainAverageZ() const { return m_terrainAverageZ; }
  188. inline Real getWaterAverageZ() const { return m_waterAverageZ; }
  189. inline const RadarObject* getObjectList() const { return m_objectList; }
  190. inline const RadarObject* getLocalObjectList() const { return m_localObjectList; }
  191. void clearAllEvents( void ); ///< remove all radar events in progress
  192. // search the object list for an object that maps to the given logical radar coords
  193. Object *searchListForRadarLocationMatch( RadarObject *listHead, ICoord2D *radarMatch );
  194. Bool m_radarHidden; ///< true when radar is not visible
  195. Bool m_radarForceOn; ///< true when radar is forced to be on
  196. RadarObject *m_objectList; ///< list of objects in the radar
  197. RadarObject *m_localObjectList; /** list of objects for the local player, sorted
  198. * in exactly the same priority as the regular
  199. * object list for all other objects */
  200. Real m_terrainAverageZ; ///< average Z for terrain samples
  201. Real m_waterAverageZ; ///< average Z for water samples
  202. //
  203. // when dealing with world sampling we will sample at these intervals so that
  204. // the whole map can be accounted for within our RADAR_CELL_WIDTH and
  205. // RADAR_CELL_HEIGHT resolutions
  206. //
  207. Real m_xSample;
  208. Real m_ySample;
  209. enum { MAX_RADAR_EVENTS = 64 };
  210. struct RadarEvent
  211. {
  212. RadarEventType type; ///< type of this radar event
  213. Bool active; ///< TRUE when event is "active", otherwise it's just historical information in the event array to look through
  214. UnsignedInt createFrame; ///< frame event was created on
  215. UnsignedInt dieFrame; ///< frame the event will go away on
  216. UnsignedInt fadeFrame; ///< start fading out on this frame
  217. RGBAColorInt color1; ///< color 1 for drawing
  218. RGBAColorInt color2; ///< color 2 for drawing
  219. Coord3D worldLoc; ///< location of event in the world
  220. ICoord2D radarLoc; ///< 2D radar location of the event
  221. Bool soundPlayed; ///< TRUE when we have played the radar sound for this
  222. };
  223. RadarEvent m_event[ MAX_RADAR_EVENTS ];///< our radar events
  224. Int m_nextFreeRadarEvent; ///< index into m_event for where to store the next event
  225. Int m_lastRadarEvent; ///< index of the most recent radar event
  226. GameWindow *m_radarWindow; ///< window we display the radar in
  227. Region3D m_mapExtent; ///< extents of the current map
  228. UnsignedInt m_queueTerrainRefreshFrame; ///< frame we requested the last terrain refresh on
  229. };
  230. // EXTERNALS //////////////////////////////////////////////////////////////////////////////////////
  231. extern Radar *TheRadar; ///< the radar singleton extern
  232. #endif // __RADAR_H_