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