]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/Main.qc
Show if a map has Onslaught support in mapinfo dialog (not only if developer 1)
[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', "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 void CSQC_Init(void)\r
25 {\r
26 #ifdef USE_FTE\r
27 #pragma target ID\r
28         __engine_check = checkextension("DP_SV_WRITEPICTURE");\r
29         if(!__engine_check)\r
30         {\r
31                 print("^3Your engine build is outdated\n^3This Server uses a newer QC VM. Please update!\n");\r
32                 localcmd("\ndisconnect\n");\r
33                 return;\r
34         }\r
35 #pragma target FTE\r
36 #endif\r
37         \r
38         float i;\r
39         CSQC_CheckEngine();\r
40         drawfont = 0;\r
41         menu_visible = FALSE;\r
42         menu_show = menu_show_error;\r
43         menu_action = menu_sub_null;\r
44         using_gps = false;\r
45         //ctf_temp_1 = "";\r
46         localcmd("alias order \"cmd order $*\"");\r
47         //registercmd("ctf_menu");\r
48         registercmd("ons_map");\r
49         //registercmd("menu_action");\r
50         registercmd("sbar_columns_set");\r
51         registercmd("sbar_columns_help");\r
52 \r
53         registercvar("sbar_usecsqc", "1");\r
54         registercvar("sbar_columns", "ping name | caps returns frags deaths", CVAR_SAVE);\r
55 \r
56         gametype = 0;\r
57 \r
58         gps_start = world;\r
59 \r
60         // sbar_fields uses strunzone on the titles!\r
61         for(i = 0; i < MAX_SBAR_FIELDS; ++i)\r
62                 sbar_title[i] = strzone("(null)");\r
63 \r
64         postinit = false;\r
65 \r
66         Sbar_Init();\r
67 }\r
68 \r
69 // CSQC_Shutdown : Called every time the CSQC code is shutdown (changing maps, quitting, etc)\r
70 void CSQC_Shutdown(void)\r
71 {\r
72 #ifdef USE_FTE\r
73 #pragma TARGET id\r
74         if(!__engine_check)\r
75                 return 0;\r
76 #pragma TARGET fte\r
77 #endif\r
78         buf_del(databuf);\r
79 }\r
80 \r
81 void PostInit(void)\r
82 {\r
83         float i;\r
84 \r
85         print(strcat("PostInit\n    maxclients = ", ftos(maxclients), "\n"));\r
86         databuf = buf_create();\r
87         for(i = 0; i < maxclients; ++i)\r
88         {\r
89                 bufstr_set(databuf, DATABUF_PING + i, "N/A");\r
90                 bufstr_set(databuf, DATABUF_DEATHS + i, "0");\r
91                 bufstr_set(databuf, DATABUF_CAPTURES + i, "0");\r
92                 bufstr_set(databuf, DATABUF_RETURNS + i, "0");\r
93         }\r
94         \r
95         localcmd(strcat("\nsbar_columns_set ", cvar_string("sbar_columns"), ";\n"));\r
96 \r
97         postinit = true;\r
98 }\r
99 \r
100 // CSQC_ConsoleCommand : Used to parse commands in the console that have been registered with the "registercmd" function\r
101 // Return value should be 1 if CSQC handled the command, otherwise return 0 to have the engine handle it.\r
102 void Cmd_Sbar_SetFields(float);\r
103 void Cmd_Sbar_Help(float);\r
104 float CSQC_ConsoleCommand(string strMessage)\r
105 {\r
106         float argc;\r
107         // Tokenize String\r
108         argc = tokenize(strMessage);\r
109         \r
110         // Acquire Command\r
111         local string strCmd;\r
112         strCmd = argv(0);\r
113 \r
114         /*if(strCmd == "ctf_menu") {\r
115                 ctf_menu_show();\r
116                 nReturn = true;\r
117                 } else*/\r
118         if(strCmd == "ons_map") {\r
119                 Cmd_ons_map();\r
120                 return true;\r
121         } else if(strCmd == "sbar_columns_set") {\r
122                 Cmd_Sbar_SetFields(argc);\r
123                 return true;\r
124         } else if(strCmd == "sbar_columns_help") {\r
125                 Cmd_Sbar_Help(argc);\r
126                 return true;\r
127         }\r
128         \r
129         return false;\r
130 }\r
131 \r
132 float GameCommand(string msg)\r
133 {\r
134         float argc;\r
135         argc = tokenize(msg);\r
136         string cmd;\r
137         cmd = argv(0);\r
138         if(cmd == "mv_download") {\r
139                 Cmd_MapVote_MapDownload(argc);\r
140                 return true;\r
141         }\r
142         \r
143         return false;\r
144 }\r
145 \r
146 // CSQC_InputEvent : Used to perform actions based on any key pressed, key released and mouse on the client.\r
147 // Return value should be 1 if CSQC handled the input, otherwise return 0 to have the input passed to the engine.\r
148 // All keys are in ascii.\r
149 // bInputType = 0 is key pressed, 1 is key released, 2 is mouse input.\r
150 // In the case of keyboard input, nPrimary is the ascii code, and nSecondary is 0.\r
151 // In the case of mouse input, nPrimary is xdelta, nSecondary is ydelta.\r
152 float CSQC_InputEvent(float bInputType, float nPrimary, float nSecondary)\r
153 {\r
154         local float bSkipKey;\r
155         bSkipKey = false;\r
156         if(menu_visible)\r
157                 if(menu_action(bInputType, nPrimary, nSecondary))\r
158                         return TRUE;\r
159         return bSkipKey;\r
160 }\r
161 \r
162 // END REQUIRED CSQC FUNCTIONS\r
163 // --------------------------------------------------------------------------\r
164 \r
165 // --------------------------------------------------------------------------\r
166 // BEGIN OPTIONAL CSQC FUNCTIONS\r
167 void ReadONS()\r
168 {\r
169         entity gps;\r
170         using_gps = true;\r
171 \r
172         self.origin_x = ReadCoord();\r
173         self.origin_y = ReadCoord();\r
174         self.angles_y = ReadCoord();\r
175         self.origin_z = self.angles_x = self.angles_z = 0;\r
176 \r
177         for(gps = gps_start; gps; gps = gps.chain)\r
178         {\r
179                 if(gps == self)\r
180                         break;\r
181         }\r
182         if(!gps)\r
183         {\r
184                 self.chain = gps_start;\r
185                 gps_start = self;\r
186         }\r
187 }\r
188 \r
189 void RemoveONS()\r
190 {\r
191         if(gps_start == self)\r
192                 gps_start = self.chain;\r
193         else\r
194         {\r
195                 local entity ent;\r
196                 ent = gps_start;\r
197                         \r
198                 while(ent.chain != self && ent.chain != world)\r
199                         ent = ent.chain;\r
200                 if(ent.chain == self)\r
201                         ent.chain = self.chain;\r
202         }\r
203 }\r
204 \r
205 // CSQC_Ent_Update : Called every frame that the server has indicated an update to the SSQC / CSQC entity has occured.\r
206 // The only parameter reflects if the entity is "new" to the client, meaning it just came into the client's PVS.\r
207 void(float bIsNewEntity) CSQC_Ent_Update =\r
208 {\r
209         float msg;\r
210         self.enttype = ReadByte();\r
211         if(self.enttype == ENT_CLIENT_ENTCS)\r
212         {\r
213                 self.sv_entnum = ReadByte()-1;\r
214 \r
215                 for(msg = ReadByte(); msg != ENTCS_MSG_END; msg = ReadByte())\r
216                 {\r
217                         switch(msg)\r
218                         {\r
219                         case ENTCS_MSG_ONS_GPS: ReadONS(); break;\r
220                         case ENTCS_MSG_ONS_REMOVE: RemoveONS(); break;\r
221                         default:\r
222                                 error("unknown ENTCS_MSG type\n");\r
223                         }\r
224                 }\r
225         }\r
226         else\r
227                 error("unknown entity type in CSQC_Ent_Update\n");\r
228         \r
229 };\r
230 // CSQC_Ent_Remove : Called when the server requests a SSQC / CSQC entity to be removed.  Essentially call remove(self) as well.\r
231 void CSQC_Ent_Remove()\r
232 {\r
233         if(self.enttype == ENT_CLIENT_ENTCS)\r
234         {\r
235                 if(using_gps) //gametype == GAME_ONSLAUGHT)\r
236                 {\r
237                         if(gps_start == self)\r
238                                 gps_start = self.chain;\r
239                         else\r
240                         {\r
241                                 local entity ent;\r
242                                 ent = gps_start;\r
243                         \r
244                                 while(ent.chain != self && ent.chain != world)\r
245                                         ent = ent.chain;\r
246                                 if(ent.chain == self)\r
247                                         ent.chain = self.chain;\r
248                         }\r
249                 }\r
250         }\r
251         remove(self);\r
252 }\r
253 \r
254 void Gamemode_Init()\r
255 {\r
256         local string mapinfo, infoline;\r
257         local float len;\r
258         local float file;\r
259         local vector mi_min, mi_max;\r
260 \r
261         gametype = cvar("gametype");\r
262         if(gametype == GAME_ONSLAUGHT) {\r
263                 if(!strcasecmp(substring(mapname, 0, 5), "maps/"))\r
264                         minimapname = substring(mapname, 5, 999);\r
265                 else\r
266                         minimapname = mapname;\r
267                 len = strlen(minimapname);\r
268                 if(!strcasecmp(substring(minimapname, len-4, 4), ".bsp"))\r
269                         minimapname = substring(minimapname, 0, len-4);\r
270                 \r
271                 mapinfo = strcat("maps/", minimapname, ".info");\r
272                 minimapname = strzone(strcat("gfx/", minimapname, "_mini.tga"));\r
273 \r
274                 mi_min = world.mins;\r
275                 mi_max = world.maxs;\r
276                 \r
277                 file = fopen(mapinfo, FILE_READ);\r
278                 if(file >= 0) {\r
279                         while((infoline = fgets(file))) {\r
280                                 if(!strncasecmp(infoline, "mins", 4)) {\r
281                                         mi_min = stov(substring(infoline, 5, 999));\r
282                                 } else if(!strncasecmp(infoline, "maxs", 4)) {\r
283                                         mi_max = stov(substring(infoline, 5, 999));\r
284                                 } else if(strncasecmp(infoline, "//", 2)) { // don't print comment-style lines\r
285                                         print(strcat("mapinfo: ", infoline, "\n"));\r
286                                 }\r
287                         }\r
288                 } else {\r
289                         print(strcat("Map has no .info file (", mapinfo, ").\n"));\r
290                 }\r
291                 fclose(file);\r
292 \r
293                 print(strcat("Mins: ", vtos(mi_min), "    Maxs: ", vtos(mi_max), "\n"));\r
294                 \r
295                 mi_center = (mi_min + mi_max) * 0.5;\r
296                 mi_scale = mi_max - mi_min;\r
297                 \r
298                 \r
299                 print(strcat("Using ", minimapname, " as minimap.\n"));\r
300                 precache_pic(minimapname);\r
301                 precache_pic("gfx/ons-cp-neutral.tga");\r
302                 precache_pic("gfx/ons-cp-red.tga");\r
303                 precache_pic("gfx/ons-cp-blue.tga");\r
304                 precache_pic("gfx/ons-frame.tga");\r
305                 precache_pic("gfx/ons-frame-team.tga");\r
306         } else if(gametype == GAME_KEYHUNT) {\r
307                 precache_pic("gfx/sb_key_carrying");\r
308                 precache_pic("gfx/sb_key_carrying_outline");\r
309         }\r
310 }\r
311 // 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
312 void CSQC_Parse_StuffCmd(string strMessage)\r
313 {\r
314         localcmd(strMessage);\r
315         // watch for gametype changes!\r
316         if(gametype != cvar("gametype"))\r
317         {\r
318                 Gamemode_Init();\r
319         }\r
320 }\r
321 // 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
322 void CSQC_Parse_Print(string strMessage)\r
323 {\r
324         print(strMessage);\r
325 }\r
326 // 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
327 void CSQC_Parse_CenterPrint(string strMessage)\r
328 {\r
329         cprint(strMessage);\r
330 }\r
331 \r
332 void CSQC_CheckRevision();\r
333 \r
334 void Net_ReadInit()\r
335 {\r
336         csqc_revision = ReadShort();\r
337         maxclients = ReadByte();\r
338 \r
339         CSQC_CheckRevision();\r
340 }\r
341 \r
342 void Net_ReadPings()\r
343 {\r
344         float plnum, ping;\r
345         for(plnum = ReadByte(); plnum != 0; plnum = ReadByte())\r
346         {\r
347                 ping = ReadShort();\r
348                 bufstr_set(databuf, DATABUF_PING + plnum-1, ftos(ping));\r
349         }\r
350 }\r
351 \r
352 void Net_ReadCaptures()\r
353 {\r
354         float plnum, caps, mode;\r
355         mode = ReadByte();\r
356         caps_team1 = ReadByte();\r
357         caps_team2 = ReadByte();\r
358         for(plnum = ReadByte(); plnum != 0; plnum = ReadByte())\r
359         {\r
360                 caps = ReadByte();\r
361                 bufstr_set(databuf, DATABUF_CAPTURES + plnum-1, ftos(caps));\r
362         }\r
363 }\r
364 \r
365 void Net_ReadDatabuf(float ofs)\r
366 {\r
367         float plnum, data;\r
368         for(plnum = ReadByte(); plnum != 0; plnum = ReadByte())\r
369         {\r
370                 data = ReadByte();\r
371                 bufstr_set(databuf, ofs + plnum-1, ftos(data));\r
372         }\r
373 }\r
374 \r
375 string Net_ReadPicture()\r
376 {\r
377         string img;\r
378         if(csqc_flags & CSQC_FLAG_READPICTURE)\r
379         {\r
380                 img = ReadPicture();\r
381                 print(strcat("Got Picture: ", img, "\n"));\r
382         } else {\r
383                 img = ReadString();\r
384                 print(strcat("^3Warning: ^7Couldn't download ", img, ". This is probably because your engine build is outdated.\n"));\r
385                 float psize, i;\r
386                 psize = ReadShort();\r
387                 // Can I be sure that ReadShort is 2 bytes and ReadLong is 4 bytes?\r
388                 // Because then this could be optimized to first get all 4-byte-groups,\r
389                 // then the remaining 2, then the remaining 1\r
390                 for(i = 0; i < psize; ++i)\r
391                         ReadByte();\r
392         }\r
393         return img;\r
394 }\r
395 \r
396 // CSQC_Parse_TempEntity : Handles all temporary entity network data in the CSQC layer.\r
397 // You must ALWAYS first acquire the temporary ID, which is sent as a byte.\r
398 // Return value should be 1 if CSQC handled the temporary entity, otherwise return 0 to have the engine process the event.\r
399 float CSQC_Parse_TempEntity()\r
400 {\r
401         local float bHandled;\r
402                 bHandled  = true;\r
403         // Acquire TE ID\r
404         local float nTEID;\r
405                 nTEID = ReadByte();\r
406 \r
407                 // NOTE: Could just do return instead of break...\r
408         switch(nTEID)\r
409         {\r
410                 case TE_CSQC_INIT:\r
411                         Net_ReadInit();\r
412                         bHandled = true;\r
413                         break;\r
414                 case TE_CSQC_PING:\r
415                         Net_ReadPings();\r
416                         bHandled = true;\r
417                         break;\r
418                 case TE_CSQC_CAPTURES:\r
419                         Net_ReadCaptures();\r
420                         bHandled = true;\r
421                         break;\r
422                 case TE_CSQC_RETURNS:\r
423                         Net_ReadDatabuf(DATABUF_RETURNS);\r
424                         bHandled = true;\r
425                         break;\r
426                 case TE_CSQC_DEATHS:\r
427                         Net_ReadDatabuf(DATABUF_DEATHS);\r
428                         bHandled = true;\r
429                         break;\r
430                 case TE_CSQC_MAPVOTE:\r
431                         Net_Mapvote();\r
432                         bHandled = true;\r
433                         break;\r
434                 \r
435                 default:\r
436                         // No special logic for this temporary entity; return 0 so the engine can handle it\r
437                         bHandled = false;\r
438                         break;\r
439         }\r
440         \r
441         if(!postinit)\r
442                 PostInit();\r
443                 \r
444         return bHandled;\r
445 }\r
446 \r
447 // COMMIT-TODO: Update if necessare, before committing\r
448 float csqc_svn_map[CSQC_REVISION] =\r
449 {\r
450         3812, // 3795,\r
451         3820 // mapvote protocol changed from there\r
452 };\r
453 \r
454 // COMMIT-TODO: Update if necessare, before committing\r
455 void CSQC_CheckRevision()\r
456 {\r
457         if(csqc_revision == CSQC_REVISION)\r
458         {\r
459                 print("^2SVQC and CSQC revisions are compatible.\n");\r
460         } else if(csqc_revision < CSQC_REVISION) {\r
461                 print("^1Your csprogs.dat (CSQC) version is newer than the one on the server.\n");\r
462                 print("^1The last known svn revision for the server's CSQC is: ^7");\r
463                 print(ftos(csqc_svn_map[csqc_revision])); // don't use strcat, fteqcc loves screwing up arrays...\r
464                 print("\n");\r
465         } else if(csqc_revision > CSQC_REVISION) {\r
466                 print("^1Your csprogs.dat (CSQC) is too old for this server.\n");\r
467                 print("^1Please update to a newer version.\n");\r
468         }\r
469 }\r