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