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