]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator_superspec.qc
Hook: merge offhand and weapon behaviour
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator_superspec.qc
1 #include "../_all.qh"
2
3 #include "mutator.qh"
4
5 #define _SSMAGIX "SUPERSPEC_OPTIONSFILE_V1"
6 #define _ISLOCAL ((edict_num(1) == self) ? true : false)
7
8 const float ASF_STRENGTH                = 1;
9 const float ASF_SHIELD                  = 2;
10 const float ASF_MEGA_AR                 = 4;
11 const float ASF_MEGA_HP                 = 8;
12 const float ASF_FLAG_GRAB               = 16;
13 const float ASF_OBSERVER_ONLY   = 32;
14 const float ASF_SHOWWHAT                = 64;
15 const float ASF_SSIM                    = 128;
16 const float ASF_FOLLOWKILLER    = 256;
17 const float ASF_ALL                     = 0xFFFFFF;
18 .float autospec_flags;
19
20 const float SSF_SILENT = 1;
21 const float SSF_VERBOSE = 2;
22 const float SSF_ITEMMSG = 4;
23 .float superspec_flags;
24
25 .string superspec_itemfilter; //"classname1 classname2 ..."
26
27 float _spectate(entity _player)
28 {SELFPARAM();
29         if(Spectate(_player) == 1)
30                 self.classname = "spectator";
31
32         return true;
33 }
34
35 void superspec_save_client_conf()
36 {SELFPARAM();
37         string fn = "superspec-local.options";
38         float fh;
39
40         if (!_ISLOCAL)
41         {
42                 if(self.crypto_idfp == "")
43                         return;
44
45                 fn = sprintf("superspec-%s.options", uri_escape(self.crypto_idfp));
46         }
47
48         fh = fopen(fn, FILE_WRITE);
49         if(fh < 0)
50         {
51                 LOG_TRACE("^1ERROR: ^7 superspec can not open ", fn, " for writing.\n");
52         }
53         else
54         {
55                 fputs(fh, _SSMAGIX);
56                 fputs(fh, "\n");
57                 fputs(fh, ftos(self.autospec_flags));
58                 fputs(fh, "\n");
59                 fputs(fh, ftos(self.superspec_flags));
60                 fputs(fh, "\n");
61                 fputs(fh, self.superspec_itemfilter);
62                 fputs(fh, "\n");
63                 fclose(fh);
64         }
65 }
66
67 void superspec_msg(string _center_title, string _con_title, entity _to, string _msg, float _spamlevel)
68 {
69         sprint(_to, strcat(_con_title, _msg));
70
71         if(_to.superspec_flags & SSF_SILENT)
72                 return;
73
74         if(_spamlevel > 1)
75                 if (!(_to.superspec_flags & SSF_VERBOSE))
76                         return;
77
78         centerprint(_to, strcat(_center_title, _msg));
79 }
80
81 float superspec_filteritem(entity _for, entity _item)
82 {
83         float i;
84
85         if(_for.superspec_itemfilter == "")
86                 return true;
87
88         if(_for.superspec_itemfilter == "")
89                 return true;
90
91         float l = tokenize_console(_for.superspec_itemfilter);
92         for(i = 0; i < l; ++i)
93         {
94                 if(argv(i) == _item.classname)
95                         return true;
96         }
97
98         return false;
99 }
100
101 MUTATOR_HOOKFUNCTION(superspec_ItemTouch)
102 {SELFPARAM();
103         entity _item = self;
104
105         entity e;
106         FOR_EACH_SPEC(e)
107         {
108                 setself(e);
109                 if(self.superspec_flags & SSF_ITEMMSG)
110                         if(superspec_filteritem(self, _item))
111                         {
112                                 if(self.superspec_flags & SSF_VERBOSE)
113                                         superspec_msg("", "", self, sprintf("Player %s^7 just picked up ^3%s\n", other.netname, _item.netname), 1);
114                                 else
115                                         superspec_msg("", "", self, sprintf("Player %s^7 just picked up ^3%s\n^8(%s^8)\n", other.netname, _item.netname, _item.classname), 1);
116                                 if((self.autospec_flags & ASF_SSIM) && self.enemy != other)
117                                 {
118                                         _spectate(other);
119
120                                         setself(this);
121                                         return MUT_ITEMTOUCH_CONTINUE;
122                                 }
123                         }
124
125                 if((self.autospec_flags & ASF_SHIELD && _item.invincible_finished) ||
126                         (self.autospec_flags & ASF_STRENGTH && _item.strength_finished) ||
127                         (self.autospec_flags & ASF_MEGA_AR && _item.itemdef == ITEM_ArmorMega) ||
128                         (self.autospec_flags & ASF_MEGA_HP && _item.itemdef == ITEM_HealthMega) ||
129                         (self.autospec_flags & ASF_FLAG_GRAB && _item.classname == "item_flag_team"))
130                 {
131
132                         if((self.enemy != other) || IS_OBSERVER(self))
133                         {
134                                 if(self.autospec_flags & ASF_OBSERVER_ONLY && !IS_OBSERVER(self))
135                                 {
136                                         if(self.superspec_flags & SSF_VERBOSE)
137                                                 superspec_msg("", "", self, sprintf("^8Ignored that ^7%s^8 grabbed %s^8 since the observer_only option is ON\n", other.netname, _item.netname), 2);
138                                 }
139                                 else
140                                 {
141                                         if(self.autospec_flags & ASF_SHOWWHAT)
142                                                 superspec_msg("", "", self, sprintf("^7Following %s^7 due to picking up %s\n", other.netname, _item.netname), 2);
143
144                                         _spectate(other);
145                                 }
146                         }
147                 }
148         }
149
150         setself(this);
151
152         return MUT_ITEMTOUCH_CONTINUE;
153 }
154
155 MUTATOR_HOOKFUNCTION(superspec_SV_ParseClientCommand)
156 {SELFPARAM();
157 #define OPTIONINFO(flag,var,test,text,long,short) \
158     var = strcat(var, ((flag & test) ? "^2[ON]  ^7" : "^1[OFF] ^7")); \
159     var = strcat(var, text," ^7(^3 ", long, "^7 | ^3", short, " ^7)\n")
160
161         if(MUTATOR_RETURNVALUE) // command was already handled?
162                 return false;
163
164         if(IS_PLAYER(self))
165                 return false;
166
167         if(cmd_name == "superspec_itemfilter")
168         {
169                 if(argv(1) == "help")
170                 {
171                         string _aspeco;
172                         _aspeco = "^7 superspec_itemfilter ^3\"item_classname1 item_classname2\"^7 only show thise items when ^2superspec ^3item_message^7 is on\n";
173                         _aspeco = strcat(_aspeco, "^3 clear^7 Remove the filter (show all pickups)\n");
174                         _aspeco = strcat(_aspeco, "^3 show ^7 Display current filter\n");
175                         superspec_msg("^3superspec_itemfilter help:\n\n\n", "\n^3superspec_itemfilter help:\n", self, _aspeco, 1);
176                 }
177                 else if(argv(1) == "clear")
178                 {
179                         if(self.superspec_itemfilter != "")
180                                 strunzone(self.superspec_itemfilter);
181
182                         self.superspec_itemfilter = "";
183                 }
184                 else if(argv(1) == "show" || argv(1) == "")
185                 {
186                         if(self.superspec_itemfilter == "")
187                         {
188                                 superspec_msg("^3superspec_itemfilter^7 is ^1not^7 set", "\n^3superspec_itemfilter^7 is ^1not^7 set\n", self, "", 1);
189                                 return true;
190                         }
191                         float i;
192                         float l = tokenize_console(self.superspec_itemfilter);
193                         string _msg = "";
194                         for(i = 0; i < l; ++i)
195                                 _msg = strcat(_msg, "^3#", ftos(i), " ^7", argv(i), "\n");
196                                 //_msg = sprintf("^3#%d^7 %s\n%s", i, _msg, argv(i));
197
198                         _msg = strcat(_msg,"\n");
199
200                         superspec_msg("^3superspec_itemfilter is:\n\n\n", "\n^3superspec_itemfilter is:\n", self, _msg, 1);
201                 }
202                 else
203                 {
204                         if(self.superspec_itemfilter != "")
205                                 strunzone(self.superspec_itemfilter);
206
207                         self.superspec_itemfilter = strzone(argv(1));
208                 }
209
210                 return true;
211         }
212
213         if(cmd_name == "superspec")
214         {
215                 string _aspeco;
216
217                 if(cmd_argc > 1)
218                 {
219                         float i, _bits = 0, _start = 1;
220                         if(argv(1) == "help")
221                         {
222                                 _aspeco = "use cmd superspec [option] [on|off] to set options\n\n";
223                                 _aspeco = strcat(_aspeco, "^3 silent ^7(short^5 si^7) supresses ALL messages from superspectate.\n");
224                                 _aspeco = strcat(_aspeco, "^3 verbose ^7(short^5 ve^7) makes superspectate print some additional information.\n");
225                                 _aspeco = strcat(_aspeco, "^3 item_message ^7(short^5 im^7) makes superspectate print items that were picked up.\n");
226                                 _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");
227                                 superspec_msg("^2Available Super Spectate ^3options:\n\n\n", "\n^2Available Super Spectate ^3options:\n", self, _aspeco, 1);
228                                 return true;
229                         }
230
231                         if(argv(1) == "clear")
232                         {
233                                 self.superspec_flags = 0;
234                                 _start = 2;
235                         }
236
237                         for(i = _start; i < cmd_argc; ++i)
238                         {
239                                 if(argv(i) == "on" || argv(i) == "1")
240                                 {
241                                         self.superspec_flags |= _bits;
242                                         _bits = 0;
243                                 }
244                                 else if(argv(i) == "off" || argv(i) == "0")
245                                 {
246                                         if(_start == 1)
247                                                 self.superspec_flags &= ~_bits;
248
249                                         _bits = 0;
250                                 }
251                                 else
252                                 {
253                                         if((argv(i) == "silent") || (argv(i) == "si")) _bits |= SSF_SILENT ;
254                                         if((argv(i) == "verbose") || (argv(i) == "ve")) _bits |= SSF_VERBOSE;
255                                         if((argv(i) == "item_message") || (argv(i) == "im")) _bits |= SSF_ITEMMSG;
256                                 }
257                         }
258                 }
259
260                 _aspeco = "";
261                 OPTIONINFO(self.superspec_flags, _aspeco, SSF_SILENT, "Silent", "silent", "si");
262                 OPTIONINFO(self.superspec_flags, _aspeco, SSF_VERBOSE, "Verbose", "verbose", "ve");
263                 OPTIONINFO(self.superspec_flags, _aspeco, SSF_ITEMMSG, "Item pickup messages", "item_message", "im");
264
265                 superspec_msg("^3Current Super Spectate options are:\n\n\n\n\n", "\n^3Current Super Spectate options are:\n", self, _aspeco, 1);
266
267                 return true;
268         }
269
270 /////////////////////
271
272         if(cmd_name == "autospec")
273         {
274                 string _aspeco;
275                 if(cmd_argc > 1)
276                 {
277                         if(argv(1) == "help")
278                         {
279                                 _aspeco = "use cmd autospec [option] [on|off] to set options\n\n";
280                                 _aspeco = strcat(_aspeco, "^3 strength ^7(short^5 st^7) for automatic spectate on strength powerup\n");
281                                 _aspeco = strcat(_aspeco, "^3 shield ^7(short^5 sh^7) for automatic spectate on shield powerup\n");
282                                 _aspeco = strcat(_aspeco, "^3 mega_health ^7(short^5 mh^7) for automatic spectate on mega health\n");
283                                 _aspeco = strcat(_aspeco, "^3 mega_armor ^7(short^5 ma^7) for automatic spectate on mega armor\n");
284                                 _aspeco = strcat(_aspeco, "^3 flag_grab ^7(short^5 fg^7) for automatic spectate on CTF flag grab\n");
285                                 _aspeco = strcat(_aspeco, "^3 observer_only ^7(short^5 oo^7) for automatic spectate only if in observer mode\n");
286                                 _aspeco = strcat(_aspeco, "^3 show_what ^7(short^5 sw^7) to display what event triggered autospectate\n");
287                                 _aspeco = strcat(_aspeco, "^3 item_msg ^7(short^5 im^7) to autospec when item_message in superspectate is triggered\n");
288                                 _aspeco = strcat(_aspeco, "^3 followkiller ^7(short ^5fk^7) to autospec the killer/off\n");
289                                 _aspeco = strcat(_aspeco, "^3 all ^7(short ^5aa^7) to turn everything on/off\n");
290                                 superspec_msg("^2Available Auto Spectate ^3options:\n\n\n", "\n^2Available Auto Spectate ^3options:\n", self, _aspeco, 1);
291                                 return true;
292                         }
293
294                         float i, _bits = 0, _start = 1;
295                         if(argv(1) == "clear")
296                         {
297                                 self.autospec_flags = 0;
298                                 _start = 2;
299                         }
300
301                         for(i = _start; i < cmd_argc; ++i)
302                         {
303                                 if(argv(i) == "on" || argv(i) == "1")
304                                 {
305                                         self.autospec_flags |= _bits;
306                                         _bits = 0;
307                                 }
308                                 else if(argv(i) == "off" || argv(i) == "0")
309                                 {
310                                         if(_start == 1)
311                                                 self.autospec_flags &= ~_bits;
312
313                                         _bits = 0;
314                                 }
315                                 else
316                                 {
317                                         if((argv(i) == "strength") || (argv(i) == "st")) _bits |= ASF_STRENGTH;
318                                         if((argv(i) == "shield") || (argv(i) == "sh")) _bits |= ASF_SHIELD;
319                                         if((argv(i) == "mega_health") || (argv(i) == "mh")) _bits |= ASF_MEGA_HP;
320                                         if((argv(i) == "mega_armor") || (argv(i) == "ma")) _bits |= ASF_MEGA_AR;
321                                         if((argv(i) == "flag_grab") || (argv(i) == "fg")) _bits |= ASF_FLAG_GRAB;
322                                         if((argv(i) == "observer_only") || (argv(i) == "oo")) _bits |= ASF_OBSERVER_ONLY;
323                                         if((argv(i) == "show_what") || (argv(i) == "sw")) _bits |= ASF_SHOWWHAT;
324                                         if((argv(i) == "item_msg") || (argv(i) == "im")) _bits |= ASF_SSIM;
325                                         if((argv(i) == "followkiller") || (argv(i) == "fk")) _bits |= ASF_FOLLOWKILLER;
326                                         if((argv(i) == "all") || (argv(i) == "aa")) _bits |= ASF_ALL;
327                                 }
328                         }
329                 }
330
331                 _aspeco = "";
332                 OPTIONINFO(self.autospec_flags, _aspeco, ASF_STRENGTH, "Strength", "strength", "st");
333                 OPTIONINFO(self.autospec_flags, _aspeco, ASF_SHIELD, "Shield", "shield", "sh");
334                 OPTIONINFO(self.autospec_flags, _aspeco, ASF_MEGA_HP, "Mega Health", "mega_health", "mh");
335                 OPTIONINFO(self.autospec_flags, _aspeco, ASF_MEGA_AR, "Mega Armor", "mega_armor", "ma");
336                 OPTIONINFO(self.autospec_flags, _aspeco, ASF_FLAG_GRAB, "Flag grab", "flag_grab","fg");
337                 OPTIONINFO(self.autospec_flags, _aspeco, ASF_OBSERVER_ONLY, "Only switch if observer", "observer_only", "oo");
338                 OPTIONINFO(self.autospec_flags, _aspeco, ASF_SHOWWHAT, "Show what item triggered spectate", "show_what", "sw");
339                 OPTIONINFO(self.autospec_flags, _aspeco, ASF_SSIM, "Switch on superspec item message", "item_msg", "im");
340                 OPTIONINFO(self.autospec_flags, _aspeco, ASF_FOLLOWKILLER, "Followkiller", "followkiller", "fk");
341
342                 superspec_msg("^3Current auto spectate options are:\n\n\n\n\n", "\n^3Current auto spectate options are:\n", self, _aspeco, 1);
343                 return true;
344         }
345
346         if(cmd_name == "followpowerup")
347         {
348                 entity _player;
349                 FOR_EACH_PLAYER(_player)
350                 {
351                         if(_player.strength_finished > time || _player.invincible_finished > time)
352                                 return _spectate(_player);
353                 }
354
355                 superspec_msg("", "", self, "No active powerup\n", 1);
356                 return true;
357         }
358
359         if(cmd_name == "followstrength")
360         {
361                 entity _player;
362                 FOR_EACH_PLAYER(_player)
363                 {
364                         if(_player.strength_finished > time)
365                                 return _spectate(_player);
366                 }
367
368                 superspec_msg("", "", self, "No active Strength\n", 1);
369                 return true;
370         }
371
372         if(cmd_name == "followshield")
373         {
374                 entity _player;
375                 FOR_EACH_PLAYER(_player)
376                 {
377                         if(_player.invincible_finished > time)
378                                 return _spectate(_player);
379                 }
380
381                 superspec_msg("", "", self, "No active Shield\n", 1);
382                 return true;
383         }
384
385         if(cmd_name == "followfc")
386         {
387                 if(!g_ctf)
388                         return true;
389
390                 entity _player;
391                 int _team = 0;
392                 bool found = false;
393
394                 if(cmd_argc == 2)
395                 {
396                         switch(argv(1))
397                         {
398                                 case "red": _team = NUM_TEAM_1; break;
399                                 case "blue": _team = NUM_TEAM_2; break;
400                                 case "yellow": if(ctf_teams >= 3) _team = NUM_TEAM_3; break;
401                                 case "pink": if(ctf_teams >= 4) _team = NUM_TEAM_4; break;
402                         }
403                 }
404
405                 FOR_EACH_PLAYER(_player)
406                 {
407                         if(_player.flagcarried && (_player.team == _team || _team == 0))
408                         {
409                                 found = true;
410                                 if(_team == 0 && IS_SPEC(self) && self.enemy == _player)
411                                         continue; // already spectating a fc, try to find the other fc
412                                 return _spectate(_player);
413                         }
414                 }
415
416                 if(!found)
417                         superspec_msg("", "", self, "No active flag carrier\n", 1);
418                 return true;
419         }
420
421         return false;
422 #undef OPTIONINFO
423 }
424
425 MUTATOR_HOOKFUNCTION(superspec_BuildMutatorsString)
426 {
427         ret_string = strcat(ret_string, ":SS");
428         return 0;
429 }
430
431 MUTATOR_HOOKFUNCTION(superspec_BuildMutatorsPrettyString)
432 {
433         ret_string = strcat(ret_string, ", Super Spectators");
434         return 0;
435 }
436
437 void superspec_hello()
438 {SELFPARAM();
439         if(self.enemy.crypto_idfp == "")
440                 Send_Notification(NOTIF_ONE_ONLY, self.enemy, MSG_INFO, INFO_SUPERSPEC_MISSING_UID);
441
442         remove(self);
443 }
444
445 MUTATOR_HOOKFUNCTION(superspec_ClientConnect)
446 {SELFPARAM();
447         if(!IS_REAL_CLIENT(self))
448                 return false;
449
450         string fn = "superspec-local.options";
451         float fh;
452
453         self.superspec_flags = SSF_VERBOSE;
454         self.superspec_itemfilter = "";
455
456         entity _hello = spawn();
457         _hello.enemy = self;
458         _hello.think = superspec_hello;
459         _hello.nextthink = time + 5;
460
461         if (!_ISLOCAL)
462         {
463                 if(self.crypto_idfp == "")
464                         return false;
465
466                 fn = sprintf("superspec-%s.options", uri_escape(self.crypto_idfp));
467         }
468
469         fh = fopen(fn, FILE_READ);
470         if(fh < 0)
471         {
472                 LOG_TRACE("^1ERROR: ^7 superspec can not open ", fn, " for reading.\n");
473         }
474         else
475         {
476                 string _magic = fgets(fh);
477                 if(_magic != _SSMAGIX)
478                 {
479                         LOG_TRACE("^1ERROR^7 While reading superspec options file: unknown magic\n");
480                 }
481                 else
482                 {
483                         self.autospec_flags = stof(fgets(fh));
484                         self.superspec_flags = stof(fgets(fh));
485                         self.superspec_itemfilter = strzone(fgets(fh));
486                 }
487                 fclose(fh);
488         }
489
490         return false;
491 }
492
493 MUTATOR_HOOKFUNCTION(superspec_PlayerDies)
494 {SELFPARAM();
495         entity e;
496         FOR_EACH_SPEC(e)
497         {
498                 setself(e);
499                 if(self.autospec_flags & ASF_FOLLOWKILLER && IS_PLAYER(frag_attacker) && self.enemy == this)
500                 {
501                         if(self.autospec_flags & ASF_SHOWWHAT)
502                                 superspec_msg("", "", self, sprintf("^7Following %s^7 due to followkiller\n", frag_attacker.netname), 2);
503
504                         _spectate(frag_attacker);
505                 }
506         }
507
508         setself(this);
509         return false;
510 }
511
512 MUTATOR_HOOKFUNCTION(superspec_ClientDisconnect)
513 {
514         superspec_save_client_conf();
515         return false;
516 }
517
518 MUTATOR_DEFINITION(mutator_superspec)
519 {
520
521         MUTATOR_HOOK(BuildMutatorsString, superspec_BuildMutatorsString, CBC_ORDER_ANY);
522         MUTATOR_HOOK(BuildMutatorsPrettyString, superspec_BuildMutatorsPrettyString, CBC_ORDER_ANY);
523         MUTATOR_HOOK(SV_ParseClientCommand, superspec_SV_ParseClientCommand, CBC_ORDER_ANY);
524         MUTATOR_HOOK(ItemTouch, superspec_ItemTouch, CBC_ORDER_ANY);
525         MUTATOR_HOOK(ClientConnect, superspec_ClientConnect, CBC_ORDER_ANY);
526         MUTATOR_HOOK(ClientDisconnect, superspec_ClientDisconnect, CBC_ORDER_ANY);
527         MUTATOR_HOOK(PlayerDies, superspec_PlayerDies, CBC_ORDER_ANY);
528
529         return 0;
530 }