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