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;
13 int autocvar_g_lms_leader_wp_lives;
14 float autocvar_g_lms_leader_wp_max_relative;
15 float autocvar_g_lms_leader_wp_time;
16 float autocvar_g_lms_leader_wp_time_repeat;
17 float autocvar_g_lms_dynamic_respawn_delay;
18 float autocvar_g_lms_dynamic_respawn_delay_base;
19 float autocvar_g_lms_dynamic_respawn_delay_increase;
20 bool autocvar_g_lms_dynamic_vampire;
21 float autocvar_g_lms_dynamic_vampire_factor_base;
22 float autocvar_g_lms_dynamic_vampire_factor_increase;
23 float autocvar_g_lms_dynamic_vampire_factor_max;
24 int autocvar_g_lms_dynamic_vampire_min_lives_diff;
29 int LMS_NewPlayerLives()
31 int fl = floor(autocvar_fraglimit);
32 if(fl == 0 || fl > 999)
35 // first player has left the game for dying too much? Nobody else can get in.
36 if(lms_lowest_lives < 1)
39 if(!autocvar_g_lms_join_anytime)
40 if(lms_lowest_lives < fl - max(0, floor(autocvar_g_lms_last_join)))
43 return bound(1, lms_lowest_lives, fl);
48 // LMS winning condition: game terminates if and only if there's at most one
49 // one player who's living lives. Top two scores being equal cancels the time
51 int WinningCondition_LMS()
53 entity first_player = NULL;
55 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
65 // two or more active players - continue with the game
67 if (autocvar_g_campaign)
69 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
70 float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
72 return WINNING_YES; // human player lost, game over
79 // exactly one player?
82 SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
84 if (LMS_NewPlayerLives())
86 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
92 // and assign him his first place
93 GameRules_scoring_add(first_player, LMS_RANK, 1);
103 // nobody is playing at all...
104 if (LMS_NewPlayerLives())
106 // wait for players...
110 // SNAFU (maybe a draw game?)
112 LOG_TRACE("No players, ending game.");
117 // When we get here, we have at least two players who are actually LIVING,
118 // now check if the top two players have equal score.
119 WinningConditionHelper(NULL);
122 if(WinningConditionHelper_winner)
123 WinningConditionHelper_winner.winning = true;
124 if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
125 return WINNING_NEVER;
127 // Top two have different scores? Way to go for our beloved TIMELIMIT!
132 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
134 lms_lowest_lives = 999;
137 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
139 FOREACH_CLIENT(true, {
140 TRANSMUTE(Player, it);
141 it.frags = FRAGS_PLAYER;
142 GameRules_scoring_add(it, LMS_LIVES, LMS_NewPlayerLives());
143 PutClientInServer(it);
144 if (it.waypointsprite_attachedforcarrier)
145 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
149 // FIXME add support for sv_ready_restart_after_countdown
150 // that is find a way to respawn/reset players IN GAME without setting lives to 0
151 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
154 sv_ready_restart_after_countdown = 0;
157 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
159 entity player = M_ARGV(0, entity);
161 if(player.frags == FRAGS_SPECTATOR)
162 TRANSMUTE(Observer, player);
165 float tl = GameRules_scoring_add(player, LMS_LIVES, 0);
166 if(tl < lms_lowest_lives)
167 lms_lowest_lives = tl;
169 TRANSMUTE(Observer, player);
171 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
175 MUTATOR_HOOKFUNCTION(lms, CalculateRespawnTime)
177 entity player = M_ARGV(0, entity);
178 player.respawn_flags |= RESPAWN_FORCE;
180 if (autocvar_g_lms_dynamic_respawn_delay <= 0)
183 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
186 FOREACH_CLIENT(it != player && IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
187 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
188 if (lives > max_lives)
193 // min delay with only 2 players
194 if (pl_cnt == 1) // player wasn't counted
197 player.respawn_time = time + autocvar_g_lms_dynamic_respawn_delay_base +
198 autocvar_g_lms_dynamic_respawn_delay_increase * max(0, max_lives - pl_lives);
202 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
204 entity player = M_ARGV(0, entity);
208 if(player.frags == FRAGS_SPECTATOR || GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
210 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
216 void lms_RemovePlayer(entity player)
218 static int quitters = 0;
219 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
222 if (player.lms_spectate_warning < 2)
224 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
226 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
229 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
233 FOREACH_CLIENT(true, {
234 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
236 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
237 if (it_rank > player_rank && it_rank <= 256)
238 GameRules_scoring_add(it, LMS_RANK, -1);
240 else if (it.frags != FRAGS_SPECTATOR)
242 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
243 if(tl < lms_lowest_lives)
244 lms_lowest_lives = tl;
247 GameRules_scoring_add(player, LMS_RANK, 665 - quitters); // different from 666
250 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
253 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
254 TRANSMUTE(Observer, player);
258 if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
260 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
261 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
263 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
267 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
269 entity player = M_ARGV(0, entity);
271 // no further message other than the disconnect message
272 player.lms_spectate_warning = 3;
274 lms_RemovePlayer(player);
277 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
279 entity player = M_ARGV(0, entity);
281 if (!IS_PLAYER(player))
284 lms_RemovePlayer(player);
285 return true; // prevent team reset
288 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
290 entity player = M_ARGV(0, entity);
292 if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
294 GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
295 player.frags = FRAGS_SPECTATOR;
299 // FIXME LMS doesn't allow clients to spectate due to its particular implementation
300 MUTATOR_HOOKFUNCTION(lms, AutoJoinOnConnection)
302 if(autocvar_g_campaign)
307 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
309 entity player = M_ARGV(0, entity);
311 if(player.deadflag == DEAD_DYING)
312 player.deadflag = DEAD_RESPAWNING;
315 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
317 if(autocvar_g_lms_regenerate)
322 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
328 MUTATOR_HOOKFUNCTION(lms, Damage_Calculate)
330 if (!autocvar_g_lms_dynamic_vampire)
333 entity frag_attacker = M_ARGV(1, entity);
334 entity frag_target = M_ARGV(2, entity);
335 float frag_damage = M_ARGV(4, float);
337 if (IS_PLAYER(frag_attacker) && !IS_DEAD(frag_target) && frag_attacker != frag_target)
339 float vampire_factor = 0;
341 int frag_attacker_lives = GameRules_scoring_add(frag_attacker, LMS_LIVES, 0);
342 int frag_target_lives = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
343 int diff = frag_target_lives - frag_attacker_lives - autocvar_g_lms_dynamic_vampire_min_lives_diff;
346 vampire_factor = autocvar_g_lms_dynamic_vampire_factor_base + diff * autocvar_g_lms_dynamic_vampire_factor_increase;
347 if (vampire_factor > 0)
349 vampire_factor = min(vampire_factor, autocvar_g_lms_dynamic_vampire_factor_max);
350 SetResourceExplicit(frag_attacker, RES_HEALTH,
351 min(GetResource(frag_attacker, RES_HEALTH) + frag_damage * vampire_factor, start_health));
356 bool lms_waypointsprite_visible_for_player(entity this, entity player, entity view) // runs on waypoints which are attached to ballcarriers, updates once per frame
360 return false; // we don't want spectators of leaders to see the attached waypoint on the top of their screen
362 float leader_time = autocvar_g_lms_leader_wp_time;
363 float leader_repeat_time = leader_time + autocvar_g_lms_leader_wp_time_repeat;
364 float wp_time = this.owner.lms_wp_time;
365 if (wp_time && (time - wp_time) % leader_repeat_time > leader_time)
371 void lms_UpdateWaypoints()
375 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
376 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
377 if (lives > max_lives)
382 int second_max_lives = 0;
383 int pl_cnt_with_max_lives = 0;
384 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
385 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
386 if (lives == max_lives)
387 pl_cnt_with_max_lives++;
388 else if (lives > second_max_lives)
389 second_max_lives = lives;
392 int lives_diff = autocvar_g_lms_leader_wp_lives;
393 if (max_lives - second_max_lives >= lives_diff && pl_cnt_with_max_lives <= pl_cnt * autocvar_g_lms_leader_wp_max_relative)
394 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
395 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
396 if (lives == max_lives)
398 if (!it.waypointsprite_attachedforcarrier)
400 WaypointSprite_AttachCarrier(WP_LmsLeader, it, RADARICON_FLAGCARRIER);
401 it.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = lms_waypointsprite_visible_for_player;
402 WaypointSprite_UpdateRule(it.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
403 vector pl_color = colormapPaletteColor(it.clientcolors & 0x0F, false);
404 WaypointSprite_UpdateTeamRadar(it.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, pl_color);
405 WaypointSprite_Ping(it.waypointsprite_attachedforcarrier);
408 it.lms_wp_time = time;
412 if (it.waypointsprite_attachedforcarrier)
413 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
418 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
419 if (it.waypointsprite_attachedforcarrier)
420 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
425 MUTATOR_HOOKFUNCTION(lms, PlayerDied)
427 if (!warmup_stage && autocvar_g_lms_leader_wp_lives > 0)
428 lms_UpdateWaypoints();
431 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
433 entity frag_target = M_ARGV(1, entity);
438 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
439 if(tl < lms_lowest_lives)
440 lms_lowest_lives = tl;
444 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
447 frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
448 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
451 M_ARGV(2, float) = 0; // frag score
456 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
458 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
459 start_health = warmup_start_health = cvar("g_lms_start_health");
460 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
461 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
462 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
463 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
464 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
465 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
466 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
469 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
471 // don't clear player score
475 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
477 entity definition = M_ARGV(0, entity);
479 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
486 void lms_extralife(entity this)
488 StartItem(this, ITEM_ExtraLife);
491 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
493 if (MUTATOR_RETURNVALUE) return false;
494 if (!autocvar_g_powerups) return false;
495 if (!autocvar_g_lms_extra_lives) return false;
497 entity ent = M_ARGV(0, entity);
499 // Can't use .itemdef here
500 if (ent.classname != "item_health_mega") return false;
503 setthink(e, lms_extralife);
505 e.nextthink = time + 0.1;
506 e.spawnflags = ent.spawnflags;
507 e.noalign = ent.noalign;
508 setorigin(e, ent.origin);
513 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
515 if(MUTATOR_RETURNVALUE) return false;
517 entity item = M_ARGV(0, entity);
518 entity toucher = M_ARGV(1, entity);
520 if(item.itemdef == ITEM_ExtraLife)
522 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
523 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
524 return MUT_ITEMTOUCH_PICKUP;
527 return MUT_ITEMTOUCH_CONTINUE;
530 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
532 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
533 ++M_ARGV(0, int); // activerealplayers
534 ++M_ARGV(1, int); // realplayers
540 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
542 entity player = M_ARGV(0, entity);
544 if(warmup_stage || player.lms_spectate_warning)
546 // for the forfeit message...
547 player.lms_spectate_warning = 2;
551 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
553 player.lms_spectate_warning = 1;
554 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");
556 return MUT_SPECCMD_RETURN;
558 return MUT_SPECCMD_CONTINUE;
561 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
563 M_ARGV(0, float) = WinningCondition_LMS();
567 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
569 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
570 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
573 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
578 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
581 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
582 return true; // allow writing to this field in intermission as it is needed for newly joining players
585 void lms_Initialize()
587 lms_lowest_lives = 999;