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