Subversion Repositories Games.Carmageddon

Rev

Rev 18 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include "utility.h"
  2. #include <stdlib.h>
  3.  
  4. #include "brender/brender.h"
  5. #include "constants.h"
  6. #include "errors.h"
  7. #include "globvars.h"
  8. #include "globvrpb.h"
  9. #include "graphics.h"
  10. #include "harness/config.h"
  11. #include "harness/trace.h"
  12. #include "input.h"
  13. #include "loading.h"
  14. #include "loadsave.h"
  15. #include "mainmenu.h"
  16. #include "network.h"
  17. #include "pd/sys.h"
  18. #include "sound.h"
  19. #include "world.h"
  20.  
  21. #include <ctype.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24.  
  25. // Added >>
  26. #define MIN_SERVICE_INTERVAL 200
  27. // <<
  28.  
  29. int gIn_check_quit = 0;
  30. tU32 gLost_time = 0;
  31. //#if BR_ENDIAN_BIG
  32. //tU32 gLong_key[4] = { 0x6c1b995f, 0xb9cd5f13, 0xcb04200e, 0x5e1ca10e };
  33. char gLong_key[16]       = { 0x6c, 0x1b, 0x99, 0x5f, 0xb9, 0xcd, 0x5f, 0x13, 0xcb, 0x04, 0x20, 0x0e, 0x5e, 0x1c, 0xa1, 0x0e }; // Pierre-Marie Baty -- have an endianness-independent pattern
  34. //tU32 gOther_long_key[4] = { 0x67a8d626, 0xb6dd451b, 0x327e2213, 0x15c29437 };
  35. char gOther_long_key[16] = { 0x67, 0xa8, 0xd6, 0x26, 0xb6, 0xdd, 0x45, 0x1b, 0x32, 0x7e, 0x22, 0x13, 0x15, 0xc2, 0x94, 0x37 }; // Pierre-Marie Baty -- have an endianness-independent pattern
  36. //#else
  37. //tU32 gLong_key[4] = { 0x5f991b6c, 0x135fcdb9, 0x0e2004cb, 0x0ea11c5e };
  38. //tU32 gOther_long_key[4] = { 0x26d6a867, 0x1b45ddb6, 0x13227e32, 0x3794c215 };
  39. //#endif
  40. int gEncryption_method = 0;
  41. char* gMisc_strings[250];
  42. br_pixelmap* g16bit_palette;
  43. br_pixelmap* gSource_for_16bit_palette;
  44.  
  45. // IDA: int __cdecl CheckQuit()
  46. int CheckQuit(void) {
  47.     LOG_TRACE8("()");
  48.  
  49.     if (!gIn_check_quit && KeyIsDown(KEYMAP_CTRL_QUIT) && KeyIsDown(KEYMAP_CONTROL_ANY)) {
  50.         gIn_check_quit = 1;
  51.         while (AnyKeyDown()) {
  52.             ;
  53.         }
  54.  
  55.         if (DoVerifyQuit(1)) {
  56.             DoSaveGame(1);
  57.         }
  58.         gIn_check_quit = 0;
  59.     }
  60.     return 0;
  61. }
  62.  
  63. // IDA: double __cdecl sqr(double pN)
  64. double sqr(double pN) {
  65.  
  66.     return pN * pN;
  67. }
  68.  
  69. // Added to handle demo-specific text file decryption behavior
  70. void EncodeLine_DEMO(char* pS) {
  71.     int len;
  72.     int seed;
  73.     int i;
  74.     char* key;
  75.     unsigned char c;
  76.     //FILE* test; // Pierre-Marie Baty -- unused variable
  77.     //tPath_name the_path; // Pierre-Marie Baty -- unused variable
  78. //#if BR_ENDIAN_BIG
  79. //    const tU32 gLong_key_DEMO[] = { 0x58503A76, 0xCBB68565, 0x15CD5B07, 0xB168DE3A };
  80.     const char gLong_key_DEMO[16] = { 0x58, 0x50, 0x3A, 0x76, 0xCB, 0xB6, 0x85, 0x65, 0x15, 0xCD, 0x5B, 0x07, 0xB1, 0x68, 0xDE, 0x3A }; // Pierre-Marie Baty -- have an endianness-independent pattern
  81. //#else
  82. //    const tU32 gLong_key_DEMO[] = { 0x763A5058, 0x6585B6CB, 0x75BCD15, 0x3ADE68B1 };
  83. //#endif
  84.  
  85.     len = strlen(pS);
  86.     key = (char*)gLong_key_DEMO;
  87.  
  88.     while (len > 0 && (pS[len - 1] == '\r' || pS[len - 1] == '\n')) {
  89.         len--;
  90.         pS[len] = 0;
  91.     }
  92.     seed = len % 16;
  93.     for (i = 0; i < len; i++) {
  94.         c = pS[i];
  95.         if (c == '\t') {
  96.             c = 0x9F;
  97.         }
  98.         c = ((key[seed] ^ (c - 32)) & 0x7F) + 32;
  99.         seed = (seed + 7) % 16;
  100.         if (c == 0x9F) {
  101.             c = '\t';
  102.         }
  103.         pS[i] = c;
  104.     }
  105. }
  106.  
  107. // IDA: void __usercall EncodeLine(char *pS@<EAX>)
  108. void EncodeLine(char* pS) {
  109.     int len;
  110.     int seed;
  111.     int i;
  112.     char* key;
  113.     unsigned char c;
  114.     //FILE* test; // Pierre-Marie Baty -- unused variable
  115.     //tPath_name the_path; // Pierre-Marie Baty -- unused variable
  116.     //char s[256]; // Pierre-Marie Baty -- unused variable
  117.  
  118.     // Demo has its own decryption key + behavior
  119.     if (harness_game_info.mode == eGame_carmageddon_demo) {
  120.         EncodeLine_DEMO(pS);
  121.         return;
  122.     }
  123.  
  124.     len = strlen(pS);
  125.     key = (char*)gLong_key;
  126. #if 0 // Pierre-Marie Baty -- consider the line individually
  127.     if (gEncryption_method == 0) {
  128.         PathCat(the_path, gApplication_path, "GENERAL.TXT");
  129.  
  130.         test = fopen(the_path, "rt");
  131.         if (test != NULL) {
  132.             fgets(s, 256, test);
  133.             if (s[0] != '@') {
  134.                 gEncryption_method = 2;
  135.             } else {
  136.                 gEncryption_method = 1;
  137.                 EncodeLine(&s[1]);
  138.                 s[7] = '\0';
  139.                 if (strcmp(&s[1], "0.01\t\t") != 0) {
  140.                     gEncryption_method = 2;
  141.                 }
  142.             }
  143.             fclose(test);
  144.         } else {
  145.             gEncryption_method = 2;
  146.         }
  147.     }
  148. #endif // 0
  149.     while (len > 0 && (pS[len - 1] == '\r' || pS[len - 1] == '\n')) {
  150.         len--;
  151.         pS[len] = '\0';
  152.     }
  153.  
  154.     seed = len % 16;
  155.  
  156.     for (i = 0; i < len; i++) {
  157.         c = pS[i];
  158. #if defined(DETHRACE_FIX_BUGS)
  159.         // When loading game data, Carmageddon does not switch the XOR cypher when the comments start.
  160.         if (i >= 2) {
  161.             if (pS[i - 1] == '/' && pS[i - 2] == '/') {
  162.                 key = (char*)gOther_long_key;
  163.             }
  164.         }
  165. #endif
  166. #if 0 // Pierre-Marie Baty -- decode systematically
  167.         if (gEncryption_method == 1) {
  168.             if (c == '\t') {
  169.                 c = 0x9f;
  170.             }
  171.  
  172.             c -= 0x20;
  173.             c ^= key[seed];
  174.             c &= 0x7f;
  175.             c += 0x20;
  176.  
  177.             seed += 7;
  178.             seed %= 16;
  179.  
  180.             if (c == 0x9f) {
  181.                 c = '\t';
  182.             }
  183.         } else {
  184. #endif // 0
  185.             if (c == '\t') {
  186.                 c = 0x80;
  187.             }
  188.  
  189.             c -= 0x20;
  190.             if ((c & 0x80) == 0) {
  191.                 c ^= key[seed] & 0x7f;
  192.             }
  193.             c += 0x20;
  194.  
  195.             seed += 7;
  196.             seed %= 16;
  197.  
  198.             if (c == 0x80) {
  199.                 c = '\t';
  200.             }
  201. #if 0 // Pierre-Marie Baty -- decode systematically
  202.         }
  203. #endif // 0
  204.         pS[i] = c;
  205.     }
  206. }
  207.  
  208. // IDA: int __usercall IRandomBetween@<EAX>(int pA@<EAX>, int pB@<EDX>)
  209. int IRandomBetween(int pA, int pB) {
  210. #if 1
  211.     int num;
  212.     //char s[32]; // Pierre-Marie Baty -- unused variable
  213.  
  214.     num = rand();
  215. #if (INT_MAX > 0x7fffffff) && (RAND_MAX == 0x7fff) // Pierre-Marie Baty -- this hack just doesn't work when sizeof(int) == 32 (e.g. on Windows)
  216.     //  If RAND_MAX == 0x7fff, then `num` can be seen as a fixed point number with 15 fractional and 17 integral bits
  217.     return pA + ((num * (pB + 1 - pA)) >> 15);
  218. #else
  219.     //  If RAND_MAX != 0x7fff, then use floating numbers (alternative is using modulo)
  220.     return pA + (int)((pB + 1 - pA) * (num / ((float)RAND_MAX + 1)));
  221. #endif
  222. #else // Pierre-Marie Baty -- madebr's code -- TODO: test
  223.     long long int a = rand () * (pB + 1 - pA);
  224.     a &= 0x7fffffffffffffff;
  225.     return pA + (a >> 15);
  226. #endif
  227. }
  228.  
  229. // IDA: int __usercall PercentageChance@<EAX>(int pC@<EAX>)
  230. int PercentageChance(int pC) {
  231.     LOG_TRACE("(%d)", pC);
  232.  
  233.     return IRandomBetween(0, 99) < pC;
  234. }
  235.  
  236. // IDA: int __usercall IRandomPosNeg@<EAX>(int pN@<EAX>)
  237. int IRandomPosNeg(int pN) {
  238.     LOG_TRACE("(%d)", pN);
  239.  
  240.     return IRandomBetween(-pN, pN);
  241. }
  242.  
  243. // IDA: float __cdecl FRandomBetween(float pA, float pB)
  244. float FRandomBetween(float pA, float pB) {
  245.     LOG_TRACE8("(%f, %f)", pA, pB);
  246.     return (double)rand() * (pB - pA) / (double)RAND_MAX + pA;
  247. }
  248.  
  249. // IDA: float __cdecl FRandomPosNeg(float pN)
  250. float FRandomPosNeg(float pN) {
  251.     LOG_TRACE("(%f)", pN);
  252.  
  253.     return FRandomBetween(-pN, pN);
  254. }
  255.  
  256. // IDA: br_scalar __cdecl SRandomBetween(br_scalar pA, br_scalar pB)
  257. br_scalar SRandomBetween(br_scalar pA, br_scalar pB) {
  258.     LOG_TRACE8("(%f, %f)", pA, pB);
  259.  
  260.     return FRandomBetween(pA, pB);
  261. }
  262.  
  263. // IDA: br_scalar __cdecl SRandomPosNeg(br_scalar pN)
  264. br_scalar SRandomPosNeg(br_scalar pN) {
  265.     LOG_TRACE("(%f)", pN);
  266.  
  267.     return SRandomBetween(-pN, pN);
  268. }
  269.  
  270. // IDA: char* __usercall GetALineWithNoPossibleService@<EAX>(FILE *pF@<EAX>, unsigned char *pS@<EDX>)
  271. char* GetALineWithNoPossibleService(FILE* pF, unsigned char* pS) {
  272.     // Jeff removed "signed' to avoid compiler warnings..
  273.     /*signed*/ char* result;
  274.     /*signed*/ char s[256];
  275.     int ch;
  276.     int len;
  277.     int i;
  278.  
  279.     do {
  280.         result = fgets(s, 256, pF);
  281.         if (result == NULL) {
  282.             s[0] = '\0';
  283.             break;
  284.         }
  285.         if (s[0] == '@') {
  286.             EncodeLine(&s[1]);
  287.             len = strlen(s);
  288.             memmove(s, &s[1], len);
  289.         } else {
  290.             while (s[0] == ' ' || s[0] == '\t') {
  291.                 len = strlen(s);
  292.                 memmove(s, &s[1], len);
  293.             }
  294.         }
  295.  
  296.         while (1) {
  297.             ch = fgetc(pF);
  298.             if (ch != '\r' && ch != '\n') {
  299.                 break;
  300.             }
  301.         }
  302.         if (ch != -1) {
  303.             ungetc(ch, pF);
  304.         }
  305.     } while (!isalnum(s[0])
  306.         && s[0] != '-'
  307.         && s[0] != '.'
  308.         && s[0] != '!'
  309.         && s[0] != '&'
  310.         && s[0] != '('
  311.         && s[0] != '\''
  312.         && s[0] != '\"'
  313.         && s[0] >= 0);
  314.  
  315.     if (result) {
  316.         len = strlen(result);
  317.         if (len != 0 && (result[len - 1] == '\r' || result[len - 1] == '\n')) {
  318.             result[len - 1] = 0;
  319.         }
  320.         if (len != 1 && (result[len - 2] == '\r' || result[len - 2] == '\n')) {
  321.             result[len - 2] = 0;
  322.         }
  323.     }
  324.     strcpy((char*)pS, s);
  325.     len = strlen(s);
  326.     for (i = 0; i < len; i++) {
  327.         if (pS[i] >= 0xe0) {
  328.             pS[i] -= 32;
  329.         }
  330.     }
  331.     // LOG_DEBUG("%s", result);
  332.     return result;
  333. }
  334.  
  335. // IDA: char* __usercall GetALineAndDontArgue@<EAX>(FILE *pF@<EAX>, char *pS@<EDX>)
  336. char* GetALineAndDontArgue(FILE* pF, char* pS) {
  337.     // LOG_TRACE10("(%p, \"%s\")", pF, pS);
  338.  
  339.     PossibleService();
  340.     return GetALineWithNoPossibleService(pF, (unsigned char*)pS);
  341. }
  342.  
  343. // IDA: void __usercall PathCat(char *pDestn_str@<EAX>, char *pStr_1@<EDX>, char *pStr_2@<EBX>)
  344. void PathCat(char* pDestn_str, char* pStr_1, char* pStr_2) {
  345.  
  346.     if (pDestn_str != pStr_1) { // Added to avoid strcpy overlap checks
  347.         strcpy(pDestn_str, pStr_1);
  348.     }
  349.     if (strlen(pStr_2) != 0) {
  350.         strcat(pDestn_str, gDir_separator);
  351.         strcat(pDestn_str, pStr_2);
  352.     }
  353. }
  354.  
  355. // IDA: int __cdecl Chance(float pChance_per_second, int pPeriod)
  356. int Chance(float pChance_per_second, int pPeriod) {
  357.     LOG_TRACE("(%f, %d)", pChance_per_second, pPeriod);
  358.  
  359.     return FRandomBetween(0.f, 1.f) < (pPeriod * pChance_per_second / 1000.f);
  360. }
  361.  
  362. // IDA: float __cdecl tandeg(float pAngle)
  363. float tandeg(float pAngle) {
  364.     LOG_TRACE("(%f)", pAngle);
  365.  
  366.     pAngle = DEG_TO_RAD(pAngle);
  367.     return sinf(pAngle) / cosf(pAngle);
  368. }
  369.  
  370. // IDA: tU32 __usercall GetFileLength@<EAX>(FILE *pF@<EAX>)
  371. tU32 GetFileLength(FILE* pF) {
  372.     tU32 the_size;
  373.  
  374.     fseek(pF, 0, SEEK_END);
  375.     the_size = ftell(pF);
  376.     rewind(pF);
  377.     return the_size;
  378. }
  379.  
  380. // IDA: int __usercall BooleanTo1Or0@<EAX>(int pB@<EAX>)
  381. int BooleanTo1Or0(int pB) {
  382.     LOG_TRACE("(%d)", pB);
  383.  
  384.     return pB != 0;
  385. }
  386.  
  387. // IDA: br_pixelmap* __usercall DRPixelmapAllocate@<EAX>(br_uint_8 pType@<EAX>, br_uint_16 pW@<EDX>, br_uint_16 pH@<EBX>, void *pPixels@<ECX>, int pFlags)
  388. br_pixelmap* DRPixelmapAllocate(br_uint_8 pType, br_uint_16 pW, br_uint_16 pH, void* pPixels, int pFlags) {
  389.     br_pixelmap* the_map;
  390.  
  391.     the_map = BrPixelmapAllocate(pType, pW, pH, pPixels, pFlags);
  392.     if (the_map != NULL) {
  393.         the_map->origin_y = 0;
  394.         the_map->origin_x = 0;
  395.     }
  396.     return the_map;
  397. }
  398.  
  399. // IDA: br_pixelmap* __usercall DRPixelmapAllocateSub@<EAX>(br_pixelmap *pPm@<EAX>, br_uint_16 pX@<EDX>, br_uint_16 pY@<EBX>, br_uint_16 pW@<ECX>, br_uint_16 pH)
  400. br_pixelmap* DRPixelmapAllocateSub(br_pixelmap* pPm, br_uint_16 pX, br_uint_16 pY, br_uint_16 pW, br_uint_16 pH) {
  401.     br_pixelmap* the_map;
  402.     LOG_TRACE("(%p, %d, %d, %d, %d)", pPm, pX, pY, pW, pH);
  403.  
  404.     the_map = BrPixelmapAllocateSub(pPm, pX, pY, pW, pH);
  405.     if (the_map != NULL) {
  406.         the_map->origin_y = 0;
  407.         the_map->origin_x = 0;
  408.     }
  409.     return the_map;
  410. }
  411.  
  412. // IDA: br_pixelmap* __usercall DRPixelmapMatchSized@<EAX>(br_pixelmap *pSrc@<EAX>, tU8 pMatch_type@<EDX>, tS32 pWidth@<EBX>, tS32 pHeight@<ECX>)
  413. br_pixelmap* DRPixelmapMatchSized(br_pixelmap* pSrc, tU8 pMatch_type, tS32 pWidth, tS32 pHeight) {
  414.     //br_pixelmap* result; // Pierre-Marie Baty -- unused variable
  415.     LOG_TRACE("(%p, %d, %d, %d)", pSrc, pMatch_type, pWidth, pHeight);
  416.     NOT_IMPLEMENTED();
  417. }
  418.  
  419. // IDA: void __usercall CopyDoubled8BitTo16BitRectangle(br_pixelmap *pDst@<EAX>, br_pixelmap *pSrc@<EDX>, int pSrc_width@<EBX>, int pSrc_height@<ECX>, int pDst_x, int pDst_y, br_pixelmap *pPalette)
  420. void CopyDoubled8BitTo16BitRectangle(br_pixelmap* pDst, br_pixelmap* pSrc, int pSrc_width, int pSrc_height, int pDst_x, int pDst_y, br_pixelmap* pPalette) {
  421.     //int x; // Pierre-Marie Baty -- unused variable
  422.     //int y; // Pierre-Marie Baty -- unused variable
  423.     //tU8* src_start; // Pierre-Marie Baty -- unused variable
  424.     //tU16* dst_start0; // Pierre-Marie Baty -- unused variable
  425.     //tU16* dst_start1; // Pierre-Marie Baty -- unused variable
  426.     //tU16* palette_entry; // Pierre-Marie Baty -- unused variable
  427.     LOG_TRACE("(%p, %p, %d, %d, %d, %d, %p)", pDst, pSrc, pSrc_width, pSrc_height, pDst_x, pDst_y, pPalette);
  428.     NOT_IMPLEMENTED();
  429. }
  430.  
  431. // IDA: br_pixelmap* __usercall Scale8BitPixelmap@<EAX>(br_pixelmap *pSrc@<EAX>, int pWidth@<EDX>, int pHeight@<EBX>)
  432. br_pixelmap* Scale8BitPixelmap(br_pixelmap* pSrc, int pWidth, int pHeight) {
  433.     //br_pixelmap* result; // Pierre-Marie Baty -- unused variable
  434.     //int x; // Pierre-Marie Baty -- unused variable
  435.     //int y; // Pierre-Marie Baty -- unused variable
  436.     //tU8* src_pixels; // Pierre-Marie Baty -- unused variable
  437.     //tU8* dst_pixels; // Pierre-Marie Baty -- unused variable
  438.     LOG_TRACE("(%p, %d, %d)", pSrc, pWidth, pHeight);
  439.     NOT_IMPLEMENTED();
  440. }
  441.  
  442. // IDA: br_pixelmap* __usercall Tile8BitPixelmap@<EAX>(br_pixelmap *pSrc@<EAX>, int pN@<EDX>)
  443. br_pixelmap* Tile8BitPixelmap(br_pixelmap* pSrc, int pN) {
  444.     //br_pixelmap* result; // Pierre-Marie Baty -- unused variable
  445.     //int new_width; // Pierre-Marie Baty -- unused variable
  446.     //int new_height; // Pierre-Marie Baty -- unused variable
  447.     //int x; // Pierre-Marie Baty -- unused variable
  448.     //int y2; // Pierre-Marie Baty -- unused variable
  449.     //int y; // Pierre-Marie Baty -- unused variable
  450.     //tU8* src_pixels; // Pierre-Marie Baty -- unused variable
  451.     //tU8* dst_pixels; // Pierre-Marie Baty -- unused variable
  452.     LOG_TRACE("(%p, %d)", pSrc, pN);
  453.     NOT_IMPLEMENTED();
  454. }
  455.  
  456. // IDA: tException_list __usercall FindExceptionInList@<EAX>(char *pName@<EAX>, tException_list pList@<EDX>)
  457. tException_list FindExceptionInList(char* pName, tException_list pList) {
  458.     LOG_TRACE("(\"%s\", %d)", pName, pList);
  459.     NOT_IMPLEMENTED();
  460. }
  461.  
  462. // IDA: br_pixelmap* __usercall PurifiedPixelmap@<EAX>(br_pixelmap *pSrc@<EAX>)
  463. br_pixelmap* PurifiedPixelmap(br_pixelmap* pSrc) {
  464.     //br_pixelmap* intermediate; // Pierre-Marie Baty -- unused variable
  465.     //br_pixelmap* result; // Pierre-Marie Baty -- unused variable
  466.     //int new_width; // Pierre-Marie Baty -- unused variable
  467.     //int new_height; // Pierre-Marie Baty -- unused variable
  468.     //tException_list e; // Pierre-Marie Baty -- unused variable
  469.     LOG_TRACE("(%p)", pSrc);
  470.     NOT_IMPLEMENTED();
  471. }
  472.  
  473. // IDA: br_pixelmap* __usercall DRPixelmapLoad@<EAX>(char *pFile_name@<EAX>)
  474. br_pixelmap* DRPixelmapLoad(char* pFile_name) {
  475.     br_pixelmap* the_map;
  476.     //br_int_8 lobyte; // Pierre-Marie Baty -- unused variable
  477.     LOG_TRACE("(\"%s\")", pFile_name);
  478.  
  479.     the_map = BrPixelmapLoad(pFile_name);
  480.     if (the_map != NULL) {
  481.         the_map->origin_x = 0;
  482.         the_map->origin_y = 0;
  483. #if !defined(DETHRACE_FIX_BUGS)
  484.         the_map->row_bytes = (the_map->row_bytes + sizeof(int32_t) - 1) & ~(sizeof(int32_t) - 1);
  485. #endif
  486.     }
  487.     return the_map;
  488. }
  489.  
  490. // IDA: br_uint_32 __usercall DRPixelmapLoadMany@<EAX>(char *pFile_name@<EAX>, br_pixelmap **pPixelmaps@<EDX>, br_uint_16 pNum@<EBX>)
  491. br_uint_32 DRPixelmapLoadMany(char* pFile_name, br_pixelmap** pPixelmaps, br_uint_16 pNum) {
  492.     br_pixelmap* the_map;
  493.     int number_loaded;
  494.     int i;
  495.     //br_uint_8 lobyte; // Pierre-Marie Baty -- unused variable
  496.     LOG_TRACE("(\"%s\", %p, %d)", pFile_name, pPixelmaps, pNum);
  497.     number_loaded = BrPixelmapLoadMany(pFile_name, pPixelmaps, pNum);
  498.     for (i = 0; i < number_loaded; i++) {
  499.         the_map = pPixelmaps[i];
  500. #if !defined(DETHRACE_FIX_BUGS)
  501.         the_map->row_bytes = (the_map->row_bytes + sizeof(int32_t) - 1) & ~(sizeof(int32_t) - 1);
  502. #endif
  503.         the_map->base_x = 0;
  504.         the_map->base_y = 0;
  505.     }
  506.     return number_loaded;
  507. }
  508.  
  509. // IDA: void __usercall WaitFor(tU32 pDelay@<EAX>)
  510. void WaitFor(tU32 pDelay) {
  511.     tU32 start_time;
  512.     LOG_TRACE("(%d)", pDelay);
  513.  
  514.     start_time = PDGetTotalTime();
  515.     while (start_time + pDelay < (tU32) PDGetTotalTime()) { // Pierre-Marie Baty -- added type cast
  516.         SoundService();
  517.     }
  518. }
  519.  
  520. // IDA: br_uint_32 __usercall DRActorEnumRecurse@<EAX>(br_actor *pActor@<EAX>, br_actor_enum_cbfn *callback@<EDX>, void *arg@<EBX>)
  521. intptr_t DRActorEnumRecurse(br_actor* pActor, br_actor_enum_cbfn* callback, void* arg) {
  522.     intptr_t result;
  523.  
  524.     result = callback(pActor, arg);
  525.     if (result != 0) {
  526.         return result;
  527.     }
  528.     for (pActor = pActor->children; pActor != NULL; pActor = pActor->next) {
  529.         result = DRActorEnumRecurse(pActor, callback, arg);
  530.         if (result != 0) {
  531.             return result;
  532.         }
  533.     }
  534.     return 0;
  535. }
  536.  
  537. // IDA: br_uint_32 __cdecl CompareActorID(br_actor *pActor, void *pArg)
  538. intptr_t CompareActorID(br_actor* pActor, void* pArg) {
  539.     LOG_TRACE("(%p, %p)", pActor, pArg);
  540.  
  541.     if (pActor->identifier && !strcmp(pActor->identifier, (const char*)pArg)) {
  542.         return (intptr_t)pActor;
  543.     } else {
  544.         return 0;
  545.     }
  546. }
  547.  
  548. // IDA: br_actor* __usercall DRActorFindRecurse@<EAX>(br_actor *pSearch_root@<EAX>, char *pName@<EDX>)
  549. br_actor* DRActorFindRecurse(br_actor* pSearch_root, char* pName) {
  550.     LOG_TRACE("(%p, \"%s\")", pSearch_root, pName);
  551.  
  552.     return (br_actor*)DRActorEnumRecurse(pSearch_root, CompareActorID, pName);
  553. }
  554.  
  555. // IDA: br_uint_32 __usercall DRActorEnumRecurseWithMat@<EAX>(br_actor *pActor@<EAX>, br_material *pMat@<EDX>, br_uint_32 (*pCall_back)(br_actor*, br_material*, void*)@<EBX>, void *pArg@<ECX>)
  556. br_uint_32 DRActorEnumRecurseWithMat(br_actor* pActor, br_material* pMat, recurse_with_mat_cbfn* pCall_back, void* pArg) {
  557.     br_uint_32 result;
  558.     LOG_TRACE("(%p, %p, %p, %p)", pActor, pMat, pCall_back, pArg);
  559.  
  560.     if (pActor->material != NULL) {
  561.         pMat = pActor->material;
  562.     }
  563.     result = pCall_back(pActor, pMat, pArg);
  564.     if (result != 0) {
  565.         return result;
  566.     }
  567.     for (pActor = pActor->children; pActor != NULL; pActor = pActor->next) {
  568.         result = DRActorEnumRecurseWithMat(pActor, pMat, pCall_back, pArg);
  569.         if (result != 0) {
  570.             return result;
  571.         }
  572.     }
  573.     return 0;
  574. }
  575.  
  576. // IDA: br_uint_32 __usercall DRActorEnumRecurseWithTrans@<EAX>(br_actor *pActor@<EAX>, br_matrix34 *pMatrix@<EDX>, br_uint_32 (*pCall_back)(br_actor*, br_matrix34*, void*)@<EBX>, void *pArg@<ECX>)
  577. br_uint_32 DRActorEnumRecurseWithTrans(br_actor* pActor, br_matrix34* pMatrix, br_uint_32 (*pCall_back)(br_actor*, br_matrix34*, void*), void* pArg) {
  578.     br_uint_32 result;
  579.     br_matrix34 combined_transform;
  580.     LOG_TRACE("(%p, %p, %p, %p)", pActor, pMatrix, pCall_back, pArg);
  581.  
  582.     if (pMatrix == NULL) {
  583.         BrMatrix34Copy(&combined_transform, &pActor->t.t.mat);
  584.     } else {
  585.         BrMatrix34Mul(&combined_transform, pMatrix, &pActor->t.t.mat);
  586.     }
  587.     result = pCall_back(pActor, &combined_transform, pArg);
  588.     if (result != 0) {
  589.         return result;
  590.     }
  591.     for (pActor = pActor->children; pActor != NULL; pActor = pActor->next) {
  592.         result = DRActorEnumRecurseWithTrans(pActor, &combined_transform, pCall_back, pArg);
  593.         if (result != 0) {
  594.             return result;
  595.         }
  596.     }
  597.     return 0;
  598. }
  599.  
  600. // IDA: int __usercall sign@<EAX>(int pNumber@<EAX>)
  601. int sign(int pNumber) {
  602.     LOG_TRACE("(%d)", pNumber);
  603.  
  604.     if (pNumber > 0) {
  605.         return 1;
  606.     } else if (pNumber < 0) {
  607.         return -1;
  608.     } else {
  609.         return 0;
  610.     }
  611. }
  612.  
  613. // IDA: float __cdecl fsign(float pNumber)
  614. float fsign(float pNumber) {
  615.     LOG_TRACE("(%f)", pNumber);
  616.     if (pNumber > 0.f) {
  617.         return 1;
  618.     } else if (pNumber < 0.f) {
  619.         return -1.f;
  620.     } else {
  621.         return 0.f;
  622.     }
  623. }
  624.  
  625. // IDA: FILE* __usercall OpenUniqueFileB@<EAX>(char *pPrefix@<EAX>, char *pExtension@<EDX>)
  626. FILE* OpenUniqueFileB(char* pPrefix, char* pExtension) {
  627.     int index;
  628.     FILE* f;
  629.     tPath_name the_path;
  630.     LOG_TRACE("(\"%s\", \"%s\")", pPrefix, pExtension);
  631.  
  632.     for (index = 0; index < 10000; index++) {
  633.         PathCat(the_path, gApplication_path, pPrefix);
  634.         sprintf(the_path + strlen(the_path), "%04d.%s", index, pExtension);
  635.         f = DRfopen(the_path, "rt");
  636.         if (f == NULL) {
  637.             return DRfopen(the_path, "wb");
  638.         }
  639.         fclose(f);
  640.     }
  641.     return NULL;
  642. }
  643.  
  644. // IDA: void __usercall PrintScreenFile(FILE *pF@<EAX>)
  645. void PrintScreenFile(FILE* pF) {
  646.     int i;
  647.     int j;
  648.     int bit_map_size;
  649.     int offset;
  650.     //tU8* pixel_ptr; // Pierre-Marie Baty -- unused variable
  651.     LOG_TRACE("(%p)", pF);
  652.  
  653.     bit_map_size = gBack_screen->height * gBack_screen->row_bytes;
  654.  
  655.     // 1. BMP Header
  656.     //    1. 'BM' Signature
  657.     WriteU8L(pF, 'B');
  658.     WriteU8L(pF, 'M');
  659.     //    2. File size in bytes (header = 0xe bytes; infoHeader = 0x28 bytes; colorTable = 0x400 bytes; pixelData = xxx)
  660.     WriteU32L(pF, bit_map_size + 0x436);
  661.     //    3. unused
  662.     WriteU16L(pF, 0);
  663.     //    4. unused
  664.     WriteU16L(pF, 0);
  665.     //    5. pixelData offset (from beginning of file)
  666.     WriteU32L(pF, 0x436);
  667.  
  668.     // 2. Info Header
  669.     //    1. InfoHeader Size
  670.     WriteU32L(pF, 0x28);
  671.     //    2. Width of bitmap in pixels
  672.     WriteU32L(pF, gBack_screen->row_bytes);
  673.     //    3. Height of bitmap in pixels
  674.     WriteU32L(pF, gBack_screen->height);
  675.     //    4. Number of planes
  676.     WriteU16L(pF, 1);
  677.     //    5. Bits per pixels / palletization (8 -> 8bit palletized ==> #colors = 256)
  678.     WriteU16L(pF, 8);
  679.     //    6. Compression (0 = BI_RGB -> no compression)
  680.     WriteU32L(pF, 0);
  681.     //    7. Image Size (0 --> no compression)
  682.     WriteU32L(pF, 0);
  683.     //    8. Horizontal Pixels Per Meter
  684.     WriteU32L(pF, 0);
  685.     //    9. Vertical Pixels Per Meter
  686.     WriteU32L(pF, 0);
  687.     //    10. # Actually used colors
  688.     WriteU32L(pF, 0);
  689.     //    11. Number of important colors
  690.     WriteU32L(pF, 256);
  691.  
  692.     // 3. Color table (=palette)
  693.     for (i = 0; i < 256; i++) {
  694.         // red, green, blue, unused
  695.         WriteU8L(pF, ((tU8*)gCurrent_palette->pixels)[4 * i]);
  696.         WriteU8L(pF, ((tU8*)gCurrent_palette->pixels)[4 * i + 1]);
  697.         WriteU8L(pF, ((tU8*)gCurrent_palette->pixels)[4 * i + 2]);
  698.         WriteU8L(pF, 0);
  699.     }
  700.  
  701.     // 4. Pixel Data (=LUT)
  702.     offset = bit_map_size - gBack_screen->row_bytes;
  703.     for (i = 0; i < gBack_screen->height; i++) {
  704.         for (j = 0; j < gBack_screen->row_bytes; j++) {
  705.             WriteU8L(pF, ((tU8*)gBack_screen->pixels)[offset]);
  706.             offset++;
  707.         }
  708.         offset -= 2 * gBack_screen->row_bytes;
  709.     }
  710.     WriteU16L(pF, 0);
  711. }
  712.  
  713. // IDA: void __usercall PrintScreenFile16(FILE *pF@<EAX>)
  714. void PrintScreenFile16(FILE* pF) {
  715.     //int i; // Pierre-Marie Baty -- unused variable
  716.     //int j; // Pierre-Marie Baty -- unused variable
  717.     //int bit_map_size; // Pierre-Marie Baty -- unused variable
  718.     //int offset; // Pierre-Marie Baty -- unused variable
  719.     //tU8* pixel_ptr; // Pierre-Marie Baty -- unused variable
  720.     //tU16 pixel; // Pierre-Marie Baty -- unused variable
  721.     LOG_TRACE("(%p)", pF);
  722.     NOT_IMPLEMENTED();
  723. }
  724.  
  725. // IDA: void __cdecl PrintScreen()
  726. void PrintScreen(void) {
  727.     FILE* f;
  728.     LOG_TRACE("()");
  729.  
  730.     f = OpenUniqueFileB("DUMP", "BMP");
  731.     if (f != NULL) {
  732.         PrintScreenFile(f);
  733.         fclose(f);
  734.     }
  735. }
  736.  
  737. // IDA: tU32 __cdecl GetTotalTime()
  738. tU32 GetTotalTime(void) {
  739.     LOG_TRACE9("()");
  740.  
  741.     if (gAction_replay_mode) {
  742.         return gLast_replay_frame_time;
  743.     }
  744.     if (gNet_mode != eNet_mode_none) {
  745.         return PDGetTotalTime();
  746.     }
  747.     return PDGetTotalTime() - gLost_time;
  748. }
  749.  
  750. // IDA: tU32 __cdecl GetRaceTime()
  751. tU32 GetRaceTime(void) {
  752.     LOG_TRACE("()");
  753.  
  754.     return GetTotalTime() - gRace_start;
  755. }
  756.  
  757. // IDA: void __usercall AddLostTime(tU32 pLost_time@<EAX>)
  758. void AddLostTime(tU32 pLost_time) {
  759.  
  760.     gLost_time += pLost_time;
  761. }
  762.  
  763. // IDA: void __usercall TimerString(tU32 pTime@<EAX>, char *pStr@<EDX>, int pFudge_colon@<EBX>, int pForce_colon@<ECX>)
  764. void TimerString(tU32 pTime, char* pStr, int pFudge_colon, int pForce_colon) {
  765.     int seconds;
  766.     LOG_TRACE("(%d, \"%s\", %d, %d)", pTime, pStr, pFudge_colon, pForce_colon);
  767.  
  768.     seconds = (pTime + 500) / 1000;
  769.     if (pForce_colon || seconds > 59) {
  770.         if (pFudge_colon) {
  771.             sprintf(pStr, "%d/%02d", seconds / 60, seconds % 60);
  772.         } else {
  773.             sprintf(pStr, "%d:%02d", seconds / 60, seconds % 60);
  774.         }
  775.     } else {
  776.         sprintf(pStr, "%d", seconds);
  777.     }
  778. }
  779.  
  780. // IDA: char* __usercall GetMiscString@<EAX>(int pIndex@<EAX>)
  781. char* GetMiscString(int pIndex) {
  782.  
  783.     return gMisc_strings[pIndex];
  784. }
  785.  
  786. // IDA: void __usercall GetCopyOfMiscString(int pIndex@<EAX>, char *pStr@<EDX>)
  787. void GetCopyOfMiscString(int pIndex, char* pStr) {
  788.     LOG_TRACE("(%d, \"%s\")", pIndex, pStr);
  789.  
  790.     strcpy(pStr, GetMiscString(pIndex));
  791. }
  792.  
  793. // IDA: int __usercall Flash@<EAX>(tU32 pPeriod@<EAX>, tU32 *pLast_change@<EDX>, int *pCurrent_state@<EBX>)
  794. int Flash(tU32 pPeriod, tU32* pLast_change, int* pCurrent_state) {
  795.     tU32 the_time;
  796.     LOG_TRACE("(%d, %p, %p)", pPeriod, pLast_change, pCurrent_state);
  797.  
  798.     the_time = PDGetTotalTime();
  799.     if (the_time - *pLast_change > pPeriod) {
  800.         *pCurrent_state = !*pCurrent_state;
  801.         *pLast_change = the_time;
  802.     }
  803.     return *pCurrent_state;
  804. }
  805.  
  806. // IDA: void __usercall MaterialCopy(br_material *pDst@<EAX>, br_material *pSrc@<EDX>)
  807. void MaterialCopy(br_material* pDst, br_material* pSrc) {
  808.     LOG_TRACE("(%p, %p)", pDst, pSrc);
  809.  
  810.     pDst->flags = pSrc->flags;
  811.     pDst->ka = pSrc->ka;
  812.     pDst->kd = pSrc->kd;
  813.     pDst->ks = pSrc->ks;
  814.     pDst->power = pSrc->power;
  815.     pDst->colour = pSrc->colour;
  816.     pDst->index_base = pSrc->index_base;
  817.     pDst->index_range = pSrc->index_range;
  818.     pDst->index_shade = pSrc->index_shade;
  819.     pDst->colour_map = pSrc->colour_map;
  820.     pDst->map_transform = pSrc->map_transform;
  821.     pDst->identifier = pSrc->identifier;
  822. }
  823.  
  824. // IDA: double __usercall RGBDifferenceSqr@<ST0>(tRGB_colour *pColour_1@<EAX>, tRGB_colour *pColour_2@<EDX>)
  825. double RGBDifferenceSqr(tRGB_colour* pColour_1, tRGB_colour* pColour_2) {
  826.     LOG_TRACE("(%p, %p)", pColour_1, pColour_2);
  827.  
  828.     return ((pColour_1->red - pColour_2->red) * (pColour_1->red - pColour_2->red))
  829.         + ((pColour_1->green - pColour_2->green) * (pColour_1->green - pColour_2->green))
  830.         + ((pColour_1->blue - pColour_2->blue) * (pColour_1->blue - pColour_2->blue));
  831. }
  832.  
  833. // IDA: int __usercall FindBestMatch@<EAX>(tRGB_colour *pRGB_colour@<EAX>, br_pixelmap *pPalette@<EDX>)
  834. int FindBestMatch(tRGB_colour* pRGB_colour, br_pixelmap* pPalette) {
  835.     int n;
  836.     int near_c;
  837.     double min_d;
  838.     double d;
  839.     tRGB_colour trial_RGB;
  840.     br_colour* dp;
  841.     LOG_TRACE("(%p, %p)", pRGB_colour, pPalette);
  842.  
  843.     near_c = 127;
  844.     min_d = 1.79769e+308; // max double
  845.     dp = pPalette->pixels;
  846.     for (n = 0; n < 256; n++) {
  847.         trial_RGB.red = (dp[n] >> 16) & 0xff;
  848.         trial_RGB.green = (dp[n] >> 8) & 0xff;
  849.         trial_RGB.blue = (dp[n] >> 0) & 0xff;
  850.         d = RGBDifferenceSqr(pRGB_colour, &trial_RGB);
  851.         if (d < min_d) {
  852.             min_d = d;
  853.             near_c = n;
  854.         }
  855.     }
  856.     return near_c;
  857. }
  858.  
  859. // IDA: void __usercall BuildShadeTablePath(char *pThe_path@<EAX>, int pR@<EDX>, int pG@<EBX>, int pB@<ECX>)
  860. void BuildShadeTablePath(char* pThe_path, int pR, int pG, int pB) {
  861.     char s[32];
  862.     LOG_TRACE("(\"%s\", %d, %d, %d)", pThe_path, pR, pG, pB);
  863.  
  864.     s[0] = 's';
  865.     s[1] = 't';
  866.     s[2] = 'A' + ((pR & 0xf0) >> 4);
  867.     s[3] = 'A' + ((pR & 0x0f) >> 0);
  868.     s[4] = 'A' + ((pG & 0xf0) >> 4);
  869.     s[5] = 'A' + ((pG & 0x0f) >> 0);
  870.     s[6] = 'A' + ((pB & 0xf0) >> 4);
  871.     s[7] = 'A' + ((pB & 0x0f) >> 0);
  872.     s[8] = '\0';
  873.     strcat(s, ".TAB");
  874.     PathCat(pThe_path, gApplication_path, "SHADETAB");
  875.     PathCat(pThe_path, pThe_path, s);
  876. }
  877.  
  878. // IDA: br_pixelmap* __usercall LoadGeneratedShadeTable@<EAX>(int pR@<EAX>, int pG@<EDX>, int pB@<EBX>)
  879. br_pixelmap* LoadGeneratedShadeTable(int pR, int pG, int pB) {
  880.     char the_path[256];
  881.     LOG_TRACE("(%d, %d, %d)", pR, pG, pB);
  882.  
  883.     BuildShadeTablePath(the_path, pR, pG, pB);
  884.     return BrPixelmapLoad(the_path);
  885. }
  886.  
  887. // IDA: void __usercall SaveGeneratedShadeTable(br_pixelmap *pThe_table@<EAX>, int pR@<EDX>, int pG@<EBX>, int pB@<ECX>)
  888. void SaveGeneratedShadeTable(br_pixelmap* pThe_table, int pR, int pG, int pB) {
  889.     char the_path[256];
  890.     LOG_TRACE("(%p, %d, %d, %d)", pThe_table, pR, pG, pB);
  891.  
  892.     BuildShadeTablePath(the_path, pR, pG, pB);
  893.     BrPixelmapSave(the_path, pThe_table);
  894. }
  895.  
  896. // IDA: br_pixelmap* __usercall GenerateShadeTable@<EAX>(int pHeight@<EAX>, br_pixelmap *pPalette@<EDX>, int pRed_mix@<EBX>, int pGreen_mix@<ECX>, int pBlue_mix, float pQuarter, float pHalf, float pThree_quarter)
  897. br_pixelmap* GenerateShadeTable(int pHeight, br_pixelmap* pPalette, int pRed_mix, int pGreen_mix, int pBlue_mix, float pQuarter, float pHalf, float pThree_quarter) {
  898.     LOG_TRACE("(%d, %p, %d, %d, %d, %f, %f, %f)", pHeight, pPalette, pRed_mix, pGreen_mix, pBlue_mix, pQuarter, pHalf, pThree_quarter);
  899.  
  900.     PossibleService();
  901.     return GenerateDarkenedShadeTable(
  902.         pHeight,
  903.         pPalette,
  904.         pRed_mix,
  905.         pGreen_mix,
  906.         pBlue_mix,
  907.         pQuarter,
  908.         pHalf,
  909.         pThree_quarter,
  910.         1.0);
  911. }
  912.  
  913. // IDA: br_pixelmap* __usercall GenerateDarkenedShadeTable@<EAX>(int pHeight@<EAX>, br_pixelmap *pPalette@<EDX>, int pRed_mix@<EBX>, int pGreen_mix@<ECX>, int pBlue_mix, float pQuarter, float pHalf, float pThree_quarter, br_scalar pDarken)
  914. br_pixelmap* GenerateDarkenedShadeTable(int pHeight, br_pixelmap* pPalette, int pRed_mix, int pGreen_mix, int pBlue_mix, float pQuarter, float pHalf, float pThree_quarter, br_scalar pDarken) {
  915.     br_pixelmap* the_table;
  916.     tRGB_colour the_RGB;
  917.     tRGB_colour new_RGB;
  918.     tRGB_colour ref_col;
  919.     br_colour* cp;
  920.     char* tab_ptr;
  921.     char* shade_ptr;
  922.     double f_i;
  923.     double f_total_minus_1;
  924.     double ratio1;
  925.     double ratio2;
  926.     int i;
  927.     int c;
  928.     LOG_TRACE("(%d, %p, %d, %d, %d, %f, %f, %f, %f)", pHeight, pPalette, pRed_mix, pGreen_mix, pBlue_mix, pQuarter, pHalf, pThree_quarter, pDarken);
  929.  
  930.     the_table = LoadGeneratedShadeTable(pRed_mix, pGreen_mix, pBlue_mix);
  931.     if (the_table == NULL) {
  932.         the_table = BrPixelmapAllocate(BR_PMT_INDEX_8, 256, pHeight, NULL, 0);
  933.         if (the_table == NULL) {
  934.             FatalError(kFatalError_LoadGeneratedShadeTable);
  935.         }
  936.         cp = pPalette->pixels;
  937.  
  938.         ref_col.red = pRed_mix;
  939.         ref_col.green = pGreen_mix;
  940.         ref_col.blue = pBlue_mix;
  941.  
  942.         for (c = 0, tab_ptr = the_table->pixels; c < 256; c++, tab_ptr++) {
  943.             the_RGB.red = ((cp[c] >> 16) & 0xff) * pDarken;
  944.             the_RGB.green = ((cp[c] >> 8) & 0xff) * pDarken;
  945.             the_RGB.blue = ((cp[c] >> 0) & 0xff) * pDarken;
  946.  
  947.             if (pHeight == 1) {
  948.                 f_total_minus_1 = 1.;
  949.             } else {
  950.                 f_total_minus_1 = pHeight - 1;
  951.             }
  952.             shade_ptr = tab_ptr;
  953.             for (i = 0, shade_ptr = tab_ptr; i < pHeight; i++, shade_ptr += 0x100) {
  954.                 f_i = i;
  955.                 ratio1 = f_i / f_total_minus_1;
  956.                 if (ratio1 < .5) {
  957.                     if (ratio1 < .25) {
  958.                         ratio2 = pQuarter * ratio1 * 4.;
  959.                     } else {
  960.                         ratio2 = (ratio1 - .25) * (pHalf - pQuarter) * 4. + pQuarter;
  961.                     }
  962.                 } else {
  963.                     if (ratio1 < 0.75) {
  964.                         ratio2 = (ratio1 - .5) * (pThree_quarter - pHalf) * 4. + pHalf;
  965.                     } else {
  966.                         ratio2 = 1. - (1. - pThree_quarter) * (1. - ratio1) * 4.;
  967.                     }
  968.                 }
  969.                 new_RGB.red = ref_col.red * ratio2 + the_RGB.red * (1. - ratio2);
  970.                 new_RGB.green = ref_col.green * ratio2 + the_RGB.green * (1. - ratio2);
  971.                 new_RGB.blue = ref_col.blue * ratio2 + the_RGB.blue * (1. - ratio2);
  972.                 *shade_ptr = FindBestMatch(&new_RGB, pPalette);
  973.             }
  974.         }
  975.         SaveGeneratedShadeTable(the_table, pRed_mix, pGreen_mix, pBlue_mix);
  976.     }
  977.     BrTableAdd(the_table);
  978.     return the_table;
  979. }
  980.  
  981. // IDA: void __cdecl PossibleService()
  982. void PossibleService(void) {
  983.     tU32 time;
  984.     static tU32 last_service = 0;
  985.  
  986.     time = PDGetTotalTime();
  987.     if (time - last_service > MIN_SERVICE_INTERVAL && !gProgram_state.racing) {
  988.         SoundService();
  989.         NetService(gProgram_state.racing);
  990.         last_service = time;
  991.     }
  992. }
  993.  
  994. // IDA: void __usercall DRMatrix34TApplyP(br_vector3 *pA@<EAX>, br_vector3 *pB@<EDX>, br_matrix34 *pC@<EBX>)
  995. void DRMatrix34TApplyP(br_vector3* pA, br_vector3* pB, br_matrix34* pC) {
  996.     br_scalar t1;
  997.     br_scalar t2;
  998.     br_scalar t3;
  999.     LOG_TRACE("(%p, %p, %p)", pA, pB, pC);
  1000.  
  1001.     t1 = pB->v[0] - pC->m[3][0];
  1002.     t2 = pB->v[1] - pC->m[3][1];
  1003.     t3 = pB->v[2] - pC->m[3][2];
  1004.     pA->v[0] = pC->m[0][0] * t1 + pC->m[0][1] * t2 + pC->m[0][2] * t3;
  1005.     pA->v[1] = pC->m[1][0] * t1 + pC->m[1][1] * t2 + pC->m[1][2] * t3;
  1006.     pA->v[2] = pC->m[2][0] * t1 + pC->m[2][1] * t2 + pC->m[2][2] * t3;
  1007. }
  1008.  
  1009. // IDA: tU16 __usercall PaletteEntry16Bit@<AX>(br_pixelmap *pPal@<EAX>, int pEntry@<EDX>)
  1010. tU16 PaletteEntry16Bit(br_pixelmap* pPal, int pEntry) {
  1011.     //tU32* src_entry; // Pierre-Marie Baty -- unused variable
  1012.     //int red; // Pierre-Marie Baty -- unused variable
  1013.     //int green; // Pierre-Marie Baty -- unused variable
  1014.     //int blue; // Pierre-Marie Baty -- unused variable
  1015.     LOG_TRACE("(%p, %d)", pPal, pEntry);
  1016.     NOT_IMPLEMENTED();
  1017. }
  1018.  
  1019. // IDA: br_pixelmap* __usercall PaletteOf16Bits@<EAX>(br_pixelmap *pSrc@<EAX>)
  1020. br_pixelmap* PaletteOf16Bits(br_pixelmap* pSrc) {
  1021.     //tU16* dst_entry; // Pierre-Marie Baty -- unused variable
  1022.     //int value; // Pierre-Marie Baty -- unused variable
  1023.     LOG_TRACE("(%p)", pSrc);
  1024.     NOT_IMPLEMENTED();
  1025. }
  1026.  
  1027. // IDA: void __usercall Copy8BitTo16Bit(br_pixelmap *pDst@<EAX>, br_pixelmap *pSrc@<EDX>, br_pixelmap *pPalette@<EBX>)
  1028. void Copy8BitTo16Bit(br_pixelmap* pDst, br_pixelmap* pSrc, br_pixelmap* pPalette) {
  1029.     //int x; // Pierre-Marie Baty -- unused variable
  1030.     //int y; // Pierre-Marie Baty -- unused variable
  1031.     //tU8* src_start; // Pierre-Marie Baty -- unused variable
  1032.     //tU16* dst_start; // Pierre-Marie Baty -- unused variable
  1033.     //tU16* palette_entry; // Pierre-Marie Baty -- unused variable
  1034.     LOG_TRACE("(%p, %p, %p)", pDst, pSrc, pPalette);
  1035.     NOT_IMPLEMENTED();
  1036. }
  1037.  
  1038. // IDA: void __usercall Copy8BitTo16BitRectangle(br_pixelmap *pDst@<EAX>, tS16 pDst_x@<EDX>, tS16 pDst_y@<EBX>, br_pixelmap *pSrc@<ECX>, tS16 pSrc_x, tS16 pSrc_y, tS16 pWidth, tS16 pHeight, br_pixelmap *pPalette)
  1039. void Copy8BitTo16BitRectangle(br_pixelmap* pDst, tS16 pDst_x, tS16 pDst_y, br_pixelmap* pSrc, tS16 pSrc_x, tS16 pSrc_y, tS16 pWidth, tS16 pHeight, br_pixelmap* pPalette) {
  1040.     //int x; // Pierre-Marie Baty -- unused variable
  1041.     //int y; // Pierre-Marie Baty -- unused variable
  1042.     //tU8* src_start; // Pierre-Marie Baty -- unused variable
  1043.     //tU16* dst_start; // Pierre-Marie Baty -- unused variable
  1044.     //tU16* palette_entry; // Pierre-Marie Baty -- unused variable
  1045.     LOG_TRACE("(%p, %d, %d, %p, %d, %d, %d, %d, %p)", pDst, pDst_x, pDst_y, pSrc, pSrc_x, pSrc_y, pWidth, pHeight, pPalette);
  1046.     NOT_IMPLEMENTED();
  1047. }
  1048.  
  1049. // IDA: void __usercall Copy8BitTo16BitRectangleWithTransparency(br_pixelmap *pDst@<EAX>, tS16 pDst_x@<EDX>, tS16 pDst_y@<EBX>, br_pixelmap *pSrc@<ECX>, tS16 pSrc_x, tS16 pSrc_y, tS16 pWidth, tS16 pHeight, br_pixelmap *pPalette)
  1050. void Copy8BitTo16BitRectangleWithTransparency(br_pixelmap* pDst, tS16 pDst_x, tS16 pDst_y, br_pixelmap* pSrc, tS16 pSrc_x, tS16 pSrc_y, tS16 pWidth, tS16 pHeight, br_pixelmap* pPalette) {
  1051.     //int x; // Pierre-Marie Baty -- unused variable
  1052.     //int y; // Pierre-Marie Baty -- unused variable
  1053.     //tU8* src_start; // Pierre-Marie Baty -- unused variable
  1054.     //tU16* dst_start; // Pierre-Marie Baty -- unused variable
  1055.     //tU16* palette_entry; // Pierre-Marie Baty -- unused variable
  1056.     LOG_TRACE("(%p, %d, %d, %p, %d, %d, %d, %d, %p)", pDst, pDst_x, pDst_y, pSrc, pSrc_x, pSrc_y, pWidth, pHeight, pPalette);
  1057.     NOT_IMPLEMENTED();
  1058. }
  1059.  
  1060. // IDA: void __usercall Copy8BitToOnscreen16BitRectangleWithTransparency(br_pixelmap *pDst@<EAX>, tS16 pDst_x@<EDX>, tS16 pDst_y@<EBX>, br_pixelmap *pSrc@<ECX>, tS16 pSrc_x, tS16 pSrc_y, tS16 pWidth, tS16 pHeight, br_pixelmap *pPalette)
  1061. void Copy8BitToOnscreen16BitRectangleWithTransparency(br_pixelmap* pDst, tS16 pDst_x, tS16 pDst_y, br_pixelmap* pSrc, tS16 pSrc_x, tS16 pSrc_y, tS16 pWidth, tS16 pHeight, br_pixelmap* pPalette) {
  1062.     //int x; // Pierre-Marie Baty -- unused variable
  1063.     //int y; // Pierre-Marie Baty -- unused variable
  1064.     //tU8* src_start; // Pierre-Marie Baty -- unused variable
  1065.     //tU16* dst_start; // Pierre-Marie Baty -- unused variable
  1066.     //tU16* palette_entry; // Pierre-Marie Baty -- unused variable
  1067.     LOG_TRACE("(%p, %d, %d, %p, %d, %d, %d, %d, %p)", pDst, pDst_x, pDst_y, pSrc, pSrc_x, pSrc_y, pWidth, pHeight, pPalette);
  1068.     NOT_IMPLEMENTED();
  1069. }
  1070.  
  1071. // IDA: void __usercall Copy8BitRectangleTo16BitRhombusWithTransparency(br_pixelmap *pDst@<EAX>, tS16 pDst_x@<EDX>, tS16 pDst_y@<EBX>, br_pixelmap *pSrc@<ECX>, tS16 pSrc_x, tS16 pSrc_y, tS16 pWidth, tS16 pHeight, tX1616 pShear, br_pixelmap *pPalette)
  1072. void Copy8BitRectangleTo16BitRhombusWithTransparency(br_pixelmap* pDst, tS16 pDst_x, tS16 pDst_y, br_pixelmap* pSrc, tS16 pSrc_x, tS16 pSrc_y, tS16 pWidth, tS16 pHeight, tX1616 pShear, br_pixelmap* pPalette) {
  1073.     //int x; // Pierre-Marie Baty -- unused variable
  1074.     //int y; // Pierre-Marie Baty -- unused variable
  1075.     //tU8* src_start; // Pierre-Marie Baty -- unused variable
  1076.     //tU16* dst_start; // Pierre-Marie Baty -- unused variable
  1077.     //tU16* palette_entry; // Pierre-Marie Baty -- unused variable
  1078.     //tX1616 total_shear; // Pierre-Marie Baty -- unused variable
  1079.     //tS16 sheared_x; // Pierre-Marie Baty -- unused variable
  1080.     //tS16 clipped_src_x; // Pierre-Marie Baty -- unused variable
  1081.     //tS16 clipped_width; // Pierre-Marie Baty -- unused variable
  1082.     LOG_TRACE("(%p, %d, %d, %p, %d, %d, %d, %d, %d, %p)", pDst, pDst_x, pDst_y, pSrc, pSrc_x, pSrc_y, pWidth, pHeight, pShear, pPalette);
  1083.     NOT_IMPLEMENTED();
  1084. }
  1085.  
  1086. // IDA: void __usercall DRPixelmapRectangleCopy(br_pixelmap *dst@<EAX>, br_int_16 dx@<EDX>, br_int_16 dy@<EBX>, br_pixelmap *src@<ECX>, br_int_16 sx, br_int_16 sy, br_uint_16 w, br_uint_16 h)
  1087. void DRPixelmapRectangleCopy(br_pixelmap* dst, br_int_16 dx, br_int_16 dy, br_pixelmap* src, br_int_16 sx, br_int_16 sy, br_uint_16 w, br_uint_16 h) {
  1088.     LOG_TRACE("(%p, %d, %d, %p, %d, %d, %d, %d)", dst, dx, dy, src, sx, sy, w, h);
  1089.  
  1090.     BrPixelmapRectangleCopy(dst, dx, dy, src, sx, sy, w, h);
  1091. }
  1092.  
  1093. // IDA: void __usercall DRPixelmapCopy(br_pixelmap *dst@<EAX>, br_pixelmap *src@<EDX>)
  1094. void DRPixelmapCopy(br_pixelmap* dst, br_pixelmap* src) {
  1095.     LOG_TRACE("(%p, %p)", dst, src);
  1096.  
  1097.     BrPixelmapCopy(dst, src);
  1098. }
  1099.  
  1100. // IDA: void __usercall DRPixelmapRectangleFill(br_pixelmap *dst@<EAX>, br_int_16 x@<EDX>, br_int_16 y@<EBX>, br_uint_16 w@<ECX>, br_uint_16 h, br_uint_32 colour)
  1101. void DRPixelmapRectangleFill(br_pixelmap* dst, br_int_16 x, br_int_16 y, br_uint_16 w, br_uint_16 h, br_uint_32 colour) {
  1102.     LOG_TRACE("(%p, %d, %d, %d, %d, %d)", dst, x, y, w, h, colour);
  1103.  
  1104.     BrPixelmapRectangleFill(dst, x, y, w, h, colour);
  1105. }
  1106.  
  1107. // IDA: int __usercall NormalSideOfPlane@<EAX>(br_vector3 *pPoint@<EAX>, br_vector3 *pNormal@<EDX>, br_scalar pD)
  1108. int NormalSideOfPlane(br_vector3* pPoint, br_vector3* pNormal, br_scalar pD) {
  1109.     //br_scalar numer; // Pierre-Marie Baty -- unused variable
  1110.     //br_scalar denom; // Pierre-Marie Baty -- unused variable
  1111.     LOG_TRACE("(%p, %p, %f)", pPoint, pNormal, pD);
  1112.  
  1113.     return (BrVector3Dot(pNormal, pPoint) - pD) >= 0.f;
  1114. }
  1115.  
  1116. // IDA: br_material* __usercall DRMaterialClone@<EAX>(br_material *pMaterial@<EAX>)
  1117. br_material* DRMaterialClone(br_material* pMaterial) {
  1118.     br_material* the_material;
  1119.     char s[256];
  1120.     static int name_suffix = 0;
  1121.     LOG_TRACE("(%p)", pMaterial);
  1122.  
  1123.     the_material = BrMaterialAllocate(NULL);
  1124.     the_material->flags = pMaterial->flags;
  1125.     the_material->ka = pMaterial->ka;
  1126.     the_material->kd = pMaterial->kd;
  1127.     the_material->ks = pMaterial->ks;
  1128.     the_material->power = pMaterial->power;
  1129.     the_material->colour = pMaterial->colour;
  1130.     the_material->index_base = pMaterial->index_base;
  1131.     the_material->index_range = pMaterial->index_range;
  1132.     the_material->index_shade = pMaterial->index_shade;
  1133.     the_material->index_blend = pMaterial->index_blend;
  1134.     the_material->colour_map = pMaterial->colour_map;
  1135.     memcpy(&the_material->map_transform, &pMaterial->map_transform, sizeof(the_material->map_transform));
  1136.     sprintf(s, "%s(%d)", pMaterial->identifier, name_suffix);
  1137.     name_suffix++;
  1138.     the_material->identifier = BrResAllocate(the_material, strlen(s) + 1, BR_MEMORY_STRING);
  1139.     strcpy(the_material->identifier, s);
  1140.     BrMaterialAdd(the_material);
  1141.     return the_material;
  1142. }
  1143.  
  1144. // IDA: void __usercall StripCR(char *s@<EAX>)
  1145. void StripCR(char* s) {
  1146.     char* pos;
  1147.  
  1148.     pos = s;
  1149.     while (*pos != '\0') {
  1150.         if (*pos == '\r' || *pos == '\n') {
  1151.             *pos = '\0';
  1152.             break;
  1153.         }
  1154.         pos++;
  1155.     }
  1156. }
  1157.  
  1158. // IDA: void __cdecl SubsStringJob(char *pStr, ...)
  1159. void SubsStringJob(char* pStr, ...) {
  1160.     //char* sub_str; // Pierre-Marie Baty -- unused variable
  1161.     //char temp_str[256]; // Pierre-Marie Baty -- unused variable
  1162.     //char* sub_pt; // Pierre-Marie Baty -- unused variable
  1163.     //va_list ap; // Pierre-Marie Baty -- unused variable
  1164.     LOG_TRACE("(\"%s\")", pStr);
  1165.     NOT_IMPLEMENTED();
  1166. }
  1167.  
  1168. // IDA: void __usercall DecodeLine2(char *pS@<EAX>)
  1169. void DecodeLine2(char* pS) {
  1170.     int len;
  1171.     int seed;
  1172.     int i;
  1173.     unsigned char c;
  1174.     char* key;
  1175.  
  1176.     len = strlen(pS);
  1177.     key = (char*)gLong_key;
  1178.     while (len > 0 && (pS[len - 1] == '\r' || pS[len - 1] == '\n')) {
  1179.         len--;
  1180.         pS[len] = '\0';
  1181.     }
  1182.     seed = len % 16;
  1183.     for (i = 0; i < len; i++) {
  1184.         c = pS[i];
  1185.         if (i >= 2) {
  1186.             if (pS[i - 1] == '/' && pS[i - 2] == '/') {
  1187.                 key = (char*)gOther_long_key;
  1188.             }
  1189.         }
  1190.         if (gEncryption_method == 1) {
  1191.             if (c == '\t') {
  1192.                 c = 0x9f;
  1193.             }
  1194.  
  1195.             c -= 0x20;
  1196.             c ^= key[seed];
  1197.             c &= 0x7f;
  1198.             c += 0x20;
  1199.  
  1200.             seed += 7;
  1201.             seed %= 16;
  1202.  
  1203.             if (c == 0x9f) {
  1204.                 c = '\t';
  1205.             }
  1206.         } else {
  1207.             if (c == '\t') {
  1208.                 c = 0x80;
  1209.             }
  1210.             c -= 0x20;
  1211.             if ((c & 0x80) == 0) {
  1212.                 c ^= key[seed] & 0x7f;
  1213.             }
  1214.             c += 0x20;
  1215.  
  1216.             seed += 7;
  1217.             seed %= 16;
  1218.  
  1219.             if (c == 0x80) {
  1220.                 c = '\t';
  1221.             }
  1222.         }
  1223.         pS[i] = c;
  1224.     }
  1225. }
  1226.  
  1227. // IDA: void __usercall EncodeLine2(char *pS@<EAX>)
  1228. void EncodeLine2(char* pS) {
  1229.     int len;
  1230.     int seed;
  1231.     int i;
  1232.     int count;
  1233.     unsigned char c;
  1234.     char* key;
  1235.  
  1236.     len = strlen(pS);
  1237.     count = 0;
  1238.     key = (char*)gLong_key;
  1239.     while (len > 0 && (pS[len - 1] == '\r' || pS[len - 1] == '\n')) {
  1240.         len--;
  1241.         pS[len] = '\0';
  1242.     }
  1243.  
  1244.     seed = len % 16;
  1245.  
  1246.     for (i = 0; i < len; i++) {
  1247.         if (count == 2) {
  1248.             key = (char*)gOther_long_key;
  1249.         }
  1250.         if (pS[i] == '/') {
  1251.             count++;
  1252.         } else {
  1253.             count = 0;
  1254.         }
  1255.         if (pS[i] == '\t') {
  1256.             pS[i] = 0x80;
  1257.         }
  1258.  
  1259.         c = pS[i] - 0x20;
  1260.         if ((c & 0x80) == 0) {
  1261.             c ^= key[seed] & 0x7f;
  1262.         }
  1263.         c += 0x20;
  1264.  
  1265.         seed += 7;
  1266.         seed %= 16;
  1267.  
  1268.         if (c == 0x80) {
  1269.             c = '\t';
  1270.         }
  1271.         pS[i] = c;
  1272.     }
  1273. }
  1274.  
  1275. // IDA: void __usercall EncodeFile(char *pThe_path@<EAX>)
  1276. void EncodeFile(char* pThe_path) {
  1277.     FILE* f;
  1278.     FILE* d;
  1279.     char line[257];
  1280.     char new_file[256];
  1281.     char* s;
  1282.     char* result;
  1283.     int ch;
  1284.     int decode;
  1285.     int len;
  1286.     int count;
  1287.     LOG_TRACE("(\"%s\")", pThe_path);
  1288.  
  1289.     len = strlen(pThe_path);
  1290.     strcpy(new_file, pThe_path);
  1291.     strcpy(&new_file[len - 3], "ENC");
  1292.  
  1293.     f = fopen(pThe_path, "rt");
  1294.     if (f == NULL) {
  1295.         FatalError(kFatalError_Open_S, pThe_path);
  1296.     }
  1297.  
  1298.     ch = fgetc(f);
  1299.     ungetc(ch, f);
  1300.  
  1301.     if (gDecode_thing == '@' && gDecode_thing == (char)ch) {
  1302.         fclose(f);
  1303.         return;
  1304.     }
  1305.  
  1306.     d = fopen(new_file, "wb");
  1307.     if (d == NULL) {
  1308.         FatalError(kFatalError_Open_S, new_file);
  1309.     }
  1310.  
  1311.     result = &line[1];
  1312.  
  1313.     while (!feof(f)) {
  1314.         s = fgets(result, 256, f);
  1315.  
  1316.         if (s == NULL) {
  1317.             continue;
  1318.         }
  1319.  
  1320.         if (result[0] == '@') {
  1321.             decode = 1;
  1322.         } else {
  1323.             decode = 0;
  1324.             // Strip leading whitespace
  1325.             while (result[0] == ' ' || result[0] == '\t') {
  1326.                 memmove(result, &result[1], strlen(result));
  1327.             }
  1328.         }
  1329.  
  1330.         if (decode) {
  1331.             DecodeLine2(&result[decode]);
  1332.         } else {
  1333.             EncodeLine2(&result[decode]);
  1334.         }
  1335.  
  1336.         line[0] = '@';
  1337.         fputs(&line[decode * 2], d);
  1338.         count = -1;
  1339.         while (1) {
  1340.             count++;
  1341.             ch = fgetc(f);
  1342.             if (ch != '\r' && ch != '\n') {
  1343.                 break;
  1344.             }
  1345.         }
  1346.         if (count > 2) {
  1347.             fputc('\r', d);
  1348.             fputc('\n', d);
  1349.         }
  1350.         fputc('\r', d);
  1351.         fputc('\n', d);
  1352.  
  1353.         if (ch != -1) {
  1354.             ungetc(ch, f);
  1355.         }
  1356.     }
  1357.     fclose(f);
  1358.     fclose(d);
  1359.  
  1360.     PDFileUnlock(pThe_path);
  1361.     remove(pThe_path);
  1362.     rename(new_file, pThe_path);
  1363. }
  1364.  
  1365. // IDA: void __usercall EncodeFileWrapper(char *pThe_path@<EAX>)
  1366. void EncodeFileWrapper(char* pThe_path) {
  1367.     int len;
  1368.     LOG_TRACE("(\"%s\")", pThe_path);
  1369.  
  1370.     len = strlen(pThe_path);
  1371.  
  1372.     // if file doesn't end in .txt, bail out
  1373.     if (STR_ENDS_WITH(pThe_path, ".TXT") != 0) {
  1374.         return;
  1375.     }
  1376.     if (STR_ENDS_WITH(pThe_path, "DKEYMAP0.TXT") == 0) {
  1377.         return;
  1378.     }
  1379.     if (STR_ENDS_WITH(pThe_path, "DKEYMAP1.TXT") == 0) {
  1380.         return;
  1381.     }
  1382.     if (STR_ENDS_WITH(pThe_path, "DKEYMAP2.TXT") == 0) {
  1383.         return;
  1384.     }
  1385.     if (STR_ENDS_WITH(pThe_path, "DKEYMAP3.TXT") == 0) {
  1386.         return;
  1387.     }
  1388.     if (STR_ENDS_WITH(pThe_path, "KEYMAP_0.TXT") == 0) {
  1389.         return;
  1390.     }
  1391.     if (STR_ENDS_WITH(pThe_path, "KEYMAP_1.TXT") == 0) {
  1392.         return;
  1393.     }
  1394.     if (STR_ENDS_WITH(pThe_path, "KEYMAP_2.TXT") == 0) {
  1395.         return;
  1396.     }
  1397.     if (STR_ENDS_WITH(pThe_path, "KEYMAP_3.TXT") == 0) {
  1398.         return;
  1399.     }
  1400.     if (STR_ENDS_WITH(pThe_path, "OPTIONS.TXT") == 0) {
  1401.         return;
  1402.     }
  1403.     if (STR_ENDS_WITH(pThe_path, "KEYNAMES.TXT") == 0) {
  1404.         return;
  1405.     }
  1406.     if (STR_ENDS_WITH(pThe_path, "KEYMAP.TXT") == 0) {
  1407.         return;
  1408.     }
  1409.     if (STR_ENDS_WITH(pThe_path, "PATHS.TXT") == 0) {
  1410.         return;
  1411.     }
  1412.  
  1413.     EncodeFile(pThe_path);
  1414. }
  1415.  
  1416. // IDA: void __usercall EncodeAllFilesInDirectory(char *pThe_path@<EAX>)
  1417. void EncodeAllFilesInDirectory(char* pThe_path) {
  1418.     char s[256];
  1419.     LOG_TRACE("(\"%s\")", pThe_path);
  1420.  
  1421.     PathCat(s, gApplication_path, pThe_path);
  1422.     PDForEveryFile(s, EncodeFileWrapper);
  1423. }
  1424.  
  1425. // IDA: void __usercall SkipNLines(FILE *pF@<EAX>)
  1426. void SkipNLines(FILE* pF) {
  1427.     int i;
  1428.     int count;
  1429.     char s[256];
  1430.     LOG_TRACE("(%p)", pF);
  1431.  
  1432.     count = GetAnInt(pF);
  1433.     for (i = 0; i < count; i++) {
  1434.         GetALineAndDontArgue(pF, s);
  1435.     }
  1436. }
  1437.  
  1438. // IDA: int __usercall DRStricmp@<EAX>(char *p1@<EAX>, char *p2@<EDX>)
  1439. int DRStricmp(char* p1, char* p2) {
  1440.     int val;
  1441.     while (p1) {
  1442.         val = tolower(*p1) - tolower(*p2);
  1443.         if (val != 0) {
  1444.             return val;
  1445.         }
  1446.         p1++;
  1447.         p2++;
  1448.     }
  1449.     return 0;
  1450. }
  1451.  
  1452. // IDA: void __usercall GlorifyMaterial(br_material **pArray@<EAX>, int pCount@<EDX>)
  1453. void GlorifyMaterial(br_material** pArray, int pCount) {
  1454.     //int i; // Pierre-Marie Baty -- unused variable
  1455.     //int c; // Pierre-Marie Baty -- unused variable
  1456.     //br_pixelmap* big_tile; // Pierre-Marie Baty -- unused variable
  1457.     //tException_list e; // Pierre-Marie Baty -- unused variable
  1458.     LOG_TRACE("(%p, %d)", pArray, pCount);
  1459.     NOT_IMPLEMENTED();
  1460. }
  1461.  
  1462. // IDA: void __usercall WhitenVertexRGB(br_model **pArray@<EAX>, int pN@<EDX>)
  1463. void WhitenVertexRGB(br_model** pArray, int pN) {
  1464.     //int m; // Pierre-Marie Baty -- unused variable
  1465.     //int v; // Pierre-Marie Baty -- unused variable
  1466.     //br_vertex* vertex; // Pierre-Marie Baty -- unused variable
  1467.     LOG_TRACE("(%p, %d)", pArray, pN);
  1468.     NOT_IMPLEMENTED();
  1469. }
  1470.  
  1471. // IDA: void __usercall NobbleNonzeroBlacks(br_pixelmap *pPalette@<EAX>)
  1472. void NobbleNonzeroBlacks(br_pixelmap* pPalette) {
  1473.     //tU32 red; // Pierre-Marie Baty -- unused variable
  1474.     //tU32 green; // Pierre-Marie Baty -- unused variable
  1475.     //tU32 blue; // Pierre-Marie Baty -- unused variable
  1476.     //tU32 value; // Pierre-Marie Baty -- unused variable
  1477.     //tU32* palette_entry; // Pierre-Marie Baty -- unused variable
  1478.     //tU32 frobbed; // Pierre-Marie Baty -- unused variable
  1479.     LOG_TRACE("(%p)", pPalette);
  1480.     NOT_IMPLEMENTED();
  1481. }
  1482.  
  1483. // IDA: int __usercall PDCheckDriveExists@<EAX>(char *pThe_path@<EAX>)
  1484. int PDCheckDriveExists(char* pThe_path) {
  1485.     LOG_TRACE9("(\"%s\")", pThe_path);
  1486.  
  1487.     return PDCheckDriveExists2(pThe_path, NULL, 0);
  1488. }
  1489.  
  1490. // IDA: int __usercall OpacityInPrims@<EAX>(br_token_value *pPrims@<EAX>)
  1491. int OpacityInPrims(br_token_value* pPrims) {
  1492.     LOG_TRACE("(%p)", pPrims);
  1493.  
  1494.     for (; pPrims->t != 0 && pPrims->t != BRT_OPACITY_X; pPrims++) {
  1495.     }
  1496.     return pPrims->t != 0;
  1497. }
  1498.  
  1499. // IDA: int __usercall AlreadyBlended@<EAX>(br_material *pMaterial@<EAX>)
  1500. int AlreadyBlended(br_material* pMaterial) {
  1501.     LOG_TRACE("(%p)", pMaterial);
  1502.  
  1503.     if (pMaterial->index_blend != NULL) {
  1504.         return 1;
  1505.     }
  1506.     if (pMaterial->extra_prim == NULL) {
  1507.         return 0;
  1508.     }
  1509.     return OpacityInPrims(pMaterial->extra_prim);
  1510. }
  1511.  
  1512. // IDA: void __usercall BlendifyMaterialTablishly(br_material *pMaterial@<EAX>, int pPercent@<EDX>)
  1513. void BlendifyMaterialTablishly(br_material* pMaterial, int pPercent) {
  1514.     char* s = NULL;
  1515.     LOG_TRACE("(%p, %d)", pMaterial, pPercent);
  1516.  
  1517.     switch (pPercent) {
  1518.     case 25:
  1519.         s = "BLEND75.TAB";
  1520.         break;
  1521.     case 50:
  1522.         s = "BLEND50.TAB";
  1523.         break;
  1524.     case 75:
  1525.         s = "BLEND25.TAB";
  1526.         break;
  1527.     default:
  1528.         PDFatalError("Invalid alpha");
  1529.         break;
  1530.     }
  1531.     pMaterial->index_blend = BrTableFind(s);
  1532.     if (pMaterial->index_blend == NULL) {
  1533.         pMaterial->index_blend = LoadSingleShadeTable(&gTrack_storage_space, s);
  1534.     }
  1535. }
  1536.  
  1537. // IDA: void __usercall BlendifyMaterialPrimitively(br_material *pMaterial@<EAX>, int pPercent@<EDX>)
  1538. void BlendifyMaterialPrimitively(br_material* pMaterial, int pPercent) {
  1539.  
  1540.     static br_token_value alpha25[3] = {
  1541.         { BRT_BLEND_B, { .b = 1 } },
  1542.         { BRT_OPACITY_X, { .x = 0x400000 } },
  1543.         { 0 },
  1544.     };
  1545.     static br_token_value alpha50[3] = {
  1546.         { BRT_BLEND_B, { .b = 1 } },
  1547.         { BRT_OPACITY_X, { .x = 0x800000 } },
  1548.         { 0 },
  1549.     };
  1550.     static br_token_value alpha75[3] = {
  1551.         { BRT_BLEND_B, { .b = 1 } },
  1552.         { BRT_OPACITY_X, { .x = 0xc00000 } },
  1553.         { 0 },
  1554.     };
  1555.     LOG_TRACE("(%p, %d)", pMaterial, pPercent);
  1556.  
  1557.     switch (pPercent) {
  1558.     case 25:
  1559.         pMaterial->extra_prim = alpha25;
  1560.         break;
  1561.     case 50:
  1562.         pMaterial->extra_prim = alpha50;
  1563.         break;
  1564.     case 75:
  1565.         pMaterial->extra_prim = alpha75;
  1566.         break;
  1567.     default:
  1568.         PDFatalError("Invalid alpha");
  1569.     }
  1570. }
  1571.  
  1572. // IDA: void __usercall BlendifyMaterial(br_material *pMaterial@<EAX>, int pPercent@<EDX>)
  1573. void BlendifyMaterial(br_material* pMaterial, int pPercent) {
  1574.     LOG_TRACE("(%p, %d)", pMaterial, pPercent);
  1575.  
  1576.     if (gScreen->type == BR_PMT_INDEX_8) {
  1577.         BlendifyMaterialTablishly(pMaterial, pPercent);
  1578.     } else {
  1579.         BlendifyMaterialPrimitively(pMaterial, pPercent);
  1580.     }
  1581. }
  1582.