1 // ================================================
2 // Unified notification system, written by Samual
3 // Last updated: February, 2013
4 // ================================================
6 entity Get_Notif_Ent(float net_type, float net_name)
10 case MSG_INFO: return msg_info_notifs[net_name - 1];
11 case MSG_CENTER: return msg_center_notifs[net_name - 1];
12 case MSG_WEAPON: return msg_weapon_notifs[net_name - 1];
13 case MSG_DEATH: return msg_death_notifs[net_name - 1];
15 backtrace(sprintf("Get_Notif_Ent(%d, %d): Improper net type!\n", net_type, net_name));
20 // ===============================
21 // Frontend Notification Pushing
22 // ===============================
24 void Dump_Notifications(float fh, float alsoprint)
26 #define NOTIF_WRITE(a) { \
28 if(alsoprint) { print(a); } }
29 #define NOTIF_WRITE_SETA(name,default,text) { \
32 "seta notification_%s %d \"notif string: %s^7\"\n", \
33 name, default, strreplace("\{3}", "", strreplace("\n", "\\n", text)) \
35 NOTIF_WRITE(notif_msg) }
41 // Note: This warning only applies to the notifications.cfg file that is output...
43 // You ARE supposed to manually edit this function to add i.e. hard coded
44 // notification variables for mutators or game modes or such and then
45 // regenerate the notifications.cfg file from the new code.
47 NOTIF_WRITE("// ********************************************** //\n");
48 NOTIF_WRITE("// ** WARNING - DO NOT MANUALLY EDIT THIS FILE ** //\n");
49 NOTIF_WRITE("// ** ** //\n");
50 NOTIF_WRITE("// ** This file is automatically generated ** //\n");
51 NOTIF_WRITE("// ** by code with the command 'dumpnotifs'. ** //\n");
52 NOTIF_WRITE("// ** ** //\n");
53 NOTIF_WRITE("// ** If you add a new notification, please ** //\n");
54 NOTIF_WRITE("// ** regenerate this file with that command ** //\n");
55 NOTIF_WRITE("// ** making sure that the output matches ** //\n");
56 NOTIF_WRITE("// ** with the lists and defaults in code. ** //\n");
57 NOTIF_WRITE("// ** ** //\n");
58 NOTIF_WRITE("// ********************************************** //\n");
60 // These notifications will also append their string as a comment...
61 // This is not necessary, and does not matter if they vary between config versions,
62 // it is just a semi-helpful tool for those who want to manually change their user settings.
64 NOTIF_WRITE(sprintf("\n// MSG_INFO notifications (count = %d):\n", NOTIF_INFO_COUNT));
65 for(i = 1; i <= NOTIF_INFO_COUNT; ++i)
67 e = Get_Notif_Ent(MSG_INFO, i);
68 if not(e) { backtrace("Dump_Notifications(): Missing notification entity!\n"); return; }
69 NOTIF_WRITE_SETA(e.nent_name, e.nent_default, e.nent_string);
72 NOTIF_WRITE(sprintf("\n// MSG_CENTER notifications (count = %d):\n", NOTIF_CENTER_COUNT));
73 for(i = 1; i <= NOTIF_CENTER_COUNT; ++i)
75 e = Get_Notif_Ent(MSG_CENTER, i);
76 if not(e) { backtrace("Dump_Notifications(): Missing notification entity!\n"); return; }
77 NOTIF_WRITE_SETA(e.nent_name, e.nent_default, e.nent_string);
80 NOTIF_WRITE(sprintf("\n// MSG_WEAPON notifications (count = %d):\n", NOTIF_WEAPON_COUNT));
81 for(i = 1; i <= NOTIF_WEAPON_COUNT; ++i)
83 e = Get_Notif_Ent(MSG_WEAPON, i);
84 if not(e) { backtrace("Dump_Notifications(): Missing notification entity!\n"); return; }
85 NOTIF_WRITE_SETA(e.nent_name, e.nent_default, sprintf("infoname: %s, centername: %s",
86 e.nent_msginfo.nent_name, e.nent_msgcenter.nent_name));
89 NOTIF_WRITE(sprintf("\n// MSG_DEATH notifications (count = %d):\n", NOTIF_DEATH_COUNT));
90 for(i = 1; i <= NOTIF_DEATH_COUNT; ++i)
92 e = Get_Notif_Ent(MSG_DEATH, i);
93 if not(e) { backtrace("Dump_Notifications(): Missing notification entity!\n"); return; }
94 NOTIF_WRITE_SETA(e.nent_name, e.nent_default, sprintf("infoname: %s, centername: %s",
95 e.nent_msginfo.nent_name, e.nent_msgcenter.nent_name));
98 // edit these to match whichever cvars are used for specific notification options
99 NOTIF_WRITE("\n// HARD CODED notification variables:\n");
100 NOTIF_WRITE("seta notification_allow_chatboxprint 1 \"Allow notifications to be printed to chat box by setting notification cvar to 2 (You can also set this cvar to 2 to force ALL notifications to be printed to the chatbox)\"\n");
101 NOTIF_WRITE("seta notification_show_sprees 1 \"Print information about sprees in death/kill messages\"\n");
102 NOTIF_WRITE("seta notification_show_sprees_center 1 \"Show spree information in MSG_CENTER messages... 0 = off, 1 = target (but only for first victim) and attacker\"\n");
103 NOTIF_WRITE("seta notification_show_sprees_center_specialonly 1 \"Don't show spree information in MSG_CENTER messages if it isn't an achievement\"\n");
104 NOTIF_WRITE("seta notification_show_sprees_info 3 \"Show spree information in MSG_INFO messages... 0 = off, 1 = target only, 2 = attacker only, 3 = target and attacker\"\n");
105 NOTIF_WRITE("seta notification_show_sprees_info_newline 0 \"Show attacker spree information for MSG_INFO messages on a separate line than the death notification itself\"\n");
106 NOTIF_WRITE("seta notification_show_sprees_info_specialonly 1 \"Don't show attacker spree information in MSG_INFO messages if it isn't an achievement\"\n");
107 NOTIF_WRITE("seta notification_version_mismatch_client_error 0 \"Cause a notif error on client if the version in notifications.cfg mismatches the code\"\n");
108 NOTIF_WRITE("seta notification_version_mismatch_server_error 1 \"Cause a notif error on server if the version in notifications.cfg mismatches the code\"\n");
109 NOTIF_WRITE("seta notification_errors_are_fatal 1 \"If a notification fails upon initialization, cause a Host_Error to stop the program\"\n");
110 NOTIF_WRITE("seta notification_ctf_pickup_team_verbose 1 \"Show extra information if a team mate picks up a flag\"\n");
111 NOTIF_WRITE("seta notification_ctf_pickup_enemy_verbose 1 \"Show extra information if an enemy picks up a flag\"\n");
112 NOTIF_WRITE("seta notification_ctf_capture_verbose 1 \"Show extra information when someone captures a flag\"\n");
113 NOTIF_WRITE("seta notification_frag_verbose 1 \"Show extra information when you frag someone (or when you are fragged\"\n");
115 NOTIF_WRITE(sprintf("\n// Notification counts (total = %d): MSG_INFO = %d, MSG_CENTER = %d, MSG_WEAPON = %d, MSG_DEATH = %d\n",
116 (NOTIF_INFO_COUNT + NOTIF_CENTER_COUNT + NOTIF_WEAPON_COUNT + NOTIF_DEATH_COUNT),
117 NOTIF_INFO_COUNT, NOTIF_CENTER_COUNT, NOTIF_WEAPON_COUNT, NOTIF_DEATH_COUNT));
120 #undef NOTIF_WRITE_SETA
125 void Notification_GetCvars()
127 GetCvars_handleFloat(get_cvars_s, get_cvars_f, FRAG_VERBOSE, "notification_frag_verbose");
131 string Local_Notification_sprintf(string input, string args,
132 string s1, string s2, string s3, string s4,
133 float f1, float f2, float f3, float f4)
135 #ifdef NOTIFICATIONS_DEBUG
137 sprintf("Local_Notification_sprintf('%s^7', '%s', %s, %s);\n",
138 strreplace("\n", "\\n", input),
140 sprintf("'%s^7', '%s^7', '%s^7', '%s^7'", s1, s2, s3, s4),
141 sprintf("%d, %d, %d, %d", f1, f2, f3, f4)
148 for(sel_num = 0; sel_num < NOTIF_MAX_ARGS; ++sel_num) { arg_slot[sel_num] = ""; }
154 for(sel_num = 0;(args != "");)
156 selected = car(args); args = cdr(args);
157 NOTIF_HIT_MAX(NOTIF_MAX_ARGS, "Local_Notification_sprintf")
158 switch(strtolower(selected))
160 #define ARG_CASE(prog,selected,result) \
162 #if (prog != ARG_SV) \
163 case selected: { arg_slot[sel_num] = result; ++sel_num; break; } \
166 #if (prog != ARG_CS) \
167 case selected: { arg_slot[sel_num] = result; ++sel_num; break; } \
172 default: NOTIF_HIT_UNKNOWN(NOTIF_MAX_ARGS, "Local_Notification_sprintf")
175 return sprintf(input, arg_slot[0], arg_slot[1], arg_slot[2], arg_slot[3], arg_slot[4], arg_slot[5], arg_slot[6]);
179 void Local_Notification_HUD_Notify_Push(string icon, string hudargs, string s1, string s2, string s3, string s4)
183 arg_slot[0] = ""; arg_slot[1] = "";
185 for(sel_num = 0;(hudargs != "");)
187 selected = car(hudargs); hudargs = cdr(hudargs);
188 NOTIF_HIT_MAX(NOTIF_MAX_HUDARGS, "Local_Notification_HUD_Notify_Push")
189 switch(strtolower(selected))
191 #define ARG_CASE(prog,selected,result) \
192 #if (prog == ARG_CS_SV_HA) \
193 case selected: { arg_slot[sel_num] = result; ++sel_num; break; } \
197 default: NOTIF_HIT_UNKNOWN(NOTIF_MAX_HUDARGS, "Local_Notification_HUD_Notify_Push")
200 HUD_Notify_Push(icon, arg_slot[0], arg_slot[1]);
203 void Local_Notification_centerprint_generic(string input, string durcnt, float cpid, float f1, float f2)
207 arg_slot[0] = ""; arg_slot[1] = "";
209 for(sel_num = 0;(durcnt != "");)
211 selected = car(durcnt); durcnt = cdr(durcnt);
212 NOTIF_HIT_MAX(NOTIF_MAX_DURCNT, "Local_Notification_centerprint_generic")
213 switch(strtolower(selected))
215 #define ARG_CASE(prog,selected,result) \
216 #if (prog == ARG_CS_SV_DC) \
217 case selected: { arg_slot[sel_num] = result; ++sel_num; break; } \
223 if(ftos(stof(selected)) != "") { arg_slot[sel_num] = selected; ++sel_num; }
224 else { NOTIF_HIT_UNKNOWN(NOTIF_MAX_DURCNT, "Local_Notification_centerprint_generic") }
229 centerprint_generic(cpid, input, stof(arg_slot[0]), stof(arg_slot[1]));
233 void Local_Notification(float net_type, float net_name, ...count)
235 // check supplied type and name for errors
236 string checkargs = "";
237 #define CHECKARG_TYPENAME(type) case MSG_##type##: \
238 { if(!net_name || (net_name > NOTIF_##type##_COUNT)) \
239 { checkargs = sprintf("Improper name: %d!", net_name); } break; }
242 CHECKARG_TYPENAME(INFO)
243 CHECKARG_TYPENAME(CENTER)
244 CHECKARG_TYPENAME(WEAPON)
245 CHECKARG_TYPENAME(DEATH)
246 default: { checkargs = sprintf("Improper type: %d!", checkargs, net_type); break; }
248 #undef CHECKARG_TYPENAME
249 if(checkargs != "") { backtrace(sprintf("Incorrect usage of Local_Notification: %s\n", checkargs)); return; }
251 entity notif = Get_Notif_Ent(net_type, net_name);
252 if not(notif) { backtrace("Local_Notification: Could not find notification entity!\n"); return; }
253 if not(notif.nent_enabled) { print("Local_Notification: Entity was disabled...\n"); return; }
255 if((notif.nent_stringcount + notif.nent_floatcount) > count)
259 "Not enough arguments for Local_Notification(%d, %s, ...)! ",
260 "stringcount(%d) + floatcount(%d) > count(%d)\n",
261 "Check the definition and function call for accuracy...?\n"
263 net_type, notif.nent_name, notif.nent_stringcount, notif.nent_floatcount, count));
266 else if((notif.nent_stringcount + notif.nent_floatcount) < count)
270 "Too many arguments for Local_Notification(%d, %s, ...)! ",
271 "stringcount(%d) + floatcount(%d) < count(%d)\n",
272 "Check the definition and function call for accuracy...?\n"
274 net_type, notif.nent_name, notif.nent_stringcount, notif.nent_floatcount, count));
278 string s1 = ((0 < notif.nent_stringcount) ? ...(0, string) : "");
279 string s2 = ((1 < notif.nent_stringcount) ? ...(1, string) : "");
280 string s3 = ((2 < notif.nent_stringcount) ? ...(2, string) : "");
281 string s4 = ((3 < notif.nent_stringcount) ? ...(3, string) : "");
282 float f1 = ((0 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 0), float) : 0);
283 float f2 = ((1 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 1), float) : 0);
284 float f3 = ((2 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 2), float) : 0);
285 float f4 = ((3 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 3), float) : 0);
287 #ifdef NOTIFICATIONS_DEBUG
289 sprintf("Local_Notification(%d, %s, %s, %s);\n",
292 sprintf("'%s^7', '%s^7', '%s^7', '%s^7'", s1, s2, s3, s4),
293 sprintf("%d, %d, %d, %d", f1, f2, f3, f4)
303 Local_Notification_sprintf(
310 if(notif.nent_icon != "")
312 Local_Notification_HUD_Notify_Push(
324 Local_Notification_centerprint_generic(
325 Local_Notification_sprintf(
340 if(notif.nent_msginfo)
341 if(notif.nent_msginfo.nent_enabled)
343 Local_Notification_WOVA(
345 notif.nent_msginfo.nent_id,
346 notif.nent_msginfo.nent_stringcount,
347 notif.nent_msginfo.nent_floatcount,
352 if(notif.nent_msgcenter)
353 if(notif.nent_msgcenter.nent_enabled)
355 Local_Notification_WOVA(
357 notif.nent_msgcenter.nent_id,
358 notif.nent_msgcenter.nent_stringcount,
359 notif.nent_msgcenter.nent_floatcount,
369 // WOVA = Without Variable Arguments
370 void Local_Notification_WOVA(float net_type, float net_name,
371 float stringcount, float floatcount,
372 string s1, string s2, string s3, string s4,
373 float f1, float f2, float f3, float f4)
375 #define VARITEM(stringc,floatc,args) \
376 if((stringcount == stringc) && (floatcount == floatc)) \
377 { Local_Notification(net_type, net_name, args); return; }
378 EIGHT_VARS_TO_VARARGS_VARLIST
380 Local_Notification(net_type, net_name); // some notifications don't have any arguments at all
384 // =========================
385 // Notification Networking
386 // =========================
389 void Read_Notification(float is_new)
391 float net_type = ReadByte();
392 float net_name = ReadShort();
394 entity notif = Get_Notif_Ent(net_type, net_name);
395 if not(notif) { print("Read_Notification: Could not find notification entity!\n"); return; }
397 string s1 = ((0 < notif.nent_stringcount) ? ReadString() : "");
398 string s2 = ((1 < notif.nent_stringcount) ? ReadString() : "");
399 string s3 = ((2 < notif.nent_stringcount) ? ReadString() : "");
400 string s4 = ((3 < notif.nent_stringcount) ? ReadString() : "");
401 float f1 = ((0 < notif.nent_floatcount) ? ReadLong() : 0);
402 float f2 = ((1 < notif.nent_floatcount) ? ReadLong() : 0);
403 float f3 = ((2 < notif.nent_floatcount) ? ReadLong() : 0);
404 float f4 = ((3 < notif.nent_floatcount) ? ReadLong() : 0);
406 #ifdef NOTIFICATIONS_DEBUG
407 dprint(sprintf("Read_Notification(%d) at %f: net_name = %s.\n", is_new, time, notif.nent_name));
412 Local_Notification_WOVA(
414 notif.nent_stringcount,
415 notif.nent_floatcount,
423 void Net_Notification_Remove()
426 for(i = 0; i < 4; ++i) { if(self.nent_strings[i]) { strunzone(self.nent_strings[i]); } }
430 float Net_Write_Notification(entity client, float sf)
432 float i, send = FALSE;
434 switch(self.nent_broadcast)
436 case NOTIF_ONE: // send to one client and their spectator
439 (client == self.nent_client)
442 (client.classname == STR_SPECTATOR)
444 (client.enemy == self.nent_client)
449 case NOTIF_ONE_ONLY: // send ONLY to one client
451 if(client == self.nent_client) { send = TRUE; }
454 case NOTIF_TEAM: // send only to X team and their spectators
457 (client.team == self.nent_client.team)
460 (client.classname == STR_SPECTATOR)
462 (client.enemy.team == self.nent_client.team)
467 case NOTIF_TEAM_EXCEPT: // send only to X team and their spectators, except for Y person and their spectators
470 (client != self.nent_client)
473 (client.team == self.nent_client.team)
476 (client.classname == STR_SPECTATOR)
479 (client.enemy != self.nent_client)
481 (client.enemy.team == self.nent_client.team)
488 case NOTIF_ANY: // send to everyone
493 case NOTIF_ANY_EXCEPT: // send to everyone except X person and their spectators
496 (client != self.nent_client)
499 (client.classname == STR_SPECTATOR)
501 (client.enemy == self.nent_client)
506 default: { send = FALSE; break; }
511 WriteByte(MSG_ENTITY, ENT_CLIENT_NOTIFICATION);
512 WriteByte(MSG_ENTITY, self.nent_net_type);
513 WriteShort(MSG_ENTITY, self.nent_net_name);
514 for(i = 0; i < self.nent_stringcount; ++i) { WriteString(MSG_ENTITY, self.nent_strings[i]); }
515 for(i = 0; i < self.nent_floatcount; ++i) { WriteLong(MSG_ENTITY, self.nent_floats[i]); }
521 void Send_Notification(float broadcast, entity client,
522 float net_type, float net_name, ...count)
524 // check supplied broadcast, target, type, and name for errors
525 string checkargs = "";
526 #define CHECKARG_TYPENAME(type) case MSG_##type##: \
527 { if(!net_name || (net_name > NOTIF_##type##_COUNT)) \
528 { checkargs = sprintf("Improper name: %d!", net_name); } break; }
531 CHECKARG_TYPENAME(INFO)
532 CHECKARG_TYPENAME(CENTER)
533 CHECKARG_TYPENAME(WEAPON)
534 CHECKARG_TYPENAME(DEATH)
535 default: { checkargs = sprintf("Improper type: %d!", checkargs, net_type); break; }
537 #undef CHECKARG_TYPENAME
538 if(checkargs != "") { checkargs = strcat(checkargs, " "); }
544 if(IS_NOT_A_CLIENT(client))
545 { checkargs = sprintf("%sNo client provided!", checkargs); }
549 case NOTIF_ANY_EXCEPT:
551 if(IS_NOT_A_CLIENT(client))
552 { checkargs = sprintf("%sException can't be a non-client!", checkargs); }
559 { checkargs = sprintf("%sEntity provided when world was required!", checkargs); }
564 case NOTIF_TEAM_EXCEPT:
566 if not(teamplay) { checkargs = sprintf("%sTeamplay not active!", checkargs); }
567 else if(IS_NOT_A_CLIENT(client))
569 if(broadcast == NOTIF_TEAM) { checkargs = sprintf("%sNo client provided!", checkargs); }
570 else { checkargs = sprintf("%sException can't be a non-client!", checkargs); }
575 default: { checkargs = sprintf("%sImproper broadcast: %d!", checkargs, broadcast); break; }
577 if(checkargs != "") { backtrace(sprintf("Incorrect usage of Send_Notification: %s\n", checkargs)); return; }
579 // retreive counts for the arguments of this notification
580 entity notif = Get_Notif_Ent(net_type, net_name);
581 if not(notif) { backtrace("Send_Notification: Could not find notification entity!\n"); return; }
583 if((notif.nent_stringcount + notif.nent_floatcount) > count)
587 "Not enough arguments for Send_Notification(%d, %d, %s, ...)! ",
588 "stringcount(%d) + floatcount(%d) > count(%d)\n",
589 "Check the definition and function call for accuracy...?\n"
591 broadcast, net_type, notif.nent_name, notif.nent_stringcount, notif.nent_floatcount, count));
594 else if((notif.nent_stringcount + notif.nent_floatcount) < count)
598 "Too many arguments for Send_Notification(%d, %d, %s, ...)! ",
599 "stringcount(%d) + floatcount(%d) < count(%d)\n",
600 "Check the definition and function call for accuracy...?\n"
602 broadcast, net_type, notif.nent_name, notif.nent_stringcount, notif.nent_floatcount, count));
606 #ifdef NOTIFICATIONS_DEBUG
608 sprintf("Send_Notification(%d, %d, %s, stringcount: %d, floatcount: %d, varargs: %d);\n",
612 notif.nent_stringcount,
613 notif.nent_floatcount,
619 entity net_notif = spawn();
620 net_notif.nent_broadcast = broadcast;
621 net_notif.nent_client = client;
622 net_notif.nent_net_type = net_type;
623 net_notif.nent_net_name = net_name;
624 net_notif.nent_stringcount = notif.nent_stringcount;
625 net_notif.nent_floatcount = notif.nent_floatcount;
628 for(i = 0; i < net_notif.nent_stringcount; ++i) { net_notif.nent_strings[i] = strzone(...(i, string)); }
629 for(i = 0; i < net_notif.nent_floatcount; ++i) { net_notif.nent_floats[i] = ...((net_notif.nent_stringcount + i), float); }
631 net_notif.think = Net_Notification_Remove;
632 net_notif.nextthink = (time + 0.5);
634 Net_LinkEntity(net_notif, FALSE, 0, Net_Write_Notification);
636 if((!server_is_local) && (broadcast == NOTIF_ANY || broadcast == NOTIF_ANY_EXCEPT) && (net_type != MSG_CENTER))
638 Local_Notification_WOVA(
640 notif.nent_stringcount,
641 notif.nent_floatcount,
642 IFSTR(0), IFSTR(1), IFSTR(2), IFSTR(3),
643 IFFL(0), IFFL(1), IFFL(2), IFFL(3));
647 // WOVA = Without Variable Arguments
648 void Send_Notification_WOVA(float broadcast, entity client,
649 float net_type, float net_name,
650 string s1, string s2, string s3, string s4,
651 float f1, float f2, float f3, float f4)
653 entity notif = Get_Notif_Ent(net_type, net_name);
654 #define VARITEM(stringc,floatc,args) \
655 if((notif.nent_stringcount == stringc) && (notif.nent_floatcount == floatc)) \
656 { Send_Notification(broadcast, client, net_type, net_name, args); return; }
657 EIGHT_VARS_TO_VARARGS_VARLIST
659 Send_Notification(broadcast, client, net_type, net_name); // some notifications don't have any arguments at all
663 // =============================
664 // LEGACY NOTIFICATION SYSTEMS
665 // =============================
667 void Send_CSQC_Centerprint_Generic(entity e, float id, string s, float duration, float countdown_num)
669 if ((clienttype(e) == CLIENTTYPE_REAL) && (e.flags & FL_CLIENT))
672 WRITESPECTATABLE_MSG_ONE({
673 WriteByte(MSG_ONE, SVC_TEMPENTITY);
674 WriteByte(MSG_ONE, TE_CSQC_CENTERPRINT_GENERIC);
675 WriteByte(MSG_ONE, id);
676 WriteString(MSG_ONE, s);
677 if (id != 0 && s != "")
679 WriteByte(MSG_ONE, duration);
680 WriteByte(MSG_ONE, countdown_num);