FACING.CPP 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: /CounterStrike/FACING.CPP 1 3/03/97 10:24a Joe_bostic $ */
  15. /***********************************************************************************************
  16. *** 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 ***
  17. ***********************************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : FACING.CPP *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : 03/21/95 *
  26. * *
  27. * Last Update : March 21, 1995 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * FacingClass::FacingClass -- Default constructor for the facing class. *
  32. * FacingClass::Rotation_Adjust -- Perform a rotation adjustment to current facing. *
  33. * FacingClass::Set_Current -- Sets the current rotation value. *
  34. * FacingClass::Set_Desired -- Sets the desired facing value. *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #include "function.h"
  37. #include "facing.h"
  38. /***********************************************************************************************
  39. * FacingClass::FacingClass -- Default constructor for the facing class. *
  40. * *
  41. * This default constructor merely sets the desired and current facing values to be the *
  42. * same (North). *
  43. * *
  44. * INPUT: none *
  45. * *
  46. * OUTPUT: none *
  47. * *
  48. * WARNINGS: none *
  49. * *
  50. * HISTORY: *
  51. * 03/21/1995 JLB : Created. *
  52. *=============================================================================================*/
  53. FacingClass::FacingClass(void)
  54. {
  55. CurrentFacing = DIR_N;
  56. DesiredFacing = DIR_N;
  57. }
  58. /***********************************************************************************************
  59. * FacingClass::Set_Desired -- Sets the desired facing value. *
  60. * *
  61. * This routine is used to set the desired facing value without altering the current *
  62. * facing setting. Typical use of this routine is when a vehicle needs to face a *
  63. * direction, but currently isn't facing the correct direction. After this routine is *
  64. * called, it is presumed that subsequent calls to Rotation_Adjust() will result in the *
  65. * eventual alignment of the current facing. *
  66. * *
  67. * INPUT: facing -- The new facing to assign to the desired value. *
  68. * *
  69. * OUTPUT: bool; Did the desired facing value actually change by this routine call? *
  70. * *
  71. * WARNINGS: none *
  72. * *
  73. * HISTORY: *
  74. * 03/21/1995 JLB : Created. *
  75. *=============================================================================================*/
  76. int FacingClass::Set_Desired(DirType facing)
  77. {
  78. if (DesiredFacing != facing) {
  79. DesiredFacing = facing;
  80. return(true);
  81. }
  82. return(false);
  83. }
  84. /***********************************************************************************************
  85. * FacingClass::Set_Current -- Sets the current rotation value. *
  86. * *
  87. * This routine will set the current rotation value. It is used to override the facing *
  88. * value without adjusting the desired setting. *
  89. * *
  90. * INPUT: facing -- The new facing to assign to the current facing value. *
  91. * *
  92. * OUTPUT: bool; Was the current setting changed by this routine. Failure means that the *
  93. * current setting was already at the value specified. *
  94. * *
  95. * WARNINGS: none *
  96. * *
  97. * HISTORY: *
  98. * 03/21/1995 JLB : Created. *
  99. *=============================================================================================*/
  100. int FacingClass::Set_Current(DirType facing)
  101. {
  102. if (CurrentFacing != facing) {
  103. CurrentFacing = facing;
  104. return(true);
  105. }
  106. return(false);
  107. }
  108. /***********************************************************************************************
  109. * FacingClass::Rotation_Adjust -- Perform a rotation adjustment to current facing. *
  110. * *
  111. * This routine is used when the current and desired facings differ but the current *
  112. * facing should be adjusted toward the desired facing. The amount of rotation to adjust *
  113. * is provided as a rotation rate parameter. Typical use of this routine is for turrets *
  114. * and other vehicle related rotating. *
  115. * *
  116. * INPUT: rate -- The rotation rate to use when adjusting the current facing toward the *
  117. * desired facing. A value of 127 means instantaneous rotation. *
  118. * *
  119. * OUTPUT: bool; Did the rotation result in the current facing transitioning from one *
  120. * 1/32 zone to another? If true, then the owning object most likely will *
  121. * need to be redrawn to reflect the change. *
  122. * *
  123. * WARNINGS: none *
  124. * *
  125. * HISTORY: *
  126. * 03/21/1995 JLB : Created. *
  127. *=============================================================================================*/
  128. int FacingClass::Rotation_Adjust(int rate)
  129. {
  130. /*
  131. ** Only perform the rotation adjustment if the desired facing is not the
  132. ** same as the current facing.
  133. */
  134. if (Is_Rotating()) {
  135. rate = min(rate, 127);
  136. DirType oldfacing = CurrentFacing;
  137. int diff = Difference();
  138. /*
  139. ** If the allowed facing change is greater than the difference between
  140. ** the current facing and the desired facing, then just snap the
  141. ** facing to the new value.
  142. */
  143. if (ABS(diff) < rate) {
  144. CurrentFacing = DesiredFacing;
  145. } else {
  146. /*
  147. ** Adjust the current facing clockwise or counterclockwise depending
  148. ** on the shortest distance to the desired facing from the current
  149. ** facing.
  150. */
  151. if (diff < 0) {
  152. CurrentFacing = (DirType)(CurrentFacing - (DirType)rate);
  153. } else {
  154. CurrentFacing = (DirType)(CurrentFacing + (DirType)rate);
  155. }
  156. }
  157. /*
  158. ** If this facing adjustment caused the current facing to rotate into a
  159. ** new 1/32 rotation zone (likely to cause a redraw), then return
  160. ** this fact with a true value.
  161. */
  162. return(Dir_To_32(CurrentFacing) != Dir_To_32(oldfacing));
  163. }
  164. return(false);
  165. }