]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_lms.qc
Merge branch 'terencehill/menu_optimization' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator / gamemode_lms.qc
1 #ifndef GAMEMODE_LMS_H
2 #define GAMEMODE_LMS_H
3
4 #define autocvar_g_lms_lives_override cvar("g_lms_lives_override")
5 void lms_Initialize();
6
7 REGISTER_MUTATOR(lms, false)
8 {
9         MUTATOR_ONADD
10         {
11                 if (time > 1) // game loads at time 1
12                         error("This is a game type and it cannot be added at runtime.");
13                 lms_Initialize();
14
15                 SetLimits(((!autocvar_g_lms_lives_override) ? -1 : autocvar_g_lms_lives_override), 0, -1, -1);
16         }
17
18         MUTATOR_ONROLLBACK_OR_REMOVE
19         {
20                 // we actually cannot roll back lms_Initialize here
21                 // BUT: we don't need to! If this gets called, adding always
22                 // succeeds.
23         }
24
25         MUTATOR_ONREMOVE
26         {
27                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
28                 return -1;
29         }
30
31         return 0;
32 }
33
34 // scoreboard stuff
35 const float SP_LMS_LIVES = 4;
36 const float SP_LMS_RANK = 5;
37
38 // lives related defs
39 float lms_lowest_lives;
40 float lms_next_place;
41 float LMS_NewPlayerLives();
42
43 #endif
44
45 #ifdef IMPLEMENTATION
46
47 #include <server/campaign.qh>
48 #include <server/command/cmd.qh>
49
50 int autocvar_g_lms_extra_lives;
51 bool autocvar_g_lms_join_anytime;
52 int autocvar_g_lms_last_join;
53 bool autocvar_g_lms_regenerate;
54
55 // main functions
56 float LMS_NewPlayerLives()
57 {
58         float fl;
59         fl = autocvar_fraglimit;
60         if(fl == 0)
61                 fl = 999;
62
63         // first player has left the game for dying too much? Nobody else can get in.
64         if(lms_lowest_lives < 1)
65                 return 0;
66
67         if(!autocvar_g_lms_join_anytime)
68                 if(lms_lowest_lives < fl - autocvar_g_lms_last_join)
69                         return 0;
70
71         return bound(1, lms_lowest_lives, fl);
72 }
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(world, 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();
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         return false;
157 }
158
159 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
160 {SELFPARAM();
161         if(restart_mapalreadyrestarted || (time < game_starttime))
162         FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(WITH(entity, self, it, PlayerScore_Add(it, SP_LMS_LIVES, LMS_NewPlayerLives()))));
163
164         return false;
165 }
166
167 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
168 {SELFPARAM();
169         // player is dead and becomes observer
170         // FIXME fix LMS scoring for new system
171         if(PlayerScore_Add(self, SP_LMS_RANK, 0) > 0)
172         {
173                 self.classname = STR_OBSERVER;
174                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_LMS_NOLIVES);
175         }
176
177         return false;
178 }
179
180 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
181 {SELFPARAM();
182         self.respawn_flags |= RESPAWN_FORCE;
183
184         return false;
185 }
186
187 void lms_RemovePlayer(entity player)
188 {
189         // Only if the player cannot play at all
190         if(PlayerScore_Add(player, SP_LMS_RANK, 0) == 666)
191                 player.frags = FRAGS_SPECTATOR;
192         else
193                 player.frags = FRAGS_LMS_LOSER;
194
195         if(player.killcount != FRAGS_SPECTATOR)
196                 if(PlayerScore_Add(player, SP_LMS_RANK, 0) > 0 && player.lms_spectate_warning != 2)
197                         Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
198                 else
199                         Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
200 }
201
202 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
203 {SELFPARAM();
204         lms_RemovePlayer(self);
205         return false;
206 }
207
208 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
209 {SELFPARAM();
210         lms_RemovePlayer(self);
211         return false;
212 }
213
214 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
215 {SELFPARAM();
216         self.classname = STR_PLAYER;
217         campaign_bots_may_start = 1;
218
219         if(PlayerScore_Add(self, SP_LMS_LIVES, LMS_NewPlayerLives()) <= 0)
220         {
221                 PlayerScore_Add(self, SP_LMS_RANK, 666);
222                 self.frags = FRAGS_SPECTATOR;
223         }
224
225         return false;
226 }
227
228 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
229 {SELFPARAM();
230         if(self.deadflag == DEAD_DYING)
231                 self.deadflag = DEAD_RESPAWNING;
232
233         return false;
234 }
235
236 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
237 {
238         if(autocvar_g_lms_regenerate)
239                 return false;
240         return true;
241 }
242
243 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
244 {
245         // forbode!
246         return true;
247 }
248
249 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
250 {
251         // remove a life
252         float tl;
253         tl = PlayerScore_Add(frag_target, SP_LMS_LIVES, -1);
254         if(tl < lms_lowest_lives)
255                 lms_lowest_lives = tl;
256         if(tl <= 0)
257         {
258                 if(!lms_next_place)
259                         lms_next_place = player_count;
260                 else
261                         lms_next_place = min(lms_next_place, player_count);
262                 PlayerScore_Add(frag_target, SP_LMS_RANK, lms_next_place); // won't ever spawn again
263                 --lms_next_place;
264         }
265         frag_score = 0;
266
267         return true;
268 }
269
270 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
271 {
272         start_items &= ~IT_UNLIMITED_AMMO;
273         start_health       = warmup_start_health       = cvar("g_lms_start_health");
274         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
275         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
276         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
277         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
278         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
279         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
280         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
281
282         return false;
283 }
284
285 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
286 {
287         // don't clear player score
288         return true;
289 }
290
291 MUTATOR_HOOKFUNCTION(lms, FilterItem)
292 {SELFPARAM();
293         if(autocvar_g_lms_extra_lives)
294         if(self.itemdef == ITEM_ExtraLife)
295                 return false;
296
297         return true;
298 }
299
300 void lms_extralife()
301 {SELFPARAM();
302         StartItem(this, ITEM_ExtraLife);
303 }
304
305 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
306 {SELFPARAM();
307         if (!autocvar_g_powerups) return false;
308         if (!autocvar_g_lms_extra_lives) return false;
309
310         // Can't use .itemdef here
311         if (self.classname != "item_health_mega") return false;
312
313         entity e = spawn();
314         e.think = lms_extralife;
315
316         e.nextthink = time + 0.1;
317         e.spawnflags = self.spawnflags;
318         e.noalign = self.noalign;
319         setorigin(e, self.origin);
320
321         return true;
322 }
323
324 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
325 {SELFPARAM();
326         if(self.itemdef == ITEM_ExtraLife)
327         {
328                 Send_Notification(NOTIF_ONE, other, MSG_CENTER, CENTER_EXTRALIVES);
329                 PlayerScore_Add(other, SP_LMS_LIVES, autocvar_g_lms_extra_lives);
330                 return MUT_ITEMTOUCH_PICKUP;
331         }
332
333         return MUT_ITEMTOUCH_CONTINUE;
334 }
335
336 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
337 {
338         FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
339                 ++bot_activerealplayers;
340                 ++bot_realplayers;
341         ));
342
343         return true;
344 }
345
346 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
347 {
348         if(self.lms_spectate_warning)
349         {
350                 // for the forfeit message...
351                 self.lms_spectate_warning = 2;
352                 // mark player as spectator
353                 PlayerScore_Add(self, SP_LMS_RANK, 666 - PlayerScore_Add(self, SP_LMS_RANK, 0));
354         }
355         else
356         {
357                 self.lms_spectate_warning = 1;
358                 sprint(self, "WARNING: you won't be able to enter the game again after spectating in LMS. Use the same command again to spectate anyway.\n");
359                 return MUT_SPECCMD_RETURN;
360         }
361         return MUT_SPECCMD_CONTINUE;
362 }
363
364 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
365 {
366         ret_float = WinningCondition_LMS();
367         return true;
368 }
369
370 MUTATOR_HOOKFUNCTION(lms, WantWeapon)
371 {
372         want_allguns = true;
373         return false;
374 }
375
376 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
377 {
378         return true;
379 }
380
381 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
382 {
383         if(gameover)
384         if(score_field == SP_LMS_RANK)
385                 return true; // allow writing to this field in intermission as it is needed for newly joining players
386         return false;
387 }
388
389 // scoreboard stuff
390 void lms_ScoreRules()
391 {
392         ScoreRules_basics(0, 0, 0, false);
393         ScoreInfo_SetLabel_PlayerScore(SP_LMS_LIVES,    "lives",     SFL_SORT_PRIO_SECONDARY);
394         ScoreInfo_SetLabel_PlayerScore(SP_LMS_RANK,     "rank",      SFL_LOWER_IS_BETTER | SFL_RANK | SFL_SORT_PRIO_PRIMARY | SFL_ALLOW_HIDE);
395         ScoreRules_basics_end();
396 }
397
398 void lms_Initialize()
399 {
400         lms_lowest_lives = 9999;
401         lms_next_place = 0;
402
403         lms_ScoreRules();
404 }
405
406
407 #endif