]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_client.qc
func_door fixes
[divverent/nexuiz.git] / data / qcsrc / server / cl_client.qc
1 // let's abuse an existing field
2 #define SPAWNPOINT_SCORE frags
3
4 .float wantswelcomemessage;
5 .string netname_previous;
6
7 void spawnfunc_info_player_survivor (void)
8 {
9         spawnfunc_info_player_deathmatch();
10 }
11
12 void spawnfunc_info_player_start (void)
13 {
14         spawnfunc_info_player_deathmatch();
15 }
16
17 void spawnfunc_info_player_deathmatch (void)
18 {
19         self.classname = "info_player_deathmatch";
20         relocate_spawnpoint();
21 }
22
23 void spawnpoint_use()
24 {
25         if(teams_matter)
26         if(have_team_spawns)
27         {
28                 self.team = activator.team;
29                 some_spawn_has_been_used = 1;
30         }
31 };
32
33 // Returns:
34 //   -1 if a spawn can't be used
35 //   otherwise, a weight of the spawnpoint
36 float Spawn_Score(entity spot, entity playerlist, float teamcheck)
37 {
38         float shortest, thisdist;
39         entity player;
40
41         // filter out spots for the wrong team
42         if(teamcheck)
43         if(spot.team != teamcheck)
44                 return -1;
45
46         if(race_spawns)
47                 if(spot.target == "")
48                         return -1;
49
50         // filter out spots for assault
51         if(spot.target != "") {
52                 local entity ent;
53                 float good;
54                 ent = find(world, targetname, spot.target);
55                 while(ent) {
56                         if(ent.classname == "target_objective")
57                         {
58                                 if(ent.health < 0 || ent.health >= ASSAULT_VALUE_INACTIVE)
59                                         return -1;
60                                 good = 1;
61                         }
62                         else if(ent.classname == "trigger_race_checkpoint")
63                         {
64                                 if(self.classname == "player") // spectators may spawn everywhere
65                                 {
66                                         if(g_race_qualifying)
67                                         {
68                                                 // spawn at first
69                                                 if(ent.race_checkpoint != 0)
70                                                         return -1;
71                                                 if(spot.race_place != race_lowest_place_spawn)
72                                                         return -1;
73                                         }
74                                         else
75                                         {
76                                                 if(ent.race_checkpoint != race_PreviousCheckpoint(self.race_checkpoint))
77                                                         return -1;
78                                                 float pl;
79                                                 pl = self.race_place;
80                                                 if(pl > race_highest_place_spawn)
81                                                         pl = 0;
82                                                 if(spot.race_place != pl)
83                                                         return -1;
84                                         }
85                                 }
86                                 good = 1;
87                         }
88                         else
89                         {
90                         }
91                         ent = find(ent, targetname, spot.target);
92                 }
93
94                 if(!good)
95                         return -1;
96         }
97
98         player = playerlist;
99         shortest = vlen(world.maxs - world.mins);
100         for(player = playerlist; player; player = player.chain)
101                 if (player != self)
102                 {
103                         thisdist = vlen(player.origin - spot.origin);
104                         if (thisdist < shortest)
105                                 shortest = thisdist;
106                 }
107         return shortest;
108 }
109
110 float spawn_allbad;
111 float spawn_allgood;
112 entity Spawn_FilterOutBadSpots(entity firstspot, entity playerlist, float mindist, float teamcheck)
113 {
114         local entity spot, spotlist, spotlistend;
115         spawn_allgood = TRUE;
116         spawn_allbad = TRUE;
117
118         spotlist = world;
119         spotlistend = world;
120
121         for(spot = firstspot; spot; spot = spot.chain)
122         {
123                 spot.SPAWNPOINT_SCORE = Spawn_Score(spot, playerlist, teamcheck);
124
125                 if(cvar("spawn_debugview"))
126                 {
127                         setmodel(spot, "models/runematch/rune.mdl");
128                         if(spot.SPAWNPOINT_SCORE < mindist)
129                         {
130                                 spot.colormod = '1 0 0';
131                                 spot.scale = 1;
132                         }
133                         else
134                         {
135                                 spot.colormod = '0 1 0';
136                                 spot.scale = spot.SPAWNPOINT_SCORE / mindist;
137                         }
138                 }
139
140                 if(spot.SPAWNPOINT_SCORE >= 0) // spawning allowed here
141                 {
142                         if(spot.SPAWNPOINT_SCORE < mindist)
143                         {
144                                 // too short distance
145                                 spawn_allgood = FALSE;
146                         }
147                         else 
148                         {
149                                 // perfect
150                                 spawn_allbad = FALSE;
151
152                                 if(spotlistend)
153                                         spotlistend.chain = spot;
154                                 spotlistend = spot;
155                                 if(!spotlist)
156                                         spotlist = spot;
157
158                                 /*
159                                 if(teamcheck)
160                                 if(spot.team != teamcheck)
161                                         error("invalid spawn added");
162
163                                 print("added ", etos(spot), "\n");
164                                 */
165                         }
166                 }
167         }
168         if(spotlistend)
169                 spotlistend.chain = world;
170
171         /*
172                 entity e;
173                 if(teamcheck)
174                         for(e = spotlist; e; e = e.chain)
175                         {
176                                 print("seen ", etos(e), "\n");
177                                 if(e.team != teamcheck)
178                                         error("invalid spawn found");
179                         }
180         */
181
182         return spotlist;
183 }
184
185 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
186 {
187         // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
188         // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
189         local entity spot;
190
191         RandomSelection_Init();
192         for(spot = firstspot; spot; spot = spot.chain)
193                 RandomSelection_Add(spot, 0, pow(bound(lower, spot.SPAWNPOINT_SCORE, upper), exponent) * spot.cnt, spot.SPAWNPOINT_SCORE >= lower);
194
195         return RandomSelection_chosen_ent;
196 }
197
198 /*
199 =============
200 SelectSpawnPoint
201
202 Finds a point to respawn
203 =============
204 */
205 entity SelectSpawnPoint (float anypoint)
206 {
207         local float teamcheck;
208         local entity firstspot_new;
209         local entity spot, firstspot, playerlist;
210
211         spot = find (world, classname, "testplayerstart");
212         if (spot)
213                 return spot;
214
215         teamcheck = 0;
216
217         if(!anypoint && have_team_spawns)
218                 teamcheck = self.team;
219
220         // get the list of players
221         playerlist = findchain(classname, "player");
222         // get the entire list of spots
223         firstspot = findchain(classname, "info_player_deathmatch");
224         // filter out the bad ones
225         // (note this returns the original list if none survived)
226         firstspot_new = Spawn_FilterOutBadSpots(firstspot, playerlist, 100, teamcheck);
227         if(!firstspot_new)
228                 firstspot_new = Spawn_FilterOutBadSpots(firstspot, playerlist, -1, teamcheck);
229         firstspot = firstspot_new;
230
231         // there is 50/50 chance of choosing a random spot or the furthest spot
232         // (this means that roughly every other spawn will be furthest, so you
233         // usually won't get fragged at spawn twice in a row)
234         if (arena_roundbased)
235         {
236                 firstspot_new = Spawn_FilterOutBadSpots(firstspot, playerlist, 800, teamcheck);
237                 if(firstspot_new)
238                         firstspot = firstspot_new;
239                 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
240         }
241         else if (random() > cvar("g_spawn_furthest"))
242                 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
243         else
244                 spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
245
246         if(cvar("spawn_debugview"))
247         {
248                 print("spot mindistance: ", ftos(spot.SPAWNPOINT_SCORE), "\n");
249
250                 entity e;
251                 if(teamcheck)
252                         for(e = firstspot; e; e = e.chain)
253                                 if(e.team != teamcheck)
254                                         error("invalid spawn found");
255         }
256
257         if (!spot)
258         {
259                 if(cvar("spawn_debug"))
260                         GotoNextMap();
261                 else
262                 {
263                         if(some_spawn_has_been_used)
264                                 return world; // team can't spawn any more, because of actions of other team
265                         else
266                                 error("Cannot find a spawn point - please fix the map!");
267                 }
268         }
269
270         return spot;
271 }
272
273 /*
274 =============
275 CheckPlayerModel
276
277 Checks if the argument string can be a valid playermodel.
278 Returns a valid one in doubt.
279 =============
280 */
281 string FallbackPlayerModel = "models/player/marine.zym";
282 string CheckPlayerModel(string plyermodel) {
283         if(strlen(plyermodel) < 4)
284                 return FallbackPlayerModel;
285         if( substring(plyermodel,0,14) != "models/player/")
286                 return FallbackPlayerModel;
287         else if(cvar("sv_servermodelsonly"))
288         {
289                 if(substring(plyermodel,strlen(plyermodel)-4,4) != ".zym")
290                 if(substring(plyermodel,strlen(plyermodel)-4,4) != ".dpm")
291                 if(substring(plyermodel,strlen(plyermodel)-4,4) != ".md3")
292                 if(substring(plyermodel,strlen(plyermodel)-4,4) != ".psk")
293                         return FallbackPlayerModel;
294                 if(plyermodel != strtolower(plyermodel))
295                         return FallbackPlayerModel;
296                 if(!fexists(plyermodel))
297                         return FallbackPlayerModel;
298         }
299         return plyermodel;
300 }
301
302 /*
303 =============
304 Client_customizeentityforclient
305
306 LOD reduction
307 =============
308 */
309 float Client_customizeentityforclient()
310 {
311 #ifdef ALLOW_VARIABLE_LOD
312         // self: me
313         // other: the player viewing me
314         float distance;
315         float f;
316
317         if(self.flags & FL_NOTARGET) // we don't need LOD for spectators
318                 return TRUE;
319
320         if(other.cvar_cl_playerdetailreduction <= 0)
321         {
322                 if(other.cvar_cl_playerdetailreduction <= -2)
323                         self.modelindex = self.modelindex_lod2;
324                 else if(other.cvar_cl_playerdetailreduction <= -1)
325                         self.modelindex = self.modelindex_lod1;
326                 else
327                         self.modelindex = self.modelindex_lod0;
328         }
329         else
330         {
331                 distance = vlen(self.origin - other.origin);
332                 f = (distance + 100.0) * other.cvar_cl_playerdetailreduction;
333                 if(f > 10000)
334                         self.modelindex = self.modelindex_lod2;
335                 else if(f > 5000)
336                         self.modelindex = self.modelindex_lod1;
337                 else
338                         self.modelindex = self.modelindex_lod0;
339         }
340 #endif
341
342         return TRUE;
343 }
344
345 void UpdatePlayerSounds();
346 void setmodel_lod(entity e, string modelname)
347 {
348 #ifdef ALLOW_VARIABLE_LOD
349         string s;
350
351         // FIXME: this only supports 3-letter extensions
352         s = strcat(substring(modelname, 0, strlen(modelname) - 4), "_1", substring(modelname, 0, strlen(modelname) - 4));
353         if(fexists(s))
354         {
355                 precache_model(s);
356                 setmodel(e, s); // players have high precision
357                 self.modelindex_lod1 = self.modelindex;
358         }
359         else
360                 self.modelindex_lod1 = -1;
361
362         s = strcat(substring(modelname, 0, strlen(modelname) - 4), "_2", substring(modelname, 0, strlen(modelname) - 4));
363         if(fexists(s))
364         {
365                 precache_model(s);
366                 setmodel(e, s); // players have high precision
367                 self.modelindex_lod2 = self.modelindex;
368         }
369         else
370                 self.modelindex_lod2 = -1;
371
372         precache_model(modelname);
373         setmodel(e, modelname); // players have high precision
374         self.modelindex_lod0 = self.modelindex;
375
376         if(self.modelindex_lod1 < 0)
377                 self.modelindex_lod1 = self.modelindex;
378
379         if(self.modelindex_lod2 < 0)
380                 self.modelindex_lod2 = self.modelindex;
381 #else
382         precache_model(modelname);
383         setmodel(e, modelname); // players have high precision
384 #endif
385         player_setupanimsformodel();
386         UpdatePlayerSounds();
387 }
388
389 /*
390 =============
391 PutObserverInServer
392
393 putting a client as observer in the server
394 =============
395 */
396 void PutObserverInServer (void)
397 {
398         entity  spot;
399
400         race_PreSpawnObserver();
401
402         spot = SelectSpawnPoint (TRUE);
403         if(!spot)
404                 error("No spawnpoints for observers?!?\n");
405         RemoveGrapplingHook(self); // Wazat's Grappling Hook
406
407         if(clienttype(self) == CLIENTTYPE_REAL)
408         {
409                 msg_entity = self;
410                 WriteByte(MSG_ONE, SVC_SETVIEW);
411                 WriteEntity(MSG_ONE, self);
412         }
413
414         DropAllRunes(self);
415         kh_Key_DropAll(self, TRUE);
416
417         Portal_ClearAll(self);
418
419         if(self.flagcarried)
420                 DropFlag(self.flagcarried, world, world);
421
422         WaypointSprite_PlayerDead();
423         
424         if(self.killcount != -666)
425         {
426                 if(g_lms)
427                 {
428                         if(PlayerScore_Add(self, SP_LMS_RANK, 0) > 0)
429                                 bprint ("^4", self.netname, "^4 has no more lives left\n");
430                         else
431                                 bprint ("^4", self.netname, "^4 is spectating now\n"); // TODO turn this into a proper forfeit?
432                 }
433                 else
434                         bprint ("^4", self.netname, "^4 is spectating now\n");
435         }
436
437         PlayerScore_Clear(self); // clear scores when needed
438
439         self.spectatortime = time;
440         
441         self.classname = "observer";
442         self.iscreature = FALSE;
443         self.health = -666;
444         self.takedamage = DAMAGE_NO;
445         self.solid = SOLID_NOT;
446         self.movetype = MOVETYPE_NOCLIP;
447         self.flags = FL_CLIENT | FL_NOTARGET;
448         self.armorvalue = 666;
449         self.effects = 0;
450         self.armorvalue = cvar("g_balance_armor_start");
451         self.pauserotarmor_finished = 0;
452         self.pauserothealth_finished = 0;
453         self.pauseregen_finished = 0;
454         self.damageforcescale = 0;
455         self.death_time = 0;
456         self.dead_frame = 0;
457         self.alpha = 0;
458         self.scale = 0;
459         self.fade_time = 0;
460         self.pain_frame = 0;
461         self.pain_finished = 0;
462         self.strength_finished = 0;
463         self.invincible_finished = 0;
464         self.pushltime = 0;
465         self.think = SUB_Null;
466         self.nextthink = 0;
467         self.hook_time = 0;
468         self.runes = 0;
469         self.deadflag = DEAD_NO;
470         self.angles = spot.angles;
471         self.angles_z = 0;
472         self.fixangle = TRUE;
473         self.crouch = FALSE;
474
475         self.view_ofs = PL_VIEW_OFS;
476         setorigin (self, spot.origin);
477         setsize (self, '0 0 0', '0 0 0');
478         self.oldorigin = self.origin;
479         self.items = 0;
480         self.weapons = 0;
481         self.model = "";
482         self.modelindex = 0;
483         self.weapon = 0;
484         self.weaponmodel = "";
485         self.weaponentity = world;
486         self.killcount = -666;
487         self.velocity = '0 0 0';
488         self.avelocity = '0 0 0';
489         self.punchangle = '0 0 0';
490         self.punchvector = '0 0 0';
491         self.oldvelocity = self.velocity;
492         self.customizeentityforclient = Client_customizeentityforclient;
493         self.wantswelcomemessage = 1;
494
495         if(g_arena)
496         {
497                 if(self.version_mismatch)
498                 {
499                         Spawnqueue_Unmark(self);
500                         Spawnqueue_Remove(self);
501                 }
502                 else
503                 {
504                         Spawnqueue_Insert(self);
505                 }
506         }
507         else if(g_lms)
508         {
509                 // Only if the player cannot play at all
510                 if(PlayerScore_Add(self, SP_LMS_RANK, 0) == 666)
511                         self.frags = -666;
512                 else
513                         self.frags = -667;
514         }
515         else
516                 self.frags = -666;
517 }
518
519 float RestrictSkin(float s)
520 {
521         if(!teams_matter)
522                 return s;
523         if(s == 6)
524                 return 6;
525         return mod(s, 3);
526 }
527
528 void FixPlayermodel()
529 {
530         local string defaultmodel;
531         local float defaultskin;
532         local vector m1, m2;
533
534         defaultmodel = "";
535
536         if(cvar("sv_defaultcharacter") == 1) {
537                 defaultskin = 0;
538
539                 if(teams_matter)
540                 {
541                         defaultmodel = cvar_string(strcat("sv_defaultplayermodel_", Team_ColorNameLowerCase(self.team)));
542                         defaultskin = cvar(strcat("sv_defaultplayerskin_", Team_ColorNameLowerCase(self.team)));
543                 }
544
545                 if(defaultmodel == "")
546                 {
547                         defaultmodel = cvar_string("sv_defaultplayermodel");
548                         defaultskin = cvar("sv_defaultplayerskin");
549                 }
550         }
551
552         if(defaultmodel != "")
553         {
554                 if (defaultmodel != self.model)
555                 {
556                         m1 = self.mins;
557                         m2 = self.maxs;
558                         setmodel_lod (self, defaultmodel);
559                         setsize (self, m1, m2);
560                 }
561
562                 self.skin = defaultskin;
563         } else {
564                 if (self.playermodel != self.model)
565                 {
566                         self.playermodel = CheckPlayerModel(self.playermodel);
567                         m1 = self.mins;
568                         m2 = self.maxs;
569                         setmodel_lod (self, self.playermodel);
570                         setsize (self, m1, m2);
571                 }
572
573                 self.skin = RestrictSkin(stof(self.playerskin));
574         }
575
576         if(!teams_matter)
577                 if(strlen(cvar_string("sv_defaultplayercolors")))
578                         if(self.clientcolors != cvar("sv_defaultplayercolors"))
579                                 setcolor(self, cvar("sv_defaultplayercolors"));
580 }
581
582 /*
583 =============
584 PutClientInServer
585
586 Called when a client spawns in the server
587 =============
588 */
589 //void() ctf_playerchanged;
590 void PutClientInServer (void)
591 {
592         if(clienttype(self) == CLIENTTYPE_BOT)
593         {
594                 self.classname = "player";
595         }
596         else if(clienttype(self) == CLIENTTYPE_REAL)
597         {
598                 msg_entity = self;
599                 WriteByte(MSG_ONE, SVC_SETVIEW);
600                 WriteEntity(MSG_ONE, self);
601         }
602
603         // player is dead and becomes observer
604         // FIXME fix LMS scoring for new system
605         if(g_lms)
606         {
607                 if(PlayerScore_Add(self, SP_LMS_RANK, 0) > 0)
608                         self.classname = "observer";
609         }
610
611         if(g_arena)
612         if(!self.spawned)
613                 self.classname = "observer";
614
615         if(self.classname == "player") {
616                 entity  spot;
617
618                 race_PreSpawn();
619
620                 spot = SelectSpawnPoint (FALSE);
621                 if(!spot)
622                 {
623                         centerprint(self, "Sorry, no spawnpoints available!\nHope your team can fix it...");
624                         return; // spawn failed
625                 }
626
627                 RemoveGrapplingHook(self); // Wazat's Grappling Hook
628
629                 self.classname = "player";
630                 self.iscreature = TRUE;
631                 self.movetype = MOVETYPE_WALK;
632                 self.solid = SOLID_SLIDEBOX;
633                 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
634                 if(independent_players)
635                         MAKE_INDEPENDENT_PLAYER(self);
636                 self.flags = FL_CLIENT;
637                 self.takedamage = DAMAGE_AIM;
638                 if(g_minstagib)
639                         self.effects = EF_FULLBRIGHT;
640                 else
641                         self.effects = 0;
642                 self.air_finished = time + 12;
643                 self.dmg = 2;
644
645                 if(inWarmupStage)
646                 {
647                         self.ammo_shells = warmup_start_ammo_shells;
648                         self.ammo_nails = warmup_start_ammo_nails;
649                         self.ammo_rockets = warmup_start_ammo_rockets;
650                         self.ammo_cells = warmup_start_ammo_cells;
651                         self.health = warmup_start_health;
652                         self.armorvalue = warmup_start_armorvalue;
653                         self.weapons = warmup_start_weapons;
654                 }
655                 else
656                 {
657                         self.ammo_shells = start_ammo_shells;
658                         self.ammo_nails = start_ammo_nails;
659                         self.ammo_rockets = start_ammo_rockets;
660                         self.ammo_cells = start_ammo_cells;
661                         self.health = start_health;
662                         self.armorvalue = start_armorvalue;
663                         self.weapons = start_weapons;
664                 }
665                 self.items = start_items;
666                 self.switchweapon = w_getbestweapon(self);
667                 self.cnt = self.switchweapon;
668                 self.weapon = 0;
669                 self.jump_interval = time;
670
671                 self.spawnshieldtime = time + cvar("g_spawnshieldtime");
672                 self.pauserotarmor_finished = time + cvar("g_balance_pause_armor_rot_spawn");
673                 self.pauserothealth_finished = time + cvar("g_balance_pause_health_rot_spawn");
674                 self.pauseregen_finished = time + cvar("g_balance_pause_health_regen_spawn");
675                 //extend the pause of rotting if client was reset at the beginning of the countdown
676                 if(!cvar("sv_ready_restart_after_countdown") && time < game_starttime) { // TODO why is this cvar NOTted?
677                         self.spawnshieldtime += game_starttime - time;
678                         self.pauserotarmor_finished += game_starttime - time;
679                         self.pauserothealth_finished += game_starttime - time;
680                         self.pauseregen_finished += game_starttime - time;
681                 }
682                 self.damageforcescale = 2;
683                 self.death_time = 0;
684                 self.dead_frame = 0;
685                 self.alpha = 0;
686                 self.scale = 0;
687                 self.fade_time = 0;
688                 self.pain_frame = 0;
689                 self.pain_finished = 0;
690                 self.strength_finished = 0;
691                 self.invincible_finished = 0;
692                 self.pushltime = 0;
693                 //self.speed_finished = 0;
694                 //self.slowmo_finished = 0;
695                 // players have no think function
696                 self.think = SUB_Null;
697                 self.nextthink = 0;
698                 self.hook_time = 0;
699
700                 self.runes = 0;
701
702                 self.deadflag = DEAD_NO;
703
704                 self.angles = spot.angles;
705
706                 self.angles_z = 0; // never spawn tilted even if the spot says to
707                 self.fixangle = TRUE; // turn this way immediately
708                 self.velocity = '0 0 0';
709                 self.avelocity = '0 0 0';
710                 self.punchangle = '0 0 0';
711                 self.punchvector = '0 0 0';
712                 self.oldvelocity = self.velocity;
713
714                 msg_entity = self;
715                 WRITESPECTATABLE_MSG_ONE({
716                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
717                         WriteByte(MSG_ONE, TE_CSQC_SPAWN);
718                 });
719
720                 self.customizeentityforclient = Client_customizeentityforclient;
721
722                 self.model = "";
723                 FixPlayermodel();
724
725                 self.crouch = FALSE;
726                 self.view_ofs = PL_VIEW_OFS;
727                 setsize (self, PL_MIN, PL_MAX);
728                 self.spawnorigin = spot.origin;
729                 setorigin (self, spot.origin + '0 0 1' * (1 - self.mins_z - 24));
730                 // don't reset back to last position, even if new position is stuck in solid
731                 self.oldorigin = self.origin;
732
733                 if(g_arena)
734                 {
735                         Spawnqueue_Remove(self);
736                         Spawnqueue_Mark(self);
737                 }
738
739                 self.event_damage = PlayerDamage;
740
741                 self.bot_attack = TRUE;
742
743                 self.statdraintime = time + 5;
744                 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = 0;
745
746                 if(self.killcount == -666) {
747                         PlayerScore_Clear(self);
748                         self.killcount = 0;
749                         self.frags = 0;
750                 }
751
752                 self.cnt = WEP_LASER;
753                 self.nixnex_lastchange_id = -1;
754
755                 CL_SpawnWeaponentity();
756                 self.alpha = default_player_alpha;
757                 self.colormod = '1 1 1' * cvar("g_player_brightness");
758                 self.exteriorweaponentity.alpha = default_weapon_alpha;
759
760                 self.lms_nextcheck = time + cvar("g_lms_campcheck_interval")*2;
761                 self.lms_traveled_distance = 0;
762                 self.speedrunning = FALSE;
763
764                 race_PostSpawn(spot);
765
766                 if(cvar("spawn_debug"))
767                 {
768                         sprint(self, strcat("spawnpoint origin:  ", vtos(spot.origin), "\n"));
769                         remove(spot);   // usefull for checking if there are spawnpoints, that let drop through the floor
770                 }
771
772                 //stuffcmd(self, "chase_active 0");
773                 //stuffcmd(self, "set viewsize $tmpviewsize \n");
774
775                 if (cvar("g_spawnsound"))
776                         sound (self, CHAN_TRIGGER, "misc/spawn.wav", VOL_BASE, ATTN_NORM);
777
778                 if(g_assault) {
779                         if(self.team == assault_attacker_team)
780                                 centerprint(self, "You are attacking!\n");
781                         else
782                                 centerprint(self, "You are defending!\n");
783                 }
784
785         } else if(self.classname == "observer") {
786                 PutObserverInServer ();
787         }
788
789         //if(g_ctf)
790         //      ctf_playerchanged();
791 }
792
793 /*
794 =============
795 SendCSQCInfo
796
797 Send whatever CSQC needs NOW and cannot wait for SendServerInfo to happen...
798 =============
799 */
800 void SendCSQCInfo(void)
801 {
802         float i;
803         if(clienttype(self) != CLIENTTYPE_REAL)
804                 return;
805         msg_entity = self;
806         WriteByte(MSG_ONE, SVC_TEMPENTITY);
807         WriteByte(MSG_ONE, TE_CSQC_INIT);
808         WriteShort(MSG_ONE, CSQC_REVISION);
809         WriteByte(MSG_ONE, maxclients);
810         for(i = 1; i <= 24; ++i)
811                 WriteByte(MSG_ONE, (get_weaponinfo(i)).impulse + 1);
812         WriteCoord(MSG_ONE, hook_shotorigin_x);
813         WriteCoord(MSG_ONE, hook_shotorigin_y);
814         WriteCoord(MSG_ONE, hook_shotorigin_z);
815 }
816
817 /*
818 =============
819 SetNewParms
820 =============
821 */
822 void SetNewParms (void)
823 {
824         // initialize parms for a new player
825         parm1 = -(86400 * 366);
826 }
827
828 /*
829 =============
830 SetChangeParms
831 =============
832 */
833 void SetChangeParms (void)
834 {
835         // save parms for level change
836         parm1 = self.parm_idlesince - time;
837 }
838
839 /*
840 =============
841 DecodeLevelParms
842 =============
843 */
844 void DecodeLevelParms (void)
845 {
846         // load parms
847         self.parm_idlesince = parm1;
848         if(self.parm_idlesince == -(86400 * 366))
849                 self.parm_idlesince = time;
850 }
851
852 /*
853 =============
854 ClientKill
855
856 Called when a client types 'kill' in the console
857 =============
858 */
859
860 void ClientKill_Now_TeamChange()
861 {
862         if(self.killindicator_teamchange == -1)
863         {
864                 self.team = -1;
865                 JoinBestTeam( self, FALSE, FALSE );
866         }
867         else
868         {
869                 SV_ChangeTeam(self.killindicator_teamchange - 1);
870         }
871 }
872
873 void ClientKill_Now()
874 {
875         if(self.killindicator_teamchange)
876                 ClientKill_Now_TeamChange();
877
878         // in any case:
879         Damage(self, self, self, 100000, DEATH_KILL, self.origin, '0 0 0');
880
881         if(self.killindicator)
882         {
883                 dprint("Cleaned up after a leaked kill indicator.\n");
884                 remove(self.killindicator);
885                 self.killindicator = world;
886         }
887 }
888 void KillIndicator_Think()
889 {
890         if (!self.owner.modelindex)
891         {
892                 self.owner.killindicator = world;
893                 remove(self);
894                 return;
895         }
896
897         if(self.cnt <= 0)
898         {
899                 self = self.owner;
900                 ClientKill_Now(); // no oldself needed
901                 return;
902         }
903         else
904         {
905                 if(self.cnt <= 10)
906                         setmodel(self, strcat("models/sprites/", ftos(self.cnt), ".spr32"));
907                 if(clienttype(self.owner) == CLIENTTYPE_REAL)
908                 {
909                         if(self.cnt <= 10)
910                                 announce(self.owner, strcat("announcer/robotic/", ftos(self.cnt), ".ogg"));
911                         if(self.owner.killindicator_teamchange)
912                         {
913                                 if(self.owner.killindicator_teamchange == -1)
914                                         centerprint(self.owner, strcat("Changing team in ", ftos(self.cnt), " seconds"));
915                                 else
916                                         centerprint(self.owner, strcat("Changing to ", ColoredTeamName(self.owner.killindicator_teamchange), " in ", ftos(self.cnt), " seconds"));
917                         }
918                         else
919                                 centerprint(self.owner, strcat("^1Suicide in ", ftos(self.cnt), " seconds"));
920                 }
921                 self.nextthink = time + 1;
922                 self.cnt -= 1;
923         }
924 }
925
926 void ClientKill_TeamChange (float targetteam) // 0 = don't change, -1 = auto
927 {
928         float killtime;
929         entity e;
930         killtime = cvar("g_balance_kill_delay");
931
932         self.killindicator_teamchange = targetteam;
933
934         if(!self.killindicator)
935         {
936                 if(killtime <= 0 || !self.modelindex || self.deadflag != DEAD_NO)
937                 {
938                         ClientKill_Now();
939                 }
940                 else
941                 {
942                         self.killindicator = spawn();
943                         self.killindicator.owner = self;
944                         self.killindicator.scale = 0.5;
945                         setattachment(self.killindicator, self, "");
946                         setorigin(self.killindicator, '0 0 52');
947                         self.killindicator.think = KillIndicator_Think;
948                         self.killindicator.nextthink = time + (self.lip) * 0.05;
949                         self.killindicator.cnt = ceil(killtime);
950                         self.killindicator.count = bound(0, ceil(killtime), 10);
951                         sprint(self, strcat("^1You'll be dead in ", ftos(self.killindicator.cnt), " seconds\n"));
952
953                         for(e = world; (e = find(e, classname, "body")) != world; )
954                         {
955                                 if(e.enemy != self)
956                                         continue;
957                                 e.killindicator = spawn();
958                                 e.killindicator.owner = e;
959                                 e.killindicator.scale = 0.5;
960                                 setattachment(e.killindicator, e, "");
961                                 setorigin(e.killindicator, '0 0 52');
962                                 e.killindicator.think = KillIndicator_Think;
963                                 e.killindicator.nextthink = time + (e.lip) * 0.05;
964                                 e.killindicator.cnt = ceil(killtime);
965                         }
966                         self.lip = 0;
967                 }
968         }
969         if(self.killindicator)
970         {
971                 if(targetteam)
972                         self.killindicator.colormod = TeamColor(targetteam);
973                 else
974                         self.killindicator.colormod = '0 0 0';
975         }
976 }
977
978 void ClientKill (void)
979 {
980         ClientKill_TeamChange(0);
981 }
982
983 void DoTeamChange(float destteam)
984 {
985         float t, c0;
986         if(!cvar("teamplay"))
987         {
988                 if(destteam >= 0)
989                         SetPlayerColors(self, destteam);
990                 return;
991         }
992         if(self.classname == "player")
993         if(destteam == -1)
994         {
995                 CheckAllowedTeams(self);
996                 t = FindSmallestTeam(self, TRUE);
997                 switch(self.team)
998                 {
999                         case COLOR_TEAM1: c0 = c1; break;
1000                         case COLOR_TEAM2: c0 = c2; break;
1001                         case COLOR_TEAM3: c0 = c3; break;
1002                         case COLOR_TEAM4: c0 = c4; break;
1003                         default:          c0 = 999;
1004                 }
1005                 switch(t)
1006                 {
1007                         case 1:
1008                                 if(c0 > c1)
1009                                         destteam = COLOR_TEAM1;
1010                                 break;
1011                         case 2:
1012                                 if(c0 > c2)
1013                                         destteam = COLOR_TEAM2;
1014                                 break;
1015                         case 3:
1016                                 if(c0 > c3)
1017                                         destteam = COLOR_TEAM3;
1018                                 break;
1019                         case 4:
1020                                 if(c0 > c4)
1021                                         destteam = COLOR_TEAM4;
1022                                 break;
1023                 }
1024                 if(destteam == -1)
1025                         return;
1026         }
1027         if(destteam == self.team && !self.killindicator)
1028                 return;
1029         ClientKill_TeamChange(destteam);
1030 }
1031
1032 void FixClientCvars(entity e)
1033 {
1034         // send prediction settings to the client
1035         stuffcmd(e, "\nin_bindmap 0 0\n");
1036         /*
1037          * we no longer need to stuff this. Remove this comment block if you feel 
1038          * 2.3 and higher (or was it 2.2.3?) don't need these any more
1039         stuffcmd(e, strcat("cl_gravity ", ftos(cvar("sv_gravity")), "\n"));
1040         stuffcmd(e, strcat("cl_movement_accelerate ", ftos(cvar("sv_accelerate")), "\n"));
1041         stuffcmd(e, strcat("cl_movement_friction ", ftos(cvar("sv_friction")), "\n"));
1042         stuffcmd(e, strcat("cl_movement_maxspeed ", ftos(cvar("sv_maxspeed")), "\n"));
1043         stuffcmd(e, strcat("cl_movement_airaccelerate ", ftos(cvar("sv_airaccelerate")), "\n"));
1044         stuffcmd(e, strcat("cl_movement_maxairspeed ", ftos(cvar("sv_maxairspeed")), "\n"));
1045         stuffcmd(e, strcat("cl_movement_stopspeed ", ftos(cvar("sv_stopspeed")), "\n"));
1046         stuffcmd(e, strcat("cl_movement_jumpvelocity ", ftos(cvar("sv_jumpvelocity")), "\n"));
1047         stuffcmd(e, strcat("cl_movement_stepheight ", ftos(cvar("sv_stepheight")), "\n"));
1048         stuffcmd(e, strcat("set cl_movement_friction_on_land ", ftos(cvar("sv_friction_on_land")), "\n"));
1049         stuffcmd(e, strcat("set cl_movement_airaccel_qw ", ftos(cvar("sv_airaccel_qw")), "\n"));
1050         stuffcmd(e, strcat("set cl_movement_airaccel_sideways_friction ", ftos(cvar("sv_airaccel_sideways_friction")), "\n"));
1051         stuffcmd(e, "cl_movement_edgefriction 1\n");
1052          */
1053 }
1054
1055 .float fog_set;
1056 void SendFog()
1057 {
1058         if(clienttype(self) == CLIENTTYPE_REAL)
1059         if(world.fog) // NOT string_null!
1060         {
1061                 if(!self.fog_set)
1062                 {
1063                         if(sv_foginterval)
1064                         {
1065                                 msg_entity = self;
1066                                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1067                                 WriteByte(MSG_ONE, TE_CSQC_FOG);
1068                                 WriteCoord(MSG_ONE, sv_foginterval);
1069                                 WriteString(MSG_ONE, world.fog);
1070                         }
1071                         else
1072                                 // set it once
1073                                 stuffcmd(self, strcat("\nfog ", world.fog, "\nr_fog_exp2 0\nr_drawfog 1\n"));
1074
1075                         self.fog_set = 1;
1076                 }
1077         }
1078 }
1079
1080 /*
1081 =============
1082 ClientConnect
1083
1084 Called when a client connects to the server
1085 =============
1086 */
1087 //void ctf_clientconnect();
1088 string ColoredTeamName(float t);
1089 void DecodeLevelParms (void);
1090 //void dom_player_join_team(entity pl);
1091 void ClientConnect (void)
1092 {
1093         local string s;
1094
1095         if(self.flags & FL_CLIENT)
1096         {
1097                 print("Warning: ClientConnect, but already connected!\n");
1098                 return;
1099         }
1100
1101         if(Ban_MaybeEnforceBan(self))
1102                 return;
1103
1104         DecodeLevelParms();
1105
1106         self.classname = "player_joining";
1107
1108         self.flags = self.flags | FL_CLIENT;
1109         self.version_nagtime = time + 10 + random() * 10;
1110
1111         if(player_count<0)
1112         {
1113                 dprint("BUG player count is lower than zero, this cannot happen!\n");
1114                 player_count = 0;
1115         }
1116
1117         PlayerScore_Attach(self);
1118
1119         bot_clientconnect();
1120
1121         race_PreSpawnObserver();
1122
1123         //if(g_domination)
1124         //      dom_player_join_team(self);
1125
1126         //JoinBestTeam(self, FALSE, FALSE);
1127         //
1128         if((cvar("sv_spectate") == 1 && !g_lms) || cvar("g_campaign")) {
1129                 self.classname = "observer";
1130         } else {
1131                 self.classname = "player";
1132                 campaign_bots_may_start = 1;
1133         }
1134
1135         self.playerid = (playerid_last = playerid_last + 1);
1136         if(cvar("sv_eventlog"))
1137         {
1138                 if(clienttype(self) == CLIENTTYPE_REAL)
1139                         GameLogEcho(strcat(":join:", ftos(self.playerid), ":", ftos(num_for_edict(self)), ":", self.netaddress, ":", self.netname));
1140                 else
1141                         GameLogEcho(strcat(":join:", ftos(self.playerid), ":", ftos(num_for_edict(self)), ":bot:", self.netname));
1142                 s = strcat(":team:", ftos(self.playerid), ":");
1143                 s = strcat(s, ftos(self.team));
1144                 GameLogEcho(s);
1145         }
1146         self.netname_previous = strzone(self.netname);
1147
1148         //stuffcmd(self, "set tmpviewsize $viewsize \n");
1149
1150         bprint ("^4",self.netname);
1151         bprint ("^4 connected");
1152
1153         if(g_domination || g_ctf)
1154         {
1155                 bprint(" and joined the ");
1156                 bprint(ColoredTeamName(self.team));
1157         }
1158
1159         bprint("\n");
1160
1161         self.welcomemessage_time = 0;
1162
1163         stuffcmd(self, strcat(clientstuff, "\n"));
1164         stuffcmd(self, strcat("exec maps/", mapname, ".cfg\n"));
1165         stuffcmd(self, "cl_particles_reloadeffects\n");
1166
1167         FixClientCvars(self);
1168
1169         // spawnfunc_waypoint sprites
1170         WaypointSprite_InitClient(self);
1171
1172         // Wazat's grappling hook
1173         SetGrappleHookBindings();
1174
1175         // get autoswitch state from player when he toggles it
1176         stuffcmd(self, "alias autoswitch \"set cl_autoswitch $1 ; cmd autoswitch $1\"\n"); // default.cfg-ed in 2.4.1
1177
1178         // get version info from player
1179         stuffcmd(self, "cmd clientversion $gameversion\n");
1180
1181         // get other cvars from player
1182         GetCvars(0);
1183
1184         // set cvar for team scoreboard
1185         if (teams_matter)
1186         {
1187                 local float t;
1188                 t = cvar("teamplay");
1189                 // we have to stuff the correct teamplay value because if this is a listen server, this changes the teamplay mode of the server itself, which is bad
1190                 stuffcmd(self, strcat("set teamplay ", ftos(t), "\n"));
1191         }
1192         else
1193                 stuffcmd(self, "set teamplay 0\n");
1194
1195         // notify about available teams
1196         if(teamplay)
1197         {
1198                 CheckAllowedTeams(self);
1199                 t = 0; if(c1 >= 0) t |= 1; if(c2 >= 0) t |= 2; if(c3 >= 0) t |= 4; if(c4 >= 0) t |= 8;
1200                 stuffcmd(self, strcat("set _teams_available ", ftos(t), "\n"));
1201         }
1202         else
1203                 stuffcmd(self, "set _teams_available 0\n");
1204
1205         stuffcmd(self, strcat("set gametype ", ftos(game), "\n"));
1206
1207         if(g_arena)
1208         {
1209                 self.classname = "observer";
1210                 Spawnqueue_Insert(self);
1211         }
1212         /*else if(g_ctf)
1213         {
1214                 ctf_clientconnect();
1215         }*/
1216
1217         if(teams_matter)
1218                 attach_entcs();
1219
1220         bot_relinkplayerlist();
1221
1222         self.spectatortime = time;
1223         if(blockSpectators)
1224         {
1225                 sprint(self, strcat("^7You have to become a player within the next ", ftos(cvar("g_maxplayers_spectator_blocktime")), " seconds, otherwise you will be kicked, because spectators aren't allowed at this time!\n"));
1226         }
1227
1228         self.jointime = time;
1229         self.allowedTimeouts = cvar("sv_timeout_number");
1230
1231         if(clienttype(self) == CLIENTTYPE_REAL)
1232         {
1233                 sprint(self, strcat("nexuiz-csqc protocol ", ftos(CSQC_REVISION), "\n"));
1234                 SendCSQCInfo();
1235                 msg_entity = self;
1236                 if(mapvote_initialized && !cvar("g_maplist_textonly"))
1237                 {
1238                         MapVote_SendData(MSG_ONE);
1239                         MapVote_UpdateData(MSG_ONE);
1240                 }
1241                 ScoreInfo_Write(MSG_ONE);
1242         }
1243
1244         if(inWarmupStage)
1245         {
1246                 msg_entity = self;
1247                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1248                 WriteByte(MSG_ONE, TE_CSQC_WARMUP);
1249                 WriteByte(MSG_ONE, 1);
1250         }
1251
1252         if(g_lms)
1253         {
1254                 if(PlayerScore_Add(self, SP_LMS_LIVES, LMS_NewPlayerLives()) <= 0)
1255                 {
1256                         PlayerScore_Add(self, SP_LMS_RANK, 666);
1257                         self.frags = -666; // FIXME do we still need this?
1258                 }
1259         }
1260
1261         SendFog();
1262 }
1263
1264 /*
1265 =============
1266 ClientDisconnect
1267
1268 Called when a client disconnects from the server
1269 =============
1270 */
1271 .entity chatbubbleentity;
1272 .entity teambubbleentity;
1273 void ReadyCount();
1274 //void() ctf_clientdisconnect;
1275 void ClientDisconnect (void)
1276 {
1277         float save;
1278
1279         if not(self.flags & FL_CLIENT)
1280         {
1281                 print("Warning: ClientDisconnect without ClientConnect\n");
1282                 return;
1283         }
1284
1285         bot_clientdisconnect();
1286
1287         if(teams_matter)
1288                 detach_entcs();
1289         
1290         if(cvar("sv_eventlog"))
1291                 GameLogEcho(strcat(":part:", ftos(self.playerid)));
1292         bprint ("^4",self.netname);
1293         bprint ("^4 disconnected\n");
1294
1295         if (self.chatbubbleentity)
1296         {
1297                 remove (self.chatbubbleentity);
1298                 self.chatbubbleentity = world;
1299         }
1300
1301         if (self.teambubbleentity)
1302         {
1303                 remove (self.teambubbleentity);
1304                 self.teambubbleentity = world;
1305         }
1306
1307         if (self.killindicator)
1308         {
1309                 remove (self.killindicator);
1310                 self.killindicator = world;
1311         }
1312
1313         DropAllRunes(self);
1314         kh_Key_DropAll(self, TRUE);
1315
1316         Portal_ClearAll(self);
1317
1318         if(self.flagcarried)
1319                 DropFlag(self.flagcarried, world, world);
1320
1321         WaypointSprite_PlayerGone();
1322
1323         save = self.flags;
1324         self.flags = self.flags - (self.flags & FL_CLIENT);
1325         bot_relinkplayerlist();
1326         self.flags = save;
1327
1328         // remove laserdot
1329         if(self.weaponentity)
1330                 if(self.weaponentity.lasertarget)
1331                         remove(self.weaponentity.lasertarget);
1332
1333         if(g_arena)
1334         {
1335                 Spawnqueue_Unmark(self);
1336                 Spawnqueue_Remove(self);
1337         }
1338         /*if(g_ctf)
1339         {
1340                 ctf_clientdisconnect();
1341         }
1342         */
1343
1344         PlayerScore_Detach(self);
1345
1346         if(self.netname_previous)
1347                 strunzone(self.netname_previous);
1348
1349         ClearPlayerSounds();
1350
1351         // free cvars
1352         GetCvars(-1);
1353         self.playerid = 0;
1354
1355         ReadyCount();
1356 }
1357
1358 .float BUTTON_CHAT;
1359 void ChatBubbleThink()
1360 {
1361         self.nextthink = time;
1362         if (!self.owner.modelindex || self.owner.chatbubbleentity != self)
1363         {
1364                 self.owner.chatbubbleentity = world;
1365                 remove(self);
1366                 return;
1367         }
1368         setorigin(self, self.owner.origin + '0 0 15' + self.owner.maxs_z * '0 0 1');
1369         if (self.owner.BUTTON_CHAT && !self.owner.deadflag)
1370                 self.model = self.mdl;
1371         else
1372                 self.model = "";
1373 };
1374
1375 void UpdateChatBubble()
1376 {
1377         if (!self.modelindex)
1378                 return;
1379         // spawn a chatbubble entity if needed
1380         if (!self.chatbubbleentity)
1381         {
1382                 self.chatbubbleentity = spawn();
1383                 self.chatbubbleentity.owner = self;
1384                 self.chatbubbleentity.exteriormodeltoclient = self;
1385                 self.chatbubbleentity.think = ChatBubbleThink;
1386                 self.chatbubbleentity.nextthink = time;
1387                 setmodel(self.chatbubbleentity, "models/misc/chatbubble.spr"); // precision set below
1388                 setorigin(self.chatbubbleentity, self.origin + '0 0 15' + self.maxs_z * '0 0 1');
1389                 self.chatbubbleentity.mdl = self.chatbubbleentity.model;
1390                 self.chatbubbleentity.model = "";
1391                 self.chatbubbleentity.effects = EF_LOWPRECISION;
1392         }
1393 }
1394
1395
1396 void TeamBubbleThink()
1397 {
1398         self.nextthink = time;
1399         if (!self.owner.modelindex || self.owner.teambubbleentity != self)
1400         {
1401                 self.owner.teambubbleentity = world;
1402                 remove(self);
1403                 return;
1404         }
1405 //      setorigin(self, self.owner.origin + '0 0 15' + self.owner.maxs_z * '0 0 1');  // bandwidth hog. setattachment does this now
1406         if (self.owner.BUTTON_CHAT || self.owner.deadflag || self.owner.killindicator)
1407                 self.model = "";
1408         else
1409                 self.model = self.mdl;
1410
1411 };
1412
1413 float TeamBubble_customizeentityforclient()
1414 {
1415         return (self.owner != other && self.owner.team == other.team && other.killcount > -666);
1416 }
1417
1418 void UpdateTeamBubble()
1419 {
1420         if (!self.modelindex || !cvar("teamplay"))
1421                 return;
1422         // spawn a teambubble entity if needed
1423         if (!self.teambubbleentity && cvar("teamplay"))
1424         {
1425                 self.teambubbleentity = spawn();
1426                 self.teambubbleentity.owner = self;
1427                 self.teambubbleentity.exteriormodeltoclient = self;
1428                 self.teambubbleentity.think = TeamBubbleThink;
1429                 self.teambubbleentity.nextthink = time;
1430                 setmodel(self.teambubbleentity, "models/misc/teambubble.spr"); // precision set below
1431 //              setorigin(self.teambubbleentity, self.origin + '0 0 15' + self.maxs_z * '0 0 1');
1432                 setorigin(self.teambubbleentity, self.teambubbleentity.origin + '0 0 15' + self.maxs_z * '0 0 1');
1433                 setattachment(self.teambubbleentity, self, "");  // sticks to moving player better, also conserves bandwidth
1434                 self.teambubbleentity.mdl = self.teambubbleentity.model;
1435                 self.teambubbleentity.model = self.teambubbleentity.mdl;
1436                 self.teambubbleentity.customizeentityforclient = TeamBubble_customizeentityforclient;
1437                 self.teambubbleentity.effects = EF_LOWPRECISION;
1438         }
1439 }
1440
1441 // LordHavoc: this hack will be removed when proper _pants/_shirt layers are
1442 // added to the model skins
1443 /*void UpdateColorModHack()
1444 {
1445         local float c;
1446         c = self.clientcolors & 15;
1447         // LordHavoc: only bothering to support white, green, red, yellow, blue
1448              if (teamplay == 0) self.colormod = '0 0 0';
1449         else if (c ==  0) self.colormod = '1.00 1.00 1.00';
1450         else if (c ==  3) self.colormod = '0.10 1.73 0.10';
1451         else if (c ==  4) self.colormod = '1.73 0.10 0.10';
1452         else if (c == 12) self.colormod = '1.22 1.22 0.10';
1453         else if (c == 13) self.colormod = '0.10 0.10 1.73';
1454         else self.colormod = '1 1 1';
1455 };*/
1456
1457 void respawn(void)
1458 {
1459         CopyBody(1);
1460         self.effects |= EF_NODRAW; // prevent another CopyBody
1461         PutClientInServer();
1462 }
1463
1464 /**
1465  * When sv_timeout is used this function returs strings like
1466  * "Timeout begins in 2 seconds!\n" or "Timeout ends in 23 seconds!\n".
1467  * Called by centerprint functions
1468  * @param addOneSecond boolean, set to 1 if the welcome-message centerprint asks for the text
1469  */
1470 string getTimeoutText(float addOneSecond) {
1471         if (!cvar("sv_timeout") || !timeoutStatus)
1472                 return "";
1473
1474         local string retStr;
1475         if (timeoutStatus == 1) {
1476                 if (addOneSecond == 1) {
1477                         retStr = strcat("Timeout begins in ", ftos(remainingLeadTime + 1), " seconds!\n");
1478                 }
1479                 else {
1480                         retStr = strcat("Timeout begins in ", ftos(remainingLeadTime), " seconds!\n");
1481                 }
1482                 return retStr;
1483         }
1484         else if (timeoutStatus == 2) {
1485                 if (addOneSecond) {
1486                         retStr = strcat("Timeout ends in ", ftos(remainingTimeoutTime + 1), " seconds!\n");
1487                         //don't show messages like "Timeout ends in 0 seconds"...
1488                         if ((remainingTimeoutTime + 1) > 0)
1489                                 return retStr;
1490                         else
1491                                 return "";
1492                 }
1493                 else {
1494                         retStr = strcat("Timeout ends in ", ftos(remainingTimeoutTime), " seconds!\n");
1495                         //don't show messages like "Timeout ends in 0 seconds"...
1496                         if (remainingTimeoutTime > 0)
1497                                 return retStr;
1498                         else
1499                                 return "";
1500                 }
1501         }
1502         else return "";
1503 }
1504
1505 void player_powerups (void)
1506 {
1507         if (g_minstagib)
1508         {
1509                 if (self.items & IT_STRENGTH)
1510                 {
1511                         if (time > self.strength_finished)
1512                         {
1513                                 if (g_minstagib_invis_alpha > 0)
1514                                 {
1515                                         self.alpha = default_player_alpha;
1516                                         self.exteriorweaponentity.alpha = default_weapon_alpha;
1517                                         self.effects = self.effects | EF_FULLBRIGHT;
1518                                 }
1519                                 else
1520                                 {
1521                                         self.effects -= self.effects & EF_NODRAW;
1522                                 }
1523                                 self.items = self.items - (self.items & IT_STRENGTH);
1524                                 sprint(self, "^3Invisibility has worn off\n");
1525                         }
1526                 }
1527                 else
1528                 {
1529                         if (time < self.strength_finished)
1530                         {
1531                                 if (g_minstagib_invis_alpha > 0)
1532                                 {
1533                                         self.alpha = g_minstagib_invis_alpha;
1534                                         self.exteriorweaponentity.alpha = g_minstagib_invis_alpha;
1535                                         self.effects -= self.effects & EF_FULLBRIGHT;
1536                                 }
1537                                 else
1538                                 {
1539                                         self.effects = self.effects | EF_NODRAW;
1540                                 }
1541                                 self.items = self.items | IT_STRENGTH;
1542                                 sprint(self, "^3You are invisible\n");
1543                         }
1544                 }
1545
1546                 if (self.items & IT_INVINCIBLE)
1547                 {
1548                         if (time > self.invincible_finished)
1549                         {
1550                                 self.items = self.items - (self.items & IT_INVINCIBLE);
1551                                 sprint(self, "^3Speed has worn off\n");
1552                         }
1553                 }
1554                 else
1555                 {
1556                         if (time < self.invincible_finished)
1557                         {
1558                                 self.items = self.items | IT_INVINCIBLE;
1559                                 sprint(self, "^3You are on speed\n");
1560                         }
1561                 }
1562                 return;
1563         }
1564
1565         self.effects = self.effects - (self.effects & (EF_RED | EF_BLUE | EF_ADDITIVE | EF_FULLBRIGHT));
1566         if (self.items & IT_STRENGTH)
1567         {
1568                 self.effects = self.effects | (EF_BLUE | EF_ADDITIVE | EF_FULLBRIGHT);
1569                 if (time > self.strength_finished)
1570                 {
1571                         self.items = self.items - (self.items & IT_STRENGTH);
1572                         sprint(self, "^3Strength has worn off\n");
1573                 }
1574         }
1575         else
1576         {
1577                 if (time < self.strength_finished)
1578                 {
1579                         self.items = self.items | IT_STRENGTH;
1580                         sprint(self, "^3Strength infuses your weapons with devastating power\n");
1581                 }
1582         }
1583         if (self.items & IT_INVINCIBLE)
1584         {
1585                 self.effects = self.effects | (EF_RED | EF_ADDITIVE | EF_FULLBRIGHT);
1586                 if (time > self.invincible_finished)
1587                 {
1588                         self.items = self.items - (self.items & IT_INVINCIBLE);
1589                         sprint(self, "^3Shield has worn off\n");
1590                 }
1591         }
1592         else
1593         {
1594                 if (time < self.invincible_finished)
1595                 {
1596                         self.items = self.items | IT_INVINCIBLE;
1597                         sprint(self, "^3Shield surrounds you\n");
1598                 }
1599         }
1600
1601         if (cvar("g_fullbrightplayers"))
1602                 self.effects = self.effects | EF_FULLBRIGHT;
1603
1604         // midair gamemode: damage only while in the air
1605         // if in midair mode, being on ground grants temporary invulnerability
1606         // (this is so that multishot weapon don't clear the ground flag on the
1607         // first damage in the frame, leaving the player vulnerable to the
1608         // remaining hits in the same frame)
1609         if (self.flags & FL_ONGROUND)
1610         if (g_midair)
1611                 self.spawnshieldtime = max(self.spawnshieldtime, time + cvar("g_midair_shieldtime"));
1612
1613         if (time >= game_starttime)
1614         if (time < self.spawnshieldtime)
1615                 self.effects = self.effects | (EF_ADDITIVE | EF_FULLBRIGHT);
1616 }
1617
1618 float CalcRegen(float current, float stable, float regenfactor)
1619 {
1620         if(current > stable)
1621                 return current;
1622         else if(current > stable - 0.25) // when close enough, "snap"
1623                 return stable;
1624         else
1625                 return min(stable, current + (stable - current) * regenfactor * frametime);
1626 }
1627
1628 void player_regen (void)
1629 {
1630         float maxh, maxa, limith, limita, max_mod, regen_mod, rot_mod, limit_mod;
1631         maxh = cvar("g_balance_health_stable");
1632         maxa = cvar("g_balance_armor_stable");
1633         limith = cvar("g_balance_health_limit");
1634         limita = cvar("g_balance_armor_limit");
1635
1636         if (g_minstagib || (g_lms && !cvar("g_lms_regenerate")))
1637                 return;
1638
1639         max_mod = regen_mod = rot_mod = limit_mod = 1;
1640
1641         if (self.runes & RUNE_REGEN)
1642         {
1643                 if (self.runes & CURSE_VENOM) // do we have both rune/curse?
1644                 {
1645                         regen_mod = cvar("g_balance_rune_regen_combo_regenrate");
1646                         max_mod = cvar("g_balance_rune_regen_combo_hpmod");
1647                         limit_mod = cvar("g_balance_rune_regen_combo_limitmod");
1648                 }
1649                 else
1650                 {
1651                         regen_mod = cvar("g_balance_rune_regen_regenrate");
1652                         max_mod = cvar("g_balance_rune_regen_hpmod");
1653                         limit_mod = cvar("g_balance_rune_regen_limitmod");
1654                 }
1655         }
1656         else if (self.runes & CURSE_VENOM)
1657         {
1658                 max_mod = cvar("g_balance_curse_venom_hpmod");
1659                 if (self.runes & RUNE_REGEN) // do we have both rune/curse?
1660                         rot_mod = cvar("g_balance_rune_regen_combo_rotrate");
1661                 else
1662                         rot_mod = cvar("g_balance_curse_venom_rotrate");
1663                 limit_mod = cvar("g_balance_curse_venom_limitmod");
1664                 //if (!self.runes & RUNE_REGEN)
1665                 //      rot_mod = cvar("g_balance_curse_venom_rotrate");
1666         }
1667         maxh = maxh * max_mod;
1668         //maxa = maxa * max_mod;
1669         limith = limith * limit_mod;
1670         limita = limita * limit_mod;
1671
1672         if (self.armorvalue > maxa)
1673         {
1674                 if (time > self.pauserotarmor_finished)
1675                 {
1676                         self.armorvalue = max(maxa, self.armorvalue + (maxa - self.armorvalue) * cvar("g_balance_armor_rot") * frametime);
1677                         self.armorvalue = max(maxa, self.armorvalue - cvar("g_balance_armor_rotlinear") * frametime);
1678                 }
1679         }
1680         else if (self.armorvalue < maxa)
1681         {
1682                 if (time > self.pauseregen_finished)
1683                 {
1684                         self.armorvalue = CalcRegen(self.armorvalue, maxa, cvar("g_balance_armor_regen"));
1685                         self.armorvalue = min(maxa, self.armorvalue + cvar("g_balance_armor_regenlinear") * frametime);
1686                 }
1687         }
1688         if (self.health > maxh)
1689         {
1690                 if (time > self.pauserothealth_finished)
1691                 {
1692                         self.health = max(maxh, self.health + (maxh - self.health) * rot_mod*cvar("g_balance_health_rot") * frametime);
1693                         self.health = max(maxh, self.health - rot_mod*cvar("g_balance_health_rotlinear") * frametime);
1694                 }
1695         }
1696         else if (self.health < maxh)
1697         {
1698                 if (time > self.pauseregen_finished)
1699                 {
1700                         self.health = CalcRegen(self.health, maxh, regen_mod * cvar("g_balance_health_regen"));
1701                         self.health = min(maxh, self.health + regen_mod*cvar("g_balance_health_regenlinear") * frametime);
1702                 }
1703         }
1704
1705         if (self.health > limith)
1706                 self.health = limith;
1707         if (self.armorvalue > limita)
1708                 self.armorvalue = limita;
1709
1710         // if player rotted to death...  die!
1711         if(self.health < 1)
1712                 self.event_damage(self, self, 1, DEATH_ROT, self.origin, '0 0 0');
1713 }
1714
1715 .float zoomstate;
1716 float zoomstate_set;
1717 void SetZoomState(float z)
1718 {
1719         if(z != self.zoomstate)
1720         {
1721                 msg_entity = self;
1722                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
1723                 WriteByte(MSG_ONE, TE_CSQC_ZOOMNOTIFY);
1724                 WriteByte(MSG_ONE, z);
1725                 self.zoomstate = z;
1726         }
1727         zoomstate_set = 1;
1728 }
1729
1730 /*
1731 ======================
1732 spectate mode routines
1733 ======================
1734 */
1735 void SpectateCopy(entity spectatee) {
1736         self.armortype = spectatee.armortype;
1737         self.armorvalue = spectatee.armorvalue;
1738         self.currentammo = spectatee.currentammo;
1739         self.effects = spectatee.effects;
1740         self.health = spectatee.health;
1741         self.impulse = 0;
1742         self.items = spectatee.items;
1743         self.weapons = spectatee.weapons;
1744         self.switchweapon = spectatee.switchweapon;
1745         self.weapon = spectatee.weapon;
1746         self.punchangle = spectatee.punchangle;
1747         self.view_ofs = spectatee.view_ofs;
1748         self.v_angle = spectatee.v_angle;
1749         self.velocity = spectatee.velocity;
1750         self.dmg_take = spectatee.dmg_take;
1751         self.dmg_save = spectatee.dmg_save;
1752         self.dmg_inflictor = spectatee.dmg_inflictor;
1753         self.angles = spectatee.v_angle;
1754         self.fixangle = TRUE;
1755         setorigin(self, spectatee.origin);
1756         setsize(self, spectatee.mins, spectatee.maxs);
1757         SetZoomState(spectatee.zoomstate);
1758 }
1759
1760 float SpectateUpdate() {
1761         if(!self.enemy)
1762                 return 0;
1763
1764         if (self == self.enemy)
1765                 return 0;
1766         
1767         if(self.enemy.flags & FL_NOTARGET)
1768                 return 0;
1769
1770         SpectateCopy(self.enemy);
1771
1772         return 1;
1773 }
1774
1775 float SpectateNext() {
1776         other = find(self.enemy, classname, "player");
1777         if (!other) {
1778                 other = find(other, classname, "player");
1779         }
1780         if (other) {
1781                 self.enemy = other;
1782         }
1783         if(self.enemy.classname == "player") {
1784                 msg_entity = self;
1785                 WriteByte(MSG_ONE, SVC_SETVIEW);
1786                 WriteEntity(MSG_ONE, self.enemy);
1787                 self.wantswelcomemessage = 1;
1788                 //stuffcmd(self, "set viewsize $tmpviewsize \n");
1789                 if(!SpectateUpdate())
1790                         PutObserverInServer();
1791                 return 1;
1792         } else {
1793                 return 0;
1794         }
1795 }
1796
1797 /*
1798 =============
1799 ShowRespawnCountdown()
1800
1801 Update a respawn countdown display.
1802 =============
1803 */
1804 void ShowRespawnCountdown()
1805 {
1806         float number;
1807         if(self.deadflag == DEAD_NO) // just respawned?
1808                 return;
1809         else
1810         {
1811                 number = ceil(self.death_time - time);
1812                 if(number <= 0)
1813                         return;
1814                 if(number <= self.respawn_countdown)
1815                 {
1816                         self.respawn_countdown = number - 1;
1817                         if(ceil(self.death_time - (time + 0.5)) == number) // only say it if it is the same number even in 0.5s; to prevent overlapping sounds
1818                                 announce(self, strcat("announcer/robotic/", ftos(number), ".ogg"));
1819                 }
1820         }
1821 }
1822
1823 void LeaveSpectatorMode()
1824 {
1825         if(isJoinAllowed()) {
1826                 if(!cvar("teamplay") || cvar("g_campaign") || cvar("g_balance_teams")) {
1827                         self.classname = "player";
1828                         if(cvar("g_campaign") || cvar("g_balance_teams") || cvar("g_balance_teams_force"))
1829                                 JoinBestTeam(self, FALSE, TRUE);
1830                         if(cvar("g_campaign"))
1831                                 campaign_bots_may_start = 1;
1832                         PutClientInServer();
1833                         if(!(self.flags & FL_NOTARGET))
1834                                 bprint ("^4", self.netname, "^4 is playing now\n");
1835                         centerprint(self,"");
1836                         return;
1837                 } else {
1838                         stuffcmd(self,"menu_showteamselect\n");
1839                         return;
1840                 }
1841         }
1842         else {
1843                 //player may not join because of g_maxplayers is set
1844                 centerprint_atprio(self, CENTERPRIO_MAPVOTE, PREVENT_JOIN_TEXT);
1845         }
1846 }
1847
1848 /**
1849  * Determines whether the player is allowed to join. This depends on cvar
1850  * g_maxplayers, if it isn't used this function always return TRUE, otherwise
1851  * it checks whether the number of currently playing players exceeds g_maxplayers.
1852  * @return bool TRUE if the player is allowed to join, false otherwise
1853  */
1854 float isJoinAllowed() {
1855         if (!cvar("g_maxplayers"))
1856                 return TRUE;
1857
1858         local entity e;
1859         local float currentlyPlaying;
1860         FOR_EACH_REALPLAYER(e) {
1861                 if(e.classname == "player")
1862                         currentlyPlaying += 1;
1863         }
1864         if(currentlyPlaying < cvar("g_maxplayers"))
1865                 return TRUE;
1866
1867         return FALSE;
1868 }
1869
1870 /**
1871  * Checks whether the client is an observer or spectator, if so, he will get kicked after
1872  * g_maxplayers_spectator_blocktime seconds
1873  */
1874 void checkSpectatorBlock() {
1875         if(self.classname == "spectator" || self.classname == "observer") {
1876                 if( time > (self.spectatortime + cvar("g_maxplayers_spectator_blocktime")) ) {
1877                         sprint(self, "^7You were kicked from the server because you are spectator and spectators aren't allowed at the moment.\n");
1878                         dropclient(self);
1879                 }
1880         }
1881 }
1882
1883 float vercmp_recursive(string v1, string v2)
1884 {
1885         float dot1, dot2;
1886         string s1, s2;
1887         float r;
1888
1889         dot1 = strstrofs(v1, ".", 0);
1890         dot2 = strstrofs(v2, ".", 0);
1891         if(dot1 == -1)
1892                 s1 = v1;
1893         else
1894                 s1 = substring(v1, 0, dot1);
1895         if(dot2 == -1)
1896                 s2 = v2;
1897         else
1898                 s2 = substring(v2, 0, dot2);
1899
1900         r = stof(s1) - stof(s2);
1901         if(r != 0)
1902                 return r;
1903
1904         r = strcasecmp(s1, s2);
1905         if(r != 0)
1906                 return r;
1907
1908         if(dot1 == -1)
1909                 if(dot2 == -1)
1910                         return 0;
1911                 else
1912                         return -1;
1913         else
1914                 if(dot2 == -1)
1915                         return 1;
1916                 else
1917                         return vercmp_recursive(substring(v1, dot1 + 1, 999), substring(v2, dot2 + 1, 999));
1918 }
1919
1920 float vercmp(string v1, string v2)
1921 {
1922         if(strcasecmp(v1, v2) == 0) // early out check
1923                 return 0;
1924         return vercmp_recursive(v1, v2);
1925 }
1926
1927 void ObserverThink()
1928 {
1929         if (self.flags & FL_JUMPRELEASED) {
1930                 if (self.BUTTON_JUMP && !self.version_mismatch) {
1931                         self.welcomemessage_time = 0;
1932                         self.flags = self.flags - FL_JUMPRELEASED;
1933                         LeaveSpectatorMode();
1934                         return;
1935                 } else if(self.BUTTON_ATCK && !self.version_mismatch) {
1936                         self.welcomemessage_time = 0;
1937                         self.flags = self.flags - FL_JUMPRELEASED;
1938                         if(SpectateNext() == 1) {
1939                                 self.classname = "spectator";
1940                         }
1941                 }
1942         } else {
1943                 if (!(self.BUTTON_ATCK || self.BUTTON_JUMP)) {
1944                         self.flags = self.flags | FL_JUMPRELEASED;
1945                 }
1946         }
1947         /*
1948         if(self.BUTTON_ZOOM)
1949                 self.wantswelcomemessage = 0;
1950         */
1951         if(self.wantswelcomemessage)
1952                 PrintWelcomeMessage(self);
1953 }
1954
1955 void SpectatorThink()
1956 {
1957         if (self.flags & FL_JUMPRELEASED) {
1958                 if (self.BUTTON_JUMP && !self.version_mismatch) {
1959                         self.welcomemessage_time = 0;
1960                         self.flags = self.flags - FL_JUMPRELEASED;
1961                         LeaveSpectatorMode();
1962                         return;
1963                 } else if(self.BUTTON_ATCK) {
1964                         self.welcomemessage_time = 0;
1965                         self.flags = self.flags - FL_JUMPRELEASED;
1966                         if(SpectateNext() == 1) {
1967                                 self.classname = "spectator";
1968                         } else {
1969                                 self.classname = "observer";
1970                                 PutClientInServer();
1971                         }
1972                 } else if (self.BUTTON_ATCK2) {
1973                         self.welcomemessage_time = 0;
1974                         self.flags = self.flags - FL_JUMPRELEASED;
1975                         self.classname = "observer";
1976                         PutClientInServer();
1977                 } else {
1978                         if(!SpectateUpdate())
1979                                 PutObserverInServer();
1980                 }
1981         } else {
1982                 if (!(self.BUTTON_ATCK || self.BUTTON_ATCK2)) {
1983                         self.flags = self.flags | FL_JUMPRELEASED;
1984                 }
1985         }
1986         /*
1987         if(self.BUTTON_ZOOM)
1988                 self.wantswelcomemessage = 0;
1989         */
1990         if(self.wantswelcomemessage)
1991                 PrintWelcomeMessage(self);
1992         self.flags = self.flags | FL_CLIENT | FL_NOTARGET;
1993 }
1994
1995 /*
1996 =============
1997 PlayerPreThink
1998
1999 Called every frame for each client before the physics are run
2000 =============
2001 */
2002 void() ctf_setstatus;
2003 .float spectatee_status;
2004 void PlayerPreThink (void)
2005 {
2006         self.stat_sys_ticrate = cvar("sys_ticrate");
2007         self.stat_game_starttime = game_starttime;
2008
2009         if(blockSpectators)
2010                 checkSpectatorBlock();
2011         
2012         zoomstate_set = 0;
2013
2014         if(self.netname_previous != self.netname)
2015         {
2016                 if(cvar("sv_eventlog"))
2017                         GameLogEcho(strcat(":name:", ftos(self.playerid), ":", self.netname));
2018                 if(self.netname_previous)
2019                         strunzone(self.netname_previous);
2020                 self.netname_previous = strzone(self.netname);
2021         }
2022
2023         // version nagging
2024         if(self.version_nagtime)
2025                 if(self.cvar_g_nexuizversion)
2026                         if(time > self.version_nagtime)
2027                         {
2028                                 if(strstr(self.cvar_g_nexuizversion, "svn", 0) < 0)
2029                                 {
2030                                         if(strstr(cvar_string("g_nexuizversion"), "svn", 0) >= 0)
2031                                         {
2032                                                 dprint("^1NOTE^7 to ", self.netname, "^7 - the server is running ^3Nexuiz ", cvar_string("g_nexuizversion"), " (beta)^7, you have ^3Nexuiz ", self.cvar_g_nexuizversion, "^1\n");
2033                                                 sprint(self, strcat("\{1}^1NOTE: ^7the server is running ^3Nexuiz ", cvar_string("g_nexuizversion"), " (beta)^7, you have ^3Nexuiz ", self.cvar_g_nexuizversion, "^1\n"));
2034                                         }
2035                                         else
2036                                         {
2037                                                 float r;
2038                                                 r = vercmp(self.cvar_g_nexuizversion, cvar_string("g_nexuizversion"));
2039                                                 if(r < 0)
2040                                                 {
2041                                                         dprint("^1NOTE^7 to ", self.netname, "^7 - ^3Nexuiz ", cvar_string("g_nexuizversion"), "^7 is out, and you still have ^3Nexuiz ", self.cvar_g_nexuizversion, "^1 - get the update from ^4http://www.nexuiz.com/^1!\n");
2042                                                         sprint(self, strcat("\{1}^1NOTE: ^3Nexuiz ", cvar_string("g_nexuizversion"), "^7 is out, and you still have ^3Nexuiz ", self.cvar_g_nexuizversion, "^1 - get the update from ^4http://www.nexuiz.com/^1!\n"));
2043                                                 }
2044                                                 else if(r > 0)
2045                                                 {
2046                                                         dprint("^1NOTE^7 to ", self.netname, "^7 - the server is running ^3Nexuiz ", cvar_string("g_nexuizversion"), "^7, you have ^3Nexuiz ", self.cvar_g_nexuizversion, "^1\n");
2047                                                         sprint(self, strcat("\{1}^1NOTE: ^7the server is running ^3Nexuiz ", cvar_string("g_nexuizversion"), "^7, you have ^3Nexuiz ", self.cvar_g_nexuizversion, "^1\n"));
2048                                                 }
2049                                         }
2050                                 }
2051                                 self.version_nagtime = 0;
2052                         }
2053
2054         // GOD MODE info
2055         if(!(self.flags & FL_GODMODE)) if(self.max_armorvalue)
2056         {
2057                 sprint(self, strcat("godmode saved you ", ftos(self.max_armorvalue), " units of damage, cheater!\n"));
2058                 self.max_armorvalue = 0;
2059         }
2060
2061         if(frametime)
2062         {
2063                 SendFog();
2064                 antilag_record(self);
2065         }
2066
2067         if(self.classname == "player") {
2068 //              if(self.netname == "Wazat")
2069 //                      bprint(self.classname, "\n");
2070
2071                 CheckRules_Player();
2072
2073                 if(self.BUTTON_INFO)
2074                         PrintWelcomeMessage(self);
2075
2076                 if(g_lms || !cvar("sv_spectate"))
2077                 if((time - self.jointime) <= cvar("welcome_message_time"))
2078                         PrintWelcomeMessage(self);
2079
2080                 if (intermission_running)
2081                 {
2082                         IntermissionThink ();   // otherwise a button could be missed between
2083                         return;                                 // the think tics
2084                 }
2085
2086                 if(self.teleport_time)
2087                 if(time > self.teleport_time)
2088                 {
2089                         self.teleport_time = 0;
2090                         self.effects = self.effects - (self.effects & EF_NODRAW);
2091                         if(self.weaponentity)
2092                                 self.weaponentity.flags = self.weaponentity.flags - (self.weaponentity.flags & EF_NODRAW);
2093                 }
2094
2095                 Nixnex_GiveCurrentWeapon();
2096
2097                 if(frametime > 0) // don't do this in cl_movement frames, just in server ticks
2098                         UpdateSelectedPlayer();
2099
2100                 //don't allow the player to turn around while game is paused!
2101                 if(timeoutStatus == 2) {
2102                         self.v_angle = self.lastV_angle;
2103                         self.angles = self.lastV_angle;
2104                         self.fixangle = TRUE;
2105                 }
2106
2107                 if (self.deadflag != DEAD_NO)
2108                 {
2109                         float button_pressed, force_respawn;
2110                         player_anim();
2111                         button_pressed = (self.BUTTON_ATCK || self.BUTTON_JUMP || self.BUTTON_ATCK2 || self.BUTTON_HOOK || self.BUTTON_USE);
2112                         force_respawn = (g_lms || cvar("g_forced_respawn"));
2113                         if (self.deadflag == DEAD_DYING)
2114                         {
2115                                 if(force_respawn)
2116                                         self.deadflag = DEAD_RESPAWNING;
2117                                 else if(!button_pressed)
2118                                         self.deadflag = DEAD_DEAD;
2119                         }
2120                         else if (self.deadflag == DEAD_DEAD)
2121                         {
2122                                 if(button_pressed)
2123                                         self.deadflag = DEAD_RESPAWNABLE;
2124                         }
2125                         else if (self.deadflag == DEAD_RESPAWNABLE)
2126                         {
2127                                 if(!button_pressed)
2128                                         self.deadflag = DEAD_RESPAWNING;
2129                         }
2130                         else if (self.deadflag == DEAD_RESPAWNING)
2131                         {
2132                                 if(time > self.death_time)
2133                                 {
2134                                         self.death_time = time + 1; // only retry once a second
2135                                         respawn();
2136                                 }
2137                         }
2138                         ShowRespawnCountdown();
2139                         return;
2140                 }
2141
2142                 if(g_lms && !self.deadflag && cvar("g_lms_campcheck_interval"))
2143                 {
2144                         vector dist;
2145
2146                         // calculate player movement (in 2 dimensions only, so jumping on one spot doesn't count as movement)
2147                         dist = self.oldorigin - self.origin;
2148                         dist_z = 0;
2149                         self.lms_traveled_distance += fabs(vlen(dist));
2150
2151                         if((cvar("g_campaign") && !campaign_bots_may_start) || (time < game_starttime))
2152                         {
2153                                 self.lms_nextcheck = time + cvar("g_lms_campcheck_interval")*2;
2154                                 self.lms_traveled_distance = 0;
2155                         }
2156
2157                         if(time > self.lms_nextcheck)
2158                         {
2159                                 //sprint(self, "distance: ", ftos(self.lms_traveled_distance), "\n");
2160                                 if(self.lms_traveled_distance < cvar("g_lms_campcheck_distance"))
2161                                 {
2162                                         centerprint(self, cvar_string("g_lms_campcheck_message"));
2163                                         // FIXME KadaverJack: gibbing player here causes playermodel to bounce around, instead of eye.md3
2164                                         // I wasn't able to find out WHY that happens, so I put a workaround in place that shall prevent players from being gibbed :(
2165                                         Damage(self, self, self, bound(0, cvar("g_lms_campcheck_damage"), self.health + self.armorvalue * cvar("g_balance_armor_blockpercent") + 5), DEATH_CAMP, self.origin, '0 0 0');
2166                                 }
2167                                 self.lms_nextcheck = time + cvar("g_lms_campcheck_interval");
2168                                 self.lms_traveled_distance = 0;
2169                         }
2170                 }
2171
2172                 if (self.BUTTON_CROUCH && !self.hook.state)
2173                 {
2174                         if (!self.crouch)
2175                         {
2176                                 self.crouch = TRUE;
2177                                 self.view_ofs = PL_CROUCH_VIEW_OFS;
2178                                 setsize (self, PL_CROUCH_MIN, PL_CROUCH_MAX);
2179                                 player_setanim(self.anim_duck, FALSE, TRUE, TRUE);
2180                         }
2181                 }
2182                 else
2183                 {
2184                         if (self.crouch)
2185                         {
2186                                 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, FALSE, self);
2187                                 if (!trace_startsolid)
2188                                 {
2189                                         self.crouch = FALSE;
2190                                         self.view_ofs = PL_VIEW_OFS;
2191                                         setsize (self, PL_MIN, PL_MAX);
2192                                 }
2193                         }
2194                 }
2195
2196                 FixPlayermodel();
2197
2198                 GrapplingHookFrame();
2199
2200                 W_WeaponFrame();
2201
2202                 player_powerups();
2203                 player_regen();
2204                 player_anim();
2205
2206                 if (g_minstagib)
2207                         minstagib_ammocheck();
2208
2209                 ctf_setstatus();
2210                 kh_setstatus();
2211
2212                 //self.angles_y=self.v_angle_y + 90;   // temp
2213
2214                 //if (TetrisPreFrame()) return;
2215         } else if(gameover) {
2216                 if (intermission_running)
2217                         IntermissionThink ();   // otherwise a button could be missed between
2218                 return;
2219         } else if(self.classname == "observer") {
2220                 ObserverThink();
2221         } else if(self.classname == "spectator") {
2222                 SpectatorThink();
2223         }
2224
2225         if(!zoomstate_set)
2226                 SetZoomState(self.BUTTON_ZOOM || (self.BUTTON_ATCK2 && self.weapon == WEP_NEX));
2227
2228         float oldspectatee_status;
2229         oldspectatee_status = self.spectatee_status;
2230         if(self.classname == "spectator")
2231                 self.spectatee_status = num_for_edict(self.enemy);
2232         else if(self.classname == "observer")
2233                 self.spectatee_status = num_for_edict(self);
2234         else
2235                 self.spectatee_status = 0;
2236         if(self.spectatee_status != oldspectatee_status)
2237         {
2238                 msg_entity = self;
2239                 WriteByte(MSG_ONE, SVC_TEMPENTITY);
2240                 WriteByte(MSG_ONE, TE_CSQC_SPECTATING);
2241                 WriteByte(MSG_ONE, self.spectatee_status);
2242                 if(g_race)
2243                         race_InitSpectator();
2244         }
2245
2246         if(self.teamkill_soundtime)
2247         if(time > self.teamkill_soundtime)
2248         {
2249                 self.teamkill_soundtime = 0;
2250
2251                 entity oldpusher, oldself;
2252
2253                 oldself = self; self = self.teamkill_soundsource;
2254                 oldpusher = self.pusher; self.pusher = oldself;
2255
2256                 VoiceMessage("teamshoot_auto");
2257
2258                 self.pusher = oldpusher;
2259                 self = oldself;
2260         }
2261 }
2262
2263
2264 /*
2265 =============
2266 PlayerPostThink
2267
2268 Called every frame for each client after the physics are run
2269 =============
2270 */
2271 .float idlekick_lasttimeleft;
2272 .float race_penalty;
2273 .float race_penalty_nagged;
2274 .float race_penalty_nagtime;
2275 void PlayerPostThink (void)
2276 {
2277         // Savage: Check for nameless players
2278         if (strlen(self.netname) < 1) {
2279                 self.netname = "Player";
2280                 stuffcmd(self, "seta _cl_name Player\n");
2281         }
2282
2283         if(sv_maxidle)
2284         {
2285                 float timeleft;
2286                 timeleft = ceil(sv_maxidle - (time - self.parm_idlesince));
2287                 if(timeleft <= 0)
2288                 {
2289                         bprint("^3", self.netname, "^3 was kicked for idling.\n");
2290                         announce(self, "announcer/robotic/terminated.ogg");
2291                         dropclient(self);
2292                         return;
2293                 }
2294                 else if(timeleft <= 10)
2295                 {
2296                         if(timeleft != self.idlekick_lasttimeleft)
2297                         {
2298                                 centerprint_atprio(self, CENTERPRIO_IDLEKICK, strcat("^3Stop idling!\n^3Disconnecting in ", ftos(timeleft), "..."));
2299                                 announce(self, strcat("announcer/robotic/", ftos(timeleft), ".ogg"));
2300                         }
2301                 }
2302                 else
2303                 {
2304                         centerprint_expire(self, CENTERPRIO_IDLEKICK);
2305                 }
2306                 self.idlekick_lasttimeleft = timeleft;
2307         }
2308
2309         if(self.classname == "player") {
2310                 CheckRules_Player();
2311                 UpdateChatBubble();
2312                 UpdateTeamBubble();
2313                 if (self.impulse)
2314                         ImpulseCommands();
2315                 if (intermission_running)
2316                         return;         // intermission or finale
2317
2318                 //PrintWelcomeMessage(self);
2319                 //if (TetrisPostFrame()) return;
2320
2321                 // restart countdown
2322                         if(time < game_starttime) {
2323                                 if (!cvar("sv_ready_restart_after_countdown"))
2324                                 {
2325                                         if(self.movement != '0 0 0' && g_race && !g_race_qualifying)
2326                                         {
2327                                                 if(time < game_starttime - 2)
2328                                                 {
2329                                                         if(!self.race_penalty_nagged)
2330                                                         {
2331                                                                 // TODO better notification for this!
2332                                                                 self.race_penalty_nagtime = 0;
2333                                                                 self.race_penalty_nagged = 1;
2334                                                         }
2335                                                 }
2336                                                 else if(!self.race_penalty)
2337                                                 {
2338                                                         self.race_penalty_nagtime = 0;
2339                                                         self.race_penalty = time + 5;
2340                                                 }
2341                                         }
2342                                         if(time > self.race_penalty_nagtime)
2343                                         {
2344                                                 if(self.race_penalty > time)
2345                                                 {
2346                                                         centerprint_atprio(self, CENTERPRIO_IDLEKICK, "^1FIVE SECONDS PENALTY.");
2347                                                 }
2348                                                 else if(self.race_penalty_nagged && time < game_starttime - 2)
2349                                                 {
2350                                                         centerprint_atprio(self, CENTERPRIO_IDLEKICK, "^1DO NOT MOVE DURING THE COUNTDOWN.");
2351                                                 }
2352                                                 self.race_penalty_nagtime = time + self.cvar_scr_centertime * 0.6;
2353                                         }
2354                                         self.movetype = MOVETYPE_NONE;          
2355                                         self.velocity = '0 0 0';
2356                                         self.avelocity = '0 0 0';
2357                                         self.movement = '0 0 0';
2358                                 }
2359                         }
2360                         else if (time < self.race_penalty)
2361                         {
2362                                 self.movetype = MOVETYPE_NONE;          
2363                                 self.velocity = '0 0 0';
2364                                 self.avelocity = '0 0 0';
2365                                 self.movement = '0 0 0';
2366                         }
2367                         else
2368                         {
2369                                 //allow the player to move again if sv_ready_restart_after_countdown is not used and countdown is over
2370                                 if (!cvar("sv_ready_restart_after_countdown"))
2371                                 {
2372                                         if(self.movetype == MOVETYPE_NONE)
2373                                         {
2374                                                 self.movetype = MOVETYPE_WALK;
2375                                         }
2376                                         self.race_penalty = 0;
2377                                         self.race_penalty_nagged = 0;
2378                                 }
2379                         }
2380                 
2381         } else if (self.classname == "observer") {
2382                 //do nothing
2383         } else if (self.classname == "spectator") {
2384                 //do nothing
2385         }
2386
2387         /*
2388         float i;
2389         for(i = 0; i < 1000; ++i)
2390         {
2391                 vector end;
2392                 end = self.origin + '0 0 1024' + 512 * randomvec();
2393                 tracebox(self.origin, self.mins, self.maxs, end, MOVE_NORMAL, self);
2394                 if(trace_fraction < 1)
2395                 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT))
2396                 {
2397                         print("I HIT SOLID: ", vtos(self.origin), " -> ", vtos(end), "\n");
2398                         break;
2399                 }
2400         }
2401         */
2402
2403         Arena_Warmup();
2404
2405         //pointparticles(particleeffectnum("machinegun_impact"), self.origin + self.view_ofs + '0 0 7', '0 0 0', 1);
2406 }