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