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