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