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