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