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