Fix extra life powerup giving infinite lives in lms & minstagib
[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_ALL             0xFFFFFF
13 .float autospec_flags;
14
15 #define SSF_SILENT          1
16 #define SSF_VERBOSE         2
17 #define SSF_ITEMMSG         4
18 .float superspec_flags;
19
20 .string superspec_itemfilter; //"classname1 classname2 ..."
21
22 float _spectate(entity _player)
23 {
24         if(Spectate(_player) == 1)
25         {
26                 PutObserverInServer();
27                 self.classname = "spectator";
28         }
29
30         return TRUE;
31 }
32
33 void superspec_save_client_conf()
34 {
35         string fn = "superspec-local.options";
36         float fh;
37
38
39         if not(_ISLOCAL)
40         {
41                 if(self.crypto_idfp == "")
42                         return;
43
44                 fn = sprintf("superspec-%s.options", uri_escape(self.crypto_idfp));
45         }
46
47         fh = fopen(fn, FILE_WRITE);
48         if(fh < 0)
49         {
50                 dprint("^1ERROR: ^7 superspec can not open ", fn, " for writing.\n");
51         }
52         else
53         {
54                 fputs(fh, _SSMAGIX);
55                 fputs(fh, "\n");
56                 fputs(fh, ftos(self.autospec_flags));
57                 fputs(fh, "\n");
58                 fputs(fh, ftos(self.superspec_flags));
59                 fputs(fh, "\n");
60                 fputs(fh, self.superspec_itemfilter);
61                 fputs(fh, "\n");
62                 fclose(fh);
63         }
64 }
65
66 void superspec_msg(string _center_title, string _con_title, entity _to, string _msg, float _spamlevel)
67 {
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 not(_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 FALSE;
121                                 }
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.classname == "item_armor_large") ||
128                                 (self.autospec_flags& ASF_MEGA_HP && _item.classname == "item_health_mega") ||
129                                 (self.autospec_flags& ASF_FLAG_GRAB && _item.classname == "item_flag_team"))
130                 {
131
132                         if((self.enemy != other) || self.classname == "observer")
133                         {
134                                 if(self.autospec_flags & ASF_OBSERVER_ONLY && self.classname != "observer")
135                                 {
136                                         if(self.superspec_flags & SSF_VERBOSE)
137                                                 superspec_msg("", "", self, sprintf("^8Ignored that %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         self = _oldself;
151
152         return FALSE;
153 }
154
155 MUTATOR_HOOKFUNCTION(superspec_SV_ParseClientCommand)
156 {
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(self.classname == "player")
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
211                 return TRUE;
212         }
213
214         if(cmd_name == "superspec")
215         {
216                 string _aspeco;
217
218                 if(cmd_argc > 1)
219                 {
220                         float i, _bits = 0, _start = 1;
221                         if(argv(1) == "help")
222                         {
223                                 _aspeco = "use cmd superspec [option] [on|off] to set options\n\n";
224                                 _aspeco = strcat(_aspeco, "^3 silent ^7(short^5 si^7) supress ALL mesagess from superspectate.\n");
225                                 _aspeco = strcat(_aspeco, "^3 verrbose ^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 was 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", self, _aspeco, 1);
229                                 return TRUE;
230                         }
231
232                         if(argv(1) == "clear")
233                         {
234                                 self.superspec_flags = 0;
235                                 _start = 2;
236                         }
237
238                         for(i = _start; i < cmd_argc; ++i)
239                         {
240                                 if(argv(i) == "on" || argv(i) == "1")
241                                 {
242                                         self.superspec_flags |= _bits;
243                                         _bits = 0;
244                                 }
245                                 else if(argv(i) == "off" || argv(i) == "0")
246                                 {
247                                         if(_start == 1)
248                                                 self.superspec_flags &~= _bits;
249
250                                         _bits = 0;
251                                 }
252                                 else
253                                 {
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;
257                                 }
258                         }
259                 }
260
261
262                 _aspeco = "";
263                 OPTIONINFO(self.superspec_flags, _aspeco, SSF_SILENT, "Silent", "silent", "si");
264                 OPTIONINFO(self.superspec_flags, _aspeco, SSF_VERBOSE, "Verbose", "verbose", "ve");
265                 OPTIONINFO(self.superspec_flags, _aspeco, SSF_ITEMMSG, "Item pickup messages", "item_message", "im");
266
267                 superspec_msg("^3Current Super Spectate options are:\n\n\n\n\n", "\n^3Current Super Spectate options are:\n", self, _aspeco, 1);
268                 return TRUE;
269
270         }
271
272 /////////////////////
273
274         if(cmd_name == "autospec")
275         {
276                 string _aspeco;
277                 if(cmd_argc > 1)
278                 {
279                         if(argv(1) == "help")
280                         {
281                                 _aspeco = "use cmd autospec [option] [on|off] to set options\n\n";
282                                 _aspeco = strcat(_aspeco, "^3 strength ^7(short^5 st^7) for automatic spectate on strength powerup\n");
283                                 _aspeco = strcat(_aspeco, "^3 shield ^7(short^5 sh^7) for automatic spectate on shield powerup\n");
284                                 _aspeco = strcat(_aspeco, "^3 mega_health ^7(short^5 mh^7) for automatic spectate on mega health\n");
285                                 _aspeco = strcat(_aspeco, "^3 mega_armor ^7(short^5 ma^7) for automatic spectate on mega armor\n");
286                                 _aspeco = strcat(_aspeco, "^3 flag_grab ^7(short^5 fg^7) for automatic spectate on CTF flag grab\n");
287                                 _aspeco = strcat(_aspeco, "^3 observer_only (short^5 oo^7) for automatic spectate only if in observer mode\n");
288                                 _aspeco = strcat(_aspeco, "^3 show_what (short^5 sw^7) to display what event triggerd autospectate\n");
289                                 _aspeco = strcat(_aspeco, "^3 item_msg ^7(short^5 im^7) to autospec when item_message in superspectate is triggerd\n");
290                                 _aspeco = strcat(_aspeco, "^3 all ^7(short ^5aa^7) turn everything on/off\n");
291                                 superspec_msg("^2Available Auto Spectate ^3options:\n\n\n", "\n^2Available Auto Spectate ^3options:\n", self, _aspeco, 1);
292                                 return TRUE;
293                         }
294
295                         float i, _bits = 0, _start = 1;
296                         if(argv(1) == "clear")
297                         {
298                                 self.autospec_flags = 0;
299                                 _start = 2;
300                         }
301
302                         for(i = _start; i < cmd_argc; ++i)
303                         {
304                                 if(argv(i) == "on" || argv(i) == "1")
305                                 {
306                                         self.autospec_flags |= _bits;
307                                         _bits = 0;
308                                 }
309                                 else if(argv(i) == "off" || argv(i) == "0")
310                                 {
311                                         if(_start == 1)
312                                                 self.autospec_flags &~= _bits;
313
314                                         _bits = 0;
315                                 }
316                                 else
317                                 {
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) == "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
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 powerups\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 == "followstshield")
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
392                 if(cmd_argc == 2)
393                 {
394                         if(argv(1) == "red")
395                                 _team = NUM_TEAM_1;
396                         else
397                                 _team = NUM_TEAM_2;
398                 }
399
400                 FOR_EACH_PLAYER(_player)
401                 {
402                         if(_player.flagcarried && (_player.team == _team || _team == 0))
403                                 return _spectate(_player);
404                 }
405
406                 superspec_msg("", "", self, "No active flag carrier\n", 1);
407                 return TRUE;
408         }
409
410         return FALSE;
411 #undef OPTIONINFO
412 }
413
414 MUTATOR_HOOKFUNCTION(superspec_BuildMutatorsString)
415 {
416         ret_string = strcat(ret_string, ":SS");
417         return 0;
418 }
419
420 MUTATOR_HOOKFUNCTION(superspec_BuildMutatorsPrettyString)
421 {
422         ret_string = strcat(ret_string, ", Super Spectators");
423         return 0;
424 }
425
426 /*
427 MUTATOR_HOOKFUNCTION(superspec_PlayerSpawn)
428 {
429
430     return FALSE;
431 }
432 */
433
434 void superspec_hello()
435 {
436         if(self.enemy.crypto_idfp == "")
437                 centerprint(self.enemy, "Your client have/allow no crypto id, superspec options will not be saved/restored.");
438         else
439                 centerprint(self.enemy, sprintf("Hello %s\nSince your client has a Crypto ID, your superspec preferenses will be presisted on this server.", self.enemy.netname));
440
441         remove(self);
442 }
443
444 MUTATOR_HOOKFUNCTION(superspec_ClientConnect)
445 {
446         if(clienttype(self) != CLIENTTYPE_REAL)
447                 return FALSE;
448
449         string fn = "superspec-local.options";
450         float fh;
451
452         self.superspec_flags = SSF_VERBOSE;
453         self.superspec_itemfilter = "";
454
455         entity _hello = spawn();
456         _hello.enemy = self;
457         _hello.think = superspec_hello;
458         _hello.nextthink = time + 5;
459
460         if not(_ISLOCAL)
461         {
462                 if(self.crypto_idfp == "")
463                         return FALSE;
464
465                 fn = sprintf("superspec-%s.options", uri_escape(self.crypto_idfp));
466         }
467
468         fh = fopen(fn, FILE_READ);
469         if(fh < 0)
470         {
471                 dprint("^1ERROR: ^7 superspec can not open ", fn, " for reading.\n");
472         }
473         else
474         {
475                 string _magic = fgets(fh);
476                 if(_magic != _SSMAGIX)
477                 {
478                         dprint("^1ERROR^7 While reading superspec options file: unkown magic\n");
479                 }
480                 else
481                 {
482                         self.autospec_flags = stof(fgets(fh));
483                         self.superspec_flags = stof(fgets(fh));
484                         self.superspec_itemfilter = strzone(fgets(fh));
485                 }
486                 fclose(fh);
487         }
488
489         return FALSE;
490 }
491
492 MUTATOR_HOOKFUNCTION(superspec_ClientDisconnect)
493 {
494         superspec_save_client_conf();
495         return FALSE;
496 }
497
498
499 /*
500 MUTATOR_HOOKFUNCTION(superspec_MakePlayerObserver)
501 {
502     return FALSE;
503 }
504
505 MUTATOR_HOOKFUNCTION(superspec_PlayerPreThink)
506 {
507     return FALSE;
508 }
509 */
510
511 MUTATOR_DEFINITION(mutator_superspec)
512 {
513
514         MUTATOR_HOOK(BuildMutatorsString, superspec_BuildMutatorsString, CBC_ORDER_ANY);
515         MUTATOR_HOOK(BuildMutatorsPrettyString, superspec_BuildMutatorsPrettyString, CBC_ORDER_ANY);
516         MUTATOR_HOOK(SV_ParseClientCommand, superspec_SV_ParseClientCommand, CBC_ORDER_ANY);
517         MUTATOR_HOOK(ItemTouch, superspec_ItemTouch, CBC_ORDER_ANY);
518         MUTATOR_HOOK(ClientConnect, superspec_ClientConnect, CBC_ORDER_ANY);
519         //MUTATOR_HOOK(PlayerSpawn, superspec_PlayerSpawn, CBC_ORDER_ANY);
520         //MUTATOR_HOOK(PlayerPreThink, superspec_PlayerPreThink, CBC_ORDER_ANY);
521         //MUTATOR_HOOK(MakePlayerObserver, superspec_MakePlayerObserver, CBC_ORDER_ANY);
522         MUTATOR_HOOK(ClientDisconnect, superspec_ClientDisconnect, CBC_ORDER_ANY);
523
524         return 0;
525 }