4 #include <common/mutators/mutator/instagib/items.qh>
5 #include <server/campaign.qh>
6 #include <server/command/_mod.qh>
8 int autocvar_g_lms_extra_lives;
9 bool autocvar_g_lms_join_anytime;
10 int autocvar_g_lms_last_join;
11 bool autocvar_g_lms_regenerate;
14 float LMS_NewPlayerLives()
17 fl = autocvar_fraglimit;
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 - 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 entity first_player = NULL;
40 int total_players = 0;
41 FOREACH_CLIENT(IS_PLAYER(it), {
49 if (total_players > 1)
51 // two or more active players - continue with the game
53 if (autocvar_g_campaign)
55 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
56 float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
58 return WINNING_YES; // human player lost, game over
65 // exactly one player?
68 SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
70 if (LMS_NewPlayerLives())
72 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
78 // and assign him his first place
79 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 TRANSMUTE(Player, it);
127 it.frags = FRAGS_PLAYER;
128 GameRules_scoring_add(it, LMS_LIVES, LMS_NewPlayerLives());
129 PutClientInServer(it);
133 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
135 entity player = M_ARGV(0, entity);
137 if(player.frags == FRAGS_SPECTATOR)
138 TRANSMUTE(Observer, player);
141 float tl = GameRules_scoring_add(player, LMS_LIVES, 0);
142 if(tl < lms_lowest_lives)
143 lms_lowest_lives = tl;
145 TRANSMUTE(Observer, player);
147 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
151 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
153 entity player = M_ARGV(0, entity);
157 if(player.frags == FRAGS_SPECTATOR)
159 if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
161 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
167 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
169 entity frag_target = M_ARGV(2, entity);
171 frag_target.respawn_flags |= RESPAWN_FORCE;
174 void lms_RemovePlayer(entity player)
176 static int quitters = 0;
177 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
181 FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
182 if (player.lms_spectate_warning != 2)
184 if(IS_BOT_CLIENT(player))
186 player.frags = FRAGS_LMS_LOSER;
187 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
191 lms_lowest_lives = 999;
192 FOREACH_CLIENT(true, {
193 if (it.frags == FRAGS_LMS_LOSER)
195 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
196 if (it_rank > player_rank && it_rank <= 256)
197 GameRules_scoring_add(it, LMS_RANK, -1);
198 lms_lowest_lives = 0;
200 else if (it.frags != FRAGS_SPECTATOR)
202 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
203 if(tl < lms_lowest_lives)
204 lms_lowest_lives = tl;
207 GameRules_scoring_add(player, LMS_RANK, 665 - quitters); // different from 666
210 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
213 player.frags = FRAGS_LMS_LOSER;
214 TRANSMUTE(Observer, player);
216 if (pl_cnt == 2 && !warmup_stage) // a player is forfeiting leaving only one player
217 lms_lowest_lives = 0; // end the game now!
220 if(CS(player).killcount != FRAGS_SPECTATOR)
221 if(GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning != 2)
222 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
224 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
227 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
229 entity player = M_ARGV(0, entity);
231 lms_RemovePlayer(player);
234 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
236 entity player = M_ARGV(0, entity);
238 lms_RemovePlayer(player);
239 return true; // prevent team reset
242 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
244 entity player = M_ARGV(0, entity);
246 TRANSMUTE(Player, player);
247 campaign_bots_may_start = true;
249 if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
251 GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
252 player.frags = FRAGS_SPECTATOR;
256 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
258 entity player = M_ARGV(0, entity);
260 if(player.deadflag == DEAD_DYING)
261 player.deadflag = DEAD_RESPAWNING;
264 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
266 if(autocvar_g_lms_regenerate)
271 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
277 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
279 entity frag_target = M_ARGV(1, entity);
284 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
285 if(tl < lms_lowest_lives)
286 lms_lowest_lives = tl;
290 FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
291 if(IS_BOT_CLIENT(frag_target))
292 bot_clear(frag_target);
293 frag_target.frags = FRAGS_LMS_LOSER;
294 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
297 M_ARGV(2, float) = 0; // frag score
302 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
304 start_items &= ~IT_UNLIMITED_AMMO;
305 start_health = warmup_start_health = cvar("g_lms_start_health");
306 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
307 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
308 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
309 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
310 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
311 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
312 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
315 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
317 // don't clear player score
321 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
323 entity definition = M_ARGV(0, entity);
325 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
332 void lms_extralife(entity this)
334 StartItem(this, ITEM_ExtraLife);
337 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
339 if (!autocvar_g_powerups) return false;
340 if (!autocvar_g_lms_extra_lives) return false;
342 entity ent = M_ARGV(0, entity);
344 // Can't use .itemdef here
345 if (ent.classname != "item_health_mega") return false;
348 setthink(e, lms_extralife);
350 e.nextthink = time + 0.1;
351 e.spawnflags = ent.spawnflags;
352 e.noalign = ent.noalign;
353 setorigin(e, ent.origin);
358 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
360 entity item = M_ARGV(0, entity);
361 entity toucher = M_ARGV(1, entity);
363 if(item.itemdef == ITEM_ExtraLife)
365 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES);
366 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
367 return MUT_ITEMTOUCH_PICKUP;
370 return MUT_ITEMTOUCH_CONTINUE;
373 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
375 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
376 ++M_ARGV(0, int); // activerealplayers
377 ++M_ARGV(1, int); // realplayers
383 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
385 entity player = M_ARGV(0, entity);
387 if(warmup_stage || player.lms_spectate_warning)
389 // for the forfeit message...
390 player.lms_spectate_warning = 2;
394 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_LMS_LOSER)
396 player.lms_spectate_warning = 1;
397 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");
399 return MUT_SPECCMD_RETURN;
401 return MUT_SPECCMD_CONTINUE;
404 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
406 M_ARGV(0, float) = WinningCondition_LMS();
410 MUTATOR_HOOKFUNCTION(lms, WantWeapon)
412 M_ARGV(2, bool) = true; // all weapons
415 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
420 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
423 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
424 return true; // allow writing to this field in intermission as it is needed for newly joining players
427 void lms_Initialize()
429 lms_lowest_lives = 9999;