1 // ================================================
2 // Unified notification system, written by Samual
3 // Last updated: February, 2013
4 // ================================================
8 entity Get_Notif_Ent(float net_type, float net_name)
12 case MSG_INFO: return msg_info_notifs[net_name - 1];
13 case MSG_CENTER: return msg_center_notifs[net_name - 1];
14 case MSG_WEAPON: return msg_weapon_notifs[net_name - 1];
15 case MSG_DEATH: return msg_death_notifs[net_name - 1];
17 backtrace(sprintf("Get_Notif_Ent(%d, %d): Improper net type!\n", net_type, net_name));
21 string Get_Notif_Name(float net_type, float net_name)
23 entity e = Get_Notif_Ent(net_type, net_name);
24 if(e) { return e.nent_name; }
25 backtrace(sprintf("Get_Notif_Name(%d, %d): Could not find entity!\n", net_type, net_name));
29 float Get_Notif_Infval(float net_type, float net_name)
31 entity e = Get_Notif_Ent(net_type, net_name);
32 if(e) { return e.nent_infoname; }
33 backtrace(sprintf("Get_Notif_Infval(%d, %d): Could not find entity!\n", net_type, net_name));
37 float Get_Notif_Cenval(float net_type, float net_name)
39 entity e = Get_Notif_Ent(net_type, net_name);
40 if(e) { return e.nent_centername; }
41 backtrace(sprintf("Get_Notif_Cenval(%d, %d): Could not find entity!\n", net_type, net_name));
45 float Get_Notif_Strnum(float net_type, float net_name)
47 entity e = Get_Notif_Ent(net_type, net_name);
48 if(e) { return e.nent_stringcount; }
49 backtrace(sprintf("Get_Notif_Strnum(%d, %d): Could not find entity!\n", net_type, net_name));
53 float Get_Notif_Flnum(float net_type, float net_name)
55 entity e = Get_Notif_Ent(net_type, net_name);
56 if(e) { return e.nent_floatcount; }
57 backtrace(sprintf("Get_Notif_Flnum(%d, %d): Could not find entity!\n", net_type, net_name));
60 #endif // ifndef MENUQC
63 // ===============================
64 // Frontend Notification Pushing
65 // ===============================
67 void Dump_Notifications(float fh, float alsoprint)
69 float MSG_INFO_NOTIFS = 0, MSG_CENTER_NOTIFS = 0, MSG_WEAPON_NOTIFS = 0, MSG_DEATH_NOTIFS = 0;
71 #define NOTIF_WRITE(type,name,text) { \
73 notif_msg = sprintf("seta %s 1 // %s - %s\n", name, #type, strreplace("\n", "\\n", text)); \
74 fputs(fh, notif_msg); \
75 if(alsoprint) { print(strreplace("^", "^^", notif_msg)); } }
82 for(i = 0; i < NOTIF_INFO_COUNT; ++i) {
83 e = Get_Notif_Ent(MSG_INFO, i);
84 NOTIF_WRITE(MSG_INFO, e.nent_name, e.nent_string); }
86 for(i = 0; i < NOTIF_CENTER_COUNT; ++i) {
87 e = Get_Notif_Ent(MSG_CENTER, i);
88 NOTIF_WRITE(MSG_CENTER, e.nent_name, e.nent_string); }
90 for(i = 0; i < NOTIF_WEAPON_COUNT; ++i) {
91 e = Get_Notif_Ent(MSG_WEAPON, i);
92 NOTIF_WRITE(MSG_WEAPON, e.nent_name, sprintf("infoname: %s, centername: %s",
93 Get_Notif_Name(MSG_INFO, Get_Notif_Infval(MSG_WEAPON, i)),
94 Get_Notif_Name(MSG_CENTER, Get_Notif_Cenval(MSG_WEAPON, i)) ) ); }
96 for(i = 0; i < NOTIF_DEATH_COUNT; ++i) {
97 e = Get_Notif_Ent(MSG_DEATH, i);
98 NOTIF_WRITE(MSG_DEATH, e.nent_name, sprintf("infoname: %s, centername: %s",
99 Get_Notif_Name(MSG_INFO, Get_Notif_Infval(MSG_DEATH, i)),
100 Get_Notif_Name(MSG_CENTER, Get_Notif_Cenval(MSG_DEATH, i)) ) ); }
104 print(sprintf("Notification counts: MSG_INFO = %d, MSG_CENTER = %d, MSG_WEAPON = %d, MSG_DEATH = %d\n",
105 MSG_INFO_NOTIFS, MSG_CENTER_NOTIFS, MSG_WEAPON_NOTIFS, MSG_DEATH_NOTIFS));
113 void HUD_Notify_Push(string icon, string attacker, string victim)
118 if (kn_index == -1) { kn_index = KN_MAX_ENTRIES-1; }
119 killnotify_times[kn_index] = time;
122 if(killnotify_icon[kn_index]) { strunzone(killnotify_icon[kn_index]); }
123 killnotify_icon[kn_index] = strzone(icon);
126 if(killnotify_attackers[kn_index]) { strunzone(killnotify_attackers[kn_index]); }
127 killnotify_attackers[kn_index] = strzone(attacker);
130 if(killnotify_victims[kn_index]) { strunzone(killnotify_victims[kn_index]); }
131 killnotify_victims[kn_index] = strzone(victim);
137 string Local_Notification_sprintf(string input, string args,
138 string s1, string s2, string s3, string s4,
139 float f1, float f2, float f3, float f4)
141 #ifdef NOTIFICATIONS_DEBUG
143 sprintf("Local_Notification_sprintf('%s', '%s', %s, %s);\n",
145 sprintf("%s, %s, %s, %s", s1, s2, s3, s4),
146 sprintf("%d, %d, %d, %d", f1, f2, f3, f4)));
149 string selected, remaining = args;
156 if((remaining != "") && (input != ""))
160 selected = car(remaining); remaining = cdr(remaining);
162 switch(strtolower(selected))
164 #define ADD_ARG_CASE(selected,result) case selected: { arg_slot[sel_num] = result; ++sel_num; break; }
166 ADD_ARG_CASE("s1", s1)
167 ADD_ARG_CASE("s2", s2)
168 ADD_ARG_CASE("s3", s3)
169 ADD_ARG_CASE("s4", s4)
170 ADD_ARG_CASE("f1", ftos(f1))
171 ADD_ARG_CASE("f2", ftos(f2))
172 ADD_ARG_CASE("f3", ftos(f3))
173 ADD_ARG_CASE("f4", ftos(f4))
175 #ifdef CSQC // CSQC replacements
176 ADD_ARG_CASE("pass_key", ((((tmp_s = getcommandkey("pass", "+use")) != "pass") && !(strstrofs(tmp_s, "not bound", 0) >= 0)) ? sprintf(CCR(_(" ^F1(Press %s)")), tmp_s) : ""))
177 ADD_ARG_CASE("frag_ping", ((f2 != BOT_PING) ? sprintf(CCR(_("\n(Ping ^2%d^BG)")), f2) : ""))
178 ADD_ARG_CASE("frag_stats", sprintf(CCR(_("\n(Health ^1%d^BG / Armor ^2%d^BG)%s")), f1, f2, ((f3 != BOT_PING) ? sprintf(CCR(_(" (Ping ^2%d^BG)")), f3) : "")))
179 //ADD_ARG_CASE("frag_pos", ((Should_Print_Score_Pos(f1)) ? sprintf("\n^BG%s", Read_Score_Pos(f1)) : ""))
180 ADD_ARG_CASE("spree_cen", (((f1 == 3) || (f1 == 5) || (f1 == 10) || (f1 == 15) || (f1 == 20) || (f1 == 25) || (f1 == 30)) ? sprintf(normal_or_gentle(_("%d frag spree! "), _("%d score spree! ")), f1) : ""))
181 ADD_ARG_CASE("spree_inf", (((f1 == 3) || (f1 == 5) || (f1 == 10) || (f1 == 15) || (f1 == 20) || (f1 == 25) || (f1 == 30)) ? sprintf(normal_or_gentle(_("%d frag spree! "), _("%d score spree! ")), f1) : ""))
182 ADD_ARG_CASE("spree_end", ((f1 >= 3) ? sprintf(normal_or_gentle(_(", ending their %d frag spree"), _(", ending their %d score spree")), f1) : ""))
183 ADD_ARG_CASE("spree_lost", ((f1 >= 3) ? sprintf(normal_or_gentle(_(", losing their %d frag spree"), _(", losing their %d score spree")), f1) : ""))
184 ADD_ARG_CASE("death_team", Team_ColoredFullName(f1 - 1))
185 ADD_ARG_CASE("weapon_name", ftos(f1)) // weaponorder[f1].netname
187 #else // SVQC replacements
188 ADD_ARG_CASE("spree_inf", (((f1 == 3) || (f1 == 5) || (f1 == 10) || (f1 == 15) || (f1 == 20) || (f1 == 25) || (f1 == 30)) ? sprintf(normal_or_gentle(_("%d frag spree! "), _("%d score spree! ")), f1) : ""))
189 ADD_ARG_CASE("spree_end", ((f1 >= 3) ? sprintf(normal_or_gentle(_(", ending their %d frag spree"), _(", ending their %d score spree")), f1) : ""))
190 ADD_ARG_CASE("spree_lost", ((f1 >= 3) ? sprintf(normal_or_gentle(_(", losing their %d frag spree"), _(", losing their %d score spree")), f1) : ""))
191 ADD_ARG_CASE("death_team", Team_ColoredFullName(f1))
192 ADD_ARG_CASE("weapon_name", ftos(f1)) // weaponorder[f1].netname
196 default: { backtrace(sprintf("Hit unknown token in selected string! '%s'\n", selected)); break; }
198 if(sel_num == 6) { backtrace("Hit maximum arguments!\n"); break; }
201 return sprintf(input, arg_slot[0], arg_slot[1], arg_slot[2], arg_slot[3], arg_slot[4], arg_slot[5], arg_slot[6]);
207 void Local_Notification(float net_type, float net_name, ...count)
209 // check supplied type and name for errors
210 #ifdef NOTIFICATIONS_DEBUG
212 #define CHECKARG_TYPENAME(type) case MSG_##type##: \
213 { if(!net_name || (net_name > NOTIF_##type##_COUNT)) \
214 { checkargs = sprintf("Improper name: %d!", net_name); } break; }
216 string checkargs = "";
219 CHECKARG_TYPENAME(INFO)
220 CHECKARG_TYPENAME(CENTER)
221 CHECKARG_TYPENAME(WEAPON)
222 CHECKARG_TYPENAME(DEATH)
224 default: { checkargs = sprintf("Improper type: %d!", checkargs, net_type); break; }
226 #undef CHECKARG_TYPENAME
228 if(checkargs != "") { backtrace(sprintf("Incorrect usage of Local_Notification: %s\n", checkargs)); return; }
232 entity notif = Get_Notif_Ent(net_type, net_name);
233 if not(notif) { backtrace("Local_Notification: Could not find notification entity! (This wasn't caught by usage check?...)\n"); return; }
234 if not(notif.nent_enabled) { print("entity notification was disabled...\n"); return; }
236 string s1 = ((0 < notif.nent_stringcount) ? ...(0, string) : NO_STR_ARG);
237 string s2 = ((1 < notif.nent_stringcount) ? ...(1, string) : NO_STR_ARG);
238 string s3 = ((2 < notif.nent_stringcount) ? ...(2, string) : NO_STR_ARG);
239 string s4 = ((3 < notif.nent_stringcount) ? ...(3, string) : NO_STR_ARG);
240 float f1 = ((notif.nent_stringcount < count) ? ...(notif.nent_stringcount, float) : NO_FL_ARG);
241 float f2 = (((notif.nent_stringcount + 1) < count) ? ...((notif.nent_stringcount + 1), float) : NO_FL_ARG);
242 float f3 = (((notif.nent_stringcount + 2) < count) ? ...((notif.nent_stringcount + 2), float) : NO_FL_ARG);
243 float f4 = (((notif.nent_stringcount + 3) < count) ? ...((notif.nent_stringcount + 3), float) : NO_FL_ARG);
245 #ifdef NOTIFICATIONS_DEBUG
247 dprint(sprintf("Local_Notification(%d, %s, %s, %s, %s, %s, %d, %d, %d, %d);\n",
248 net_type, notif.nent_name,
249 s1, s2, s3, s4, f1, f2, f3, f4));
251 if((notif.nent_stringcount + notif.nent_floatcount) > count)
252 { backtrace(sprintf(strcat("Not enough arguments for Send_Notification! stringcount(%d) + floatcount(%d) > count(%d)\n",
253 "Check the notification definition and function call for accuracy...?\n"), notif.nent_stringcount, notif.nent_floatcount, count)); return; }
254 else if((notif.nent_stringcount + notif.nent_floatcount) < count)
255 { backtrace(sprintf(strcat("Too many arguments for Send_Notification! stringcount(%d) + floatcount(%d) < count(%d)\n",
256 "Check the notification definition and function call for accuracy...?\n"), notif.nent_stringcount, notif.nent_floatcount, count)); return; }
264 print(Local_Notification_sprintf(notif.nent_string, notif.nent_args,
265 s1, s2, s3, s4, f1, f2, f3, f4));
268 if(notif.nent_icon != "") { HUD_Notify_Push(notif.nent_icon, s1, s2); }
276 centerprint_generic(notif.nent_cpid,
277 Local_Notification_sprintf(notif.nent_string, notif.nent_args,
278 s1, s2, s3, s4, f1, f2, f3, f4), 0, 0);
286 if(notif.nent_infoname)
287 if(msg_info_notifs[notif.nent_infoname - 1].nent_enabled)
289 Local_Notification_Without_VarArgs(MSG_INFO, notif.nent_infoname,
290 Get_Notif_Strnum(MSG_INFO, notif.nent_infoname),
291 Get_Notif_Flnum(MSG_INFO, notif.nent_infoname),
292 s1, s2, s3, s4, f1, f2, f3, f4);
295 if(notif.nent_centername)
296 if(msg_center_notifs[notif.nent_centername - 1].nent_enabled)
298 Local_Notification_Without_VarArgs(MSG_CENTER, notif.nent_centername,
299 Get_Notif_Strnum(MSG_CENTER, notif.nent_centername),
300 Get_Notif_Flnum(MSG_CENTER, notif.nent_centername),
301 s1, s2, s3, s4, f1, f2, f3, f4);
309 void Local_Notification_Without_VarArgs(float net_type, float net_name,
310 float stringcount, float floatcount,
311 string s1, string s2, string s3, string s4,
312 float f1, float f2, float f3, float f4)
314 #define VARITEM(stringc,floatc,args) if((stringcount == stringc) && (floatcount == floatc)) { Local_Notification(net_type, net_name, args); return; }
315 EIGHT_VARS_TO_VARARGS_VARLIST
318 Local_Notification(net_type, net_name); // some notifications don't have any arguments at all
322 // =========================
323 // Notification Networking
324 // =========================
327 void Read_Notification(float is_new)
329 float net_type = ReadByte();
330 float net_name = ReadShort();
332 float stringcount = Get_Notif_Strnum(net_type, net_name);
333 float floatcount = Get_Notif_Flnum(net_type, net_name);
335 string s1 = ((stringcount >= 1) ? ReadString() : NO_STR_ARG);
336 string s2 = ((stringcount >= 2) ? ReadString() : NO_STR_ARG);
337 string s3 = ((stringcount >= 3) ? ReadString() : NO_STR_ARG);
338 string s4 = ((stringcount == 4) ? ReadString() : NO_STR_ARG);
339 float f1 = ((floatcount >= 1) ? ReadLong() : NO_FL_ARG);
340 float f2 = ((floatcount >= 2) ? ReadLong() : NO_FL_ARG);
341 float f3 = ((floatcount >= 3) ? ReadLong() : NO_FL_ARG);
342 float f4 = ((floatcount == 4) ? ReadLong() : NO_FL_ARG);
344 #ifdef NOTIFICATIONS_DEBUG
345 dprint(sprintf("Read_Notification(%d) at %f: net_name = %s.\n", is_new, time, Get_Notif_Name(net_type, net_name)));
348 if(is_new) { Local_Notification_Without_VarArgs(net_type, net_name, stringcount, floatcount, s1, s2, s3, s4, f1, f2, f3, f4); }
353 void Notification_Remove()
356 for(i = 0; i < 4; ++i) { if(self.nent_strings[i]) { strunzone(self.nent_strings[i]); } }
360 float Write_Notification(entity client, float sf)
362 float i, send = FALSE;
364 switch(self.nent_broadcast)
366 case NOTIF_ONE: { if((client == self.nent_client) || (client.classname == STR_SPECTATOR && client.enemy == self.nent_client)) { send = TRUE; } break; }
367 case NOTIF_ONE_ONLY: { if(client == self.nent_client) { send = TRUE; } break; }
368 case NOTIF_TEAM: { if((client.team == self.nent_client.team) || (client.classname == STR_SPECTATOR && client.enemy.team == self.nent_client.team)) { send = TRUE; } break; }
369 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; }
370 case NOTIF_ANY: { send = TRUE; break; }
371 case NOTIF_ANY_EXCEPT: { if((client != self.nent_client) && !(client.classname == STR_SPECTATOR && client.enemy == self.nent_client)) { send = TRUE; } break; }
372 default: { send = FALSE; break; }
377 WriteByte(MSG_ENTITY, ENT_CLIENT_NOTIFICATION);
378 WriteByte(MSG_ENTITY, self.nent_net_type);
379 WriteShort(MSG_ENTITY, self.nent_net_name);
380 for(i = 0; i < self.nent_stringcount; ++i) { WriteString(MSG_ENTITY, self.nent_strings[i]); }
381 for(i = 0; i < self.nent_floatcount; ++i) { WriteLong(MSG_ENTITY, self.nent_floats[i]); }
387 void Send_Notification(float broadcast, entity client,
388 float net_type, float net_name, ...count)
390 // check supplied broadcast, target, type, and name for errors
391 #ifdef NOTIFICATIONS_DEBUG
393 #define CHECKARG_TYPENAME(type) case MSG_##type##: \
394 { if(!net_name || (net_name > NOTIF_##type##_COUNT)) \
395 { checkargs = sprintf("Improper name: %d!", net_name); } break; }
397 string checkargs = "";
400 CHECKARG_TYPENAME(INFO)
401 CHECKARG_TYPENAME(CENTER)
402 CHECKARG_TYPENAME(WEAPON)
403 CHECKARG_TYPENAME(DEATH)
405 default: { checkargs = sprintf("Improper type: %d!", checkargs, net_type); break; }
407 #undef CHECKARG_TYPENAME
409 if(checkargs != "") { checkargs = strcat(checkargs, " "); }
413 case NOTIF_ONE_ONLY: { if(clienttype(client) == CLIENTTYPE_NOTACLIENT) { checkargs = sprintf("%sNo client provided!", checkargs); } break; }
414 case NOTIF_ANY_EXCEPT: { if(clienttype(client) == CLIENTTYPE_NOTACLIENT) { checkargs = sprintf("%sException can't be a non-client!", checkargs); } break; }
415 case NOTIF_ANY: { if(client) { checkargs = sprintf("%sEntity provided when world was required!", checkargs); } break; }
418 case NOTIF_TEAM_EXCEPT:
420 if not(teamplay) { checkargs = sprintf("%sTeamplay not active!", checkargs); }
421 else if(clienttype(client) == CLIENTTYPE_NOTACLIENT)
423 if(broadcast == NOTIF_TEAM) { checkargs = sprintf("%sNo client provided!", checkargs); }
424 else { checkargs = sprintf("%sException can't be a non-client!", checkargs); }
429 default: { checkargs = sprintf("%sImproper broadcast: %d!", checkargs, broadcast); break; }
432 if(checkargs != "") { backtrace(sprintf("Incorrect usage of Send_Notification: %s\n", checkargs)); return; }
436 // retreive counts for the arguments of this notification
437 float stringcount = Get_Notif_Strnum(net_type, net_name);
438 float floatcount = Get_Notif_Flnum(net_type, net_name);
440 #ifdef NOTIFICATIONS_DEBUG
442 dprint(sprintf("Send_Notification(%d, %d, %s, stringcount: %d, floatcount: %d, varargs: %d);\n",
443 broadcast, net_type, Get_Notif_Name(net_type, net_name), stringcount, floatcount, count));
445 if((stringcount + floatcount) > count)
446 { backtrace(sprintf(strcat("Not enough arguments for Send_Notification! stringcount(%d) + floatcount(%d) > count(%d)\n",
447 "Check the notification definition and function call for accuracy...?\n"), stringcount, floatcount, count)); return; }
448 else if((stringcount + floatcount) < count)
449 { backtrace(sprintf(strcat("Too many arguments for Send_Notification! stringcount(%d) + floatcount(%d) < count(%d)\n",
450 "Check the notification definition and function call for accuracy...?\n"), stringcount, floatcount, count)); return; }
454 entity notif = spawn();
455 notif.nent_broadcast = broadcast;
456 notif.nent_client = client;
457 notif.nent_net_type = net_type;
458 notif.nent_net_name = net_name;
459 notif.nent_stringcount = stringcount;
460 notif.nent_floatcount = floatcount;
464 for(i = 0; i < stringcount; ++i) { notif.nent_strings[i] = strzone(...(i, string)); }
465 for(i = 0; i < floatcount; ++i) { notif.nent_floats[i] = ...((stringcount + i), float); }
467 notif.think = Notification_Remove;
468 notif.nextthink = (time + 0.5);
470 Net_LinkEntity(notif, FALSE, 0, Write_Notification);
472 if((!server_is_local) && (broadcast == NOTIF_ANY || broadcast == NOTIF_ANY_EXCEPT) && (net_type != MSG_CENTER))
473 { Local_Notification_Without_VarArgs(net_type, net_name, stringcount, floatcount,
474 IFSTR(0), IFSTR(1), IFSTR(2), IFSTR(3), IFFL(0), IFFL(1), IFFL(2), IFFL(3)); }
477 void Send_Notification_Without_VarArgs(float broadcast, entity client,
478 float net_type, float net_name,
479 float stringcount, float floatcount,
480 string s1, string s2, string s3, string s4,
481 float f1, float f2, float f3, float f4)
483 #define VARITEM(stringc,floatc,args) if((stringcount == stringc) && (floatcount == floatc)) { Send_Notification(broadcast, client, net_type, net_name, args); return; }
484 EIGHT_VARS_TO_VARARGS_VARLIST
487 Send_Notification(broadcast, client, net_type, net_name); // some notifications don't have any arguments at all
490 void Send_Notification_Legacy_Wrapper(float broadcast, entity client,
491 float net_type, float net_name,
492 string s1, string s2,
493 float f1, float f2, float f3)
495 float stringcount = Get_Notif_Strnum(net_type, net_name);
496 float floatcount = Get_Notif_Flnum(net_type, net_name);
497 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);
501 // =============================
502 // LEGACY NOTIFICATION SYSTEMS
503 // =============================
505 void Send_CSQC_Centerprint_Generic(entity e, float id, string s, float duration, float countdown_num)
507 if ((clienttype(e) == CLIENTTYPE_REAL) && (e.flags & FL_CLIENT))
510 WRITESPECTATABLE_MSG_ONE({
511 WriteByte(MSG_ONE, SVC_TEMPENTITY);
512 WriteByte(MSG_ONE, TE_CSQC_CENTERPRINT_GENERIC);
513 WriteByte(MSG_ONE, id);
514 WriteString(MSG_ONE, s);
515 if (id != 0 && s != "")
517 WriteByte(MSG_ONE, duration);
518 WriteByte(MSG_ONE, countdown_num);
523 void Send_CSQC_Centerprint_Generic_Expire(entity e, float id)
525 Send_CSQC_Centerprint_Generic(e, id, "", 1, 0);
528 #endif // ifndef MENUQC