Debian Codegraph @

code/game/g_mover.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
27
28 /*
29 ===============================================================================
30
31 PUSHMOVE
32
33 ===============================================================================
34 */
35
36 typedef struct {
37 gentity_t *ent;
38 vec3_t origin;
39 vec3_t angles;
40 float deltayaw;
41 } pushed_t;
42 pushed_t pushed[MAX_GENTITIES], *pushed_p;
43
44
45 /*
46 ============
47 G_TestEntityPosition
48
49 ============
50 */
51 gentity_t *G_TestEntityPosition( gentity_t *ent ) {
52 trace_t tr;
53 int mask;
54
55 if ( ent->clipmask ) {
56 mask = ent->clipmask;
57 } else {
58 mask = MASK_SOLID;
59 }
60 if ( ent->client ) {
61 trap_Trace( &tr, ent->client->ps.origin, ent->r.mins, ent->r.maxs, ent->client->ps.origin, ent->s.number, mask );
62 } else {
63 trap_Trace( &tr, ent->s.pos.trBase, ent->r.mins, ent->r.maxs, ent->s.pos.trBase, ent->s.number, mask );
64 }
65
66 if (tr.startsolid)
67 return &g_entities[ tr.entityNum ];
68
69 return NULL;
70 }
71
72 /*
73 ================
74 G_CreateRotationMatrix
75 ================
76 */
77 void G_CreateRotationMatrix(vec3_t angles, vec3_t matrix[3]) {
78 AngleVectors(angles, matrix[0], matrix[1], matrix[2]);
79 VectorInverse(matrix[1]);
80 }
81
82 /*
83 ================
84 G_TransposeMatrix
85 ================
86 */
87 void G_TransposeMatrix(vec3_t matrix[3], vec3_t transpose[3]) {
88 int i, j;
89 for (i = 0; i < 3; i++) {
90 for (j = 0; j < 3; j++) {
91 transpose[i][j] = matrix[j][i];
92 }
93 }
94 }
95
96 /*
97 ================
98 G_RotatePoint
99 ================
100 */
101 void G_RotatePoint(vec3_t point, vec3_t matrix[3]) {
102 vec3_t tvec;
103
104 VectorCopy(point, tvec);
105 point[0] = DotProduct(matrix[0], tvec);
106 point[1] = DotProduct(matrix[1], tvec);
107 point[2] = DotProduct(matrix[2], tvec);
108 }
109
110 /*
111 ==================
112 G_TryPushingEntity
113
114 Returns qfalse if the move is blocked
115 ==================
116 */
117 qboolean G_TryPushingEntity( gentity_t *check, gentity_t *pusher, vec3_t move, vec3_t amove ) {
118 vec3_t matrix[3], transpose[3];
119 vec3_t org, org2, move2;
120 gentity_t *block;
121
122 // EF_MOVER_STOP will just stop when contacting another entity
123 // instead of pushing it, but entities can still ride on top of it
124 if ( ( pusher->s.eFlags & EF_MOVER_STOP ) &&
125 check->s.groundEntityNum != pusher->s.number ) {
126 return qfalse;
127 }
128
129 // save off the old position
130 if (pushed_p > &pushed[MAX_GENTITIES]) {
131 G_Error( "pushed_p > &pushed[MAX_GENTITIES]" );
132 }
133 pushed_p->ent = check;
134 VectorCopy (check->s.pos.trBase, pushed_p->origin);
135 VectorCopy (check->s.apos.trBase, pushed_p->angles);
136 if ( check->client ) {
137 pushed_p->deltayaw = check->client->ps.delta_angles[YAW];
138 VectorCopy (check->client->ps.origin, pushed_p->origin);
139 }
140 pushed_p++;
141
142 // try moving the contacted entity
143 // figure movement due to the pusher's amove
144 G_CreateRotationMatrix( amove, transpose );
145 G_TransposeMatrix( transpose, matrix );
146 if ( check->client ) {
147 VectorSubtract (check->client->ps.origin, pusher->r.currentOrigin, org);
148 }
149 else {
150 VectorSubtract (check->s.pos.trBase, pusher->r.currentOrigin, org);
151 }
152 VectorCopy( org, org2 );
153 G_RotatePoint( org2, matrix );
154 VectorSubtract (org2, org, move2);
155 // add movement
156 VectorAdd (check->s.pos.trBase, move, check->s.pos.trBase);
157 VectorAdd (check->s.pos.trBase, move2, check->s.pos.trBase);
158 if ( check->client ) {
159 VectorAdd (check->client->ps.origin, move, check->client->ps.origin);
160 VectorAdd (check->client->ps.origin, move2, check->client->ps.origin);
161 // make sure the client's view rotates when on a rotating mover
162 check->client->ps.delta_angles[YAW] += ANGLE2SHORT(amove[YAW]);
163 }
164
165 // may have pushed them off an edge
166 if ( check->s.groundEntityNum != pusher->s.number ) {
167 check->s.groundEntityNum = ENTITYNUM_NONE;
168 }
169
170 block = G_TestEntityPosition( check );
171 if (!block) {
172 // pushed ok
173 if ( check->client ) {
174 VectorCopy( check->client->ps.origin, check->r.currentOrigin );
175 } else {
176 VectorCopy( check->s.pos.trBase, check->r.currentOrigin );
177 }
178 trap_LinkEntity (check);
179 return qtrue;
180 }
181
182 // if it is ok to leave in the old position, do it
183 // this is only relevant for riding entities, not pushed
184 // Sliding trapdoors can cause this.
185 VectorCopy( (pushed_p-1)->origin, check->s.pos.trBase);
186 if ( check->client ) {
187 VectorCopy( (pushed_p-1)->origin, check->client->ps.origin);
188 }
189 VectorCopy( (pushed_p-1)->angles, check->s.apos.trBase );
190 block = G_TestEntityPosition (check);
191 if ( !block ) {
192 check->s.groundEntityNum = ENTITYNUM_NONE;
193 pushed_p--;
194 return qtrue;
195 }
196
197 // blocked
198 return qfalse;
199 }
200
201 /*
202 ==================
203 G_CheckProxMinePosition
204 ==================
205 */
206 qboolean G_CheckProxMinePosition( gentity_t *check ) {
207 vec3_t start, end;
208 trace_t tr;
209
210 VectorMA(check->s.pos.trBase, 0.125, check->movedir, start);
211 VectorMA(check->s.pos.trBase, 2, check->movedir, end);
212 trap_Trace( &tr, start, NULL, NULL, end, check->s.number, MASK_SOLID );
213
214 if (tr.startsolid || tr.fraction < 1)
215 return qfalse;
216
217 return qtrue;
218 }
219
220 /*
221 ==================
222 G_TryPushingProxMine
223 ==================
224 */
225 qboolean G_TryPushingProxMine( gentity_t *check, gentity_t *pusher, vec3_t move, vec3_t amove ) {
226 vec3_t forward, right, up;
227 vec3_t org, org2, move2;
228 int ret;
229
230 // we need this for pushing things later
231 VectorSubtract (vec3_origin, amove, org);
232 AngleVectors (org, forward, right, up);
233
234 // try moving the contacted entity
235 VectorAdd (check->s.pos.trBase, move, check->s.pos.trBase);
236
237 // figure movement due to the pusher's amove
238 VectorSubtract (check->s.pos.trBase, pusher->r.currentOrigin, org);
239 org2[0] = DotProduct (org, forward);
240 org2[1] = -DotProduct (org, right);
241 org2[2] = DotProduct (org, up);
242 VectorSubtract (org2, org, move2);
243 VectorAdd (check->s.pos.trBase, move2, check->s.pos.trBase);
244
245 ret = G_CheckProxMinePosition( check );
246 if (ret) {
247 VectorCopy( check->s.pos.trBase, check->r.currentOrigin );
248 trap_LinkEntity (check);
249 }
250 return ret;
251 }
252
253 void G_ExplodeMissile( gentity_t *ent );
254
255 /*
256 ============
257 G_MoverPush
258
259 Objects need to be moved back on a failed push,
260 otherwise riders would continue to slide.
261 If qfalse is returned, *obstacle will be the blocking entity
262 ============
263 */
264 qboolean G_MoverPush( gentity_t *pusher, vec3_t move, vec3_t amove, gentity_t **obstacle ) {
265 int i, e;
266 gentity_t *check;
267 vec3_t mins, maxs;
268 pushed_t *p;
269 int entityList[MAX_GENTITIES];
270 int listedEntities;
271 vec3_t totalMins, totalMaxs;
272
273 *obstacle = NULL;
274
275
276 // mins/maxs are the bounds at the destination
277 // totalMins / totalMaxs are the bounds for the entire move
278 if ( pusher->r.currentAngles[0] || pusher->r.currentAngles[1] || pusher->r.currentAngles[2]
279 || amove[0] || amove[1] || amove[2] ) {
280 float radius;
281
282 radius = RadiusFromBounds( pusher->r.mins, pusher->r.maxs );
283 for ( i = 0 ; i < 3 ; i++ ) {
284 mins[i] = pusher->r.currentOrigin[i] + move[i] - radius;
285 maxs[i] = pusher->r.currentOrigin[i] + move[i] + radius;
286 totalMins[i] = mins[i] - move[i];
287 totalMaxs[i] = maxs[i] - move[i];
288 }
289 } else {
290 for (i=0 ; i<3 ; i++) {
291 mins[i] = pusher->r.absmin[i] + move[i];
292 maxs[i] = pusher->r.absmax[i] + move[i];
293 }
294
295 VectorCopy( pusher->r.absmin, totalMins );
296 VectorCopy( pusher->r.absmax, totalMaxs );
297 for (i=0 ; i<3 ; i++) {
298 if ( move[i] > 0 ) {
299 totalMaxs[i] += move[i];
300 } else {
301 totalMins[i] += move[i];
302 }
303 }
304 }
305
306 // unlink the pusher so we don't get it in the entityList
307 trap_UnlinkEntity( pusher );
308
309 listedEntities = trap_EntitiesInBox( totalMins, totalMaxs, entityList, MAX_GENTITIES );
310
311 // move the pusher to its final position
312 VectorAdd( pusher->r.currentOrigin, move, pusher->r.currentOrigin );
313 VectorAdd( pusher->r.currentAngles, amove, pusher->r.currentAngles );
314 trap_LinkEntity( pusher );
315
316 // see if any solid entities are inside the final position
317 for ( e = 0 ; e < listedEntities ; e++ ) {
318 check = &g_entities[ entityList[ e ] ];
319
320 #ifdef MISSIONPACK
321 if ( check->s.eType == ET_MISSILE ) {
322 // if it is a prox mine
323 if ( !strcmp(check->classname, "prox mine") ) {
324 // if this prox mine is attached to this mover try to move it with the pusher
325 if ( check->enemy == pusher ) {
326 if (!G_TryPushingProxMine( check, pusher, move, amove )) {
327 //explode
328 check->s.loopSound = 0;
329 G_AddEvent( check, EV_PROXIMITY_MINE_TRIGGER, 0 );
330 G_ExplodeMissile(check);
331 if (check->activator) {
332 G_FreeEntity(check->activator);
333 check->activator = NULL;
334 }
335 //G_Printf("prox mine explodes\n");
336 }
337 }
338 else {
339 //check if the prox mine is crushed by the mover
340 if (!G_CheckProxMinePosition( check )) {
341 //explode
342 check->s.loopSound = 0;
343 G_AddEvent( check, EV_PROXIMITY_MINE_TRIGGER, 0 );
344 G_ExplodeMissile(check);
345 if (check->activator) {
346 G_FreeEntity(check->activator);
347 check->activator = NULL;
348 }
349 //G_Printf("prox mine explodes\n");
350 }
351 }
352 continue;
353 }
354 }
355 #endif
356 // only push items and players
357 if ( check->s.eType != ET_ITEM && check->s.eType != ET_PLAYER && !check->physicsObject ) {
358 continue;
359 }
360
361 // if the entity is standing on the pusher, it will definitely be moved
362 if ( check->s.groundEntityNum != pusher->s.number ) {
363 // see if the ent needs to be tested
364 if ( check->r.absmin[0] >= maxs[0]
365 || check->r.absmin[1] >= maxs[1]
366 || check->r.absmin[2] >= maxs[2]
367 || check->r.absmax[0] <= mins[0]
368 || check->r.absmax[1] <= mins[1]
369 || check->r.absmax[2] <= mins[2] ) {
370 continue;
371 }
372 // see if the ent's bbox is inside the pusher's final position
373 // this does allow a fast moving object to pass through a thin entity...
374 if (!G_TestEntityPosition (check)) {
375 continue;
376 }
377 }
378
379 // the entity needs to be pushed
380 if ( G_TryPushingEntity( check, pusher, move, amove ) ) {
381 continue;
382 }
383
384 // the move was blocked an entity
385
386 // bobbing entities are instant-kill and never get blocked
387 if ( pusher->s.pos.trType == TR_SINE || pusher->s.apos.trType == TR_SINE ) {
388 G_Damage( check, pusher, pusher, NULL, NULL, 99999, 0, MOD_CRUSH );
389 continue;
390 }
391
392
393 // save off the obstacle so we can call the block function (crush, etc)
394 *obstacle = check;
395
396 // move back any entities we already moved
397 // go backwards, so if the same entity was pushed
398 // twice, it goes back to the original position
399 for ( p=pushed_p-1 ; p>=pushed ; p-- ) {
400 VectorCopy (p->origin, p->ent->s.pos.trBase);
401 VectorCopy (p->angles, p->ent->s.apos.trBase);
402 if ( p->ent->client ) {
403 p->ent->client->ps.delta_angles[YAW] = p->deltayaw;
404 VectorCopy (p->origin, p->ent->client->ps.origin);
405 }
406 trap_LinkEntity (p->ent);
407 }
408 return qfalse;
409 }
410
411 return qtrue;
412 }
413
414
415 /*
416 =================
417 G_MoverTeam
418 =================
419 */
420 void G_MoverTeam( gentity_t *ent ) {
421 vec3_t move, amove;
422 gentity_t *part, *obstacle;
423 vec3_t origin, angles;
424
425 obstacle = NULL;
426
427 // make sure all team members can move before committing
428 // any moves or calling any think functions
429 // if the move is blocked, all moved objects will be backed out
430 pushed_p = pushed;
431 for (part = ent ; part ; part=part->teamchain) {
432 // get current position
433 BG_EvaluateTrajectory( &part->s.pos, level.time, origin );
434 BG_EvaluateTrajectory( &part->s.apos, level.time, angles );
435 VectorSubtract( origin, part->r.currentOrigin, move );
436 VectorSubtract( angles, part->r.currentAngles, amove );
437 if ( !G_MoverPush( part, move, amove, &obstacle ) ) {
438 break; // move was blocked
439 }
440 }
441
442 if (part) {
443 // go back to the previous position
444 for ( part = ent ; part ; part = part->teamchain ) {
445 part->s.pos.trTime += level.time - level.previousTime;
446 part->s.apos.trTime += level.time - level.previousTime;
447 BG_EvaluateTrajectory( &part->s.pos, level.time, part->r.currentOrigin );
448 BG_EvaluateTrajectory( &part->s.apos, level.time, part->r.currentAngles );
449 trap_LinkEntity( part );
450 }
451
452 // if the pusher has a "blocked" function, call it
453 if (ent->blocked) {
454 ent->blocked( ent, obstacle );
455 }
456 return;
457 }
458
459 // the move succeeded
460 for ( part = ent ; part ; part = part->teamchain ) {
461 // call the reached function if time is at or past end point
462 if ( part->s.pos.trType == TR_LINEAR_STOP ) {
463 if ( level.time >= part->s.pos.trTime + part->s.pos.trDuration ) {
464 if ( part->reached ) {
465 part->reached( part );
466 }
467 }
468 }
469 }
470 }
471
472 /*
473 ================
474 G_RunMover
475
476 ================
477 */
478 void G_RunMover( gentity_t *ent ) {
479 // if not a team captain, don't do anything, because
480 // the captain will handle everything
481 if ( ent->flags & FL_TEAMMEMBER ) {
482 return;
483 }
484
485 // if stationary at one of the positions, don't move anything
486 if ( ent->s.pos.trType != TR_STATIONARY || ent->s.apos.trType != TR_STATIONARY ) {
487 G_MoverTeam( ent );
488 }
489
490 // check think function
491 G_RunThink( ent );
492 }
493
494 /*
495 ============================================================================
496
497 GENERAL MOVERS
498
499 Doors, plats, and buttons are all binary (two position) movers
500 Pos1 is "at rest", pos2 is "activated"
501 ============================================================================
502 */
503
504 /*
505 ===============
506 SetMoverState
507 ===============
508 */
509 void SetMoverState( gentity_t *ent, moverState_t moverState, int time ) {
510 vec3_t delta;
511 float f;
512
513 ent->moverState = moverState;
514
515 ent->s.pos.trTime = time;
516 switch( moverState ) {
517 case MOVER_POS1:
518 VectorCopy( ent->pos1, ent->s.pos.trBase );
519 ent->s.pos.trType = TR_STATIONARY;
520 break;
521 case MOVER_POS2:
522 VectorCopy( ent->pos2, ent->s.pos.trBase );
523 ent->s.pos.trType = TR_STATIONARY;
524 break;
525 case MOVER_1TO2:
526 VectorCopy( ent->pos1, ent->s.pos.trBase );
527 VectorSubtract( ent->pos2, ent->pos1, delta );
528 f = 1000.0 / ent->s.pos.trDuration;
529 VectorScale( delta, f, ent->s.pos.trDelta );
530 ent->s.pos.trType = TR_LINEAR_STOP;
531 break;
532 case MOVER_2TO1:
533 VectorCopy( ent->pos2, ent->s.pos.trBase );
534 VectorSubtract( ent->pos1, ent->pos2, delta );
535 f = 1000.0 / ent->s.pos.trDuration;
536 VectorScale( delta, f, ent->s.pos.trDelta );
537 ent->s.pos.trType = TR_LINEAR_STOP;
538 break;
539 }
540 BG_EvaluateTrajectory( &ent->s.pos, level.time, ent->r.currentOrigin );
541 trap_LinkEntity( ent );
542 }
543
544 /*
545 ================
546 MatchTeam
547
548 All entities in a mover team will move from pos1 to pos2
549 in the same amount of time
550 ================
551 */
552 void MatchTeam( gentity_t *teamLeader, int moverState, int time ) {
553 gentity_t *member;
554
555 for ( member = teamLeader ; member ; member = member->teamchain ) {
556 SetMoverState( member, moverState, time );
557 }
558 }
559
560
561
562 /*
563 ================
564 ReturnToPos1
565 ================
566 */
567 void ReturnToPos1( gentity_t *ent ) {
568 MatchTeam( ent, MOVER_2TO1, level.time );
569
570 // looping sound
571 ent->s.loopSound = ent->soundLoop;
572
573 // starting sound
574 if ( ent->sound2to1 ) {
575 G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound2to1 );
576 }
577 }
578
579
580 /*
581 ================
582 Reached_BinaryMover
583 ================
584 */
585 void Reached_BinaryMover( gentity_t *ent ) {
586
587 // stop the looping sound
588 ent->s.loopSound = ent->soundLoop;
589
590 if ( ent->moverState == MOVER_1TO2 ) {
591 // reached pos2
592 SetMoverState( ent, MOVER_POS2, level.time );
593
594 // play sound
595 if ( ent->soundPos2 ) {
596 G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundPos2 );
597 }
598
599 // return to pos1 after a delay
600 ent->think = ReturnToPos1;
601 ent->nextthink = level.time + ent->wait;
602
603 // fire targets
604 if ( !ent->activator ) {
605 ent->activator = ent;
606 }
607 G_UseTargets( ent, ent->activator );
608 } else if ( ent->moverState == MOVER_2TO1 ) {
609 // reached pos1
610 SetMoverState( ent, MOVER_POS1, level.time );
611
612 // play sound
613 if ( ent->soundPos1 ) {
614 G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundPos1 );
615 }
616
617 // close areaportals
618 if ( ent->teammaster == ent || !ent->teammaster ) {
619 trap_AdjustAreaPortalState( ent, qfalse );
620 }
621 } else {
622 G_Error( "Reached_BinaryMover: bad moverState" );
623 }
624 }
625
626
627 /*
628 ================
629 Use_BinaryMover
630 ================
631 */
632 void Use_BinaryMover( gentity_t *ent, gentity_t *other, gentity_t *activator ) {
633 int total;
634 int partial;
635
636 // only the master should be used
637 if ( ent->flags & FL_TEAMMEMBER ) {
638 Use_BinaryMover( ent->teammaster, other, activator );
639 return;
640 }
641
642 ent->activator = activator;
643
644 if ( ent->moverState == MOVER_POS1 ) {
645 // start moving 50 msec later, becase if this was player
646 // triggered, level.time hasn't been advanced yet
647 MatchTeam( ent, MOVER_1TO2, level.time + 50 );
648
649 // starting sound
650 if ( ent->sound1to2 ) {
651 G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound1to2 );
652 }
653
654 // looping sound
655 ent->s.loopSound = ent->soundLoop;
656
657 // open areaportal
658 if ( ent->teammaster == ent || !ent->teammaster ) {
659 trap_AdjustAreaPortalState( ent, qtrue );
660 }
661 return;
662 }
663
664 // if all the way up, just delay before coming down
665 if ( ent->moverState == MOVER_POS2 ) {
666 ent->nextthink = level.time + ent->wait;
667 return;
668 }
669
670 // only partway down before reversing
671 if ( ent->moverState == MOVER_2TO1 ) {
672 total = ent->s.pos.trDuration;
673 partial = level.time - ent->s.pos.trTime;
674 if ( partial > total ) {
675 partial = total;
676 }
677
678 MatchTeam( ent, MOVER_1TO2, level.time - ( total - partial ) );
679
680 if ( ent->sound1to2 ) {
681 G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound1to2 );
682 }
683 return;
684 }
685
686 // only partway up before reversing
687 if ( ent->moverState == MOVER_1TO2 ) {
688 total = ent->s.pos.trDuration;
689 partial = level.time - ent->s.pos.trTime;
690 if ( partial > total ) {
691 partial = total;
692 }
693
694 MatchTeam( ent, MOVER_2TO1, level.time - ( total - partial ) );
695
696 if ( ent->sound2to1 ) {
697 G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound2to1 );
698 }
699 return;
700 }
701 }
702
703
704
705 /*
706 ================
707 InitMover
708
709 "pos1", "pos2", and "speed" should be set before calling,
710 so the movement delta can be calculated
711 ================
712 */
713 void InitMover( gentity_t *ent ) {
714 vec3_t move;
715 float distance;
716 float light;
717 vec3_t color;
718 qboolean lightSet, colorSet;
719 char *sound;
720
721 // if the "model2" key is set, use a separate model
722 // for drawing, but clip against the brushes
723 if ( ent->model2 ) {
724 ent->s.modelindex2 = G_ModelIndex( ent->model2 );
725 }
726
727 // if the "loopsound" key is set, use a constant looping sound when moving
728 if ( G_SpawnString( "noise", "100", &sound ) ) {
729 ent->s.loopSound = G_SoundIndex( sound );
730 }
731
732 // if the "color" or "light" keys are set, setup constantLight
733 lightSet = G_SpawnFloat( "light", "100", &light );
734 colorSet = G_SpawnVector( "color", "1 1 1", color );
735 if ( lightSet || colorSet ) {
736 int r, g, b, i;
737
738 r = color[0] * 255;
739 if ( r > 255 ) {
740 r = 255;
741 }
742 g = color[1] * 255;
743 if ( g > 255 ) {
744 g = 255;
745 }
746 b = color[2] * 255;
747 if ( b > 255 ) {
748 b = 255;
749 }
750 i = light / 4;
751 if ( i > 255 ) {
752 i = 255;
753 }
754 ent->s.constantLight = r | ( g << 8 ) | ( b << 16 ) | ( i << 24 );
755 }
756
757
758 ent->use = Use_BinaryMover;
759 ent->reached = Reached_BinaryMover;
760
761 ent->moverState = MOVER_POS1;
762 ent->r.svFlags = SVF_USE_CURRENT_ORIGIN;
763 ent->s.eType = ET_MOVER;
764 VectorCopy (ent->pos1, ent->r.currentOrigin);
765 trap_LinkEntity (ent);
766
767 ent->s.pos.trType = TR_STATIONARY;
768 VectorCopy( ent->pos1, ent->s.pos.trBase );
769
770 // calculate time to reach second position from speed
771 VectorSubtract( ent->pos2, ent->pos1, move );
772 distance = VectorLength( move );
773 if ( ! ent->speed ) {
774 ent->speed = 100;
775 }
776 VectorScale( move, ent->speed, ent->s.pos.trDelta );
777 ent->s.pos.trDuration = distance * 1000 / ent->speed;
778 if ( ent->s.pos.trDuration <= 0 ) {
779 ent->s.pos.trDuration = 1;
780 }
781 }
782
783
784 /*
785 ===============================================================================
786
787 DOOR
788
789 A use can be triggered either by a touch function, by being shot, or by being
790 targeted by another entity.
791
792 ===============================================================================
793 */
794
795 /*
796 ================
797 Blocked_Door
798 ================
799 */
800 void Blocked_Door( gentity_t *ent, gentity_t *other ) {
801 // remove anything other than a client
802 if ( !other->client ) {
803 // except CTF flags!!!!
804 if( other->s.eType == ET_ITEM && other->item->giType == IT_TEAM ) {
805 Team_DroppedFlagThink( other );
806 return;
807 }
808 G_TempEntity( other->s.origin, EV_ITEM_POP );
809 G_FreeEntity( other );
810 return;
811 }
812
813 if ( ent->damage ) {
814 G_Damage( other, ent, ent, NULL, NULL, ent->damage, 0, MOD_CRUSH );
815 }
816 if ( ent->spawnflags & 4 ) {
817 return; // crushers don't reverse
818 }
819
820 // reverse direction
821 Use_BinaryMover( ent, ent, other );
822 }
823
824 /*
825 ================
826 Touch_DoorTriggerSpectator
827 ================
828 */
829 static void Touch_DoorTriggerSpectator( gentity_t *ent, gentity_t *other, trace_t *trace ) {
830 int axis;
831 float doorMin, doorMax;
832 vec3_t origin;
833
834 axis = ent->count;
835 // the constants below relate to constants in Think_SpawnNewDoorTrigger()
836 doorMin = ent->r.absmin[axis] + 100;
837 doorMax = ent->r.absmax[axis] - 100;
838
839 VectorCopy(other->client->ps.origin, origin);
840
841 if (origin[axis] < doorMin || origin[axis] > doorMax) return;
842
843 if (fabs(origin[axis] - doorMax) < fabs(origin[axis] - doorMin)) {
844 origin[axis] = doorMin - 10;
845 } else {
846 origin[axis] = doorMax + 10;
847 }
848
849 TeleportPlayer(other, origin, tv(10000000.0, 0, 0));
850 }
851
852 /*
853 ================
854 Touch_DoorTrigger
855 ================
856 */
857 void Touch_DoorTrigger( gentity_t *ent, gentity_t *other, trace_t *trace ) {
858 if ( other->client && other->client->sess.sessionTeam == TEAM_SPECTATOR ) {
859 // if the door is not open and not opening
860 if ( ent->parent->moverState != MOVER_1TO2 &&
861 ent->parent->moverState != MOVER_POS2) {
862 Touch_DoorTriggerSpectator( ent, other, trace );
863 }
864 }
865 else if ( ent->parent->moverState != MOVER_1TO2 ) {
866 Use_BinaryMover( ent->parent, ent, other );
867 }
868 }
869
870
871 /*
872 ======================
873 Think_SpawnNewDoorTrigger
874
875 All of the parts of a door have been spawned, so create
876 a trigger that encloses all of them
877 ======================
878 */
879 void Think_SpawnNewDoorTrigger( gentity_t *ent ) {
880 gentity_t *other;
881 vec3_t mins, maxs;
882 int i, best;
883
884 if (!ent) {
885 return;
886 }
887
888 // set all of the members as shootable
889 for ( other = ent ; other ; other = other->teamchain ) {
890 other->takedamage = qtrue;
891 }
892
893 // find the bounds of everything on the team
894 VectorCopy (ent->r.absmin, mins);
895 VectorCopy (ent->r.absmax, maxs);
896
897 for (other = ent->teamchain ; other ; other=other->teamchain) {
898 AddPointToBounds (other->r.absmin, mins, maxs);
899 AddPointToBounds (other->r.absmax, mins, maxs);
900 }
901
902 // find the thinnest axis, which will be the one we expand
903 best = 0;
904 for ( i = 1 ; i < 3 ; i++ ) {
905 if ( maxs[i] - mins[i] < maxs[best] - mins[best] ) {
906 best = i;
907 }
908 }
909 maxs[best] += 120;
910 mins[best] -= 120;
911
912 // create a trigger with this size
913 other = G_Spawn ();
914 other->classname = "door_trigger";
915 VectorCopy (mins, other->r.mins);
916 VectorCopy (maxs, other->r.maxs);
917 other->parent = ent;
918 other->r.contents = CONTENTS_TRIGGER;
919 other->touch = Touch_DoorTrigger;
920 // remember the thinnest axis
921 other->count = best;
922 trap_LinkEntity (other);
923
924 MatchTeam( ent, ent->moverState, level.time );
925 }
926
927 void Think_MatchTeam( gentity_t *ent ) {
928 MatchTeam( ent, ent->moverState, level.time );
929 }
930
931
932 /*QUAKED func_door (0 .5 .8) ? START_OPEN x CRUSHER
933 TOGGLE wait in both the start and end states for a trigger event.
934 START_OPEN the door to moves to its destination when spawned, and operate in reverse. It is used to temporarily or permanently close off an area when triggered (not useful for touch or takedamage doors).
935 NOMONSTER monsters will not trigger this door
936
937 "model2" .md3 model to also draw
938 "angle" determines the opening direction
939 "targetname" if set, no touch field will be spawned and a remote button or trigger field activates the door.
940 "speed" movement speed (100 default)
941 "wait" wait before returning (3 default, -1 = never return)
942 "lip" lip remaining at end of move (8 default)
943 "dmg" damage to inflict when blocked (2 default)
944 "color" constantLight color
945 "light" constantLight radius
946 "health" if set, the door must be shot open
947 */
948 void SP_func_door (gentity_t *ent) {
949 vec3_t abs_movedir;
950 float distance;
951 vec3_t size;
952 float lip;
953
954 ent->sound1to2 = ent->sound2to1 = G_SoundIndex("sound/movers/doors/dr1_strt.wav");
955 ent->soundPos1 = ent->soundPos2 = G_SoundIndex("sound/movers/doors/dr1_end.wav");
956
957 ent->blocked = Blocked_Door;
958
959 // default speed of 400
960 if (!ent->speed)
961 ent->speed = 400;
962
963 // default wait of 2 seconds
964 if (!ent->wait)
965 ent->wait = 2;
966 ent->wait *= 1000;
967
968 // default lip of 8 units
969 G_SpawnFloat( "lip", "8", &lip );
970
971 // default damage of 2 points
972 G_SpawnInt( "dmg", "2", &ent->damage );
973
974 // first position at start
975 VectorCopy( ent->s.origin, ent->pos1 );
976
977 // calculate second position
978 trap_SetBrushModel( ent, ent->model );
979 G_SetMovedir (ent->s.angles, ent->movedir);
980 abs_movedir[0] = fabs(ent->movedir[0]);
981 abs_movedir[1] = fabs(ent->movedir[1]);
982 abs_movedir[2] = fabs(ent->movedir[2]);
983 VectorSubtract( ent->r.maxs, ent->r.mins, size );
984 distance = DotProduct( abs_movedir, size ) - lip;
985 VectorMA( ent->pos1, distance, ent->movedir, ent->pos2 );
986
987 // if "start_open", reverse position 1 and 2
988 if ( ent->spawnflags & 1 ) {
989 vec3_t temp;
990
991 VectorCopy( ent->pos2, temp );
992 VectorCopy( ent->s.origin, ent->pos2 );
993 VectorCopy( temp, ent->pos1 );
994 }
995
996 InitMover( ent );
997
998 ent->nextthink = level.time + FRAMETIME;
999
1000 if ( ! (ent->flags & FL_TEAMMEMBER ) ) {
1001 int health;
1002
1003 G_SpawnInt( "health", "0", &health );
1004 if ( health ) {
1005 ent->takedamage = qtrue;
1006 }
1007 if ( ent->targetname || health ) {
1008 // non touch/shoot doors
1009 ent->think = Think_MatchTeam;
1010 } else {
1011 ent->think = Think_SpawnNewDoorTrigger;
1012 }
1013 }
1014
1015
1016 }
1017
1018 /*
1019 ===============================================================================
1020
1021 PLAT
1022
1023 ===============================================================================
1024 */
1025
1026 /*
1027 ==============
1028 Touch_Plat
1029
1030 Don't allow decent if a living player is on it
1031 ===============
1032 */
1033 void Touch_Plat( gentity_t *ent, gentity_t *other, trace_t *trace ) {
1034 if ( !other->client || other->client->ps.stats[STAT_HEALTH] <= 0 ) {
1035 return;
1036 }
1037
1038 // delay return-to-pos1 by one second
1039 if ( ent->moverState == MOVER_POS2 ) {
1040 ent->nextthink = level.time + 1000;
1041 }
1042 }
1043
1044 /*
1045 ==============
1046 Touch_PlatCenterTrigger
1047
1048 If the plat is at the bottom position, start it going up
1049 ===============
1050 */
1051 void Touch_PlatCenterTrigger(gentity_t *ent, gentity_t *other, trace_t *trace ) {
1052 if ( !other->client ) {
1053 return;
1054 }
1055
1056 if ( ent->parent->moverState == MOVER_POS1 ) {
1057 Use_BinaryMover( ent->parent, ent, other );
1058 }
1059 }
1060
1061
1062 /*
1063 ================
1064 SpawnPlatTrigger
1065
1066 Spawn a trigger in the middle of the plat's low position
1067 Elevator cars require that the trigger extend through the entire low position,
1068 not just sit on top of it.
1069 ================
1070 */
1071 void SpawnPlatTrigger( gentity_t *ent ) {
1072 gentity_t *trigger;
1073 vec3_t tmin, tmax;
1074
1075 // the middle trigger will be a thin trigger just
1076 // above the starting position
1077 trigger = G_Spawn();
1078 trigger->classname = "plat_trigger";
1079 trigger->touch = Touch_PlatCenterTrigger;
1080 trigger->r.contents = CONTENTS_TRIGGER;
1081 trigger->parent = ent;
1082
1083 tmin[0] = ent->pos1[0] + ent->r.mins[0] + 33;
1084 tmin[1] = ent->pos1[1] + ent->r.mins[1] + 33;
1085 tmin[2] = ent->pos1[2] + ent->r.mins[2];
1086
1087 tmax[0] = ent->pos1[0] + ent->r.maxs[0] - 33;
1088 tmax[1] = ent->pos1[1] + ent->r.maxs[1] - 33;
1089 tmax[2] = ent->pos1[2] + ent->r.maxs[2] + 8;
1090
1091 if ( tmax[0] <= tmin[0] ) {
1092 tmin[0] = ent->pos1[0] + (ent->r.mins[0] + ent->r.maxs[0]) *0.5;
1093 tmax[0] = tmin[0] + 1;
1094 }
1095 if ( tmax[1] <= tmin[1] ) {
1096 tmin[1] = ent->pos1[1] + (ent->r.mins[1] + ent->r.maxs[1]) *0.5;
1097 tmax[1] = tmin[1] + 1;
1098 }
1099
1100 VectorCopy (tmin, trigger->r.mins);
1101 VectorCopy (tmax, trigger->r.maxs);
1102
1103 trap_LinkEntity (trigger);
1104 }
1105
1106
1107 /*QUAKED func_plat (0 .5 .8) ?
1108 Plats are always drawn in the extended position so they will light correctly.
1109
1110 "lip" default 8, protrusion above rest position
1111 "height" total height of movement, defaults to model height
1112 "speed" overrides default 200.
1113 "dmg" overrides default 2
1114 "model2" .md3 model to also draw
1115 "color" constantLight color
1116 "light" constantLight radius
1117 */
1118 void SP_func_plat (gentity_t *ent) {
1119 float lip, height;
1120
1121 ent->sound1to2 = ent->sound2to1 = G_SoundIndex("sound/movers/plats/pt1_strt.wav");
1122 ent->soundPos1 = ent->soundPos2 = G_SoundIndex("sound/movers/plats/pt1_end.wav");
1123
1124 VectorClear (ent->s.angles);
1125
1126 G_SpawnFloat( "speed", "200", &ent->speed );
1127 G_SpawnInt( "dmg", "2", &ent->damage );
1128 G_SpawnFloat( "wait", "1", &ent->wait );
1129 G_SpawnFloat( "lip", "8", &lip );
1130
1131 ent->wait = 1000;
1132
1133 // create second position
1134 trap_SetBrushModel( ent, ent->model );
1135
1136 if ( !G_SpawnFloat( "height", "0", &height ) ) {
1137 height = (ent->r.maxs[2] - ent->r.mins[2]) - lip;
1138 }
1139
1140 // pos1 is the rest (bottom) position, pos2 is the top
1141 VectorCopy( ent->s.origin, ent->pos2 );
1142 VectorCopy( ent->pos2, ent->pos1 );
1143 ent->pos1[2] -= height;
1144
1145 InitMover( ent );
1146
1147 // touch function keeps the plat from returning while
1148 // a live player is standing on it
1149 ent->touch = Touch_Plat;
1150
1151 ent->blocked = Blocked_Door;
1152
1153 ent->parent = ent; // so it can be treated as a door
1154
1155 // spawn the trigger if one hasn't been custom made
1156 if ( !ent->targetname ) {
1157 SpawnPlatTrigger(ent);
1158 }
1159 }
1160
1161
1162 /*
1163 ===============================================================================
1164
1165 BUTTON
1166
1167 ===============================================================================
1168 */
1169
1170 /*
1171 ==============
1172 Touch_Button
1173
1174 ===============
1175 */
1176 void Touch_Button(gentity_t *ent, gentity_t *other, trace_t *trace ) {
1177 if ( !other->client ) {
1178 return;
1179 }
1180
1181 if ( ent->moverState == MOVER_POS1 ) {
1182 Use_BinaryMover( ent, other, other );
1183 }
1184 }
1185
1186
1187 /*QUAKED func_button (0 .5 .8) ?
1188 When a button is touched, it moves some distance in the direction of its angle, triggers all of its targets, waits some time, then returns to its original position where it can be triggered again.
1189
1190 "model2" .md3 model to also draw
1191 "angle" determines the opening direction
1192 "target" all entities with a matching targetname will be used
1193 "speed" override the default 40 speed
1194 "wait" override the default 1 second wait (-1 = never return)
1195 "lip" override the default 4 pixel lip remaining at end of move
1196 "health" if set, the button must be killed instead of touched
1197 "color" constantLight color
1198 "light" constantLight radius
1199 */
1200 void SP_func_button( gentity_t *ent ) {
1201 vec3_t abs_movedir;
1202 float distance;
1203 vec3_t size;
1204 float lip;
1205
1206 ent->sound1to2 = G_SoundIndex("sound/movers/switches/butn2.wav");
1207
1208 if ( !ent->speed ) {
1209 ent->speed = 40;
1210 }
1211
1212 if ( !ent->wait ) {
1213 ent->wait = 1;
1214 }
1215 ent->wait *= 1000;
1216
1217 // first position
1218 VectorCopy( ent->s.origin, ent->pos1 );
1219
1220 // calculate second position
1221 trap_SetBrushModel( ent, ent->model );
1222
1223 G_SpawnFloat( "lip", "4", &lip );
1224
1225 G_SetMovedir( ent->s.angles, ent->movedir );
1226 abs_movedir[0] = fabs(ent->movedir[0]);
1227 abs_movedir[1] = fabs(ent->movedir[1]);
1228 abs_movedir[2] = fabs(ent->movedir[2]);
1229 VectorSubtract( ent->r.maxs, ent->r.mins, size );
1230 distance = abs_movedir[0] * size[0] + abs_movedir[1] * size[1] + abs_movedir[2] * size[2] - lip;
1231 VectorMA (ent->pos1, distance, ent->movedir, ent->pos2);
1232
1233 if (ent->health) {
1234 // shootable button
1235 ent->takedamage = qtrue;
1236 } else {
1237 // touchable button
1238 ent->touch = Touch_Button;
1239 }
1240
1241 InitMover( ent );
1242 }
1243
1244
1245
1246 /*
1247 ===============================================================================
1248
1249 TRAIN
1250
1251 ===============================================================================
1252 */
1253
1254
1255 #define TRAIN_START_ON 1
1256 #define TRAIN_TOGGLE 2
1257 #define TRAIN_BLOCK_STOPS 4
1258
1259 /*
1260 ===============
1261 Think_BeginMoving
1262
1263 The wait time at a corner has completed, so start moving again
1264 ===============
1265 */
1266 void Think_BeginMoving( gentity_t *ent ) {
1267 ent->s.pos.trTime = level.time;
1268 ent->s.pos.trType = TR_LINEAR_STOP;
1269 }
1270
1271 /*
1272 ===============
1273 Reached_Train
1274 ===============
1275 */
1276 void Reached_Train( gentity_t *ent ) {
1277 gentity_t *next;
1278 float speed;
1279 vec3_t move;
1280 float length;
1281
1282 // copy the appropriate values
1283 next = ent->nextTrain;
1284 if ( !next || !next->nextTrain ) {
1285 return; // just stop
1286 }
1287
1288 // fire all other targets
1289 G_UseTargets( next, NULL );
1290
1291 // set the new trajectory
1292 ent->nextTrain = next->nextTrain;
1293 VectorCopy( next->s.origin, ent->pos1 );
1294 VectorCopy( next->nextTrain->s.origin, ent->pos2 );
1295
1296 // if the path_corner has a speed, use that
1297 if ( next->speed ) {
1298 speed = next->speed;
1299 } else {
1300 // otherwise use the train's speed
1301 speed = ent->speed;
1302 }
1303 if ( speed < 1 ) {
1304 speed = 1;
1305 }
1306
1307 // calculate duration
1308 VectorSubtract( ent->pos2, ent->pos1, move );
1309 length = VectorLength( move );
1310
1311 ent->s.pos.trDuration = length * 1000 / speed;
1312
1313 // Tequila comment: Be sure to send to clients after any fast move case
1314 ent->r.svFlags &= ~SVF_NOCLIENT;
1315
1316 // Tequila comment: Fast move case
1317 if(ent->s.pos.trDuration<1) {
1318 // Tequila comment: As trDuration is used later in a division, we need to avoid that case now
1319 // With null trDuration,
1320 // the calculated rocks bounding box becomes infinite and the engine think for a short time
1321 // any entity is riding that mover but not the world entity... In rare case, I found it
1322 // can also stuck every map entities after func_door are used.
1323 // The desired effect with very very big speed is to have instant move, so any not null duration
1324 // lower than a frame duration should be sufficient.
1325 // Afaik, the negative case don't have to be supported.
1326 ent->s.pos.trDuration=1;
1327
1328 // Tequila comment: Don't send entity to clients so it becomes really invisible
1329 ent->r.svFlags |= SVF_NOCLIENT;
1330 }
1331
1332 // looping sound
1333 ent->s.loopSound = next->soundLoop;
1334
1335 // start it going
1336 SetMoverState( ent, MOVER_1TO2, level.time );
1337
1338 // if there is a "wait" value on the target, don't start moving yet
1339 if ( next->wait ) {
1340 ent->nextthink = level.time + next->wait * 1000;
1341 ent->think = Think_BeginMoving;
1342 ent->s.pos.trType = TR_STATIONARY;
1343 }
1344 }
1345
1346
1347 /*
1348 ===============
1349 Think_SetupTrainTargets
1350
1351 Link all the corners together
1352 ===============
1353 */
1354 void Think_SetupTrainTargets( gentity_t *ent ) {
1355 gentity_t *path, *next, *start;
1356
1357 ent->nextTrain = G_Find( NULL, FOFS(targetname), ent->target );
1358 if ( !ent->nextTrain ) {
1359 G_Printf( "func_train at %s with an unfound target\n",
1360 vtos(ent->r.absmin) );
1361 return;
1362 }
1363
1364 start = NULL;
1365 for ( path = ent->nextTrain ; path != start ; path = next ) {
1366 if ( !start ) {
1367 start = path;
1368 }
1369
1370 if ( !path->target ) {
1371 G_Printf( "Train corner at %s without a target\n",
1372 vtos(path->s.origin) );
1373 return;
1374 }
1375
1376 // find a path_corner among the targets
1377 // there may also be other targets that get fired when the corner
1378 // is reached
1379 next = NULL;
1380 do {
1381 next = G_Find( next, FOFS(targetname), path->target );
1382 if ( !next ) {
1383 G_Printf( "Train corner at %s without a target path_corner\n",
1384 vtos(path->s.origin) );
1385 return;
1386 }
1387 } while ( strcmp( next->classname, "path_corner" ) );
1388
1389 path->nextTrain = next;
1390 }
1391
1392 // start the train moving from the first corner
1393 Reached_Train( ent );
1394 }
1395
1396
1397
1398 /*QUAKED path_corner (.5 .3 0) (-8 -8 -8) (8 8 8)
1399 Train path corners.
1400 Target: next path corner and other targets to fire
1401 "speed" speed to move to the next corner
1402 "wait" seconds to wait before behining move to next corner
1403 */
1404 void SP_path_corner( gentity_t *self ) {
1405 if ( !self->targetname ) {
1406 G_Printf ("path_corner with no targetname at %s\n", vtos(self->s.origin));
1407 G_FreeEntity( self );
1408 return;
1409 }
1410 // path corners don't need to be linked in
1411 }
1412
1413
1414
1415 /*QUAKED func_train (0 .5 .8) ? START_ON TOGGLE BLOCK_STOPS
1416 A train is a mover that moves between path_corner target points.
1417 Trains MUST HAVE AN ORIGIN BRUSH.
1418 The train spawns at the first target it is pointing at.
1419 "model2" .md3 model to also draw
1420 "speed" default 100
1421 "dmg" default 2
1422 "noise" looping sound to play when the train is in motion
1423 "target" next path corner
1424 "color" constantLight color
1425 "light" constantLight radius
1426 */
1427 void SP_func_train (gentity_t *self) {
1428 VectorClear (self->s.angles);
1429
1430 if (self->spawnflags & TRAIN_BLOCK_STOPS) {
1431 self->damage = 0;
1432 } else {
1433 if (!self->damage) {
1434 self->damage = 2;
1435 }
1436 }
1437
1438 if ( !self->speed ) {
1439 self->speed = 100;
1440 }
1441
1442 if ( !self->target ) {
1443 G_Printf ("func_train without a target at %s\n", vtos(self->r.absmin));
1444 G_FreeEntity( self );
1445 return;
1446 }
1447
1448 trap_SetBrushModel( self, self->model );
1449 InitMover( self );
1450
1451 self->reached = Reached_Train;
1452
1453 // start trains on the second frame, to make sure their targets have had
1454 // a chance to spawn
1455 self->nextthink = level.time + FRAMETIME;
1456 self->think = Think_SetupTrainTargets;
1457 }
1458
1459 /*
1460 ===============================================================================
1461
1462 STATIC
1463
1464 ===============================================================================
1465 */
1466
1467
1468 /*QUAKED func_static (0 .5 .8) ?
1469 A bmodel that just sits there, doing nothing. Can be used for conditional walls and models.
1470 "model2" .md3 model to also draw
1471 "color" constantLight color
1472 "light" constantLight radius
1473 */
1474 void SP_func_static( gentity_t *ent ) {
1475 trap_SetBrushModel( ent, ent->model );
1476 InitMover( ent );
1477 VectorCopy( ent->s.origin, ent->s.pos.trBase );
1478 VectorCopy( ent->s.origin, ent->r.currentOrigin );
1479 }
1480
1481
1482 /*
1483 ===============================================================================
1484
1485 ROTATING
1486
1487 ===============================================================================
1488 */
1489
1490
1491 /*QUAKED func_rotating (0 .5 .8) ? START_ON - X_AXIS Y_AXIS
1492 You need to have an origin brush as part of this entity. The center of that brush will be
1493 the point around which it is rotated. It will rotate around the Z axis by default. You can
1494 check either the X_AXIS or Y_AXIS box to change that.
1495
1496 "model2" .md3 model to also draw
1497 "speed" determines how fast it moves; default value is 100.
1498 "dmg" damage to inflict when blocked (2 default)
1499 "color" constantLight color
1500 "light" constantLight radius
1501 */
1502 void SP_func_rotating (gentity_t *ent) {
1503 if ( !ent->speed ) {
1504 ent->speed = 100;
1505 }
1506
1507 // set the axis of rotation
1508 ent->s.apos.trType = TR_LINEAR;
1509 if ( ent->spawnflags & 4 ) {
1510 ent->s.apos.trDelta[2] = ent->speed;
1511 } else if ( ent->spawnflags & 8 ) {
1512 ent->s.apos.trDelta[0] = ent->speed;
1513 } else {
1514 ent->s.apos.trDelta[1] = ent->speed;
1515 }
1516
1517 if (!ent->damage) {
1518 ent->damage = 2;
1519 }
1520
1521 trap_SetBrushModel( ent, ent->model );
1522 InitMover( ent );
1523
1524 VectorCopy( ent->s.origin, ent->s.pos.trBase );
1525 VectorCopy( ent->s.pos.trBase, ent->r.currentOrigin );
1526 VectorCopy( ent->s.apos.trBase, ent->r.currentAngles );
1527
1528 trap_LinkEntity( ent );
1529 }
1530
1531
1532 /*
1533 ===============================================================================
1534
1535 BOBBING
1536
1537 ===============================================================================
1538 */
1539
1540
1541 /*QUAKED func_bobbing (0 .5 .8) ? X_AXIS Y_AXIS
1542 Normally bobs on the Z axis
1543 "model2" .md3 model to also draw
1544 "height" amplitude of bob (32 default)
1545 "speed" seconds to complete a bob cycle (4 default)
1546 "phase" the 0.0 to 1.0 offset in the cycle to start at
1547 "dmg" damage to inflict when blocked (2 default)
1548 "color" constantLight color
1549 "light" constantLight radius
1550 */
1551 void SP_func_bobbing (gentity_t *ent) {
1552 float height;
1553 float phase;
1554
1555 G_SpawnFloat( "speed", "4", &ent->speed );
1556 G_SpawnFloat( "height", "32", &height );
1557 G_SpawnInt( "dmg", "2", &ent->damage );
1558 G_SpawnFloat( "phase", "0", &phase );
1559
1560 trap_SetBrushModel( ent, ent->model );
1561 InitMover( ent );
1562
1563 VectorCopy( ent->s.origin, ent->s.pos.trBase );
1564 VectorCopy( ent->s.origin, ent->r.currentOrigin );
1565
1566 ent->s.pos.trDuration = ent->speed * 1000;
1567 ent->s.pos.trTime = ent->s.pos.trDuration * phase;
1568 ent->s.pos.trType = TR_SINE;
1569
1570 // set the axis of bobbing
1571 if ( ent->spawnflags & 1 ) {
1572 ent->s.pos.trDelta[0] = height;
1573 } else if ( ent->spawnflags & 2 ) {
1574 ent->s.pos.trDelta[1] = height;
1575 } else {
1576 ent->s.pos.trDelta[2] = height;
1577 }
1578 }
1579
1580 /*
1581 ===============================================================================
1582
1583 PENDULUM
1584
1585 ===============================================================================
1586 */
1587
1588
1589 /*QUAKED func_pendulum (0 .5 .8) ?
1590 You need to have an origin brush as part of this entity.
1591 Pendulums always swing north / south on unrotated models. Add an angles field to the model to allow rotation in other directions.
1592 Pendulum frequency is a physical constant based on the length of the beam and gravity.
1593 "model2" .md3 model to also draw
1594 "speed" the number of degrees each way the pendulum swings, (30 default)
1595 "phase" the 0.0 to 1.0 offset in the cycle to start at
1596 "dmg" damage to inflict when blocked (2 default)
1597 "color" constantLight color
1598 "light" constantLight radius
1599 */
1600 void SP_func_pendulum(gentity_t *ent) {
1601 float freq;
1602 float length;
1603 float phase;
1604 float speed;
1605
1606 G_SpawnFloat( "speed", "30", &speed );
1607 G_SpawnInt( "dmg", "2", &ent->damage );
1608 G_SpawnFloat( "phase", "0", &phase );
1609
1610 trap_SetBrushModel( ent, ent->model );
1611
1612 // find pendulum length
1613 length = fabs( ent->r.mins[2] );
1614 if ( length < 8 ) {
1615 length = 8;
1616 }
1617
1618 freq = 1 / ( M_PI * 2 ) * sqrt( g_gravity.value / ( 3 * length ) );
1619
1620 ent->s.pos.trDuration = ( 1000 / freq );
1621
1622 InitMover( ent );
1623
1624 VectorCopy( ent->s.origin, ent->s.pos.trBase );
1625 VectorCopy( ent->s.origin, ent->r.currentOrigin );
1626
1627 VectorCopy( ent->s.angles, ent->s.apos.trBase );
1628
1629 ent->s.apos.trDuration = 1000 / freq;
1630 ent->s.apos.trTime = ent->s.apos.trDuration * phase;
1631 ent->s.apos.trType = TR_SINE;
1632 ent->s.apos.trDelta[2] = speed;
1633 }