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