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