]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/Main.qc
fix scoreboard fading
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / Main.qc
1 // --------------------------------------------------------------------------
2 // BEGIN REQUIRED CSQC FUNCTIONS
3 //include "main.qh"
4
5 #define DP_CSQC_ENTITY_REMOVE_IS_B0RKED
6
7 void cvar_clientsettemp(string cv, string val)
8 {
9         entity e;
10         for(e = world; (e = find(e, classname, "saved_cvar_value")); )
11                 if(e.netname == cv)
12                         goto saved;
13         e = spawn();
14         e.classname = "saved_cvar_value";
15         e.netname = strzone(cv);
16         e.message = strzone(cvar_string(cv));
17 :saved
18         cvar_set(cv, val);
19 }
20
21 void cvar_clientsettemp_restore()
22 {
23         entity e;
24         for(e = world; (e = find(e, classname, "saved_cvar_value")); )
25                         cvar_set(e.netname, e.message);
26 }
27
28 void() menu_show_error =
29 {
30         drawstring('0 200 0', "ERROR - MENU IS VISIBLE BUT NO MENU WAS DEFINED!", '8 8 0', '1 0 0', 1, 0);
31 };
32
33 // CSQC_Init : Called every time the CSQC code is initialized (essentially at map load)
34 // Useful for precaching things
35
36 void() menu_sub_null =
37 {
38 };
39
40 #ifdef USE_FTE
41 float __engine_check;
42 #endif
43
44 string forcefog;
45 string cl_announcer_prev;
46 void WaypointSprite_Load();
47 void CSQC_Init(void)
48 {
49 #ifdef USE_FTE
50 #pragma target ID
51         __engine_check = checkextension("DP_SV_WRITEPICTURE");
52         if(!__engine_check)
53         {
54                 print("^3Your engine build is outdated\n^3This Server uses a newer QC VM. Please update!\n");
55                 localcmd("\ndisconnect\n");
56                 return;
57         }
58 #pragma target FTE
59 #endif
60         
61         check_unacceptable_compiler_bugs();
62
63 #ifdef WATERMARK
64         print("^4CSQC Build information: ", WATERMARK(), "\n");
65 #endif
66
67         float i;
68         CSQC_CheckEngine();
69
70         binddb = db_create();
71         tempdb = db_create();
72         ClientProgsDB = db_load("client.db");
73         compressShortVector_init();
74
75         drawfont = 0;
76         menu_visible = FALSE;
77         menu_show = menu_show_error;
78         menu_action = menu_sub_null;
79
80         for(i = 0; i < 255; ++i)
81                 if(getplayerkey(i, "viewentity") == "")
82                         break;
83         maxclients = i;
84
85         //ctf_temp_1 = "";
86         // localcmd("alias order \"cmd order $*\""); enable if ctf-command thingy is used
87         //registercmd("ctf_menu");
88         registercmd("ons_map");
89         registercmd("hud_configure");
90         registercmd("hud_save");
91         //registercmd("menu_action");
92
93         registercmd("+button3");
94         registercmd("-button3");
95         registercmd("+button4");
96         registercmd("-button4");
97         registercmd("+showscores");registercmd("-showscores");
98         registercmd("+showaccuracy");registercmd("-showaccuracy");
99
100 #ifndef CAMERATEST
101         if(isdemo())
102         {
103 #endif
104                 registercmd("+forward");registercmd("-forward");
105                 registercmd("+back");registercmd("-back");
106                 registercmd("+moveup");registercmd("-moveup");
107                 registercmd("+movedown");registercmd("-movedown");
108                 registercmd("+moveright");registercmd("-moveright");
109                 registercmd("+moveleft");registercmd("-moveleft");
110                 registercmd("+roll_right");registercmd("-roll_right");
111                 registercmd("+roll_left");registercmd("-roll_left");
112 #ifndef CAMERATEST
113         }
114 #endif
115         registercvar("hud_usecsqc", "1");
116         registercvar("hud_columns", "default", CVAR_SAVE);
117
118         gametype = 0;
119
120         // hud_fields uses strunzone on the titles!
121         for(i = 0; i < MAX_HUD_FIELDS; ++i)
122                 hud_title[i] = strzone("(null)");
123
124         postinit = false;
125
126         calledhooks = 0;
127
128         teams = Sort_Spawn();
129         players = Sort_Spawn();
130
131         GetTeam(COLOR_SPECTATOR, true); // add specs first
132
133         cvar_clientsettemp("_supports_weaponpriority", "1");
134
135         RegisterWeapons();
136
137         WaypointSprite_Load();
138
139         // precaches
140         Projectile_Precache();
141         GibSplash_Precache();
142         Casings_Precache();
143         DamageInfo_Precache();
144         if(cvar_string("cl_announcer") != cl_announcer_prev) {
145                 Announcer_Precache();
146                 if(cl_announcer_prev)
147                         strunzone(cl_announcer_prev);
148                 cl_announcer_prev = strzone(cvar_string("cl_announcer"));
149         }
150         Tuba_Precache();
151
152 #ifdef UID
153         {
154                 // find the user ID
155                 string uid;
156                 registercvar("_cl_userid", "", CVAR_SAVE);
157                 uid = cvar_string("_cl_userid");
158                 if(strlen(uid) < 16)
159                 {
160                         uid = "";
161                         for(i = 0; i < 4; ++i)
162                                 uid = strcat(uid, substring(ftos(floor(10000 + random() * 10000)), 1, -1));
163                 }
164                 cvar_set("_cl_userid", uid);
165                 localcmd(strcat("\ncmd uid ", uid, "\n"));
166         }
167 #endif
168
169         get_mi_min_max_texcoords(1); // try the CLEVER way first
170         minimapname = strcat("gfx/", mi_shortname, "_radar.tga");
171         shortmapname = mi_shortname;
172
173         if(precache_pic(minimapname) == "")
174         {
175                 // but maybe we have a non-clever minimap
176                 minimapname = strcat("gfx/", mi_shortname, "_mini.tga");
177                 if(precache_pic(minimapname) == "")
178                         minimapname = ""; // FAIL
179                 else
180                         get_mi_min_max_texcoords(0); // load new texcoords
181         }
182
183         mi_center = (mi_min + mi_max) * 0.5;
184         mi_scale = mi_max - mi_min;
185         minimapname = strzone(minimapname);
186
187         WarpZone_Init();
188 }
189
190 // CSQC_Shutdown : Called every time the CSQC code is shutdown (changing maps, quitting, etc)
191 void CSQC_Shutdown(void)
192 {
193 #ifdef USE_FTE
194 #pragma TARGET id
195         if(!__engine_check)
196                 return 0;
197 #pragma TARGET fte
198 #endif
199
200         WarpZone_Shutdown();
201
202         remove(teams);
203         remove(players);
204         db_close(binddb);
205         db_close(tempdb);
206         db_save(ClientProgsDB, "client.db");
207         db_close(ClientProgsDB);
208
209         cvar_clientsettemp_restore();
210
211         if(camera_active)
212                 cvar_set("chase_active",ftos(chase_active_backup));
213
214         if not(isdemo())
215         {
216                 if not(calledhooks & HOOK_START)
217                         localcmd("\n_cl_hook_gamestart nop;");
218                 if not(calledhooks & HOOK_END)
219                         localcmd("\ncl_hook_gameend;");
220         }
221 }
222
223 .float has_team;
224 float SetTeam(entity o, float Team)
225 {
226         entity tm;
227         if(teamplay)
228         {
229                 switch(Team)
230                 {
231                         case -1:
232                         case COLOR_TEAM1:
233                         case COLOR_TEAM2:
234                         case COLOR_TEAM3:
235                         case COLOR_TEAM4:
236                                 break;
237                         default:
238                                 if(GetTeam(Team, false) == NULL)
239                                 {
240                                         print("trying to switch to unsupported team ", ftos(Team), "\n");
241                                         Team = COLOR_SPECTATOR;
242                                 }
243                                 break;
244                 }
245         }
246         else
247         {
248                 switch(Team)
249                 {
250                         case -1:
251                         case 0:
252                                 break;
253                         default:
254                                 if(GetTeam(Team, false) == NULL)
255                                 {
256                                         print("trying to switch to unsupported team ", ftos(Team), "\n");
257                                         Team = COLOR_SPECTATOR;
258                                 }
259                                 break;
260                 }
261         }
262         if(Team == -1) // leave
263         {
264                 if(o.has_team)
265                 {
266                         //print("(DISCONNECT) leave team ", ftos(o.team), "\n");
267                         tm = GetTeam(o.team, false);
268                         tm.team_size -= 1;
269                         o.has_team = 0;
270                         return TRUE;
271                 }
272         }
273         else
274         {
275                 if not(o.has_team)
276                 {
277                         //print("(CONNECT) enter team ", ftos(o.team), "\n");
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                         //print("(CHANGE) leave team ", ftos(o.team), "\n");
287                         tm = GetTeam(o.team, false);
288                         tm.team_size -= 1;
289                         o.team = Team;
290                         //print("(CHANGE) enter team ", ftos(o.team), "\n");
291                         tm = GetTeam(Team, true);
292                         tm.team_size += 1;
293                         return TRUE;
294                 }
295         }
296         return FALSE;
297 }
298
299 void Playerchecker_Think()
300 {
301         float i;
302         entity e;
303         for(i = 0; i < maxclients; ++i)
304         {
305                 e = playerslots[i];
306                 if(GetPlayerName(i) == "")
307                 {
308                         if(e.sort_prev)
309                         {
310                                 //print("playerchecker: KILL KILL KILL\n");
311                                 // player disconnected
312                                 SetTeam(e, -1);
313                                 RemovePlayer(e);
314                                 e.sort_prev = world;
315                                 //e.gotscores = 0;
316                         }
317                 }
318                 else
319                 {
320                         if not(e.sort_prev)
321                         {
322                                 //print("playerchecker: SPAWN SPAWN SPAWN\n");
323                                 // player connected
324                                 if not(e)
325                                         playerslots[i] = e = spawn();
326                                 e.sv_entnum = i;
327                                 e.ping = 0;
328                                 e.ping_packetloss = 0;
329                                 e.ping_movementloss = 0;
330                                 //e.gotscores = 0; // we might already have the scores...
331                                 SetTeam(e, GetPlayerColor(i)); // will not hurt; later updates come with HUD_UpdatePlayerTeams
332                                 RegisterPlayer(e);
333                                 HUD_UpdatePlayerPos(e);
334                         }
335                 }
336         }
337         self.nextthink = time + 0.2;
338 }
339
340 void Porto_Init();
341 void TrueAim_Init();
342 void PostInit(void)
343 {
344         print(strcat("PostInit\n    maxclients = ", ftos(maxclients), "\n"));
345         localcmd(strcat("\nhud_columns_set ", cvar_string("hud_columns"), ";\n"));
346
347         entity playerchecker;
348         playerchecker = spawn();
349         playerchecker.think = Playerchecker_Think;
350         playerchecker.nextthink = time + 0.2;
351
352         Porto_Init();
353         TrueAim_Init();
354
355         postinit = true;
356 }
357
358 // CSQC_ConsoleCommand : Used to parse commands in the console that have been registered with the "registercmd" function
359 // Return value should be 1 if CSQC handled the command, otherwise return 0 to have the engine handle it.
360 float button_zoom;
361 void Cmd_HUD_SetFields(float);
362 void Cmd_HUD_Help(float);
363 float CSQC_ConsoleCommand(string strMessage)
364 {
365         float argc;
366         // Tokenize String
367         //argc = tokenize(strMessage);
368         argc = tokenize_console(strMessage);
369
370         // Acquire Command
371         local string strCmd;
372         strCmd = argv(0);
373
374         if(strCmd == "hud_configure") { // config hud
375                 cvar_set("_hud_configure", ftos(!cvar("_hud_configure")));
376                 return true;
377         } else if(strCmd == "hud_save") { // save hud config
378                 if(argv(1) == "" || argv(2)) {
379                         print("Usage:\n");
380                         print("hud_save configname   (saves to hud_skinname_configname.cfg)\n");
381                 }
382                 else
383                         HUD_Panel_ExportCfg(argv(1));
384                 return true;
385         } else if(strCmd == "+button4") { // zoom
386                 // return false, because the message shall be sent to the server anyway (for demos/speccing)
387                 if(ignore_plus_zoom)
388                 {
389                         --ignore_plus_zoom;
390                         return false;
391                 }
392                 button_zoom = 1;
393                 return true;
394         } else if(strCmd == "-button4") { // zoom
395                 if(ignore_minus_zoom)
396                 {
397                         --ignore_minus_zoom;
398                         return false;
399                 }
400                 button_zoom = 0;
401                 return true;
402         } else if(strCmd == "+button3") { // secondary
403                 button_attack2 = 1;
404                 return false;
405         } else if(strCmd == "-button3") { // secondary
406                 button_attack2 = 0;
407                 return false;
408         } else if(strCmd == "+showscores") {
409                 scoreboard_showscores = true;
410                 return true;
411         } else if(strCmd == "-showscores") {
412                 scoreboard_showscores = false;
413                 return true;
414         } else if(strCmd == "+showaccuracy") {
415                 scoreboard_showaccuracy = true;
416                 return true;
417         } else if(strCmd == "-showaccuracy") {
418                 scoreboard_showaccuracy = false;
419                 return true;
420         }
421
422         if(camera_active)
423         if(strCmd == "+forward" || strCmd == "-back") {
424                 ++camera_direction_x;
425                 return true;
426         } else if(strCmd == "-forward" || strCmd == "+back") {
427                 --camera_direction_x;
428                 return true;
429         } else if(strCmd == "+moveright" || strCmd == "-moveleft") {
430                 --camera_direction_y;
431                 return true;
432         } else if(strCmd == "-moveright" || strCmd == "+moveleft") {
433                 ++camera_direction_y;
434                 return true;
435         } else if(strCmd == "+moveup" || strCmd == "-movedown") {
436                 ++camera_direction_z;
437                 return true;
438         } else if(strCmd == "-moveup" || strCmd == "+movedown") {
439                 --camera_direction_z;
440                 return true;
441         } else if(strCmd == "+roll_right" || strCmd == "-roll_left") {
442                 ++camera_roll;
443                 return true;
444         } else if(strCmd == "+roll_left" || strCmd == "-roll_right") {
445                 --camera_roll;
446                 return true;
447         }
448
449         return false;
450 }
451
452 .vector view_ofs;
453 entity debug_shotorg;
454 void ShotOrg_Draw()
455 {
456         self.origin = view_origin + view_forward * self.view_ofs_x + view_right * self.view_ofs_y + view_up * self.view_ofs_z;
457         self.angles = view_angles;
458         self.angles_x = -self.angles_x;
459         if not(self.cnt)
460                 R_AddEntity(self);
461 }
462 void ShotOrg_Draw2D()
463 {
464         vector coord2d_topleft, coord2d_topright, coord2d;
465         string s;
466         vector fs;
467
468         s = vtos(self.view_ofs);
469         s = substring(s, 1, strlen(s) - 2);
470         if(tokenize_console(s) == 3)
471                 s = strcat(argv(0), " ", argv(1), " ", argv(2));
472
473         coord2d_topleft = project_3d_to_2d(self.origin + view_up * 4 - view_right * 4);
474         coord2d_topright = project_3d_to_2d(self.origin + view_up * 4 + view_right * 4);
475
476         fs = '1 1 0' * ((coord2d_topright_x - coord2d_topleft_x) / stringwidth(s, FALSE, '8 8 0'));
477
478         coord2d = coord2d_topleft;
479         if(fs_x < 8)
480         {
481                 coord2d_x += (coord2d_topright_x - coord2d_topleft_x) * (1 - 8 / fs_x) * 0.5;
482                 fs = '8 8 0';
483         }
484         coord2d_y -= fs_y;
485         coord2d_z = 0;
486         drawstring(coord2d, s, fs, '1 1 1', 1, 0);
487 }
488
489 void ShotOrg_Spawn()
490 {
491         debug_shotorg = spawn();
492         debug_shotorg.draw = ShotOrg_Draw;
493         debug_shotorg.draw2d = ShotOrg_Draw2D;
494         debug_shotorg.renderflags = RF_VIEWMODEL;
495         debug_shotorg.effects = EF_FULLBRIGHT;
496         precache_model("models/shotorg_adjuster.md3");
497         setmodel(debug_shotorg, "models/shotorg_adjuster.md3");
498         debug_shotorg.scale = 2;
499         debug_shotorg.view_ofs = '25 8 -8';
500 }
501
502 void DrawDebugModel()
503 {
504         if(time - floor(time) > 0.5)
505         {
506                 PolyDrawModel(self);
507         }
508         else
509         {
510                 self.renderflags = 0;
511                 R_AddEntity(self);
512         }
513 }
514
515 void GameCommand(string msg)
516 {
517         string s;
518         float argc;
519         entity e;
520         argc = tokenize_console(msg);
521
522         if(argv(0) == "help" || argc == 0)
523         {
524                 print("Usage: cl_cmd COMMAND..., where possible commands are:\n");
525                 print("  settemp cvar value\n");
526                 print("  hud_columns_set ...\n");
527                 print("  hud_columns_help\n");
528                 GameCommand_Generic("help");
529                 return;
530         }
531
532         if(GameCommand_Generic(msg))
533                 return;
534
535         string cmd;
536         cmd = argv(0);
537         if(cmd == "mv_download") {
538                 Cmd_MapVote_MapDownload(argc);
539         }
540         else if(cmd == "settemp") {
541                 cvar_clientsettemp(argv(1), argv(2));
542         }
543         else if(cmd == "hud_columns_set") {
544                 Cmd_HUD_SetFields(argc);
545         }
546         else if(cmd == "hud_columns_help") {
547                 Cmd_HUD_Help(argc);
548         }
549 #ifdef BLURTEST
550         else if(cmd == "blurtest") {
551                 blurtest_time0 = time;
552                 blurtest_time1 = time + stof(argv(1));
553                 blurtest_radius = stof(argv(2));
554                 blurtest_power = stof(argv(3));
555         }
556 #endif
557         else if(cmd == "shotorg_move") {
558                 if(!debug_shotorg)
559                         ShotOrg_Spawn();
560                 else
561                         debug_shotorg.view_ofs = debug_shotorg.view_ofs + stov(argv(1));
562                 localcmd("sv_cmd debug_shotorg \"", vtos(debug_shotorg.view_ofs), "\"\n");
563         }
564         else if(cmd == "shotorg_movez") {
565                 if(!debug_shotorg)
566                         ShotOrg_Spawn();
567                 else
568                         debug_shotorg.view_ofs = debug_shotorg.view_ofs + stof(argv(1)) * (debug_shotorg.view_ofs * (1 / debug_shotorg.view_ofs_x)); // closer/farther, same xy pos
569                 localcmd("sv_cmd debug_shotorg \"", vtos(debug_shotorg.view_ofs), "\"\n");
570         }
571         else if(cmd == "shotorg_set") {
572                 if(!debug_shotorg)
573                         ShotOrg_Spawn();
574                 else
575                         debug_shotorg.view_ofs = stov(argv(1));
576                 localcmd("sv_cmd debug_shotorg \"", vtos(debug_shotorg.view_ofs), "\"\n");
577         }
578         else if(cmd == "shotorg_setz") {
579                 if(!debug_shotorg)
580                         ShotOrg_Spawn();
581                 else
582                         debug_shotorg.view_ofs = debug_shotorg.view_ofs * (stof(argv(1)) / debug_shotorg.view_ofs_x); // closer/farther, same xy pos
583                 localcmd("sv_cmd debug_shotorg \"", vtos(debug_shotorg.view_ofs), "\"\n");
584         }
585         else if(cmd == "shotorg_toggle_hide") {
586                 if(debug_shotorg)
587                 {
588                         debug_shotorg.cnt = !debug_shotorg.cnt;
589                 }
590         }
591         else if(cmd == "shotorg_end") {
592                 if(debug_shotorg)
593                 {
594                         print(vtos(debug_shotorg.view_ofs), "\n");
595                         remove(debug_shotorg);
596                         debug_shotorg = world;
597                 }
598                 localcmd("sv_cmd debug_shotorg\n");
599         }
600         else if(cmd == "sendcvar") {
601                 // W_FixWeaponOrder will trash argv, so save what we need.
602                 string thiscvar;
603                 thiscvar = strzone(argv(1));
604                 s = cvar_string(thiscvar);
605                 if(thiscvar == "cl_weaponpriority")
606                         s = W_FixWeaponOrder(W_NumberWeaponOrder(s), 1);
607                 else if(substring(thiscvar, 0, 17) == "cl_weaponpriority" && strlen(thiscvar) == 18)
608                         s = W_FixWeaponOrder(W_NumberWeaponOrder(s), 0);
609                 localcmd("cmd sentcvar ", thiscvar, " \"", s, "\"\n");
610                 strunzone(thiscvar);
611         }
612         else if(cmd == "spawn") {
613                 s = argv(1);
614                 e = spawn();
615                 precache_model(s);
616                 setmodel(e, s);
617                 setorigin(e, view_origin);
618                 e.angles = view_angles;
619                 e.draw = DrawDebugModel;
620                 e.classname = "debugmodel";
621         }
622         else
623         {
624                 print("Invalid command. For a list of supported commands, try cl_cmd help.\n");
625         }
626
627         return;
628 }
629
630 // CSQC_InputEvent : Used to perform actions based on any key pressed, key released and mouse on the client.
631 // Return value should be 1 if CSQC handled the input, otherwise return 0 to have the input passed to the engine.
632 // All keys are in ascii.
633 // bInputType = 0 is key pressed, 1 is key released, 2 is mouse input.
634 // In the case of keyboard input, nPrimary is the ascii code, and nSecondary is 0.
635 // In the case of mouse input, nPrimary is xdelta, nSecondary is ydelta.
636 float hudconf_active;
637 float CSQC_InputEvent(float bInputType, float nPrimary, float nSecondary)
638 {
639         local float bSkipKey;
640         bSkipKey = false;
641
642         if(hudconf_active)
643                 if (HUD_Panel_InputEvent(bInputType, nPrimary, nSecondary))
644                         return true;
645
646         if (MapVote_InputEvent(bInputType, nPrimary, nSecondary))
647                 return true;
648
649         if(menu_visible)
650                 if(menu_action(bInputType, nPrimary, nSecondary))
651                         return TRUE;
652         return bSkipKey;
653 }
654
655 // END REQUIRED CSQC FUNCTIONS
656 // --------------------------------------------------------------------------
657
658 // --------------------------------------------------------------------------
659 // BEGIN OPTIONAL CSQC FUNCTIONS
660 void Ent_ReadEntCS()
661 {
662         InterpolateOrigin_Undo();
663
664         self.classname = "entcs_receiver";
665         self.sv_entnum = ReadByte() - 1;
666         self.origin_x = ReadShort();
667         self.origin_y = ReadShort();
668         self.origin_z = ReadShort();
669         self.angles_y = ReadByte() * 360.0 / 256;
670         self.origin_z = self.angles_x = self.angles_z = 0;
671
672         InterpolateOrigin_Note();
673 }
674
675 void Ent_Remove();
676
677 void Ent_RemovePlayerScore()
678 {
679         float i;
680
681         if(self.owner)
682         {
683                 SetTeam(self.owner, -1);
684                 self.owner.gotscores = 0;
685                 for(i = 0; i < MAX_SCORE; ++i)
686                         self.owner.(scores[i]) = 0; // clear all scores
687         }
688 }
689
690 void Ent_ReadPlayerScore()
691 {
692         float i, n;
693         float isNew;
694         entity o;
695
696         // damnit -.- don't want to go change every single .sv_entnum in hud.qc AGAIN
697         // (no I've never heard of M-x replace-string, sed, or anything like that)
698         isNew = !self.owner; // workaround for DP bug
699         n = ReadByte()-1;
700
701 #ifdef DP_CSQC_ENTITY_REMOVE_IS_B0RKED
702         if(!isNew && n != self.sv_entnum)
703         {
704                 print("A CSQC entity changed its owner!\n");
705                 isNew = true;
706                 Ent_Remove();
707                 self.enttype = ENT_CLIENT_SCORES;
708         }
709 #endif
710
711         self.sv_entnum = n;
712
713         if not(playerslots[self.sv_entnum])
714                 playerslots[self.sv_entnum] = spawn();
715         o = self.owner = playerslots[self.sv_entnum];
716         o.sv_entnum = self.sv_entnum;
717         o.gotscores = 1;
718
719         //if not(o.sort_prev)
720         //      RegisterPlayer(o);
721         //playerchecker will do this for us later, if it has not already done so
722
723         float sf, lf;
724 #if MAX_SCORE <= 8
725         sf = ReadByte();
726         lf = ReadByte();
727 #else
728         sf = ReadShort();
729         lf = ReadShort();
730 #endif
731         float p;
732         for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
733                 if(sf & p)
734                 {
735                         if(lf & p)
736                                 o.(scores[i]) = ReadInt24_t();
737                         else
738                                 o.(scores[i]) = ReadChar();
739                 }
740
741         if(o.sort_prev)
742                 HUD_UpdatePlayerPos(o); // if not registered, we cannot do this yet!
743
744         self.entremove = Ent_RemovePlayerScore;
745 }
746
747 void Ent_ReadTeamScore()
748 {
749         float i;
750         entity o;
751
752         self.team = ReadByte();
753         o = self.owner = GetTeam(self.team, true); // these team numbers can always be trusted
754
755         float sf, lf;
756 #if MAX_TEAMSCORE <= 8
757         sf = ReadByte();
758         lf = ReadByte();
759 #else
760         sf = ReadShort();
761         lf = ReadShort();
762 #endif
763         float p;
764         for(i = 0, p = 1; i < MAX_TEAMSCORE; ++i, p *= 2)
765                 if(sf & p)
766                 {
767                         if(lf & p)
768                                 o.(teamscores[i]) = ReadInt24_t();
769                         else
770                                 o.(teamscores[i]) = ReadChar();
771                 }
772
773         HUD_UpdateTeamPos(o);
774 }
775
776 void Net_Reset()
777 {
778 }
779
780 void Ent_ClientData()
781 {
782         float f;
783         float newspectatee_status;
784
785         f = ReadByte();
786
787         scoreboard_showscores_force = (f & 1);
788
789         if(f & 2)
790         {
791                 newspectatee_status = ReadByte();
792                 if(newspectatee_status == player_localentnum)
793                         newspectatee_status = -1; // observing
794         }
795         else
796                 newspectatee_status = 0;
797
798         spectatorbutton_zoom = (f & 4);
799
800         if(f & 8)
801         {
802                 angles_held_status = 1;
803                 angles_held_x = ReadAngle();
804                 angles_held_y = ReadAngle();
805                 angles_held_z = 0;
806         }
807         else
808                 angles_held_status = 0;
809
810         if(newspectatee_status != spectatee_status)
811         {
812                 float i;
813                 // clear the weapon accuracy stats
814                 for(i = WEP_FIRST; i <= WEP_LAST; ++i) {
815                         weapon_hits[i] = 0;
816                         weapon_fired[i] = 0;
817                 }
818
819                 // clear race stuff
820                 race_laptime = 0;
821                 race_checkpointtime = 0;
822         }
823         spectatee_status = newspectatee_status;
824 }
825
826 void Ent_Nagger()
827 {
828         float nags, i, j, b, f;
829
830         nags = ReadByte();
831
832         if(nags & 128)
833         {
834                 if(vote_called_vote)
835                         strunzone(vote_called_vote);
836                 vote_called_vote = strzone(ColorTranslateRGB(ReadString()));
837         }
838
839         if(nags & 1)
840         {
841                 for(j = 0; j < maxclients; ++j)
842                         if(playerslots[j])
843                                 playerslots[j].ready = 1;
844                 for(i = 1; i <= maxclients; i += 8)
845                 {
846                         f = ReadByte();
847                         for(j = i-1, b = 1; b < 256; b *= 2, ++j)
848                                 if not(f & b)
849                                         if(playerslots[j])
850                                                 playerslots[j].ready = 0;
851                 }
852         }
853
854         ready_waiting = (nags & 1);
855         ready_waiting_for_me = (nags & 2);
856         vote_waiting = (nags & 4);
857         vote_waiting_for_me = (nags & 8);
858         warmup_stage = (nags & 16);
859 }
860
861 void Ent_RandomSeed()
862 {
863         float s;
864         prandom_debug();
865         s = ReadShort();
866         psrandom(s);
867 }
868
869 // CSQC_Ent_Update : Called every frame that the server has indicated an update to the SSQC / CSQC entity has occured.
870 // The only parameter reflects if the entity is "new" to the client, meaning it just came into the client's PVS.
871 void Ent_RadarLink();
872 void Ent_Init();
873 void Ent_ScoresInfo();
874 void(float bIsNewEntity) CSQC_Ent_Update =
875 {
876         float t;
877         float savetime;
878         t = ReadByte();
879
880         // set up the "time" global for received entities to be correct for interpolation purposes
881         savetime = time;
882         if(servertime)
883         {
884                 time = servertime;
885         }
886         else
887         {
888                 serverprevtime = time;
889                 serverdeltatime = getstatf(STAT_MOVEVARS_TICRATE) * getstatf(STAT_MOVEVARS_TIMESCALE);
890                 time = serverprevtime + serverdeltatime;
891         }
892
893 #ifdef DP_CSQC_ENTITY_REMOVE_IS_B0RKED
894         if(self.enttype)
895                 if(t != self.enttype)
896                 {
897                         print("A CSQC entity changed its type!\n");
898                         Ent_Remove();
899                         bIsNewEntity = 1;
900                 }
901 #endif
902         self.enttype = t;
903         switch(t)
904         {
905                 case ENT_CLIENT_ENTCS: Ent_ReadEntCS(); break;
906                 case ENT_CLIENT_SCORES: Ent_ReadPlayerScore(); break;
907                 case ENT_CLIENT_TEAMSCORES: Ent_ReadTeamScore(); break;
908                 case ENT_CLIENT_POINTPARTICLES: Ent_PointParticles(); break;
909                 case ENT_CLIENT_RAINSNOW: Ent_RainOrSnow(); break;
910                 case ENT_CLIENT_LASER: Ent_Laser(); break;
911                 case ENT_CLIENT_NAGGER: Ent_Nagger(); break;
912                 case ENT_CLIENT_WAYPOINT: Ent_WaypointSprite(); break;
913                 case ENT_CLIENT_RADARLINK: Ent_RadarLink(); break;
914                 case ENT_CLIENT_PROJECTILE: Ent_Projectile(); break;
915                 case ENT_CLIENT_GIBSPLASH: Ent_GibSplash(bIsNewEntity); break;
916                 case ENT_CLIENT_DAMAGEINFO: Ent_DamageInfo(bIsNewEntity); break;
917                 case ENT_CLIENT_CASING: Ent_Casing(bIsNewEntity); break;
918                 case ENT_CLIENT_INIT: Ent_Init(); break;
919                 case ENT_CLIENT_SCORES_INFO: Ent_ScoresInfo(); break;
920                 case ENT_CLIENT_MAPVOTE: Ent_MapVote(); break;
921                 case ENT_CLIENT_CLIENTDATA: Ent_ClientData(); break;
922                 case ENT_CLIENT_RANDOMSEED: Ent_RandomSeed(); break;
923                 case ENT_CLIENT_WALL: Ent_Wall(); break;
924                 case ENT_CLIENT_MODELEFFECT: Ent_ModelEffect(bIsNewEntity); break;
925                 case ENT_CLIENT_TUBANOTE: Ent_TubaNote(bIsNewEntity); break;
926                 case ENT_CLIENT_WARPZONE: WarpZone_Read(bIsNewEntity); break;
927                 case ENT_CLIENT_WARPZONE_CAMERA: WarpZone_Camera_Read(bIsNewEntity); break;
928                 case ENT_CLIENT_TRIGGER_MUSIC: Ent_ReadTriggerMusic(); break;
929                 default:
930                         error(strcat("unknown entity type in CSQC_Ent_Update: ", ftos(self.enttype), "\n"));
931                         break;
932         }
933
934         time = savetime;
935 };
936 // Destructor, but does NOT deallocate the entity by calling remove(). Also
937 // used when an entity changes its type. For an entity that someone interacts
938 // with others, make sure it can no longer do so.
939 void Ent_Remove()
940 {
941         if(self.entremove)
942                 self.entremove();
943
944         self.enttype = 0;
945         self.classname = "";
946         self.draw = menu_sub_null;
947         self.entremove = menu_sub_null;
948         // TODO possibly set more stuff to defaults
949 }
950 // CSQC_Ent_Remove : Called when the server requests a SSQC / CSQC entity to be removed.  Essentially call remove(self) as well.
951 void CSQC_Ent_Remove()
952 {
953         if(self.enttype)
954                 Ent_Remove();
955         remove(self);
956 }
957
958 void Gamemode_Init()
959 {
960         if(gametype == GAME_ONSLAUGHT) {
961                 print(strcat("Using ", minimapname, " as minimap.\n"));
962                 precache_pic("gfx/ons-cp-neutral.tga");
963                 precache_pic("gfx/ons-cp-red.tga");
964                 precache_pic("gfx/ons-cp-blue.tga");
965                 precache_pic("gfx/ons-frame.tga");
966                 precache_pic("gfx/ons-frame-team.tga");
967         }
968
969         if not(isdemo())
970         {
971                 localcmd("\n_cl_hook_gamestart ", GametypeNameFromType(gametype), ";");
972                 calledhooks |= HOOK_START;
973         }
974 }
975 // 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.
976 void CSQC_Parse_StuffCmd(string strMessage)
977 {
978         localcmd(strMessage);
979 }
980 // 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.
981 void CSQC_Parse_Print(string strMessage)
982 {
983         print(ColorTranslateRGB(strMessage));
984 }
985
986 // CSQC_Parse_CenterPrint : Provides the centerprint string in the first parameter that the server provided.
987 void CSQC_Parse_CenterPrint(string strMessage)
988 {
989         centerprint(strMessage);
990 }
991
992 string notranslate_fogcmd1 = "\nfog ";
993 string notranslate_fogcmd2 = "\nr_fog_exp2 0\nr_drawfog 1\n";
994 void Fog_Force()
995 {
996         // TODO somehow thwart prvm_globalset client ...
997
998         if(forcefog != "")
999                 localcmd(strcat(notranslate_fogcmd1, forcefog, notranslate_fogcmd2));
1000 }
1001
1002 void Gamemode_Init();
1003 void Ent_ScoresInfo()
1004 {
1005         float i;
1006         self.classname = "ent_client_scores_info";
1007         gametype = ReadByte();
1008         for(i = 0; i < MAX_SCORE; ++i)
1009         {
1010                 scores_label[i] = strzone(ReadString());
1011                 scores_flags[i] = ReadByte();
1012         }
1013         for(i = 0; i < MAX_TEAMSCORE; ++i)
1014         {
1015                 teamscores_label[i] = strzone(ReadString());
1016                 teamscores_flags[i] = ReadByte();
1017         }
1018         HUD_InitScores();
1019         Gamemode_Init();
1020 }
1021
1022 void Ent_Init()
1023 {
1024         self.classname = "ent_client_init";
1025
1026         nb_pb_period = ReadByte() / 32; //Accuracy of 1/32th
1027
1028         hook_shotorigin_x = ReadCoord();
1029         hook_shotorigin_y = ReadCoord();
1030         hook_shotorigin_z = ReadCoord();
1031
1032         if(forcefog)
1033                 strunzone(forcefog);
1034         forcefog = strzone(ReadString());
1035
1036         armorblockpercent = ReadByte() / 255.0;
1037
1038         g_weaponswitchdelay = ReadByte() / 255.0;
1039
1040         g_balance_grenadelauncher_secondary_bouncefactor = ReadCoord();
1041         g_balance_grenadelauncher_secondary_bouncestop = ReadCoord();
1042
1043         if(!postinit)
1044                 PostInit();
1045 }
1046
1047 void Net_ReadRace()
1048 {
1049         float b;
1050
1051         b = ReadByte();
1052
1053         switch(b)
1054         {
1055                 case RACE_NET_CHECKPOINT_HIT_QUALIFYING:
1056                         race_checkpoint = ReadByte();
1057                         race_time = ReadInt24_t();
1058                         race_previousbesttime = ReadInt24_t();
1059                         if(race_previousbestname)
1060                                 strunzone(race_previousbestname);
1061                         race_previousbestname = strzone(ColorTranslateRGB(ReadString()));
1062
1063                         race_checkpointtime = time;
1064
1065                         if(race_checkpoint == 0 || race_checkpoint == 254)
1066                         {
1067                                 race_penaltyaccumulator = 0;
1068                                 race_laptime = time; // valid
1069                         }
1070
1071                         break;
1072
1073                 case RACE_NET_CHECKPOINT_CLEAR:
1074                         race_laptime = 0;
1075                         race_checkpointtime = 0;
1076                         break;
1077
1078                 case RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING:
1079                         race_laptime = ReadCoord();
1080                         race_checkpointtime = -99999;
1081                         // fall through
1082                 case RACE_NET_CHECKPOINT_NEXT_QUALIFYING:
1083                         race_nextcheckpoint = ReadByte();
1084
1085                         race_nextbesttime = ReadInt24_t();
1086                         if(race_nextbestname)
1087                                 strunzone(race_nextbestname);
1088                         race_nextbestname = strzone(ColorTranslateRGB(ReadString()));
1089                         break;
1090
1091                 case RACE_NET_CHECKPOINT_HIT_RACE:
1092                         race_mycheckpoint = ReadByte();
1093                         race_mycheckpointtime = time;
1094                         race_mycheckpointdelta = ReadInt24_t();
1095                         race_mycheckpointlapsdelta = ReadByte();
1096                         if(race_mycheckpointlapsdelta >= 128)
1097                                 race_mycheckpointlapsdelta -= 256;
1098                         if(race_mycheckpointenemy)
1099                                 strunzone(race_mycheckpointenemy);
1100                         race_mycheckpointenemy = strzone(ColorTranslateRGB(ReadString()));
1101                         break;
1102
1103                 case RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT:
1104                         race_othercheckpoint = ReadByte();
1105                         race_othercheckpointtime = time;
1106                         race_othercheckpointdelta = ReadInt24_t();
1107                         race_othercheckpointlapsdelta = ReadByte();
1108                         if(race_othercheckpointlapsdelta >= 128)
1109                                 race_othercheckpointlapsdelta -= 256;
1110                         if(race_othercheckpointenemy)
1111                                 strunzone(race_othercheckpointenemy);
1112                         race_othercheckpointenemy = strzone(ColorTranslateRGB(ReadString()));
1113                         break;
1114
1115                 case RACE_NET_PENALTY_RACE:
1116                         race_penaltyeventtime = time;
1117                         race_penaltytime = ReadShort();
1118                         //race_penaltyaccumulator += race_penaltytime;
1119                         if(race_penaltyreason)
1120                                 strunzone(race_penaltyreason);
1121                         race_penaltyreason = strzone(ReadString());
1122                         break;
1123
1124                 case RACE_NET_PENALTY_QUALIFYING:
1125                         race_penaltyeventtime = time;
1126                         race_penaltytime = ReadShort();
1127                         race_penaltyaccumulator += race_penaltytime;
1128                         if(race_penaltyreason)
1129                                 strunzone(race_penaltyreason);
1130                         race_penaltyreason = strzone(ReadString());
1131                         break;
1132
1133                 case RACE_NET_SERVER_RECORD:
1134                         race_server_record = ReadInt24_t();
1135                         break;
1136                 case RACE_NET_SPEED_AWARD:
1137                         race_speedaward = ReadInt24_t();
1138                         if(race_speedaward_holder)
1139                                 strunzone(race_speedaward_holder);
1140                         race_speedaward_holder = strzone(ReadString());
1141                         break;
1142                 case RACE_NET_SPEED_AWARD_BEST:
1143                         race_speedaward_alltimebest = ReadInt24_t();
1144                         if(race_speedaward_alltimebest_holder)
1145                                 strunzone(race_speedaward_alltimebest_holder);
1146                         race_speedaward_alltimebest_holder = strzone(ReadString());
1147                         break;
1148                 case RACE_NET_SERVER_RANKINGS:
1149                         float pos, prevpos, del;
1150                         pos = ReadShort();
1151                         prevpos = ReadShort();
1152                         del = ReadShort();
1153
1154                         // move other rankings out of the way
1155                         float i;
1156                         if (prevpos) {
1157                                 for (i=prevpos-1;i>pos-1;--i) {
1158                                         grecordtime[i] = grecordtime[i-1];
1159                                         if(grecordholder[i])
1160                                                 strunzone(grecordholder[i]);
1161                                         grecordholder[i] = strzone(grecordholder[i-1]);
1162                                 }
1163                         } else if (del) { // a record has been deleted by the admin
1164                                 for (i=pos-1; i<= RANKINGS_CNT-1; ++i) {
1165                                         if (i == RANKINGS_CNT-1) { // clear out last record
1166                                                 grecordtime[i] = 0;
1167                                                 if (grecordholder[i])
1168                                                         strunzone(grecordholder[i]);
1169                                                 grecordholder[i] = string_null;
1170                                         }
1171                                         else {
1172                                                 grecordtime[i] = grecordtime[i+1];
1173                                                 if (grecordholder[i])
1174                                                         strunzone(grecordholder[i]);
1175                                                 grecordholder[i] = strzone(grecordholder[i+1]);
1176                                         }
1177                                 }
1178                         } else { // player has no ranked record yet
1179                                 for (i=RANKINGS_CNT-1;i>pos-1;--i) {
1180                                         grecordtime[i] = grecordtime[i-1];
1181                                         if(grecordholder[i])
1182                                                 strunzone(grecordholder[i]);
1183                                         grecordholder[i] = strzone(grecordholder[i-1]);
1184                                 }
1185                         }
1186
1187                         // store new ranking
1188                         if(grecordholder[pos-1] != "")
1189                                 strunzone(grecordholder[pos-1]);
1190                         grecordholder[pos-1] = strzone(ReadString());
1191                         grecordtime[pos-1] = ReadInt24_t();
1192                         if(grecordholder[pos-1] == GetPlayerName(player_localentnum -1))
1193                                 race_myrank = pos;
1194                         break;
1195                 case RACE_NET_SERVER_STATUS:
1196                         race_status = ReadShort();
1197                         if(race_status_name)
1198                                 strunzone(race_status_name);
1199                         race_status_name = strzone(ReadString());
1200         }
1201 }
1202
1203 void Net_ReadSpawn()
1204 {
1205         zoomin_effect = 1;
1206         current_viewzoom = 0.6;
1207 }
1208
1209 void Net_TeamNagger()
1210 {
1211         teamnagger = 1;
1212 }
1213
1214 void Net_ReadPingPLReport()
1215 {
1216         float e, pi, pl, ml;
1217         e = ReadByte();
1218         pi = ReadShort();
1219         pl = ReadByte();
1220         ml = ReadByte();
1221         if not(playerslots[e])
1222                 return;
1223         playerslots[e].ping = pi;
1224         playerslots[e].ping_packetloss = pl / 255.0;
1225         playerslots[e].ping_movementloss = ml / 255.0;
1226 }
1227
1228 void Net_VoteDialog(float highlight) {
1229         if(highlight) {
1230                 vote_highlighted = ReadShort();
1231                 return;
1232         }
1233
1234         vote_yescount = ReadShort();
1235         vote_nocount = ReadShort();
1236         vote_needed = ReadShort();
1237         vote_active = 1;
1238 }
1239
1240 void Net_VoteDialogReset() {
1241         vote_active = 0;
1242 }
1243
1244 // CSQC_Parse_TempEntity : Handles all temporary entity network data in the CSQC layer.
1245 // You must ALWAYS first acquire the temporary ID, which is sent as a byte.
1246 // Return value should be 1 if CSQC handled the temporary entity, otherwise return 0 to have the engine process the event.
1247 float CSQC_Parse_TempEntity()
1248 {
1249         local float bHandled;
1250                 bHandled  = true;
1251         // Acquire TE ID
1252         local float nTEID;
1253                 nTEID = ReadByte();
1254
1255                 // NOTE: Could just do return instead of break...
1256         switch(nTEID)
1257         {
1258                 case TE_CSQC_TARGET_MUSIC:
1259                         Net_TargetMusic();
1260                         bHandled = true;
1261                         break;
1262                 case TE_CSQC_PICTURE:
1263                         Net_MapVote_Picture();
1264                         bHandled = true;
1265                         break;
1266                 case TE_CSQC_RACE:
1267                         Net_ReadRace();
1268                         bHandled = true;
1269                         break;
1270                 case 13: // TE_BEAM
1271                         Net_GrapplingHook();
1272                         bHandled = true;
1273                         break;
1274                 case TE_CSQC_SPAWN:
1275                         Net_ReadSpawn();
1276                         bHandled = true;
1277                         break;
1278                 case TE_CSQC_ZCURVEPARTICLES:
1279                         Net_ReadZCurveParticles();
1280                         bHandled = true;
1281                         break;
1282                 case TE_CSQC_NEXGUNBEAMPARTICLE:
1283                         Net_ReadNexgunBeamParticle();
1284                         bHandled = true;
1285                         break;
1286                 case TE_CSQC_TEAMNAGGER:
1287                         Net_TeamNagger();
1288                         bHandled = true;
1289                         break;
1290                 case TE_CSQC_VOTE:
1291                         Net_VoteDialog(ReadByte());
1292                         bHandled = true;
1293                         break;
1294                 case TE_CSQC_VOTERESET:
1295                         Net_VoteDialogReset();
1296                         bHandled = true;
1297                         break;
1298                 case TE_CSQC_LIGHTNINGARC:
1299                         Net_ReadLightningarc();
1300                         bHandled = true;
1301                         break;
1302                 case TE_CSQC_PINGPLREPORT:
1303                         Net_ReadPingPLReport();
1304                         bHandled = true;
1305                         break;
1306                 case TE_CSQC_ANNOUNCE:
1307                         announce_snd = strzone(ReadString());
1308                         bHandled = true;
1309                         break;
1310                 default:
1311                         // No special logic for this temporary entity; return 0 so the engine can handle it
1312                         bHandled = false;
1313                         break;
1314         }
1315
1316         return bHandled;
1317 }
1318
1319 string getcommandkey(string text, string command)
1320 {
1321         string keys;
1322         float n, j, k, l;
1323
1324         if (!hud_showbinds)
1325                 return text;
1326
1327         keys = db_get(binddb, command);
1328         if (!keys)
1329         {
1330                 n = tokenize(findkeysforcommand(command)); // uses '...' strings
1331                 for(j = 0; j < n; ++j)
1332                 {
1333                         k = stof(argv(j));
1334                         if(k != -1)
1335                         {
1336                                 if ("" == keys)
1337                                         keys = keynumtostring(k);
1338                                 else
1339                                         keys = strcat(keys, ", ", keynumtostring(k));
1340
1341                                 ++l;
1342                                 if (hud_showbinds_limit > 0 && hud_showbinds_limit >= l) break;
1343                         }
1344
1345                 }
1346                 db_put(binddb, command, keys);
1347         }
1348
1349         if ("" == keys) {
1350                 if (hud_showbinds > 1)
1351                         return strcat(text, " (not bound)");
1352                 else
1353                         return text;
1354         }
1355         else if (hud_showbinds > 1)
1356                 return strcat(text, " (", keys, ")");
1357         else
1358                 return keys;
1359 }