]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/lms/sv_lms.qc
Merge branch 'master' into develop
[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         entity first_player = NULL;
40         int totalplayers = 0;
41         FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
42                 if (!totalplayers)
43                         first_player = it;
44                 ++totalplayers;
45         });
46
47         if (totalplayers)
48         {
49                 if (totalplayers > 1)
50                 {
51                         // two or more active players - continue with the game
52
53                         if (autocvar_g_campaign)
54                         {
55                                 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
56                                         float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
57                                         if (!pl_lives)
58                                                 return WINNING_YES; // human player lost, game over
59                                         break;
60                                 });
61                         }
62                 }
63                 else
64                 {
65                         // exactly one player?
66
67                         ClearWinners();
68                         SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
69
70                         if (LMS_NewPlayerLives())
71                         {
72                                 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
73                                 return WINNING_NO;
74                         }
75                         else
76                         {
77                                 // a winner!
78                                 // and assign him his first place
79                                 GameRules_scoring_add(first_player, LMS_RANK, 1);
80                                 if(warmup_stage)
81                                         return WINNING_NO;
82                                 else
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                 TRANSMUTE(Player, it);
127                 it.frags = FRAGS_PLAYER;
128                 GameRules_scoring_add(it, LMS_LIVES, LMS_NewPlayerLives());
129                 PutClientInServer(it);
130         });
131 }
132
133 // FIXME add support for sv_ready_restart_after_countdown
134 // that is find a way to respawn/reset players IN GAME without setting lives to 0
135 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
136 {
137         // incompatible
138         sv_ready_restart_after_countdown = 0;
139 }
140
141 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
142 {
143         entity player = M_ARGV(0, entity);
144
145         if(player.frags == FRAGS_SPECTATOR)
146                 TRANSMUTE(Observer, player);
147         else
148         {
149                 float tl = GameRules_scoring_add(player, LMS_LIVES, 0);
150                 if(tl < lms_lowest_lives)
151                         lms_lowest_lives = tl;
152                 if(tl <= 0)
153                         TRANSMUTE(Observer, player);
154                 if(warmup_stage)
155                         GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
156         }
157 }
158
159 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
160 {
161         entity player = M_ARGV(0, entity);
162
163         if(warmup_stage)
164                 return false;
165         if(player.frags == FRAGS_SPECTATOR || 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         float tl = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
178         if (tl <= 0)
179         {
180                 frag_target.respawn_flags = RESPAWN_SILENT;
181                 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
182                 frag_target.respawn_time = time + 2;
183         }
184         frag_target.respawn_flags |= RESPAWN_FORCE;
185 }
186
187 void lms_RemovePlayer(entity player)
188 {
189         static int quitters = 0;
190         float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
191         if (!player_rank)
192         {
193                 if (player.lms_spectate_warning < 2)
194                 {
195                         if(IS_BOT_CLIENT(player))
196                                 bot_clear(player);
197                         player.frags = FRAGS_PLAYER_OUT_OF_GAME;
198                         int pl_cnt = 0;
199                         FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
200                                 pl_cnt++;
201                         });
202                         GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
203                 }
204                 else
205                 {
206                         FOREACH_CLIENT(true, {
207                                 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
208                                 {
209                                         float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
210                                         if (it_rank > player_rank && it_rank <= 256)
211                                                 GameRules_scoring_add(it, LMS_RANK, -1);
212                                 }
213                                 else if (it.frags != FRAGS_SPECTATOR)
214                                 {
215                                         float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
216                                         if(tl < lms_lowest_lives)
217                                                 lms_lowest_lives = tl;
218                                 }
219                         });
220                         GameRules_scoring_add(player, LMS_RANK, 665 - quitters); // different from 666
221                         if(!warmup_stage)
222                         {
223                                 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
224                                 ++quitters;
225                         }
226                         player.frags = FRAGS_PLAYER_OUT_OF_GAME;
227                         TRANSMUTE(Observer, player);
228                 }
229         }
230
231         if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
232         {
233                 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
234                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
235                 else
236                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
237         }
238 }
239
240 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
241 {
242         entity player = M_ARGV(0, entity);
243
244         // no further message other than the disconnect message
245         player.lms_spectate_warning = 3;
246
247         lms_RemovePlayer(player);
248 }
249
250 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
251 {
252         entity player = M_ARGV(0, entity);
253
254         if (!IS_PLAYER(player))
255                 return true;
256
257         lms_RemovePlayer(player);
258         return true;  // prevent team reset
259 }
260
261 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
262 {
263         entity player = M_ARGV(0, entity);
264
265         if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
266         {
267                 GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
268                 player.frags = FRAGS_SPECTATOR;
269         }
270 }
271
272 // FIXME LMS doesn't allow clients to spectate due to its particular implementation
273 MUTATOR_HOOKFUNCTION(lms, AutoJoinOnConnection)
274 {
275         if(autocvar_g_campaign)
276                 return false;
277         return true;
278 }
279
280 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
281 {
282         entity player = M_ARGV(0, entity);
283
284         if(player.deadflag == DEAD_DYING)
285                 player.deadflag = DEAD_RESPAWNING;
286 }
287
288 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
289 {
290         if(autocvar_g_lms_regenerate)
291                 return false;
292         return true;
293 }
294
295 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
296 {
297         // forbode!
298         return true;
299 }
300
301 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
302 {
303         entity frag_target = M_ARGV(1, entity);
304
305         if (!warmup_stage)
306         {
307                 // remove a life
308                 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
309                 if(tl < lms_lowest_lives)
310                         lms_lowest_lives = tl;
311                 if(tl <= 0)
312                 {
313                         int pl_cnt = 0;
314                         FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
315                                 pl_cnt++;
316                         });
317                         if(IS_BOT_CLIENT(frag_target))
318                                 bot_clear(frag_target);
319                         frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
320                         GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
321                 }
322         }
323         M_ARGV(2, float) = 0; // frag score
324
325         return true;
326 }
327
328 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
329 {
330         start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
331         start_health       = warmup_start_health       = cvar("g_lms_start_health");
332         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
333         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
334         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
335         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
336         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
337         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
338         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
339 }
340
341 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
342 {
343         // don't clear player score
344         return true;
345 }
346
347 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
348 {
349         entity definition = M_ARGV(0, entity);
350
351         if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
352         {
353                 return false;
354         }
355         return true;
356 }
357
358 void lms_extralife(entity this)
359 {
360         StartItem(this, ITEM_ExtraLife);
361 }
362
363 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
364 {
365         if (MUTATOR_RETURNVALUE) return false;
366         if (!autocvar_g_powerups) return false;
367         if (!autocvar_g_lms_extra_lives) return false;
368
369         entity ent = M_ARGV(0, entity);
370
371         // Can't use .itemdef here
372         if (ent.classname != "item_health_mega") return false;
373
374         entity e = spawn();
375         setthink(e, lms_extralife);
376
377         e.nextthink = time + 0.1;
378         e.spawnflags = ent.spawnflags;
379         e.noalign = ent.noalign;
380         setorigin(e, ent.origin);
381
382         return true;
383 }
384
385 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
386 {
387         if(MUTATOR_RETURNVALUE) return false;
388
389         entity item = M_ARGV(0, entity);
390         entity toucher = M_ARGV(1, entity);
391
392         if(item.itemdef == ITEM_ExtraLife)
393         {
394                 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
395                 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
396                 return MUT_ITEMTOUCH_PICKUP;
397         }
398
399         return MUT_ITEMTOUCH_CONTINUE;
400 }
401
402 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
403 {
404         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
405                 ++M_ARGV(0, int); // activerealplayers
406                 ++M_ARGV(1, int); // realplayers
407         });
408
409         return true;
410 }
411
412 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
413 {
414         entity player = M_ARGV(0, entity);
415
416         if(warmup_stage || player.lms_spectate_warning)
417         {
418                 // for the forfeit message...
419                 player.lms_spectate_warning = 2;
420         }
421         else
422         {
423                 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
424                 {
425                         player.lms_spectate_warning = 1;
426                         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");
427                 }
428                 return MUT_SPECCMD_RETURN;
429         }
430         return MUT_SPECCMD_CONTINUE;
431 }
432
433 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
434 {
435         M_ARGV(0, float) = WinningCondition_LMS();
436         return true;
437 }
438
439 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
440 {
441         if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
442                 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
443 }
444
445 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
446 {
447         return true;
448 }
449
450 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
451 {
452         if(game_stopped)
453         if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
454                 return true; // allow writing to this field in intermission as it is needed for newly joining players
455 }
456
457 void lms_Initialize()
458 {
459         lms_lowest_lives = 999;
460 }