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