Subversion Repositories eduke32

Rev

Rev 3618 | Rev 3637 | 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
221
const char * G_GetSteamPath(void)
222
{
223
    static char spath[BMAX_PATH];
224
    static int32_t success = -1;
225
    int32_t siz = BMAX_PATH;
226
 
227
    if (success == -1)
228
        success = SHGetValueA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Steam App 225140", "InstallLocation", NULL, spath, (LPDWORD)&siz);
229
 
230
    if (success == ERROR_SUCCESS)
231
        return spath;
232
 
233
    return NULL;
234
}
235
#endif
236
 
3581 hendricks2 237
void G_AddSearchPaths(void)
238
{
239
#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
240
    addsearchpath("/usr/share/games/jfduke3d");
241
    addsearchpath("/usr/local/share/games/jfduke3d");
242
    addsearchpath("/usr/share/games/eduke32");
243
    addsearchpath("/usr/local/share/games/eduke32");
244
#elif defined(__APPLE__)
245
    addsearchpath("/Library/Application Support/JFDuke3D");
246
    addsearchpath("/Library/Application Support/EDuke32");
247
#elif defined (_WIN32)
3615 terminx 248
    // detect Steam and GOG versions of Duke3D
249
    char buf[BMAX_PATH];
250
    int32_t siz = BMAX_PATH, ret;
251
 
3622 terminx 252
    if (G_GetSteamPath())
253
    {
254
        Bsprintf(buf, "%s/gameroot/classic", G_GetSteamPath());
255
        addsearchpath(buf);
3615 terminx 256
 
3622 terminx 257
        Bsprintf(buf, "%s/gameroot/addons", G_GetSteamPath());
258
        addsearchpath(buf);
3615 terminx 259
    }
260
 
3622 terminx 261
    ret = SHGetValueA(HKEY_LOCAL_MACHINE, "SOFTWARE\\GOG.com\\GOGDUKE3D", "PATH", NULL, buf, (LPDWORD)&siz);
262
 
3615 terminx 263
    if (ret == ERROR_SUCCESS)
264
        addsearchpath(buf);
3581 hendricks2 265
#endif
266
}
267
 
268
//////////
269
 
2542 helixhorne 270
struct strllist *CommandPaths, *CommandGrps;
271
 
272
void G_AddGroup(const char *buffer)
273
{
274
    char buf[BMAX_PATH];
275
 
3176 helixhorne 276
    struct strllist *s = (struct strllist *)Bcalloc(1,sizeof(struct strllist));
2542 helixhorne 277
 
278
    Bstrcpy(buf, buffer);
279
 
280
    if (Bstrchr(buf,'.') == 0)
281
        Bstrcat(buf,".grp");
282
 
283
    s->str = Bstrdup(buf);
284
 
285
    if (CommandGrps)
286
    {
287
        struct strllist *t;
288
        for (t = CommandGrps; t->next; t=t->next) ;
289
        t->next = s;
290
        return;
291
    }
292
    CommandGrps = s;
293
}
294
 
295
void G_AddPath(const char *buffer)
296
{
3176 helixhorne 297
    struct strllist *s = (struct strllist *)Bcalloc(1,sizeof(struct strllist));
2542 helixhorne 298
    s->str = Bstrdup(buffer);
299
 
300
    if (CommandPaths)
301
    {
302
        struct strllist *t;
303
        for (t = CommandPaths; t->next; t=t->next) ;
304
        t->next = s;
305
        return;
306
    }
307
    CommandPaths = s;
308
}
2549 helixhorne 309
 
310
//////////
311
 
312
int32_t getatoken(scriptfile *sf, const tokenlist *tl, int32_t ntokens)
313
{
314
    char *tok;
315
    int32_t i;
316
 
317
    if (!sf) return T_ERROR;
318
    tok = scriptfile_gettoken(sf);
319
    if (!tok) return T_EOF;
320
 
321
    for (i=ntokens-1; i>=0; i--)
322
    {
323
        if (!Bstrcasecmp(tok, tl[i].text))
324
            return tl[i].tokenid;
325
    }
326
    return T_ERROR;
327
}
2554 helixhorne 328
 
329
//////////
330
 
2752 helixhorne 331
// returns: 1 if file could be opened, 0 else
332
int32_t testkopen(const char *filename, char searchfirst)
333
{
334
    int32_t fd = kopen4load(filename, searchfirst);
335
    if (fd >= 0)
336
        kclose(fd);
337
    return (fd >= 0);
338
}
339
 
2554 helixhorne 340
// checks from path and in ZIPs, returns 1 if NOT found
341
int32_t check_file_exist(const char *fn)
342
{
343
    int32_t opsm = pathsearchmode;
344
    char *tfn;
345
 
346
    pathsearchmode = 1;
347
    if (findfrompath(fn,&tfn) < 0)
348
    {
349
        char buf[BMAX_PATH];
350
 
351
        Bstrcpy(buf,fn);
352
        kzfindfilestart(buf);
353
        if (!kzfindfile(buf))
354
        {
355
            initprintf("Error: file \"%s\" does not exist\n",fn);
356
            pathsearchmode = opsm;
357
            return 1;
358
        }
359
    }
360
    else Bfree(tfn);
361
    pathsearchmode = opsm;
362
 
363
    return 0;
364
}
2555 helixhorne 365
 
366
 
367
//// FILE NAME / DIRECTORY LISTS ////
368
void fnlist_clearnames(fnlist_t *fnl)
369
{
370
    klistfree(fnl->finddirs);
371
    klistfree(fnl->findfiles);
372
 
373
    fnl->finddirs = fnl->findfiles = NULL;
374
    fnl->numfiles = fnl->numdirs = 0;
375
}
376
 
377
// dirflags, fileflags:
378
//  -1 means "don't get dirs/files",
379
//  otherwise ORed to flags for respective klistpath
380
int32_t fnlist_getnames(fnlist_t *fnl, const char *dirname, const char *pattern,
381
                        int32_t dirflags, int32_t fileflags)
382
{
383
    CACHE1D_FIND_REC *r;
384
 
385
    fnlist_clearnames(fnl);
386
 
387
    if (dirflags != -1)
388
        fnl->finddirs = klistpath(dirname, "*", CACHE1D_FIND_DIR|dirflags);
389
    if (fileflags != -1)
390
        fnl->findfiles = klistpath(dirname, pattern, CACHE1D_FIND_FILE|fileflags);
391
 
392
    for (r=fnl->finddirs; r; r=r->next)
393
        fnl->numdirs++;
394
    for (r=fnl->findfiles; r; r=r->next)
395
        fnl->numfiles++;
396
 
397
    return(0);
398
}
399
 
400
 
3269 terminx 401
// loads all group (grp, zip, pk3/4) files in the given directory
2555 helixhorne 402
void G_LoadGroupsInDir(const char *dirname)
403
{
3269 terminx 404
    static const char *extensions[4] = { "*.grp", "*.zip", "*.pk3", "*.pk4" };
2555 helixhorne 405
 
406
    char buf[BMAX_PATH];
407
    int32_t i;
408
 
409
    fnlist_t fnlist = FNLIST_INITIALIZER;
410
 
3269 terminx 411
    for (i=0; i<4; i++)
2555 helixhorne 412
    {
413
        CACHE1D_FIND_REC *rec;
414
 
415
        fnlist_getnames(&fnlist, dirname, extensions[i], -1, 0);
416
 
417
        for (rec=fnlist.findfiles; rec; rec=rec->next)
418
        {
419
            Bsnprintf(buf, sizeof(buf), "%s/%s", dirname, rec->name);
420
            initprintf("Using group file \"%s\".\n", buf);
421
            initgroupfile(buf);
422
        }
423
 
424
        fnlist_clearnames(&fnlist);
425
    }
426
}
427
 
428
void G_DoAutoload(const char *dirname)
429
{
430
    char buf[BMAX_PATH];
431
 
432
    Bsnprintf(buf, sizeof(buf), "autoload/%s", dirname);
433
    G_LoadGroupsInDir(buf);
434
}
2560 helixhorne 435
 
436
////
437
 
438
// returns a buffer of size BMAX_PATH
439
char *dup_filename(const char *fn)
440
{
3176 helixhorne 441
    char *buf = (char *)Bmalloc(BMAX_PATH);
2560 helixhorne 442
 
443
    return Bstrncpyz(buf, fn, BMAX_PATH);
444
}
3004 helixhorne 445
 
446
 
447
// Copy FN to WBUF and append an extension if it's not there, which is checked
448
// case-insensitively.
449
// Returns: 1 if not all characters could be written to WBUF, 0 else.
450
int32_t maybe_append_ext(char *wbuf, int32_t wbufsiz, const char *fn, const char *ext)
451
{
452
    const int32_t slen=Bstrlen(fn), extslen=Bstrlen(ext);
453
    const int32_t haveext = (slen>=extslen && Bstrcasecmp(&fn[slen-extslen], ext)==0);
454
 
455
    Bassert((intptr_t)wbuf != (intptr_t)fn);  // no aliasing
456
 
457
    // If 'fn' has no extension suffixed, append one.
458
    return (Bsnprintf(wbuf, wbufsiz, "%s%s", fn, haveext ? "" : ext) >= wbufsiz);
459
}
3243 helixhorne 460
 
461
 
3489 helixhorne 462
// Approximations to 2D and 3D Euclidean distances. Initial EDuke32 SVN import says
3243 helixhorne 463
// in jmact/mathutil.c: "Ken's reverse-engineering job".
464
// Note that jmact/mathutil.c contains practically the same code, but where the
465
// individual x/y(/z) distances are passed instead.
466
int32_t ldist(const spritetype *s1, const spritetype *s2)
467
{
468
    int32_t x = klabs(s1->x-s2->x);
469
    int32_t y = klabs(s1->y-s2->y);
470
 
471
    if (x<y) swaplong(&x,&y);
472
 
473
    {
474
        int32_t t = y + (y>>1);
475
        return (x - (x>>5) - (x>>7)  + (t>>2) + (t>>6));
476
    }
477
}
478
 
479
int32_t dist(const spritetype *s1, const spritetype *s2)
480
{
481
    int32_t x = klabs(s1->x-s2->x);
482
    int32_t y = klabs(s1->y-s2->y);
483
    int32_t z = klabs((s1->z-s2->z)>>4);
484
 
485
    if (x<y) swaplong(&x,&y);
486
    if (x<z) swaplong(&x,&z);
487
 
488
    {
489
        int32_t t = y + z;
490
        return (x - (x>>4) + (t>>2) + (t>>3));
491
    }
492
}
3321 helixhorne 493
 
494
 
495
// Clear OSD background
496
void COMMON_clearbackground(int32_t numcols, int32_t numrows)
497
{
498
    UNREFERENCED_PARAMETER(numcols);
499
 
500
# ifdef USE_OPENGL
501
    if (rendmode>=3 && qsetmode==200)
502
    {
503
        setpolymost2dview();
504
        bglColor4f(0,0,0,0.67f);
505
        bglEnable(GL_BLEND);
506
        bglRectd(0,0, xdim,8*numrows+8);
507
        bglColor4f(0,0,0,1);
508
        bglRectd(0,8*numrows+4, xdim,8*numrows+8);
509
        return;
510
    }
511
# endif
512
 
513
    CLEARLINES2D(0, min(ydim, numrows*8+8), editorcolors[16]);
514
}