3 #include <common/mutators/mutator/instagib/items.qh>
4 #include <server/campaign.qh>
5 #include <server/command/_mod.qh>
7 int autocvar_g_lms_extra_lives;
8 bool autocvar_g_lms_join_anytime;
9 int autocvar_g_lms_last_join;
10 bool autocvar_g_lms_regenerate;
13 int LMS_NewPlayerLives()
15 int fl = floor(autocvar_fraglimit);
16 if(fl == 0 || fl > 999)
19 // first player has left the game for dying too much? Nobody else can get in.
20 if(lms_lowest_lives < 1)
23 if(!autocvar_g_lms_join_anytime)
24 if(lms_lowest_lives < fl - max(0, floor(autocvar_g_lms_last_join)))
27 return bound(1, lms_lowest_lives, fl);
32 // LMS winning condition: game terminates if and only if there's at most one
33 // one player who's living lives. Top two scores being equal cancels the time
35 int WinningCondition_LMS()
37 entity first_player = NULL;
39 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
49 // two or more active players - continue with the game
51 if (autocvar_g_campaign)
53 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
54 float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
56 return WINNING_YES; // human player lost, game over
63 // exactly one player?
66 SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
68 if (LMS_NewPlayerLives())
70 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
76 // and assign him his first place
77 GameRules_scoring_add(first_player, LMS_RANK, 1);
87 // nobody is playing at all...
88 if (LMS_NewPlayerLives())
90 // wait for players...
94 // SNAFU (maybe a draw game?)
96 LOG_TRACE("No players, ending game.");
101 // When we get here, we have at least two players who are actually LIVING,
102 // now check if the top two players have equal score.
103 WinningConditionHelper(NULL);
106 if(WinningConditionHelper_winner)
107 WinningConditionHelper_winner.winning = true;
108 if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
109 return WINNING_NEVER;
111 // Top two have different scores? Way to go for our beloved TIMELIMIT!
116 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
118 lms_lowest_lives = 999;
121 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
123 FOREACH_CLIENT(true, {
124 TRANSMUTE(Player, it);
125 it.frags = FRAGS_PLAYER;
126 GameRules_scoring_add(it, LMS_LIVES, LMS_NewPlayerLives());
127 PutClientInServer(it);
131 // FIXME add support for sv_ready_restart_after_countdown
132 // that is find a way to respawn/reset players IN GAME without setting lives to 0
133 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
136 sv_ready_restart_after_countdown = 0;
139 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
141 entity player = M_ARGV(0, entity);
143 if(player.frags == FRAGS_SPECTATOR)
144 TRANSMUTE(Observer, player);
147 float tl = GameRules_scoring_add(player, LMS_LIVES, 0);
148 if(tl < lms_lowest_lives)
149 lms_lowest_lives = tl;
151 TRANSMUTE(Observer, player);
153 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
157 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
159 entity player = M_ARGV(0, entity);
163 if(player.frags == FRAGS_SPECTATOR)
165 if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
167 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
173 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
175 entity frag_target = M_ARGV(2, entity);
177 frag_target.respawn_flags |= RESPAWN_FORCE;
180 void lms_RemovePlayer(entity player)
182 static int quitters = 0;
183 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
186 if (player.lms_spectate_warning < 2)
188 if(IS_BOT_CLIENT(player))
190 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
192 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
195 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
199 FOREACH_CLIENT(true, {
200 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
202 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
203 if (it_rank > player_rank && it_rank <= 256)
204 GameRules_scoring_add(it, LMS_RANK, -1);
206 else if (it.frags != FRAGS_SPECTATOR)
208 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
209 if(tl < lms_lowest_lives)
210 lms_lowest_lives = tl;
213 GameRules_scoring_add(player, LMS_RANK, 665 - quitters); // different from 666
216 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
219 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
220 TRANSMUTE(Observer, player);
224 if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
226 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
227 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
229 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
233 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
235 entity player = M_ARGV(0, entity);
237 // no further message other than the disconnect message
238 player.lms_spectate_warning = 3;
240 lms_RemovePlayer(player);
243 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
245 entity player = M_ARGV(0, entity);
247 if (!IS_PLAYER(player))
250 lms_RemovePlayer(player);
251 return true; // prevent team reset
254 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
256 entity player = M_ARGV(0, entity);
258 if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
260 GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
261 player.frags = FRAGS_SPECTATOR;
265 // FIXME LMS doesn't allow clients to spectate due to its particular implementation
266 MUTATOR_HOOKFUNCTION(lms, AutoJoinOnConnection)
268 if(autocvar_g_campaign)
273 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
275 entity player = M_ARGV(0, entity);
277 if(player.deadflag == DEAD_DYING)
278 player.deadflag = DEAD_RESPAWNING;
281 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
283 if(autocvar_g_lms_regenerate)
288 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
294 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
296 entity frag_target = M_ARGV(1, entity);
301 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
302 if(tl < lms_lowest_lives)
303 lms_lowest_lives = tl;
307 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
310 if(IS_BOT_CLIENT(frag_target))
311 bot_clear(frag_target);
312 frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
313 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
316 M_ARGV(2, float) = 0; // frag score
321 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
323 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
324 start_health = warmup_start_health = cvar("g_lms_start_health");
325 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
326 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
327 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
328 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
329 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
330 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
331 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
334 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
336 // don't clear player score
340 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
342 entity definition = M_ARGV(0, entity);
344 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
351 void lms_extralife(entity this)
353 StartItem(this, ITEM_ExtraLife);
356 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
358 if (!autocvar_g_powerups) return false;
359 if (!autocvar_g_lms_extra_lives) return false;
361 entity ent = M_ARGV(0, entity);
363 // Can't use .itemdef here
364 if (ent.classname != "item_health_mega") return false;
367 setthink(e, lms_extralife);
369 e.nextthink = time + 0.1;
370 e.spawnflags = ent.spawnflags;
371 e.noalign = ent.noalign;
372 setorigin(e, ent.origin);
377 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
379 entity item = M_ARGV(0, entity);
380 entity toucher = M_ARGV(1, entity);
382 if(item.itemdef == ITEM_ExtraLife)
384 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
385 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
386 return MUT_ITEMTOUCH_PICKUP;
389 return MUT_ITEMTOUCH_CONTINUE;
392 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
394 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
395 ++M_ARGV(0, int); // activerealplayers
396 ++M_ARGV(1, int); // realplayers
402 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
404 entity player = M_ARGV(0, entity);
406 if(warmup_stage || player.lms_spectate_warning)
408 // for the forfeit message...
409 player.lms_spectate_warning = 2;
413 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
415 player.lms_spectate_warning = 1;
416 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");
418 return MUT_SPECCMD_RETURN;
420 return MUT_SPECCMD_CONTINUE;
423 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
425 M_ARGV(0, float) = WinningCondition_LMS();
429 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
431 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
432 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
435 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
440 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
443 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
444 return true; // allow writing to this field in intermission as it is needed for newly joining players
447 void lms_Initialize()
449 lms_lowest_lives = 999;