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