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