Subversion Repositories eduke32

Rev

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