1 // ================================================
2 // Unified notification system, written by Samual
3 // Last updated: February, 2013
4 // ================================================
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)
10 //dprint("Get_Field_Value(", ftos(field), ", ", ftos(net_type), ", ", ftos(net_name), ");\n");
12 #define GET_FIELD_VALUE_OUTPUT(field,name,strnum,flnum) switch(field) { \
13 case F_NAME: { return VAR_TO_TEXT(name); } \
14 case F_STRNUM: { return ftos(strnum); } \
15 case F_FLNUM: { return ftos(flnum); } }
17 #define GET_FIELD_VALUE_OUTPUT_PAIR(field,name,infoname,centername,strnum,flnum) switch(field) { \
18 case F_NAME: { return VAR_TO_TEXT(name); } \
19 case F_INFNAME: { return VAR_TO_TEXT(infoname); } \
20 case F_CENNAME: { return VAR_TO_TEXT(centername); } \
21 case F_INFVAL: { return ftos(infoname); } \
22 case F_CENVAL: { return ftos(centername); } \
23 case F_STRNUM: { return ftos(strnum); } \
24 case F_FLNUM: { return ftos(flnum); } }
26 #define CLPSE_GETVALUE(name,arg,earg) \
37 #define MSG_INFO_NOTIF(name,strnum,flnum,args,hudargs,icon,normal,gentle) case name: { GET_FIELD_VALUE_OUTPUT(field,name,strnum,flnum) }
38 NOTIF_SWITCH_LIST(MSG_INFO, net_name, return "")
44 #define MSG_CENTER_NOTIF(name,strnum,flnum,args,cpid,durcnt,normal,gentle) case name: { GET_FIELD_VALUE_OUTPUT(field,name,strnum,flnum) }
45 NOTIF_SWITCH_LIST(MSG_CENTER, net_name, return "")
46 #undef MSG_CENTER_NOTIF
51 #define MSG_WEAPON_NOTIF(name,infoname,centername) case name: { GET_FIELD_VALUE_OUTPUT_PAIR(field,name, \
52 CLPSE_GETVALUE(infoname, infoname, NO_MSG), CLPSE_GETVALUE(centername, centername, NO_MSG), \
53 max(CLPSE_GETVALUE(infoname, stof(Get_Field_Value(F_STRNUM, MSG_INFO, infoname)), 0), CLPSE_GETVALUE(centername, stof(Get_Field_Value(F_STRNUM, MSG_CENTER, centername)), 0)), \
54 max(CLPSE_GETVALUE(infoname, stof(Get_Field_Value(F_FLNUM, MSG_INFO, infoname)), 0), CLPSE_GETVALUE(centername, stof(Get_Field_Value(F_FLNUM, MSG_CENTER, centername)), 0))) }
55 NOTIF_SWITCH_LIST(MSG_WEAPON, net_name, return "")
56 #undef MSG_WEAPON_NOTIF
61 #define MSG_DEATH_NOTIF(name,infoname,centername) case name: { GET_FIELD_VALUE_OUTPUT_PAIR(field,name, \
62 CLPSE_GETVALUE(infoname, infoname, NO_MSG), CLPSE_GETVALUE(centername, centername, NO_MSG), \
63 max(CLPSE_GETVALUE(infoname, stof(Get_Field_Value(F_STRNUM, MSG_INFO, infoname)), 0), CLPSE_GETVALUE(centername, stof(Get_Field_Value(F_STRNUM, MSG_CENTER, centername)), 0)), \
64 max(CLPSE_GETVALUE(infoname, stof(Get_Field_Value(F_FLNUM, MSG_INFO, infoname)), 0), CLPSE_GETVALUE(centername, stof(Get_Field_Value(F_FLNUM, MSG_CENTER, centername)), 0))) }
65 NOTIF_SWITCH_LIST(MSG_DEATH, net_name, return "")
66 #undef MSG_DEATH_NOTIF
71 #undef GET_FIELD_VALUE_OUTPUT
72 #undef GET_FIELD_VALUE_OUTPUT_PAIR
76 #endif // ifndef MENUQC
79 // ===============================
80 // Frontend Notification Pushing
81 // ===============================
83 void Dump_Notifications(float fh, float alsoprint)
85 float MSG_INFO_NOTIFS = 0, MSG_CENTER_NOTIFS = 0, MSG_WEAPON_NOTIFS = 0, MSG_DEATH_NOTIFS = 0;
88 #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)); }
89 #define MSG_INFO_NOTIF(name,strnum,flnum,args,hudargs,icon,normal,gentle) { ++MSG_INFO_NOTIFS; NOTIF_WRITE("MSG_INFO", VAR_TO_TEXT(name), normal) }
90 #define MSG_CENTER_NOTIF(name,strnum,flnum,args,cpid,durcnt,normal,gentle) { ++MSG_CENTER_NOTIFS; NOTIF_WRITE("MSG_CENTER", VAR_TO_TEXT(name), normal) }
91 #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))) }
92 #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))) }
93 MSG_INFO_NOTIFICATIONS
94 MSG_CENTER_NOTIFICATIONS
95 MSG_WEAPON_NOTIFICATIONS
96 MSG_DEATH_NOTIFICATIONS
99 #undef MSG_CENTER_NOTIF
100 #undef MSG_WEAPON_NOTIF
101 #undef MSG_DEATH_NOTIF
103 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));
109 void HUD_Notify_Push(string icon, string attacker, string victim)
114 if (kn_index == -1) { kn_index = KN_MAX_ENTRIES-1; }
115 killnotify_times[kn_index] = time;
118 if(killnotify_icon[kn_index]) { strunzone(killnotify_icon[kn_index]); }
119 killnotify_icon[kn_index] = strzone(icon);
122 if(killnotify_attackers[kn_index]) { strunzone(killnotify_attackers[kn_index]); }
123 killnotify_attackers[kn_index] = strzone(attacker);
126 if(killnotify_victims[kn_index]) { strunzone(killnotify_victims[kn_index]); }
127 killnotify_victims[kn_index] = strzone(victim);
132 void Local_Notification(float net_type, float net_name, ...count)
134 if(net_type && net_name)
136 float stringcount = stof(Get_Field_Value(F_STRNUM, net_type, net_name));
137 float floatcount = stof(Get_Field_Value(F_FLNUM, net_type, net_name));
139 string s1 = ((0 < stringcount) ? ...(0, string) : NO_STR_ARG);
140 string s2 = ((1 < stringcount) ? ...(1, string) : NO_STR_ARG);
141 string s3 = ((2 < stringcount) ? ...(2, string) : NO_STR_ARG);
142 string s4 = ((3 < stringcount) ? ...(3, string) : NO_STR_ARG);
143 float f1 = ((stringcount < count) ? ...(stringcount, float) : NO_FL_ARG);
144 float f2 = (((stringcount + 1) < count) ? ...((stringcount + 1), float) : NO_FL_ARG);
145 float f3 = (((stringcount + 2) < count) ? ...((stringcount + 2), float) : NO_FL_ARG);
146 float f4 = (((stringcount + 3) < count) ? ...((stringcount + 3), float) : NO_FL_ARG);
148 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")));
149 dprint(" ^--: stringcount: ", ftos(stringcount), ", floatcount: ", ftos(floatcount), ".\n");
151 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; }
152 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; }
158 #define MSG_INFO_NOTIF(name,strnum,flnum,args,hudargs,icon,normal,gentle) \
159 case name: { CHECK_AUTOCVAR(name) \
161 print(sprintf(CCR(normal_or_gentle(normal, gentle)), args)); \
163 if(icon != "") { HUD_Notify_Push(icon, hudargs); } \
167 NOTIF_SWITCH_LIST(MSG_INFO, net_name, return)
169 #undef MSG_INFO_NOTIF
175 #define MSG_CENTER_NOTIF(name,strnum,flnum,args,cpid,durcnt,normal,gentle) \
176 case name: { CHECK_AUTOCVAR(name) \
178 centerprint_generic(HANDLE_CPID(cpid), sprintf(CCR(normal_or_gentle(normal, gentle)), args), durcnt); \
181 NOTIF_SWITCH_LIST(MSG_CENTER, net_name, return)
183 #undef MSG_CENTER_NOTIF
189 #define MSG_WEAPON_NOTIF(name,infoname,centername) \
190 case name: { CHECK_AUTOCVAR(name) \
192 #if infoname != NO_MSG \
193 Local_Notification_Without_VarArgs(MSG_INFO, infoname, \
194 stof(Get_Field_Value(F_STRNUM, MSG_INFO, infoname)), \
195 stof(Get_Field_Value(F_FLNUM, MSG_INFO, infoname)), \
196 s1, s2, s3, s4, f1, f2, f3, f4); \
199 #if centername != NO_MSG \
200 Local_Notification_Without_VarArgs(MSG_CENTER, centername, \
201 stof(Get_Field_Value(F_STRNUM, MSG_CENTER, centername)), \
202 stof(Get_Field_Value(F_FLNUM, MSG_CENTER, centername)), \
203 s1, s2, s3, s4, f1, f2, f3, f4); \
208 NOTIF_SWITCH_LIST(MSG_WEAPON, net_name, return)
210 #undef MSG_WEAPON_NOTIF
215 #define MSG_DEATH_NOTIF(name,infoname,centername) \
216 case name: { CHECK_AUTOCVAR(name) \
218 #if infoname != NO_MSG \
219 Local_Notification_Without_VarArgs(MSG_INFO, infoname, \
220 stof(Get_Field_Value(F_STRNUM, MSG_INFO, infoname)), \
221 stof(Get_Field_Value(F_FLNUM, MSG_INFO, infoname)), \
222 s1, s2, s3, s4, f1, f2, f3, f4); \
225 #if centername != NO_MSG \
226 Local_Notification_Without_VarArgs(MSG_CENTER, centername, \
227 stof(Get_Field_Value(F_STRNUM, MSG_CENTER, centername)), \
228 stof(Get_Field_Value(F_FLNUM, MSG_CENTER, centername)), \
229 s1, s2, s3, s4, f1, f2, f3, f4); \
234 NOTIF_SWITCH_LIST(MSG_DEATH, net_name, return)
236 #undef MSG_DEATH_NOTIF
241 else { backtrace("Incorrect usage of Local_Notification!\n"); }
244 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)
246 #define VARITEM(stringc,floatc,args) if((stringcount == stringc) && (floatcount == floatc)) { Local_Notification(net_type, net_name, args); return; }
247 EIGHT_VARS_TO_VARARGS_VARLIST
250 Local_Notification(net_type, net_name); // some notifications don't have any arguments at all
254 // =========================
255 // Notification Networking
256 // =========================
259 void Read_Notification(float is_new)
261 float net_type = ReadByte();
262 float net_name = ReadShort();
264 float stringcount = stof(Get_Field_Value(F_STRNUM, net_type, net_name));
265 float floatcount = stof(Get_Field_Value(F_FLNUM, net_type, net_name));
267 string s1 = ((stringcount >= 1) ? ReadString() : NO_STR_ARG);
268 string s2 = ((stringcount >= 2) ? ReadString() : NO_STR_ARG);
269 string s3 = ((stringcount >= 3) ? ReadString() : NO_STR_ARG);
270 string s4 = ((stringcount == 4) ? ReadString() : NO_STR_ARG);
271 float f1 = ((floatcount >= 1) ? ReadLong() : NO_FL_ARG);
272 float f2 = ((floatcount >= 2) ? ReadLong() : NO_FL_ARG);
273 float f3 = ((floatcount >= 3) ? ReadLong() : NO_FL_ARG);
274 float f4 = ((floatcount == 4) ? ReadLong() : NO_FL_ARG);
276 dprint("Read_Notification(", ftos(is_new), ") at ", ftos(time), ": net_name = ", Get_Field_Value(F_NAME, net_type, net_name), ".\n");
278 if(is_new) { Local_Notification_Without_VarArgs(net_type, net_name, stringcount, floatcount, s1, s2, s3, s4, f1, f2, f3, f4); }
283 void Notification_Remove()
286 for(i = 0; i < 4; ++i) { if(self.nent_strings[i]) { strunzone(self.nent_strings[i]); } }
290 float Write_Notification(entity client, float sf)
292 float i, send = FALSE;
294 switch(self.nent_broadcast)
296 case NOTIF_ONE: { if((client == self.nent_client) || (client.classname == STR_SPECTATOR && client.enemy == self.nent_client)) { send = TRUE; } break; }
297 case NOTIF_ONE_ONLY: { if(client == self.nent_client) { send = TRUE; } break; }
298 case NOTIF_TEAM: { if((client.team == self.nent_client.team) || (client.classname == STR_SPECTATOR && client.enemy.team == self.nent_client.team)) { send = TRUE; } break; }
299 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; }
300 case NOTIF_ANY: { send = TRUE; break; }
301 case NOTIF_ANY_EXCEPT: { if((client != self.nent_client) && !(client.classname == STR_SPECTATOR && client.enemy == self.nent_client)) { send = TRUE; } break; }
302 default: { send = FALSE; break; }
307 WriteByte(MSG_ENTITY, ENT_CLIENT_NOTIFICATION);
308 WriteByte(MSG_ENTITY, self.nent_net_type);
309 WriteShort(MSG_ENTITY, self.nent_net_name);
310 for(i = 0; i < self.nent_stringcount; ++i) { WriteString(MSG_ENTITY, self.nent_strings[i]); }
311 for(i = 0; i < self.nent_floatcount; ++i) { WriteLong(MSG_ENTITY, self.nent_floats[i]); }
317 string Send_Notification_CheckTarget(float broadcast, entity client)
324 if(clienttype(client) == CLIENTTYPE_NOTACLIENT) { return "No client provided!"; }
329 case NOTIF_TEAM_EXCEPT:
331 if not(teamplay) { return "Teamplay not active!"; }
332 else if(clienttype(client) == CLIENTTYPE_NOTACLIENT) { return "No client provided!"; }
333 else if((broadcast == NOTIF_TEAM_EXCEPT) && (clienttype(client) == CLIENTTYPE_NOTACLIENT)) { return "Exception can't be a non-client!"; }
337 case NOTIF_ANY_EXCEPT:
339 if(clienttype(client) == CLIENTTYPE_NOTACLIENT) { return "Exception can't be a non-client!"; }
345 if(client) { return "Entity provided when world was required!"; }
350 return strcat("Improper broadcast type: ", ftos(broadcast), "!");
353 void Send_Notification(float broadcast, entity client, float net_type, float net_name, ...count)
355 string checktarget = Send_Notification_CheckTarget(broadcast, client);
357 if((checktarget == "") && net_type && net_name)
359 float stringcount = stof(Get_Field_Value(F_STRNUM, net_type, net_name));
360 float floatcount = stof(Get_Field_Value(F_FLNUM, net_type, net_name));
363 dprint("Send_Notification(", ftos(broadcast), ", ", ftos(net_type), ", ", Get_Field_Value(F_NAME, net_type, net_name), strcat(", ", ftos(count), ");\n"));
364 dprint(" ^--: stringcount: ", ftos(stringcount), ", floatcount: ", ftos(floatcount), ".\n");
366 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; }
367 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; }
369 entity notif = spawn();
370 notif.nent_broadcast = broadcast;
371 notif.nent_client = client;
372 notif.nent_net_type = net_type;
373 notif.nent_net_name = net_name;
374 notif.nent_stringcount = stringcount;
375 notif.nent_floatcount = floatcount;
376 for(i = 0; i < stringcount; ++i) { notif.nent_strings[i] = strzone(...(i, string)); }
377 for(i = 0; i < floatcount; ++i) { notif.nent_floats[i] = ...((stringcount + i), float); }
379 notif.think = Notification_Remove;
380 notif.nextthink = (time + 0.5);
382 Net_LinkEntity(notif, FALSE, 0, Write_Notification);
384 if((!server_is_local) && (broadcast == NOTIF_ANY || broadcast == NOTIF_ANY_EXCEPT) && (net_type != MSG_CENTER))
386 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));
389 else { backtrace(strcat("Incorrect usage of Send_Notification: ", checktarget, "\n")); }
392 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)
394 #define VARITEM(stringc,floatc,args) if((stringcount == stringc) && (floatcount == floatc)) { Send_Notification(broadcast, client, net_type, net_name, args); return; }
395 EIGHT_VARS_TO_VARARGS_VARLIST
398 Send_Notification(broadcast, client, net_type, net_name); // some notifications don't have any arguments at all
401 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)
403 float stringcount = stof(Get_Field_Value(F_STRNUM, net_type, net_name));
404 float floatcount = stof(Get_Field_Value(F_FLNUM, net_type, net_name));
405 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);
409 // =============================
410 // LEGACY NOTIFICATION SYSTEMS
411 // =============================
413 void Send_CSQC_Centerprint_Generic(entity e, float id, string s, float duration, float countdown_num)
415 if ((clienttype(e) == CLIENTTYPE_REAL) && (e.flags & FL_CLIENT))
418 WRITESPECTATABLE_MSG_ONE({
419 WriteByte(MSG_ONE, SVC_TEMPENTITY);
420 WriteByte(MSG_ONE, TE_CSQC_CENTERPRINT_GENERIC);
421 WriteByte(MSG_ONE, id);
422 WriteString(MSG_ONE, s);
423 if (id != 0 && s != "")
425 WriteByte(MSG_ONE, duration);
426 WriteByte(MSG_ONE, countdown_num);
431 void Send_CSQC_Centerprint_Generic_Expire(entity e, float id)
433 Send_CSQC_Centerprint_Generic(e, id, "", 1, 0);
436 #endif // ifndef MENUQC