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