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