JSHELL.CPP 29 KB

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