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