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