1 #include "gamemode_lms.qh"
5 #define autocvar_g_lms_lives_override cvar("g_lms_lives_override")
8 REGISTER_MUTATOR(lms, false)
12 if (time > 1) // game loads at time 1
13 error("This is a game type and it cannot be added at runtime.");
16 SetLimits(((!autocvar_g_lms_lives_override) ? -1 : autocvar_g_lms_lives_override), 0, autocvar_timelimit_override, -1);
19 MUTATOR_ONROLLBACK_OR_REMOVE
21 // we actually cannot roll back lms_Initialize here
22 // BUT: we don't need to! If this gets called, adding always
28 LOG_INFO("This is a game type and it cannot be removed at runtime.");
36 const float SP_LMS_LIVES = 4;
37 const float SP_LMS_RANK = 5;
40 float lms_lowest_lives;
42 float LMS_NewPlayerLives();
48 #include <common/mutators/mutator/instagib/items.qc>
49 #include <server/campaign.qh>
50 #include <server/command/cmd.qh>
52 int autocvar_g_lms_extra_lives;
53 bool autocvar_g_lms_join_anytime;
54 int autocvar_g_lms_last_join;
55 bool autocvar_g_lms_regenerate;
58 float LMS_NewPlayerLives()
61 fl = autocvar_fraglimit;
65 // first player has left the game for dying too much? Nobody else can get in.
66 if(lms_lowest_lives < 1)
69 if(!autocvar_g_lms_join_anytime)
70 if(lms_lowest_lives < fl - autocvar_g_lms_last_join)
73 return bound(1, lms_lowest_lives, fl);
78 // LMS winning condition: game terminates if and only if there's at most one
79 // one player who's living lives. Top two scores being equal cancels the time
81 int WinningCondition_LMS()
84 bool have_player = false;
85 bool have_players = false;
87 int l = LMS_NewPlayerLives();
89 head = find(world, classname, STR_PLAYER);
92 head2 = find(head, classname, STR_PLAYER);
98 // we have at least one player
101 // two or more active players - continue with the game
105 // exactly one player?
108 SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
112 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
118 // and assign him his first place
119 PlayerScore_Add(head, SP_LMS_RANK, 1);
126 // nobody is playing at all...
129 // wait for players...
133 // SNAFU (maybe a draw game?)
135 LOG_TRACE("No players, ending game.\n");
140 // When we get here, we have at least two players who are actually LIVING,
141 // now check if the top two players have equal score.
142 WinningConditionHelper();
145 if(WinningConditionHelper_winner)
146 WinningConditionHelper_winner.winning = true;
147 if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
148 return WINNING_NEVER;
150 // Top two have different scores? Way to go for our beloved TIMELIMIT!
155 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
157 lms_lowest_lives = 999;
158 lms_next_place = player_count;
163 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
165 if(restart_mapalreadyrestarted || (time < game_starttime))
166 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(PlayerScore_Add(it, SP_LMS_LIVES, LMS_NewPlayerLives())));
170 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
172 entity player = M_ARGV(0, entity);
174 // player is dead and becomes observer
175 // FIXME fix LMS scoring for new system
176 if(PlayerScore_Add(player, SP_LMS_RANK, 0) > 0)
178 TRANSMUTE(Observer, player);
179 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
183 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
185 entity frag_target = M_ARGV(2, entity);
187 frag_target.respawn_flags |= RESPAWN_FORCE;
191 void lms_RemovePlayer(entity player)
193 // Only if the player cannot play at all
194 if(PlayerScore_Add(player, SP_LMS_RANK, 0) == 666)
195 player.frags = FRAGS_SPECTATOR;
197 player.frags = FRAGS_LMS_LOSER;
199 if(player.killcount != FRAGS_SPECTATOR)
200 if(PlayerScore_Add(player, SP_LMS_RANK, 0) > 0 && player.lms_spectate_warning != 2)
201 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
203 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
206 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
208 entity player = M_ARGV(0, entity);
210 lms_RemovePlayer(player);
213 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
215 entity player = M_ARGV(0, entity);
217 lms_RemovePlayer(player);
218 return true; // prevent team reset
221 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
223 entity player = M_ARGV(0, entity);
225 TRANSMUTE(Player, player);
226 campaign_bots_may_start = true;
228 if(PlayerScore_Add(player, SP_LMS_LIVES, LMS_NewPlayerLives()) <= 0)
230 PlayerScore_Add(player, SP_LMS_RANK, 666);
231 player.frags = FRAGS_SPECTATOR;
235 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
237 entity player = M_ARGV(0, entity);
239 if(player.deadflag == DEAD_DYING)
240 player.deadflag = DEAD_RESPAWNING;
243 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
245 if(autocvar_g_lms_regenerate)
250 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
256 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
258 entity frag_target = M_ARGV(1, entity);
262 tl = PlayerScore_Add(frag_target, SP_LMS_LIVES, -1);
263 if(tl < lms_lowest_lives)
264 lms_lowest_lives = tl;
268 lms_next_place = player_count;
270 lms_next_place = min(lms_next_place, player_count);
271 PlayerScore_Add(frag_target, SP_LMS_RANK, lms_next_place); // won't ever spawn again
274 M_ARGV(2, float) = 0;
279 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
281 start_items &= ~IT_UNLIMITED_AMMO;
282 start_health = warmup_start_health = cvar("g_lms_start_health");
283 start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
284 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
285 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
286 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
287 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
288 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
289 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
294 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
296 // don't clear player score
300 MUTATOR_HOOKFUNCTION(lms, FilterItem)
302 entity item = M_ARGV(0, entity);
304 if(autocvar_g_lms_extra_lives)
305 if(item.itemdef == ITEM_ExtraLife)
311 void lms_extralife(entity this)
313 StartItem(this, ITEM_ExtraLife);
316 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
318 if (!autocvar_g_powerups) return false;
319 if (!autocvar_g_lms_extra_lives) return false;
321 entity ent = M_ARGV(0, entity);
323 // Can't use .itemdef here
324 if (ent.classname != "item_health_mega") return false;
327 setthink(e, lms_extralife);
329 e.nextthink = time + 0.1;
330 e.spawnflags = ent.spawnflags;
331 e.noalign = ent.noalign;
332 setorigin(e, ent.origin);
337 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
339 entity item = M_ARGV(0, entity);
340 entity toucher = M_ARGV(1, entity);
342 if(item.itemdef == ITEM_ExtraLife)
344 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES);
345 PlayerScore_Add(toucher, SP_LMS_LIVES, autocvar_g_lms_extra_lives);
346 return MUT_ITEMTOUCH_PICKUP;
349 return MUT_ITEMTOUCH_CONTINUE;
352 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
354 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
362 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
364 entity player = M_ARGV(0, entity);
366 if(player.lms_spectate_warning)
368 // for the forfeit message...
369 player.lms_spectate_warning = 2;
370 // mark player as spectator
371 PlayerScore_Add(player, SP_LMS_RANK, 666 - PlayerScore_Add(player, SP_LMS_RANK, 0));
375 player.lms_spectate_warning = 1;
376 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");
377 return MUT_SPECCMD_RETURN;
379 return MUT_SPECCMD_CONTINUE;
382 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
384 M_ARGV(0, float) = WinningCondition_LMS();
388 MUTATOR_HOOKFUNCTION(lms, WantWeapon)
390 M_ARGV(2, bool) = true; // all weapons
393 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
398 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
401 if(M_ARGV(0, int) == SP_LMS_RANK) // score field
402 return true; // allow writing to this field in intermission as it is needed for newly joining players
407 void lms_ScoreRules()
409 ScoreRules_basics(0, 0, 0, false);
410 ScoreInfo_SetLabel_PlayerScore(SP_LMS_LIVES, "lives", SFL_SORT_PRIO_SECONDARY);
411 ScoreInfo_SetLabel_PlayerScore(SP_LMS_RANK, "rank", SFL_LOWER_IS_BETTER | SFL_RANK | SFL_SORT_PRIO_PRIMARY | SFL_ALLOW_HIDE);
412 ScoreRules_basics_end();
415 void lms_Initialize()
417 lms_lowest_lives = 9999;