]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/lms/sv_lms.qc
Split gamemodes into cl and sv.
[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 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 first_player = NULL;
39         int total_players = 0;
40         FOREACH_CLIENT(IS_PLAYER(it), {
41                 if (!total_players)
42                         first_player = it;
43                 ++total_players;
44         });
45
46         if (total_players)
47         {
48                 if (total_players > 1)
49                 {
50                         // two or more active players - continue with the game
51
52                         if (autocvar_g_campaign)
53                         {
54                                 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
55                                         float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
56                                         if (!pl_lives)
57                                                 return WINNING_YES; // human player lost, game over
58                                         break;
59                                 });
60                         }
61                 }
62                 else
63                 {
64                         // exactly one player?
65
66                         ClearWinners();
67                         SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
68
69                         if (LMS_NewPlayerLives())
70                         {
71                                 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
72                                 return WINNING_NO;
73                         }
74                         else
75                         {
76                                 // a winner!
77                                 // and assign him his first place
78                                 GameRules_scoring_add(first_player, LMS_RANK, 1);
79                                 if(warmup_stage)
80                                         return WINNING_NO;
81                                 else
82                                         return WINNING_YES;
83                         }
84                 }
85         }
86         else
87         {
88                 // nobody is playing at all...
89                 if (LMS_NewPlayerLives())
90                 {
91                         // wait for players...
92                 }
93                 else
94                 {
95                         // SNAFU (maybe a draw game?)
96                         ClearWinners();
97                         LOG_TRACE("No players, ending game.");
98                         return WINNING_YES;
99                 }
100         }
101
102         // When we get here, we have at least two players who are actually LIVING,
103         // now check if the top two players have equal score.
104         WinningConditionHelper(NULL);
105
106         ClearWinners();
107         if(WinningConditionHelper_winner)
108                 WinningConditionHelper_winner.winning = true;
109         if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
110                 return WINNING_NEVER;
111
112         // Top two have different scores? Way to go for our beloved TIMELIMIT!
113         return WINNING_NO;
114 }
115
116 // mutator hooks
117 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
118 {
119         lms_lowest_lives = 999;
120 }
121
122 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
123 {
124         FOREACH_CLIENT(true, {
125                 TRANSMUTE(Player, it);
126                 it.frags = FRAGS_PLAYER;
127                 GameRules_scoring_add(it, LMS_LIVES, LMS_NewPlayerLives());
128                 PutClientInServer(it);
129         });
130 }
131
132 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
133 {
134         entity player = M_ARGV(0, entity);
135
136         if(player.frags == FRAGS_SPECTATOR)
137                 TRANSMUTE(Observer, player);
138         else
139         {
140                 float tl = GameRules_scoring_add(player, LMS_LIVES, 0);
141                 if(tl < lms_lowest_lives)
142                         lms_lowest_lives = tl;
143                 if(tl <= 0)
144                         TRANSMUTE(Observer, player);
145                 if(warmup_stage)
146                         GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
147         }
148 }
149
150 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
151 {
152         entity player = M_ARGV(0, entity);
153
154         if(warmup_stage)
155                 return false;
156         if(player.frags == FRAGS_SPECTATOR)
157                 return true;
158         if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
159         {
160                 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
161                 return true;
162         }
163         return false;
164 }
165
166 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
167 {
168         entity frag_target = M_ARGV(2, entity);
169
170         frag_target.respawn_flags |= RESPAWN_FORCE;
171 }
172
173 void lms_RemovePlayer(entity player)
174 {
175         static int quitters = 0;
176         float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
177         if (!player_rank)
178         {
179                 int pl_cnt = 0;
180                 FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
181                 if (player.lms_spectate_warning != 2)
182                 {
183                         if(IS_BOT_CLIENT(player))
184                                 bot_clear(player);
185                         player.frags = FRAGS_LMS_LOSER;
186                         GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
187                 }
188                 else
189                 {
190                         lms_lowest_lives = 999;
191                         FOREACH_CLIENT(true, {
192                                 if (it.frags == FRAGS_LMS_LOSER)
193                                 {
194                                         float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
195                                         if (it_rank > player_rank && it_rank <= 256)
196                                                 GameRules_scoring_add(it, LMS_RANK, -1);
197                                         lms_lowest_lives = 0;
198                                 }
199                                 else if (it.frags != FRAGS_SPECTATOR)
200                                 {
201                                         float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
202                                         if(tl < lms_lowest_lives)
203                                                 lms_lowest_lives = tl;
204                                 }
205                         });
206                         GameRules_scoring_add(player, LMS_RANK, 665 - quitters); // different from 666
207                         if(!warmup_stage)
208                         {
209                                 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
210                                 ++quitters;
211                         }
212                         player.frags = FRAGS_LMS_LOSER;
213                         TRANSMUTE(Observer, player);
214                 }
215                 if (pl_cnt == 2 && !warmup_stage) // a player is forfeiting leaving only one player
216                         lms_lowest_lives = 0; // end the game now!
217         }
218
219         if(CS(player).killcount != FRAGS_SPECTATOR)
220                 if(GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning != 2)
221                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
222                 else
223                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
224 }
225
226 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
227 {
228         entity player = M_ARGV(0, entity);
229
230         lms_RemovePlayer(player);
231 }
232
233 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
234 {
235         entity player = M_ARGV(0, entity);
236
237         if (!IS_PLAYER(player))
238                 return true;
239
240         lms_RemovePlayer(player);
241         return true;  // prevent team reset
242 }
243
244 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
245 {
246         entity player = M_ARGV(0, entity);
247
248         if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
249         {
250                 GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
251                 player.frags = FRAGS_SPECTATOR;
252         }
253 }
254
255 MUTATOR_HOOKFUNCTION(lms, AutoJoinOnConnection)
256 {
257         if(autocvar_g_campaign)
258                 return false;
259         return true;
260 }
261
262 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
263 {
264         entity player = M_ARGV(0, entity);
265
266         if(player.deadflag == DEAD_DYING)
267                 player.deadflag = DEAD_RESPAWNING;
268 }
269
270 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
271 {
272         if(autocvar_g_lms_regenerate)
273                 return false;
274         return true;
275 }
276
277 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
278 {
279         // forbode!
280         return true;
281 }
282
283 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
284 {
285         entity frag_target = M_ARGV(1, entity);
286
287         if (!warmup_stage)
288         {
289                 // remove a life
290                 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
291                 if(tl < lms_lowest_lives)
292                         lms_lowest_lives = tl;
293                 if(tl <= 0)
294                 {
295                         int pl_cnt = 0;
296                         FOREACH_CLIENT(IS_PLAYER(it), { pl_cnt++; });
297                         if(IS_BOT_CLIENT(frag_target))
298                                 bot_clear(frag_target);
299                         frag_target.frags = FRAGS_LMS_LOSER;
300                         GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
301                 }
302         }
303         M_ARGV(2, float) = 0; // frag score
304
305         return true;
306 }
307
308 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
309 {
310         start_items &= ~IT_UNLIMITED_AMMO;
311         start_health       = warmup_start_health       = cvar("g_lms_start_health");
312         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
313         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
314         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
315         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
316         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
317         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
318         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
319 }
320
321 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
322 {
323         // don't clear player score
324         return true;
325 }
326
327 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
328 {
329         entity definition = M_ARGV(0, entity);
330
331         if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
332         {
333                 return false;
334         }
335         return true;
336 }
337
338 void lms_extralife(entity this)
339 {
340         StartItem(this, ITEM_ExtraLife);
341 }
342
343 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
344 {
345         if (!autocvar_g_powerups) return false;
346         if (!autocvar_g_lms_extra_lives) return false;
347
348         entity ent = M_ARGV(0, entity);
349
350         // Can't use .itemdef here
351         if (ent.classname != "item_health_mega") return false;
352
353         entity e = spawn();
354         setthink(e, lms_extralife);
355
356         e.nextthink = time + 0.1;
357         e.spawnflags = ent.spawnflags;
358         e.noalign = ent.noalign;
359         setorigin(e, ent.origin);
360
361         return true;
362 }
363
364 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
365 {
366         entity item = M_ARGV(0, entity);
367         entity toucher = M_ARGV(1, entity);
368
369         if(item.itemdef == ITEM_ExtraLife)
370         {
371                 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES);
372                 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
373                 return MUT_ITEMTOUCH_PICKUP;
374         }
375
376         return MUT_ITEMTOUCH_CONTINUE;
377 }
378
379 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
380 {
381         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
382                 ++M_ARGV(0, int); // activerealplayers
383                 ++M_ARGV(1, int); // realplayers
384         });
385
386         return true;
387 }
388
389 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
390 {
391         entity player = M_ARGV(0, entity);
392
393         if(warmup_stage || player.lms_spectate_warning)
394         {
395                 // for the forfeit message...
396                 player.lms_spectate_warning = 2;
397         }
398         else
399         {
400                 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_LMS_LOSER)
401                 {
402                         player.lms_spectate_warning = 1;
403                         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");
404                 }
405                 return MUT_SPECCMD_RETURN;
406         }
407         return MUT_SPECCMD_CONTINUE;
408 }
409
410 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
411 {
412         M_ARGV(0, float) = WinningCondition_LMS();
413         return true;
414 }
415
416 MUTATOR_HOOKFUNCTION(lms, WantWeapon)
417 {
418         M_ARGV(2, bool) = true; // all weapons
419 }
420
421 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
422 {
423         return true;
424 }
425
426 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
427 {
428         if(game_stopped)
429         if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
430                 return true; // allow writing to this field in intermission as it is needed for newly joining players
431 }
432
433 void lms_Initialize()
434 {
435         lms_lowest_lives = 9999;
436 }