]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/lms/sv_lms.qc
43bf50621e85882ede0ad34976d999df6859757b
[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                 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 = 2; // temp value indicating player has just joined the game (but not spawned yet)
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         if (player.lmsplayer == 2) // just joined the game
208         {
209                 // spawn player with the same amount of health / armor
210                 // as the least healthy player with the least number of lives
211                 int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
212                 float min_health = start_health;
213                 float min_armorvalue = start_armorvalue;
214                 FOREACH_CLIENT(it != player && IS_PLAYER(it) && !IS_DEAD(it) && GameRules_scoring_add(it, LMS_LIVES, 0) == pl_lives, {
215                         if (GetResource(it, RES_HEALTH) < min_health)
216                                 min_health = GetResource(it, RES_HEALTH);
217                         if (GetResource(it, RES_ARMOR) < min_armorvalue)
218                                 min_armorvalue = GetResource(it, RES_ARMOR);
219                 });
220                 if (min_health != start_health)
221                         SetResource(player, RES_HEALTH, max(1, min_health));
222                 if (min_armorvalue != start_armorvalue)
223                         SetResource(player, RES_ARMOR, min_armorvalue);
224                 player.lmsplayer = 1;
225         }
226 }
227
228 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
229 {
230         entity player = M_ARGV(0, entity);
231
232         if (warmup_stage || lms_AddPlayer(player))
233                 return false;
234
235         return true;
236 }
237
238 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
239 {
240         entity frag_target = M_ARGV(2, entity);
241
242         float tl = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
243         if (tl <= 0)
244         {
245                 frag_target.respawn_flags = RESPAWN_SILENT;
246                 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
247                 frag_target.respawn_time = time + 2;
248         }
249         frag_target.respawn_flags |= RESPAWN_FORCE;
250 }
251
252 void lms_RemovePlayer(entity player)
253 {
254         if (warmup_stage || time < game_starttime)
255                 return;
256
257         float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
258         if (!player_rank)
259         {
260                 if (player.lms_spectate_warning < 2)
261                 {
262                         player.frags = FRAGS_PLAYER_OUT_OF_GAME;
263                         int pl_cnt = 0;
264                         FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
265                                 pl_cnt++;
266                         });
267                         GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
268                 }
269                 else
270                 {
271                         int min_forfeiter_rank = 665; // different from 666
272                         FOREACH_CLIENT(true, {
273                                 // update rank of other players that were eliminated
274                                 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
275                                 {
276                                         float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
277                                         if (it_rank > player_rank && it_rank <= 256)
278                                                 GameRules_scoring_add(it, LMS_RANK, -1);
279                                         if (it_rank > 256 && it_rank <= min_forfeiter_rank)
280                                                 min_forfeiter_rank = it_rank - 1;
281                                 }
282                                 else if (it.frags != FRAGS_SPECTATOR)
283                                 {
284                                         float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
285                                         if(tl < lms_lowest_lives)
286                                                 lms_lowest_lives = tl;
287                                 }
288                         });
289                         GameRules_scoring_add(player, LMS_RANK, min_forfeiter_rank);
290                         if(!warmup_stage)
291                                 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
292                         player.frags = FRAGS_PLAYER_OUT_OF_GAME;
293                         TRANSMUTE(Observer, player);
294                 }
295         }
296
297         if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
298         {
299                 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
300                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
301                 else
302                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
303         }
304 }
305
306 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
307 {
308         entity player = M_ARGV(0, entity);
309
310         // no further message other than the disconnect message
311         player.lms_spectate_warning = 3;
312
313         lms_RemovePlayer(player);
314         player.lmsplayer = 0;
315 }
316
317 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
318 {
319         entity player = M_ARGV(0, entity);
320         bool is_forced = M_ARGV(1, bool);
321
322         if (!IS_PLAYER(player))
323                 return true;
324
325         if (warmup_stage || time <= game_starttime)
326         {
327                 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
328                 player.frags = FRAGS_SPECTATOR;
329                 TRANSMUTE(Observer, player);
330                 player.lmsplayer = 0;
331         }
332         else
333         {
334                 if (is_forced)
335                         player.lms_spectate_warning = 2;
336                 if (!GameRules_scoring_add(player, LMS_RANK, 0))
337                         lms_RemovePlayer(player);
338         }
339         return true;  // prevent team reset
340 }
341
342 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
343 {
344         entity player = M_ARGV(0, entity);
345         player.frags = FRAGS_SPECTATOR;
346 }
347
348 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
349 {
350         entity player = M_ARGV(0, entity);
351
352         if(player.deadflag == DEAD_DYING)
353                 player.deadflag = DEAD_RESPAWNING;
354 }
355
356 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
357 {
358         if(autocvar_g_lms_regenerate)
359                 return false;
360         return true;
361 }
362
363 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
364 {
365         // forbode!
366         return true;
367 }
368
369 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
370 {
371         entity frag_target = M_ARGV(1, entity);
372
373         if (!warmup_stage && time > game_starttime)
374         {
375                 // remove a life
376                 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
377                 if(tl < lms_lowest_lives)
378                         lms_lowest_lives = tl;
379                 if(tl <= 0)
380                 {
381                         int pl_cnt = 0;
382                         FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
383                                 pl_cnt++;
384                         });
385                         frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
386                         GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
387                 }
388         }
389         M_ARGV(2, float) = 0; // frag score
390
391         return true;
392 }
393
394 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
395 {
396         start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
397         if(!cvar("g_use_ammunition"))
398                 start_items |= IT_UNLIMITED_AMMO;
399
400         start_health       = warmup_start_health       = cvar("g_lms_start_health");
401         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
402         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
403         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
404         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
405         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
406         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
407         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
408 }
409
410 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
411 {
412         // don't clear player score
413         return true;
414 }
415
416 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
417 {
418         if (autocvar_g_lms_items)
419                 return false;
420
421         entity definition = M_ARGV(0, entity);
422
423         if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
424         {
425                 return false;
426         }
427         return (autocvar_g_pickup_items <= 0); // only allow items if explicitly enabled
428 }
429
430 void lms_extralife(entity this)
431 {
432         StartItem(this, ITEM_ExtraLife);
433 }
434
435 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
436 {
437         if (MUTATOR_RETURNVALUE) return false;
438         if (!autocvar_g_powerups) return false;
439         if (!autocvar_g_lms_extra_lives) return false;
440
441         entity ent = M_ARGV(0, entity);
442
443         // Can't use .itemdef here
444         if (ent.classname != "item_health_mega") return false;
445
446         entity e = spawn();
447         setthink(e, lms_extralife);
448
449         e.nextthink = time + 0.1;
450         e.spawnflags = ent.spawnflags;
451         e.noalign = ent.noalign;
452         setorigin(e, ent.origin);
453
454         return true;
455 }
456
457 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
458 {
459         if(MUTATOR_RETURNVALUE) return false;
460
461         entity item = M_ARGV(0, entity);
462         entity toucher = M_ARGV(1, entity);
463
464         if(item.itemdef == ITEM_ExtraLife)
465         {
466                 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
467                 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
468                 return MUT_ITEMTOUCH_PICKUP;
469         }
470
471         return MUT_ITEMTOUCH_CONTINUE;
472 }
473
474 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
475 {
476         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
477                 if (it.lmsplayer && it.lms_spectate_warning < 2)
478                         ++M_ARGV(0, int); // activerealplayers
479                 ++M_ARGV(1, int); // realplayers
480         });
481
482         return true;
483 }
484
485 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
486 {
487         entity player = M_ARGV(0, entity);
488
489         if(warmup_stage || time < game_starttime || player.lms_spectate_warning)
490         {
491                 // for the forfeit message...
492                 player.lms_spectate_warning = 2;
493         }
494         else
495         {
496                 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
497                 {
498                         player.lms_spectate_warning = 1;
499                         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");
500                 }
501                 return MUT_SPECCMD_RETURN;
502         }
503         return MUT_SPECCMD_CONTINUE;
504 }
505
506 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
507 {
508         M_ARGV(0, float) = WinningCondition_LMS();
509         return true;
510 }
511
512 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
513 {
514         if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
515                 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
516 }
517
518 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
519 {
520         entity player = M_ARGV(0, entity);
521
522         return boolean(player.lmsplayer);
523 }
524
525 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
526 {
527         if(game_stopped)
528         if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
529                 return true; // allow writing to this field in intermission as it is needed for newly joining players
530 }
531
532 void lms_Initialize()
533 {
534         lms_lowest_lives = 999;
535 }