FACING.CPP 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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\facing.cpv 1.9 16 Oct 1995 16:49:40 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 : FACING.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 03/21/95 *
  30. * *
  31. * Last Update : March 21, 1995 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * FacingClass::Rotation_Adjust -- Perform a rotation adjustment to current facing. *
  36. * FacingClass::Set_Current -- Sets the current rotation value. *
  37. * FacingClass::Set_Desired -- Sets the desired facing value. *
  38. * FacingClass::FacingClass -- Default constructor for the facing class. *
  39. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  40. #include "function.h"
  41. #include "facing.h"
  42. /***********************************************************************************************
  43. * FacingClass::FacingClass -- Default constructor for the facing class. *
  44. * *
  45. * This default constructor merely sets the desired and current facing values to be the *
  46. * same (North). *
  47. * *
  48. * INPUT: none *
  49. * *
  50. * OUTPUT: none *
  51. * *
  52. * WARNINGS: none *
  53. * *
  54. * HISTORY: *
  55. * 03/21/1995 JLB : Created. *
  56. *=============================================================================================*/
  57. FacingClass::FacingClass(void)
  58. {
  59. CurrentFacing = DIR_N;
  60. DesiredFacing = DIR_N;
  61. }
  62. /***********************************************************************************************
  63. * FacingClass::Set_Desired -- Sets the desired facing value. *
  64. * *
  65. * This routine is used to set the desired facing value without altering the current *
  66. * facing setting. Typicall use of this routine is when a vehicle needs to face a *
  67. * direction, but currently isn't facing the correct direction. After this routine is *
  68. * called, it is presumed that subsequent calls to Rotation_Adjust() will result in the *
  69. * eventual alignment of the current facing. *
  70. * *
  71. * INPUT: facing -- The new facing to assign to the desired value. *
  72. * *
  73. * OUTPUT: bool; Did the desired facing value actually change by this routine call? *
  74. * *
  75. * WARNINGS: none *
  76. * *
  77. * HISTORY: *
  78. * 03/21/1995 JLB : Created. *
  79. *=============================================================================================*/
  80. int FacingClass::Set_Desired(DirType facing)
  81. {
  82. if (DesiredFacing != facing) {
  83. DesiredFacing = facing;
  84. return(true);
  85. }
  86. return(false);
  87. }
  88. /***********************************************************************************************
  89. * FacingClass::Set_Current -- Sets the current rotation value. *
  90. * *
  91. * This routine will set the current rotation value. It is used to override the facing *
  92. * value without adjusting the desired setting. *
  93. * *
  94. * INPUT: facing -- The new facing to assign to the current facing value. *
  95. * *
  96. * OUTPUT: bool; Was the current setting changed by this routine. Failure means that the *
  97. * current setting was already at the value specified. *
  98. * *
  99. * WARNINGS: none *
  100. * *
  101. * HISTORY: *
  102. * 03/21/1995 JLB : Created. *
  103. *=============================================================================================*/
  104. int FacingClass::Set_Current(DirType facing)
  105. {
  106. if (CurrentFacing != facing) {
  107. CurrentFacing = facing;
  108. return(true);
  109. }
  110. return(false);
  111. }
  112. /***********************************************************************************************
  113. * FacingClass::Rotation_Adjust -- Perform a rotation adjustment to current facing. *
  114. * *
  115. * This routine is used when the current and desired facings differ but the current *
  116. * facing should be adjusted toward the desired facing. The amount of rotation to adjust *
  117. * is provided as a rotation rate parameter. Typical use of this routine is for turrets *
  118. * and other vehicle related rotating. *
  119. * *
  120. * INPUT: rate -- The rotation rate to use when adjusting the current facing toward the *
  121. * desired facing. A value of 127 means instantaneous rotation. *
  122. * *
  123. * OUTPUT: bool; Did the rotation result in the current facing transitioning from one *
  124. * 1/32 zone to another? If true, then the owning object most likely will *
  125. * need to be redrawn to reflect the change. *
  126. * *
  127. * WARNINGS: none *
  128. * *
  129. * HISTORY: *
  130. * 03/21/1995 JLB : Created. *
  131. *=============================================================================================*/
  132. int FacingClass::Rotation_Adjust(int rate)
  133. {
  134. /*
  135. ** Only perform the rotation adjustment if the desired facing is not the
  136. ** same as the current facing.
  137. */
  138. if (Is_Rotating()) {
  139. rate = MIN(rate, 127);
  140. DirType oldfacing = CurrentFacing;
  141. int diff = Difference();
  142. /*
  143. ** If the allowed facing change is greater than the difference between
  144. ** the current facing and the desired facing, then just snap the
  145. ** facing to the new value.
  146. */
  147. if (ABS(diff) < rate) {
  148. CurrentFacing = DesiredFacing;
  149. } else {
  150. /*
  151. ** Adjust the current facing clockwise or counterclockwise depending
  152. ** on the shortest distance to the desired facing from the current
  153. ** facing.
  154. */
  155. if (diff < 0) {
  156. CurrentFacing = (DirType)(CurrentFacing - (DirType)rate);
  157. } else {
  158. CurrentFacing = (DirType)(CurrentFacing + (DirType)rate);
  159. }
  160. }
  161. /*
  162. ** If this facing adjustment caused the current facing to rotate into a
  163. ** new 1/32 rotation zone (likely to cause a redraw), then return
  164. ** this fact with a true value.
  165. */
  166. return(Facing_To_32(CurrentFacing) != Facing_To_32(oldfacing));
  167. }
  168. return(false);
  169. }