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