new packet loss column ("pl")
[divverent/nexuiz.git] / data / qcsrc / client / Main.qc
1 // --------------------------------------------------------------------------\r
2 // BEGIN REQUIRED CSQC FUNCTIONS\r
3 //include "main.qh"\r
4 \r
5 void() menu_show_error =\r
6 {\r
7         drawstring('0 200 0', "ERROR - MENU IS VISIBLE BUT NO MENU WAS DEFINED!", '8 8 0', '1 0 0', 1, 0);\r
8 };\r
9 \r
10 // CSQC_Init : Called every time the CSQC code is initialized (essentially at map load)\r
11 // Useful for precaching things\r
12 \r
13 void() menu_sub_null =\r
14 {\r
15 };\r
16 \r
17 // let's make this a general data buffer...\r
18 float using_gps;\r
19 \r
20 #ifdef USE_FTE\r
21 float __engine_check;\r
22 #endif\r
23 \r
24 string config_get(string key, string defaultvalue)\r
25 {\r
26         string s;\r
27         s = db_get(configdb, strcat("/s/", key));\r
28         if(s == "")\r
29                 return defaultvalue;\r
30         else\r
31                 return db_get(configdb, strcat("/v/", key));\r
32 }\r
33 \r
34 void CSQC_Init(void)\r
35 {\r
36 #ifdef USE_FTE\r
37 #pragma target ID\r
38         __engine_check = checkextension("DP_SV_WRITEPICTURE");\r
39         if(!__engine_check)\r
40         {\r
41                 print("^3Your engine build is outdated\n^3This Server uses a newer QC VM. Please update!\n");\r
42                 localcmd("\ndisconnect\n");\r
43                 return;\r
44         }\r
45 #pragma target FTE\r
46 #endif\r
47         \r
48         float i;\r
49         CSQC_CheckEngine();\r
50 \r
51         configdb = db_create();\r
52 \r
53         drawfont = 0;\r
54         menu_visible = FALSE;\r
55         menu_show = menu_show_error;\r
56         menu_action = menu_sub_null;\r
57         using_gps = false;\r
58         maxclients = 255; // we later get the real maxclients to speed up stuff\r
59         //ctf_temp_1 = "";\r
60         // localcmd("alias order \"cmd order $*\""); enable if ctf-command thingy is used\r
61         //registercmd("ctf_menu");\r
62         registercmd("ons_map");\r
63         //registercmd("menu_action");\r
64         registercmd("sbar_columns_set");\r
65         registercmd("sbar_columns_help");\r
66 \r
67         registercvar("sbar_usecsqc", "1");\r
68         registercvar("sbar_columns", "default", CVAR_SAVE);\r
69 \r
70         gametype = 0;\r
71 \r
72         gps_start = world;\r
73 \r
74         // sbar_fields uses strunzone on the titles!\r
75         for(i = 0; i < MAX_SBAR_FIELDS; ++i)\r
76                 sbar_title[i] = strzone("(null)");\r
77 \r
78         postinit = false;\r
79 \r
80         teams = Sort_Spawn();\r
81         players = Sort_Spawn();\r
82         \r
83         GetTeam(COLOR_SPECTATOR, true); // add specs first\r
84 }\r
85 \r
86 // CSQC_Shutdown : Called every time the CSQC code is shutdown (changing maps, quitting, etc)\r
87 void CSQC_Shutdown(void)\r
88 {\r
89 #ifdef USE_FTE\r
90 #pragma TARGET id\r
91         if(!__engine_check)\r
92                 return 0;\r
93 #pragma TARGET fte\r
94 #endif\r
95 \r
96         remove(teams);\r
97         remove(players);\r
98         db_close(configdb);\r
99 }\r
100 \r
101 .float has_team;\r
102 float SetTeam(entity o, float Team)\r
103 {\r
104         entity tm;\r
105         if(Team == -1) // leave\r
106         {\r
107                 if(o.has_team)\r
108                 {\r
109                         //print("(DISCONNECT) leave team ", ftos(o.team), "\n");\r
110                         tm = GetTeam(o.team, false);\r
111                         tm.team_size -= 1;\r
112                         o.has_team = 0;\r
113                         return TRUE;\r
114                 }\r
115         }\r
116         else\r
117         {\r
118                 if not(o.has_team)\r
119                 {\r
120                         //print("(CONNECT) enter team ", ftos(o.team), "\n");\r
121                         o.team = Team;\r
122                         tm = GetTeam(Team, true);\r
123                         tm.team_size += 1;\r
124                         o.has_team = 1;\r
125                         return TRUE;\r
126                 }\r
127                 else if(Team != o.team)\r
128                 {\r
129                         //print("(CHANGE) leave team ", ftos(o.team), "\n");\r
130                         tm = GetTeam(o.team, false);\r
131                         tm.team_size -= 1;\r
132                         o.team = Team;\r
133                         //print("(CHANGE) enter team ", ftos(o.team), "\n");\r
134                         tm = GetTeam(Team, true);\r
135                         tm.team_size += 1;\r
136                         return TRUE;\r
137                 }\r
138         }\r
139         return FALSE;\r
140 }\r
141 \r
142 void Playerchecker_Think()\r
143 {\r
144         entity pl;\r
145         float i;\r
146         for(i = 0; i < maxclients; ++i)\r
147         {\r
148                 if(getplayerkey(i, "name") == "")\r
149                 {\r
150                         if(playerslots[i].sort_prev)\r
151                         {\r
152                                 //print("playerchecker: KILL KILL KILL\n");\r
153                                 // player disconnected\r
154                                 SetTeam(playerslots[i], -1);\r
155                                 RemovePlayer(playerslots[i]);\r
156                                 playerslots[i].sort_prev = world;\r
157                                 playerslots[i].gotscores = 0;\r
158                         }\r
159                 }\r
160                 else\r
161                 {\r
162                         if not(playerslots[i].sort_prev)\r
163                         {\r
164                                 //print("playerchecker: SPAWN SPAWN SPAWN\n");\r
165                                 // player connected\r
166                                 if not(playerslots[i])\r
167                                         playerslots[i] = spawn();\r
168                                 playerslots[i].sv_entnum = i;\r
169                                 playerslots[i].gotscores = 0;\r
170                                 SetTeam(playerslots[i], COLOR_SPECTATOR);\r
171                                 RegisterPlayer(playerslots[i]);\r
172                         }\r
173                 }\r
174         }\r
175         self.nextthink = time + 0.2;\r
176 }\r
177 \r
178 void PostInit(void)\r
179 {\r
180         float i;\r
181 \r
182         print(strcat("PostInit\n    maxclients = ", ftos(maxclients), "\n"));\r
183         localcmd(strcat("\nsbar_columns_set ", cvar_string("sbar_columns"), ";\n"));\r
184 \r
185         entity playerchecker;\r
186         playerchecker = spawn();\r
187         playerchecker.think = Playerchecker_Think;\r
188         playerchecker.nextthink = time + 0.2;\r
189 \r
190         postinit = true;\r
191 }\r
192 \r
193 // CSQC_ConsoleCommand : Used to parse commands in the console that have been registered with the "registercmd" function\r
194 // Return value should be 1 if CSQC handled the command, otherwise return 0 to have the engine handle it.\r
195 void Cmd_Sbar_SetFields(float);\r
196 void Cmd_Sbar_Help(float);\r
197 float CSQC_ConsoleCommand(string strMessage)\r
198 {\r
199         float argc;\r
200         // Tokenize String\r
201         //argc = tokenize(strMessage);\r
202         argc = tokenizebyseparator(strMessage, " ");\r
203         \r
204         // Acquire Command\r
205         local string strCmd;\r
206         strCmd = argv(0);\r
207 \r
208         /*if(strCmd == "ctf_menu") {\r
209                 ctf_menu_show();\r
210                 nReturn = true;\r
211                 } else*/\r
212         if(strCmd == "ons_map") {\r
213                 Cmd_ons_map();\r
214                 return true;\r
215         } else if(strCmd == "sbar_columns_set") {\r
216                 Cmd_Sbar_SetFields(argc);\r
217                 return true;\r
218         } else if(strCmd == "sbar_columns_help") {\r
219                 Cmd_Sbar_Help(argc);\r
220                 return true;\r
221         } else if(strCmd == "+showscores") {\r
222                 sb_showscores = true;\r
223                 return true;\r
224         } else if(strCmd == "-showscores") {\r
225                 sb_showscores = false;\r
226                 return true;\r
227         }\r
228         \r
229         return false;\r
230 }\r
231 \r
232 float GameCommand(string msg)\r
233 {\r
234         float argc;\r
235         argc = tokenize(msg);\r
236         string cmd;\r
237         cmd = argv(0);\r
238         if(cmd == "mv_download") {\r
239                 Cmd_MapVote_MapDownload(argc);\r
240                 return true;\r
241         }\r
242         \r
243         return false;\r
244 }\r
245 \r
246 // CSQC_InputEvent : Used to perform actions based on any key pressed, key released and mouse on the client.\r
247 // Return value should be 1 if CSQC handled the input, otherwise return 0 to have the input passed to the engine.\r
248 // All keys are in ascii.\r
249 // bInputType = 0 is key pressed, 1 is key released, 2 is mouse input.\r
250 // In the case of keyboard input, nPrimary is the ascii code, and nSecondary is 0.\r
251 // In the case of mouse input, nPrimary is xdelta, nSecondary is ydelta.\r
252 float CSQC_InputEvent(float bInputType, float nPrimary, float nSecondary)\r
253 {\r
254         local float bSkipKey;\r
255         bSkipKey = false;\r
256         \r
257         if(menu_visible)\r
258                 if(menu_action(bInputType, nPrimary, nSecondary))\r
259                         return TRUE;\r
260         return bSkipKey;\r
261 }\r
262 \r
263 // END REQUIRED CSQC FUNCTIONS\r
264 // --------------------------------------------------------------------------\r
265 \r
266 // --------------------------------------------------------------------------\r
267 // BEGIN OPTIONAL CSQC FUNCTIONS\r
268 void Ent_ReadONS()\r
269 {\r
270         entity gps;\r
271         using_gps = true;\r
272 \r
273         self.origin_x = ReadCoord();\r
274         self.origin_y = ReadCoord();\r
275         self.angles_y = ReadCoord();\r
276         self.origin_z = self.angles_x = self.angles_z = 0;\r
277 \r
278         for(gps = gps_start; gps; gps = gps.chain)\r
279         {\r
280                 if(gps == self)\r
281                         break;\r
282         }\r
283         if(!gps)\r
284         {\r
285                 self.chain = gps_start;\r
286                 gps_start = self;\r
287         }\r
288 }\r
289 \r
290 void Ent_RemoveONS()\r
291 {\r
292         if(gps_start == self)\r
293                 gps_start = self.chain;\r
294         else\r
295         {\r
296                 local entity ent;\r
297                 ent = gps_start;\r
298                         \r
299                 while(ent.chain != self && ent.chain != world)\r
300                         ent = ent.chain;\r
301                 if(ent.chain == self)\r
302                         ent.chain = self.chain;\r
303         }\r
304 }\r
305 \r
306 void Ent_ReadPlayerScore(float isNew)\r
307 {\r
308         float i, Team;\r
309         entity tm, o;\r
310 \r
311         // damnit -.- don't want to go change every single .sv_entnum in sbar.qc AGAIN\r
312         // (no I've never heard of M-x replace-string, sed, or anything like that)\r
313         isNew = !self.owner; // workaround for DP bug\r
314         self.sv_entnum = ReadByte()-1;\r
315         Team = GetPlayerColor(self.sv_entnum);\r
316 \r
317         if not(playerslots[self.sv_entnum])\r
318                 playerslots[self.sv_entnum] = spawn();\r
319         o = self.owner = playerslots[self.sv_entnum];\r
320         o.sv_entnum = self.sv_entnum;\r
321         o.gotscores = 1;\r
322         if not(o.sort_prev)\r
323                 RegisterPlayer(o);\r
324 \r
325         SetTeam(o, Team);\r
326 \r
327         for(i = 0; i < MAX_SCORE; ++i)\r
328                 o.(scores[i]) = ReadShort();\r
329 \r
330         Sbar_UpdatePlayerPos(o);\r
331 }\r
332 \r
333 void Ent_ReadTeamScore(float isNew)\r
334 {\r
335         float i;\r
336         entity o;\r
337         \r
338         self.team = ReadByte();\r
339         o = self.owner = GetTeam(self.team, true);\r
340 \r
341         for(i = 0; i < MAX_TEAMSCORE; ++i)\r
342                 o.(teamscores[i]) = ReadShort();\r
343 \r
344         Sbar_UpdateTeamPos(o);\r
345 }\r
346 \r
347 // CSQC_Ent_Update : Called every frame that the server has indicated an update to the SSQC / CSQC entity has occured.\r
348 // The only parameter reflects if the entity is "new" to the client, meaning it just came into the client's PVS.\r
349 void(float bIsNewEntity) CSQC_Ent_Update =\r
350 {\r
351         float msg;\r
352         self.enttype = ReadByte();\r
353         if(self.enttype == ENT_CLIENT_ENTCS)\r
354         {\r
355                 self.sv_entnum = ReadByte()-1;\r
356 \r
357                 for(msg = ReadByte(); msg != ENTCS_MSG_END; msg = ReadByte())\r
358                 {\r
359                         switch(msg)\r
360                         {\r
361                         case ENTCS_MSG_ONS_GPS: Ent_ReadONS(); break;\r
362                         case ENTCS_MSG_ONS_REMOVE: Ent_RemoveONS(); break;\r
363                         default:\r
364                                 error("unknown ENTCS_MSG type\n");\r
365                         }\r
366                 }\r
367         }\r
368         else if(self.enttype == ENT_CLIENT_SCORES)\r
369                 Ent_ReadPlayerScore(bIsNewEntity);\r
370         else if(self.enttype == ENT_CLIENT_TEAMSCORES)\r
371                 Ent_ReadTeamScore(bIsNewEntity);\r
372         else\r
373                 error("unknown entity type in CSQC_Ent_Update\n");\r
374         \r
375 };\r
376 // CSQC_Ent_Remove : Called when the server requests a SSQC / CSQC entity to be removed.  Essentially call remove(self) as well.\r
377 void CSQC_Ent_Remove()\r
378 {\r
379         if(self.enttype == ENT_CLIENT_ENTCS)\r
380         {\r
381                 if(using_gps) //gametype == GAME_ONSLAUGHT)\r
382                 {\r
383                         if(gps_start == self)\r
384                                 gps_start = self.chain;\r
385                         else\r
386                         {\r
387                                 local entity ent;\r
388                                 ent = gps_start;\r
389                         \r
390                                 while(ent.chain != self && ent.chain != world)\r
391                                         ent = ent.chain;\r
392                                 if(ent.chain == self)\r
393                                         ent.chain = self.chain;\r
394                         }\r
395                 }\r
396         } else if(self.enttype == ENT_CLIENT_SCORES)\r
397         {\r
398                 entity tm;\r
399                 if(self.owner)\r
400                 {\r
401                         SetTeam(self.owner, -1);\r
402                         RemovePlayer(self.owner);\r
403                         self.owner.sort_prev = NULL;\r
404                 }\r
405         } else if(self.enttype == ENT_CLIENT_TEAMSCORES)\r
406         {\r
407                 /*\r
408                 if(self.owner)\r
409                         RemoveTeam(self.owner);\r
410                 */\r
411                 // we don't NEED to remove them... they won't display anyway\r
412                 // plus, svqc never does this anyway\r
413         }\r
414         remove(self);\r
415 }\r
416 \r
417 void Gamemode_Init()\r
418 {\r
419         local string mapinfo, infoline;\r
420         local float len;\r
421         local float file;\r
422         local vector mi_min, mi_max;\r
423 \r
424         if(!strcasecmp(substring(mapname, 0, 5), "maps/"))\r
425                 shortmapname = substring(mapname, 5, 999);\r
426         else\r
427                 shortmapname = mapname;\r
428         len = strlen(shortmapname);\r
429         if(!strcasecmp(substring(shortmapname, len-4, 4), ".bsp"))\r
430                 shortmapname = substring(shortmapname, 0, len-4);\r
431         shortmapname = strzone(shortmapname);\r
432 \r
433         if(gametype == GAME_ONSLAUGHT) {\r
434                 mapinfo = strcat("maps/", minimapname, ".info");\r
435                 minimapname = strzone(strcat("gfx/", shortmapname, "_mini.tga"));\r
436 \r
437                 mi_min = world.mins;\r
438                 mi_max = world.maxs;\r
439                 \r
440                 file = fopen(mapinfo, FILE_READ);\r
441                 if(file >= 0) {\r
442                         while((infoline = fgets(file))) {\r
443                                 if(!strncasecmp(infoline, "mins", 4)) {\r
444                                         mi_min = stov(substring(infoline, 5, 999));\r
445                                 } else if(!strncasecmp(infoline, "maxs", 4)) {\r
446                                         mi_max = stov(substring(infoline, 5, 999));\r
447                                 } else if(strncasecmp(infoline, "//", 2)) { // don't print comment-style lines\r
448                                         print(strcat("mapinfo: ", infoline, "\n"));\r
449                                 }\r
450                         }\r
451                 } else {\r
452                         print(strcat("Map has no .info file (", mapinfo, ").\n"));\r
453                 }\r
454                 fclose(file);\r
455 \r
456                 print(strcat("Mins: ", vtos(mi_min), "    Maxs: ", vtos(mi_max), "\n"));\r
457                 \r
458                 mi_center = (mi_min + mi_max) * 0.5;\r
459                 mi_scale = mi_max - mi_min;\r
460                 \r
461                 \r
462                 print(strcat("Using ", minimapname, " as minimap.\n"));\r
463                 precache_pic(minimapname);\r
464                 precache_pic("gfx/ons-cp-neutral.tga");\r
465                 precache_pic("gfx/ons-cp-red.tga");\r
466                 precache_pic("gfx/ons-cp-blue.tga");\r
467                 precache_pic("gfx/ons-frame.tga");\r
468                 precache_pic("gfx/ons-frame-team.tga");\r
469         } else if(gametype == GAME_KEYHUNT) {\r
470                 precache_pic("gfx/sb_key_carrying");\r
471                 precache_pic("gfx/sb_key_carrying_outline");\r
472         }\r
473 }\r
474 // 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
475 void CSQC_Parse_StuffCmd(string strMessage)\r
476 {\r
477         localcmd(strMessage);\r
478 }\r
479 // 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
480 void CSQC_Parse_Print(string strMessage)\r
481 {\r
482         print(strMessage);\r
483 }\r
484 // 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
485 void CSQC_Parse_CenterPrint(string strMessage)\r
486 {\r
487         cprint(strMessage);\r
488 }\r
489 \r
490 void CSQC_CheckRevision();\r
491 \r
492 void Gamemode_Init();\r
493 void Net_ReadScoresInfo()\r
494 {\r
495         float i;\r
496         gametype = ReadByte();\r
497         for(i = 0; i < MAX_SCORE; ++i)\r
498         {\r
499                 scores_label[i] = strzone(ReadString());\r
500                 scores_flags[i] = ReadByte();\r
501         }\r
502         for(i = 0; i < MAX_TEAMSCORE; ++i)\r
503         {\r
504                 teamscores_label[i] = strzone(ReadString());\r
505                 teamscores_flags[i] = ReadByte();\r
506         }\r
507         Sbar_InitScores();\r
508         Gamemode_Init();\r
509 }\r
510 \r
511 void Net_ReadInit()\r
512 {\r
513         csqc_revision = ReadShort();\r
514         maxclients = ReadByte();\r
515 \r
516         CSQC_CheckRevision();\r
517 }\r
518 \r
519 string Net_ReadPicture()\r
520 {\r
521         string img;\r
522         if(csqc_flags & CSQC_FLAG_READPICTURE)\r
523         {\r
524                 img = ReadPicture();\r
525                 print(strcat("Got Picture: ", img, "\n"));\r
526         } else {\r
527                 img = ReadString();\r
528                 print(strcat("^3Warning: ^7Couldn't download ", img, ". This is probably because your engine build is outdated.\n"));\r
529                 float psize, i;\r
530                 psize = ReadShort();\r
531                 // Can I be sure that ReadShort is 2 bytes and ReadLong is 4 bytes?\r
532                 // Because then this could be optimized to first get all 4-byte-groups,\r
533                 // then the remaining 2, then the remaining 1\r
534                 for(i = 0; i < psize; ++i)\r
535                         ReadByte();\r
536         }\r
537         return img;\r
538 }\r
539 \r
540 void Net_Config()\r
541 {\r
542         string key, value;\r
543         key = ReadString();\r
544         value = ReadString();\r
545         db_put(configdb, strcat("/v/", key), value);\r
546         db_put(configdb, strcat("/s/", key), "1");\r
547 }\r
548 \r
549 // CSQC_Parse_TempEntity : Handles all temporary entity network data in the CSQC layer.\r
550 // You must ALWAYS first acquire the temporary ID, which is sent as a byte.\r
551 // Return value should be 1 if CSQC handled the temporary entity, otherwise return 0 to have the engine process the event.\r
552 float CSQC_Parse_TempEntity()\r
553 {\r
554         local float bHandled;\r
555                 bHandled  = true;\r
556         // Acquire TE ID\r
557         local float nTEID;\r
558                 nTEID = ReadByte();\r
559 \r
560                 // NOTE: Could just do return instead of break...\r
561         switch(nTEID)\r
562         {\r
563                 case TE_CSQC_INIT:\r
564                         Net_ReadInit();\r
565                         bHandled = true;\r
566                         break;\r
567                 case TE_CSQC_MAPVOTE:\r
568                         Net_Mapvote();\r
569                         bHandled = true;\r
570                         break;\r
571                 case TE_CSQC_CONFIG:\r
572                         Net_Config();\r
573                         bHandled = true;\r
574                         break;\r
575                 case TE_CSQC_SCORESINFO:\r
576                         Net_ReadScoresInfo();\r
577                         bHandled = true;\r
578                         break;\r
579                 default:\r
580                         // No special logic for this temporary entity; return 0 so the engine can handle it\r
581                         bHandled = false;\r
582                         break;\r
583         }\r
584         \r
585         if(!postinit)\r
586                 PostInit();\r
587                 \r
588         return bHandled;\r
589 }\r
590 \r
591 // COMMIT-TODO: Update if necessare, before committing\r
592 float csqc_svn_map[CSQC_REVISION] =\r
593 {\r
594         3812, // 3795,\r
595         3820 // mapvote protocol changed from there\r
596 };\r
597 \r
598 // COMMIT-TODO: Update if necessare, before committing\r
599 void CSQC_CheckRevision()\r
600 {\r
601         if(csqc_revision == CSQC_REVISION)\r
602         {\r
603                 print("^2SVQC and CSQC revisions are compatible.\n");\r
604         } else if(csqc_revision < CSQC_REVISION) {\r
605                 print("^1Your csprogs.dat (CSQC) version is newer than the one on the server.\n");\r
606                 print("^1The last known svn revision for the server's CSQC is: ^7");\r
607                 print(ftos(csqc_svn_map[csqc_revision])); // don't use strcat, fteqcc loves screwing up arrays...\r
608                 print("\n");\r
609         } else if(csqc_revision > CSQC_REVISION) {\r
610                 print("^1Your csprogs.dat (CSQC) is too old for this server.\n");\r
611                 print("^1Please update to a newer version.\n");\r
612         }\r
613 }\r