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