Subversion Repositories eduke32

Rev

Rev 3637 | Rev 3671 | 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"
3615 terminx 15
#include <shlwapi.h>
3581 hendricks2 16
#endif
17
 
2542 helixhorne 18
#include "common.h"
2726 hendricks2 19
#include "common_game.h"
2542 helixhorne 20
 
2789 hendricks2 21
int32_t g_gameType = GAMEFLAG_DUKE;
2542 helixhorne 22
 
2726 hendricks2 23
// grp/con/def handling
24
 
2796 helixhorne 25
const char *defaultgamegrp[GAMECOUNT] = { "DUKE3D.GRP", "NAM.GRP", "NAPALM.GRP", "WW2GI.GRP" };
26
const char *defaultdeffilename[GAMECOUNT] = { "duke3d.def", "nam.def", "napalm.def", "ww2gi.def" };
27
const char *defaultconfilename = "GAME.CON";
28
const char *defaultgameconfilename[GAMECOUNT] = { "EDUKE.CON", "NAM.CON", "NAPALM.CON", "WW2GI.CON" };
2726 hendricks2 29
 
30
// g_grpNamePtr can ONLY point to a malloc'd block (length BMAX_PATH)
31
char *g_grpNamePtr = NULL;
32
// g_defNamePtr can ONLY point to a malloc'd block (length BMAX_PATH)
33
char *g_defNamePtr = NULL;
34
// g_scriptNamePtr can ONLY point to a malloc'd block (length BMAX_PATH)
35
char *g_scriptNamePtr = NULL;
36
 
37
void clearGrpNamePtr(void)
38
{
39
    if (g_grpNamePtr != NULL)
40
        Bfree(g_grpNamePtr);
41
    // g_grpNamePtr assumed to be assigned to right after
42
}
43
 
44
void clearDefNamePtr(void)
45
{
46
    if (g_defNamePtr != NULL)
47
        Bfree(g_defNamePtr);
48
    // g_defNamePtr assumed to be assigned to right after
49
}
50
 
51
void clearScriptNamePtr(void)
52
{
53
    if (g_scriptNamePtr != NULL)
54
        Bfree(g_scriptNamePtr);
55
    // g_scriptNamePtr assumed to be assigned to right after
56
}
57
 
2796 helixhorne 58
const char *G_DefaultGrpFile(void)
2726 hendricks2 59
{
60
    if (DUKE)
61
        return defaultgamegrp[GAME_DUKE];
62
    // order is important for the following three because GAMEFLAG_NAM overlaps all
63
    else if (NAPALM)
64
        return defaultgamegrp[GAME_NAPALM];
65
    else if (WW2GI)
66
        return defaultgamegrp[GAME_WW2GI];
67
    else if (NAM)
68
        return defaultgamegrp[GAME_NAM];
69
 
70
    return defaultgamegrp[0];
71
}
2796 helixhorne 72
const char *G_DefaultDefFile(void)
2726 hendricks2 73
{
74
    if (DUKE)
75
        return defaultdeffilename[GAME_DUKE];
76
    else if (WW2GI)
77
        return defaultdeffilename[GAME_WW2GI];
78
    else if (NAPALM)
79
    {
2752 helixhorne 80
        if (!testkopen(defaultdeffilename[GAME_NAPALM],0) && testkopen(defaultdeffilename[GAME_NAM],0))
3618 hendricks2 81
            return defaultdeffilename[GAME_NAM]; // NAM/NAPALM Sharing
2726 hendricks2 82
        else
83
            return defaultdeffilename[GAME_NAPALM];
84
    }
85
    else if (NAM)
86
    {
2752 helixhorne 87
        if (!testkopen(defaultdeffilename[GAME_NAM],0) && testkopen(defaultdeffilename[GAME_NAPALM],0))
3618 hendricks2 88
            return defaultdeffilename[GAME_NAPALM]; // NAM/NAPALM Sharing
2726 hendricks2 89
        else
90
            return defaultdeffilename[GAME_NAM];
91
    }
92
 
93
    return defaultdeffilename[0];
94
}
2796 helixhorne 95
const char *G_DefaultConFile(void)
2726 hendricks2 96
{
2752 helixhorne 97
    if (DUKE && testkopen(defaultgameconfilename[GAME_DUKE],0))
2726 hendricks2 98
        return defaultgameconfilename[GAME_DUKE];
2752 helixhorne 99
    else if (WW2GI && testkopen(defaultgameconfilename[GAME_WW2GI],0))
2726 hendricks2 100
        return defaultgameconfilename[GAME_WW2GI];
101
    else if (NAPALM)
102
    {
2752 helixhorne 103
        if (!testkopen(defaultgameconfilename[GAME_NAPALM],0))
2726 hendricks2 104
        {
2752 helixhorne 105
            if (testkopen(defaultgameconfilename[GAME_NAM],0))
3618 hendricks2 106
                return defaultgameconfilename[GAME_NAM]; // NAM/NAPALM Sharing
2726 hendricks2 107
        }
108
        else
109
            return defaultgameconfilename[GAME_NAPALM];
110
    }
111
    else if (NAM)
112
    {
2752 helixhorne 113
        if (!testkopen(defaultgameconfilename[GAME_NAM],0))
2726 hendricks2 114
        {
2752 helixhorne 115
            if (testkopen(defaultgameconfilename[GAME_NAPALM],0))
3618 hendricks2 116
                return defaultgameconfilename[GAME_NAPALM]; // NAM/NAPALM Sharing
2726 hendricks2 117
        }
118
        else
119
            return defaultgameconfilename[GAME_NAM];
120
    }
121
 
122
    return defaultconfilename;
123
}
124
 
2796 helixhorne 125
const char *G_GrpFile(void)
2726 hendricks2 126
{
127
    if (g_grpNamePtr == NULL)
128
        return G_DefaultGrpFile();
129
    else
130
        return g_grpNamePtr;
131
}
3654 terminx 132
 
2796 helixhorne 133
const char *G_DefFile(void)
2726 hendricks2 134
{
135
    if (g_defNamePtr == NULL)
136
        return G_DefaultDefFile();
137
    else
138
        return g_defNamePtr;
139
}
3654 terminx 140
 
2796 helixhorne 141
const char *G_ConFile(void)
2726 hendricks2 142
{
143
    if (g_scriptNamePtr == NULL)
144
        return G_DefaultConFile();
145
    else
146
        return g_scriptNamePtr;
147
}
148
 
149
//////////
150
 
3220 hendricks2 151
void G_MultiPskyInit(void)
152
{
153
    int32_t i;
154
 
155
    // new-style multi-psky handling
156
    pskymultilist[0] = MOONSKY1;
157
    pskymultilist[1] = BIGORBIT1;
158
    pskymultilist[2] = LA;
159
 
160
    pskymultiyscale[0] = 32768;
161
    pskymultiyscale[1] = 32768;
162
    pskymultiyscale[2] = 16384+1024;
163
 
164
    for (i=0; i<3; ++i)
165
    {
166
        pskymultibits[i] = 3;
167
        Bmemset(pskymultioff[i], 0, sizeof(pskymultioff[i]));
168
    }
169
 
170
    // KEEPINSYNC with Polymer MAX OFFSET = 4
171
 
172
    // MOONSKY1
173
    //        earth          mountain   mountain         sun
174
    pskymultioff[0][6]=1;
175
    pskymultioff[0][1]=2;
176
    pskymultioff[0][4]=2;
177
    pskymultioff[0][2]=3;
178
 
179
    // BIGORBIT1   // orbit
180
    //       earth1         2           3           moon/sun
181
    pskymultioff[1][5]=1;
182
    pskymultioff[1][6]=2;
183
    pskymultioff[1][7]=3;
184
    pskymultioff[1][2]=4;
185
 
186
    // LA // la city
187
    //       earth1         2           3           moon/sun
188
    pskymultioff[2][0]=1;
189
    pskymultioff[2][1]=2;
190
    pskymultioff[2][2]=1;
191
    pskymultioff[2][3]=3;
192
    pskymultioff[2][4]=4;
193
    pskymultioff[2][5]=0;
194
    pskymultioff[2][6]=2;
195
    pskymultioff[2][7]=3;
196
 
197
    pskynummultis = 3;
198
 
199
    // default in game:
200
    parallaxyscale = 32768;
201
}
202
 
203
//////////
204
 
3582 hendricks2 205
#ifdef GEKKO
206
#include "gctypes.h" // for bool
207
void L2Enhance();
208
void CON_EnableGecko(int channel,int safe);
209
bool fatInit (uint32_t cacheSize, bool setAsDefaultDevice);
210
#endif
211
 
212
void G_ExtPreInit(void)
213
{
214
#ifdef GEKKO
215
    L2Enhance();
216
    CON_EnableGecko(1, 1);
217
    Bprintf("Console started\n");
218
    fatInit(28, true);
219
#endif
220
}
221
 
3622 terminx 222
#ifdef _WIN32
3637 terminx 223
const char * G_GetInstallPath(int32_t insttype)
3622 terminx 224
{
3637 terminx 225
    static char spath[NUMINSTPATHS][BMAX_PATH];
226
    static int32_t success[NUMINSTPATHS] = { -1, -1 };
3622 terminx 227
    int32_t siz = BMAX_PATH;
228
 
3637 terminx 229
    // this still needs to be fixed for win64 builds
230
    if (success[insttype] == -1)
231
    {
232
        switch (insttype)
233
        {
234
        case INSTPATH_STEAM:
235
            success[insttype] = SHGetValueA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Steam App 225140", "InstallLocation", NULL, spath[insttype], (LPDWORD)&siz);
236
            break;
237
        case INSTPATH_GOG:
238
            success[insttype] = SHGetValueA(HKEY_LOCAL_MACHINE, "SOFTWARE\\GOG.com\\GOGDUKE3D", "PATH", NULL, spath[insttype], (LPDWORD)&siz);
239
            break;
240
        }
241
    }
3622 terminx 242
 
3637 terminx 243
    if (success[insttype] == ERROR_SUCCESS)
244
        return spath[insttype];
3622 terminx 245
 
246
    return NULL;
247
}
248
#endif
249
 
3581 hendricks2 250
void G_AddSearchPaths(void)
251
{
252
#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
253
    addsearchpath("/usr/share/games/jfduke3d");
254
    addsearchpath("/usr/local/share/games/jfduke3d");
255
    addsearchpath("/usr/share/games/eduke32");
256
    addsearchpath("/usr/local/share/games/eduke32");
257
#elif defined(__APPLE__)
258
    addsearchpath("/Library/Application Support/JFDuke3D");
259
    addsearchpath("/Library/Application Support/EDuke32");
260
#elif defined (_WIN32)
3615 terminx 261
    // detect Steam and GOG versions of Duke3D
262
    char buf[BMAX_PATH];
263
 
3637 terminx 264
    if (G_GetInstallPath(INSTPATH_STEAM))
3622 terminx 265
    {
3637 terminx 266
        Bsprintf(buf, "%s/gameroot", G_GetInstallPath(INSTPATH_STEAM));
3622 terminx 267
        addsearchpath(buf);
3615 terminx 268
 
3637 terminx 269
        Bsprintf(buf, "%s/gameroot/addons", G_GetInstallPath(INSTPATH_STEAM));
3622 terminx 270
        addsearchpath(buf);
3615 terminx 271
    }
272
 
3637 terminx 273
    if (G_GetInstallPath(INSTPATH_GOG))
274
        addsearchpath(G_GetInstallPath(INSTPATH_GOG));
3581 hendricks2 275
#endif
276
}
277
 
3654 terminx 278
void G_CleanupSearchPaths(void)
279
{
280
#ifdef _WIN32
281
    char buf[BMAX_PATH];
282
 
283
    if (G_GetInstallPath(INSTPATH_STEAM))
284
    {
285
        Bsprintf(buf, "%s/gameroot", G_GetInstallPath(INSTPATH_STEAM));
286
        removesearchpath(buf);
287
 
288
        Bsprintf(buf, "%s/gameroot/addons", G_GetInstallPath(INSTPATH_STEAM));
289
        removesearchpath(buf);
290
    }
291
 
292
    if (G_GetInstallPath(INSTPATH_GOG))
293
        removesearchpath(G_GetInstallPath(INSTPATH_GOG));
294
#endif
295
}
296
 
3581 hendricks2 297
//////////
298
 
2542 helixhorne 299
struct strllist *CommandPaths, *CommandGrps;
300
 
301
void G_AddGroup(const char *buffer)
302
{
303
    char buf[BMAX_PATH];
304
 
3176 helixhorne 305
    struct strllist *s = (struct strllist *)Bcalloc(1,sizeof(struct strllist));
2542 helixhorne 306
 
307
    Bstrcpy(buf, buffer);
308
 
309
    if (Bstrchr(buf,'.') == 0)
310
        Bstrcat(buf,".grp");
311
 
312
    s->str = Bstrdup(buf);
313
 
314
    if (CommandGrps)
315
    {
316
        struct strllist *t;
317
        for (t = CommandGrps; t->next; t=t->next) ;
318
        t->next = s;
319
        return;
320
    }
321
    CommandGrps = s;
322
}
323
 
324
void G_AddPath(const char *buffer)
325
{
3176 helixhorne 326
    struct strllist *s = (struct strllist *)Bcalloc(1,sizeof(struct strllist));
2542 helixhorne 327
    s->str = Bstrdup(buffer);
328
 
329
    if (CommandPaths)
330
    {
331
        struct strllist *t;
332
        for (t = CommandPaths; t->next; t=t->next) ;
333
        t->next = s;
334
        return;
335
    }
336
    CommandPaths = s;
337
}
2549 helixhorne 338
 
339
//////////
340
 
341
int32_t getatoken(scriptfile *sf, const tokenlist *tl, int32_t ntokens)
342
{
343
    char *tok;
344
    int32_t i;
345
 
346
    if (!sf) return T_ERROR;
347
    tok = scriptfile_gettoken(sf);
348
    if (!tok) return T_EOF;
349
 
350
    for (i=ntokens-1; i>=0; i--)
351
    {
352
        if (!Bstrcasecmp(tok, tl[i].text))
353
            return tl[i].tokenid;
354
    }
355
    return T_ERROR;
356
}
2554 helixhorne 357
 
358
//////////
359
 
2752 helixhorne 360
// returns: 1 if file could be opened, 0 else
361
int32_t testkopen(const char *filename, char searchfirst)
362
{
363
    int32_t fd = kopen4load(filename, searchfirst);
364
    if (fd >= 0)
365
        kclose(fd);
366
    return (fd >= 0);
367
}
368
 
2554 helixhorne 369
// checks from path and in ZIPs, returns 1 if NOT found
370
int32_t check_file_exist(const char *fn)
371
{
372
    int32_t opsm = pathsearchmode;
373
    char *tfn;
374
 
375
    pathsearchmode = 1;
376
    if (findfrompath(fn,&tfn) < 0)
377
    {
378
        char buf[BMAX_PATH];
379
 
380
        Bstrcpy(buf,fn);
381
        kzfindfilestart(buf);
382
        if (!kzfindfile(buf))
383
        {
384
            initprintf("Error: file \"%s\" does not exist\n",fn);
385
            pathsearchmode = opsm;
386
            return 1;
387
        }
388
    }
389
    else Bfree(tfn);
390
    pathsearchmode = opsm;
391
 
392
    return 0;
393
}
2555 helixhorne 394
 
395
 
396
//// FILE NAME / DIRECTORY LISTS ////
397
void fnlist_clearnames(fnlist_t *fnl)
398
{
399
    klistfree(fnl->finddirs);
400
    klistfree(fnl->findfiles);
401
 
402
    fnl->finddirs = fnl->findfiles = NULL;
403
    fnl->numfiles = fnl->numdirs = 0;
404
}
405
 
406
// dirflags, fileflags:
407
//  -1 means "don't get dirs/files",
408
//  otherwise ORed to flags for respective klistpath
409
int32_t fnlist_getnames(fnlist_t *fnl, const char *dirname, const char *pattern,
410
                        int32_t dirflags, int32_t fileflags)
411
{
412
    CACHE1D_FIND_REC *r;
413
 
414
    fnlist_clearnames(fnl);
415
 
416
    if (dirflags != -1)
417
        fnl->finddirs = klistpath(dirname, "*", CACHE1D_FIND_DIR|dirflags);
418
    if (fileflags != -1)
419
        fnl->findfiles = klistpath(dirname, pattern, CACHE1D_FIND_FILE|fileflags);
420
 
421
    for (r=fnl->finddirs; r; r=r->next)
422
        fnl->numdirs++;
423
    for (r=fnl->findfiles; r; r=r->next)
424
        fnl->numfiles++;
425
 
426
    return(0);
427
}
428
 
429
 
3269 terminx 430
// loads all group (grp, zip, pk3/4) files in the given directory
2555 helixhorne 431
void G_LoadGroupsInDir(const char *dirname)
432
{
3269 terminx 433
    static const char *extensions[4] = { "*.grp", "*.zip", "*.pk3", "*.pk4" };
2555 helixhorne 434
 
435
    char buf[BMAX_PATH];
436
    int32_t i;
437
 
438
    fnlist_t fnlist = FNLIST_INITIALIZER;
439
 
3269 terminx 440
    for (i=0; i<4; i++)
2555 helixhorne 441
    {
442
        CACHE1D_FIND_REC *rec;
443
 
444
        fnlist_getnames(&fnlist, dirname, extensions[i], -1, 0);
445
 
446
        for (rec=fnlist.findfiles; rec; rec=rec->next)
447
        {
448
            Bsnprintf(buf, sizeof(buf), "%s/%s", dirname, rec->name);
449
            initprintf("Using group file \"%s\".\n", buf);
450
            initgroupfile(buf);
451
        }
452
 
453
        fnlist_clearnames(&fnlist);
454
    }
455
}
456
 
457
void G_DoAutoload(const char *dirname)
458
{
459
    char buf[BMAX_PATH];
460
 
461
    Bsnprintf(buf, sizeof(buf), "autoload/%s", dirname);
462
    G_LoadGroupsInDir(buf);
463
}
2560 helixhorne 464
 
465
////
466
 
467
// returns a buffer of size BMAX_PATH
468
char *dup_filename(const char *fn)
469
{
3176 helixhorne 470
    char *buf = (char *)Bmalloc(BMAX_PATH);
2560 helixhorne 471
 
472
    return Bstrncpyz(buf, fn, BMAX_PATH);
473
}
3004 helixhorne 474
 
475
 
476
// Copy FN to WBUF and append an extension if it's not there, which is checked
477
// case-insensitively.
478
// Returns: 1 if not all characters could be written to WBUF, 0 else.
479
int32_t maybe_append_ext(char *wbuf, int32_t wbufsiz, const char *fn, const char *ext)
480
{
481
    const int32_t slen=Bstrlen(fn), extslen=Bstrlen(ext);
482
    const int32_t haveext = (slen>=extslen && Bstrcasecmp(&fn[slen-extslen], ext)==0);
483
 
484
    Bassert((intptr_t)wbuf != (intptr_t)fn);  // no aliasing
485
 
486
    // If 'fn' has no extension suffixed, append one.
487
    return (Bsnprintf(wbuf, wbufsiz, "%s%s", fn, haveext ? "" : ext) >= wbufsiz);
488
}
3243 helixhorne 489
 
490
 
3489 helixhorne 491
// Approximations to 2D and 3D Euclidean distances. Initial EDuke32 SVN import says
3243 helixhorne 492
// in jmact/mathutil.c: "Ken's reverse-engineering job".
493
// Note that jmact/mathutil.c contains practically the same code, but where the
494
// individual x/y(/z) distances are passed instead.
495
int32_t ldist(const spritetype *s1, const spritetype *s2)
496
{
497
    int32_t x = klabs(s1->x-s2->x);
498
    int32_t y = klabs(s1->y-s2->y);
499
 
500
    if (x<y) swaplong(&x,&y);
501
 
502
    {
503
        int32_t t = y + (y>>1);
504
        return (x - (x>>5) - (x>>7)  + (t>>2) + (t>>6));
505
    }
506
}
507
 
508
int32_t dist(const spritetype *s1, const spritetype *s2)
509
{
510
    int32_t x = klabs(s1->x-s2->x);
511
    int32_t y = klabs(s1->y-s2->y);
512
    int32_t z = klabs((s1->z-s2->z)>>4);
513
 
514
    if (x<y) swaplong(&x,&y);
515
    if (x<z) swaplong(&x,&z);
516
 
517
    {
518
        int32_t t = y + z;
519
        return (x - (x>>4) + (t>>2) + (t>>3));
520
    }
521
}
3321 helixhorne 522
 
523
 
524
// Clear OSD background
525
void COMMON_clearbackground(int32_t numcols, int32_t numrows)
526
{
527
    UNREFERENCED_PARAMETER(numcols);
528
 
529
# ifdef USE_OPENGL
530
    if (rendmode>=3 && qsetmode==200)
531
    {
532
        setpolymost2dview();
533
        bglColor4f(0,0,0,0.67f);
534
        bglEnable(GL_BLEND);
535
        bglRectd(0,0, xdim,8*numrows+8);
536
        bglColor4f(0,0,0,1);
537
        bglRectd(0,8*numrows+4, xdim,8*numrows+8);
538
        return;
539
    }
540
# endif
541
 
542
    CLEARLINES2D(0, min(ydim, numrows*8+8), editorcolors[16]);
543
}