]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_lms.qc
77648434436182640b86eca028848dae65c8fc40
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator / gamemode_lms.qc
1 #include "gamemode_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 float LMS_NewPlayerLives()
14 {
15         float fl;
16         fl = autocvar_fraglimit;
17         if(fl == 0)
18                 fl = 999;
19
20         // first player has left the game for dying too much? Nobody else can get in.
21         if(lms_lowest_lives < 1)
22                 return 0;
23
24         if(!autocvar_g_lms_join_anytime)
25                 if(lms_lowest_lives < fl - autocvar_g_lms_last_join)
26                         return 0;
27
28         return bound(1, lms_lowest_lives, fl);
29 }
30
31 void ClearWinners();
32
33 // LMS winning condition: game terminates if and only if there's at most one
34 // one player who's living lives. Top two scores being equal cancels the time
35 // limit.
36 int WinningCondition_LMS()
37 {
38         entity head, head2;
39         bool have_player = false;
40         bool have_players = false;
41
42         int l = LMS_NewPlayerLives();
43
44         head = find(NULL, classname, STR_PLAYER);
45         if(head)
46                 have_player = true;
47         head2 = find(head, classname, STR_PLAYER);
48         if(head2)
49                 have_players = true;
50
51         if(have_player)
52         {
53                 // we have at least one player
54                 if(have_players)
55                 {
56                         // two or more active players - continue with the game
57                 }
58                 else
59                 {
60                         // exactly one player?
61
62                         ClearWinners();
63                         SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
64
65                         if(l)
66                         {
67                                 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
68                                 return WINNING_NO;
69                         }
70                         else
71                         {
72                                 // a winner!
73                                 // and assign him his first place
74                                 GameRules_scoring_add(head, LMS_RANK, 1);
75                                 if(warmup_stage)
76                                         return WINNING_NO;
77                                 else
78                                         return WINNING_YES;
79                         }
80                 }
81         }
82         else
83         {
84                 // nobody is playing at all...
85                 if(l)
86                 {
87                         // wait for players...
88                 }
89                 else
90                 {
91                         // SNAFU (maybe a draw game?)
92                         ClearWinners();
93                         LOG_TRACE("No players, ending game.");
94                         return WINNING_YES;
95                 }
96         }
97
98         // When we get here, we have at least two players who are actually LIVING,
99         // now check if the top two players have equal score.
100         WinningConditionHelper(NULL);
101
102         ClearWinners();
103         if(WinningConditionHelper_winner)
104                 WinningConditionHelper_winner.winning = true;
105         if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
106                 return WINNING_NEVER;
107
108         // Top two have different scores? Way to go for our beloved TIMELIMIT!
109         return WINNING_NO;
110 }
111
112 // mutator hooks
113 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
114 {
115         lms_lowest_lives = 999;
116 }
117
118 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
119 {
120         FOREACH_CLIENT(true, {
121                 TRANSMUTE(Player, it);
122                 it.frags = FRAGS_PLAYER;
123                 GameRules_scoring_add(it, LMS_LIVES, LMS_NewPlayerLives());
124                 PutClientInServer(it);
125         });
126 }
127
128 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
129 {
130         entity player = M_ARGV(0, entity);
131
132         if(player.frags == FRAGS_SPECTATOR)
133                 TRANSMUTE(Observer, player);
134         else
135         {
136                 float tl = GameRules_scoring_add(player, LMS_LIVES, 0);
137                 if(tl < lms_lowest_lives)
138                         lms_lowest_lives = tl;
139                 if(tl <= 0)
140                         TRANSMUTE(Observer, player);
141                 if(warmup_stage)
142                         GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
143         }
144 }
145
146 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
147 {
148         entity player = M_ARGV(0, entity);
149
150         if(warmup_stage)
151                 return false;
152         if(player.frags == FRAGS_SPECTATOR)
153                 return true;
154         if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
155         {
156                 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
157                 return true;
158         }
159         return false;
160 }
161
162 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
163 {
164         entity frag_target = M_ARGV(2, entity);
165
166         frag_target.respawn_flags |= RESPAWN_FORCE;
167 }
168
169 void lms_RemovePlayer(entity player)
170 {
171         static int quitters = 0;
172         float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
173         if (!player_rank)
174         {
175                 int pl_cnt = 0;
176                 FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
177                 if (player.lms_spectate_warning != 2)
178                 {
179                         if(IS_BOT_CLIENT(player))
180                                 bot_clear(player);
181                         player.frags = FRAGS_LMS_LOSER;
182                         GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
183                 }
184                 else
185                 {
186                         lms_lowest_lives = 999;
187                         FOREACH_CLIENT(true, {
188                                 if (it.frags == FRAGS_LMS_LOSER)
189                                 {
190                                         float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
191                                         if (it_rank > player_rank && it_rank <= 256)
192                                                 GameRules_scoring_add(it, LMS_RANK, -1);
193                                         lms_lowest_lives = 0;
194                                 }
195                                 else if (it.frags != FRAGS_SPECTATOR)
196                                 {
197                                         float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
198                                         if(tl < lms_lowest_lives)
199                                                 lms_lowest_lives = tl;
200                                 }
201                         });
202                         GameRules_scoring_add(player, LMS_RANK, 665 - quitters); // different from 666
203                         if(!warmup_stage)
204                         {
205                                 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
206                                 ++quitters;
207                         }
208                         player.frags = FRAGS_LMS_LOSER;
209                         TRANSMUTE(Observer, player);
210                 }
211                 if (pl_cnt == 2 && !warmup_stage) // a player is forfeiting leaving only one player
212                         lms_lowest_lives = 0; // end the game now!
213         }
214
215         if(CS(player).killcount != FRAGS_SPECTATOR)
216                 if(GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning != 2)
217                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
218                 else
219                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
220 }
221
222 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
223 {
224         entity player = M_ARGV(0, entity);
225
226         lms_RemovePlayer(player);
227 }
228
229 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
230 {
231         entity player = M_ARGV(0, entity);
232
233         if (!IS_PLAYER(player))
234                 return true;
235
236         lms_RemovePlayer(player);
237         return true;  // prevent team reset
238 }
239
240 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
241 {
242         entity player = M_ARGV(0, entity);
243
244         if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
245         {
246                 GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
247                 player.frags = FRAGS_SPECTATOR;
248         }
249 }
250
251 MUTATOR_HOOKFUNCTION(lms, AutoJoinOnConnection)
252 {
253         return true;
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, FilterItem)
322 {
323         entity item = M_ARGV(0, entity);
324
325         if(autocvar_g_lms_extra_lives)
326         if(item.itemdef == ITEM_ExtraLife)
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 }