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