summaryrefslogtreecommitdiff
path: root/src/cgame/cg_event.c
blob: b8c5e44cc69ac8954a7c415118643d89d072c031 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
// Copyright (C) 1999-2000 Id Software, Inc.
//
// cg_event.c -- handle entity events at snapshot or playerstate transitions

/*
 *  Portions Copyright (C) 2000-2001 Tim Angus
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the OSML - Open Source Modification License v1.0 as
 *  described in the file COPYING which is distributed with this source
 *  code.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

#include "cg_local.h"

// for the voice chats
#include "../../ui/menudef.h"

//==========================================================================

/*
===================
CG_PlaceString

Also called by scoreboard drawing
===================
*/
const char  *CG_PlaceString( int rank ) {
  static char str[64];
  char  *s, *t;

  if ( rank & RANK_TIED_FLAG ) {
    rank &= ~RANK_TIED_FLAG;
    t = "Tied for ";
  } else {
    t = "";
  }

  if ( rank == 1 ) {
    s = S_COLOR_BLUE "1st" S_COLOR_WHITE;   // draw in blue
  } else if ( rank == 2 ) {
    s = S_COLOR_RED "2nd" S_COLOR_WHITE;    // draw in red
  } else if ( rank == 3 ) {
    s = S_COLOR_YELLOW "3rd" S_COLOR_WHITE;   // draw in yellow
  } else if ( rank == 11 ) {
    s = "11th";
  } else if ( rank == 12 ) {
    s = "12th";
  } else if ( rank == 13 ) {
    s = "13th";
  } else if ( rank % 10 == 1 ) {
    s = va("%ist", rank);
  } else if ( rank % 10 == 2 ) {
    s = va("%ind", rank);
  } else if ( rank % 10 == 3 ) {
    s = va("%ird", rank);
  } else {
    s = va("%ith", rank);
  }

  Com_sprintf( str, sizeof( str ), "%s%s", t, s );
  return str;
}

/*
=============
CG_Obituary
=============
*/
static void CG_Obituary( entityState_t *ent ) {
  int     mod;
  int     target, attacker;
  char    *message;
  char    *message2;
  const char  *targetInfo;
  const char  *attackerInfo;
  char    targetName[32];
  char    attackerName[32];
  gender_t  gender;
  clientInfo_t  *ci;

  target = ent->otherEntityNum;
  attacker = ent->otherEntityNum2;
  mod = ent->eventParm;

  if ( target < 0 || target >= MAX_CLIENTS ) {
    CG_Error( "CG_Obituary: target out of range" );
  }
  ci = &cgs.clientinfo[target];

  if ( attacker < 0 || attacker >= MAX_CLIENTS ) {
    attacker = ENTITYNUM_WORLD;
    attackerInfo = NULL;
  } else {
    attackerInfo = CG_ConfigString( CS_PLAYERS + attacker );
  }

  targetInfo = CG_ConfigString( CS_PLAYERS + target );
  if ( !targetInfo ) {
    return;
  }
  Q_strncpyz( targetName, Info_ValueForKey( targetInfo, "n" ), sizeof(targetName) - 2);
  strcat( targetName, S_COLOR_WHITE );

  message2 = "";

  // check for single client messages

  switch( mod ) {
  case MOD_SUICIDE:
    message = "suicides";
    break;
  case MOD_FALLING:
    message = "cratered";
    break;
  case MOD_CRUSH:
    message = "was squished";
    break;
  case MOD_WATER:
    message = "sank like a rock";
    break;
  case MOD_SLIME:
    message = "melted";
    break;
  case MOD_LAVA:
    message = "does a back flip into the lava";
    break;
  case MOD_TARGET_LASER:
    message = "saw the light";
    break;
  case MOD_TRIGGER_HURT:
    message = "was in the wrong place";
    break;
  case MOD_HSPAWN:
    message = "should have run further";
    break;
  case MOD_ASPAWN:
    message = "was melted by the acid blood";
    break;
  default:
    message = NULL;
    break;
  }

  if (attacker == target) {
    gender = ci->gender;
    switch (mod) {
    case MOD_GRENADE_SPLASH:
      if ( gender == GENDER_FEMALE )
        message = "tripped on her own grenade";
      else if ( gender == GENDER_NEUTER )
        message = "tripped on its own grenade";
      else
        message = "tripped on his own grenade";
      break;
    case MOD_ROCKET_SPLASH:
      if ( gender == GENDER_FEMALE )
        message = "blew herself up";
      else if ( gender == GENDER_NEUTER )
        message = "blew itself up";
      else
        message = "blew himself up";
      break;
    case MOD_FLAMER_SPLASH:
      if ( gender == GENDER_FEMALE )
        message = "toasted herself";
      else if ( gender == GENDER_NEUTER )
        message = "toasted itself";
      else
        message = "toasted himself";
      break;
    case MOD_BFG_SPLASH:
      message = "should have used a smaller gun";
      break;
    default:
      if ( gender == GENDER_FEMALE )
        message = "killed herself";
      else if ( gender == GENDER_NEUTER )
        message = "killed itself";
      else
        message = "killed himself";
      break;
    }
  }

  if (message) {
    CG_Printf( "%s %s.\n", targetName, message);
    return;
  }

  // check for kill messages from the current clientNum
  if ( attacker == cg.snap->ps.clientNum ) {
    char  *s;

    s = va("You fragged %s", targetName );
    CG_CenterPrint( s, SCREEN_HEIGHT * 0.30, BIGCHAR_WIDTH );

    // print the text message as well
  }

  // check for double client messages
  if ( !attackerInfo ) {
    attacker = ENTITYNUM_WORLD;
    strcpy( attackerName, "noname" );
  } else {
    Q_strncpyz( attackerName, Info_ValueForKey( attackerInfo, "n" ), sizeof(attackerName) - 2);
    strcat( attackerName, S_COLOR_WHITE );
    // check for kill messages about the current clientNum
    if ( target == cg.snap->ps.clientNum ) {
      Q_strncpyz( cg.killerName, attackerName, sizeof( cg.killerName ) );
    }
  }

  if ( attacker != ENTITYNUM_WORLD ) {
    switch (mod) {
    case MOD_GRAPPLE:
      message = "was caught by";
      break;
    case MOD_GAUNTLET:
      message = "was pummeled by";
      break;
    case MOD_MACHINEGUN:
      message = "was machinegunned by";
      break;
    case MOD_CHAINGUN:
      message = "was chaingunned by";
      break;
    case MOD_SHOTGUN:
      message = "was gunned down by";
      break;
    case MOD_GRENADE:
      message = "ate";
      message2 = "'s grenade";
      break;
    case MOD_GRENADE_SPLASH:
      message = "was shredded by";
      message2 = "'s shrapnel";
      break;
    case MOD_ROCKET:
      message = "ate";
      message2 = "'s rocket";
      break;
    case MOD_ROCKET_SPLASH:
      message = "almost dodged";
      message2 = "'s rocket";
      break;
    case MOD_FLAMER:
      message = "was toasted by";
      message2 = "'s flamer";
      break;
    case MOD_FLAMER_SPLASH:
      message = "was toasted by";
      message2 = "'s flamer";
      break;
    case MOD_RAILGUN:
      message = "was railed by";
      break;
    case MOD_LIGHTNING:
      message = "was electrocuted by";
      break;
    case MOD_VENOM:
          message = "was biten by";
      break;
    case MOD_BFG:
    case MOD_BFG_SPLASH:
      message = "was blasted by";
      message2 = "'s BFG";
      break;
    case MOD_TELEFRAG:
      message = "tried to invade";
      message2 = "'s personal space";
      break;
    default:
      message = "was killed by";
      break;
    }

    if (message) {
      CG_Printf( "%s %s %s%s\n",
        targetName, message, attackerName, message2);
      return;
    }
  }

  // we don't know what it was
  CG_Printf( "%s died.\n", targetName );
}

//==========================================================================

/*
===============
CG_UseItem
===============
*/
static void CG_UseItem( centity_t *cent ) {
  clientInfo_t *ci;
  int     itemNum, clientNum;
  gitem_t   *item;
  entityState_t *es;

  es = &cent->currentState;

  itemNum = (es->event & ~EV_EVENT_BITS) - EV_USE_ITEM0;
  if ( itemNum < 0 || itemNum > HI_NUM_HOLDABLE ) {
    itemNum = 0;
  }

  // print a message if the local player
  if ( es->number == cg.snap->ps.clientNum ) {
    if ( !itemNum ) {
      CG_CenterPrint( "No item to use", SCREEN_HEIGHT * 0.30, BIGCHAR_WIDTH );
    } else {
      /*item = BG_FindItemForHoldable( itemNum );*/
      CG_CenterPrint( va("Use %s", item->pickup_name), SCREEN_HEIGHT * 0.30, BIGCHAR_WIDTH );
    }
  }

  switch ( itemNum ) {
  default:
  case HI_NONE:
    trap_S_StartSound (NULL, es->number, CHAN_BODY, cgs.media.useNothingSound );
    break;

  case HI_TELEPORTER:
    break;

  case HI_MEDKIT:
    clientNum = cent->currentState.clientNum;
    if ( clientNum >= 0 && clientNum < MAX_CLIENTS ) {
      ci = &cgs.clientinfo[ clientNum ];
      ci->medkitUsageTime = cg.time;
    }
    trap_S_StartSound (NULL, es->number, CHAN_BODY, cgs.media.medkitSound );
    break;
  }

}

/*
================
CG_ItemPickup

A new item was picked up this frame
================
*/
static void CG_ItemPickup( int itemNum ) {
  cg.itemPickup = itemNum;
  cg.itemPickupTime = cg.time;
  cg.itemPickupBlendTime = cg.time;
  // see if it should be the grabbed weapon

}


/*
================
CG_PainEvent

Also called by playerstate transition
================
*/
void CG_PainEvent( centity_t *cent, int health ) {
  char  *snd;

  // don't do more than two pain sounds a second
  if ( cg.time - cent->pe.painTime < 500 ) {
    return;
  }

  if ( health < 25 ) {
    snd = "*pain25_1.wav";
  } else if ( health < 50 ) {
    snd = "*pain50_1.wav";
  } else if ( health < 75 ) {
    snd = "*pain75_1.wav";
  } else {
    snd = "*pain100_1.wav";
  }
  trap_S_StartSound( NULL, cent->currentState.number, CHAN_VOICE,
    CG_CustomSound( cent->currentState.number, snd ) );

  // save pain time for programitic twitch animation
  cent->pe.painTime = cg.time;
  cent->pe.painDirection ^= 1;
}


/*
==============
CG_Menu
==============
*/
void CG_Menu( int eventParm )
{
  int   i;
  char  carriageCvar[ MAX_TOKEN_CHARS ];

  *carriageCvar = 0;
  
  //determine what the player is carrying
  for( i = WP_NONE +1; i < WP_NUM_WEAPONS; i++ )
  {
    if( BG_gotWeapon( i, cg.snap->ps.stats ) )
      strcat( carriageCvar, va( "W%d ", i ) );
  }
  for( i = UP_NONE +1; i < UP_NUM_UPGRADES; i++ )
  {
    if( BG_gotItem( i, cg.snap->ps.stats ) )
      strcat( carriageCvar, va( "U%d ", i ) );
  }
  strcat( carriageCvar, "$" );
  
  trap_Cvar_Set( "ui_carriage", carriageCvar );
  
  trap_Cvar_Set( "ui_stages", va( "%d %d", cgs.alienStage, cgs.humanStage ) );
  
  switch( eventParm )
  {
    case MN_TEAM:       trap_SendConsoleCommand( "menu tremulous_teamselect\n" );                 break;
    case MN_A_CLASS:    trap_SendConsoleCommand( "menu tremulous_alienclass\n" );                 break;
    case MN_H_SPAWN:    trap_SendConsoleCommand( "menu tremulous_humanitem\n" );                  break;
    case MN_A_BUILD:    trap_SendConsoleCommand( "menu tremulous_alienbuild\n" );                 break;
    case MN_H_BUILD:    trap_SendConsoleCommand( "menu tremulous_humanbuild\n" );                 break;
    case MN_H_MCU:      trap_SendConsoleCommand( "menu tremulous_humanmcu\n" );                   break;
    case MN_H_BANK:     trap_SendConsoleCommand( "menu tremulous_humanbank\n" );                  break;
    case MN_A_OBANK:    trap_SendConsoleCommand( "menu tremulous_alienbank\n" );                  break;
                        
    case MN_H_NOROOM:
      trap_Cvar_Set( "ui_dialog", "There is no room to build here. Move until the buildable turns "
                                  "translucent green indicating a valid build location." );
      trap_SendConsoleCommand( "menu tremulous_human_dialog\n" );
      break;
      
    case MN_H_NOPOWER:
      trap_Cvar_Set( "ui_dialog", "There is no power remaining. Free up power by destroying existing "
                                  "buildable objects." );
      trap_SendConsoleCommand( "menu tremulous_human_dialog\n" );
      break;
      
    case MN_H_REACTOR:
      trap_Cvar_Set( "ui_dialog", "There can only be one reactor. Destroy the existing one if you "
                                  "wish to move it." );
      trap_SendConsoleCommand( "menu tremulous_human_dialog\n" );
      break;
      
    case MN_H_REPEATER:
      trap_Cvar_Set( "ui_dialog", "There is no power here. If available, a Repeater may be used to "
                                  "transmit power to this location." );
      trap_SendConsoleCommand( "menu tremulous_human_dialog\n" );
      break;
      
    case MN_H_NODCC:
      trap_Cvar_Set( "ui_dialog", "There is no Defense Computer. A Defense Computer is needed to build "
                                  "this." );
      trap_SendConsoleCommand( "menu tremulous_human_dialog\n" );
      break;
      
    case MN_H_RPLWARN:
      trap_Cvar_Set( "ui_dialog", "WARNING: This replicator will not be powered. Build a reactor to "
                                  "prevent seeing this message again." );
      trap_SendConsoleCommand( "menu tremulous_human_dialog\n" );
      break;
      
    case MN_H_RPTWARN:
      trap_Cvar_Set( "ui_dialog", "WARNING: This repeater will not be powered as there is no parent "
                                  "reactor providing power. Build a reactor." );
      trap_SendConsoleCommand( "menu tremulous_human_dialog\n" );
      break;
      
    case MN_H_NOSLOTS:
      trap_Cvar_Set( "ui_dialog", "You have no room to carry this. Please sell any conflicting "
                                  "upgrades before purchasing this item." );
      trap_SendConsoleCommand( "menu tremulous_human_dialog\n" );
      break;
      
    case MN_H_NOFUNDS:
      trap_Cvar_Set( "ui_dialog", "Insufficient funds. You do not have enough credits to perform this "
                                  "action." );
      trap_SendConsoleCommand( "menu tremulous_human_dialog\n" );
      break;
      
    case MN_H_ITEMHELD:
      trap_Cvar_Set( "ui_dialog", "You already hold this item. It is not possible to carry multiple items "
                                  "of the same type." );
      trap_SendConsoleCommand( "menu tremulous_human_dialog\n" );
      break;
      
    case MN_A_NOROOM:
      trap_Cvar_Set( "ui_dialog", "There is no room to build here. Move until the structure turns "
                                  "translucent green indicating a valid build location." );
      trap_SendConsoleCommand( "menu tremulous_alien_dialog\n" );
      break;
      
    case MN_A_NOCREEP:
      trap_Cvar_Set( "ui_dialog", "There is no creep here. You must build near existing Eggs or "
                                  "the Overmind. Alien structures will not support themselves." );
      trap_SendConsoleCommand( "menu tremulous_alien_dialog\n" );
      break;
      
    case MN_A_NOHVMND:
      trap_Cvar_Set( "ui_dialog", "There is no Overmind. An Overmind must be built to control "
                                  "the structure you tried to place" );
      trap_SendConsoleCommand( "menu tremulous_alien_dialog\n" );
      break;
      
    case MN_A_HIVEMIND:
      trap_Cvar_Set( "ui_dialog", "There can only be one Overmind. Destroy the existing one if you "
                                  "wish to move it." );
      trap_SendConsoleCommand( "menu tremulous_alien_dialog\n" );
      break;
      
    case MN_A_NOASSERT:
      trap_Cvar_Set( "ui_dialog", "The Overmind cannot control anymore structures. Destroy existing "
                                  "structures to build more." );
      trap_SendConsoleCommand( "menu tremulous_alien_dialog\n" );
      break;
      
    case MN_A_SPWNWARN:
      trap_Cvar_Set( "ui_dialog", "WARNING: This spawn will not be controlled by an Overmind. "
                                  "Build an Overmind to prevent seeing this message again." );
      trap_SendConsoleCommand( "menu tremulous_alien_dialog\n" );
      break;
      
    case MN_A_NORMAL:
      trap_Cvar_Set( "ui_dialog", "Cannot build on this surface. This surface is too steep or unsuitable "
                                  "to build on. Please choose another site for this structure." );
      trap_SendConsoleCommand( "menu tremulous_alien_dialog\n" );
      break;
      
    case MN_A_NOFUNDS:
      trap_Cvar_Set( "ui_dialog", "You do not possess sufficient organs to perform this action." );
      trap_SendConsoleCommand( "menu tremulous_alien_dialog\n" );
      break;
      
    case MN_A_HOVEL_OCCUPIED:
      trap_Cvar_Set( "ui_dialog", "This Hovel is occupied by another builder. Please find or build "
                                  "another." );
      trap_SendConsoleCommand( "menu tremulous_alien_dialog\n" );
      break;
      
    case MN_A_HOVEL_BLOCKED:
      trap_Cvar_Set( "ui_dialog", "The exit to this Hovel is currently blocked. Please wait until it "
                                  "becomes clear then try again." );
      trap_SendConsoleCommand( "menu tremulous_alien_dialog\n" );
      break;
      
    case MN_A_INFEST:
      trap_Cvar_Set( "ui_currentClass", va( "%d %d",  cg.snap->ps.stats[ STAT_PCLASS ],
                                                      cg.snap->ps.persistant[ PERS_CREDIT ] ) );
      trap_SendConsoleCommand( "menu tremulous_alienupgrade\n" );
      break;

    default:
      Com_Printf( "cgame: debug: no such menu %d\n", eventParm );
  }
}

/*
==============
CG_EntityEvent

An entity has an event value
also called by CG_CheckPlayerstateEvents
==============
*/
#define DEBUGNAME(x) if(cg_debugEvents.integer){CG_Printf(x"\n");}
void CG_EntityEvent( centity_t *cent, vec3_t position )
{
  entityState_t *es;
  int           event;
  vec3_t        dir;
  const char    *s;
  int           clientNum;
  clientInfo_t  *ci;
  int           steptime, i;

  steptime = BG_FindSteptimeForClass( cg.predictedPlayerState.stats[ STAT_PCLASS ] );

  es = &cent->currentState;
  event = es->event & ~EV_EVENT_BITS;

  if ( cg_debugEvents.integer ) {
    CG_Printf( "ent:%3i  event:%3i ", es->number, event );
  }

  if ( !event ) {
    DEBUGNAME("ZEROEVENT");
    return;
  }

  clientNum = es->clientNum;
  if ( clientNum < 0 || clientNum >= MAX_CLIENTS ) {
    clientNum = 0;
  }
  ci = &cgs.clientinfo[ clientNum ];

  switch ( event ) {
  //
  // movement generated events
  //
  case EV_FOOTSTEP:
    DEBUGNAME("EV_FOOTSTEP");
    if (cg_footsteps.integer) {
      trap_S_StartSound (NULL, es->number, CHAN_BODY,
        cgs.media.footsteps[ ci->footsteps ][rand()&3] );
    }
    break;
  case EV_FOOTSTEP_METAL:
    DEBUGNAME("EV_FOOTSTEP_METAL");
    if (cg_footsteps.integer) {
      trap_S_StartSound (NULL, es->number, CHAN_BODY,
        cgs.media.footsteps[ FOOTSTEP_METAL ][rand()&3] );
    }
    break;
  case EV_FOOTSTEP_SQUELCH:
    DEBUGNAME("EV_FOOTSTEP_SQUELCH");
    if (cg_footsteps.integer) {
      trap_S_StartSound (NULL, es->number, CHAN_BODY,
        cgs.media.footsteps[ FOOTSTEP_FLESH ][rand()&3] );
    }
    break;
  case EV_FOOTSPLASH:
    DEBUGNAME("EV_FOOTSPLASH");
    if (cg_footsteps.integer) {
      trap_S_StartSound (NULL, es->number, CHAN_BODY,
        cgs.media.footsteps[ FOOTSTEP_SPLASH ][rand()&3] );
    }
    break;
  case EV_FOOTWADE:
    DEBUGNAME("EV_FOOTWADE");
    if (cg_footsteps.integer) {
      trap_S_StartSound (NULL, es->number, CHAN_BODY,
        cgs.media.footsteps[ FOOTSTEP_SPLASH ][rand()&3] );
    }
    break;
  case EV_SWIM:
    DEBUGNAME("EV_SWIM");
    if (cg_footsteps.integer) {
      trap_S_StartSound (NULL, es->number, CHAN_BODY,
        cgs.media.footsteps[ FOOTSTEP_SPLASH ][rand()&3] );
    }
    break;


  case EV_FALL_SHORT:
    DEBUGNAME("EV_FALL_SHORT");
    trap_S_StartSound (NULL, es->number, CHAN_AUTO, cgs.media.landSound );
    if ( clientNum == cg.predictedPlayerState.clientNum ) {
      // smooth landing z changes
      cg.landChange = -8;
      cg.landTime = cg.time;
    }
    break;
  case EV_FALL_MEDIUM:
    DEBUGNAME("EV_FALL_MEDIUM");
    // use normal pain sound
    trap_S_StartSound( NULL, es->number, CHAN_VOICE, CG_CustomSound( es->number, "*pain100_1.wav" ) );
    if ( clientNum == cg.predictedPlayerState.clientNum ) {
      // smooth landing z changes
      cg.landChange = -16;
      cg.landTime = cg.time;
    }
    break;
  case EV_FALL_FAR:
    DEBUGNAME("EV_FALL_FAR");
    trap_S_StartSound (NULL, es->number, CHAN_AUTO, CG_CustomSound( es->number, "*fall1.wav" ) );
    cent->pe.painTime = cg.time;  // don't play a pain sound right after this
    if ( clientNum == cg.predictedPlayerState.clientNum ) {
      // smooth landing z changes
      cg.landChange = -24;
      cg.landTime = cg.time;
    }
    break;

	case EV_STEP_4:
	case EV_STEP_8:
	case EV_STEP_12:
	case EV_STEP_16:		// smooth out step up transitions
	case EV_STEPDN_4:
	case EV_STEPDN_8:
	case EV_STEPDN_12:
	case EV_STEPDN_16:		// smooth out step down transitions
		DEBUGNAME("EV_STEP");
	{
		float	oldStep;
		int		delta;
		int		step;

		if( clientNum != cg.predictedPlayerState.clientNum )
			break;
    
		// if we are interpolating, we don't need to smooth steps
		if( cg.demoPlayback || ( cg.snap->ps.pm_flags & PMF_FOLLOW ) ||
			  cg_nopredict.integer || cg_synchronousClients.integer )
	    break;
    
		// check for stepping up before a previous step is completed
		delta = cg.time - cg.stepTime;
    
		if( delta < STEP_TIME )
			oldStep = cg.stepChange * ( STEP_TIME - delta ) / STEP_TIME;
		else
			oldStep = 0;

		// add this amount
    if( event >= EV_STEPDN_4 )
    {
		  step = 4 * ( event - EV_STEPDN_4 + 1 );
      cg.stepChange = oldStep - step;
    }
    else
    {
      step = 4 * ( event - EV_STEP_4 + 1 );
      cg.stepChange = oldStep + step;
    }
      
		if( cg.stepChange > MAX_STEP_CHANGE )
			cg.stepChange = MAX_STEP_CHANGE;
    else if( cg.stepChange < -MAX_STEP_CHANGE )
			cg.stepChange = -MAX_STEP_CHANGE;

		cg.stepTime = cg.time;
		break;
	}

  case EV_JUMP_PAD:
    DEBUGNAME("EV_JUMP_PAD");
//    CG_Printf( "EV_JUMP_PAD w/effect #%i\n", es->eventParm );
    {
      localEntity_t *smoke;
      vec3_t      up = {0, 0, 1};


      smoke = CG_SmokePuff( cent->lerpOrigin, up, 
              32, 
              1, 1, 1, 0.33f,
              1000, 
              cg.time, 0,
              LEF_PUFF_DONT_SCALE, 
              cgs.media.smokePuffShader );
    }
    // boing sound at origin, jump sound on player
    trap_S_StartSound ( cent->lerpOrigin, -1, CHAN_VOICE, cgs.media.jumpPadSound );
    trap_S_StartSound (NULL, es->number, CHAN_VOICE, CG_CustomSound( es->number, "*jump1.wav" ) );
    break;

  case EV_JUMP:
    DEBUGNAME("EV_JUMP");
    trap_S_StartSound (NULL, es->number, CHAN_VOICE, CG_CustomSound( es->number, "*jump1.wav" ) );
    break;
  case EV_TAUNT:
    DEBUGNAME("EV_TAUNT");
    trap_S_StartSound (NULL, es->number, CHAN_VOICE, CG_CustomSound( es->number, "*taunt.wav" ) );
    break;
  case EV_WATER_TOUCH:
    DEBUGNAME("EV_WATER_TOUCH");
    trap_S_StartSound (NULL, es->number, CHAN_AUTO, cgs.media.watrInSound );
    break;
  case EV_WATER_LEAVE:
    DEBUGNAME("EV_WATER_LEAVE");
    trap_S_StartSound (NULL, es->number, CHAN_AUTO, cgs.media.watrOutSound );
    break;
  case EV_WATER_UNDER:
    DEBUGNAME("EV_WATER_UNDER");
    trap_S_StartSound (NULL, es->number, CHAN_AUTO, cgs.media.watrUnSound );
    break;
  case EV_WATER_CLEAR:
    DEBUGNAME("EV_WATER_CLEAR");
    trap_S_StartSound (NULL, es->number, CHAN_AUTO, CG_CustomSound( es->number, "*gasp.wav" ) );
    break;

  case EV_ITEM_PICKUP:
    DEBUGNAME("EV_ITEM_PICKUP");
    {
    }
    break;

  case EV_GLOBAL_ITEM_PICKUP:
    DEBUGNAME("EV_GLOBAL_ITEM_PICKUP");
    {
    }
    break;

  //
  // weapon events
  //
  case EV_NOAMMO:
    DEBUGNAME("EV_NOAMMO");
//    trap_S_StartSound (NULL, es->number, CHAN_AUTO, cgs.media.noAmmoSound );
    if ( es->number == cg.snap->ps.clientNum ) {
      CG_OutOfAmmoChange();
    }
    break;
  case EV_CHANGE_WEAPON:
    DEBUGNAME("EV_CHANGE_WEAPON");
    trap_S_StartSound (NULL, es->number, CHAN_AUTO, cgs.media.selectSound );
    break;
  case EV_NEXT_WEAPON:
    DEBUGNAME("EV_NEXT_WEAPON");
    CG_NextWeapon_f( );
    break;
  case EV_FIRE_WEAPON:
    DEBUGNAME("EV_FIRE_WEAPON");
    CG_FireWeapon( cent, 0 );
    break;
  case EV_FIRE_WEAPON2:
    DEBUGNAME("EV_FIRE_WEAPON2");
    CG_FireWeapon( cent, 1 ); //FIXME:??
    break;
  case EV_FIRE_WEAPON3:
    DEBUGNAME("EV_FIRE_WEAPON3");
    CG_FireWeapon( cent, 2 ); //FIXME:??
    break;
  case EV_USE_ITEM0:
    DEBUGNAME("EV_USE_ITEM0");
    CG_UseItem( cent );
    break;
  case EV_USE_ITEM1:
    DEBUGNAME("EV_USE_ITEM1");
    CG_UseItem( cent );
    break;
  case EV_USE_ITEM2:
    DEBUGNAME("EV_USE_ITEM2");
    CG_UseItem( cent );
    break;
  case EV_USE_ITEM3:
    DEBUGNAME("EV_USE_ITEM3");
    CG_UseItem( cent );
    break;
  case EV_USE_ITEM4:
    DEBUGNAME("EV_USE_ITEM4");
    CG_UseItem( cent );
    break;
  case EV_USE_ITEM5:
    DEBUGNAME("EV_USE_ITEM5");
    CG_UseItem( cent );
    break;
  case EV_USE_ITEM6:
    DEBUGNAME("EV_USE_ITEM6");
    CG_UseItem( cent );
    break;
  case EV_USE_ITEM7:
    DEBUGNAME("EV_USE_ITEM7");
    CG_UseItem( cent );
    break;
  case EV_USE_ITEM8:
    DEBUGNAME("EV_USE_ITEM8");
    CG_UseItem( cent );
    break;
  case EV_USE_ITEM9:
    DEBUGNAME("EV_USE_ITEM9");
    CG_UseItem( cent );
    break;
  case EV_USE_ITEM10:
    DEBUGNAME("EV_USE_ITEM10");
    CG_UseItem( cent );
    break;
  case EV_USE_ITEM11:
    DEBUGNAME("EV_USE_ITEM11");
    CG_UseItem( cent );
    break;
  case EV_USE_ITEM12:
    DEBUGNAME("EV_USE_ITEM12");
    CG_UseItem( cent );
    break;
  case EV_USE_ITEM13:
    DEBUGNAME("EV_USE_ITEM13");
    CG_UseItem( cent );
    break;
  case EV_USE_ITEM14:
    DEBUGNAME("EV_USE_ITEM14");
    CG_UseItem( cent );
    break;

  //=================================================================

  //
  // other events
  //
  case EV_PLAYER_TELEPORT_IN:
    DEBUGNAME("EV_PLAYER_TELEPORT_IN");
    trap_S_StartSound (NULL, es->number, CHAN_AUTO, cgs.media.teleInSound );
    CG_SpawnEffect( position);
    break;

  case EV_PLAYER_TELEPORT_OUT:
    DEBUGNAME("EV_PLAYER_TELEPORT_OUT");
    trap_S_StartSound (NULL, es->number, CHAN_AUTO, cgs.media.teleOutSound );
    CG_SpawnEffect(  position);
    break;

  case EV_ITEM_POP:
    DEBUGNAME("EV_ITEM_POP");
    trap_S_StartSound (NULL, es->number, CHAN_AUTO, cgs.media.respawnSound );
    break;
  case EV_ITEM_RESPAWN:
    DEBUGNAME("EV_ITEM_RESPAWN");
    cent->miscTime = cg.time; // scale up from this
    trap_S_StartSound (NULL, es->number, CHAN_AUTO, cgs.media.respawnSound );
    break;

  //TA: make alien items "grow"
  case EV_BUILD_CONSTRUCT:
    DEBUGNAME("EV_BUILD_CONSTRUCT");
    cent->miscTime = cg.time; // scale up from this
    //probably a better place for this, but for the time being it lives here
    memset( &cent->lerpFrame, 0, sizeof( lerpFrame_t ) );
    cent->buildableAnim = es->eventParm;
    break;
    
  //TA: make alien creep "recede"
  case EV_BUILD_DESTROY:
    DEBUGNAME("EV_BUILD_DESTROY");
    cent->miscTime = -cg.time; // scale down from this
    break;

  //TA: trigger an anim on a buildable item
  case EV_BUILD_ANIM:
    DEBUGNAME("EV_BUILD_ANIM");
    if( cent->buildableAnim == es->torsoAnim || es->eventParm & ANIM_TOGGLEBIT )
      cent->buildableAnim = es->eventParm;
    break;
    
  case EV_GRENADE_BOUNCE:
    DEBUGNAME("EV_GRENADE_BOUNCE");
    if ( rand() & 1 ) {
      trap_S_StartSound (NULL, es->number, CHAN_AUTO, cgs.media.hgrenb1aSound );
    } else {
      trap_S_StartSound (NULL, es->number, CHAN_AUTO, cgs.media.hgrenb2aSound );
    }
    break;

  //
  // missile impacts
  //
  case EV_MISSILE_HIT:
    DEBUGNAME("EV_MISSILE_HIT");
    ByteToDir( es->eventParm, dir );
    CG_MissileHitPlayer( es->weapon, position, dir, es->otherEntityNum, es->generic1 );
    break;

  case EV_MISSILE_MISS:
    DEBUGNAME("EV_MISSILE_MISS");
    ByteToDir( es->eventParm, dir );
    CG_MissileHitWall( es->weapon, 0, position, dir, IMPACTSOUND_DEFAULT, es->generic1 );
    break;

  case EV_MISSILE_MISS_METAL:
    DEBUGNAME("EV_MISSILE_MISS_METAL");
    ByteToDir( es->eventParm, dir );
    CG_MissileHitWall( es->weapon, 0, position, dir, IMPACTSOUND_METAL, es->generic1 );
    break;

  case EV_ITEM_EXPLOSION:
    DEBUGNAME("EV_ITEM_EXPLOSION");
    ByteToDir( es->eventParm, dir );
    CG_Explosion( 0, position, dir );
    break;

  case EV_RAILTRAIL:
/*    DEBUGNAME("EV_RAILTRAIL");
    cent->currentState.weapon = WP_RAILGUN;
    // if the end was on a nomark surface, don't make an explosion
    CG_RailTrail( es->origin2, es->pos.trBase );
    if ( es->eventParm != 255 ) {
      ByteToDir( es->eventParm, dir );
      CG_MissileHitWall( es->weapon, es->clientNum, position, dir, IMPACTSOUND_DEFAULT );
    }*/
    break;

  case EV_TESLATRAIL:
    DEBUGNAME("EV_TESLATRAIL");
    cent->currentState.weapon = WP_TESLAGEN;
    CG_TeslaTrail( es->origin2, es->pos.trBase, es->generic1, es->clientNum );
    break;

  case EV_ALIENZAP:
    DEBUGNAME("EV_ALIENZAP");
    CG_AlienZap( es->origin2, es->pos.trBase, es->generic1, es->clientNum );
    break;

  case EV_BULLET_HIT_WALL:
    DEBUGNAME("EV_BULLET_HIT_WALL");
    ByteToDir( es->eventParm, dir );
    CG_Bullet( es->pos.trBase, es->otherEntityNum, dir, qfalse, ENTITYNUM_WORLD );
    break;

  case EV_BULLET_HIT_FLESH:
    DEBUGNAME("EV_BULLET_HIT_FLESH");
    CG_Bullet( es->pos.trBase, es->otherEntityNum, dir, qtrue, es->eventParm );
    break;

  case EV_LAS_HIT_WALL:
    DEBUGNAME("EV_LAS_HIT_WALL");
    ByteToDir( es->eventParm, dir );
    CG_LasGunHit( es->pos.trBase, es->otherEntityNum, dir, qfalse, ENTITYNUM_WORLD );
    break;

  case EV_LAS_HIT_FLESH:
    DEBUGNAME("EV_LAS_HIT_FLESH");
    CG_LasGunHit( es->pos.trBase, es->otherEntityNum, dir, qtrue, es->eventParm );
    break;
    
#define MASS_EJECTION_VEL 300
  case EV_MASS_DRIVER_HIT:
    DEBUGNAME("EV_MASS_DRIVER_HIT");
    for( i = 0; i <= 10; i++ )
    {
      qhandle_t spark;
      vec3_t    velocity;
      vec3_t    accel = { 0.0f, 0.0f, -DEFAULT_GRAVITY };
      vec3_t    origin, normal;

      ByteToDir( es->eventParm, normal );

      VectorMA( es->pos.trBase, 10.0f, normal, origin );

      if( crandom( ) > 0.5f )
        spark = cgs.media.gibSpark1;
      else
        spark = cgs.media.gibSpark2;

      velocity[ 0 ] = ( 2 * random( ) - 1.0f ) * MASS_EJECTION_VEL;
      velocity[ 1 ] = ( 2 * random( ) - 1.0f ) * MASS_EJECTION_VEL;
      velocity[ 2 ] = ( 2 * random( ) - 1.0f ) * MASS_EJECTION_VEL;
      
      CG_LaunchSprite( origin, velocity, accel, 0.0f, 0.5f, 4.0f, 2.0f, 255, 0, rand( ) % 360,
                       cg.time, cg.time, 5000 + ( crandom( ) * 3000 ),
                       spark, qfalse, qfalse );
    }

    break;

  case EV_SHOTGUN:
    DEBUGNAME("EV_SHOTGUN");
    CG_ShotgunFire( es );
    break;

  case EV_GENERAL_SOUND:
    DEBUGNAME("EV_GENERAL_SOUND");
    if ( cgs.gameSounds[ es->eventParm ] ) {
      trap_S_StartSound (NULL, es->number, CHAN_VOICE, cgs.gameSounds[ es->eventParm ] );
    } else {
      s = CG_ConfigString( CS_SOUNDS + es->eventParm );
      trap_S_StartSound (NULL, es->number, CHAN_VOICE, CG_CustomSound( es->number, s ) );
    }
    break;

  case EV_GLOBAL_SOUND: // play from the player's head so it never diminishes
    DEBUGNAME("EV_GLOBAL_SOUND");
    if ( cgs.gameSounds[ es->eventParm ] ) {
      trap_S_StartSound (NULL, cg.snap->ps.clientNum, CHAN_AUTO, cgs.gameSounds[ es->eventParm ] );
    } else {
      s = CG_ConfigString( CS_SOUNDS + es->eventParm );
      trap_S_StartSound (NULL, cg.snap->ps.clientNum, CHAN_AUTO, CG_CustomSound( es->number, s ) );
    }
    break;

  case EV_PAIN:
    // local player sounds are triggered in CG_CheckLocalSounds,
    // so ignore events on the player
    DEBUGNAME("EV_PAIN");
    if ( cent->currentState.number != cg.snap->ps.clientNum ) {
      CG_PainEvent( cent, es->eventParm );
    }
    break;

  case EV_DEATH1:
  case EV_DEATH2:
  case EV_DEATH3:
    DEBUGNAME("EV_DEATHx");
    trap_S_StartSound( NULL, es->number, CHAN_VOICE,
        CG_CustomSound( es->number, va("*death%i.wav", event - EV_DEATH1 + 1) ) );
    break;


  case EV_OBITUARY:
    DEBUGNAME("EV_OBITUARY");
    CG_Obituary( es );
    break;

  //
  // powerup events
  //
  case EV_POWERUP_QUAD:
    DEBUGNAME("EV_POWERUP_QUAD");
    if ( es->number == cg.snap->ps.clientNum ) {
      cg.powerupActive = PW_QUAD;
      cg.powerupTime = cg.time;
    }
    trap_S_StartSound (NULL, es->number, CHAN_ITEM, cgs.media.quadSound );
    break;
  case EV_POWERUP_BATTLESUIT:
    DEBUGNAME("EV_POWERUP_BATTLESUIT");
    if ( es->number == cg.snap->ps.clientNum ) {
      cg.powerupActive = PW_BATTLESUIT;
      cg.powerupTime = cg.time;
    }
    trap_S_StartSound (NULL, es->number, CHAN_ITEM, cgs.media.protectSound );
    break;
  case EV_POWERUP_REGEN:
    DEBUGNAME("EV_POWERUP_REGEN");
    if ( es->number == cg.snap->ps.clientNum ) {
      cg.powerupActive = PW_REGEN;
      cg.powerupTime = cg.time;
    }
    trap_S_StartSound (NULL, es->number, CHAN_ITEM, cgs.media.regenSound );
    break;

  case EV_GIB_PLAYER:
    DEBUGNAME("EV_GIB_PLAYER");
    trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.gibSound );
    CG_GibPlayer( cent->lerpOrigin );
    break;

  case EV_GIB_ALIEN:
    DEBUGNAME("EV_GIB_ALIEN");
    trap_S_StartSound( NULL, es->number, CHAN_BODY, cgs.media.gibSound );
    CG_GenericGib( cent->lerpOrigin );
    break;

  case EV_STOPLOOPINGSOUND:
    DEBUGNAME("EV_STOPLOOPINGSOUND");
    trap_S_StopLoopingSound( es->number );
    es->loopSound = 0;
    break;
                  
  case EV_DEBUG_LINE:
    DEBUGNAME("EV_DEBUG_LINE");
    CG_Beam( cent );
    break;

  case EV_MENU:
    DEBUGNAME("EV_MENU");
    if( es->number == cg.clientNum )
      CG_Menu( es->eventParm );
    break;

  case EV_BUILD_DELAY:
    DEBUGNAME("EV_BUILD_DELAY");
    //FIXME: change to "negative" sound
    trap_S_StartLocalSound( cgs.media.hitSound, CHAN_LOCAL_SOUND );
    cg.lastBuildAttempt = cg.time;
    break;

  case EV_POISONCLOUD:
    DEBUGNAME("EV_POISONCLOUD");
    cg.firstPoisonedTime = cg.time;
    break;

  case EV_KNOCKOVER:
    DEBUGNAME("EV_KNOCKOVER");
    cg.firstKnockedTime = cg.time;
    break;

  case EV_GETUP:
    DEBUGNAME("EV_GETUP");
    cg.firstGetUpTime = cg.time;
    break;

  case EV_PLAYER_RESPAWN:
    DEBUGNAME("EV_PLAYER_RESPAWN");
    if( es->number == cg.clientNum )
      cg.spawnTime = cg.time;
    break;

  default:
    DEBUGNAME("UNKNOWN");
    CG_Error( "Unknown event: %i", event );
    break;
  }

}


/*
==============
CG_CheckEvents

==============
*/
void CG_CheckEvents( centity_t *cent ) {
  // check for event-only entities
  if ( cent->currentState.eType > ET_EVENTS ) {
    if ( cent->previousEvent ) {
      return; // already fired
    }
    cent->previousEvent = 1;

    cent->currentState.event = cent->currentState.eType - ET_EVENTS;
  } else {
    // check for events riding with another entity
    if ( cent->currentState.event == cent->previousEvent ) {
      return;
    }
    cent->previousEvent = cent->currentState.event;
    if ( ( cent->currentState.event & ~EV_EVENT_BITS ) == 0 ) {
      return;
    }
  }

  // calculate the position at exactly the frame time
  BG_EvaluateTrajectory( &cent->currentState.pos, cg.snap->serverTime, cent->lerpOrigin );
  CG_SetEntitySoundPosition( cent );

  CG_EntityEvent( cent, cent->lerpOrigin );
}