Subversion Repositories eduke32

Rev

Rev 4127 | Rev 4408 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2542 helixhorne 1
//
2
// Common non-engine code/data for EDuke32 and Mapster32
3
//
4
 
5
#include "compat.h"
2726 hendricks2 6
#include "build.h"
2549 helixhorne 7
#include "scriptfile.h"
2554 helixhorne 8
#include "cache1d.h"
9
#include "kplib.h"
10
#include "baselayer.h"
3220 hendricks2 11
#include "names.h"
2542 helixhorne 12
 
3581 hendricks2 13
#ifdef _WIN32
14
# include "winbits.h"
3671 hendricks2 15
# include <shlwapi.h>
16
# include <winnt.h>
17
# ifndef KEY_WOW64_32KEY
18
#  define KEY_WOW64_32KEY 0x0200
19
# endif
3581 hendricks2 20
#endif
21
 
2542 helixhorne 22
#include "common.h"
2726 hendricks2 23
#include "common_game.h"
2542 helixhorne 24
 
2789 hendricks2 25
int32_t g_gameType = GAMEFLAG_DUKE;
2542 helixhorne 26
 
2726 hendricks2 27
// grp/con/def handling
28
 
2796 helixhorne 29
const char *defaultgamegrp[GAMECOUNT] = { "DUKE3D.GRP", "NAM.GRP", "NAPALM.GRP", "WW2GI.GRP" };
30
const char *defaultdeffilename[GAMECOUNT] = { "duke3d.def", "nam.def", "napalm.def", "ww2gi.def" };
31
const char *defaultconfilename = "GAME.CON";
32
const char *defaultgameconfilename[GAMECOUNT] = { "EDUKE.CON", "NAM.CON", "NAPALM.CON", "WW2GI.CON" };
2726 hendricks2 33
 
34
// g_grpNamePtr can ONLY point to a malloc'd block (length BMAX_PATH)
35
char *g_grpNamePtr = NULL;
36
// g_defNamePtr can ONLY point to a malloc'd block (length BMAX_PATH)
37
char *g_defNamePtr = NULL;
38
// g_scriptNamePtr can ONLY point to a malloc'd block (length BMAX_PATH)
39
char *g_scriptNamePtr = NULL;
40
 
41
void clearGrpNamePtr(void)
42
{
43
    if (g_grpNamePtr != NULL)
44
        Bfree(g_grpNamePtr);
45
    // g_grpNamePtr assumed to be assigned to right after
46
}
47
 
48
void clearDefNamePtr(void)
49
{
50
    if (g_defNamePtr != NULL)
51
        Bfree(g_defNamePtr);
52
    // g_defNamePtr assumed to be assigned to right after
53
}
54
 
55
void clearScriptNamePtr(void)
56
{
57
    if (g_scriptNamePtr != NULL)
58
        Bfree(g_scriptNamePtr);
59
    // g_scriptNamePtr assumed to be assigned to right after
60
}
61
 
2796 helixhorne 62
const char *G_DefaultGrpFile(void)
2726 hendricks2 63
{
64
    if (DUKE)
65
        return defaultgamegrp[GAME_DUKE];
66
    // order is important for the following three because GAMEFLAG_NAM overlaps all
67
    else if (NAPALM)
68
        return defaultgamegrp[GAME_NAPALM];
69
    else if (WW2GI)
70
        return defaultgamegrp[GAME_WW2GI];
71
    else if (NAM)
72
        return defaultgamegrp[GAME_NAM];
73
 
74
    return defaultgamegrp[0];
75
}
2796 helixhorne 76
const char *G_DefaultDefFile(void)
2726 hendricks2 77
{
78
    if (DUKE)
79
        return defaultdeffilename[GAME_DUKE];
80
    else if (WW2GI)
81
        return defaultdeffilename[GAME_WW2GI];
82
    else if (NAPALM)
83
    {
2752 helixhorne 84
        if (!testkopen(defaultdeffilename[GAME_NAPALM],0) && testkopen(defaultdeffilename[GAME_NAM],0))
3618 hendricks2 85
            return defaultdeffilename[GAME_NAM]; // NAM/NAPALM Sharing
2726 hendricks2 86
        else
87
            return defaultdeffilename[GAME_NAPALM];
88
    }
89
    else if (NAM)
90
    {
2752 helixhorne 91
        if (!testkopen(defaultdeffilename[GAME_NAM],0) && testkopen(defaultdeffilename[GAME_NAPALM],0))
3618 hendricks2 92
            return defaultdeffilename[GAME_NAPALM]; // NAM/NAPALM Sharing
2726 hendricks2 93
        else
94
            return defaultdeffilename[GAME_NAM];
95
    }
96
 
97
    return defaultdeffilename[0];
98
}
2796 helixhorne 99
const char *G_DefaultConFile(void)
2726 hendricks2 100
{
2752 helixhorne 101
    if (DUKE && testkopen(defaultgameconfilename[GAME_DUKE],0))
2726 hendricks2 102
        return defaultgameconfilename[GAME_DUKE];
2752 helixhorne 103
    else if (WW2GI && testkopen(defaultgameconfilename[GAME_WW2GI],0))
2726 hendricks2 104
        return defaultgameconfilename[GAME_WW2GI];
105
    else if (NAPALM)
106
    {
2752 helixhorne 107
        if (!testkopen(defaultgameconfilename[GAME_NAPALM],0))
2726 hendricks2 108
        {
2752 helixhorne 109
            if (testkopen(defaultgameconfilename[GAME_NAM],0))
3618 hendricks2 110
                return defaultgameconfilename[GAME_NAM]; // NAM/NAPALM Sharing
2726 hendricks2 111
        }
112
        else
113
            return defaultgameconfilename[GAME_NAPALM];
114
    }
115
    else if (NAM)
116
    {
2752 helixhorne 117
        if (!testkopen(defaultgameconfilename[GAME_NAM],0))
2726 hendricks2 118
        {
2752 helixhorne 119
            if (testkopen(defaultgameconfilename[GAME_NAPALM],0))
3618 hendricks2 120
                return defaultgameconfilename[GAME_NAPALM]; // NAM/NAPALM Sharing
2726 hendricks2 121
        }
122
        else
123
            return defaultgameconfilename[GAME_NAM];
124
    }
125
 
126
    return defaultconfilename;
127
}
128
 
2796 helixhorne 129
const char *G_GrpFile(void)
2726 hendricks2 130
{
131
    if (g_grpNamePtr == NULL)
132
        return G_DefaultGrpFile();
133
    else
134
        return g_grpNamePtr;
135
}
3654 terminx 136
 
2796 helixhorne 137
const char *G_DefFile(void)
2726 hendricks2 138
{
139
    if (g_defNamePtr == NULL)
140
        return G_DefaultDefFile();
141
    else
142
        return g_defNamePtr;
143
}
3654 terminx 144
 
2796 helixhorne 145
const char *G_ConFile(void)
2726 hendricks2 146
{
147
    if (g_scriptNamePtr == NULL)
148
        return G_DefaultConFile();
149
    else
150
        return g_scriptNamePtr;
151
}
152
 
153
//////////
154
 
3976 helixhorne 155
#define NUMPSKYMULTIS 5
156
EDUKE32_STATIC_ASSERT(NUMPSKYMULTIS <= MAXPSKYMULTIS);
157
EDUKE32_STATIC_ASSERT(PSKYOFF_MAX <= MAXPSKYTILES);
158
 
3975 helixhorne 159
// Set up new-style multi-psky handling.
3976 helixhorne 160
void G_InitMultiPsky(int32_t CLOUDYOCEAN__DYN, int32_t MOONSKY1__DYN, int32_t BIGORBIT1__DYN, int32_t LA__DYN)
3220 hendricks2 161
{
162
    int32_t i;
163
 
3976 helixhorne 164
    psky_t *defaultsky = &multipsky[0];
165
    psky_t *oceansky = &multipsky[1];
166
    psky_t *moonsky = &multipsky[2];
167
    psky_t *spacesky = &multipsky[3];
168
    psky_t *citysky = &multipsky[4];
3220 hendricks2 169
 
3975 helixhorne 170
    static int32_t inited;
171
    if (inited)
172
        return;
173
    inited = 1;
3220 hendricks2 174
 
3976 helixhorne 175
    multipskytile[0] = -1;
176
    multipskytile[1] = CLOUDYOCEAN__DYN;
177
    multipskytile[2] = MOONSKY1__DYN;
178
    multipskytile[3] = BIGORBIT1__DYN;
179
    multipskytile[4] = LA__DYN;
3220 hendricks2 180
 
3976 helixhorne 181
    pskynummultis = NUMPSKYMULTIS;
3220 hendricks2 182
 
3975 helixhorne 183
    // When adding other multi-skies, take care that the tileofs[] values are
184
    // <= PSKYOFF_MAX. (It can be increased up to MAXPSKYTILES, but should be
185
    // set as tight as possible.)
186
 
3976 helixhorne 187
    // The default sky properties (all others are implicitly zero):
188
    defaultsky->lognumtiles = 3;
189
    defaultsky->horizfrac = 32768;
190
 
191
    // CLOUDYOCEAN
192
    // Aligns with the drawn scene horizon because it has one itself.
193
    oceansky->lognumtiles = 3;
194
    oceansky->horizfrac = 65536;
195
 
3220 hendricks2 196
    // MOONSKY1
197
    //        earth          mountain   mountain         sun
3975 helixhorne 198
    moonsky->lognumtiles = 3;
199
    moonsky->horizfrac = 32768;
200
    moonsky->tileofs[6] = 1;
201
    moonsky->tileofs[1] = 2;
202
    moonsky->tileofs[4] = 2;
203
    moonsky->tileofs[2] = 3;
3220 hendricks2 204
 
205
    // BIGORBIT1   // orbit
206
    //       earth1         2           3           moon/sun
3975 helixhorne 207
    spacesky->lognumtiles = 3;
208
    spacesky->horizfrac = 32768;
209
    spacesky->tileofs[5] = 1;
210
    spacesky->tileofs[6] = 2;
211
    spacesky->tileofs[7] = 3;
212
    spacesky->tileofs[2] = 4;
3220 hendricks2 213
 
214
    // LA // la city
215
    //       earth1         2           3           moon/sun
3975 helixhorne 216
    citysky->lognumtiles = 3;
217
    citysky->horizfrac = 16384+1024;
218
    citysky->tileofs[0] = 1;
219
    citysky->tileofs[1] = 2;
220
    citysky->tileofs[2] = 1;
221
    citysky->tileofs[3] = 3;
222
    citysky->tileofs[4] = 4;
223
    citysky->tileofs[5] = 0;
224
    citysky->tileofs[6] = 2;
225
    citysky->tileofs[7] = 3;
3220 hendricks2 226
 
3975 helixhorne 227
    for (i=0; i<pskynummultis; ++i)
228
    {
229
        int32_t j;
230
        for (j=0; j<(1<<multipsky[i].lognumtiles); ++j)
231
            Bassert(multipsky[i].tileofs[j] <= PSKYOFF_MAX);
232
    }
3976 helixhorne 233
}
3220 hendricks2 234
 
3976 helixhorne 235
void G_SetupGlobalPsky(void)
236
{
237
    int32_t i, mskyidx=0;
238
 
239
    // NOTE: Loop must be running backwards for the same behavior as the game
240
    // (greatest sector index with matching parallaxed sky takes precedence).
241
    for (i=numsectors-1; i>=0; i--)
242
    {
243
        if (sector[i].ceilingstat & 1)
244
        {
4006 helixhorne 245
            mskyidx = getpskyidx(sector[i].ceilingpicnum);
3976 helixhorne 246
            if (mskyidx > 0)
247
                break;
248
        }
249
    }
250
 
251
    g_pskyidx = mskyidx;
3220 hendricks2 252
}
253
 
254
//////////
255
 
3582 hendricks2 256
#ifdef GEKKO
257
#include "gctypes.h" // for bool
258
void L2Enhance();
259
void CON_EnableGecko(int channel,int safe);
260
bool fatInit (uint32_t cacheSize, bool setAsDefaultDevice);
261
#endif
262
 
263
void G_ExtPreInit(void)
264
{
265
#ifdef GEKKO
266
    L2Enhance();
267
    CON_EnableGecko(1, 1);
268
    Bprintf("Console started\n");
269
    fatInit(28, true);
270
#endif
271
}
272
 
3622 terminx 273
#ifdef _WIN32
3637 terminx 274
const char * G_GetInstallPath(int32_t insttype)
3622 terminx 275
{
3637 terminx 276
    static char spath[NUMINSTPATHS][BMAX_PATH];
277
    static int32_t success[NUMINSTPATHS] = { -1, -1 };
3622 terminx 278
    int32_t siz = BMAX_PATH;
279
 
3637 terminx 280
    if (success[insttype] == -1)
281
    {
3671 hendricks2 282
        HKEY HKLM32;
283
        LONG keygood = RegOpenKeyEx(HKEY_LOCAL_MACHINE, NULL, 0, KEY_READ | KEY_WOW64_32KEY, &HKLM32);
284
        // KEY_WOW64_32KEY gets us around Wow6432Node on 64-bit builds
285
 
286
        if (keygood == ERROR_SUCCESS)
4127 hendricks2 287
        {
3671 hendricks2 288
            switch (insttype)
289
            {
290
            case INSTPATH_STEAM:
291
                success[insttype] = SHGetValueA(HKLM32, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Steam App 225140", "InstallLocation", NULL, spath[insttype], (LPDWORD)&siz);
292
                break;
293
            case INSTPATH_GOG:
294
                success[insttype] = SHGetValueA(HKLM32, "SOFTWARE\\GOG.com\\GOGDUKE3D", "PATH", NULL, spath[insttype], (LPDWORD)&siz);
295
                break;
296
            }
4127 hendricks2 297
 
298
            RegCloseKey(HKLM32);
299
        }
3637 terminx 300
    }
3622 terminx 301
 
3637 terminx 302
    if (success[insttype] == ERROR_SUCCESS)
303
        return spath[insttype];
3622 terminx 304
 
305
    return NULL;
306
}
307
#endif
308
 
3581 hendricks2 309
void G_AddSearchPaths(void)
310
{
311
#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
312
    addsearchpath("/usr/share/games/jfduke3d");
313
    addsearchpath("/usr/local/share/games/jfduke3d");
314
    addsearchpath("/usr/share/games/eduke32");
315
    addsearchpath("/usr/local/share/games/eduke32");
316
#elif defined(__APPLE__)
317
    addsearchpath("/Library/Application Support/JFDuke3D");
318
    addsearchpath("/Library/Application Support/EDuke32");
319
#elif defined (_WIN32)
3615 terminx 320
    // detect Steam and GOG versions of Duke3D
321
    char buf[BMAX_PATH];
3671 hendricks2 322
    const char* instpath;
3615 terminx 323
 
3671 hendricks2 324
    if ((instpath = G_GetInstallPath(INSTPATH_STEAM)))
3622 terminx 325
    {
3671 hendricks2 326
        Bsprintf(buf, "%s/gameroot", instpath);
3622 terminx 327
        addsearchpath(buf);
3615 terminx 328
 
3671 hendricks2 329
        Bsprintf(buf, "%s/gameroot/addons", instpath);
3622 terminx 330
        addsearchpath(buf);
3615 terminx 331
    }
332
 
3671 hendricks2 333
    if ((instpath = G_GetInstallPath(INSTPATH_GOG)))
334
        addsearchpath(instpath);
3581 hendricks2 335
#endif
336
}
337
 
3654 terminx 338
void G_CleanupSearchPaths(void)
339
{
340
#ifdef _WIN32
341
    char buf[BMAX_PATH];
3671 hendricks2 342
    const char* instpath;
3654 terminx 343
 
3671 hendricks2 344
    if ((instpath = G_GetInstallPath(INSTPATH_STEAM)))
3654 terminx 345
    {
3671 hendricks2 346
        Bsprintf(buf, "%s/gameroot", instpath);
3654 terminx 347
        removesearchpath(buf);
348
 
3671 hendricks2 349
        Bsprintf(buf, "%s/gameroot/addons", instpath);
3654 terminx 350
        removesearchpath(buf);
351
    }
352
 
3671 hendricks2 353
    if ((instpath = G_GetInstallPath(INSTPATH_GOG)))
354
        removesearchpath(instpath);
3654 terminx 355
#endif
356
}
357
 
3581 hendricks2 358
//////////
359
 
2542 helixhorne 360
struct strllist *CommandPaths, *CommandGrps;
361
 
4128 hendricks2 362
char **g_scriptModules = NULL;
363
int32_t g_scriptModulesNum = 0;
364
char **g_defModules = NULL;
365
int32_t g_defModulesNum = 0;
366
 
367
#ifdef HAVE_CLIPSHAPE_FEATURE
368
char **g_clipMapFiles = NULL;
369
int32_t g_clipMapFilesNum = 0;
370
#endif
371
 
2542 helixhorne 372
void G_AddGroup(const char *buffer)
373
{
374
    char buf[BMAX_PATH];
375
 
3176 helixhorne 376
    struct strllist *s = (struct strllist *)Bcalloc(1,sizeof(struct strllist));
2542 helixhorne 377
 
378
    Bstrcpy(buf, buffer);
379
 
380
    if (Bstrchr(buf,'.') == 0)
381
        Bstrcat(buf,".grp");
382
 
383
    s->str = Bstrdup(buf);
384
 
385
    if (CommandGrps)
386
    {
387
        struct strllist *t;
388
        for (t = CommandGrps; t->next; t=t->next) ;
389
        t->next = s;
390
        return;
391
    }
392
    CommandGrps = s;
393
}
394
 
395
void G_AddPath(const char *buffer)
396
{
3176 helixhorne 397
    struct strllist *s = (struct strllist *)Bcalloc(1,sizeof(struct strllist));
2542 helixhorne 398
    s->str = Bstrdup(buffer);
399
 
400
    if (CommandPaths)
401
    {
402
        struct strllist *t;
403
        for (t = CommandPaths; t->next; t=t->next) ;
404
        t->next = s;
405
        return;
406
    }
407
    CommandPaths = s;
408
}
2549 helixhorne 409
 
4128 hendricks2 410
void G_AddDef(const char *buffer)
411
{
412
    clearDefNamePtr();
413
    g_defNamePtr = dup_filename(buffer);
414
    initprintf("Using DEF file \"%s\".\n",g_defNamePtr);
415
}
416
 
417
void G_AddDefModule(const char *buffer)
418
{
419
    g_defModules = (char **) Brealloc (g_defModules, (g_defModulesNum+1) * sizeof(char *));
420
    g_defModules[g_defModulesNum] = Bstrdup(buffer);
421
    ++g_defModulesNum;
422
}
423
 
424
#ifdef HAVE_CLIPSHAPE_FEATURE
425
void G_AddClipMap(const char *buffer)
426
{
427
    g_clipMapFiles = (char **) Brealloc (g_clipMapFiles, (g_clipMapFilesNum+1) * sizeof(char *));
428
    g_clipMapFiles[g_clipMapFilesNum] = Bstrdup(buffer);
429
    ++g_clipMapFilesNum;
430
}
431
#endif
432
 
433
void G_AddCon(const char *buffer)
434
{
435
    clearScriptNamePtr();
436
    g_scriptNamePtr = dup_filename(buffer);
437
    initprintf("Using CON file \"%s\".\n",g_scriptNamePtr);
438
}
439
 
440
void G_AddConModule(const char *buffer)
441
{
442
    g_scriptModules = (char **) Brealloc (g_scriptModules, (g_scriptModulesNum+1) * sizeof(char *));
443
    g_scriptModules[g_scriptModulesNum] = Bstrdup(buffer);
444
    ++g_scriptModulesNum;
445
}
446
 
2549 helixhorne 447
//////////
448
 
449
int32_t getatoken(scriptfile *sf, const tokenlist *tl, int32_t ntokens)
450
{
451
    char *tok;
452
    int32_t i;
453
 
454
    if (!sf) return T_ERROR;
455
    tok = scriptfile_gettoken(sf);
456
    if (!tok) return T_EOF;
457
 
458
    for (i=ntokens-1; i>=0; i--)
459
    {
460
        if (!Bstrcasecmp(tok, tl[i].text))
461
            return tl[i].tokenid;
462
    }
463
    return T_ERROR;
464
}
2554 helixhorne 465
 
466
//////////
467
 
2752 helixhorne 468
// returns: 1 if file could be opened, 0 else
469
int32_t testkopen(const char *filename, char searchfirst)
470
{
471
    int32_t fd = kopen4load(filename, searchfirst);
472
    if (fd >= 0)
473
        kclose(fd);
474
    return (fd >= 0);
475
}
476
 
2554 helixhorne 477
// checks from path and in ZIPs, returns 1 if NOT found
478
int32_t check_file_exist(const char *fn)
479
{
480
    int32_t opsm = pathsearchmode;
481
    char *tfn;
482
 
483
    pathsearchmode = 1;
484
    if (findfrompath(fn,&tfn) < 0)
485
    {
486
        char buf[BMAX_PATH];
487
 
488
        Bstrcpy(buf,fn);
489
        kzfindfilestart(buf);
490
        if (!kzfindfile(buf))
491
        {
492
            initprintf("Error: file \"%s\" does not exist\n",fn);
493
            pathsearchmode = opsm;
494
            return 1;
495
        }
496
    }
497
    else Bfree(tfn);
498
    pathsearchmode = opsm;
499
 
500
    return 0;
501
}
2555 helixhorne 502
 
503
 
504
//// FILE NAME / DIRECTORY LISTS ////
505
void fnlist_clearnames(fnlist_t *fnl)
506
{
507
    klistfree(fnl->finddirs);
508
    klistfree(fnl->findfiles);
509
 
510
    fnl->finddirs = fnl->findfiles = NULL;
511
    fnl->numfiles = fnl->numdirs = 0;
512
}
513
 
514
// dirflags, fileflags:
515
//  -1 means "don't get dirs/files",
516
//  otherwise ORed to flags for respective klistpath
517
int32_t fnlist_getnames(fnlist_t *fnl, const char *dirname, const char *pattern,
518
                        int32_t dirflags, int32_t fileflags)
519
{
520
    CACHE1D_FIND_REC *r;
521
 
522
    fnlist_clearnames(fnl);
523
 
524
    if (dirflags != -1)
525
        fnl->finddirs = klistpath(dirname, "*", CACHE1D_FIND_DIR|dirflags);
526
    if (fileflags != -1)
527
        fnl->findfiles = klistpath(dirname, pattern, CACHE1D_FIND_FILE|fileflags);
528
 
529
    for (r=fnl->finddirs; r; r=r->next)
530
        fnl->numdirs++;
531
    for (r=fnl->findfiles; r; r=r->next)
532
        fnl->numfiles++;
533
 
534
    return(0);
535
}
536
 
537
 
3269 terminx 538
// loads all group (grp, zip, pk3/4) files in the given directory
2555 helixhorne 539
void G_LoadGroupsInDir(const char *dirname)
540
{
3269 terminx 541
    static const char *extensions[4] = { "*.grp", "*.zip", "*.pk3", "*.pk4" };
2555 helixhorne 542
 
543
    char buf[BMAX_PATH];
544
    int32_t i;
545
 
546
    fnlist_t fnlist = FNLIST_INITIALIZER;
547
 
3269 terminx 548
    for (i=0; i<4; i++)
2555 helixhorne 549
    {
550
        CACHE1D_FIND_REC *rec;
551
 
552
        fnlist_getnames(&fnlist, dirname, extensions[i], -1, 0);
553
 
554
        for (rec=fnlist.findfiles; rec; rec=rec->next)
555
        {
556
            Bsnprintf(buf, sizeof(buf), "%s/%s", dirname, rec->name);
557
            initprintf("Using group file \"%s\".\n", buf);
558
            initgroupfile(buf);
559
        }
560
 
561
        fnlist_clearnames(&fnlist);
562
    }
563
}
564
 
565
void G_DoAutoload(const char *dirname)
566
{
567
    char buf[BMAX_PATH];
568
 
569
    Bsnprintf(buf, sizeof(buf), "autoload/%s", dirname);
570
    G_LoadGroupsInDir(buf);
571
}
2560 helixhorne 572
 
573
////
574
 
575
// returns a buffer of size BMAX_PATH
576
char *dup_filename(const char *fn)
577
{
3176 helixhorne 578
    char *buf = (char *)Bmalloc(BMAX_PATH);
2560 helixhorne 579
 
580
    return Bstrncpyz(buf, fn, BMAX_PATH);
581
}
3004 helixhorne 582
 
583
 
584
// Copy FN to WBUF and append an extension if it's not there, which is checked
585
// case-insensitively.
586
// Returns: 1 if not all characters could be written to WBUF, 0 else.
587
int32_t maybe_append_ext(char *wbuf, int32_t wbufsiz, const char *fn, const char *ext)
588
{
589
    const int32_t slen=Bstrlen(fn), extslen=Bstrlen(ext);
590
    const int32_t haveext = (slen>=extslen && Bstrcasecmp(&fn[slen-extslen], ext)==0);
591
 
592
    Bassert((intptr_t)wbuf != (intptr_t)fn);  // no aliasing
593
 
594
    // If 'fn' has no extension suffixed, append one.
595
    return (Bsnprintf(wbuf, wbufsiz, "%s%s", fn, haveext ? "" : ext) >= wbufsiz);
596
}
3243 helixhorne 597
 
598
 
3489 helixhorne 599
// Approximations to 2D and 3D Euclidean distances. Initial EDuke32 SVN import says
3243 helixhorne 600
// in jmact/mathutil.c: "Ken's reverse-engineering job".
601
// Note that jmact/mathutil.c contains practically the same code, but where the
602
// individual x/y(/z) distances are passed instead.
603
int32_t ldist(const spritetype *s1, const spritetype *s2)
604
{
605
    int32_t x = klabs(s1->x-s2->x);
606
    int32_t y = klabs(s1->y-s2->y);
607
 
608
    if (x<y) swaplong(&x,&y);
609
 
610
    {
611
        int32_t t = y + (y>>1);
612
        return (x - (x>>5) - (x>>7)  + (t>>2) + (t>>6));
613
    }
614
}
615
 
616
int32_t dist(const spritetype *s1, const spritetype *s2)
617
{
618
    int32_t x = klabs(s1->x-s2->x);
619
    int32_t y = klabs(s1->y-s2->y);
620
    int32_t z = klabs((s1->z-s2->z)>>4);
621
 
622
    if (x<y) swaplong(&x,&y);
623
    if (x<z) swaplong(&x,&z);
624
 
625
    {
626
        int32_t t = y + z;
627
        return (x - (x>>4) + (t>>2) + (t>>3));
628
    }
629
}
3321 helixhorne 630
 
631
 
632
// Clear OSD background
633
void COMMON_clearbackground(int32_t numcols, int32_t numrows)
634
{
635
    UNREFERENCED_PARAMETER(numcols);
636
 
637
# ifdef USE_OPENGL
3784 terminx 638
    if (getrendermode() >= REND_POLYMOST && qsetmode==200)
3321 helixhorne 639
    {
640
        setpolymost2dview();
641
        bglColor4f(0,0,0,0.67f);
642
        bglEnable(GL_BLEND);
643
        bglRectd(0,0, xdim,8*numrows+8);
644
        bglColor4f(0,0,0,1);
645
        bglRectd(0,8*numrows+4, xdim,8*numrows+8);
646
        return;
647
    }
648
# endif
649
 
650
    CLEARLINES2D(0, min(ydim, numrows*8+8), editorcolors[16]);
651
}