Subversion Repositories eduke32

Rev

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