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