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