TEMPLATE.CPP 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/TEMPLATE.CPP 1 3/03/97 10:25a 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 : TEMPLATE.CPP *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : May 17, 1994 *
  26. * *
  27. * Last Update : January 23, 1995 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * TemplateClass::Init -- Resets the template object system. *
  32. * TemplateClass::Mark -- Lifts or drops a template object. *
  33. * TemplateClass::Select -- Select the template object. *
  34. * TemplateClass::TemplateClass -- Template object constructor. *
  35. * TemplateClass::Unlimbo -- Places a template object into the game/map system. *
  36. * TemplateClass::delete -- Returns a template object to the pool. *
  37. * TemplateClass::new -- Allocates a template object from pool *
  38. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  39. #include "function.h"
  40. #include "template.h"
  41. /***********************************************************************************************
  42. * TemplateClass::Init -- Resets the template object system. *
  43. * *
  44. * This routine resets the template object system. It is called *
  45. * prior to loading a new scenario. *
  46. * *
  47. * INPUT: none *
  48. * *
  49. * OUTPUT: none *
  50. * *
  51. * WARNINGS: none *
  52. * *
  53. * HISTORY: *
  54. * 05/24/1994 JLB : Created. *
  55. *=============================================================================================*/
  56. void TemplateClass::Init(void)
  57. {
  58. Templates.Free_All();
  59. }
  60. /***********************************************************************************************
  61. * TemplateClass::Mark -- Lifts or drops a template object. *
  62. * *
  63. * This routine handles placing or removing a template object. This *
  64. * entails marking the map as appropriate and redisplaying affected *
  65. * cells. *
  66. * *
  67. * INPUT: mark -- The marking operation to perform. *
  68. * *
  69. * OUTPUT: bool; Was the template successfully marked? *
  70. * *
  71. * WARNINGS: none *
  72. * *
  73. * HISTORY: *
  74. * 05/17/1994 JLB : Created. *
  75. * 12/23/1994 JLB : Examines low level legality before processing. *
  76. *=============================================================================================*/
  77. bool TemplateClass::Mark(MarkType mark)
  78. {
  79. assert(Templates.ID(this) == ID);
  80. assert(IsActive);
  81. static bool noup = false;
  82. void const * iset = Get_Image_Data();
  83. if (iset && ObjectClass::Mark(mark)) {
  84. void * map = Get_Icon_Set_Map(iset);
  85. for (int y = 0; y < Class->Height; y++) {
  86. for (int x = 0; x < Class->Width; x++) {
  87. CELL cell = Coord_Cell(Coord) + y*MAP_CELL_W + x;
  88. if (Map.In_Radar(cell)) {
  89. CellClass * cellptr = &Map[cell];
  90. int number = y*Class->Width + x;
  91. /*
  92. ** Determine if this logical icon actually maps to a real icon. If no real
  93. ** icon is associated with this logical position, then don't do any action
  94. ** since none is required.
  95. */
  96. char * mapptr = (char*)map;
  97. bool real = (mapptr[number] != -1);
  98. if (real) {
  99. /*
  100. ** Lift the terrain object from the map.
  101. */
  102. if (mark == MARK_UP && !noup) {
  103. if (cellptr->TType == Class->Type && cellptr->TIcon == number) {
  104. cellptr->TType = TEMPLATE_NONE;
  105. cellptr->TIcon = 0;
  106. }
  107. }
  108. /*
  109. ** Place the terrain object down.
  110. */
  111. if (mark == MARK_DOWN) {
  112. if (*this == TEMPLATE_CLEAR1) {
  113. cellptr->TType = TEMPLATE_NONE;
  114. cellptr->TIcon = 0;
  115. } else {
  116. cellptr->TType = Class->Type;
  117. cellptr->TIcon = number;
  118. }
  119. /*
  120. ** Make sure that no overlays or smudges exist after
  121. ** placing the template down.
  122. */
  123. cellptr->Smudge = SMUDGE_NONE;
  124. cellptr->SmudgeData = 0;
  125. cellptr->Overlay = OVERLAY_NONE;
  126. cellptr->OverlayData = 0;
  127. }
  128. cellptr->Redraw_Objects();
  129. cellptr->Recalc_Attributes();
  130. }
  131. }
  132. }
  133. }
  134. /*
  135. ** When marking this template down onto the map, the map template numbers are update
  136. ** but the template is removed from existence. Make sure that the deletion of the
  137. ** template object doesn't also lift the template numbers up from the map.
  138. */
  139. if (mark == MARK_DOWN) {
  140. noup = true;
  141. delete this;
  142. noup = false;
  143. }
  144. return(true);
  145. }
  146. return(false);
  147. }
  148. /***********************************************************************************************
  149. * TemplateClass::new -- Allocates a template object from pool *
  150. * *
  151. * This routine is used to allocate a template object from the *
  152. * template object pool. *
  153. * *
  154. * INPUT: size -- The size of a template object (not used). *
  155. * *
  156. * OUTPUT: Returns with a pointer to an available template object. *
  157. * *
  158. * WARNINGS: none *
  159. * *
  160. * HISTORY: *
  161. * 05/17/1994 JLB : Created. *
  162. *=============================================================================================*/
  163. void * TemplateClass::operator new(size_t )
  164. {
  165. void * ptr = Templates.Allocate();
  166. if (ptr) {
  167. ((TemplateClass *)ptr)->IsActive = true;
  168. }
  169. return(ptr);
  170. }
  171. /***********************************************************************************************
  172. * TemplateClass::delete -- Returns a template object to the pool. *
  173. * *
  174. * This routine will return a template object to the template object *
  175. * pool. A template so returned is available for allocation again. *
  176. * *
  177. * INPUT: ptr -- Pointer to the object to be returned. *
  178. * *
  179. * OUTPUT: none *
  180. * *
  181. * WARNINGS: none *
  182. * *
  183. * HISTORY: *
  184. * 05/17/1994 JLB : Created. *
  185. *=============================================================================================*/
  186. void TemplateClass::operator delete(void * ptr)
  187. {
  188. if (ptr) {
  189. ((TemplateClass *)ptr)->IsActive = false;
  190. }
  191. Templates.Free((TemplateClass *)ptr);
  192. }
  193. /***********************************************************************************************
  194. * TemplateClass::TemplateClass -- Template object constructor. *
  195. * *
  196. * This is the constructor for a template object. *
  197. * *
  198. * INPUT: type -- The template object this is to become. *
  199. * *
  200. * pos -- The position on the map to place the object. *
  201. * *
  202. * OUTPUT: none *
  203. * *
  204. * WARNINGS: none *
  205. * *
  206. * HISTORY: *
  207. * 05/17/1994 JLB : Created. *
  208. *=============================================================================================*/
  209. TemplateClass::TemplateClass(TemplateType type, CELL pos) :
  210. ObjectClass(RTTI_TEMPLATE, Templates.ID(this)),
  211. Class(TemplateTypes.Ptr((int)type))
  212. {
  213. if (pos != -1) {
  214. Unlimbo(Cell_Coord(pos));
  215. }
  216. }