]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/Main.qc
do bouncing client side (sound is still server side)
[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 config_get(string key, string defaultvalue)\r
45 {\r
46         string s;\r
47         s = db_get(configdb, strcat("/s/", key));\r
48         if(s == "")\r
49                 return defaultvalue;\r
50         else\r
51                 return db_get(configdb, strcat("/v/", key));\r
52 }\r
53 \r
54 string forcefog;\r
55 void WaypointSprite_Load();\r
56 void CSQC_Init(void)\r
57 {\r
58 #ifdef USE_FTE\r
59 #pragma target ID\r
60         __engine_check = checkextension("DP_SV_WRITEPICTURE");\r
61         if(!__engine_check)\r
62         {\r
63                 print("^3Your engine build is outdated\n^3This Server uses a newer QC VM. Please update!\n");\r
64                 localcmd("\ndisconnect\n");\r
65                 return;\r
66         }\r
67 #pragma target FTE\r
68 #endif\r
69         \r
70         float i;\r
71         CSQC_CheckEngine();\r
72         dprint_load();\r
73 \r
74         configdb = db_create();\r
75         binddb = db_create();\r
76         tempdb = db_create();\r
77         compressShortVector_init();\r
78 \r
79         drawfont = 0;\r
80         menu_visible = FALSE;\r
81         menu_show = menu_show_error;\r
82         menu_action = menu_sub_null;\r
83         maxclients = 255; // we later get the real maxclients to speed up stuff\r
84         //ctf_temp_1 = "";\r
85         // localcmd("alias order \"cmd order $*\""); enable if ctf-command thingy is used\r
86         //registercmd("ctf_menu");\r
87         registercmd("ons_map");\r
88         //registercmd("menu_action");\r
89         registercmd("sbar_columns_set");\r
90         registercmd("sbar_columns_help");\r
91 \r
92         registercmd("+button3");\r
93         registercmd("-button3");\r
94         registercmd("+button4");\r
95         registercmd("-button4");\r
96 \r
97         registercvar("sbar_usecsqc", "1");\r
98         registercvar("sbar_columns", "default", CVAR_SAVE);\r
99 \r
100         gametype = 0;\r
101 \r
102         // sbar_fields uses strunzone on the titles!\r
103         for(i = 0; i < MAX_SBAR_FIELDS; ++i)\r
104                 sbar_title[i] = strzone("(null)");\r
105 \r
106         postinit = false;\r
107 \r
108         teams = Sort_Spawn();\r
109         players = Sort_Spawn();\r
110         \r
111         GetTeam(COLOR_SPECTATOR, true); // add specs first\r
112 \r
113         cvar_clientsettemp("_supports_weaponpriority", "1");\r
114 \r
115 \r
116 \r
117 \r
118         cs_project_is_b0rked = TRUE;\r
119         R_SetView(VF_VIEWPORT, '0 0 0', '640 480 0');\r
120         R_SetView(VF_FOV, '90 90 0');\r
121         R_SetView(VF_ORIGIN, '0 0 0');\r
122         R_SetView(VF_ANGLES, '0 0 0');\r
123         R_SetView(VF_PERSPECTIVE, 1);\r
124         makevectors('0 0 0');\r
125         vector v;\r
126         v = cs_project(v_forward);\r
127         if(v_x - 320 < +1)\r
128         if(v_x - 320 > -1)\r
129         if(v_y - 240 < +1)\r
130         if(v_y - 240 > -1)\r
131                 cs_project_is_b0rked = FALSE;\r
132 \r
133         WaypointSprite_Load();\r
134 }\r
135 \r
136 // CSQC_Shutdown : Called every time the CSQC code is shutdown (changing maps, quitting, etc)\r
137 void CSQC_Shutdown(void)\r
138 {\r
139 #ifdef USE_FTE\r
140 #pragma TARGET id\r
141         if(!__engine_check)\r
142                 return 0;\r
143 #pragma TARGET fte\r
144 #endif\r
145 \r
146         remove(teams);\r
147         remove(players);\r
148         db_close(configdb);\r
149         db_close(binddb);\r
150         db_close(tempdb);\r
151 \r
152         cvar_clientsettemp_restore();\r
153 }\r
154 \r
155 .float has_team;\r
156 float SetTeam(entity o, float Team)\r
157 {\r
158         entity tm;\r
159         if(Team == -1) // leave\r
160         {\r
161                 if(o.has_team)\r
162                 {\r
163                         //print("(DISCONNECT) leave team ", ftos(o.team), "\n");\r
164                         tm = GetTeam(o.team, false);\r
165                         tm.team_size -= 1;\r
166                         o.has_team = 0;\r
167                         return TRUE;\r
168                 }\r
169         }\r
170         else\r
171         {\r
172                 if not(o.has_team)\r
173                 {\r
174                         //print("(CONNECT) enter team ", ftos(o.team), "\n");\r
175                         o.team = Team;\r
176                         tm = GetTeam(Team, true);\r
177                         tm.team_size += 1;\r
178                         o.has_team = 1;\r
179                         return TRUE;\r
180                 }\r
181                 else if(Team != o.team)\r
182                 {\r
183                         //print("(CHANGE) leave team ", ftos(o.team), "\n");\r
184                         tm = GetTeam(o.team, false);\r
185                         tm.team_size -= 1;\r
186                         o.team = Team;\r
187                         //print("(CHANGE) enter team ", ftos(o.team), "\n");\r
188                         tm = GetTeam(Team, true);\r
189                         tm.team_size += 1;\r
190                         return TRUE;\r
191                 }\r
192         }\r
193         return FALSE;\r
194 }\r
195 \r
196 void Playerchecker_Think()\r
197 {\r
198         float i;\r
199         entity e;\r
200         for(i = 0; i < maxclients; ++i)\r
201         {\r
202                 e = playerslots[i];\r
203                 if(GetPlayerName(i) == "")\r
204                 {\r
205                         if(e.sort_prev)\r
206                         {\r
207                                 //print("playerchecker: KILL KILL KILL\n");\r
208                                 // player disconnected\r
209                                 SetTeam(e, -1);\r
210                                 RemovePlayer(e);\r
211                                 e.sort_prev = world;\r
212                                 //e.gotscores = 0;\r
213                         }\r
214                 }\r
215                 else\r
216                 {\r
217                         if not(e.sort_prev)\r
218                         {\r
219                                 //print("playerchecker: SPAWN SPAWN SPAWN\n");\r
220                                 // player connected\r
221                                 if not(e)\r
222                                         playerslots[i] = e = spawn();\r
223                                 e.sv_entnum = i;\r
224                                 //e.gotscores = 0; // we might already have the scores...\r
225                                 SetTeam(e, GetPlayerColor(i)); // will not hurt; later updates come with Sbar_UpdatePlayerTeams\r
226                                 RegisterPlayer(e);\r
227                                 Sbar_UpdatePlayerPos(e);\r
228                         }\r
229                 }\r
230         }\r
231         self.nextthink = time + 0.2;\r
232 }\r
233 \r
234 void Porto_Init();\r
235 void PostInit(void)\r
236 {\r
237         print(strcat("PostInit\n    maxclients = ", ftos(maxclients), "\n"));\r
238         localcmd(strcat("\nsbar_columns_set ", cvar_string("sbar_columns"), ";\n"));\r
239 \r
240         entity playerchecker;\r
241         playerchecker = spawn();\r
242         playerchecker.think = Playerchecker_Think;\r
243         playerchecker.nextthink = time + 0.2;\r
244 \r
245         Porto_Init();\r
246 \r
247         postinit = true;\r
248 }\r
249 \r
250 // CSQC_ConsoleCommand : Used to parse commands in the console that have been registered with the "registercmd" function\r
251 // Return value should be 1 if CSQC handled the command, otherwise return 0 to have the engine handle it.\r
252 float button_zoom;\r
253 void Cmd_Sbar_SetFields(float);\r
254 void Cmd_Sbar_Help(float);\r
255 float CSQC_ConsoleCommand(string strMessage)\r
256 {\r
257         float argc;\r
258         // Tokenize String\r
259         //argc = tokenize(strMessage);\r
260         argc = tokenize_sane(strMessage);\r
261         \r
262         // Acquire Command\r
263         local string strCmd;\r
264         strCmd = argv(0);\r
265 \r
266         if(strCmd == "+button4") { // zoom\r
267                 // return false, because the message shall be sent to the server anyway (for demos/speccing)\r
268                 if(ignore_plus_zoom)\r
269                 {\r
270                         --ignore_plus_zoom;\r
271                         return false;\r
272                 }\r
273                 button_zoom = 1;\r
274                 return true;\r
275         } else if(strCmd == "-button4") { // zoom\r
276                 if(ignore_minus_zoom)\r
277                 {\r
278                         --ignore_minus_zoom;\r
279                         return false;\r
280                 }\r
281                 button_zoom = 0;\r
282                 return true;\r
283         } else if(strCmd == "+button3") { // secondary\r
284                 button_attack2 = 1;\r
285                 return false;\r
286         } else if(strCmd == "-button3") { // secondary\r
287                 button_attack2 = 0;\r
288                 return false;\r
289         } else if(strCmd == "+showscores") {\r
290                 sb_showscores = true;\r
291                 return true;\r
292         } else if(strCmd == "-showscores") {\r
293                 sb_showscores = false;\r
294                 return true;\r
295         }\r
296         \r
297         return false;\r
298 }\r
299 \r
300 float GameCommand(string msg)\r
301 {\r
302         float argc;\r
303         argc = tokenize_sane(msg);\r
304         string cmd;\r
305         cmd = argv(0);\r
306         if(cmd == "mv_download") {\r
307                 Cmd_MapVote_MapDownload(argc);\r
308                 return true;\r
309         }\r
310         else if(cmd == "settemp") {\r
311                 cvar_clientsettemp(argv(1), argv(2));\r
312         }\r
313         else if(cmd == "radar") {\r
314                 ons_showmap = !ons_showmap;\r
315         }\r
316         else if(cmd == "sbar_columns_set") {\r
317                 Cmd_Sbar_SetFields(argc);\r
318         }\r
319         else if(cmd == "sbar_columns_help") {\r
320                 Cmd_Sbar_Help(argc);\r
321         }\r
322 #ifdef BLURTEST\r
323         else if(cmd == "blurtest") {\r
324                 blurtest_time0 = time;\r
325                 blurtest_time1 = time + stof(argv(1));\r
326                 blurtest_radius = stof(argv(2));\r
327                 blurtest_power = stof(argv(3));\r
328         }\r
329 #endif\r
330         \r
331         return false;\r
332 }\r
333 \r
334 // CSQC_InputEvent : Used to perform actions based on any key pressed, key released and mouse on the client.\r
335 // Return value should be 1 if CSQC handled the input, otherwise return 0 to have the input passed to the engine.\r
336 // All keys are in ascii.\r
337 // bInputType = 0 is key pressed, 1 is key released, 2 is mouse input.\r
338 // In the case of keyboard input, nPrimary is the ascii code, and nSecondary is 0.\r
339 // In the case of mouse input, nPrimary is xdelta, nSecondary is ydelta.\r
340 float CSQC_InputEvent(float bInputType, float nPrimary, float nSecondary)\r
341 {\r
342         local float bSkipKey;\r
343         bSkipKey = false;\r
344         \r
345         if(menu_visible)\r
346                 if(menu_action(bInputType, nPrimary, nSecondary))\r
347                         return TRUE;\r
348         return bSkipKey;\r
349 }\r
350 \r
351 // END REQUIRED CSQC FUNCTIONS\r
352 // --------------------------------------------------------------------------\r
353 \r
354 // --------------------------------------------------------------------------\r
355 // BEGIN OPTIONAL CSQC FUNCTIONS\r
356 void Ent_ReadEntCS()\r
357 {\r
358         InterpolateOrigin_Undo();\r
359 \r
360         self.classname = "entcs_receiver";\r
361         self.sv_entnum = ReadByte() - 1;\r
362         self.origin_x = ReadShort();\r
363         self.origin_y = ReadShort();\r
364         self.origin_z = ReadShort();\r
365         self.angles_y = ReadByte() * 360.0 / 256;\r
366         self.origin_z = self.angles_x = self.angles_z = 0;\r
367 \r
368         InterpolateOrigin_Note();\r
369 }\r
370 \r
371 void Ent_RemoveEntCS()\r
372 {\r
373 }\r
374 \r
375 void Ent_Remove();\r
376 void Ent_ReadPlayerScore()\r
377 {\r
378         float i, n;\r
379         float isNew;\r
380         entity o;\r
381 \r
382         // damnit -.- don't want to go change every single .sv_entnum in sbar.qc AGAIN\r
383         // (no I've never heard of M-x replace-string, sed, or anything like that)\r
384         isNew = !self.owner; // workaround for DP bug\r
385         n = ReadByte()-1;\r
386 \r
387 #ifdef DP_CSQC_ENTITY_REMOVE_IS_B0RKED\r
388         if(!isNew && n != self.sv_entnum)\r
389         {\r
390                 print("A CSQC entity changed its owner!\n");\r
391                 isNew = true;\r
392                 Ent_Remove();\r
393                 self.enttype = ENT_CLIENT_SCORES;\r
394         }\r
395 #endif\r
396 \r
397         self.sv_entnum = n;\r
398 \r
399         if not(playerslots[self.sv_entnum])\r
400                 playerslots[self.sv_entnum] = spawn();\r
401         o = self.owner = playerslots[self.sv_entnum];\r
402         o.sv_entnum = self.sv_entnum;\r
403         o.gotscores = 1;\r
404 \r
405         //if not(o.sort_prev)\r
406         //      RegisterPlayer(o);\r
407         //playerchecker will do this for us later, if it has not already done so\r
408 \r
409 #if MAX_SCORE <= 3\r
410         for(i = 0; i < MAX_SCORE; ++i)\r
411                 o.(scores[i]) = ReadShort();\r
412 #else\r
413         float sf;\r
414 #if MAX_SCORE <= 8\r
415         sf = ReadByte();\r
416 #else\r
417         sf = ReadShort();\r
418 #endif\r
419         float p;\r
420         for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)\r
421                 if(sf & p)\r
422                         o.(scores[i]) = ReadShort();\r
423 #endif\r
424 \r
425         if(o.sort_prev)\r
426                 Sbar_UpdatePlayerPos(o); // if not registered, we cannot do this yet!\r
427 }\r
428 \r
429 void Ent_ReadTeamScore()\r
430 {\r
431         float i;\r
432         entity o;\r
433         \r
434         self.team = ReadByte();\r
435         o = self.owner = GetTeam(self.team, true);\r
436 \r
437 #if MAX_TEAMSCORE <= 3\r
438         for(i = 0; i < MAX_TEAMSCORE; ++i)\r
439                 o.(teamscores[i]) = ReadShort();\r
440 #else\r
441         float sf;\r
442 #if MAX_TEAMSCORE <= 8\r
443         sf = ReadByte();\r
444 #else\r
445         sf = ReadShort();\r
446 #endif\r
447         float p;\r
448         for(i = 0, p = 1; i < MAX_TEAMSCORE; ++i, p *= 2)\r
449                 if(sf & p)\r
450                         o.(teamscores[i]) = ReadShort();\r
451 #endif\r
452 \r
453         Sbar_UpdateTeamPos(o);\r
454 }\r
455 \r
456 void Ent_Nagger()\r
457 {\r
458         float nags;\r
459 \r
460         nags = ReadByte();\r
461 \r
462         if(nags & 128)\r
463         {\r
464                 if(vote_called_vote)\r
465                         strunzone(vote_called_vote);\r
466                 vote_called_vote = strzone(ColorTranslateRGB(ReadString()));\r
467         }\r
468 \r
469         ready_waiting = (nags & 1);\r
470         ready_waiting_for_me = (nags & 2);\r
471         vote_waiting = (nags & 4);\r
472         vote_waiting_for_me = (nags & 8);\r
473 }\r
474 \r
475 // CSQC_Ent_Update : Called every frame that the server has indicated an update to the SSQC / CSQC entity has occured.\r
476 // The only parameter reflects if the entity is "new" to the client, meaning it just came into the client's PVS.\r
477 void Ent_RadarLink();\r
478 void(float bIsNewEntity) CSQC_Ent_Update =\r
479 {\r
480         float t;\r
481         t = ReadByte();\r
482 #ifdef DP_CSQC_ENTITY_REMOVE_IS_B0RKED\r
483         if(self.enttype)\r
484                 if(t != self.enttype)\r
485                 {\r
486                         print("A CSQC entity changed its type!\n");\r
487                         Ent_Remove();\r
488                 }\r
489 #endif\r
490         self.enttype = t;\r
491         if(self.enttype == ENT_CLIENT_ENTCS)\r
492                 Ent_ReadEntCS();\r
493         else if(self.enttype == ENT_CLIENT_SCORES)\r
494                 Ent_ReadPlayerScore();\r
495         else if(self.enttype == ENT_CLIENT_TEAMSCORES)\r
496                 Ent_ReadTeamScore();\r
497         else if(self.enttype == ENT_CLIENT_POINTPARTICLES)\r
498                 Ent_PointParticles();\r
499         else if(self.enttype == ENT_CLIENT_RAINSNOW)\r
500                 Ent_RainOrSnow();\r
501         else if(self.enttype == ENT_CLIENT_LASER)\r
502                 Ent_Laser();\r
503         else if(self.enttype == ENT_CLIENT_NAGGER)\r
504                 Ent_Nagger();\r
505         else if(self.enttype == ENT_CLIENT_WAYPOINT)\r
506                 Ent_WaypointSprite();\r
507         else if(self.enttype == ENT_CLIENT_RADARLINK)\r
508                 Ent_RadarLink();\r
509         else if(self.enttype == ENT_CLIENT_PROJECTILE)\r
510                 Ent_Projectile();\r
511         else\r
512                 error(strcat("unknown entity type in CSQC_Ent_Update: ", ftos(self.enttype), "\n"));\r
513         \r
514 };\r
515 // Destructor, but does NOT deallocate the entity by calling remove(). Also\r
516 // used when an entity changes its type. For an entity that someone interacts\r
517 // with others, make sure it can no longer do so.\r
518 void Ent_Remove()\r
519 {\r
520         float i;\r
521         if(self.enttype == ENT_CLIENT_ENTCS)\r
522         {\r
523                 Ent_RemoveEntCS();\r
524         } else if(self.enttype == ENT_CLIENT_SCORES)\r
525         {\r
526                 if(self.owner)\r
527                 {\r
528                         SetTeam(self.owner, -1);\r
529                         self.owner.gotscores = 0;\r
530                         for(i = 0; i < MAX_SCORE; ++i)\r
531                                 self.owner.(scores[i]) = 0; // clear all scores\r
532                 }\r
533         } else if(self.enttype == ENT_CLIENT_TEAMSCORES)\r
534         {\r
535                 /*\r
536                 if(self.owner)\r
537                         RemoveTeam(self.owner);\r
538                 */\r
539                 // we don't NEED to remove them... they won't display anyway\r
540                 // plus, svqc never does this anyway\r
541         } else if(self.enttype == ENT_CLIENT_POINTPARTICLES)\r
542         {\r
543                 Ent_PointParticles_Remove();\r
544         }\r
545         else if(self.enttype == ENT_CLIENT_WAYPOINT)\r
546                 Ent_RemoveWaypointSprite();\r
547         else if(self.enttype == ENT_CLIENT_PROJECTILE)\r
548                 Ent_RemoveProjectile();\r
549 \r
550         self.enttype = 0;\r
551         self.classname = "";\r
552         self.draw = menu_sub_null;\r
553         // TODO possibly set more stuff to defaults\r
554 }\r
555 // CSQC_Ent_Remove : Called when the server requests a SSQC / CSQC entity to be removed.  Essentially call remove(self) as well.\r
556 void CSQC_Ent_Remove()\r
557 {\r
558         if(self.enttype)\r
559                 Ent_Remove();\r
560         remove(self);\r
561 }\r
562 \r
563 void Gamemode_Init()\r
564 {\r
565         get_mi_min_max_texcoords(1); // try the CLEVER way first\r
566         minimapname = strcat("gfx/", mi_shortname, "_radar.tga");\r
567         shortmapname = mi_shortname;\r
568 \r
569         if(precache_pic(minimapname) == "")\r
570         {\r
571                 // but maybe we have a non-clever minimap\r
572                 minimapname = strcat("gfx/", mi_shortname, "_mini.tga");\r
573                 if(precache_pic(minimapname) == "")\r
574                         minimapname = ""; // FAIL\r
575                 else\r
576                         get_mi_min_max_texcoords(0); // load new texcoords\r
577         }\r
578 \r
579         mi_center = (mi_min + mi_max) * 0.5;\r
580         mi_scale = mi_max - mi_min;\r
581         minimapname = strzone(minimapname);\r
582 \r
583         if(gametype == GAME_ONSLAUGHT) {\r
584                 print(strcat("Using ", minimapname, " as minimap.\n"));\r
585                 precache_pic("gfx/ons-cp-neutral.tga");\r
586                 precache_pic("gfx/ons-cp-red.tga");\r
587                 precache_pic("gfx/ons-cp-blue.tga");\r
588                 precache_pic("gfx/ons-frame.tga");\r
589                 precache_pic("gfx/ons-frame-team.tga");\r
590         } else if(gametype == GAME_KEYHUNT) {\r
591                 precache_pic("gfx/sb_key_carrying");\r
592                 precache_pic("gfx/sb_key_carrying_outline");\r
593         }\r
594 }\r
595 // 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
596 void CSQC_Parse_StuffCmd(string strMessage)\r
597 {\r
598         localcmd(strMessage);\r
599 }\r
600 // 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
601 void CSQC_Parse_Print(string strMessage)\r
602 {\r
603         print(ColorTranslateRGB(strMessage));\r
604 }\r
605 // CSQC_Parse_CenterPrint : Provides the centerprint string in the first parameter that the server provided.  To execute standard behavior, simply execute cprint with the string.\r
606 void CSQC_Parse_CenterPrint(string strMessage)\r
607 {\r
608         cprint(ColorTranslateRGB(strMessage));\r
609 }\r
610 \r
611 void CSQC_CheckRevision();\r
612 \r
613 void Fog_Force()\r
614 {\r
615         // TODO somehow thwart prvm_globalset client ...\r
616 \r
617         if(forcefog != "")\r
618                 localcmd(strcat("\nfog ", forcefog, "\nr_fog_exp2 0\nr_drawfog 1\n"));\r
619 }\r
620 \r
621 void Net_ReadFog()\r
622 {\r
623         if(forcefog)\r
624                 strunzone(forcefog);\r
625         ReadCoord(); // ignore fog interval\r
626         forcefog = strzone(ReadString());\r
627 }\r
628 \r
629 void Gamemode_Init();\r
630 void Net_ReadScoresInfo()\r
631 {\r
632         float i;\r
633         gametype = ReadByte();\r
634         for(i = 0; i < MAX_SCORE; ++i)\r
635         {\r
636                 scores_label[i] = strzone(ReadString());\r
637                 scores_flags[i] = ReadByte();\r
638         }\r
639         for(i = 0; i < MAX_TEAMSCORE; ++i)\r
640         {\r
641                 teamscores_label[i] = strzone(ReadString());\r
642                 teamscores_flags[i] = ReadByte();\r
643         }\r
644         Sbar_InitScores();\r
645         Gamemode_Init();\r
646 }\r
647 \r
648 void Net_ReadInit()\r
649 {\r
650         float i;\r
651         csqc_revision = ReadShort();\r
652         maxclients = ReadByte();\r
653         for(i = 0; i < 24; ++i)\r
654                 weaponimpulse[i] = ReadByte() - 1;\r
655         hook_shotorigin_x = ReadCoord();\r
656         hook_shotorigin_y = ReadCoord();\r
657         hook_shotorigin_z = ReadCoord();\r
658         CSQC_CheckRevision();\r
659 }\r
660 \r
661 string Net_ReadPicture()\r
662 {\r
663         string img;\r
664         if(csqc_flags & CSQC_FLAG_READPICTURE)\r
665         {\r
666                 img = ReadPicture();\r
667                 print(strcat("Got Picture: ", img, "\n"));\r
668         } else {\r
669                 img = ReadString();\r
670                 print(strcat("^3Warning: ^7Couldn't download ", img, ". This is probably because your engine build is outdated.\n"));\r
671                 float psize, i;\r
672                 psize = ReadShort();\r
673                 // Can I be sure that ReadShort is 2 bytes and ReadLong is 4 bytes?\r
674                 // Because then this could be optimized to first get all 4-byte-groups,\r
675                 // then the remaining 2, then the remaining 1\r
676                 for(i = 0; i < psize; ++i)\r
677                         ReadByte();\r
678         }\r
679         return img;\r
680 }\r
681 \r
682 void Net_Config()\r
683 {\r
684         string key, value;\r
685         key = ReadString();\r
686         value = ReadString();\r
687         db_put(configdb, strcat("/v/", key), value);\r
688         db_put(configdb, strcat("/s/", key), "1");\r
689 }\r
690 \r
691 void Net_ReadRace()\r
692 {\r
693         float b;\r
694 \r
695         b = ReadByte();\r
696 \r
697         switch(b)\r
698         {\r
699                 case RACE_NET_CHECKPOINT_HIT_QUALIFYING:\r
700                         race_checkpoint = ReadByte();\r
701                         race_time = ReadShort();\r
702                         race_previousbesttime = ReadShort();\r
703                         if(race_previousbestname)\r
704                                 strunzone(race_previousbestname);\r
705                         race_previousbestname = strzone(ColorTranslateRGB(ReadString()));\r
706 \r
707                         race_checkpointtime = time;\r
708 \r
709                         if(race_checkpoint == 0)\r
710                                 race_laptime = time; // valid\r
711 \r
712                         break;\r
713 \r
714                 case RACE_NET_CHECKPOINT_CLEAR:\r
715                         race_laptime = 0;\r
716                         race_checkpointtime = 0;\r
717                         break;\r
718 \r
719                 case RACE_NET_CHECKPOINT_NEXT_SPEC_QUALIFYING:\r
720                         race_laptime = ReadCoord();\r
721                         race_checkpointtime = -99999;\r
722                         // fall through\r
723                 case RACE_NET_CHECKPOINT_NEXT_QUALIFYING:\r
724                         race_nextcheckpoint = ReadByte();\r
725 \r
726                         race_nextbesttime = ReadShort();\r
727                         if(race_nextbestname)\r
728                                 strunzone(race_nextbestname);\r
729                         race_nextbestname = strzone(ColorTranslateRGB(ReadString()));\r
730                         break;\r
731 \r
732                 case RACE_NET_CHECKPOINT_HIT_RACE:\r
733                         race_mycheckpoint = ReadByte();\r
734                         race_mycheckpointtime = time;\r
735                         race_mycheckpointdelta = ReadShort();\r
736                         race_mycheckpointlapsdelta = ReadByte();\r
737                         if(race_mycheckpointlapsdelta >= 128)\r
738                                 race_mycheckpointlapsdelta -= 256;\r
739                         if(race_mycheckpointenemy)\r
740                                 strunzone(race_mycheckpointenemy);\r
741                         race_mycheckpointenemy = strzone(ColorTranslateRGB(ReadString()));\r
742                         break;\r
743 \r
744                 case RACE_NET_CHECKPOINT_HIT_RACE_BY_OPPONENT:\r
745                         race_othercheckpoint = ReadByte();\r
746                         race_othercheckpointtime = time;\r
747                         race_othercheckpointdelta = ReadShort();\r
748                         race_othercheckpointlapsdelta = ReadByte();\r
749                         if(race_othercheckpointlapsdelta >= 128)\r
750                                 race_othercheckpointlapsdelta -= 256;\r
751                         if(race_othercheckpointenemy)\r
752                                 strunzone(race_othercheckpointenemy);\r
753                         race_othercheckpointenemy = strzone(ColorTranslateRGB(ReadString()));\r
754                         break;\r
755         }\r
756 }\r
757 \r
758 void Net_ReadForceScoreboard()\r
759 {\r
760         sb_showscores_force = ReadByte();\r
761 }\r
762 \r
763 void Net_Reset()\r
764 {\r
765         float i;\r
766         race_laptime = 0;\r
767         race_checkpointtime = 0;\r
768         for(i = 0; i < 24; ++i)\r
769                 angles_held_status[i] = 0;\r
770 }\r
771 \r
772 void Net_ReadSpectating()\r
773 {\r
774         float newspectatee_status;\r
775         newspectatee_status = ReadByte();\r
776         if(newspectatee_status == player_localentnum)\r
777                 newspectatee_status = -1; // observing\r
778         if(newspectatee_status != spectatee_status)\r
779                 Net_Reset();\r
780         spectatee_status = newspectatee_status;\r
781 }\r
782 \r
783 void Net_ReadZoomNotify()\r
784 {\r
785         spectatorbutton_zoom = ReadByte();\r
786 }\r
787 \r
788 void Net_ReadSpawn()\r
789 {\r
790         zoomin_effect = 1;\r
791         current_viewzoom = 0.6;\r
792 }\r
793 \r
794 void Net_ReadWarmupStage()\r
795 {\r
796         warmup_stage = ReadByte();\r
797 }\r
798 \r
799 // CSQC_Parse_TempEntity : Handles all temporary entity network data in the CSQC layer.\r
800 // You must ALWAYS first acquire the temporary ID, which is sent as a byte.\r
801 // Return value should be 1 if CSQC handled the temporary entity, otherwise return 0 to have the engine process the event.\r
802 void Net_ReadHoldAngles();\r
803 float CSQC_Parse_TempEntity()\r
804 {\r
805         local float bHandled;\r
806                 bHandled  = true;\r
807         // Acquire TE ID\r
808         local float nTEID;\r
809                 nTEID = ReadByte();\r
810 \r
811                 // NOTE: Could just do return instead of break...\r
812         switch(nTEID)\r
813         {\r
814                 case TE_CSQC_INIT:\r
815                         Net_ReadInit();\r
816                         bHandled = true;\r
817                         break;\r
818                 case TE_CSQC_MAPVOTE:\r
819                         Net_Mapvote();\r
820                         bHandled = true;\r
821                         break;\r
822                 case TE_CSQC_CONFIG:\r
823                         Net_Config();\r
824                         bHandled = true;\r
825                         break;\r
826                 case TE_CSQC_SCORESINFO:\r
827                         Net_ReadScoresInfo();\r
828                         bHandled = true;\r
829                         break;\r
830                 case TE_CSQC_RACE:\r
831                         Net_ReadRace();\r
832                         bHandled = true;\r
833                         break;\r
834                 case TE_CSQC_FORCESCOREBOARD:\r
835                         Net_ReadForceScoreboard();\r
836                         bHandled = true;\r
837                         break;\r
838                 case TE_CSQC_SPECTATING:\r
839                         Net_ReadSpectating();\r
840                         bHandled = true;\r
841                         break;\r
842                 case 13: // TE_BEAM\r
843                         Net_GrapplingHook();\r
844                         bHandled = true;\r
845                         break;\r
846                 case TE_CSQC_SPAWN:\r
847                         Net_ReadSpawn();\r
848                         bHandled = true;\r
849                         break;\r
850                 case TE_CSQC_ZOOMNOTIFY:\r
851                         Net_ReadZoomNotify();\r
852                         bHandled = true;\r
853                         break;\r
854                 case TE_CSQC_HOLDANGLES:\r
855                         Net_ReadHoldAngles();\r
856                         bHandled = true;\r
857                         break;\r
858                 case TE_CSQC_WARMUP:\r
859                         Net_ReadWarmupStage();\r
860                         bHandled = true;\r
861                         break;\r
862                 case TE_CSQC_FOG:\r
863                         Net_ReadFog();\r
864                         bHandled = true;\r
865                         break;\r
866                 default:\r
867                         // No special logic for this temporary entity; return 0 so the engine can handle it\r
868                         bHandled = false;\r
869                         break;\r
870         }\r
871         \r
872         if(!postinit)\r
873                 PostInit();\r
874                 \r
875         return bHandled;\r
876 }\r
877 \r
878 // COMMIT-TODO: Update if necessare, before committing\r
879 void CSQC_CheckRevision()\r
880 {\r
881         if(csqc_revision == CSQC_REVISION)\r
882         {\r
883                 print("^2SVQC and CSQC revisions are compatible.\n");\r
884         } else if(csqc_revision < CSQC_REVISION) {\r
885                 print("^1Your csprogs.dat (CSQC) version is newer than the one on the server.\n");\r
886         } else if(csqc_revision > CSQC_REVISION) {\r
887                 print("^1Your csprogs.dat (CSQC) is too old for this server.\n");\r
888                 print("^1Please update to a newer version.\n");\r
889         }\r
890 }\r
891 \r
892 string getcommandkey(string text, string command)\r
893 {\r
894         string keys;\r
895         float n, j, k, l;\r
896 \r
897         if (!sbar_showbinds)\r
898                 return text;\r
899 \r
900         keys = db_get(binddb, command);\r
901         if(csqc_flags & CSQC_FLAG_READPICTURE)\r
902         {\r
903                 if (!keys) \r
904                 {\r
905                         n = tokenize_insane(findkeysforcommand(command)); // uses '...' strings\r
906                         for(j = 0; j < n; ++j)\r
907                         {\r
908                                 k = stof(argv(j));\r
909                                 if(k != -1) \r
910                                 {\r
911                                         if ("" == keys)\r
912                                                 keys = keynumtostring(k);\r
913                                         else\r
914                                                 keys = strcat(keys, ", ", keynumtostring(k));\r
915 \r
916                                         ++l;\r
917                                         if (sbar_showbinds_limit > 0 && sbar_showbinds_limit >= l) break;\r
918                                 }\r
919                         \r
920                         }\r
921                         db_put(binddb, command, keys);\r
922                 }\r
923         }\r
924         \r
925         if ("" == keys) {\r
926                 if (sbar_showbinds > 1)\r
927                         return strcat(text, " (not bound)");\r
928                 else\r
929                         return text;\r
930         }\r
931         else if (sbar_showbinds > 1)\r
932                 return strcat(text, " (", keys, ")");\r
933         else\r
934                 return keys;\r
935 }\r