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