]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/Main.qc
Merge remote branch 'origin/master' into samual/updatecommands
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / Main.qc
1 // --------------------------------------------------------------------------
2 // BEGIN REQUIRED CSQC FUNCTIONS
3 //include "main.qh"
4
5 #define DP_CSQC_ENTITY_REMOVE_IS_B0RKED
6 void menu_show_error()
7 {
8         drawstring('0 200 0', _("ERROR - MENU IS VISIBLE BUT NO MENU WAS DEFINED!"), '8 8 0', '1 0 0', 1, 0);
9 }
10
11 // CSQC_Init : Called every time the CSQC code is initialized (essentially at map load)
12 // Useful for precaching things
13
14 void menu_sub_null()
15 {
16 }
17
18 #ifdef USE_FTE
19 float __engine_check;
20 #endif
21
22 void precache_playermodel(string m)
23 {
24         string f;
25
26         if(substring(m, -9,5) == "_lod1")
27                 return;
28         if(substring(m, -9,5) == "_lod2")
29                 return;
30         precache_model(m);
31         f = strcat(substring(m, 0, -5), "_lod1", substring(m, -4, -1));
32         if(fexists(f))
33                 precache_model(f);
34         f = strcat(substring(m, 0, -5), "_lod2", substring(m, -4, -1));
35         if(fexists(f))
36                 precache_model(f);
37
38         /*
39         float globhandle, i, n;
40         globhandle = search_begin(strcat(m, "_*.sounds"), TRUE, FALSE);
41         if (globhandle < 0)
42                 return;
43         n = search_getsize(globhandle);
44         for (i = 0; i < n; ++i)
45         {
46                 //print(search_getfilename(globhandle, i), "\n");
47                 f = search_getfilename(globhandle, i);
48                 PrecachePlayerSounds(f);
49         }
50         search_end(globhandle);
51         */
52 }
53 void precache_all_playermodels(string pattern)
54 {
55         float globhandle, i, n;
56         string f;
57
58         globhandle = search_begin(pattern, TRUE, FALSE);
59         if (globhandle < 0)
60                 return;
61         n = search_getsize(globhandle);
62         for (i = 0; i < n; ++i)
63         {
64                 //print(search_getfilename(globhandle, i), "\n");
65                 f = search_getfilename(globhandle, i);
66                 precache_playermodel(f);
67         }
68         search_end(globhandle);
69 }
70
71 string forcefog;
72 void WaypointSprite_Load();
73 void ConsoleCommand_macro_init();
74 void CSQC_Init(void)
75 {
76         prvm_language = cvar_string("prvm_language");
77
78 #ifdef USE_FTE
79 #pragma target ID
80         __engine_check = checkextension("DP_SV_WRITEPICTURE");
81         if(!__engine_check)
82         {
83                 print(_("^3Your engine build is outdated\n^3This Server uses a newer QC VM. Please update!\n"));
84                 localcmd("\ndisconnect\n");
85                 return;
86         }
87 #pragma target FTE
88 #endif
89
90         check_unacceptable_compiler_bugs();
91
92 #ifdef WATERMARK
93         print(sprintf(_("^4CSQC Build information: ^1%s\n"), WATERMARK()));
94 #endif
95
96         float i;
97
98 #ifdef COMPAT_XON050_ENGINE
99         // old engine lacks implementation of player_localnum
100         player_localnum = player_localentnum - 1;
101 #endif
102
103         binddb = db_create();
104         tempdb = db_create();
105         ClientProgsDB = db_load("client.db");
106         compressShortVector_init();
107
108         drawfont = FONT_USER+1;
109         menu_visible = FALSE;
110         menu_show = menu_show_error;
111         menu_action = menu_sub_null;
112
113         for(i = 0; i < 255; ++i)
114                 if(getplayerkeyvalue(i, "viewentity") == "")
115                         break;
116         maxclients = i;
117
118         //registercommand("hud_configure");
119         //registercommand("hud_save");
120         //registercommand("menu_action");
121         
122         ConsoleCommand_macro_init();
123
124         registercvar("hud_usecsqc", "1");
125         registercvar("scoreboard_columns", "default");
126
127         gametype = 0;
128
129         // hud_fields uses strunzone on the titles!
130         for(i = 0; i < MAX_HUD_FIELDS; ++i)
131                 hud_title[i] = strzone("(null)");
132
133         postinit = false;
134
135         calledhooks = 0;
136
137         teams = Sort_Spawn();
138         players = Sort_Spawn();
139
140         GetTeam(COLOR_SPECTATOR, true); // add specs first
141
142         RegisterWeapons();
143
144         WaypointSprite_Load();
145
146         // precaches
147         precache_model("null");
148         precache_sound("misc/hit.wav");
149         precache_sound("misc/typehit.wav");
150         if (autocvar_cl_precacheplayermodels)
151         {
152                 precache_all_playermodels("models/player/*.zym");
153                 precache_all_playermodels("models/player/*.dpm");
154                 precache_all_playermodels("models/player/*.md3");
155                 precache_all_playermodels("models/player/*.psk");
156                 precache_all_playermodels("models/player/*.iqm");
157         }
158
159         Projectile_Precache();
160         Hook_Precache();
161         GibSplash_Precache();
162         Casings_Precache();
163         DamageInfo_Precache();
164         Vehicles_Precache();
165         turrets_precache();
166   Announcer_Precache();
167         Tuba_Precache();
168         
169         if(autocvar_cl_reticle)
170         {
171                 if(autocvar_cl_reticle_item_normal) { precache_pic("gfx/reticle_normal"); }
172                 if(autocvar_cl_reticle_item_nex) { precache_pic("gfx/reticle_nex"); }
173         }
174         
175         get_mi_min_max_texcoords(1); // try the CLEVER way first
176         minimapname = strcat("gfx/", mi_shortname, "_radar.tga");
177         shortmapname = mi_shortname;
178
179         if(precache_pic(minimapname) == "")
180         {
181                 // but maybe we have a non-clever minimap
182                 minimapname = strcat("gfx/", mi_shortname, "_mini.tga");
183                 if(precache_pic(minimapname) == "")
184                         minimapname = ""; // FAIL
185                 else
186                         get_mi_min_max_texcoords(0); // load new texcoords
187         }
188
189         mi_center = (mi_min + mi_max) * 0.5;
190         mi_scale = mi_max - mi_min;
191         minimapname = strzone(minimapname);
192
193         WarpZone_Init();
194
195         hud_configure_prev = -1;
196         tab_panel = -1;
197 }
198
199 // CSQC_Shutdown : Called every time the CSQC code is shutdown (changing maps, quitting, etc)
200 float cvar_clientsettemp_restore();
201 void CSQC_Shutdown(void)
202 {
203 #ifdef USE_FTE
204 #pragma TARGET id
205         if(!__engine_check)
206                 return 0;
207 #pragma TARGET fte
208 #endif
209
210         WarpZone_Shutdown();
211
212         remove(teams);
213         remove(players);
214         db_close(binddb);
215         db_close(tempdb);
216         if(autocvar_cl_db_saveasdump)
217                 db_dump(ClientProgsDB, "client.db");
218         else
219                 db_save(ClientProgsDB, "client.db");
220         db_close(ClientProgsDB);
221
222         cvar_clientsettemp_restore();
223
224         if(camera_active)
225                 cvar_set("chase_active",ftos(chase_active_backup));
226
227         // unset the event chasecam's chase_active
228         if(autocvar_chase_active < 0)
229                 cvar_set("chase_active", "0");
230
231         if not(isdemo())
232         {
233                 if not(calledhooks & HOOK_START)
234                         localcmd("\n_cl_hook_gamestart nop\n");
235                 if not(calledhooks & HOOK_END)
236                         localcmd("\ncl_hook_gameend\n");
237         }
238 }
239
240 .float has_team;
241 float SetTeam(entity o, float Team)
242 {
243         entity tm;
244         if(teamplay)
245         {
246                 switch(Team)
247                 {
248                         case -1:
249                         case COLOR_TEAM1:
250                         case COLOR_TEAM2:
251                         case COLOR_TEAM3:
252                         case COLOR_TEAM4:
253                                 break;
254                         default:
255                                 if(GetTeam(Team, false) == world)
256                                 {
257                                         print(sprintf(_("trying to switch to unsupported team %d\n"), Team));
258                                         Team = COLOR_SPECTATOR;
259                                 }
260                                 break;
261                 }
262         }
263         else
264         {
265                 switch(Team)
266                 {
267                         case -1:
268                         case 0:
269                                 break;
270                         default:
271                                 if(GetTeam(Team, false) == world)
272                                 {
273                                         print(sprintf(_("trying to switch to unsupported team %d\n"), Team));
274                                         Team = COLOR_SPECTATOR;
275                                 }
276                                 break;
277                 }
278         }
279         if(Team == -1) // leave
280         {
281                 if(o.has_team)
282                 {
283                         tm = GetTeam(o.team, false);
284                         tm.team_size -= 1;
285                         o.has_team = 0;
286                         return TRUE;
287                 }
288         }
289         else
290         {
291                 if not(o.has_team)
292                 {
293                         o.team = Team;
294                         tm = GetTeam(Team, true);
295                         tm.team_size += 1;
296                         o.has_team = 1;
297                         return TRUE;
298                 }
299                 else if(Team != o.team)
300                 {
301                         tm = GetTeam(o.team, false);
302                         tm.team_size -= 1;
303                         o.team = Team;
304                         tm = GetTeam(Team, true);
305                         tm.team_size += 1;
306                         return TRUE;
307                 }
308         }
309         return FALSE;
310 }
311
312 void Playerchecker_Think()
313 {
314         float i;
315         entity e;
316         for(i = 0; i < maxclients; ++i)
317         {
318                 e = playerslots[i];
319                 if(GetPlayerName(i) == "")
320                 {
321                         if(e.sort_prev)
322                         {
323                                 // player disconnected
324                                 SetTeam(e, -1);
325                                 RemovePlayer(e);
326                                 e.sort_prev = world;
327                                 //e.gotscores = 0;
328                         }
329                 }
330                 else
331                 {
332                         if not(e.sort_prev)
333                         {
334                                 // player connected
335                                 if not(e)
336                                         playerslots[i] = e = spawn();
337                                 e.sv_entnum = i;
338                                 e.ping = 0;
339                                 e.ping_packetloss = 0;
340                                 e.ping_movementloss = 0;
341                                 //e.gotscores = 0; // we might already have the scores...
342                                 SetTeam(e, GetPlayerColor(i)); // will not hurt; later updates come with HUD_UpdatePlayerTeams
343                                 RegisterPlayer(e);
344                                 HUD_UpdatePlayerPos(e);
345                         }
346                 }
347         }
348         self.nextthink = time + 0.2;
349 }
350
351 void Porto_Init();
352 void TrueAim_Init();
353 void PostInit(void)
354 {
355         localcmd(strcat("\nscoreboard_columns_set ", autocvar_scoreboard_columns, ";\n"));
356
357         entity playerchecker;
358         playerchecker = spawn();
359         playerchecker.think = Playerchecker_Think;
360         playerchecker.nextthink = time + 0.2;
361
362         Porto_Init();
363         TrueAim_Init();
364
365         postinit = true;
366 }
367
368 float button_zoom;
369
370 // CSQC_InputEvent : Used to perform actions based on any key pressed, key released and mouse on the client.
371 // Return value should be 1 if CSQC handled the input, otherwise return 0 to have the input passed to the engine.
372 // All keys are in ascii.
373 // bInputType = 0 is key pressed, 1 is key released, 2 is mouse input.
374 // In the case of keyboard input, nPrimary is the ascii code, and nSecondary is 0.
375 // In the case of mouse input, nPrimary is xdelta, nSecondary is ydelta.
376 float CSQC_InputEvent(float bInputType, float nPrimary, float nSecondary)
377 {
378         float bSkipKey;
379         bSkipKey = false;
380
381         if (HUD_Panel_InputEvent(bInputType, nPrimary, nSecondary))
382                 return true;
383
384         if (MapVote_InputEvent(bInputType, nPrimary, nSecondary))
385                 return true;
386
387         if(menu_visible)
388                 if(menu_action(bInputType, nPrimary, nSecondary))
389                         return TRUE;
390
391         return bSkipKey;
392 }
393
394 // END REQUIRED CSQC FUNCTIONS
395 // --------------------------------------------------------------------------
396
397 // --------------------------------------------------------------------------
398 // BEGIN OPTIONAL CSQC FUNCTIONS
399 void Ent_RemoveEntCS()
400 {
401         entcs_receiver[self.sv_entnum] = world;
402 }
403 void Ent_ReadEntCS()
404 {
405         float sf;
406         InterpolateOrigin_Undo();
407
408         self.classname = "entcs_receiver";
409         sf = ReadByte();
410
411         if(sf & 1)
412                 self.sv_entnum = ReadByte();
413         if(sf & 2)
414         {
415                 self.origin_x = ReadShort();
416                 self.origin_y = ReadShort();
417                 self.origin_z = ReadShort();
418         }
419         if(sf & 4)
420         {
421                 self.angles_y = ReadByte() * 360.0 / 256;
422                 self.angles_x = self.angles_z = 0;
423         }
424         if(sf & 8)
425                 self.healthvalue = ReadByte() * 10;
426         if(sf & 16)
427                 self.armorvalue = ReadByte() * 10;
428
429         entcs_receiver[self.sv_entnum] = self;
430         self.entremove = Ent_RemoveEntCS;
431
432         InterpolateOrigin_Note();
433 }
434
435 void Ent_Remove();
436
437 void Ent_RemovePlayerScore()
438 {
439         float i;
440
441         if(self.owner)
442         {
443                 SetTeam(self.owner, -1);
444                 self.owner.gotscores = 0;
445                 for(i = 0; i < MAX_SCORE; ++i)
446                         self.owner.(scores[i]) = 0; // clear all scores
447         }
448 }
449
450 void Ent_ReadPlayerScore()
451 {
452         float i, n;
453         float isNew;
454         entity o;
455
456         // damnit -.- don't want to go change every single .sv_entnum in hud.qc AGAIN
457         // (no I've never heard of M-x replace-string, sed, or anything like that)
458         isNew = !self.owner; // workaround for DP bug
459         n = ReadByte()-1;
460
461 #ifdef DP_CSQC_ENTITY_REMOVE_IS_B0RKED
462         if(!isNew && n != self.sv_entnum)
463         {
464                 //print(_("A CSQC entity changed its owner!\n"));
465                 print(sprintf(_("A CSQC entity changed its owner! (edict: %d, classname: %s)\n"), num_for_edict(self), self.classname));
466                 isNew = true;
467                 Ent_Remove();
468                 self.enttype = ENT_CLIENT_SCORES;
469         }
470 #endif
471
472         self.sv_entnum = n;
473
474         if not(playerslots[self.sv_entnum])
475                 playerslots[self.sv_entnum] = spawn();
476         o = self.owner = playerslots[self.sv_entnum];
477         o.sv_entnum = self.sv_entnum;
478         o.gotscores = 1;
479
480         //if not(o.sort_prev)
481         //      RegisterPlayer(o);
482         //playerchecker will do this for us later, if it has not already done so
483
484         float sf, lf;
485 #if MAX_SCORE <= 8
486         sf = ReadByte();
487         lf = ReadByte();
488 #else
489         sf = ReadShort();
490         lf = ReadShort();
491 #endif
492         float p;
493         for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
494                 if(sf & p)
495                 {
496                         if(lf & p)
497                                 o.(scores[i]) = ReadInt24_t();
498                         else
499                                 o.(scores[i]) = ReadChar();
500                 }
501
502         if(o.sort_prev)
503                 HUD_UpdatePlayerPos(o); // if not registered, we cannot do this yet!
504
505         self.entremove = Ent_RemovePlayerScore;
506 }
507
508 void Ent_ReadTeamScore()
509 {
510         float i;
511         entity o;
512
513         self.team = ReadByte();
514         o = self.owner = GetTeam(self.team, true); // these team numbers can always be trusted
515
516         float sf, lf;
517 #if MAX_TEAMSCORE <= 8
518         sf = ReadByte();
519         lf = ReadByte();
520 #else
521         sf = ReadShort();
522         lf = ReadShort();
523 #endif
524         float p;
525         for(i = 0, p = 1; i < MAX_TEAMSCORE; ++i, p *= 2)
526                 if(sf & p)
527                 {
528                         if(lf & p)
529                                 o.(teamscores[i]) = ReadInt24_t();
530                         else
531                                 o.(teamscores[i]) = ReadChar();
532                 }
533
534         HUD_UpdateTeamPos(o);
535 }
536
537 void Ent_ClientData()
538 {
539         float f;
540         float newspectatee_status;
541
542         f = ReadByte();
543
544         scoreboard_showscores_force = (f & 1);
545
546         if(f & 2)
547         {
548                 newspectatee_status = ReadByte();
549                 if(newspectatee_status == player_localnum + 1)
550                         newspectatee_status = -1; // observing
551         }
552         else
553                 newspectatee_status = 0;
554
555         spectatorbutton_zoom = (f & 4);
556
557         if(f & 8)
558         {
559                 angles_held_status = 1;
560                 angles_held_x = ReadAngle();
561                 angles_held_y = ReadAngle();
562                 angles_held_z = 0;
563         }
564         else
565                 angles_held_status = 0;
566
567         if(newspectatee_status != spectatee_status)
568         {
569                 // clear race stuff
570                 race_laptime = 0;
571                 race_checkpointtime = 0;
572         }
573         if (autocvar_hud_panel_healtharmor_progressbar_gfx)
574         {
575                 if ( (spectatee_status == -1 && newspectatee_status > 0) //before observing, now spectating
576                   || (spectatee_status > 0 && newspectatee_status > 0 && spectatee_status != newspectatee_status) //changed spectated player
577                 )
578                         prev_p_health = -1;
579                 else if(spectatee_status && !newspectatee_status) //before observing/spectating, now playing
580                         prev_health = -1;
581         }
582         spectatee_status = newspectatee_status;
583
584         // non-COMPAT_XON050_ENGINE: we could get rid of spectatee_status, and derive it from player_localentnum and player_localnum
585 }
586
587 void Ent_Nagger()
588 {
589         float nags, i, j, b, f;
590
591         nags = ReadByte(); // NAGS NAGS NAGS NAGS NAGS NAGS NADZ NAGS NAGS NAGS
592
593         if(!(nags & 4))
594         {
595                 if(vote_called_vote)
596                         strunzone(vote_called_vote);
597                 vote_called_vote = string_null;
598                 vote_active = 0;
599         }
600         else
601         {
602                 vote_active = 1;
603         }
604
605         if(nags & 64)
606         {
607                 vote_yescount = ReadByte();
608                 vote_nocount = ReadByte();
609                 vote_needed = ReadByte();
610                 vote_highlighted = ReadChar();
611         }
612
613         if(nags & 128)
614         {
615                 if(vote_called_vote)
616                         strunzone(vote_called_vote);
617                 vote_called_vote = strzone(ColorTranslateRGB(ReadString()));
618         }
619
620         if(nags & 1)
621         {
622                 for(j = 0; j < maxclients; ++j)
623                         if(playerslots[j])
624                                 playerslots[j].ready = 1;
625                 for(i = 1; i <= maxclients; i += 8)
626                 {
627                         f = ReadByte();
628                         for(j = i-1, b = 1; b < 256; b *= 2, ++j)
629                                 if not(f & b)
630                                         if(playerslots[j])
631                                                 playerslots[j].ready = 0;
632                 }
633         }
634
635         ready_waiting = (nags & 1);
636         ready_waiting_for_me = (nags & 2);
637         vote_waiting = (nags & 4);
638         vote_waiting_for_me = (nags & 8);
639         warmup_stage = (nags & 16);
640 }
641
642 void Ent_RandomSeed()
643 {
644         float s;
645         prandom_debug();
646         s = ReadShort();
647         psrandom(s);
648 }
649
650 void Ent_ReadAccuracy(void)
651 {
652         float sf, f, w, b;
653         sf = ReadInt24_t();
654         if(sf == 0)
655         {
656                 for(w = 0; w <= WEP_LAST - WEP_FIRST; ++w)
657                         weapon_accuracy[w] = -1;
658                 return;
659         }
660
661         for(w = 0, f = 1; w <= WEP_LAST - WEP_FIRST; ++w, f *= 2)
662         {
663                 if(sf & f)
664                 {
665                         b = ReadByte();
666                         if(b == 0)
667                                 weapon_accuracy[w] = -1;
668                         else if(b == 255)
669                                 weapon_accuracy[w] = 1.0; // no better error handling yet, sorry
670                         else
671                                 weapon_accuracy[w] = (b - 1.0) / 100.0;
672                 }
673         }
674 }
675
676 // CSQC_Ent_Update : Called every frame that the server has indicated an update to the SSQC / CSQC entity has occured.
677 // The only parameter reflects if the entity is "new" to the client, meaning it just came into the client's PVS.
678 void Ent_RadarLink();
679 void Ent_Init();
680 void Ent_ScoresInfo();
681 void CSQC_Ent_Update(float bIsNewEntity)
682 {
683         float t;
684         float savetime;
685         t = ReadByte();
686
687         // set up the "time" global for received entities to be correct for interpolation purposes
688         savetime = time;
689         if(servertime)
690         {
691                 time = servertime;
692         }
693         else
694         {
695                 serverprevtime = time;
696                 serverdeltatime = getstatf(STAT_MOVEVARS_TICRATE) * getstatf(STAT_MOVEVARS_TIMESCALE);
697                 time = serverprevtime + serverdeltatime;
698         }
699
700 #ifdef DP_CSQC_ENTITY_REMOVE_IS_B0RKED
701         if(self.enttype)
702         {
703                 if(t != self.enttype || bIsNewEntity)
704                 {
705                         //print(_("A CSQC entity changed its type!\n"));
706                         print(sprintf(_("A CSQC entity changed its type! (edict: %d, server: %d, type: %d -> %d)\n"), num_for_edict(self), self.entnum, self.enttype, t));
707                         Ent_Remove();
708                         bIsNewEntity = 1;
709                 }
710         }
711         else
712         {
713                 if(!bIsNewEntity)
714                 {
715                         print(sprintf(_("A CSQC entity appeared out of nowhere! (edict: %d, server: %d, type: %d)\n"), num_for_edict(self), self.entnum, t));
716                         bIsNewEntity = 1;
717                 }
718         }
719 #endif
720         self.enttype = t;
721         switch(t)
722         {
723                 case ENT_CLIENT_ENTCS: Ent_ReadEntCS(); break;
724                 case ENT_CLIENT_SCORES: Ent_ReadPlayerScore(); break;
725                 case ENT_CLIENT_TEAMSCORES: Ent_ReadTeamScore(); break;
726                 case ENT_CLIENT_POINTPARTICLES: Ent_PointParticles(); break;
727                 case ENT_CLIENT_RAINSNOW: Ent_RainOrSnow(); break;
728                 case ENT_CLIENT_LASER: Ent_Laser(); break;
729                 case ENT_CLIENT_NAGGER: Ent_Nagger(); break;
730                 case ENT_CLIENT_WAYPOINT: Ent_WaypointSprite(); break;
731                 case ENT_CLIENT_RADARLINK: Ent_RadarLink(); break;
732                 case ENT_CLIENT_PROJECTILE: Ent_Projectile(); break;
733                 case ENT_CLIENT_GIBSPLASH: Ent_GibSplash(bIsNewEntity); break;
734                 case ENT_CLIENT_DAMAGEINFO: Ent_DamageInfo(bIsNewEntity); break;
735                 case ENT_CLIENT_CASING: Ent_Casing(bIsNewEntity); break;
736                 case ENT_CLIENT_INIT: Ent_Init(); break;
737                 case ENT_CLIENT_SCORES_INFO: Ent_ScoresInfo(); break;
738                 case ENT_CLIENT_MAPVOTE: Ent_MapVote(); break;
739                 case ENT_CLIENT_CLIENTDATA: Ent_ClientData(); break;
740                 case ENT_CLIENT_RANDOMSEED: Ent_RandomSeed(); break;
741                 case ENT_CLIENT_WALL: Ent_Wall(); break;
742                 case ENT_CLIENT_MODELEFFECT: Ent_ModelEffect(bIsNewEntity); break;
743                 case ENT_CLIENT_TUBANOTE: Ent_TubaNote(bIsNewEntity); break;
744                 case ENT_CLIENT_WARPZONE: WarpZone_Read(bIsNewEntity); break;
745                 case ENT_CLIENT_WARPZONE_CAMERA: WarpZone_Camera_Read(bIsNewEntity); break;
746                 case ENT_CLIENT_WARPZONE_TELEPORTED: WarpZone_Teleported_Read(bIsNewEntity); break;
747                 case ENT_CLIENT_TRIGGER_MUSIC: Ent_ReadTriggerMusic(); break;
748                 case ENT_CLIENT_HOOK: Ent_ReadHook(bIsNewEntity, ENT_CLIENT_HOOK); break;
749                 case ENT_CLIENT_LGBEAM: Ent_ReadHook(bIsNewEntity, ENT_CLIENT_LGBEAM); break;
750                 case ENT_CLIENT_GAUNTLET: Ent_ReadHook(bIsNewEntity, ENT_CLIENT_GAUNTLET); break;
751                 case ENT_CLIENT_ACCURACY: Ent_ReadAccuracy(); break;
752                 case ENT_CLIENT_AUXILIARYXHAIR: Net_AuXair2(bIsNewEntity); break;
753                 case ENT_CLIENT_TURRET: ent_turret(); break; 
754                 case ENT_CLIENT_MODEL: CSQCModel_Read(); break; 
755                 default:
756                         //error(strcat(_("unknown entity type in CSQC_Ent_Update: %d\n"), self.enttype));
757                         error(sprintf(_("Unknown entity type in CSQC_Ent_Update (enttype: %d, edict: %d, classname: %s)\n"), self.enttype, num_for_edict(self), self.classname));
758                         break;
759         }
760
761         time = savetime;
762 }
763 // Destructor, but does NOT deallocate the entity by calling remove(). Also
764 // used when an entity changes its type. For an entity that someone interacts
765 // with others, make sure it can no longer do so.
766 void Ent_Remove()
767 {
768         if(self.entremove)
769                 self.entremove();
770
771         self.enttype = 0;
772         self.classname = "";
773         self.draw = menu_sub_null;
774         self.entremove = menu_sub_null;
775         // TODO possibly set more stuff to defaults
776 }
777 // CSQC_Ent_Remove : Called when the server requests a SSQC / CSQC entity to be removed.  Essentially call remove(self) as well.
778 void CSQC_Ent_Remove()
779 {
780         if(self.enttype)
781                 Ent_Remove();
782         remove(self);
783 }
784
785 void Gamemode_Init()
786 {
787         if not(isdemo())
788         {
789                 localcmd("\n_cl_hook_gamestart ", GametypeNameFromType(gametype), "\n");
790                 calledhooks |= HOOK_START;
791         }
792 }
793 // CSQC_Parse_StuffCmd : Provides the stuffcmd string in the first parameter that the server provided.  To execute standard behavior, simply execute localcmd with the string.
794 void CSQC_Parse_StuffCmd(string strMessage)
795 {
796         localcmd(strMessage);
797 }
798 // CSQC_Parse_Print : Provides the print string in the first parameter that the server provided.  To execute standard behavior, simply execute print with the string.
799 void CSQC_Parse_Print(string strMessage)
800 {
801         print(ColorTranslateRGB(strMessage));
802 }
803
804 // CSQC_Parse_CenterPrint : Provides the centerprint_hud string in the first parameter that the server provided.
805 void CSQC_Parse_CenterPrint(string strMessage)
806 {
807         centerprint_hud(strMessage);
808 }
809
810 string notranslate_fogcmd1 = "\nfog ";
811 string notranslate_fogcmd2 = "\nr_fog_exp2 0\nr_drawfog 1\n";
812 void Fog_Force()
813 {
814         // TODO somehow thwart prvm_globalset client ...
815
816         if(forcefog != "")
817                 localcmd(strcat(notranslate_fogcmd1, forcefog, notranslate_fogcmd2));
818 }
819
820 void Gamemode_Init();
821 void Ent_ScoresInfo()
822 {
823         float i;
824         self.classname = "ent_client_scores_info";
825         gametype = ReadByte();
826         for(i = 0; i < MAX_SCORE; ++i)
827         {
828                 scores_label[i] = strzone(ReadString());
829                 scores_flags[i] = ReadByte();
830         }
831         for(i = 0; i < MAX_TEAMSCORE; ++i)
832         {
833                 teamscores_label[i] = strzone(ReadString());
834                 teamscores_flags[i] = ReadByte();
835         }
836         HUD_InitScores();
837         Gamemode_Init();
838 }
839
840 void Ent_Init()
841 {
842         self.classname = "ent_client_init";
843
844         nb_pb_period = ReadByte() / 32; //Accuracy of 1/32th
845
846         hook_shotorigin[0] = decompressShotOrigin(ReadInt24_t());
847         hook_shotorigin[1] = decompressShotOrigin(ReadInt24_t());
848         hook_shotorigin[2] = decompressShotOrigin(ReadInt24_t());
849         hook_shotorigin[3] = decompressShotOrigin(ReadInt24_t());
850         electro_shotorigin[0] = decompressShotOrigin(ReadInt24_t());
851         electro_shotorigin[1] = decompressShotOrigin(ReadInt24_t());
852         electro_shotorigin[2] = decompressShotOrigin(ReadInt24_t());
853         electro_shotorigin[3] = decompressShotOrigin(ReadInt24_t());
854         gauntlet_shotorigin[0] = decompressShotOrigin(ReadInt24_t());
855         gauntlet_shotorigin[1] = decompressShotOrigin(ReadInt24_t());
856         gauntlet_shotorigin[2] = decompressShotOrigin(ReadInt24_t());
857         gauntlet_shotorigin[3] = decompressShotOrigin(ReadInt24_t());
858
859         if(forcefog)
860                 strunzone(forcefog);
861         forcefog = strzone(ReadString());
862
863         armorblockpercent = ReadByte() / 255.0;
864
865         g_weaponswitchdelay = ReadByte() / 255.0;
866
867         g_balance_grenadelauncher_bouncefactor = ReadCoord();
868         g_balance_grenadelauncher_bouncestop = ReadCoord();
869         g_balance_electro_secondary_bouncefactor = ReadCoord();
870         g_balance_electro_secondary_bouncestop = ReadCoord();
871
872         nex_scope = !ReadByte();
873         rifle_scope = !ReadByte();
874
875         serverflags = ReadByte();
876
877         minelayer_maxmines = ReadByte();
878
879         hagar_maxrockets = ReadByte();
880
881         g_trueaim_minrange = ReadCoord();
882
883         if(!postinit)
884                 PostInit();
885 }
886
887 void Net_ReadRace()
888 {
889         float b;
890
891         b = ReadByte();
892
893         switch(b)
894         {
895                 case RACE_NET_CHECKPOINT_HIT_QUALIFYING:
896                         race_checkpoint = ReadByte();
897                         race_time = ReadInt24_t();
898                         race_previousbesttime = ReadInt24_t();
899                         if(race_previousbestname)
900                                 strunzone(race_previousbestname);
901                         race_previousbestname = strzone(ColorTranslateRGB(ReadString()));
902
903                         race_checkpointtime = time;
904
905                         if(race_checkpoint == 0 || race_checkpoint == 254)
906                         {
907                                 race_penaltyaccumulator = 0;
908                                 race_laptime = time; // valid
909                         }
910
911                         break;
912
913                 case RACE_NET_CHECKPOINT_CLEAR:
914                         race_laptime = 0;
915                         race_checkpointtime = 0;
916                         break;
917
918                 case RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING:
919                         race_laptime = ReadCoord();
920                         race_checkpointtime = -99999;
921                         // fall through
922                 case RACE_NET_CHECKPOINT_NEXT_QUALIFYING:
923                         race_nextcheckpoint = ReadByte();
924
925                         race_nextbesttime = ReadInt24_t();
926                         if(race_nextbestname)
927                                 strunzone(race_nextbestname);
928                         race_nextbestname = strzone(ColorTranslateRGB(ReadString()));
929                         break;
930
931                 case RACE_NET_CHECKPOINT_HIT_RACE:
932                         race_mycheckpoint = ReadByte();
933                         race_mycheckpointtime = time;
934                         race_mycheckpointdelta = ReadInt24_t();
935                         race_mycheckpointlapsdelta = ReadByte();
936                         if(race_mycheckpointlapsdelta >= 128)
937                                 race_mycheckpointlapsdelta -= 256;
938                         if(race_mycheckpointenemy)
939                                 strunzone(race_mycheckpointenemy);
940                         race_mycheckpointenemy = strzone(ColorTranslateRGB(ReadString()));
941                         break;
942
943                 case RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT:
944                         race_othercheckpoint = ReadByte();
945                         race_othercheckpointtime = time;
946                         race_othercheckpointdelta = ReadInt24_t();
947                         race_othercheckpointlapsdelta = ReadByte();
948                         if(race_othercheckpointlapsdelta >= 128)
949                                 race_othercheckpointlapsdelta -= 256;
950                         if(race_othercheckpointenemy)
951                                 strunzone(race_othercheckpointenemy);
952                         race_othercheckpointenemy = strzone(ColorTranslateRGB(ReadString()));
953                         break;
954
955                 case RACE_NET_PENALTY_RACE:
956                         race_penaltyeventtime = time;
957                         race_penaltytime = ReadShort();
958                         //race_penaltyaccumulator += race_penaltytime;
959                         if(race_penaltyreason)
960                                 strunzone(race_penaltyreason);
961                         race_penaltyreason = strzone(ReadString());
962                         break;
963
964                 case RACE_NET_PENALTY_QUALIFYING:
965                         race_penaltyeventtime = time;
966                         race_penaltytime = ReadShort();
967                         race_penaltyaccumulator += race_penaltytime;
968                         if(race_penaltyreason)
969                                 strunzone(race_penaltyreason);
970                         race_penaltyreason = strzone(ReadString());
971                         break;
972
973                 case RACE_NET_SERVER_RECORD:
974                         race_server_record = ReadInt24_t();
975                         break;
976                 case RACE_NET_SPEED_AWARD:
977                         race_speedaward = ReadInt24_t();
978                         if(race_speedaward_holder)
979                                 strunzone(race_speedaward_holder);
980                         race_speedaward_holder = strzone(ReadString());
981                         break;
982                 case RACE_NET_SPEED_AWARD_BEST:
983                         race_speedaward_alltimebest = ReadInt24_t();
984                         if(race_speedaward_alltimebest_holder)
985                                 strunzone(race_speedaward_alltimebest_holder);
986                         race_speedaward_alltimebest_holder = strzone(ReadString());
987                         break;
988                 case RACE_NET_SERVER_RANKINGS:
989                         float pos, prevpos, del;
990                         pos = ReadShort();
991                         prevpos = ReadShort();
992                         del = ReadShort();
993
994                         // move other rankings out of the way
995                         float i;
996                         if (prevpos) {
997                                 for (i=prevpos-1;i>pos-1;--i) {
998                                         grecordtime[i] = grecordtime[i-1];
999                                         if(grecordholder[i])
1000                                                 strunzone(grecordholder[i]);
1001                                         grecordholder[i] = strzone(grecordholder[i-1]);
1002                                 }
1003                         } else if (del) { // a record has been deleted by the admin
1004                                 for (i=pos-1; i<= RANKINGS_CNT-1; ++i) {
1005                                         if (i == RANKINGS_CNT-1) { // clear out last record
1006                                                 grecordtime[i] = 0;
1007                                                 if (grecordholder[i])
1008                                                         strunzone(grecordholder[i]);
1009                                                 grecordholder[i] = string_null;
1010                                         }
1011                                         else {
1012                                                 grecordtime[i] = grecordtime[i+1];
1013                                                 if (grecordholder[i])
1014                                                         strunzone(grecordholder[i]);
1015                                                 grecordholder[i] = strzone(grecordholder[i+1]);
1016                                         }
1017                                 }
1018                         } else { // player has no ranked record yet
1019                                 for (i=RANKINGS_CNT-1;i>pos-1;--i) {
1020                                         grecordtime[i] = grecordtime[i-1];
1021                                         if(grecordholder[i])
1022                                                 strunzone(grecordholder[i]);
1023                                         grecordholder[i] = strzone(grecordholder[i-1]);
1024                                 }
1025                         }
1026
1027                         // store new ranking
1028                         if(grecordholder[pos-1] != "")
1029                                 strunzone(grecordholder[pos-1]);
1030                         grecordholder[pos-1] = strzone(ReadString());
1031                         grecordtime[pos-1] = ReadInt24_t();
1032                         if(grecordholder[pos-1] == GetPlayerName(player_localnum))
1033                                 race_myrank = pos;
1034                         break;
1035                 case RACE_NET_SERVER_STATUS:
1036                         race_status = ReadShort();
1037                         if(race_status_name)
1038                                 strunzone(race_status_name);
1039                         race_status_name = strzone(ReadString());
1040         }
1041 }
1042
1043 void Net_ReadSpawn()
1044 {
1045         zoomin_effect = 1;
1046         current_viewzoom = 0.6;
1047 }
1048
1049 void Net_TeamNagger()
1050 {
1051         teamnagger = 1;
1052 }
1053
1054 void Net_ReadPingPLReport()
1055 {
1056         float e, pi, pl, ml;
1057         e = ReadByte();
1058         pi = ReadShort();
1059         pl = ReadByte();
1060         ml = ReadByte();
1061         if not(playerslots[e])
1062                 return;
1063         playerslots[e].ping = pi;
1064         playerslots[e].ping_packetloss = pl / 255.0;
1065         playerslots[e].ping_movementloss = ml / 255.0;
1066 }
1067
1068 void Net_WeaponComplain() {
1069         complain_weapon = ReadByte();
1070
1071         if(complain_weapon_name)
1072                 strunzone(complain_weapon_name);
1073         complain_weapon_name = strzone(ReadString());
1074
1075         complain_weapon_type = ReadByte();
1076
1077         complain_weapon_time = time;
1078         weapontime = time; // ping the weapon panel
1079 }
1080
1081 // CSQC_Parse_TempEntity : Handles all temporary entity network data in the CSQC layer.
1082 // You must ALWAYS first acquire the temporary ID, which is sent as a byte.
1083 // Return value should be 1 if CSQC handled the temporary entity, otherwise return 0 to have the engine process the event.
1084 float CSQC_Parse_TempEntity()
1085 {
1086         float bHandled;
1087                 bHandled  = true;
1088         // Acquire TE ID
1089         float nTEID;
1090                 nTEID = ReadByte();
1091
1092                 // NOTE: Could just do return instead of break...
1093         switch(nTEID)
1094         {
1095                 case TE_CSQC_TARGET_MUSIC:
1096                         Net_TargetMusic();
1097                         bHandled = true;
1098                         break;
1099                 case TE_CSQC_PICTURE:
1100                         Net_MapVote_Picture();
1101                         bHandled = true;
1102                         break;
1103                 case TE_CSQC_RACE:
1104                         Net_ReadRace();
1105                         bHandled = true;
1106                         break;
1107                 case TE_CSQC_SPAWN:
1108                         Net_ReadSpawn();
1109                         bHandled = true;
1110                         break;
1111                 case TE_CSQC_ZCURVEPARTICLES:
1112                         Net_ReadZCurveParticles();
1113                         bHandled = true;
1114                         break;
1115                 case TE_CSQC_NEXGUNBEAMPARTICLE:
1116                         Net_ReadNexgunBeamParticle();
1117                         bHandled = true;
1118                         break;
1119                 case TE_CSQC_TEAMNAGGER:
1120                         Net_TeamNagger();
1121                         bHandled = true;
1122                         break;
1123                 case TE_CSQC_LIGHTNINGARC:
1124                         Net_ReadLightningarc();
1125                         bHandled = true;
1126                         break;
1127                 case TE_CSQC_PINGPLREPORT:
1128                         Net_ReadPingPLReport();
1129                         bHandled = true;
1130                         break;
1131                 case TE_CSQC_ANNOUNCE:
1132                         Announcer_Play(ReadString());
1133                         bHandled = true;
1134                         break;
1135                 case TE_CSQC_KILLNOTIFY:
1136                         HUD_KillNotify(ReadString(), ReadString(), ReadString(), ReadShort(), ReadByte());
1137                         bHandled = true;
1138                         break;
1139                 case TE_CSQC_KILLCENTERPRINT:
1140                         HUD_KillCenterprint(ReadString(), ReadString(), ReadShort(), ReadByte());
1141                         bHandled = true;
1142                         break;
1143                 case TE_CSQC_CENTERPRINT_GENERIC:
1144                         float id;
1145                         string s;
1146                         id = ReadByte();
1147                         s = ReadString();
1148                         if (id != 0 && s != "")
1149                                 centerprint_generic(id, s, ReadByte(), ReadByte());
1150                         else
1151                                 centerprint_generic(id, s, 0, 0);
1152                         bHandled = true;
1153                         break;
1154                 case TE_CSQC_WEAPONCOMPLAIN:
1155                         Net_WeaponComplain();
1156                         bHandled = true;
1157                         break;
1158                 case TE_CSQC_VEHICLESETUP:
1159                         Net_VehicleSetup();
1160                         bHandled = true;
1161                         break;
1162                 default:
1163                         // No special logic for this temporary entity; return 0 so the engine can handle it
1164                         bHandled = false;
1165                         break;
1166         }
1167
1168         return bHandled;
1169 }
1170
1171 string getcommandkey(string text, string command)
1172 {
1173         string keys;
1174         float n, j, k, l;
1175
1176         if (!autocvar_hud_showbinds)
1177                 return text;
1178
1179         keys = db_get(binddb, command);
1180         if (!keys)
1181         {
1182                 n = tokenize(findkeysforcommand(command, 0)); // uses '...' strings
1183                 for(j = 0; j < n; ++j)
1184                 {
1185                         k = stof(argv(j));
1186                         if(k != -1)
1187                         {
1188                                 if ("" == keys)
1189                                         keys = keynumtostring(k);
1190                                 else
1191                                         keys = strcat(keys, ", ", keynumtostring(k));
1192
1193                                 ++l;
1194                                 if (autocvar_hud_showbinds_limit > 0 && autocvar_hud_showbinds_limit >= l) break;
1195                         }
1196
1197                 }
1198                 db_put(binddb, command, keys);
1199         }
1200
1201         if ("" == keys) {
1202                 if (autocvar_hud_showbinds > 1)
1203                         return sprintf(_("%s (not bound)"), text);
1204                 else
1205                         return text;
1206         }
1207         else if (autocvar_hud_showbinds > 1)
1208                 return sprintf(_("%s (%s)"), text, keys);
1209         else
1210                 return keys;
1211 }