TEMPLATE.CPP 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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\template.cpv 2.18 16 Oct 1995 16:51:46 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 : TEMPLATE.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : May 17, 1994 *
  30. * *
  31. * Last Update : January 23, 1995 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * TemplateClass::As_Target -- Converts a template object into a target number. *
  36. * TemplateClass::Init -- Resets the template object system. *
  37. * TemplateClass::Mark -- Lifts or drops a template object. *
  38. * TemplateClass::Read_INI -- Reads the scenario control INI file. *
  39. * TemplateClass::Select -- Select the template object. *
  40. * TemplateClass::TemplateClass -- Default constructor for template class objects. *
  41. * TemplateClass::TemplateClass -- Template object constructor. *
  42. * TemplateClass::Unlimbo -- Places a template object into the game/map system. *
  43. * TemplateClass::Write_INI -- Writes the template objects to the INI file. *
  44. * TemplateClass::delete -- Returns a template object to the pool. *
  45. * TemplateClass::new -- Allocates a template object from pool *
  46. * TemplateClass::Validate -- validates template pointer *
  47. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  48. #include "function.h"
  49. #include "template.h"
  50. /*
  51. ** This contains the value of the Virtual Function Table Pointer
  52. */
  53. void * TemplateClass::VTable;
  54. /***********************************************************************************************
  55. * TemplateClass::Validate -- validates template pointer *
  56. * *
  57. * INPUT: *
  58. * none. *
  59. * *
  60. * OUTPUT: *
  61. * 1 = ok, 0 = error *
  62. * *
  63. * WARNINGS: *
  64. * none. *
  65. * *
  66. * HISTORY: *
  67. * 08/09/1995 BRR : Created. *
  68. *=============================================================================================*/
  69. #ifdef CHEAT_KEYS
  70. int TemplateClass::Validate(void) const
  71. {
  72. int num;
  73. num = Templates.ID(this);
  74. if (num < 0 || num >= TEMPLATE_MAX) {
  75. Validate_Error("TEMPLATE");
  76. return (0);
  77. }
  78. else
  79. return (1);
  80. }
  81. #else
  82. #define Validate()
  83. #endif
  84. /***********************************************************************************************
  85. * TemplateClass::Read_INI -- Reads the scenario control INI file. *
  86. * *
  87. * This routine reads the scenario control INI file and creates all *
  88. * templates specified therein. *
  89. * *
  90. * INPUT: buffer -- Pointer to the loaded scenario control INI file. *
  91. * *
  92. * OUTPUT: none *
  93. * *
  94. * WARNINGS: none *
  95. * *
  96. * HISTORY: *
  97. * 05/24/1994 JLB : Created. *
  98. *=============================================================================================*/
  99. void TemplateClass::Read_INI(char *buffer)
  100. {
  101. char *tbuffer; // Accumulation buffer of unit IDs.
  102. int len; // Size of data in buffer.
  103. CELL cell; // Cell of building.
  104. char buf[128]; // Working string staging buffer.
  105. len = strlen(buffer) + 2;
  106. tbuffer = buffer + len;
  107. WWGetPrivateProfileString(INI_Name(), NULL, NULL, tbuffer, ShapeBufferSize-len, buffer);
  108. while (*tbuffer != '\0') {
  109. TemplateType temp; // Terrain type.
  110. cell = atoi(tbuffer);
  111. WWGetPrivateProfileString(INI_Name(), tbuffer, NULL, buf, sizeof(buf)-1, buffer);
  112. temp = TemplateTypeClass::From_Name(strtok(buf, ",\r\n"));
  113. if (temp != TEMPLATE_NONE) {
  114. new TemplateClass(temp, cell);
  115. }
  116. tbuffer += strlen(tbuffer)+1;
  117. }
  118. }
  119. /***********************************************************************************************
  120. * TemplateClass::Write_INI -- Writes the template objects to the INI file. *
  121. * *
  122. * This routine is used to write all the template objects out to the INI file specified. *
  123. * It is used by the scenario editor when saving the game. *
  124. * *
  125. * INPUT: buffer -- Pointer to the INI file staging buffer. *
  126. * *
  127. * OUTPUT: none *
  128. * *
  129. * WARNINGS: none *
  130. * *
  131. * HISTORY: *
  132. * 05/28/1994 JLB : Created. *
  133. *=============================================================================================*/
  134. void TemplateClass::Write_INI(char *buffer)
  135. {
  136. char uname[10];
  137. char buf[127];
  138. char *tbuffer; // Accumulation buffer of unit IDs.
  139. /*
  140. ** First, clear out all existing template data from the ini file.
  141. */
  142. tbuffer = buffer + strlen(buffer) + 2;
  143. WWGetPrivateProfileString(INI_Name(), NULL, NULL, tbuffer, ShapeBufferSize-strlen(buffer), buffer);
  144. while (*tbuffer != '\0') {
  145. WWWritePrivateProfileString(INI_Name(), tbuffer, NULL, buffer);
  146. tbuffer += strlen(tbuffer)+1;
  147. }
  148. /*
  149. ** Find all templates and write them to the file.
  150. */
  151. for (int index = 0; index < MAP_CELL_TOTAL; index++) {
  152. CellClass * ptr;
  153. ptr = &Map[index];
  154. if (ptr->TType != TEMPLATE_NONE && ptr->TIcon == 0) {
  155. sprintf(uname, "%03d", index);
  156. sprintf(buf, "%s", TemplateTypeClass::As_Reference(ptr->TType).IniName);
  157. WWWritePrivateProfileString(INI_Name(), uname, buf, buffer);
  158. }
  159. }
  160. }
  161. /***********************************************************************************************
  162. * TemplateClass::TemplateClass -- Default constructor for template class objects. *
  163. * *
  164. * This is the default constructor for a template class object. This construction method *
  165. * should NEVER be used by the game except as a consequence of declaring an array of *
  166. * uninitialized template objects. *
  167. * *
  168. * INPUT: none *
  169. * *
  170. * OUTPUT: none *
  171. * *
  172. * WARNINGS: none *
  173. * *
  174. * HISTORY: *
  175. * 01/23/1995 JLB : Created. *
  176. *=============================================================================================*/
  177. TemplateClass::TemplateClass(void) :
  178. Class(0)
  179. {
  180. }
  181. /***********************************************************************************************
  182. * TemplateClass::As_Target -- Converts a template object into a target number. *
  183. * *
  184. * This routine will convert a template object into a target number. Because templates *
  185. * never exist as a template object in the game system, this routine will never be called. *
  186. * *
  187. * INPUT: *
  188. * *
  189. * OUTPUT: *
  190. * *
  191. * WARNINGS: *
  192. * *
  193. * HISTORY: *
  194. * 09/25/1994 JLB : Created. *
  195. *=============================================================================================*/
  196. TARGET TemplateClass::As_Target(void) const
  197. {
  198. Validate();
  199. return(Build_Target(KIND_TEMPLATE, Templates.ID(this)));
  200. }
  201. /***********************************************************************************************
  202. * TemplateClass::Init -- Resets the template object system. *
  203. * *
  204. * This routine resets the template object system. It is called *
  205. * prior to loading a new scenario. *
  206. * *
  207. * INPUT: none *
  208. * *
  209. * OUTPUT: none *
  210. * *
  211. * WARNINGS: none *
  212. * *
  213. * HISTORY: *
  214. * 05/24/1994 JLB : Created. *
  215. *=============================================================================================*/
  216. void TemplateClass::Init(void)
  217. {
  218. TemplateClass *ptr;
  219. Templates.Free_All();
  220. ptr = new TemplateClass();
  221. VTable = ((void **)(((char *)ptr) + sizeof(AbstractClass) - 4))[0];
  222. delete ptr;
  223. }
  224. /***********************************************************************************************
  225. * TemplateClass::Mark -- Lifts or drops a template object. *
  226. * *
  227. * This routine handles placing or removing a template object. This *
  228. * entails marking the map as appropriate and redisplaying affected *
  229. * cells. *
  230. * *
  231. * INPUT: mark -- The marking operation to perform. *
  232. * *
  233. * OUTPUT: bool; Was the template successfully marked? *
  234. * *
  235. * WARNINGS: none *
  236. * *
  237. * HISTORY: *
  238. * 05/17/1994 JLB : Created. *
  239. * 12/23/1994 JLB : Examines low level legality before processing. *
  240. *=============================================================================================*/
  241. bool TemplateClass::Mark(MarkType mark)
  242. {
  243. Validate();
  244. static bool noup = false;
  245. void const * iset = Class->Get_Image_Data();
  246. if (iset && ObjectClass::Mark(mark)) {
  247. void * map = Get_Icon_Set_Map(iset);
  248. for (int y = 0; y < Class->Height; y++) {
  249. for (int x = 0; x < Class->Width; x++) {
  250. CELL cell = Coord_Cell(Coord) + y*MAP_CELL_W + x;
  251. if (Map.In_Radar(cell)) {
  252. CellClass * cellptr = &Map[cell];
  253. int number = y*Class->Width + x;
  254. /*
  255. ** Determine if this logical icon actually maps to a real icon. If no real
  256. ** icon is associated with this logical position, then don't do any action
  257. ** since none is required.
  258. */
  259. char * mapptr = (char*)map;
  260. bool real = (mapptr[number] != -1);
  261. if (real) {
  262. /*
  263. ** Lift the terrain object from the map.
  264. */
  265. if (mark == MARK_UP && !noup) {
  266. if (cellptr->TType == Class->Type && cellptr->TIcon == number) {
  267. cellptr->TType = TEMPLATE_NONE;
  268. cellptr->TIcon = 0;
  269. }
  270. }
  271. /*
  272. ** Place the terrain object down.
  273. */
  274. if (mark == MARK_DOWN) {
  275. if (*this == TEMPLATE_CLEAR1) {
  276. cellptr->TType = TEMPLATE_NONE;
  277. cellptr->TIcon = 0;
  278. } else {
  279. cellptr->TType = Class->Type;
  280. // cellptr->TIcon = real;
  281. cellptr->TIcon = number;
  282. }
  283. }
  284. cellptr->Redraw_Objects();
  285. cellptr->Recalc_Attributes();
  286. }
  287. }
  288. }
  289. }
  290. /*
  291. ** When marking this template down onto the map, the map template numbers are update
  292. ** but the template is removed from existence. Make sure that the deletion of the
  293. ** template object doesn't also lift the template numbers up from the map.
  294. */
  295. if (mark == MARK_DOWN) {
  296. noup = true;
  297. delete this;
  298. noup = false;
  299. }
  300. return(true);
  301. }
  302. return(false);
  303. }
  304. /***********************************************************************************************
  305. * TemplateClass::new -- Allocates a template object from pool *
  306. * *
  307. * This routine is used to allocate a template object from the *
  308. * template object pool. *
  309. * *
  310. * INPUT: size -- The size of a template object (not used). *
  311. * *
  312. * OUTPUT: Returns with a pointer to an available template object. *
  313. * *
  314. * WARNINGS: none *
  315. * *
  316. * HISTORY: *
  317. * 05/17/1994 JLB : Created. *
  318. *=============================================================================================*/
  319. void * TemplateClass::operator new(size_t )
  320. {
  321. void * ptr = Templates.Allocate();
  322. if (ptr) {
  323. ((TemplateClass *)ptr)->IsActive = true;
  324. }
  325. return(ptr);
  326. }
  327. /***********************************************************************************************
  328. * TemplateClass::delete -- Returns a template object to the pool. *
  329. * *
  330. * This routine will return a template object to the template object *
  331. * pool. A template so returned is available for allocation again. *
  332. * *
  333. * INPUT: ptr -- Pointer to the object to be returned. *
  334. * *
  335. * OUTPUT: none *
  336. * *
  337. * WARNINGS: none *
  338. * *
  339. * HISTORY: *
  340. * 05/17/1994 JLB : Created. *
  341. *=============================================================================================*/
  342. void TemplateClass::operator delete(void *ptr)
  343. {
  344. if (ptr) {
  345. ((TemplateClass *)ptr)->IsActive = false;
  346. }
  347. Templates.Free((TemplateClass *)ptr);
  348. }
  349. /***********************************************************************************************
  350. * TemplateClass::TemplateClass -- Template object constructor. *
  351. * *
  352. * This is the constructor for a template object. *
  353. * *
  354. * INPUT: type -- The template object this is to become. *
  355. * *
  356. * pos -- The position on the map to place the object. *
  357. * *
  358. * OUTPUT: none *
  359. * *
  360. * WARNINGS: none *
  361. * *
  362. * HISTORY: *
  363. * 05/17/1994 JLB : Created. *
  364. *=============================================================================================*/
  365. TemplateClass::TemplateClass(TemplateType type, CELL pos) :
  366. Class(&TemplateTypeClass::As_Reference(type))
  367. {
  368. if (pos != -1) {
  369. Unlimbo(Cell_Coord(pos));
  370. }
  371. }