1 #include "sv_superspec.qh"
3 string autocvar_g_superspectate;
4 REGISTER_MUTATOR(superspec, expr_evaluate(autocvar_g_superspectate));
6 #define _SSMAGIX "SUPERSPEC_OPTIONSFILE_V1"
7 #define _ISLOCAL(ent) ((edict_num(1) == (ent)) ? true : false)
9 const float ASF_STRENGTH = BIT(0);
10 const float ASF_SHIELD = BIT(1);
11 const float ASF_MEGA_AR = BIT(2);
12 const float ASF_MEGA_HP = BIT(3);
13 const float ASF_FLAG_GRAB = BIT(4);
14 const float ASF_OBSERVER_ONLY = BIT(5);
15 const float ASF_SHOWWHAT = BIT(6);
16 const float ASF_SSIM = BIT(7);
17 const float ASF_FOLLOWKILLER = BIT(8);
18 const float ASF_ALL = 0xFFFFFF;
19 .float autospec_flags;
21 const float SSF_SILENT = 1;
22 const float SSF_VERBOSE = 2;
23 const float SSF_ITEMMSG = 4;
24 .float superspec_flags;
26 .string superspec_itemfilter; //"classname1 classname2 ..."
28 bool superspec_Spectate(entity this, entity targ)
30 if(Spectate(this, targ) == 1)
31 TRANSMUTE(Spectator, this);
36 void superspec_save_client_conf(entity this)
38 string fn = "superspec-local.options";
43 if(this.crypto_idfp == "")
46 fn = sprintf("superspec-%s.options", uri_escape(this.crypto_idfp));
49 fh = fopen(fn, FILE_WRITE);
52 LOG_TRACE("^1ERROR: ^7 superspec can not open ", fn, " for writing.");
58 fputs(fh, ftos(this.autospec_flags));
60 fputs(fh, ftos(this.superspec_flags));
62 fputs(fh, this.superspec_itemfilter);
68 void superspec_msg(string _center_title, string _con_title, entity _to, string _msg, float _spamlevel)
70 sprint(_to, strcat(_con_title, _msg));
72 if(_to.superspec_flags & SSF_SILENT)
76 if (!(_to.superspec_flags & SSF_VERBOSE))
79 centerprint(_to, strcat(_center_title, _msg));
82 float superspec_filteritem(entity _for, entity _item)
86 if(_for.superspec_itemfilter == "")
89 if(_for.superspec_itemfilter == "")
92 float l = tokenize_console(_for.superspec_itemfilter);
93 for(i = 0; i < l; ++i)
95 if(argv(i) == _item.classname)
102 MUTATOR_HOOKFUNCTION(superspec, ItemTouch)
104 entity item = M_ARGV(0, entity);
105 entity toucher = M_ARGV(1, entity);
107 FOREACH_CLIENT(true, {
108 if(!IS_SPEC(it) && !IS_OBSERVER(it))
110 if(it.superspec_flags & SSF_ITEMMSG)
111 if(superspec_filteritem(it, item))
113 if(it.superspec_flags & SSF_VERBOSE)
114 superspec_msg("", "", it, sprintf("Player %s^7 just picked up ^3%s\n", toucher.netname, item.netname), 1);
116 superspec_msg("", "", it, sprintf("Player %s^7 just picked up ^3%s\n^8(%s^8)\n", toucher.netname, item.netname, item.classname), 1);
117 if((it.autospec_flags & ASF_SSIM) && it.enemy != toucher)
119 superspec_Spectate(it, toucher);
120 return MUT_ITEMTOUCH_CONTINUE;
124 if(((it.autospec_flags & ASF_SHIELD) && item.invincible_finished) ||
125 ((it.autospec_flags & ASF_STRENGTH) && item.strength_finished) ||
126 ((it.autospec_flags & ASF_MEGA_AR) && item.itemdef == ITEM_ArmorMega) ||
127 ((it.autospec_flags & ASF_MEGA_HP) && item.itemdef == ITEM_HealthMega) ||
128 ((it.autospec_flags & ASF_FLAG_GRAB) && item.classname == "item_flag_team"))
131 if((it.enemy != toucher) || IS_OBSERVER(it))
133 if((it.autospec_flags & ASF_OBSERVER_ONLY) && !IS_OBSERVER(it))
135 if(it.superspec_flags & SSF_VERBOSE)
136 superspec_msg("", "", it, sprintf("^8Ignored that ^7%s^8 grabbed %s^8 since the observer_only option is ON\n", toucher.netname, item.netname), 2);
140 if(it.autospec_flags & ASF_SHOWWHAT)
141 superspec_msg("", "", it, sprintf("^7Following %s^7 due to picking up %s\n", toucher.netname, item.netname), 2);
143 superspec_Spectate(it, toucher);
149 return MUT_ITEMTOUCH_CONTINUE;
152 MUTATOR_HOOKFUNCTION(superspec, SV_ParseClientCommand)
154 #define OPTIONINFO(flag,var,test,text,long,short) \
155 var = strcat(var, ((flag & test) ? "^2[ON] ^7" : "^1[OFF] ^7")); \
156 var = strcat(var, text," ^7(^3 ", long, "^7 | ^3", short, " ^7)\n")
158 if(MUTATOR_RETURNVALUE) // command was already handled?
161 entity player = M_ARGV(0, entity);
162 string cmd_name = M_ARGV(1, string);
163 int cmd_argc = M_ARGV(2, int);
165 if(IS_PLAYER(player))
168 if(cmd_name == "superspec_itemfilter")
170 if(argv(1) == "help")
173 _aspeco = "^7 superspec_itemfilter ^3\"item_classname1 item_classname2\"^7 only show thise items when ^2superspec ^3item_message^7 is on\n";
174 _aspeco = strcat(_aspeco, "^3 clear^7 Remove the filter (show all pickups)\n");
175 _aspeco = strcat(_aspeco, "^3 show ^7 Display current filter\n");
176 superspec_msg("^3superspec_itemfilter help:\n\n\n", "\n^3superspec_itemfilter help:\n", player, _aspeco, 1);
178 else if(argv(1) == "clear")
180 if(player.superspec_itemfilter != "")
181 strunzone(player.superspec_itemfilter);
183 player.superspec_itemfilter = "";
185 else if(argv(1) == "show" || argv(1) == "")
187 if(player.superspec_itemfilter == "")
189 superspec_msg("^3superspec_itemfilter^7 is ^1not^7 set", "\n^3superspec_itemfilter^7 is ^1not^7 set\n", player, "", 1);
193 float l = tokenize_console(player.superspec_itemfilter);
195 for(i = 0; i < l; ++i)
196 _msg = strcat(_msg, "^3#", ftos(i), " ^7", argv(i), "\n");
197 //_msg = sprintf("^3#%d^7 %s\n%s", i, _msg, argv(i));
199 _msg = strcat(_msg,"\n");
201 superspec_msg("^3superspec_itemfilter is:\n\n\n", "\n^3superspec_itemfilter is:\n", player, _msg, 1);
205 if(player.superspec_itemfilter != "")
206 strunzone(player.superspec_itemfilter);
208 player.superspec_itemfilter = strzone(argv(1));
214 if(cmd_name == "superspec")
220 float i, _bits = 0, _start = 1;
221 if(argv(1) == "help")
223 _aspeco = "use cmd superspec [option] [on|off] to set options\n\n";
224 _aspeco = strcat(_aspeco, "^3 silent ^7(short^5 si^7) supresses ALL messages from superspectate.\n");
225 _aspeco = strcat(_aspeco, "^3 verbose ^7(short^5 ve^7) makes superspectate print some additional information.\n");
226 _aspeco = strcat(_aspeco, "^3 item_message ^7(short^5 im^7) makes superspectate print items that were picked up.\n");
227 _aspeco = strcat(_aspeco, "^7 Use cmd superspec_itemfilter \"item_class1 item_class2\" to set up a filter of what to show with ^3item_message.\n");
228 superspec_msg("^2Available Super Spectate ^3options:\n\n\n", "\n^2Available Super Spectate ^3options:\n", player, _aspeco, 1);
232 if(argv(1) == "clear")
234 player.superspec_flags = 0;
238 for(i = _start; i < cmd_argc; ++i)
240 if(argv(i) == "on" || argv(i) == "1")
242 player.superspec_flags |= _bits;
245 else if(argv(i) == "off" || argv(i) == "0")
248 player.superspec_flags &= ~_bits;
254 if((argv(i) == "silent") || (argv(i) == "si")) _bits |= SSF_SILENT ;
255 if((argv(i) == "verbose") || (argv(i) == "ve")) _bits |= SSF_VERBOSE;
256 if((argv(i) == "item_message") || (argv(i) == "im")) _bits |= SSF_ITEMMSG;
262 OPTIONINFO(player.superspec_flags, _aspeco, SSF_SILENT, "Silent", "silent", "si");
263 OPTIONINFO(player.superspec_flags, _aspeco, SSF_VERBOSE, "Verbose", "verbose", "ve");
264 OPTIONINFO(player.superspec_flags, _aspeco, SSF_ITEMMSG, "Item pickup messages", "item_message", "im");
266 superspec_msg("^3Current Super Spectate options are:\n\n\n\n\n", "\n^3Current Super Spectate options are:\n", player, _aspeco, 1);
271 /////////////////////
273 if(cmd_name == "autospec")
278 if(argv(1) == "help")
280 _aspeco = "use cmd autospec [option] [on|off] to set options\n\n";
281 _aspeco = strcat(_aspeco, "^3 strength ^7(short^5 st^7) for automatic spectate on strength powerup\n");
282 _aspeco = strcat(_aspeco, "^3 shield ^7(short^5 sh^7) for automatic spectate on shield powerup\n");
283 _aspeco = strcat(_aspeco, "^3 mega_health ^7(short^5 mh^7) for automatic spectate on mega health\n");
284 _aspeco = strcat(_aspeco, "^3 mega_armor ^7(short^5 ma^7) for automatic spectate on mega armor\n");
285 _aspeco = strcat(_aspeco, "^3 flag_grab ^7(short^5 fg^7) for automatic spectate on CTF flag grab\n");
286 _aspeco = strcat(_aspeco, "^3 observer_only ^7(short^5 oo^7) for automatic spectate only if in observer mode\n");
287 _aspeco = strcat(_aspeco, "^3 show_what ^7(short^5 sw^7) to display what event triggered autospectate\n");
288 _aspeco = strcat(_aspeco, "^3 item_msg ^7(short^5 im^7) to autospec when item_message in superspectate is triggered\n");
289 _aspeco = strcat(_aspeco, "^3 followkiller ^7(short ^5fk^7) to autospec the killer/off\n");
290 _aspeco = strcat(_aspeco, "^3 all ^7(short ^5aa^7) to turn everything on/off\n");
291 superspec_msg("^2Available Auto Spectate ^3options:\n\n\n", "\n^2Available Auto Spectate ^3options:\n", player, _aspeco, 1);
295 float i, _bits = 0, _start = 1;
296 if(argv(1) == "clear")
298 player.autospec_flags = 0;
302 for(i = _start; i < cmd_argc; ++i)
304 if(argv(i) == "on" || argv(i) == "1")
306 player.autospec_flags |= _bits;
309 else if(argv(i) == "off" || argv(i) == "0")
312 player.autospec_flags &= ~_bits;
318 if((argv(i) == "strength") || (argv(i) == "st")) _bits |= ASF_STRENGTH;
319 if((argv(i) == "shield") || (argv(i) == "sh")) _bits |= ASF_SHIELD;
320 if((argv(i) == "mega_health") || (argv(i) == "mh")) _bits |= ASF_MEGA_HP;
321 if((argv(i) == "mega_armor") || (argv(i) == "ma")) _bits |= ASF_MEGA_AR;
322 if((argv(i) == "flag_grab") || (argv(i) == "fg")) _bits |= ASF_FLAG_GRAB;
323 if((argv(i) == "observer_only") || (argv(i) == "oo")) _bits |= ASF_OBSERVER_ONLY;
324 if((argv(i) == "show_what") || (argv(i) == "sw")) _bits |= ASF_SHOWWHAT;
325 if((argv(i) == "item_msg") || (argv(i) == "im")) _bits |= ASF_SSIM;
326 if((argv(i) == "followkiller") || (argv(i) == "fk")) _bits |= ASF_FOLLOWKILLER;
327 if((argv(i) == "all") || (argv(i) == "aa")) _bits |= ASF_ALL;
333 OPTIONINFO(player.autospec_flags, _aspeco, ASF_STRENGTH, "Strength", "strength", "st");
334 OPTIONINFO(player.autospec_flags, _aspeco, ASF_SHIELD, "Shield", "shield", "sh");
335 OPTIONINFO(player.autospec_flags, _aspeco, ASF_MEGA_HP, "Mega Health", "mega_health", "mh");
336 OPTIONINFO(player.autospec_flags, _aspeco, ASF_MEGA_AR, "Mega Armor", "mega_armor", "ma");
337 OPTIONINFO(player.autospec_flags, _aspeco, ASF_FLAG_GRAB, "Flag grab", "flag_grab","fg");
338 OPTIONINFO(player.autospec_flags, _aspeco, ASF_OBSERVER_ONLY, "Only switch if observer", "observer_only", "oo");
339 OPTIONINFO(player.autospec_flags, _aspeco, ASF_SHOWWHAT, "Show what item triggered spectate", "show_what", "sw");
340 OPTIONINFO(player.autospec_flags, _aspeco, ASF_SSIM, "Switch on superspec item message", "item_msg", "im");
341 OPTIONINFO(player.autospec_flags, _aspeco, ASF_FOLLOWKILLER, "Followkiller", "followkiller", "fk");
343 superspec_msg("^3Current auto spectate options are:\n\n\n\n\n", "\n^3Current auto spectate options are:\n", player, _aspeco, 1);
347 if(cmd_name == "followpowerup")
349 FOREACH_CLIENT(IS_PLAYER(it) && (STAT(STRENGTH_FINISHED, it) > time || STAT(INVINCIBLE_FINISHED, it) > time), { return superspec_Spectate(player, it); });
351 superspec_msg("", "", player, "No active powerup\n", 1);
355 if(cmd_name == "followstrength")
357 FOREACH_CLIENT(IS_PLAYER(it) && STAT(STRENGTH_FINISHED, it) > time, { return superspec_Spectate(player, it); });
359 superspec_msg("", "", player, "No active Strength\n", 1);
363 if(cmd_name == "followshield")
365 FOREACH_CLIENT(IS_PLAYER(it) && STAT(INVINCIBLE_FINISHED, it) > time, { return superspec_Spectate(player, it); });
367 superspec_msg("", "", player, "No active Shield\n", 1);
373 MUTATOR_HOOKFUNCTION(superspec, BuildMutatorsString)
375 M_ARGV(0, string) = strcat(M_ARGV(0, string), ":SS");
378 MUTATOR_HOOKFUNCTION(superspec, BuildMutatorsPrettyString)
380 M_ARGV(0, string) = strcat(M_ARGV(0, string), ", Super Spectators");
383 void superspec_hello(entity this)
385 if(this.enemy.crypto_idfp == "")
386 Send_Notification(NOTIF_ONE_ONLY, this.enemy, MSG_INFO, INFO_SUPERSPEC_MISSING_UID);
391 MUTATOR_HOOKFUNCTION(superspec, ClientConnect)
393 entity player = M_ARGV(0, entity);
395 if(!IS_REAL_CLIENT(player))
398 string fn = "superspec-local.options";
401 player.superspec_flags = SSF_VERBOSE;
402 player.superspec_itemfilter = "";
404 entity _hello = new_pure(superspec_delayed_hello);
405 _hello.enemy = player;
406 setthink(_hello, superspec_hello);
407 _hello.nextthink = time + 5;
409 if (!_ISLOCAL(player))
411 if(player.crypto_idfp == "")
414 fn = sprintf("superspec-%s.options", uri_escape(player.crypto_idfp));
417 fh = fopen(fn, FILE_READ);
420 LOG_TRACE("^1ERROR: ^7 superspec can not open ", fn, " for reading.");
424 string _magic = fgets(fh);
425 if(_magic != _SSMAGIX)
427 LOG_TRACE("^1ERROR^7 While reading superspec options file: unknown magic");
431 player.autospec_flags = stof(fgets(fh));
432 player.superspec_flags = stof(fgets(fh));
433 player.superspec_itemfilter = strzone(fgets(fh));
439 MUTATOR_HOOKFUNCTION(superspec, PlayerDies)
441 entity frag_attacker = M_ARGV(1, entity);
442 entity frag_target = M_ARGV(2, entity);
444 FOREACH_CLIENT(IS_SPEC(it), {
445 if((it.autospec_flags & ASF_FOLLOWKILLER) && IS_PLAYER(frag_attacker) && it.enemy == frag_target)
447 if(it.autospec_flags & ASF_SHOWWHAT)
448 superspec_msg("", "", it, sprintf("^7Following %s^7 due to followkiller\n", frag_attacker.netname), 2);
450 superspec_Spectate(it, frag_attacker);
455 MUTATOR_HOOKFUNCTION(superspec, ClientDisconnect)
457 entity player = M_ARGV(0, entity);
459 superspec_save_client_conf(player);