]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/lms/lms.qc
compress skies in default effects
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / lms / lms.qc
1 #include "lms.qh"
2
3 #ifdef SVQC
4 #include <common/mutators/mutator/instagib/items.qh>
5 #include <server/campaign.qh>
6 #include <server/command/_mod.qh>
7
8 int autocvar_g_lms_extra_lives;
9 bool autocvar_g_lms_join_anytime;
10 int autocvar_g_lms_last_join;
11 bool autocvar_g_lms_regenerate;
12
13 // main functions
14 float LMS_NewPlayerLives()
15 {
16         float fl;
17         fl = autocvar_fraglimit;
18         if(fl == 0)
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 - 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 total_players = 0;
41         FOREACH_CLIENT(IS_PLAYER(it), {
42                 if (!total_players)
43                         first_player = it;
44                 ++total_players;
45         });
46
47         if (total_players)
48         {
49                 if (total_players > 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 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
134 {
135         entity player = M_ARGV(0, entity);
136
137         if(player.frags == FRAGS_SPECTATOR)
138                 TRANSMUTE(Observer, player);
139         else
140         {
141                 float tl = GameRules_scoring_add(player, LMS_LIVES, 0);
142                 if(tl < lms_lowest_lives)
143                         lms_lowest_lives = tl;
144                 if(tl <= 0)
145                         TRANSMUTE(Observer, player);
146                 if(warmup_stage)
147                         GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
148         }
149 }
150
151 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
152 {
153         entity player = M_ARGV(0, entity);
154
155         if(warmup_stage)
156                 return false;
157         if(player.frags == FRAGS_SPECTATOR)
158                 return true;
159         if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
160         {
161                 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
162                 return true;
163         }
164         return false;
165 }
166
167 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
168 {
169         entity frag_target = M_ARGV(2, entity);
170
171         frag_target.respawn_flags |= RESPAWN_FORCE;
172 }
173
174 void lms_RemovePlayer(entity player)
175 {
176         static int quitters = 0;
177         float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
178         if (!player_rank)
179         {
180                 int pl_cnt = 0;
181                 FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
182                 if (player.lms_spectate_warning != 2)
183                 {
184                         if(IS_BOT_CLIENT(player))
185                                 bot_clear(player);
186                         player.frags = FRAGS_LMS_LOSER;
187                         GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
188                 }
189                 else
190                 {
191                         lms_lowest_lives = 999;
192                         FOREACH_CLIENT(true, {
193                                 if (it.frags == FRAGS_LMS_LOSER)
194                                 {
195                                         float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
196                                         if (it_rank > player_rank && it_rank <= 256)
197                                                 GameRules_scoring_add(it, LMS_RANK, -1);
198                                         lms_lowest_lives = 0;
199                                 }
200                                 else if (it.frags != FRAGS_SPECTATOR)
201                                 {
202                                         float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
203                                         if(tl < lms_lowest_lives)
204                                                 lms_lowest_lives = tl;
205                                 }
206                         });
207                         GameRules_scoring_add(player, LMS_RANK, 665 - quitters); // different from 666
208                         if(!warmup_stage)
209                         {
210                                 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
211                                 ++quitters;
212                         }
213                         player.frags = FRAGS_LMS_LOSER;
214                         TRANSMUTE(Observer, player);
215                 }
216                 if (pl_cnt == 2 && !warmup_stage) // a player is forfeiting leaving only one player
217                         lms_lowest_lives = 0; // end the game now!
218         }
219
220         if(CS(player).killcount != FRAGS_SPECTATOR)
221                 if(GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning != 2)
222                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
223                 else
224                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
225 }
226
227 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
228 {
229         entity player = M_ARGV(0, entity);
230
231         lms_RemovePlayer(player);
232 }
233
234 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
235 {
236         entity player = M_ARGV(0, entity);
237
238         if (!IS_PLAYER(player))
239                 return true;
240
241         lms_RemovePlayer(player);
242         return true;  // prevent team reset
243 }
244
245 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
246 {
247         entity player = M_ARGV(0, entity);
248
249         if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
250         {
251                 GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
252                 player.frags = FRAGS_SPECTATOR;
253         }
254 }
255
256 MUTATOR_HOOKFUNCTION(lms, AutoJoinOnConnection)
257 {
258         if(autocvar_g_campaign)
259                 return false;
260         return true;
261 }
262
263 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
264 {
265         entity player = M_ARGV(0, entity);
266
267         if(player.deadflag == DEAD_DYING)
268                 player.deadflag = DEAD_RESPAWNING;
269 }
270
271 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
272 {
273         if(autocvar_g_lms_regenerate)
274                 return false;
275         return true;
276 }
277
278 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
279 {
280         // forbode!
281         return true;
282 }
283
284 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
285 {
286         entity frag_target = M_ARGV(1, entity);
287
288         if (!warmup_stage)
289         {
290                 // remove a life
291                 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
292                 if(tl < lms_lowest_lives)
293                         lms_lowest_lives = tl;
294                 if(tl <= 0)
295                 {
296                         int pl_cnt = 0;
297                         FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
298                         if(IS_BOT_CLIENT(frag_target))
299                                 bot_clear(frag_target);
300                         frag_target.frags = FRAGS_LMS_LOSER;
301                         GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
302                 }
303         }
304         M_ARGV(2, float) = 0; // frag score
305
306         return true;
307 }
308
309 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
310 {
311         start_items &= ~IT_UNLIMITED_AMMO;
312         start_health       = warmup_start_health       = cvar("g_lms_start_health");
313         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
314         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
315         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
316         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
317         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
318         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
319         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
320 }
321
322 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
323 {
324         // don't clear player score
325         return true;
326 }
327
328 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
329 {
330         entity definition = M_ARGV(0, entity);
331
332         if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
333         {
334                 return false;
335         }
336         return true;
337 }
338
339 void lms_extralife(entity this)
340 {
341         StartItem(this, ITEM_ExtraLife);
342 }
343
344 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
345 {
346         if (!autocvar_g_powerups) return false;
347         if (!autocvar_g_lms_extra_lives) return false;
348
349         entity ent = M_ARGV(0, entity);
350
351         // Can't use .itemdef here
352         if (ent.classname != "item_health_mega") return false;
353
354         entity e = spawn();
355         setthink(e, lms_extralife);
356
357         e.nextthink = time + 0.1;
358         e.spawnflags = ent.spawnflags;
359         e.noalign = ent.noalign;
360         setorigin(e, ent.origin);
361
362         return true;
363 }
364
365 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
366 {
367         entity item = M_ARGV(0, entity);
368         entity toucher = M_ARGV(1, entity);
369
370         if(item.itemdef == ITEM_ExtraLife)
371         {
372                 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES);
373                 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
374                 return MUT_ITEMTOUCH_PICKUP;
375         }
376
377         return MUT_ITEMTOUCH_CONTINUE;
378 }
379
380 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
381 {
382         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
383                 ++M_ARGV(0, int); // activerealplayers
384                 ++M_ARGV(1, int); // realplayers
385         });
386
387         return true;
388 }
389
390 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
391 {
392         entity player = M_ARGV(0, entity);
393
394         if(warmup_stage || player.lms_spectate_warning)
395         {
396                 // for the forfeit message...
397                 player.lms_spectate_warning = 2;
398         }
399         else
400         {
401                 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_LMS_LOSER)
402                 {
403                         player.lms_spectate_warning = 1;
404                         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");
405                 }
406                 return MUT_SPECCMD_RETURN;
407         }
408         return MUT_SPECCMD_CONTINUE;
409 }
410
411 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
412 {
413         M_ARGV(0, float) = WinningCondition_LMS();
414         return true;
415 }
416
417 MUTATOR_HOOKFUNCTION(lms, WantWeapon)
418 {
419         M_ARGV(2, bool) = true; // all weapons
420 }
421
422 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
423 {
424         return true;
425 }
426
427 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
428 {
429         if(game_stopped)
430         if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
431                 return true; // allow writing to this field in intermission as it is needed for newly joining players
432 }
433
434 void lms_Initialize()
435 {
436         lms_lowest_lives = 9999;
437 }
438 #endif