| 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 |
#include "g_local.h" |
| 26 |
|
| 27 |
typedef struct { |
| 28 |
char oldShader[MAX_QPATH]; |
| 29 |
char newShader[MAX_QPATH]; |
| 30 |
float timeOffset; |
| 31 |
} shaderRemap_t; |
| 32 |
|
| 33 |
#define MAX_SHADER_REMAPS 128 |
| 34 |
|
| 35 |
int remapCount = 0; |
| 36 |
shaderRemap_t remappedShaders[MAX_SHADER_REMAPS]; |
| 37 |
|
| 38 |
void AddRemap(const char *oldShader, const char *newShader, float timeOffset) { |
| 39 |
int i; |
| 40 |
|
| 41 |
for (i = 0; i < remapCount; i++) { |
| 42 |
if (Q_stricmp(oldShader, remappedShaders[i].oldShader) == 0) { |
| 43 |
|
| 44 |
strcpy(remappedShaders[i].newShader,newShader); |
| 45 |
remappedShaders[i].timeOffset = timeOffset; |
| 46 |
return; |
| 47 |
} |
| 48 |
} |
| 49 |
if (remapCount < MAX_SHADER_REMAPS) { |
| 50 |
strcpy(remappedShaders[remapCount].newShader,newShader); |
| 51 |
strcpy(remappedShaders[remapCount].oldShader,oldShader); |
| 52 |
remappedShaders[remapCount].timeOffset = timeOffset; |
| 53 |
remapCount++; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
const char *BuildShaderStateConfig(void) { |
| 58 |
static char buff[MAX_STRING_CHARS*4]; |
| 59 |
char out[(MAX_QPATH * 2) + 5]; |
| 60 |
int i; |
| 61 |
|
| 62 |
memset(buff, 0, sizeof(buff)); |
| 63 |
for (i = 0; i < remapCount; i++) { |
| 64 |
Com_sprintf(out, (MAX_QPATH * 2) + 5, "%s=%s:%5.2f@", remappedShaders[i].oldShader, remappedShaders[i].newShader, remappedShaders[i].timeOffset); |
| 65 |
Q_strcat( buff, sizeof( buff ), out); |
| 66 |
} |
| 67 |
return buff; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
int G_FindConfigstringIndex( char *name, int start, int max, qboolean create ) { |
| 85 |
int i; |
| 86 |
char s[MAX_STRING_CHARS]; |
| 87 |
|
| 88 |
if ( !name || !name[0] ) { |
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
for ( i=1 ; i<max ; i++ ) { |
| 93 |
trap_GetConfigstring( start + i, s, sizeof( s ) ); |
| 94 |
if ( !s[0] ) { |
| 95 |
break; |
| 96 |
} |
| 97 |
if ( !strcmp( s, name ) ) { |
| 98 |
return i; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
if ( !create ) { |
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
if ( i == max ) { |
| 107 |
G_Error( "G_FindConfigstringIndex: overflow" ); |
| 108 |
} |
| 109 |
|
| 110 |
trap_SetConfigstring( start + i, name ); |
| 111 |
|
| 112 |
return i; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
int G_ModelIndex( char *name ) { |
| 117 |
return G_FindConfigstringIndex (name, CS_MODELS, MAX_MODELS, qtrue); |
| 118 |
} |
| 119 |
|
| 120 |
int G_SoundIndex( char *name ) { |
| 121 |
return G_FindConfigstringIndex (name, CS_SOUNDS, MAX_SOUNDS, qtrue); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
void G_TeamCommand( team_t team, char *cmd ) { |
| 135 |
int i; |
| 136 |
|
| 137 |
for ( i = 0 ; i < level.maxclients ; i++ ) { |
| 138 |
if ( level.clients[i].pers.connected == CON_CONNECTED ) { |
| 139 |
if ( level.clients[i].sess.sessionTeam == team ) { |
| 140 |
trap_SendServerCommand( i, va("%s", cmd )); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
gentity_t *G_Find (gentity_t *from, int fieldofs, const char *match) |
| 160 |
{ |
| 161 |
char *s; |
| 162 |
|
| 163 |
if (!from) |
| 164 |
from = g_entities; |
| 165 |
else |
| 166 |
from++; |
| 167 |
|
| 168 |
for ( ; from < &g_entities[level.num_entities] ; from++) |
| 169 |
{ |
| 170 |
if (!from->inuse) |
| 171 |
continue; |
| 172 |
s = *(char **) ((byte *)from + fieldofs); |
| 173 |
if (!s) |
| 174 |
continue; |
| 175 |
if (!Q_stricmp (s, match)) |
| 176 |
return from; |
| 177 |
} |
| 178 |
|
| 179 |
return NULL; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
#define MAXCHOICES 32 |
| 191 |
|
| 192 |
gentity_t *G_PickTarget (char *targetname) |
| 193 |
{ |
| 194 |
gentity_t *ent = NULL; |
| 195 |
int num_choices = 0; |
| 196 |
gentity_t *choice[MAXCHOICES]; |
| 197 |
|
| 198 |
if (!targetname) |
| 199 |
{ |
| 200 |
G_Printf("G_PickTarget called with NULL targetname\n"); |
| 201 |
return NULL; |
| 202 |
} |
| 203 |
|
| 204 |
while(1) |
| 205 |
{ |
| 206 |
ent = G_Find (ent, FOFS(targetname), targetname); |
| 207 |
if (!ent) |
| 208 |
break; |
| 209 |
choice[num_choices++] = ent; |
| 210 |
if (num_choices == MAXCHOICES) |
| 211 |
break; |
| 212 |
} |
| 213 |
|
| 214 |
if (!num_choices) |
| 215 |
{ |
| 216 |
G_Printf("G_PickTarget: target %s not found\n", targetname); |
| 217 |
return NULL; |
| 218 |
} |
| 219 |
|
| 220 |
return choice[rand() % num_choices]; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
void G_UseTargets( gentity_t *ent, gentity_t *activator ) { |
| 236 |
gentity_t *t; |
| 237 |
|
| 238 |
if ( !ent ) { |
| 239 |
return; |
| 240 |
} |
| 241 |
|
| 242 |
if (ent->targetShaderName && ent->targetShaderNewName) { |
| 243 |
float f = level.time * 0.001; |
| 244 |
AddRemap(ent->targetShaderName, ent->targetShaderNewName, f); |
| 245 |
trap_SetConfigstring(CS_SHADERSTATE, BuildShaderStateConfig()); |
| 246 |
} |
| 247 |
|
| 248 |
if ( !ent->target ) { |
| 249 |
return; |
| 250 |
} |
| 251 |
|
| 252 |
t = NULL; |
| 253 |
while ( (t = G_Find (t, FOFS(targetname), ent->target)) != NULL ) { |
| 254 |
if ( t == ent ) { |
| 255 |
G_Printf ("WARNING: Entity used itself.\n"); |
| 256 |
} else { |
| 257 |
if ( t->use ) { |
| 258 |
t->use (t, ent, activator); |
| 259 |
} |
| 260 |
} |
| 261 |
if ( !ent->inuse ) { |
| 262 |
G_Printf("entity was removed while using targets\n"); |
| 263 |
return; |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
float *tv( float x, float y, float z ) { |
| 278 |
static int index; |
| 279 |
static vec3_t vecs[8]; |
| 280 |
float *v; |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
v = vecs[index]; |
| 285 |
index = (index + 1)&7; |
| 286 |
|
| 287 |
v[0] = x; |
| 288 |
v[1] = y; |
| 289 |
v[2] = z; |
| 290 |
|
| 291 |
return v; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
char *vtos( const vec3_t v ) { |
| 304 |
static int index; |
| 305 |
static char str[8][32]; |
| 306 |
char *s; |
| 307 |
|
| 308 |
|
| 309 |
s = str[index]; |
| 310 |
index = (index + 1)&7; |
| 311 |
|
| 312 |
Com_sprintf (s, 32, "(%i %i %i)", (int)v[0], (int)v[1], (int)v[2]); |
| 313 |
|
| 314 |
return s; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
void G_SetMovedir( vec3_t angles, vec3_t movedir ) { |
| 329 |
static vec3_t VEC_UP = {0, -1, 0}; |
| 330 |
static vec3_t MOVEDIR_UP = {0, 0, 1}; |
| 331 |
static vec3_t VEC_DOWN = {0, -2, 0}; |
| 332 |
static vec3_t MOVEDIR_DOWN = {0, 0, -1}; |
| 333 |
|
| 334 |
if ( VectorCompare (angles, VEC_UP) ) { |
| 335 |
VectorCopy (MOVEDIR_UP, movedir); |
| 336 |
} else if ( VectorCompare (angles, VEC_DOWN) ) { |
| 337 |
VectorCopy (MOVEDIR_DOWN, movedir); |
| 338 |
} else { |
| 339 |
AngleVectors (angles, movedir, NULL, NULL); |
| 340 |
} |
| 341 |
VectorClear( angles ); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
float vectoyaw( const vec3_t vec ) { |
| 346 |
float yaw; |
| 347 |
|
| 348 |
if (vec[YAW] == 0 && vec[PITCH] == 0) { |
| 349 |
yaw = 0; |
| 350 |
} else { |
| 351 |
if (vec[PITCH]) { |
| 352 |
yaw = ( atan2( vec[YAW], vec[PITCH]) * 180 / M_PI ); |
| 353 |
} else if (vec[YAW] > 0) { |
| 354 |
yaw = 90; |
| 355 |
} else { |
| 356 |
yaw = 270; |
| 357 |
} |
| 358 |
if (yaw < 0) { |
| 359 |
yaw += 360; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
return yaw; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
void G_InitGentity( gentity_t *e ) { |
| 368 |
e->inuse = qtrue; |
| 369 |
e->classname = "noclass"; |
| 370 |
e->s.number = e - g_entities; |
| 371 |
e->r.ownerNum = ENTITYNUM_NONE; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
gentity_t *G_Spawn( void ) { |
| 390 |
int i, force; |
| 391 |
gentity_t *e; |
| 392 |
|
| 393 |
e = NULL; |
| 394 |
for ( force = 0 ; force < 2 ; force++ ) { |
| 395 |
|
| 396 |
|
| 397 |
e = &g_entities[MAX_CLIENTS]; |
| 398 |
for ( i = MAX_CLIENTS ; i<level.num_entities ; i++, e++) { |
| 399 |
if ( e->inuse ) { |
| 400 |
continue; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
if ( !force && e->freetime > level.startTime + 2000 && level.time - e->freetime < 1000 ) { |
| 406 |
continue; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
G_InitGentity( e ); |
| 411 |
return e; |
| 412 |
} |
| 413 |
if ( level.num_entities < ENTITYNUM_MAX_NORMAL ) { |
| 414 |
break; |
| 415 |
} |
| 416 |
} |
| 417 |
if ( level.num_entities == ENTITYNUM_MAX_NORMAL ) { |
| 418 |
for (i = 0; i < MAX_GENTITIES; i++) { |
| 419 |
G_Printf("%4i: %s\n", i, g_entities[i].classname); |
| 420 |
} |
| 421 |
G_Error( "G_Spawn: no free entities" ); |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
level.num_entities++; |
| 426 |
|
| 427 |
|
| 428 |
trap_LocateGameData( level.gentities, level.num_entities, sizeof( gentity_t ), |
| 429 |
&level.clients[0].ps, sizeof( level.clients[0] ) ); |
| 430 |
|
| 431 |
G_InitGentity( e ); |
| 432 |
return e; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
qboolean G_EntitiesFree( void ) { |
| 441 |
int i; |
| 442 |
gentity_t *e; |
| 443 |
|
| 444 |
if ( level.num_entities < ENTITYNUM_MAX_NORMAL ) { |
| 445 |
|
| 446 |
return qtrue; |
| 447 |
} |
| 448 |
|
| 449 |
e = &g_entities[MAX_CLIENTS]; |
| 450 |
for ( i = MAX_CLIENTS; i < level.num_entities; i++, e++) { |
| 451 |
if ( e->inuse ) { |
| 452 |
continue; |
| 453 |
} |
| 454 |
|
| 455 |
return qtrue; |
| 456 |
} |
| 457 |
return qfalse; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
void G_FreeEntity( gentity_t *ed ) { |
| 469 |
trap_UnlinkEntity (ed); |
| 470 |
|
| 471 |
if ( ed->neverFree ) { |
| 472 |
return; |
| 473 |
} |
| 474 |
|
| 475 |
memset (ed, 0, sizeof(*ed)); |
| 476 |
ed->classname = "freed"; |
| 477 |
ed->freetime = level.time; |
| 478 |
ed->inuse = qfalse; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
gentity_t *G_TempEntity( vec3_t origin, int event ) { |
| 491 |
gentity_t *e; |
| 492 |
vec3_t snapped; |
| 493 |
|
| 494 |
e = G_Spawn(); |
| 495 |
e->s.eType = ET_EVENTS + event; |
| 496 |
|
| 497 |
e->classname = "tempEntity"; |
| 498 |
e->eventTime = level.time; |
| 499 |
e->freeAfterEvent = qtrue; |
| 500 |
|
| 501 |
VectorCopy( origin, snapped ); |
| 502 |
SnapVector( snapped ); |
| 503 |
G_SetOrigin( e, snapped ); |
| 504 |
|
| 505 |
|
| 506 |
trap_LinkEntity( e ); |
| 507 |
|
| 508 |
return e; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
void G_KillBox (gentity_t *ent) { |
| 530 |
int i, num; |
| 531 |
int touch[MAX_GENTITIES]; |
| 532 |
gentity_t *hit; |
| 533 |
vec3_t mins, maxs; |
| 534 |
|
| 535 |
VectorAdd( ent->client->ps.origin, ent->r.mins, mins ); |
| 536 |
VectorAdd( ent->client->ps.origin, ent->r.maxs, maxs ); |
| 537 |
num = trap_EntitiesInBox( mins, maxs, touch, MAX_GENTITIES ); |
| 538 |
|
| 539 |
for (i=0 ; i<num ; i++) { |
| 540 |
hit = &g_entities[touch[i]]; |
| 541 |
if ( !hit->client ) { |
| 542 |
continue; |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
G_Damage ( hit, ent, ent, NULL, NULL, |
| 547 |
100000, DAMAGE_NO_PROTECTION, MOD_TELEFRAG); |
| 548 |
} |
| 549 |
|
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
void G_AddPredictableEvent( gentity_t *ent, int event, int eventParm ) { |
| 564 |
if ( !ent->client ) { |
| 565 |
return; |
| 566 |
} |
| 567 |
BG_AddPredictableEventToPlayerstate( event, eventParm, &ent->client->ps ); |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
void G_AddEvent( gentity_t *ent, int event, int eventParm ) { |
| 579 |
int bits; |
| 580 |
|
| 581 |
if ( !event ) { |
| 582 |
G_Printf( "G_AddEvent: zero event added for entity %i\n", ent->s.number ); |
| 583 |
return; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
if ( ent->client ) { |
| 588 |
bits = ent->client->ps.externalEvent & EV_EVENT_BITS; |
| 589 |
bits = ( bits + EV_EVENT_BIT1 ) & EV_EVENT_BITS; |
| 590 |
ent->client->ps.externalEvent = event | bits; |
| 591 |
ent->client->ps.externalEventParm = eventParm; |
| 592 |
ent->client->ps.externalEventTime = level.time; |
| 593 |
} else { |
| 594 |
bits = ent->s.event & EV_EVENT_BITS; |
| 595 |
bits = ( bits + EV_EVENT_BIT1 ) & EV_EVENT_BITS; |
| 596 |
ent->s.event = event | bits; |
| 597 |
ent->s.eventParm = eventParm; |
| 598 |
} |
| 599 |
ent->eventTime = level.time; |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
void G_Sound( gentity_t *ent, int channel, int soundIndex ) { |
| 609 |
gentity_t *te; |
| 610 |
|
| 611 |
te = G_TempEntity( ent->r.currentOrigin, EV_GENERAL_SOUND ); |
| 612 |
te->s.eventParm = soundIndex; |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
void G_SetOrigin( gentity_t *ent, vec3_t origin ) { |
| 627 |
VectorCopy( origin, ent->s.pos.trBase ); |
| 628 |
ent->s.pos.trType = TR_STATIONARY; |
| 629 |
ent->s.pos.trTime = 0; |
| 630 |
ent->s.pos.trDuration = 0; |
| 631 |
VectorClear( ent->s.pos.trDelta ); |
| 632 |
|
| 633 |
VectorCopy( origin, ent->r.currentOrigin ); |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
int DebugLine(vec3_t start, vec3_t end, int color) { |
| 645 |
vec3_t points[4], dir, cross, up = {0, 0, 1}; |
| 646 |
float dot; |
| 647 |
|
| 648 |
VectorCopy(start, points[0]); |
| 649 |
VectorCopy(start, points[1]); |
| 650 |
|
| 651 |
VectorCopy(end, points[2]); |
| 652 |
|
| 653 |
VectorCopy(end, points[3]); |
| 654 |
|
| 655 |
|
| 656 |
VectorSubtract(end, start, dir); |
| 657 |
VectorNormalize(dir); |
| 658 |
dot = DotProduct(dir, up); |
| 659 |
if (dot > 0.99 || dot < -0.99) VectorSet(cross, 1, 0, 0); |
| 660 |
else CrossProduct(dir, up, cross); |
| 661 |
|
| 662 |
VectorNormalize(cross); |
| 663 |
|
| 664 |
VectorMA(points[0], 2, cross, points[0]); |
| 665 |
VectorMA(points[1], -2, cross, points[1]); |
| 666 |
VectorMA(points[2], -2, cross, points[2]); |
| 667 |
VectorMA(points[3], 2, cross, points[3]); |
| 668 |
|
| 669 |
return trap_DebugPolygonCreate(color, 4, points); |
| 670 |
} |