]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_lms.qc
Merge branch 'master' into terencehill/ca_bots_fix
[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         // 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                                 PlayerScore_Add(head, SP_LMS_RANK, 1);
75                                 return WINNING_YES;
76                         }
77                 }
78         }
79         else
80         {
81                 // nobody is playing at all...
82                 if(l)
83                 {
84                         // wait for players...
85                 }
86                 else
87                 {
88                         // SNAFU (maybe a draw game?)
89                         ClearWinners();
90                         LOG_TRACE("No players, ending game.");
91                         return WINNING_YES;
92                 }
93         }
94
95         // When we get here, we have at least two players who are actually LIVING,
96         // now check if the top two players have equal score.
97         WinningConditionHelper(NULL);
98
99         ClearWinners();
100         if(WinningConditionHelper_winner)
101                 WinningConditionHelper_winner.winning = true;
102         if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
103                 return WINNING_NEVER;
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         lms_next_place = player_count;
114 }
115
116 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
117 {
118         if(restart_mapalreadyrestarted || (time < game_starttime))
119         FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(PlayerScore_Add(it, SP_LMS_LIVES, LMS_NewPlayerLives())));
120 }
121
122 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
123 {
124         entity player = M_ARGV(0, entity);
125
126         // player is dead and becomes observer
127         // FIXME fix LMS scoring for new system
128         if(PlayerScore_Add(player, SP_LMS_RANK, 0) > 0)
129         {
130                 TRANSMUTE(Observer, player);
131                 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
132         }
133 }
134
135 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
136 {
137         entity frag_target = M_ARGV(2, entity);
138
139         frag_target.respawn_flags |= RESPAWN_FORCE;
140 }
141
142 void lms_RemovePlayer(entity player)
143 {
144         // Only if the player cannot play at all
145         if(PlayerScore_Add(player, SP_LMS_RANK, 0) == 666)
146                 player.frags = FRAGS_SPECTATOR;
147         else
148         {
149                 if(IS_BOT_CLIENT(player))
150                         bot_clear(player);
151                 player.frags = FRAGS_LMS_LOSER;
152         }
153
154         if(player.killcount != FRAGS_SPECTATOR)
155                 if(PlayerScore_Add(player, SP_LMS_RANK, 0) > 0 && player.lms_spectate_warning != 2)
156                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
157                 else
158                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
159 }
160
161 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
162 {
163         entity player = M_ARGV(0, entity);
164
165         lms_RemovePlayer(player);
166 }
167
168 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
169 {
170     entity player = M_ARGV(0, entity);
171
172         lms_RemovePlayer(player);
173         return true;  // prevent team reset
174 }
175
176 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
177 {
178         entity player = M_ARGV(0, entity);
179
180         TRANSMUTE(Player, player);
181         campaign_bots_may_start = true;
182
183         if(PlayerScore_Add(player, SP_LMS_LIVES, LMS_NewPlayerLives()) <= 0)
184         {
185                 PlayerScore_Add(player, SP_LMS_RANK, 666);
186                 player.frags = FRAGS_SPECTATOR;
187         }
188 }
189
190 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
191 {
192         entity player = M_ARGV(0, entity);
193
194         if(player.deadflag == DEAD_DYING)
195                 player.deadflag = DEAD_RESPAWNING;
196 }
197
198 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
199 {
200         if(autocvar_g_lms_regenerate)
201                 return false;
202         return true;
203 }
204
205 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
206 {
207         // forbode!
208         return true;
209 }
210
211 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
212 {
213         entity frag_target = M_ARGV(1, entity);
214
215         // remove a life
216         float tl;
217         tl = PlayerScore_Add(frag_target, SP_LMS_LIVES, -1);
218         if(tl < lms_lowest_lives)
219                 lms_lowest_lives = tl;
220         if(tl <= 0)
221         {
222                 if(!lms_next_place)
223                         lms_next_place = player_count;
224                 else
225                         lms_next_place = min(lms_next_place, player_count);
226                 PlayerScore_Add(frag_target, SP_LMS_RANK, lms_next_place); // won't ever spawn again
227                 --lms_next_place;
228         }
229         M_ARGV(2, float) = 0;
230
231         return true;
232 }
233
234 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
235 {
236         start_items &= ~IT_UNLIMITED_AMMO;
237         start_health       = warmup_start_health       = cvar("g_lms_start_health");
238         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
239         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
240         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
241         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
242         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
243         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
244         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
245 }
246
247 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
248 {
249         // don't clear player score
250         return true;
251 }
252
253 MUTATOR_HOOKFUNCTION(lms, FilterItem)
254 {
255         entity item = M_ARGV(0, entity);
256
257         if(autocvar_g_lms_extra_lives)
258         if(item.itemdef == ITEM_ExtraLife)
259                 return false;
260
261         return true;
262 }
263
264 void lms_extralife(entity this)
265 {
266         StartItem(this, ITEM_ExtraLife);
267 }
268
269 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
270 {
271         if (!autocvar_g_powerups) return false;
272         if (!autocvar_g_lms_extra_lives) return false;
273
274         entity ent = M_ARGV(0, entity);
275
276         // Can't use .itemdef here
277         if (ent.classname != "item_health_mega") return false;
278
279         entity e = spawn();
280         setthink(e, lms_extralife);
281
282         e.nextthink = time + 0.1;
283         e.spawnflags = ent.spawnflags;
284         e.noalign = ent.noalign;
285         setorigin(e, ent.origin);
286
287         return true;
288 }
289
290 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
291 {
292         entity item = M_ARGV(0, entity);
293         entity toucher = M_ARGV(1, entity);
294
295         if(item.itemdef == ITEM_ExtraLife)
296         {
297                 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES);
298                 PlayerScore_Add(toucher, SP_LMS_LIVES, autocvar_g_lms_extra_lives);
299                 return MUT_ITEMTOUCH_PICKUP;
300         }
301
302         return MUT_ITEMTOUCH_CONTINUE;
303 }
304
305 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
306 {
307         FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
308                 ++M_ARGV(0, int);
309                 ++M_ARGV(1, int);
310         ));
311
312         return true;
313 }
314
315 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
316 {
317     entity player = M_ARGV(0, entity);
318
319         if(player.lms_spectate_warning)
320         {
321                 // for the forfeit message...
322                 player.lms_spectate_warning = 2;
323                 // mark player as spectator
324                 PlayerScore_Add(player, SP_LMS_RANK, 666 - PlayerScore_Add(player, SP_LMS_RANK, 0));
325         }
326         else
327         {
328                 player.lms_spectate_warning = 1;
329                 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");
330                 return MUT_SPECCMD_RETURN;
331         }
332         return MUT_SPECCMD_CONTINUE;
333 }
334
335 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
336 {
337         M_ARGV(0, float) = WinningCondition_LMS();
338         return true;
339 }
340
341 MUTATOR_HOOKFUNCTION(lms, WantWeapon)
342 {
343         M_ARGV(2, bool) = true; // all weapons
344 }
345
346 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
347 {
348         return true;
349 }
350
351 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
352 {
353         if(gameover)
354         if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
355                 return true; // allow writing to this field in intermission as it is needed for newly joining players
356 }
357
358 // scoreboard stuff
359 void lms_ScoreRules()
360 {
361         ScoreRules_basics(0, 0, 0, false);
362         ScoreInfo_SetLabel_PlayerScore(SP_LMS_LIVES,    "lives",     SFL_SORT_PRIO_SECONDARY);
363         ScoreInfo_SetLabel_PlayerScore(SP_LMS_RANK,     "rank",      SFL_LOWER_IS_BETTER | SFL_RANK | SFL_SORT_PRIO_PRIMARY | SFL_ALLOW_HIDE);
364         ScoreRules_basics_end();
365 }
366
367 void lms_Initialize()
368 {
369         lms_lowest_lives = 9999;
370         lms_next_place = 0;
371
372         lms_ScoreRules();
373 }