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