1 // ================================================
2 // Unified notification system, written by Samual
3 // Last updated: December, 2012
4 // ================================================
6 // ======================
7 // Supporting Functions
8 // ======================
11 string TCR(string input, string teamcolor, string teamtext) // TODO: MOVE TO UTIL.QC
13 input = strreplace("^TC", teamcolor, input);
14 input = strreplace("^TT", teamtext, input);
18 // color code replace, place inside of sprintf and parse the string
19 string CCR(string input) // TODO: MOVE TO UTIL.QC
21 // foreground/normal colors
22 input = strreplace("^F1", "^2", input); // primary priority (important names, etc)
23 input = strreplace("^F2", "^3", input); // secondary priority (items, locations, numbers, etc)
26 input = strreplace("^K1", "^1", input); // "bad" or "dangerous" text (death messages against you, kill notifications, etc)
27 input = strreplace("^K2", "^3", input); // similar to above, but less important... OR, a highlight out of above message type
28 input = strreplace("^K3", "^4", input); // "good" or "beneficial" text (you fragging someone, etc)
31 input = strreplace("^BG", "^7", input); // neutral/unimportant text
32 input = strreplace("^N", "^7", input); // "none"-- reset to white...
37 // select between the normal or the gentle message string based on client (or server) settings
38 string normal_or_gentle(string normal, string gentle)
41 if(autocvar_cl_gentle || autocvar_cl_gentle_messages)
43 if(autocvar_sv_gentle)
45 return ((gentle != "") ? gentle : normal);
50 float notif_checkstring(string input)
52 if not(input == "") { return TRUE; }
53 else { return FALSE; }
56 // get the actual name of a notification and return it as a string
57 string Get_Field_Value(float field, float net_type, float net_name)
61 #define GET_FIELD_VALUE_OUTPUT(field,name,strnum,flnum) \
63 case F_NAME: { output = VAR_TO_TEXT(name); break; } \
64 case F_STRNUM: { output = ftos(strnum); break; } \
65 case F_FLNUM: { output = ftos(flnum); break; } }
71 #define MSG_INFO_NOTIF(name,strnum,flnum,args,hudargs,icon,normal,gentle) \
72 { NOTIF_MATCH(name, net_name) { GET_FIELD_VALUE_OUTPUT(field,name,strnum,flnum) } }
73 MSG_INFO_NOTIFICATIONS
79 #define MSG_CENTER_NOTIF(name,strnum,flnum,args,cpid,durcnt,normal,gentle) \
80 { NOTIF_MATCH(name, net_name) { GET_FIELD_VALUE_OUTPUT(field,name,strnum,flnum) } }
81 MSG_CENTER_NOTIFICATIONS
82 #undef MSG_CENTER_NOTIF
87 #define MSG_WEAPON_NOTIF(name,infoname,centername) \
88 { NOTIF_MATCH(name, net_name) { GET_FIELD_VALUE_OUTPUT(field,name, \
89 max(stof(Get_Field_Value(F_STRNUM, MSG_INFO, infoname)), stof(Get_Field_Value(F_STRNUM, MSG_CENTER, centername))), \
90 max(stof(Get_Field_Value(F_FLNUM, MSG_INFO, infoname)), stof(Get_Field_Value(F_FLNUM, MSG_CENTER, centername)))) } }
91 MSG_WEAPON_NOTIFICATIONS
92 #undef MSG_WEAPON_NOTIF
97 #define MSG_DEATH_NOTIF(name,infoname,centername) \
98 { NOTIF_MATCH(name, net_name) { GET_FIELD_VALUE_OUTPUT(field,name, \
99 max(stof(Get_Field_Value(F_STRNUM, MSG_INFO, infoname)), stof(Get_Field_Value(F_STRNUM, MSG_CENTER, centername))), \
100 max(stof(Get_Field_Value(F_FLNUM, MSG_INFO, infoname)), stof(Get_Field_Value(F_FLNUM, MSG_CENTER, centername)))) } }
101 MSG_DEATH_NOTIFICATIONS
102 #undef MSG_DEATH_NOTIF
107 #undef GET_FIELD_VALUE_OUTPUT
110 #endif // ifndef MENUQC
113 // ===============================
114 // Frontend Notification Pushing
115 // ===============================
117 void Dump_Notifications(float fh, float alsoprint)
119 float MSG_INFO_NOTIFS = 0, MSG_CENTER_NOTIFS = 0, MSG_WEAPON_NOTIFS = 0, MSG_DEATH_NOTIFS = 0;
122 #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)); }
123 #define MSG_INFO_NOTIF(name,strnum,flnum,args,hudargs,icon,normal,gentle) { ++MSG_INFO_NOTIFS; NOTIF_WRITE("MSG_INFO", VAR_TO_TEXT(name), normal) }
124 #define MSG_CENTER_NOTIF(name,strnum,flnum,args,cpid,durcnt,normal,gentle) { ++MSG_CENTER_NOTIFS; NOTIF_WRITE("MSG_CENTER", VAR_TO_TEXT(name), normal) }
125 #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))) }
126 #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))) }
127 MSG_INFO_NOTIFICATIONS
128 MSG_CENTER_NOTIFICATIONS
129 MSG_WEAPON_NOTIFICATIONS
130 MSG_DEATH_NOTIFICATIONS
132 #undef MSG_INFO_NOTIF
133 #undef MSG_CENTER_NOTIF
134 #undef MSG_WEAPON_NOTIF
135 #undef MSG_DEATH_NOTIF
137 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));
143 void HUD_Notify_Push(string icon, string attacker, string victim)
148 if (kn_index == -1) { kn_index = KN_MAX_ENTRIES-1; }
149 killnotify_times[kn_index] = time;
152 if(killnotify_icon[kn_index]) { strunzone(killnotify_icon[kn_index]); }
153 killnotify_icon[kn_index] = strzone(icon);
156 if(killnotify_attackers[kn_index]) { strunzone(killnotify_attackers[kn_index]); }
157 killnotify_attackers[kn_index] = strzone(attacker);
160 if(killnotify_victims[kn_index]) { strunzone(killnotify_victims[kn_index]); }
161 killnotify_victims[kn_index] = strzone(victim);
165 void backtrace(string msg) // TODO: MOVE TO UTIL.QC
168 dev = cvar("developer");
169 war = cvar("prvm_backtraceforwarnings");
170 cvar_set("developer", "1");
171 cvar_set("prvm_backtraceforwarnings", "1");
173 print("--- CUT HERE ---\nWARNING: ");
176 remove(world); // isn't there any better way to cause a backtrace?
177 print("\n--- CUT UNTIL HERE ---\n");
178 cvar_set("developer", ftos(dev));
179 cvar_set("prvm_backtraceforwarnings", ftos(war));
183 void Local_Notification(float net_type, float net_name, ...count)
185 float stringcount = stof(Get_Field_Value(F_STRNUM, net_type, net_name));
186 float floatcount = stof(Get_Field_Value(F_FLNUM, net_type, net_name));
188 string s1 = ((0 < stringcount) ? ...(0, string) : NO_STR_ARG);
189 string s2 = ((1 < stringcount) ? ...(1, string) : NO_STR_ARG);
190 string s3 = ((2 < stringcount) ? ...(2, string) : NO_STR_ARG);
191 string s4 = ((3 < stringcount) ? ...(3, string) : NO_STR_ARG);
192 float f1 = ((stringcount < count) ? ...(stringcount, float) : NO_FL_ARG);
193 float f2 = (((stringcount + 1) < count) ? ...((stringcount + 1), float) : NO_FL_ARG);
194 float f3 = (((stringcount + 2) < count) ? ...((stringcount + 2), float) : NO_FL_ARG);
195 float f4 = (((stringcount + 3) < count) ? ...((stringcount + 3), float) : NO_FL_ARG);
197 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")));
198 dprint(" ^--: stringcount: ", ftos(stringcount), ", floatcount: ", ftos(floatcount), ".\n");
200 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; }
201 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; }
207 #define MSG_INFO_NOTIF(name,strnum,flnum,args,hudargs,icon,normal,gentle) \
208 { NOTIF_MATCH(name, net_name) CHECK_AUTOCVAR(name) \
210 if(notif_checkstring(normal)) { print(sprintf(CCR(normal_or_gentle(normal, gentle)), args)); } \
212 if(notif_checkstring(icon)) { HUD_Notify_Push(icon, hudargs); } \
215 MSG_INFO_NOTIFICATIONS
216 #undef MSG_INFO_NOTIF
222 #define MSG_CENTER_NOTIF(name,strnum,flnum,args,cpid,durcnt,normal,gentle) \
223 { NOTIF_MATCH(name, net_name) CHECK_AUTOCVAR(name) \
225 if(notif_checkstring(normal)) { centerprint_generic(HANDLE_CPID(cpid), sprintf(CCR(normal_or_gentle(normal, gentle)), args), durcnt); } \
227 MSG_CENTER_NOTIFICATIONS
228 #undef MSG_CENTER_NOTIF
234 #define MSG_WEAPON_NOTIF(name,infoname,centername) \
235 { NOTIF_MATCH(name, net_name) CHECK_AUTOCVAR(name) \
237 #if infoname != NO_MSG \
238 Local_Notification_Without_VarArgs(MSG_INFO, infoname, \
239 stof(Get_Field_Value(F_STRNUM, MSG_INFO, infoname)), \
240 stof(Get_Field_Value(F_FLNUM, MSG_INFO, infoname)), \
241 s1, s2, s3, s4, f1, f2, f3, f4); \
244 #if centername != NO_MSG \
245 Local_Notification_Without_VarArgs(MSG_CENTER, centername, \
246 stof(Get_Field_Value(F_STRNUM, MSG_CENTER, centername)), \
247 stof(Get_Field_Value(F_FLNUM, MSG_CENTER, centername)), \
248 s1, s2, s3, s4, f1, f2, f3, f4); \
252 MSG_WEAPON_NOTIFICATIONS
253 #undef MSG_WEAPON_NOTIF
258 #define MSG_DEATH_NOTIF(name,infoname,centername) \
259 { NOTIF_MATCH(name, net_name) CHECK_AUTOCVAR(name) \
261 #if infoname != NO_MSG \
262 Local_Notification_Without_VarArgs(MSG_INFO, infoname, \
263 stof(Get_Field_Value(F_STRNUM, MSG_INFO, infoname)), \
264 stof(Get_Field_Value(F_FLNUM, MSG_INFO, infoname)), \
265 s1, s2, s3, s4, f1, f2, f3, f4); \
268 #if centername != NO_MSG \
269 Local_Notification_Without_VarArgs(MSG_CENTER, centername, \
270 stof(Get_Field_Value(F_STRNUM, MSG_CENTER, centername)), \
271 stof(Get_Field_Value(F_FLNUM, MSG_CENTER, centername)), \
272 s1, s2, s3, s4, f1, f2, f3, f4); \
276 MSG_DEATH_NOTIFICATIONS
277 #undef MSG_DEATH_NOTIF
283 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)
285 #define VARITEM(stringc,floatc,args) if((stringcount == stringc) && (floatcount == floatc)) { Local_Notification(net_type, net_name, args); return; }
286 EIGHT_VARS_TO_VARARGS_VARLIST
289 Local_Notification(net_type, net_name); // some notifications don't have any arguments at all
293 // =========================
294 // Notification Networking
295 // =========================
298 #define NOTIF_ONE_SPECTATABLE 2
300 #define NOTIF_TEAM_EXCEPT 4
302 #define NOTIF_ANY_EXCEPT 6
304 .float nent_broadcast;
306 .float nent_net_type;
307 .float nent_net_name;
308 .string nent_strings[4];
309 .float nent_floats[4];
312 void Read_Notification(float is_new)
314 float net_type = ReadByte();
315 float net_name = ReadShort();
317 float stringcount = stof(Get_Field_Value(F_STRNUM, net_type, net_name));
318 float floatcount = stof(Get_Field_Value(F_FLNUM, net_type, net_name));
320 string s1 = ((stringcount >= 1) ? ReadString() : NO_STR_ARG);
321 string s2 = ((stringcount >= 2) ? ReadString() : NO_STR_ARG);
322 string s3 = ((stringcount >= 3) ? ReadString() : NO_STR_ARG);
323 string s4 = ((stringcount == 4) ? ReadString() : NO_STR_ARG);
324 float f1 = ((floatcount >= 1) ? ReadLong() : NO_FL_ARG);
325 float f2 = ((floatcount >= 2) ? ReadLong() : NO_FL_ARG);
326 float f3 = ((floatcount >= 3) ? ReadLong() : NO_FL_ARG);
327 float f4 = ((floatcount == 4) ? ReadLong() : NO_FL_ARG);
329 if(is_new) { Local_Notification_Without_VarArgs(net_type, net_name, stringcount, floatcount, s1, s2, s3, s4, f1, f2, f3, f4); }
334 float Write_Notification(entity client, float sf)
338 switch(self.nent_broadcast)
340 case NOTIF_ONE: { if(client == self.nent_client) { send = TRUE; } break; }
341 case NOTIF_ONE_SPECTATABLE: { if((client == self.nent_client) || (client.classname == STR_SPECTATOR && client.enemy == self.nent_client)) { send = TRUE; } break; }
342 case NOTIF_TEAM: { if(client.team == self.nent_client.team) { send = TRUE; } break; }
343 case NOTIF_TEAM_EXCEPT: { if((client != self.nent_client) && (client.team == self.nent_client.team)) { send = TRUE; } break; }
344 case NOTIF_ANY: { send = TRUE; break; }
345 case NOTIF_ANY_EXCEPT: { if(client != self.nent_client) { send = TRUE; } break; }
346 default: { send = FALSE; break; }
351 float stringcount = stof(Get_Field_Value(F_STRNUM, self.nent_net_type, self.nent_net_name));
352 float floatcount = stof(Get_Field_Value(F_FLNUM, self.nent_net_type, self.nent_net_name));
354 WriteByte(MSG_ENTITY, ENT_CLIENT_NOTIFICATION);
355 WriteByte(MSG_ENTITY, self.nent_net_type);
356 WriteShort(MSG_ENTITY, self.nent_net_name);
357 for(i = 0; i < stringcount; ++i) { WriteString(MSG_ENTITY, self.nent_strings[i]); }
358 for(i = 0; i < floatcount; ++i) { WriteLong(MSG_ENTITY, self.nent_floats[stringcount + i]); }
364 void Send_Notification(float broadcast, entity client, float net_type, float net_name, ...count)
366 if((broadcast == MSG_BROADCAST || broadcast == MSG_ONE) && net_type && net_name)
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));
373 dprint("Send_Notification(", ftos(broadcast), ", ", ftos(net_type), ", ", Get_Field_Value(F_NAME, net_type, net_name), strcat(", ", ftos(count), ");\n"));
374 dprint(" ^--: stringcount: ", ftos(stringcount), ", floatcount: ", ftos(floatcount), ".\n");
376 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; }
377 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; }
379 //if(Count_Proper_Strings(NO_STR_ARG, s1, s2) > stringcount) { backtrace("Too many string arguments for notification!\n"); return; }
380 //if(Count_Proper_Floats(NO_FL_ARG, f1, f2, f3) > floatcount) { backtrace("Too many float arguments for notification!\n"); return; }
382 entity notif = spawn();
383 notif.nent_broadcast = broadcast;
384 notif.nent_client = client;
385 notif.nent_net_type = net_type;
386 notif.nent_net_name = net_name;
387 for(i = 0; i < stringcount; ++i) { tmp_s = ...(i, string); notif.nent_strings[i] = tmp_s; dprint("WriteString(...(", ftos(i), ", string)); - ", tmp_s, "\n"); }
388 for(i = 0; i < floatcount; ++i) { tmp_f = ...((stringcount + i), float); notif.nent_floats[i] = tmp_f; dprint("WriteLong(...(", ftos((stringcount + i)), ", float)); - ", ftos(tmp_f), "\n"); }
390 Net_LinkEntity(notif, FALSE, 0.5, Write_Notification);
394 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));
397 else { backtrace("Incorrect usage of Send_Notification!\n"); }
400 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)
402 #define VARITEM(stringc,floatc,args) if((stringcount == stringc) && (floatcount == floatc)) { Send_Notification(broadcast, client, net_type, net_name, args); return; }
403 EIGHT_VARS_TO_VARARGS_VARLIST
406 Send_Notification(broadcast, client, net_type, net_name); // some notifications don't have any arguments at all
409 void Send_Notification_Legacy_Wrapper(entity client, float broadcast, float net_type, float net_name, string s1, string s2, float f1, float f2, float f3)
411 float stringcount = stof(Get_Field_Value(F_STRNUM, net_type, net_name));
412 float floatcount = stof(Get_Field_Value(F_FLNUM, net_type, net_name));
413 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);
416 /*void Send_Notification_ToTeam(float targetteam, entity except, float net_type, float net_name, ...count)
418 float stringcount = stof(Get_Field_Value(F_STRNUM, net_type, net_name));
419 float floatcount = stof(Get_Field_Value(F_FLNUM, net_type, net_name));
420 Send_Notification_Without_VarArgs(NOTIF_TEAM, tmp_entity, net_type, net_name, stringcount, floatcount, IFSTR(0), IFSTR(1), IFSTR(2), IFSTR(3), IFFL(0), IFFL(1), IFFL(2), IFFL(3));
423 // WARNING: use this ONLY if you need exceptions or want to exclude spectators, otherwise use Send_Notification(world, MSG_BROADCAST, ...)
424 void Send_Notification_ToAll(entity except, float spectators, float net_type, float net_name, ...count)
426 float stringcount = stof(Get_Field_Value(F_STRNUM, net_type, net_name));
427 float floatcount = stof(Get_Field_Value(F_FLNUM, net_type, net_name));
430 FOR_EACH_REALCLIENT(tmp_entity)
432 if((tmp_entity.classname == STR_PLAYER) || spectators)
433 if(tmp_entity != except)
435 Send_Notification_Without_VarArgs(tmp_entity, MSG_ONE, net_type, net_name, stringcount, floatcount, IFSTR(0), IFSTR(1), IFSTR(2), IFSTR(3), IFFL(0), IFFL(1), IFFL(2), IFFL(3));
441 // =============================
442 // LEGACY NOTIFICATION SYSTEMS
443 // =============================
445 void Send_CSQC_Centerprint_Generic(entity e, float id, string s, float duration, float countdown_num)
447 if ((clienttype(e) == CLIENTTYPE_REAL) && (e.flags & FL_CLIENT))
450 WRITESPECTATABLE_MSG_ONE({
451 WriteByte(MSG_ONE, SVC_TEMPENTITY);
452 WriteByte(MSG_ONE, TE_CSQC_CENTERPRINT_GENERIC);
453 WriteByte(MSG_ONE, id);
454 WriteString(MSG_ONE, s);
455 if (id != 0 && s != "")
457 WriteByte(MSG_ONE, duration);
458 WriteByte(MSG_ONE, countdown_num);
463 void Send_CSQC_Centerprint_Generic_Expire(entity e, float id)
465 Send_CSQC_Centerprint_Generic(e, id, "", 1, 0);
468 #endif // ifndef MENUQC