]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/lms/lms.qc
Move all the remaining gamemodes to common
[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         lms_RemovePlayer(player);
239         return true;  // prevent team reset
240 }
241
242 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
243 {
244         entity player = M_ARGV(0, entity);
245
246         TRANSMUTE(Player, player);
247         campaign_bots_may_start = true;
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, PlayerPreThink)
257 {
258         entity player = M_ARGV(0, entity);
259
260         if(player.deadflag == DEAD_DYING)
261                 player.deadflag = DEAD_RESPAWNING;
262 }
263
264 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
265 {
266         if(autocvar_g_lms_regenerate)
267                 return false;
268         return true;
269 }
270
271 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
272 {
273         // forbode!
274         return true;
275 }
276
277 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
278 {
279         entity frag_target = M_ARGV(1, entity);
280
281         if (!warmup_stage)
282         {
283                 // remove a life
284                 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
285                 if(tl < lms_lowest_lives)
286                         lms_lowest_lives = tl;
287                 if(tl <= 0)
288                 {
289                         int pl_cnt = 0;
290                         FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
291                         if(IS_BOT_CLIENT(frag_target))
292                                 bot_clear(frag_target);
293                         frag_target.frags = FRAGS_LMS_LOSER;
294                         GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
295                 }
296         }
297         M_ARGV(2, float) = 0; // frag score
298
299         return true;
300 }
301
302 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
303 {
304         start_items &= ~IT_UNLIMITED_AMMO;
305         start_health       = warmup_start_health       = cvar("g_lms_start_health");
306         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
307         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
308         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
309         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
310         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
311         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
312         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
313 }
314
315 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
316 {
317         // don't clear player score
318         return true;
319 }
320
321 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
322 {
323         entity definition = M_ARGV(0, entity);
324
325         if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
326         {
327                 return false;
328         }
329         return true;
330 }
331
332 void lms_extralife(entity this)
333 {
334         StartItem(this, ITEM_ExtraLife);
335 }
336
337 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
338 {
339         if (!autocvar_g_powerups) return false;
340         if (!autocvar_g_lms_extra_lives) return false;
341
342         entity ent = M_ARGV(0, entity);
343
344         // Can't use .itemdef here
345         if (ent.classname != "item_health_mega") return false;
346
347         entity e = spawn();
348         setthink(e, lms_extralife);
349
350         e.nextthink = time + 0.1;
351         e.spawnflags = ent.spawnflags;
352         e.noalign = ent.noalign;
353         setorigin(e, ent.origin);
354
355         return true;
356 }
357
358 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
359 {
360         entity item = M_ARGV(0, entity);
361         entity toucher = M_ARGV(1, entity);
362
363         if(item.itemdef == ITEM_ExtraLife)
364         {
365                 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES);
366                 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
367                 return MUT_ITEMTOUCH_PICKUP;
368         }
369
370         return MUT_ITEMTOUCH_CONTINUE;
371 }
372
373 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
374 {
375         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
376                 ++M_ARGV(0, int); // activerealplayers
377                 ++M_ARGV(1, int); // realplayers
378         });
379
380         return true;
381 }
382
383 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
384 {
385     entity player = M_ARGV(0, entity);
386
387         if(warmup_stage || player.lms_spectate_warning)
388         {
389                 // for the forfeit message...
390                 player.lms_spectate_warning = 2;
391         }
392         else
393         {
394                 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_LMS_LOSER)
395                 {
396                         player.lms_spectate_warning = 1;
397                         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");
398                 }
399                 return MUT_SPECCMD_RETURN;
400         }
401         return MUT_SPECCMD_CONTINUE;
402 }
403
404 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
405 {
406         M_ARGV(0, float) = WinningCondition_LMS();
407         return true;
408 }
409
410 MUTATOR_HOOKFUNCTION(lms, WantWeapon)
411 {
412         M_ARGV(2, bool) = true; // all weapons
413 }
414
415 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
416 {
417         return true;
418 }
419
420 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
421 {
422         if(game_stopped)
423         if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
424                 return true; // allow writing to this field in intermission as it is needed for newly joining players
425 }
426
427 void lms_Initialize()
428 {
429         lms_lowest_lives = 9999;
430 }
431 #endif