-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathzb_spawn.c
474 lines (366 loc) · 10.2 KB
/
zb_spawn.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
Copyright (C) 2000 Shane Powell
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
//
// q2admin
//
// zb_spawn.c
//
// copyright 2000 Shane Powell
//
#include "g_local.h"
#define SPAWNFILE "q2adminspawn.txt"
#define SPAWN_MAXCMDS 50
typedef struct
{
char *spawncmd;
byte type;
qboolean onelevelflag;
regex_t *r;
}
spawncmd_t;
#define SPAWN_SW 0
#define SPAWN_EX 1
#define SPAWN_RE 2
spawncmd_t spawncmds[SPAWN_MAXCMDS];
long long maxspawn_cmds = 0;
int entity_classname_offset = (sizeof(struct edict_s)) + 20; // default byte offset to the classname variable.
qboolean ReadSpawnFile(char *spawnname, qboolean onelevelflag)
{
FILE *spawnfile;
unsigned int uptoLine = 0;
if(maxspawn_cmds >= SPAWN_MAXCMDS)
{
return FALSE;
}
spawnfile = fopen(spawnname, "rt");
if(!spawnfile)
{
return FALSE;
}
while(fgets(buffer, 256, spawnfile))
{
char *cp = buffer;
size_t len;
// remove '\n'
len = q2a_strlen(buffer) - 1;
if(buffer[len] == '\n')
{
buffer[len] = 0x0;
}
SKIPBLANK(cp);
uptoLine++;
if(startContains (cp, "SW:") || startContains (cp, "EX:") || startContains (cp, "RE:"))
{
// looks ok, add...
switch(*cp)
{
case 'S':
spawncmds[maxspawn_cmds].type = SPAWN_SW;
break;
case 'E':
spawncmds[maxspawn_cmds].type = SPAWN_EX;
break;
case 'R':
spawncmds[maxspawn_cmds].type = SPAWN_RE;
break;
}
spawncmds[maxspawn_cmds].onelevelflag = onelevelflag;
cp += 3;
SKIPBLANK(cp);
len = q2a_strlen(cp) + 1;
// zero length command
if(!len)
{
gi.dprintf ("Error loading SPAWN from line %d in file %s\n", uptoLine, spawnname);
continue;
}
spawncmds[maxspawn_cmds].spawncmd = gi.TagMalloc ((int)len, TAG_GAME);
q2a_strcpy(spawncmds[maxspawn_cmds].spawncmd, cp);
if(spawncmds[maxspawn_cmds].type == SPAWN_RE)
{
q_strupr(cp);
spawncmds[maxspawn_cmds].r = gi.TagMalloc (sizeof(*spawncmds[maxspawn_cmds].r), TAG_GAME);
q2a_memset(spawncmds[maxspawn_cmds].r, 0x0, sizeof(*spawncmds[maxspawn_cmds].r));
// if(regcomp(spawncmds[maxspawn_cmds].r, strbuffer, REG_EXTENDED))
if(regcomp(spawncmds[maxspawn_cmds].r, cp, 0))
{
gi.TagFree(spawncmds[maxspawn_cmds].r);
spawncmds[maxspawn_cmds].r = 0;
// malformed re... skip this spawn command
gi.dprintf ("Error loading SPAWN from line %d in file %s\n", uptoLine, spawnname);
continue;
}
}
else
{
spawncmds[maxspawn_cmds].r = 0;
}
maxspawn_cmds++;
if(maxspawn_cmds >= SPAWN_MAXCMDS)
{
break;
}
}
else if(!(cp[0] == ';' || cp[0] == '\n' || isBlank (cp)))
{
gi.dprintf ("Error loading SPAWN from line %d in file %s\n", uptoLine, spawnname);
}
}
fclose(spawnfile);
return TRUE;
}
void freeSpawnLists(void)
{
while(maxspawn_cmds)
{
maxspawn_cmds--;
gi.TagFree(spawncmds[maxspawn_cmds].spawncmd);
if(spawncmds[maxspawn_cmds].r)
{
regfree(spawncmds[maxspawn_cmds].r);
gi.TagFree(spawncmds[maxspawn_cmds].r);
}
}
}
void freeOneLevelSpawnLists(void)
{
long long spawn = 0;
while(spawn < maxspawn_cmds)
{
if(spawncmds[spawn].onelevelflag)
{
gi.TagFree(spawncmds[spawn].spawncmd);
if(spawncmds[spawn].r)
{
regfree(spawncmds[spawn].r);
gi.TagFree(spawncmds[spawn].r);
}
if(spawn + 1 < maxspawn_cmds)
{
q2a_memmove((spawncmds + spawn), (spawncmds + spawn + 1), sizeof(spawncmd_t) * (maxspawn_cmds - spawn));
}
maxspawn_cmds--;
}
else
{
spawn++;
}
}
}
void readSpawnLists(void)
{
qboolean ret;
freeSpawnLists();
ret = ReadSpawnFile(SPAWNFILE, FALSE);
sprintf(buffer, "%s/%s", moddir, SPAWNFILE);
if(ReadSpawnFile(buffer, FALSE))
{
ret = TRUE;
}
if(!ret)
{
gi.dprintf ("WARNING: " SPAWNFILE " could not be found\n");
logEvent(LT_INTERNALWARN, 0, NULL, SPAWNFILE " could not be found", IW_SPAWNSETUPLOAD, 0.0);
}
}
void reloadSpawnFileRun(int startarg, edict_t *ent, int client)
{
readSpawnLists();
gi.cprintf (ent, PRINT_HIGH, "Disbled entities reloaded.\n");
}
qboolean checkforspawncmd(char *cp, int spawncmd)
{
switch(spawncmds[spawncmd].type)
{
case SPAWN_SW:
return startContains (cp, spawncmds[spawncmd].spawncmd);
case SPAWN_EX:
return !Q_stricmp(cp, spawncmds[spawncmd].spawncmd);
case SPAWN_RE:
return (regexec(spawncmds[spawncmd].r, cp, 0, 0, 0) != REG_NOMATCH);
}
return FALSE;
}
qboolean checkDisabledEntities(char *cp)
{
unsigned int i;
q2a_strcpy(buffer, cp);
q_strupr(buffer);
for(i = 0; i < maxspawn_cmds; i++)
{
if(checkforspawncmd(buffer, i))
{
return TRUE;
}
}
return FALSE;
}
//===================================================================
void listspawnsRun(int startarg, edict_t *ent, int client)
{
addCmdQueue(client, QCMD_DISPSPAWN, 0, 0, 0);
gi.cprintf (ent, PRINT_HIGH, "Start disbled-entities List:\n");
}
void displayNextSpawn(edict_t *ent, int client, long spawncmd)
{
if(spawncmd < maxspawn_cmds)
{
switch(spawncmds[spawncmd].type)
{
case SPAWN_SW:
gi.cprintf (ent, PRINT_HIGH, "%4d SW:\"%s\"\n", spawncmd + 1, spawncmds[spawncmd].spawncmd);
break;
case SPAWN_EX:
gi.cprintf (ent, PRINT_HIGH, "%4d EX:\"%s\"\n", spawncmd + 1, spawncmds[spawncmd].spawncmd);
break;
case SPAWN_RE:
gi.cprintf (ent, PRINT_HIGH, "%4d RE:\"%s\"\n", spawncmd + 1, spawncmds[spawncmd].spawncmd);
break;
}
spawncmd++;
addCmdQueue(client, QCMD_DISPSPAWN, 0, spawncmd, 0);
}
else
{
gi.cprintf (ent, PRINT_HIGH, "End disbled-entities List\n");
}
}
#define SPAWNCMD "[sv] !spawncmd [SW/EX/RE] \"command\"\n"
void spawncmdRun(int startarg, edict_t *ent, int client)
{
char *cmd;
size_t len;
if(maxspawn_cmds >= SPAWN_MAXCMDS)
{
gi.cprintf (ent, PRINT_HIGH, "Sorry, maximum number of disbled-entitie commands has been reached.\n");
return;
}
if (gi.argc() <= startarg + 1)
{
gi.cprintf (ent, PRINT_HIGH, SPAWNCMD);
return;
}
cmd = gi.argv(startarg);
if(Q_stricmp(cmd, "SW") == 0)
{
spawncmds[maxspawn_cmds].type = SPAWN_SW;
}
else if(Q_stricmp(cmd, "EX") == 0)
{
spawncmds[maxspawn_cmds].type = SPAWN_EX;
}
else if(Q_stricmp(cmd, "RE") == 0)
{
spawncmds[maxspawn_cmds].type = SPAWN_RE;
}
else
{
gi.cprintf (ent, PRINT_HIGH, SPAWNCMD);
return;
}
spawncmds[maxspawn_cmds].onelevelflag = FALSE;
cmd = gi.argv(startarg + 1);
if(isBlank(cmd))
{
gi.cprintf (ent, PRINT_HIGH, SPAWNCMD);
return;
}
len = q2a_strlen(cmd) + 20;
spawncmds[maxspawn_cmds].spawncmd = gi.TagMalloc ((int)len, TAG_GAME);
processstring(spawncmds[maxspawn_cmds].spawncmd, cmd, (int)len - 1, 0);
// q2a_strcpy(spawncmds[maxspawn_cmds].spawncmd, cmd);
if(spawncmds[maxspawn_cmds].type == SPAWN_RE)
{
q_strupr(cmd);
spawncmds[maxspawn_cmds].r = gi.TagMalloc (sizeof(*spawncmds[maxspawn_cmds].r), TAG_GAME);
q2a_memset(spawncmds[maxspawn_cmds].r, 0x0, sizeof(*spawncmds[maxspawn_cmds].r));
// if(regcomp(spawncmds[maxspawn_cmds].r, cmd, REG_EXTENDED))
if(regcomp(spawncmds[maxspawn_cmds].r, cmd, 0))
{
gi.TagFree(spawncmds[maxspawn_cmds].spawncmd);
gi.TagFree(spawncmds[maxspawn_cmds].r);
spawncmds[maxspawn_cmds].r = 0;
// malformed re...
gi.cprintf (ent, PRINT_HIGH, "Regular expression couldn't compile!\n");
return;
}
}
else
{
spawncmds[maxspawn_cmds].r = 0;
}
switch(spawncmds[maxspawn_cmds].type)
{
case SPAWN_SW:
gi.cprintf (ent, PRINT_HIGH, "%4d SW:\"%s\" added\n", maxspawn_cmds + 1, spawncmds[maxspawn_cmds].spawncmd);
break;
case SPAWN_EX:
gi.cprintf (ent, PRINT_HIGH, "%4d EX:\"%s\" added\n", maxspawn_cmds + 1, spawncmds[maxspawn_cmds].spawncmd);
break;
case SPAWN_RE:
gi.cprintf (ent, PRINT_HIGH, "%4d RE:\"%s\" added\n", maxspawn_cmds + 1, spawncmds[maxspawn_cmds].spawncmd);
break;
}
maxspawn_cmds++;
}
#define SPAWNDELCMD "[sv] !spawndel spawnnum\n"
void spawnDelRun(int startarg, edict_t *ent, int client)
{
size_t spawn;
if (gi.argc() <= startarg)
{
gi.cprintf (ent, PRINT_HIGH, SPAWNDELCMD);
return;
}
spawn = q2a_atoi(gi.argv(startarg));
if(spawn < 1 || spawn > maxspawn_cmds)
{
gi.cprintf (ent, PRINT_HIGH, SPAWNDELCMD);
return;
}
spawn--;
gi.TagFree(spawncmds[spawn].spawncmd);
if(spawncmds[spawn].r)
{
regfree(spawncmds[spawn].r);
gi.TagFree(spawncmds[spawn].r);
}
if(spawn + 1 < maxspawn_cmds)
{
q2a_memmove((spawncmds + spawn), (spawncmds + spawn + 1), sizeof(spawncmd_t) * (maxspawn_cmds - spawn));
}
maxspawn_cmds--;
gi.cprintf (ent, PRINT_HIGH, "Disbled-entities command deleted\n");
}
void linkentity_internal(edict_t *ent)
{
if(spawnentities_internal_enable && spawnentities_enable)
{
if(checkDisabledEntities(*((char **)((uintptr_t)ent + entity_classname_offset))))
{
char **classnameptr = ((char **)((uintptr_t)ent + entity_classname_offset));
*classnameptr = NULL;
ent->inuse = FALSE;
return;
}
}
logEvent(LT_ENTITYCREATE, 0, NULL, *((char **)((uintptr_t)ent + entity_classname_offset)), 0, 0.0);
gi.linkentity(ent);
}
void unlinkentity_internal(edict_t *ent)
{
logEvent(LT_ENTITYDELETE, 0, NULL, *((char **)((uintptr_t)ent + entity_classname_offset)), 0, 0.0);
gi.unlinkentity(ent);
}