]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_client.qc
implemented models/player/whatever.md3.animinfo file support so md3
[divverent/nexuiz.git] / data / qcsrc / server / cl_client.qc
1 void info_player_start (void)
2 {
3         info_player_deathmatch();
4 }
5
6 void info_player_deathmatch (void)
7 {
8         self.classname = "info_player_deathmatch";
9         relocate_spawnpoint();
10 }
11
12 float spawn_allbad;
13 float spawn_allgood;
14 entity Spawn_FilterOutBadSpots(entity firstspot, entity playerlist, float mindist, float teamcheck)
15 {
16         local entity spot, player, nextspot, previousspot, newfirstspot;
17         local float pcount;
18         spot = firstspot;
19         newfirstspot = world;
20         previousspot = world;
21         spawn_allgood = TRUE;
22         spawn_allbad = TRUE;
23         while (spot)
24         {
25                 nextspot = spot.chain;
26                 // count team mismatches as bad spots
27
28                 local float spotactive;
29                 spotactive = 1;
30
31                 // filter out spots for assault
32                 if(spot.target != "") {
33                         local entity ent;
34                         ent = find(world, targetname, spot.target);
35                         while(ent) {
36                                 if(ent.classname == "target_objective")
37                                         if(ent.health < 0 || ent.health >= ASSAULT_VALUE_INACTIVE)
38                                                 spotactive = 0;
39                                 ent = find(ent, targetname, spot.target);
40                         }
41                 }
42
43                 if (spot.team == teamcheck && spotactive > 0)
44                 {
45                         pcount = 0;
46                         player = playerlist;
47                         while (player)
48                         {
49                                 if (player != self)
50                                 if (vlen(player.origin - spot.origin) < mindist)
51                                         pcount = pcount + 1;
52                                 player = player.chain;
53                         }
54                         if (!pcount)
55                         {
56                                 spawn_allbad = FALSE;
57                                 if (newfirstspot)
58                                         previousspot.chain = spot;
59                                 else
60                                         newfirstspot = spot;
61                                 previousspot = spot;
62                                 spot.chain = world;
63                         }
64                         else
65                                 spawn_allgood = FALSE;
66                 }
67                 spot = nextspot;
68         }
69         // if we couldn't find ANY good points, return the original list
70         if (!newfirstspot)
71                 newfirstspot = firstspot;
72         return newfirstspot;
73 }
74
75 entity Spawn_RandomPoint(entity firstspot)
76 {
77         local entity spot;
78         local float numspots;
79         // count number of spots
80         numspots = 0;
81         spot = firstspot;
82         while (spot)
83         {
84                 numspots = numspots + 1;
85                 spot = spot.chain;
86         }
87         // pick a random one
88         numspots = numspots * random();
89         spot = firstspot;
90         while (spot.chain && numspots >= 1)
91         {
92                 numspots = numspots - 1;
93                 spot = spot.chain;
94         }
95         return spot;
96 }
97
98 entity Spawn_FurthestPoint(entity firstspot, entity playerlist)
99 {
100         local entity best, spot, player;
101         local float bestrating, rating;
102         best = world;
103         bestrating = -1000000;
104         spot = firstspot;
105         while (spot)
106         {
107                 rating = 1000000000;
108                 player = playerlist;
109                 while (player)
110                 {
111                         if (player != self)
112                                 rating = min(rating, vlen(player.origin - spot.origin));
113                         player = player.chain;
114                 }
115                 rating = rating + random() * 16;
116                 if (bestrating < rating)
117                 {
118                         best = spot;
119                         bestrating = rating;
120                 }
121                 spot = spot.chain;
122         }
123         return best;
124 }
125
126 /*
127 =============
128 SelectSpawnPoint
129
130 Finds a point to respawn
131 =============
132 */
133 entity SelectSpawnPoint (float anypoint)
134 {
135         local float teamcheck;
136         local entity spot, firstspot, playerlist;
137
138         spot = find (world, classname, "testplayerstart");
139         if (spot)
140                 return spot;
141
142         teamcheck = 0;
143
144         if(!anypoint && (g_ctf || g_assault))
145                 teamcheck = self.team;
146
147         // get the list of players
148         playerlist = findchain(classname, "player");
149         // get the entire list of spots
150         firstspot = findchain(classname, "info_player_deathmatch");
151         // filter out the bad ones
152         // (note this returns the original list if none survived)
153         firstspot = Spawn_FilterOutBadSpots(firstspot, playerlist, 100, teamcheck);
154
155         // there is 50/50 chance of choosing a random spot or the furthest spot
156         // (this means that roughly every other spawn will be furthest, so you
157         // usually won't get fragged at spawn twice in a row)
158         if (arena_roundbased)
159         {
160                 firstspot = Spawn_FilterOutBadSpots(firstspot, playerlist, 800, teamcheck);
161                 spot = Spawn_RandomPoint(firstspot);
162         }
163         else if (random() > 0.5 || spawn_allbad || spawn_allgood)
164                 spot = Spawn_RandomPoint(firstspot);
165         else
166                 spot = Spawn_FurthestPoint(firstspot, playerlist);
167
168         if (!spot)
169         {
170                 if(cvar("spawn_debug"))
171                         GotoNextMap();
172                 else
173                         error ("PutClientInServer: no start points on level");
174         }
175
176         return spot;
177 }
178
179 /*
180 =============
181 CheckPlayerModel
182
183 Checks if the argument string can be a valid playermodel.
184 Returns a valid one in doubt.
185 =============
186 */
187 string FallbackPlayerModel = "models/player/marine.zym";
188 string CheckPlayerModel(string plyermodel) {
189         if(strlen(plyermodel) < 4)
190                 return FallbackPlayerModel;
191         if( substring(plyermodel,0,14) != "models/player/")
192                 return FallbackPlayerModel;
193         else if(cvar("sv_servermodelsonly"))
194         {
195                 if(substring(plyermodel,strlen(plyermodel)-4,4) != ".zym")
196                         return FallbackPlayerModel;
197                 if(!fexists(plyermodel))
198                         return FallbackPlayerModel;
199         }
200         return plyermodel;
201 }
202
203 /*
204 =============
205 Client_customizeentityforclient
206
207 LOD reduction
208 =============
209 */
210 float Client_customizeentityforclient()
211 {
212 #ifdef ALLOW_VARIABLE_LOD
213         // self: me
214         // other: the player viewing me
215         float distance;
216         float f;
217
218         if(self.flags & FL_NOTARGET) // we don't need LOD for spectators
219                 return TRUE;
220
221         if(other.cvar_cl_playerdetailreduction <= 0)
222         {
223                 if(other.cvar_cl_playerdetailreduction <= -2)
224                         self.modelindex = self.modelindex_lod2;
225                 else if(other.cvar_cl_playerdetailreduction <= -1)
226                         self.modelindex = self.modelindex_lod1;
227                 else
228                         self.modelindex = self.modelindex_lod0;
229         }
230         else
231         {
232                 distance = vlen(self.origin - other.origin);
233                 f = (distance + 100.0) * other.cvar_cl_playerdetailreduction;
234                 if(f > 10000)
235                         self.modelindex = self.modelindex_lod2;
236                 else if(f > 5000)
237                         self.modelindex = self.modelindex_lod1;
238                 else
239                         self.modelindex = self.modelindex_lod0;
240         }
241 #endif
242
243         return TRUE;
244 }
245
246 void setmodel_lod(entity e, string modelname)
247 {
248 #ifdef ALLOW_VARIABLE_LOD
249         string s;
250
251         // FIXME: this only supports 3-letter extensions
252         s = strcat(substring(modelname, 0, strlen(modelname) - 4), "_1", substring(modelname, 0, strlen(modelname) - 4));
253         if(fexists(s))
254         {
255                 precache_model(s);
256                 setmodel(e, s); // players have high precision
257                 self.modelindex_lod1 = self.modelindex;
258         }
259         else
260                 self.modelindex_lod1 = -1;
261
262         s = strcat(substring(modelname, 0, strlen(modelname) - 4), "_2", substring(modelname, 0, strlen(modelname) - 4));
263         if(fexists(s))
264         {
265                 precache_model(s);
266                 setmodel(e, s); // players have high precision
267                 self.modelindex_lod2 = self.modelindex;
268         }
269         else
270                 self.modelindex_lod2 = -1;
271
272         precache_model(modelname);
273         setmodel(e, modelname); // players have high precision
274         self.modelindex_lod0 = self.modelindex;
275
276         if(self.modelindex_lod1 < 0)
277                 self.modelindex_lod1 = self.modelindex;
278
279         if(self.modelindex_lod2 < 0)
280                 self.modelindex_lod2 = self.modelindex;
281 #else
282         precache_model(modelname);
283         setmodel(e, modelname); // players have high precision
284 #endif
285         player_setupanimsformodel();
286 }
287
288 /*
289 =============
290 PutObserverInServer
291
292 putting a client as observer in the server
293 =============
294 */
295 void PutObserverInServer (void)
296 {
297         entity  spot;
298         spot = SelectSpawnPoint (TRUE);
299         RemoveGrapplingHook(self); // Wazat's Grappling Hook
300
301         if(clienttype(self) == CLIENTTYPE_REAL)
302         {
303                 msg_entity = self;
304                 WriteByte(MSG_ONE, SVC_SETVIEW);
305                 WriteEntity(MSG_ONE, self);
306         }
307
308         DropAllRunes(self);
309         kh_Key_DropAll(self, TRUE);
310
311         if(self.flagcarried)
312                 DropFlag(self.flagcarried);
313
314         WaypointSprite_PlayerDead();
315
316         DistributeFragsAmongTeam(self, self.team, 1);
317
318         if(self.frags <= 0 && self.frags > -666 && g_lms && self.killcount != -666)
319                 bprint ("^4", self.netname, "^4 has no more lives left\n");
320         else if(self.killcount != -666)
321                 bprint ("^4", self.netname, "^4 is spectating now\n");
322
323         self.classname = "observer";
324         self.health = -666;
325         self.takedamage = DAMAGE_NO;
326         self.solid = SOLID_NOT;
327         self.movetype = MOVETYPE_NOCLIP;
328         self.flags = FL_CLIENT | FL_NOTARGET;
329         self.armorvalue = 666;
330         self.effects = 0;
331         self.armorvalue = cvar("g_balance_armor_start");
332         self.pauserotarmor_finished = 0;
333         self.pauserothealth_finished = 0;
334         self.pauseregen_finished = 0;
335         self.damageforcescale = 0;
336         self.death_time = 0;
337         self.dead_frame = 0;
338         self.deaths = 0;
339         self.alpha = 0;
340         self.scale = 0;
341         self.fade_time = 0;
342         self.pain_frame = 0;
343         self.pain_finished = 0;
344         self.strength_finished = 0;
345         self.invincible_finished = 0;
346         self.pushltime = 0;
347         self.think = SUB_Null;
348         self.nextthink = 0;
349         self.hook_time = 0;
350         self.runes = 0;
351         self.deadflag = DEAD_NO;
352         self.angles = spot.angles;
353         self.angles_z = 0;
354         self.fixangle = TRUE;
355         self.crouch = FALSE;
356
357         self.view_ofs = PL_VIEW_OFS;
358         setorigin (self, spot.origin);
359         setsize (self, '0 0 0', '0 0 0');
360         self.oldorigin = self.origin;
361         self.items = 0;
362         self.model = "";
363         self.modelindex = 0;
364         self.weapon = 0;
365         self.weaponmodel = "";
366         self.weaponentity = world;
367         self.killcount = -666;
368         self.velocity = '0 0 0';
369         self.avelocity = '0 0 0';
370         self.punchangle = '0 0 0';
371         self.punchvector = '0 0 0';
372         self.oldvelocity = self.velocity;
373         self.customizeentityforclient = Client_customizeentityforclient;
374
375         if(g_arena)
376         {
377                 if(self.frags != -2)
378                 {
379                         Spawnqueue_Insert(self);
380                 }
381                 else
382                 {
383                         Spawnqueue_Unmark(self);
384                         Spawnqueue_Remove(self);
385                 }
386         }
387         else if(!g_lms)
388                 self.frags = -666;
389 }
390
391
392 /*
393 =============
394 PutClientInServer
395
396 Called when a client spawns in the server
397 =============
398 */
399 void PutClientInServer (void)
400 {
401         if(clienttype(self) == CLIENTTYPE_BOT)
402         {
403                 self.classname = "player";
404         }
405         else if(clienttype(self) == CLIENTTYPE_REAL)
406         {
407                 msg_entity = self;
408                 WriteByte(MSG_ONE, SVC_SETVIEW);
409                 WriteEntity(MSG_ONE, self);
410         }
411
412         // player is dead and becomes observer
413         if(g_lms && self.frags < 1)
414                 self.classname = "observer";
415
416         if(g_arena)
417         if(!self.spawned)
418                 self.classname = "observer";
419
420         if(self.classname == "player") {
421                 entity  spot;
422
423                 spot = SelectSpawnPoint (FALSE);
424
425                 RemoveGrapplingHook(self); // Wazat's Grappling Hook
426
427                 self.classname = "player";
428                 self.iscreature = TRUE;
429                 self.movetype = MOVETYPE_WALK;
430                 self.solid = SOLID_SLIDEBOX;
431                 self.flags = FL_CLIENT;
432                 self.takedamage = DAMAGE_AIM;
433                 self.effects = 0;
434                 self.air_finished = time + 12;
435                 self.dmg = 2;
436
437                 self.ammo_shells = start_ammo_shells;
438                 self.ammo_nails = start_ammo_nails;
439                 self.ammo_rockets = start_ammo_rockets;
440                 self.ammo_cells = start_ammo_cells;
441                 self.health = start_health;
442                 self.armorvalue = start_armorvalue;
443                 self.items = start_items;
444                 self.switchweapon = start_switchweapon;
445                 self.weapon = 0;
446                 self.jump_interval = time;
447
448                 self.spawnshieldtime = time + cvar("g_spawnshieldtime");
449                 self.pauserotarmor_finished = time + cvar("g_balance_pause_armor_rot_spawn");
450                 self.pauserothealth_finished = time + cvar("g_balance_pause_health_rot_spawn");
451                 self.pauseregen_finished = time + cvar("g_balance_pause_health_regen_spawn");
452                 self.damageforcescale = 2;
453                 self.death_time = 0;
454                 self.dead_frame = 0;
455                 self.alpha = 0;
456                 self.scale = 0;
457                 self.fade_time = 0;
458                 self.pain_frame = 0;
459                 self.pain_finished = 0;
460                 self.strength_finished = 0;
461                 self.invincible_finished = 0;
462                 self.pushltime = 0;
463                 //self.speed_finished = 0;
464                 //self.slowmo_finished = 0;
465                 // players have no think function
466                 self.think = SUB_Null;
467                 self.nextthink = 0;
468                 self.hook_time = 0;
469
470                 self.runes = 0;
471
472                 self.deadflag = DEAD_NO;
473
474                 self.angles = spot.angles;
475
476                 self.angles_z = 0; // never spawn tilted even if the spot says to
477                 self.fixangle = TRUE; // turn this way immediately
478                 self.velocity = '0 0 0';
479                 self.avelocity = '0 0 0';
480                 self.punchangle = '0 0 0';
481                 self.punchvector = '0 0 0';
482                 self.oldvelocity = self.velocity;
483
484                 self.viewzoom = 0.6;
485                 self.has_zoomed = 0;
486
487                 self.customizeentityforclient = Client_customizeentityforclient;
488
489                 if(cvar("sv_defaultcharacter") == 1) {
490                         local string defaultmodel;
491                         defaultmodel = cvar_string("sv_defaultplayermodel");
492                         setmodel_lod (self, defaultmodel);
493                         self.skin = stof(cvar_string("sv_defaultplayerskin"));
494                 } else {
495                         self.playermodel = CheckPlayerModel(self.playermodel);
496                         setmodel_lod (self, self.playermodel);
497                         if(teams_matter)
498                         {
499                                 float s;
500                                 s = stof(self.playerskin);
501                                 if(s >= 0 && s < 3)
502                                         self.skin = s;
503                                 else if(s == 6)
504                                         self.skin = 6;
505                                 else
506                                         self.skin = 0;
507                         }
508                         else
509                                 self.skin = stof(self.playerskin);
510                 }
511                 if(!teams_matter)
512                         if(strlen(cvar_string("sv_defaultplayercolors")))
513                                 setcolor(self, cvar("sv_defaultplayercolors"));
514
515                 self.crouch = FALSE;
516                 self.view_ofs = PL_VIEW_OFS;
517                 setsize (self, PL_MIN, PL_MAX);
518                 self.spawnorigin = spot.origin;
519                 setorigin (self, spot.origin + '0 0 1' * (1 - self.mins_z - 24));
520                 // don't reset back to last position, even if new position is stuck in solid
521                 self.oldorigin = self.origin;
522
523                 if(g_arena)
524                 {
525                         Spawnqueue_Remove(self);
526                         Spawnqueue_Mark(self);
527                 }
528
529                 self.event_damage = PlayerDamage;
530
531                 self.bot_attack = TRUE;
532
533                 self.statdraintime = time + 5;
534                 self.button0 = self.button1 = self.button2 = self.button3 = 0;
535
536                 if(self.killcount == -666) {
537                         self.killcount = 0;
538                         if(!g_arena)
539                         if(!g_lms)
540                                 self.frags = 0;
541                 }
542
543                 self.cnt = WEP_LASER;
544                 self.nixnex_lastchange_id = -1;
545
546                 CL_SpawnWeaponentity();
547                 self.alpha = default_player_alpha;
548                 self.exteriorweaponentity.alpha = default_weapon_alpha;
549
550                 self.lms_nextcheck = time + cvar("g_lms_campcheck_interval")*2;
551                 self.lms_traveled_distance = 0;
552
553                 if(cvar("spawn_debug"))
554                 {
555                         sprint(self, strcat("spawnpoint origin:  ", vtos(spot.origin), "\n"));
556                         remove(spot);   // usefull for checking if there are spawnpoints, that let drop through the floor
557                 }
558
559                 //stuffcmd(self, "chase_active 0");
560                 //stuffcmd(self, "set viewsize $tmpviewsize \n");
561
562                 if (cvar("g_spawnsound"))
563                         sound (self, CHAN_AUTO, "misc/spawn.wav", 1, ATTN_NORM);
564
565                 if(g_assault) {
566                         if(self.team == assault_attacker_team)
567                                 centerprint(self, "You are attacking!\n");
568                         else
569                                 centerprint(self, "You are defending!\n");
570                 }
571
572         } else if(self.classname == "observer") {
573                 PutObserverInServer ();
574         }
575 }
576
577 /*
578 =============
579 SetNewParms
580 =============
581 */
582 void SetNewParms (void)
583 {
584
585 }
586
587 /*
588 =============
589 SetChangeParms
590 =============
591 */
592 void SetChangeParms (void)
593 {
594
595 }
596
597 /*
598 =============
599 ClientKill
600
601 Called when a client types 'kill' in the console
602 =============
603 */
604 void ClientKill (void)
605 {
606         Damage(self, self, self, 100000, DEATH_KILL, self.origin, '0 0 0');
607 }
608
609 void FixClientCvars(entity e)
610 {
611         float t;
612         // send prediction settings to the client
613         stuffcmd(e, "\nin_bindmap 0 0\n");
614         stuffcmd(e, strcat("cl_gravity ", ftos(cvar("sv_gravity")), "\n"));
615         stuffcmd(e, strcat("cl_movement_accelerate ", ftos(cvar("sv_accelerate")), "\n"));
616         stuffcmd(e, strcat("cl_movement_friction ", ftos(cvar("sv_friction")), "\n"));
617         stuffcmd(e, strcat("cl_movement_maxspeed ", ftos(cvar("sv_maxspeed")), "\n"));
618         stuffcmd(e, strcat("cl_movement_airaccelerate ", ftos(cvar("sv_airaccelerate")), "\n"));
619         stuffcmd(e, strcat("cl_movement_maxairspeed ", ftos(cvar("sv_maxairspeed")), "\n"));
620         stuffcmd(e, strcat("cl_movement_stopspeed ", ftos(cvar("sv_stopspeed")), "\n"));
621         stuffcmd(e, strcat("cl_movement_jumpvelocity ", ftos(cvar("sv_jumpvelocity")), "\n"));
622         stuffcmd(e, strcat("cl_movement_stepheight ", ftos(cvar("sv_stepheight")), "\n"));
623         stuffcmd(e, strcat("set cl_movement_friction_on_land ", ftos(cvar("sv_friction_on_land")), "\n"));
624         stuffcmd(e, strcat("set cl_movement_airaccel_qw ", ftos(cvar("sv_airaccel_qw")), "\n"));
625         stuffcmd(e, strcat("set cl_movement_airaccel_sideways_friction ", ftos(cvar("sv_airaccel_sideways_friction")), "\n"));
626         stuffcmd(e, "cl_movement_edgefriction 1\n");
627
628         // notify about available teams
629         if(teamplay)
630         {
631                 CheckAllowedTeams(e);
632                 t = 0; if(c1 >= 0) t |= 1; if(c2 >= 0) t |= 2; if(c3 >= 0) t |= 4; if(c4 >= 0) t |= 8;
633                 stuffcmd(e, strcat("set _teams_available ", ftos(t), "\n"));
634         }
635         else
636                 stuffcmd(e, "set _teams_available 0\n");
637 }
638
639 /*
640 =============
641 ClientConnect
642
643 Called when a client connects to the server
644 =============
645 */
646 string ColoredTeamName(float t);
647 //void dom_player_join_team(entity pl);
648 void ClientConnect (void)
649 {
650         local string s;
651         float wep;
652
653         if(Ban_IsClientBanned(self))
654         {
655                 s = strcat("^1NOTE:^7 banned client ", self.netaddress, " just tried to enter\n");
656                 dropclient(self);
657                 bprint(s);
658                 return;
659         }
660
661         self.classname = "player_joining";
662         self.flags = self.flags | FL_CLIENT;
663         self.version_nagtime = time + 10 + random() * 10;
664
665         if(player_count<0)
666         {
667                 dprint("BUG player count is lower than zero, this cannot happen!\n");
668                 player_count = 0;
669         }
670
671         bot_clientconnect();
672
673         //if(cvar("g_domination"))
674         //      dom_player_join_team(self);
675
676         //JoinBestTeam(self, FALSE);
677
678         if((cvar("sv_spectate") == 1 && !g_lms) || cvar("g_campaign")) {
679                 self.classname = "observer";
680         } else {
681                 self.classname = "player";
682                 campaign_bots_may_start = 1;
683         }
684
685         self.playerid = (playerid_last = playerid_last + 1);
686         if(cvar("sv_eventlog"))
687         {
688                 if(clienttype(self) == CLIENTTYPE_REAL)
689                         s = "player";
690                 else
691                         s = "bot";
692                 GameLogEcho(strcat(":join:", ftos(self.playerid), ":", s, ":", self.netname), TRUE);
693                 s = strcat(":team:", ftos(self.playerid), ":");
694                 s = strcat(s, ftos(self.team));
695                 GameLogEcho(s, FALSE);
696         }
697
698         //stuffcmd(self, "set tmpviewsize $viewsize \n");
699
700         bprint ("^4",self.netname);
701         bprint ("^4 connected");
702
703         if(cvar("g_domination") || g_ctf)
704         {
705                 bprint(" and joined the ");
706                 bprint(ColoredTeamName(self.team));
707         }
708
709         bprint("\n");
710
711         self.welcomemessage_time = 0;
712
713         stuffcmd(self, strcat("exec maps/", mapname, ".cfg\n"));
714         // TODO: is this being used for anything else than cd tracks?
715         // Remember: SVC_CDTRACK exists. Maybe it should be used.
716
717         FixClientCvars(self);
718
719         // waypoint sprites
720         WaypointSprite_InitClient(self);
721
722         // Wazat's grappling hook
723         SetGrappleHookBindings();
724
725         // get autoswitch state from player when he toggles it
726         stuffcmd(self, "alias autoswitch \"set cl_autoswitch $1 ; cmd autoswitch $1\"\n");
727
728         // get version info from player
729         stuffcmd(self, "cmd clientversion $gameversion\n");
730
731         // send all weapon info strings
732         stuffcmd(self, "register_bestweapon clear\n"); // clear the Quake stuff
733         wep = WEP_FIRST;
734         while (wep <= WEP_LAST)
735         {
736                 weapon_action(wep, WR_REGISTER);
737                 wep = wep + 1;
738         }
739
740         // get other cvars from player
741         GetCvars(0);
742
743         // set cvar for team scoreboard
744         if (teams_matter)
745         {
746                 local float t;
747                 t = cvar("teamplay");
748                 // 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
749                 stuffcmd(self, strcat("set teamplay ", ftos(t), "\n"));
750         }
751         else
752                 stuffcmd(self, "set teamplay 0\n");
753
754         if(g_lms)
755         {
756                 self.frags = cvar("fraglimit");
757                 // no fraglimit was set, so player gets 999 lives
758                 if(self.frags < 1)
759                         self.frags = 999;
760
761                 self.frags = LMS_NewPlayerLives();
762                 if(!self.frags)
763                 {
764                         self.frags = -666;
765                 }
766         }
767         else if(g_arena)
768         {
769                 self.classname = "observer";
770                 Spawnqueue_Insert(self);
771         }
772
773         bot_relinkplayerlist();
774
775         self.jointime = time;
776 }
777
778 /*
779 =============
780 ClientDisconnect
781
782 Called when a client disconnects from the server
783 =============
784 */
785 void(entity e) DropFlag;
786 .entity chatbubbleentity;
787 .entity teambubbleentity;
788 void ClientDisconnect (void)
789 {
790         float save;
791         if(cvar("sv_eventlog"))
792                 GameLogEcho(strcat(":part:", ftos(self.playerid)), FALSE);
793         bprint ("^4",self.netname);
794         bprint ("^4 disconnected\n");
795
796         if (self.chatbubbleentity)
797         {
798                 remove (self.chatbubbleentity);
799                 self.chatbubbleentity = world;
800         }
801
802         if (self.teambubbleentity)
803         {
804                 remove (self.teambubbleentity);
805                 self.teambubbleentity = world;
806         }
807
808         WaypointSprite_PlayerGone();
809
810         DropAllRunes(self);
811         kh_Key_DropAll(self, TRUE);
812
813         if(self.flagcarried)
814                 DropFlag(self.flagcarried);
815
816         DistributeFragsAmongTeam(self, self.team, 1);
817
818         save = self.flags;
819         self.flags = self.flags - (self.flags & FL_CLIENT);
820         bot_relinkplayerlist();
821         self.flags = save;
822
823         // remove laserdot
824         if(self.weaponentity)
825                 if(self.weaponentity.lasertarget)
826                         remove(self.weaponentity.lasertarget);
827
828         if(g_arena)
829         {
830                 Spawnqueue_Unmark(self);
831                 Spawnqueue_Remove(self);
832         }
833
834         // free cvars
835         GetCvars(-1);
836 }
837
838 .float buttonchat;
839 void() ChatBubbleThink =
840 {
841         self.nextthink = time;
842         if (!self.owner.modelindex || self.owner.chatbubbleentity != self)
843         {
844                 self.owner.chatbubbleentity = world;
845                 remove(self);
846                 return;
847         }
848         setorigin(self, self.owner.origin + '0 0 15' + self.owner.maxs_z * '0 0 1');
849         if (self.owner.buttonchat && !self.owner.deadflag)
850                 self.model = self.mdl;
851         else
852                 self.model = "";
853 };
854
855 void() UpdateChatBubble =
856 {
857         if (!self.modelindex)
858                 return;
859         // spawn a chatbubble entity if needed
860         if (!self.chatbubbleentity)
861         {
862                 self.chatbubbleentity = spawn();
863                 self.chatbubbleentity.owner = self;
864                 self.chatbubbleentity.exteriormodeltoclient = self;
865                 self.chatbubbleentity.think = ChatBubbleThink;
866                 self.chatbubbleentity.nextthink = time;
867                 setmodel(self.chatbubbleentity, "models/misc/chatbubble.spr"); // precision set below
868                 setorigin(self.chatbubbleentity, self.origin + '0 0 15' + self.maxs_z * '0 0 1');
869                 self.chatbubbleentity.mdl = self.chatbubbleentity.model;
870                 self.chatbubbleentity.model = "";
871                 self.chatbubbleentity.effects = EF_LOWPRECISION;
872         }
873 }
874
875
876 void() TeamBubbleThink =
877 {
878         self.nextthink = time;
879         if (!self.owner.modelindex || self.owner.teambubbleentity != self)
880         {
881                 self.owner.teambubbleentity = world;
882                 remove(self);
883                 return;
884         }
885 //      setorigin(self, self.owner.origin + '0 0 15' + self.owner.maxs_z * '0 0 1');  // bandwidth hog. setattachment does this now
886         if (self.owner.buttonchat || self.owner.deadflag)
887                 self.model = "";
888         else
889                 self.model = self.mdl;
890
891 };
892
893 float() TeamBubble_customizeentityforclient
894 {
895         return (self.owner != other && self.owner.team == other.team && other.killcount > -666);
896 }
897
898 void() UpdateTeamBubble =
899 {
900         if (!self.modelindex || !cvar("teamplay"))
901                 return;
902         // spawn a teambubble entity if needed
903         if (!self.teambubbleentity && cvar("teamplay"))
904         {
905                 self.teambubbleentity = spawn();
906                 self.teambubbleentity.owner = self;
907                 self.teambubbleentity.exteriormodeltoclient = self;
908                 self.teambubbleentity.think = TeamBubbleThink;
909                 self.teambubbleentity.nextthink = time;
910                 setmodel(self.teambubbleentity, "models/misc/teambubble.spr"); // precision set below
911 //              setorigin(self.teambubbleentity, self.origin + '0 0 15' + self.maxs_z * '0 0 1');
912                 setorigin(self.teambubbleentity, self.teambubbleentity.origin + '0 0 15' + self.maxs_z * '0 0 1');
913                 setattachment(self.teambubbleentity, self, "");  // sticks to moving player better, also conserves bandwidth
914                 self.teambubbleentity.mdl = self.teambubbleentity.model;
915                 self.teambubbleentity.model = self.teambubbleentity.mdl;
916                 self.teambubbleentity.customizeentityforclient = TeamBubble_customizeentityforclient;
917                 self.teambubbleentity.effects = EF_LOWPRECISION;
918         }
919 }
920
921 // LordHavoc: this hack will be removed when proper _pants/_shirt layers are
922 // added to the model skins
923 /*void() UpdateColorModHack =
924 {
925         local float c;
926         c = self.clientcolors & 15;
927         // LordHavoc: only bothering to support white, green, red, yellow, blue
928              if (teamplay == 0) self.colormod = '0 0 0';
929         else if (c ==  0) self.colormod = '1.00 1.00 1.00';
930         else if (c ==  3) self.colormod = '0.10 1.73 0.10';
931         else if (c ==  4) self.colormod = '1.73 0.10 0.10';
932         else if (c == 12) self.colormod = '1.22 1.22 0.10';
933         else if (c == 13) self.colormod = '0.10 0.10 1.73';
934         else self.colormod = '1 1 1';
935 };*/
936
937 void respawn(void)
938 {
939         CopyBody(1);
940         PutClientInServer();
941 }
942
943 void player_powerups (void)
944 {
945         if (g_minstagib)
946         {
947                 self.effects = EF_FULLBRIGHT;
948                 if (self.items & IT_STRENGTH)
949                 {
950                         if (time > self.strength_finished)
951                         {
952                                 self.alpha = default_player_alpha;
953                                 self.exteriorweaponentity.alpha = default_weapon_alpha;
954                                 self.items = self.items - (self.items & IT_STRENGTH);
955                                 sprint(self, "^3Invisibility has worn off\n");
956                         }
957                 }
958                 else
959                 {
960                         if (time < self.strength_finished)
961                         {
962                                 self.alpha = cvar("g_minstagib_invis_alpha");
963                                 self.exteriorweaponentity.alpha = cvar("g_minstagib_invis_alpha");
964                                 self.items = self.items | IT_STRENGTH;
965                                 sprint(self, "^3You are invisible\n");
966                         }
967                 }
968
969                 if (self.items & IT_INVINCIBLE)
970                 {
971                         if (time > self.invincible_finished)
972                         {
973                                 self.items = self.items - (self.items & IT_INVINCIBLE);
974                                 sprint(self, "^3Speed has worn off\n");
975                         }
976                 }
977                 else
978                 {
979                         if (time < self.invincible_finished)
980                         {
981                                 self.items = self.items | IT_INVINCIBLE;
982                                 sprint(self, "^3You are on speed\n");
983                         }
984                 }
985                 return;
986         }
987
988         self.effects = self.effects - (self.effects & (EF_RED | EF_BLUE | EF_ADDITIVE | EF_FULLBRIGHT));
989         if (self.items & IT_STRENGTH)
990         {
991                 self.effects = self.effects | (EF_BLUE | EF_ADDITIVE | EF_FULLBRIGHT);
992                 if (time > self.strength_finished)
993                 {
994                         self.items = self.items - (self.items & IT_STRENGTH);
995                         sprint(self, "^3Strength has worn off\n");
996                 }
997         }
998         else
999         {
1000                 if (time < self.strength_finished)
1001                 {
1002                         self.items = self.items | IT_STRENGTH;
1003                         sprint(self, "^3Strength infuses your weapons with devastating power\n");
1004                 }
1005         }
1006         if (self.items & IT_INVINCIBLE)
1007         {
1008                 self.effects = self.effects | (EF_RED | EF_ADDITIVE | EF_FULLBRIGHT);
1009                 if (time > self.invincible_finished)
1010                 {
1011                         self.items = self.items - (self.items & IT_INVINCIBLE);
1012                         sprint(self, "^3Shield has worn off\n");
1013                 }
1014         }
1015         else
1016         {
1017                 if (time < self.invincible_finished)
1018                 {
1019                         self.items = self.items | IT_INVINCIBLE;
1020                         sprint(self, "^3Shield surrounds you\n");
1021                 }
1022         }
1023
1024         if (cvar("g_fullbrightplayers"))
1025                 self.effects = self.effects | EF_FULLBRIGHT;
1026
1027         // midair gamemode: damage only while in the air
1028         // if in midair mode, being on ground grants temporary invulnerability
1029         // (this is so that multishot weapon don't clear the ground flag on the
1030         // first damage in the frame, leaving the player vulnerable to the
1031         // remaining hits in the same frame)
1032         if (self.flags & FL_ONGROUND)
1033         if (g_midair)
1034                 self.spawnshieldtime = max(self.spawnshieldtime, time + cvar("g_midair_shieldtime"));
1035
1036         if (time < self.spawnshieldtime)
1037                 self.effects = self.effects | (EF_ADDITIVE | EF_FULLBRIGHT);
1038 }
1039
1040 float CalcRegen(float current, float stable, float regenfactor)
1041 {
1042         if(current > stable)
1043                 return current;
1044         else if(current > stable - 0.25) // when close enough, "snap"
1045                 return stable;
1046         else
1047                 return min(stable, current + (stable - current) * regenfactor * frametime);
1048 }
1049
1050 void player_regen (void)
1051 {
1052         float maxh, maxa, limith, limita, max_mod, regen_mod, rot_mod, limit_mod;
1053         maxh = cvar("g_balance_health_stable");
1054         maxa = cvar("g_balance_armor_stable");
1055         limith = cvar("g_balance_health_limit");
1056         limita = cvar("g_balance_armor_limit");
1057
1058         if (g_minstagib || (g_lms && !cvar("g_lms_regenerate")))
1059                 return;
1060
1061         max_mod = regen_mod = rot_mod = limit_mod = 1;
1062
1063         if (self.runes & RUNE_REGEN)
1064         {
1065                 if (self.runes & CURSE_VENOM) // do we have both rune/curse?
1066                 {
1067                         regen_mod = cvar("g_balance_rune_regen_combo_regenrate");
1068                         max_mod = cvar("g_balance_rune_regen_combo_hpmod");
1069                         limit_mod = cvar("g_balance_rune_regen_combo_limitmod");
1070                 }
1071                 else
1072                 {
1073                         regen_mod = cvar("g_balance_rune_regen_regenrate");
1074                         max_mod = cvar("g_balance_rune_regen_hpmod");
1075                         limit_mod = cvar("g_balance_rune_regen_limitmod");
1076                 }
1077         }
1078         else if (self.runes & CURSE_VENOM)
1079         {
1080                 max_mod = cvar("g_balance_curse_venom_hpmod");
1081                 if (self.runes & RUNE_REGEN) // do we have both rune/curse?
1082                         rot_mod = cvar("g_balance_rune_regen_combo_rotrate");
1083                 else
1084                         rot_mod = cvar("g_balance_curse_venom_rotrate");
1085                 limit_mod = cvar("g_balance_curse_venom_limitmod");
1086                 //if (!self.runes & RUNE_REGEN)
1087                 //      rot_mod = cvar("g_balance_curse_venom_rotrate");
1088         }
1089         maxh = maxh * max_mod;
1090         //maxa = maxa * max_mod;
1091         limith = limith * limit_mod;
1092         limita = limita * limit_mod;
1093
1094         if (self.armorvalue > maxa)
1095         {
1096                 if (time > self.pauserotarmor_finished)
1097                 {
1098                         self.armorvalue = max(maxa, self.armorvalue + (maxa - self.armorvalue) * cvar("g_balance_armor_rot") * frametime);
1099                         self.armorvalue = max(maxa, self.armorvalue - cvar("g_balance_armor_rotlinear") * frametime);
1100                 }
1101         }
1102         else if (self.armorvalue < maxa)
1103         {
1104                 if (time > self.pauseregen_finished)
1105                 {
1106                         self.armorvalue = CalcRegen(self.armorvalue, maxa, cvar("g_balance_armor_regen"));
1107                         self.armorvalue = min(maxa, self.armorvalue + cvar("g_balance_armor_regenlinear") * frametime);
1108                 }
1109         }
1110         if (self.health > maxh)
1111         {
1112                 if (time > self.pauserothealth_finished)
1113                 {
1114                         self.health = max(maxh, self.health + (maxh - self.health) * rot_mod*cvar("g_balance_health_rot") * frametime);
1115                         self.health = max(maxh, self.health - rot_mod*cvar("g_balance_health_rotlinear") * frametime);
1116                 }
1117         }
1118         else if (self.health < maxh)
1119         {
1120                 if (time > self.pauseregen_finished)
1121                 {
1122                         self.health = CalcRegen(self.health, maxh, regen_mod * cvar("g_balance_health_regen"));
1123                         self.health = min(maxh, self.health + regen_mod*cvar("g_balance_health_regenlinear") * frametime);
1124                 }
1125         }
1126
1127         if (self.health > limith)
1128                 self.health = limith;
1129         if (self.armorvalue > limita)
1130                 self.armorvalue = limita;
1131
1132         // if player rotted to death...  die!
1133         if(self.health < 1)
1134                 self.event_damage(self, self, 1, DEATH_ROT, self.origin, '0 0 0');
1135 }
1136
1137 /*
1138 ======================
1139 spectate mode routines
1140 ======================
1141 */
1142 void SpectateCopy(entity spectatee) {
1143         self.armortype = spectatee.armortype;
1144         self.armorvalue = spectatee.armorvalue;
1145         self.currentammo = spectatee.currentammo;
1146         self.effects = spectatee.effects;
1147         self.health = spectatee.health;
1148         self.impulse = 0;
1149         self.items = spectatee.items;
1150         self.punchangle = spectatee.punchangle;
1151         self.view_ofs = spectatee.view_ofs;
1152         self.v_angle = spectatee.v_angle;
1153         self.viewzoom = spectatee.viewzoom;
1154         setorigin(self, spectatee.origin);
1155         setsize(self, spectatee.mins, spectatee.maxs);
1156 }
1157
1158 void SpectateUpdate() {
1159         if(!self.enemy)
1160                         PutObserverInServer();
1161
1162         if (self != self.enemy) {
1163                 if(self.enemy.flags & FL_NOTARGET)
1164                         PutObserverInServer();
1165                 SpectateCopy(self.enemy);
1166                 self.dmg_take = self.enemy.dmg_take;
1167                 self.dmg_save = self.enemy.dmg_save;
1168                 self.dmg_inflictor = self.enemy.dmg_inflictor;
1169                 self.fixangle = TRUE;
1170                 self.angles = self.enemy.v_angle;
1171                 //msg_entity = self;
1172                 //WriteByte(MSG_ONE, SVC_SETANGLE);
1173                 //WriteAngle(MSG_ONE, self.enemy.v_angle_x);
1174                 //WriteAngle(MSG_ONE, self.enemy.v_angle_y);
1175                 //WriteAngle(MSG_ONE, self.enemy.v_angle_z);
1176         }
1177 }
1178
1179 float SpectateNext() {
1180         other = find(self.enemy, classname, "player");
1181         if (!other) {
1182                 other = find(other, classname, "player");
1183         }
1184         if (other) {
1185                 self.enemy = other;
1186         }
1187         if(self.enemy.classname == "player") {
1188                 msg_entity = self;
1189                 WriteByte(MSG_ONE, SVC_SETVIEW);
1190                 WriteEntity(MSG_ONE, self.enemy);
1191                 //stuffcmd(self, "set viewsize $tmpviewsize \n");
1192                 SpectateUpdate();
1193                 return 1;
1194         } else {
1195                 return 0;
1196         }
1197 }
1198
1199 /*
1200 =============
1201 ShowRespawnCountdown()
1202
1203 Update a respawn countdown display.
1204 =============
1205 */
1206 void ShowRespawnCountdown()
1207 {
1208         float number;
1209         if(self.deadflag == DEAD_NO) // just respawned?
1210                 return;
1211         else
1212         {
1213                 number = ceil(self.death_time - time);
1214                 if(number <= 0)
1215                         return;
1216                 if(number <= self.respawn_countdown)
1217                 {
1218                         self.respawn_countdown = number - 1;
1219                         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
1220                                 play2(self, strcat("announcer/robotic/", ftos(number), ".ogg"));
1221                 }
1222         }
1223 }
1224
1225 void LeaveSpectatorMode()
1226 {
1227         if(!cvar("teamplay") || cvar("g_campaign") || cvar("g_balance_teams")) {
1228                 self.classname = "player";
1229                 if(cvar("g_campaign") || cvar("g_balance_teams"))
1230                         JoinBestTeam(self, 0);
1231                 if(cvar("g_campaign"))
1232                         campaign_bots_may_start = 1;
1233                 PutClientInServer();
1234                 if(!(self.flags & FL_NOTARGET))
1235                         bprint ("^4", self.netname, "^4 is playing now\n");
1236                 centerprint(self,"");
1237                 return;
1238         } else {
1239                 stuffcmd(self,"menu_showteamselect\n");
1240                 return;
1241         }
1242 }
1243
1244 /*
1245 =============
1246 PlayerPreThink
1247
1248 Called every frame for each client before the physics are run
1249 =============
1250 */
1251 void() ctf_setstatus;
1252 .float vote_nagtime;
1253 void PlayerPreThink (void)
1254 {
1255         // version nagging
1256         if(self.version_nagtime)
1257                 if(self.cvar_g_nexuizversion)
1258                         if(time > self.version_nagtime)
1259                         {
1260                                 if(strstr(self.cvar_g_nexuizversion, "svn", 0) < 0)
1261                                         if(self.cvar_g_nexuizversion != cvar_string("g_nexuizversion"))
1262                                         {
1263                                                 dprint("^1NOTE^7 to ", self.netname, "^7 - the server is running ^3Nexuiz ", cvar_string("g_nexuizversion"), "^7, you have ^3Nexuiz ", self.cvar_g_nexuizversion, "\n");
1264                                                 sprint(self, strcat("\{1}^1NOTE: ^7the server is running ^3Nexuiz ", cvar_string("g_nexuizversion"), "^7, you have ^3Nexuiz ", self.cvar_g_nexuizversion, "\n"));
1265                                         }
1266                                 self.version_nagtime = 0;
1267                         }
1268
1269         // vote nagging
1270         if(self.cvar_scr_centertime)
1271                 if(time > self.vote_nagtime)
1272                 {
1273                         VoteNag();
1274                         self.vote_nagtime = time + self.cvar_scr_centertime * 0.6;
1275                 }
1276
1277         // GOD MODE info
1278         if(!(self.flags & FL_GODMODE)) if(self.max_armorvalue)
1279         {
1280                 sprint(self, strcat("godmode saved you ", ftos(self.max_armorvalue), " units of damage, cheater!\n"));
1281                 self.max_armorvalue = 0;
1282         }
1283
1284         if(self.classname == "player") {
1285                 local vector m1, m2;
1286
1287 //              if(self.netname == "Wazat")
1288 //                      bprint(self.classname, "\n");
1289
1290                 CheckRules_Player();
1291
1292                 if(self.button7)
1293                         PrintWelcomeMessage(self);
1294
1295                 if(g_lms || !cvar("sv_spectate"))
1296                 if((time - self.jointime) <= cvar("welcome_message_time"))
1297                         PrintWelcomeMessage(self);
1298
1299                 if (intermission_running)
1300                 {
1301                         IntermissionThink ();   // otherwise a button could be missed between
1302                         return;                                 // the think tics
1303                 }
1304
1305                 if(time > self.teleport_time)
1306                 {
1307                         self.effects = self.effects - (self.effects & EF_NODRAW);
1308                         if(self.weaponentity)
1309                                 self.weaponentity.flags = self.weaponentity.flags - (self.weaponentity.flags & EF_NODRAW);
1310                 }
1311
1312                 Nixnex_GiveCurrentWeapon();
1313
1314                 if(frametime > 0) // don't do this in cl_movement frames, just in server ticks
1315                         UpdateSelectedPlayer();
1316
1317                 if (self.deadflag != DEAD_NO)
1318                 {
1319                         float button_pressed, force_respawn;
1320                         player_anim();
1321                         button_pressed = (self.button0 || self.button2 || self.button3 || self.button6 || self.buttonuse);
1322                         force_respawn = (g_lms || cvar("g_forced_respawn"));
1323                         if (self.deadflag == DEAD_DYING)
1324                         {
1325                                 if(force_respawn)
1326                                         self.deadflag = DEAD_RESPAWNING;
1327                                 else if(!button_pressed)
1328                                         self.deadflag = DEAD_DEAD;
1329                         }
1330                         else if (self.deadflag == DEAD_DEAD)
1331                         {
1332                                 if(button_pressed)
1333                                         self.deadflag = DEAD_RESPAWNABLE;
1334                         }
1335                         else if (self.deadflag == DEAD_RESPAWNABLE)
1336                         {
1337                                 if(!button_pressed)
1338                                         self.deadflag = DEAD_RESPAWNING;
1339                         }
1340                         else if (self.deadflag == DEAD_RESPAWNING)
1341                         {
1342                                 if(time > self.death_time)
1343                                         respawn();
1344                         }
1345                         ShowRespawnCountdown();
1346                         return;
1347                 }
1348
1349                 if(g_lms && !self.deadflag && cvar("g_lms_campcheck_interval"))
1350                 {
1351                         vector dist;
1352
1353                         // calculate player movement (in 2 dimensions only, so jumping on one spot doesn't count as movement)
1354                         dist = self.oldorigin - self.origin;
1355                         dist_z = 0;
1356                         self.lms_traveled_distance += fabs(vlen(dist));
1357
1358                         if(cvar("g_campaign"))
1359                         if(!campaign_bots_may_start)
1360                         {
1361                                 self.lms_nextcheck = time + cvar("g_lms_campcheck_interval")*2;
1362                                 self.lms_traveled_distance = 0;
1363                         }
1364
1365                         if(time > self.lms_nextcheck)
1366                         {
1367                                 //sprint(self, "distance: ", ftos(self.lms_traveled_distance), "\n");
1368                                 if(self.lms_traveled_distance < cvar("g_lms_campcheck_distance"))
1369                                 {
1370                                         centerprint(self, cvar_string("g_lms_campcheck_message"));
1371                                         // FIXME KadaverJack: gibbing player here causes playermodel to bounce around, instead of eye.md3
1372                                         // I wasn't able to find out WHY that happens, so I put a workaround in place that shall prevent players from being gibbed :(
1373                                         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');
1374                                 }
1375                                 self.lms_nextcheck = time + cvar("g_lms_campcheck_interval");
1376                                 self.lms_traveled_distance = 0;
1377                         }
1378                 }
1379
1380                 if (self.button5 && !self.hook.state)
1381                 {
1382                         if (!self.crouch)
1383                         {
1384                                 self.crouch = TRUE;
1385                                 self.view_ofs = PL_CROUCH_VIEW_OFS;
1386                                 setsize (self, PL_CROUCH_MIN, PL_CROUCH_MAX);
1387                                 player_setanim(self.anim_duck, FALSE, TRUE, TRUE);
1388                         }
1389                 }
1390                 else
1391                 {
1392                         if (self.crouch)
1393                         {
1394                                 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, FALSE, self);
1395                                 if (!trace_startsolid)
1396                                 {
1397                                         self.crouch = FALSE;
1398                                         self.view_ofs = PL_VIEW_OFS;
1399                                         setsize (self, PL_MIN, PL_MAX);
1400                                 }
1401                         }
1402                 }
1403
1404                 if(cvar("sv_defaultcharacter") == 1) {
1405                         local string defaultmodel;
1406                         defaultmodel = cvar_string("sv_defaultplayermodel");
1407
1408                         if (defaultmodel != self.model)
1409                         {
1410                                 m1 = self.mins;
1411                                 m2 = self.maxs;
1412                                 setmodel_lod (self, defaultmodel);
1413                                 setsize (self, m1, m2);
1414                         }
1415
1416                         if (self.skin != cvar("sv_defaultplayerskin"))
1417                                 self.skin = cvar("sv_defaultplayerskin");
1418                 } else {
1419                         if (self.playermodel != self.model)
1420                         {
1421                                 self.playermodel = CheckPlayerModel(self.playermodel);
1422                                 m1 = self.mins;
1423                                 m2 = self.maxs;
1424                                 setmodel_lod (self, self.playermodel);
1425                                 setsize (self, m1, m2);
1426                         }
1427
1428                         if(teams_matter)
1429                         {
1430                                 if (self.skin != math_mod(stof(self.playerskin), NUM_PLAYERSKINS_TEAMPLAY))
1431                                         self.skin = math_mod(stof(self.playerskin), NUM_PLAYERSKINS_TEAMPLAY);
1432                         }
1433                         else
1434                         {
1435                                 if (self.skin != stof(self.playerskin))
1436                                         self.skin = stof(self.playerskin);
1437                         }
1438                 }
1439                 if(!teams_matter)
1440                         if(strlen(cvar_string("sv_defaultplayercolors")))
1441                                 if(self.clientcolors != cvar("sv_defaultplayercolors"))
1442                                         setcolor(self, cvar("sv_defaultplayercolors"));
1443
1444                 GrapplingHookFrame();
1445
1446                 W_WeaponFrame();
1447
1448                 {
1449                         float zoomfactor, zoomspeed, zoomdir;
1450                         zoomfactor = self.cvar_cl_zoomfactor;
1451                         if(zoomfactor < 1 || zoomfactor > 16)
1452                                 zoomfactor = 2.5;
1453                         zoomspeed = self.cvar_cl_zoomspeed;
1454                         if(zoomspeed >= 0) // < 0 is instant zoom
1455                                 if(zoomspeed < 0.5 || zoomspeed > 16)
1456                                         zoomspeed = 3.5;
1457
1458                         zoomdir = self.button4;
1459                         if(self.button3)
1460                                 if(self.weapon == WEP_NEX)
1461                                         if(!g_minstagib)
1462                                                 zoomdir = 1;
1463
1464                         if(zoomdir)
1465                                 self.has_zoomed = 1;
1466
1467                         if(self.has_zoomed)
1468                         {
1469                                 if(zoomspeed <= 0) // instant zoom
1470                                 {
1471                                         if(zoomdir)
1472                                                 self.viewzoom = 1 / zoomfactor;
1473                                         else
1474                                                 self.viewzoom = 1;
1475                                 }
1476                                 else
1477                                 {
1478                                         // geometric zoom would be:
1479                                         //   self.viewzoom = bound(1 / zoomfactor, self.viewzoom * pow(zoomfactor, (zoomdir ? -1 : 1) * frametime * zoomspeed), 1);
1480                                         // however, testing showed that arithmetic/harmonic zoom works better
1481                                         if(zoomdir)
1482                                                 // self.viewzoom = 1 / bound(1, 1 / self.viewzoom + (zoomdir ? 1 : -1) * frametime * zoomspeed * (zoomfactor - 1), zoomfactor);
1483                                                 // zoom in = arithmetic: 1x, 2x, 3x, 4x, ..., 8x
1484                                                 self.viewzoom = 1 / bound(1, 1 / self.viewzoom + frametime * zoomspeed * (zoomfactor - 1), zoomfactor);
1485                                         else
1486                                                 // self.viewzoom = bound(1 / zoomfactor, self.viewzoom + (zoomdir ? -1 : 1) * frametime * zoomspeed * (1 - 1 / zoomfactor), 1);
1487                                                 // zoom out = harmonic: 8/1x, 8/2x, 8/3x, 8/4x, ..., 8/8x
1488                                                 self.viewzoom = bound(1 / zoomfactor, self.viewzoom + frametime * zoomspeed * (1 - 1 / zoomfactor), 1);
1489                                 }
1490                         }
1491                         else
1492                                 self.viewzoom = min(1, self.viewzoom + frametime); // spawn zoom-in
1493                 }
1494
1495                 player_powerups();
1496                 player_regen();
1497                 player_anim();
1498
1499                 if (g_minstagib)
1500                         minstagib_ammocheck();
1501
1502                 ctf_setstatus();
1503                 kh_setstatus();
1504
1505                 //self.angles_y=self.v_angle_y + 90;   // temp
1506
1507                 //if (TetrisPreFrame()) return;
1508         } else if(gameover) {
1509                 if (intermission_running)
1510                         IntermissionThink ();   // otherwise a button could be missed between
1511                 return;
1512         } else if(self.classname == "observer") {
1513
1514                 if (self.flags & FL_JUMPRELEASED) {
1515                         if (self.button2 && self.version == cvar("gameversion")) {
1516                                 self.welcomemessage_time = 0;
1517                                 self.flags = self.flags - FL_JUMPRELEASED;
1518                                 LeaveSpectatorMode();
1519                                 return;
1520                         } else if(self.button0 && self.version == cvar("gameversion")) {
1521                                 self.welcomemessage_time = 0;
1522                                 self.flags = self.flags - FL_JUMPRELEASED;
1523                                 if(SpectateNext() == 1) {
1524                                         self.classname = "spectator";
1525                                 }
1526                         }
1527                 } else {
1528                         if (!(self.button0 || self.button2)) {
1529                                 self.flags = self.flags | FL_JUMPRELEASED;
1530                         }
1531                 }
1532                 PrintWelcomeMessage(self);
1533         } else if(self.classname == "spectator") {
1534                 if (self.flags & FL_JUMPRELEASED) {
1535                         if (self.button2 && self.version == cvar("gameversion")) {
1536                                 self.welcomemessage_time = 0;
1537                                 self.flags = self.flags - FL_JUMPRELEASED;
1538                                 LeaveSpectatorMode();
1539                                 return;
1540                         } else if(self.button0) {
1541                                 self.welcomemessage_time = 0;
1542                                 self.flags = self.flags - FL_JUMPRELEASED;
1543                                 if(SpectateNext() == 1) {
1544                                         self.classname = "spectator";
1545                                 } else {
1546                                         self.classname = "observer";
1547                                         PutClientInServer();
1548                                 }
1549                         } else if (self.button3) {
1550                                 self.welcomemessage_time = 0;
1551                                 self.flags = self.flags - FL_JUMPRELEASED;
1552                                 self.classname = "observer";
1553                                 PutClientInServer();
1554                         } else {
1555                                 SpectateUpdate();
1556                         }
1557                 } else {
1558                         if (!(self.button0 || self.button3)) {
1559                                 self.flags = self.flags | FL_JUMPRELEASED;
1560                         }
1561                 }
1562                 PrintWelcomeMessage(self);
1563                 self.flags = self.flags | FL_CLIENT | FL_NOTARGET;
1564         }
1565 }
1566
1567
1568 /*
1569 =============
1570 PlayerPostThink
1571
1572 Called every frame for each client after the physics are run
1573 =============
1574 */
1575 void PlayerPostThink (void)
1576 {
1577         // Savage: Check for nameless players
1578         if (strlen(self.netname) < 1) {
1579                 self.netname = "Player";
1580                 stuffcmd(self, "seta _cl_name Player\n");
1581         }
1582
1583         if(self.classname == "player") {
1584                 CheckRules_Player();
1585                 UpdateChatBubble();
1586                 UpdateTeamBubble();
1587                 if (self.impulse)
1588                         ImpulseCommands ();
1589                 if (intermission_running)
1590                         return;         // intermission or finale
1591
1592                 //PrintWelcomeMessage(self);
1593                 //if (TetrisPostFrame()) return;
1594         } else if (self.classname == "observer") {
1595                 //do nothing
1596         } else if (self.classname == "spectator") {
1597                 //do nothing
1598         }
1599         Arena_Warmup();
1600 }