]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/notifications.qc
Let Local_Notification handle that check
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / notifications.qc
1 // ================================================
2 //  Unified notification system, written by Samual
3 //  Last updated: February, 2013
4 // ================================================
5
6 #ifndef MENUQC
7 entity Get_Notif_Ent(float net_type, float net_name)
8 {
9         switch(net_type)
10         {
11                 case MSG_INFO: return msg_info_notifs[net_name - 1];
12                 case MSG_CENTER: return msg_center_notifs[net_name - 1];
13                 case MSG_WEAPON: return msg_weapon_notifs[net_name - 1];
14                 case MSG_DEATH: return msg_death_notifs[net_name - 1];
15         }
16         backtrace(sprintf("Get_Notif_Ent(%d, %d): Improper net type!\n", net_type, net_name));
17         return world;
18 }
19 #endif // ifndef MENUQC
20
21
22 // ===============================
23 //  Frontend Notification Pushing
24 // ===============================
25
26 void Dump_Notifications(float fh, float alsoprint)
27 {
28         float MSG_INFO_NOTIFS = 0, MSG_CENTER_NOTIFS = 0, MSG_WEAPON_NOTIFS = 0, MSG_DEATH_NOTIFS = 0;
29
30         #define NOTIF_WRITE(type,name,text) { \
31                 ++##type##_NOTIFS; \
32                 notif_msg = sprintf("seta %s 1 // %s - %s\n", name, #type, strreplace("\n", "\\n", text)); \
33                 fputs(fh, notif_msg); \
34                 if(alsoprint) { print(strreplace("^", "^^", notif_msg)); } }
35
36         #ifndef MENUQC
37         string notif_msg;
38         float i;
39         entity e;
40
41         for(i = 0; i < NOTIF_INFO_COUNT; ++i) {
42                 e = Get_Notif_Ent(MSG_INFO, i);
43                 NOTIF_WRITE(MSG_INFO, e.nent_name, e.nent_string); }
44                 
45         for(i = 0; i < NOTIF_CENTER_COUNT; ++i) {
46                 e = Get_Notif_Ent(MSG_CENTER, i);
47                 NOTIF_WRITE(MSG_CENTER, e.nent_name, e.nent_string); }
48         
49         for(i = 0; i < NOTIF_WEAPON_COUNT; ++i) {
50                 e = Get_Notif_Ent(MSG_WEAPON, i);
51                 NOTIF_WRITE(MSG_WEAPON, e.nent_name, sprintf("infoname: %s, centername: %s",
52                         e.nent_msginfo.nent_name, e.nent_msgcenter.nent_name)); }
53                 
54         for(i = 0; i < NOTIF_DEATH_COUNT; ++i) {
55                 e = Get_Notif_Ent(MSG_DEATH, i);
56                 NOTIF_WRITE(MSG_DEATH, e.nent_name, sprintf("infoname: %s, centername: %s",
57                         e.nent_msginfo.nent_name, e.nent_msgcenter.nent_name)); }
58                 
59         #endif
60
61         print(sprintf("Notification counts: MSG_INFO = %d, MSG_CENTER = %d, MSG_WEAPON = %d, MSG_DEATH = %d\n",
62                 MSG_INFO_NOTIFS, MSG_CENTER_NOTIFS, MSG_WEAPON_NOTIFS, MSG_DEATH_NOTIFS));
63         
64         return;
65         #undef NOTIF_WRITE
66 }
67
68 #ifndef MENUQC
69 string Local_Notification_sprintf(string input, string args, 
70         string s1, string s2, string s3, string s4,
71         float f1, float f2, float f3, float f4)
72 {
73         #ifdef NOTIFICATIONS_DEBUG
74         dprint(sprintf("Local_Notification_sprintf('%s^7', '%s', %s, %s);\n",
75                 strreplace("\n", "\\n", input), args,
76                 sprintf("'%s^7', '%s^7', '%s^7', '%s^7'", s1, s2, s3, s4),
77                 sprintf("%d, %d, %d, %d", f1, f2, f3, f4)));
78         #endif
79         
80         string selected, remaining = args;
81         float sel_num = 0;
82         
83         #ifdef CSQC
84         string tmp_s;
85         #endif
86         
87         arg_slot[0] = ""; arg_slot[1] = ""; arg_slot[2] = ""; arg_slot[3] = ""; arg_slot[4] = ""; arg_slot[5] = ""; arg_slot[6] = "";
88         for(;(remaining != "");)
89         {
90                 selected = car(remaining); remaining = cdr(remaining);
91                 if(sel_num == 7) { backtrace("Local_Notification_sprintf: Hit maximum arguments!\n"); break; }
92                 switch(strtolower(selected))
93                 {
94                         #define ARG_CASE(prog,selected,result) \
95                                 #ifdef CSQC \
96                                 #if (prog == ARG_BOTH) || (prog == ARG_CSQC) \
97                                         case selected: { arg_slot[sel_num] = result; ++sel_num; break; } \
98                                 #endif \
99                                 #else \
100                                 #if (prog == ARG_BOTH) || (prog == ARG_SVQC) \
101                                         case selected: { arg_slot[sel_num] = result; ++sel_num; break; } \
102                                 #endif \
103                                 #endif
104                         NOTIF_ARGUMENT_LIST
105                         #undef ARG_CASE
106                         default: { backtrace(sprintf("Local_Notification_sprintf: Hit unknown token in selected string! '%s'\n", selected)); break; }
107                 }
108         }
109         return sprintf(input, arg_slot[0], arg_slot[1], arg_slot[2], arg_slot[3], arg_slot[4], arg_slot[5], arg_slot[6]);
110 }
111
112 #ifdef CSQC
113 void Local_Notification_HUD_Notify_Push(string icon, string hudargs, string s1, string s2)
114 {
115         string selected, remaining = hudargs;
116         float sel_num = 0;
117         arg_slot[0] = ""; arg_slot[1] = "";
118         for(;(remaining != "");)
119         {
120                 selected = car(remaining); remaining = cdr(remaining);
121                 if(sel_num == 2) { backtrace("Local_Notification_HUD_Notify_Push: Hit maximum arguments!\n"); break; }
122                 switch(strtolower(selected))
123                 {
124                         #define ARG_CASE(selected,result) case selected: { arg_slot[sel_num] = result; ++sel_num; break; }
125                         ARG_CASE("s1", s1)
126                         ARG_CASE("s2", s2)
127                         #undef ARG_CASE
128                         default: { backtrace(sprintf("Local_Notification_HUD_Notify_Push: Hit unknown token in selected string! '%s'\n", selected)); break; }
129                 }
130         }
131         HUD_Notify_Push(icon, arg_slot[0], arg_slot[1]);
132 }
133 #endif
134
135 void Local_Notification(float net_type, float net_name, ...count)
136 {
137         // check supplied type and name for errors
138         #define CHECKARG_TYPENAME(type) case MSG_##type##: \
139                 { if(!net_name || (net_name > NOTIF_##type##_COUNT)) \
140                 { checkargs = sprintf("Improper name: %d!", net_name); } break; }
141         string checkargs = "";
142         switch(net_type)
143         {
144                 CHECKARG_TYPENAME(INFO)
145                 CHECKARG_TYPENAME(CENTER)
146                 CHECKARG_TYPENAME(WEAPON)
147                 CHECKARG_TYPENAME(DEATH)
148                 default: { checkargs = sprintf("Improper type: %d!", checkargs, net_type); break; }
149         }
150         #undef CHECKARG_TYPENAME
151         if(checkargs != "") { backtrace(sprintf("Incorrect usage of Local_Notification: %s\n", checkargs)); return; }
152
153         entity notif = Get_Notif_Ent(net_type, net_name);
154         if not(notif) { backtrace("Local_Notification: Could not find notification entity! (This wasn't caught by usage check?...)\n"); return; }
155         if not(notif.nent_enabled) { print("Local_Notification: Entity was disabled...\n"); return; }
156
157         if((notif.nent_stringcount + notif.nent_floatcount) > count)
158                 { backtrace(sprintf(strcat("Not enough arguments for Local_Notification! stringcount(%d) + floatcount(%d) > count(%d)\n", 
159                 "Check the notification definition and function call for accuracy...?\n"), notif.nent_stringcount, notif.nent_floatcount, count)); return; }
160         else if((notif.nent_stringcount + notif.nent_floatcount) < count)
161                 { backtrace(sprintf(strcat("Too many arguments for Local_Notification! stringcount(%d) + floatcount(%d) < count(%d)\n",
162                 "Check the notification definition and function call for accuracy...?\n"), notif.nent_stringcount, notif.nent_floatcount, count)); return; }
163
164         string s1 = ((0 < notif.nent_stringcount) ? ...(0, string) : "");
165         string s2 = ((1 < notif.nent_stringcount) ? ...(1, string) : "");
166         string s3 = ((2 < notif.nent_stringcount) ? ...(2, string) : "");
167         string s4 = ((3 < notif.nent_stringcount) ? ...(3, string) : "");
168         float f1 = ((0 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 0), float) : 0);
169         float f2 = ((1 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 1), float) : 0);
170         float f3 = ((2 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 2), float) : 0);
171         float f4 = ((3 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 3), float) : 0);
172
173         #ifdef NOTIFICATIONS_DEBUG
174         dprint(sprintf("Local_Notification(%d, %s, %s, %s);\n",
175                 net_type, notif.nent_name,
176                 sprintf("'%s^7', '%s^7', '%s^7', '%s^7'", s1, s2, s3, s4),
177                 sprintf("%d, %d, %d, %d", f1, f2, f3, f4)));
178         #endif
179         
180         switch(net_type)
181         {
182                 case MSG_INFO:
183                 {
184                         print(Local_Notification_sprintf(notif.nent_string, notif.nent_args, 
185                                 s1, s2, s3, s4, f1, f2, f3, f4));
186                         #ifdef CSQC 
187                         if(notif.nent_icon != "") { Local_Notification_HUD_Notify_Push(notif.nent_icon, notif.nent_hudargs, s1, s2); } 
188                         #endif 
189                         break;
190                 }
191                 
192                 #ifdef CSQC
193                 case MSG_CENTER:
194                 {
195                         centerprint_generic(notif.nent_cpid,
196                                 Local_Notification_sprintf(notif.nent_string, notif.nent_args, 
197                                 s1, s2, s3, s4, f1, f2, f3, f4), 0, 0);
198                         break;
199                 }
200                 #endif
201                 
202                 case MSG_WEAPON:
203                 case MSG_DEATH:
204                 {
205                         if(notif.nent_msginfo)
206                         if(notif.nent_msginfo.nent_enabled)
207                         {
208                                 Local_Notification_Without_VarArgs(MSG_INFO, notif.nent_msginfo.nent_id, 
209                                         notif.nent_msginfo.nent_stringcount, 
210                                         notif.nent_msginfo.nent_floatcount, 
211                                         s1, s2, s3, s4, f1, f2, f3, f4);
212                         }
213                         #ifdef CSQC
214                         if(notif.nent_msgcenter)
215                         if(notif.nent_msgcenter.nent_enabled)
216                         {
217                                 Local_Notification_Without_VarArgs(MSG_CENTER, notif.nent_msgcenter.nent_id, 
218                                         notif.nent_msgcenter.nent_stringcount, 
219                                         notif.nent_msgcenter.nent_floatcount, 
220                                         s1, s2, s3, s4, f1, f2, f3, f4); 
221                         }
222                         #endif
223                         break;
224                 }
225         }
226 }
227
228 void Local_Notification_Without_VarArgs(float net_type, float net_name,
229         float stringcount, float floatcount,
230         string s1, string s2, string s3, string s4,
231         float f1, float f2, float f3, float f4)
232 {
233         #define VARITEM(stringc,floatc,args) if((stringcount == stringc) && (floatcount == floatc)) { Local_Notification(net_type, net_name, args); return; }
234         EIGHT_VARS_TO_VARARGS_VARLIST
235         #undef VARITEM
236         Local_Notification(net_type, net_name); // some notifications don't have any arguments at all
237 }
238
239
240 // =========================
241 //  Notification Networking
242 // =========================
243
244 #ifdef CSQC
245 void Read_Notification(float is_new)
246 {
247         float net_type = ReadByte();
248         float net_name = ReadShort();
249
250         entity notif = Get_Notif_Ent(net_type, net_name);
251         if not(notif) { print("Read_Notification: Could not find notification entity!\n"); return; }
252
253         string s1 = ((0 < notif.nent_stringcount) ? ReadString() : "");
254         string s2 = ((1 < notif.nent_stringcount) ? ReadString() : "");
255         string s3 = ((2 < notif.nent_stringcount) ? ReadString() : "");
256         string s4 = ((3 < notif.nent_stringcount) ? ReadString() : "");
257         float f1 = ((0 < notif.nent_floatcount) ? ReadLong() : 0);
258         float f2 = ((1 < notif.nent_floatcount) ? ReadLong() : 0);
259         float f3 = ((2 < notif.nent_floatcount) ? ReadLong() : 0);
260         float f4 = ((3 < notif.nent_floatcount) ? ReadLong() : 0);
261
262         #ifdef NOTIFICATIONS_DEBUG
263         dprint(sprintf("Read_Notification(%d) at %f: net_name = %s.\n", is_new, time, notif.nent_name));
264         #endif
265         
266         if(is_new) { Local_Notification_Without_VarArgs(net_type, net_name, notif.nent_stringcount, notif.nent_floatcount, s1, s2, s3, s4, f1, f2, f3, f4); }
267 }
268 #endif
269
270 #ifdef SVQC
271 void Notification_Remove()
272 {
273         float i;
274         for(i = 0; i < 4; ++i) { if(self.nent_strings[i]) { strunzone(self.nent_strings[i]); } }
275         remove(self);
276 }
277
278 float Write_Notification(entity client, float sf)
279 {
280         float i, send = FALSE;
281         
282         switch(self.nent_broadcast)
283         {
284                 case NOTIF_ONE: { if((client == self.nent_client) || (client.classname == STR_SPECTATOR && client.enemy == self.nent_client)) { send = TRUE; } break; }
285                 case NOTIF_ONE_ONLY: { if(client == self.nent_client) { send = TRUE; } break; }
286                 case NOTIF_TEAM: { if((client.team == self.nent_client.team) || (client.classname == STR_SPECTATOR && client.enemy.team == self.nent_client.team)) { send = TRUE; } break; }
287                 case NOTIF_TEAM_EXCEPT: { if(((client != self.nent_client) && (client.team == self.nent_client.team) && !(client.classname == STR_SPECTATOR && client.enemy == self.nent_client))) { send = TRUE; } break; }
288                 case NOTIF_ANY: { send = TRUE; break; }
289                 case NOTIF_ANY_EXCEPT: { if((client != self.nent_client) && !(client.classname == STR_SPECTATOR && client.enemy == self.nent_client)) { send = TRUE; } break; }
290                 default: { send = FALSE; break; }
291         }
292
293         if(send)
294         {               
295                 WriteByte(MSG_ENTITY, ENT_CLIENT_NOTIFICATION);
296                 WriteByte(MSG_ENTITY, self.nent_net_type);
297                 WriteShort(MSG_ENTITY, self.nent_net_name);
298                 for(i = 0; i < self.nent_stringcount; ++i) { WriteString(MSG_ENTITY, self.nent_strings[i]); } 
299                 for(i = 0; i < self.nent_floatcount; ++i) { WriteLong(MSG_ENTITY, self.nent_floats[i]); }
300         }
301
302         return send; 
303 }
304
305 void Send_Notification(float broadcast, entity client,
306         float net_type, float net_name, ...count)
307 {
308         // check supplied broadcast, target, type, and name for errors
309         #define CHECKARG_TYPENAME(type) case MSG_##type##: \
310                 { if(!net_name || (net_name > NOTIF_##type##_COUNT)) \
311                 { checkargs = sprintf("Improper name: %d!", net_name); } break; }
312         string checkargs = "";
313         switch(net_type)
314         {
315                 CHECKARG_TYPENAME(INFO)
316                 CHECKARG_TYPENAME(CENTER)
317                 CHECKARG_TYPENAME(WEAPON)
318                 CHECKARG_TYPENAME(DEATH)
319                 default: { checkargs = sprintf("Improper type: %d!", checkargs, net_type); break; }
320         }
321         #undef CHECKARG_TYPENAME
322         if(checkargs != "") { checkargs = strcat(checkargs, " "); }
323         switch(broadcast)
324         {
325                 case NOTIF_ONE:
326                 case NOTIF_ONE_ONLY: { if(clienttype(client) == CLIENTTYPE_NOTACLIENT) { checkargs = sprintf("%sNo client provided!", checkargs); } break; }
327                 case NOTIF_ANY_EXCEPT: { if(clienttype(client) == CLIENTTYPE_NOTACLIENT) { checkargs = sprintf("%sException can't be a non-client!", checkargs); } break; }
328                 case NOTIF_ANY: { if(client) { checkargs = sprintf("%sEntity provided when world was required!", checkargs); } break; }
329                 case NOTIF_TEAM:
330                 case NOTIF_TEAM_EXCEPT:
331                 {
332                         if not(teamplay) { checkargs = sprintf("%sTeamplay not active!", checkargs); }
333                         else if(clienttype(client) == CLIENTTYPE_NOTACLIENT)
334                         {
335                                 if(broadcast == NOTIF_TEAM) { checkargs = sprintf("%sNo client provided!", checkargs); }
336                                 else { checkargs = sprintf("%sException can't be a non-client!", checkargs); }
337                         }
338                         break;
339                 }
340                 default: { checkargs = sprintf("%sImproper broadcast: %d!", checkargs, broadcast); break; }
341         }
342         if(checkargs != "") { backtrace(sprintf("Incorrect usage of Send_Notification: %s\n", checkargs)); return; }
343
344         // retreive counts for the arguments of this notification
345         entity notif = Get_Notif_Ent(net_type, net_name);
346         if not(notif) { backtrace("Send_Notification: Could not find notification entity! (This wasn't caught by usage check?...)\n"); return; }
347
348         if((notif.nent_stringcount + notif.nent_floatcount) > count)
349                 { backtrace(sprintf(strcat("Not enough arguments for Send_Notification! stringcount(%d) + floatcount(%d) > count(%d)\n", 
350                 "Check the notification definition and function call for accuracy...?\n"), notif.nent_stringcount, notif.nent_floatcount, count)); return; }
351         else if((notif.nent_stringcount + notif.nent_floatcount) < count)
352                 { backtrace(sprintf(strcat("Too many arguments for Send_Notification! stringcount(%d) + floatcount(%d) < count(%d)\n",
353                 "Check the notification definition and function call for accuracy...?\n"), notif.nent_stringcount, notif.nent_floatcount, count)); return; }
354
355         #ifdef NOTIFICATIONS_DEBUG
356         dprint(sprintf("Send_Notification(%d, %d, %s, stringcount: %d, floatcount: %d, varargs: %d);\n",
357                 broadcast, net_type, notif.nent_name, notif.nent_stringcount, notif.nent_floatcount, count));
358         #endif
359
360         entity net_notif = spawn();
361         net_notif.nent_broadcast = broadcast;
362         net_notif.nent_client = client;
363         net_notif.nent_net_type = net_type;
364         net_notif.nent_net_name = net_name;
365         net_notif.nent_stringcount = notif.nent_stringcount;
366         net_notif.nent_floatcount = notif.nent_floatcount;
367         
368         float i;
369         for(i = 0; i < net_notif.nent_stringcount; ++i) { net_notif.nent_strings[i] = strzone(...(i, string)); }
370         for(i = 0; i < net_notif.nent_floatcount; ++i) { net_notif.nent_floats[i] = ...((net_notif.nent_stringcount + i), float); }
371         
372         net_notif.think = Notification_Remove;
373         net_notif.nextthink = (time + 0.5); 
374
375         Net_LinkEntity(net_notif, FALSE, 0, Write_Notification);
376
377         if((!server_is_local) && (broadcast == NOTIF_ANY || broadcast == NOTIF_ANY_EXCEPT) && (net_type != MSG_CENTER))
378                 { Local_Notification_Without_VarArgs(net_type, net_name, notif.nent_stringcount, notif.nent_floatcount,
379                         IFSTR(0), IFSTR(1), IFSTR(2), IFSTR(3), IFFL(0), IFFL(1), IFFL(2), IFFL(3)); }
380 }
381
382 void Send_Notification_Without_VarArgs(float broadcast, entity client,
383         float net_type, float net_name,
384         float stringcount, float floatcount,
385         string s1, string s2, string s3, string s4,
386         float f1, float f2, float f3, float f4)
387 {
388         #define VARITEM(stringc,floatc,args) if((stringcount == stringc) && (floatcount == floatc)) { Send_Notification(broadcast, client, net_type, net_name, args); return; }
389         EIGHT_VARS_TO_VARARGS_VARLIST
390         #undef VARITEM
391         Send_Notification(broadcast, client, net_type, net_name); // some notifications don't have any arguments at all
392 }
393
394 void Send_Notification_Legacy_Wrapper(float broadcast, entity client,
395         float net_type, float net_name,
396         string s1, string s2,
397         float f1, float f2, float f3)
398 {
399         entity notif = Get_Notif_Ent(net_type, net_name);
400         Send_Notification_Without_VarArgs(broadcast, client, net_type, net_name,
401                 notif.nent_stringcount, notif.nent_floatcount,
402                 s1, s2, "", "", f1, f2, f3, 0);
403 }
404
405
406 // =============================
407 //  LEGACY NOTIFICATION SYSTEMS
408 // =============================
409
410 void Send_CSQC_Centerprint_Generic(entity e, float id, string s, float duration, float countdown_num)
411 {
412         if ((clienttype(e) == CLIENTTYPE_REAL) && (e.flags & FL_CLIENT))
413         {
414                 msg_entity = e;
415                 WRITESPECTATABLE_MSG_ONE({
416                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
417                         WriteByte(MSG_ONE, TE_CSQC_CENTERPRINT_GENERIC);
418                         WriteByte(MSG_ONE, id);
419                         WriteString(MSG_ONE, s);
420                         if (id != 0 && s != "")
421                         {
422                                 WriteByte(MSG_ONE, duration);
423                                 WriteByte(MSG_ONE, countdown_num);
424                         }
425                 });
426         }
427 }
428 void Send_CSQC_Centerprint_Generic_Expire(entity e, float id)
429 {
430         Send_CSQC_Centerprint_Generic(e, id, "", 1, 0);
431 }
432 #endif // ifdef SVQC
433 #endif // ifndef MENUQC