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