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