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