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