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 player.frags = FRAGS_LMS_LOSER;
180 PlayerScore_Add(player, SP_LMS_RANK, pl_cnt + 1);
184 lms_lowest_lives = 999;
185 FOREACH_CLIENT(true, {
186 if (it.frags == FRAGS_LMS_LOSER)
188 float it_rank = PlayerScore_Add(it, SP_LMS_RANK, 0);
189 if (it_rank > player_rank && it_rank <= 256)
190 PlayerScore_Add(it, SP_LMS_RANK, -1);
191 lms_lowest_lives = 0;
193 else if (it.frags != FRAGS_SPECTATOR)
195 float tl = PlayerScore_Add(it, SP_LMS_LIVES, 0);
196 if(tl < lms_lowest_lives)
197 lms_lowest_lives = tl;
200 PlayerScore_Add(player, SP_LMS_RANK, 665 - quitters); // different from 666
203 PlayerScore_Add(player, SP_LMS_LIVES, -PlayerScore_Add(player, SP_LMS_LIVES, 0));
206 player.frags = FRAGS_LMS_LOSER;
207 TRANSMUTE(Observer, player);
209 if (pl_cnt == 2 && !warmup_stage) // a player is forfeiting leaving only one player
210 lms_lowest_lives = 0; // end the game now!
213 if(player.killcount != FRAGS_SPECTATOR)
214 if(PlayerScore_Add(player, SP_LMS_RANK, 0) > 0 && player.lms_spectate_warning != 2)
215 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
217 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
220 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
222 entity player = M_ARGV(0, entity);
224 lms_RemovePlayer(player);
227 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
229 entity player = M_ARGV(0, entity);
231 lms_RemovePlayer(player);
232 return true; // prevent team reset
235 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
237 entity player = M_ARGV(0, entity);
239 TRANSMUTE(Player, player);
240 campaign_bots_may_start = true;
242 if(PlayerScore_Add(player, SP_LMS_LIVES, LMS_NewPlayerLives()) <= 0)
244 PlayerScore_Add(player, SP_LMS_RANK, 666); // mark as forced spectator for the hud code
245 player.frags = FRAGS_SPECTATOR;
249 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
251 entity player = M_ARGV(0, entity);
253 if(player.deadflag == DEAD_DYING)
254 player.deadflag = DEAD_RESPAWNING;
257 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
259 if(autocvar_g_lms_regenerate)
264 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
270 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
272 entity frag_target = M_ARGV(1, entity);
277 int tl = PlayerScore_Add(frag_target, SP_LMS_LIVES, -1);
278 if(tl < lms_lowest_lives)
279 lms_lowest_lives = tl;
283 FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
284 frag_target.frags = FRAGS_LMS_LOSER;
285 PlayerScore_Add(frag_target, SP_LMS_RANK, pl_cnt);
288 M_ARGV(2, float) = 0; // frag score
293 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
295 start_items &= ~IT_UNLIMITED_AMMO;
296 start_health = warmup_start_health = cvar("g_lms_start_health");
297 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
298 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
299 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
300 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
301 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
302 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
303 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
306 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
308 // don't clear player score
312 MUTATOR_HOOKFUNCTION(lms, FilterItem)
314 entity item = M_ARGV(0, entity);
316 if(autocvar_g_lms_extra_lives)
317 if(item.itemdef == ITEM_ExtraLife)
323 void lms_extralife(entity this)
325 StartItem(this, ITEM_ExtraLife);
328 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
330 if (!autocvar_g_powerups) return false;
331 if (!autocvar_g_lms_extra_lives) return false;
333 entity ent = M_ARGV(0, entity);
335 // Can't use .itemdef here
336 if (ent.classname != "item_health_mega") return false;
339 setthink(e, lms_extralife);
341 e.nextthink = time + 0.1;
342 e.spawnflags = ent.spawnflags;
343 e.noalign = ent.noalign;
344 setorigin(e, ent.origin);
349 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
351 entity item = M_ARGV(0, entity);
352 entity toucher = M_ARGV(1, entity);
354 if(item.itemdef == ITEM_ExtraLife)
356 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES);
357 PlayerScore_Add(toucher, SP_LMS_LIVES, autocvar_g_lms_extra_lives);
358 return MUT_ITEMTOUCH_PICKUP;
361 return MUT_ITEMTOUCH_CONTINUE;
364 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
366 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
367 ++M_ARGV(0, int); // activerealplayers
368 ++M_ARGV(1, int); // realplayers
374 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
376 entity player = M_ARGV(0, entity);
378 if(warmup_stage || player.lms_spectate_warning)
380 // for the forfeit message...
381 player.lms_spectate_warning = 2;
385 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_LMS_LOSER)
387 player.lms_spectate_warning = 1;
388 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");
390 return MUT_SPECCMD_RETURN;
392 return MUT_SPECCMD_CONTINUE;
395 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
397 M_ARGV(0, float) = WinningCondition_LMS();
401 MUTATOR_HOOKFUNCTION(lms, WantWeapon)
403 M_ARGV(2, bool) = true; // all weapons
406 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
411 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
414 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
415 return true; // allow writing to this field in intermission as it is needed for newly joining players
419 void lms_ScoreRules()
421 ScoreRules_basics(0, 0, 0, false);
422 ScoreInfo_SetLabel_PlayerScore(SP_LMS_LIVES, "lives", SFL_SORT_PRIO_SECONDARY);
423 ScoreInfo_SetLabel_PlayerScore(SP_LMS_RANK, "rank", SFL_LOWER_IS_BETTER | SFL_RANK | SFL_SORT_PRIO_PRIMARY | SFL_ALLOW_HIDE);
424 ScoreRules_basics_end();
427 void lms_Initialize()
429 lms_lowest_lives = 9999;