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