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