]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_lms.qc
LMS: remove unneeded usage of self in reset_map_players hook function
[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 "../../campaign.qh"
48 #include "../../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 float WinningCondition_LMS()
78 {
79         entity head, head2;
80         float have_player;
81         float have_players;
82         float l;
83
84         have_player = false;
85         have_players = false;
86         l = LMS_NewPlayerLives();
87
88         head = find(world, classname, "player");
89         if(head)
90                 have_player = true;
91         head2 = find(head, classname, "player");
92         if(head2)
93                 have_players = true;
94
95         if(have_player)
96         {
97                 // we have at least one player
98                 if(have_players)
99                 {
100                         // two or more active players - continue with the game
101                 }
102                 else
103                 {
104                         // exactly one player?
105
106                         ClearWinners();
107                         SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
108
109                         if(l)
110                         {
111                                 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
112                                 return WINNING_NO;
113                         }
114                         else
115                         {
116                                 // a winner!
117                                 // and assign him his first place
118                                 PlayerScore_Add(head, SP_LMS_RANK, 1);
119                                 return WINNING_YES;
120                         }
121                 }
122         }
123         else
124         {
125                 // nobody is playing at all...
126                 if(l)
127                 {
128                         // wait for players...
129                 }
130                 else
131                 {
132                         // SNAFU (maybe a draw game?)
133                         ClearWinners();
134                         LOG_TRACE("No players, ending game.\n");
135                         return WINNING_YES;
136                 }
137         }
138
139         // When we get here, we have at least two players who are actually LIVING,
140         // now check if the top two players have equal score.
141         WinningConditionHelper();
142
143         ClearWinners();
144         if(WinningConditionHelper_winner)
145                 WinningConditionHelper_winner.winning = true;
146         if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
147                 return WINNING_NEVER;
148
149         // Top two have different scores? Way to go for our beloved TIMELIMIT!
150         return WINNING_NO;
151 }
152
153 // mutator hooks
154 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
155 {
156         lms_lowest_lives = 999;
157         lms_next_place = player_count;
158
159         return false;
160 }
161
162 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
163 {
164         entity e;
165         if(restart_mapalreadyrestarted || (time < game_starttime))
166                 FOR_EACH_PLAYER(e)
167                 {
168                         PlayerScore_Add(e, SP_LMS_LIVES, LMS_NewPlayerLives());
169                 }
170
171         return false;
172 }
173
174 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
175 {SELFPARAM();
176         // player is dead and becomes observer
177         // FIXME fix LMS scoring for new system
178         if(PlayerScore_Add(self, SP_LMS_RANK, 0) > 0)
179         {
180                 self.classname = STR_OBSERVER;
181                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_LMS_NOLIVES);
182         }
183
184         return false;
185 }
186
187 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
188 {SELFPARAM();
189         self.respawn_flags |= RESPAWN_FORCE;
190
191         return false;
192 }
193
194 void lms_RemovePlayer(entity player)
195 {
196         // Only if the player cannot play at all
197         if(PlayerScore_Add(player, SP_LMS_RANK, 0) == 666)
198                 player.frags = FRAGS_SPECTATOR;
199         else
200                 player.frags = FRAGS_LMS_LOSER;
201
202         if(player.killcount != FRAGS_SPECTATOR)
203                 if(PlayerScore_Add(player, SP_LMS_RANK, 0) > 0 && player.lms_spectate_warning != 2)
204                         Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
205                 else
206                         Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
207 }
208
209 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
210 {SELFPARAM();
211         lms_RemovePlayer(self);
212         return false;
213 }
214
215 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
216 {SELFPARAM();
217         lms_RemovePlayer(self);
218         return false;
219 }
220
221 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
222 {SELFPARAM();
223         self.classname = STR_PLAYER;
224         campaign_bots_may_start = 1;
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(self.deadflag == DEAD_DYING)
238                 self.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         // remove a life
259         float tl;
260         tl = PlayerScore_Add(frag_target, SP_LMS_LIVES, -1);
261         if(tl < lms_lowest_lives)
262                 lms_lowest_lives = tl;
263         if(tl <= 0)
264         {
265                 if(!lms_next_place)
266                         lms_next_place = player_count;
267                 else
268                         lms_next_place = min(lms_next_place, player_count);
269                 PlayerScore_Add(frag_target, SP_LMS_RANK, lms_next_place); // won't ever spawn again
270                 --lms_next_place;
271         }
272         frag_score = 0;
273
274         return true;
275 }
276
277 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
278 {
279         start_items &= ~IT_UNLIMITED_AMMO;
280         start_health       = warmup_start_health       = cvar("g_lms_start_health");
281         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
282         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
283         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
284         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
285         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
286         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
287         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
288
289         return false;
290 }
291
292 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
293 {
294         // don't clear player score
295         return true;
296 }
297
298 MUTATOR_HOOKFUNCTION(lms, FilterItem)
299 {SELFPARAM();
300         if(autocvar_g_lms_extra_lives)
301         if(self.itemdef == ITEM_ExtraLife)
302                 return false;
303
304         return true;
305 }
306
307 void lms_extralife()
308 {SELFPARAM();
309         StartItem(this, ITEM_ExtraLife);
310 }
311
312 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
313 {SELFPARAM();
314         if (!autocvar_g_powerups) return false;
315         if (!autocvar_g_lms_extra_lives) return false;
316
317         // Can't use .itemdef here
318         if (self.classname != "item_health_mega") return false;
319
320         entity e = spawn();
321         e.think = lms_extralife;
322
323         e.nextthink = time + 0.1;
324         e.spawnflags = self.spawnflags;
325         e.noalign = self.noalign;
326         setorigin(e, self.origin);
327
328         return true;
329 }
330
331 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
332 {SELFPARAM();
333         if(self.itemdef == ITEM_ExtraLife)
334         {
335                 Send_Notification(NOTIF_ONE, other, MSG_CENTER, CENTER_EXTRALIVES);
336                 PlayerScore_Add(other, SP_LMS_LIVES, autocvar_g_lms_extra_lives);
337                 return MUT_ITEMTOUCH_PICKUP;
338         }
339
340         return MUT_ITEMTOUCH_CONTINUE;
341 }
342
343 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
344 {
345         entity head;
346         FOR_EACH_REALCLIENT(head)
347         {
348                 ++bot_activerealplayers;
349                 ++bot_realplayers;
350         }
351
352         return true;
353 }
354
355 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
356 {
357         if(self.lms_spectate_warning)
358         {
359                 // for the forfeit message...
360                 self.lms_spectate_warning = 2;
361                 // mark player as spectator
362                 PlayerScore_Add(self, SP_LMS_RANK, 666 - PlayerScore_Add(self, SP_LMS_RANK, 0));
363         }
364         else
365         {
366                 self.lms_spectate_warning = 1;
367                 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");
368                 return MUT_SPECCMD_RETURN;
369         }
370         return MUT_SPECCMD_CONTINUE;
371 }
372
373 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
374 {
375         ret_float = WinningCondition_LMS();
376         return true;
377 }
378
379 MUTATOR_HOOKFUNCTION(lms, WantWeapon)
380 {
381         want_allguns = true;
382         return false;
383 }
384
385 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
386 {
387         return true;
388 }
389
390 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
391 {
392         if(gameover)
393         if(score_field == SP_LMS_RANK)
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