Subversion Repositories Games.Carmageddon

Rev

Rev 8 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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