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