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