Subversion Repositories eduke32

Rev

Rev 3784 | Rev 3976 | 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
 
3975 helixhorne 155
// Set up new-style multi-psky handling.
156
// NOTE: When adding more multi-pskies, take a look whether the engine-side
157
// MAXPSKYMULTIS needs to be increased.
158
void G_MultiPskyInit(int32_t MOONSKY1__DYN, int32_t BIGORBIT1__DYN, int32_t LA__DYN)
3220 hendricks2 159
{
160
    int32_t i;
161
 
3975 helixhorne 162
    psky_t *moonsky = &multipsky[0];
163
    psky_t *spacesky = &multipsky[1];
164
    psky_t *citysky = &multipsky[2];
3220 hendricks2 165
 
3975 helixhorne 166
    static int32_t inited;
167
    if (inited)
168
        return;
169
    inited = 1;
3220 hendricks2 170
 
3975 helixhorne 171
    multipskytile[0] = MOONSKY1__DYN;
172
    multipskytile[1] = BIGORBIT1__DYN;
173
    multipskytile[2] = LA__DYN;
3220 hendricks2 174
 
3975 helixhorne 175
    pskynummultis = 3;
3220 hendricks2 176
 
3975 helixhorne 177
    // When adding other multi-skies, take care that the tileofs[] values are
178
    // <= PSKYOFF_MAX. (It can be increased up to MAXPSKYTILES, but should be
179
    // set as tight as possible.)
180
 
3220 hendricks2 181
    // MOONSKY1
182
    //        earth          mountain   mountain         sun
3975 helixhorne 183
    moonsky->lognumtiles = 3;
184
    moonsky->horizfrac = 32768;
185
    moonsky->tileofs[6] = 1;
186
    moonsky->tileofs[1] = 2;
187
    moonsky->tileofs[4] = 2;
188
    moonsky->tileofs[2] = 3;
3220 hendricks2 189
 
190
    // BIGORBIT1   // orbit
191
    //       earth1         2           3           moon/sun
3975 helixhorne 192
    spacesky->lognumtiles = 3;
193
    spacesky->horizfrac = 32768;
194
    spacesky->tileofs[5] = 1;
195
    spacesky->tileofs[6] = 2;
196
    spacesky->tileofs[7] = 3;
197
    spacesky->tileofs[2] = 4;
3220 hendricks2 198
 
199
    // LA // la city
200
    //       earth1         2           3           moon/sun
3975 helixhorne 201
    citysky->lognumtiles = 3;
202
    citysky->horizfrac = 16384+1024;
203
    citysky->tileofs[0] = 1;
204
    citysky->tileofs[1] = 2;
205
    citysky->tileofs[2] = 1;
206
    citysky->tileofs[3] = 3;
207
    citysky->tileofs[4] = 4;
208
    citysky->tileofs[5] = 0;
209
    citysky->tileofs[6] = 2;
210
    citysky->tileofs[7] = 3;
3220 hendricks2 211
 
3975 helixhorne 212
    for (i=0; i<pskynummultis; ++i)
213
    {
214
        int32_t j;
215
        for (j=0; j<(1<<multipsky[i].lognumtiles); ++j)
216
            Bassert(multipsky[i].tileofs[j] <= PSKYOFF_MAX);
217
    }
3220 hendricks2 218
 
219
    // default in game:
3975 helixhorne 220
    g_psky.horizfrac = 32768;
3220 hendricks2 221
}
222
 
223
//////////
224
 
3582 hendricks2 225
#ifdef GEKKO
226
#include "gctypes.h" // for bool
227
void L2Enhance();
228
void CON_EnableGecko(int channel,int safe);
229
bool fatInit (uint32_t cacheSize, bool setAsDefaultDevice);
230
#endif
231
 
232
void G_ExtPreInit(void)
233
{
234
#ifdef GEKKO
235
    L2Enhance();
236
    CON_EnableGecko(1, 1);
237
    Bprintf("Console started\n");
238
    fatInit(28, true);
239
#endif
240
}
241
 
3622 terminx 242
#ifdef _WIN32
3637 terminx 243
const char * G_GetInstallPath(int32_t insttype)
3622 terminx 244
{
3637 terminx 245
    static char spath[NUMINSTPATHS][BMAX_PATH];
246
    static int32_t success[NUMINSTPATHS] = { -1, -1 };
3622 terminx 247
    int32_t siz = BMAX_PATH;
248
 
3637 terminx 249
    if (success[insttype] == -1)
250
    {
3671 hendricks2 251
        HKEY HKLM32;
252
        LONG keygood = RegOpenKeyEx(HKEY_LOCAL_MACHINE, NULL, 0, KEY_READ | KEY_WOW64_32KEY, &HKLM32);
253
        // KEY_WOW64_32KEY gets us around Wow6432Node on 64-bit builds
254
 
255
        if (keygood == ERROR_SUCCESS)
256
            switch (insttype)
257
            {
258
            case INSTPATH_STEAM:
259
                success[insttype] = SHGetValueA(HKLM32, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Steam App 225140", "InstallLocation", NULL, spath[insttype], (LPDWORD)&siz);
260
                break;
261
            case INSTPATH_GOG:
262
                success[insttype] = SHGetValueA(HKLM32, "SOFTWARE\\GOG.com\\GOGDUKE3D", "PATH", NULL, spath[insttype], (LPDWORD)&siz);
263
                break;
264
            }
3637 terminx 265
    }
3622 terminx 266
 
3637 terminx 267
    if (success[insttype] == ERROR_SUCCESS)
268
        return spath[insttype];
3622 terminx 269
 
270
    return NULL;
271
}
272
#endif
273
 
3581 hendricks2 274
void G_AddSearchPaths(void)
275
{
276
#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
277
    addsearchpath("/usr/share/games/jfduke3d");
278
    addsearchpath("/usr/local/share/games/jfduke3d");
279
    addsearchpath("/usr/share/games/eduke32");
280
    addsearchpath("/usr/local/share/games/eduke32");
281
#elif defined(__APPLE__)
282
    addsearchpath("/Library/Application Support/JFDuke3D");
283
    addsearchpath("/Library/Application Support/EDuke32");
284
#elif defined (_WIN32)
3615 terminx 285
    // detect Steam and GOG versions of Duke3D
286
    char buf[BMAX_PATH];
3671 hendricks2 287
    const char* instpath;
3615 terminx 288
 
3671 hendricks2 289
    if ((instpath = G_GetInstallPath(INSTPATH_STEAM)))
3622 terminx 290
    {
3671 hendricks2 291
        Bsprintf(buf, "%s/gameroot", instpath);
3622 terminx 292
        addsearchpath(buf);
3615 terminx 293
 
3671 hendricks2 294
        Bsprintf(buf, "%s/gameroot/addons", instpath);
3622 terminx 295
        addsearchpath(buf);
3615 terminx 296
    }
297
 
3671 hendricks2 298
    if ((instpath = G_GetInstallPath(INSTPATH_GOG)))
299
        addsearchpath(instpath);
3581 hendricks2 300
#endif
301
}
302
 
3654 terminx 303
void G_CleanupSearchPaths(void)
304
{
305
#ifdef _WIN32
306
    char buf[BMAX_PATH];
3671 hendricks2 307
    const char* instpath;
3654 terminx 308
 
3671 hendricks2 309
    if ((instpath = G_GetInstallPath(INSTPATH_STEAM)))
3654 terminx 310
    {
3671 hendricks2 311
        Bsprintf(buf, "%s/gameroot", instpath);
3654 terminx 312
        removesearchpath(buf);
313
 
3671 hendricks2 314
        Bsprintf(buf, "%s/gameroot/addons", instpath);
3654 terminx 315
        removesearchpath(buf);
316
    }
317
 
3671 hendricks2 318
    if ((instpath = G_GetInstallPath(INSTPATH_GOG)))
319
        removesearchpath(instpath);
3654 terminx 320
#endif
321
}
322
 
3581 hendricks2 323
//////////
324
 
2542 helixhorne 325
struct strllist *CommandPaths, *CommandGrps;
326
 
327
void G_AddGroup(const char *buffer)
328
{
329
    char buf[BMAX_PATH];
330
 
3176 helixhorne 331
    struct strllist *s = (struct strllist *)Bcalloc(1,sizeof(struct strllist));
2542 helixhorne 332
 
333
    Bstrcpy(buf, buffer);
334
 
335
    if (Bstrchr(buf,'.') == 0)
336
        Bstrcat(buf,".grp");
337
 
338
    s->str = Bstrdup(buf);
339
 
340
    if (CommandGrps)
341
    {
342
        struct strllist *t;
343
        for (t = CommandGrps; t->next; t=t->next) ;
344
        t->next = s;
345
        return;
346
    }
347
    CommandGrps = s;
348
}
349
 
350
void G_AddPath(const char *buffer)
351
{
3176 helixhorne 352
    struct strllist *s = (struct strllist *)Bcalloc(1,sizeof(struct strllist));
2542 helixhorne 353
    s->str = Bstrdup(buffer);
354
 
355
    if (CommandPaths)
356
    {
357
        struct strllist *t;
358
        for (t = CommandPaths; t->next; t=t->next) ;
359
        t->next = s;
360
        return;
361
    }
362
    CommandPaths = s;
363
}
2549 helixhorne 364
 
365
//////////
366
 
367
int32_t getatoken(scriptfile *sf, const tokenlist *tl, int32_t ntokens)
368
{
369
    char *tok;
370
    int32_t i;
371
 
372
    if (!sf) return T_ERROR;
373
    tok = scriptfile_gettoken(sf);
374
    if (!tok) return T_EOF;
375
 
376
    for (i=ntokens-1; i>=0; i--)
377
    {
378
        if (!Bstrcasecmp(tok, tl[i].text))
379
            return tl[i].tokenid;
380
    }
381
    return T_ERROR;
382
}
2554 helixhorne 383
 
384
//////////
385
 
2752 helixhorne 386
// returns: 1 if file could be opened, 0 else
387
int32_t testkopen(const char *filename, char searchfirst)
388
{
389
    int32_t fd = kopen4load(filename, searchfirst);
390
    if (fd >= 0)
391
        kclose(fd);
392
    return (fd >= 0);
393
}
394
 
2554 helixhorne 395
// checks from path and in ZIPs, returns 1 if NOT found
396
int32_t check_file_exist(const char *fn)
397
{
398
    int32_t opsm = pathsearchmode;
399
    char *tfn;
400
 
401
    pathsearchmode = 1;
402
    if (findfrompath(fn,&tfn) < 0)
403
    {
404
        char buf[BMAX_PATH];
405
 
406
        Bstrcpy(buf,fn);
407
        kzfindfilestart(buf);
408
        if (!kzfindfile(buf))
409
        {
410
            initprintf("Error: file \"%s\" does not exist\n",fn);
411
            pathsearchmode = opsm;
412
            return 1;
413
        }
414
    }
415
    else Bfree(tfn);
416
    pathsearchmode = opsm;
417
 
418
    return 0;
419
}
2555 helixhorne 420
 
421
 
422
//// FILE NAME / DIRECTORY LISTS ////
423
void fnlist_clearnames(fnlist_t *fnl)
424
{
425
    klistfree(fnl->finddirs);
426
    klistfree(fnl->findfiles);
427
 
428
    fnl->finddirs = fnl->findfiles = NULL;
429
    fnl->numfiles = fnl->numdirs = 0;
430
}
431
 
432
// dirflags, fileflags:
433
//  -1 means "don't get dirs/files",
434
//  otherwise ORed to flags for respective klistpath
435
int32_t fnlist_getnames(fnlist_t *fnl, const char *dirname, const char *pattern,
436
                        int32_t dirflags, int32_t fileflags)
437
{
438
    CACHE1D_FIND_REC *r;
439
 
440
    fnlist_clearnames(fnl);
441
 
442
    if (dirflags != -1)
443
        fnl->finddirs = klistpath(dirname, "*", CACHE1D_FIND_DIR|dirflags);
444
    if (fileflags != -1)
445
        fnl->findfiles = klistpath(dirname, pattern, CACHE1D_FIND_FILE|fileflags);
446
 
447
    for (r=fnl->finddirs; r; r=r->next)
448
        fnl->numdirs++;
449
    for (r=fnl->findfiles; r; r=r->next)
450
        fnl->numfiles++;
451
 
452
    return(0);
453
}
454
 
455
 
3269 terminx 456
// loads all group (grp, zip, pk3/4) files in the given directory
2555 helixhorne 457
void G_LoadGroupsInDir(const char *dirname)
458
{
3269 terminx 459
    static const char *extensions[4] = { "*.grp", "*.zip", "*.pk3", "*.pk4" };
2555 helixhorne 460
 
461
    char buf[BMAX_PATH];
462
    int32_t i;
463
 
464
    fnlist_t fnlist = FNLIST_INITIALIZER;
465
 
3269 terminx 466
    for (i=0; i<4; i++)
2555 helixhorne 467
    {
468
        CACHE1D_FIND_REC *rec;
469
 
470
        fnlist_getnames(&fnlist, dirname, extensions[i], -1, 0);
471
 
472
        for (rec=fnlist.findfiles; rec; rec=rec->next)
473
        {
474
            Bsnprintf(buf, sizeof(buf), "%s/%s", dirname, rec->name);
475
            initprintf("Using group file \"%s\".\n", buf);
476
            initgroupfile(buf);
477
        }
478
 
479
        fnlist_clearnames(&fnlist);
480
    }
481
}
482
 
483
void G_DoAutoload(const char *dirname)
484
{
485
    char buf[BMAX_PATH];
486
 
487
    Bsnprintf(buf, sizeof(buf), "autoload/%s", dirname);
488
    G_LoadGroupsInDir(buf);
489
}
2560 helixhorne 490
 
491
////
492
 
493
// returns a buffer of size BMAX_PATH
494
char *dup_filename(const char *fn)
495
{
3176 helixhorne 496
    char *buf = (char *)Bmalloc(BMAX_PATH);
2560 helixhorne 497
 
498
    return Bstrncpyz(buf, fn, BMAX_PATH);
499
}
3004 helixhorne 500
 
501
 
502
// Copy FN to WBUF and append an extension if it's not there, which is checked
503
// case-insensitively.
504
// Returns: 1 if not all characters could be written to WBUF, 0 else.
505
int32_t maybe_append_ext(char *wbuf, int32_t wbufsiz, const char *fn, const char *ext)
506
{
507
    const int32_t slen=Bstrlen(fn), extslen=Bstrlen(ext);
508
    const int32_t haveext = (slen>=extslen && Bstrcasecmp(&fn[slen-extslen], ext)==0);
509
 
510
    Bassert((intptr_t)wbuf != (intptr_t)fn);  // no aliasing
511
 
512
    // If 'fn' has no extension suffixed, append one.
513
    return (Bsnprintf(wbuf, wbufsiz, "%s%s", fn, haveext ? "" : ext) >= wbufsiz);
514
}
3243 helixhorne 515
 
516
 
3489 helixhorne 517
// Approximations to 2D and 3D Euclidean distances. Initial EDuke32 SVN import says
3243 helixhorne 518
// in jmact/mathutil.c: "Ken's reverse-engineering job".
519
// Note that jmact/mathutil.c contains practically the same code, but where the
520
// individual x/y(/z) distances are passed instead.
521
int32_t ldist(const spritetype *s1, const spritetype *s2)
522
{
523
    int32_t x = klabs(s1->x-s2->x);
524
    int32_t y = klabs(s1->y-s2->y);
525
 
526
    if (x<y) swaplong(&x,&y);
527
 
528
    {
529
        int32_t t = y + (y>>1);
530
        return (x - (x>>5) - (x>>7)  + (t>>2) + (t>>6));
531
    }
532
}
533
 
534
int32_t dist(const spritetype *s1, const spritetype *s2)
535
{
536
    int32_t x = klabs(s1->x-s2->x);
537
    int32_t y = klabs(s1->y-s2->y);
538
    int32_t z = klabs((s1->z-s2->z)>>4);
539
 
540
    if (x<y) swaplong(&x,&y);
541
    if (x<z) swaplong(&x,&z);
542
 
543
    {
544
        int32_t t = y + z;
545
        return (x - (x>>4) + (t>>2) + (t>>3));
546
    }
547
}
3321 helixhorne 548
 
549
 
550
// Clear OSD background
551
void COMMON_clearbackground(int32_t numcols, int32_t numrows)
552
{
553
    UNREFERENCED_PARAMETER(numcols);
554
 
555
# ifdef USE_OPENGL
3784 terminx 556
    if (getrendermode() >= REND_POLYMOST && qsetmode==200)
3321 helixhorne 557
    {
558
        setpolymost2dview();
559
        bglColor4f(0,0,0,0.67f);
560
        bglEnable(GL_BLEND);
561
        bglRectd(0,0, xdim,8*numrows+8);
562
        bglColor4f(0,0,0,1);
563
        bglRectd(0,8*numrows+4, xdim,8*numrows+8);
564
        return;
565
    }
566
# endif
567
 
568
    CLEARLINES2D(0, min(ydim, numrows*8+8), editorcolors[16]);
569
}