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