LAYER.CPP 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. ** Command & Conquer Red Alert(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: /CounterStrike/LAYER.CPP 1 3/03/97 10:25a 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 : LAYER.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : May 31, 1994 *
  30. * *
  31. * Last Update : March 10, 1995 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * LayerClass::Sort -- Perform an incremental sort pass on the layer's objects. *
  36. * LayerClass::Sorted_Add -- Adds object in sorted order to layer. *
  37. * LayerClass::Submit -- Adds an object to a layer list. *
  38. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  39. #include "function.h"
  40. #include "layer.h"
  41. /***********************************************************************************************
  42. * LayerClass::Submit -- Adds an object to a layer list. *
  43. * *
  44. * This routine is used to add an object to the layer list. If the list is full, then the *
  45. * object is not added. *
  46. * *
  47. * INPUT: object -- Pointer to the object to add. *
  48. * *
  49. * OUTPUT: bool; Was the object added successfully? *
  50. * *
  51. * WARNINGS: none *
  52. * *
  53. * HISTORY: *
  54. * 05/31/1994 JLB : Created. *
  55. * 05/31/1994 JLB : Allows sorted insert. *
  56. * 01/02/1995 JLB : Fixed to work with EMSListOf template. *
  57. *=============================================================================================*/
  58. bool LayerClass::Submit(ObjectClass const * object, bool sort)
  59. {
  60. /*
  61. ** Add the object to the layer. Either at the end (if "sort" is false) or at the
  62. ** appropriately sorted position.
  63. */
  64. if (sort) {
  65. return(Sorted_Add(object));
  66. }
  67. return(Add((ObjectClass *)object));
  68. }
  69. /***********************************************************************************************
  70. * LayerClass::Sort -- Handles sorting the objects in the layer. *
  71. * *
  72. * This routine is used if the layer objects must be sorted and sorting is to occur now. *
  73. * *
  74. * INPUT: none *
  75. * *
  76. * OUTPUT: none *
  77. * *
  78. * WARNINGS: Don't call this routine too often since it does take a bit of time to *
  79. * execute. It is a single pass binary sort and thus isn't horribly slow, *
  80. * but it does take some time. *
  81. * *
  82. * HISTORY: *
  83. * 10/17/1994 JLB : Created. *
  84. * 03/10/1995 JLB : Uses comparison operator. *
  85. *=============================================================================================*/
  86. void LayerClass::Sort(void)
  87. {
  88. for (int index = 0; index < Count()-1; index++) {
  89. if (*(*this)[index+1] < *(*this)[index]) {
  90. ObjectClass * temp;
  91. temp = (*this)[index+1];
  92. (*this)[index+1] = (*this)[index];
  93. (*this)[index] = temp;
  94. }
  95. }
  96. }
  97. /***********************************************************************************************
  98. * DynamicVectorClass<T>::Sorted_Add -- Adds object in sorted order to vector. *
  99. * *
  100. * Use this routine to add an object to the vector but it will be inserted in sorted *
  101. * order. This depends on the ">" operator being defined for the vector object. *
  102. * *
  103. * INPUT: object -- Reference to the object that will be added to the vector. *
  104. * *
  105. * OUTPUT: bool; Was the object added to the vector successfully? *
  106. * *
  107. * WARNINGS: none *
  108. * *
  109. * HISTORY: *
  110. * 03/10/1995 JLB : Created. *
  111. *=============================================================================================*/
  112. int LayerClass::Sorted_Add(ObjectClass const * const object)
  113. {
  114. if (ActiveCount >= Length()) {
  115. if ((IsAllocated || !VectorMax) && GrowthStep > 0) {
  116. if (!Resize(Length() + GrowthStep)) {
  117. /*
  118. ** Failure to increase the size of the vector is an error condition.
  119. ** Return with the error flag.
  120. */
  121. return(false);
  122. }
  123. } else {
  124. /*
  125. ** Increasing the size of this vector is not allowed! Bail this
  126. ** routine with the error code.
  127. */
  128. return(false);
  129. }
  130. }
  131. /*
  132. ** There is room for the new object now. Add it to the right sorted position.
  133. */
  134. for (int index = 0; index < ActiveCount; index++) {
  135. if ((*(*this)[index]) > (*object)) {
  136. break;
  137. }
  138. }
  139. /*
  140. ** Make room if the insertion spot is not at the end of the vector.
  141. */
  142. for (int i = ActiveCount-1; i >= index; i--) {
  143. (*this)[i+1] = (*this)[i];
  144. }
  145. (*this)[index] = (ObjectClass *)object;
  146. ActiveCount++;
  147. return(true);
  148. }