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