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;
18 bool autocvar_g_lms_dynamic_vampire;
19 float autocvar_g_lms_dynamic_vampire_factor_base;
20 float autocvar_g_lms_dynamic_vampire_factor_increase;
21 float autocvar_g_lms_dynamic_vampire_factor_max;
22 int autocvar_g_lms_dynamic_vampire_min_lives_diff;
27 int LMS_NewPlayerLives()
29 int fl = floor(autocvar_fraglimit);
30 if(fl == 0 || fl > 999)
33 // first player has left the game for dying too much? Nobody else can get in.
34 if(lms_lowest_lives < 1)
37 if(!autocvar_g_lms_join_anytime)
38 if(lms_lowest_lives < fl - max(0, floor(autocvar_g_lms_last_join)))
41 return bound(1, lms_lowest_lives, fl);
46 // LMS winning condition: game terminates if and only if there's at most one
47 // one player who's living lives. Top two scores being equal cancels the time
49 int WinningCondition_LMS()
51 entity first_player = NULL;
53 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
63 // two or more active players - continue with the game
65 if (autocvar_g_campaign)
67 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
68 float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
70 return WINNING_YES; // human player lost, game over
77 // exactly one player?
80 SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
82 if (LMS_NewPlayerLives())
84 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
90 // and assign him his first place
91 GameRules_scoring_add(first_player, LMS_RANK, 1);
101 // nobody is playing at all...
102 if (LMS_NewPlayerLives())
104 // wait for players...
108 // SNAFU (maybe a draw game?)
110 LOG_TRACE("No players, ending game.");
115 // When we get here, we have at least two players who are actually LIVING,
116 // now check if the top two players have equal score.
117 WinningConditionHelper(NULL);
120 if(WinningConditionHelper_winner)
121 WinningConditionHelper_winner.winning = true;
122 if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
123 return WINNING_NEVER;
125 // Top two have different scores? Way to go for our beloved TIMELIMIT!
130 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
132 lms_lowest_lives = 999;
135 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
137 FOREACH_CLIENT(true, {
138 TRANSMUTE(Player, it);
139 it.frags = FRAGS_PLAYER;
140 GameRules_scoring_add(it, LMS_LIVES, LMS_NewPlayerLives());
141 PutClientInServer(it);
142 if (it.waypointsprite_attachedforcarrier)
143 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
147 // FIXME add support for sv_ready_restart_after_countdown
148 // that is find a way to respawn/reset players IN GAME without setting lives to 0
149 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
152 sv_ready_restart_after_countdown = 0;
155 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
157 entity player = M_ARGV(0, entity);
159 if(player.frags == FRAGS_SPECTATOR)
160 TRANSMUTE(Observer, player);
163 float tl = GameRules_scoring_add(player, LMS_LIVES, 0);
164 if(tl < lms_lowest_lives)
165 lms_lowest_lives = tl;
167 TRANSMUTE(Observer, player);
169 GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
173 MUTATOR_HOOKFUNCTION(lms, CalculateRespawnTime)
175 entity player = M_ARGV(0, entity);
176 player.respawn_flags |= RESPAWN_FORCE;
178 if (autocvar_g_lms_dynamic_respawn_delay <= 0)
181 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
184 FOREACH_CLIENT(it != player && IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
185 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
186 if (lives > max_lives)
191 // min delay with only 2 players
192 if (pl_cnt == 1) // player wasn't counted
195 player.respawn_time = time + autocvar_g_lms_dynamic_respawn_delay_base +
196 autocvar_g_lms_dynamic_respawn_delay_increase * max(0, max_lives - pl_lives);
200 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
202 entity player = M_ARGV(0, entity);
206 if(player.frags == FRAGS_SPECTATOR)
208 if(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 if(IS_BOT_CLIENT(player))
226 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
228 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
231 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
235 FOREACH_CLIENT(true, {
236 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
238 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
239 if (it_rank > player_rank && it_rank <= 256)
240 GameRules_scoring_add(it, LMS_RANK, -1);
242 else if (it.frags != FRAGS_SPECTATOR)
244 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
245 if(tl < lms_lowest_lives)
246 lms_lowest_lives = tl;
249 GameRules_scoring_add(player, LMS_RANK, 665 - quitters); // different from 666
252 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
255 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
256 TRANSMUTE(Observer, player);
260 if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
262 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
263 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
265 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
269 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
271 entity player = M_ARGV(0, entity);
273 // no further message other than the disconnect message
274 player.lms_spectate_warning = 3;
276 lms_RemovePlayer(player);
279 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
281 entity player = M_ARGV(0, entity);
283 if (!IS_PLAYER(player))
286 lms_RemovePlayer(player);
287 return true; // prevent team reset
290 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
292 entity player = M_ARGV(0, entity);
294 if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
296 GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
297 player.frags = FRAGS_SPECTATOR;
301 // FIXME LMS doesn't allow clients to spectate due to its particular implementation
302 MUTATOR_HOOKFUNCTION(lms, AutoJoinOnConnection)
304 if(autocvar_g_campaign)
309 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
311 entity player = M_ARGV(0, entity);
313 if(player.deadflag == DEAD_DYING)
314 player.deadflag = DEAD_RESPAWNING;
317 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
319 if(autocvar_g_lms_regenerate)
324 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
330 MUTATOR_HOOKFUNCTION(lms, Damage_Calculate)
332 if (!autocvar_g_lms_dynamic_vampire)
335 entity frag_attacker = M_ARGV(1, entity);
336 entity frag_target = M_ARGV(2, entity);
337 float frag_damage = M_ARGV(4, float);
339 if (IS_PLAYER(frag_attacker) && !IS_DEAD(frag_target) && frag_attacker != frag_target)
341 float vampire_factor = 0;
343 int frag_attacker_lives = GameRules_scoring_add(frag_attacker, LMS_LIVES, 0);
344 int frag_target_lives = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
345 int diff = frag_target_lives - frag_attacker_lives - autocvar_g_lms_dynamic_vampire_min_lives_diff;
348 vampire_factor = autocvar_g_lms_dynamic_vampire_factor_base + diff * autocvar_g_lms_dynamic_vampire_factor_increase;
349 if (vampire_factor > 0)
351 vampire_factor = min(vampire_factor, autocvar_g_lms_dynamic_vampire_factor_max);
352 SetResourceExplicit(frag_attacker, RES_HEALTH,
353 min(GetResource(frag_attacker, RES_HEALTH) + frag_damage * vampire_factor, start_health));
358 bool lms_waypointsprite_visible_for_player(entity this, entity player, entity view) // runs on waypoints which are attached to ballcarriers, updates once per frame
362 return false; // we don't want spectators of leaders to see the attached waypoint on the top of their screen
364 float leader_time = autocvar_g_lms_leader_wp_time;
365 float leader_repeat_time = leader_time + autocvar_g_lms_leader_wp_time_repeat;
366 float wp_time = this.owner.lms_wp_time;
367 if (wp_time && (time - wp_time) % leader_repeat_time > leader_time)
373 void lms_UpdateWaypoints()
377 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
378 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
379 if (lives > max_lives)
384 int second_max_lives = 0;
385 int pl_cnt_with_max_lives = 0;
386 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
387 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
388 if (lives == max_lives)
389 pl_cnt_with_max_lives++;
390 else if (lives > second_max_lives)
391 second_max_lives = lives;
394 int lives_diff = autocvar_g_lms_leader_wp_lives;
395 if (max_lives - second_max_lives >= lives_diff && pl_cnt_with_max_lives <= pl_cnt * autocvar_g_lms_leader_wp_max_relative)
396 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
397 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
398 if (lives == max_lives)
400 if (!it.waypointsprite_attachedforcarrier)
402 WaypointSprite_AttachCarrier(WP_LmsLeader, it, RADARICON_FLAGCARRIER);
403 it.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = lms_waypointsprite_visible_for_player;
404 WaypointSprite_UpdateRule(it.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
405 vector pl_color = colormapPaletteColor(it.clientcolors & 0x0F, false);
406 WaypointSprite_UpdateTeamRadar(it.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, pl_color);
407 WaypointSprite_Ping(it.waypointsprite_attachedforcarrier);
410 it.lms_wp_time = time;
414 if (it.waypointsprite_attachedforcarrier)
415 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
420 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
421 if (it.waypointsprite_attachedforcarrier)
422 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
427 MUTATOR_HOOKFUNCTION(lms, PlayerDied)
429 if (!warmup_stage && autocvar_g_lms_leader_wp_lives > 0)
430 lms_UpdateWaypoints();
433 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
435 entity frag_target = M_ARGV(1, entity);
440 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
441 if(tl < lms_lowest_lives)
442 lms_lowest_lives = tl;
446 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
449 if(IS_BOT_CLIENT(frag_target))
450 bot_clear(frag_target);
451 frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
452 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
455 M_ARGV(2, float) = 0; // frag score
460 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
462 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
463 start_health = warmup_start_health = cvar("g_lms_start_health");
464 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
465 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
466 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
467 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
468 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
469 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
470 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
473 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
475 // don't clear player score
479 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
481 entity definition = M_ARGV(0, entity);
483 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
490 void lms_extralife(entity this)
492 StartItem(this, ITEM_ExtraLife);
495 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
497 if (!autocvar_g_powerups) return false;
498 if (!autocvar_g_lms_extra_lives) return false;
500 entity ent = M_ARGV(0, entity);
502 // Can't use .itemdef here
503 if (ent.classname != "item_health_mega") return false;
506 setthink(e, lms_extralife);
508 e.nextthink = time + 0.1;
509 e.spawnflags = ent.spawnflags;
510 e.noalign = ent.noalign;
511 setorigin(e, ent.origin);
516 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
518 entity item = M_ARGV(0, entity);
519 entity toucher = M_ARGV(1, entity);
521 if(item.itemdef == ITEM_ExtraLife)
523 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
524 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
525 return MUT_ITEMTOUCH_PICKUP;
528 return MUT_ITEMTOUCH_CONTINUE;
531 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
533 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
534 ++M_ARGV(0, int); // activerealplayers
535 ++M_ARGV(1, int); // realplayers
541 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
543 entity player = M_ARGV(0, entity);
545 if(warmup_stage || player.lms_spectate_warning)
547 // for the forfeit message...
548 player.lms_spectate_warning = 2;
552 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
554 player.lms_spectate_warning = 1;
555 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");
557 return MUT_SPECCMD_RETURN;
559 return MUT_SPECCMD_CONTINUE;
562 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
564 M_ARGV(0, float) = WinningCondition_LMS();
568 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
570 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
571 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
574 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
579 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
582 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
583 return true; // allow writing to this field in intermission as it is needed for newly joining players
586 void lms_Initialize()
588 lms_lowest_lives = 999;