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, ForbidSpawn)
202 entity player = M_ARGV(0, entity);
204 if (warmup_stage || lms_AddPlayer(player))
210 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
212 entity frag_target = M_ARGV(2, entity);
214 float tl = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
217 frag_target.respawn_flags = RESPAWN_SILENT;
218 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
219 frag_target.respawn_time = time + 2;
221 frag_target.respawn_flags |= RESPAWN_FORCE;
224 void lms_RemovePlayer(entity player)
226 if (warmup_stage || time < game_starttime)
229 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
232 if (player.lms_spectate_warning < 2)
234 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
236 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
239 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
243 FOREACH_CLIENT(true, {
244 // update rank of other players that were eliminated
245 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
247 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
248 if (it_rank > player_rank && it_rank <= 256)
249 GameRules_scoring_add(it, LMS_RANK, -1);
251 else if (it.frags != FRAGS_SPECTATOR)
253 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
254 if(tl < lms_lowest_lives)
255 lms_lowest_lives = tl;
258 GameRules_scoring_add(player, LMS_RANK, 665 - lms_quitters); // different from 666
261 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
264 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
265 TRANSMUTE(Observer, player);
269 if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
271 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
272 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
274 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
278 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
280 entity player = M_ARGV(0, entity);
282 // no further message other than the disconnect message
283 player.lms_spectate_warning = 3;
285 lms_RemovePlayer(player);
286 player.lmsplayer = 0;
289 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
291 entity player = M_ARGV(0, entity);
293 if (!IS_PLAYER(player))
296 if (warmup_stage || time <= game_starttime)
298 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
299 player.frags = FRAGS_SPECTATOR;
300 TRANSMUTE(Observer, player);
301 player.lmsplayer = 0;
303 else if (!GameRules_scoring_add(player, LMS_RANK, 0))
304 lms_RemovePlayer(player);
305 return true; // prevent team reset
308 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
310 entity player = M_ARGV(0, entity);
311 TRANSMUTE(Observer, player);
312 player.frags = FRAGS_SPECTATOR;
313 player.lms_spectate_warning = 0;
316 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
318 entity player = M_ARGV(0, entity);
320 if(player.deadflag == DEAD_DYING)
321 player.deadflag = DEAD_RESPAWNING;
324 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
326 if(autocvar_g_lms_regenerate)
331 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
337 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
339 entity frag_target = M_ARGV(1, entity);
341 if (!warmup_stage && time > game_starttime)
344 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
345 if(tl < lms_lowest_lives)
346 lms_lowest_lives = tl;
350 FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
353 frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
354 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
357 M_ARGV(2, float) = 0; // frag score
362 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
364 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
365 start_health = warmup_start_health = cvar("g_lms_start_health");
366 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
367 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
368 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
369 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
370 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
371 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
372 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
375 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
377 // don't clear player score
381 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
383 entity definition = M_ARGV(0, entity);
385 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
392 void lms_extralife(entity this)
394 StartItem(this, ITEM_ExtraLife);
397 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
399 if (MUTATOR_RETURNVALUE) return false;
400 if (!autocvar_g_powerups) return false;
401 if (!autocvar_g_lms_extra_lives) return false;
403 entity ent = M_ARGV(0, entity);
405 // Can't use .itemdef here
406 if (ent.classname != "item_health_mega") return false;
409 setthink(e, lms_extralife);
411 e.nextthink = time + 0.1;
412 e.spawnflags = ent.spawnflags;
413 e.noalign = ent.noalign;
414 setorigin(e, ent.origin);
419 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
421 if(MUTATOR_RETURNVALUE) return false;
423 entity item = M_ARGV(0, entity);
424 entity toucher = M_ARGV(1, entity);
426 if(item.itemdef == ITEM_ExtraLife)
428 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
429 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
430 return MUT_ITEMTOUCH_PICKUP;
433 return MUT_ITEMTOUCH_CONTINUE;
436 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
438 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
439 if (it.lmsplayer && it.lms_spectate_warning < 2)
440 ++M_ARGV(0, int); // activerealplayers
441 ++M_ARGV(1, int); // realplayers
447 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
449 entity player = M_ARGV(0, entity);
451 if(warmup_stage || time < game_starttime || player.lms_spectate_warning)
453 // for the forfeit message...
454 player.lms_spectate_warning = 2;
458 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
460 player.lms_spectate_warning = 1;
461 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");
463 return MUT_SPECCMD_RETURN;
465 return MUT_SPECCMD_CONTINUE;
468 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
470 M_ARGV(0, float) = WinningCondition_LMS();
474 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
476 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
477 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
480 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
482 entity player = M_ARGV(0, entity);
484 return boolean(player.lmsplayer);
487 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
490 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
491 return true; // allow writing to this field in intermission as it is needed for newly joining players
494 void lms_Initialize()
496 lms_lowest_lives = 999;