]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/lms/sv_lms.qc
Merge branch 'master' into terencehill/lms_updates
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / lms / sv_lms.qc
1 #include "sv_lms.qh"
2
3 #include <common/mutators/mutator/instagib/items.qh>
4 #include <server/campaign.qh>
5 #include <server/command/_mod.qh>
6 #include <server/world.qh>
7 #include <server/items/items.qh>
8
9 int autocvar_g_lms_extra_lives;
10 bool autocvar_g_lms_join_anytime;
11 int autocvar_g_lms_last_join;
12 bool autocvar_g_lms_regenerate;
13 int autocvar_g_lms_leader_wp_lives;
14 float autocvar_g_lms_leader_wp_max_relative;
15 float autocvar_g_lms_leader_wp_time;
16 float autocvar_g_lms_leader_wp_time_repeat;
17 float autocvar_g_lms_dynamic_respawn_delay;
18 float autocvar_g_lms_dynamic_respawn_delay_base;
19 float autocvar_g_lms_dynamic_respawn_delay_increase;
20 bool autocvar_g_lms_dynamic_vampire;
21 float autocvar_g_lms_dynamic_vampire_factor_base;
22 float autocvar_g_lms_dynamic_vampire_factor_increase;
23 float autocvar_g_lms_dynamic_vampire_factor_max;
24 int autocvar_g_lms_dynamic_vampire_min_lives_diff;
25
26 .float lms_wp_time;
27
28 // main functions
29 int LMS_NewPlayerLives()
30 {
31         int fl = floor(autocvar_fraglimit);
32         if(fl == 0 || fl > 999)
33                 fl = 999;
34
35         // first player has left the game for dying too much? Nobody else can get in.
36         if(lms_lowest_lives < 1)
37                 return 0;
38
39         if(!autocvar_g_lms_join_anytime)
40                 if(lms_lowest_lives < fl - max(0, floor(autocvar_g_lms_last_join)))
41                         return 0;
42
43         return bound(1, lms_lowest_lives, fl);
44 }
45
46 void ClearWinners();
47
48 // LMS winning condition: game terminates if and only if there's at most one
49 // one player who's living lives. Top two scores being equal cancels the time
50 // limit.
51 int WinningCondition_LMS()
52 {
53         if (warmup_stage || time <= game_starttime)
54                 return WINNING_NO;
55
56         entity first_player = NULL;
57         int totalplayers = 0;
58         FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
59                 if (!totalplayers)
60                         first_player = it;
61                 ++totalplayers;
62         });
63
64         if (totalplayers)
65         {
66                 if (totalplayers > 1)
67                 {
68                         // two or more active players - continue with the game
69
70                         if (autocvar_g_campaign)
71                         {
72                                 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
73                                         float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
74                                         if (!pl_lives)
75                                                 return WINNING_YES; // human player lost, game over
76                                         break;
77                                 });
78                         }
79                 }
80                 else
81                 {
82                         // exactly one player?
83
84                         ClearWinners();
85                         SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
86
87                         if (LMS_NewPlayerLives())
88                         {
89                                 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
90                                 return WINNING_NO;
91                         }
92                         else
93                         {
94                                 // a winner!
95                                 // and assign him his first place
96                                 GameRules_scoring_add(first_player, LMS_RANK, 1);
97                                 return WINNING_YES;
98                         }
99                 }
100         }
101         else
102         {
103                 // nobody is playing at all...
104                 if (LMS_NewPlayerLives())
105                 {
106                         // wait for players...
107                 }
108                 else
109                 {
110                         // SNAFU (maybe a draw game?)
111                         ClearWinners();
112                         LOG_TRACE("No players, ending game.");
113                         return WINNING_YES;
114                 }
115         }
116
117         // When we get here, we have at least two players who are actually LIVING,
118         // now check if the top two players have equal score.
119         WinningConditionHelper(NULL);
120
121         ClearWinners();
122         if(WinningConditionHelper_winner)
123                 WinningConditionHelper_winner.winning = true;
124         if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
125                 return WINNING_NEVER;
126
127         // Top two have different scores? Way to go for our beloved TIMELIMIT!
128         return WINNING_NO;
129 }
130
131 // mutator hooks
132 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
133 {
134         lms_lowest_lives = 999;
135 }
136
137 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
138 {
139         FOREACH_CLIENT(true, {
140                 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
141                 {
142                         // players who forfeited (rank >= 256) become spectators
143                         if (it.lms_spectate_warning == 2)
144                                 it.frags = FRAGS_SPECTATOR;
145                         else
146                                 it.frags = FRAGS_PLAYER;
147                 }
148
149                 CS(it).killcount = 0;
150                 it.lmsplayer = 0;
151                 it.lms_spectate_warning = 0;
152                 GameRules_scoring_add(it, LMS_RANK, -GameRules_scoring_add(it, LMS_RANK, 0));
153                 GameRules_scoring_add(it, LMS_LIVES, -GameRules_scoring_add(it, LMS_LIVES, 0));
154
155                 if (it.frags != FRAGS_PLAYER)
156                         continue;
157
158                 TRANSMUTE(Player, it);
159                 PutClientInServer(it);
160                 if (it.waypointsprite_attachedforcarrier)
161                         WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
162         });
163 }
164
165 // FIXME add support for sv_ready_restart_after_countdown
166 // that is find a way to respawn/reset players IN GAME without setting lives to 0
167 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
168 {
169         // incompatible
170         sv_ready_restart_after_countdown = 0;
171 }
172
173 // returns true if player is added to the game
174 bool lms_AddPlayer(entity player)
175 {
176         if (!player.lmsplayer)
177         {
178                 int lives = GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
179                 if(lives <= 0)
180                         return false;
181                 player.lmsplayer = 2; // temp value indicating player has just joined the game (but not spawned yet)
182         }
183         if (warmup_stage || time <= game_starttime)
184         {
185                 if(player.lms_spectate_warning)
186                 {
187                         player.lms_spectate_warning = 0;
188                         GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
189                         int lives = GameRules_scoring_add(player, LMS_LIVES, 0);
190                         if(lives <= 0)
191                                 GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
192                 }
193         }
194         else
195         {
196                 if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
197                 {
198                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
199                         return false;
200                 }
201         }
202         return true;
203 }
204
205 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
206 {
207         entity player = M_ARGV(0, entity);
208         if (!warmup_stage && (IS_BOT_CLIENT(player) || CS(player).jointime != time))
209         {
210                 if (GameRules_scoring_add(player, LMS_RANK, 0) || !lms_AddPlayer(player))
211                         TRANSMUTE(Observer, player);
212         }
213 }
214
215 MUTATOR_HOOKFUNCTION(lms, PlayerSpawn)
216 {
217         entity player = M_ARGV(0, entity);
218
219         if (warmup_stage || time < game_starttime)
220                 return true;
221
222         if (player.lmsplayer == 2) // just joined the game
223         {
224                 // spawn player with the same amount of health / armor
225                 // as the least healthy player with the least number of lives
226                 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
227                 float min_health = start_health;
228                 float min_armorvalue = start_armorvalue;
229                 FOREACH_CLIENT(it != player && IS_PLAYER(it) && !IS_DEAD(it) && GameRules_scoring_add(it, LMS_LIVES, 0) == pl_lives, {
230                         if (GetResource(it, RES_HEALTH) < min_health)
231                                 min_health = GetResource(it, RES_HEALTH);
232                         if (GetResource(it, RES_ARMOR) < min_armorvalue)
233                                 min_armorvalue = GetResource(it, RES_ARMOR);
234                 });
235                 if (min_health != start_health)
236                         SetResource(player, RES_HEALTH, max(1, min_health));
237                 if (min_armorvalue != start_armorvalue)
238                         SetResource(player, RES_ARMOR, min_armorvalue);
239                 player.lmsplayer = 1;
240         }
241 }
242
243 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
244 {
245         entity player = M_ARGV(0, entity);
246
247         if (warmup_stage || lms_AddPlayer(player))
248                 return false;
249
250         return true;
251 }
252
253 void lms_RemovePlayer(entity player)
254 {
255         if (warmup_stage || time < game_starttime)
256                 return;
257
258         float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
259         if (!player_rank)
260         {
261                 if (player.lms_spectate_warning < 2)
262                 {
263                         player.frags = FRAGS_PLAYER_OUT_OF_GAME;
264                         int pl_cnt = 0;
265                         FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
266                                 pl_cnt++;
267                         });
268                         GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
269                 }
270                 else
271                 {
272                         int min_forfeiter_rank = 665; // different from 666
273                         FOREACH_CLIENT(true, {
274                                 // update rank of other players that were eliminated
275                                 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
276                                 {
277                                         float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
278                                         if (it_rank > player_rank && it_rank <= 256)
279                                                 GameRules_scoring_add(it, LMS_RANK, -1);
280                                         if (it_rank > 256 && it_rank <= min_forfeiter_rank)
281                                                 min_forfeiter_rank = it_rank - 1;
282                                 }
283                                 else if (it.frags != FRAGS_SPECTATOR)
284                                 {
285                                         float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
286                                         if(tl < lms_lowest_lives)
287                                                 lms_lowest_lives = tl;
288                                 }
289                         });
290                         GameRules_scoring_add(player, LMS_RANK, min_forfeiter_rank);
291                         if(!warmup_stage)
292                                 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
293                         player.frags = FRAGS_PLAYER_OUT_OF_GAME;
294                         TRANSMUTE(Observer, player);
295                 }
296         }
297
298         if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
299         {
300                 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
301                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
302                 else
303                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
304         }
305 }
306
307 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
308 {
309         entity player = M_ARGV(0, entity);
310
311         // no further message other than the disconnect message
312         player.lms_spectate_warning = 3;
313
314         lms_RemovePlayer(player);
315         player.lmsplayer = 0;
316 }
317
318 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
319 {
320         entity player = M_ARGV(0, entity);
321         bool is_forced = M_ARGV(1, bool);
322
323         if (!IS_PLAYER(player))
324                 return true;
325
326         if (warmup_stage || time <= game_starttime)
327         {
328                 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
329                 player.frags = FRAGS_SPECTATOR;
330                 TRANSMUTE(Observer, player);
331                 player.lmsplayer = 0;
332         }
333         else
334         {
335                 if (is_forced)
336                         player.lms_spectate_warning = 2;
337                 if (!GameRules_scoring_add(player, LMS_RANK, 0))
338                         lms_RemovePlayer(player);
339         }
340         return true;  // prevent team reset
341 }
342
343 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
344 {
345         entity player = M_ARGV(0, entity);
346         player.frags = FRAGS_SPECTATOR;
347 }
348
349 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
350 {
351         entity player = M_ARGV(0, entity);
352
353         if(player.deadflag == DEAD_DYING)
354                 player.deadflag = DEAD_RESPAWNING;
355 }
356
357 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
358 {
359         if(autocvar_g_lms_regenerate)
360                 return false;
361         return true;
362 }
363
364 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
365 {
366         // forbode!
367         return true;
368 }
369
370 MUTATOR_HOOKFUNCTION(lms, Damage_Calculate)
371 {
372         if (!autocvar_g_lms_dynamic_vampire)
373                 return;
374
375         entity frag_attacker = M_ARGV(1, entity);
376         entity frag_target = M_ARGV(2, entity);
377         float frag_damage = M_ARGV(4, float);
378
379         if (IS_PLAYER(frag_attacker) && !IS_DEAD(frag_attacker)
380                 && IS_PLAYER(frag_target) && !IS_DEAD(frag_target) && frag_attacker != frag_target)
381         {
382                 float vampire_factor = 0;
383
384                 int frag_attacker_lives = GameRules_scoring_add(frag_attacker, LMS_LIVES, 0);
385                 int frag_target_lives = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
386                 int diff = frag_target_lives - frag_attacker_lives - autocvar_g_lms_dynamic_vampire_min_lives_diff;
387
388                 if (diff >= 0)
389                         vampire_factor = autocvar_g_lms_dynamic_vampire_factor_base + diff * autocvar_g_lms_dynamic_vampire_factor_increase;
390                 if (vampire_factor > 0)
391                 {
392                         vampire_factor = min(vampire_factor, autocvar_g_lms_dynamic_vampire_factor_max);
393                         SetResourceExplicit(frag_attacker, RES_HEALTH,
394                                 min(GetResource(frag_attacker, RES_HEALTH) + frag_damage * vampire_factor, start_health));
395                 }
396         }
397 }
398
399 bool lms_waypointsprite_visible_for_player(entity this, entity player, entity view) // runs on waypoints which are attached to ballcarriers, updates once per frame
400 {
401         if(view.lms_wp_time)
402                 if(IS_SPEC(player))
403                         return false; // we don't want spectators of leaders to see the attached waypoint on the top of their screen
404
405         float leader_time = autocvar_g_lms_leader_wp_time;
406         float leader_repeat_time = leader_time + autocvar_g_lms_leader_wp_time_repeat;
407         float wp_time = this.owner.lms_wp_time;
408         if (wp_time && (time - wp_time) % leader_repeat_time > leader_time)
409                 return false;
410
411         return true;
412 }
413
414 void lms_UpdateWaypoints()
415 {
416         int max_lives = 0;
417         int pl_cnt = 0;
418         FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
419                 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
420                 if (lives > max_lives)
421                         max_lives = lives;
422                 pl_cnt++;
423         });
424
425         int second_max_lives = 0;
426         int pl_cnt_with_max_lives = 0;
427         FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
428                 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
429                 if (lives == max_lives)
430                         pl_cnt_with_max_lives++;
431                 else if (lives > second_max_lives)
432                         second_max_lives = lives;
433         });
434
435         int lives_diff = autocvar_g_lms_leader_wp_lives;
436         if (max_lives - second_max_lives >= lives_diff && pl_cnt_with_max_lives <= pl_cnt * autocvar_g_lms_leader_wp_max_relative)
437                 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
438                         int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
439                         if (lives == max_lives)
440                         {
441                                 if (!it.waypointsprite_attachedforcarrier)
442                                 {
443                                         WaypointSprite_AttachCarrier(WP_LmsLeader, it, RADARICON_FLAGCARRIER);
444                                         it.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = lms_waypointsprite_visible_for_player;
445                                         WaypointSprite_UpdateRule(it.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
446                                         vector pl_color = colormapPaletteColor(it.clientcolors & 0x0F, false);
447                                         WaypointSprite_UpdateTeamRadar(it.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, pl_color);
448                                         WaypointSprite_Ping(it.waypointsprite_attachedforcarrier);
449                                 }
450                                 if (!it.lms_wp_time)
451                                         it.lms_wp_time = time;
452                         }
453                         else
454                         {
455                                 if (it.waypointsprite_attachedforcarrier)
456                                         WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
457                                 it.lms_wp_time = 0;
458                         }
459                 });
460         else
461                 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
462                         if (it.waypointsprite_attachedforcarrier)
463                                 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
464                         it.lms_wp_time = 0;
465                 });
466 }
467
468 MUTATOR_HOOKFUNCTION(lms, PlayerDied)
469 {
470         if (!warmup_stage && autocvar_g_lms_leader_wp_lives > 0)
471                 lms_UpdateWaypoints();
472 }
473
474 MUTATOR_HOOKFUNCTION(lms, CalculateRespawnTime)
475 {
476         entity player = M_ARGV(0, entity);
477         player.respawn_flags |= RESPAWN_FORCE;
478
479         int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
480         if (pl_lives <= 0)
481         {
482                 player.respawn_flags = RESPAWN_SILENT;
483                 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
484                 player.respawn_time = time + 2;
485                 return true;
486         }
487
488         if (autocvar_g_lms_dynamic_respawn_delay <= 0)
489                 return false;
490
491         int max_lives = 0;
492         int pl_cnt = 0;
493         FOREACH_CLIENT(it != player && IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
494                 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
495                 if (lives > max_lives)
496                         max_lives = lives;
497                 pl_cnt++;
498         });
499
500         // min delay with only 2 players
501         if (pl_cnt == 1) // player wasn't counted
502                 max_lives = 0;
503
504         player.respawn_time = time + autocvar_g_lms_dynamic_respawn_delay_base +
505                 autocvar_g_lms_dynamic_respawn_delay_increase * max(0, max_lives - pl_lives);
506         return true;
507 }
508
509 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
510 {
511         entity frag_target = M_ARGV(1, entity);
512
513         if (!warmup_stage && time > game_starttime)
514         {
515                 // remove a life
516                 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
517                 if(tl < lms_lowest_lives)
518                         lms_lowest_lives = tl;
519                 if(tl <= 0)
520                 {
521                         int pl_cnt = 0;
522                         FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
523                                 pl_cnt++;
524                         });
525                         frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
526                         GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
527                 }
528         }
529         M_ARGV(2, float) = 0; // frag score
530
531         return true;
532 }
533
534 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
535 {
536         start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
537         start_health       = warmup_start_health       = cvar("g_lms_start_health");
538         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
539         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
540         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
541         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
542         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
543         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
544         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
545 }
546
547 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
548 {
549         // don't clear player score
550         return true;
551 }
552
553 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
554 {
555         entity definition = M_ARGV(0, entity);
556
557         if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
558         {
559                 return false;
560         }
561         return true;
562 }
563
564 void lms_extralife(entity this)
565 {
566         StartItem(this, ITEM_ExtraLife);
567 }
568
569 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
570 {
571         if (MUTATOR_RETURNVALUE) return false;
572         if (!autocvar_g_powerups) return false;
573         if (!autocvar_g_lms_extra_lives) return false;
574
575         entity ent = M_ARGV(0, entity);
576
577         // Can't use .itemdef here
578         if (ent.classname != "item_health_mega") return false;
579
580         entity e = spawn();
581         setthink(e, lms_extralife);
582
583         e.nextthink = time + 0.1;
584         e.spawnflags = ent.spawnflags;
585         e.noalign = ent.noalign;
586         setorigin(e, ent.origin);
587
588         return true;
589 }
590
591 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
592 {
593         if(MUTATOR_RETURNVALUE) return false;
594
595         entity item = M_ARGV(0, entity);
596         entity toucher = M_ARGV(1, entity);
597
598         if(item.itemdef == ITEM_ExtraLife)
599         {
600                 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
601                 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
602                 return MUT_ITEMTOUCH_PICKUP;
603         }
604
605         return MUT_ITEMTOUCH_CONTINUE;
606 }
607
608 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
609 {
610         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
611                 if (it.lmsplayer && it.lms_spectate_warning < 2)
612                         ++M_ARGV(0, int); // activerealplayers
613                 ++M_ARGV(1, int); // realplayers
614         });
615
616         return true;
617 }
618
619 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
620 {
621         entity player = M_ARGV(0, entity);
622
623         if(warmup_stage || time < game_starttime || player.lms_spectate_warning)
624         {
625                 // for the forfeit message...
626                 player.lms_spectate_warning = 2;
627         }
628         else
629         {
630                 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
631                 {
632                         player.lms_spectate_warning = 1;
633                         sprint(player, "WARNING: you won't be able to enter the game again after spectating in LMS. Use the same command again to spectate anyway.\n");
634                 }
635                 return MUT_SPECCMD_RETURN;
636         }
637         return MUT_SPECCMD_CONTINUE;
638 }
639
640 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
641 {
642         M_ARGV(0, float) = WinningCondition_LMS();
643         return true;
644 }
645
646 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
647 {
648         if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
649                 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
650 }
651
652 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
653 {
654         entity player = M_ARGV(0, entity);
655
656         return boolean(player.lmsplayer);
657 }
658
659 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
660 {
661         if(game_stopped)
662         if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
663                 return true; // allow writing to this field in intermission as it is needed for newly joining players
664 }
665
666 void lms_Initialize()
667 {
668         lms_lowest_lives = 999;
669 }