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