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