]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/notifications.qc
315b2f931388d0706b454c406536428d3b73d7c7
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / notifications.qc
1 #if defined(CSQC)
2         #include "../client/announcer.qh"
3 #elif defined(MENUQC)
4 #elif defined(SVQC)
5         #include "constants.qh"
6         #include "teams.qh"
7         #include "../server/autocvars.qh"
8         #include "../server/constants.qh"
9         #include "../server/defs.qh"
10         #include "notifications.qh"
11         #include <server/mutators/all.qh>
12 #endif
13
14 // ================================================
15 //  Unified notification system, written by Samual
16 //  Last updated: August, 2013
17 // ================================================
18
19 #ifdef SVQC
20 string Notification_CheckArgs(
21         NOTIF broadcast, entity client,
22         Notification net_name)
23 {
24         // check supplied broadcast, target, type, and name for errors
25         string checkargs = "";
26         if (!net_name) { checkargs = "No notification provided! "; }
27         switch (broadcast)
28         {
29                 case NOTIF_ONE:
30                 case NOTIF_ONE_ONLY:
31                 {
32                         if (IS_NOT_A_CLIENT(client)) {
33                                 checkargs = sprintf("%sNo client provided!", checkargs);
34                         }
35                         break;
36                 }
37
38                 case NOTIF_ALL_EXCEPT:
39                 {
40                         if (IS_NOT_A_CLIENT(client)) {
41                                 checkargs = sprintf("%sException can't be a non-client!", checkargs);
42                         }
43                         break;
44                 }
45
46                 case NOTIF_ALL:
47                 {
48                         if (client) {
49                                 checkargs = sprintf("%sEntity provided when world was required!", checkargs);
50                         }
51                         break;
52                 }
53
54                 case NOTIF_TEAM:
55                 {
56                         if (!teamplay) {
57                                 checkargs = sprintf("%sTeamplay not active!", checkargs);
58                         } else if (!client.team) {
59                                 // checkargs = sprintf("%sNo team provided!", checkargs);
60                         }
61                         break;
62                 }
63
64                 case NOTIF_TEAM_EXCEPT:
65                 {
66                         if (!teamplay) {
67                                 checkargs = sprintf("%sTeamplay not active!", checkargs);
68                         } else if (IS_NOT_A_CLIENT(client)) {
69                                 checkargs = sprintf("%sException can't be a non-client!", checkargs);
70                         }
71                         break;
72                 }
73
74                 default:
75                 {
76                         checkargs = sprintf("%sImproper broadcast: %d!", checkargs, broadcast);
77                         break;
78                 }
79         }
80         return checkargs;
81 }
82
83 bool Notification_ShouldSend(NOTIF broadcast, entity to_client, entity other_client)
84 {
85         switch (broadcast)
86         {
87                 case NOTIF_ONE:
88                         return (
89                                 (to_client == other_client)
90                                 ||
91                                 (
92                                         IS_SPEC(to_client)
93                                         &&
94                                         (to_client.enemy == other_client)
95                                 )
96                         );
97                 case NOTIF_ONE_ONLY:
98                         return (to_client == other_client);
99                 case NOTIF_TEAM:
100                         return (
101                                 (to_client.team == other_client.team)
102                                 ||
103                                 (
104                                         IS_SPEC(to_client)
105                                         &&
106                                         (to_client.enemy.team == other_client.team)
107                                 )
108                         );
109                 case NOTIF_TEAM_EXCEPT:
110                         return (
111                                 (to_client != other_client)
112                                 &&
113                                 (
114                                         (to_client.team == other_client.team)
115                                         ||
116                                         (
117                                                 IS_SPEC(to_client)
118                                                 &&
119                                                 (
120                                                         (to_client.enemy != other_client)
121                                                         &&
122                                                         (to_client.enemy.team == other_client.team)
123                                                 )
124                                         )
125                                 )
126                         );
127                 case NOTIF_ALL:
128                         return true;
129                 case NOTIF_ALL_EXCEPT:
130                         return (
131                                 (to_client != other_client)
132                                 &&
133                                 !(
134                                         IS_SPEC(to_client)
135                                         &&
136                                         (to_client.enemy == other_client)
137                                 )
138                         );
139                 default:
140                         return false;
141         }
142 }
143
144 #endif
145
146 // ===============================
147 //  Initialization Core Functions
148 // ===============================
149
150 // used by restartnotifs command to initialize notifications
151 void Destroy_Notification_Entity(entity notif)
152 {
153         if (notif.nent_name != "") strunzone(notif.nent_name);
154         if (notif.nent_snd != "") strunzone(notif.nent_snd);
155         if (notif.nent_args != "") strunzone(notif.nent_args);
156         if (notif.nent_hudargs != "") strunzone(notif.nent_hudargs);
157         if (notif.nent_icon != "") strunzone(notif.nent_icon);
158         if (notif.nent_durcnt != "") strunzone(notif.nent_durcnt);
159         if (notif.nent_string != "") strunzone(notif.nent_string);
160         remove(notif);
161 }
162
163 void Destroy_All_Notifications()
164 {
165         // kill all networked notifications and centerprints
166         #ifdef SVQC
167         Kill_Notification(NOTIF_ALL, NULL, MSG_Null, 0);
168         #else
169         reset_centerprint_messages();
170         #endif
171
172         // kill all real notification entities
173         FOREACH(Notifications, true, { Destroy_Notification_Entity(it); });
174 }
175
176 string Process_Notif_Line(
177         MSG typeId,
178         bool chat,
179         string input,
180         string notiftype,
181         string notifname,
182         string stringtype)
183 {
184         #ifdef CSQC
185         if(typeId == MSG_INFO)
186         {
187                 if((chat && autocvar_notification_allow_chatboxprint)
188                         || (autocvar_notification_allow_chatboxprint == 2))
189                 {
190                         // pass 1: add ETX char at beginning of line
191                         input = strcat("\{3}", input);
192
193                         // pass 2: add ETX char at end of each new line (so that
194                         // messages with multiple lines are put through chatbox too)
195                         input = strreplace("\n", "\n\{3}", input);
196
197                         // pass 3: strip trailing ETX char
198                         if(substring(input, (strlen(input) - 1), 1) == "\{3}")
199                                 { input = substring(input, 0, (strlen(input) - 1)); }
200                 }
201         }
202         #endif
203
204         // done to both MSG_INFO and MSG_CENTER
205         if(substring(input, (strlen(input) - 1), 1) == "\n")
206         {
207                 LOG_INFOF(
208                         (
209                                 "^1TRAILING NEW LINE AT END OF NOTIFICATION: "
210                                 "^7net_type = %s, net_name = %s, string = %s.\n"
211                         ),
212                         notiftype,
213                         notifname,
214                         stringtype
215                 );
216                 notif_error = true;
217                 input = substring(input, 1, (strlen(input) - 1));
218         }
219
220         return input;
221 }
222
223 string Process_Notif_Args(
224         float arg_type,
225         string args,
226         string notiftype,
227         string notifname)
228 {
229         string selected, remaining = args;
230         float sel_num = 0;
231
232         for (;(remaining != "");)
233         {
234                 selected = car(remaining); remaining = cdr(remaining);
235
236                 switch(arg_type)
237                 {
238                         case 1: // normal args
239                         {
240                                 if(sel_num == NOTIF_MAX_ARGS)
241                                 {
242                                         LOG_INFOF(
243                                                 (
244                                                         "^1NOTIFICATION HAS TOO MANY ARGUMENTS: "
245                                                         "^7net_type = %s, net_name = %s, max args = %d.\n"
246                                                 ),
247                                                 notiftype,
248                                                 notifname,
249                                                 NOTIF_MAX_ARGS
250                                         );
251                                         notif_error = true;
252                                         break;
253                                 }
254
255                                 switch(strtolower(selected))
256                                 {
257                                         #define ARG_CASE_ARG_CS_SV_HA(selected,result) case selected: { ++sel_num; break; }
258                                         #define ARG_CASE_ARG_CS_SV_DC(selected,result) case selected: { ++sel_num; break; }
259                                         #define ARG_CASE_ARG_CS_SV(selected,result)    case selected: { ++sel_num; break; }
260                                         #define ARG_CASE_ARG_CS(selected,result)       case selected: { ++sel_num; break; }
261                                         #define ARG_CASE_ARG_SV(selected,result)       case selected: { ++sel_num; break; }
262                                         #define ARG_CASE_ARG_DC(selected,result)
263                                         #define ARG_CASE(prog,selected,result)         ARG_CASE_##prog(selected,result)
264                                         NOTIF_ARGUMENT_LIST
265                                         #undef ARG_CASE
266                                         #undef ARG_CASE_ARG_DC
267                                         #undef ARG_CASE_ARG_SV
268                                         #undef ARG_CASE_ARG_CS
269                                         #undef ARG_CASE_ARG_CS_SV
270                                         #undef ARG_CASE_ARG_CS_SV_DC
271                                         #undef ARG_CASE_ARG_CS_SV_HA
272                                         default:
273                                         {
274                                                 LOG_INFOF(
275                                                         (
276                                                                 "^1NOTIFICATION WITH UNKNOWN TOKEN IN ARGUMENT STRING: "
277                                                                 "^7net_type = %s, net_name = %s, args arg = '%s'.\n"
278                                                         ),
279                                                         notiftype,
280                                                         notifname,
281                                                         selected
282                                                 );
283                                                 notif_error = true;
284                                                 break;
285                                         }
286                                 }
287                                 break;
288                         }
289                         case 2: // hudargs
290                         {
291                                 if(sel_num == NOTIF_MAX_HUDARGS)
292                                 {
293                                         LOG_INFOF(
294                                                 (
295                                                         "^1NOTIFICATION HAS TOO MANY ARGUMENTS: "
296                                                         "^7net_type = %s, net_name = %s, max hudargs = %d.\n"
297                                                 ),
298                                                 notiftype,
299                                                 notifname,
300                                                 NOTIF_MAX_HUDARGS
301                                         );
302                                         notif_error = true;
303                                         break;
304                                 }
305
306                                 switch(strtolower(selected))
307                                 {
308                                         #define ARG_CASE_ARG_CS_SV_HA(selected,result) case selected: { ++sel_num; break; }
309                                         #define ARG_CASE_ARG_CS_SV_DC(selected,result)
310                                         #define ARG_CASE_ARG_CS_SV(selected,result)
311                                         #define ARG_CASE_ARG_CS(selected,result)
312                                         #define ARG_CASE_ARG_SV(selected,result)
313                                         #define ARG_CASE_ARG_DC(selected,result)
314                                         #define ARG_CASE(prog,selected,result)         ARG_CASE_##prog(selected,result)
315                                         NOTIF_ARGUMENT_LIST
316                                         #undef ARG_CASE
317                                         #undef ARG_CASE_ARG_DC
318                                         #undef ARG_CASE_ARG_SV
319                                         #undef ARG_CASE_ARG_CS
320                                         #undef ARG_CASE_ARG_CS_SV
321                                         #undef ARG_CASE_ARG_CS_SV_DC
322                                         #undef ARG_CASE_ARG_CS_SV_HA
323                                         default:
324                                         {
325                                                 LOG_INFOF(
326                                                         (
327                                                                 "^1NOTIFICATION WITH UNKNOWN TOKEN IN ARGUMENT STRING: "
328                                                                 "^7net_type = %s, net_name = %s, hudargs arg = '%s'.\n"
329                                                         ),
330                                                         notiftype,
331                                                         notifname,
332                                                         selected
333                                                 );
334                                                 notif_error = true;
335                                                 break;
336                                         }
337                                 }
338                                 break;
339                         }
340                         case 3: // durcnt
341                         {
342                                 if(sel_num == NOTIF_MAX_DURCNT)
343                                 {
344                                         LOG_INFOF(
345                                                 (
346                                                         "^1NOTIFICATION HAS TOO MANY ARGUMENTS: "
347                                                         "^7net_type = %s, net_name = %s, max durcnt = %d.\n"
348                                                 ),
349                                                 notiftype,
350                                                 notifname,
351                                                 NOTIF_MAX_DURCNT
352                                         );
353                                         notif_error = true;
354                                         break;
355                                 }
356
357                                 switch(strtolower(selected))
358                                 {
359                                         #define ARG_CASE_ARG_CS_SV_HA(selected,result)
360                                         #define ARG_CASE_ARG_CS_SV_DC(selected,result) case selected: { ++sel_num; break; }
361                                         #define ARG_CASE_ARG_CS_SV(selected,result)
362                                         #define ARG_CASE_ARG_CS(selected,result)
363                                         #define ARG_CASE_ARG_SV(selected,result)
364                                         #define ARG_CASE_ARG_DC(selected,result)       case selected: { ++sel_num; break; }
365                                         #define ARG_CASE(prog,selected,result)         ARG_CASE_##prog(selected,result)
366                                         NOTIF_ARGUMENT_LIST
367                                         #undef ARG_CASE
368                                         #undef ARG_CASE_ARG_DC
369                                         #undef ARG_CASE_ARG_SV
370                                         #undef ARG_CASE_ARG_CS
371                                         #undef ARG_CASE_ARG_CS_SV
372                                         #undef ARG_CASE_ARG_CS_SV_DC
373                                         #undef ARG_CASE_ARG_CS_SV_HA
374                                         default:
375                                         {
376                                                 if(ftos(stof(selected)) != "") { ++sel_num; }
377                                                 else
378                                                 {
379                                                         LOG_INFOF(
380                                                                 (
381                                                                         "^1NOTIFICATION WITH UNKNOWN TOKEN IN ARGUMENT STRING: "
382                                                                         "^7net_type = %s, net_name = %s, durcnt arg = '%s'.\n"
383                                                                 ),
384                                                                 notiftype,
385                                                                 notifname,
386                                                                 selected
387                                                         );
388                                                         notif_error = true;
389                                                 }
390                                                 break;
391                                         }
392                                 }
393                                 break;
394                         }
395                 }
396         }
397         return args;
398 }
399
400 void Create_Notification_Entity(entity notif,
401         float var_default,
402         float var_cvar,
403         MSG typeId,
404         string namestring)
405 {
406         // =====================
407         //  Global Entity Setup
408         // =====================
409         notif.nent_default = var_default;
410         notif.nent_enabled = (var_cvar >= 1);
411         notif.nent_type = typeId;
412         notif.nent_name = strzone(namestring);
413
414         // Other pre-notif-setup requisites
415         notif_error = false;
416
417         switch (typeId)
418         {
419                 case MSG_ANNCE:
420                 case MSG_INFO:
421                 case MSG_CENTER:
422                 case MSG_MULTI:
423                 case MSG_CHOICE:
424                         break;
425                 default:
426                         LOG_INFOF(
427                                 (
428                                         "^1NOTIFICATION WITH IMPROPER TYPE: "
429                                         "^7net_type = %d, net_name = %s.\n"
430                                 ),
431                                 typeId,
432                                 namestring
433                         );
434                         notif_error = true;
435                         break;
436         }
437
438         // now check to see if any errors happened
439         if (notif_error)
440         {
441                 notif.nent_enabled = false; // disable the notification so it can't cause trouble
442                 notif_global_error = true; // throw the red flag that an error happened on init
443         }
444 }
445
446 void Create_Notification_Entity_Annce(entity notif,
447                                                                                 float var_cvar,
448                                                                                 string namestring,
449                                                                                 /* MSG_ANNCE */
450                                                                                 float channel,
451                                                                                 string snd,
452                                                                                 float vol,
453                                                                                 float position)
454                 {
455                         // Set MSG_ANNCE information and handle precaching
456                         #ifdef CSQC
457                         MSG typeId = MSG_ANNCE;
458                         if (!(GENTLE && (var_cvar == 1)))
459                         {
460                                 if(snd != "")
461                                 {
462                                         if(notif.nent_enabled)
463                                         {
464                                                 precache_sound(sprintf("announcer/%s/%s.wav", AnnouncerOption(), snd));
465                                                 notif.nent_channel = channel;
466                                                 notif.nent_snd = strzone(snd);
467                                                 notif.nent_vol = vol;
468                                                 notif.nent_position = position;
469                                         }
470                                 }
471                                 else
472                                 {
473                                         string typestring = Get_Notif_TypeName(typeId);
474                                         LOG_INFOF(
475                                                 (
476                                                         "^1NOTIFICATION WITH NO SOUND: "
477                                                         "^7net_type = %s, net_name = %s.\n"
478                                                 ),
479                                                 typestring,
480                                                 namestring
481                                         );
482                                         notif_error = true;
483                                 }
484                         }
485                         else { notif.nent_enabled = false; }
486                         #else
487                         notif.nent_enabled = false;
488                         #endif
489
490                 }
491
492 void Create_Notification_Entity_InfoCenter(entity notif,
493                                                                                         float var_cvar,
494                                                                                         string namestring,
495                                                                                         int strnum,
496                                                                                         int flnum,
497                                                                                         /* MSG_INFO & MSG_CENTER */
498                                                                                         string args,
499                                                                                         string hudargs,
500                                                                                         string icon,
501                                                                                         float cpid,
502                                                                                         string durcnt,
503                                                                                         string normal,
504                                                                                         string gentle)
505                 {
506                         MSG typeId = notif.nent_type;
507                         // Set MSG_INFO and MSG_CENTER string/float counts
508                         notif.nent_stringcount = strnum;
509                         notif.nent_floatcount = flnum;
510
511                         // Only initialize arguments if we're either a client or on a dedicated server
512                         #ifdef SVQC
513                         float should_process_args = server_is_dedicated;
514                         #else
515                         float should_process_args = true;
516                         #endif
517                         string typestring = Get_Notif_TypeName(typeId);
518                         if(should_process_args)
519                         {
520                                 // ========================
521                                 //  Process Main Arguments
522                                 // ========================
523                                 if(strnum + flnum)
524                                 {
525                                         if(args != "")
526                                         {
527                                                 notif.nent_args = strzone(
528                                                         Process_Notif_Args(1, args, typestring, namestring));
529                                         }
530                                         else if((hudargs == "") && (durcnt ==""))
531                                         {
532                                                 LOG_INFOF(
533                                                         (
534                                                                 "^1NOTIFICATION HAS ARG COUNTS BUT NO ARGS OR HUDARGS OR DURCNT: "
535                                                                 "^7net_type = %s, net_name = %s, strnum = %d, flnum = %d\n"
536                                                         ),
537                                                         typestring,
538                                                         namestring,
539                                                         strnum,
540                                                         flnum
541                                                 );
542                                                 notif_error = true;
543                                         }
544                                 }
545                                 else if(args != "")
546                                 {
547                                         notif.nent_args = strzone(
548                                                 Process_Notif_Args(1, args, typestring, namestring));
549                                 }
550
551
552                                 // =======================================
553                                 //  Process HUD and Centerprint Arguments
554                                 //    Only processed on CSQC, as these
555                                 //    args are only for HUD features.
556                                 // =======================================
557                                 #ifdef CSQC
558                                 if(hudargs != "")
559                                 {
560                                         notif.nent_hudargs = strzone(
561                                                 Process_Notif_Args(2, hudargs, typestring, namestring));
562
563                                         if(icon != "") { notif.nent_icon = strzone(icon); }
564                                         else
565                                         {
566                                                 LOG_INFOF(
567                                                         (
568                                                                 "^1NOTIFICATION HAS HUDARGS BUT NO ICON: "
569                                                                 "^7net_type = %s, net_name = %s.\n"
570                                                         ),
571                                                         typestring,
572                                                         namestring
573                                                 );
574                                                 notif_error = true;
575                                         }
576                                 }
577                                 else if(icon != "")
578                                 {
579                                         LOG_INFOF(
580                                                 (
581                                                         "^1NOTIFICATION HAS ICON BUT NO HUDARGS: "
582                                                         "^7net_type = %s, net_name = %s.\n"
583                                                 ),
584                                                 typestring,
585                                                 namestring
586                                         );
587                                         notif_error = true;
588                                 }
589
590                                 if(durcnt != "")
591                                 {
592                                         notif.nent_durcnt = strzone(
593                                                 Process_Notif_Args(3, durcnt, typestring, namestring));
594
595                                         if(cpid != NO_MSG_) { notif.nent_cpid = cpid; }
596                                         else
597                                         {
598                                                 LOG_INFOF(
599                                                         (
600                                                                 "^1NOTIFICATION HAS DURCNT BUT NO CPID: "
601                                                                 "^7net_type = %s, net_name = %s.\n"
602                                                         ),
603                                                         typestring,
604                                                         namestring
605                                                 );
606                                                 notif_error = true;
607                                         }
608                                 }
609                                 else if(cpid != NO_MSG_) { notif.nent_cpid = cpid; }
610                                 #endif
611
612
613                                 // ======================
614                                 //  Process Notif String
615                                 // ======================
616                                 #define SET_NOTIF_STRING(string,stringname) MACRO_BEGIN { \
617                                         notif.nent_string = strzone(CCR( \
618                                                 Process_Notif_Line( \
619                                                         typeId, \
620                                                         (var_cvar > 1), \
621                                                         string, \
622                                                         typestring, \
623                                                         namestring, \
624                                                         stringname \
625                                                 )) \
626                                         ); \
627                                 } MACRO_END
628
629                                 if(GENTLE)
630                                 {
631                                         if(gentle != "") { SET_NOTIF_STRING(gentle, "GENTLE"); }
632                                         else if(normal != "") { SET_NOTIF_STRING(normal, "NORMAL"); }
633                                 }
634                                 else if(normal != "") { SET_NOTIF_STRING(normal, "NORMAL"); }
635                                 #undef SET_NOTIF_STRING
636
637                                 // Check to make sure a string was chosen
638                                 if(notif.nent_string == "")
639                                 {
640                                         LOG_INFOF(
641                                                 (
642                                                         "^1EMPTY NOTIFICATION: "
643                                                         "^7net_type = %s, net_name = %s.\n"
644                                                 ),
645                                                 typestring,
646                                                 namestring
647                                         );
648                                         notif_error = true;
649                                 }
650                         }
651                 }
652
653 void Create_Notification_Entity_Multi(entity notif,
654                                                                                 float var_cvar,
655                                                                                 string namestring,
656                                                                                 /* MSG_MULTI */
657                                                                                 Notification anncename,
658                                                                                 Notification infoname,
659                                                                                 Notification centername)
660                 {
661                         MSG typeId = MSG_MULTI;
662                         // Set MSG_MULTI string/float counts
663                         if((anncename == NO_MSG) && (infoname == NO_MSG) && (centername == NO_MSG))
664                         {
665                                 string typestring = Get_Notif_TypeName(typeId);
666                                 LOG_INFOF(
667                                         (
668                                                 "^1NOTIFICATION WITH NO SUBCALLS: "
669                                                 "^7net_type = %s, net_name = %s.\n"
670                                         ),
671                                         typestring,
672                                         namestring
673                                 );
674                                 notif_error = true;
675                         }
676                         else
677                         {
678                                 // announcements don't actually need any arguments, so lets not even count them.
679                                 if(anncename != NO_MSG) { notif.nent_msgannce = anncename; }
680
681                                 float infoname_stringcount = 0, infoname_floatcount = 0;
682                                 float centername_stringcount = 0, centername_floatcount = 0;
683
684                                 if(infoname != NO_MSG)
685                                 {
686                                         notif.nent_msginfo = infoname;
687                                         infoname_stringcount = notif.nent_msginfo.nent_stringcount;
688                                         infoname_floatcount = notif.nent_msginfo.nent_floatcount;
689                                 }
690
691                                 if(centername != NO_MSG)
692                                 {
693                                         notif.nent_msgcenter = centername;
694                                         centername_stringcount = notif.nent_msgcenter.nent_stringcount;
695                                         centername_floatcount = notif.nent_msgcenter.nent_floatcount;
696                                 }
697
698                                 // set the requirements of THIS notification to the totals of its subcalls
699                                 notif.nent_stringcount = max(infoname_stringcount, centername_stringcount);
700                                 notif.nent_floatcount = max(infoname_floatcount, centername_floatcount);
701                         }
702                 }
703
704 void Create_Notification_Entity_Choice(entity notif,
705                                                                                 float var_cvar,
706                                                                                 string namestring,
707                                                                                 /* MSG_CHOICE */
708                                                                                 float challow_def,
709                                                                                 float challow_var,
710                                                                                 MSG chtype,
711                                                                                 Notification optiona,
712                                                                                 Notification optionb)
713                 {
714                         MSG typeId = MSG_CHOICE;
715                         if((chtype == MSG_Null) || (optiona == NO_MSG) || (optionb == NO_MSG))
716                         {
717                                 string typestring = Get_Notif_TypeName(typeId);
718                                 LOG_INFOF(
719                                         (
720                                                 "^1NOTIFICATION IS MISSING CHOICE PARAMS: "
721                                                 "^7net_type = %s, net_name = %s.\n"
722                                         ),
723                                         typestring,
724                                         namestring
725                                 );
726                                 notif_error = true;
727                         }
728                         else
729                         {
730                                 notif.nent_optiona = optiona;
731                                 notif.nent_optionb = optionb;
732                                 notif.nent_challow_def = challow_def; // 0: never allowed, 1: allowed in warmup, 2: always allowed
733                                 notif.nent_challow_var = challow_var; // 0: never allowed, 1: allowed in warmup, 2: always allowed
734                                 notif.nent_stringcount = max(notif.nent_optiona.nent_stringcount, notif.nent_optionb.nent_stringcount);
735                                 notif.nent_floatcount = max(notif.nent_optiona.nent_floatcount, notif.nent_optionb.nent_floatcount);
736
737                                 /*#ifdef NOTIFICATIONS_DEBUG
738                                 Debug_Notification(sprintf(
739                                         "Create_Notification_Entity(...): MSG_CHOICE: %s\n%s\n%s\n",
740                                         notif.nent_name,
741                                         sprintf(
742                                                 "^ optiona: %s %s : %d %d",
743                                                 Get_Notif_TypeName(notif.nent_optiona.nent_type),
744                                                 notif.nent_optiona.nent_name,
745                                                 notif.nent_optiona.nent_stringcount,
746                                                 notif.nent_optiona.nent_floatcount
747                                         ),
748                                         sprintf(
749                                                 "^ optionb: %s %s : %d %d",
750                                                 Get_Notif_TypeName(notif.nent_optionb.nent_type),
751                                                 notif.nent_optionb.nent_name,
752                                                 notif.nent_optionb.nent_stringcount,
753                                                 notif.nent_optionb.nent_floatcount
754                                         )
755                                 ));
756                                 #endif*/
757                         }
758                 }
759
760
761 // ===============
762 //  Cvar Handling
763 // ===============
764
765 // used by MSG_CHOICE to build list of choices
766 #ifdef SVQC
767 void Notification_GetCvars()
768 {
769         int idx = 0;
770         FOREACH(Notifications, it.nent_type == MSG_CHOICE, {
771                 GetCvars_handleFloat(
772                         get_cvars_s,
773                         get_cvars_f,
774                         msg_choice_choices[idx++],
775                         sprintf("notification_%s", it.nent_name)
776                 );
777         });
778 }
779 #endif
780
781 /** used to output notifications.cfg file */
782 void Dump_Notifications(int fh, bool alsoprint)
783 {
784         #define NOTIF_WRITE(a) MACRO_BEGIN { \
785                 fputs(fh, a); \
786                 if (alsoprint) LOG_INFO(a); \
787         } MACRO_END
788
789         #define NOTIF_WRITE_ENTITY(e, description) MACRO_BEGIN { \
790                 string notif_msg = sprintf( \
791                         "seta notification_%s \"%d\" \"%s\"\n", \
792                         e.nent_name, e.nent_default, description \
793                 ); \
794                 NOTIF_WRITE(notif_msg); \
795         } MACRO_END
796
797         #define NOTIF_WRITE_ENTITY_CHOICE(e, descriptiona, descriptionb) MACRO_BEGIN { \
798                 string notif_msg = sprintf( \
799                         "seta notification_%s \"%d\" \"%s\"\n" \
800                         "seta notification_%s_ALLOWED \"%d\" \"%s\"\n", \
801                         e.nent_name, e.nent_default, descriptiona, \
802                         e.nent_name, e.nent_challow_def, descriptionb \
803                 ); \
804                 NOTIF_WRITE(notif_msg); \
805         } MACRO_END
806
807         #define NOTIF_WRITE_HARDCODED(cvar, default, description) MACRO_BEGIN { \
808                 string notif_msg = sprintf( \
809                         "seta notification_%s \"%s\" \"%s\"\n", \
810                         cvar, default, description \
811                 ); \
812                 NOTIF_WRITE(notif_msg); \
813         } MACRO_END
814
815         // Note: This warning only applies to the notifications.cfg file that is output...
816         // You ARE supposed to manually edit this function to add i.e. hard coded
817         // notification variables for mutators or game modes or such and then
818         // regenerate the notifications.cfg file from the new code.
819
820         NOTIF_WRITE("// ********************************************** //\n");
821         NOTIF_WRITE("// ** WARNING - DO NOT MANUALLY EDIT THIS FILE ** //\n");
822         NOTIF_WRITE("// **                                          ** //\n");
823         NOTIF_WRITE("// **  This file is automatically generated    ** //\n");
824         NOTIF_WRITE("// **  by code with the command 'dumpnotifs'.  ** //\n");
825         NOTIF_WRITE("// **                                          ** //\n");
826         NOTIF_WRITE("// **  If you add a new notification, please   ** //\n");
827         NOTIF_WRITE("// **  regenerate this file with that command  ** //\n");
828         NOTIF_WRITE("// **  making sure that the output matches     ** //\n");
829         NOTIF_WRITE("// **  with the lists and defaults in code.    ** //\n");
830         NOTIF_WRITE("// **                                          ** //\n");
831         NOTIF_WRITE("// ********************************************** //\n");
832
833         // These notifications will also append their string as a comment...
834         // This is not necessary, and does not matter if they vary between config versions,
835         // it is just a semi-helpful tool for those who want to manually change their user settings.
836
837         int NOTIF_ANNCE_COUNT = 0; FOREACH(Notifications, it.nent_type == MSG_ANNCE, { ++NOTIF_ANNCE_COUNT; });
838         NOTIF_WRITE(sprintf("\n// MSG_ANNCE notifications (count = %d):\n", NOTIF_ANNCE_COUNT));
839         FOREACH(Notifications, it.nent_type == MSG_ANNCE, {
840                 NOTIF_WRITE_ENTITY(it,
841                         "0 = disabled, 1 = enabled if gentle mode is off, 2 = always enabled"
842                 );
843         });
844
845         int NOTIF_INFO_COUNT = 0; FOREACH(Notifications, it.nent_type == MSG_INFO, { ++NOTIF_INFO_COUNT; });
846         NOTIF_WRITE(sprintf("\n// MSG_INFO notifications (count = %d):\n", NOTIF_INFO_COUNT));
847         FOREACH(Notifications, it.nent_type == MSG_INFO, {
848                 NOTIF_WRITE_ENTITY(it,
849                         "0 = off, 1 = print to console, "
850                         "2 = print to console and chatbox (if notification_allow_chatboxprint is enabled)"
851                 );
852         });
853
854         int NOTIF_CENTER_COUNT = 0; FOREACH(Notifications, it.nent_type == MSG_CENTER, { ++NOTIF_CENTER_COUNT; });
855         NOTIF_WRITE(sprintf("\n// MSG_CENTER notifications (count = %d):\n", NOTIF_CENTER_COUNT));
856         FOREACH(Notifications, it.nent_type == MSG_CENTER, {
857                 NOTIF_WRITE_ENTITY(it,
858                         "0 = off, 1 = centerprint"
859                 );
860         });
861
862         int NOTIF_MULTI_COUNT = 0; FOREACH(Notifications, it.nent_type == MSG_MULTI, { ++NOTIF_MULTI_COUNT; });
863         NOTIF_WRITE(sprintf("\n// MSG_MULTI notifications (count = %d):\n", NOTIF_MULTI_COUNT));
864         FOREACH(Notifications, it.nent_type == MSG_MULTI, {
865                 NOTIF_WRITE_ENTITY(it,
866                         "Enable this multiple notification"
867                 );
868         });
869
870         int NOTIF_CHOICE_COUNT = 0; FOREACH(Notifications, it.nent_type == MSG_CHOICE, { ++NOTIF_CHOICE_COUNT; });
871         NOTIF_WRITE(sprintf("\n// MSG_CHOICE notifications (count = %d):\n", NOTIF_CHOICE_COUNT));
872         FOREACH(Notifications, it.nent_type == MSG_CHOICE, {
873                 NOTIF_WRITE_ENTITY_CHOICE(it,
874                         "Choice for this notification 0 = off, 1 = default message, 2 = verbose message",
875                         "Allow choice for this notification 0 = off, 1 = only in warmup mode, 2 = always"
876                 );
877         });
878
879         // edit these to match whichever cvars are used for specific notification options
880         NOTIF_WRITE("\n// HARD CODED notification variables:\n");
881
882         NOTIF_WRITE_HARDCODED(
883                 "allow_chatboxprint", "1",
884                 "Allow INFO notifications to be printed to chat box"
885                 "0 = do not allow, "
886                 "1 = allow only if allowed by individual notification_INFO* cvars, "
887                 "2 = force all INFO notifications to be printed to the chatbox"
888         );
889
890         NOTIF_WRITE_HARDCODED(
891                 "debug", "0",
892                 "Print extra debug information on all notification function calls "
893                 "(Requires -DNOTIFICATIONS_DEBUG flag to be enabled on QCSRC compilation)... "
894                 "0 = disabled, 1 = dprint, 2 = print"
895         );
896
897         NOTIF_WRITE_HARDCODED(
898                 "errors_are_fatal", "1",
899                 "If a notification fails upon initialization, cause a Host_Error to stop the program"
900         );
901
902         NOTIF_WRITE_HARDCODED(
903                 "item_centerprinttime", "1.5",
904                 "How long to show item information centerprint messages (like 'You got the Electro' or such)"
905         );
906
907         NOTIF_WRITE_HARDCODED(
908                 "lifetime_mapload", "10",
909                 "Amount of time that notification entities last immediately at mapload (in seconds) "
910                 "to help prevent notifications from being lost on early init (like gamestart countdown)"
911         );
912
913         NOTIF_WRITE_HARDCODED(
914                 "lifetime_runtime", "0.5",
915                 "Amount of time that notification entities last on the server during runtime (In seconds)"
916         );
917
918         NOTIF_WRITE_HARDCODED(
919                 "server_allows_location", "1",
920                 "Server side cvar for allowing death messages to show location information too"
921         );
922
923         NOTIF_WRITE_HARDCODED(
924                 "show_location", "0",
925                 "Append location information to MSG_INFO death/kill messages"
926         );
927
928         NOTIF_WRITE_HARDCODED(
929                 "show_location_string", "",
930                 "Replacement string piped into sprintf, "
931                 "so you can do different messages like this: ' at the %s' or ' (near %s)'"
932         );
933
934         NOTIF_WRITE_HARDCODED(
935                 "show_sprees", "1",
936                 "Print information about sprees in death/kill messages"
937         );
938
939         NOTIF_WRITE_HARDCODED(
940                 "show_sprees_center", "1",
941                 "Show spree information in MSG_CENTER messages... "
942                 "0 = off, 1 = target (but only for first victim) and attacker"
943         );
944
945         NOTIF_WRITE_HARDCODED(
946                 "show_sprees_center_specialonly", "1",
947                 "Don't show spree information in MSG_CENTER messages if it isn't an achievement"
948         );
949
950         NOTIF_WRITE_HARDCODED(
951                 "show_sprees_info", "3",
952                 "Show spree information in MSG_INFO messages... "
953                 "0 = off, 1 = target only, 2 = attacker only, 3 = target and attacker"
954         );
955
956         NOTIF_WRITE_HARDCODED(
957                 "show_sprees_info_newline", "1",
958                 "Show attacker spree information for MSG_INFO messages on a separate line than the death notification itself"
959         );
960
961         NOTIF_WRITE_HARDCODED(
962                 "show_sprees_info_specialonly", "1",
963                 "Don't show attacker spree information in MSG_INFO messages if it isn't an achievement"
964         );
965
966         NOTIF_WRITE(sprintf(
967                 (
968                         "\n// Notification counts (total = %d): "
969                         "MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d\n"
970                 ),
971                 (
972                         NOTIF_ANNCE_COUNT +
973                         NOTIF_INFO_COUNT +
974                         NOTIF_CENTER_COUNT +
975                         NOTIF_MULTI_COUNT +
976                         NOTIF_CHOICE_COUNT
977                 ),
978                 NOTIF_ANNCE_COUNT,
979                 NOTIF_INFO_COUNT,
980                 NOTIF_CENTER_COUNT,
981                 NOTIF_MULTI_COUNT,
982                 NOTIF_CHOICE_COUNT
983         ));
984         #undef NOTIF_WRITE_HARDCODED
985         #undef NOTIF_WRITE_ENTITY
986         #undef NOTIF_WRITE
987 }
988
989
990 // ===============================
991 //  Frontend Notification Pushing
992 // ===============================
993
994 string Local_Notification_sprintf(
995         string input, string args,
996         string s1, string s2, string s3, string s4,
997         int f1, float f2, float f3, float f4)
998 {
999         #ifdef NOTIFICATIONS_DEBUG
1000         Debug_Notification(sprintf(
1001                 "Local_Notification_sprintf('%s^7', '%s', %s, %s);\n",
1002                 MakeConsoleSafe(input),
1003                 args,
1004                 MakeConsoleSafe(sprintf("'%s^7', '%s^7', '%s^7', '%s^7'", s1, s2, s3, s4)),
1005                 sprintf("%d, %d, %d, %d", f1, f2, f3, f4)
1006         ));
1007         #endif
1008
1009         for (int sel_num = 0; sel_num < NOTIF_MAX_ARGS; ++sel_num) { arg_slot[sel_num] = ""; }
1010
1011         for (int sel_num = 0; (args != ""); )
1012         {
1013                 string selected = car(args); args = cdr(args);
1014                 NOTIF_HIT_MAX(NOTIF_MAX_ARGS, "Local_Notification_sprintf");
1015                 string tmp_s; // used by NOTIF_ARGUMENT_LIST
1016                 switch (strtolower(selected))
1017                 {
1018                         #define ARG_CASE_ARG_CS_SV_HA(selected, result) case selected: { arg_slot[sel_num++] = result; break; }
1019                         #define ARG_CASE_ARG_CS_SV_DC(selected, result) case selected: { arg_slot[sel_num++] = result; break; }
1020                         #define ARG_CASE_ARG_CS_SV(selected, result)    case selected: { arg_slot[sel_num++] = result; break; }
1021 #ifdef CSQC
1022                         #define ARG_CASE_ARG_CS(selected, result)       case selected: { arg_slot[sel_num++] = result; break; }
1023                         #define ARG_CASE_ARG_SV(selected, result)
1024 #else
1025                         #define ARG_CASE_ARG_CS(selected, result)
1026                         #define ARG_CASE_ARG_SV(selected, result)       case selected: { arg_slot[sel_num++] = result; break; }
1027 #endif
1028                         #define ARG_CASE_ARG_DC(selected, result)
1029                         #define ARG_CASE(prog, selected, result)        ARG_CASE_##prog(selected, result)
1030                         NOTIF_ARGUMENT_LIST
1031                         #undef ARG_CASE
1032                         #undef ARG_CASE_ARG_DC
1033                         #undef ARG_CASE_ARG_SV
1034                         #undef ARG_CASE_ARG_CS
1035                         #undef ARG_CASE_ARG_CS_SV
1036                         #undef ARG_CASE_ARG_CS_SV_DC
1037                         #undef ARG_CASE_ARG_CS_SV_HA
1038                         default: NOTIF_HIT_UNKNOWN(NOTIF_MAX_ARGS, "Local_Notification_sprintf")
1039                 }
1040         }
1041         return sprintf(
1042                 strcat(input, "\n"),
1043                 arg_slot[0],
1044                 arg_slot[1],
1045                 arg_slot[2],
1046                 arg_slot[3],
1047                 arg_slot[4],
1048                 arg_slot[5],
1049                 arg_slot[6]
1050         );
1051 }
1052
1053 #ifdef CSQC
1054 void Local_Notification_sound(
1055         int soundchannel, string soundfile,
1056         float soundvolume, float soundposition)
1057 {
1058         if ((soundfile != prev_soundfile) || (time >= (prev_soundtime + autocvar_cl_announcer_antispam)))
1059         {
1060                 #ifdef NOTIFICATIONS_DEBUG
1061                 Debug_Notification(sprintf(
1062                         "Local_Notification_sound(%f, '%s', %f, %f);\n",
1063                         soundchannel,
1064                         sprintf(
1065                                 "announcer/%s/%s.wav",
1066                                 AnnouncerOption(),
1067                                 soundfile
1068                         ),
1069                         soundvolume,
1070                         soundposition
1071                 ));
1072                 #endif
1073
1074                 _sound(
1075                         NULL,
1076                         soundchannel,
1077                         sprintf(
1078                                 "announcer/%s/%s.wav",
1079                                 AnnouncerOption(),
1080                                 soundfile
1081                         ),
1082                         soundvolume,
1083                         soundposition
1084                 );
1085
1086                 if (prev_soundfile) strunzone(prev_soundfile);
1087                 prev_soundfile = strzone(soundfile);
1088                 prev_soundtime = time;
1089         }
1090         else
1091         {
1092                 #ifdef NOTIFICATIONS_DEBUG
1093                 Debug_Notification(sprintf(
1094                         (
1095                                 "Local_Notification_sound(world, %f, '%s', %f, %f) "
1096                                 "^1BLOCKED BY ANTISPAM:^7 prevsnd: '%s', timediff: %f, limit: %f\n"
1097                          ),
1098                         soundchannel,
1099                         sprintf(
1100                                 "announcer/%s/%s.wav",
1101                                 AnnouncerOption(),
1102                                 soundfile
1103                         ),
1104                         soundvolume,
1105                         soundposition,
1106                         prev_soundfile,
1107                         (time - prev_soundtime),
1108                         autocvar_cl_announcer_antispam
1109                 ));
1110                 #endif
1111         }
1112 }
1113
1114 void Local_Notification_HUD_Notify_Push(
1115         string icon, string hudargs,
1116         string s1, string s2, string s3, string s4,
1117         float f1, float f2, float f3, float f4)
1118 {
1119         arg_slot[0] = ""; arg_slot[1] = "";
1120
1121         for (int sel_num = 0; (hudargs != ""); )
1122         {
1123                 string selected = car(hudargs); hudargs = cdr(hudargs);
1124                 NOTIF_HIT_MAX(NOTIF_MAX_HUDARGS, "Local_Notification_HUD_Notify_Push");
1125                 switch (strtolower(selected))
1126                 {
1127                         #define ARG_CASE_ARG_CS_SV_HA(selected, result) case selected: { arg_slot[sel_num++] = result; break; }
1128                         #define ARG_CASE_ARG_CS_SV_DC(selected, result)
1129                         #define ARG_CASE_ARG_CS_SV(selected, result)
1130                         #define ARG_CASE_ARG_CS(selected, result)
1131                         #define ARG_CASE_ARG_SV(selected, result)
1132                         #define ARG_CASE_ARG_DC(selected, result)
1133                         #define ARG_CASE(prog, selected, result)        ARG_CASE_##prog(selected, result)
1134                         NOTIF_ARGUMENT_LIST
1135                         #undef ARG_CASE
1136                         #undef ARG_CASE_ARG_DC
1137                         #undef ARG_CASE_ARG_SV
1138                         #undef ARG_CASE_ARG_CS
1139                         #undef ARG_CASE_ARG_CS_SV
1140                         #undef ARG_CASE_ARG_CS_SV_DC
1141                         #undef ARG_CASE_ARG_CS_SV_HA
1142                         default: NOTIF_HIT_UNKNOWN(NOTIF_MAX_HUDARGS, "Local_Notification_HUD_Notify_Push")
1143                 }
1144         }
1145         #ifdef NOTIFICATIONS_DEBUG
1146         Debug_Notification(sprintf(
1147                 "Local_Notification_HUD_Notify_Push('%s^7', '%s', %s, %s, %s);\n",
1148                 icon,
1149                 hudargs,
1150                 MakeConsoleSafe(sprintf("'%s^7', '%s^7', '%s^7', '%s^7'", s1, s2, s3, s4)),
1151                 sprintf("%d, %d, %d, %d", f1, f2, f3, f4),
1152                 MakeConsoleSafe(sprintf("'%s^7', '%s^7'", stof(arg_slot[0]), stof(arg_slot[1])))
1153         ));
1154         #endif
1155         HUD_Notify_Push(icon, arg_slot[0], arg_slot[1]);
1156 }
1157
1158 void Local_Notification_centerprint_generic(
1159         string input, string durcnt,
1160         int cpid, float f1, float f2)
1161 {
1162         arg_slot[0] = ""; arg_slot[1] = "";
1163
1164         for (int sel_num = 0; (durcnt != ""); )
1165         {
1166                 string selected = car(durcnt); durcnt = cdr(durcnt);
1167                 NOTIF_HIT_MAX(NOTIF_MAX_DURCNT, "Local_Notification_centerprint_generic");
1168                 switch (strtolower(selected))
1169                 {
1170                         #define ARG_CASE_ARG_CS_SV_HA(selected, result)
1171                         #define ARG_CASE_ARG_CS_SV_DC(selected, result) case selected: { arg_slot[sel_num++] = result; break; }
1172                         #define ARG_CASE_ARG_CS_SV(selected, result)
1173                         #define ARG_CASE_ARG_CS(selected, result)
1174                         #define ARG_CASE_ARG_SV(selected, result)
1175                         #define ARG_CASE_ARG_DC(selected, result)       case selected: { arg_slot[sel_num++] = result; break; }
1176                         #define ARG_CASE(prog, selected, result)        ARG_CASE_##prog(selected,result)
1177                         NOTIF_ARGUMENT_LIST
1178                         #undef ARG_CASE
1179                         #undef ARG_CASE_ARG_DC
1180                         #undef ARG_CASE_ARG_SV
1181                         #undef ARG_CASE_ARG_CS
1182                         #undef ARG_CASE_ARG_CS_SV
1183                         #undef ARG_CASE_ARG_CS_SV_DC
1184                         #undef ARG_CASE_ARG_CS_SV_HA
1185                         default:
1186                         {
1187                                 if (/* wtf */ ftos(stof(selected)) != "") { arg_slot[sel_num++] = selected; }
1188                                 else { NOTIF_HIT_UNKNOWN(NOTIF_MAX_DURCNT, "Local_Notification_centerprint_generic") }
1189                                 break;
1190                         }
1191                 }
1192         }
1193         #ifdef NOTIFICATIONS_DEBUG
1194         Debug_Notification(sprintf(
1195                 "Local_Notification_centerprint_generic('%s^7', '%s', %d, %d, %d, %d);\n",
1196                 MakeConsoleSafe(input),
1197                 durcnt,
1198                 f1, f2,
1199                 stof(arg_slot[0]),
1200                 stof(arg_slot[1])
1201         ));
1202         #endif
1203         centerprint_generic(cpid, input, stof(arg_slot[0]), stof(arg_slot[1]));
1204 }
1205 #endif
1206
1207 void Local_Notification(MSG net_type, Notification net_name, ...count)
1208 {
1209         // check if this should be aborted
1210         if (net_name == NOTIF_ABORT)
1211         {
1212                 #ifdef NOTIFICATIONS_DEBUG
1213                 Debug_Notification(sprintf(
1214                         "Local_Notification(%s, %s, ...);\n",
1215                         Get_Notif_TypeName(net_type),
1216                         "NOTIF_ABORT"
1217                 ));
1218                 #endif
1219                 return;
1220         }
1221
1222         // retreive entity of this notification
1223         entity notif = net_name;
1224         if (!notif)
1225         {
1226                 #ifdef NOTIFICATIONS_DEBUG
1227                 Debug_Notification(sprintf(
1228                         "Local_Notification(%s, %d, ...);\n",
1229                         Get_Notif_TypeName(net_type),
1230                         net_name
1231                 ));
1232                 #endif
1233                 backtrace(sprintf("Incorrect usage of Local_Notification: %s\n", "Null notification"));
1234                 return;
1235         }
1236
1237         // check if the notification is enabled
1238         if (!notif.nent_enabled)
1239         {
1240                 #ifdef NOTIFICATIONS_DEBUG
1241                 Debug_Notification(sprintf(
1242                         "Local_Notification(%s, %s, ...): Entity was disabled...\n",
1243                         Get_Notif_TypeName(net_type),
1244                         notif.nent_name
1245                 ));
1246                 #endif
1247                 return;
1248         }
1249
1250         string s1 = ((notif.nent_stringcount > 0) ? ...(0, string) : "");
1251         string s2 = ((notif.nent_stringcount > 1) ? ...(1, string) : "");
1252         string s3 = ((notif.nent_stringcount > 2) ? ...(2, string) : "");
1253         string s4 = ((notif.nent_stringcount > 3) ? ...(3, string) : "");
1254         float f1 =  ((notif.nent_floatcount  > 0) ? ...((notif.nent_stringcount + 0), float) : 0);
1255         float f2 =  ((notif.nent_floatcount  > 1) ? ...((notif.nent_stringcount + 1), float) : 0);
1256         float f3 =  ((notif.nent_floatcount  > 2) ? ...((notif.nent_stringcount + 2), float) : 0);
1257         float f4 =  ((notif.nent_floatcount  > 3) ? ...((notif.nent_stringcount + 3), float) : 0);
1258
1259         #ifdef NOTIFICATIONS_DEBUG
1260         Debug_Notification(sprintf(
1261                 "Local_Notification(%s, %s, %s, %s);\n",
1262                 Get_Notif_TypeName(net_type),
1263                 notif.nent_name,
1264                 MakeConsoleSafe(sprintf("'%s^7', '%s^7', '%s^7', '%s^7'", s1, s2, s3, s4)),
1265                 sprintf("%d, %d, %d, %d", f1, f2, f3, f4)
1266         ));
1267         #endif
1268
1269         if ((notif.nent_stringcount + notif.nent_floatcount) != count)
1270         {
1271                 backtrace(sprintf(
1272                         (
1273                                 "Arguments mismatch for Local_Notification(%s, %s, ...)! "
1274                                 "stringcount(%d) + floatcount(%d) != count(%d)\n"
1275                                 "Check the definition and function call for accuracy...?\n"
1276                         ),
1277                         Get_Notif_TypeName(net_type),
1278                         notif.nent_name,
1279                         notif.nent_stringcount,
1280                         notif.nent_floatcount,
1281                         count
1282                 ));
1283                 return;
1284         }
1285
1286         switch (net_type)
1287         {
1288                 case MSG_ANNCE:
1289                 {
1290                         #ifdef CSQC
1291                         Local_Notification_sound(
1292                                 notif.nent_channel,
1293                                 notif.nent_snd,
1294                                 notif.nent_vol,
1295                                 notif.nent_position
1296                         );
1297                         #else
1298                         backtrace("MSG_ANNCE on server?... Please notify Samual immediately!\n");
1299                         #endif
1300                         break;
1301                 }
1302
1303                 case MSG_INFO:
1304                 {
1305                         print(
1306                                 Local_Notification_sprintf(
1307                                         notif.nent_string,
1308                                         notif.nent_args,
1309                                         s1, s2, s3, s4,
1310                                         f1, f2, f3, f4)
1311                         );
1312                         #ifdef CSQC
1313                         if (notif.nent_icon != "")
1314                         {
1315                                 if (notif.nent_iconargs != "")
1316                                 {
1317                                         string s = Local_Notification_sprintf(
1318                                                 notif.nent_icon,notif.nent_iconargs,
1319                                                 s1, s2, s3, s4, f1, f2, f3, f4);
1320                                         // remove the trailing newline
1321                                         notif.nent_icon = strzone(substring(s, 0, -1));
1322                                 }
1323                                 Local_Notification_HUD_Notify_Push(
1324                                         notif.nent_icon,
1325                                         notif.nent_hudargs,
1326                                         s1, s2, s3, s4,
1327                                         f1, f2, f3, f4);
1328                         }
1329                         #endif
1330                         break;
1331                 }
1332
1333                 #ifdef CSQC
1334                 case MSG_CENTER:
1335                 {
1336                         Local_Notification_centerprint_generic(
1337                                 Local_Notification_sprintf(
1338                                         notif.nent_string,
1339                                         notif.nent_args,
1340                                         s1, s2, s3, s4,
1341                                         f1, f2, f3, f4),
1342                                 notif.nent_durcnt,
1343                                 notif.nent_cpid,
1344                                 f1, f2);
1345                         break;
1346                 }
1347                 #endif
1348
1349                 case MSG_MULTI:
1350                 {
1351                         if (notif.nent_msginfo && notif.nent_msginfo.nent_enabled)
1352                         {
1353                                 Local_Notification_WOVA(
1354                                         MSG_INFO,
1355                                         notif.nent_msginfo,
1356                                         notif.nent_msginfo.nent_stringcount,
1357                                         notif.nent_msginfo.nent_floatcount,
1358                                         s1, s2, s3, s4,
1359                                         f1, f2, f3, f4);
1360                         }
1361                         #ifdef CSQC
1362                         if (notif.nent_msgannce && notif.nent_msgannce.nent_enabled)
1363                         {
1364                                 Local_Notification_WOVA(
1365                                         MSG_ANNCE,
1366                                         notif.nent_msgannce,
1367                                         0, 0,
1368                                         "", "", "", "",
1369                                         0, 0, 0, 0);
1370                         }
1371                         if (notif.nent_msgcenter && notif.nent_msgcenter.nent_enabled)
1372                         {
1373                                 Local_Notification_WOVA(
1374                                         MSG_CENTER,
1375                                         notif.nent_msgcenter,
1376                                         notif.nent_msgcenter.nent_stringcount,
1377                                         notif.nent_msgcenter.nent_floatcount,
1378                                         s1, s2, s3, s4,
1379                                         f1, f2, f3, f4);
1380                         }
1381                         #endif
1382                         break;
1383                 }
1384
1385                 case MSG_CHOICE:
1386                 {
1387                         entity found_choice = notif.nent_optiona;
1388                         if (notif.nent_challow_var && (warmup_stage || (notif.nent_challow_var == 2))) {
1389                                 switch (cvar(sprintf("notification_%s", notif.nent_name)))
1390                                 {
1391                                         case 1: break;
1392                                         case 2: found_choice = notif.nent_optionb; break;
1393                                         default: return; // not enabled anyway
1394                                 }
1395                         }
1396
1397                         Local_Notification_WOVA(
1398                                 found_choice.nent_type,
1399                                 found_choice,
1400                                 found_choice.nent_stringcount,
1401                                 found_choice.nent_floatcount,
1402                                 s1, s2, s3, s4,
1403                                 f1, f2, f3, f4);
1404                 }
1405         }
1406 }
1407
1408 // WOVA = Without Variable Arguments
1409 void Local_Notification_WOVA(
1410         MSG net_type, Notification net_name,
1411         float stringcount, float floatcount,
1412         string s1, string s2, string s3, string s4,
1413         float f1, float f2, float f3, float f4)
1414 {
1415         #define VARITEM(stringc, floatc, args) \
1416                 if ((stringcount == stringc) && (floatcount == floatc)) \
1417                 { Local_Notification(net_type, net_name, args); return; }
1418         EIGHT_VARS_TO_VARARGS_VARLIST
1419         #undef VARITEM
1420         Local_Notification(net_type, net_name); // some notifications don't have any arguments at all
1421 }
1422
1423
1424 // =========================
1425 //  Notification Networking
1426 // =========================
1427
1428 /** networked as a linked entity to give newly connecting clients some notification context */
1429 REGISTER_NET_LINKED(ENT_CLIENT_NOTIFICATION)
1430
1431 #ifdef CSQC
1432 NET_HANDLE(ENT_CLIENT_NOTIFICATION, bool is_new)
1433 {
1434         make_pure(this);
1435         MSG net_type = ENUMCAST(MSG, ReadByte());
1436         int net_name = ReadShort();
1437         Notification notif = Get_Notif_Ent(net_type, net_name);
1438
1439         #ifdef NOTIFICATIONS_DEBUG
1440         Debug_Notification(sprintf(
1441                 "Read_Notification(%d) at %f: net_type = %s, net_name = %s (%d)\n",
1442                 is_new,
1443                 time,
1444                 Get_Notif_TypeName(net_type),
1445                 notif.registered_id,
1446                 net_name
1447         ));
1448         #endif
1449
1450         if (net_type == MSG_CENTER_CPID)
1451         {
1452                 // killing
1453                 return = true;
1454                 if (!is_new) return;
1455                 if (net_name == NO_CPID) {
1456                         backtrace(sprintf(
1457                                 "Read_Notification(%d) at %f: ^1TRIED TO KILL NO_CPID CENTERPRINT!\n",
1458                                 is_new,
1459                                 time
1460                         ));
1461                         return;
1462                 }
1463                 if (net_name == 0) {
1464                         // kill all
1465                         reset_centerprint_messages();
1466                         return;
1467                 }
1468                 // kill one
1469                 centerprint_generic(net_name, "", 0, 0);
1470                 return;
1471         }
1472         else
1473         {
1474                 // creating
1475                 if (!notif) {
1476                         backtrace("Read_Notification: Could not find notification entity!\n");
1477                         return false;
1478                 }
1479
1480                 string s1 = ((notif.nent_stringcount > 0) ? ReadString() : "");
1481                 string s2 = ((notif.nent_stringcount > 1) ? ReadString() : "");
1482                 string s3 = ((notif.nent_stringcount > 2) ? ReadString() : "");
1483                 string s4 = ((notif.nent_stringcount > 3) ? ReadString() : "");
1484                 float f1 = ((notif.nent_floatcount > 0) ? ReadLong() : 0);
1485                 float f2 = ((notif.nent_floatcount > 1) ? ReadLong() : 0);
1486                 float f3 = ((notif.nent_floatcount > 2) ? ReadLong() : 0);
1487                 float f4 = ((notif.nent_floatcount > 3) ? ReadLong() : 0);
1488
1489                 return = true;
1490
1491                 if (!is_new) return;
1492                 Local_Notification_WOVA(
1493                         net_type, notif,
1494                         notif.nent_stringcount,
1495                         notif.nent_floatcount,
1496                         s1, s2, s3, s4,
1497                         f1, f2, f3, f4);
1498                 return;
1499         }
1500 }
1501 #endif
1502
1503 #ifdef SVQC
1504 void Net_Notification_Remove()
1505 {
1506         SELFPARAM();
1507         #ifdef NOTIFICATIONS_DEBUG
1508         Debug_Notification(sprintf(
1509                 "Net_Notification_Remove() at %f: %s '%s - %s' notification\n",
1510                 time,
1511                 ((this.nent_net_name == -1) ? "Killed" : "Removed"),
1512                 Get_Notif_TypeName(this.nent_net_type),
1513                 this.owner.nent_name
1514         ));
1515         #endif
1516         for (int i = 0; i < this.nent_stringcount; ++i) { if (this.nent_strings[i]) strunzone(this.nent_strings[i]); }
1517         remove(this);
1518 }
1519
1520 bool Net_Write_Notification(entity this, entity client, int sf)
1521 {
1522         if (!Notification_ShouldSend(this.nent_broadcast, client, this.nent_client)) return false;
1523         WriteHeader(MSG_ENTITY, ENT_CLIENT_NOTIFICATION);
1524         WriteByte(MSG_ENTITY, ORDINAL(this.nent_net_type));
1525         WriteShort(MSG_ENTITY, this.nent_net_name);
1526         for (int i = 0; i < this.nent_stringcount; ++i) { WriteString(MSG_ENTITY, this.nent_strings[i]); }
1527         for (int i = 0; i < this.nent_floatcount; ++i) { WriteLong(MSG_ENTITY, this.nent_floats[i]); }
1528         return true;
1529 }
1530
1531 void Kill_Notification(
1532         NOTIF broadcast, entity client,
1533         /** primary group */
1534         MSG net_type,
1535         /** secondary group */
1536         float net_name)
1537 {
1538         #ifdef NOTIFICATIONS_DEBUG
1539         Debug_Notification(sprintf(
1540                 "Kill_Notification(%s, '%s', %s, %d);\n",
1541                 Get_Notif_BroadcastName(broadcast),
1542                 client.netname,
1543                 (net_type ? Get_Notif_TypeName(net_type) : "0"),
1544                 net_name
1545         ));
1546         #endif
1547
1548         string checkargs = Notification_CheckArgs(broadcast, client, NO_MSG);
1549         if (checkargs != "") { backtrace(sprintf("Incorrect usage of Kill_Notification: %s\n", checkargs)); return; }
1550
1551         entity net_notif;
1552         float killed_cpid = NO_CPID;
1553
1554         switch (net_type)
1555         {
1556                 case MSG_Null:
1557                 {
1558                         killed_cpid = 0; // kill ALL centerprints
1559                         break;
1560                 }
1561
1562                 case MSG_CENTER:
1563                 {
1564                         if(net_name)
1565                         {
1566                                 entity notif = Get_Notif_Ent(net_type, net_name);
1567                                 if (!notif) { backtrace("Kill_Notification: Could not find notification entity!\n"); return; }
1568
1569                                 if(notif.nent_cpid)
1570                                         killed_cpid = notif.nent_cpid;
1571                                 else
1572                                         killed_cpid = NO_CPID;
1573                         }
1574                         else
1575                         {
1576                                 killed_cpid = 0; // kill ALL centerprints
1577                         }
1578                         break;
1579                 }
1580
1581                 case MSG_CENTER_CPID:
1582                 {
1583                         killed_cpid = net_name;
1584                         break;
1585                 }
1586         }
1587
1588         if (killed_cpid != NO_CPID)
1589         {
1590                 net_notif = new(net_kill_notification);
1591                 make_pure(net_notif);
1592                 net_notif.nent_broadcast = broadcast;
1593                 net_notif.nent_client = client;
1594                 net_notif.nent_net_type = MSG_CENTER_CPID;
1595                 net_notif.nent_net_name = killed_cpid;
1596                 Net_LinkEntity(net_notif, false, autocvar_notification_lifetime_runtime, Net_Write_Notification);
1597         }
1598
1599         FOREACH_ENTITY_CLASS("net_notification", true,
1600         {
1601                 if (net_type)
1602                 {
1603                         if ((killed_cpid != NO_CPID) && (it.nent_net_type == MSG_CENTER))
1604                         {
1605                                 if(it.owner.nent_cpid == killed_cpid)
1606                                 {
1607                                         it.nent_net_name = -1;
1608                                         it.nextthink = time;
1609                                 }
1610                                 else { continue; } // we ARE looking for a specific CPID, don't kill everything else too
1611                         }
1612                         else if (it.nent_net_type == net_type)
1613                         {
1614                                 if (net_name)
1615                                 {
1616                                         if (it.nent_net_name == net_name) { it.nent_net_name = -1; it.nextthink = time; }
1617                                         else { continue; } // we ARE looking for a certain net_name, don't kill everything else too
1618                                 }
1619                                 else { it.nent_net_name = -1; it.nextthink = time; }
1620                         }
1621                         else { continue; } // we ARE looking for a certain net_type, don't kill everything else too
1622                 }
1623                 else { it.nent_net_name = -1; it.nextthink = time; }
1624         });
1625 }
1626
1627 void Send_Notification(
1628         NOTIF broadcast, entity client,
1629         MSG net_type, Notification net_name,
1630         ...count)
1631 {
1632         entity notif = net_name;
1633         string parms = sprintf("%s, '%s', %s, %s",
1634                 Get_Notif_BroadcastName(broadcast),
1635                 client.classname,
1636                 Get_Notif_TypeName(net_type),
1637                 net_name.registered_id
1638         );
1639         #ifdef NOTIFICATIONS_DEBUG
1640         Debug_Notification(sprintf("Send_Notification(%s, ...%d);\n", parms, count));
1641         #endif
1642         // check if this should be aborted
1643         if (net_name == NOTIF_ABORT) return;
1644
1645         if (!notif)
1646         {
1647                 LOG_WARNING("Send_Notification: Could not find notification entity!");
1648                 return;
1649         }
1650
1651         // check supplied broadcast, target, type, and name for errors
1652         string checkargs = Notification_CheckArgs(broadcast, client, net_name);
1653         if (checkargs != "")
1654         {
1655                 LOG_WARNINGF("Incorrect usage of Send_Notification: %s", checkargs);
1656                 return;
1657         }
1658
1659         string s1 = ((0 < notif.nent_stringcount) ? ...(0, string) : "");
1660         string s2 = ((1 < notif.nent_stringcount) ? ...(1, string) : "");
1661         string s3 = ((2 < notif.nent_stringcount) ? ...(2, string) : "");
1662         string s4 = ((3 < notif.nent_stringcount) ? ...(3, string) : "");
1663         float f1 = ((0 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 0), float) : 0);
1664         float f2 = ((1 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 1), float) : 0);
1665         float f3 = ((2 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 2), float) : 0);
1666         float f4 = ((3 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 3), float) : 0);
1667
1668         #ifdef NOTIFICATIONS_DEBUG
1669         Debug_Notification(sprintf(
1670                 "Send_Notification(%s, %s, %s);\n",
1671                 parms,
1672                 MakeConsoleSafe(sprintf("'%s^7', '%s^7', '%s^7', '%s^7'", s1, s2, s3, s4)),
1673                 sprintf("%d, %d, %d, %d", f1, f2, f3, f4)
1674         ));
1675         #endif
1676
1677         if ((notif.nent_stringcount + notif.nent_floatcount) != count)
1678         {
1679                 LOG_WARNINGF(
1680                         "Argument mismatch for Send_Notification(%s, ...)! "
1681                         "stringcount(%d) + floatcount(%d) != count(%d)\n"
1682                         "Check the definition and function call for accuracy...?\n",
1683                         parms,
1684                         notif.nent_stringcount,
1685                         notif.nent_floatcount,
1686                         count
1687                 );
1688                 return;
1689         }
1690
1691         if (
1692                 server_is_dedicated
1693                 &&
1694                 (
1695                         broadcast == NOTIF_ALL
1696                         ||
1697                         broadcast == NOTIF_ALL_EXCEPT
1698                 )
1699                 &&
1700                 !(
1701                         net_type == MSG_ANNCE
1702                         ||
1703                         net_type == MSG_CENTER
1704                 )
1705         )
1706         {
1707                 Local_Notification_WOVA(
1708                         net_type, net_name,
1709                         notif.nent_stringcount,
1710                         notif.nent_floatcount,
1711                         s1, s2, s3, s4,
1712                         f1, f2, f3, f4);
1713         }
1714
1715         if (net_type == MSG_CHOICE)
1716         {
1717                 // THIS GETS TRICKY... now we have to cycle through each possible player (checking broadcast)
1718                 // and then do an individual NOTIF_ONE_ONLY recursive call for each one depending on their option...
1719                 // It's slow, but it's better than the alternatives:
1720                 //   1. Constantly networking all info and letting client decide
1721                 //   2. Manually handling each separate call on per-usage basis (See old CTF usage of verbose)
1722                 entity found_choice;
1723
1724                 #define RECURSE_FROM_CHOICE(ent,action) MACRO_BEGIN { \
1725                         if (notif.nent_challow_var && (warmup_stage || (notif.nent_challow_var == 2))) { \
1726                                 switch (ent.msg_choice_choices[net_name.nent_choice_idx]) \
1727                                 { \
1728                                         case 1: found_choice = notif.nent_optiona; break; \
1729                                         case 2: found_choice = notif.nent_optionb; break; \
1730                                         default: action; \
1731                                 } \
1732                         } else { \
1733                                 found_choice = notif.nent_optiona; \
1734                         } \
1735                         Send_Notification_WOVA( \
1736                                 NOTIF_ONE_ONLY, \
1737                                 ent, \
1738                                 found_choice.nent_type, \
1739                                 found_choice, \
1740                                 found_choice.nent_stringcount, \
1741                                 found_choice.nent_floatcount, \
1742                                 s1, s2, s3, s4, \
1743                                 f1, f2, f3, f4); \
1744                 } MACRO_END
1745
1746                 switch (broadcast)
1747                 {
1748                         case NOTIF_ONE_ONLY: // we can potentially save processing power with this broadcast method
1749                         {
1750                                 if (IS_REAL_CLIENT(client)) {
1751                                         RECURSE_FROM_CHOICE(client, return);
1752                                 }
1753                                 break;
1754                         }
1755                         default:
1756                         {
1757                                 FOREACH_CLIENT(IS_REAL_CLIENT(it) && Notification_ShouldSend(broadcast, it, client), {
1758                                         RECURSE_FROM_CHOICE(it, continue);
1759                                 });
1760                                 break;
1761                         }
1762                 }
1763         }
1764         else
1765         {
1766                 entity net_notif = new(net_notification);
1767                 make_pure(net_notif);
1768                 net_notif.owner = notif;
1769                 net_notif.nent_broadcast = broadcast;
1770                 net_notif.nent_client = client;
1771                 net_notif.nent_net_type = net_type;
1772                 net_notif.nent_net_name = notif.m_id;
1773                 net_notif.nent_stringcount = notif.nent_stringcount;
1774                 net_notif.nent_floatcount = notif.nent_floatcount;
1775
1776                 for (int i = 0; i < net_notif.nent_stringcount; ++i) {
1777                         net_notif.nent_strings[i] = strzone(...(i, string));
1778                 }
1779                 for (int i = 0; i < net_notif.nent_floatcount; ++i) {
1780                         net_notif.nent_floats[i] = ...((net_notif.nent_stringcount + i), float);
1781                 }
1782
1783                 net_notif.think = Net_Notification_Remove;
1784                 net_notif.nextthink = (time > autocvar_notification_lifetime_mapload)
1785                         ? (time + autocvar_notification_lifetime_runtime)
1786                         : autocvar_notification_lifetime_mapload;
1787
1788                 Net_LinkEntity(net_notif, false, 0, Net_Write_Notification);
1789         }
1790 }
1791
1792 // WOVA = Without Variable Arguments
1793 void Send_Notification_WOVA(
1794         NOTIF broadcast, entity client,
1795         MSG net_type, Notification net_name,
1796         float stringcount, float floatcount,
1797         string s1, string s2, string s3, string s4,
1798         float f1, float f2, float f3, float f4)
1799 {
1800         #ifdef NOTIFICATIONS_DEBUG
1801         entity notif = net_name;
1802         Debug_Notification(sprintf(
1803                 "Send_Notification_WOVA(%s, %d, %d, %s, %s);\n",
1804                 sprintf(
1805                         "%s, '%s', %s, %s",
1806                         Get_Notif_BroadcastName(broadcast),
1807                         client.classname,
1808                         Get_Notif_TypeName(net_type),
1809                         notif.nent_name
1810                 ),
1811                 stringcount,
1812                 floatcount,
1813                 MakeConsoleSafe(sprintf("'%s^7', '%s^7', '%s^7', '%s^7'", s1, s2, s3, s4)),
1814                 sprintf("%d, %d, %d, %d", f1, f2, f3, f4)
1815         ));
1816         #endif
1817
1818         #define VARITEM(stringc, floatc, args) \
1819                 if ((stringcount == stringc) && (floatcount == floatc)) \
1820                 { Send_Notification(broadcast, client, net_type, net_name, args); return; }
1821         EIGHT_VARS_TO_VARARGS_VARLIST
1822         #undef VARITEM
1823         Send_Notification(broadcast, client, net_type, net_name); // some notifications don't have any arguments at all
1824 }
1825
1826 // WOCOVA = Without Counts Or Variable Arguments
1827 void Send_Notification_WOCOVA(
1828         NOTIF broadcast, entity client,
1829         MSG net_type, Notification net_name,
1830         string s1, string s2, string s3, string s4,
1831         float f1, float f2, float f3, float f4)
1832 {
1833         entity notif = net_name;
1834
1835         #ifdef NOTIFICATIONS_DEBUG
1836         Debug_Notification(sprintf(
1837                 "Send_Notification_WOCOVA(%s, %s, %s);\n",
1838                 sprintf(
1839                         "%s, '%s', %s, %s",
1840                         Get_Notif_BroadcastName(broadcast),
1841                         client.classname,
1842                         Get_Notif_TypeName(net_type),
1843                         notif.nent_name
1844                 ),
1845                 MakeConsoleSafe(sprintf("'%s^7', '%s^7', '%s^7', '%s^7'", s1, s2, s3, s4)),
1846                 sprintf("%d, %d, %d, %d", f1, f2, f3, f4)
1847         ));
1848         #endif
1849
1850         #define VARITEM(stringc, floatc, args) \
1851                 if ((notif.nent_stringcount == stringc) && (notif.nent_floatcount == floatc)) \
1852                 { Send_Notification(broadcast, client, net_type, net_name, args); return; }
1853         EIGHT_VARS_TO_VARARGS_VARLIST
1854         #undef VARITEM
1855         Send_Notification(broadcast, client, net_type, net_name); // some notifications don't have any arguments at all
1856 }
1857 #endif // ifdef SVQC