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