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 if((remaining != "") && (input != ""))
91 arg_slot[0] = ""; arg_slot[1] = ""; arg_slot[2] = ""; arg_slot[3] = ""; arg_slot[4] = ""; arg_slot[5] = ""; arg_slot[6] = "";
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))
98 #define ARG_CASE(prog,selected,result) \
100 #if (prog == ARG_BOTH) || (prog == ARG_CSQC) \
101 case selected: { arg_slot[sel_num] = result; ++sel_num; break; } \
104 #if (prog == ARG_BOTH) || (prog == ARG_SVQC) \
105 case selected: { arg_slot[sel_num] = result; ++sel_num; break; } \
110 default: { backtrace(sprintf("Local_Notification_sprintf: Hit unknown token in selected string! '%s'\n", selected)); break; }
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]);
120 void Local_Notification_HUD_Notify_Push(string icon, string hudargs, string s1, string s2)
122 string selected, remaining = hudargs;
124 arg_slot[0] = ""; arg_slot[1] = "";
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))
133 #define ARG_CASE(selected,result) case selected: { arg_slot[sel_num] = result; ++sel_num; break; }
137 default: { backtrace(sprintf("Local_Notification_HUD_Notify_Push: Hit unknown token in selected string! '%s'\n", selected)); break; }
141 HUD_Notify_Push(icon, arg_slot[0], arg_slot[1]);
145 void Local_Notification(float net_type, float net_name, ...count)
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 = "";
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; }
160 #undef CHECKARG_TYPENAME
161 if(checkargs != "") { backtrace(sprintf("Incorrect usage of Local_Notification: %s\n", checkargs)); return; }
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; }
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; }
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);
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)));
194 print(Local_Notification_sprintf(notif.nent_string, notif.nent_args,
195 s1, s2, s3, s4, f1, f2, f3, f4));
197 if(notif.nent_icon != "") { Local_Notification_HUD_Notify_Push(notif.nent_icon, notif.nent_hudargs, s1, s2); }
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);
215 if(notif.nent_infoname)
216 if(msg_info_notifs[notif.nent_infoname - 1].nent_enabled)
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);
224 if(notif.nent_centername)
225 if(msg_center_notifs[notif.nent_centername - 1].nent_enabled)
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);
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)
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
246 Local_Notification(net_type, net_name); // some notifications don't have any arguments at all
250 // =========================
251 // Notification Networking
252 // =========================
255 void Read_Notification(float is_new)
257 float net_type = ReadByte();
258 float net_name = ReadShort();
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; }
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);
273 #ifdef NOTIFICATIONS_DEBUG
274 dprint(sprintf("Read_Notification(%d) at %f: net_name = %s.\n", is_new, time, notif.nent_name));
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); }
282 void Notification_Remove()
285 for(i = 0; i < 4; ++i) { if(self.nent_strings[i]) { strunzone(self.nent_strings[i]); } }
289 float Write_Notification(entity client, float sf)
291 float i, send = FALSE;
293 switch(self.nent_broadcast)
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; }
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]); }
316 void Send_Notification(float broadcast, entity client,
317 float net_type, float net_name, ...count)
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 = "";
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; }
332 #undef CHECKARG_TYPENAME
333 if(checkargs != "") { checkargs = strcat(checkargs, " "); }
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; }
341 case NOTIF_TEAM_EXCEPT:
343 if not(teamplay) { checkargs = sprintf("%sTeamplay not active!", checkargs); }
344 else if(clienttype(client) == CLIENTTYPE_NOTACLIENT)
346 if(broadcast == NOTIF_TEAM) { checkargs = sprintf("%sNo client provided!", checkargs); }
347 else { checkargs = sprintf("%sException can't be a non-client!", checkargs); }
351 default: { checkargs = sprintf("%sImproper broadcast: %d!", checkargs, broadcast); break; }
353 if(checkargs != "") { backtrace(sprintf("Incorrect usage of Send_Notification: %s\n", checkargs)); return; }
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; }
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; }
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));
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;
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); }
384 net_notif.think = Notification_Remove;
385 net_notif.nextthink = (time + 0.5);
387 Net_LinkEntity(net_notif, FALSE, 0, Write_Notification);
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)); }
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)
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
403 Send_Notification(broadcast, client, net_type, net_name); // some notifications don't have any arguments at all
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)
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);
418 // =============================
419 // LEGACY NOTIFICATION SYSTEMS
420 // =============================
422 void Send_CSQC_Centerprint_Generic(entity e, float id, string s, float duration, float countdown_num)
424 if ((clienttype(e) == CLIENTTYPE_REAL) && (e.flags & FL_CLIENT))
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 != "")
434 WriteByte(MSG_ONE, duration);
435 WriteByte(MSG_ONE, countdown_num);
440 void Send_CSQC_Centerprint_Generic_Expire(entity e, float id)
442 Send_CSQC_Centerprint_Generic(e, id, "", 1, 0);
445 #endif // ifndef MENUQC