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