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, PlayerSpawn)
179 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
181 entity player = M_ARGV(0, entity);
185 if(player.frags == FRAGS_SPECTATOR || GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
187 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
193 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
197 void lms_RemovePlayer(entity player)
199 static int quitters = 0;
200 float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
203 if (player.lms_spectate_warning < 2)
205 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
207 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
210 GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
214 FOREACH_CLIENT(true, {
215 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
217 float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
218 if (it_rank > player_rank && it_rank <= 256)
219 GameRules_scoring_add(it, LMS_RANK, -1);
221 else if (it.frags != FRAGS_SPECTATOR)
223 float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
224 if(tl < lms_lowest_lives)
225 lms_lowest_lives = tl;
228 GameRules_scoring_add(player, LMS_RANK, 665 - quitters); // different from 666
231 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
234 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
235 TRANSMUTE(Observer, player);
239 if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
241 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
242 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
244 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
248 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
250 entity player = M_ARGV(0, entity);
252 // no further message other than the disconnect message
253 player.lms_spectate_warning = 3;
255 lms_RemovePlayer(player);
258 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
260 entity player = M_ARGV(0, entity);
262 if (!IS_PLAYER(player))
265 lms_RemovePlayer(player);
266 return true; // prevent team reset
269 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
271 entity player = M_ARGV(0, entity);
273 if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
275 GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
276 player.frags = FRAGS_SPECTATOR;
280 // FIXME LMS doesn't allow clients to spectate due to its particular implementation
281 MUTATOR_HOOKFUNCTION(lms, AutoJoinOnConnection)
283 if(autocvar_g_campaign)
288 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
290 entity player = M_ARGV(0, entity);
292 if(player.deadflag == DEAD_DYING)
293 player.deadflag = DEAD_RESPAWNING;
296 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
298 if(autocvar_g_lms_regenerate)
303 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
309 MUTATOR_HOOKFUNCTION(lms, Damage_Calculate)
311 if (!autocvar_g_lms_dynamic_vampire)
314 entity frag_attacker = M_ARGV(1, entity);
315 entity frag_target = M_ARGV(2, entity);
316 float frag_damage = M_ARGV(4, float);
318 if (IS_PLAYER(frag_attacker) && !IS_DEAD(frag_attacker)
319 && IS_PLAYER(frag_target) && !IS_DEAD(frag_target) && frag_attacker != frag_target)
321 float vampire_factor = 0;
323 int frag_attacker_lives = GameRules_scoring_add(frag_attacker, LMS_LIVES, 0);
324 int frag_target_lives = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
325 int diff = frag_target_lives - frag_attacker_lives - autocvar_g_lms_dynamic_vampire_min_lives_diff;
328 vampire_factor = autocvar_g_lms_dynamic_vampire_factor_base + diff * autocvar_g_lms_dynamic_vampire_factor_increase;
329 if (vampire_factor > 0)
331 vampire_factor = min(vampire_factor, autocvar_g_lms_dynamic_vampire_factor_max);
332 SetResourceExplicit(frag_attacker, RES_HEALTH,
333 min(GetResource(frag_attacker, RES_HEALTH) + frag_damage * vampire_factor, start_health));
338 bool lms_waypointsprite_visible_for_player(entity this, entity player, entity view) // runs on waypoints which are attached to ballcarriers, updates once per frame
342 return false; // we don't want spectators of leaders to see the attached waypoint on the top of their screen
344 float leader_time = autocvar_g_lms_leader_wp_time;
345 float leader_repeat_time = leader_time + autocvar_g_lms_leader_wp_time_repeat;
346 float wp_time = this.owner.lms_wp_time;
347 if (wp_time && (time - wp_time) % leader_repeat_time > leader_time)
353 void lms_UpdateWaypoints()
357 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
358 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
359 if (lives > max_lives)
364 int second_max_lives = 0;
365 int pl_cnt_with_max_lives = 0;
366 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
367 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
368 if (lives == max_lives)
369 pl_cnt_with_max_lives++;
370 else if (lives > second_max_lives)
371 second_max_lives = lives;
374 int lives_diff = autocvar_g_lms_leader_wp_lives;
375 if (max_lives - second_max_lives >= lives_diff && pl_cnt_with_max_lives <= pl_cnt * autocvar_g_lms_leader_wp_max_relative)
376 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
377 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
378 if (lives == max_lives)
380 if (!it.waypointsprite_attachedforcarrier)
382 WaypointSprite_AttachCarrier(WP_LmsLeader, it, RADARICON_FLAGCARRIER);
383 it.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = lms_waypointsprite_visible_for_player;
384 WaypointSprite_UpdateRule(it.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
385 vector pl_color = colormapPaletteColor(it.clientcolors & 0x0F, false);
386 WaypointSprite_UpdateTeamRadar(it.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, pl_color);
387 WaypointSprite_Ping(it.waypointsprite_attachedforcarrier);
390 it.lms_wp_time = time;
394 if (it.waypointsprite_attachedforcarrier)
395 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
400 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
401 if (it.waypointsprite_attachedforcarrier)
402 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
407 MUTATOR_HOOKFUNCTION(lms, PlayerDied)
409 if (!warmup_stage && autocvar_g_lms_leader_wp_lives > 0)
410 lms_UpdateWaypoints();
413 MUTATOR_HOOKFUNCTION(lms, CalculateRespawnTime)
415 entity player = M_ARGV(0, entity);
416 player.respawn_flags |= RESPAWN_FORCE;
418 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
421 player.respawn_flags = RESPAWN_SILENT;
422 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
423 player.respawn_time = time + 2;
427 if (autocvar_g_lms_dynamic_respawn_delay <= 0)
432 FOREACH_CLIENT(it != player && IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
433 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
434 if (lives > max_lives)
439 // min delay with only 2 players
440 if (pl_cnt == 1) // player wasn't counted
443 player.respawn_time = time + autocvar_g_lms_dynamic_respawn_delay_base +
444 autocvar_g_lms_dynamic_respawn_delay_increase * max(0, max_lives - pl_lives);
448 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
450 entity frag_target = M_ARGV(1, entity);
455 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
456 if(tl < lms_lowest_lives)
457 lms_lowest_lives = tl;
461 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
464 frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
465 GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
468 M_ARGV(2, float) = 0; // frag score
473 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
475 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
476 start_health = warmup_start_health = cvar("g_lms_start_health");
477 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
478 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
479 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
480 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
481 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
482 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
483 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
486 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
488 // don't clear player score
492 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
494 entity definition = M_ARGV(0, entity);
496 if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
503 void lms_extralife(entity this)
505 StartItem(this, ITEM_ExtraLife);
508 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
510 if (MUTATOR_RETURNVALUE) return false;
511 if (!autocvar_g_powerups) return false;
512 if (!autocvar_g_lms_extra_lives) return false;
514 entity ent = M_ARGV(0, entity);
516 // Can't use .itemdef here
517 if (ent.classname != "item_health_mega") return false;
520 setthink(e, lms_extralife);
522 e.nextthink = time + 0.1;
523 e.spawnflags = ent.spawnflags;
524 e.noalign = ent.noalign;
525 setorigin(e, ent.origin);
530 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
532 if(MUTATOR_RETURNVALUE) return false;
534 entity item = M_ARGV(0, entity);
535 entity toucher = M_ARGV(1, entity);
537 if(item.itemdef == ITEM_ExtraLife)
539 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
540 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
541 return MUT_ITEMTOUCH_PICKUP;
544 return MUT_ITEMTOUCH_CONTINUE;
547 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
549 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
550 ++M_ARGV(0, int); // activerealplayers
551 ++M_ARGV(1, int); // realplayers
557 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
559 entity player = M_ARGV(0, entity);
561 if(warmup_stage || player.lms_spectate_warning)
563 // for the forfeit message...
564 player.lms_spectate_warning = 2;
568 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
570 player.lms_spectate_warning = 1;
571 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");
573 return MUT_SPECCMD_RETURN;
575 return MUT_SPECCMD_CONTINUE;
578 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
580 M_ARGV(0, float) = WinningCondition_LMS();
584 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
586 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
587 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
590 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
595 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
598 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
599 return true; // allow writing to this field in intermission as it is needed for newly joining players
602 void lms_Initialize()
604 lms_lowest_lives = 999;