]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/Main.qc
Merge remote branch 'origin/diabolik/newpanelhud' into fruitiex/newpanelhud
[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("scoreboard_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("\nscoreboard_columns_set ", cvar_string("scoreboard_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("  scoreboard_columns_set ...\n");
527                 print("  scoreboard_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 == "scoreboard_columns_set") {
544                 Cmd_HUD_SetFields(argc);
545         }
546         else if(cmd == "scoreboard_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 CSQC_InputEvent(float bInputType, float nPrimary, float nSecondary)
637 {
638         local float bSkipKey;
639         bSkipKey = false;
640
641         if (HUD_Panel_InputEvent(bInputType, nPrimary, nSecondary))
642                 return true;
643
644         if (MapVote_InputEvent(bInputType, nPrimary, nSecondary))
645                 return true;
646
647         if(menu_visible)
648                 if(menu_action(bInputType, nPrimary, nSecondary))
649                         return TRUE;
650         return bSkipKey;
651 }
652
653 // END REQUIRED CSQC FUNCTIONS
654 // --------------------------------------------------------------------------
655
656 // --------------------------------------------------------------------------
657 // BEGIN OPTIONAL CSQC FUNCTIONS
658 void Ent_ReadEntCS()
659 {
660         InterpolateOrigin_Undo();
661
662         self.classname = "entcs_receiver";
663         self.sv_entnum = ReadByte() - 1;
664         self.origin_x = ReadShort();
665         self.origin_y = ReadShort();
666         self.origin_z = ReadShort();
667         self.angles_y = ReadByte() * 360.0 / 256;
668         self.origin_z = self.angles_x = self.angles_z = 0;
669
670         InterpolateOrigin_Note();
671 }
672
673 void Ent_Remove();
674
675 void Ent_RemovePlayerScore()
676 {
677         float i;
678
679         if(self.owner)
680         {
681                 SetTeam(self.owner, -1);
682                 self.owner.gotscores = 0;
683                 for(i = 0; i < MAX_SCORE; ++i)
684                         self.owner.(scores[i]) = 0; // clear all scores
685         }
686 }
687
688 void Ent_ReadPlayerScore()
689 {
690         float i, n;
691         float isNew;
692         entity o;
693
694         // damnit -.- don't want to go change every single .sv_entnum in hud.qc AGAIN
695         // (no I've never heard of M-x replace-string, sed, or anything like that)
696         isNew = !self.owner; // workaround for DP bug
697         n = ReadByte()-1;
698
699 #ifdef DP_CSQC_ENTITY_REMOVE_IS_B0RKED
700         if(!isNew && n != self.sv_entnum)
701         {
702                 print("A CSQC entity changed its owner!\n");
703                 isNew = true;
704                 Ent_Remove();
705                 self.enttype = ENT_CLIENT_SCORES;
706         }
707 #endif
708
709         self.sv_entnum = n;
710
711         if not(playerslots[self.sv_entnum])
712                 playerslots[self.sv_entnum] = spawn();
713         o = self.owner = playerslots[self.sv_entnum];
714         o.sv_entnum = self.sv_entnum;
715         o.gotscores = 1;
716
717         //if not(o.sort_prev)
718         //      RegisterPlayer(o);
719         //playerchecker will do this for us later, if it has not already done so
720
721         float sf, lf;
722 #if MAX_SCORE <= 8
723         sf = ReadByte();
724         lf = ReadByte();
725 #else
726         sf = ReadShort();
727         lf = ReadShort();
728 #endif
729         float p;
730         for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
731                 if(sf & p)
732                 {
733                         if(lf & p)
734                                 o.(scores[i]) = ReadInt24_t();
735                         else
736                                 o.(scores[i]) = ReadChar();
737                 }
738
739         if(o.sort_prev)
740                 HUD_UpdatePlayerPos(o); // if not registered, we cannot do this yet!
741
742         self.entremove = Ent_RemovePlayerScore;
743 }
744
745 void Ent_ReadTeamScore()
746 {
747         float i;
748         entity o;
749
750         self.team = ReadByte();
751         o = self.owner = GetTeam(self.team, true); // these team numbers can always be trusted
752
753         float sf, lf;
754 #if MAX_TEAMSCORE <= 8
755         sf = ReadByte();
756         lf = ReadByte();
757 #else
758         sf = ReadShort();
759         lf = ReadShort();
760 #endif
761         float p;
762         for(i = 0, p = 1; i < MAX_TEAMSCORE; ++i, p *= 2)
763                 if(sf & p)
764                 {
765                         if(lf & p)
766                                 o.(teamscores[i]) = ReadInt24_t();
767                         else
768                                 o.(teamscores[i]) = ReadChar();
769                 }
770
771         HUD_UpdateTeamPos(o);
772 }
773
774 void Net_Reset()
775 {
776 }
777
778 void Ent_ClientData()
779 {
780         float f;
781         float newspectatee_status;
782
783         f = ReadByte();
784
785         scoreboard_showscores_force = (f & 1);
786
787         if(f & 2)
788         {
789                 newspectatee_status = ReadByte();
790                 if(newspectatee_status == player_localentnum)
791                         newspectatee_status = -1; // observing
792         }
793         else
794                 newspectatee_status = 0;
795
796         spectatorbutton_zoom = (f & 4);
797
798         if(f & 8)
799         {
800                 angles_held_status = 1;
801                 angles_held_x = ReadAngle();
802                 angles_held_y = ReadAngle();
803                 angles_held_z = 0;
804         }
805         else
806                 angles_held_status = 0;
807
808         if(newspectatee_status != spectatee_status)
809         {
810                 float i;
811                 // clear the weapon accuracy stats
812                 for(i = WEP_FIRST; i <= WEP_LAST; ++i) {
813                         weapon_hits[i] = 0;
814                         weapon_fired[i] = 0;
815                 }
816
817                 // clear race stuff
818                 race_laptime = 0;
819                 race_checkpointtime = 0;
820         }
821         spectatee_status = newspectatee_status;
822 }
823
824 void Ent_Nagger()
825 {
826         float nags, i, j, b, f;
827
828         nags = ReadByte();
829
830         if(nags & 128)
831         {
832                 if(vote_called_vote)
833                         strunzone(vote_called_vote);
834                 vote_called_vote = strzone(ColorTranslateRGB(ReadString()));
835         }
836
837         if(nags & 1)
838         {
839                 for(j = 0; j < maxclients; ++j)
840                         if(playerslots[j])
841                                 playerslots[j].ready = 1;
842                 for(i = 1; i <= maxclients; i += 8)
843                 {
844                         f = ReadByte();
845                         for(j = i-1, b = 1; b < 256; b *= 2, ++j)
846                                 if not(f & b)
847                                         if(playerslots[j])
848                                                 playerslots[j].ready = 0;
849                 }
850         }
851
852         ready_waiting = (nags & 1);
853         ready_waiting_for_me = (nags & 2);
854         vote_waiting = (nags & 4);
855         vote_waiting_for_me = (nags & 8);
856         warmup_stage = (nags & 16);
857 }
858
859 void Ent_RandomSeed()
860 {
861         float s;
862         prandom_debug();
863         s = ReadShort();
864         psrandom(s);
865 }
866
867 // CSQC_Ent_Update : Called every frame that the server has indicated an update to the SSQC / CSQC entity has occured.
868 // The only parameter reflects if the entity is "new" to the client, meaning it just came into the client's PVS.
869 void Ent_RadarLink();
870 void Ent_Init();
871 void Ent_ScoresInfo();
872 void(float bIsNewEntity) CSQC_Ent_Update =
873 {
874         float t;
875         float savetime;
876         t = ReadByte();
877
878         // set up the "time" global for received entities to be correct for interpolation purposes
879         savetime = time;
880         if(servertime)
881         {
882                 time = servertime;
883         }
884         else
885         {
886                 serverprevtime = time;
887                 serverdeltatime = getstatf(STAT_MOVEVARS_TICRATE) * getstatf(STAT_MOVEVARS_TIMESCALE);
888                 time = serverprevtime + serverdeltatime;
889         }
890
891 #ifdef DP_CSQC_ENTITY_REMOVE_IS_B0RKED
892         if(self.enttype)
893                 if(t != self.enttype)
894                 {
895                         print("A CSQC entity changed its type!\n");
896                         Ent_Remove();
897                         bIsNewEntity = 1;
898                 }
899 #endif
900         self.enttype = t;
901         switch(t)
902         {
903                 case ENT_CLIENT_ENTCS: Ent_ReadEntCS(); break;
904                 case ENT_CLIENT_SCORES: Ent_ReadPlayerScore(); break;
905                 case ENT_CLIENT_TEAMSCORES: Ent_ReadTeamScore(); break;
906                 case ENT_CLIENT_POINTPARTICLES: Ent_PointParticles(); break;
907                 case ENT_CLIENT_RAINSNOW: Ent_RainOrSnow(); break;
908                 case ENT_CLIENT_LASER: Ent_Laser(); break;
909                 case ENT_CLIENT_NAGGER: Ent_Nagger(); break;
910                 case ENT_CLIENT_WAYPOINT: Ent_WaypointSprite(); break;
911                 case ENT_CLIENT_RADARLINK: Ent_RadarLink(); break;
912                 case ENT_CLIENT_PROJECTILE: Ent_Projectile(); break;
913                 case ENT_CLIENT_GIBSPLASH: Ent_GibSplash(bIsNewEntity); break;
914                 case ENT_CLIENT_DAMAGEINFO: Ent_DamageInfo(bIsNewEntity); break;
915                 case ENT_CLIENT_CASING: Ent_Casing(bIsNewEntity); break;
916                 case ENT_CLIENT_INIT: Ent_Init(); break;
917                 case ENT_CLIENT_SCORES_INFO: Ent_ScoresInfo(); break;
918                 case ENT_CLIENT_MAPVOTE: Ent_MapVote(); break;
919                 case ENT_CLIENT_CLIENTDATA: Ent_ClientData(); break;
920                 case ENT_CLIENT_RANDOMSEED: Ent_RandomSeed(); break;
921                 case ENT_CLIENT_WALL: Ent_Wall(); break;
922                 case ENT_CLIENT_MODELEFFECT: Ent_ModelEffect(bIsNewEntity); break;
923                 case ENT_CLIENT_TUBANOTE: Ent_TubaNote(bIsNewEntity); break;
924                 case ENT_CLIENT_WARPZONE: WarpZone_Read(bIsNewEntity); break;
925                 case ENT_CLIENT_WARPZONE_CAMERA: WarpZone_Camera_Read(bIsNewEntity); break;
926                 case ENT_CLIENT_TRIGGER_MUSIC: Ent_ReadTriggerMusic(); break;
927                 default:
928                         error(strcat("unknown entity type in CSQC_Ent_Update: ", ftos(self.enttype), "\n"));
929                         break;
930         }
931
932         time = savetime;
933 };
934 // Destructor, but does NOT deallocate the entity by calling remove(). Also
935 // used when an entity changes its type. For an entity that someone interacts
936 // with others, make sure it can no longer do so.
937 void Ent_Remove()
938 {
939         if(self.entremove)
940                 self.entremove();
941
942         self.enttype = 0;
943         self.classname = "";
944         self.draw = menu_sub_null;
945         self.entremove = menu_sub_null;
946         // TODO possibly set more stuff to defaults
947 }
948 // CSQC_Ent_Remove : Called when the server requests a SSQC / CSQC entity to be removed.  Essentially call remove(self) as well.
949 void CSQC_Ent_Remove()
950 {
951         if(self.enttype)
952                 Ent_Remove();
953         remove(self);
954 }
955
956 void Gamemode_Init()
957 {
958         if(gametype == GAME_ONSLAUGHT) {
959                 print(strcat("Using ", minimapname, " as minimap.\n"));
960                 precache_pic("gfx/ons-cp-neutral.tga");
961                 precache_pic("gfx/ons-cp-red.tga");
962                 precache_pic("gfx/ons-cp-blue.tga");
963                 precache_pic("gfx/ons-frame.tga");
964                 precache_pic("gfx/ons-frame-team.tga");
965         }
966
967         if not(isdemo())
968         {
969                 localcmd("\n_cl_hook_gamestart ", GametypeNameFromType(gametype), ";");
970                 calledhooks |= HOOK_START;
971         }
972 }
973 // 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.
974 void CSQC_Parse_StuffCmd(string strMessage)
975 {
976         localcmd(strMessage);
977 }
978 // 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.
979 void CSQC_Parse_Print(string strMessage)
980 {
981         print(ColorTranslateRGB(strMessage));
982 }
983
984 // CSQC_Parse_CenterPrint : Provides the centerprint string in the first parameter that the server provided.
985 void CSQC_Parse_CenterPrint(string strMessage)
986 {
987         centerprint(strMessage);
988 }
989
990 string notranslate_fogcmd1 = "\nfog ";
991 string notranslate_fogcmd2 = "\nr_fog_exp2 0\nr_drawfog 1\n";
992 void Fog_Force()
993 {
994         // TODO somehow thwart prvm_globalset client ...
995
996         if(forcefog != "")
997                 localcmd(strcat(notranslate_fogcmd1, forcefog, notranslate_fogcmd2));
998 }
999
1000 void Gamemode_Init();
1001 void Ent_ScoresInfo()
1002 {
1003         float i;
1004         self.classname = "ent_client_scores_info";
1005         gametype = ReadByte();
1006         for(i = 0; i < MAX_SCORE; ++i)
1007         {
1008                 scores_label[i] = strzone(ReadString());
1009                 scores_flags[i] = ReadByte();
1010         }
1011         for(i = 0; i < MAX_TEAMSCORE; ++i)
1012         {
1013                 teamscores_label[i] = strzone(ReadString());
1014                 teamscores_flags[i] = ReadByte();
1015         }
1016         HUD_InitScores();
1017         Gamemode_Init();
1018 }
1019
1020 void Ent_Init()
1021 {
1022         self.classname = "ent_client_init";
1023
1024         nb_pb_period = ReadByte() / 32; //Accuracy of 1/32th
1025
1026         hook_shotorigin_x = ReadCoord();
1027         hook_shotorigin_y = ReadCoord();
1028         hook_shotorigin_z = ReadCoord();
1029
1030         if(forcefog)
1031                 strunzone(forcefog);
1032         forcefog = strzone(ReadString());
1033
1034         armorblockpercent = ReadByte() / 255.0;
1035
1036         g_weaponswitchdelay = ReadByte() / 255.0;
1037
1038         g_balance_grenadelauncher_secondary_bouncefactor = ReadCoord();
1039         g_balance_grenadelauncher_secondary_bouncestop = ReadCoord();
1040
1041         if(!postinit)
1042                 PostInit();
1043 }
1044
1045 void Net_ReadRace()
1046 {
1047         float b;
1048
1049         b = ReadByte();
1050
1051         switch(b)
1052         {
1053                 case RACE_NET_CHECKPOINT_HIT_QUALIFYING:
1054                         race_checkpoint = ReadByte();
1055                         race_time = ReadInt24_t();
1056                         race_previousbesttime = ReadInt24_t();
1057                         if(race_previousbestname)
1058                                 strunzone(race_previousbestname);
1059                         race_previousbestname = strzone(ColorTranslateRGB(ReadString()));
1060
1061                         race_checkpointtime = time;
1062
1063                         if(race_checkpoint == 0 || race_checkpoint == 254)
1064                         {
1065                                 race_penaltyaccumulator = 0;
1066                                 race_laptime = time; // valid
1067                         }
1068
1069                         break;
1070
1071                 case RACE_NET_CHECKPOINT_CLEAR:
1072                         race_laptime = 0;
1073                         race_checkpointtime = 0;
1074                         break;
1075
1076                 case RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING:
1077                         race_laptime = ReadCoord();
1078                         race_checkpointtime = -99999;
1079                         // fall through
1080                 case RACE_NET_CHECKPOINT_NEXT_QUALIFYING:
1081                         race_nextcheckpoint = ReadByte();
1082
1083                         race_nextbesttime = ReadInt24_t();
1084                         if(race_nextbestname)
1085                                 strunzone(race_nextbestname);
1086                         race_nextbestname = strzone(ColorTranslateRGB(ReadString()));
1087                         break;
1088
1089                 case RACE_NET_CHECKPOINT_HIT_RACE:
1090                         race_mycheckpoint = ReadByte();
1091                         race_mycheckpointtime = time;
1092                         race_mycheckpointdelta = ReadInt24_t();
1093                         race_mycheckpointlapsdelta = ReadByte();
1094                         if(race_mycheckpointlapsdelta >= 128)
1095                                 race_mycheckpointlapsdelta -= 256;
1096                         if(race_mycheckpointenemy)
1097                                 strunzone(race_mycheckpointenemy);
1098                         race_mycheckpointenemy = strzone(ColorTranslateRGB(ReadString()));
1099                         break;
1100
1101                 case RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT:
1102                         race_othercheckpoint = ReadByte();
1103                         race_othercheckpointtime = time;
1104                         race_othercheckpointdelta = ReadInt24_t();
1105                         race_othercheckpointlapsdelta = ReadByte();
1106                         if(race_othercheckpointlapsdelta >= 128)
1107                                 race_othercheckpointlapsdelta -= 256;
1108                         if(race_othercheckpointenemy)
1109                                 strunzone(race_othercheckpointenemy);
1110                         race_othercheckpointenemy = strzone(ColorTranslateRGB(ReadString()));
1111                         break;
1112
1113                 case RACE_NET_PENALTY_RACE:
1114                         race_penaltyeventtime = time;
1115                         race_penaltytime = ReadShort();
1116                         //race_penaltyaccumulator += race_penaltytime;
1117                         if(race_penaltyreason)
1118                                 strunzone(race_penaltyreason);
1119                         race_penaltyreason = strzone(ReadString());
1120                         break;
1121
1122                 case RACE_NET_PENALTY_QUALIFYING:
1123                         race_penaltyeventtime = time;
1124                         race_penaltytime = ReadShort();
1125                         race_penaltyaccumulator += race_penaltytime;
1126                         if(race_penaltyreason)
1127                                 strunzone(race_penaltyreason);
1128                         race_penaltyreason = strzone(ReadString());
1129                         break;
1130
1131                 case RACE_NET_SERVER_RECORD:
1132                         race_server_record = ReadInt24_t();
1133                         break;
1134                 case RACE_NET_SPEED_AWARD:
1135                         race_speedaward = ReadInt24_t();
1136                         if(race_speedaward_holder)
1137                                 strunzone(race_speedaward_holder);
1138                         race_speedaward_holder = strzone(ReadString());
1139                         break;
1140                 case RACE_NET_SPEED_AWARD_BEST:
1141                         race_speedaward_alltimebest = ReadInt24_t();
1142                         if(race_speedaward_alltimebest_holder)
1143                                 strunzone(race_speedaward_alltimebest_holder);
1144                         race_speedaward_alltimebest_holder = strzone(ReadString());
1145                         break;
1146                 case RACE_NET_SERVER_RANKINGS:
1147                         float pos, prevpos, del;
1148                         pos = ReadShort();
1149                         prevpos = ReadShort();
1150                         del = ReadShort();
1151
1152                         // move other rankings out of the way
1153                         float i;
1154                         if (prevpos) {
1155                                 for (i=prevpos-1;i>pos-1;--i) {
1156                                         grecordtime[i] = grecordtime[i-1];
1157                                         if(grecordholder[i])
1158                                                 strunzone(grecordholder[i]);
1159                                         grecordholder[i] = strzone(grecordholder[i-1]);
1160                                 }
1161                         } else if (del) { // a record has been deleted by the admin
1162                                 for (i=pos-1; i<= RANKINGS_CNT-1; ++i) {
1163                                         if (i == RANKINGS_CNT-1) { // clear out last record
1164                                                 grecordtime[i] = 0;
1165                                                 if (grecordholder[i])
1166                                                         strunzone(grecordholder[i]);
1167                                                 grecordholder[i] = string_null;
1168                                         }
1169                                         else {
1170                                                 grecordtime[i] = grecordtime[i+1];
1171                                                 if (grecordholder[i])
1172                                                         strunzone(grecordholder[i]);
1173                                                 grecordholder[i] = strzone(grecordholder[i+1]);
1174                                         }
1175                                 }
1176                         } else { // player has no ranked record yet
1177                                 for (i=RANKINGS_CNT-1;i>pos-1;--i) {
1178                                         grecordtime[i] = grecordtime[i-1];
1179                                         if(grecordholder[i])
1180                                                 strunzone(grecordholder[i]);
1181                                         grecordholder[i] = strzone(grecordholder[i-1]);
1182                                 }
1183                         }
1184
1185                         // store new ranking
1186                         if(grecordholder[pos-1] != "")
1187                                 strunzone(grecordholder[pos-1]);
1188                         grecordholder[pos-1] = strzone(ReadString());
1189                         grecordtime[pos-1] = ReadInt24_t();
1190                         if(grecordholder[pos-1] == GetPlayerName(player_localentnum -1))
1191                                 race_myrank = pos;
1192                         break;
1193                 case RACE_NET_SERVER_STATUS:
1194                         race_status = ReadShort();
1195                         if(race_status_name)
1196                                 strunzone(race_status_name);
1197                         race_status_name = strzone(ReadString());
1198         }
1199 }
1200
1201 void Net_ReadSpawn()
1202 {
1203         zoomin_effect = 1;
1204         current_viewzoom = 0.6;
1205 }
1206
1207 void Net_TeamNagger()
1208 {
1209         teamnagger = 1;
1210 }
1211
1212 void Net_ReadPingPLReport()
1213 {
1214         float e, pi, pl, ml;
1215         e = ReadByte();
1216         pi = ReadShort();
1217         pl = ReadByte();
1218         ml = ReadByte();
1219         if not(playerslots[e])
1220                 return;
1221         playerslots[e].ping = pi;
1222         playerslots[e].ping_packetloss = pl / 255.0;
1223         playerslots[e].ping_movementloss = ml / 255.0;
1224 }
1225
1226 void Net_VoteDialog(float highlight) {
1227         if(highlight) {
1228                 vote_highlighted = ReadShort();
1229                 return;
1230         }
1231
1232         vote_yescount = ReadShort();
1233         vote_nocount = ReadShort();
1234         vote_needed = ReadShort();
1235         vote_active = 1;
1236 }
1237
1238 void Net_VoteDialogReset() {
1239         vote_active = 0;
1240 }
1241
1242 void Net_Notify() {
1243         float type;
1244         type = ReadByte();
1245
1246         if(type == CSQC_KILLNOTIFY)
1247         {
1248                 HUD_KillNotify(ReadString(), ReadString(), ReadString(), ReadShort(), ReadByte());
1249         }
1250         else if(type == CSQC_CENTERPRINT)
1251         {
1252                 HUD_Centerprint(ReadString(), ReadShort(), ReadByte());
1253         }
1254 }
1255
1256 // CSQC_Parse_TempEntity : Handles all temporary entity network data in the CSQC layer.
1257 // You must ALWAYS first acquire the temporary ID, which is sent as a byte.
1258 // Return value should be 1 if CSQC handled the temporary entity, otherwise return 0 to have the engine process the event.
1259 float CSQC_Parse_TempEntity()
1260 {
1261         local float bHandled;
1262                 bHandled  = true;
1263         // Acquire TE ID
1264         local float nTEID;
1265                 nTEID = ReadByte();
1266
1267                 // NOTE: Could just do return instead of break...
1268         switch(nTEID)
1269         {
1270                 case TE_CSQC_TARGET_MUSIC:
1271                         Net_TargetMusic();
1272                         bHandled = true;
1273                         break;
1274                 case TE_CSQC_PICTURE:
1275                         Net_MapVote_Picture();
1276                         bHandled = true;
1277                         break;
1278                 case TE_CSQC_RACE:
1279                         Net_ReadRace();
1280                         bHandled = true;
1281                         break;
1282                 case 13: // TE_BEAM
1283                         Net_GrapplingHook();
1284                         bHandled = true;
1285                         break;
1286                 case TE_CSQC_SPAWN:
1287                         Net_ReadSpawn();
1288                         bHandled = true;
1289                         break;
1290                 case TE_CSQC_ZCURVEPARTICLES:
1291                         Net_ReadZCurveParticles();
1292                         bHandled = true;
1293                         break;
1294                 case TE_CSQC_NEXGUNBEAMPARTICLE:
1295                         Net_ReadNexgunBeamParticle();
1296                         bHandled = true;
1297                         break;
1298                 case TE_CSQC_TEAMNAGGER:
1299                         Net_TeamNagger();
1300                         bHandled = true;
1301                         break;
1302                 case TE_CSQC_VOTE:
1303                         Net_VoteDialog(ReadByte());
1304                         bHandled = true;
1305                         break;
1306                 case TE_CSQC_VOTERESET:
1307                         Net_VoteDialogReset();
1308                         bHandled = true;
1309                         break;
1310                 case TE_CSQC_LIGHTNINGARC:
1311                         Net_ReadLightningarc();
1312                         bHandled = true;
1313                         break;
1314                 case TE_CSQC_PINGPLREPORT:
1315                         Net_ReadPingPLReport();
1316                         bHandled = true;
1317                         break;
1318                 case TE_CSQC_ANNOUNCE:
1319                         announce_snd = strzone(ReadString());
1320                         bHandled = true;
1321                         break;
1322                 case TE_CSQC_NOTIFY:
1323                         Net_Notify();
1324                         bHandled = true;
1325                         break;
1326                 default:
1327                         // No special logic for this temporary entity; return 0 so the engine can handle it
1328                         bHandled = false;
1329                         break;
1330         }
1331
1332         return bHandled;
1333 }
1334
1335 string getcommandkey(string text, string command)
1336 {
1337         string keys;
1338         float n, j, k, l;
1339
1340         if (!hud_showbinds)
1341                 return text;
1342
1343         keys = db_get(binddb, command);
1344         if (!keys)
1345         {
1346                 n = tokenize(findkeysforcommand(command)); // uses '...' strings
1347                 for(j = 0; j < n; ++j)
1348                 {
1349                         k = stof(argv(j));
1350                         if(k != -1)
1351                         {
1352                                 if ("" == keys)
1353                                         keys = keynumtostring(k);
1354                                 else
1355                                         keys = strcat(keys, ", ", keynumtostring(k));
1356
1357                                 ++l;
1358                                 if (hud_showbinds_limit > 0 && hud_showbinds_limit >= l) break;
1359                         }
1360
1361                 }
1362                 db_put(binddb, command, keys);
1363         }
1364
1365         if ("" == keys) {
1366                 if (hud_showbinds > 1)
1367                         return strcat(text, " (not bound)");
1368                 else
1369                         return text;
1370         }
1371         else if (hud_showbinds > 1)
1372                 return strcat(text, " (", keys, ")");
1373         else
1374                 return keys;
1375 }