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