Debian Codegraph @

code/cgame/cg_ents.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 // cg_ents.c -- present snapshot entities, happens every single frame
24
25 #include "cg_local.h"
26
27
28 /*
29 ======================
30 CG_PositionEntityOnTag
31
32 Modifies the entities position and axis by the given
33 tag location
34 ======================
35 */
36 void CG_PositionEntityOnTag( refEntity_t *entity, const refEntity_t *parent,
37 qhandle_t parentModel, char *tagName ) {
38 int i;
39 orientation_t lerped;
40
41 // lerp the tag
42 trap_R_LerpTag( &lerped, parentModel, parent->oldframe, parent->frame,
43 1.0 - parent->backlerp, tagName );
44
45 // FIXME: allow origin offsets along tag?
46 VectorCopy( parent->origin, entity->origin );
47 for ( i = 0 ; i < 3 ; i++ ) {
48 VectorMA( entity->origin, lerped.origin[i], parent->axis[i], entity->origin );
49 }
50
51 // had to cast away the const to avoid compiler problems...
52 MatrixMultiply( lerped.axis, ((refEntity_t *)parent)->axis, entity->axis );
53 entity->backlerp = parent->backlerp;
54 }
55
56
57 /*
58 ======================
59 CG_PositionRotatedEntityOnTag
60
61 Modifies the entities position and axis by the given
62 tag location
63 ======================
64 */
65 void CG_PositionRotatedEntityOnTag( refEntity_t *entity, const refEntity_t *parent,
66 qhandle_t parentModel, char *tagName ) {
67 int i;
68 orientation_t lerped;
69 vec3_t tempAxis[3];
70
71 //AxisClear( entity->axis );
72 // lerp the tag
73 trap_R_LerpTag( &lerped, parentModel, parent->oldframe, parent->frame,
74 1.0 - parent->backlerp, tagName );
75
76 // FIXME: allow origin offsets along tag?
77 VectorCopy( parent->origin, entity->origin );
78 for ( i = 0 ; i < 3 ; i++ ) {
79 VectorMA( entity->origin, lerped.origin[i], parent->axis[i], entity->origin );
80 }
81
82 // had to cast away the const to avoid compiler problems...
83 MatrixMultiply( entity->axis, lerped.axis, tempAxis );
84 MatrixMultiply( tempAxis, ((refEntity_t *)parent)->axis, entity->axis );
85 }
86
87
88
89 /*
90 ==========================================================================
91
92 FUNCTIONS CALLED EACH FRAME
93
94 ==========================================================================
95 */
96
97 /*
98 ======================
99 CG_SetEntitySoundPosition
100
101 Also called by event processing code
102 ======================
103 */
104 void CG_SetEntitySoundPosition( centity_t *cent ) {
105 if ( cent->currentState.solid == SOLID_BMODEL ) {
106 vec3_t origin;
107 float *v;
108
109 v = cgs.inlineModelMidpoints[ cent->currentState.modelindex ];
110 VectorAdd( cent->lerpOrigin, v, origin );
111 trap_S_UpdateEntityPosition( cent->currentState.number, origin );
112 } else {
113 trap_S_UpdateEntityPosition( cent->currentState.number, cent->lerpOrigin );
114 }
115 }
116
117 /*
118 ==================
119 CG_EntityEffects
120
121 Add continuous entity effects, like local entity emission and lighting
122 ==================
123 */
124 static void CG_EntityEffects( centity_t *cent ) {
125
126 // update sound origins
127 CG_SetEntitySoundPosition( cent );
128
129 // add loop sound
130 if ( cent->currentState.loopSound ) {
131 if (cent->currentState.eType != ET_SPEAKER) {
132 trap_S_AddLoopingSound( cent->currentState.number, cent->lerpOrigin, vec3_origin,
133 cgs.gameSounds[ cent->currentState.loopSound ] );
134 } else {
135 trap_S_AddRealLoopingSound( cent->currentState.number, cent->lerpOrigin, vec3_origin,
136 cgs.gameSounds[ cent->currentState.loopSound ] );
137 }
138 }
139
140
141 // constant light glow
142 if(cent->currentState.constantLight)
143 {
144 int cl;
145 float i, r, g, b;
146
147 cl = cent->currentState.constantLight;
148 r = (float) (cl & 0xFF) / 255.0;
149 g = (float) ((cl >> 8) & 0xFF) / 255.0;
150 b = (float) ((cl >> 16) & 0xFF) / 255.0;
151 i = (float) ((cl >> 24) & 0xFF) * 4.0;
152 trap_R_AddLightToScene(cent->lerpOrigin, i, r, g, b);
153 }
154
155 }
156
157
158 /*
159 ==================
160 CG_General
161 ==================
162 */
163 static void CG_General( centity_t *cent ) {
164 refEntity_t ent;
165 entityState_t *s1;
166
167 s1 = &cent->currentState;
168
169 // if set to invisible, skip
170 if (!s1->modelindex) {
171 return;
172 }
173
174 memset (&ent, 0, sizeof(ent));
175
176 // set frame
177
178 ent.frame = s1->frame;
179 ent.oldframe = ent.frame;
180 ent.backlerp = 0;
181
182 VectorCopy( cent->lerpOrigin, ent.origin);
183 VectorCopy( cent->lerpOrigin, ent.oldorigin);
184
185 ent.hModel = cgs.gameModels[s1->modelindex];
186
187 // player model
188 if (s1->number == cg.snap->ps.clientNum) {
189 ent.renderfx |= RF_THIRD_PERSON; // only draw from mirrors
190 }
191
192 // convert angles to axis
193 AnglesToAxis( cent->lerpAngles, ent.axis );
194
195 // add to refresh list
196 trap_R_AddRefEntityToScene (&ent);
197 }
198
199 /*
200 ==================
201 CG_Speaker
202
203 Speaker entities can automatically play sounds
204 ==================
205 */
206 static void CG_Speaker( centity_t *cent ) {
207 if ( ! cent->currentState.clientNum ) { // FIXME: use something other than clientNum...
208 return; // not auto triggering
209 }
210
211 if ( cg.time < cent->miscTime ) {
212 return;
213 }
214
215 trap_S_StartSound (NULL, cent->currentState.number, CHAN_ITEM, cgs.gameSounds[cent->currentState.eventParm] );
216
217 // ent->s.frame = ent->wait * 10;
218 // ent->s.clientNum = ent->random * 10;
219 cent->miscTime = cg.time + cent->currentState.frame * 100 + cent->currentState.clientNum * 100 * crandom();
220 }
221
222 /*
223 ==================
224 CG_Item
225 ==================
226 */
227 static void CG_Item( centity_t *cent ) {
228 refEntity_t ent;
229 entityState_t *es;
230 gitem_t *item;
231 int msec;
232 float frac;
233 float scale;
234 weaponInfo_t *wi;
235
236 es = &cent->currentState;
237 if ( es->modelindex >= bg_numItems ) {
238 CG_Error( "Bad item index %i on entity", es->modelindex );
239 }
240
241 // if set to invisible, skip
242 if ( !es->modelindex || ( es->eFlags & EF_NODRAW ) ) {
243 return;
244 }
245
246 item = &bg_itemlist[ es->modelindex ];
247 if ( cg_simpleItems.integer && item->giType != IT_TEAM ) {
248 memset( &ent, 0, sizeof( ent ) );
249 ent.reType = RT_SPRITE;
250 VectorCopy( cent->lerpOrigin, ent.origin );
251 ent.radius = 14;
252 ent.customShader = cg_items[es->modelindex].icon;
253 ent.shaderRGBA[0] = 255;
254 ent.shaderRGBA[1] = 255;
255 ent.shaderRGBA[2] = 255;
256 ent.shaderRGBA[3] = 255;
257 trap_R_AddRefEntityToScene(&ent);
258 return;
259 }
260
261 // items bob up and down continuously
262 scale = 0.005 + cent->currentState.number * 0.00001;
263 cent->lerpOrigin[2] += 4 + cos( ( cg.time + 1000 ) * scale ) * 4;
264
265 memset (&ent, 0, sizeof(ent));
266
267 // autorotate at one of two speeds
268 if ( item->giType == IT_HEALTH ) {
269 VectorCopy( cg.autoAnglesFast, cent->lerpAngles );
270 AxisCopy( cg.autoAxisFast, ent.axis );
271 } else {
272 VectorCopy( cg.autoAngles, cent->lerpAngles );
273 AxisCopy( cg.autoAxis, ent.axis );
274 }
275
276 wi = NULL;
277 // the weapons have their origin where they attatch to player
278 // models, so we need to offset them or they will rotate
279 // eccentricly
280 if ( item->giType == IT_WEAPON ) {
281 wi = &cg_weapons[item->giTag];
282 cent->lerpOrigin[0] -=
283 wi->weaponMidpoint[0] * ent.axis[0][0] +
284 wi->weaponMidpoint[1] * ent.axis[1][0] +
285 wi->weaponMidpoint[2] * ent.axis[2][0];
286 cent->lerpOrigin[1] -=
287 wi->weaponMidpoint[0] * ent.axis[0][1] +
288 wi->weaponMidpoint[1] * ent.axis[1][1] +
289 wi->weaponMidpoint[2] * ent.axis[2][1];
290 cent->lerpOrigin[2] -=
291 wi->weaponMidpoint[0] * ent.axis[0][2] +
292 wi->weaponMidpoint[1] * ent.axis[1][2] +
293 wi->weaponMidpoint[2] * ent.axis[2][2];
294
295 cent->lerpOrigin[2] += 8; // an extra height boost
296 }
297
298 ent.hModel = cg_items[es->modelindex].models[0];
299
300 VectorCopy( cent->lerpOrigin, ent.origin);
301 VectorCopy( cent->lerpOrigin, ent.oldorigin);
302
303 ent.nonNormalizedAxes = qfalse;
304
305 // if just respawned, slowly scale up
306 msec = cg.time - cent->miscTime;
307 if ( msec >= 0 && msec < ITEM_SCALEUP_TIME ) {
308 frac = (float)msec / ITEM_SCALEUP_TIME;
309 VectorScale( ent.axis[0], frac, ent.axis[0] );
310 VectorScale( ent.axis[1], frac, ent.axis[1] );
311 VectorScale( ent.axis[2], frac, ent.axis[2] );
312 ent.nonNormalizedAxes = qtrue;
313 } else {
314 frac = 1.0;
315 }
316
317 // items without glow textures need to keep a minimum light value
318 // so they are always visible
319 if ( ( item->giType == IT_WEAPON ) ||
320 ( item->giType == IT_ARMOR ) ) {
321 ent.renderfx |= RF_MINLIGHT;
322 }
323
324 // increase the size of the weapons when they are presented as items
325 if ( item->giType == IT_WEAPON ) {
326 VectorScale( ent.axis[0], 1.5, ent.axis[0] );
327 VectorScale( ent.axis[1], 1.5, ent.axis[1] );
328 VectorScale( ent.axis[2], 1.5, ent.axis[2] );
329 ent.nonNormalizedAxes = qtrue;
330 trap_S_AddLoopingSound( cent->currentState.number, cent->lerpOrigin, vec3_origin, cgs.media.weaponHoverSound );
331 }
332
333 if ( item->giType == IT_HOLDABLE && item->giTag == HI_KAMIKAZE ) {
334 VectorScale( ent.axis[0], 2, ent.axis[0] );
335 VectorScale( ent.axis[1], 2, ent.axis[1] );
336 VectorScale( ent.axis[2], 2, ent.axis[2] );
337 ent.nonNormalizedAxes = qtrue;
338 }
339
340 // add to refresh list
341 trap_R_AddRefEntityToScene(&ent);
342
343 if ( item->giType == IT_WEAPON && wi->barrelModel ) {
344 refEntity_t barrel;
345
346 memset( &barrel, 0, sizeof( barrel ) );
347
348 barrel.hModel = wi->barrelModel;
349
350 VectorCopy( ent.lightingOrigin, barrel.lightingOrigin );
351 barrel.shadowPlane = ent.shadowPlane;
352 barrel.renderfx = ent.renderfx;
353
354 CG_PositionRotatedEntityOnTag( &barrel, &ent, wi->weaponModel, "tag_barrel" );
355
356 AxisCopy( ent.axis, barrel.axis );
357 barrel.nonNormalizedAxes = ent.nonNormalizedAxes;
358
359 trap_R_AddRefEntityToScene( &barrel );
360 }
361
362 // accompanying rings / spheres for powerups
363 if ( !cg_simpleItems.integer )
364 {
365 vec3_t spinAngles;
366
367 VectorClear( spinAngles );
368
369 if ( item->giType == IT_HEALTH || item->giType == IT_POWERUP )
370 {
371 if ( ( ent.hModel = cg_items[es->modelindex].models[1] ) != 0 )
372 {
373 if ( item->giType == IT_POWERUP )
374 {
375 ent.origin[2] += 12;
376 spinAngles[1] = ( cg.time & 1023 ) * 360 / -1024.0f;
377 }
378 AnglesToAxis( spinAngles, ent.axis );
379
380 // scale up if respawning
381 if ( frac != 1.0 ) {
382 VectorScale( ent.axis[0], frac, ent.axis[0] );
383 VectorScale( ent.axis[1], frac, ent.axis[1] );
384 VectorScale( ent.axis[2], frac, ent.axis[2] );
385 ent.nonNormalizedAxes = qtrue;
386 }
387 trap_R_AddRefEntityToScene( &ent );
388 }
389 }
390 }
391 }
392
393 //============================================================================
394
395 /*
396 ===============
397 CG_Missile
398 ===============
399 */
400 static void CG_Missile( centity_t *cent ) {
401 refEntity_t ent;
402 entityState_t *s1;
403 const weaponInfo_t *weapon;
404 // int col;
405
406 s1 = &cent->currentState;
407 if ( s1->weapon >= WP_NUM_WEAPONS ) {
408 s1->weapon = 0;
409 }
410 weapon = &cg_weapons[s1->weapon];
411
412 // calculate the axis
413 VectorCopy( s1->angles, cent->lerpAngles);
414
415 // add trails
416 if ( weapon->missileTrailFunc )
417 {
418 weapon->missileTrailFunc( cent, weapon );
419 }
420 /*
421 if ( cent->currentState.modelindex == TEAM_RED ) {
422 col = 1;
423 }
424 else if ( cent->currentState.modelindex == TEAM_BLUE ) {
425 col = 2;
426 }
427 else {
428 col = 0;
429 }
430
431 // add dynamic light
432 if ( weapon->missileDlight ) {
433 trap_R_AddLightToScene(cent->lerpOrigin, weapon->missileDlight,
434 weapon->missileDlightColor[col][0], weapon->missileDlightColor[col][1], weapon->missileDlightColor[col][2] );
435 }
436 */
437 // add dynamic light
438 if ( weapon->missileDlight ) {
439 trap_R_AddLightToScene(cent->lerpOrigin, weapon->missileDlight,
440 weapon->missileDlightColor[0], weapon->missileDlightColor[1], weapon->missileDlightColor[2] );
441 }
442
443 // add missile sound
444 if ( weapon->missileSound ) {
445 vec3_t velocity;
446
447 BG_EvaluateTrajectoryDelta( &cent->currentState.pos, cg.time, velocity );
448
449 trap_S_AddLoopingSound( cent->currentState.number, cent->lerpOrigin, velocity, weapon->missileSound );
450 }
451
452 // create the render entity
453 memset (&ent, 0, sizeof(ent));
454 VectorCopy( cent->lerpOrigin, ent.origin);
455 VectorCopy( cent->lerpOrigin, ent.oldorigin);
456
457 if ( cent->currentState.weapon == WP_PLASMAGUN ) {
458 ent.reType = RT_SPRITE;
459 ent.radius = 16;
460 ent.rotation = 0;
461 ent.customShader = cgs.media.plasmaBallShader;
462 trap_R_AddRefEntityToScene( &ent );
463 return;
464 }
465
466 // flicker between two skins
467 ent.skinNum = cg.clientFrame & 1;
468 ent.hModel = weapon->missileModel;
469 ent.renderfx = weapon->missileRenderfx | RF_NOSHADOW;
470
471 //#ifdef MISSIONPACK
472 if ( cent->currentState.weapon == WP_PROX_LAUNCHER ) {
473 if (s1->generic1 == TEAM_BLUE) {
474 ent.hModel = cgs.media.blueProxMine;
475 }
476 }
477 //#endif
478
479 // convert direction of travel into axis
480 if ( VectorNormalize2( s1->pos.trDelta, ent.axis[0] ) == 0 ) {
481 ent.axis[0][2] = 1;
482 }
483
484 // spin as it moves
485 if ( s1->pos.trType != TR_STATIONARY ) {
486 RotateAroundDirection( ent.axis, cg.time / 4 );
487 } else {
488 //#ifdef MISSIONPACK
489 if ( s1->weapon == WP_PROX_LAUNCHER ) {
490 AnglesToAxis( cent->lerpAngles, ent.axis );
491 }
492 else
493 //#endif
494 {
495 RotateAroundDirection( ent.axis, s1->time );
496 }
497 }
498
499 // add to refresh list, possibly with quad glow
500 CG_AddRefEntityWithPowerups( &ent, s1, TEAM_FREE, qtrue );
501 }
502
503 /*
504 ===============
505 CG_Grapple
506
507 This is called when the grapple is sitting up against the wall
508 ===============
509 */
510 static void CG_Grapple( centity_t *cent ) {
511 refEntity_t ent;
512 entityState_t *s1;
513 const weaponInfo_t *weapon;
514
515 s1 = &cent->currentState;
516 if ( s1->weapon >= WP_NUM_WEAPONS ) {
517 s1->weapon = 0;
518 }
519 weapon = &cg_weapons[s1->weapon];
520
521 // calculate the axis
522 VectorCopy( s1->angles, cent->lerpAngles);
523
524 // FIXME add grapple pull sound here..?
525 // add missile sound
526 if ( weapon->missileSound ) {
527 trap_S_AddLoopingSound( cent->currentState.number, cent->lerpOrigin, vec3_origin, weapon->missileSound );
528 }
529
530
531 // Will draw cable if needed
532 CG_GrappleTrail ( cent, weapon );
533
534 // create the render entity
535 memset (&ent, 0, sizeof(ent));
536 VectorCopy( cent->lerpOrigin, ent.origin);
537 VectorCopy( cent->lerpOrigin, ent.oldorigin);
538
539 // flicker between two skins
540 ent.skinNum = cg.clientFrame & 1;
541 ent.hModel = weapon->missileModel;
542 ent.renderfx = weapon->missileRenderfx | RF_NOSHADOW;
543
544 // convert direction of travel into axis
545 if ( VectorNormalize2( s1->pos.trDelta, ent.axis[0] ) == 0 ) {
546 ent.axis[0][2] = 1;
547 }
548
549 trap_R_AddRefEntityToScene( &ent );
550 }
551
552 /*
553 ===============
554 CG_Mover
555 ===============
556 */
557 static void CG_Mover( centity_t *cent ) {
558 refEntity_t ent;
559 entityState_t *s1;
560
561 s1 = &cent->currentState;
562
563 // create the render entity
564 memset (&ent, 0, sizeof(ent));
565 VectorCopy( cent->lerpOrigin, ent.origin);
566 VectorCopy( cent->lerpOrigin, ent.oldorigin);
567 AnglesToAxis( cent->lerpAngles, ent.axis );
568
569 ent.renderfx = RF_NOSHADOW;
570
571 // flicker between two skins (FIXME?)
572 ent.skinNum = ( cg.time >> 6 ) & 1;
573
574 // get the model, either as a bmodel or a modelindex
575 if ( s1->solid == SOLID_BMODEL ) {
576 ent.hModel = cgs.inlineDrawModel[s1->modelindex];
577 } else {
578 ent.hModel = cgs.gameModels[s1->modelindex];
579 }
580
581 // add to refresh list
582 trap_R_AddRefEntityToScene(&ent);
583
584 // add the secondary model
585 if ( s1->modelindex2 ) {
586 ent.skinNum = 0;
587 ent.hModel = cgs.gameModels[s1->modelindex2];
588 trap_R_AddRefEntityToScene(&ent);
589 }
590
591 }
592
593 /*
594 ===============
595 CG_Beam
596
597 Also called as an event
598 ===============
599 */
600 void CG_Beam( centity_t *cent ) {
601 refEntity_t ent;
602 entityState_t *s1;
603
604 s1 = &cent->currentState;
605
606 // create the render entity
607 memset (&ent, 0, sizeof(ent));
608 VectorCopy( s1->pos.trBase, ent.origin );
609 VectorCopy( s1->origin2, ent.oldorigin );
610 AxisClear( ent.axis );
611 ent.reType = RT_BEAM;
612
613 ent.renderfx = RF_NOSHADOW;
614
615 // add to refresh list
616 trap_R_AddRefEntityToScene(&ent);
617 }
618
619
620 /*
621 ===============
622 CG_Portal
623 ===============
624 */
625 static void CG_Portal( centity_t *cent ) {
626 refEntity_t ent;
627 entityState_t *s1;
628
629 s1 = &cent->currentState;
630
631 // create the render entity
632 memset (&ent, 0, sizeof(ent));
633 VectorCopy( cent->lerpOrigin, ent.origin );
634 VectorCopy( s1->origin2, ent.oldorigin );
635 ByteToDir( s1->eventParm, ent.axis[0] );
636 PerpendicularVector( ent.axis[1], ent.axis[0] );
637
638 // negating this tends to get the directions like they want
639 // we really should have a camera roll value
640 VectorSubtract( vec3_origin, ent.axis[1], ent.axis[1] );
641
642 CrossProduct( ent.axis[0], ent.axis[1], ent.axis[2] );
643 ent.reType = RT_PORTALSURFACE;
644 ent.oldframe = s1->powerups;
645 ent.frame = s1->frame; // rotation speed
646 ent.skinNum = s1->clientNum/256.0 * 360; // roll offset
647
648 // add to refresh list
649 trap_R_AddRefEntityToScene(&ent);
650 }
651
652
653 /*
654 =========================
655 CG_AdjustPositionForMover
656
657 Also called by client movement prediction code
658 =========================
659 */
660 void CG_AdjustPositionForMover( const vec3_t in, int moverNum, int fromTime, int toTime, vec3_t out ) {
661 centity_t *cent;
662 vec3_t oldOrigin, origin, deltaOrigin;
663 vec3_t oldAngles, angles, deltaAngles;
664
665 if ( moverNum <= 0 || moverNum >= ENTITYNUM_MAX_NORMAL ) {
666 VectorCopy( in, out );
667 return;
668 }
669
670 cent = &cg_entities[ moverNum ];
671 if ( cent->currentState.eType != ET_MOVER ) {
672 VectorCopy( in, out );
673 return;
674 }
675
676 BG_EvaluateTrajectory( &cent->currentState.pos, fromTime, oldOrigin );
677 BG_EvaluateTrajectory( &cent->currentState.apos, fromTime, oldAngles );
678
679 BG_EvaluateTrajectory( &cent->currentState.pos, toTime, origin );
680 BG_EvaluateTrajectory( &cent->currentState.apos, toTime, angles );
681
682 VectorSubtract( origin, oldOrigin, deltaOrigin );
683 VectorSubtract( angles, oldAngles, deltaAngles );
684
685 VectorAdd( in, deltaOrigin, out );
686
687 // FIXME: origin change when on a rotating object
688 }
689
690
691 /*
692 =============================
693 CG_InterpolateEntityPosition
694 =============================
695 */
696 static void CG_InterpolateEntityPosition( centity_t *cent ) {
697 vec3_t current, next;
698 float f;
699
700 // it would be an internal error to find an entity that interpolates without
701 // a snapshot ahead of the current one
702 if ( cg.nextSnap == NULL ) {
703 CG_Error( "CG_InterpoateEntityPosition: cg.nextSnap == NULL" );
704 }
705
706 f = cg.frameInterpolation;
707
708 // this will linearize a sine or parabolic curve, but it is important
709 // to not extrapolate player positions if more recent data is available
710 BG_EvaluateTrajectory( &cent->currentState.pos, cg.snap->serverTime, current );
711 BG_EvaluateTrajectory( &cent->nextState.pos, cg.nextSnap->serverTime, next );
712
713 cent->lerpOrigin[0] = current[0] + f * ( next[0] - current[0] );
714 cent->lerpOrigin[1] = current[1] + f * ( next[1] - current[1] );
715 cent->lerpOrigin[2] = current[2] + f * ( next[2] - current[2] );
716
717 BG_EvaluateTrajectory( &cent->currentState.apos, cg.snap->serverTime, current );
718 BG_EvaluateTrajectory( &cent->nextState.apos, cg.nextSnap->serverTime, next );
719
720 cent->lerpAngles[0] = LerpAngle( current[0], next[0], f );
721 cent->lerpAngles[1] = LerpAngle( current[1], next[1], f );
722 cent->lerpAngles[2] = LerpAngle( current[2], next[2], f );
723
724 }
725
726 /*
727 ===============
728 CG_CalcEntityLerpPositions
729
730 ===============
731 */
732 static void CG_CalcEntityLerpPositions( centity_t *cent ) {
733
734 //unlagged - projectile nudge
735 // this will be set to how far forward projectiles will be extrapolated
736 int timeshift = 0;
737 //unlagged - projectile nudge
738
739 //unlagged - smooth clients #2
740 // this is done server-side now - cg_smoothClients is undefined
741 // players will always be TR_INTERPOLATE
742 /*
743 // if this player does not want to see extrapolated players
744 if ( !cg_smoothClients.integer ) {
745 // make sure the clients use TR_INTERPOLATE
746 if ( cent->currentState.number < MAX_CLIENTS ) {
747 cent->currentState.pos.trType = TR_INTERPOLATE;
748 cent->nextState.pos.trType = TR_INTERPOLATE;
749 }
750 }
751 */
752 //unlagged - smooth clients #2
753
754 if ( cent->interpolate && cent->currentState.pos.trType == TR_INTERPOLATE ) {
755 CG_InterpolateEntityPosition( cent );
756 return;
757 }
758
759 // first see if we can interpolate between two snaps for
760 // linear extrapolated clients
761 if ( cent->interpolate && cent->currentState.pos.trType == TR_LINEAR_STOP &&
762 cent->currentState.number < MAX_CLIENTS) {
763 CG_InterpolateEntityPosition( cent );
764 return;
765 }
766
767 //unlagged - timenudge extrapolation
768 // interpolating failed (probably no nextSnap), so extrapolate
769 // this can also happen if the teleport bit is flipped, but that
770 // won't be noticeable
771 if ( cent->currentState.number < MAX_CLIENTS &&
772 cent->currentState.clientNum != cg.predictedPlayerState.clientNum ) {
773 cent->currentState.pos.trType = TR_LINEAR_STOP;
774 cent->currentState.pos.trTime = cg.snap->serverTime;
775 cent->currentState.pos.trDuration = 1000 / sv_fps.integer;
776 }
777 //unlagged - timenudge extrapolation
778
779 //unlagged - projectile nudge
780 // if it's a missile but not a grappling hook
781 if ( cent->currentState.eType == ET_MISSILE && cent->currentState.weapon != WP_GRAPPLING_HOOK ) {
782 // if it's one of ours
783 if ( cent->currentState.otherEntityNum == cg.clientNum ) {
784 // extrapolate one server frame's worth - this will correct for tiny
785 // visual inconsistencies introduced by backward-reconciling all players
786 // one server frame before running projectiles
787 timeshift = 1000 / sv_fps.integer;
788 }
789 // if it's not, and it's not a grenade launcher
790 else if ( cent->currentState.weapon != WP_GRENADE_LAUNCHER ) {
791 // extrapolate based on cg_projectileNudge
792 timeshift = cg_projectileNudge.integer + 1000 / sv_fps.integer;
793 }
794 }
795
796 // just use the current frame and evaluate as best we can
797 // BG_EvaluateTrajectory( &cent->currentState.pos, cg.time, cent->lerpOrigin );
798 // BG_EvaluateTrajectory( &cent->currentState.apos, cg.time, cent->lerpAngles );
799 BG_EvaluateTrajectory( &cent->currentState.pos, cg.time + timeshift, cent->lerpOrigin );
800 BG_EvaluateTrajectory( &cent->currentState.apos, cg.time + timeshift, cent->lerpAngles );
801
802 // if there's a time shift
803 if ( timeshift != 0 ) {
804 trace_t tr;
805 vec3_t lastOrigin;
806
807 BG_EvaluateTrajectory( &cent->currentState.pos, cg.time, lastOrigin );
808
809 CG_Trace( &tr, lastOrigin, vec3_origin, vec3_origin, cent->lerpOrigin, cent->currentState.number, MASK_SHOT );
810
811 // don't let the projectile go through the floor
812 if ( tr.fraction < 1.0f ) {
813 cent->lerpOrigin[0] = lastOrigin[0] + tr.fraction * ( cent->lerpOrigin[0] - lastOrigin[0] );
814 cent->lerpOrigin[1] = lastOrigin[1] + tr.fraction * ( cent->lerpOrigin[1] - lastOrigin[1] );
815 cent->lerpOrigin[2] = lastOrigin[2] + tr.fraction * ( cent->lerpOrigin[2] - lastOrigin[2] );
816 }
817 }
818 //unlagged - projectile nudge
819
820 // adjust for riding a mover if it wasn't rolled into the predicted
821 // player state
822 if ( cent != &cg.predictedPlayerEntity ) {
823 CG_AdjustPositionForMover( cent->lerpOrigin, cent->currentState.groundEntityNum,
824 cg.snap->serverTime, cg.time, cent->lerpOrigin );
825 }
826 }
827
828 /*
829 ===============
830 CG_TeamBase
831 ===============
832 */
833 static void CG_TeamBase( centity_t *cent ) {
834 refEntity_t model;
835 //#ifdef MISSIONPACK
836 vec3_t angles;
837 int t, h;
838 float c;
839
840 if ( cgs.gametype == GT_CTF || cgs.gametype == GT_1FCTF ) {
841 //#else
842 // if ( cgs.gametype == GT_CTF) {
843 //#endif
844 // show the flag base
845 memset(&model, 0, sizeof(model));
846 model.reType = RT_MODEL;
847 VectorCopy( cent->lerpOrigin, model.lightingOrigin );
848 VectorCopy( cent->lerpOrigin, model.origin );
849 AnglesToAxis( cent->currentState.angles, model.axis );
850 if ( cent->currentState.modelindex == TEAM_RED ) {
851 model.hModel = cgs.media.redFlagBaseModel;
852 }
853 else if ( cent->currentState.modelindex == TEAM_BLUE ) {
854 model.hModel = cgs.media.blueFlagBaseModel;
855 }
856 else {
857 model.hModel = cgs.media.neutralFlagBaseModel;
858 }
859 trap_R_AddRefEntityToScene( &model );
860 }
861 //#ifdef MISSIONPACK
862 else if ( cgs.gametype == GT_OBELISK ) {
863 // show the obelisk
864 memset(&model, 0, sizeof(model));
865 model.reType = RT_MODEL;
866 VectorCopy( cent->lerpOrigin, model.lightingOrigin );
867 VectorCopy( cent->lerpOrigin, model.origin );
868 AnglesToAxis( cent->currentState.angles, model.axis );
869
870 model.hModel = cgs.media.overloadBaseModel;
871 trap_R_AddRefEntityToScene( &model );
872 // if hit
873 if ( cent->currentState.frame == 1) {
874 // show hit model
875 // modelindex2 is the health value of the obelisk
876 c = cent->currentState.modelindex2;
877 model.shaderRGBA[0] = 0xff;
878 model.shaderRGBA[1] = c;
879 model.shaderRGBA[2] = c;
880 model.shaderRGBA[3] = 0xff;
881 //
882 model.hModel = cgs.media.overloadEnergyModel;
883 trap_R_AddRefEntityToScene( &model );
884 }
885 // if respawning
886 if ( cent->currentState.frame == 2) {
887 if ( !cent->miscTime ) {
888 cent->miscTime = cg.time;
889 }
890 t = cg.time - cent->miscTime;
891 h = (cg_obeliskRespawnDelay.integer - 5) * 1000;
892 //
893 if (t > h) {
894 c = (float) (t - h) / h;
895 if (c > 1)
896 c = 1;
897 }
898 else {
899 c = 0;
900 }
901 // show the lights
902 AnglesToAxis( cent->currentState.angles, model.axis );
903 //
904 model.shaderRGBA[0] = c * 0xff;
905 model.shaderRGBA[1] = c * 0xff;
906 model.shaderRGBA[2] = c * 0xff;
907 model.shaderRGBA[3] = c * 0xff;
908
909 model.hModel = cgs.media.overloadLightsModel;
910 trap_R_AddRefEntityToScene( &model );
911 // show the target
912 if (t > h) {
913 if ( !cent->muzzleFlashTime ) {
914 trap_S_StartSound (cent->lerpOrigin, ENTITYNUM_NONE, CHAN_BODY, cgs.media.obeliskRespawnSound);
915 cent->muzzleFlashTime = 1;
916 }
917 VectorCopy(cent->currentState.angles, angles);
918 angles[YAW] += (float) 16 * acos(1-c) * 180 / M_PI;
919 AnglesToAxis( angles, model.axis );
920
921 VectorScale( model.axis[0], c, model.axis[0]);
922 VectorScale( model.axis[1], c, model.axis[1]);
923 VectorScale( model.axis[2], c, model.axis[2]);
924
925 model.shaderRGBA[0] = 0xff;
926 model.shaderRGBA[1] = 0xff;
927 model.shaderRGBA[2] = 0xff;
928 model.shaderRGBA[3] = 0xff;
929 //
930 model.origin[2] += 56;
931 model.hModel = cgs.media.overloadTargetModel;
932 trap_R_AddRefEntityToScene( &model );
933 }
934 else {
935 //FIXME: show animated smoke
936 }
937 }
938 else {
939 cent->miscTime = 0;
940 cent->muzzleFlashTime = 0;
941 // modelindex2 is the health value of the obelisk
942 c = cent->currentState.modelindex2;
943 model.shaderRGBA[0] = 0xff;
944 model.shaderRGBA[1] = c;
945 model.shaderRGBA[2] = c;
946 model.shaderRGBA[3] = 0xff;
947 // show the lights
948 model.hModel = cgs.media.overloadLightsModel;
949 trap_R_AddRefEntityToScene( &model );
950 // show the target
951 model.origin[2] += 56;
952 model.hModel = cgs.media.overloadTargetModel;
953 trap_R_AddRefEntityToScene( &model );
954 }
955 }
956 else if ( cgs.gametype == GT_HARVESTER ) {
957 // show harvester model
958 memset(&model, 0, sizeof(model));
959 model.reType = RT_MODEL;
960 VectorCopy( cent->lerpOrigin, model.lightingOrigin );
961 VectorCopy( cent->lerpOrigin, model.origin );
962 AnglesToAxis( cent->currentState.angles, model.axis );
963
964 if ( cent->currentState.modelindex == TEAM_RED ) {
965 model.hModel = cgs.media.harvesterModel;
966 model.customSkin = cgs.media.harvesterRedSkin;
967 }
968 else if ( cent->currentState.modelindex == TEAM_BLUE ) {
969 model.hModel = cgs.media.harvesterModel;
970 model.customSkin = cgs.media.harvesterBlueSkin;
971 }
972 else {
973 model.hModel = cgs.media.harvesterNeutralModel;
974 model.customSkin = 0;
975 }
976 trap_R_AddRefEntityToScene( &model );
977 }
978 //#endif
979 }
980
981 /*
982 ===============
983 CG_AddCEntity
984
985 ===============
986 */
987 static void CG_AddCEntity( centity_t *cent ) {
988 // event-only entities will have been dealt with already
989 if ( cent->currentState.eType >= ET_EVENTS ) {
990 return;
991 }
992
993 // calculate the current origin
994 CG_CalcEntityLerpPositions( cent );
995
996 // add automatic effects
997 CG_EntityEffects( cent );
998
999 switch ( cent->currentState.eType ) {
1000 default:
1001 CG_Error( "Bad entity type: %i\n", cent->currentState.eType );
1002 break;
1003 case ET_INVISIBLE:
1004 case ET_PUSH_TRIGGER:
1005 case ET_TELEPORT_TRIGGER:
1006 break;
1007 case ET_GENERAL:
1008 CG_General( cent );
1009 break;
1010 case ET_PLAYER:
1011 CG_Player( cent );
1012 break;
1013 case ET_ITEM:
1014 CG_Item( cent );
1015 break;
1016 case ET_MISSILE:
1017 CG_Missile( cent );
1018 break;
1019 case ET_MOVER:
1020 CG_Mover( cent );
1021 break;
1022 case ET_BEAM:
1023 CG_Beam( cent );
1024 break;
1025 case ET_PORTAL:
1026 CG_Portal( cent );
1027 break;
1028 case ET_SPEAKER:
1029 CG_Speaker( cent );
1030 break;
1031 case ET_GRAPPLE:
1032 CG_Grapple( cent );
1033 break;
1034 case ET_TEAM:
1035 CG_TeamBase( cent );
1036 break;
1037 }
1038 }
1039
1040 /*
1041 ===============
1042 CG_AddPacketEntities
1043
1044 ===============
1045 */
1046 void CG_AddPacketEntities( void ) {
1047 int num;
1048 centity_t *cent;
1049 playerState_t *ps;
1050
1051 // set cg.frameInterpolation
1052 if ( cg.nextSnap ) {
1053 int delta;
1054
1055 delta = (cg.nextSnap->serverTime - cg.snap->serverTime);
1056 if ( delta == 0 ) {
1057 cg.frameInterpolation = 0;
1058 } else {
1059 cg.frameInterpolation = (float)( cg.time - cg.snap->serverTime ) / delta;
1060 }
1061 } else {
1062 cg.frameInterpolation = 0; // actually, it should never be used, because
1063 // no entities should be marked as interpolating
1064 }
1065
1066 // the auto-rotating items will all have the same axis
1067 cg.autoAngles[0] = 0;
1068 cg.autoAngles[1] = ( cg.time & 2047 ) * 360 / 2048.0;
1069 cg.autoAngles[2] = 0;
1070
1071 cg.autoAnglesFast[0] = 0;
1072 cg.autoAnglesFast[1] = ( cg.time & 1023 ) * 360 / 1024.0f;
1073 cg.autoAnglesFast[2] = 0;
1074
1075 AnglesToAxis( cg.autoAngles, cg.autoAxis );
1076 AnglesToAxis( cg.autoAnglesFast, cg.autoAxisFast );
1077
1078 // generate and add the entity from the playerstate
1079 ps = &cg.predictedPlayerState;
1080 BG_PlayerStateToEntityState( ps, &cg.predictedPlayerEntity.currentState, qfalse );
1081 CG_AddCEntity( &cg.predictedPlayerEntity );
1082
1083 // lerp the non-predicted value for lightning gun origins
1084 CG_CalcEntityLerpPositions( &cg_entities[ cg.snap->ps.clientNum ] );
1085
1086 //unlagged - early transitioning
1087 if ( cg.nextSnap ) {
1088 // pre-add some of the entities sent over by the server
1089 // we have data for them and they don't need to interpolate
1090 for ( num = 0 ; num < cg.nextSnap->numEntities ; num++ ) {
1091 cent = &cg_entities[ cg.nextSnap->entities[ num ].number ];
1092 if ( cent->nextState.eType == ET_MISSILE || cent->nextState.eType == ET_GENERAL ) {
1093 // transition it immediately and add it
1094 CG_TransitionEntity( cent );
1095 cent->interpolate = qtrue;
1096 CG_AddCEntity( cent );
1097 }
1098 }
1099 }
1100 //unlagged - early transitioning
1101
1102 // add each entity sent over by the server
1103 for ( num = 0 ; num < cg.snap->numEntities ; num++ ) {
1104 cent = &cg_entities[ cg.snap->entities[ num ].number ];
1105 //unlagged - early transitioning
1106 if ( !cg.nextSnap || (cent->nextState.eType != ET_MISSILE && cent->nextState.eType != ET_GENERAL) ) {
1107 //unlagged - early transitioning
1108 CG_AddCEntity( cent );
1109 } //Also unlagged
1110 }
1111 }
1112