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 entity first_player = NULL;
41 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
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 // FIXME add support for sv_ready_restart_after_countdown
134 // that is find a way to respawn/reset players IN GAME without setting lives to 0
135 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
138 sv_ready_restart_after_countdown = 0;
141 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
143 entity player = M_ARGV(0, entity);
145 if(player.frags == FRAGS_SPECTATOR)
146 TRANSMUTE(Observer, player);
149 float tl = GameRules_scoring_add(player, LMS_LIVES, 0);
150 if(tl < lms_lowest_lives)
151 lms_lowest_lives = tl;
153 TRANSMUTE(Observer, player);
155 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
159 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
161 entity player = M_ARGV(0, entity);
165 if(player.frags == FRAGS_SPECTATOR || 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 float tl = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
180 frag_target.respawn_flags = RESPAWN_SILENT;
181 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
182 frag_target.respawn_time = time + 2;
184 frag_target.respawn_flags |= RESPAWN_FORCE;
187 void lms_RemovePlayer(entity player)
189 static int quitters = 0;
190 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
193 if (player.lms_spectate_warning < 2)
195 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
197 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
200 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
204 FOREACH_CLIENT(true, {
205 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
207 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
208 if (it_rank > player_rank && it_rank <= 256)
209 GameRules_scoring_add(it, LMS_RANK, -1);
211 else if (it.frags != FRAGS_SPECTATOR)
213 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
214 if(tl < lms_lowest_lives)
215 lms_lowest_lives = tl;
218 GameRules_scoring_add(player, LMS_RANK, 665 - quitters); // different from 666
221 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
224 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
225 TRANSMUTE(Observer, player);
229 if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
231 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
232 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
234 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
238 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
240 entity player = M_ARGV(0, entity);
242 // no further message other than the disconnect message
243 player.lms_spectate_warning = 3;
245 lms_RemovePlayer(player);
248 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
250 entity player = M_ARGV(0, entity);
252 if (!IS_PLAYER(player))
255 lms_RemovePlayer(player);
256 return true; // prevent team reset
259 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
261 entity player = M_ARGV(0, entity);
263 if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
265 GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
266 player.frags = FRAGS_SPECTATOR;
270 // FIXME LMS doesn't allow clients to spectate due to its particular implementation
271 MUTATOR_HOOKFUNCTION(lms, AutoJoinOnConnection)
273 if(autocvar_g_campaign)
278 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
280 entity player = M_ARGV(0, entity);
282 if(player.deadflag == DEAD_DYING)
283 player.deadflag = DEAD_RESPAWNING;
286 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
288 if(autocvar_g_lms_regenerate)
293 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
299 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
301 entity frag_target = M_ARGV(1, entity);
306 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
307 if(tl < lms_lowest_lives)
308 lms_lowest_lives = tl;
312 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
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 (MUTATOR_RETURNVALUE) return false;
362 if (!autocvar_g_powerups) return false;
363 if (!autocvar_g_lms_extra_lives) return false;
365 entity ent = M_ARGV(0, entity);
367 // Can't use .itemdef here
368 if (ent.classname != "item_health_mega") return false;
371 setthink(e, lms_extralife);
373 e.nextthink = time + 0.1;
374 e.spawnflags = ent.spawnflags;
375 e.noalign = ent.noalign;
376 setorigin(e, ent.origin);
381 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
383 if(MUTATOR_RETURNVALUE) return false;
385 entity item = M_ARGV(0, entity);
386 entity toucher = M_ARGV(1, entity);
388 if(item.itemdef == ITEM_ExtraLife)
390 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
391 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
392 return MUT_ITEMTOUCH_PICKUP;
395 return MUT_ITEMTOUCH_CONTINUE;
398 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
400 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
401 ++M_ARGV(0, int); // activerealplayers
402 ++M_ARGV(1, int); // realplayers
408 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
410 entity player = M_ARGV(0, entity);
412 if(warmup_stage || player.lms_spectate_warning)
414 // for the forfeit message...
415 player.lms_spectate_warning = 2;
419 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
421 player.lms_spectate_warning = 1;
422 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");
424 return MUT_SPECCMD_RETURN;
426 return MUT_SPECCMD_CONTINUE;
429 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
431 M_ARGV(0, float) = WinningCondition_LMS();
435 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
437 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
438 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
441 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
446 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
449 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
450 return true; // allow writing to this field in intermission as it is needed for newly joining players
453 void lms_Initialize()
455 lms_lowest_lives = 999;