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