]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/lms/sv_lms.qc
Merge remote-tracking branch 'origin/master' into Juhu/strafehud
[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 || GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
164         {
165                 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
166                 return true;
167         }
168         return false;
169 }
170
171 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
172 {
173         entity frag_target = M_ARGV(2, entity);
174
175         frag_target.respawn_flags |= RESPAWN_FORCE;
176 }
177
178 void lms_RemovePlayer(entity player)
179 {
180         static int quitters = 0;
181         float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
182         if (!player_rank)
183         {
184                 if (player.lms_spectate_warning < 2)
185                 {
186                         if(IS_BOT_CLIENT(player))
187                                 bot_clear(player);
188                         player.frags = FRAGS_PLAYER_OUT_OF_GAME;
189                         int pl_cnt = 0;
190                         FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
191                                 pl_cnt++;
192                         });
193                         GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
194                 }
195                 else
196                 {
197                         FOREACH_CLIENT(true, {
198                                 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
199                                 {
200                                         float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
201                                         if (it_rank > player_rank && it_rank <= 256)
202                                                 GameRules_scoring_add(it, LMS_RANK, -1);
203                                 }
204                                 else if (it.frags != FRAGS_SPECTATOR)
205                                 {
206                                         float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
207                                         if(tl < lms_lowest_lives)
208                                                 lms_lowest_lives = tl;
209                                 }
210                         });
211                         GameRules_scoring_add(player, LMS_RANK, 665 - quitters); // different from 666
212                         if(!warmup_stage)
213                         {
214                                 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
215                                 ++quitters;
216                         }
217                         player.frags = FRAGS_PLAYER_OUT_OF_GAME;
218                         TRANSMUTE(Observer, player);
219                 }
220         }
221
222         if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
223         {
224                 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
225                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
226                 else
227                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
228         }
229 }
230
231 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
232 {
233         entity player = M_ARGV(0, entity);
234
235         // no further message other than the disconnect message
236         player.lms_spectate_warning = 3;
237
238         lms_RemovePlayer(player);
239 }
240
241 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
242 {
243         entity player = M_ARGV(0, entity);
244
245         if (!IS_PLAYER(player))
246                 return true;
247
248         lms_RemovePlayer(player);
249         return true;  // prevent team reset
250 }
251
252 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
253 {
254         entity player = M_ARGV(0, entity);
255
256         if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
257         {
258                 GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
259                 player.frags = FRAGS_SPECTATOR;
260         }
261 }
262
263 // FIXME LMS doesn't allow clients to spectate due to its particular implementation
264 MUTATOR_HOOKFUNCTION(lms, AutoJoinOnConnection)
265 {
266         if(autocvar_g_campaign)
267                 return false;
268         return true;
269 }
270
271 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
272 {
273         entity player = M_ARGV(0, entity);
274
275         if(player.deadflag == DEAD_DYING)
276                 player.deadflag = DEAD_RESPAWNING;
277 }
278
279 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
280 {
281         if(autocvar_g_lms_regenerate)
282                 return false;
283         return true;
284 }
285
286 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
287 {
288         // forbode!
289         return true;
290 }
291
292 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
293 {
294         entity frag_target = M_ARGV(1, entity);
295
296         if (!warmup_stage)
297         {
298                 // remove a life
299                 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
300                 if(tl < lms_lowest_lives)
301                         lms_lowest_lives = tl;
302                 if(tl <= 0)
303                 {
304                         int pl_cnt = 0;
305                         FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
306                                 pl_cnt++;
307                         });
308                         if(IS_BOT_CLIENT(frag_target))
309                                 bot_clear(frag_target);
310                         frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
311                         GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
312                 }
313         }
314         M_ARGV(2, float) = 0; // frag score
315
316         return true;
317 }
318
319 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
320 {
321         start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
322         start_health       = warmup_start_health       = cvar("g_lms_start_health");
323         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
324         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
325         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
326         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
327         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
328         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
329         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
330 }
331
332 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
333 {
334         // don't clear player score
335         return true;
336 }
337
338 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
339 {
340         entity definition = M_ARGV(0, entity);
341
342         if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
343         {
344                 return false;
345         }
346         return true;
347 }
348
349 void lms_extralife(entity this)
350 {
351         StartItem(this, ITEM_ExtraLife);
352 }
353
354 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
355 {
356         if (!autocvar_g_powerups) return false;
357         if (!autocvar_g_lms_extra_lives) return false;
358
359         entity ent = M_ARGV(0, entity);
360
361         // Can't use .itemdef here
362         if (ent.classname != "item_health_mega") return false;
363
364         entity e = spawn();
365         setthink(e, lms_extralife);
366
367         e.nextthink = time + 0.1;
368         e.spawnflags = ent.spawnflags;
369         e.noalign = ent.noalign;
370         setorigin(e, ent.origin);
371
372         return true;
373 }
374
375 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
376 {
377         entity item = M_ARGV(0, entity);
378         entity toucher = M_ARGV(1, entity);
379
380         if(item.itemdef == ITEM_ExtraLife)
381         {
382                 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
383                 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
384                 return MUT_ITEMTOUCH_PICKUP;
385         }
386
387         return MUT_ITEMTOUCH_CONTINUE;
388 }
389
390 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
391 {
392         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
393                 ++M_ARGV(0, int); // activerealplayers
394                 ++M_ARGV(1, int); // realplayers
395         });
396
397         return true;
398 }
399
400 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
401 {
402         entity player = M_ARGV(0, entity);
403
404         if(warmup_stage || player.lms_spectate_warning)
405         {
406                 // for the forfeit message...
407                 player.lms_spectate_warning = 2;
408         }
409         else
410         {
411                 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
412                 {
413                         player.lms_spectate_warning = 1;
414                         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");
415                 }
416                 return MUT_SPECCMD_RETURN;
417         }
418         return MUT_SPECCMD_CONTINUE;
419 }
420
421 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
422 {
423         M_ARGV(0, float) = WinningCondition_LMS();
424         return true;
425 }
426
427 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
428 {
429         if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
430                 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
431 }
432
433 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
434 {
435         return true;
436 }
437
438 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
439 {
440         if(game_stopped)
441         if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
442                 return true; // allow writing to this field in intermission as it is needed for newly joining players
443 }
444
445 void lms_Initialize()
446 {
447         lms_lowest_lives = 999;
448 }