0f8057656039549adea22e0f25273c7fe215df36
[voretournament/voretournament.git] / data / qcsrc / server / cl_client.qc
1 void race_send_recordtime(float msg);\r
2 void race_SendRankings(float pos, float prevpos, float del, float msg);\r
3 \r
4 void send_CSQC_teamnagger() {\r
5         WriteByte(0, SVC_TEMPENTITY);\r
6         WriteByte(0, TE_CSQC_TEAMNAGGER);\r
7 }\r
8 \r
9 void Announce(string snd) {\r
10         WriteByte(MSG_ALL, SVC_TEMPENTITY);\r
11         WriteByte(MSG_ALL, TE_CSQC_ANNOUNCE);\r
12         WriteString(MSG_ALL, snd);\r
13 }\r
14 \r
15 void AnnounceTo(entity e, string snd) {\r
16         if (clienttype(e) == CLIENTTYPE_REAL)\r
17         {\r
18                 msg_entity = e;\r
19                 WriteByte(MSG_ONE, SVC_TEMPENTITY);\r
20                 WriteByte(MSG_ONE, TE_CSQC_ANNOUNCE);\r
21                 WriteString(MSG_ONE, snd);\r
22         }\r
23 }\r
24 \r
25 float ClientData_Send(entity to, float sf)\r
26 {\r
27         if(to != self.owner)\r
28         {\r
29                 error("wtf");\r
30                 return FALSE;\r
31         }\r
32 \r
33         entity e;\r
34 \r
35         e = to;\r
36         if(to.classname == "spectator")\r
37                 e = to.enemy;\r
38 \r
39         sf = 0;\r
40 \r
41         if(e.race_completed)\r
42                 sf |= 1; // forced scoreboard\r
43         if(to.spectatee_status)\r
44                 sf |= 2; // spectator ent number follows\r
45         if(e.zoomstate)\r
46                 sf |= 4; // zoomed\r
47 \r
48         WriteByte(MSG_ENTITY, ENT_CLIENT_CLIENTDATA);\r
49         WriteByte(MSG_ENTITY, sf);\r
50 \r
51         if(sf & 2)\r
52                 WriteByte(MSG_ENTITY, to.spectatee_status);\r
53 \r
54         if(sf & 8)\r
55         {\r
56                 WriteAngle(MSG_ENTITY, e.v_angle_x);\r
57                 WriteAngle(MSG_ENTITY, e.v_angle_y);\r
58         }\r
59 \r
60         return TRUE;\r
61 }\r
62 \r
63 void ClientData_Attach()\r
64 {\r
65         Net_LinkEntity(self.clientdata = spawn(), FALSE, 0, ClientData_Send);\r
66         self.clientdata.drawonlytoclient = self;\r
67         self.clientdata.owner = self;\r
68 }\r
69 \r
70 void ClientData_Detach()\r
71 {\r
72         remove(self.clientdata);\r
73         self.clientdata = world;\r
74 }\r
75 \r
76 void ClientData_Touch(entity e)\r
77 {\r
78         e.clientdata.SendFlags = 1;\r
79 \r
80         // make it spectatable\r
81         entity e2;\r
82         FOR_EACH_REALCLIENT(e2)\r
83         {\r
84                 if(e2 != e)\r
85                         if(e2.classname == "spectator")\r
86                                 if(e2.enemy == e)\r
87                                         e2.clientdata.SendFlags = 1;\r
88         }\r
89 }\r
90 \r
91 \r
92 .vector spawnpoint_score;\r
93 .string netname_previous;\r
94 \r
95 void spawnfunc_info_player_survivor (void)\r
96 {\r
97         spawnfunc_info_player_deathmatch();\r
98 }\r
99 \r
100 void spawnfunc_info_player_start (void)\r
101 {\r
102         spawnfunc_info_player_deathmatch();\r
103 }\r
104 \r
105 void spawnfunc_info_player_deathmatch (void)\r
106 {\r
107         self.classname = "info_player_deathmatch";\r
108         relocate_spawnpoint();\r
109 }\r
110 \r
111 void spawnpoint_use()\r
112 {\r
113         if(teams_matter)\r
114         if(have_team_spawns > 0)\r
115         {\r
116                 self.team = activator.team;\r
117                 some_spawn_has_been_used = 1;\r
118         }\r
119 };\r
120 \r
121 // Returns:\r
122 //   _x: prio (-1 if unusable)\r
123 //   _y: weight\r
124 vector Spawn_Score(entity spot, entity playerlist, float teamcheck, float anypoint)\r
125 {\r
126         float shortest, thisdist;\r
127         float prio;\r
128         entity player;\r
129 \r
130         prio = 0;\r
131 \r
132         // filter out spots for the wrong team\r
133         if(teamcheck)\r
134         if(spot.team != teamcheck)\r
135                 return '-1 0 0';\r
136 \r
137         if(race_spawns)\r
138                 if(spot.target == "")\r
139                         return '-1 0 0';\r
140 \r
141         if(clienttype(self) == CLIENTTYPE_REAL)\r
142         {\r
143                 if(spot.restriction == 1)\r
144                         return '-1 0 0';\r
145         }\r
146         else\r
147         {\r
148                 if(spot.restriction == 2)\r
149                         return '-1 0 0';\r
150         }\r
151 \r
152         // filter out spots for assault\r
153         if(spot.target != "") {\r
154                 local entity ent;\r
155                 float good, found;\r
156                 ent = find(world, targetname, spot.target);\r
157 \r
158                 while(ent) {\r
159                         if(ent.classname == "target_objective")\r
160                         {\r
161                                 found = 1;\r
162                                 if(ent.health < 0 || ent.health >= ASSAULT_VALUE_INACTIVE)\r
163                                         return '-1 0 0';\r
164                                 good = 1;\r
165                         }\r
166                         else if(ent.classname == "trigger_race_checkpoint")\r
167                         {\r
168                                 found = 1;\r
169                                 if(!anypoint) // spectators may spawn everywhere\r
170 \r
171                                 {\r
172                                         if(g_race_qualifying)\r
173                                         {\r
174                                                 // spawn at first\r
175                                                 if(ent.race_checkpoint != 0)\r
176                                                         return '-1 0 0';\r
177                                                 if(spot.race_place != race_lowest_place_spawn)\r
178                                                         return '-1 0 0';\r
179                                         }\r
180                                         else\r
181                                         {\r
182                                                 if(ent.race_checkpoint != self.race_respawn_checkpoint)\r
183                                                         return '-1 0 0';\r
184                                                 // try reusing the previous spawn\r
185                                                 if(ent == self.race_respawn_spotref || spot == self.race_respawn_spotref)\r
186                                                         prio += 1;\r
187                                                 if(ent.race_checkpoint == 0)\r
188                                                 {\r
189                                                         float pl;\r
190                                                         pl = self.race_place;\r
191                                                         if(pl > race_highest_place_spawn)\r
192                                                                 pl = 0;\r
193                                                         if(pl == 0 && !self.race_started)\r
194                                                                 pl = race_highest_place_spawn; // use last place if he has not even touched finish yet\r
195                                                         if(spot.race_place != pl)\r
196                                                                 return '-1 0 0';\r
197                                                 }\r
198                                         }\r
199                                 }\r
200                                 good = 1;\r
201                         }\r
202                         ent = find(ent, targetname, spot.target);\r
203                 }\r
204 \r
205                 if(found && !good)\r
206                         return '-1 0 0';\r
207         }\r
208 \r
209         player = playerlist;\r
210         shortest = vlen(world.maxs - world.mins);\r
211         for(player = playerlist; player; player = player.chain)\r
212                 if (player != self)\r
213                 {\r
214                         thisdist = vlen(player.origin - spot.origin);\r
215                         if (thisdist < shortest)\r
216                                 shortest = thisdist;\r
217                 }\r
218         return prio * '1 0 0' + shortest * '0 1 0';\r
219 }\r
220 \r
221 float spawn_allbad;\r
222 float spawn_allgood;\r
223 entity Spawn_FilterOutBadSpots(entity firstspot, entity playerlist, float mindist, float teamcheck, float anypoint)\r
224 {\r
225         local entity spot, spotlist, spotlistend;\r
226         spawn_allgood = TRUE;\r
227         spawn_allbad = TRUE;\r
228 \r
229         spotlist = world;\r
230         spotlistend = world;\r
231 \r
232         for(spot = firstspot; spot; spot = spot.chain)\r
233         {\r
234                 spot.spawnpoint_score = Spawn_Score(spot, playerlist, teamcheck, anypoint);\r
235 \r
236                 if(cvar("spawn_debugview"))\r
237                 {\r
238                         setmodel(spot, "models/rune.mdl");\r
239                         if(spot.spawnpoint_score_y < mindist)\r
240                         {\r
241                                 spot.colormod = '1 0 0';\r
242                                 spot.scale = 1;\r
243                         }\r
244                         else\r
245                         {\r
246                                 spot.colormod = '0 1 0';\r
247                                 spot.scale = spot.spawnpoint_score_y / mindist;\r
248                         }\r
249                 }\r
250 \r
251                 if(spot.spawnpoint_score_x >= 0) // spawning allowed here\r
252                 {\r
253                         if(spot.spawnpoint_score_y < mindist)\r
254                         {\r
255                                 // too short distance\r
256                                 spawn_allgood = FALSE;\r
257                         }\r
258                         else\r
259                         {\r
260                                 // perfect\r
261                                 spawn_allbad = FALSE;\r
262 \r
263                                 if(spotlistend)\r
264                                         spotlistend.chain = spot;\r
265                                 spotlistend = spot;\r
266                                 if(!spotlist)\r
267                                         spotlist = spot;\r
268 \r
269                                 /*\r
270                                 if(teamcheck)\r
271                                 if(spot.team != teamcheck)\r
272                                         error("invalid spawn added");\r
273 \r
274                                 print("added ", etos(spot), "\n");\r
275                                 */\r
276                         }\r
277                 }\r
278         }\r
279         if(spotlistend)\r
280                 spotlistend.chain = world;\r
281 \r
282         /*\r
283                 entity e;\r
284                 if(teamcheck)\r
285                         for(e = spotlist; e; e = e.chain)\r
286                         {\r
287                                 print("seen ", etos(e), "\n");\r
288                                 if(e.team != teamcheck)\r
289                                         error("invalid spawn found");\r
290                         }\r
291         */\r
292 \r
293         return spotlist;\r
294 }\r
295 \r
296 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)\r
297 {\r
298         // weight of a point: bound(lower, mindisttoplayer, upper)^exponent\r
299         // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)\r
300         local entity spot;\r
301 \r
302         RandomSelection_Init();\r
303         for(spot = firstspot; spot; spot = spot.chain)\r
304                 RandomSelection_Add(spot, 0, string_null, pow(bound(lower, spot.spawnpoint_score_y, upper), exponent) * spot.cnt, (spot.spawnpoint_score_y >= lower) * 0.5 + spot.spawnpoint_score_x);\r
305 \r
306         return RandomSelection_chosen_ent;\r
307 }\r
308 \r
309 /*\r
310 =============\r
311 SelectSpawnPoint\r
312 \r
313 Finds a point to respawn\r
314 =============\r
315 */\r
316 entity SelectSpawnPoint (float anypoint)\r
317 {\r
318         local float teamcheck;\r
319         local entity firstspot_new;\r
320         local entity spot, firstspot, playerlist;\r
321 \r
322         spot = find (world, classname, "testplayerstart");\r
323         if (spot)\r
324                 return spot;\r
325 \r
326         teamcheck = 0;\r
327 \r
328         if(!anypoint && have_team_spawns > 0)\r
329                 teamcheck = self.team;\r
330 \r
331         // get the list of players\r
332         playerlist = findchain(classname, "player");\r
333         // get the entire list of spots\r
334         firstspot = findchain(classname, "info_player_deathmatch");\r
335         // filter out the bad ones\r
336         // (note this returns the original list if none survived)\r
337         if(anypoint)\r
338         {\r
339                 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);\r
340         }\r
341         else\r
342         {\r
343                 firstspot_new = Spawn_FilterOutBadSpots(firstspot, playerlist, 100, teamcheck, anypoint);\r
344                 if(!firstspot_new)\r
345                         firstspot_new = Spawn_FilterOutBadSpots(firstspot, playerlist, -1, teamcheck, anypoint);\r
346                 firstspot = firstspot_new;\r
347 \r
348                 // there is 50/50 chance of choosing a random spot or the furthest spot\r
349                 // (this means that roughly every other spawn will be furthest, so you\r
350                 // usually won't get fragged at spawn twice in a row)\r
351                 if (arena_roundbased && !g_ca)\r
352                 {\r
353                         firstspot_new = Spawn_FilterOutBadSpots(firstspot, playerlist, 800, teamcheck, anypoint);\r
354                         if(firstspot_new)\r
355                                 firstspot = firstspot_new;\r
356                         spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);\r
357                 }\r
358                 else if (random() > cvar("g_spawn_furthest"))\r
359                         spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);\r
360                 else\r
361                         spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint\r
362         }\r
363 \r
364         if(cvar("spawn_debugview"))\r
365         {\r
366                 print("spot mindistance: ", vtos(spot.spawnpoint_score), "\n");\r
367 \r
368                 entity e;\r
369                 if(teamcheck)\r
370                         for(e = firstspot; e; e = e.chain)\r
371                                 if(e.team != teamcheck)\r
372                                         error("invalid spawn found");\r
373         }\r
374 \r
375         if (!spot)\r
376         {\r
377                 if(cvar("spawn_debug"))\r
378                         GotoNextMap();\r
379                 else\r
380                 {\r
381                         if(some_spawn_has_been_used)\r
382                                 return world; // team can't spawn any more, because of actions of other team\r
383                         else\r
384                                 error("Cannot find a spawn point - please fix the map!");\r
385                 }\r
386         }\r
387 \r
388         return spot;\r
389 }\r
390 \r
391 /*\r
392 =============\r
393 CheckPlayerModel\r
394 \r
395 Checks if the argument string can be a valid playermodel.\r
396 Returns a valid one in doubt.\r
397 =============\r
398 */\r
399 string FallbackPlayerModel = "models/player/vixen.zym";\r
400 string CheckPlayerModel(string plyermodel) {\r
401         if(strlen(plyermodel) < 4)\r
402                 return FallbackPlayerModel;\r
403         if( substring(plyermodel,0,14) != "models/player/")\r
404                 return FallbackPlayerModel;\r
405         else if(cvar("sv_servermodelsonly"))\r
406         {\r
407                 if(substring(plyermodel,-4,4) != ".zym")\r
408                 if(substring(plyermodel,-4,4) != ".dpm")\r
409                 if(substring(plyermodel,-4,4) != ".iqm")\r
410                 if(substring(plyermodel,-4,4) != ".md3")\r
411                 if(substring(plyermodel,-4,4) != ".psk")\r
412                         return FallbackPlayerModel;\r
413                 if(plyermodel != strtolower(plyermodel))\r
414                         return FallbackPlayerModel;\r
415                 if(!fexists(plyermodel))\r
416                         return FallbackPlayerModel;\r
417         }\r
418         return plyermodel;\r
419 }\r
420 \r
421 void setmodel_apply(string modelname, float reset_anims)\r
422 {\r
423         precache_model(modelname);\r
424         setmodel(self, modelname); // players have high precision\r
425         if(reset_anims)\r
426                 player_setupanimsformodel();\r
427 }\r
428 \r
429 string setmodel_state()\r
430 {\r
431         // set the proper belly model depending on how full we are\r
432         string newmodel_name, newmodel_extension, applymodel;\r
433 \r
434         // 4 is the extension length\r
435         newmodel_name = substring(self.playermodel, 0, strlen(self.playermodel) - 4);\r
436         newmodel_extension = substring(self.playermodel, strlen(self.playermodel) - 4, 4);\r
437 \r
438         if(self.stomach_load)\r
439                 applymodel = strcat(newmodel_name, "_state", ftos(floor(self.stomach_load)), newmodel_extension);\r
440         else\r
441                 applymodel = self.playermodel;\r
442 \r
443         return applymodel;\r
444 }\r
445 \r
446 /*\r
447 =============\r
448 Client_customizeentityforclient\r
449 =============\r
450 */\r
451 void Client_setmodel(string applymodel)\r
452 {\r
453         local vector m1, m2;\r
454         if(self.classname != "player" || self.deadflag != DEAD_NO) // prevent some bugs\r
455                 return;\r
456         if(applymodel == self.model || self.spectatee_status) // no change to apply\r
457                 return;\r
458 \r
459         applymodel = CheckPlayerModel(applymodel); // this is never "", so no endless loop\r
460         m1 = self.mins;\r
461         m2 = self.maxs;\r
462         setmodel_apply(applymodel, FALSE);\r
463         setsize (self, m1, m2);\r
464 }\r
465 \r
466 void Client_uncustomizeentityforclient()\r
467 {\r
468         self.skin = self.skinindex;\r
469 }\r
470 \r
471 float Client_customizeentityforclient()\r
472 {\r
473         entity modelsource;\r
474         string stomachmodel;\r
475 \r
476         if(self.modelindex == 0)\r
477                 return TRUE;\r
478 \r
479         // forcemodel stuff\r
480 \r
481 #ifdef PROFILING\r
482         float t0;\r
483         t0 = gettime(GETTIME_HIRES); // reference\r
484 #endif\r
485 \r
486         modelsource = self;\r
487 \r
488 #ifdef ALLOW_FORCEMODELS\r
489         if(other.cvar_cl_forceplayermodelsfromvoretournament)\r
490                 modelsource = other;\r
491         if(other.cvar_cl_forceplayermodels && sv_clforceplayermodels)\r
492                 modelsource = other;\r
493 #endif\r
494 \r
495         self.skin = modelsource.skinindex;\r
496 \r
497 #if 0\r
498         if(modelsource == self)\r
499                 self.skin = modelsource.skinindex;\r
500         else\r
501                 self.skin = mod(modelsource.skinindex, 3); // forbid the fbskins as forced skins\r
502 #endif\r
503 \r
504         // self: me\r
505         // other: the player viewing me\r
506 \r
507 #ifdef PROFILING\r
508         float t1;\r
509         t1 = gettime(GETTIME_HIRES); // reference\r
510         client_cefc_accumulator += (t1 - t0);\r
511 #endif\r
512 \r
513         // now change the predator's player model into a stomach model for the prey\r
514         // in other words, when a player is swallowed by another player, the predator becomes an inward stomach model so the prey can see theirself in the stomach\r
515         // this is only visible to the prey however, otherwise players would appear as a floating stomach to everyone (ewww)\r
516         stomachmodel = strcat(substring(self.playermodel, 0, strlen(self.playermodel) - 4), "_stomach.md3"); // 4 is the extension length\r
517 \r
518         if(other.spectatee_status)\r
519                 other = other.enemy; // also do this for the player we are spectating\r
520 \r
521         // don't do this if we have chase_active enabled, as we'd be seeing a floating stomach from third person view\r
522         if not(other.cvar_chase_active || other.classname == "observer") // the observer check prevents a bug\r
523         if(other.predator == self || other.fakepredator == self)\r
524         {\r
525                 Client_setmodel(stomachmodel);\r
526                 self.alpha = other.cvar_cl_vore_stomachmodel;\r
527                 return TRUE;\r
528         }\r
529 \r
530         Client_setmodel(setmodel_state());\r
531         if not(self.stat_eaten || self.fakeprey)\r
532                 self.alpha = default_player_alpha;\r
533         else\r
534                 self.alpha = -1; // hide all prey\r
535         return TRUE;\r
536 }\r
537 \r
538 /*\r
539 =============\r
540 PutObserverInServer\r
541 \r
542 putting a client as observer in the server\r
543 =============\r
544 */\r
545 void FixPlayermodel();\r
546 void PutObserverInServer (void)\r
547 {\r
548         entity  spot;\r
549 \r
550         race_PreSpawnObserver();\r
551 \r
552         spot = SelectSpawnPoint (TRUE);\r
553         if(!spot)\r
554                 error("No spawnpoints for observers?!?\n");\r
555         RemoveGrabber(self); // Wazat's Grabber\r
556 \r
557         if(clienttype(self) == CLIENTTYPE_REAL)\r
558         {\r
559                 msg_entity = self;\r
560                 WriteByte(MSG_ONE, SVC_SETVIEW);\r
561                 WriteEntity(MSG_ONE, self);\r
562         }\r
563 \r
564         Vore_Disconnect();\r
565 \r
566         kh_Key_DropAll(self, TRUE);\r
567 \r
568         if(self.flagcarried)\r
569                 DropFlag(self.flagcarried, world, world);\r
570 \r
571         WaypointSprite_PlayerDead();\r
572 \r
573         if not(g_ca)  // don't reset teams when moving a ca player to the spectators\r
574                 self.team = -1;  // move this as it is needed to log the player spectating in eventlog\r
575 \r
576         if(self.killcount != -666) {\r
577                 if(g_lms) {\r
578                         if(PlayerScore_Add(self, SP_LMS_RANK, 0) > 0)\r
579                                 bprint ("^4", self.netname, "^4 has no more lives left\n");\r
580                         else\r
581                                 bprint ("^4", self.netname, "^4 is spectating now\n"); // TODO turn this into a proper forfeit?\r
582                 } else\r
583                         bprint ("^4", self.netname, "^4 is spectating now\n");\r
584 \r
585                 if(self.just_joined == FALSE) {\r
586                         LogTeamchange(self.playerid, -1, 4);\r
587                 } else\r
588                         self.just_joined = FALSE;\r
589         }\r
590 \r
591         PlayerScore_Clear(self); // clear scores when needed\r
592 \r
593         self.spectatortime = time;\r
594 \r
595         self.classname = "observer";\r
596         self.iscreature = FALSE;\r
597         self.health = -666;\r
598         self.takedamage = DAMAGE_NO;\r
599         self.solid = SOLID_NOT;\r
600         self.movetype = MOVETYPE_NOCLIP;\r
601         self.flags = FL_CLIENT | FL_NOTARGET;\r
602         self.armorvalue = 666;\r
603         self.effects = 0;\r
604         self.armorvalue = cvar("g_balance_armor_start");\r
605         self.pauserotarmor_finished = 0;\r
606         self.pauserothealth_finished = 0;\r
607         self.pauseregenhealth_finished = 0;\r
608         self.pauseregenarmor_finished = 0;\r
609         self.damageforcescale = 0;\r
610         self.death_time = 0;\r
611         self.dead_frame = 0;\r
612         self.alpha = 0;\r
613         self.scale = 0;\r
614         self.fade_time = 0;\r
615         self.pain_frame = 0;\r
616         self.pain_finished = 0;\r
617         self.strength_finished = 0;\r
618         self.invincible_finished = 0;\r
619         self.pushltime = 0;\r
620         self.think = SUB_Null;\r
621         self.nextthink = 0;\r
622         self.grabber_time = 0;\r
623         self.deadflag = DEAD_NO;\r
624         self.angles = spot.angles;\r
625         self.angles_z = 0;\r
626         self.fixangle = TRUE;\r
627         self.crouch = FALSE;\r
628 \r
629         self.view_ofs = PL_VIEW_OFS;\r
630         setorigin (self, spot.origin);\r
631         setsize (self, '0 0 0', '0 0 0');\r
632         self.prevorigin = self.origin;\r
633         self.items = 0;\r
634         self.weapons = 0;\r
635         self.model = "";\r
636         FixPlayermodel();\r
637         self.model = "";\r
638         self.modelindex = 0;\r
639         self.weapon = 0;\r
640         self.weaponmodel = "";\r
641         self.weaponentity = world;\r
642         self.exteriorweaponentity = world;\r
643         self.killcount = -666;\r
644         self.velocity = '0 0 0';\r
645         self.avelocity = '0 0 0';\r
646         self.punchangle = '0 0 0';\r
647         self.punchvector = '0 0 0';\r
648         self.oldvelocity = self.velocity;\r
649         self.fire_endtime = -1;\r
650 \r
651         SetCustomizer(self, Client_customizeentityforclient, Client_uncustomizeentityforclient);\r
652 \r
653         if(g_arena)\r
654         {\r
655                 if(self.version_mismatch)\r
656                 {\r
657                         Spawnqueue_Unmark(self);\r
658                         Spawnqueue_Remove(self);\r
659                 }\r
660                 else\r
661                 {\r
662                         Spawnqueue_Insert(self);\r
663                 }\r
664         }\r
665         else if(g_lms)\r
666         {\r
667                 // Only if the player cannot play at all\r
668                 if(PlayerScore_Add(self, SP_LMS_RANK, 0) == 666)\r
669                         self.frags = FRAGS_SPECTATOR;\r
670                 else\r
671                         self.frags = FRAGS_LMS_LOSER;\r
672         }\r
673         else\r
674                 self.frags = FRAGS_SPECTATOR;\r
675 }\r
676 \r
677 float RestrictSkin(float s)\r
678 {\r
679         if(!teams_matter)\r
680                 return s;\r
681         if(s == 6)\r
682                 return 6;\r
683         return mod(s, 3);\r
684 }\r
685 \r
686 void FixPlayermodel()\r
687 {\r
688         local string defaultmodel;\r
689         local float defaultskin, chmdl, oldskin;\r
690         local vector m1, m2;\r
691 \r
692         defaultmodel = "";\r
693 \r
694         if(cvar("sv_defaultcharacter") == 1) {\r
695                 defaultskin = 0;\r
696 \r
697                 if(teams_matter)\r
698                 {\r
699                         string s;\r
700                         s = Team_ColorNameLowerCase(self.team);\r
701                         if(s != "neutral")\r
702                         {\r
703                                 defaultmodel = cvar_string(strcat("sv_defaultplayermodel_", s));\r
704                                 defaultskin = cvar(strcat("sv_defaultplayerskin_", s));\r
705                         }\r
706                 }\r
707 \r
708                 if(defaultmodel == "")\r
709                 {\r
710                         defaultmodel = cvar_string("sv_defaultplayermodel");\r
711                         defaultskin = cvar("sv_defaultplayerskin");\r
712                 }\r
713         }\r
714 \r
715         if(self.modelindex == 0 && self.deadflag == DEAD_NO)\r
716         {\r
717                 if(self.model != "")\r
718                         bprint("\{1}^1Player ", self.netname, "^1 has a zero modelindex, trying to fix...\n");\r
719                 self.model = ""; // force the != checks to return true\r
720         }\r
721 \r
722         if(defaultmodel != "")\r
723         {\r
724                 if (defaultmodel != self.model)\r
725                 {\r
726                         m1 = self.mins;\r
727                         m2 = self.maxs;\r
728                         setmodel_apply (defaultmodel, TRUE);\r
729                         setsize (self, m1, m2);\r
730                         chmdl = TRUE;\r
731                 }\r
732 \r
733                 oldskin = self.skinindex;\r
734                 self.skinindex = defaultskin;\r
735         } else {\r
736                 if (self.model == "")\r
737                 {\r
738                         self.playermodel = CheckPlayerModel(self.playermodel); // this is never "", so no endless loop\r
739                         m1 = self.mins;\r
740                         m2 = self.maxs;\r
741                         setmodel_apply (self.playermodel, TRUE);\r
742                         setsize (self, m1, m2);\r
743                         chmdl = TRUE;\r
744                 }\r
745                 // update player sounds if we changed model\r
746                 if(self.model == self.playermodel) // don't apply sounds for other models (like the player stomach state models)\r
747                         UpdatePlayerSounds();\r
748 \r
749                 oldskin = self.skinindex;\r
750                 self.skinindex = RestrictSkin(stof(self.playerskin));\r
751         }\r
752 \r
753         if(chmdl || oldskin != self.skinindex)\r
754                 self.species = player_getspecies(); // model or skin has changed\r
755 \r
756         if(!teams_matter)\r
757                 if(strlen(cvar_string("sv_defaultplayercolors")))\r
758                         if(self.clientcolors != cvar("sv_defaultplayercolors"))\r
759                                 setcolor(self, cvar("sv_defaultplayercolors"));\r
760 }\r
761 \r
762 void PlayerTouchExplode(entity p1, entity p2)\r
763 {\r
764         vector org;\r
765         org = (p1.origin + p2.origin) * 0.5;\r
766         org_z += (p1.mins_z + p2.mins_z) * 0.5;\r
767 \r
768         te_explosion(org);\r
769 \r
770         entity e;\r
771         e = spawn();\r
772         setorigin(e, org);\r
773         RadiusDamage(e, world, g_touchexplode_damage, g_touchexplode_edgedamage, g_touchexplode_radius, world, g_touchexplode_force, DEATH_TOUCHEXPLODE, world);\r
774         remove(e);\r
775 }\r
776 \r
777 /*\r
778 =============\r
779 PutClientInServer\r
780 \r
781 Called when a client spawns in the server\r
782 =============\r
783 */\r
784 //void() ctf_playerchanged;\r
785 void PutClientInServer (void)\r
786 {\r
787         if(clienttype(self) == CLIENTTYPE_BOT)\r
788         {\r
789                 self.classname = "player";\r
790         }\r
791         else if(clienttype(self) == CLIENTTYPE_REAL)\r
792         {\r
793                 msg_entity = self;\r
794                 WriteByte(MSG_ONE, SVC_SETVIEW);\r
795                 WriteEntity(MSG_ONE, self);\r
796         }\r
797 \r
798         // player is dead and becomes observer\r
799         // FIXME fix LMS scoring for new system\r
800         if(g_lms)\r
801         {\r
802                 if(PlayerScore_Add(self, SP_LMS_RANK, 0) > 0)\r
803                         self.classname = "observer";\r
804         }\r
805 \r
806         if(g_arena || (g_ca && !allowed_to_spawn))\r
807         if(!self.spawned)\r
808                 self.classname = "observer";\r
809 \r
810         if(gameover)\r
811                 self.classname = "observer";\r
812 \r
813         if(self.classname == "player" && (!g_ca || (g_ca && allowed_to_spawn))) {\r
814                 entity spot, oldself;\r
815                 float j;\r
816 \r
817                 if(self.team < 0)\r
818                         JoinBestTeam(self, FALSE, TRUE);\r
819 \r
820                 race_PreSpawn();\r
821 \r
822                 spot = SelectSpawnPoint (FALSE);\r
823                 if(!spot)\r
824                 {\r
825                         centerprint(self, "Sorry, no spawnpoints available!\nHope your team can fix it...");\r
826                         return; // spawn failed\r
827                 }\r
828 \r
829                 RemoveGrabber(self); // Wazat's Grabber\r
830 \r
831                 Vore_DeadPrey_Detach(self);\r
832                 self.fakeprey = FALSE; // clear the fakeprey status\r
833 \r
834                 self.classname = "player";\r
835                 self.wasplayer = TRUE;\r
836                 self.iscreature = TRUE;\r
837                 self.movetype = MOVETYPE_WALK;\r
838                 if(cvar("g_player_colisions"))\r
839                         self.solid = SOLID_SLIDEBOX;\r
840                 else\r
841                         self.solid = SOLID_CORPSE;\r
842                 self.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_SOLID;\r
843                 if(cvar("g_playerclip_collisions"))\r
844                         self.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;\r
845                 if(clienttype(self) == CLIENTTYPE_BOT && cvar("g_botclip_collisions"))\r
846                         self.dphitcontentsmask |= DPCONTENTS_BOTCLIP;\r
847                 self.frags = FRAGS_PLAYER;\r
848                 if(independent_players)\r
849                         MAKE_INDEPENDENT_PLAYER(self);\r
850                 self.flags = FL_CLIENT;\r
851                 self.takedamage = DAMAGE_AIM;\r
852                 self.effects = 0;\r
853                 self.air_finished = time + 12;\r
854                 self.dmg = 2;\r
855 \r
856                 if(inWarmupStage)\r
857                 {\r
858                         self.ammo_fuel = warmup_start_ammo_fuel;\r
859                         self.health = warmup_start_health;\r
860                         self.armorvalue = warmup_start_armorvalue;\r
861                         self.weapons = warmup_start_weapons;\r
862                 }\r
863                 else\r
864                 {\r
865                         self.ammo_fuel = start_ammo_fuel;\r
866                         self.health = start_health;\r
867                         self.armorvalue = start_armorvalue;\r
868                         self.weapons = start_weapons;\r
869                 }\r
870 \r
871                 self.items = start_items;\r
872                 self.switchweapon = w_getbestweapon(self);\r
873                 self.cnt = self.switchweapon;\r
874                 self.weapon = 0;\r
875 \r
876                 self.spawnshieldtime = time + cvar("g_spawnshieldtime");\r
877                 self.pauserotarmor_finished = time + cvar("g_balance_pause_armor_rot_spawn");\r
878                 self.pauserothealth_finished = time + cvar("g_balance_pause_health_rot_spawn");\r
879                 self.pauserotfuel_finished = time + cvar("g_balance_pause_fuel_rot_spawn");\r
880                 self.pauseregenhealth_finished = time + cvar("g_balance_pause_health_regen_spawn");\r
881                 self.pauseregenarmor_finished = time + cvar("g_balance_pause_armor_regen_spawn");\r
882                 //extend the pause of rotting if client was reset at the beginning of the countdown\r
883                 if(!cvar("sv_ready_restart_after_countdown") && time < game_starttime) { // TODO why is this cvar NOTted?\r
884                         self.spawnshieldtime += game_starttime - time;\r
885                         self.pauserotarmor_finished += game_starttime - time;\r
886                         self.pauserothealth_finished += game_starttime - time;\r
887                         self.pauseregenhealth_finished += game_starttime - time;\r
888                         self.pauseregenarmor_finished += game_starttime - time;\r
889                 }\r
890                 self.damageforcescale = 2;\r
891                 self.death_time = 0;\r
892                 self.dead_frame = 0;\r
893                 self.alpha = 0;\r
894                 self.scale = 0;\r
895                 self.fade_time = 0;\r
896                 self.pain_frame = 0;\r
897                 self.pain_finished = 0;\r
898                 self.strength_finished = 0;\r
899                 self.invincible_finished = 0;\r
900                 self.pushltime = 0;\r
901                 // players have no think function\r
902                 self.think = SUB_Null;\r
903                 self.nextthink = 0;\r
904                 self.grabber_time = 0;\r
905                 self.dmg_team = 0;\r
906 \r
907                 self.deadflag = DEAD_NO;\r
908                 self.stomach_load = 0;\r
909                 self.angles = spot.angles;\r
910 \r
911                 self.angles_z = 0; // never spawn tilted even if the spot says to\r
912                 self.fixangle = TRUE; // turn this way immediately\r
913                 self.velocity = '0 0 0';\r
914                 self.avelocity = '0 0 0';\r
915                 self.punchangle = '0 0 0';\r
916                 self.punchvector = '0 0 0';\r
917                 self.oldvelocity = self.velocity;\r
918                 self.fire_endtime = -1;\r
919 \r
920                 msg_entity = self;\r
921                 WRITESPECTATABLE_MSG_ONE({\r
922                         WriteByte(MSG_ONE, SVC_TEMPENTITY);\r
923                         WriteByte(MSG_ONE, TE_CSQC_SPAWN);\r
924                 });\r
925 \r
926                 SetCustomizer(self, Client_customizeentityforclient, Client_uncustomizeentityforclient);\r
927 \r
928                 self.model = "";\r
929                 FixPlayermodel();\r
930 \r
931                 self.crouch = FALSE;\r
932                 self.view_ofs = PL_VIEW_OFS;\r
933                 setsize (self, PL_MIN, PL_MAX);\r
934                 self.spawnorigin = spot.origin;\r
935                 setorigin (self, spot.origin + '0 0 1' * (1 - self.mins_z - 24));\r
936                 // don't reset back to last position, even if new position is stuck in solid\r
937                 self.oldorigin = self.origin;\r
938                 self.prevorigin = self.origin;\r
939                 self.lastteleporttime = time; // prevent insane speeds due to changing origin\r
940 \r
941                 if(g_arena)\r
942                 {\r
943                         Spawnqueue_Remove(self);\r
944                         Spawnqueue_Mark(self);\r
945                 }\r
946 \r
947                 else if(g_ca)\r
948                         self.caplayer = 1;\r
949 \r
950                 self.event_damage = PlayerDamage;\r
951 \r
952                 self.bot_attack = TRUE;\r
953 \r
954                 self.statdraintime = time + 5;\r
955                 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = 0;\r
956 \r
957                 if(self.killcount == -666) {\r
958                         PlayerScore_Clear(self);\r
959                         self.killcount = 0;\r
960                 }\r
961 \r
962                 CL_SpawnWeaponentity();\r
963                 self.alpha = default_player_alpha;\r
964                 self.colormod = '1 1 1' * cvar("g_player_brightness");\r
965                 self.exteriorweaponentity.alpha = default_weapon_alpha;\r
966 \r
967                 self.lms_nextcheck = time + cvar("g_lms_campcheck_interval")*2;\r
968                 self.lms_traveled_distance = 0;\r
969                 self.speedrunning = FALSE;\r
970 \r
971                 race_PostSpawn(spot);\r
972 \r
973                 if(cvar("spawn_debug"))\r
974                 {\r
975                         sprint(self, strcat("spawnpoint origin:  ", vtos(spot.origin), "\n"));\r
976                         remove(spot);   // usefull for checking if there are spawnpoints, that let drop through the floor\r
977                 }\r
978 \r
979                 //stuffcmd(self, "chase_active 0");\r
980                 //stuffcmd(self, "set viewsize $tmpviewsize \n");\r
981 \r
982                 if (cvar("g_spawnsound"))\r
983                         sound (self, CHAN_TRIGGER, "misc/spawn.wav", VOL_BASE, ATTN_NORM);\r
984 \r
985                 if(g_assault) {\r
986                         if(self.team == assault_attacker_team)\r
987                                 centerprint(self, "You are attacking!");\r
988                         else\r
989                                 centerprint(self, "You are defending!");\r
990                 }\r
991 \r
992                 target_voicescript_clear(self);\r
993 \r
994                 // reset fields the weapons may use\r
995         for (j = WEP_FIRST; j <= WEP_LAST; ++j)\r
996                 {\r
997             weapon_action(j, WR_RESETPLAYER);\r
998 \r
999                         // all weapons must be fully loaded when we spawn\r
1000                         entity e;\r
1001                         e = get_weaponinfo(j);\r
1002                         if(e.spawnflags & WEP_FLAG_RELOADABLE) // prevent accessing undefined cvars\r
1003                                 self.weapon_load[j] = cvar(strcat("g_balance_", e.netname, "_reload_ammo"));\r
1004                 }\r
1005 \r
1006                 oldself = self;\r
1007                 self = spot;\r
1008                         activator = oldself;\r
1009                                 SUB_UseTargets();\r
1010                         activator = world;\r
1011                 self = oldself;\r
1012         } else if(self.classname == "observer" || (g_ca && !allowed_to_spawn)) {\r
1013                 PutObserverInServer ();\r
1014         }\r
1015 \r
1016         //if(g_ctf)\r
1017         //      ctf_playerchanged();\r
1018 }\r
1019 \r
1020 float ClientInit_SendEntity(entity to, float sf)\r
1021 {\r
1022         WriteByte(MSG_ENTITY, ENT_CLIENT_INIT);\r
1023         WriteCoord(MSG_ENTITY, grabber_shotorigin_x);\r
1024         WriteCoord(MSG_ENTITY, grabber_shotorigin_y);\r
1025         WriteCoord(MSG_ENTITY, grabber_shotorigin_z);\r
1026 \r
1027         if(sv_foginterval && world.fog != "")\r
1028                 WriteString(MSG_ENTITY, world.fog);\r
1029         else\r
1030                 WriteString(MSG_ENTITY, "");\r
1031         WriteCoord(MSG_ENTITY, cvar("g_campaign"));\r
1032         WriteByte(MSG_ENTITY, cvar("g_balance_armor_blockpercent") * 255.0);\r
1033         WriteByte(MSG_ENTITY, cvar("g_balance_weaponswitchdelay") * 255.0);\r
1034 \r
1035         WriteCoord(MSG_ENTITY, cvar("g_vore"));\r
1036         WriteCoord(MSG_ENTITY, cvar("g_balance_vore_swallow_limit"));\r
1037         return TRUE;\r
1038 }\r
1039 \r
1040 void ClientInit_Spawn()\r
1041 {\r
1042         Net_LinkEntity(spawn(), FALSE, 0, ClientInit_SendEntity);\r
1043 }\r
1044 \r
1045 /*\r
1046 =============\r
1047 SetNewParms\r
1048 =============\r
1049 */\r
1050 void SetNewParms (void)\r
1051 {\r
1052         // initialize parms for a new player\r
1053         parm1 = -(86400 * 366);\r
1054 }\r
1055 \r
1056 /*\r
1057 =============\r
1058 SetChangeParms\r
1059 =============\r
1060 */\r
1061 void SetChangeParms (void)\r
1062 {\r
1063         // save parms for level change\r
1064         parm1 = self.parm_idlesince - time;\r
1065 }\r
1066 \r
1067 /*\r
1068 =============\r
1069 DecodeLevelParms\r
1070 =============\r
1071 */\r
1072 void DecodeLevelParms (void)\r
1073 {\r
1074         // load parms\r
1075         self.parm_idlesince = parm1;\r
1076         if(self.parm_idlesince == -(86400 * 366))\r
1077                 self.parm_idlesince = time;\r
1078 \r
1079         // whatever happens, allow 60 seconds of idling directly after connect for map loading\r
1080         self.parm_idlesince = max(self.parm_idlesince, time - sv_maxidle + 60);\r
1081 }\r
1082 \r
1083 /*\r
1084 =============\r
1085 ClientKill\r
1086 \r
1087 Called when a client types 'kill' in the console\r
1088 =============\r
1089 */\r
1090 \r
1091 void ClientKill_Now_TeamChange()\r
1092 {\r
1093         if(self.killindicator_teamchange == -1)\r
1094         {\r
1095                 self.team = -1;\r
1096                 JoinBestTeam( self, FALSE, FALSE );\r
1097         }\r
1098         else\r
1099                 SV_ChangeTeam(self.killindicator_teamchange - 1);\r
1100 }\r
1101 \r
1102 void ClientKill_Now()\r
1103 {\r
1104         if(self.killindicator_teamchange)\r
1105                 ClientKill_Now_TeamChange();\r
1106 \r
1107         // in any case:\r
1108         Damage(self, self, self, 100000, DEATH_KILL, self.origin, '0 0 0');\r
1109 \r
1110         if(self.killindicator)\r
1111         {\r
1112                 dprint("Cleaned up after a leaked kill indicator.\n");\r
1113                 remove(self.killindicator);\r
1114                 self.killindicator = world;\r
1115         }\r
1116 }\r
1117 void KillIndicator_Think()\r
1118 {\r
1119         if (!self.owner.modelindex)\r
1120         {\r
1121                 self.owner.killindicator = world;\r
1122                 remove(self);\r
1123                 return;\r
1124         }\r
1125 \r
1126         if(self.cnt <= 0)\r
1127         {\r
1128                 self = self.owner;\r
1129                 ClientKill_Now(); // no oldself needed\r
1130                 return;\r
1131         }\r
1132         else\r
1133         {\r
1134                 if(self.cnt <= 10)\r
1135                         setmodel(self, strcat("models/sprites/", ftos(self.cnt), ".spr32"));\r
1136                 if(clienttype(self.owner) == CLIENTTYPE_REAL)\r
1137                 {\r
1138                         if(self.cnt <= 10)\r
1139                                 AnnounceTo(self.owner, strcat(ftos(self.cnt), ""));\r
1140                         if(self.owner.killindicator_teamchange)\r
1141                         {\r
1142                                 if(self.owner.killindicator_teamchange == -1)\r
1143                                         centerprint(self.owner, strcat("Changing team in ", ftos(self.cnt), " seconds"));\r
1144                                 else\r
1145                                         centerprint(self.owner, strcat("Changing to ", ColoredTeamName(self.owner.killindicator_teamchange), " in ", ftos(self.cnt), " seconds"));\r
1146                         }\r
1147                         else\r
1148                                 centerprint(self.owner, strcat("^1Suicide in ", ftos(self.cnt), " seconds"));\r
1149                 }\r
1150                 self.nextthink = time + 1;\r
1151                 self.cnt -= 1;\r
1152         }\r
1153 }\r
1154 \r
1155 void ClientKill_TeamChange (float targetteam) // 0 = don't change, -1 = auto\r
1156 {\r
1157         float killtime;\r
1158         entity e;\r
1159         killtime = cvar("g_balance_kill_delay");\r
1160 \r
1161         if(g_race_qualifying)\r
1162                 killtime = 0;\r
1163 \r
1164         self.killindicator_teamchange = targetteam;\r
1165 \r
1166         if(!self.killindicator)\r
1167         {\r
1168                 if(killtime <= 0 || !self.modelindex || self.deadflag != DEAD_NO)\r
1169                 {\r
1170                         ClientKill_Now();\r
1171                 }\r
1172                 else\r
1173                 {\r
1174                         self.killindicator = spawn();\r
1175                         self.killindicator.owner = self;\r
1176                         self.killindicator.scale = 0.5;\r
1177                         setattachment(self.killindicator, self, "");\r
1178                         setorigin(self.killindicator, '0 0 52');\r
1179                         self.killindicator.think = KillIndicator_Think;\r
1180                         self.killindicator.nextthink = time + (self.lip) * 0.05;\r
1181                         self.killindicator.cnt = ceil(killtime);\r
1182                         self.killindicator.count = bound(0, ceil(killtime), 10);\r
1183                         sprint(self, strcat("^1You'll be dead in ", ftos(self.killindicator.cnt), " seconds\n"));\r
1184 \r
1185                         for(e = world; (e = find(e, classname, "body")) != world; )\r
1186                         {\r
1187                                 if(e.enemy != self)\r
1188                                         continue;\r
1189                                 e.killindicator = spawn();\r
1190                                 e.killindicator.owner = e;\r
1191                                 e.killindicator.scale = 0.5;\r
1192                                 setattachment(e.killindicator, e, "");\r
1193                                 setorigin(e.killindicator, '0 0 52');\r
1194                                 e.killindicator.think = KillIndicator_Think;\r
1195                                 e.killindicator.nextthink = time + (e.lip) * 0.05;\r
1196                                 e.killindicator.cnt = ceil(killtime);\r
1197                         }\r
1198                         self.lip = 0;\r
1199                 }\r
1200         }\r
1201         if(self.killindicator)\r
1202         {\r
1203                 if(targetteam)\r
1204                         self.killindicator.colormod = TeamColor(targetteam);\r
1205                 else\r
1206                         self.killindicator.colormod = '0 0 0';\r
1207         }\r
1208 }\r
1209 \r
1210 void ClientKill (void)\r
1211 {\r
1212         ClientKill_TeamChange(0);\r
1213 }\r
1214 \r
1215 void DoTeamChange(float destteam)\r
1216 {\r
1217         float t, c0;\r
1218         if(!teams_matter)\r
1219         {\r
1220                 if(destteam >= 0)\r
1221                         SetPlayerColors(self, destteam);\r
1222                 return;\r
1223         }\r
1224         if(self.classname == "player")\r
1225         if(destteam == -1)\r
1226         {\r
1227                 CheckAllowedTeams(self);\r
1228                 t = FindSmallestTeam(self, TRUE);\r
1229                 switch(self.team)\r
1230                 {\r
1231                         case COLOR_TEAM1: c0 = c1; break;\r
1232                         case COLOR_TEAM2: c0 = c2; break;\r
1233                         case COLOR_TEAM3: c0 = c3; break;\r
1234                         case COLOR_TEAM4: c0 = c4; break;\r
1235                         default:          c0 = 999;\r
1236                 }\r
1237                 switch(t)\r
1238                 {\r
1239                         case 1:\r
1240                                 if(c0 > c1)\r
1241                                         destteam = COLOR_TEAM1;\r
1242                                 break;\r
1243                         case 2:\r
1244                                 if(c0 > c2)\r
1245                                         destteam = COLOR_TEAM2;\r
1246                                 break;\r
1247                         case 3:\r
1248                                 if(c0 > c3)\r
1249                                         destteam = COLOR_TEAM3;\r
1250                                 break;\r
1251                         case 4:\r
1252                                 if(c0 > c4)\r
1253                                         destteam = COLOR_TEAM4;\r
1254                                 break;\r
1255                 }\r
1256                 if(destteam == -1)\r
1257                         return;\r
1258         }\r
1259         if(destteam == self.team && destteam >= 0 && !self.killindicator)\r
1260                 return;\r
1261         ClientKill_TeamChange(destteam);\r
1262 }\r
1263 \r
1264 void FixClientCvars(entity e)\r
1265 {\r
1266         // send prediction settings to the client\r
1267         stuffcmd(e, "\nin_bindmap 0 0\n");\r
1268         if(g_race || g_cts)\r
1269                 stuffcmd(e, "cl_cmd settemp cl_movecliptokeyboard 2\n");\r
1270         if(cvar("g_antilag") == 3) // client side hitscan\r
1271                 //stuffcmd(e, "cl_cmd settemp cl_prydoncursor -1\ncl_cmd settemp cl_prydoncursor_notrace 0\n");\r
1272                 stuffcmd(e, "cl_cmd settemp cl_prydoncursor_notrace 0\n");\r
1273         /*\r
1274          * we no longer need to stuff this. Remove this comment block if you feel\r
1275          * 2.3 and higher (or was it 2.2.3?) don't need these any more\r
1276         stuffcmd(e, strcat("cl_gravity ", ftos(cvar("sv_gravity")), "\n"));\r
1277         stuffcmd(e, strcat("cl_movement_accelerate ", ftos(cvar("sv_accelerate")), "\n"));\r
1278         stuffcmd(e, strcat("cl_movement_friction ", ftos(cvar("sv_friction")), "\n"));\r
1279         stuffcmd(e, strcat("cl_movement_maxspeed ", ftos(cvar("sv_maxspeed")), "\n"));\r
1280         stuffcmd(e, strcat("cl_movement_airaccelerate ", ftos(cvar("sv_airaccelerate")), "\n"));\r
1281         stuffcmd(e, strcat("cl_movement_maxairspeed ", ftos(cvar("sv_maxairspeed")), "\n"));\r
1282         stuffcmd(e, strcat("cl_movement_stopspeed ", ftos(cvar("sv_stopspeed")), "\n"));\r
1283         stuffcmd(e, strcat("cl_movement_jumpvelocity ", ftos(cvar("sv_jumpvelocity")), "\n"));\r
1284         stuffcmd(e, strcat("cl_movement_stepheight ", ftos(cvar("sv_stepheight")), "\n"));\r
1285         stuffcmd(e, strcat("set cl_movement_friction_on_land ", ftos(cvar("sv_friction_on_land")), "\n"));\r
1286         stuffcmd(e, strcat("set cl_movement_airaccel_qw ", ftos(cvar("sv_airaccel_qw")), "\n"));\r
1287         stuffcmd(e, strcat("set cl_movement_airaccel_sideways_friction ", ftos(cvar("sv_airaccel_sideways_friction")), "\n"));\r
1288         stuffcmd(e, "cl_movement_edgefriction 1\n");\r
1289          */\r
1290 }\r
1291 \r
1292 /*\r
1293 =============\r
1294 ClientConnect\r
1295 \r
1296 Called when a client connects to the server\r
1297 =============\r
1298 */\r
1299 //void ctf_clientconnect();\r
1300 string ColoredTeamName(float t);\r
1301 void DecodeLevelParms (void);\r
1302 //void dom_player_join_team(entity pl);\r
1303 #ifdef UID\r
1304 .float uid_kicktime;\r
1305 .string uid;\r
1306 #endif\r
1307 void ClientConnect (void)\r
1308 {\r
1309         float t;\r
1310 \r
1311         if(self.flags & FL_CLIENT)\r
1312         {\r
1313                 print("Warning: ClientConnect, but already connected!\n");\r
1314                 return;\r
1315         }\r
1316 \r
1317         if(Ban_MaybeEnforceBan(self))\r
1318                 return;\r
1319 \r
1320         DecodeLevelParms();\r
1321 \r
1322         self.classname = "player_joining";\r
1323 \r
1324         self.flags = FL_CLIENT;\r
1325         self.version_nagtime = time + 10 + random() * 10;\r
1326 \r
1327         if(player_count<0)\r
1328         {\r
1329                 dprint("BUG player count is lower than zero, this cannot happen!\n");\r
1330                 player_count = 0;\r
1331         }\r
1332 \r
1333         PlayerScore_Attach(self);\r
1334         ClientData_Attach();\r
1335 \r
1336         bot_clientconnect();\r
1337 \r
1338         playerdemo_init();\r
1339 \r
1340         anticheat_init();\r
1341         \r
1342         race_PreSpawnObserver();\r
1343 \r
1344         //if(g_domination)\r
1345         //      dom_player_join_team(self);\r
1346 \r
1347         JoinBestTeam(self, FALSE, FALSE); // if the team number is valid, keep it\r
1348 \r
1349         if((cvar("sv_spectate") == 1 && !g_lms) || cvar("g_campaign")) {\r
1350                 self.classname = "observer";\r
1351         } else {\r
1352                 if(teams_matter)\r
1353                 {\r
1354                         if(cvar("g_balance_teams") || cvar("g_balance_teams_force"))\r
1355                         {\r
1356                                 self.classname = "player";\r
1357                                 campaign_bots_may_start = 1;\r
1358                         }\r
1359                         else\r
1360                         {\r
1361                                 self.classname = "observer"; // do it anyway\r
1362                         }\r
1363                 }\r
1364                 else\r
1365                 {\r
1366                         self.classname = "player";\r
1367                         campaign_bots_may_start = 1;\r
1368                 }\r
1369         }\r
1370 \r
1371         self.playerid = (playerid_last = playerid_last + 1);\r
1372 \r
1373         if(cvar("sv_eventlog"))\r
1374                 GameLogEcho(strcat(":join:", ftos(self.playerid), ":", ftos(num_for_edict(self)), ":", ((clienttype(self) == CLIENTTYPE_REAL) ? self.netaddress : "bot"), ":", self.netname));\r
1375 \r
1376         LogTeamchange(self.playerid, self.team, 1);\r
1377 \r
1378         self.just_joined = TRUE;  // stop spamming the eventlog with additional lines when the client connects\r
1379 \r
1380         self.netname_previous = strzone(self.netname);\r
1381 \r
1382         bprint("^4", self.netname, "^4 connected");\r
1383 \r
1384         if(self.classname != "observer" && (g_domination || g_ctf))\r
1385                 bprint(" and joined the ", ColoredTeamName(self.team));\r
1386 \r
1387         bprint("\n");\r
1388 \r
1389         self.welcomemessage_time = 0;\r
1390 \r
1391         stuffcmd(self, strcat(clientstuff, "\n"));\r
1392         stuffcmd(self, strcat("exec maps/", mapname, ".cfg\n"));\r
1393         stuffcmd(self, "cl_particles_reloadeffects\n");\r
1394 \r
1395         FixClientCvars(self);\r
1396 \r
1397         // spawnfunc_waypoint sprites\r
1398         WaypointSprite_InitClient(self);\r
1399 \r
1400         // Wazat's grabber\r
1401         SetGrabberBindings();\r
1402 \r
1403         // get autoswitch state from player when he toggles it\r
1404         stuffcmd(self, "alias autoswitch \"set cl_autoswitch $1 ; cmd autoswitch $1\"\n"); // default.cfg-ed in 2.4.1\r
1405 \r
1406         // get version info from player\r
1407         stuffcmd(self, "cmd clientversion $gameversion\n");\r
1408 \r
1409         // get other cvars from player\r
1410         GetCvars(0);\r
1411 \r
1412         // set cvar for team scoreboard\r
1413         stuffcmd(self, strcat("set teamplay ", ftos(teamplay), "\n"));\r
1414 \r
1415         // notify about available teams\r
1416         if(teams_matter)\r
1417         {\r
1418                 CheckAllowedTeams(self);\r
1419                 t = 0; if(c1 >= 0) t |= 1; if(c2 >= 0) t |= 2; if(c3 >= 0) t |= 4; if(c4 >= 0) t |= 8;\r
1420                 stuffcmd(self, strcat("set _teams_available ", ftos(t), "\n"));\r
1421         }\r
1422         else\r
1423                 stuffcmd(self, "set _teams_available 0\n");\r
1424 \r
1425         stuffcmd(self, strcat("set gametype ", ftos(game), "\n"));\r
1426 \r
1427         if(g_arena || g_ca)\r
1428         {\r
1429                 self.classname = "observer";\r
1430                 if(g_arena)\r
1431                         Spawnqueue_Insert(self);\r
1432         }\r
1433         /*else if(g_ctf)\r
1434         {\r
1435                 ctf_clientconnect();\r
1436         }*/\r
1437 \r
1438         if(teams_matter || radar_showennemies)\r
1439                 attach_entcs();\r
1440 \r
1441         bot_relinkplayerlist();\r
1442 \r
1443         self.spectatortime = time;\r
1444         if(blockSpectators)\r
1445         {\r
1446                 sprint(self, strcat("^7You have to become a player within the next ", ftos(cvar("g_maxplayers_spectator_blocktime")), " seconds, otherwise you will be kicked, because spectators aren't allowed at this time!\n"));\r
1447         }\r
1448 \r
1449         self.jointime = time;\r
1450         self.allowedTimeouts = cvar("sv_timeout_number");\r
1451 \r
1452         if(clienttype(self) == CLIENTTYPE_REAL)\r
1453         {\r
1454                 if(cvar("g_bugrigs"))\r
1455                         stuffcmd(self, "cl_cmd settemp chase_active 1\n");\r
1456         }\r
1457 \r
1458         if(g_lms)\r
1459         {\r
1460                 if(PlayerScore_Add(self, SP_LMS_LIVES, LMS_NewPlayerLives()) <= 0)\r
1461                 {\r
1462                         PlayerScore_Add(self, SP_LMS_RANK, 666);\r
1463                         self.frags = FRAGS_SPECTATOR;\r
1464                 }\r
1465         }\r
1466 \r
1467         if(!sv_foginterval && world.fog != "")\r
1468                 stuffcmd(self, strcat("\nfog ", world.fog, "\nr_fog_exp2 0\nr_drawfog 1\n"));\r
1469 \r
1470         SoundEntity_Attach(self);\r
1471 \r
1472         if(cvar("g_hitplots") || strstrofs(strcat(" ", cvar_string("g_hitplots_individuals"), " "), strcat(" ", self.netaddress, " "), 0) >= 0)\r
1473         {\r
1474                 self.hitplotfh = fopen(strcat("hits-", matchid, "-", self.netaddress, "-", ftos(self.playerid), ".plot"), FILE_WRITE);\r
1475                 fputs(self.hitplotfh, strcat("#name ", self.netname, "\n"));\r
1476         }\r
1477         else\r
1478                 self.hitplotfh = -1;\r
1479 \r
1480 #ifdef UID\r
1481         if(clienttype(self) == CLIENTTYPE_REAL)\r
1482         if not(self.uid)\r
1483                 self.uid_kicktime = time + 60;\r
1484 #endif\r
1485 \r
1486         if(g_race || g_cts) {\r
1487                 string rr;\r
1488                 if(g_cts)\r
1489                         rr = CTS_RECORD;\r
1490                 else\r
1491                         rr = RACE_RECORD;\r
1492                 t = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "time")));\r
1493 \r
1494                 race_send_recordtime(MSG_ONE);\r
1495                 race_send_speedaward(MSG_ONE);\r
1496 \r
1497                 speedaward_alltimebest = stof(db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed")));\r
1498                 speedaward_alltimebest_holder = db_get(ServerProgsDB, strcat(GetMapname(), rr, "speed/netname"));\r
1499                 race_send_speedaward_alltimebest(MSG_ONE);\r
1500 \r
1501                 float i;\r
1502                 for (i = 1; i <= RANKINGS_CNT; ++i) {\r
1503                         race_SendRankings(i, 0, 0, MSG_ONE);\r
1504                 }\r
1505         }\r
1506         else if(cvar("sv_teamnagger") && !g_ca) // teamnagger is currently bad for ca\r
1507                 send_CSQC_teamnagger();\r
1508 \r
1509         CheatInitClient();\r
1510 }\r
1511 \r
1512 /*\r
1513 =============\r
1514 ClientDisconnect\r
1515 \r
1516 Called when a client disconnects from the server\r
1517 =============\r
1518 */\r
1519 .entity chatbubbleentity;\r
1520 .entity teambubbleentity;\r
1521 void ReadyCount();\r
1522 void ClientDisconnect (void)\r
1523 {\r
1524         if not(self.flags & FL_CLIENT)\r
1525         {\r
1526                 print("Warning: ClientDisconnect without ClientConnect\n");\r
1527                 return;\r
1528         }\r
1529 \r
1530         Vore_Disconnect();\r
1531 \r
1532         CheatShutdownClient();\r
1533 \r
1534         if(self.hitplotfh >= 0)\r
1535         {\r
1536                 fclose(self.hitplotfh);\r
1537                 self.hitplotfh = -1;\r
1538         }\r
1539 \r
1540         anticheat_report();\r
1541         anticheat_shutdown();\r
1542 \r
1543         playerdemo_shutdown();\r
1544 \r
1545         bot_clientdisconnect();\r
1546 \r
1547         if(self.entcs)\r
1548                 detach_entcs();\r
1549 \r
1550         if(cvar("sv_eventlog"))\r
1551                 GameLogEcho(strcat(":part:", ftos(self.playerid)));\r
1552         bprint ("^4",self.netname);\r
1553         bprint ("^4 disconnected\n");\r
1554 \r
1555         SoundEntity_Detach(self);\r
1556 \r
1557         kh_Key_DropAll(self, TRUE);\r
1558 \r
1559         if(self.flagcarried)\r
1560                 DropFlag(self.flagcarried, world, world);\r
1561         // Here, everything has been done that requires this player to be a client.\r
1562 \r
1563         self.flags &~= FL_CLIENT;\r
1564 \r
1565         if (self.chatbubbleentity)\r
1566                 remove (self.chatbubbleentity);\r
1567 \r
1568         if (self.teambubbleentity)\r
1569                 remove (self.teambubbleentity);\r
1570 \r
1571         if (self.killindicator)\r
1572                 remove (self.killindicator);\r
1573 \r
1574         WaypointSprite_PlayerGone();\r
1575 \r
1576         bot_relinkplayerlist();\r
1577 \r
1578         if(g_arena)\r
1579         {\r
1580                 Spawnqueue_Unmark(self);\r
1581                 Spawnqueue_Remove(self);\r
1582         }\r
1583 \r
1584         ClientData_Detach();\r
1585         PlayerScore_Detach(self);\r
1586 \r
1587         if(self.netname_previous)\r
1588                 strunzone(self.netname_previous);\r
1589         if(self.clientstatus)\r
1590                 strunzone(self.clientstatus);\r
1591 \r
1592         ClearPlayerSounds();\r
1593 \r
1594         if(self.personal)\r
1595                 remove(self.personal);\r
1596 \r
1597         self.playerid = 0;\r
1598         ReadyCount();\r
1599 \r
1600         // free cvars\r
1601         GetCvars(-1);\r
1602 }\r
1603 \r
1604 .float BUTTON_CHAT;\r
1605 void ChatBubbleThink()\r
1606 {\r
1607         self.nextthink = time;\r
1608         if (!self.owner.modelindex || self.owner.chatbubbleentity != self)\r
1609         {\r
1610                 if(self.owner) // but why can that ever be world?\r
1611                         self.owner.chatbubbleentity = world;\r
1612                 remove(self);\r
1613                 return;\r
1614         }\r
1615         if ((self.owner.BUTTON_CHAT && !self.owner.deadflag && !self.owner.stat_eaten && self.owner.fakepredator.classname != "player")\r
1616 #ifdef TETRIS\r
1617                 || self.owner.tetris_on\r
1618 #endif\r
1619         )\r
1620                 self.model = self.mdl;\r
1621         else\r
1622                 self.model = "";\r
1623 };\r
1624 \r
1625 void UpdateChatBubble()\r
1626 {\r
1627         if (!self.modelindex)\r
1628                 return;\r
1629         // spawn a chatbubble entity if needed\r
1630         if (!self.chatbubbleentity)\r
1631         {\r
1632                 self.chatbubbleentity = spawn();\r
1633                 self.chatbubbleentity.owner = self;\r
1634                 self.chatbubbleentity.exteriormodeltoclient = self;\r
1635                 self.chatbubbleentity.think = ChatBubbleThink;\r
1636                 self.chatbubbleentity.nextthink = time;\r
1637                 setmodel(self.chatbubbleentity, "models/misc/chatbubble.spr"); // precision set below\r
1638                 //setorigin(self.chatbubbleentity, self.origin + '0 0 15' + self.maxs_z * '0 0 1');\r
1639                 setorigin(self.chatbubbleentity, '0 0 15' + self.maxs_z * '0 0 1');\r
1640                 setattachment(self.chatbubbleentity, self, "");  // sticks to moving player better, also conserves bandwidth\r
1641                 self.chatbubbleentity.mdl = self.chatbubbleentity.model;\r
1642                 self.chatbubbleentity.model = "";\r
1643                 self.chatbubbleentity.effects = EF_LOWPRECISION;\r
1644         }\r
1645 }\r
1646 \r
1647 void TeamBubbleThink()\r
1648 {\r
1649         self.nextthink = time;\r
1650         if (!self.owner.modelindex || self.owner.teambubbleentity != self)\r
1651         {\r
1652                 if(self.owner) // but why can that ever be world?\r
1653                         self.owner.teambubbleentity = world;\r
1654                 remove(self);\r
1655                 return;\r
1656         }\r
1657 //      setorigin(self, self.owner.origin + '0 0 15' + self.owner.maxs_z * '0 0 1');  // bandwidth hog. setattachment does this now\r
1658         if (self.owner.BUTTON_CHAT || self.owner.deadflag || self.owner.killindicator || self.owner.stat_eaten)\r
1659                 self.model = "";\r
1660         else\r
1661         {\r
1662                 if(cvar("g_balance_vore_teamheal") && cvar("g_vore_teamvore"))\r
1663                 if(self.owner.health < cvar("g_balance_vore_teamheal_stable"))\r
1664                         setmodel(self, "models/misc/teambubbleheal.spr"); // indicate that this player can be teamhealed\r
1665                 else\r
1666                         setmodel(self, "models/misc/teambubble.spr");\r
1667         }\r
1668 };\r
1669 \r
1670 float TeamBubble_customizeentityforclient()\r
1671 {\r
1672         return (self.owner != other && self.owner.team == other.team && other.killcount > -666);\r
1673 }\r
1674 \r
1675 void UpdateTeamBubble()\r
1676 {\r
1677         if (!self.modelindex || !teams_matter)\r
1678                 return;\r
1679         // spawn a teambubble entity if needed\r
1680         if (!self.teambubbleentity && teams_matter)\r
1681         {\r
1682                 self.teambubbleentity = spawn();\r
1683                 self.teambubbleentity.owner = self;\r
1684                 self.teambubbleentity.exteriormodeltoclient = self;\r
1685                 self.teambubbleentity.think = TeamBubbleThink;\r
1686                 self.teambubbleentity.nextthink = time;\r
1687                 setmodel(self.teambubbleentity, "models/misc/teambubble.spr"); // precision set below\r
1688 //              setorigin(self.teambubbleentity, self.origin + '0 0 15' + self.maxs_z * '0 0 1');\r
1689                 setorigin(self.teambubbleentity, '0 0 15' + self.maxs_z * '0 0 1');\r
1690                 setattachment(self.teambubbleentity, self, "");  // sticks to moving player better, also conserves bandwidth\r
1691 //              self.teambubbleentity.mdl = self.teambubbleentity.model;\r
1692 //              self.teambubbleentity.model = self.teambubbleentity.mdl;\r
1693                 self.teambubbleentity.customizeentityforclient = TeamBubble_customizeentityforclient;\r
1694                 self.teambubbleentity.effects = EF_LOWPRECISION;\r
1695         }\r
1696 }\r
1697 \r
1698 // LordHavoc: this hack will be removed when proper _pants/_shirt layers are\r
1699 // added to the model skins\r
1700 /*void UpdateColorModHack()\r
1701 {\r
1702         local float c;\r
1703         c = self.clientcolors & 15;\r
1704         // LordHavoc: only bothering to support white, green, red, yellow, blue\r
1705              if (!teams_matter) self.colormod = '0 0 0';\r
1706         else if (c ==  0) self.colormod = '1.00 1.00 1.00';\r
1707         else if (c ==  3) self.colormod = '0.10 1.73 0.10';\r
1708         else if (c ==  4) self.colormod = '1.73 0.10 0.10';\r
1709         else if (c == 12) self.colormod = '1.22 1.22 0.10';\r
1710         else if (c == 13) self.colormod = '0.10 0.10 1.73';\r
1711         else self.colormod = '1 1 1';\r
1712 };*/\r
1713 \r
1714 .float oldcolormap;\r
1715 void respawn(void)\r
1716 {\r
1717         if(self.alpha >= 0 && self.modelindex != 0 && cvar("g_respawn_ghosts"))\r
1718         {\r
1719                 self.solid = SOLID_NOT;\r
1720                 self.takedamage = DAMAGE_NO;\r
1721                 self.movetype = MOVETYPE_FLY;\r
1722                 self.velocity = '0 0 1' * cvar("g_respawn_ghosts_speed");\r
1723                 self.avelocity = randomvec() * cvar("g_respawn_ghosts_speed") * 3 - randomvec() * cvar("g_respawn_ghosts_speed") * 3;\r
1724                 self.effects |= EF_ADDITIVE;\r
1725                 self.oldcolormap = self.colormap;\r
1726                 self.colormap = 512;\r
1727                 pointparticles(particleeffectnum("respawn_ghost"), self.origin, '0 0 0', 1);\r
1728                 if(cvar("g_respawn_ghosts_maxtime"))\r
1729                         SUB_SetFade (self, time + cvar("g_respawn_ghosts_maxtime") / 2 + random () * (cvar("g_respawn_ghosts_maxtime") - cvar("g_respawn_ghosts_maxtime") / 2), 1.5);\r
1730         }\r
1731 \r
1732         CopyBody(1);\r
1733         self.effects |= EF_NODRAW; // prevent another CopyBody\r
1734         if(self.oldcolormap)\r
1735         {\r
1736                 self.colormap = self.oldcolormap;\r
1737                 self.oldcolormap = 0;\r
1738         }\r
1739         PutClientInServer();\r
1740 }\r
1741 \r
1742 void play_countdown(float finished, string samp)\r
1743 {\r
1744         if(clienttype(self) == CLIENTTYPE_REAL)\r
1745                 if(floor(finished - time - frametime) != floor(finished - time))\r
1746                         if(finished - time < 6)\r
1747                                 sound (self, CHAN_AUTO, samp, VOL_BASE, ATTN_NORM);\r
1748 }\r
1749 \r
1750 /**\r
1751  * When sv_timeout is used this function returs strings like\r
1752  * "Timeout begins in 2 seconds!\n" or "Timeout ends in 23 seconds!\n".\r
1753  * Called by centerprint functions\r
1754  * @param addOneSecond boolean, set to 1 if the welcome-message centerprint asks for the text\r
1755  */\r
1756 string getTimeoutText(float addOneSecond) {\r
1757         if (!cvar("sv_timeout") || !timeoutStatus)\r
1758                 return "";\r
1759 \r
1760         local string retStr;\r
1761         if (timeoutStatus == 1) {\r
1762                 if (addOneSecond == 1) {\r
1763                         retStr = strcat("Timeout begins in ", ftos(remainingLeadTime + 1), " seconds!\n");\r
1764                 }\r
1765                 else {\r
1766                         retStr = strcat("Timeout begins in ", ftos(remainingLeadTime), " seconds!\n");\r
1767                 }\r
1768                 return retStr;\r
1769         }\r
1770         else if (timeoutStatus == 2) {\r
1771                 if (addOneSecond) {\r
1772                         retStr = strcat("Timeout ends in ", ftos(remainingTimeoutTime + 1), " seconds!\n");\r
1773                         //don't show messages like "Timeout ends in 0 seconds"...\r
1774                         if ((remainingTimeoutTime + 1) > 0)\r
1775                                 return retStr;\r
1776                         else\r
1777                                 return "";\r
1778                 }\r
1779                 else {\r
1780                         retStr = strcat("Timeout ends in ", ftos(remainingTimeoutTime), " seconds!\n");\r
1781                         //don't show messages like "Timeout ends in 0 seconds"...\r
1782                         if (remainingTimeoutTime > 0)\r
1783                                 return retStr;\r
1784                         else\r
1785                                 return "";\r
1786                 }\r
1787         }\r
1788         else return "";\r
1789 }\r
1790 \r
1791 void player_powerups (void)\r
1792 {\r
1793         if((self.items & IT_USING_JETPACK) && !self.deadflag)\r
1794         {\r
1795                 SoundEntity_StartSound(self, CHAN_PLAYER, "misc/jetpack_fly.wav", VOL_BASE, cvar("g_jetpack_attenuation"));\r
1796                 self.modelflags |= MF_ROCKET;\r
1797         }\r
1798         else\r
1799         {\r
1800                 SoundEntity_StopSound(self, CHAN_PLAYER);\r
1801                 self.modelflags &~= MF_ROCKET;\r
1802         }\r
1803 \r
1804         self.effects &~= (EF_RED | EF_BLUE | EF_ADDITIVE | EF_FULLBRIGHT | EF_FLAME | EF_NODEPTHTEST);\r
1805 \r
1806         if(!self.modelindex || self.deadflag) // don't apply the flags if the player is gibbed\r
1807                 return;\r
1808         \r
1809         Fire_ApplyDamage(self);\r
1810         Fire_ApplyEffect(self);\r
1811 \r
1812         if (self.items & IT_STRENGTH)\r
1813         {\r
1814                 play_countdown(self.strength_finished, "misc/poweroff.wav");\r
1815                 self.effects = self.effects | (EF_BLUE | EF_ADDITIVE | EF_FULLBRIGHT);\r
1816                 if (time > self.strength_finished && cvar("g_balance_powerup_timer"))\r
1817                 {\r
1818                         self.items = self.items - (self.items & IT_STRENGTH);\r
1819                         sprint(self, "^3Strength has worn off\n");\r
1820                 }\r
1821         }\r
1822         else\r
1823         {\r
1824                 if (time < self.strength_finished)\r
1825                 {\r
1826                         self.items = self.items | IT_STRENGTH;\r
1827                         sprint(self, "^3Strength infuses your weapons with devastating power\n");\r
1828                 }\r
1829         }\r
1830         if (self.items & IT_INVINCIBLE)\r
1831         {\r
1832                 play_countdown(self.invincible_finished, "misc/poweroff.wav");\r
1833                 self.effects = self.effects | (EF_RED | EF_ADDITIVE | EF_FULLBRIGHT);\r
1834                 if (time > self.invincible_finished && cvar("g_balance_powerup_timer"))\r
1835                 {\r
1836                         self.items = self.items - (self.items & IT_INVINCIBLE);\r
1837                         sprint(self, "^3Shield has worn off\n");\r
1838                 }\r
1839         }\r
1840         else\r
1841         {\r
1842                 if (time < self.invincible_finished)\r
1843                 {\r
1844                         self.items = self.items | IT_INVINCIBLE;\r
1845                         sprint(self, "^3Shield surrounds you\n");\r
1846                 }\r
1847         }\r
1848 \r
1849         if(cvar("g_nodepthtestplayers"))\r
1850                 self.effects = self.effects | EF_NODEPTHTEST;\r
1851 \r
1852         if(cvar("g_fullbrightplayers"))\r
1853                 self.effects = self.effects | EF_FULLBRIGHT;\r
1854 \r
1855         // midair gamemode: damage only while in the air\r
1856         // if in midair mode, being on ground grants temporary invulnerability\r
1857         // (this is so that multishot weapon don't clear the ground flag on the\r
1858         // first damage in the frame, leaving the player vulnerable to the\r
1859         // remaining hits in the same frame)\r
1860         if (self.flags & FL_ONGROUND)\r
1861         if (g_midair)\r
1862                 self.spawnshieldtime = max(self.spawnshieldtime, time + cvar("g_midair_shieldtime"));\r
1863 \r
1864         if (time >= game_starttime)\r
1865         if (time < self.spawnshieldtime)\r
1866                 self.effects = self.effects | (EF_ADDITIVE | EF_FULLBRIGHT);\r
1867 }\r
1868 \r
1869 float CalcRegen(float current, float stable, float regenfactor, float regenframetime)\r
1870 {\r
1871         if(current > stable)\r
1872                 return current;\r
1873         else if(current > stable - 0.25) // when close enough, "snap"\r
1874                 return stable;\r
1875         else\r
1876                 return min(stable, current + (stable - current) * regenfactor * regenframetime);\r
1877 }\r
1878 \r
1879 float CalcRot(float current, float stable, float rotfactor, float rotframetime)\r
1880 {\r
1881         if(current < stable)\r
1882                 return current;\r
1883         else if(current < stable + 0.25) // when close enough, "snap"\r
1884                 return stable;\r
1885         else\r
1886                 return max(stable, current + (stable - current) * rotfactor * rotframetime);\r
1887 }\r
1888 \r
1889 .float regen_soundtime;\r
1890 float CalcRotRegen(float current, float regenstable, float regenfactor, float regenlinear, float regenframetime, float rotstable, float rotfactor, float rotlinear, float rotframetime, float limit, string regensound)\r
1891 {\r
1892         if(current > rotstable)\r
1893         {\r
1894                 if(rotframetime > 0)\r
1895                 {\r
1896                         current = CalcRot(current, rotstable, rotfactor, rotframetime);\r
1897                         current = max(rotstable, current - rotlinear * rotframetime);\r
1898                 }\r
1899         }\r
1900         else if(current < regenstable)\r
1901         {\r
1902                 if(regenframetime > 0)\r
1903                 {\r
1904                         current = CalcRegen(current, regenstable, regenfactor, regenframetime);\r
1905                         current = min(regenstable, current + regenlinear * regenframetime);\r
1906 \r
1907                         if(regensound != "")\r
1908                         if(regenfactor || regenlinear)\r
1909                         {\r
1910                                 if(self.regen_soundtime < time)\r
1911                                 {\r
1912                                         msg_entity = self;\r
1913                                         if(clienttype(msg_entity) == CLIENTTYPE_REAL)\r
1914                                                 soundto(MSG_ONE, self, CHAN_AUTO, regensound, VOL_BASE, ATTN_NONE);\r
1915                                 }\r
1916                                 self.regen_soundtime = time + 1; // only replay the sound if regen was paused for one second\r
1917                         }\r
1918                 }\r
1919         }\r
1920 \r
1921         if(current > limit)\r
1922                 current = limit;\r
1923 \r
1924         return current;\r
1925 }\r
1926 \r
1927 void player_regen (void)\r
1928 {\r
1929         float minh, mina, minf, maxh, maxa, maxf, limith, limita, limitf, max_mod, regen_mod, rot_mod, limit_mod;\r
1930         maxh = cvar("g_balance_health_rotstable");\r
1931         maxa = cvar("g_balance_armor_rotstable");\r
1932         maxf = cvar("g_balance_fuel_rotstable");\r
1933         minh = cvar("g_balance_health_regenstable");\r
1934         mina = cvar("g_balance_armor_regenstable");\r
1935         minf = cvar("g_balance_fuel_regenstable");\r
1936         limith = cvar("g_balance_health_limit");\r
1937         limita = cvar("g_balance_armor_limit");\r
1938         limitf = cvar("g_balance_fuel_limit");\r
1939 \r
1940         max_mod = regen_mod = rot_mod = limit_mod = 1;\r
1941 \r
1942         maxh = maxh * max_mod;\r
1943         //maxa = maxa * max_mod;\r
1944         //maxf = maxf * max_mod;\r
1945         minh = minh * max_mod;\r
1946         //mina = mina * max_mod;\r
1947         //minf = minf * max_mod;\r
1948         limith = limith * limit_mod;\r
1949         limita = limita * limit_mod;\r
1950         //limitf = limitf * limit_mod;\r
1951 \r
1952         if(g_lms && g_ca)\r
1953                 rot_mod = 0;\r
1954 \r
1955         if (!g_ca && (!g_lms || cvar("g_lms_regenerate")))\r
1956         {\r
1957                 self.armorvalue = CalcRotRegen(self.armorvalue, mina, cvar("g_balance_armor_regen"), cvar("g_balance_armor_regenlinear"), regen_mod * frametime * (time > self.pauseregenarmor_finished), maxa, cvar("g_balance_armor_rot"), cvar("g_balance_armor_rotlinear"), rot_mod * frametime * (time > self.pauserotarmor_finished), limita, "misc/armor_regen.wav");\r
1958                 self.health = CalcRotRegen(self.health, minh, cvar("g_balance_health_regen"), cvar("g_balance_health_regenlinear"), regen_mod * frametime * (time > self.pauseregenhealth_finished), maxh, cvar("g_balance_health_rot"), cvar("g_balance_health_rotlinear"), rot_mod * frametime * (time > self.pauserothealth_finished), limith, "misc/health_regen.wav");\r
1959 \r
1960                 // if player rotted to death...  die!\r
1961                 if(self.health < 1)\r
1962                         self.event_damage(self, self, 1, DEATH_ROT, self.origin, '0 0 0');\r
1963         }\r
1964 \r
1965         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)\r
1966                 self.ammo_fuel = CalcRotRegen(self.ammo_fuel, minf, cvar("g_balance_fuel_regen"), cvar("g_balance_fuel_regenlinear"), regen_mod * frametime * (time > self.pauseregenhealth_finished) * (self.items & IT_FUEL_REGEN != 0), maxf, cvar("g_balance_fuel_rot"), cvar("g_balance_fuel_rotlinear"), rot_mod * frametime * (time > self.pauserotfuel_finished), limitf, "");\r
1967 }\r
1968 \r
1969 float zoomstate_set;\r
1970 void SetZoomState(float z)\r
1971 {\r
1972         if(z != self.zoomstate)\r
1973         {\r
1974                 self.zoomstate = z;\r
1975                 ClientData_Touch(self);\r
1976         }\r
1977         zoomstate_set = 1;\r
1978 }\r
1979 \r
1980 void GetPressedKeys(void) {\r
1981         if (self.movement_x > 0) // get if movement keys are pressed\r
1982         {       // forward key pressed\r
1983                 self.pressedkeys |= KEY_FORWARD;\r
1984                 self.pressedkeys &~= KEY_BACKWARD;\r
1985         }\r
1986         else if (self.movement_x < 0)\r
1987         {       // backward key pressed\r
1988                 self.pressedkeys |= KEY_BACKWARD;\r
1989                 self.pressedkeys &~= KEY_FORWARD;\r
1990         }\r
1991         else\r
1992         {       // no x input\r
1993                 self.pressedkeys &~= KEY_FORWARD;\r
1994                 self.pressedkeys &~= KEY_BACKWARD;\r
1995         }\r
1996 \r
1997         if (self.movement_y > 0)\r
1998         {       // right key pressed\r
1999                 self.pressedkeys |= KEY_RIGHT;\r
2000                 self.pressedkeys &~= KEY_LEFT;\r
2001         }\r
2002         else if (self.movement_y < 0)\r
2003         {       // left key pressed\r
2004                 self.pressedkeys |= KEY_LEFT;\r
2005                 self.pressedkeys &~= KEY_RIGHT;\r
2006         }\r
2007         else\r
2008         {       // no y input\r
2009                 self.pressedkeys &~= KEY_RIGHT;\r
2010                 self.pressedkeys &~= KEY_LEFT;\r
2011         }\r
2012 \r
2013         if (self.BUTTON_JUMP) // get if jump and crouch keys are pressed\r
2014                 self.pressedkeys |= KEY_JUMP;\r
2015         else\r
2016                 self.pressedkeys &~= KEY_JUMP;\r
2017         if (self.BUTTON_CROUCH)\r
2018                 self.pressedkeys |= KEY_CROUCH;\r
2019         else\r
2020                 self.pressedkeys &~= KEY_CROUCH;\r
2021 }\r
2022 \r
2023 void update_stats (float number, float hit, float fired) {\r
2024 // self.stat_hit   = number + ((number==0) ? 1 : 64) * hit   * sv_accuracy_data_share;\r
2025 // self.stat_fired = number + ((number==0) ? 1 : 64) * fired * sv_accuracy_data_share;\r
2026 \r
2027         if(number) {\r
2028                 self.stat_hit = number + 64 * hit * sv_accuracy_data_share;\r
2029                 self.stat_fired = number + 64 * fired * sv_accuracy_data_share;\r
2030         } else {\r
2031                 self.stat_hit = hit * sv_accuracy_data_share;\r
2032                 self.stat_fired = fired * sv_accuracy_data_share;\r
2033         }\r
2034 }\r
2035 \r
2036 /*\r
2037 ======================\r
2038 spectate mode routines\r
2039 ======================\r
2040 */\r
2041 \r
2042 .float weapon_count;\r
2043 void SpectateCopy(entity spectatee) {\r
2044         if(spectatee.weapon_count < WEP_LAST) {\r
2045                 update_stats (spectatee.weapon_count, spectatee.cvar_cl_accuracy_data_share * floor(spectatee.stats_hit[spectatee.weapon_count - 1]), spectatee.cvar_cl_accuracy_data_share * floor(spectatee.stats_fired[spectatee.weapon_count - 1]));\r
2046                 spectatee.weapon_count ++;\r
2047         } else\r
2048                 update_stats (0, spectatee.cvar_cl_accuracy_data_share * spectatee.stat_hit, spectatee.cvar_cl_accuracy_data_share * spectatee.stat_fired);\r
2049 \r
2050         self.kh_state = spectatee.kh_state;\r
2051         self.armortype = spectatee.armortype;\r
2052         self.armorvalue = spectatee.armorvalue;\r
2053         self.ammo_fuel = spectatee.ammo_fuel;\r
2054         self.clip_load = spectatee.clip_load;\r
2055         self.clip_size = spectatee.clip_size;\r
2056         self.effects = spectatee.effects & EFMASK_CHEAP; // eat performance\r
2057         self.health = spectatee.health;\r
2058         self.impulse = 0;\r
2059         self.items = spectatee.items;\r
2060         self.last_pickup = spectatee.last_pickup;\r
2061         self.strength_finished = spectatee.strength_finished;\r
2062         self.invincible_finished = spectatee.invincible_finished;\r
2063         self.pressedkeys = spectatee.pressedkeys;\r
2064         self.weapons = spectatee.weapons;\r
2065         self.switchweapon = spectatee.switchweapon;\r
2066         self.weapon = spectatee.weapon;\r
2067         self.punchangle = spectatee.punchangle;\r
2068         self.view_ofs = spectatee.view_ofs;\r
2069         self.v_angle = spectatee.v_angle;\r
2070         self.velocity = spectatee.velocity;\r
2071         self.dmg_take = spectatee.dmg_take;\r
2072         self.dmg_save = spectatee.dmg_save;\r
2073         self.dmg_inflictor = spectatee.dmg_inflictor;\r
2074         self.angles = spectatee.v_angle;\r
2075         self.scale = spectatee.scale;\r
2076         self.fixangle = TRUE;\r
2077         self.stomach_load = spectatee.stomach_load;\r
2078         self.stat_eaten = spectatee.stat_eaten;\r
2079         self.stat_stomachload = spectatee.stat_stomachload;\r
2080         self.stat_digesting = spectatee.stat_digesting;\r
2081         self.stat_canleave = spectatee.stat_canleave;\r
2082         self.stat_canswallow = spectatee.stat_canswallow;\r
2083         self.stat_sbring1_type = spectatee.stat_sbring1_type;\r
2084         self.stat_sbring1_clip = spectatee.stat_sbring1_clip;\r
2085         self.stat_sbring2_type = spectatee.stat_sbring2_type;\r
2086         self.stat_sbring2_clip = spectatee.stat_sbring2_clip;\r
2087         setorigin(self, spectatee.origin);\r
2088         setsize(self, spectatee.mins, spectatee.maxs);\r
2089         SetZoomState(spectatee.zoomstate);\r
2090 \r
2091         anticheat_spectatecopy(spectatee);\r
2092 }\r
2093 \r
2094 float SpectateUpdate() {\r
2095         if(!self.enemy)\r
2096                 return 0;\r
2097 \r
2098         if (self == self.enemy)\r
2099                 return 0;\r
2100 \r
2101         if(self.enemy.classname != "player")\r
2102                 return 0;\r
2103 \r
2104         SpectateCopy(self.enemy);\r
2105 \r
2106         return 1;\r
2107 }\r
2108 \r
2109 float SpectateNext() {\r
2110         other = find(self.enemy, classname, "player");\r
2111 \r
2112         if (!other)\r
2113                 other = find(other, classname, "player");\r
2114 \r
2115         if (other)\r
2116                 self.enemy = other;\r
2117 \r
2118         if(self.enemy.classname == "player") {\r
2119                 msg_entity = self;\r
2120                 WriteByte(MSG_ONE, SVC_SETVIEW);\r
2121                 WriteEntity(MSG_ONE, self.enemy);\r
2122                 //stuffcmd(self, "set viewsize $tmpviewsize \n");\r
2123                 self.movetype = MOVETYPE_NONE;\r
2124 \r
2125                 self.enemy.weapon_count = 0;\r
2126 \r
2127                 if(!SpectateUpdate())\r
2128                         PutObserverInServer();\r
2129 \r
2130                 return 1;\r
2131         } else {\r
2132                 return 0;\r
2133         }\r
2134 }\r
2135 \r
2136 /*\r
2137 =============\r
2138 ShowRespawnCountdown()\r
2139 \r
2140 Update a respawn countdown display.\r
2141 =============\r
2142 */\r
2143 void ShowRespawnCountdown()\r
2144 {\r
2145         float number;\r
2146         if(self.deadflag == DEAD_NO) // just respawned?\r
2147                 return;\r
2148         else\r
2149         {\r
2150                 number = ceil(self.death_time - time);\r
2151                 if(number <= 0)\r
2152                         return;\r
2153                 if(number <= self.respawn_countdown)\r
2154                 {\r
2155                         self.respawn_countdown = number - 1;\r
2156                         if(ceil(self.death_time - (time + 0.5)) == number) // only say it if it is the same number even in 0.5s; to prevent overlapping sounds\r
2157                                 AnnounceTo(self, strcat(ftos(number), ""));\r
2158                 }\r
2159         }\r
2160 }\r
2161 \r
2162 void LeaveSpectatorMode()\r
2163 {\r
2164         if(isJoinAllowed()) {\r
2165                 if(!teams_matter || cvar("g_campaign") || cvar("g_balance_teams") || (self.wasplayer && cvar("g_changeteam_banned"))) {\r
2166                         self.classname = "player";\r
2167 \r
2168                         if(cvar("g_campaign") || cvar("g_balance_teams") || cvar("g_balance_teams_force"))\r
2169                                 JoinBestTeam(self, FALSE, TRUE);\r
2170 \r
2171                         if(cvar("g_campaign"))\r
2172                                 campaign_bots_may_start = 1;\r
2173 \r
2174                         self.stat_count = WEP_LAST;\r
2175 \r
2176                         PutClientInServer();\r
2177 \r
2178                         if(self.classname == "player")\r
2179                                 bprint ("^4", self.netname, "^4 is playing now\n");\r
2180 \r
2181                         if(!cvar("g_campaign"))\r
2182                                 centerprint(self,""); // clear MOTD\r
2183 \r
2184                         return;\r
2185                 } else {\r
2186                         if (g_ca && self.caplayer) {\r
2187                         }       // do nothing\r
2188                         else\r
2189                                 stuffcmd(self,"menu_showteamselect\n");\r
2190                         return;\r
2191                 }\r
2192         }\r
2193         else {\r
2194                 //player may not join because of g_maxplayers is set\r
2195                 centerprint_atprio(self, CENTERPRIO_MAPVOTE, PREVENT_JOIN_TEXT);\r
2196         }\r
2197 }\r
2198 \r
2199 /**\r
2200  * Determines whether the player is allowed to join. This depends on cvar\r
2201  * g_maxplayers, if it isn't used this function always return TRUE, otherwise\r
2202  * it checks whether the number of currently playing players exceeds g_maxplayers.\r
2203  * @return bool TRUE if the player is allowed to join, false otherwise\r
2204  */\r
2205 float isJoinAllowed() {\r
2206         if (!cvar("g_maxplayers"))\r
2207                 return TRUE;\r
2208 \r
2209         local entity e;\r
2210         local float currentlyPlaying;\r
2211         FOR_EACH_REALPLAYER(e) {\r
2212                 if(e.classname == "player")\r
2213                         currentlyPlaying += 1;\r
2214         }\r
2215         if(currentlyPlaying < cvar("g_maxplayers"))\r
2216                 return TRUE;\r
2217 \r
2218         return FALSE;\r
2219 }\r
2220 \r
2221 /**\r
2222  * Checks whether the client is an observer or spectator, if so, he will get kicked after\r
2223  * g_maxplayers_spectator_blocktime seconds\r
2224  */\r
2225 void checkSpectatorBlock() {\r
2226         if(self.classname == "spectator" || self.classname == "observer") {\r
2227                 if( time > (self.spectatortime + cvar("g_maxplayers_spectator_blocktime")) ) {\r
2228                         sprint(self, "^7You were kicked from the server because you are spectator and spectators aren't allowed at the moment.\n");\r
2229                         dropclient(self);\r
2230                 }\r
2231         }\r
2232 }\r
2233 \r
2234 float vercmp_recursive(string v1, string v2)\r
2235 {\r
2236         float dot1, dot2;\r
2237         string s1, s2;\r
2238         float r;\r
2239 \r
2240         dot1 = strstrofs(v1, ".", 0);\r
2241         dot2 = strstrofs(v2, ".", 0);\r
2242         if(dot1 == -1)\r
2243                 s1 = v1;\r
2244         else\r
2245                 s1 = substring(v1, 0, dot1);\r
2246         if(dot2 == -1)\r
2247                 s2 = v2;\r
2248         else\r
2249                 s2 = substring(v2, 0, dot2);\r
2250 \r
2251         r = stof(s1) - stof(s2);\r
2252         if(r != 0)\r
2253                 return r;\r
2254 \r
2255         r = strcasecmp(s1, s2);\r
2256         if(r != 0)\r
2257                 return r;\r
2258 \r
2259         if(dot1 == -1)\r
2260                 if(dot2 == -1)\r
2261                         return 0;\r
2262                 else\r
2263                         return -1;\r
2264         else\r
2265                 if(dot2 == -1)\r
2266                         return 1;\r
2267                 else\r
2268                         return vercmp_recursive(substring(v1, dot1 + 1, 999), substring(v2, dot2 + 1, 999));\r
2269 }\r
2270 \r
2271 float vercmp(string v1, string v2)\r
2272 {\r
2273         if(strcasecmp(v1, v2) == 0) // early out check\r
2274                 return 0;\r
2275         return vercmp_recursive(v1, v2);\r
2276 }\r
2277 \r
2278 void ApplyHealthSize()\r
2279 {\r
2280         // change player scale based on the amount of health we have\r
2281 \r
2282         if not(cvar("g_healthsize"))\r
2283                 return;\r
2284 \r
2285         self.scale = bound(cvar("g_healthsize_min"), self.health, cvar("g_healthsize_max")) / cvar("g_healthsize");\r
2286 \r
2287         // The following code sets the bounding box to match the player's size.\r
2288         // It is currently disabled because of issues with engine movement prediction (cl_movement).\r
2289         // The engine expects the bounding box to be default size, and changing it will cause glitches.\r
2290         // This code may be enabled once the engine has the ability to use different bbox sizes for movement prediction.\r
2291         if(self.crouch)\r
2292         {\r
2293                 //setsize (self, PL_CROUCH_MIN * self.scale, PL_CROUCH_MAX * self.scale);\r
2294                 if(!self.stat_eaten)\r
2295                         self.view_ofs = PL_CROUCH_VIEW_OFS * self.scale;\r
2296         }\r
2297         else\r
2298         {\r
2299                 //setsize (self, PL_MIN * self.scale, PL_MAX * self.scale);\r
2300                 if(!self.stat_eaten)\r
2301                         self.view_ofs = PL_VIEW_OFS * self.scale;\r
2302         }\r
2303 }\r
2304 \r
2305 void ObserverThink()\r
2306 {\r
2307         if (self.flags & FL_JUMPRELEASED) {\r
2308                 if (self.BUTTON_JUMP && !self.version_mismatch) {\r
2309                         self.welcomemessage_time = 0;\r
2310                         self.flags &~= FL_JUMPRELEASED;\r
2311                         self.flags |= FL_SPAWNING;\r
2312                 } else if(self.BUTTON_ATCK && !self.version_mismatch) {\r
2313                         self.welcomemessage_time = 0;\r
2314                         self.flags &~= FL_JUMPRELEASED;\r
2315                         if(SpectateNext() == 1) {\r
2316                                 self.classname = "spectator";\r
2317                         }\r
2318                 }\r
2319         } else {\r
2320                 if (!(self.BUTTON_ATCK || self.BUTTON_JUMP)) {\r
2321                         self.flags |= FL_JUMPRELEASED;\r
2322                         if(self.flags & FL_SPAWNING)\r
2323                         {\r
2324                                 self.flags &~= FL_SPAWNING;\r
2325                                 LeaveSpectatorMode();\r
2326                                 return;\r
2327                         }\r
2328                 }\r
2329         }\r
2330         PrintWelcomeMessage(self);\r
2331 }\r
2332 \r
2333 void SpectatorThink()\r
2334 {\r
2335         if (self.flags & FL_JUMPRELEASED) {\r
2336                 if (self.BUTTON_JUMP && !self.version_mismatch) {\r
2337                         self.welcomemessage_time = 0;\r
2338                         self.flags &~= FL_JUMPRELEASED;\r
2339                         self.flags |= FL_SPAWNING;\r
2340                 } else if(self.BUTTON_ATCK) {\r
2341                         self.welcomemessage_time = 0;\r
2342                         self.flags &~= FL_JUMPRELEASED;\r
2343                         if(SpectateNext() == 1) {\r
2344                                 self.classname = "spectator";\r
2345                         } else {\r
2346                                 self.classname = "observer";\r
2347                                 self.stat_count = WEP_LAST;\r
2348                                 PutClientInServer();\r
2349                         }\r
2350                 } else if (self.BUTTON_ATCK2) {\r
2351                         self.welcomemessage_time = 0;\r
2352                         self.flags &~= FL_JUMPRELEASED;\r
2353                         self.classname = "observer";\r
2354                         self.stat_count = WEP_LAST;\r
2355                         PutClientInServer();\r
2356                 } else {\r
2357                         if(!SpectateUpdate())\r
2358                                 PutObserverInServer();\r
2359                 }\r
2360         } else {\r
2361                 if (!(self.BUTTON_ATCK || self.BUTTON_ATCK2)) {\r
2362                         self.flags |= FL_JUMPRELEASED;\r
2363                         if(self.flags & FL_SPAWNING)\r
2364                         {\r
2365                                 self.flags &~= FL_SPAWNING;\r
2366                                 LeaveSpectatorMode();\r
2367                                 return;\r
2368                         }\r
2369                 }\r
2370         }\r
2371 \r
2372         PrintWelcomeMessage(self);\r
2373         self.flags |= FL_CLIENT | FL_NOTARGET;\r
2374 }\r
2375 \r
2376 .float touchexplode_time;\r
2377 \r
2378 /*\r
2379 =============\r
2380 PlayerPreThink\r
2381 \r
2382 Called every frame for each client before the physics are run\r
2383 =============\r
2384 */\r
2385 void() ctf_setstatus;\r
2386 .float items_added;\r
2387 void PlayerPreThink (void)\r
2388 {\r
2389         self.stat_game_starttime = game_starttime;\r
2390         self.stat_allow_oldnexbeam = cvar("g_allow_oldnexbeam");\r
2391         self.stat_leadlimit = cvar("leadlimit");\r
2392 \r
2393         if(frametime)\r
2394         {\r
2395                 // physics frames: update anticheat stuff\r
2396                 anticheat_prethink();\r
2397         }\r
2398 \r
2399         if(blockSpectators && frametime)\r
2400                 // WORKAROUND: only use dropclient in server frames (frametime set). Never use it in cl_movement frames (frametime zero).\r
2401                 checkSpectatorBlock();\r
2402 \r
2403         zoomstate_set = 0;\r
2404 \r
2405         if(self.netname_previous != self.netname)\r
2406         {\r
2407                 if(cvar("sv_eventlog"))\r
2408                         GameLogEcho(strcat(":name:", ftos(self.playerid), ":", self.netname));\r
2409                 if(self.netname_previous)\r
2410                         strunzone(self.netname_previous);\r
2411                 self.netname_previous = strzone(self.netname);\r
2412         }\r
2413 \r
2414         // core code for the vore system\r
2415         Vore();\r
2416 \r
2417         // version nagging\r
2418         if(self.version_nagtime)\r
2419                 if(self.cvar_g_voretournamentversion)\r
2420                         if(time > self.version_nagtime)\r
2421                         {\r
2422                                 if(strstr(self.cvar_g_voretournamentversion, "svn", 0) < 0)\r
2423                                 {\r
2424                                         if(strstr(cvar_string("g_voretournamentversion"), "svn", 0) >= 0)\r
2425                                         {\r
2426                                                 dprint("^1NOTE^7 to ", self.netname, "^7 - the server is running ^3Voretournament ", cvar_string("g_voretournamentversion"), " (beta)^7, you have ^3Voretournament ", self.cvar_g_voretournamentversion, "^1\n");\r
2427                                                 sprint(self, strcat("\{1}^1NOTE: ^7the server is running ^3Voretournament ", cvar_string("g_voretournamentversion"), " (beta)^7, you have ^3Voretournament ", self.cvar_g_voretournamentversion, "^1\n"));\r
2428                                         }\r
2429                                         else\r
2430                                         {\r
2431                                                 float r;\r
2432                                                 r = vercmp(self.cvar_g_voretournamentversion, cvar_string("g_voretournamentversion"));\r
2433                                                 if(r < 0)\r
2434                                                 {\r
2435                                                         dprint("^1NOTE^7 to ", self.netname, "^7 - ^3Voretournament ", cvar_string("g_voretournamentversion"), "^7 is out, and you still have ^3Voretournament ", self.cvar_g_voretournamentversion, "^1 - get the update from ^4http://www.alientrap.org/voretournament/^1!\n");\r
2436                                                         sprint(self, strcat("\{1}^1NOTE: ^3Voretournament ", cvar_string("g_voretournamentversion"), "^7 is out, and you still have ^3Voretournament ", self.cvar_g_voretournamentversion, "^1 - get the update from ^4http://www.alientrap.org/voretournament/^1!\n"));\r
2437                                                 }\r
2438                                                 else if(r > 0)\r
2439                                                 {\r
2440                                                         dprint("^1NOTE^7 to ", self.netname, "^7 - the server is running ^3Voretournament ", cvar_string("g_voretournamentversion"), "^7, you have ^3Voretournament ", self.cvar_g_voretournamentversion, "^1\n");\r
2441                                                         sprint(self, strcat("\{1}^1NOTE: ^7the server is running ^3Voretournament ", cvar_string("g_voretournamentversion"), "^7, you have ^3Voretournament ", self.cvar_g_voretournamentversion, "^1\n"));\r
2442                                                 }\r
2443                                         }\r
2444                                 }\r
2445                                 self.version_nagtime = 0;\r
2446                         }\r
2447 \r
2448         // GOD MODE info\r
2449         if(!(self.flags & FL_GODMODE)) if(self.max_armorvalue)\r
2450         {\r
2451                 sprint(self, strcat("godmode saved you ", ftos(self.max_armorvalue), " units of damage, cheater!\n"));\r
2452                 self.max_armorvalue = 0;\r
2453         }\r
2454 \r
2455 #ifdef TETRIS\r
2456         if (TetrisPreFrame())\r
2457                 return;\r
2458 #endif\r
2459 \r
2460         if(self.classname == "player") {\r
2461 //              if(self.netname == "Wazat")\r
2462 //                      bprint(self.classname, "\n");\r
2463 \r
2464                 CheckRules_Player();\r
2465 \r
2466                 PrintWelcomeMessage(self);\r
2467 \r
2468                 if (intermission_running)\r
2469                 {\r
2470                         IntermissionThink ();   // otherwise a button could be missed between\r
2471                         return;                                 // the think tics\r
2472                 }\r
2473 \r
2474                 if(self.teleport_time)\r
2475                 if(time > self.teleport_time)\r
2476                 {\r
2477                         self.teleport_time = 0;\r
2478                         self.effects = self.effects - (self.effects & EF_NODRAW);\r
2479                 }\r
2480 \r
2481                 if(frametime > 0) // don't do this in cl_movement frames, just in server ticks\r
2482                         UpdateSelectedPlayer();\r
2483 \r
2484                 //don't allow the player to turn around while game is paused!\r
2485                 if(timeoutStatus == 2) {\r
2486                         self.v_angle = self.lastV_angle;\r
2487                         self.angles = self.lastV_angle;\r
2488                         self.fixangle = TRUE;\r
2489                 }\r
2490 \r
2491                 if(frametime)\r
2492                 {\r
2493                         player_powerups();\r
2494                 }\r
2495 \r
2496                 if (self.deadflag != DEAD_NO)\r
2497                 {\r
2498                         float button_pressed, force_respawn;\r
2499                         if(self.personal && g_race_qualifying)\r
2500                         {\r
2501                                 if(time > self.death_time)\r
2502                                 {\r
2503                                         self.death_time = time + 1; // only retry once a second\r
2504                                         respawn();\r
2505                                         self.impulse = 141;\r
2506                                 }\r
2507                         }\r
2508                         else\r
2509                         {\r
2510                                 if(frametime)\r
2511                                         player_anim();\r
2512                                 button_pressed = (self.BUTTON_ATCK || self.BUTTON_JUMP || self.BUTTON_ATCK2 || self.BUTTON_JETPACK || self.BUTTON_USE);\r
2513                                 force_respawn = (g_lms || (g_ca) || cvar("g_forced_respawn"));\r
2514                                 if (self.deadflag == DEAD_DYING)\r
2515                                 {\r
2516                                         if(force_respawn)\r
2517                                                 self.deadflag = DEAD_RESPAWNING;\r
2518                                         else if(!button_pressed)\r
2519                                                 self.deadflag = DEAD_DEAD;\r
2520                                 }\r
2521                                 else if (self.deadflag == DEAD_DEAD)\r
2522                                 {\r
2523                                         if(button_pressed)\r
2524                                                 self.deadflag = DEAD_RESPAWNABLE;\r
2525                                 }\r
2526                                 else if (self.deadflag == DEAD_RESPAWNABLE)\r
2527                                 {\r
2528                                         if(!button_pressed)\r
2529                                                 self.deadflag = DEAD_RESPAWNING;\r
2530                                 }\r
2531                                 else if (self.deadflag == DEAD_RESPAWNING)\r
2532                                 {\r
2533                                         if(time > self.death_time)\r
2534                                         {\r
2535                                                 self.death_time = time + 1; // only retry once a second\r
2536                                                 respawn();\r
2537                                         }\r
2538                                 }\r
2539                                 ShowRespawnCountdown();\r
2540                         }\r
2541                         return;\r
2542                 }\r
2543 \r
2544                 if(g_touchexplode)\r
2545                 if(time > self.touchexplode_time)\r
2546                 if(self.classname == "player")\r
2547                 if(self.deadflag == DEAD_NO)\r
2548                 if not(IS_INDEPENDENT_PLAYER(self))\r
2549                 FOR_EACH_PLAYER(other) if(self != other)\r
2550                 {\r
2551                         if(time > other.touchexplode_time)\r
2552                         if(other.classname == "player")\r
2553                         if(other.deadflag == DEAD_NO)\r
2554                         if not(IS_INDEPENDENT_PLAYER(other))\r
2555                         if(boxesoverlap(self.absmin, self.absmax, other.absmin, other.absmax))\r
2556                         {\r
2557                                 PlayerTouchExplode(self, other);\r
2558                                 self.touchexplode_time = other.touchexplode_time = time + 0.2;\r
2559                         }\r
2560                 }\r
2561 \r
2562                 if(g_lms && !self.deadflag && cvar("g_lms_campcheck_interval"))\r
2563                 {\r
2564                         vector dist;\r
2565 \r
2566                         // calculate player movement (in 2 dimensions only, so jumping on one spot doesn't count as movement)\r
2567                         dist = self.prevorigin - self.origin;\r
2568                         dist_z = 0;\r
2569                         self.lms_traveled_distance += fabs(vlen(dist));\r
2570 \r
2571                         if((cvar("g_campaign") && !campaign_bots_may_start) || (time < game_starttime))\r
2572                         {\r
2573                                 self.lms_nextcheck = time + cvar("g_lms_campcheck_interval")*2;\r
2574                                 self.lms_traveled_distance = 0;\r
2575                         }\r
2576 \r
2577                         if(time > self.lms_nextcheck)\r
2578                         {\r
2579                                 //sprint(self, "distance: ", ftos(self.lms_traveled_distance), "\n");\r
2580                                 if(self.lms_traveled_distance < cvar("g_lms_campcheck_distance"))\r
2581                                 {\r
2582                                         centerprint(self, cvar_string("g_lms_campcheck_message"));\r
2583                                         // FIXME KadaverJack: gibbing player here causes playermodel to bounce around, instead of eye.md3\r
2584                                         // I wasn't able to find out WHY that happens, so I put a workaround in place that shall prevent players from being gibbed :(\r
2585                                         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');\r
2586                                 }\r
2587                                 self.lms_nextcheck = time + cvar("g_lms_campcheck_interval");\r
2588                                 self.lms_traveled_distance = 0;\r
2589                         }\r
2590                 }\r
2591 \r
2592                 self.prevorigin = self.origin;\r
2593 \r
2594                 if ((self.BUTTON_CROUCH && !self.grabber.state) || self.health <= g_bloodloss)\r
2595                 {\r
2596                         if (!self.crouch)\r
2597                         {\r
2598                                 self.crouch = TRUE;\r
2599                                 self.view_ofs = PL_CROUCH_VIEW_OFS;\r
2600                                 setsize (self, PL_CROUCH_MIN, PL_CROUCH_MAX);\r
2601                                 setanim(self, self.anim_duck, FALSE, TRUE, TRUE);\r
2602                         }\r
2603                 }\r
2604                 else\r
2605                 {\r
2606                         if (self.crouch)\r
2607                         {\r
2608                                 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, FALSE, self);\r
2609                                 if (!trace_startsolid)\r
2610                                 {\r
2611                                         self.crouch = FALSE;\r
2612                                         self.view_ofs = PL_VIEW_OFS;\r
2613                                         setsize (self, PL_MIN, PL_MAX);\r
2614                                 }\r
2615                         }\r
2616                 }\r
2617 \r
2618                 if(self.health <= g_bloodloss && self.deadflag == DEAD_NO)\r
2619                 {\r
2620                         if(self.bloodloss_timer < time)\r
2621                         {\r
2622                                 self.event_damage(self, self, 1, DEATH_ROT, self.origin, '0 0 0');\r
2623                                 self.bloodloss_timer = time + 0.5 + random() * 0.5;\r
2624                         }\r
2625                 }\r
2626 \r
2627                 ApplyHealthSize();\r
2628 \r
2629                 FixPlayermodel();\r
2630 \r
2631                 GrabberFrame();\r
2632 \r
2633                 // LordHavoc: allow firing on move frames (sub-ticrate), this gives better timing on slow servers\r
2634                 //if(frametime)\r
2635                 {\r
2636                         self.items &~= self.items_added;\r
2637 \r
2638                         W_WeaponFrame();\r
2639 \r
2640                         self.items_added = 0;\r
2641                         if(self.items & IT_JETPACK)\r
2642                                 if(self.items & IT_FUEL_REGEN || self.ammo_fuel >= 0.01)\r
2643                                         self.items_added |= IT_FUEL;\r
2644 \r
2645                         self.items |= self.items_added;\r
2646                 }\r
2647 \r
2648                 player_regen();\r
2649                 if(frametime)\r
2650                         player_anim();\r
2651 \r
2652                 ctf_setstatus();\r
2653 \r
2654                 self.dmg_team = max(0, self.dmg_team - cvar("g_teamdamage_resetspeed") * frametime);\r
2655 \r
2656                 //self.angles_y=self.v_angle_y + 90;   // temp\r
2657         } else if(gameover) {\r
2658                 if (intermission_running)\r
2659                         IntermissionThink ();   // otherwise a button could be missed between\r
2660                 return;\r
2661         } else if(self.classname == "observer") {\r
2662                 ObserverThink();\r
2663         } else if(self.classname == "spectator") {\r
2664                 SpectatorThink();\r
2665         }\r
2666 \r
2667         if(!zoomstate_set)\r
2668                 SetZoomState(self.BUTTON_ZOOM);\r
2669 \r
2670         float oldspectatee_status;\r
2671         oldspectatee_status = self.spectatee_status;\r
2672         if(self.classname == "spectator")\r
2673                 self.spectatee_status = num_for_edict(self.enemy);\r
2674         else if(self.classname == "observer")\r
2675                 self.spectatee_status = num_for_edict(self);\r
2676         else\r
2677                 self.spectatee_status = 0;\r
2678         if(self.spectatee_status != oldspectatee_status)\r
2679         {\r
2680                 ClientData_Touch(self);\r
2681                 if(g_race || g_cts)\r
2682                         race_InitSpectator();\r
2683         }\r
2684 \r
2685         if(self.teamkill_soundtime)\r
2686         if(time > self.teamkill_soundtime)\r
2687         {\r
2688                 self.teamkill_soundtime = 0;\r
2689 \r
2690                 entity oldpusher, oldself;\r
2691 \r
2692                 oldself = self; self = self.teamkill_soundsource;\r
2693                 oldpusher = self.pusher; self.pusher = oldself;\r
2694 \r
2695                 PlayerSound(self, playersound_teamshoot, CHAN_VOICE, VOICETYPE_LASTATTACKER_ONLY);\r
2696 \r
2697                 self.pusher = oldpusher;\r
2698                 self = oldself;\r
2699         }\r
2700 \r
2701         if(self.taunt_soundtime)\r
2702         if(time > self.taunt_soundtime)\r
2703         {\r
2704                 switch(self.taunt_soundtype)\r
2705                 {\r
2706                         case TAUNTTYPE_DEATH:\r
2707                                 PlayerSound(self, playersound_taunt, CHAN_VOICE, VOICETYPE_AUTOTAUNT);\r
2708                                 break;\r
2709                         case TAUNTTYPE_VOREPRED:\r
2710                                 PlayerSound(self, playersound_pred, CHAN_VOICE, VOICETYPE_AUTOTAUNT);\r
2711                                 break;\r
2712                         case TAUNTTYPE_VOREPREY:\r
2713                                 PlayerSound(self, playersound_prey, CHAN_VOICE, VOICETYPE_AUTOTAUNT);\r
2714                                 break;\r
2715                         default:\r
2716                                 dprint("Incorrect autotaunt type\n");\r
2717                                 break;\r
2718                 }\r
2719 \r
2720                 self.taunt_soundtime = 0;\r
2721                 self.taunt_soundtype = 0;\r
2722         }\r
2723 \r
2724         target_voicescript_next(self);\r
2725 \r
2726         // if a player goes unarmed after holding a loaded weapon, empty his clip size and remove the crosshair ammo ring\r
2727         if(!self.weapon)\r
2728                 self.clip_load = self.clip_size = 0;\r
2729 }\r
2730 \r
2731 float isInvisibleString(string s)\r
2732 {\r
2733         float i, n, c;\r
2734         s = strdecolorize(s);\r
2735         for((i = 0), (n = strlen(s)); i < n; ++i)\r
2736         {\r
2737                 c = str2chr(s, i);\r
2738                 switch(c)\r
2739                 {\r
2740                         case 0:\r
2741                         case 32: // space\r
2742                                 break;\r
2743                         case 192: // charmap space\r
2744                                 if (!cvar("utf8_enable"))\r
2745                                         break;\r
2746                                 return FALSE;\r
2747                         case 160: // space in unicode fonts\r
2748                         case 0xE000 + 192: // utf8 charmap space\r
2749                                 if (cvar("utf8_enable"))\r
2750                                         break;\r
2751                         default:\r
2752                                 return FALSE;\r
2753                 }\r
2754         }\r
2755         return TRUE;\r
2756 }\r
2757 \r
2758 /*\r
2759 =============\r
2760 PlayerPostThink\r
2761 \r
2762 Called every frame for each client after the physics are run\r
2763 =============\r
2764 */\r
2765 .float idlekick_lasttimeleft;\r
2766 void PlayerPostThink (void)\r
2767 {\r
2768         // Savage: Check for nameless players\r
2769         if (isInvisibleString(self.netname)) {\r
2770                 self.netname = "Player";\r
2771                 stuffcmd(self, strcat("name ", self.netname, substring(ftos(random()), 2, -1), "\n"));\r
2772         }\r
2773 \r
2774         // send the clients accuracy stats to the client\r
2775         if(self.stat_count > 0)\r
2776         if(frametime)\r
2777         {\r
2778                 self.stat_hit = self.stat_count + 64 * floor(self.(stats_hit[self.stat_count - 1]));\r
2779                 self.stat_fired = self.stat_count + 64 * floor(self.(stats_fired[self.stat_count - 1]));\r
2780                 self.stat_count -= 1;\r
2781         }\r
2782 \r
2783 #ifdef UID\r
2784         if(self.uid_kicktime)\r
2785         if(time > self.uid_kicktime)\r
2786         {\r
2787                 bprint("^3", self.netname, "^3 was kicked for missing UID.\n");\r
2788                 dropclient(self);\r
2789                 return;\r
2790         }\r
2791 #endif\r
2792 \r
2793         if(sv_maxidle && frametime)\r
2794         {\r
2795                 // WORKAROUND: only use dropclient in server frames (frametime set). Never use it in cl_movement frames (frametime zero).\r
2796                 float timeleft;\r
2797                 timeleft = ceil(sv_maxidle - (time - self.parm_idlesince));\r
2798                 if(timeleft <= 0)\r
2799                 {\r
2800                         bprint("^3", self.netname, "^3 was kicked for idling.\n");\r
2801                         AnnounceTo(self, "terminated");\r
2802                         dropclient(self);\r
2803                         return;\r
2804                 }\r
2805                 else if(timeleft <= 10)\r
2806                 {\r
2807                         if(timeleft != self.idlekick_lasttimeleft)\r
2808                         {\r
2809                                 centerprint_atprio(self, CENTERPRIO_IDLEKICK, strcat("^3Stop idling!\n^3Disconnecting in ", ftos(timeleft), "..."));\r
2810                                 AnnounceTo(self, strcat(ftos(timeleft), ""));\r
2811                         }\r
2812                 }\r
2813                 else\r
2814                 {\r
2815                         centerprint_expire(self, CENTERPRIO_IDLEKICK);\r
2816                 }\r
2817                 self.idlekick_lasttimeleft = timeleft;\r
2818         }\r
2819 \r
2820 #ifdef TETRIS\r
2821         if(self.impulse == 100)\r
2822                 ImpulseCommands();\r
2823         if (TetrisPostFrame())\r
2824                 return;\r
2825 #endif\r
2826 \r
2827         CheatFrame();\r
2828 \r
2829         if(self.classname == "player") {\r
2830                 CheckRules_Player();\r
2831                 UpdateChatBubble();\r
2832                 UpdateTeamBubble();\r
2833                 if (self.impulse)\r
2834                         ImpulseCommands();\r
2835                 if (intermission_running)\r
2836                         return;         // intermission or finale\r
2837 \r
2838                 GetPressedKeys();\r
2839         } else if (self.classname == "observer") {\r
2840                 //do nothing\r
2841         } else if (self.classname == "spectator") {\r
2842                 //do nothing\r
2843         }\r
2844 \r
2845         /*\r
2846         float i;\r
2847         for(i = 0; i < 1000; ++i)\r
2848         {\r
2849                 vector end;\r
2850                 end = self.origin + '0 0 1024' + 512 * randomvec();\r
2851                 tracebox(self.origin, self.mins, self.maxs, end, MOVE_NORMAL, self);\r
2852                 if(trace_fraction < 1)\r
2853                 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT))\r
2854                 {\r
2855                         print("I HIT SOLID: ", vtos(self.origin), " -> ", vtos(end), "\n");\r
2856                         break;\r
2857                 }\r
2858         }\r
2859         */\r
2860 \r
2861         Arena_Warmup();\r
2862 \r
2863         //pointparticles(particleeffectnum("machinegun_impact"), self.origin + self.view_ofs + '0 0 7', '0 0 0', 1);\r
2864 \r
2865         if(self.waypointsprite_attachedforcarrier)\r
2866                 WaypointSprite_UpdateHealth(self.waypointsprite_attachedforcarrier, '1 0 0' * healtharmor_maxdamage(self.health, self.armorvalue, cvar("g_balance_armor_blockpercent")));\r
2867         \r
2868         playerdemo_write();\r
2869 \r
2870         /*\r
2871         if(g_race)\r
2872                 dprint(sprintf("%f %.6f\n", time, race_GetFractionalLapCount(self)));\r
2873         */\r
2874 }\r