JSHELL.CPP 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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/JSHELL.CPP 2 3/13/97 2:05p Steve_tall $ */
  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 : JSHELL.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : April 2, 1994 *
  30. * *
  31. * Last Update : May 11, 1995 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * Build_Translucent_Table -- Creates a translucent control table. *
  36. * Conquer_Build_Translucent_Table -- Builds fading table for shadow colors only. *
  37. * Fatal -- General purpose fatal error handler. *
  38. * Load_Alloc_Data -- Allocates a buffer and loads the file into it. *
  39. * Load_Uncompress -- Loads and uncompresses data to a buffer. *
  40. * Set_Window -- Sets the window dimensions to that specified. *
  41. * Small_Icon -- Create a small icon from a big one. *
  42. * Translucent_Table_Size -- Determines the size of a translucent table. *
  43. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  44. #include "function.h"
  45. #include "wwfile.h"
  46. /***********************************************************************************************
  47. * Small_Icon -- Create a small icon from a big one. *
  48. * *
  49. * This routine will extract the specified icon from the icon data file and convert that *
  50. * icon into a small (3x3) representation. Typical use of this mini-icon is for the radar *
  51. * map. *
  52. * *
  53. * INPUT: iconptr -- Pointer to the icon data file. *
  54. * *
  55. * iconnum -- The embedded icon number to convert into a small image. *
  56. * *
  57. * OUTPUT: Returns with a pointer to the small icon imagery. This is exactly 9 bytes long. *
  58. * *
  59. * WARNINGS: none *
  60. * *
  61. * HISTORY: *
  62. * 05/11/1995 JLB : Created. *
  63. *=============================================================================================*/
  64. void * Small_Icon(void const * iconptr, int iconnum)
  65. {
  66. static unsigned char _icon[9];
  67. IControl_Type const * iptr = (IControl_Type const *)iconptr;
  68. unsigned char * data;
  69. if (iconptr) {
  70. iconnum = ((char *)((char *)iptr + iptr->Map))[iconnum];
  71. data = &((unsigned char *)((unsigned char *)iptr + iptr->Icons))[iconnum*(24*24)];
  72. // data = &iptr->Icons[iconnum*(24*24)];
  73. for (int index = 0; index < 9; index++) {
  74. int _offsets[9] = {
  75. 4+4*24,
  76. 12+4*24,
  77. 20+4*24,
  78. 4+12*24,
  79. 12+12*24,
  80. 20+12*24,
  81. 4+20*24,
  82. 12+20*24,
  83. 20+20*24
  84. };
  85. _icon[index] = data[_offsets[index]];
  86. }
  87. }
  88. return(_icon);
  89. }
  90. /***********************************************************************************************
  91. * Set_Window -- Sets the window dimensions to that specified. *
  92. * *
  93. * Use this routine to set the windows dimensions to the coordinates and dimensions *
  94. * specified. *
  95. * *
  96. * INPUT: x -- Window X pixel position. *
  97. * *
  98. * y -- Window Y pixel position. *
  99. * *
  100. * w -- Window width in pixels. *
  101. * *
  102. * h -- Window height in pixels. *
  103. * *
  104. * OUTPUT: none *
  105. * *
  106. * WARNINGS: The X and width values are truncated to an even 8 pixel boundary. This is *
  107. * the same as stripping off the lower 3 bits. *
  108. * *
  109. * HISTORY: *
  110. * 01/15/1995 JLB : Created. *
  111. *=============================================================================================*/
  112. void Set_Window(int window, int x, int y, int w, int h)
  113. {
  114. WindowList[window][WINDOWWIDTH] = w;
  115. WindowList[window][WINDOWHEIGHT] = h;
  116. WindowList[window][WINDOWX] = x;
  117. WindowList[window][WINDOWY] = y;
  118. }
  119. /***********************************************************************************************
  120. * Fatal -- General purpose fatal error handler. *
  121. * *
  122. * This is a very simple general purpose fatal error handler. It goes directly to text *
  123. * mode, prints the error, and then aborts with a failure code. *
  124. * *
  125. * INPUT: message -- The text message to display. *
  126. * *
  127. * ... -- Any optional parameters that are used in formatting the message. *
  128. * *
  129. * OUTPUT: none *
  130. * *
  131. * WARNINGS: This routine never returns. The game exits immediately. *
  132. * *
  133. * HISTORY: *
  134. * 10/17/1994 JLB : Created. *
  135. *=============================================================================================*/
  136. void Fatal(char const * message, ...)
  137. {
  138. va_list va;
  139. va_start(va, message);
  140. //Prog_End();
  141. vfprintf(stderr, message, va);
  142. Mono_Printf(message);
  143. Emergency_Exit(EXIT_FAILURE);
  144. }
  145. #ifdef NEVER
  146. void File_Fatal(char const * message)
  147. {
  148. //Prog_End();
  149. perror(message);
  150. Emergency_Exit(EXIT_FAILURE);
  151. }
  152. #endif
  153. /***********************************************************************************************
  154. * Load_Uncompress -- Loads and uncompresses data to a buffer. *
  155. * *
  156. * This is the C++ counterpart to the Load_Uncompress function. It will load the file *
  157. * specified into the graphic buffer indicated and uncompress it. *
  158. * *
  159. * INPUT: file -- The file to load and uncompress. *
  160. * *
  161. * uncomp_buff -- The graphic buffer that initial loading will use. *
  162. * *
  163. * dest_buff -- The buffer that will hold the uncompressed data. *
  164. * *
  165. * reserved_data -- This is an optional pointer to a buffer that will hold any *
  166. * reserved data the compressed file may contain. This is *
  167. * typically a palette. *
  168. * *
  169. * OUTPUT: Returns with the size of the uncompressed data in the destination buffer. *
  170. * *
  171. * WARNINGS: none *
  172. * *
  173. * HISTORY: *
  174. * 10/17/1994 JLB : Created. *
  175. *=============================================================================================*/
  176. long Load_Uncompress(FileClass &file, BuffType &uncomp_buff, BuffType &dest_buff, void * reserved_data)
  177. {
  178. unsigned short size;
  179. void * sptr = uncomp_buff.Get_Buffer();
  180. void * dptr = dest_buff.Get_Buffer();
  181. int opened = false;
  182. CompHeaderType header;
  183. /*
  184. ** The file must be opened in order to be read from. If the file
  185. ** isn't opened, then open it. Record this fact so that it can be
  186. ** restored to its closed state at the end.
  187. */
  188. if (!file.Is_Open()) {
  189. if (!file.Open()) {
  190. return(0);
  191. }
  192. opened = true;
  193. }
  194. /*
  195. ** Read in the size of the file (supposedly).
  196. */
  197. file.Read(&size, sizeof(size));
  198. /*
  199. ** Read in the header block. This block contains the compression type
  200. ** and skip data (among other things).
  201. */
  202. file.Read(&header, sizeof(header));
  203. size -= sizeof(header);
  204. /*
  205. ** If there are skip bytes then they must be processed. Either read
  206. ** them into the buffer provided or skip past them. No check is made
  207. ** to ensure that the reserved data buffer is big enough (watch out!).
  208. */
  209. if (header.Skip) {
  210. size -= header.Skip;
  211. if (reserved_data) {
  212. file.Read(reserved_data, header.Skip);
  213. } else {
  214. file.Seek(header.Skip, SEEK_CUR);
  215. }
  216. header.Skip = 0;
  217. }
  218. /*
  219. ** Determine where is the proper place to load the data. If both buffers
  220. ** specified are identical, then the data should be loaded at the end of
  221. ** the buffer and decompressed at the beginning.
  222. */
  223. if (uncomp_buff.Get_Buffer() == dest_buff.Get_Buffer()) {
  224. sptr = (char *)sptr + uncomp_buff.Get_Size()-(size+sizeof(header));
  225. }
  226. /*
  227. ** Read in the bulk of the data.
  228. */
  229. Mem_Copy(&header, sptr, sizeof(header));
  230. file.Read((char *)sptr + sizeof(header), size);
  231. /*
  232. ** Decompress the data.
  233. */
  234. size = (unsigned int) Uncompress_Data(sptr, dptr);
  235. /*
  236. ** Close the file if necessary.
  237. */
  238. if (opened) {
  239. file.Close();
  240. }
  241. return((long)size);
  242. }
  243. int Load_Picture(char const * filename, BufferClass& scratchbuf, BufferClass& destbuf, unsigned char * palette, PicturePlaneType )
  244. {
  245. return(Load_Uncompress(CCFileClass(filename), scratchbuf, destbuf, palette ) / 8000);
  246. }
  247. /***********************************************************************************************
  248. * Load_Alloc_Data -- Allocates a buffer and loads the file into it. *
  249. * *
  250. * This is the C++ replacement for the Load_Alloc_Data function. It will allocate the *
  251. * memory big enough to hold the file and then read the file into it. *
  252. * *
  253. * INPUT: file -- The file to read. *
  254. * *
  255. * mem -- The memory system to use for allocation. *
  256. * *
  257. * OUTPUT: Returns with a pointer to the allocated and filled memory block. *
  258. * *
  259. * WARNINGS: none *
  260. * *
  261. * HISTORY: *
  262. * 10/17/1994 JLB : Created. *
  263. *=============================================================================================*/
  264. void * Load_Alloc_Data(FileClass &file)
  265. {
  266. void * ptr = 0;
  267. long size = file.Size();
  268. ptr = new char [size];
  269. if (ptr) {
  270. file.Read(ptr, size);
  271. }
  272. return(ptr);
  273. }
  274. /***********************************************************************************************
  275. * Translucent_Table_Size -- Determines the size of a translucent table. *
  276. * *
  277. * Use this routine to determine how big the translucent table needs *
  278. * to be given the specified number of colors. This value is typically *
  279. * used when allocating the buffer for the translucent table. *
  280. * *
  281. * INPUT: count -- The number of colors that are translucent. *
  282. * *
  283. * OUTPUT: Returns the size of the translucent table. *
  284. * *
  285. * WARNINGS: none *
  286. * *
  287. * HISTORY: *
  288. * 04/02/1994 JLB : Created. *
  289. *=============================================================================================*/
  290. long Translucent_Table_Size(int count)
  291. {
  292. return(256L + (256L * count));
  293. }
  294. /***********************************************************************************************
  295. * Build_Translucent_Table -- Creates a translucent control table. *
  296. * *
  297. * The table created by this routine is used by Draw_Shape (GHOST) to *
  298. * achieve a translucent affect. The original color of the shape will *
  299. * show through. This differs from the fading effect, since that *
  300. * affect only alters the background color toward a single destination *
  301. * color. *
  302. * *
  303. * INPUT: palette -- Pointer to the control palette. *
  304. * *
  305. * control -- Pointer to array of structures that control how *
  306. * the translucent table will be built. *
  307. * *
  308. * count -- The number of entries in the control array. *
  309. * *
  310. * buffer -- Pointer to buffer to place the translucent table. *
  311. * If NULL is passed in, then the buffer will be *
  312. * allocated. *
  313. * *
  314. * OUTPUT: Returns with pointer to the translucent table. *
  315. * *
  316. * WARNINGS: This routine is exceedingly slow. Use sparingly. *
  317. * *
  318. * HISTORY: *
  319. * 04/02/1994 JLB : Created. *
  320. *=============================================================================================*/
  321. void * Build_Translucent_Table(PaletteClass const & palette, TLucentType const * control, int count, void * buffer)
  322. {
  323. unsigned char const *table; // Remap table pointer.
  324. int index; // Working color index.
  325. if (count && control && palette) {
  326. if (!buffer) {
  327. buffer = new char [Translucent_Table_Size(count)];
  328. }
  329. if (buffer) {
  330. memset(buffer, -1, 256);
  331. table = (unsigned char*)buffer + 256;
  332. /*
  333. ** Build the individual remap tables for each translucent color.
  334. */
  335. for (index = 0; index < count; index++) {
  336. ((unsigned char*)buffer)[control[index].SourceColor] = index;
  337. Build_Fading_Table(palette, (void*)table, control[index].DestColor, control[index].Fading);
  338. table = (unsigned char*)table + 256;
  339. }
  340. }
  341. }
  342. return(buffer);
  343. }
  344. /***********************************************************************************************
  345. * Conquer_Build_Translucent_Table -- Builds fading table for shadow colors only. *
  346. * *
  347. * This routine will build a translucent (fading) table to remap colors into the shadow *
  348. * color region of the palette. Shadow colors are not affected by this translucent table. *
  349. * This means that a shape can be overlapped any number of times and the imagery will *
  350. * remain deterministic (and constant). *
  351. * *
  352. * INPUT: palette -- Pointer to the palette to base the translucent process on. *
  353. * *
  354. * control -- Pointer to special control structure that specifies the *
  355. * target color, and percentage of fade. *
  356. * *
  357. * count -- The number of colors to be remapped (entries in the control array). *
  358. * *
  359. * buffer -- Pointer to the staging buffer that will hold the translucent table *
  360. * data. If this parameter is NULL, then an appropriate sized table *
  361. * will be allocated. *
  362. * *
  363. * OUTPUT: Returns with a pointer to the translucent table data. *
  364. * *
  365. * WARNINGS: none *
  366. * *
  367. * HISTORY: *
  368. * 06/27/1994 JLB : Created. *
  369. *=============================================================================================*/
  370. void * Conquer_Build_Translucent_Table(PaletteClass const & palette, TLucentType const * control, int count, void * buffer)
  371. {
  372. unsigned char const *table; // Remap table pointer.
  373. if (count && control) {
  374. if (!buffer) {
  375. buffer = new char [Translucent_Table_Size(count)];
  376. }
  377. if (buffer) {
  378. memset(buffer, -1, 256);
  379. table = (unsigned char*)buffer + 256;
  380. /*
  381. ** Build the individual remap tables for each translucent color.
  382. */
  383. for (int index = 0; index < count; index++) {
  384. ((unsigned char*)buffer)[control[index].SourceColor] = index;
  385. Conquer_Build_Fading_Table(palette, (void*)table, control[index].DestColor, control[index].Fading);
  386. table = (unsigned char*)table + 256;
  387. }
  388. }
  389. }
  390. return(buffer);
  391. }
  392. void * Make_Fading_Table(PaletteClass const & palette, void * dest, int color, int frac)
  393. {
  394. if (dest) {
  395. unsigned char * ptr = (unsigned char *)dest;
  396. /*
  397. ** Find an appropriate remap color index for every color in the palette.
  398. ** There are certain exceptions to this, but they are trapped within the
  399. ** loop.
  400. */
  401. for (int index = 0; index < PaletteClass::COLOR_COUNT; index++) {
  402. /*
  403. ** Find the color that, ideally, the working color should be remapped
  404. ** to in the special remap range.
  405. */
  406. RGBClass trycolor = palette[index];
  407. trycolor.Adjust(frac, palette[color]); // Try to match this color.
  408. /*
  409. ** Search through the remap range to find the color that should be remapped
  410. ** to. This special range is used for shadows or other effects that are
  411. ** not compounded if additively applied.
  412. */
  413. *ptr++ = palette.Closest_Color(trycolor);
  414. }
  415. }
  416. return(dest);
  417. }
  418. void * Conquer_Build_Fading_Table(PaletteClass const & palette, void * dest, int color, int frac)
  419. {
  420. if (dest) {
  421. unsigned char * ptr = (unsigned char *)dest;
  422. // HSVClass desthsv = palette[color];
  423. /*
  424. ** Find an appropriate remap color index for every color in the palette.
  425. ** There are certain exceptions to this, but they are trapped within the
  426. ** loop.
  427. */
  428. for (int index = 0; index < PaletteClass::COLOR_COUNT; index++) {
  429. /*
  430. ** If this color should not be remapped, then it will be stored as a remap
  431. ** to itself. This is effectively no remap.
  432. */
  433. if (index > PaletteClass::COLOR_COUNT-16 || index == 0) {
  434. *ptr++ = index;
  435. } else {
  436. /*
  437. ** Find the color that, ideally, the working color should be remapped
  438. ** to in the special remap range.
  439. */
  440. RGBClass trycolor = palette[index];
  441. trycolor.Adjust(frac, palette[color]); // Try to match this color.
  442. /*
  443. ** Search through the remap range to find the color that should be remapped
  444. ** to. This special range is used for shadows or other effects that are
  445. ** not compounded if additively applied.
  446. */
  447. int best = -1;
  448. int bvalue = 0;
  449. for (int id = PaletteClass::COLOR_COUNT-16; id < PaletteClass::COLOR_COUNT-1; id++) {
  450. int diff = palette[id].Difference(trycolor);
  451. if (best == -1 || diff < bvalue) {
  452. best = id;
  453. bvalue = diff;
  454. }
  455. }
  456. *ptr++ = best;
  457. }
  458. }
  459. }
  460. return(dest);
  461. }
  462. #ifdef OBSOLETE
  463. //int Desired_Facing8(int x1, int y1, int x2, int y2)
  464. DirType xDesired_Facing8(int x1, int y1, int x2, int y2)
  465. {
  466. int index = 0; // Facing composite value.
  467. /*
  468. ** Figure the absolute X difference. This determines
  469. ** if the facing is leftward or not.
  470. */
  471. int xdiff = x2-x1;
  472. if (xdiff < 0) {
  473. index |= 0x00C0;
  474. xdiff = -xdiff;
  475. }
  476. /*
  477. ** Figure the absolute Y difference. This determines
  478. ** if the facing is downward or not. This also clarifies
  479. ** exactly which quadrant the facing lies.
  480. */
  481. int ydiff = y1-y2;
  482. if (ydiff < 0) {
  483. index ^= 0x0040;
  484. ydiff = -ydiff;
  485. }
  486. /*
  487. ** Determine which of the two direction offsets it bigger. The
  488. ** offset direction that is bigger (X or Y) will indicate which
  489. ** orthogonal direction the facing is closer to.
  490. */
  491. unsigned bigger;
  492. unsigned smaller;
  493. if (xdiff < ydiff) {
  494. smaller = xdiff;
  495. bigger = ydiff;
  496. } else {
  497. smaller = ydiff;
  498. bigger = xdiff;
  499. }
  500. /*
  501. ** If on the diagonal, then incorporate this into the facing
  502. ** and then bail. The facing is known.
  503. */
  504. if (((bigger+1)/2) <= smaller) {
  505. index += 0x0020;
  506. return(DirType(index));
  507. }
  508. /*
  509. ** Determine if the facing is closer to the Y axis or
  510. ** the X axis.
  511. */
  512. int adder = (index & 0x0040);
  513. if (xdiff == bigger) {
  514. adder ^= 0x0040;
  515. }
  516. index += adder;
  517. return(DirType(index));
  518. }
  519. //int Desired_Facing256(int srcx, int srcy, int dstx, int dsty)
  520. DirType xDesired_Facing256(int srcx, int srcy, int dstx, int dsty)
  521. {
  522. int composite=0; // Facing built from intermediate calculations.
  523. /*
  524. ** Fetch the absolute X difference. This also gives a clue as
  525. ** to which hemisphere the direction lies.
  526. */
  527. int xdiff = dstx - srcx;
  528. if (xdiff < 0) {
  529. composite |= 0x00C0;
  530. xdiff = -xdiff;
  531. }
  532. /*
  533. ** Fetch the absolute Y difference. This clarifies the exact
  534. ** quadrant that the direction lies.
  535. */
  536. int ydiff = srcy - dsty;
  537. if (ydiff < 0) {
  538. composite ^= 0x0040;
  539. ydiff = -ydiff;
  540. }
  541. /*
  542. ** Bail early if the coordinates are the same. This check also
  543. ** has the added bonus of ensuring that checking for division
  544. ** by zero is not needed in the following section.
  545. */
  546. if (xdiff == 0 && ydiff == 0) return(DirType(0xFF));
  547. /*
  548. ** Determine which of the two direction offsets it bigger. The
  549. ** offset direction that is bigger (X or Y) will indicate which
  550. ** orthogonal direction the facing is closer to.
  551. */
  552. unsigned bigger;
  553. unsigned smaller;
  554. if (xdiff < ydiff) {
  555. smaller = xdiff;
  556. bigger = ydiff;
  557. } else {
  558. smaller = ydiff;
  559. bigger = xdiff;
  560. }
  561. /*
  562. ** Now that the quadrant is known, we need to determine how far
  563. ** from the orthogonal directions, the facing lies. This value
  564. ** is calculated as a ratio from 0 (matches orthogonal) to 31
  565. ** (matches diagonal).
  566. */
  567. int frac = (smaller * 32U) / bigger;
  568. /*
  569. ** Given the quadrant and knowing whether the facing is closer
  570. ** to the X or Y axis, we must make an adjustment toward the
  571. ** subsequent quadrant if necessary.
  572. */
  573. int adder = (composite & 0x0040);
  574. if (xdiff > ydiff) {
  575. adder ^= 0x0040;
  576. }
  577. if (adder) {
  578. frac = (adder - frac)-1;
  579. }
  580. /*
  581. ** Integrate the fraction value into the quadrant.
  582. */
  583. composite += frac;
  584. /*
  585. ** Return with the final facing value.
  586. */
  587. return(DirType(composite & 0x00FF));
  588. }
  589. #endif