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