1 // ================================================
2 // Unified notification system, written by Samual
3 // Last updated: February, 2013
4 // ================================================
7 entity Get_Notif_Ent(float net_type, float net_name)
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];
16 backtrace(sprintf("Get_Notif_Ent(%d, %d): Improper net type!\n", net_type, net_name));
19 #endif // ifndef MENUQC
22 // ===============================
23 // Frontend Notification Pushing
24 // ===============================
26 void Dump_Notifications(float fh, float alsoprint)
28 float MSG_INFO_NOTIFS = 0, MSG_CENTER_NOTIFS = 0, MSG_WEAPON_NOTIFS = 0, MSG_DEATH_NOTIFS = 0;
30 #define NOTIF_WRITE(type,name,text) { \
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)); } }
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); }
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); }
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)); }
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)); }
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));
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)
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)));
80 string selected, remaining = args;
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 != "");)
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))
94 #define ARG_CASE(prog,selected,result) \
96 #if (prog == ARG_BOTH) || (prog == ARG_CSQC) \
97 case selected: { arg_slot[sel_num] = result; ++sel_num; break; } \
100 #if (prog == ARG_BOTH) || (prog == ARG_SVQC) \
101 case selected: { arg_slot[sel_num] = result; ++sel_num; break; } \
106 default: { backtrace(sprintf("Local_Notification_sprintf: Hit unknown token in selected string! '%s'\n", selected)); break; }
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]);
113 void Local_Notification_HUD_Notify_Push(string icon, string hudargs, string s1, string s2)
115 string selected, remaining = hudargs;
117 arg_slot[0] = ""; arg_slot[1] = "";
118 for(;(remaining != "");)
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))
124 #define ARG_CASE(selected,result) case selected: { arg_slot[sel_num] = result; ++sel_num; break; }
128 default: { backtrace(sprintf("Local_Notification_HUD_Notify_Push: Hit unknown token in selected string! '%s'\n", selected)); break; }
131 HUD_Notify_Push(icon, arg_slot[0], arg_slot[1]);
135 void Local_Notification(float net_type, float net_name, ...count)
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 = "";
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; }
150 #undef CHECKARG_TYPENAME
151 if(checkargs != "") { backtrace(sprintf("Incorrect usage of Local_Notification: %s\n", checkargs)); return; }
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; }
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; }
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);
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)));
184 print(Local_Notification_sprintf(notif.nent_string, notif.nent_args,
185 s1, s2, s3, s4, f1, f2, f3, f4));
187 if(notif.nent_icon != "") { Local_Notification_HUD_Notify_Push(notif.nent_icon, notif.nent_hudargs, s1, s2); }
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);
205 if(notif.nent_msginfo)
206 if(notif.nent_msginfo.nent_enabled)
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);
214 if(notif.nent_msgcenter)
215 if(notif.nent_msgcenter.nent_enabled)
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);
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)
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
236 Local_Notification(net_type, net_name); // some notifications don't have any arguments at all
240 // =========================
241 // Notification Networking
242 // =========================
245 void Read_Notification(float is_new)
247 float net_type = ReadByte();
248 float net_name = ReadShort();
250 entity notif = Get_Notif_Ent(net_type, net_name);
251 if not(notif) { print("Read_Notification: Could not find notification entity!\n"); return; }
253 string s1 = ((0 < notif.nent_stringcount) ? ReadString() : "");
254 string s2 = ((1 < notif.nent_stringcount) ? ReadString() : "");
255 string s3 = ((2 < notif.nent_stringcount) ? ReadString() : "");
256 string s4 = ((3 < notif.nent_stringcount) ? ReadString() : "");
257 float f1 = ((0 < notif.nent_floatcount) ? ReadLong() : 0);
258 float f2 = ((1 < notif.nent_floatcount) ? ReadLong() : 0);
259 float f3 = ((2 < notif.nent_floatcount) ? ReadLong() : 0);
260 float f4 = ((3 < notif.nent_floatcount) ? ReadLong() : 0);
262 #ifdef NOTIFICATIONS_DEBUG
263 dprint(sprintf("Read_Notification(%d) at %f: net_name = %s.\n", is_new, time, notif.nent_name));
266 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); }
271 void Notification_Remove()
274 for(i = 0; i < 4; ++i) { if(self.nent_strings[i]) { strunzone(self.nent_strings[i]); } }
278 float Write_Notification(entity client, float sf)
280 float i, send = FALSE;
282 switch(self.nent_broadcast)
284 case NOTIF_ONE: { if((client == self.nent_client) || (client.classname == STR_SPECTATOR && client.enemy == self.nent_client)) { send = TRUE; } break; }
285 case NOTIF_ONE_ONLY: { if(client == self.nent_client) { send = TRUE; } break; }
286 case NOTIF_TEAM: { if((client.team == self.nent_client.team) || (client.classname == STR_SPECTATOR && client.enemy.team == self.nent_client.team)) { send = TRUE; } break; }
287 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; }
288 case NOTIF_ANY: { send = TRUE; break; }
289 case NOTIF_ANY_EXCEPT: { if((client != self.nent_client) && !(client.classname == STR_SPECTATOR && client.enemy == self.nent_client)) { send = TRUE; } break; }
290 default: { send = FALSE; break; }
295 WriteByte(MSG_ENTITY, ENT_CLIENT_NOTIFICATION);
296 WriteByte(MSG_ENTITY, self.nent_net_type);
297 WriteShort(MSG_ENTITY, self.nent_net_name);
298 for(i = 0; i < self.nent_stringcount; ++i) { WriteString(MSG_ENTITY, self.nent_strings[i]); }
299 for(i = 0; i < self.nent_floatcount; ++i) { WriteLong(MSG_ENTITY, self.nent_floats[i]); }
305 void Send_Notification(float broadcast, entity client,
306 float net_type, float net_name, ...count)
308 // check supplied broadcast, target, type, and name for errors
309 #define CHECKARG_TYPENAME(type) case MSG_##type##: \
310 { if(!net_name || (net_name > NOTIF_##type##_COUNT)) \
311 { checkargs = sprintf("Improper name: %d!", net_name); } break; }
312 string checkargs = "";
315 CHECKARG_TYPENAME(INFO)
316 CHECKARG_TYPENAME(CENTER)
317 CHECKARG_TYPENAME(WEAPON)
318 CHECKARG_TYPENAME(DEATH)
319 default: { checkargs = sprintf("Improper type: %d!", checkargs, net_type); break; }
321 #undef CHECKARG_TYPENAME
322 if(checkargs != "") { checkargs = strcat(checkargs, " "); }
326 case NOTIF_ONE_ONLY: { if(clienttype(client) == CLIENTTYPE_NOTACLIENT) { checkargs = sprintf("%sNo client provided!", checkargs); } break; }
327 case NOTIF_ANY_EXCEPT: { if(clienttype(client) == CLIENTTYPE_NOTACLIENT) { checkargs = sprintf("%sException can't be a non-client!", checkargs); } break; }
328 case NOTIF_ANY: { if(client) { checkargs = sprintf("%sEntity provided when world was required!", checkargs); } break; }
330 case NOTIF_TEAM_EXCEPT:
332 if not(teamplay) { checkargs = sprintf("%sTeamplay not active!", checkargs); }
333 else if(clienttype(client) == CLIENTTYPE_NOTACLIENT)
335 if(broadcast == NOTIF_TEAM) { checkargs = sprintf("%sNo client provided!", checkargs); }
336 else { checkargs = sprintf("%sException can't be a non-client!", checkargs); }
340 default: { checkargs = sprintf("%sImproper broadcast: %d!", checkargs, broadcast); break; }
342 if(checkargs != "") { backtrace(sprintf("Incorrect usage of Send_Notification: %s\n", checkargs)); return; }
344 // retreive counts for the arguments of this notification
345 entity notif = Get_Notif_Ent(net_type, net_name);
346 if not(notif) { backtrace("Send_Notification: Could not find notification entity! (This wasn't caught by usage check?...)\n"); return; }
348 if((notif.nent_stringcount + notif.nent_floatcount) > count)
349 { backtrace(sprintf(strcat("Not enough arguments for Send_Notification! stringcount(%d) + floatcount(%d) > count(%d)\n",
350 "Check the notification definition and function call for accuracy...?\n"), notif.nent_stringcount, notif.nent_floatcount, count)); return; }
351 else if((notif.nent_stringcount + notif.nent_floatcount) < count)
352 { backtrace(sprintf(strcat("Too many arguments for Send_Notification! stringcount(%d) + floatcount(%d) < count(%d)\n",
353 "Check the notification definition and function call for accuracy...?\n"), notif.nent_stringcount, notif.nent_floatcount, count)); return; }
355 #ifdef NOTIFICATIONS_DEBUG
356 dprint(sprintf("Send_Notification(%d, %d, %s, stringcount: %d, floatcount: %d, varargs: %d);\n",
357 broadcast, net_type, notif.nent_name, notif.nent_stringcount, notif.nent_floatcount, count));
360 entity net_notif = spawn();
361 net_notif.nent_broadcast = broadcast;
362 net_notif.nent_client = client;
363 net_notif.nent_net_type = net_type;
364 net_notif.nent_net_name = net_name;
365 net_notif.nent_stringcount = notif.nent_stringcount;
366 net_notif.nent_floatcount = notif.nent_floatcount;
369 for(i = 0; i < net_notif.nent_stringcount; ++i) { net_notif.nent_strings[i] = strzone(...(i, string)); }
370 for(i = 0; i < net_notif.nent_floatcount; ++i) { net_notif.nent_floats[i] = ...((net_notif.nent_stringcount + i), float); }
372 net_notif.think = Notification_Remove;
373 net_notif.nextthink = (time + 0.5);
375 Net_LinkEntity(net_notif, FALSE, 0, Write_Notification);
377 if((!server_is_local) && (broadcast == NOTIF_ANY || broadcast == NOTIF_ANY_EXCEPT) && (net_type != MSG_CENTER))
378 { Local_Notification_Without_VarArgs(net_type, net_name, notif.nent_stringcount, notif.nent_floatcount,
379 IFSTR(0), IFSTR(1), IFSTR(2), IFSTR(3), IFFL(0), IFFL(1), IFFL(2), IFFL(3)); }
382 void Send_Notification_Without_VarArgs(float broadcast, entity client,
383 float net_type, float net_name,
384 float stringcount, float floatcount,
385 string s1, string s2, string s3, string s4,
386 float f1, float f2, float f3, float f4)
388 #define VARITEM(stringc,floatc,args) if((stringcount == stringc) && (floatcount == floatc)) { Send_Notification(broadcast, client, net_type, net_name, args); return; }
389 EIGHT_VARS_TO_VARARGS_VARLIST
391 Send_Notification(broadcast, client, net_type, net_name); // some notifications don't have any arguments at all
394 void Send_Notification_Legacy_Wrapper(float broadcast, entity client,
395 float net_type, float net_name,
396 string s1, string s2,
397 float f1, float f2, float f3)
399 entity notif = Get_Notif_Ent(net_type, net_name);
400 Send_Notification_Without_VarArgs(broadcast, client, net_type, net_name,
401 notif.nent_stringcount, notif.nent_floatcount,
402 s1, s2, "", "", f1, f2, f3, 0);
406 // =============================
407 // LEGACY NOTIFICATION SYSTEMS
408 // =============================
410 void Send_CSQC_Centerprint_Generic(entity e, float id, string s, float duration, float countdown_num)
412 if ((clienttype(e) == CLIENTTYPE_REAL) && (e.flags & FL_CLIENT))
415 WRITESPECTATABLE_MSG_ONE({
416 WriteByte(MSG_ONE, SVC_TEMPENTITY);
417 WriteByte(MSG_ONE, TE_CSQC_CENTERPRINT_GENERIC);
418 WriteByte(MSG_ONE, id);
419 WriteString(MSG_ONE, s);
420 if (id != 0 && s != "")
422 WriteByte(MSG_ONE, duration);
423 WriteByte(MSG_ONE, countdown_num);
428 void Send_CSQC_Centerprint_Generic_Expire(entity e, float id)
430 Send_CSQC_Centerprint_Generic(e, id, "", 1, 0);
433 #endif // ifndef MENUQC