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