1 #include "gamemode_lms.qh"
3 #include <common/mutators/mutator/instagib/items.qc>
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;
13 float LMS_NewPlayerLives()
16 fl = autocvar_fraglimit;
20 // first player has left the game for dying too much? Nobody else can get in.
21 if(lms_lowest_lives < 1)
24 if(!autocvar_g_lms_join_anytime)
25 if(lms_lowest_lives < fl - autocvar_g_lms_last_join)
28 return bound(1, lms_lowest_lives, fl);
33 // LMS winning condition: game terminates if and only if there's at most one
34 // one player who's living lives. Top two scores being equal cancels the time
36 int WinningCondition_LMS()
39 bool have_player = false;
40 bool have_players = false;
42 int l = LMS_NewPlayerLives();
44 head = find(NULL, classname, STR_PLAYER);
47 head2 = find(head, classname, STR_PLAYER);
53 // we have at least one player
56 // two or more active players - continue with the game
60 // exactly one player?
63 SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
67 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
73 // and assign him his first place
74 PlayerScore_Add(head, SP_LMS_RANK, 1);
84 // nobody is playing at all...
87 // wait for players...
91 // SNAFU (maybe a draw game?)
93 LOG_TRACE("No players, ending game.");
98 // When we get here, we have at least two players who are actually LIVING,
99 // now check if the top two players have equal score.
100 WinningConditionHelper(NULL);
103 if(WinningConditionHelper_winner)
104 WinningConditionHelper_winner.winning = true;
105 if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
106 return WINNING_NEVER;
108 // Top two have different scores? Way to go for our beloved TIMELIMIT!
113 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
115 lms_lowest_lives = 999;
118 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
120 FOREACH_CLIENT(true, {
121 TRANSMUTE(Player, it);
122 it.frags = FRAGS_PLAYER;
123 PlayerScore_Add(it, SP_LMS_LIVES, LMS_NewPlayerLives());
124 PutClientInServer(it);
128 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
130 entity player = M_ARGV(0, entity);
132 if(player.frags == FRAGS_SPECTATOR)
133 TRANSMUTE(Observer, player);
136 float tl = PlayerScore_Add(player, SP_LMS_LIVES, 0);
137 if(tl < lms_lowest_lives)
138 lms_lowest_lives = tl;
140 TRANSMUTE(Observer, player);
142 PlayerScore_Add(player, SP_LMS_RANK, -PlayerScore_Add(player, SP_LMS_RANK, 0));
146 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
148 entity player = M_ARGV(0, entity);
152 if(player.frags == FRAGS_SPECTATOR)
154 if(PlayerScore_Add(player, SP_LMS_LIVES, 0) <= 0)
156 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
162 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
164 entity frag_target = M_ARGV(2, entity);
166 frag_target.respawn_flags |= RESPAWN_FORCE;
169 void lms_RemovePlayer(entity player)
171 static int quitters = 0;
172 float player_rank = PlayerScore_Add(player, SP_LMS_RANK, 0);
176 FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
177 if (player.lms_spectate_warning != 2)
179 if(IS_BOT_CLIENT(player))
181 player.frags = FRAGS_LMS_LOSER;
182 PlayerScore_Add(player, SP_LMS_RANK, pl_cnt + 1);
186 lms_lowest_lives = 999;
187 FOREACH_CLIENT(true, {
188 if (it.frags == FRAGS_LMS_LOSER)
190 float it_rank = PlayerScore_Add(it, SP_LMS_RANK, 0);
191 if (it_rank > player_rank && it_rank <= 256)
192 PlayerScore_Add(it, SP_LMS_RANK, -1);
193 lms_lowest_lives = 0;
195 else if (it.frags != FRAGS_SPECTATOR)
197 float tl = PlayerScore_Add(it, SP_LMS_LIVES, 0);
198 if(tl < lms_lowest_lives)
199 lms_lowest_lives = tl;
202 PlayerScore_Add(player, SP_LMS_RANK, 665 - quitters); // different from 666
205 PlayerScore_Add(player, SP_LMS_LIVES, -PlayerScore_Add(player, SP_LMS_LIVES, 0));
208 player.frags = FRAGS_LMS_LOSER;
209 TRANSMUTE(Observer, player);
211 if (pl_cnt == 2 && !warmup_stage) // a player is forfeiting leaving only one player
212 lms_lowest_lives = 0; // end the game now!
215 if(CS(player).killcount != FRAGS_SPECTATOR)
216 if(PlayerScore_Add(player, SP_LMS_RANK, 0) > 0 && player.lms_spectate_warning != 2)
217 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
219 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
222 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
224 entity player = M_ARGV(0, entity);
226 lms_RemovePlayer(player);
229 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
231 entity player = M_ARGV(0, entity);
233 lms_RemovePlayer(player);
234 return true; // prevent team reset
237 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
239 entity player = M_ARGV(0, entity);
241 TRANSMUTE(Player, player);
242 campaign_bots_may_start = true;
244 if(PlayerScore_Add(player, SP_LMS_LIVES, LMS_NewPlayerLives()) <= 0)
246 PlayerScore_Add(player, SP_LMS_RANK, 666); // mark as forced spectator for the hud code
247 player.frags = FRAGS_SPECTATOR;
251 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
253 entity player = M_ARGV(0, entity);
255 if(player.deadflag == DEAD_DYING)
256 player.deadflag = DEAD_RESPAWNING;
259 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
261 if(autocvar_g_lms_regenerate)
266 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
272 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
274 entity frag_target = M_ARGV(1, entity);
279 int tl = PlayerScore_Add(frag_target, SP_LMS_LIVES, -1);
280 if(tl < lms_lowest_lives)
281 lms_lowest_lives = tl;
285 FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
286 if(IS_BOT_CLIENT(frag_target))
287 bot_clear(frag_target);
288 frag_target.frags = FRAGS_LMS_LOSER;
289 PlayerScore_Add(frag_target, SP_LMS_RANK, pl_cnt);
292 M_ARGV(2, float) = 0; // frag score
297 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
299 start_items &= ~IT_UNLIMITED_AMMO;
300 start_health = warmup_start_health = cvar("g_lms_start_health");
301 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
302 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
303 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
304 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
305 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
306 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
307 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
310 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
312 // don't clear player score
316 MUTATOR_HOOKFUNCTION(lms, FilterItem)
318 entity item = M_ARGV(0, entity);
320 if(autocvar_g_lms_extra_lives)
321 if(item.itemdef == ITEM_ExtraLife)
327 void lms_extralife(entity this)
329 StartItem(this, ITEM_ExtraLife);
332 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
334 if (!autocvar_g_powerups) return false;
335 if (!autocvar_g_lms_extra_lives) return false;
337 entity ent = M_ARGV(0, entity);
339 // Can't use .itemdef here
340 if (ent.classname != "item_health_mega") return false;
343 setthink(e, lms_extralife);
345 e.nextthink = time + 0.1;
346 e.spawnflags = ent.spawnflags;
347 e.noalign = ent.noalign;
348 setorigin(e, ent.origin);
353 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
355 entity item = M_ARGV(0, entity);
356 entity toucher = M_ARGV(1, entity);
358 if(item.itemdef == ITEM_ExtraLife)
360 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES);
361 PlayerScore_Add(toucher, SP_LMS_LIVES, autocvar_g_lms_extra_lives);
362 return MUT_ITEMTOUCH_PICKUP;
365 return MUT_ITEMTOUCH_CONTINUE;
368 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
370 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
371 ++M_ARGV(0, int); // activerealplayers
372 ++M_ARGV(1, int); // realplayers
378 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
380 entity player = M_ARGV(0, entity);
382 if(warmup_stage || player.lms_spectate_warning)
384 // for the forfeit message...
385 player.lms_spectate_warning = 2;
389 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_LMS_LOSER)
391 player.lms_spectate_warning = 1;
392 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");
394 return MUT_SPECCMD_RETURN;
396 return MUT_SPECCMD_CONTINUE;
399 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
401 M_ARGV(0, float) = WinningCondition_LMS();
405 MUTATOR_HOOKFUNCTION(lms, WantWeapon)
407 M_ARGV(2, bool) = true; // all weapons
410 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
415 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
418 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
419 return true; // allow writing to this field in intermission as it is needed for newly joining players
423 void lms_ScoreRules()
425 GameRules_score_enabled(false);
426 GameRules_scoring(0, 0, 0, {
427 field(SP_LMS_LIVES, "lives", SFL_SORT_PRIO_SECONDARY);
428 field(SP_LMS_RANK, "rank", SFL_LOWER_IS_BETTER | SFL_RANK | SFL_SORT_PRIO_PRIMARY | SFL_ALLOW_HIDE);
432 void lms_Initialize()
434 lms_lowest_lives = 9999;