]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/lms/sv_lms.qc
LMS: make leaders glow so they are immediately recognizable
[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, PlayerPowerups)
365 {
366         entity player = M_ARGV(0, entity);
367         if (player.waypointsprite_attachedforcarrier)
368                 player.effects |= (EF_ADDITIVE | EF_FULLBRIGHT);
369         else
370                 player.effects &= ~(EF_ADDITIVE | EF_FULLBRIGHT);
371 }
372
373 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
374 {
375         // forbode!
376         return true;
377 }
378
379 MUTATOR_HOOKFUNCTION(lms, Damage_Calculate)
380 {
381         if (!autocvar_g_lms_dynamic_vampire)
382                 return;
383
384         entity frag_attacker = M_ARGV(1, entity);
385         entity frag_target = M_ARGV(2, entity);
386         float frag_damage = M_ARGV(4, float);
387
388         if (IS_PLAYER(frag_attacker) && !IS_DEAD(frag_attacker)
389                 && IS_PLAYER(frag_target) && !IS_DEAD(frag_target) && frag_attacker != frag_target)
390         {
391                 float vampire_factor = 0;
392
393                 int frag_attacker_lives = GameRules_scoring_add(frag_attacker, LMS_LIVES, 0);
394                 int frag_target_lives = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
395                 int diff = frag_target_lives - frag_attacker_lives - autocvar_g_lms_dynamic_vampire_min_lives_diff;
396
397                 if (diff >= 0)
398                         vampire_factor = autocvar_g_lms_dynamic_vampire_factor_base + diff * autocvar_g_lms_dynamic_vampire_factor_increase;
399                 if (vampire_factor > 0)
400                 {
401                         vampire_factor = min(vampire_factor, autocvar_g_lms_dynamic_vampire_factor_max);
402                         SetResourceExplicit(frag_attacker, RES_HEALTH,
403                                 min(GetResource(frag_attacker, RES_HEALTH) + frag_damage * vampire_factor, start_health));
404                 }
405         }
406 }
407
408 bool lms_waypointsprite_visible_for_player(entity this, entity player, entity view) // runs on waypoints which are attached to ballcarriers, updates once per frame
409 {
410         if(view.lms_wp_time)
411                 if(IS_SPEC(player))
412                         return false; // we don't want spectators of leaders to see the attached waypoint on the top of their screen
413
414         float leader_time = autocvar_g_lms_leader_wp_time;
415         float leader_repeat_time = leader_time + autocvar_g_lms_leader_wp_time_repeat;
416         float wp_time = this.owner.lms_wp_time;
417         if (wp_time && (time - wp_time) % leader_repeat_time > leader_time)
418                 return false;
419
420         return true;
421 }
422
423 void lms_UpdateWaypoints()
424 {
425         int max_lives = 0;
426         int pl_cnt = 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                         max_lives = lives;
431                 pl_cnt++;
432         });
433
434         int second_max_lives = 0;
435         int pl_cnt_with_max_lives = 0;
436         FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
437                 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
438                 if (lives == max_lives)
439                         pl_cnt_with_max_lives++;
440                 else if (lives > second_max_lives)
441                         second_max_lives = lives;
442         });
443
444         int lives_diff = autocvar_g_lms_leader_wp_lives;
445         if (max_lives - second_max_lives >= lives_diff && pl_cnt_with_max_lives <= pl_cnt * autocvar_g_lms_leader_wp_max_relative)
446                 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
447                         int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
448                         if (lives == max_lives)
449                         {
450                                 if (!it.waypointsprite_attachedforcarrier)
451                                 {
452                                         WaypointSprite_AttachCarrier(WP_LmsLeader, it, RADARICON_FLAGCARRIER);
453                                         it.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = lms_waypointsprite_visible_for_player;
454                                         WaypointSprite_UpdateRule(it.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
455                                         vector pl_color = colormapPaletteColor(it.clientcolors & 0x0F, false);
456                                         WaypointSprite_UpdateTeamRadar(it.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, pl_color);
457                                         WaypointSprite_Ping(it.waypointsprite_attachedforcarrier);
458                                 }
459                                 if (!it.lms_wp_time)
460                                         it.lms_wp_time = time;
461                         }
462                         else
463                         {
464                                 if (it.waypointsprite_attachedforcarrier)
465                                         WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
466                                 it.lms_wp_time = 0;
467                         }
468                 });
469         else
470                 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
471                         if (it.waypointsprite_attachedforcarrier)
472                                 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
473                         it.lms_wp_time = 0;
474                 });
475 }
476
477 MUTATOR_HOOKFUNCTION(lms, PlayerDied)
478 {
479         if (!warmup_stage && autocvar_g_lms_leader_wp_lives > 0)
480                 lms_UpdateWaypoints();
481 }
482
483 MUTATOR_HOOKFUNCTION(lms, CalculateRespawnTime)
484 {
485         entity player = M_ARGV(0, entity);
486         player.respawn_flags |= RESPAWN_FORCE;
487
488         int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
489         if (pl_lives <= 0)
490         {
491                 player.respawn_flags = RESPAWN_SILENT;
492                 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
493                 player.respawn_time = time + 2;
494                 return true;
495         }
496
497         if (autocvar_g_lms_dynamic_respawn_delay <= 0)
498                 return false;
499
500         int max_lives = 0;
501         int pl_cnt = 0;
502         FOREACH_CLIENT(it != player && IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
503                 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
504                 if (lives > max_lives)
505                         max_lives = lives;
506                 pl_cnt++;
507         });
508
509         // min delay with only 2 players
510         if (pl_cnt == 1) // player wasn't counted
511                 max_lives = 0;
512
513         player.respawn_time = time + autocvar_g_lms_dynamic_respawn_delay_base +
514                 autocvar_g_lms_dynamic_respawn_delay_increase * max(0, max_lives - pl_lives);
515         return true;
516 }
517
518 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
519 {
520         entity frag_target = M_ARGV(1, entity);
521
522         if (!warmup_stage && time > game_starttime)
523         {
524                 // remove a life
525                 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
526                 if(tl < lms_lowest_lives)
527                         lms_lowest_lives = tl;
528                 if(tl <= 0)
529                 {
530                         int pl_cnt = 0;
531                         FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
532                                 pl_cnt++;
533                         });
534                         frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
535                         GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
536                 }
537         }
538         M_ARGV(2, float) = 0; // frag score
539
540         return true;
541 }
542
543 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
544 {
545         start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
546         start_health       = warmup_start_health       = cvar("g_lms_start_health");
547         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
548         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
549         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
550         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
551         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
552         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
553         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
554 }
555
556 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
557 {
558         // don't clear player score
559         return true;
560 }
561
562 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
563 {
564         entity definition = M_ARGV(0, entity);
565
566         if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
567         {
568                 return false;
569         }
570         return true;
571 }
572
573 void lms_extralife(entity this)
574 {
575         StartItem(this, ITEM_ExtraLife);
576 }
577
578 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
579 {
580         if (MUTATOR_RETURNVALUE) return false;
581         if (!autocvar_g_powerups) return false;
582         if (!autocvar_g_lms_extra_lives) return false;
583
584         entity ent = M_ARGV(0, entity);
585
586         // Can't use .itemdef here
587         if (ent.classname != "item_health_mega") return false;
588
589         entity e = spawn();
590         setthink(e, lms_extralife);
591
592         e.nextthink = time + 0.1;
593         e.spawnflags = ent.spawnflags;
594         e.noalign = ent.noalign;
595         setorigin(e, ent.origin);
596
597         return true;
598 }
599
600 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
601 {
602         if(MUTATOR_RETURNVALUE) return false;
603
604         entity item = M_ARGV(0, entity);
605         entity toucher = M_ARGV(1, entity);
606
607         if(item.itemdef == ITEM_ExtraLife)
608         {
609                 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
610                 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
611                 return MUT_ITEMTOUCH_PICKUP;
612         }
613
614         return MUT_ITEMTOUCH_CONTINUE;
615 }
616
617 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
618 {
619         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
620                 if (it.lmsplayer && it.lms_spectate_warning < 2)
621                         ++M_ARGV(0, int); // activerealplayers
622                 ++M_ARGV(1, int); // realplayers
623         });
624
625         return true;
626 }
627
628 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
629 {
630         entity player = M_ARGV(0, entity);
631
632         if(warmup_stage || time < game_starttime || player.lms_spectate_warning)
633         {
634                 // for the forfeit message...
635                 player.lms_spectate_warning = 2;
636         }
637         else
638         {
639                 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
640                 {
641                         player.lms_spectate_warning = 1;
642                         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");
643                 }
644                 return MUT_SPECCMD_RETURN;
645         }
646         return MUT_SPECCMD_CONTINUE;
647 }
648
649 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
650 {
651         M_ARGV(0, float) = WinningCondition_LMS();
652         return true;
653 }
654
655 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
656 {
657         if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
658                 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
659 }
660
661 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
662 {
663         entity player = M_ARGV(0, entity);
664
665         return boolean(player.lmsplayer);
666 }
667
668 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
669 {
670         if(game_stopped)
671         if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
672                 return true; // allow writing to this field in intermission as it is needed for newly joining players
673 }
674
675 void lms_Initialize()
676 {
677         lms_lowest_lives = 999;
678 }