Debian Codegraph @

code/game/g_spawn.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 //
23
24 #include "g_local.h"
25
26 qboolean G_SpawnString( const char *key, const char *defaultString, char **out ) {
27 int i;
28
29 if ( !level.spawning ) {
30 *out = (char *)defaultString;
31 // G_Error( "G_SpawnString() called while not spawning" );
32 }
33
34 for ( i = 0 ; i < level.numSpawnVars ; i++ ) {
35 if ( !Q_stricmp( key, level.spawnVars[i][0] ) ) {
36 *out = level.spawnVars[i][1];
37 return qtrue;
38 }
39 }
40
41 *out = (char *)defaultString;
42 return qfalse;
43 }
44
45 qboolean G_SpawnFloat( const char *key, const char *defaultString, float *out ) {
46 char *s;
47 qboolean present;
48
49 present = G_SpawnString( key, defaultString, &s );
50 *out = atof( s );
51 return present;
52 }
53
54 qboolean G_SpawnInt( const char *key, const char *defaultString, int *out ) {
55 char *s;
56 qboolean present;
57
58 present = G_SpawnString( key, defaultString, &s );
59 *out = atoi( s );
60 return present;
61 }
62
63 qboolean G_SpawnVector( const char *key, const char *defaultString, float *out ) {
64 char *s;
65 qboolean present;
66
67 present = G_SpawnString( key, defaultString, &s );
68 sscanf( s, "%f %f %f", &out[0], &out[1], &out[2] );
69 return present;
70 }
71
72
73
74 //
75 // fields are needed for spawning from the entity string
76 //
77 typedef enum {
78 F_INT,
79 F_FLOAT,
80 F_STRING,
81 F_VECTOR,
82 F_ANGLEHACK
83 } fieldtype_t;
84
85 typedef struct
86 {
87 char *name;
88 size_t ofs;
89 fieldtype_t type;
90 } field_t;
91
92 field_t fields[] = {
93 {"classname", FOFS(classname), F_STRING},
94 {"origin", FOFS(s.origin), F_VECTOR},
95 {"model", FOFS(model), F_STRING},
96 {"model2", FOFS(model2), F_STRING},
97 {"spawnflags", FOFS(spawnflags), F_INT},
98 {"speed", FOFS(speed), F_FLOAT},
99 {"target", FOFS(target), F_STRING},
100 {"targetname", FOFS(targetname), F_STRING},
101 {"message", FOFS(message), F_STRING},
102 {"team", FOFS(team), F_STRING},
103 {"wait", FOFS(wait), F_FLOAT},
104 {"random", FOFS(random), F_FLOAT},
105 {"count", FOFS(count), F_INT},
106 {"health", FOFS(health), F_INT},
107 {"dmg", FOFS(damage), F_INT},
108 {"angles", FOFS(s.angles), F_VECTOR},
109 {"angle", FOFS(s.angles), F_ANGLEHACK},
110 {"targetShaderName", FOFS(targetShaderName), F_STRING},
111 {"targetShaderNewName", FOFS(targetShaderNewName), F_STRING},
112
113 {NULL}
114 };
115
116
117 typedef struct {
118 char *name;
119 void (*spawn)(gentity_t *ent);
120 } spawn_t;
121
122 void SP_info_player_start (gentity_t *ent);
123 void SP_info_player_deathmatch (gentity_t *ent);
124 void SP_info_player_intermission (gentity_t *ent);
125
126 void SP_func_plat (gentity_t *ent);
127 void SP_func_static (gentity_t *ent);
128 void SP_func_rotating (gentity_t *ent);
129 void SP_func_bobbing (gentity_t *ent);
130 void SP_func_pendulum( gentity_t *ent );
131 void SP_func_button (gentity_t *ent);
132 void SP_func_door (gentity_t *ent);
133 void SP_func_train (gentity_t *ent);
134 void SP_func_timer (gentity_t *self);
135
136 void SP_trigger_always (gentity_t *ent);
137 void SP_trigger_multiple (gentity_t *ent);
138 void SP_trigger_push (gentity_t *ent);
139 void SP_trigger_teleport (gentity_t *ent);
140 void SP_trigger_hurt (gentity_t *ent);
141
142 void SP_target_remove_powerups( gentity_t *ent );
143 void SP_target_give (gentity_t *ent);
144 void SP_target_delay (gentity_t *ent);
145 void SP_target_speaker (gentity_t *ent);
146 void SP_target_print (gentity_t *ent);
147 void SP_target_laser (gentity_t *self);
148 void SP_target_score( gentity_t *ent );
149 void SP_target_teleporter( gentity_t *ent );
150 void SP_target_relay (gentity_t *ent);
151 void SP_target_kill (gentity_t *ent);
152 void SP_target_position (gentity_t *ent);
153 void SP_target_location (gentity_t *ent);
154 void SP_target_push (gentity_t *ent);
155
156 void SP_light (gentity_t *self);
157 void SP_info_null (gentity_t *self);
158 void SP_info_notnull (gentity_t *self);
159 void SP_info_camp (gentity_t *self);
160 void SP_path_corner (gentity_t *self);
161
162 void SP_misc_teleporter_dest (gentity_t *self);
163 void SP_misc_model(gentity_t *ent);
164 void SP_misc_portal_camera(gentity_t *ent);
165 void SP_misc_portal_surface(gentity_t *ent);
166
167 void SP_shooter_rocket( gentity_t *ent );
168 void SP_shooter_plasma( gentity_t *ent );
169 void SP_shooter_grenade( gentity_t *ent );
170
171 void SP_team_CTF_redplayer( gentity_t *ent );
172 void SP_team_CTF_blueplayer( gentity_t *ent );
173
174 void SP_team_CTF_redspawn( gentity_t *ent );
175 void SP_team_CTF_bluespawn( gentity_t *ent );
176
177 #ifdef MISSIONPACK
178 void SP_team_blueobelisk( gentity_t *ent );
179 void SP_team_redobelisk( gentity_t *ent );
180 void SP_team_neutralobelisk( gentity_t *ent );
181 #endif
182 void SP_item_botroam( gentity_t *ent ) { }
183
184 spawn_t spawns[] = {
185 // info entities don't do anything at all, but provide positional
186 // information for things controlled by other processes
187 {"info_player_start", SP_info_player_start},
188 {"info_player_deathmatch", SP_info_player_deathmatch},
189 {"info_player_intermission", SP_info_player_intermission},
190 {"info_null", SP_info_null},
191 {"info_notnull", SP_info_notnull}, // use target_position instead
192 {"info_camp", SP_info_camp},
193
194 {"func_plat", SP_func_plat},
195 {"func_button", SP_func_button},
196 {"func_door", SP_func_door},
197 {"func_static", SP_func_static},
198 {"func_rotating", SP_func_rotating},
199 {"func_bobbing", SP_func_bobbing},
200 {"func_pendulum", SP_func_pendulum},
201 {"func_train", SP_func_train},
202 {"func_group", SP_info_null},
203 {"func_timer", SP_func_timer}, // rename trigger_timer?
204
205 // Triggers are brush objects that cause an effect when contacted
206 // by a living player, usually involving firing targets.
207 // While almost everything could be done with
208 // a single trigger class and different targets, triggered effects
209 // could not be client side predicted (push and teleport).
210 {"trigger_always", SP_trigger_always},
211 {"trigger_multiple", SP_trigger_multiple},
212 {"trigger_push", SP_trigger_push},
213 {"trigger_teleport", SP_trigger_teleport},
214 {"trigger_hurt", SP_trigger_hurt},
215
216 // targets perform no action by themselves, but must be triggered
217 // by another entity
218 {"target_give", SP_target_give},
219 {"target_remove_powerups", SP_target_remove_powerups},
220 {"target_delay", SP_target_delay},
221 {"target_speaker", SP_target_speaker},
222 {"target_print", SP_target_print},
223 {"target_laser", SP_target_laser},
224 {"target_score", SP_target_score},
225 {"target_teleporter", SP_target_teleporter},
226 {"target_relay", SP_target_relay},
227 {"target_kill", SP_target_kill},
228 {"target_position", SP_target_position},
229 {"target_location", SP_target_location},
230 {"target_push", SP_target_push},
231
232 {"light", SP_light},
233 {"path_corner", SP_path_corner},
234
235 {"misc_teleporter_dest", SP_misc_teleporter_dest},
236 {"misc_model", SP_misc_model},
237 {"misc_portal_surface", SP_misc_portal_surface},
238 {"misc_portal_camera", SP_misc_portal_camera},
239
240 {"shooter_rocket", SP_shooter_rocket},
241 {"shooter_grenade", SP_shooter_grenade},
242 {"shooter_plasma", SP_shooter_plasma},
243
244 {"team_CTF_redplayer", SP_team_CTF_redplayer},
245 {"team_CTF_blueplayer", SP_team_CTF_blueplayer},
246
247 {"team_CTF_redspawn", SP_team_CTF_redspawn},
248 {"team_CTF_bluespawn", SP_team_CTF_bluespawn},
249
250 #ifdef MISSIONPACK
251 {"team_redobelisk", SP_team_redobelisk},
252 {"team_blueobelisk", SP_team_blueobelisk},
253 {"team_neutralobelisk", SP_team_neutralobelisk},
254 #endif
255 {"item_botroam", SP_item_botroam},
256
257 {NULL, 0}
258 };
259
260 /*
261 ===============
262 G_CallSpawn
263
264 Finds the spawn function for the entity and calls it,
265 returning qfalse if not found
266 ===============
267 */
268 qboolean G_CallSpawn( gentity_t *ent ) {
269 spawn_t *s;
270 gitem_t *item;
271
272 if ( !ent->classname ) {
273 G_Printf ("G_CallSpawn: NULL classname\n");
274 return qfalse;
275 }
276
277 // check item spawn functions
278 for ( item=bg_itemlist+1 ; item->classname ; item++ ) {
279 if ( !strcmp(item->classname, ent->classname) ) {
280 G_SpawnItem( ent, item );
281 return qtrue;
282 }
283 }
284
285 // check normal spawn functions
286 for ( s=spawns ; s->name ; s++ ) {
287 if ( !strcmp(s->name, ent->classname) ) {
288 // found it
289 s->spawn(ent);
290 return qtrue;
291 }
292 }
293 G_Printf ("%s doesn't have a spawn function\n", ent->classname);
294 return qfalse;
295 }
296
297 /*
298 =============
299 G_NewString
300
301 Builds a copy of the string, translating \n to real linefeeds
302 so message texts can be multi-line
303 =============
304 */
305 char *G_NewString( const char *string ) {
306 char *newb, *new_p;
307 int i,l;
308
309 l = strlen(string) + 1;
310
311 newb = G_Alloc( l );
312
313 new_p = newb;
314
315 // turn \n into a real linefeed
316 for ( i=0 ; i< l ; i++ ) {
317 if (string[i] == '\\' && i < l-1) {
318 i++;
319 if (string[i] == 'n') {
320 *new_p++ = '\n';
321 } else {
322 *new_p++ = '\\';
323 }
324 } else {
325 *new_p++ = string[i];
326 }
327 }
328
329 return newb;
330 }
331
332
333
334
335 /*
336 ===============
337 G_ParseField
338
339 Takes a key/value pair and sets the binary values
340 in a gentity
341 ===============
342 */
343 void G_ParseField( const char *key, const char *value, gentity_t *ent ) {
344 field_t *f;
345 byte *b;
346 float v;
347 vec3_t vec;
348
349 for ( f=fields ; f->name ; f++ ) {
350 if ( !Q_stricmp(f->name, key) ) {
351 // found it
352 b = (byte *)ent;
353
354 switch( f->type ) {
355 case F_STRING:
356 *(char **)(b+f->ofs) = G_NewString (value);
357 break;
358 case F_VECTOR:
359 sscanf (value, "%f %f %f", &vec[0], &vec[1], &vec[2]);
360 ((float *)(b+f->ofs))[0] = vec[0];
361 ((float *)(b+f->ofs))[1] = vec[1];
362 ((float *)(b+f->ofs))[2] = vec[2];
363 break;
364 case F_INT:
365 *(int *)(b+f->ofs) = atoi(value);
366 break;
367 case F_FLOAT:
368 *(float *)(b+f->ofs) = atof(value);
369 break;
370 case F_ANGLEHACK:
371 v = atof(value);
372 ((float *)(b+f->ofs))[0] = 0;
373 ((float *)(b+f->ofs))[1] = v;
374 ((float *)(b+f->ofs))[2] = 0;
375 break;
376 }
377 return;
378 }
379 }
380 }
381
382 #define ADJUST_AREAPORTAL() \
383 if(ent->s.eType == ET_MOVER) \
384 { \
385 trap_LinkEntity(ent); \
386 trap_AdjustAreaPortalState(ent, qtrue); \
387 }
388
389 /*
390 ===================
391 G_SpawnGEntityFromSpawnVars
392
393 Spawn an entity and fill in all of the level fields from
394 level.spawnVars[], then call the class specific spawn function
395 ===================
396 */
397 void G_SpawnGEntityFromSpawnVars( void ) {
398 int i;
399 gentity_t *ent;
400 char *s, *value, *gametypeName;
401 static char *gametypeNames[] = {"ffa", "tournament", "single", "team", "ctf", "oneflag", "obelisk", "harvester"};
402
403 // get the next free entity
404 ent = G_Spawn();
405
406 for ( i = 0 ; i < level.numSpawnVars ; i++ ) {
407 G_ParseField( level.spawnVars[i][0], level.spawnVars[i][1], ent );
408 }
409
410 // check for "notsingle" flag
411 if ( g_gametype.integer == GT_SINGLE_PLAYER ) {
412 G_SpawnInt( "notsingle", "0", &i );
413 if ( i ) {
414 ADJUST_AREAPORTAL();
415 G_FreeEntity( ent );
416 return;
417 }
418 }
419 // check for "notteam" flag (GT_FFA, GT_TOURNAMENT, GT_SINGLE_PLAYER)
420 if ( g_gametype.integer >= GT_TEAM ) {
421 G_SpawnInt( "notteam", "0", &i );
422 if ( i ) {
423 ADJUST_AREAPORTAL();
424 G_FreeEntity( ent );
425 return;
426 }
427 } else {
428 G_SpawnInt( "notfree", "0", &i );
429 if ( i ) {
430 ADJUST_AREAPORTAL();
431 G_FreeEntity( ent );
432 return;
433 }
434 }
435
436 #ifdef MISSIONPACK
437 G_SpawnInt( "notta", "0", &i );
438 if ( i ) {
439 ADJUST_AREAPORTAL();
440 G_FreeEntity( ent );
441 return;
442 }
443 #else
444 G_SpawnInt( "notq3a", "0", &i );
445 if ( i ) {
446 ADJUST_AREAPORTAL();
447 G_FreeEntity( ent );
448 return;
449 }
450 #endif
451
452 if( G_SpawnString( "gametype", NULL, &value ) ) {
453 if( g_gametype.integer >= GT_FFA && g_gametype.integer < GT_MAX_GAME_TYPE ) {
454 gametypeName = gametypeNames[g_gametype.integer];
455
456 s = strstr( value, gametypeName );
457 if( !s ) {
458 ADJUST_AREAPORTAL();
459 G_FreeEntity( ent );
460 return;
461 }
462 }
463 }
464
465 // move editor origin to pos
466 VectorCopy( ent->s.origin, ent->s.pos.trBase );
467 VectorCopy( ent->s.origin, ent->r.currentOrigin );
468
469 // if we didn't get a classname, don't bother spawning anything
470 if ( !G_CallSpawn( ent ) ) {
471 G_FreeEntity( ent );
472 }
473 }
474
475
476
477 /*
478 ====================
479 G_AddSpawnVarToken
480 ====================
481 */
482 char *G_AddSpawnVarToken( const char *string ) {
483 int l;
484 char *dest;
485
486 l = strlen( string );
487 if ( level.numSpawnVarChars + l + 1 > MAX_SPAWN_VARS_CHARS ) {
488 G_Error( "G_AddSpawnVarToken: MAX_SPAWN_VARS_CHARS" );
489 }
490
491 dest = level.spawnVarChars + level.numSpawnVarChars;
492 memcpy( dest, string, l+1 );
493
494 level.numSpawnVarChars += l + 1;
495
496 return dest;
497 }
498
499 /*
500 ====================
501 G_ParseSpawnVars
502
503 Parses a brace bounded set of key / value pairs out of the
504 level's entity strings into level.spawnVars[]
505
506 This does not actually spawn an entity.
507 ====================
508 */
509 qboolean G_ParseSpawnVars( void ) {
510 char keyname[MAX_TOKEN_CHARS];
511 char com_token[MAX_TOKEN_CHARS];
512
513 level.numSpawnVars = 0;
514 level.numSpawnVarChars = 0;
515
516 // parse the opening brace
517 if ( !trap_GetEntityToken( com_token, sizeof( com_token ) ) ) {
518 // end of spawn string
519 return qfalse;
520 }
521 if ( com_token[0] != '{' ) {
522 G_Error( "G_ParseSpawnVars: found %s when expecting {",com_token );
523 }
524
525 // go through all the key / value pairs
526 while ( 1 ) {
527 // parse key
528 if ( !trap_GetEntityToken( keyname, sizeof( keyname ) ) ) {
529 G_Error( "G_ParseSpawnVars: EOF without closing brace" );
530 }
531
532 if ( keyname[0] == '}' ) {
533 break;
534 }
535
536 // parse value
537 if ( !trap_GetEntityToken( com_token, sizeof( com_token ) ) ) {
538 G_Error( "G_ParseSpawnVars: EOF without closing brace" );
539 }
540
541 if ( com_token[0] == '}' ) {
542 G_Error( "G_ParseSpawnVars: closing brace without data" );
543 }
544 if ( level.numSpawnVars == MAX_SPAWN_VARS ) {
545 G_Error( "G_ParseSpawnVars: MAX_SPAWN_VARS" );
546 }
547 level.spawnVars[ level.numSpawnVars ][0] = G_AddSpawnVarToken( keyname );
548 level.spawnVars[ level.numSpawnVars ][1] = G_AddSpawnVarToken( com_token );
549 level.numSpawnVars++;
550 }
551
552 return qtrue;
553 }
554
555
556
557 /*QUAKED worldspawn (0 0 0) ?
558
559 Every map should have exactly one worldspawn.
560 "music" music wav file
561 "gravity" 800 is default gravity
562 "message" Text to print during connection process
563 */
564 void SP_worldspawn( void ) {
565 char *s;
566
567 G_SpawnString( "classname", "", &s );
568 if ( Q_stricmp( s, "worldspawn" ) ) {
569 G_Error( "SP_worldspawn: The first entity isn't 'worldspawn'" );
570 }
571
572 // make some data visible to connecting client
573 trap_SetConfigstring( CS_GAME_VERSION, GAME_VERSION );
574
575 trap_SetConfigstring( CS_LEVEL_START_TIME, va("%i", level.startTime ) );
576
577 G_SpawnString( "music", "", &s );
578 trap_SetConfigstring( CS_MUSIC, s );
579
580 G_SpawnString( "message", "", &s );
581 trap_SetConfigstring( CS_MESSAGE, s ); // map specific message
582
583 trap_SetConfigstring( CS_MOTD, g_motd.string ); // message of the day
584
585 G_SpawnString( "gravity", "800", &s );
586 trap_Cvar_Set( "g_gravity", s );
587
588 G_SpawnString( "enableDust", "0", &s );
589 trap_Cvar_Set( "g_enableDust", s );
590
591 G_SpawnString( "enableBreath", "0", &s );
592 trap_Cvar_Set( "g_enableBreath", s );
593
594 g_entities[ENTITYNUM_WORLD].s.number = ENTITYNUM_WORLD;
595 g_entities[ENTITYNUM_WORLD].r.ownerNum = ENTITYNUM_NONE;
596 g_entities[ENTITYNUM_WORLD].classname = "worldspawn";
597
598 g_entities[ENTITYNUM_NONE].s.number = ENTITYNUM_NONE;
599 g_entities[ENTITYNUM_NONE].r.ownerNum = ENTITYNUM_NONE;
600 g_entities[ENTITYNUM_NONE].classname = "nothing";
601
602 // see if we want a warmup time
603 trap_SetConfigstring( CS_WARMUP, "" );
604 if ( g_restarted.integer ) {
605 trap_Cvar_Set( "g_restarted", "0" );
606 level.warmupTime = 0;
607 } else if ( g_doWarmup.integer ) { // Turn it on
608 level.warmupTime = -1;
609 trap_SetConfigstring( CS_WARMUP, va("%i", level.warmupTime) );
610 G_LogPrintf( "Warmup:\n" );
611 }
612
613 }
614
615
616 /*
617 ==============
618 G_SpawnEntitiesFromString
619
620 Parses textual entity definitions out of an entstring and spawns gentities.
621 ==============
622 */
623 void G_SpawnEntitiesFromString( void ) {
624 // allow calls to G_Spawn*()
625 level.spawning = qtrue;
626 level.numSpawnVars = 0;
627
628 // the worldspawn is not an actual entity, but it still
629 // has a "spawn" function to perform any global setup
630 // needed by a level (setting configstrings or cvars, etc)
631 if ( !G_ParseSpawnVars() ) {
632 G_Error( "SpawnEntities: no entities" );
633 }
634 SP_worldspawn();
635
636 // parse ents
637 while( G_ParseSpawnVars() ) {
638 G_SpawnGEntityFromSpawnVars();
639 }
640
641 level.spawning = qfalse; // any future calls to G_Spawn*() will be errors
642 }
643