-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.qc
537 lines (436 loc) · 11.4 KB
/
main.qc
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
const string GAMEVERSION = "progs";
// extended features
typedef enumflags : int
{
GMF_CLIENTNUM,
GMF_PROPERINUSE,
GMF_MVDSPEC,
GMF_WANT_ALL_DISCONNECTS,
GMF_ENHANCED_SAVEGAMES = 0x00000400,
GMF_VARIABLE_FPS,
GMF_EXTRA_USERINFO,
GMF_IPV6_ADDRESS_AWARE
} game_features_t;
// features this game supports
const game_features_t G_FEATURES = (GMF_PROPERINUSE | GMF_WANT_ALL_DISCONNECTS | GMF_ENHANCED_SAVEGAMES);
/*
============
InitGame
This will be called when the QC is first loaded, which
only happens when a new game is started or a save game
is loaded.
============
*/
static void() InitGame =
{
gi.dprintf("===== "__FUNC__" =====\n");
//FIXME: sv_ prefix is wrong for these
sv_rollspeed = gi.cvar("sv_rollspeed", "200", 0);
sv_rollangle = gi.cvar("sv_rollangle", "2", 0);
sv_maxvelocity = gi.cvar("sv_maxvelocity", "2000", 0);
sv_gravity = gi.cvar("sv_gravity", "800", 0);
// noset vars
dedicated = gi.cvar("dedicated", "0", CVAR_NOSET);
// latched vars
sv_cheats = gi.cvar("cheats", "0", CVAR_SERVERINFO | CVAR_LATCH);
gi.cvar("gamename", GAMEVERSION, CVAR_SERVERINFO | CVAR_LATCH);
gi.cvar("gamedate", __DATE__ , CVAR_SERVERINFO | CVAR_LATCH);
cvar_t maxclients = gi.cvar("maxclients", "4", CVAR_SERVERINFO | CVAR_LATCH);
maxspectators = gi.cvar("maxspectators", "4", CVAR_SERVERINFO);
#ifdef SINGLE_PLAYER
deathmatch = gi.cvar("deathmatch", "0", CVAR_LATCH);
coop = gi.cvar("coop", "0", CVAR_LATCH);
skill = gi.cvar("skill", "1", CVAR_LATCH);
#else
gi.cvar("deathmatch", "1", CVAR_NOSET);
gi.cvar_forceset("deathmatch", "1");
gi.cvar_forceset("coop", "0");
#endif
maxentities = gi.cvar("maxentities", "1024", CVAR_LATCH);
// change anytime vars
dmflags = gi.cvar("dmflags", "0", CVAR_SERVERINFO);
fraglimit = gi.cvar("fraglimit", "0", CVAR_SERVERINFO);
timelimit = gi.cvar("timelimit", "0", CVAR_SERVERINFO);
#ifdef CTF
capturelimit = gi.cvar("capturelimit", "0", CVAR_SERVERINFO);
instantweap = gi.cvar("instantweap", "0", CVAR_SERVERINFO);
#endif
password = gi.cvar("password", "", CVAR_USERINFO);
spectator_password = gi.cvar("spectator_password", "", CVAR_USERINFO);
needpass = gi.cvar("needpass", "0", CVAR_SERVERINFO);
filterban = gi.cvar("filterban", "1", 0);
g_select_empty = gi.cvar("g_select_empty", "0", CVAR_ARCHIVE);
run_pitch = gi.cvar("run_pitch", "0.002", 0);
run_roll = gi.cvar("run_roll", "0.005", 0);
bob_up = gi.cvar("bob_up", "0.005", 0);
bob_pitch = gi.cvar("bob_pitch", "0.002", 0);
bob_roll = gi.cvar("bob_roll", "0.002", 0);
// flood control
flood_msgs = gi.cvar("flood_msgs", "4", 0);
flood_persecond = gi.cvar("flood_persecond", "4", 0);
flood_waitdelay = gi.cvar("flood_waitdelay", "10", 0);
// dm map list
sv_maplist = gi.cvar("sv_maplist", "", 0);
// obtain server features
sv_features = gi.cvar("sv_features", "", 0);
// export our own features
gi.cvar_forceset("g_features", va("%i", G_FEATURES));
game.maxclients = maxclients.intVal;
game.maxentities = maxentities.intVal;
// sync with Q2
globals.num_edicts = game.maxclients + 1;
InitItems();
#ifdef CTF
CTFInit();
#endif
};
static void() ShutdownGame =
{
gi.dprintf("===== "__FUNC__" =====\n");
};
// from spawn.qc
void() PreSpawnEntities;
void(string mapname, string entities, string spawnpoint) SpawnEntities;
void() PostSpawnEntities;
/*
=================
ClientEndServerFrames
=================
*/
static void() ClientEndServerFrames =
{
// calc the player views now that all pushing
// and damage has been added
for (int i = 0; i < game.maxclients; i++)
{
entity ent = itoe(1 + i);
if (!ent.inuse || !ent.is_client)
continue;
ClientEndServerFrame(ent);
}
}
/*
=================
CreateTargetChangeLevel
Returns the created target changelevel
=================
*/
static entity(string new_map) CreateTargetChangeLevel =
{
entity ent = G_Spawn();
ent.classname = "target_changelevel";
level.nextmap = new_map;
ent.map = level.nextmap;
return ent;
}
/*
=================
EndDMLevel
The timelimit or fraglimit has been exceeded
=================
*/
void() EndDMLevel =
{
entity ent;
// stay on same level flag
if (dmflags.intVal & DF_SAME_LEVEL) {
BeginIntermission(CreateTargetChangeLevel(level.mapname));
return;
}
// see if it's in the map list
if (sv_maplist.str) {
string st = sv_maplist.str;
string f = 0;
int s_offset = 0;
string t = strtok(st, s_offset);
while (t) {
if (striequals(t, level.mapname))
{
// it's in the list, go to the next one
t = strtok(st, s_offset);
if (!t)
{ // end of list, go to first one
if (!f) // there isn't a first one, same level
BeginIntermission(CreateTargetChangeLevel(level.mapname));
else
BeginIntermission(CreateTargetChangeLevel(f));
}
else
BeginIntermission(CreateTargetChangeLevel(t));
return;
}
if (!f)
f = t;
t = strtok(st, s_offset);
}
}
if (level.nextmap) // go to a specific map
BeginIntermission(CreateTargetChangeLevel(level.nextmap));
else
{ // search for a changelevel
ent = G_Find(world, classname, "target_changelevel");
if !(ent)
{
// the map designer didn't include a changelevel,
// so create a fake ent that goes back to the same level
BeginIntermission(CreateTargetChangeLevel(level.mapname));
return;
}
BeginIntermission(ent);
}
}
/*
=================
CheckNeedPass
=================
*/
static void() CheckNeedPass =
{
int need;
// if password or spectator_password has changed, update needpass
// as needed
if (password.modified || spectator_password.modified) {
password.modified = spectator_password.modified = false;
need = 0;
if (password.str && !striequals(password.str, "none"))
need |= 1;
if (spectator_password.str && !striequals(spectator_password.str, "none"))
need |= 2;
gi.cvar_set("needpass", va("%d", need));
}
}
/*
=================
CheckDMRules
=================
*/
static void() CheckDMRules =
{
if (level.intermission_framenum)
return;
#ifdef SINGLE_PLAYER
if (!deathmatch.intVal)
return;
#endif
#ifdef CTF
if (ctf.intVal && CTFCheckRules())
{
EndDMLevel ();
return;
}
#endif
if (timelimit.intVal)
{
if (level.time >= timelimit.intVal * 60)
{
gi.bprintf(PRINT_HIGH, "Timelimit hit.\n");
EndDMLevel();
return;
}
}
if (fraglimit.intVal)
{
for (int i = 0; i < game.maxclients; i++)
{
entity cl = itoe(i + 1);
if (!cl.inuse)
continue;
if (cl.client.resp.score >= fraglimit.intVal)
{
gi.bprintf(PRINT_HIGH, "Fraglimit hit.\n");
EndDMLevel();
return;
}
}
}
}
/*
=============
ExitLevel
=============
*/
static void(void) ExitLevel =
{
level.exitintermission = 0;
level.intermission_framenum = 0;
gi.AddCommandString(va("gamemap \"%s\"\n", level.changemap));
level.changemap = 0;
ClientEndServerFrames();
// clear some things before going to next level
for (int i = 0; i < game.maxclients; i++)
{
entity ent = itoe(1 + i);
if (!ent.inuse)
continue;
if (ent.health > ent.max_health)
ent.health = ent.max_health;
}
}
// from player.qc
void(entity) ClientBeginServerFrame;
void(entity, usercmd_t) ClientThink;
/*
================
RunFrame
Advances the world by 0.1 seconds
================
*/
static void() RunFrame =
{
level.framenum++;
level.time = level.framenum * FRAMETIME;
#ifdef SINGLE_PLAYER
// choose a client for monsters to target this frame
AI_SetSightClient();
#endif
// exit intermissions
if (level.exitintermission)
{
ExitLevel();
return;
}
//
// treat each object in turn
// even the world gets a chance to think
//
for (int i = 0 ; i < globals.num_edicts; i++)
{
entity ent = itoe(i);
if (!ent.inuse)
continue;
level.current_entity = ent;
ent.s.old_origin = ent.s.origin;
// if the ground entity moved, make sure we are still on it
if ((ent.groundentity != null_entity) && (ent.groundentity.linkcount != ent.groundentity_linkcount))
#ifdef SINGLE_PLAYER
{
#endif
ent.groundentity = null_entity;
#ifdef SINGLE_PLAYER
if (!(ent.flags & (FL_SWIM | FL_FLY)) && (ent.svflags & SVF_MONSTER))
M_CheckGround(ent);
}
#endif
if (i > 0 && i <= game.maxclients)
ClientBeginServerFrame(ent);
G_RunEntity(ent);
}
// see if it is time to end a deathmatch
CheckDMRules();
// see if needpass needs updated
CheckNeedPass();
// build the playerstate_t structures for all players
ClientEndServerFrames();
}
static void(bool autosave) PreWriteGame =
{
#ifdef SINGLE_PLAYER
if (!autosave)
SaveClientData();
game.autosaved = autosave;
#endif
};
static void(bool autosave) PostWriteGame =
{
#ifdef SINGLE_PLAYER
game.autosaved = false;
#endif
};
static void() PreReadGame =
{
};
static void() PostReadGame =
{
};
static void() PreWriteLevel =
{
};
static void() PostWriteLevel =
{
};
static void() PreReadLevel =
{
};
static void(int num_edicts) PostReadLevel =
{
#ifdef SINGLE_PLAYER
globals.num_edicts = num_edicts;
// mark all clients as unconnected
for (int i = 0 ; i < game.maxclients; i++)
{
entity ent = itoe(i + 1);
ent.client.pers.connected = false;
}
// do any load time things at this point
for (int i = 0 ; i < globals.num_edicts; i++)
{
entity ent = itoe(i);
if (!ent.inuse)
continue;
// fire any cross-level triggers
if (ent.classname == "target_crosslevel_target")
ent.nextthink = level.framenum + (int)(ent.delay * BASE_FRAMERATE);
}
#endif
};
struct game_export_t {
int apiversion;
int clientsize;
void() InitGame;
void() ShutdownGame;
void() PreSpawnEntities;
void(string mapname, string entities, string spawnpoint) SpawnEntities;
void() PostSpawnEntities;
bool(entity ent, inout string userinfo) ClientConnect;
void(entity ent) ClientBegin;
void(entity ent, string userinfo) ClientUserinfoChanged;
void(entity ent) ClientDisconnect;
void(entity ent) ClientCommand;
void(entity ent, usercmd_t cmd) ClientThink;
void() RunFrame;
void() ServerCommand;
void(bool autosave) PreWriteGame;
void(bool autosave) PostWriteGame;
void() PreReadGame;
void() PostReadGame;
void() PreWriteLevel;
void() PostWriteLevel;
void() PreReadLevel;
void(int num_edicts) PostReadLevel;
};
// Main entry point; if you need to do any global stuff, this is where
// ya do it.
API_FUNC void(out game_export_t exports) GetGameAPI =
{
// set up constants for configstrings
CS_SOUNDS = CS_MODELS + MAX_MODELS;
CS_IMAGES = CS_SOUNDS + MAX_SOUNDS;
CS_LIGHTS = CS_IMAGES + MAX_IMAGES;
CS_ITEMS = CS_LIGHTS + MAX_LIGHTSTYLES;
CS_PLAYERSKINS = CS_ITEMS + MAX_ITEMS;
CS_GENERAL = CS_PLAYERSKINS + MAX_CLIENTS;
MAX_CONFIGSTRINGS = CS_GENERAL + MAX_GENERAL;
exports.apiversion = GAMEC_API_VERSION;
exports.clientsize = sizeof(gclient_t);
exports.InitGame = InitGame;
exports.ShutdownGame = ShutdownGame;
exports.PreSpawnEntities = PreSpawnEntities;
exports.SpawnEntities = SpawnEntities;
exports.PostSpawnEntities = PostSpawnEntities;
exports.ClientConnect = ClientConnect;
exports.ClientBegin = ClientBegin;
exports.ClientUserinfoChanged = ClientUserinfoChanged;
exports.ClientDisconnect = ClientDisconnect;
exports.ClientCommand = ClientCommand;
exports.ClientThink = ClientThink;
exports.RunFrame = RunFrame;
exports.ServerCommand = ServerCommand;
exports.PreWriteGame = PreWriteGame;
exports.PostWriteGame = PostWriteGame;
exports.PreReadGame = PreReadGame;
exports.PostReadGame = PostReadGame;
exports.PreWriteLevel = PreWriteLevel;
exports.PostWriteLevel = PostWriteLevel;
exports.PreReadLevel = PreReadLevel;
exports.PostReadLevel = PostReadLevel;
}