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