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