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);
81 // nobody is playing at all...
84 // wait for players...
88 // SNAFU (maybe a draw game?)
90 LOG_TRACE("No players, ending game.");
95 // When we get here, we have at least two players who are actually LIVING,
96 // now check if the top two players have equal score.
97 WinningConditionHelper(NULL);
100 if(WinningConditionHelper_winner)
101 WinningConditionHelper_winner.winning = true;
102 if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
103 return WINNING_NEVER;
105 // Top two have different scores? Way to go for our beloved TIMELIMIT!
110 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
112 lms_lowest_lives = 999;
113 lms_next_place = player_count;
116 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
118 if(restart_mapalreadyrestarted || (time < game_starttime))
119 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(PlayerScore_Add(it, SP_LMS_LIVES, LMS_NewPlayerLives())));
122 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
124 entity player = M_ARGV(0, entity);
126 // player is dead and becomes observer
127 // FIXME fix LMS scoring for new system
128 if(PlayerScore_Add(player, SP_LMS_RANK, 0) > 0)
130 TRANSMUTE(Observer, player);
131 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
135 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
137 entity frag_target = M_ARGV(2, entity);
139 frag_target.respawn_flags |= RESPAWN_FORCE;
142 void lms_RemovePlayer(entity player)
144 // Only if the player cannot play at all
145 if(PlayerScore_Add(player, SP_LMS_RANK, 0) == 666)
146 player.frags = FRAGS_SPECTATOR;
148 player.frags = FRAGS_LMS_LOSER;
150 if(player.killcount != FRAGS_SPECTATOR)
151 if(PlayerScore_Add(player, SP_LMS_RANK, 0) > 0 && player.lms_spectate_warning != 2)
152 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
154 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
157 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
159 entity player = M_ARGV(0, entity);
161 lms_RemovePlayer(player);
164 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
166 entity player = M_ARGV(0, entity);
168 lms_RemovePlayer(player);
169 return true; // prevent team reset
172 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
174 entity player = M_ARGV(0, entity);
176 TRANSMUTE(Player, player);
177 campaign_bots_may_start = true;
179 if(PlayerScore_Add(player, SP_LMS_LIVES, LMS_NewPlayerLives()) <= 0)
181 PlayerScore_Add(player, SP_LMS_RANK, 666);
182 player.frags = FRAGS_SPECTATOR;
186 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
188 entity player = M_ARGV(0, entity);
190 if(player.deadflag == DEAD_DYING)
191 player.deadflag = DEAD_RESPAWNING;
194 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
196 if(autocvar_g_lms_regenerate)
201 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
207 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
209 entity frag_target = M_ARGV(1, entity);
213 tl = PlayerScore_Add(frag_target, SP_LMS_LIVES, -1);
214 if(tl < lms_lowest_lives)
215 lms_lowest_lives = tl;
219 lms_next_place = player_count;
221 lms_next_place = min(lms_next_place, player_count);
222 PlayerScore_Add(frag_target, SP_LMS_RANK, lms_next_place); // won't ever spawn again
225 M_ARGV(2, float) = 0;
230 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
232 start_items &= ~IT_UNLIMITED_AMMO;
233 start_health = warmup_start_health = cvar("g_lms_start_health");
234 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
235 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
236 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
237 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
238 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
239 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
240 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
243 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
245 // don't clear player score
249 MUTATOR_HOOKFUNCTION(lms, FilterItem)
251 entity item = M_ARGV(0, entity);
253 if(autocvar_g_lms_extra_lives)
254 if(item.itemdef == ITEM_ExtraLife)
260 void lms_extralife(entity this)
262 StartItem(this, ITEM_ExtraLife);
265 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
267 if (!autocvar_g_powerups) return false;
268 if (!autocvar_g_lms_extra_lives) return false;
270 entity ent = M_ARGV(0, entity);
272 // Can't use .itemdef here
273 if (ent.classname != "item_health_mega") return false;
276 setthink(e, lms_extralife);
278 e.nextthink = time + 0.1;
279 e.spawnflags = ent.spawnflags;
280 e.noalign = ent.noalign;
281 setorigin(e, ent.origin);
286 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
288 entity item = M_ARGV(0, entity);
289 entity toucher = M_ARGV(1, entity);
291 if(item.itemdef == ITEM_ExtraLife)
293 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES);
294 PlayerScore_Add(toucher, SP_LMS_LIVES, autocvar_g_lms_extra_lives);
295 return MUT_ITEMTOUCH_PICKUP;
298 return MUT_ITEMTOUCH_CONTINUE;
301 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
303 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
311 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
313 entity player = M_ARGV(0, entity);
315 if(player.lms_spectate_warning)
317 // for the forfeit message...
318 player.lms_spectate_warning = 2;
319 // mark player as spectator
320 PlayerScore_Add(player, SP_LMS_RANK, 666 - PlayerScore_Add(player, SP_LMS_RANK, 0));
324 player.lms_spectate_warning = 1;
325 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");
326 return MUT_SPECCMD_RETURN;
328 return MUT_SPECCMD_CONTINUE;
331 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
333 M_ARGV(0, float) = WinningCondition_LMS();
337 MUTATOR_HOOKFUNCTION(lms, WantWeapon)
339 M_ARGV(2, bool) = true; // all weapons
342 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
347 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
350 if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
351 return true; // allow writing to this field in intermission as it is needed for newly joining players
355 void lms_ScoreRules()
357 ScoreRules_basics(0, 0, 0, false);
358 ScoreInfo_SetLabel_PlayerScore(SP_LMS_LIVES, "lives", SFL_SORT_PRIO_SECONDARY);
359 ScoreInfo_SetLabel_PlayerScore(SP_LMS_RANK, "rank", SFL_LOWER_IS_BETTER | SFL_RANK | SFL_SORT_PRIO_PRIMARY | SFL_ALLOW_HIDE);
360 ScoreRules_basics_end();
363 void lms_Initialize()
365 lms_lowest_lives = 9999;