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>
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;
15 int LMS_NewPlayerLives()
17 int fl = floor(autocvar_fraglimit);
18 if(fl == 0 || fl > 999)
21 // first player has left the game for dying too much? Nobody else can get in.
22 if(lms_lowest_lives < 1)
25 if(!autocvar_g_lms_join_anytime)
26 if(lms_lowest_lives < fl - max(0, floor(autocvar_g_lms_last_join)))
29 return bound(1, lms_lowest_lives, fl);
34 // LMS winning condition: game terminates if and only if there's at most one
35 // one player who's living lives. Top two scores being equal cancels the time
37 int WinningCondition_LMS()
39 if (warmup_stage || time <= game_starttime)
42 entity first_player = NULL;
44 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
54 // two or more active players - continue with the game
56 if (autocvar_g_campaign)
58 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
59 float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
61 return WINNING_YES; // human player lost, game over
68 // exactly one player?
71 SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
73 if (LMS_NewPlayerLives())
75 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
81 // and assign him his first place
82 GameRules_scoring_add(first_player, LMS_RANK, 1);
89 // nobody is playing at all...
90 if (LMS_NewPlayerLives())
92 // wait for players...
96 // SNAFU (maybe a draw game?)
98 LOG_TRACE("No players, ending game.");
103 // When we get here, we have at least two players who are actually LIVING,
104 // now check if the top two players have equal score.
105 WinningConditionHelper(NULL);
108 if(WinningConditionHelper_winner)
109 WinningConditionHelper_winner.winning = true;
110 if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
111 return WINNING_NEVER;
113 // Top two have different scores? Way to go for our beloved TIMELIMIT!
118 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
120 lms_lowest_lives = 999;
124 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
126 FOREACH_CLIENT(true, {
127 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
129 // players who forfeited (rank >= 256) become spectators
130 if (it.lms_spectate_warning == 2)
131 it.frags = FRAGS_SPECTATOR;
133 it.frags = FRAGS_PLAYER;
136 CS(it).killcount = 0;
138 it.lms_spectate_warning = 0;
139 GameRules_scoring_add(it, LMS_RANK, -GameRules_scoring_add(it, LMS_RANK, 0));
140 GameRules_scoring_add(it, LMS_LIVES, -GameRules_scoring_add(it, LMS_LIVES, 0));
142 if (it.frags != FRAGS_PLAYER)
145 TRANSMUTE(Player, it);
146 PutClientInServer(it);
150 // FIXME add support for sv_ready_restart_after_countdown
151 // that is find a way to respawn/reset players IN GAME without setting lives to 0
152 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
155 sv_ready_restart_after_countdown = 0;
158 // returns true if player is added to the game
159 bool lms_AddPlayer(entity player)
161 if (!player.lmsplayer)
163 int lives = GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
166 player.lmsplayer = 1;
168 if (warmup_stage || time <= game_starttime)
170 if(player.lms_spectate_warning)
172 player.lms_spectate_warning = 0;
173 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
174 int lives = GameRules_scoring_add(player, LMS_LIVES, 0);
176 GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
181 if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
183 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
190 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
192 entity player = M_ARGV(0, entity);
193 if (!warmup_stage && (IS_BOT_CLIENT(player) || CS(player).jointime != time))
195 if (GameRules_scoring_add(player, LMS_RANK, 0) || !lms_AddPlayer(player))
196 TRANSMUTE(Observer, player);
200 MUTATOR_HOOKFUNCTION(lms, PlayerSpawn)
202 entity player = M_ARGV(0, entity);
204 if (warmup_stage || time < game_starttime)
207 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
208 float min_health = start_health;
209 float min_armorvalue = start_armorvalue;
210 FOREACH_CLIENT(it != player && IS_PLAYER(it) && !IS_DEAD(it) && GameRules_scoring_add(it, LMS_LIVES, 0) == pl_lives, {
211 if (GetResource(it, RES_HEALTH) < min_health)
212 min_health = GetResource(it, RES_HEALTH);
213 if (GetResource(it, RES_ARMOR) < min_armorvalue)
214 min_armorvalue = GetResource(it, RES_ARMOR);
216 if (min_health != start_health)
217 SetResource(player, RES_HEALTH, max(1, min_health));
218 if (min_armorvalue != start_armorvalue)
219 SetResource(player, RES_ARMOR, min_armorvalue);
222 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
224 entity player = M_ARGV(0, entity);
226 if (warmup_stage || lms_AddPlayer(player))
232 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
234 entity frag_target = M_ARGV(2, entity);
236 float tl = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
239 frag_target.respawn_flags = RESPAWN_SILENT;
240 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
241 frag_target.respawn_time = time + 2;
243 frag_target.respawn_flags |= RESPAWN_FORCE;
246 void lms_RemovePlayer(entity player)
248 if (warmup_stage || time < game_starttime)
251 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
254 if (player.lms_spectate_warning < 2)
256 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
258 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
261 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
265 FOREACH_CLIENT(true, {
266 // update rank of other players that were eliminated
267 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
269 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
270 if (it_rank > player_rank && it_rank <= 256)
271 GameRules_scoring_add(it, LMS_RANK, -1);
273 else if (it.frags != FRAGS_SPECTATOR)
275 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
276 if(tl < lms_lowest_lives)
277 lms_lowest_lives = tl;
280 GameRules_scoring_add(player, LMS_RANK, 665 - lms_quitters); // different from 666
283 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
286 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
287 TRANSMUTE(Observer, player);
291 if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
293 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
294 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
296 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
300 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
302 entity player = M_ARGV(0, entity);
304 // no further message other than the disconnect message
305 player.lms_spectate_warning = 3;
307 lms_RemovePlayer(player);
308 player.lmsplayer = 0;
311 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
313 entity player = M_ARGV(0, entity);
315 if (!IS_PLAYER(player))
318 if (warmup_stage || time <= game_starttime)
320 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
321 player.frags = FRAGS_SPECTATOR;
322 TRANSMUTE(Observer, player);
323 player.lmsplayer = 0;
325 else if (!GameRules_scoring_add(player, LMS_RANK, 0))
326 lms_RemovePlayer(player);
327 return true; // prevent team reset
330 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
332 entity player = M_ARGV(0, entity);
333 TRANSMUTE(Observer, player);
334 player.frags = FRAGS_SPECTATOR;
335 player.lms_spectate_warning = 0;
338 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
340 entity player = M_ARGV(0, entity);
342 if(player.deadflag == DEAD_DYING)
343 player.deadflag = DEAD_RESPAWNING;
346 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
348 if(autocvar_g_lms_regenerate)
353 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
359 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
361 entity frag_target = M_ARGV(1, entity);
363 if (!warmup_stage && time > game_starttime)
366 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
367 if(tl < lms_lowest_lives)
368 lms_lowest_lives = tl;
372 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
375 frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
376 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
379 M_ARGV(2, float) = 0; // frag score
384 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
386 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
387 start_health = warmup_start_health = cvar("g_lms_start_health");
388 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
389 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
390 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
391 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
392 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
393 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
394 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
397 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
399 // don't clear player score
403 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
405 entity definition = M_ARGV(0, entity);
407 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
414 void lms_extralife(entity this)
416 StartItem(this, ITEM_ExtraLife);
419 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
421 if (MUTATOR_RETURNVALUE) return false;
422 if (!autocvar_g_powerups) return false;
423 if (!autocvar_g_lms_extra_lives) return false;
425 entity ent = M_ARGV(0, entity);
427 // Can't use .itemdef here
428 if (ent.classname != "item_health_mega") return false;
431 setthink(e, lms_extralife);
433 e.nextthink = time + 0.1;
434 e.spawnflags = ent.spawnflags;
435 e.noalign = ent.noalign;
436 setorigin(e, ent.origin);
441 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
443 if(MUTATOR_RETURNVALUE) return false;
445 entity item = M_ARGV(0, entity);
446 entity toucher = M_ARGV(1, entity);
448 if(item.itemdef == ITEM_ExtraLife)
450 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
451 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
452 return MUT_ITEMTOUCH_PICKUP;
455 return MUT_ITEMTOUCH_CONTINUE;
458 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
460 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
461 if (it.lmsplayer && it.lms_spectate_warning < 2)
462 ++M_ARGV(0, int); // activerealplayers
463 ++M_ARGV(1, int); // realplayers
469 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
471 entity player = M_ARGV(0, entity);
473 if(warmup_stage || time < game_starttime || player.lms_spectate_warning)
475 // for the forfeit message...
476 player.lms_spectate_warning = 2;
480 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
482 player.lms_spectate_warning = 1;
483 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");
485 return MUT_SPECCMD_RETURN;
487 return MUT_SPECCMD_CONTINUE;
490 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
492 M_ARGV(0, float) = WinningCondition_LMS();
496 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
498 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
499 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
502 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
504 entity player = M_ARGV(0, entity);
506 return boolean(player.lmsplayer);
509 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
512 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
513 return true; // allow writing to this field in intermission as it is needed for newly joining players
516 void lms_Initialize()
518 lms_lowest_lives = 999;