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 float LMS_NewPlayerLives()
16 fl = autocvar_fraglimit;
20 // first player has left the game for dying too much? Nobody else can get in.
21 if(lms_lowest_lives < 1)
24 if(!autocvar_g_lms_join_anytime)
25 if(lms_lowest_lives < fl - autocvar_g_lms_last_join)
28 return bound(1, lms_lowest_lives, fl);
33 // LMS winning condition: game terminates if and only if there's at most one
34 // one player who's living lives. Top two scores being equal cancels the time
36 int WinningCondition_LMS()
38 entity first_player = NULL;
40 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
50 // two or more active players - continue with the game
52 if (autocvar_g_campaign)
54 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
55 float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
57 return WINNING_YES; // human player lost, game over
64 // exactly one player?
67 SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
69 if (LMS_NewPlayerLives())
71 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
77 // and assign him his first place
78 GameRules_scoring_add(first_player, LMS_RANK, 1);
88 // nobody is playing at all...
89 if (LMS_NewPlayerLives())
91 // wait for players...
95 // SNAFU (maybe a draw game?)
97 LOG_TRACE("No players, ending game.");
102 // When we get here, we have at least two players who are actually LIVING,
103 // now check if the top two players have equal score.
104 WinningConditionHelper(NULL);
107 if(WinningConditionHelper_winner)
108 WinningConditionHelper_winner.winning = true;
109 if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
110 return WINNING_NEVER;
112 // Top two have different scores? Way to go for our beloved TIMELIMIT!
117 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
119 lms_lowest_lives = 999;
122 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
124 FOREACH_CLIENT(true, {
125 TRANSMUTE(Player, it);
126 it.frags = FRAGS_PLAYER;
127 GameRules_scoring_add(it, LMS_LIVES, LMS_NewPlayerLives());
128 PutClientInServer(it);
132 // FIXME add support for sv_ready_restart_after_countdown
133 // that is find a way to respawn/reset players IN GAME without setting lives to 0
134 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
137 sv_ready_restart_after_countdown = 0;
140 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
142 entity player = M_ARGV(0, entity);
144 if(player.frags == FRAGS_SPECTATOR)
145 TRANSMUTE(Observer, player);
148 float tl = GameRules_scoring_add(player, LMS_LIVES, 0);
149 if(tl < lms_lowest_lives)
150 lms_lowest_lives = tl;
152 TRANSMUTE(Observer, player);
154 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
158 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
160 entity player = M_ARGV(0, entity);
164 if(player.frags == FRAGS_SPECTATOR)
166 if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
168 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
174 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
176 entity frag_target = M_ARGV(2, entity);
178 frag_target.respawn_flags |= RESPAWN_FORCE;
181 void lms_RemovePlayer(entity player)
183 static int quitters = 0;
184 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
188 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
191 if (player.lms_spectate_warning < 2)
193 if(IS_BOT_CLIENT(player))
195 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
196 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
200 lms_lowest_lives = 999;
201 FOREACH_CLIENT(true, {
202 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
204 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
205 if (it_rank > player_rank && it_rank <= 256)
206 GameRules_scoring_add(it, LMS_RANK, -1);
207 lms_lowest_lives = 0;
209 else if (it.frags != FRAGS_SPECTATOR)
211 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
212 if(tl < lms_lowest_lives)
213 lms_lowest_lives = tl;
216 GameRules_scoring_add(player, LMS_RANK, 665 - quitters); // different from 666
219 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
222 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
223 TRANSMUTE(Observer, player);
227 if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
229 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
230 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
232 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
236 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
238 entity player = M_ARGV(0, entity);
240 // no further message other than the disconnect message
241 player.lms_spectate_warning = 3;
243 lms_RemovePlayer(player);
246 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
248 entity player = M_ARGV(0, entity);
250 if (!IS_PLAYER(player))
253 lms_RemovePlayer(player);
254 return true; // prevent team reset
257 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
259 entity player = M_ARGV(0, entity);
261 if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
263 GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
264 player.frags = FRAGS_SPECTATOR;
268 // FIXME LMS doesn't allow clients to spectate due to its particular implementation
269 MUTATOR_HOOKFUNCTION(lms, AutoJoinOnConnection)
271 if(autocvar_g_campaign)
276 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
278 entity player = M_ARGV(0, entity);
280 if(player.deadflag == DEAD_DYING)
281 player.deadflag = DEAD_RESPAWNING;
284 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
286 if(autocvar_g_lms_regenerate)
291 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
297 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
299 entity frag_target = M_ARGV(1, entity);
304 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
305 if(tl < lms_lowest_lives)
306 lms_lowest_lives = tl;
310 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
313 if(IS_BOT_CLIENT(frag_target))
314 bot_clear(frag_target);
315 frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
316 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
319 M_ARGV(2, float) = 0; // frag score
324 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
326 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
327 start_health = warmup_start_health = cvar("g_lms_start_health");
328 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
329 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
330 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
331 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
332 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
333 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
334 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
337 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
339 // don't clear player score
343 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
345 entity definition = M_ARGV(0, entity);
347 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
354 void lms_extralife(entity this)
356 StartItem(this, ITEM_ExtraLife);
359 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
361 if (!autocvar_g_powerups) return false;
362 if (!autocvar_g_lms_extra_lives) return false;
364 entity ent = M_ARGV(0, entity);
366 // Can't use .itemdef here
367 if (ent.classname != "item_health_mega") return false;
370 setthink(e, lms_extralife);
372 e.nextthink = time + 0.1;
373 e.spawnflags = ent.spawnflags;
374 e.noalign = ent.noalign;
375 setorigin(e, ent.origin);
380 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
382 entity item = M_ARGV(0, entity);
383 entity toucher = M_ARGV(1, entity);
385 if(item.itemdef == ITEM_ExtraLife)
387 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES);
388 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
389 return MUT_ITEMTOUCH_PICKUP;
392 return MUT_ITEMTOUCH_CONTINUE;
395 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
397 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
398 ++M_ARGV(0, int); // activerealplayers
399 ++M_ARGV(1, int); // realplayers
405 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
407 entity player = M_ARGV(0, entity);
409 if(warmup_stage || player.lms_spectate_warning)
411 // for the forfeit message...
412 player.lms_spectate_warning = 2;
416 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
418 player.lms_spectate_warning = 1;
419 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");
421 return MUT_SPECCMD_RETURN;
423 return MUT_SPECCMD_CONTINUE;
426 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
428 M_ARGV(0, float) = WinningCondition_LMS();
432 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
434 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
435 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
438 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
443 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
446 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
447 return true; // allow writing to this field in intermission as it is needed for newly joining players
450 void lms_Initialize()
452 lms_lowest_lives = 9999;