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