]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_lms.qc
e96e7f2f920db0d69acfd3766e2c266abde8a4dc
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator / gamemode_lms.qc
1 #include "gamemode_lms.qh"
2 #ifndef GAMEMODE_LMS_H
3 #define GAMEMODE_LMS_H
4
5 #define autocvar_g_lms_lives_override cvar("g_lms_lives_override")
6 void lms_Initialize();
7
8 REGISTER_MUTATOR(lms, false)
9 {
10         MUTATOR_ONADD
11         {
12                 if (time > 1) // game loads at time 1
13                         error("This is a game type and it cannot be added at runtime.");
14                 lms_Initialize();
15
16                 SetLimits(((!autocvar_g_lms_lives_override) ? -1 : autocvar_g_lms_lives_override), 0, autocvar_timelimit_override, -1);
17         }
18
19         MUTATOR_ONROLLBACK_OR_REMOVE
20         {
21                 // we actually cannot roll back lms_Initialize here
22                 // BUT: we don't need to! If this gets called, adding always
23                 // succeeds.
24         }
25
26         MUTATOR_ONREMOVE
27         {
28                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
29                 return -1;
30         }
31
32         return 0;
33 }
34
35 // lives related defs
36 float lms_lowest_lives;
37 float lms_next_place;
38 float LMS_NewPlayerLives();
39
40 #endif
41
42 #ifdef IMPLEMENTATION
43
44 #include <common/mutators/mutator/instagib/items.qc>
45 #include <server/campaign.qh>
46 #include <server/command/cmd.qh>
47
48 int autocvar_g_lms_extra_lives;
49 bool autocvar_g_lms_join_anytime;
50 int autocvar_g_lms_last_join;
51 bool autocvar_g_lms_regenerate;
52
53 // main functions
54 float LMS_NewPlayerLives()
55 {
56         float fl;
57         fl = autocvar_fraglimit;
58         if(fl == 0)
59                 fl = 999;
60
61         // first player has left the game for dying too much? Nobody else can get in.
62         if(lms_lowest_lives < 1)
63                 return 0;
64
65         if(!autocvar_g_lms_join_anytime)
66                 if(lms_lowest_lives < fl - autocvar_g_lms_last_join)
67                         return 0;
68
69         return bound(1, lms_lowest_lives, fl);
70 }
71
72 void ClearWinners();
73
74 // LMS winning condition: game terminates if and only if there's at most one
75 // one player who's living lives. Top two scores being equal cancels the time
76 // limit.
77 int WinningCondition_LMS()
78 {
79         entity head, head2;
80         bool have_player = false;
81         bool have_players = false;
82
83         int l = LMS_NewPlayerLives();
84
85         head = find(NULL, classname, STR_PLAYER);
86         if(head)
87                 have_player = true;
88         head2 = find(head, classname, STR_PLAYER);
89         if(head2)
90                 have_players = true;
91
92         if(have_player)
93         {
94                 // we have at least one player
95                 if(have_players)
96                 {
97                         // two or more active players - continue with the game
98                 }
99                 else
100                 {
101                         // exactly one player?
102
103                         ClearWinners();
104                         SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
105
106                         if(l)
107                         {
108                                 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
109                                 return WINNING_NO;
110                         }
111                         else
112                         {
113                                 // a winner!
114                                 // and assign him his first place
115                                 PlayerScore_Add(head, SP_LMS_RANK, 1);
116                                 return WINNING_YES;
117                         }
118                 }
119         }
120         else
121         {
122                 // nobody is playing at all...
123                 if(l)
124                 {
125                         // wait for players...
126                 }
127                 else
128                 {
129                         // SNAFU (maybe a draw game?)
130                         ClearWinners();
131                         LOG_TRACE("No players, ending game.\n");
132                         return WINNING_YES;
133                 }
134         }
135
136         // When we get here, we have at least two players who are actually LIVING,
137         // now check if the top two players have equal score.
138         WinningConditionHelper(NULL);
139
140         ClearWinners();
141         if(WinningConditionHelper_winner)
142                 WinningConditionHelper_winner.winning = true;
143         if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
144                 return WINNING_NEVER;
145
146         // Top two have different scores? Way to go for our beloved TIMELIMIT!
147         return WINNING_NO;
148 }
149
150 // mutator hooks
151 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
152 {
153         lms_lowest_lives = 999;
154         lms_next_place = player_count;
155 }
156
157 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
158 {
159         if(restart_mapalreadyrestarted || (time < game_starttime))
160         FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(PlayerScore_Add(it, SP_LMS_LIVES, LMS_NewPlayerLives())));
161 }
162
163 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
164 {
165         entity player = M_ARGV(0, entity);
166
167         // player is dead and becomes observer
168         // FIXME fix LMS scoring for new system
169         if(PlayerScore_Add(player, SP_LMS_RANK, 0) > 0)
170         {
171                 TRANSMUTE(Observer, player);
172                 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
173         }
174 }
175
176 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
177 {
178         entity frag_target = M_ARGV(2, entity);
179
180         frag_target.respawn_flags |= RESPAWN_FORCE;
181 }
182
183 void lms_RemovePlayer(entity player)
184 {
185         // Only if the player cannot play at all
186         if(PlayerScore_Add(player, SP_LMS_RANK, 0) == 666)
187                 player.frags = FRAGS_SPECTATOR;
188         else
189                 player.frags = FRAGS_LMS_LOSER;
190
191         if(player.killcount != FRAGS_SPECTATOR)
192                 if(PlayerScore_Add(player, SP_LMS_RANK, 0) > 0 && player.lms_spectate_warning != 2)
193                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
194                 else
195                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
196 }
197
198 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
199 {
200         entity player = M_ARGV(0, entity);
201
202         lms_RemovePlayer(player);
203 }
204
205 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
206 {
207     entity player = M_ARGV(0, entity);
208
209         lms_RemovePlayer(player);
210         return true;  // prevent team reset
211 }
212
213 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
214 {
215         entity player = M_ARGV(0, entity);
216
217         TRANSMUTE(Player, player);
218         campaign_bots_may_start = true;
219
220         if(PlayerScore_Add(player, SP_LMS_LIVES, LMS_NewPlayerLives()) <= 0)
221         {
222                 PlayerScore_Add(player, SP_LMS_RANK, 666);
223                 player.frags = FRAGS_SPECTATOR;
224         }
225 }
226
227 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
228 {
229         entity player = M_ARGV(0, entity);
230
231         if(player.deadflag == DEAD_DYING)
232                 player.deadflag = DEAD_RESPAWNING;
233 }
234
235 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
236 {
237         if(autocvar_g_lms_regenerate)
238                 return false;
239         return true;
240 }
241
242 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
243 {
244         // forbode!
245         return true;
246 }
247
248 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
249 {
250         entity frag_target = M_ARGV(1, entity);
251
252         // remove a life
253         float tl;
254         tl = PlayerScore_Add(frag_target, SP_LMS_LIVES, -1);
255         if(tl < lms_lowest_lives)
256                 lms_lowest_lives = tl;
257         if(tl <= 0)
258         {
259                 if(!lms_next_place)
260                         lms_next_place = player_count;
261                 else
262                         lms_next_place = min(lms_next_place, player_count);
263                 PlayerScore_Add(frag_target, SP_LMS_RANK, lms_next_place); // won't ever spawn again
264                 --lms_next_place;
265         }
266         M_ARGV(2, float) = 0;
267
268         return true;
269 }
270
271 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
272 {
273         start_items &= ~IT_UNLIMITED_AMMO;
274         start_health       = warmup_start_health       = cvar("g_lms_start_health");
275         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
276         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
277         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
278         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
279         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
280         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
281         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
282 }
283
284 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
285 {
286         // don't clear player score
287         return true;
288 }
289
290 MUTATOR_HOOKFUNCTION(lms, FilterItem)
291 {
292         entity item = M_ARGV(0, entity);
293
294         if(autocvar_g_lms_extra_lives)
295         if(item.itemdef == ITEM_ExtraLife)
296                 return false;
297
298         return true;
299 }
300
301 void lms_extralife(entity this)
302 {
303         StartItem(this, ITEM_ExtraLife);
304 }
305
306 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
307 {
308         if (!autocvar_g_powerups) return false;
309         if (!autocvar_g_lms_extra_lives) return false;
310
311         entity ent = M_ARGV(0, entity);
312
313         // Can't use .itemdef here
314         if (ent.classname != "item_health_mega") return false;
315
316         entity e = spawn();
317         setthink(e, lms_extralife);
318
319         e.nextthink = time + 0.1;
320         e.spawnflags = ent.spawnflags;
321         e.noalign = ent.noalign;
322         setorigin(e, ent.origin);
323
324         return true;
325 }
326
327 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
328 {
329         entity item = M_ARGV(0, entity);
330         entity toucher = M_ARGV(1, entity);
331
332         if(item.itemdef == ITEM_ExtraLife)
333         {
334                 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES);
335                 PlayerScore_Add(toucher, SP_LMS_LIVES, autocvar_g_lms_extra_lives);
336                 return MUT_ITEMTOUCH_PICKUP;
337         }
338
339         return MUT_ITEMTOUCH_CONTINUE;
340 }
341
342 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
343 {
344         FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
345                 ++M_ARGV(0, int);
346                 ++M_ARGV(1, int);
347         ));
348
349         return true;
350 }
351
352 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
353 {
354     entity player = M_ARGV(0, entity);
355
356         if(player.lms_spectate_warning)
357         {
358                 // for the forfeit message...
359                 player.lms_spectate_warning = 2;
360                 // mark player as spectator
361                 PlayerScore_Add(player, SP_LMS_RANK, 666 - PlayerScore_Add(player, SP_LMS_RANK, 0));
362         }
363         else
364         {
365                 player.lms_spectate_warning = 1;
366                 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");
367                 return MUT_SPECCMD_RETURN;
368         }
369         return MUT_SPECCMD_CONTINUE;
370 }
371
372 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
373 {
374         M_ARGV(0, float) = WinningCondition_LMS();
375         return true;
376 }
377
378 MUTATOR_HOOKFUNCTION(lms, WantWeapon)
379 {
380         M_ARGV(2, bool) = true; // all weapons
381 }
382
383 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
384 {
385         return true;
386 }
387
388 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
389 {
390         if(gameover)
391         if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
392                 return true; // allow writing to this field in intermission as it is needed for newly joining players
393 }
394
395 // scoreboard stuff
396 void lms_ScoreRules()
397 {
398         ScoreRules_basics(0, 0, 0, false);
399         ScoreInfo_SetLabel_PlayerScore(SP_LMS_LIVES,    "lives",     SFL_SORT_PRIO_SECONDARY);
400         ScoreInfo_SetLabel_PlayerScore(SP_LMS_RANK,     "rank",      SFL_LOWER_IS_BETTER | SFL_RANK | SFL_SORT_PRIO_PRIMARY | SFL_ALLOW_HIDE);
401         ScoreRules_basics_end();
402 }
403
404 void lms_Initialize()
405 {
406         lms_lowest_lives = 9999;
407         lms_next_place = 0;
408
409         lms_ScoreRules();
410 }
411
412
413 #endif