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;
11 int autocvar_g_lms_leader_wp_lives;
12 float autocvar_g_lms_leader_wp_max_relative;
13 float autocvar_g_lms_leader_wp_time;
14 float autocvar_g_lms_leader_wp_time_repeat;
15 float autocvar_g_lms_dynamic_respawn_delay;
16 float autocvar_g_lms_dynamic_respawn_delay_base;
17 float autocvar_g_lms_dynamic_respawn_delay_increase;
22 int LMS_NewPlayerLives()
24 int fl = floor(autocvar_fraglimit);
25 if(fl == 0 || fl > 999)
28 // first player has left the game for dying too much? Nobody else can get in.
29 if(lms_lowest_lives < 1)
32 if(!autocvar_g_lms_join_anytime)
33 if(lms_lowest_lives < fl - max(0, floor(autocvar_g_lms_last_join)))
36 return bound(1, lms_lowest_lives, fl);
41 // LMS winning condition: game terminates if and only if there's at most one
42 // one player who's living lives. Top two scores being equal cancels the time
44 int WinningCondition_LMS()
46 entity first_player = NULL;
48 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
58 // two or more active players - continue with the game
60 if (autocvar_g_campaign)
62 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
63 float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
65 return WINNING_YES; // human player lost, game over
72 // exactly one player?
75 SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
77 if (LMS_NewPlayerLives())
79 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
85 // and assign him his first place
86 GameRules_scoring_add(first_player, LMS_RANK, 1);
96 // nobody is playing at all...
97 if (LMS_NewPlayerLives())
99 // wait for players...
103 // SNAFU (maybe a draw game?)
105 LOG_TRACE("No players, ending game.");
110 // When we get here, we have at least two players who are actually LIVING,
111 // now check if the top two players have equal score.
112 WinningConditionHelper(NULL);
115 if(WinningConditionHelper_winner)
116 WinningConditionHelper_winner.winning = true;
117 if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
118 return WINNING_NEVER;
120 // Top two have different scores? Way to go for our beloved TIMELIMIT!
125 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
127 lms_lowest_lives = 999;
130 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
132 FOREACH_CLIENT(true, {
133 TRANSMUTE(Player, it);
134 it.frags = FRAGS_PLAYER;
135 GameRules_scoring_add(it, LMS_LIVES, LMS_NewPlayerLives());
136 PutClientInServer(it);
137 if (it.waypointsprite_attachedforcarrier)
138 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
142 // FIXME add support for sv_ready_restart_after_countdown
143 // that is find a way to respawn/reset players IN GAME without setting lives to 0
144 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
147 sv_ready_restart_after_countdown = 0;
150 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
152 entity player = M_ARGV(0, entity);
154 if(player.frags == FRAGS_SPECTATOR)
155 TRANSMUTE(Observer, player);
158 float tl = GameRules_scoring_add(player, LMS_LIVES, 0);
159 if(tl < lms_lowest_lives)
160 lms_lowest_lives = tl;
162 TRANSMUTE(Observer, player);
164 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
168 MUTATOR_HOOKFUNCTION(lms, CalculateRespawnTime)
170 entity player = M_ARGV(0, entity);
171 player.respawn_flags |= RESPAWN_FORCE;
173 if (autocvar_g_lms_dynamic_respawn_delay <= 0)
176 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
179 FOREACH_CLIENT(it != player && IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
180 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
181 if (lives > max_lives)
186 // min delay with only 2 players
187 if (pl_cnt == 1) // player wasn't counted
190 player.respawn_time = time + autocvar_g_lms_dynamic_respawn_delay_base +
191 autocvar_g_lms_dynamic_respawn_delay_increase * max(0, max_lives - pl_lives);
195 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
197 entity player = M_ARGV(0, entity);
201 if(player.frags == FRAGS_SPECTATOR)
203 if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
205 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
211 void lms_RemovePlayer(entity player)
213 static int quitters = 0;
214 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
217 if (player.lms_spectate_warning < 2)
219 if(IS_BOT_CLIENT(player))
221 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
223 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
226 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
230 FOREACH_CLIENT(true, {
231 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
233 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
234 if (it_rank > player_rank && it_rank <= 256)
235 GameRules_scoring_add(it, LMS_RANK, -1);
237 else if (it.frags != FRAGS_SPECTATOR)
239 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
240 if(tl < lms_lowest_lives)
241 lms_lowest_lives = tl;
244 GameRules_scoring_add(player, LMS_RANK, 665 - quitters); // different from 666
247 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
250 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
251 TRANSMUTE(Observer, player);
255 if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
257 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
258 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
260 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
264 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
266 entity player = M_ARGV(0, entity);
268 // no further message other than the disconnect message
269 player.lms_spectate_warning = 3;
271 lms_RemovePlayer(player);
274 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
276 entity player = M_ARGV(0, entity);
278 if (!IS_PLAYER(player))
281 lms_RemovePlayer(player);
282 return true; // prevent team reset
285 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
287 entity player = M_ARGV(0, entity);
289 if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
291 GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
292 player.frags = FRAGS_SPECTATOR;
296 // FIXME LMS doesn't allow clients to spectate due to its particular implementation
297 MUTATOR_HOOKFUNCTION(lms, AutoJoinOnConnection)
299 if(autocvar_g_campaign)
304 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
306 entity player = M_ARGV(0, entity);
308 if(player.deadflag == DEAD_DYING)
309 player.deadflag = DEAD_RESPAWNING;
312 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
314 if(autocvar_g_lms_regenerate)
319 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
325 bool lms_waypointsprite_visible_for_player(entity this, entity player, entity view) // runs on waypoints which are attached to ballcarriers, updates once per frame
329 return false; // we don't want spectators of leaders to see the attached waypoint on the top of their screen
331 float leader_time = autocvar_g_lms_leader_wp_time;
332 float leader_repeat_time = leader_time + autocvar_g_lms_leader_wp_time_repeat;
333 float wp_time = this.owner.lms_wp_time;
334 if (wp_time && (time - wp_time) % leader_repeat_time > leader_time)
340 void lms_UpdateWaypoints()
344 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
345 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
346 if (lives > max_lives)
351 int second_max_lives = 0;
352 int pl_cnt_with_max_lives = 0;
353 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
354 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
355 if (lives == max_lives)
356 pl_cnt_with_max_lives++;
357 else if (lives > second_max_lives)
358 second_max_lives = lives;
361 int lives_diff = autocvar_g_lms_leader_wp_lives;
362 if (max_lives - second_max_lives >= lives_diff && pl_cnt_with_max_lives <= pl_cnt * autocvar_g_lms_leader_wp_max_relative)
363 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
364 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
365 if (lives == max_lives)
367 if (!it.waypointsprite_attachedforcarrier)
369 WaypointSprite_AttachCarrier(WP_LmsLeader, it, RADARICON_FLAGCARRIER);
370 it.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = lms_waypointsprite_visible_for_player;
371 WaypointSprite_UpdateRule(it.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
372 WaypointSprite_Ping(it.waypointsprite_attachedforcarrier);
375 it.lms_wp_time = time;
379 if (it.waypointsprite_attachedforcarrier)
380 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
385 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
386 if (it.waypointsprite_attachedforcarrier)
387 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
392 MUTATOR_HOOKFUNCTION(lms, PlayerDied)
394 if (!warmup_stage && autocvar_g_lms_leader_wp_lives > 0)
395 lms_UpdateWaypoints();
398 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
400 entity frag_target = M_ARGV(1, entity);
405 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
406 if(tl < lms_lowest_lives)
407 lms_lowest_lives = tl;
411 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
414 if(IS_BOT_CLIENT(frag_target))
415 bot_clear(frag_target);
416 frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
417 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
420 M_ARGV(2, float) = 0; // frag score
425 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
427 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
428 start_health = warmup_start_health = cvar("g_lms_start_health");
429 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
430 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
431 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
432 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
433 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
434 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
435 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
438 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
440 // don't clear player score
444 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
446 entity definition = M_ARGV(0, entity);
448 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
455 void lms_extralife(entity this)
457 StartItem(this, ITEM_ExtraLife);
460 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
462 if (!autocvar_g_powerups) return false;
463 if (!autocvar_g_lms_extra_lives) return false;
465 entity ent = M_ARGV(0, entity);
467 // Can't use .itemdef here
468 if (ent.classname != "item_health_mega") return false;
471 setthink(e, lms_extralife);
473 e.nextthink = time + 0.1;
474 e.spawnflags = ent.spawnflags;
475 e.noalign = ent.noalign;
476 setorigin(e, ent.origin);
481 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
483 entity item = M_ARGV(0, entity);
484 entity toucher = M_ARGV(1, entity);
486 if(item.itemdef == ITEM_ExtraLife)
488 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
489 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
490 return MUT_ITEMTOUCH_PICKUP;
493 return MUT_ITEMTOUCH_CONTINUE;
496 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
498 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
499 ++M_ARGV(0, int); // activerealplayers
500 ++M_ARGV(1, int); // realplayers
506 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
508 entity player = M_ARGV(0, entity);
510 if(warmup_stage || player.lms_spectate_warning)
512 // for the forfeit message...
513 player.lms_spectate_warning = 2;
517 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
519 player.lms_spectate_warning = 1;
520 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");
522 return MUT_SPECCMD_RETURN;
524 return MUT_SPECCMD_CONTINUE;
527 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
529 M_ARGV(0, float) = WinningCondition_LMS();
533 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
535 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
536 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
539 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
544 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
547 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
548 return true; // allow writing to this field in intermission as it is needed for newly joining players
551 void lms_Initialize()
553 lms_lowest_lives = 999;