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