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