]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/client/Main.qc
Added the capture-counter and changed the CTF scoreboard to display the caps.
[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 databuf;\r
19 float using_gps;\r
20 \r
21 void CSQC_Init(void)\r
22 {\r
23         drawfont = 0;\r
24         menu_visible = FALSE;\r
25         menu_show = menu_show_error;\r
26         menu_action = menu_sub_null;\r
27         using_gps = false;\r
28         //ctf_temp_1 = "";\r
29         localcmd("alias order \"cmd order $*\"");\r
30         //registercmd("ctf_menu");\r
31         registercmd("ons_map");\r
32         //registercmd("menu_action");\r
33 \r
34         registercvar("sbar_usecsqc", "1");\r
35 \r
36         gametype = 0;\r
37 \r
38         gps_start = world;\r
39 \r
40         postinit = false;\r
41 }\r
42 \r
43 void PostInit(void)\r
44 {\r
45         float i;\r
46         entity pl;\r
47 \r
48         print(strcat("PostInit\n    maxclients = ", ftos(maxclients), "\n"));\r
49         databuf = buf_create();\r
50         for(i = 0; i < maxclients; ++i)\r
51         {\r
52                 bufstr_set(databuf, DATABUF_PING + i, "N/A");\r
53                 bufstr_set(databuf, DATABUF_CAPTURES + i, "0");\r
54         }\r
55 \r
56         postinit = true;\r
57 }\r
58 // CSQC_Shutdown : Called every time the CSQC code is shutdown (changing maps, quitting, etc)\r
59 void CSQC_Shutdown(void)\r
60 {\r
61         buf_del(databuf);\r
62 }\r
63 \r
64 // CSQC_ConsoleCommand : Used to parse commands in the console that have been registered with the "registercmd" function\r
65 // Return value should be 1 if CSQC handled the command, otherwise return 0 to have the engine handle it.\r
66 float CSQC_ConsoleCommand(string strMessage)\r
67 {\r
68         local float nReturn;\r
69                 nReturn = FALSE;\r
70                 \r
71         // Tokenize String\r
72         tokenize(strMessage);\r
73         \r
74         // Acquire Command\r
75         local string strCmd;\r
76         strCmd = argv(0);\r
77 \r
78         /*if(strCmd == "ctf_menu") {\r
79                 ctf_menu_show();\r
80                 nReturn = TRUE;\r
81                 } else*/\r
82         if(strCmd == "ons_map") {\r
83                 Cmd_ons_map();\r
84                 nReturn = TRUE;\r
85         }\r
86         \r
87         return nReturn;\r
88 }\r
89 // CSQC_InputEvent : Used to perform actions based on any key pressed, key released and mouse on the client.\r
90 // Return value should be 1 if CSQC handled the input, otherwise return 0 to have the input passed to the engine.\r
91 // All keys are in ascii.\r
92 // bInputType = 0 is key pressed, 1 is key released, 2 is mouse input.\r
93 // In the case of keyboard input, nPrimary is the ascii code, and nSecondary is 0.\r
94 // In the case of mouse input, nPrimary is xdelta, nSecondary is ydelta.\r
95 float CSQC_InputEvent(float bInputType, float nPrimary, float nSecondary)\r
96 {\r
97         local float bSkipKey;\r
98         bSkipKey = false;\r
99         if(menu_visible)\r
100                 if(menu_action(bInputType, nPrimary, nSecondary))\r
101                         return TRUE;\r
102         return bSkipKey;\r
103 }\r
104 \r
105 // END REQUIRED CSQC FUNCTIONS\r
106 // --------------------------------------------------------------------------\r
107 \r
108 // --------------------------------------------------------------------------\r
109 // BEGIN OPTIONAL CSQC FUNCTIONS\r
110 void ReadONS()\r
111 {\r
112         entity gps;\r
113         using_gps = true;\r
114 \r
115         self.origin_x = ReadCoord();\r
116         self.origin_y = ReadCoord();\r
117         self.angles_y = ReadCoord();\r
118         self.origin_z = self.angles_x = self.angles_z = 0;\r
119 \r
120         for(gps = gps_start; gps; gps = gps.chain)\r
121         {\r
122                 if(gps == self)\r
123                         break;\r
124         }\r
125         if(!gps)\r
126         {\r
127                 self.chain = gps_start;\r
128                 gps_start = self;\r
129         }\r
130 }\r
131 \r
132 void RemoveONS()\r
133 {\r
134         if(gps_start == self)\r
135                 gps_start = self.chain;\r
136         else\r
137         {\r
138                 local entity ent;\r
139                 ent = gps_start;\r
140                         \r
141                 while(ent.chain != self && ent.chain != world)\r
142                         ent = ent.chain;\r
143                 if(ent.chain == self)\r
144                         ent.chain = self.chain;\r
145         }\r
146 }\r
147 \r
148 // CSQC_Ent_Update : Called every frame that the server has indicated an update to the SSQC / CSQC entity has occured.\r
149 // The only parameter reflects if the entity is "new" to the client, meaning it just came into the client's PVS.\r
150 void(float bIsNewEntity) CSQC_Ent_Update =\r
151 {\r
152         float msg;\r
153         self.enttype = ReadByte();\r
154         if(self.enttype == ENT_CLIENT_ENTCS)\r
155         {\r
156                 self.sv_entnum = ReadByte()-1;\r
157 \r
158                 for(msg = ReadByte(); msg != ENTCS_MSG_END; msg = ReadByte())\r
159                 {\r
160                         switch(msg)\r
161                         {\r
162                         case ENTCS_MSG_ONS_GPS: ReadONS(); break;\r
163                         case ENTCS_MSG_ONS_REMOVE: RemoveONS(); break;\r
164                         default:\r
165                                 error("unknown ENTCS_MSG type\n");\r
166                         }\r
167                 }\r
168         }\r
169         else\r
170                 error("unknown entity type in CSQC_Ent_Update\n");\r
171         \r
172 };\r
173 // CSQC_Ent_Remove : Called when the server requests a SSQC / CSQC entity to be removed.  Essentially call remove(self) as well.\r
174 void CSQC_Ent_Remove()\r
175 {\r
176         if(self.enttype == ENT_CLIENT_ENTCS)\r
177         {\r
178                 if(using_gps) //gametype == GAME_ONSLAUGHT)\r
179                 {\r
180                         if(gps_start == self)\r
181                                 gps_start = self.chain;\r
182                         else\r
183                         {\r
184                                 local entity ent;\r
185                                 ent = gps_start;\r
186                         \r
187                                 while(ent.chain != self && ent.chain != world)\r
188                                         ent = ent.chain;\r
189                                 if(ent.chain == self)\r
190                                         ent.chain = self.chain;\r
191                         }\r
192                 }\r
193         }\r
194         remove(self);\r
195 }\r
196 \r
197 void Gamemode_Init()\r
198 {\r
199         local string mapinfo, infoline;\r
200         local float len;\r
201         local float file;\r
202         local vector mi_min, mi_max;\r
203 \r
204         draw_enginesbar = true;\r
205         gametype = cvar("gametype");\r
206         if(gametype == GAME_ONSLAUGHT) {\r
207                 if(!strcasecmp(substring(mapname, 0, 5), "maps/"))\r
208                         minimapname = substring(mapname, 5, 999);\r
209                 else\r
210                         minimapname = mapname;\r
211                 len = strlen(minimapname);\r
212                 if(!strcasecmp(substring(minimapname, len-4, 4), ".bsp"))\r
213                         minimapname = substring(minimapname, 0, len-4);\r
214                 \r
215                 mapinfo = strcat("maps/", minimapname, ".info");\r
216                 minimapname = strzone(strcat("gfx/", minimapname, "_mini.tga"));\r
217 \r
218                 mi_min = world.mins;\r
219                 mi_max = world.maxs;\r
220                 \r
221                 file = fopen(mapinfo, FILE_READ);\r
222                 if(file >= 0) {\r
223                         while((infoline = fgets(file))) {\r
224                                 if(!strncasecmp(infoline, "mins", 4)) {\r
225                                         mi_min = stov(substring(infoline, 5, 999));\r
226                                 } else if(!strncasecmp(infoline, "maxs", 4)) {\r
227                                         mi_max = stov(substring(infoline, 5, 999));\r
228                                 } else if(strncasecmp(infoline, "//", 2)) { // don't print comment-style lines\r
229                                         print(strcat("mapinfo: ", infoline, "\n"));\r
230                                 }\r
231                         }\r
232                 } else {\r
233                         print(strcat("^1Error: ^7Mapinfo file '", mapinfo, "' missing! Minimap will be screwed.\n"));\r
234                 }\r
235                 fclose(file);\r
236 \r
237                 print(strcat("Mins: ", vtos(mi_min), "    Maxs: ", vtos(mi_max), "\n"));\r
238                 \r
239                 mi_center = (mi_min + mi_max) * 0.5;\r
240                 mi_scale = mi_max - mi_min;\r
241                 \r
242                 \r
243                 print(strcat("Using ", minimapname, " as minimap.\n"));\r
244                 precache_pic(minimapname);\r
245                 precache_pic("gfx/ons-cp-neutral.tga");\r
246                 precache_pic("gfx/ons-cp-red.tga");\r
247                 precache_pic("gfx/ons-cp-blue.tga");\r
248                 precache_pic("gfx/ons-frame.tga");\r
249                 precache_pic("gfx/ons-frame-team.tga");\r
250         } else if(gametype == GAME_KEYHUNT) {\r
251                 precache_pic("gfx/sb_key_carrying");\r
252                 precache_pic("gfx/sb_key_carrying_outline");\r
253                 draw_enginesbar = false;\r
254         }\r
255 }\r
256 // 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
257 void CSQC_Parse_StuffCmd(string strMessage)\r
258 {\r
259         localcmd(strMessage);\r
260         // watch for gametype changes!\r
261         if(gametype != cvar("gametype"))\r
262         {\r
263                 Gamemode_Init();\r
264         }\r
265 }\r
266 // 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
267 void CSQC_Parse_Print(string strMessage)\r
268 {\r
269         print(strMessage);\r
270 }\r
271 // 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
272 void CSQC_Parse_CenterPrint(string strMessage)\r
273 {\r
274         cprint(strMessage);\r
275 }\r
276 \r
277 void ReadInit()\r
278 {\r
279         maxclients = ReadByte();\r
280 }\r
281 \r
282 void ReadPings()\r
283 {\r
284         float plnum, ping;\r
285         for(plnum = ReadByte(); plnum != 0; plnum = ReadByte())\r
286         {\r
287                 ping = ReadShort();\r
288                 bufstr_set(databuf, DATABUF_PING + plnum-1, ftos(ping));\r
289         }\r
290 }\r
291 \r
292 void ReadCaptures()\r
293 {\r
294         float plnum, caps;\r
295         entity pl;\r
296         caps_team1 = ReadByte();\r
297         caps_team2 = ReadByte();\r
298         for(plnum = ReadByte(); plnum != 0; plnum = ReadByte())\r
299         {\r
300                 caps = ReadByte();\r
301                 //print(strcat("Cap update: ", ftos(plnum), " has ", ftos(caps), " caps\n"));\r
302                 //print(strcat("Index: ", ftos(DATABUF_CAPTURES + plnum-1), " -- I AM: ", ftos(player_localentnum), "\n"));\r
303                 bufstr_set(databuf, DATABUF_CAPTURES + plnum-1, ftos(caps));\r
304         }\r
305 }\r
306 \r
307 // CSQC_Parse_TempEntity : Handles all temporary entity network data in the CSQC layer.\r
308 // You must ALWAYS first acquire the temporary ID, which is sent as a byte.\r
309 // Return value should be 1 if CSQC handled the temporary entity, otherwise return 0 to have the engine process the event.\r
310 float CSQC_Parse_TempEntity()\r
311 {\r
312         local float bHandled;\r
313                 bHandled  = true;\r
314         // Acquire TE ID\r
315         local float nTEID;\r
316                 nTEID = ReadByte();\r
317                 \r
318         switch(nTEID)\r
319         {\r
320                 case TE_CSQC_INIT:\r
321                         ReadInit();\r
322                         bHandled = true;\r
323                         break;\r
324                 case TE_CSQC_PING:\r
325                         ReadPings();\r
326                         bHandled = true;\r
327                         break;\r
328                 case TE_CSQC_CAPTURES:\r
329                         ReadCaptures();\r
330                         bHandled = true;\r
331                         break;\r
332                 default:\r
333                         // No special logic for this temporary entity; return 0 so the engine can handle it\r
334                         bHandled = false;\r
335                         break;\r
336         }\r
337         \r
338         if(!postinit)\r
339                 PostInit();\r
340                 \r
341         return bHandled;\r
342 }\r