Subversion Repositories eduke32

Rev

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