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;
123 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
125 FOREACH_CLIENT(true, {
126 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
128 // players who forfeited (rank >= 256) become spectators
129 if (it.lms_spectate_warning == 2)
130 it.frags = FRAGS_SPECTATOR;
132 it.frags = FRAGS_PLAYER;
135 CS(it).killcount = 0;
137 it.lms_spectate_warning = 0;
138 GameRules_scoring_add(it, LMS_RANK, -GameRules_scoring_add(it, LMS_RANK, 0));
139 GameRules_scoring_add(it, LMS_LIVES, -GameRules_scoring_add(it, LMS_LIVES, 0));
141 if (it.frags != FRAGS_PLAYER)
144 TRANSMUTE(Player, it);
145 PutClientInServer(it);
149 // FIXME add support for sv_ready_restart_after_countdown
150 // that is find a way to respawn/reset players IN GAME without setting lives to 0
151 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
154 sv_ready_restart_after_countdown = 0;
157 // returns true if player is added to the game
158 bool lms_AddPlayer(entity player)
160 if (!player.lmsplayer)
162 int lives = GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
165 player.lmsplayer = 1;
167 if (warmup_stage || time <= game_starttime)
169 if(player.lms_spectate_warning)
171 player.lms_spectate_warning = 0;
172 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
173 int lives = GameRules_scoring_add(player, LMS_LIVES, 0);
175 GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
180 if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
182 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
189 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
191 entity player = M_ARGV(0, entity);
192 if (!warmup_stage && (IS_BOT_CLIENT(player) || CS(player).jointime != time))
194 if (GameRules_scoring_add(player, LMS_RANK, 0) || !lms_AddPlayer(player))
195 TRANSMUTE(Observer, player);
199 MUTATOR_HOOKFUNCTION(lms, PlayerSpawn)
201 entity player = M_ARGV(0, entity);
203 if (warmup_stage || time < game_starttime)
206 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
207 float min_health = start_health;
208 float min_armorvalue = start_armorvalue;
209 FOREACH_CLIENT(it != player && IS_PLAYER(it) && !IS_DEAD(it) && GameRules_scoring_add(it, LMS_LIVES, 0) == pl_lives, {
210 if (GetResource(it, RES_HEALTH) < min_health)
211 min_health = GetResource(it, RES_HEALTH);
212 if (GetResource(it, RES_ARMOR) < min_armorvalue)
213 min_armorvalue = GetResource(it, RES_ARMOR);
215 if (min_health != start_health)
216 SetResource(player, RES_HEALTH, max(1, min_health));
217 if (min_armorvalue != start_armorvalue)
218 SetResource(player, RES_ARMOR, min_armorvalue);
221 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
223 entity player = M_ARGV(0, entity);
225 if (warmup_stage || lms_AddPlayer(player))
231 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
233 entity frag_target = M_ARGV(2, entity);
235 float tl = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
238 frag_target.respawn_flags = RESPAWN_SILENT;
239 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
240 frag_target.respawn_time = time + 2;
242 frag_target.respawn_flags |= RESPAWN_FORCE;
245 void lms_RemovePlayer(entity player)
247 if (warmup_stage || time < game_starttime)
250 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
253 if (player.lms_spectate_warning < 2)
255 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
257 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
260 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
264 int min_forfeiter_rank = 665; // different from 666
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);
272 if (it_rank > 256 && it_rank <= min_forfeiter_rank)
273 min_forfeiter_rank = it_rank - 1;
275 else if (it.frags != FRAGS_SPECTATOR)
277 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
278 if(tl < lms_lowest_lives)
279 lms_lowest_lives = tl;
282 GameRules_scoring_add(player, LMS_RANK, min_forfeiter_rank);
284 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
285 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
286 TRANSMUTE(Observer, player);
290 if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
292 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
293 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
295 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
299 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
301 entity player = M_ARGV(0, entity);
303 // no further message other than the disconnect message
304 player.lms_spectate_warning = 3;
306 lms_RemovePlayer(player);
307 player.lmsplayer = 0;
310 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
312 entity player = M_ARGV(0, entity);
313 bool is_forced = M_ARGV(1, bool);
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;
328 player.lms_spectate_warning = 2;
329 if (!GameRules_scoring_add(player, LMS_RANK, 0))
330 lms_RemovePlayer(player);
332 return true; // prevent team reset
335 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
337 entity player = M_ARGV(0, entity);
338 TRANSMUTE(Observer, player);
339 player.frags = FRAGS_SPECTATOR;
340 player.lms_spectate_warning = 0;
343 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
345 entity player = M_ARGV(0, entity);
347 if(player.deadflag == DEAD_DYING)
348 player.deadflag = DEAD_RESPAWNING;
351 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
353 if(autocvar_g_lms_regenerate)
358 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
364 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
366 entity frag_target = M_ARGV(1, entity);
368 if (!warmup_stage && time > game_starttime)
371 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
372 if(tl < lms_lowest_lives)
373 lms_lowest_lives = tl;
377 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
380 frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
381 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
384 M_ARGV(2, float) = 0; // frag score
389 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
391 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
392 start_health = warmup_start_health = cvar("g_lms_start_health");
393 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
394 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
395 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
396 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
397 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
398 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
399 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
402 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
404 // don't clear player score
408 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
410 entity definition = M_ARGV(0, entity);
412 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
419 void lms_extralife(entity this)
421 StartItem(this, ITEM_ExtraLife);
424 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
426 if (MUTATOR_RETURNVALUE) return false;
427 if (!autocvar_g_powerups) return false;
428 if (!autocvar_g_lms_extra_lives) return false;
430 entity ent = M_ARGV(0, entity);
432 // Can't use .itemdef here
433 if (ent.classname != "item_health_mega") return false;
436 setthink(e, lms_extralife);
438 e.nextthink = time + 0.1;
439 e.spawnflags = ent.spawnflags;
440 e.noalign = ent.noalign;
441 setorigin(e, ent.origin);
446 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
448 if(MUTATOR_RETURNVALUE) return false;
450 entity item = M_ARGV(0, entity);
451 entity toucher = M_ARGV(1, entity);
453 if(item.itemdef == ITEM_ExtraLife)
455 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
456 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
457 return MUT_ITEMTOUCH_PICKUP;
460 return MUT_ITEMTOUCH_CONTINUE;
463 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
465 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
466 if (it.lmsplayer && it.lms_spectate_warning < 2)
467 ++M_ARGV(0, int); // activerealplayers
468 ++M_ARGV(1, int); // realplayers
474 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
476 entity player = M_ARGV(0, entity);
478 if(warmup_stage || time < game_starttime || player.lms_spectate_warning)
480 // for the forfeit message...
481 player.lms_spectate_warning = 2;
485 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
487 player.lms_spectate_warning = 1;
488 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");
490 return MUT_SPECCMD_RETURN;
492 return MUT_SPECCMD_CONTINUE;
495 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
497 M_ARGV(0, float) = WinningCondition_LMS();
501 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
503 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
504 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
507 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
509 entity player = M_ARGV(0, entity);
511 return boolean(player.lmsplayer);
514 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
517 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
518 return true; // allow writing to this field in intermission as it is needed for newly joining players
521 void lms_Initialize()
523 lms_lowest_lives = 999;