]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/notifications.qc
Cleanliness
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / notifications.qc
1 // ================================================
2 //  Unified notification system, written by Samual
3 //  Last updated: February, 2013
4 // ================================================
5
6 entity Get_Notif_Ent(float net_type, float net_name)
7 {
8         switch(net_type)
9         {
10                 case MSG_INFO: return msg_info_notifs[net_name - 1];
11                 case MSG_CENTER: return msg_center_notifs[net_name - 1];
12                 case MSG_WEAPON: return msg_weapon_notifs[net_name - 1];
13                 case MSG_DEATH: return msg_death_notifs[net_name - 1];
14         }
15         backtrace(sprintf("Get_Notif_Ent(%d, %d): Improper net type!\n", net_type, net_name));
16         return world;
17 }
18
19
20 // ===============================
21 //  Frontend Notification Pushing
22 // ===============================
23
24 void Dump_Notifications(float fh, float alsoprint)
25 {
26         #define NOTIF_WRITE(type,name,text) { \
27                 notif_msg = sprintf("seta %s 1 // %s - %s\n", name, #type, strreplace("\n", "\\n", text)); \
28                 fputs(fh, notif_msg); \
29                 if(alsoprint) { print(strreplace("^", "^^", notif_msg)); } }
30
31         string notif_msg;
32         float i;
33         entity e;
34
35         for(i = 0; i < NOTIF_INFO_COUNT; ++i) {
36                 e = Get_Notif_Ent(MSG_INFO, i);
37                 NOTIF_WRITE(MSG_INFO, e.nent_name, e.nent_string); }
38                 
39         for(i = 0; i < NOTIF_CENTER_COUNT; ++i) {
40                 e = Get_Notif_Ent(MSG_CENTER, i);
41                 NOTIF_WRITE(MSG_CENTER, e.nent_name, e.nent_string); }
42         
43         for(i = 0; i < NOTIF_WEAPON_COUNT; ++i) {
44                 e = Get_Notif_Ent(MSG_WEAPON, i);
45                 NOTIF_WRITE(MSG_WEAPON, e.nent_name, sprintf("infoname: %s, centername: %s",
46                         e.nent_msginfo.nent_name, e.nent_msgcenter.nent_name)); }
47                 
48         for(i = 0; i < NOTIF_DEATH_COUNT; ++i) {
49                 e = Get_Notif_Ent(MSG_DEATH, i);
50                 NOTIF_WRITE(MSG_DEATH, e.nent_name, sprintf("infoname: %s, centername: %s",
51                         e.nent_msginfo.nent_name, e.nent_msgcenter.nent_name)); }
52
53         print(sprintf("Notification counts: MSG_INFO = %d, MSG_CENTER = %d, MSG_WEAPON = %d, MSG_DEATH = %d\n",
54                 NOTIF_INFO_COUNT, NOTIF_CENTER_COUNT, NOTIF_WEAPON_COUNT, NOTIF_DEATH_COUNT));
55         
56         return;
57         #undef NOTIF_WRITE
58 }
59
60 string Local_Notification_sprintf(string input, string args, 
61         string s1, string s2, string s3, string s4,
62         float f1, float f2, float f3, float f4)
63 {
64         #ifdef NOTIFICATIONS_DEBUG
65         dprint(
66                 sprintf("Local_Notification_sprintf('%s^7', '%s', %s, %s);\n",
67                         strreplace("\n", "\\n", input),
68                         args,
69                         sprintf("'%s^7', '%s^7', '%s^7', '%s^7'", s1, s2, s3, s4),
70                         sprintf("%d, %d, %d, %d", f1, f2, f3, f4)
71                 )
72         );
73         #endif
74         
75         string selected;
76         float sel_num;
77         for(sel_num = 0; sel_num < NOTIF_MAX_ARGS; ++sel_num) { arg_slot[sel_num] = ""; }
78
79         #ifdef CSQC
80         string tmp_s;
81         #endif
82
83         for(sel_num = 0;(args != "");)
84         {
85                 selected = car(args); args = cdr(args);
86                 NOTIF_HIT_MAX(NOTIF_MAX_ARGS, "Local_Notification_sprintf")
87                 switch(strtolower(selected))
88                 {
89                         #define ARG_CASE(prog,selected,result) \
90                                 #ifdef CSQC \
91                                         #if (prog == ARG_BOTH) || (prog == ARG_TRIPLE) || (prog == ARG_CSQC) \
92                                                 case selected: { arg_slot[sel_num] = result; ++sel_num; break; } \
93                                         #endif \
94                                 #else \
95                                         #if (prog == ARG_BOTH) || (prog == ARG_TRIPLE) || (prog == ARG_SVQC) \
96                                                 case selected: { arg_slot[sel_num] = result; ++sel_num; break; } \
97                                         #endif \
98                                 #endif
99                         NOTIF_ARGUMENT_LIST
100                         #undef ARG_CASE
101                         NOTIF_HIT_UNKNOWN(NOTIF_MAX_ARGS, "Local_Notification_sprintf")
102                 }
103         }
104         return sprintf(input, arg_slot[0], arg_slot[1], arg_slot[2], arg_slot[3], arg_slot[4], arg_slot[5], arg_slot[6]);
105 }
106
107 #ifdef CSQC
108 void Local_Notification_HUD_Notify_Push(string icon, string hudargs, string s1, string s2, string s3, string s4)
109 {
110         string selected;
111         float sel_num;
112         arg_slot[0] = ""; arg_slot[1] = "";
113
114         for(sel_num = 0;(hudargs != "");)
115         {
116                 selected = car(hudargs); hudargs = cdr(hudargs);
117                 NOTIF_HIT_MAX(NOTIF_MAX_HUDARGS, "Local_Notification_HUD_Notify_Push")
118                 switch(strtolower(selected))
119                 {
120                         #define ARG_CASE(prog,selected,result) \
121                                 #if (prog == ARG_TRIPLE) \
122                                         case selected: { arg_slot[sel_num] = result; ++sel_num; break; } \
123                                 #endif
124                         NOTIF_ARGUMENT_LIST
125                         #undef ARG_CASE
126                         NOTIF_HIT_UNKNOWN(NOTIF_MAX_HUDARGS, "Local_Notification_HUD_Notify_Push")
127                 }
128         }
129         HUD_Notify_Push(icon, arg_slot[0], arg_slot[1]);
130 }
131 #endif
132
133 void Local_Notification(float net_type, float net_name, ...count)
134 {
135         // check supplied type and name for errors
136         string checkargs = "";
137         #define CHECKARG_TYPENAME(type) case MSG_##type##: \
138                 { if(!net_name || (net_name > NOTIF_##type##_COUNT)) \
139                 { checkargs = sprintf("Improper name: %d!", net_name); } break; }
140         switch(net_type)
141         {
142                 CHECKARG_TYPENAME(INFO)
143                 CHECKARG_TYPENAME(CENTER)
144                 CHECKARG_TYPENAME(WEAPON)
145                 CHECKARG_TYPENAME(DEATH)
146                 default: { checkargs = sprintf("Improper type: %d!", checkargs, net_type); break; }
147         }
148         #undef CHECKARG_TYPENAME
149         if(checkargs != "") { backtrace(sprintf("Incorrect usage of Local_Notification: %s\n", checkargs)); return; }
150
151         entity notif = Get_Notif_Ent(net_type, net_name);
152         if not(notif) { backtrace("Local_Notification: Could not find notification entity!\n"); return; }
153         if not(notif.nent_enabled) { print("Local_Notification: Entity was disabled...\n"); return; }
154
155         if((notif.nent_stringcount + notif.nent_floatcount) > count)
156         {
157                 backtrace(sprintf(
158                         strcat(
159                                 "Not enough arguments for Local_Notification! ",
160                                 "stringcount(%d) + floatcount(%d) > count(%d)\n", 
161                                 "Check the definition and function call for accuracy...?\n"
162                         ),
163                         notif.nent_stringcount, notif.nent_floatcount, count));
164                 return;
165         }
166         else if((notif.nent_stringcount + notif.nent_floatcount) < count)
167         {
168                 backtrace(sprintf(
169                         strcat(
170                                 "Too many arguments for Local_Notification! ",
171                                 "stringcount(%d) + floatcount(%d) < count(%d)\n",
172                                 "Check the definition and function call for accuracy...?\n"
173                         ),
174                         notif.nent_stringcount, notif.nent_floatcount, count));
175                 return;
176         }
177
178         string s1 = ((0 < notif.nent_stringcount) ? ...(0, string) : "");
179         string s2 = ((1 < notif.nent_stringcount) ? ...(1, string) : "");
180         string s3 = ((2 < notif.nent_stringcount) ? ...(2, string) : "");
181         string s4 = ((3 < notif.nent_stringcount) ? ...(3, string) : "");
182         float f1 = ((0 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 0), float) : 0);
183         float f2 = ((1 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 1), float) : 0);
184         float f3 = ((2 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 2), float) : 0);
185         float f4 = ((3 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 3), float) : 0);
186
187         #ifdef NOTIFICATIONS_DEBUG
188         dprint(
189                 sprintf("Local_Notification(%d, %s, %s, %s);\n",
190                         net_type,
191                         notif.nent_name,
192                         sprintf("'%s^7', '%s^7', '%s^7', '%s^7'", s1, s2, s3, s4),
193                         sprintf("%d, %d, %d, %d", f1, f2, f3, f4)
194                 )
195         );
196         #endif
197         
198         switch(net_type)
199         {
200                 case MSG_INFO:
201                 {
202                         print(
203                                 Local_Notification_sprintf(
204                                         notif.nent_string,
205                                         notif.nent_args, 
206                                         s1, s2, s3, s4,
207                                         f1, f2, f3, f4)
208                         );
209                         #ifdef CSQC 
210                         if(notif.nent_icon != "")
211                         {
212                                 Local_Notification_HUD_Notify_Push(
213                                         notif.nent_icon,
214                                         notif.nent_hudargs,
215                                         s1, s2, s3, s4);
216                         } 
217                         #endif 
218                         break;
219                 }
220                 
221                 #ifdef CSQC
222                 case MSG_CENTER:
223                 {
224                         centerprint_generic(
225                                 notif.nent_cpid,
226                                 Local_Notification_sprintf(
227                                         notif.nent_string,
228                                         notif.nent_args, 
229                                         s1, s2, s3, s4,
230                                         f1, f2, f3, f4),
231                                 0, 0);
232                         break;
233                 }
234                 #endif
235                 
236                 case MSG_WEAPON:
237                 case MSG_DEATH:
238                 {
239                         if(notif.nent_msginfo)
240                         if(notif.nent_msginfo.nent_enabled)
241                         {
242                                 Local_Notification_Without_VarArgs(
243                                         MSG_INFO,
244                                         notif.nent_msginfo.nent_id, 
245                                         notif.nent_msginfo.nent_stringcount, 
246                                         notif.nent_msginfo.nent_floatcount, 
247                                         s1, s2, s3, s4,
248                                         f1, f2, f3, f4);
249                         }
250                         #ifdef CSQC
251                         if(notif.nent_msgcenter)
252                         if(notif.nent_msgcenter.nent_enabled)
253                         {
254                                 Local_Notification_Without_VarArgs(
255                                         MSG_CENTER,
256                                         notif.nent_msgcenter.nent_id, 
257                                         notif.nent_msgcenter.nent_stringcount, 
258                                         notif.nent_msgcenter.nent_floatcount, 
259                                         s1, s2, s3, s4,
260                                         f1, f2, f3, f4); 
261                         }
262                         #endif
263                         break;
264                 }
265         }
266 }
267
268 void Local_Notification_Without_VarArgs(float net_type, float net_name,
269         float stringcount, float floatcount,
270         string s1, string s2, string s3, string s4,
271         float f1, float f2, float f3, float f4)
272 {
273         #define VARITEM(stringc,floatc,args) \
274                 if((stringcount == stringc) && (floatcount == floatc)) \
275                         { Local_Notification(net_type, net_name, args); return; }
276         EIGHT_VARS_TO_VARARGS_VARLIST
277         #undef VARITEM
278         Local_Notification(net_type, net_name); // some notifications don't have any arguments at all
279 }
280
281
282 // =========================
283 //  Notification Networking
284 // =========================
285
286 #ifdef CSQC
287 void Read_Notification(float is_new)
288 {
289         float net_type = ReadByte();
290         float net_name = ReadShort();
291
292         entity notif = Get_Notif_Ent(net_type, net_name);
293         if not(notif) { print("Read_Notification: Could not find notification entity!\n"); return; }
294
295         string s1 = ((0 < notif.nent_stringcount) ? ReadString() : "");
296         string s2 = ((1 < notif.nent_stringcount) ? ReadString() : "");
297         string s3 = ((2 < notif.nent_stringcount) ? ReadString() : "");
298         string s4 = ((3 < notif.nent_stringcount) ? ReadString() : "");
299         float f1 = ((0 < notif.nent_floatcount) ? ReadLong() : 0);
300         float f2 = ((1 < notif.nent_floatcount) ? ReadLong() : 0);
301         float f3 = ((2 < notif.nent_floatcount) ? ReadLong() : 0);
302         float f4 = ((3 < notif.nent_floatcount) ? ReadLong() : 0);
303
304         #ifdef NOTIFICATIONS_DEBUG
305         dprint(sprintf("Read_Notification(%d) at %f: net_name = %s.\n", is_new, time, notif.nent_name));
306         #endif
307         
308         if(is_new)
309         {
310                 Local_Notification_Without_VarArgs(
311                         net_type, net_name,
312                         notif.nent_stringcount,
313                         notif.nent_floatcount,
314                         s1, s2, s3, s4,
315                         f1, f2, f3, f4);
316         }
317 }
318 #endif
319
320 #ifdef SVQC
321 void Notification_Remove()
322 {
323         float i;
324         for(i = 0; i < 4; ++i) { if(self.nent_strings[i]) { strunzone(self.nent_strings[i]); } }
325         remove(self);
326 }
327
328 float Write_Notification(entity client, float sf)
329 {
330         float i, send = FALSE;
331         
332         switch(self.nent_broadcast)
333         {
334                 case NOTIF_ONE: { if((client == self.nent_client) || (client.classname == STR_SPECTATOR && client.enemy == self.nent_client)) { send = TRUE; } break; }
335                 case NOTIF_ONE_ONLY: { if(client == self.nent_client) { send = TRUE; } break; }
336                 case NOTIF_TEAM: { if((client.team == self.nent_client.team) || (client.classname == STR_SPECTATOR && client.enemy.team == self.nent_client.team)) { send = TRUE; } break; }
337                 case NOTIF_TEAM_EXCEPT: { if(((client != self.nent_client) && (client.team == self.nent_client.team) && !(client.classname == STR_SPECTATOR && client.enemy == self.nent_client))) { send = TRUE; } break; }
338                 case NOTIF_ANY: { send = TRUE; break; }
339                 case NOTIF_ANY_EXCEPT: { if((client != self.nent_client) && !(client.classname == STR_SPECTATOR && client.enemy == self.nent_client)) { send = TRUE; } break; }
340                 default: { send = FALSE; break; }
341         }
342
343         if(send)
344         {               
345                 WriteByte(MSG_ENTITY, ENT_CLIENT_NOTIFICATION);
346                 WriteByte(MSG_ENTITY, self.nent_net_type);
347                 WriteShort(MSG_ENTITY, self.nent_net_name);
348                 for(i = 0; i < self.nent_stringcount; ++i) { WriteString(MSG_ENTITY, self.nent_strings[i]); } 
349                 for(i = 0; i < self.nent_floatcount; ++i) { WriteLong(MSG_ENTITY, self.nent_floats[i]); }
350         }
351
352         return send; 
353 }
354
355 void Send_Notification(float broadcast, entity client,
356         float net_type, float net_name, ...count)
357 {
358         // check supplied broadcast, target, type, and name for errors
359         string checkargs = "";
360         #define CHECKARG_TYPENAME(type) case MSG_##type##: \
361                 { if(!net_name || (net_name > NOTIF_##type##_COUNT)) \
362                 { checkargs = sprintf("Improper name: %d!", net_name); } break; }
363         switch(net_type)
364         {
365                 CHECKARG_TYPENAME(INFO)
366                 CHECKARG_TYPENAME(CENTER)
367                 CHECKARG_TYPENAME(WEAPON)
368                 CHECKARG_TYPENAME(DEATH)
369                 default: { checkargs = sprintf("Improper type: %d!", checkargs, net_type); break; }
370         }
371         #undef CHECKARG_TYPENAME
372         if(checkargs != "") { checkargs = strcat(checkargs, " "); }
373         switch(broadcast)
374         {
375                 case NOTIF_ONE:
376                 case NOTIF_ONE_ONLY: { if(clienttype(client) == CLIENTTYPE_NOTACLIENT) { checkargs = sprintf("%sNo client provided!", checkargs); } break; }
377                 case NOTIF_ANY_EXCEPT: { if(clienttype(client) == CLIENTTYPE_NOTACLIENT) { checkargs = sprintf("%sException can't be a non-client!", checkargs); } break; }
378                 case NOTIF_ANY: { if(client) { checkargs = sprintf("%sEntity provided when world was required!", checkargs); } break; }
379                 case NOTIF_TEAM:
380                 case NOTIF_TEAM_EXCEPT:
381                 {
382                         if not(teamplay) { checkargs = sprintf("%sTeamplay not active!", checkargs); }
383                         else if(clienttype(client) == CLIENTTYPE_NOTACLIENT)
384                         {
385                                 if(broadcast == NOTIF_TEAM) { checkargs = sprintf("%sNo client provided!", checkargs); }
386                                 else { checkargs = sprintf("%sException can't be a non-client!", checkargs); }
387                         }
388                         break;
389                 }
390                 default: { checkargs = sprintf("%sImproper broadcast: %d!", checkargs, broadcast); break; }
391         }
392         if(checkargs != "") { backtrace(sprintf("Incorrect usage of Send_Notification: %s\n", checkargs)); return; }
393
394         // retreive counts for the arguments of this notification
395         entity notif = Get_Notif_Ent(net_type, net_name);
396         if not(notif) { backtrace("Send_Notification: Could not find notification entity!\n"); return; }
397
398         if((notif.nent_stringcount + notif.nent_floatcount) > count)
399         {
400                 backtrace(sprintf(
401                         strcat(
402                                 "Not enough arguments for Send_Notification! ",
403                                 "stringcount(%d) + floatcount(%d) > count(%d)\n", 
404                                 "Check the definition and function call for accuracy...?\n"
405                         ),
406                         notif.nent_stringcount, notif.nent_floatcount, count));
407                 return;
408         }
409         else if((notif.nent_stringcount + notif.nent_floatcount) < count)
410         {
411                 backtrace(sprintf(
412                         strcat(
413                                 "Too many arguments for Send_Notification! ",
414                                 "stringcount(%d) + floatcount(%d) < count(%d)\n",
415                                 "Check the definition and function call for accuracy...?\n"
416                         ),
417                         notif.nent_stringcount, notif.nent_floatcount, count));
418                 return;
419         }
420         
421         #ifdef NOTIFICATIONS_DEBUG
422         dprint(
423                 sprintf("Send_Notification(%d, %d, %s, stringcount: %d, floatcount: %d, varargs: %d);\n",
424                         broadcast,
425                         net_type,
426                         notif.nent_name,
427                         notif.nent_stringcount,
428                         notif.nent_floatcount,
429                         count
430                 )
431         );
432         #endif
433
434         entity net_notif = spawn();
435         net_notif.nent_broadcast = broadcast;
436         net_notif.nent_client = client;
437         net_notif.nent_net_type = net_type;
438         net_notif.nent_net_name = net_name;
439         net_notif.nent_stringcount = notif.nent_stringcount;
440         net_notif.nent_floatcount = notif.nent_floatcount;
441         
442         float i;
443         for(i = 0; i < net_notif.nent_stringcount; ++i) { net_notif.nent_strings[i] = strzone(...(i, string)); }
444         for(i = 0; i < net_notif.nent_floatcount; ++i) { net_notif.nent_floats[i] = ...((net_notif.nent_stringcount + i), float); }
445         
446         net_notif.think = Notification_Remove;
447         net_notif.nextthink = (time + 0.5); 
448
449         Net_LinkEntity(net_notif, FALSE, 0, Write_Notification);
450
451         if((!server_is_local) && (broadcast == NOTIF_ANY || broadcast == NOTIF_ANY_EXCEPT) && (net_type != MSG_CENTER))
452         {
453                 Local_Notification_Without_VarArgs(
454                         net_type, net_name,
455                         notif.nent_stringcount,
456                         notif.nent_floatcount,
457                         IFSTR(0), IFSTR(1), IFSTR(2), IFSTR(3),
458                         IFFL(0), IFFL(1), IFFL(2), IFFL(3));
459         }
460 }
461
462 void Send_Notification_Without_VarArgs(float broadcast, entity client,
463         float net_type, float net_name,
464         float stringcount, float floatcount,
465         string s1, string s2, string s3, string s4,
466         float f1, float f2, float f3, float f4)
467 {
468         #define VARITEM(stringc,floatc,args) \
469                 if((stringcount == stringc) && (floatcount == floatc)) \
470                         { Send_Notification(broadcast, client, net_type, net_name, args); return; }
471         EIGHT_VARS_TO_VARARGS_VARLIST
472         #undef VARITEM
473         Send_Notification(broadcast, client, net_type, net_name); // some notifications don't have any arguments at all
474 }
475
476 void Send_Notification_Legacy_Wrapper(float broadcast, entity client,
477         float net_type, float net_name,
478         string s1, string s2,
479         float f1, float f2, float f3)
480 {
481         entity notif = Get_Notif_Ent(net_type, net_name);
482         Send_Notification_Without_VarArgs(
483                 broadcast, client,
484                 net_type, net_name,
485                 notif.nent_stringcount,
486                 notif.nent_floatcount,
487                 s1, s2, "", "",
488                 f1, f2, f3, 0);
489 }
490
491
492 // =============================
493 //  LEGACY NOTIFICATION SYSTEMS
494 // =============================
495
496 void Send_CSQC_Centerprint_Generic(entity e, float id, string s, float duration, float countdown_num)
497 {
498         if ((clienttype(e) == CLIENTTYPE_REAL) && (e.flags & FL_CLIENT))
499         {
500                 msg_entity = e;
501                 WRITESPECTATABLE_MSG_ONE({
502                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
503                         WriteByte(MSG_ONE, TE_CSQC_CENTERPRINT_GENERIC);
504                         WriteByte(MSG_ONE, id);
505                         WriteString(MSG_ONE, s);
506                         if (id != 0 && s != "")
507                         {
508                                 WriteByte(MSG_ONE, duration);
509                                 WriteByte(MSG_ONE, countdown_num);
510                         }
511                 });
512         }
513 }
514 void Send_CSQC_Centerprint_Generic_Expire(entity e, float id)
515 {
516         Send_CSQC_Centerprint_Generic(e, id, "", 1, 0);
517 }
518 #endif // ifdef SVQC