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