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