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 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)); }
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)); }
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));
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)
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)));
82 string selected, remaining = args;
89 arg_slot[0] = ""; arg_slot[1] = ""; arg_slot[2] = ""; arg_slot[3] = ""; arg_slot[4] = ""; arg_slot[5] = ""; arg_slot[6] = "";
90 for(;(remaining != "");)
92 selected = car(remaining); remaining = cdr(remaining);
93 if(sel_num == 7) { backtrace("Local_Notification_sprintf: Hit maximum arguments!\n"); break; }
94 switch(strtolower(selected))
96 #define ARG_CASE(prog,selected,result) \
98 #if (prog == ARG_BOTH) || (prog == ARG_CSQC) \
99 case selected: { arg_slot[sel_num] = result; ++sel_num; break; } \
102 #if (prog == ARG_BOTH) || (prog == ARG_SVQC) \
103 case selected: { arg_slot[sel_num] = result; ++sel_num; break; } \
108 default: { backtrace(sprintf("Local_Notification_sprintf: Hit unknown token in selected string! '%s'\n", selected)); break; }
111 return sprintf(input, arg_slot[0], arg_slot[1], arg_slot[2], arg_slot[3], arg_slot[4], arg_slot[5], arg_slot[6]);
115 void Local_Notification_HUD_Notify_Push(string icon, string hudargs, string s1, string s2)
117 string selected, remaining = hudargs;
119 arg_slot[0] = ""; arg_slot[1] = "";
120 for(;(remaining != "");)
122 selected = car(remaining); remaining = cdr(remaining);
123 if(sel_num == 2) { backtrace("Local_Notification_HUD_Notify_Push: Hit maximum arguments!\n"); break; }
124 switch(strtolower(selected))
126 #define ARG_CASE(selected,result) case selected: { arg_slot[sel_num] = result; ++sel_num; break; }
130 default: { backtrace(sprintf("Local_Notification_HUD_Notify_Push: Hit unknown token in selected string! '%s'\n", selected)); break; }
133 HUD_Notify_Push(icon, arg_slot[0], arg_slot[1]);
137 void Local_Notification(float net_type, float net_name, ...count)
139 // check supplied type and name for errors
140 #define CHECKARG_TYPENAME(type) case MSG_##type##: \
141 { if(!net_name || (net_name > NOTIF_##type##_COUNT)) \
142 { checkargs = sprintf("Improper name: %d!", net_name); } break; }
143 string checkargs = "";
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; }
152 #undef CHECKARG_TYPENAME
153 if(checkargs != "") { backtrace(sprintf("Incorrect usage of Local_Notification: %s\n", checkargs)); return; }
155 entity notif = Get_Notif_Ent(net_type, net_name);
156 if not(notif) { backtrace("Local_Notification: Could not find notification entity! (This wasn't caught by usage check?...)\n"); return; }
157 if not(notif.nent_enabled) { print("Local_Notification: Entity was disabled...\n"); return; }
159 if((notif.nent_stringcount + notif.nent_floatcount) > count)
160 { backtrace(sprintf(strcat("Not enough arguments for Local_Notification! stringcount(%d) + floatcount(%d) > count(%d)\n",
161 "Check the notification definition and function call for accuracy...?\n"), notif.nent_stringcount, notif.nent_floatcount, count)); return; }
162 else if((notif.nent_stringcount + notif.nent_floatcount) < count)
163 { backtrace(sprintf(strcat("Too many arguments for Local_Notification! stringcount(%d) + floatcount(%d) < count(%d)\n",
164 "Check the notification definition and function call for accuracy...?\n"), notif.nent_stringcount, notif.nent_floatcount, count)); return; }
166 string s1 = ((0 < notif.nent_stringcount) ? ...(0, string) : "");
167 string s2 = ((1 < notif.nent_stringcount) ? ...(1, string) : "");
168 string s3 = ((2 < notif.nent_stringcount) ? ...(2, string) : "");
169 string s4 = ((3 < notif.nent_stringcount) ? ...(3, string) : "");
170 float f1 = ((0 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 0), float) : 0);
171 float f2 = ((1 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 1), float) : 0);
172 float f3 = ((2 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 2), float) : 0);
173 float f4 = ((3 < notif.nent_floatcount) ? ...((notif.nent_stringcount + 3), float) : 0);
175 #ifdef NOTIFICATIONS_DEBUG
176 dprint(sprintf("Local_Notification(%d, %s, %s, %s);\n",
177 net_type, notif.nent_name,
178 sprintf("'%s^7', '%s^7', '%s^7', '%s^7'", s1, s2, s3, s4),
179 sprintf("%d, %d, %d, %d", f1, f2, f3, f4)));
186 print(Local_Notification_sprintf(notif.nent_string, notif.nent_args,
187 s1, s2, s3, s4, f1, f2, f3, f4));
189 if(notif.nent_icon != "") { Local_Notification_HUD_Notify_Push(notif.nent_icon, notif.nent_hudargs, s1, s2); }
197 centerprint_generic(notif.nent_cpid,
198 Local_Notification_sprintf(notif.nent_string, notif.nent_args,
199 s1, s2, s3, s4, f1, f2, f3, f4), 0, 0);
207 if(notif.nent_infoname)
208 if(msg_info_notifs[notif.nent_infoname - 1].nent_enabled)
210 Local_Notification_Without_VarArgs(MSG_INFO, notif.nent_infoname,
211 msg_info_notifs[notif.nent_infoname - 1].nent_stringcount,
212 msg_info_notifs[notif.nent_infoname - 1].nent_floatcount,
213 s1, s2, s3, s4, f1, f2, f3, f4);
216 if(notif.nent_centername)
217 if(msg_center_notifs[notif.nent_centername - 1].nent_enabled)
219 Local_Notification_Without_VarArgs(MSG_CENTER, notif.nent_centername,
220 msg_center_notifs[notif.nent_centername - 1].nent_stringcount,
221 msg_center_notifs[notif.nent_centername - 1].nent_floatcount,
222 s1, s2, s3, s4, f1, f2, f3, f4);
230 void Local_Notification_Without_VarArgs(float net_type, float net_name,
231 float stringcount, float floatcount,
232 string s1, string s2, string s3, string s4,
233 float f1, float f2, float f3, float f4)
235 #define VARITEM(stringc,floatc,args) if((stringcount == stringc) && (floatcount == floatc)) { Local_Notification(net_type, net_name, args); return; }
236 EIGHT_VARS_TO_VARARGS_VARLIST
238 Local_Notification(net_type, net_name); // some notifications don't have any arguments at all
242 // =========================
243 // Notification Networking
244 // =========================
247 void Read_Notification(float is_new)
249 float net_type = ReadByte();
250 float net_name = ReadShort();
252 entity notif = Get_Notif_Ent(net_type, net_name);
253 if not(notif) { print("Read_Notification: Could not find notification entity!\n"); return; }
254 if not(notif.nent_enabled) { print("Read_Notification: Entity was disabled but networked anyway?!?...\n"); return; }
256 string s1 = ((0 < notif.nent_stringcount) ? ReadString() : "");
257 string s2 = ((1 < notif.nent_stringcount) ? ReadString() : "");
258 string s3 = ((2 < notif.nent_stringcount) ? ReadString() : "");
259 string s4 = ((3 < notif.nent_stringcount) ? ReadString() : "");
260 float f1 = ((0 < notif.nent_floatcount) ? ReadLong() : 0);
261 float f2 = ((1 < notif.nent_floatcount) ? ReadLong() : 0);
262 float f3 = ((2 < notif.nent_floatcount) ? ReadLong() : 0);
263 float f4 = ((3 < notif.nent_floatcount) ? ReadLong() : 0);
265 #ifdef NOTIFICATIONS_DEBUG
266 dprint(sprintf("Read_Notification(%d) at %f: net_name = %s.\n", is_new, time, notif.nent_name));
269 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); }
274 void Notification_Remove()
277 for(i = 0; i < 4; ++i) { if(self.nent_strings[i]) { strunzone(self.nent_strings[i]); } }
281 float Write_Notification(entity client, float sf)
283 float i, send = FALSE;
285 switch(self.nent_broadcast)
287 case NOTIF_ONE: { if((client == self.nent_client) || (client.classname == STR_SPECTATOR && client.enemy == self.nent_client)) { send = TRUE; } break; }
288 case NOTIF_ONE_ONLY: { if(client == self.nent_client) { send = TRUE; } break; }
289 case NOTIF_TEAM: { if((client.team == self.nent_client.team) || (client.classname == STR_SPECTATOR && client.enemy.team == self.nent_client.team)) { send = TRUE; } break; }
290 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; }
291 case NOTIF_ANY: { send = TRUE; break; }
292 case NOTIF_ANY_EXCEPT: { if((client != self.nent_client) && !(client.classname == STR_SPECTATOR && client.enemy == self.nent_client)) { send = TRUE; } break; }
293 default: { send = FALSE; break; }
298 WriteByte(MSG_ENTITY, ENT_CLIENT_NOTIFICATION);
299 WriteByte(MSG_ENTITY, self.nent_net_type);
300 WriteShort(MSG_ENTITY, self.nent_net_name);
301 for(i = 0; i < self.nent_stringcount; ++i) { WriteString(MSG_ENTITY, self.nent_strings[i]); }
302 for(i = 0; i < self.nent_floatcount; ++i) { WriteLong(MSG_ENTITY, self.nent_floats[i]); }
308 void Send_Notification(float broadcast, entity client,
309 float net_type, float net_name, ...count)
311 // check supplied broadcast, target, type, and name for errors
312 #define CHECKARG_TYPENAME(type) case MSG_##type##: \
313 { if(!net_name || (net_name > NOTIF_##type##_COUNT)) \
314 { checkargs = sprintf("Improper name: %d!", net_name); } break; }
315 string checkargs = "";
318 CHECKARG_TYPENAME(INFO)
319 CHECKARG_TYPENAME(CENTER)
320 CHECKARG_TYPENAME(WEAPON)
321 CHECKARG_TYPENAME(DEATH)
322 default: { checkargs = sprintf("Improper type: %d!", checkargs, net_type); break; }
324 #undef CHECKARG_TYPENAME
325 if(checkargs != "") { checkargs = strcat(checkargs, " "); }
329 case NOTIF_ONE_ONLY: { if(clienttype(client) == CLIENTTYPE_NOTACLIENT) { checkargs = sprintf("%sNo client provided!", checkargs); } break; }
330 case NOTIF_ANY_EXCEPT: { if(clienttype(client) == CLIENTTYPE_NOTACLIENT) { checkargs = sprintf("%sException can't be a non-client!", checkargs); } break; }
331 case NOTIF_ANY: { if(client) { checkargs = sprintf("%sEntity provided when world was required!", checkargs); } break; }
333 case NOTIF_TEAM_EXCEPT:
335 if not(teamplay) { checkargs = sprintf("%sTeamplay not active!", checkargs); }
336 else if(clienttype(client) == CLIENTTYPE_NOTACLIENT)
338 if(broadcast == NOTIF_TEAM) { checkargs = sprintf("%sNo client provided!", checkargs); }
339 else { checkargs = sprintf("%sException can't be a non-client!", checkargs); }
343 default: { checkargs = sprintf("%sImproper broadcast: %d!", checkargs, broadcast); break; }
345 if(checkargs != "") { backtrace(sprintf("Incorrect usage of Send_Notification: %s\n", checkargs)); return; }
347 // retreive counts for the arguments of this notification
348 entity notif = Get_Notif_Ent(net_type, net_name);
349 if not(notif) { backtrace("Send_Notification: Could not find notification entity! (This wasn't caught by usage check?...)\n"); return; }
350 if not(notif.nent_enabled) { print("Send_Notification: Entity was disabled...\n"); return; }
352 if((notif.nent_stringcount + notif.nent_floatcount) > count)
353 { backtrace(sprintf(strcat("Not enough arguments for Send_Notification! stringcount(%d) + floatcount(%d) > count(%d)\n",
354 "Check the notification definition and function call for accuracy...?\n"), notif.nent_stringcount, notif.nent_floatcount, count)); return; }
355 else if((notif.nent_stringcount + notif.nent_floatcount) < count)
356 { backtrace(sprintf(strcat("Too many arguments for Send_Notification! stringcount(%d) + floatcount(%d) < count(%d)\n",
357 "Check the notification definition and function call for accuracy...?\n"), notif.nent_stringcount, notif.nent_floatcount, count)); return; }
359 #ifdef NOTIFICATIONS_DEBUG
360 dprint(sprintf("Send_Notification(%d, %d, %s, stringcount: %d, floatcount: %d, varargs: %d);\n",
361 broadcast, net_type, notif.nent_name, notif.nent_stringcount, notif.nent_floatcount, count));
364 entity net_notif = spawn();
365 net_notif.nent_broadcast = broadcast;
366 net_notif.nent_client = client;
367 net_notif.nent_net_type = net_type;
368 net_notif.nent_net_name = net_name;
369 net_notif.nent_stringcount = notif.nent_stringcount;
370 net_notif.nent_floatcount = notif.nent_floatcount;
373 for(i = 0; i < net_notif.nent_stringcount; ++i) { net_notif.nent_strings[i] = strzone(...(i, string)); }
374 for(i = 0; i < net_notif.nent_floatcount; ++i) { net_notif.nent_floats[i] = ...((net_notif.nent_stringcount + i), float); }
376 net_notif.think = Notification_Remove;
377 net_notif.nextthink = (time + 0.5);
379 Net_LinkEntity(net_notif, FALSE, 0, Write_Notification);
381 if((!server_is_local) && (broadcast == NOTIF_ANY || broadcast == NOTIF_ANY_EXCEPT) && (net_type != MSG_CENTER))
382 { Local_Notification_Without_VarArgs(net_type, net_name, notif.nent_stringcount, notif.nent_floatcount,
383 IFSTR(0), IFSTR(1), IFSTR(2), IFSTR(3), IFFL(0), IFFL(1), IFFL(2), IFFL(3)); }
386 void Send_Notification_Without_VarArgs(float broadcast, entity client,
387 float net_type, float net_name,
388 float stringcount, float floatcount,
389 string s1, string s2, string s3, string s4,
390 float f1, float f2, float f3, float f4)
392 #define VARITEM(stringc,floatc,args) if((stringcount == stringc) && (floatcount == floatc)) { Send_Notification(broadcast, client, net_type, net_name, args); return; }
393 EIGHT_VARS_TO_VARARGS_VARLIST
395 Send_Notification(broadcast, client, net_type, net_name); // some notifications don't have any arguments at all
398 void Send_Notification_Legacy_Wrapper(float broadcast, entity client,
399 float net_type, float net_name,
400 string s1, string s2,
401 float f1, float f2, float f3)
403 entity notif = Get_Notif_Ent(net_type, net_name);
404 Send_Notification_Without_VarArgs(broadcast, client, net_type, net_name,
405 notif.nent_stringcount, notif.nent_floatcount,
406 s1, s2, "", "", f1, f2, f3, 0);
410 // =============================
411 // LEGACY NOTIFICATION SYSTEMS
412 // =============================
414 void Send_CSQC_Centerprint_Generic(entity e, float id, string s, float duration, float countdown_num)
416 if ((clienttype(e) == CLIENTTYPE_REAL) && (e.flags & FL_CLIENT))
419 WRITESPECTATABLE_MSG_ONE({
420 WriteByte(MSG_ONE, SVC_TEMPENTITY);
421 WriteByte(MSG_ONE, TE_CSQC_CENTERPRINT_GENERIC);
422 WriteByte(MSG_ONE, id);
423 WriteString(MSG_ONE, s);
424 if (id != 0 && s != "")
426 WriteByte(MSG_ONE, duration);
427 WriteByte(MSG_ONE, countdown_num);
432 void Send_CSQC_Centerprint_Generic_Expire(entity e, float id)
434 Send_CSQC_Centerprint_Generic(e, id, "", 1, 0);
437 #endif // ifndef MENUQC