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