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