Subversion Repositories eduke32

Rev

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