Subversion Repositories eduke32

Rev

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