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