Subversion Repositories eduke32

Rev

Rev 2560 | Rev 2752 | 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"
2542 helixhorne 11
 
12
#include "common.h"
2726 hendricks2 13
#include "common_game.h"
2542 helixhorne 14
 
2726 hendricks2 15
int32_t g_gameType = 0;
2542 helixhorne 16
 
2726 hendricks2 17
// grp/con/def handling
18
 
19
char *defaultgamegrp[GAMECOUNT] = { "DUKE3D.GRP", "NAM.GRP", "NAPALM.GRP", "WW2GI.GRP" };
20
char *defaultdeffilename[GAMECOUNT] = { "duke3d.def", "nam.def", "napalm.def", "ww2gi.def" };
21
char *defaultconfilename = "GAME.CON";
22
char *defaultgameconfilename[GAMECOUNT] = { "EDUKE.CON", "NAM.CON", "NAPALM.CON", "WW2GI.CON" };
23
 
24
// g_grpNamePtr can ONLY point to a malloc'd block (length BMAX_PATH)
25
char *g_grpNamePtr = NULL;
26
// g_defNamePtr can ONLY point to a malloc'd block (length BMAX_PATH)
27
char *g_defNamePtr = NULL;
28
// g_scriptNamePtr can ONLY point to a malloc'd block (length BMAX_PATH)
29
char *g_scriptNamePtr = NULL;
30
 
31
 
32
void clearGrpNamePtr(void)
33
{
34
    if (g_grpNamePtr != NULL)
35
        Bfree(g_grpNamePtr);
36
    // g_grpNamePtr assumed to be assigned to right after
37
}
38
 
39
void clearDefNamePtr(void)
40
{
41
    if (g_defNamePtr != NULL)
42
        Bfree(g_defNamePtr);
43
    // g_defNamePtr assumed to be assigned to right after
44
}
45
 
46
void clearScriptNamePtr(void)
47
{
48
    if (g_scriptNamePtr != NULL)
49
        Bfree(g_scriptNamePtr);
50
    // g_scriptNamePtr assumed to be assigned to right after
51
}
52
 
53
char *G_DefaultGrpFile(void)
54
{
55
    if (DUKE)
56
        return defaultgamegrp[GAME_DUKE];
57
    // order is important for the following three because GAMEFLAG_NAM overlaps all
58
    else if (NAPALM)
59
        return defaultgamegrp[GAME_NAPALM];
60
    else if (WW2GI)
61
        return defaultgamegrp[GAME_WW2GI];
62
    else if (NAM)
63
        return defaultgamegrp[GAME_NAM];
64
 
65
    return defaultgamegrp[0];
66
}
67
char *G_DefaultDefFile(void)
68
{
69
    if (DUKE)
70
        return defaultdeffilename[GAME_DUKE];
71
    else if (WW2GI)
72
        return defaultdeffilename[GAME_WW2GI];
73
    else if (NAPALM)
74
    {
75
        if ((kopen4load(defaultdeffilename[GAME_NAPALM],0) < 0) && (kopen4load(defaultdeffilename[GAME_NAM],0) >= 0))
76
            return defaultdeffilename[GAME_NAM]; // NAM/Napalm Sharing
77
        else
78
            return defaultdeffilename[GAME_NAPALM];
79
    }
80
    else if (NAM)
81
    {
82
        if ((kopen4load(defaultdeffilename[GAME_NAM],0) < 0) && (kopen4load(defaultdeffilename[GAME_NAPALM],0) >= 0))
83
            return defaultdeffilename[GAME_NAPALM]; // NAM/Napalm Sharing
84
        else
85
            return defaultdeffilename[GAME_NAM];
86
    }
87
 
88
    return defaultdeffilename[0];
89
}
90
char *G_DefaultConFile(void)
91
{
92
    if (DUKE && (kopen4load(defaultgameconfilename[GAME_DUKE],0) >= 0))
93
        return defaultgameconfilename[GAME_DUKE];
94
    else if (WW2GI && (kopen4load(defaultgameconfilename[GAME_WW2GI],0) >= 0))
95
        return defaultgameconfilename[GAME_WW2GI];
96
    else if (NAPALM)
97
    {
98
        if (kopen4load(defaultgameconfilename[GAME_NAPALM],0) < 0)
99
        {
100
            if (kopen4load(defaultgameconfilename[GAME_NAM],0) >= 0)
101
                return defaultgameconfilename[GAME_NAM]; // NAM/Napalm Sharing
102
        }
103
        else
104
            return defaultgameconfilename[GAME_NAPALM];
105
    }
106
    else if (NAM)
107
    {
108
        if (kopen4load(defaultgameconfilename[GAME_NAM],0) < 0)
109
        {
110
            if (kopen4load(defaultgameconfilename[GAME_NAPALM],0) >= 0)
111
                return defaultgameconfilename[GAME_NAPALM]; // NAM/Napalm Sharing
112
        }
113
        else
114
            return defaultgameconfilename[GAME_NAM];
115
    }
116
 
117
    return defaultconfilename;
118
}
119
 
120
char *G_GrpFile(void)
121
{
122
    if (g_grpNamePtr == NULL)
123
        return G_DefaultGrpFile();
124
    else
125
        return g_grpNamePtr;
126
}
127
char *G_DefFile(void)
128
{
129
    if (g_defNamePtr == NULL)
130
        return G_DefaultDefFile();
131
    else
132
        return g_defNamePtr;
133
}
134
char *G_ConFile(void)
135
{
136
    if (g_scriptNamePtr == NULL)
137
        return G_DefaultConFile();
138
    else
139
        return g_scriptNamePtr;
140
}
141
 
142
//////////
143
 
2542 helixhorne 144
struct strllist *CommandPaths, *CommandGrps;
145
 
146
void G_AddGroup(const char *buffer)
147
{
148
    char buf[BMAX_PATH];
149
 
150
    struct strllist *s = Bcalloc(1,sizeof(struct strllist));
151
 
152
    Bstrcpy(buf, buffer);
153
 
154
    if (Bstrchr(buf,'.') == 0)
155
        Bstrcat(buf,".grp");
156
 
157
    s->str = Bstrdup(buf);
158
 
159
    if (CommandGrps)
160
    {
161
        struct strllist *t;
162
        for (t = CommandGrps; t->next; t=t->next) ;
163
        t->next = s;
164
        return;
165
    }
166
    CommandGrps = s;
167
}
168
 
169
void G_AddPath(const char *buffer)
170
{
171
    struct strllist *s = Bcalloc(1,sizeof(struct strllist));
172
    s->str = Bstrdup(buffer);
173
 
174
    if (CommandPaths)
175
    {
176
        struct strllist *t;
177
        for (t = CommandPaths; t->next; t=t->next) ;
178
        t->next = s;
179
        return;
180
    }
181
    CommandPaths = s;
182
}
2549 helixhorne 183
 
184
//////////
185
 
186
int32_t getatoken(scriptfile *sf, const tokenlist *tl, int32_t ntokens)
187
{
188
    char *tok;
189
    int32_t i;
190
 
191
    if (!sf) return T_ERROR;
192
    tok = scriptfile_gettoken(sf);
193
    if (!tok) return T_EOF;
194
 
195
    for (i=ntokens-1; i>=0; i--)
196
    {
197
        if (!Bstrcasecmp(tok, tl[i].text))
198
            return tl[i].tokenid;
199
    }
200
    return T_ERROR;
201
}
2554 helixhorne 202
 
203
//////////
204
 
205
// checks from path and in ZIPs, returns 1 if NOT found
206
int32_t check_file_exist(const char *fn)
207
{
208
    int32_t opsm = pathsearchmode;
209
    char *tfn;
210
 
211
    pathsearchmode = 1;
212
    if (findfrompath(fn,&tfn) < 0)
213
    {
214
        char buf[BMAX_PATH];
215
 
216
        Bstrcpy(buf,fn);
217
        kzfindfilestart(buf);
218
        if (!kzfindfile(buf))
219
        {
220
            initprintf("Error: file \"%s\" does not exist\n",fn);
221
            pathsearchmode = opsm;
222
            return 1;
223
        }
224
    }
225
    else Bfree(tfn);
226
    pathsearchmode = opsm;
227
 
228
    return 0;
229
}
2555 helixhorne 230
 
231
 
232
//// FILE NAME / DIRECTORY LISTS ////
233
void fnlist_clearnames(fnlist_t *fnl)
234
{
235
    klistfree(fnl->finddirs);
236
    klistfree(fnl->findfiles);
237
 
238
    fnl->finddirs = fnl->findfiles = NULL;
239
    fnl->numfiles = fnl->numdirs = 0;
240
}
241
 
242
// dirflags, fileflags:
243
//  -1 means "don't get dirs/files",
244
//  otherwise ORed to flags for respective klistpath
245
int32_t fnlist_getnames(fnlist_t *fnl, const char *dirname, const char *pattern,
246
                        int32_t dirflags, int32_t fileflags)
247
{
248
    CACHE1D_FIND_REC *r;
249
 
250
    fnlist_clearnames(fnl);
251
 
252
    if (dirflags != -1)
253
        fnl->finddirs = klistpath(dirname, "*", CACHE1D_FIND_DIR|dirflags);
254
    if (fileflags != -1)
255
        fnl->findfiles = klistpath(dirname, pattern, CACHE1D_FIND_FILE|fileflags);
256
 
257
    for (r=fnl->finddirs; r; r=r->next)
258
        fnl->numdirs++;
259
    for (r=fnl->findfiles; r; r=r->next)
260
        fnl->numfiles++;
261
 
262
    return(0);
263
}
264
 
265
 
266
// loads all group (grp, zip, pk3) files in the given directory
267
void G_LoadGroupsInDir(const char *dirname)
268
{
269
    static const char *extensions[3] = { "*.grp", "*.zip", "*.pk3" };
270
 
271
    char buf[BMAX_PATH];
272
    int32_t i;
273
 
274
    fnlist_t fnlist = FNLIST_INITIALIZER;
275
 
276
    for (i=0; i<3; i++)
277
    {
278
        CACHE1D_FIND_REC *rec;
279
 
280
        fnlist_getnames(&fnlist, dirname, extensions[i], -1, 0);
281
 
282
        for (rec=fnlist.findfiles; rec; rec=rec->next)
283
        {
284
            Bsnprintf(buf, sizeof(buf), "%s/%s", dirname, rec->name);
285
            initprintf("Using group file \"%s\".\n", buf);
286
            initgroupfile(buf);
287
        }
288
 
289
        fnlist_clearnames(&fnlist);
290
    }
291
}
292
 
293
void G_DoAutoload(const char *dirname)
294
{
295
    char buf[BMAX_PATH];
296
 
297
    Bsnprintf(buf, sizeof(buf), "autoload/%s", dirname);
298
    G_LoadGroupsInDir(buf);
299
}
2560 helixhorne 300
 
301
////
302
 
303
// returns a buffer of size BMAX_PATH
304
char *dup_filename(const char *fn)
305
{
306
    char *buf = Bmalloc(BMAX_PATH);
307
 
308
    return Bstrncpyz(buf, fn, BMAX_PATH);
309
}