POWER.CPP 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. ** Command & Conquer(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. /* $Header: F:\projects\c&c\vcs\code\power.cpv 2.18 16 Oct 1995 16:52:10 JOE_BOSTIC $ */
  19. /***********************************************************************************************
  20. *** 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 ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : POWER.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 12/15/94 *
  30. * *
  31. * Last Update : August 7, 1995 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * PowerClass::AI -- Process the power bar logic. *
  36. * PowerClass::Draw_It -- Renders the power bar graphic. *
  37. * PowerClass::Init_Clear -- Clears all the power bar variables. *
  38. * PowerClass::One_Time -- One time processing for the power bar. *
  39. * PowerClass::PowerButtonClass::Action -- Handles the mouse over the power bar area. *
  40. * PowerClass::PowerClass -- Default constructor for the power bar class. *
  41. * PowerClass::Refresh_Cells -- Intercepts the redraw logic to see if sidebar to redraw too. *
  42. * Power_Height -- Given a value figure where it falls on bar *
  43. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  44. #include "function.h"
  45. /*
  46. ** Points to the shape to use for the "desired" power level indicator.
  47. */
  48. void const * PowerClass::PowerShape;
  49. void const * PowerClass::PowerBarShape;
  50. PowerClass::PowerButtonClass PowerClass::PowerButton;
  51. /***********************************************************************************************
  52. * PowerClass::PowerClass -- Default constructor for the power bar class. *
  53. * *
  54. * This is the default constructor for the power bar class. It doesn't really do anything. *
  55. * *
  56. * INPUT: none *
  57. * *
  58. * OUTPUT: none *
  59. * *
  60. * WARNINGS: none *
  61. * *
  62. * HISTORY: *
  63. * 12/20/1994 JLB : Created. *
  64. *=============================================================================================*/
  65. PowerClass::PowerClass(void)
  66. {
  67. IsToRedraw = false;
  68. RecordedDrain = -1;
  69. RecordedPower = -1;
  70. DesiredDrainHeight = 0;
  71. DesiredPowerHeight = 0;
  72. DrainHeight = 0;
  73. PowerHeight = 0;
  74. PowerBounce = 0;
  75. DrainBounce = 0;
  76. DrainDir = 0;
  77. PowerDir = 0;
  78. }
  79. /***********************************************************************************************
  80. * PowerClass::Init_Clear -- Clears all the power bar variables. *
  81. * *
  82. * This routine is called in preparation for the start of a scenario. The power bar is *
  83. * initialized into the null state by this routine. As soon as the scenario starts, the *
  84. * power bar will rise to reflect the actual power output and drain. *
  85. * *
  86. * INPUT: none *
  87. * *
  88. * OUTPUT: none *
  89. * *
  90. * WARNINGS: none *
  91. * *
  92. * HISTORY: *
  93. * 08/07/1995 JLB : Created. *
  94. *=============================================================================================*/
  95. void PowerClass::Init_Clear(void)
  96. {
  97. RadarClass::Init_Clear();
  98. RecordedDrain = -1;
  99. RecordedPower = -1;
  100. DesiredDrainHeight = 0;
  101. DesiredPowerHeight = 0;
  102. DrainHeight = 0;
  103. PowerHeight = 0;
  104. PowerBounce = 0;
  105. DrainBounce = 0;
  106. DrainDir = 0;
  107. PowerDir = 0;
  108. }
  109. /***********************************************************************************************
  110. * PowerClass::One_Time -- One time processing for the power bar. *
  111. * *
  112. * This routine is for code that truly only needs to be done once per game run. *
  113. * *
  114. * INPUT: none *
  115. * *
  116. * OUTPUT: none *
  117. * *
  118. * WARNINGS: none *
  119. * *
  120. * HISTORY: *
  121. * 12/26/1994 JLB : Created. *
  122. *=============================================================================================*/
  123. void PowerClass::One_Time(void)
  124. {
  125. RadarClass::One_Time();
  126. int factor = Get_Resolution_Factor();
  127. PowX = SeenBuff.Get_Width() - Map.RadWidth;
  128. PowY = Map.RadY+Map.RadHeight + (13 << factor);
  129. PowWidth = 8 << factor;
  130. PowHeight = SeenBuff.Get_Height() - PowY;
  131. PowLineSpace = 5 << factor;
  132. PowLineWidth = PowWidth - 4;
  133. PowerButton.X = PowX;
  134. PowerButton.Y = PowY;
  135. PowerButton.Width = PowWidth-1;
  136. PowerButton.Height = PowHeight;
  137. PowerShape = MixFileClass::Retrieve((factor)? "HPOWER.SHP" :"POWER.SHP");
  138. PowerBarShape = Hires_Retrieve("PWRBAR.SHP");
  139. }
  140. /***********************************************************************************************
  141. * PowerClass::Draw_It -- Renders the power bar graphic. *
  142. * *
  143. * This routine will draw the power bar graphic to the LogicPage. *
  144. * *
  145. * INPUT: complete -- Should the power bar be redrawn even if it isn't specifically flagged *
  146. * to do so? *
  147. * *
  148. * OUTPUT: none *
  149. * *
  150. * WARNINGS: none *
  151. * *
  152. * HISTORY: *
  153. * 12/20/1994 JLB : Created. *
  154. * 12/27/1994 JLB : Changes power bar color depending on amount of power. *
  155. *=============================================================================================*/
  156. void PowerClass::Draw_It(bool complete)
  157. {
  158. static int _modtable[]={
  159. 0, -1, 0, 1, 0, -1, -2, -1, 0, 1, 2, 1 ,0
  160. };
  161. int power_color;
  162. if (complete || IsToRedraw) {
  163. // PowX = TacPixelX + TacWidth*ICON_PIXEL_W; // X position of upper left corner of power bar.
  164. if (LogicPage->Lock()){
  165. if (Map.IsSidebarActive) {
  166. IsToRedraw = false;
  167. /*
  168. ** 1st get the height of the filled section of the power bar
  169. */
  170. int bottom = PowY + PowHeight - 1;
  171. int power_height = (PowerHeight == DesiredPowerHeight) ? PowerHeight + (_modtable[PowerBounce] * PowerDir) : PowerHeight;
  172. int drain_height = (DrainHeight == DesiredDrainHeight) ? DrainHeight + (_modtable[DrainBounce] * DrainDir) : DrainHeight;
  173. power_height = Bound(power_height, 0, PowHeight - 2);
  174. drain_height = Bound(drain_height, 0, PowHeight - 2);
  175. /*
  176. ** Create a clip region to draw the unfilled section of the bar
  177. */
  178. WindowList[WINDOW_CUSTOM][WINDOWX] = 0;
  179. WindowList[WINDOW_CUSTOM][WINDOWY] = 0;
  180. WindowList[WINDOW_CUSTOM][WINDOWWIDTH] = SeenBuff.Get_Width();
  181. WindowList[WINDOW_CUSTOM][WINDOWHEIGHT] = bottom-power_height;
  182. /*
  183. ** Draw the unfilled section
  184. */
  185. CC_Draw_Shape(PowerBarShape, 0, PowX, PowY, WINDOW_CUSTOM, SHAPE_WIN_REL);
  186. CC_Draw_Shape(PowerBarShape, 1 ,PowX, PowY+100, WINDOW_CUSTOM, SHAPE_WIN_REL);
  187. /*
  188. ** Set up the clip region for the filled section
  189. */
  190. WindowList[WINDOW_CUSTOM][WINDOWY] = bottom-power_height;
  191. WindowList[WINDOW_CUSTOM][WINDOWHEIGHT] = SeenBuff.Get_Height() - WindowList[WINDOW_CUSTOM][WINDOWY];
  192. /*
  193. ** What color is the filled section?
  194. */
  195. if (power_height) {
  196. power_color = 0; //green
  197. if (PlayerPtr->Drain > PlayerPtr->Power) {
  198. power_color = 2;
  199. }
  200. if (PlayerPtr->Drain > (PlayerPtr->Power * 2)) {
  201. power_color = 4;
  202. }
  203. /*
  204. ** Draw the filled section
  205. */
  206. CC_Draw_Shape(PowerBarShape, 2+power_color,
  207. PowX,
  208. PowY - WindowList[WINDOW_CUSTOM][WINDOWY],
  209. WINDOW_CUSTOM,
  210. SHAPE_WIN_REL);
  211. CC_Draw_Shape(PowerBarShape, 3+power_color,
  212. PowX,
  213. PowY - WindowList[WINDOW_CUSTOM][WINDOWY] + 100,
  214. WINDOW_CUSTOM,
  215. SHAPE_WIN_REL);
  216. }
  217. /*
  218. ** Draw the power drain threshold marker.
  219. */
  220. CC_Draw_Shape(PowerShape, 0, PowX, bottom - drain_height + 1, WINDOW_MAIN, SHAPE_NORMAL);
  221. }
  222. LogicPage->Unlock();
  223. }
  224. }
  225. RadarClass::Draw_It(complete);
  226. }
  227. /***********************************************************************************************
  228. * PowerClass::AI -- Process the power bar logic. *
  229. * *
  230. * Use this routine to process the power bar logic. This consists of animation effects. *
  231. * *
  232. * INPUT: input -- The player input value to be consumed or ignored as appropriate. *
  233. * *
  234. * x,y -- Mouse coordinate parameters to use. *
  235. * *
  236. * OUTPUT: none *
  237. * *
  238. * WARNINGS: none *
  239. * *
  240. * HISTORY: *
  241. * 12/20/1994 JLB : Created. *
  242. * 12/31/1994 JLB : Uses mouse coordinate parameters. *
  243. *=============================================================================================*/
  244. void PowerClass::AI(KeyNumType &input, int x, int y)
  245. {
  246. // if (!IsActive) {
  247. // IsActive = true;
  248. // IsToRedraw = true;
  249. // Flag_To_Redraw(false);
  250. // }
  251. if (Map.IsSidebarActive /*IsActive*/) {
  252. int olddrain = DrainHeight;
  253. int oldpower = PowerHeight;
  254. /*
  255. ** If the recorded power value has changed we need to adjust for
  256. ** it.
  257. */
  258. if (PlayerPtr->Power != RecordedPower) {
  259. DesiredPowerHeight = Power_Height(PlayerPtr->Power);
  260. RecordedPower = PlayerPtr->Power;
  261. PowerBounce = 12;
  262. if (PowerHeight > DesiredPowerHeight) {
  263. PowerDir = -1;
  264. } else if (PowerHeight < DesiredPowerHeight) {
  265. PowerDir = 1;
  266. } else {
  267. PowerBounce = 0;
  268. }
  269. }
  270. /*
  271. ** If the recorded drain value has changed we need to adjust for
  272. ** it.
  273. */
  274. if (PlayerPtr->Drain != RecordedDrain) {
  275. DesiredDrainHeight = Power_Height(PlayerPtr->Drain);
  276. RecordedDrain = PlayerPtr->Drain;
  277. DrainBounce = 12;
  278. if (DrainHeight > DesiredDrainHeight) {
  279. DrainDir = -1;
  280. } else if (DrainHeight < DesiredDrainHeight) {
  281. DrainDir = 1;
  282. } else {
  283. DrainBounce = 0;
  284. }
  285. }
  286. if (DrainBounce && DrainHeight == DesiredDrainHeight) {
  287. IsToRedraw = true;
  288. Flag_To_Redraw(false);
  289. DrainBounce--;
  290. } else {
  291. /*
  292. ** If we need to move the drain height then do so.
  293. */
  294. if (DrainHeight != DesiredDrainHeight) {
  295. DrainHeight += DrainDir;
  296. }
  297. }
  298. if (PowerBounce && PowerHeight == DesiredPowerHeight) {
  299. IsToRedraw = true;
  300. Flag_To_Redraw(false);
  301. PowerBounce--;
  302. } else {
  303. /*
  304. ** If we need to move the power height then do so.
  305. */
  306. if (PowerHeight != DesiredPowerHeight) {
  307. PowerHeight += PowerDir;
  308. }
  309. }
  310. if (olddrain != DrainHeight || oldpower != PowerHeight) {
  311. IsToRedraw = true;
  312. Flag_To_Redraw(false);
  313. }
  314. }
  315. RadarClass::AI(input, x, y);
  316. }
  317. /***********************************************************************************************
  318. * PowerClass::Refresh_Cells -- Intercepts the redraw logic to see if sidebar to redraw too. *
  319. * *
  320. * This routine will examine a refresh list request and determine if the sidebar would be *
  321. * affect. If so, it will flag the sidebar to be redrawn. *
  322. * *
  323. * INPUT: cell -- The cell that the offset list is base on. *
  324. * *
  325. * list -- The list of cell offset used to flag for redraw. If the special sidebar *
  326. * affecting cell magic offset number is detected, the sidebar is flagged *
  327. * for redraw and the magic offset is removed. *
  328. * *
  329. * OUTPUT: none *
  330. * *
  331. * WARNINGS: none *
  332. * *
  333. * HISTORY: *
  334. * 06/01/1995 JLB : Created. *
  335. *=============================================================================================*/
  336. void PowerClass::Refresh_Cells(CELL cell, short const *list)
  337. {
  338. if (*list == REFRESH_SIDEBAR) {
  339. IsToRedraw = true;
  340. Flag_To_Redraw(false);
  341. }
  342. RadarClass::Refresh_Cells(cell, list);
  343. }
  344. /***************************************************************************
  345. * PowHeight -- Given a value figure where it falls on bar *
  346. * *
  347. * INPUT: int value - the value we are testing *
  348. * *
  349. * OUTPUT: int the height of the point that this value is on graph *
  350. * *
  351. * WARNINGS: none *
  352. * *
  353. * HISTORY: *
  354. * 06/14/1995 PWG : Created. *
  355. *=========================================================================*/
  356. int PowerClass::Power_Height(int value)
  357. {
  358. int num = value/ POWER_STEP_LEVEL; // figure out the initial num of DRAIN_VALUE's
  359. int retval = 0; // currently there is no power
  360. /*
  361. ** Loop through the diffrent hundreds figuring out the fractional piece
  362. ** of each.
  363. */
  364. for (int lp = 0; lp < num; lp ++) {
  365. retval = retval + (((PowHeight - 2) - retval) / POWER_STEP_FACTOR);
  366. value -= POWER_STEP_LEVEL;
  367. }
  368. /*
  369. ** Adjust the retval to factor in the remainder
  370. */
  371. if (value) {
  372. retval = retval + (((((PowHeight - 2) - retval) / POWER_STEP_FACTOR) * value) / POWER_STEP_LEVEL);
  373. }
  374. retval = Bound(retval, 0, PowHeight -2);
  375. return(retval);
  376. }
  377. /***********************************************************************************************
  378. * PowerClass::PowerButtonClass::Action -- Handles the mouse over the power bar area. *
  379. * *
  380. * This routine handles input on the power bar area. Since no input is used for the power *
  381. * bar, this routine just pops up appropriate help text for the power bar. *
  382. * *
  383. * INPUT: flags -- The event flags that triggered this action call. *
  384. * *
  385. * key -- The key code (if any) associated with the trigger event. *
  386. * *
  387. * OUTPUT: Should further button processing be stopped? *
  388. * *
  389. * WARNINGS: none *
  390. * *
  391. * HISTORY: *
  392. * 08/07/1995 JLB : Created. *
  393. *=============================================================================================*/
  394. int PowerClass::PowerButtonClass::Action(unsigned flags, KeyNumType & key)
  395. {
  396. if (!Map.IsSidebarActive) {
  397. return(false);
  398. }
  399. /*
  400. ** Force any help label to disappear when the mouse is held over the
  401. ** radar map.
  402. */
  403. Map.Override_Mouse_Shape(MOUSE_NORMAL);
  404. if (PlayerPtr->Power_Fraction() < 0x0100 && PlayerPtr->Power > 0) {
  405. Map.Help_Text(TXT_POWER_OUTPUT_LOW, -1, -1, CC_GREEN);
  406. } else {
  407. Map.Help_Text(TXT_POWER_OUTPUT, -1, -1, CC_GREEN);
  408. }
  409. GadgetClass::Action(flags, key);
  410. return(true);
  411. }