Debian Codegraph @

code/game/g_spawn.c

raw
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_LSTRING, // string on disk, pointer in memory, TAG_LEVEL
81 F_GSTRING, // string on disk, pointer in memory, TAG_GAME
82 F_VECTOR,
83 F_ANGLEHACK,
84 F_ENTITY, // index on disk, pointer in memory
85 F_ITEM, // index on disk, pointer in memory
86 F_CLIENT, // index on disk, pointer in memory
87 F_IGNORE
88 } fieldtype_t;
89
90 typedef struct
91 {
92 char *name;
93 int ofs;
94 fieldtype_t type;
95 // int flags;
96 } field_t;
97
98 field_t fields[] = {
99 {"classname", FOFS(classname), F_LSTRING},
100 {"origin", FOFS(s.origin), F_VECTOR},
101 {"model", FOFS(model), F_LSTRING},
102 {"model2", FOFS(model2), F_LSTRING},
103 {"spawnflags", FOFS(spawnflags), F_INT},
104 {"speed", FOFS(speed), F_FLOAT},
105 {"target", FOFS(target), F_LSTRING},
106 {"targetname", FOFS(targetname), F_LSTRING},
107 {"message", FOFS(message), F_LSTRING},
108 {"team", FOFS(team), F_LSTRING},
109 {"wait", FOFS(wait), F_FLOAT},
110 {"random", FOFS(random), F_FLOAT},
111 {"count", FOFS(count), F_INT},
112 {"health", FOFS(health), F_INT},
113 {"light", 0, F_IGNORE},
114 {"dmg", FOFS(damage), F_INT},
115 {"angles", FOFS(s.angles), F_VECTOR},
116 {"angle", FOFS(s.angles), F_ANGLEHACK},
117 {"targetShaderName", FOFS(targetShaderName), F_LSTRING},
118 {"targetShaderNewName", FOFS(targetShaderNewName), F_LSTRING},
119
120 {NULL}
121 };
122
123
124 typedef struct {
125 char *name;
126 void (*spawn)(gentity_t *ent);
127 } spawn_t;
128
129 void SP_info_player_start (gentity_t *ent);
130 void SP_info_player_deathmatch (gentity_t *ent);
131 void SP_info_player_intermission (gentity_t *ent);
132 //For Double Domination:
133 void SP_info_player_dd (gentity_t *ent);
134 void SP_info_player_dd_red (gentity_t *ent);
135 void SP_info_player_dd_blue (gentity_t *ent);
136 //standard domination:
137 void SP_domination_point ( gentity_t *ent);
138
139 void SP_info_firstplace(gentity_t *ent);
140 void SP_info_secondplace(gentity_t *ent);
141 void SP_info_thirdplace(gentity_t *ent);
142 void SP_info_podium(gentity_t *ent);
143
144 void SP_func_plat (gentity_t *ent);
145 void SP_func_static (gentity_t *ent);
146 void SP_func_rotating (gentity_t *ent);
147 void SP_func_bobbing (gentity_t *ent);
148 void SP_func_pendulum( gentity_t *ent );
149 void SP_func_button (gentity_t *ent);
150 void SP_func_door (gentity_t *ent);
151 void SP_func_train (gentity_t *ent);
152 void SP_func_timer (gentity_t *self);
153
154 void SP_trigger_always (gentity_t *ent);
155 void SP_trigger_multiple (gentity_t *ent);
156 void SP_trigger_push (gentity_t *ent);
157 void SP_trigger_teleport (gentity_t *ent);
158 void SP_trigger_hurt (gentity_t *ent);
159
160 void SP_target_remove_powerups( gentity_t *ent );
161 void SP_target_give (gentity_t *ent);
162 void SP_target_delay (gentity_t *ent);
163 void SP_target_speaker (gentity_t *ent);
164 void SP_target_print (gentity_t *ent);
165 void SP_target_laser (gentity_t *self);
166 void SP_target_character (gentity_t *ent);
167 void SP_target_score( gentity_t *ent );
168 void SP_target_teleporter( gentity_t *ent );
169 void SP_target_relay (gentity_t *ent);
170 void SP_target_kill (gentity_t *ent);
171 void SP_target_position (gentity_t *ent);
172 void SP_target_location (gentity_t *ent);
173 void SP_target_push (gentity_t *ent);
174
175 void SP_light (gentity_t *self);
176 void SP_info_null (gentity_t *self);
177 void SP_info_notnull (gentity_t *self);
178 void SP_info_camp (gentity_t *self);
179 void SP_path_corner (gentity_t *self);
180
181 void SP_misc_teleporter_dest (gentity_t *self);
182 void SP_misc_model(gentity_t *ent);
183 void SP_misc_portal_camera(gentity_t *ent);
184 void SP_misc_portal_surface(gentity_t *ent);
185
186 void SP_shooter_rocket( gentity_t *ent );
187 void SP_shooter_plasma( gentity_t *ent );
188 void SP_shooter_grenade( gentity_t *ent );
189
190 void SP_team_CTF_redplayer( gentity_t *ent );
191 void SP_team_CTF_blueplayer( gentity_t *ent );
192
193 void SP_team_CTF_redspawn( gentity_t *ent );
194 void SP_team_CTF_bluespawn( gentity_t *ent );
195
196 void SP_team_blueobelisk( gentity_t *ent );
197 void SP_team_redobelisk( gentity_t *ent );
198 void SP_team_neutralobelisk( gentity_t *ent );
199
200 void SP_item_botroam( gentity_t *ent ) { }
201
202 spawn_t spawns[] = {
203 // info entities don't do anything at all, but provide positional
204 // information for things controlled by other processes
205 {"info_player_start", SP_info_player_start},
206 {"info_player_deathmatch", SP_info_player_deathmatch},
207 {"info_player_intermission", SP_info_player_intermission},
208 //Double Domination player spawn:
209 {"info_player_dd", SP_info_player_dd},
210 {"info_player_dd_red", SP_info_player_dd_red},
211 {"info_player_dd_blue", SP_info_player_dd_blue},
212 //Standard Domination point spawn:
213 {"domination_point", SP_domination_point},
214
215
216 {"info_null", SP_info_null},
217 {"info_notnull", SP_info_notnull}, // use target_position instead
218 {"info_camp", SP_info_camp},
219
220 {"func_plat", SP_func_plat},
221 {"func_button", SP_func_button},
222 {"func_door", SP_func_door},
223 {"func_static", SP_func_static},
224 {"func_rotating", SP_func_rotating},
225 {"func_bobbing", SP_func_bobbing},
226 {"func_pendulum", SP_func_pendulum},
227 {"func_train", SP_func_train},
228 {"func_group", SP_info_null},
229 {"func_timer", SP_func_timer}, // rename trigger_timer?
230
231 // Triggers are brush objects that cause an effect when contacted
232 // by a living player, usually involving firing targets.
233 // While almost everything could be done with
234 // a single trigger class and different targets, triggered effects
235 // could not be client side predicted (push and teleport).
236 {"trigger_always", SP_trigger_always},
237 {"trigger_multiple", SP_trigger_multiple},
238 {"trigger_push", SP_trigger_push},
239 {"trigger_teleport", SP_trigger_teleport},
240 {"trigger_hurt", SP_trigger_hurt},
241
242 // targets perform no action by themselves, but must be triggered
243 // by another entity
244 {"target_give", SP_target_give},
245 {"target_remove_powerups", SP_target_remove_powerups},
246 {"target_delay", SP_target_delay},
247 {"target_speaker", SP_target_speaker},
248 {"target_print", SP_target_print},
249 {"target_laser", SP_target_laser},
250 {"target_score", SP_target_score},
251 {"target_teleporter", SP_target_teleporter},
252 {"target_relay", SP_target_relay},
253 {"target_kill", SP_target_kill},
254 {"target_position", SP_target_position},
255 {"target_location", SP_target_location},
256 {"target_push", SP_target_push},
257
258 {"light", SP_light},
259 {"path_corner", SP_path_corner},
260
261 {"misc_teleporter_dest", SP_misc_teleporter_dest},
262 {"misc_model", SP_misc_model},
263 {"misc_portal_surface", SP_misc_portal_surface},
264 {"misc_portal_camera", SP_misc_portal_camera},
265
266 {"shooter_rocket", SP_shooter_rocket},
267 {"shooter_grenade", SP_shooter_grenade},
268 {"shooter_plasma", SP_shooter_plasma},
269
270 {"team_CTF_redplayer", SP_team_CTF_redplayer},
271 {"team_CTF_blueplayer", SP_team_CTF_blueplayer},
272
273 {"team_CTF_redspawn", SP_team_CTF_redspawn},
274 {"team_CTF_bluespawn", SP_team_CTF_bluespawn},
275
276 {"team_redobelisk", SP_team_redobelisk},
277 {"team_blueobelisk", SP_team_blueobelisk},
278 {"team_neutralobelisk", SP_team_neutralobelisk},
279
280 {"item_botroam", SP_item_botroam},
281
282 {NULL, 0}
283 };
284
285 /*
286 ===============
287 G_CallSpawn
288
289 Finds the spawn function for the entity and calls it,
290 returning qfalse if not found
291 ===============
292 */
293 qboolean G_CallSpawn( gentity_t *ent ) {
294 spawn_t *s;
295 gitem_t *item;
296 char cvarname[128];
297 char itemname[128];
298
299 //Construct a replace cvar:
300 Com_sprintf(cvarname, sizeof(cvarname), "replace_%s", ent->classname);
301
302 //Look an alternative item up:
303 trap_Cvar_VariableStringBuffer(cvarname,itemname,sizeof(itemname));
304 if(itemname[0]==0) //If nothing found use original
305 Com_sprintf(itemname, sizeof(itemname), "%s", ent->classname);
306 else
307 G_Printf ("%s replaced by %s\n", ent->classname, itemname);
308
309
310 if ( itemname[0]==0) {
311 G_Printf ("G_CallSpawn: NULL classname\n");
312 return qfalse;
313 }
314
315 // check item spawn functions
316 for ( item=bg_itemlist+1 ; item->classname ; item++ ) {
317 if ( !strcmp(item->classname, itemname) ) {
318 G_SpawnItem( ent, item );
319 return qtrue;
320 }
321 }
322
323 // check normal spawn functions
324 for ( s=spawns ; s->name ; s++ ) {
325 if ( !strcmp(s->name, itemname) ) {
326 // found it
327 s->spawn(ent);
328 return qtrue;
329 }
330 }
331 G_Printf ("%s doesn't have a spawn function\n", itemname);
332 return qfalse;
333 }
334
335 /*
336 =============
337 G_NewString
338
339 Builds a copy of the string, translating \n to real linefeeds
340 so message texts can be multi-line
341 =============
342 */
343 char *G_NewString( const char *string ) {
344 char *newb, *new_p;
345 int i,l;
346
347 l = strlen(string) + 1;
348 //KK-OAX Changed to Tremulous's BG_Alloc
349 newb = BG_Alloc( l );
350
351 new_p = newb;
352
353 // turn \n into a real linefeed
354 for ( i=0 ; i< l ; i++ ) {
355 if (string[i] == '\\' && i < l-1) {
356 i++;
357 if (string[i] == 'n') {
358 *new_p++ = '\n';
359 } else {
360 *new_p++ = '\\';
361 }
362 } else {
363 *new_p++ = string[i];
364 }
365 }
366
367 return newb;
368 }
369
370
371
372
373 /*
374 ===============
375 G_ParseField
376
377 Takes a key/value pair and sets the binary values
378 in a gentity
379 ===============
380 */
381 void G_ParseField( const char *key, const char *value, gentity_t *ent ) {
382 field_t *f;
383 byte *b;
384 float v;
385 vec3_t vec;
386
387 for ( f=fields ; f->name ; f++ ) {
388 if ( !Q_stricmp(f->name, key) ) {
389 // found it
390 b = (byte *)ent;
391
392 switch( f->type ) {
393 case F_LSTRING:
394 *(char **)(b+f->ofs) = G_NewString (value);
395 break;
396 case F_VECTOR:
397 sscanf (value, "%f %f %f", &vec[0], &vec[1], &vec[2]);
398 ((float *)(b+f->ofs))[0] = vec[0];
399 ((float *)(b+f->ofs))[1] = vec[1];
400 ((float *)(b+f->ofs))[2] = vec[2];
401 break;
402 case F_INT:
403 *(int *)(b+f->ofs) = atoi(value);
404 break;
405 case F_FLOAT:
406 *(float *)(b+f->ofs) = atof(value);
407 break;
408 case F_ANGLEHACK:
409 v = atof(value);
410 ((float *)(b+f->ofs))[0] = 0;
411 ((float *)(b+f->ofs))[1] = v;
412 ((float *)(b+f->ofs))[2] = 0;
413 break;
414 default:
415 case F_IGNORE:
416 break;
417 }
418 return;
419 }
420 }
421 }
422
423
424
425
426 /*
427 ===================
428 G_SpawnGEntityFromSpawnVars
429
430 Spawn an entity and fill in all of the level fields from
431 level.spawnVars[], then call the class specfic spawn function
432 ===================
433 */
434 void G_SpawnGEntityFromSpawnVars( void ) {
435 int i;
436 gentity_t *ent;
437 char *s, *value, *gametypeName;
438 static char *gametypeNames[] = {"ffa", "tournament", "single", "team", "ctf", "oneflag", "obelisk", "harvester", "elimination", "ctf", "lms", "dd", "dom"};
439
440 // get the next free entity
441 ent = G_Spawn();
442
443 for ( i = 0 ; i < level.numSpawnVars ; i++ ) {
444 G_ParseField( level.spawnVars[i][0], level.spawnVars[i][1], ent );
445 }
446
447 // check for "notsingle" flag
448 if ( g_gametype.integer == GT_SINGLE_PLAYER ) {
449 G_SpawnInt( "notsingle", "0", &i );
450 if ( i ) {
451 G_FreeEntity( ent );
452 return;
453 }
454 }
455 // check for "notteam" flag (GT_FFA, GT_TOURNAMENT, GT_SINGLE_PLAYER)
456 if ( g_gametype.integer >= GT_TEAM && !g_ffa_gt ) {
457 G_SpawnInt( "notteam", "0", &i );
458 if ( i ) {
459 G_FreeEntity( ent );
460 return;
461 }
462 } else {
463 G_SpawnInt( "notfree", "0", &i );
464 if ( i ) {
465 G_FreeEntity( ent );
466 return;
467 }
468 }
469
470 #ifdef MISSIONPACK
471 G_SpawnInt( "notta", "0", &i );
472 if ( i ) {
473 G_FreeEntity( ent );
474 return;
475 }
476 #else
477 G_SpawnInt( "notq3a", "0", &i );
478 if ( i ) {
479 G_FreeEntity( ent );
480 return;
481 }
482 #endif
483
484 if( G_SpawnString( "!gametype", NULL, &value ) ) {
485 if( g_gametype.integer >= GT_FFA && g_gametype.integer < GT_MAX_GAME_TYPE ) {
486 gametypeName = gametypeNames[g_gametype.integer];
487
488 s = strstr( value, gametypeName );
489 if( s ) {
490 G_FreeEntity( ent );
491 return;
492 }
493 }
494 }
495
496 if( G_SpawnString( "gametype", NULL, &value ) ) {
497 if( g_gametype.integer >= GT_FFA && g_gametype.integer < GT_MAX_GAME_TYPE ) {
498 gametypeName = gametypeNames[g_gametype.integer];
499
500 s = strstr( value, gametypeName );
501 if( !s ) {
502 G_FreeEntity( ent );
503 return;
504 }
505 }
506 }
507
508 // move editor origin to pos
509 VectorCopy( ent->s.origin, ent->s.pos.trBase );
510 VectorCopy( ent->s.origin, ent->r.currentOrigin );
511
512 // if we didn't get a classname, don't bother spawning anything
513 if ( !G_CallSpawn( ent ) ) {
514 G_FreeEntity( ent );
515 }
516 }
517
518
519
520 /*
521 ====================
522 G_AddSpawnVarToken
523 ====================
524 */
525 char *G_AddSpawnVarToken( const char *string ) {
526 int l;
527 char *dest;
528
529 l = strlen( string );
530 if ( level.numSpawnVarChars + l + 1 > MAX_SPAWN_VARS_CHARS ) {
531 G_Error( "G_AddSpawnVarToken: MAX_SPAWN_VARS" );
532 }
533
534 dest = level.spawnVarChars + level.numSpawnVarChars;
535 memcpy( dest, string, l+1 );
536
537 level.numSpawnVarChars += l + 1;
538
539 return dest;
540 }
541
542 /*
543 ====================
544 G_ParseSpawnVars
545
546 Parses a brace bounded set of key / value pairs out of the
547 level's entity strings into level.spawnVars[]
548
549 This does not actually spawn an entity.
550 ====================
551 */
552 qboolean G_ParseSpawnVars( void ) {
553 char keyname[MAX_TOKEN_CHARS];
554 char com_token[MAX_TOKEN_CHARS];
555
556 level.numSpawnVars = 0;
557 level.numSpawnVarChars = 0;
558
559 // parse the opening brace
560 if ( !trap_GetEntityToken( com_token, sizeof( com_token ) ) ) {
561 // end of spawn string
562 return qfalse;
563 }
564 if ( com_token[0] != '{' ) {
565 G_Error( "G_ParseSpawnVars: found %s when expecting {",com_token );
566 }
567
568 // go through all the key / value pairs
569 while ( 1 ) {
570 // parse key
571 if ( !trap_GetEntityToken( keyname, sizeof( keyname ) ) ) {
572 G_Error( "G_ParseSpawnVars: EOF without closing brace" );
573 }
574
575 if ( keyname[0] == '}' ) {
576 break;
577 }
578
579 // parse value
580 if ( !trap_GetEntityToken( com_token, sizeof( com_token ) ) ) {
581 G_Error( "G_ParseSpawnVars: EOF without closing brace" );
582 }
583
584 if ( com_token[0] == '}' ) {
585 G_Error( "G_ParseSpawnVars: closing brace without data" );
586 }
587 if ( level.numSpawnVars == MAX_SPAWN_VARS ) {
588 G_Error( "G_ParseSpawnVars: MAX_SPAWN_VARS" );
589 }
590 level.spawnVars[ level.numSpawnVars ][0] = G_AddSpawnVarToken( keyname );
591 level.spawnVars[ level.numSpawnVars ][1] = G_AddSpawnVarToken( com_token );
592 level.numSpawnVars++;
593 }
594
595 return qtrue;
596 }
597
598
599
600 /*QUAKED worldspawn (0 0 0) ?
601
602 Every map should have exactly one worldspawn.
603 "music" music wav file
604 "gravity" 800 is default gravity
605 "message" Text to print during connection process
606 */
607 void SP_worldspawn( void ) {
608 char *s;
609
610 G_SpawnString( "classname", "", &s );
611 if ( Q_stricmp( s, "worldspawn" ) ) {
612 G_Error( "SP_worldspawn: The first entity isn't 'worldspawn'" );
613 }
614
615 // make some data visible to connecting client
616 trap_SetConfigstring( CS_GAME_VERSION, GAME_VERSION );
617
618 trap_SetConfigstring( CS_LEVEL_START_TIME, va("%i", level.startTime ) );
619
620 if ( *g_music.string && Q_stricmp( g_music.string, "none" ) ) {
621 trap_SetConfigstring( CS_MUSIC, g_music.string );
622 } else {
623 G_SpawnString( "music", "", &s );
624 trap_SetConfigstring( CS_MUSIC, s );
625 }
626
627 G_SpawnString( "message", "", &s );
628 trap_SetConfigstring( CS_MESSAGE, s ); // map specific message
629
630 trap_SetConfigstring( CS_MOTD, g_motd.string ); // message of the day
631
632 G_SpawnString( "gravity", "800", &s );
633 trap_Cvar_Set( "g_gravity", s );
634
635 G_SpawnString( "enableDust", "0", &s );
636 trap_Cvar_Set( "g_enableDust", s );
637
638 G_SpawnString( "enableBreath", "0", &s );
639 trap_Cvar_Set( "g_enableBreath", s );
640
641 g_entities[ENTITYNUM_WORLD].s.number = ENTITYNUM_WORLD;
642 g_entities[ENTITYNUM_WORLD].r.ownerNum = ENTITYNUM_NONE;
643 g_entities[ENTITYNUM_WORLD].classname = "worldspawn";
644
645 g_entities[ENTITYNUM_NONE].s.number = ENTITYNUM_NONE;
646 g_entities[ENTITYNUM_NONE].r.ownerNum = ENTITYNUM_NONE;
647 g_entities[ENTITYNUM_NONE].classname = "nothing";
648
649 // see if we want a warmup time
650 trap_SetConfigstring( CS_WARMUP, "" );
651 if ( g_restarted.integer ) {
652 trap_Cvar_Set( "g_restarted", "0" );
653 level.warmupTime = 0;
654 } else if ( g_doWarmup.integer ) { // Turn it on
655 level.warmupTime = -1;
656 trap_SetConfigstring( CS_WARMUP, va("%i", level.warmupTime) );
657 G_LogPrintf( "Warmup:\n" );
658 }
659
660 }
661
662
663 /*
664 ==============
665 G_SpawnEntitiesFromString
666
667 Parses textual entity definitions out of an entstring and spawns gentities.
668 ==============
669 */
670 void G_SpawnEntitiesFromString( void ) {
671 // allow calls to G_Spawn*()
672 level.spawning = qtrue;
673 level.numSpawnVars = 0;
674
675 // the worldspawn is not an actual entity, but it still
676 // has a "spawn" function to perform any global setup
677 // needed by a level (setting configstrings or cvars, etc)
678 if ( !G_ParseSpawnVars() ) {
679 G_Error( "SpawnEntities: no entities" );
680 }
681 SP_worldspawn();
682
683 // parse ents
684 while( G_ParseSpawnVars() ) {
685 G_SpawnGEntityFromSpawnVars();
686 }
687
688 level.spawning = qfalse; // any future calls to G_Spawn*() will be errors
689 }
690