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));
20 string Get_Notif_Name(float net_type, float net_name)
22 entity e = Get_Notif_Ent(net_type, net_name);
23 if(e) { return e.nent_name; }
24 backtrace(sprintf("Get_Notif_Name(%d, %d): Could not find entity!\n", net_type, net_name));
28 float Get_Notif_Infval(float net_type, float net_name)
30 entity e = Get_Notif_Ent(net_type, net_name);
31 if(e) { return e.nent_infoname; }
32 backtrace(sprintf("Get_Notif_Infval(%d, %d): Could not find entity!\n", net_type, net_name));
36 float Get_Notif_Cenval(float net_type, float net_name)
38 entity e = Get_Notif_Ent(net_type, net_name);
39 if(e) { return e.nent_centername; }
40 backtrace(sprintf("Get_Notif_Cenval(%d, %d): Could not find entity!\n", net_type, net_name));
44 float Get_Notif_Strnum(float net_type, float net_name)
46 entity e = Get_Notif_Ent(net_type, net_name);
47 if(e) { return e.nent_stringcount; }
48 backtrace(sprintf("Get_Notif_Strnum(%d, %d): Could not find entity!\n", net_type, net_name));
52 float Get_Notif_Flnum(float net_type, float net_name)
54 entity e = Get_Notif_Ent(net_type, net_name);
55 if(e) { return e.nent_floatcount; }
56 backtrace(sprintf("Get_Notif_Flnum(%d, %d): Could not find entity!\n", net_type, net_name));
59 #endif // ifndef MENUQC
62 // ===============================
63 // Frontend Notification Pushing
64 // ===============================
66 void Dump_Notifications(float fh, float alsoprint)
68 float MSG_INFO_NOTIFS = 0, MSG_CENTER_NOTIFS = 0, MSG_WEAPON_NOTIFS = 0, MSG_DEATH_NOTIFS = 0;
70 #define NOTIF_WRITE(type,name,text) { \
72 notif_msg = sprintf("seta %s 1 // %s - %s\n", name, #type, strreplace("\n", "\\n", text)); \
73 fputs(fh, notif_msg); \
74 if(alsoprint) { print(strreplace("^", "^^", notif_msg)); } }
81 for(i = 0; i < NOTIF_INFO_COUNT; ++i) {
82 e = Get_Notif_Ent(MSG_INFO, i);
83 NOTIF_WRITE(MSG_INFO, e.nent_name, e.nent_string); }
85 for(i = 0; i < NOTIF_CENTER_COUNT; ++i) {
86 e = Get_Notif_Ent(MSG_CENTER, i);
87 NOTIF_WRITE(MSG_CENTER, e.nent_name, e.nent_string); }
89 for(i = 0; i < NOTIF_WEAPON_COUNT; ++i) {
90 e = Get_Notif_Ent(MSG_WEAPON, i);
91 NOTIF_WRITE(MSG_WEAPON, e.nent_name, sprintf("infoname: %s, centername: %s",
92 Get_Notif_Name(MSG_INFO, Get_Notif_Infval(MSG_WEAPON, i)),
93 Get_Notif_Name(MSG_CENTER, Get_Notif_Cenval(MSG_WEAPON, i)) ) ); }
95 for(i = 0; i < NOTIF_DEATH_COUNT; ++i) {
96 e = Get_Notif_Ent(MSG_DEATH, i);
97 NOTIF_WRITE(MSG_DEATH, e.nent_name, sprintf("infoname: %s, centername: %s",
98 Get_Notif_Name(MSG_INFO, Get_Notif_Infval(MSG_DEATH, i)),
99 Get_Notif_Name(MSG_CENTER, Get_Notif_Cenval(MSG_DEATH, i)) ) ); }
103 print(sprintf("Notification counts: MSG_INFO = %d, MSG_CENTER = %d, MSG_WEAPON = %d, MSG_DEATH = %d\n",
104 MSG_INFO_NOTIFS, MSG_CENTER_NOTIFS, MSG_WEAPON_NOTIFS, MSG_DEATH_NOTIFS));
111 string Local_Notification_sprintf(string input, string args,
112 string s1, string s2, string s3, string s4,
113 float f1, float f2, float f3, float f4)
115 #ifdef NOTIFICATIONS_DEBUG
116 dprint(sprintf("Local_Notification_sprintf('%s^7', '%s', %s, %s);\n",
117 strreplace("\n", "\\n", input), args,
118 sprintf("'%s^7', '%s^7', '%s^7', '%s^7'", s1, s2, s3, s4),
119 sprintf("%d, %d, %d, %d", f1, f2, f3, f4)));
122 string selected, remaining = args;
129 if((remaining != "") && (input != ""))
131 arg_slot[0] = ""; arg_slot[1] = ""; arg_slot[2] = ""; arg_slot[3] = ""; arg_slot[4] = ""; arg_slot[5] = ""; arg_slot[6] = "";
134 selected = car(remaining); remaining = cdr(remaining);
135 if(sel_num == 7) { backtrace("Local_Notification_sprintf: Hit maximum arguments!\n"); break; }
136 switch(strtolower(selected))
138 #define ARG_CASE(prog,selected,result) \
140 #if (prog == ARG_BOTH) || (prog == ARG_CSQC) \
141 case selected: { arg_slot[sel_num] = result; ++sel_num; break; } \
144 #if (prog == ARG_BOTH) || (prog == ARG_SVQC) \
145 case selected: { arg_slot[sel_num] = result; ++sel_num; break; } \
150 default: { backtrace(sprintf("Local_Notification_sprintf: Hit unknown token in selected string! '%s'\n", selected)); break; }
153 return sprintf(input, arg_slot[0], arg_slot[1], arg_slot[2], arg_slot[3], arg_slot[4], arg_slot[5], arg_slot[6]);
160 void Local_Notification_HUD_Notify_Push(string icon, string hudargs, string s1, string s2)
162 string selected, remaining = hudargs;
164 arg_slot[0] = ""; arg_slot[1] = "";
169 selected = car(remaining); remaining = cdr(remaining);
170 if(sel_num == 2) { backtrace("Local_Notification_HUD_Notify_Push: Hit maximum arguments!\n"); break; }
171 switch(strtolower(selected))
173 #define ARG_CASE(selected,result) case selected: { arg_slot[sel_num] = result; ++sel_num; break; }
177 default: { backtrace(sprintf("Local_Notification_HUD_Notify_Push: Hit unknown token in selected string! '%s'\n", selected)); break; }
181 HUD_Notify_Push(icon, arg_slot[0], arg_slot[1]);
185 void Local_Notification(float net_type, float net_name, ...count)
187 // check supplied type and name for errors
188 #define CHECKARG_TYPENAME(type) case MSG_##type##: \
189 { if(!net_name || (net_name > NOTIF_##type##_COUNT)) \
190 { checkargs = sprintf("Improper name: %d!", net_name); } break; }
191 string checkargs = "";
194 CHECKARG_TYPENAME(INFO)
195 CHECKARG_TYPENAME(CENTER)
196 CHECKARG_TYPENAME(WEAPON)
197 CHECKARG_TYPENAME(DEATH)
198 default: { checkargs = sprintf("Improper type: %d!", checkargs, net_type); break; }
200 #undef CHECKARG_TYPENAME
201 if(checkargs != "") { backtrace(sprintf("Incorrect usage of Local_Notification: %s\n", checkargs)); return; }
203 entity notif = Get_Notif_Ent(net_type, net_name);
204 if not(notif) { backtrace("Local_Notification: Could not find notification entity! (This wasn't caught by usage check?...)\n"); return; }
205 if not(notif.nent_enabled) { print("Local_Notification: Entity was disabled...\n"); return; }
207 if((notif.nent_stringcount + notif.nent_floatcount) > count)
208 { backtrace(sprintf(strcat("Not enough arguments for Local_Notification! stringcount(%d) + floatcount(%d) > count(%d)\n",
209 "Check the notification definition and function call for accuracy...?\n"), notif.nent_stringcount, notif.nent_floatcount, count)); return; }
210 else if((notif.nent_stringcount + notif.nent_floatcount) < count)
211 { backtrace(sprintf(strcat("Too many arguments for Local_Notification! stringcount(%d) + floatcount(%d) < count(%d)\n",
212 "Check the notification definition and function call for accuracy...?\n"), notif.nent_stringcount, notif.nent_floatcount, count)); return; }
214 string s1 = ((0 < notif.nent_stringcount) ? ...(0, string) : "");
215 string s2 = ((1 < notif.nent_stringcount) ? ...(1, string) : "");
216 string s3 = ((2 < notif.nent_stringcount) ? ...(2, string) : "");
217 string s4 = ((3 < notif.nent_stringcount) ? ...(3, string) : "");
218 float f1 = ((notif.nent_stringcount < count) ? ...(notif.nent_stringcount, float) : NO_FL_ARG);
219 float f2 = (((notif.nent_stringcount + 1) < count) ? ...((notif.nent_stringcount + 1), float) : NO_FL_ARG);
220 float f3 = (((notif.nent_stringcount + 2) < count) ? ...((notif.nent_stringcount + 2), float) : NO_FL_ARG);
221 float f4 = (((notif.nent_stringcount + 3) < count) ? ...((notif.nent_stringcount + 3), float) : NO_FL_ARG);
223 #ifdef NOTIFICATIONS_DEBUG
224 dprint(sprintf("Local_Notification(%d, %s, %s, %s);\n",
225 net_type, notif.nent_name,
226 sprintf("'%s^7', '%s^7', '%s^7', '%s^7'", s1, s2, s3, s4),
227 sprintf("%d, %d, %d, %d", f1, f2, f3, f4)));
234 print(Local_Notification_sprintf(notif.nent_string, notif.nent_args,
235 s1, s2, s3, s4, f1, f2, f3, f4));
237 if(notif.nent_icon != "") { Local_Notification_HUD_Notify_Push(notif.nent_icon, notif.nent_hudargs, s1, s2); }
245 centerprint_generic(notif.nent_cpid,
246 Local_Notification_sprintf(notif.nent_string, notif.nent_args,
247 s1, s2, s3, s4, f1, f2, f3, f4), 0, 0);
255 if(notif.nent_infoname)
256 if(msg_info_notifs[notif.nent_infoname - 1].nent_enabled)
258 Local_Notification_Without_VarArgs(MSG_INFO, notif.nent_infoname,
259 Get_Notif_Strnum(MSG_INFO, notif.nent_infoname),
260 Get_Notif_Flnum(MSG_INFO, notif.nent_infoname),
261 s1, s2, s3, s4, f1, f2, f3, f4);
264 if(notif.nent_centername)
265 if(msg_center_notifs[notif.nent_centername - 1].nent_enabled)
267 Local_Notification_Without_VarArgs(MSG_CENTER, notif.nent_centername,
268 Get_Notif_Strnum(MSG_CENTER, notif.nent_centername),
269 Get_Notif_Flnum(MSG_CENTER, notif.nent_centername),
270 s1, s2, s3, s4, f1, f2, f3, f4);
278 void Local_Notification_Without_VarArgs(float net_type, float net_name,
279 float stringcount, float floatcount,
280 string s1, string s2, string s3, string s4,
281 float f1, float f2, float f3, float f4)
283 #define VARITEM(stringc,floatc,args) if((stringcount == stringc) && (floatcount == floatc)) { Local_Notification(net_type, net_name, args); return; }
284 EIGHT_VARS_TO_VARARGS_VARLIST
287 Local_Notification(net_type, net_name); // some notifications don't have any arguments at all
291 // =========================
292 // Notification Networking
293 // =========================
296 void Read_Notification(float is_new)
298 float net_type = ReadByte();
299 float net_name = ReadShort();
301 entity notif = Get_Notif_Ent(net_type, net_name);
302 if not(notif) { print("Read_Notification: Could not find notification entity!\n"); return; }
303 if not(notif.nent_enabled) { print("Read_Notification: Entity was disabled but networked anyway?!?...\n"); return; }
305 string s1 = ((0 < notif.nent_stringcount) ? ReadString() : "");
306 string s2 = ((1 < notif.nent_stringcount) ? ReadString() : "");
307 string s3 = ((2 < notif.nent_stringcount) ? ReadString() : "");
308 string s4 = ((3 < notif.nent_stringcount) ? ReadString() : "");
309 float f1 = ((0 < notif.nent_floatcount) ? ReadLong() : NO_FL_ARG);
310 float f2 = ((1 < notif.nent_floatcount) ? ReadLong() : NO_FL_ARG);
311 float f3 = ((2 < notif.nent_floatcount) ? ReadLong() : NO_FL_ARG);
312 float f4 = ((3 < notif.nent_floatcount) ? ReadLong() : NO_FL_ARG);
314 #ifdef NOTIFICATIONS_DEBUG
315 dprint(sprintf("Read_Notification(%d) at %f: net_name = %s.\n", is_new, time, notif.nent_name));
318 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); }
323 void Notification_Remove()
326 for(i = 0; i < 4; ++i) { if(self.nent_strings[i]) { strunzone(self.nent_strings[i]); } }
330 float Write_Notification(entity client, float sf)
332 float i, send = FALSE;
334 switch(self.nent_broadcast)
336 case NOTIF_ONE: { if((client == self.nent_client) || (client.classname == STR_SPECTATOR && client.enemy == self.nent_client)) { send = TRUE; } break; }
337 case NOTIF_ONE_ONLY: { if(client == self.nent_client) { send = TRUE; } break; }
338 case NOTIF_TEAM: { if((client.team == self.nent_client.team) || (client.classname == STR_SPECTATOR && client.enemy.team == self.nent_client.team)) { send = TRUE; } break; }
339 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; }
340 case NOTIF_ANY: { send = TRUE; break; }
341 case NOTIF_ANY_EXCEPT: { if((client != self.nent_client) && !(client.classname == STR_SPECTATOR && client.enemy == self.nent_client)) { send = TRUE; } break; }
342 default: { send = FALSE; break; }
347 WriteByte(MSG_ENTITY, ENT_CLIENT_NOTIFICATION);
348 WriteByte(MSG_ENTITY, self.nent_net_type);
349 WriteShort(MSG_ENTITY, self.nent_net_name);
350 for(i = 0; i < self.nent_stringcount; ++i) { WriteString(MSG_ENTITY, self.nent_strings[i]); }
351 for(i = 0; i < self.nent_floatcount; ++i) { WriteLong(MSG_ENTITY, self.nent_floats[i]); }
357 void Send_Notification(float broadcast, entity client,
358 float net_type, float net_name, ...count)
360 // check supplied broadcast, target, type, and name for errors
361 #define CHECKARG_TYPENAME(type) case MSG_##type##: \
362 { if(!net_name || (net_name > NOTIF_##type##_COUNT)) \
363 { checkargs = sprintf("Improper name: %d!", net_name); } break; }
364 string checkargs = "";
367 CHECKARG_TYPENAME(INFO)
368 CHECKARG_TYPENAME(CENTER)
369 CHECKARG_TYPENAME(WEAPON)
370 CHECKARG_TYPENAME(DEATH)
371 default: { checkargs = sprintf("Improper type: %d!", checkargs, net_type); break; }
373 #undef CHECKARG_TYPENAME
374 if(checkargs != "") { checkargs = strcat(checkargs, " "); }
378 case NOTIF_ONE_ONLY: { if(clienttype(client) == CLIENTTYPE_NOTACLIENT) { checkargs = sprintf("%sNo client provided!", checkargs); } break; }
379 case NOTIF_ANY_EXCEPT: { if(clienttype(client) == CLIENTTYPE_NOTACLIENT) { checkargs = sprintf("%sException can't be a non-client!", checkargs); } break; }
380 case NOTIF_ANY: { if(client) { checkargs = sprintf("%sEntity provided when world was required!", checkargs); } break; }
382 case NOTIF_TEAM_EXCEPT:
384 if not(teamplay) { checkargs = sprintf("%sTeamplay not active!", checkargs); }
385 else if(clienttype(client) == CLIENTTYPE_NOTACLIENT)
387 if(broadcast == NOTIF_TEAM) { checkargs = sprintf("%sNo client provided!", checkargs); }
388 else { checkargs = sprintf("%sException can't be a non-client!", checkargs); }
392 default: { checkargs = sprintf("%sImproper broadcast: %d!", checkargs, broadcast); break; }
394 if(checkargs != "") { backtrace(sprintf("Incorrect usage of Send_Notification: %s\n", checkargs)); return; }
396 // retreive counts for the arguments of this notification
397 entity notif = Get_Notif_Ent(net_type, net_name);
398 if not(notif) { backtrace("Send_Notification: Could not find notification entity! (This wasn't caught by usage check?...)\n"); return; }
399 if not(notif.nent_enabled) { print("Send_Notification: Entity was disabled...\n"); return; }
401 if((notif.nent_stringcount + notif.nent_floatcount) > count)
402 { backtrace(sprintf(strcat("Not enough arguments for Send_Notification! stringcount(%d) + floatcount(%d) > count(%d)\n",
403 "Check the notification definition and function call for accuracy...?\n"), notif.nent_stringcount, notif.nent_floatcount, count)); return; }
404 else if((notif.nent_stringcount + notif.nent_floatcount) < count)
405 { backtrace(sprintf(strcat("Too many arguments for Send_Notification! stringcount(%d) + floatcount(%d) < count(%d)\n",
406 "Check the notification definition and function call for accuracy...?\n"), notif.nent_stringcount, notif.nent_floatcount, count)); return; }
408 #ifdef NOTIFICATIONS_DEBUG
409 dprint(sprintf("Send_Notification(%d, %d, %s, stringcount: %d, floatcount: %d, varargs: %d);\n",
410 broadcast, net_type, notif.nent_name, notif.nent_stringcount, notif.nent_floatcount, count));
413 entity net_notif = spawn();
414 net_notif.nent_broadcast = broadcast;
415 net_notif.nent_client = client;
416 net_notif.nent_net_type = net_type;
417 net_notif.nent_net_name = net_name;
418 net_notif.nent_stringcount = notif.nent_stringcount;
419 net_notif.nent_floatcount = notif.nent_floatcount;
422 for(i = 0; i < net_notif.nent_stringcount; ++i) { net_notif.nent_strings[i] = strzone(...(i, string)); }
423 for(i = 0; i < net_notif.nent_floatcount; ++i) { net_notif.nent_floats[i] = ...((net_notif.nent_stringcount + i), float); }
425 net_notif.think = Notification_Remove;
426 net_notif.nextthink = (time + 0.5);
428 Net_LinkEntity(net_notif, FALSE, 0, Write_Notification);
430 if((!server_is_local) && (broadcast == NOTIF_ANY || broadcast == NOTIF_ANY_EXCEPT) && (net_type != MSG_CENTER))
431 { Local_Notification_Without_VarArgs(net_type, net_name, notif.nent_stringcount, notif.nent_floatcount,
432 IFSTR(0), IFSTR(1), IFSTR(2), IFSTR(3), IFFL(0), IFFL(1), IFFL(2), IFFL(3)); }
435 void Send_Notification_Without_VarArgs(float broadcast, entity client,
436 float net_type, float net_name,
437 float stringcount, float floatcount,
438 string s1, string s2, string s3, string s4,
439 float f1, float f2, float f3, float f4)
441 #define VARITEM(stringc,floatc,args) if((stringcount == stringc) && (floatcount == floatc)) { Send_Notification(broadcast, client, net_type, net_name, args); return; }
442 EIGHT_VARS_TO_VARARGS_VARLIST
445 Send_Notification(broadcast, client, net_type, net_name); // some notifications don't have any arguments at all
448 void Send_Notification_Legacy_Wrapper(float broadcast, entity client,
449 float net_type, float net_name,
450 string s1, string s2,
451 float f1, float f2, float f3)
453 float stringcount = Get_Notif_Strnum(net_type, net_name);
454 float floatcount = Get_Notif_Flnum(net_type, net_name);
455 Send_Notification_Without_VarArgs(broadcast, client, net_type, net_name, stringcount, floatcount, s1, s2, "", "", f1, f2, f3, NO_FL_ARG);
459 // =============================
460 // LEGACY NOTIFICATION SYSTEMS
461 // =============================
463 void Send_CSQC_Centerprint_Generic(entity e, float id, string s, float duration, float countdown_num)
465 if ((clienttype(e) == CLIENTTYPE_REAL) && (e.flags & FL_CLIENT))
468 WRITESPECTATABLE_MSG_ONE({
469 WriteByte(MSG_ONE, SVC_TEMPENTITY);
470 WriteByte(MSG_ONE, TE_CSQC_CENTERPRINT_GENERIC);
471 WriteByte(MSG_ONE, id);
472 WriteString(MSG_ONE, s);
473 if (id != 0 && s != "")
475 WriteByte(MSG_ONE, duration);
476 WriteByte(MSG_ONE, countdown_num);
481 void Send_CSQC_Centerprint_Generic_Expire(entity e, float id)
483 Send_CSQC_Centerprint_Generic(e, id, "", 1, 0);
486 #endif // ifndef MENUQC