]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/lms/sv_lms.qc
LMS: forcedly turn sv_ready_restart_after_countdown off as it breaks the game mode...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / lms / sv_lms.qc
1 #include "sv_lms.qh"
2
3 #include <common/mutators/mutator/instagib/items.qh>
4 #include <server/campaign.qh>
5 #include <server/command/_mod.qh>
6
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;
11
12 // main functions
13 float LMS_NewPlayerLives()
14 {
15         float fl;
16         fl = autocvar_fraglimit;
17         if(fl == 0)
18                 fl = 999;
19
20         // first player has left the game for dying too much? Nobody else can get in.
21         if(lms_lowest_lives < 1)
22                 return 0;
23
24         if(!autocvar_g_lms_join_anytime)
25                 if(lms_lowest_lives < fl - autocvar_g_lms_last_join)
26                         return 0;
27
28         return bound(1, lms_lowest_lives, fl);
29 }
30
31 void ClearWinners();
32
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
35 // limit.
36 int WinningCondition_LMS()
37 {
38         entity first_player = NULL;
39         int totalplayers = 0;
40         FOREACH_CLIENT(IS_PLAYER(it), {
41                 if (!totalplayers)
42                         first_player = it;
43                 ++totalplayers;
44         });
45
46         if (totalplayers)
47         {
48                 if (totalplayers > 1)
49                 {
50                         // two or more active players - continue with the game
51
52                         if (autocvar_g_campaign)
53                         {
54                                 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
55                                         float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
56                                         if (!pl_lives)
57                                                 return WINNING_YES; // human player lost, game over
58                                         break;
59                                 });
60                         }
61                 }
62                 else
63                 {
64                         // exactly one player?
65
66                         ClearWinners();
67                         SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
68
69                         if (LMS_NewPlayerLives())
70                         {
71                                 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
72                                 return WINNING_NO;
73                         }
74                         else
75                         {
76                                 // a winner!
77                                 // and assign him his first place
78                                 GameRules_scoring_add(first_player, LMS_RANK, 1);
79                                 if(warmup_stage)
80                                         return WINNING_NO;
81                                 else
82                                         return WINNING_YES;
83                         }
84                 }
85         }
86         else
87         {
88                 // nobody is playing at all...
89                 if (LMS_NewPlayerLives())
90                 {
91                         // wait for players...
92                 }
93                 else
94                 {
95                         // SNAFU (maybe a draw game?)
96                         ClearWinners();
97                         LOG_TRACE("No players, ending game.");
98                         return WINNING_YES;
99                 }
100         }
101
102         // When we get here, we have at least two players who are actually LIVING,
103         // now check if the top two players have equal score.
104         WinningConditionHelper(NULL);
105
106         ClearWinners();
107         if(WinningConditionHelper_winner)
108                 WinningConditionHelper_winner.winning = true;
109         if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
110                 return WINNING_NEVER;
111
112         // Top two have different scores? Way to go for our beloved TIMELIMIT!
113         return WINNING_NO;
114 }
115
116 // mutator hooks
117 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
118 {
119         lms_lowest_lives = 999;
120 }
121
122 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
123 {
124         FOREACH_CLIENT(true, {
125                 TRANSMUTE(Player, it);
126                 it.frags = FRAGS_PLAYER;
127                 GameRules_scoring_add(it, LMS_LIVES, LMS_NewPlayerLives());
128                 PutClientInServer(it);
129         });
130 }
131
132 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
133 {
134         // incompatible
135         sv_ready_restart_after_countdown = 0;
136 }
137
138 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
139 {
140         entity player = M_ARGV(0, entity);
141
142         if(player.frags == FRAGS_SPECTATOR)
143                 TRANSMUTE(Observer, player);
144         else
145         {
146                 float tl = GameRules_scoring_add(player, LMS_LIVES, 0);
147                 if(tl < lms_lowest_lives)
148                         lms_lowest_lives = tl;
149                 if(tl <= 0)
150                         TRANSMUTE(Observer, player);
151                 if(warmup_stage)
152                         GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
153         }
154 }
155
156 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
157 {
158         entity player = M_ARGV(0, entity);
159
160         if(warmup_stage)
161                 return false;
162         if(player.frags == FRAGS_SPECTATOR)
163                 return true;
164         if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
165         {
166                 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
167                 return true;
168         }
169         return false;
170 }
171
172 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
173 {
174         entity frag_target = M_ARGV(2, entity);
175
176         frag_target.respawn_flags |= RESPAWN_FORCE;
177 }
178
179 void lms_RemovePlayer(entity player)
180 {
181         static int quitters = 0;
182         float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
183         if (!player_rank)
184         {
185                 int pl_cnt = 0;
186                 FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
187                 if (player.lms_spectate_warning != 2)
188                 {
189                         if(IS_BOT_CLIENT(player))
190                                 bot_clear(player);
191                         player.frags = FRAGS_LMS_LOSER;
192                         GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
193                 }
194                 else
195                 {
196                         lms_lowest_lives = 999;
197                         FOREACH_CLIENT(true, {
198                                 if (it.frags == FRAGS_LMS_LOSER)
199                                 {
200                                         float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
201                                         if (it_rank > player_rank && it_rank <= 256)
202                                                 GameRules_scoring_add(it, LMS_RANK, -1);
203                                         lms_lowest_lives = 0;
204                                 }
205                                 else if (it.frags != FRAGS_SPECTATOR)
206                                 {
207                                         float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
208                                         if(tl < lms_lowest_lives)
209                                                 lms_lowest_lives = tl;
210                                 }
211                         });
212                         GameRules_scoring_add(player, LMS_RANK, 665 - quitters); // different from 666
213                         if(!warmup_stage)
214                         {
215                                 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
216                                 ++quitters;
217                         }
218                         player.frags = FRAGS_LMS_LOSER;
219                         TRANSMUTE(Observer, player);
220                 }
221                 if (pl_cnt == 2 && !warmup_stage) // a player is forfeiting leaving only one player
222                         lms_lowest_lives = 0; // end the game now!
223         }
224
225         if(CS(player).killcount != FRAGS_SPECTATOR)
226                 if(GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning != 2)
227                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
228                 else
229                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
230 }
231
232 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
233 {
234         entity player = M_ARGV(0, entity);
235
236         lms_RemovePlayer(player);
237 }
238
239 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
240 {
241         entity player = M_ARGV(0, entity);
242
243         if (!IS_PLAYER(player))
244                 return true;
245
246         lms_RemovePlayer(player);
247         return true;  // prevent team reset
248 }
249
250 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
251 {
252         entity player = M_ARGV(0, entity);
253
254         if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
255         {
256                 GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
257                 player.frags = FRAGS_SPECTATOR;
258         }
259 }
260
261 MUTATOR_HOOKFUNCTION(lms, AutoJoinOnConnection)
262 {
263         if(autocvar_g_campaign)
264                 return false;
265         return true;
266 }
267
268 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
269 {
270         entity player = M_ARGV(0, entity);
271
272         if(player.deadflag == DEAD_DYING)
273                 player.deadflag = DEAD_RESPAWNING;
274 }
275
276 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
277 {
278         if(autocvar_g_lms_regenerate)
279                 return false;
280         return true;
281 }
282
283 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
284 {
285         // forbode!
286         return true;
287 }
288
289 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
290 {
291         entity frag_target = M_ARGV(1, entity);
292
293         if (!warmup_stage)
294         {
295                 // remove a life
296                 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
297                 if(tl < lms_lowest_lives)
298                         lms_lowest_lives = tl;
299                 if(tl <= 0)
300                 {
301                         int pl_cnt = 0;
302                         FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
303                         if(IS_BOT_CLIENT(frag_target))
304                                 bot_clear(frag_target);
305                         frag_target.frags = FRAGS_LMS_LOSER;
306                         GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
307                 }
308         }
309         M_ARGV(2, float) = 0; // frag score
310
311         return true;
312 }
313
314 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
315 {
316         start_items &= ~IT_UNLIMITED_AMMO;
317         start_health       = warmup_start_health       = cvar("g_lms_start_health");
318         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
319         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
320         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
321         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
322         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
323         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
324         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
325 }
326
327 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
328 {
329         // don't clear player score
330         return true;
331 }
332
333 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
334 {
335         entity definition = M_ARGV(0, entity);
336
337         if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
338         {
339                 return false;
340         }
341         return true;
342 }
343
344 void lms_extralife(entity this)
345 {
346         StartItem(this, ITEM_ExtraLife);
347 }
348
349 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
350 {
351         if (!autocvar_g_powerups) return false;
352         if (!autocvar_g_lms_extra_lives) return false;
353
354         entity ent = M_ARGV(0, entity);
355
356         // Can't use .itemdef here
357         if (ent.classname != "item_health_mega") return false;
358
359         entity e = spawn();
360         setthink(e, lms_extralife);
361
362         e.nextthink = time + 0.1;
363         e.spawnflags = ent.spawnflags;
364         e.noalign = ent.noalign;
365         setorigin(e, ent.origin);
366
367         return true;
368 }
369
370 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
371 {
372         entity item = M_ARGV(0, entity);
373         entity toucher = M_ARGV(1, entity);
374
375         if(item.itemdef == ITEM_ExtraLife)
376         {
377                 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES);
378                 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
379                 return MUT_ITEMTOUCH_PICKUP;
380         }
381
382         return MUT_ITEMTOUCH_CONTINUE;
383 }
384
385 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
386 {
387         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
388                 ++M_ARGV(0, int); // activerealplayers
389                 ++M_ARGV(1, int); // realplayers
390         });
391
392         return true;
393 }
394
395 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
396 {
397         entity player = M_ARGV(0, entity);
398
399         if(warmup_stage || player.lms_spectate_warning)
400         {
401                 // for the forfeit message...
402                 player.lms_spectate_warning = 2;
403         }
404         else
405         {
406                 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_LMS_LOSER)
407                 {
408                         player.lms_spectate_warning = 1;
409                         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");
410                 }
411                 return MUT_SPECCMD_RETURN;
412         }
413         return MUT_SPECCMD_CONTINUE;
414 }
415
416 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
417 {
418         M_ARGV(0, float) = WinningCondition_LMS();
419         return true;
420 }
421
422 MUTATOR_HOOKFUNCTION(lms, WantWeapon)
423 {
424         M_ARGV(2, bool) = true; // all weapons
425 }
426
427 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
428 {
429         return true;
430 }
431
432 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
433 {
434         if(game_stopped)
435         if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
436                 return true; // allow writing to this field in intermission as it is needed for newly joining players
437 }
438
439 void lms_Initialize()
440 {
441         lms_lowest_lives = 9999;
442 }