Xonotic
sv_lms.qc
Go to the documentation of this file.
1 #include "sv_lms.qh"
2 
4 #include <server/campaign.qh>
5 #include <server/command/_mod.qh>
6 #include <server/world.qh>
7 #include <server/items/items.qh>
8 
15 
16 // main functions
18 {
19  int fl = floor(autocvar_fraglimit);
20  if(fl == 0 || fl > 999)
21  fl = 999;
22 
23  // first player has left the game for dying too much? Nobody else can get in.
24  if(lms_lowest_lives < 1)
25  return 0;
26 
29  return 0;
30 
31  return bound(1, lms_lowest_lives, fl);
32 }
33 
34 void ClearWinners();
35 
36 // LMS winning condition: game terminates if and only if there's at most one
37 // one player who's living lives. Top two scores being equal cancels the time
38 // limit.
40 {
41  if (warmup_stage || time <= game_starttime)
42  return WINNING_NO;
43 
44  entity first_player = NULL;
45  int totalplayers = 0;
46  FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
47  if (!totalplayers)
48  first_player = it;
49  ++totalplayers;
50  });
51 
52  if (totalplayers)
53  {
54  if (totalplayers > 1)
55  {
56  // two or more active players - continue with the game
57 
59  {
61  float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
62  if (!pl_lives)
63  return WINNING_YES; // human player lost, game over
64  break;
65  });
66  }
67  }
68  else
69  {
70  // exactly one player?
71 
72  ClearWinners();
73  SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
74 
75  if (LMS_NewPlayerLives())
76  {
77  // game still running (that is, nobody got removed from the game by a frag yet)? then continue
78  return WINNING_NO;
79  }
80  else
81  {
82  // a winner!
83  // and assign him his first place
84  GameRules_scoring_add(first_player, LMS_RANK, 1);
85  return WINNING_YES;
86  }
87  }
88  }
89  else
90  {
91  // nobody is playing at all...
92  if (LMS_NewPlayerLives())
93  {
94  // wait for players...
95  }
96  else
97  {
98  // SNAFU (maybe a draw game?)
99  ClearWinners();
100  LOG_TRACE("No players, ending game.");
101  return WINNING_YES;
102  }
103  }
104 
105  // When we get here, we have at least two players who are actually LIVING,
106  // now check if the top two players have equal score.
108 
109  ClearWinners();
111  WinningConditionHelper_winner.winning = true;
113  return WINNING_NEVER;
114 
115  // Top two have different scores? Way to go for our beloved TIMELIMIT!
116  return WINNING_NO;
117 }
118 
119 // mutator hooks
120 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
121 {
122  lms_lowest_lives = 999;
123 }
124 
125 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
126 {
127  FOREACH_CLIENT(true, {
128  if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
129  {
130  // players who forfeited (rank >= 256) become spectators
131  if (it.lms_spectate_warning == 2)
132  it.frags = FRAGS_SPECTATOR;
133  else
134  it.frags = FRAGS_PLAYER;
135  }
136 
137  CS(it).killcount = 0;
139  it.lms_spectate_warning = 0;
140  GameRules_scoring_add(it, LMS_RANK, -GameRules_scoring_add(it, LMS_RANK, 0));
141  GameRules_scoring_add(it, LMS_LIVES, -GameRules_scoring_add(it, LMS_LIVES, 0));
142 
143  if (it.frags != FRAGS_PLAYER)
144  continue;
145 
146  TRANSMUTE(Player, it);
147  PutClientInServer(it);
148  });
149 }
150 
151 // FIXME add support for sv_ready_restart_after_countdown
152 // that is find a way to respawn/reset players IN GAME without setting lives to 0
153 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
154 {
155  // incompatible
157 }
158 
159 // returns true if player is added to the game
160 bool lms_AddPlayer(entity player)
161 {
162  if (!INGAME(player))
163  {
164  int lives = GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
165  if(lives <= 0)
166  return false;
167  if (time < game_starttime)
169  else
170  INGAME_STATUS_SET(player, INGAME_STATUS_JOINING); // this is just to delay setting health and armor that can't be done here
171  }
172  if (warmup_stage || time <= game_starttime)
173  {
174  if(player.lms_spectate_warning)
175  {
176  player.lms_spectate_warning = 0;
177  GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
178  int lives = GameRules_scoring_add(player, LMS_LIVES, 0);
179  if(lives <= 0)
180  GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives());
181  }
182  }
183  else
184  {
185  if(GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
186  {
187  Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
188  return false;
189  }
190  }
191  return true;
192 }
193 
195 {
196  entity player = M_ARGV(0, entity);
197  if (!warmup_stage && (IS_BOT_CLIENT(player) || CS(player).jointime != time))
198  {
199  if (GameRules_scoring_add(player, LMS_RANK, 0) || !lms_AddPlayer(player))
200  TRANSMUTE(Observer, player);
201  }
202 }
203 
204 MUTATOR_HOOKFUNCTION(lms, PlayerSpawn)
205 {
206  entity player = M_ARGV(0, entity);
207 
208  if (warmup_stage || time < game_starttime)
209  return true;
210 
211  if (INGAME_JOINING(player))
212  {
213  // spawn player with the same amount of health / armor
214  // as the least healthy player with the least number of lives
215  int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
216  float min_health = start_health;
217  float min_armorvalue = start_armorvalue;
218  FOREACH_CLIENT(it != player && IS_PLAYER(it) && !IS_DEAD(it) && GameRules_scoring_add(it, LMS_LIVES, 0) == pl_lives, {
219  if (GetResource(it, RES_HEALTH) < min_health)
220  min_health = GetResource(it, RES_HEALTH);
221  if (GetResource(it, RES_ARMOR) < min_armorvalue)
222  min_armorvalue = GetResource(it, RES_ARMOR);
223  });
224  if (min_health != start_health)
225  SetResource(player, RES_HEALTH, max(1, min_health));
226  if (min_armorvalue != start_armorvalue)
227  SetResource(player, RES_ARMOR, min_armorvalue);
229  }
230 }
231 
232 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
233 {
234  entity player = M_ARGV(0, entity);
235 
236  if (warmup_stage || lms_AddPlayer(player))
237  return false;
238 
239  return true;
240 }
241 
242 MUTATOR_HOOKFUNCTION(lms, PlayerDies)
243 {
244  entity frag_target = M_ARGV(2, entity);
245 
246  float tl = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
247  if (tl <= 0)
248  {
249  frag_target.respawn_flags = RESPAWN_SILENT;
250  // prevent unwanted sudden rejoin as spectator and movement of spectator camera
251  frag_target.respawn_time = time + 2;
252  }
253  frag_target.respawn_flags |= RESPAWN_FORCE;
254 }
255 
257 {
258  if (warmup_stage || time < game_starttime)
259  return;
260 
261  float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
262  if (!player_rank)
263  {
264  if (player.lms_spectate_warning < 2)
265  {
266  player.frags = FRAGS_PLAYER_OUT_OF_GAME;
267  int pl_cnt = 0;
268  FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
269  pl_cnt++;
270  });
271  GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
272  }
273  else if (INGAME(player))
274  {
275  int min_forfeiter_rank = 665; // different from 666
276  FOREACH_CLIENT(it != player, {
277  // update rank of other players that were eliminated
278  if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
279  {
280  float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
281  if (it_rank > player_rank && it_rank <= 256)
282  GameRules_scoring_add(it, LMS_RANK, -1);
283  if (it_rank > 256 && it_rank <= min_forfeiter_rank)
284  min_forfeiter_rank = it_rank - 1;
285  }
286  else if (it.frags != FRAGS_SPECTATOR)
287  {
288  float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
289  if(tl < lms_lowest_lives)
290  lms_lowest_lives = tl;
291  }
292  });
293  GameRules_scoring_add(player, LMS_RANK, min_forfeiter_rank);
294  if(!warmup_stage)
295  GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
296  player.frags = FRAGS_PLAYER_OUT_OF_GAME;
297  TRANSMUTE(Observer, player);
298  }
299  }
300 
301  if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
302  {
303  if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
304  Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
305  else
306  Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
307  }
308 }
309 
311 {
312  entity player = M_ARGV(0, entity);
313 
314  // no further message other than the disconnect message
315  player.lms_spectate_warning = 3;
316 
317  lms_RemovePlayer(player);
318  INGAME_STATUS_CLEAR(player);
319 }
320 
321 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
322 {
323  entity player = M_ARGV(0, entity);
324  bool is_forced = M_ARGV(1, bool);
325 
326  if (!IS_PLAYER(player))
327  return true;
328 
329  if (warmup_stage || time <= game_starttime)
330  {
331  GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
332  player.frags = FRAGS_SPECTATOR;
333  TRANSMUTE(Observer, player);
334  INGAME_STATUS_CLEAR(player);
335  }
336  else
337  {
338  if (is_forced)
339  player.lms_spectate_warning = 2;
340  if (!GameRules_scoring_add(player, LMS_RANK, 0))
341  lms_RemovePlayer(player);
342  }
343  return true; // prevent team reset
344 }
345 
347 {
348  entity player = M_ARGV(0, entity);
349  player.frags = FRAGS_SPECTATOR;
350 }
351 
353 {
354  entity player = M_ARGV(0, entity);
355 
356  if(player.deadflag == DEAD_DYING)
357  player.deadflag = DEAD_RESPAWNING;
358 }
359 
360 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
361 {
363  M_ARGV(2, float) = 0;
364  if(!autocvar_g_lms_rot)
365  M_ARGV(3, float) = 0;
367 }
368 
369 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
370 {
371  // forbode!
372  return true;
373 }
374 
375 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
376 {
377  entity frag_target = M_ARGV(1, entity);
378 
379  if (!warmup_stage && time > game_starttime)
380  {
381  // remove a life
382  int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
383  if(tl < lms_lowest_lives)
384  lms_lowest_lives = tl;
385  if(tl <= 0)
386  {
387  int pl_cnt = 0;
388  FOREACH_CLIENT(IS_PLAYER(it) && it.frags == FRAGS_PLAYER, {
389  pl_cnt++;
390  });
391  frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
392  GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
393  }
394  }
395  M_ARGV(2, float) = 0; // frag score
396 
397  return true;
398 }
399 
400 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
401 {
402  start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
403  if(!cvar("g_use_ammunition"))
404  start_items |= IT_UNLIMITED_AMMO;
405 
406  start_health = warmup_start_health = cvar("g_lms_start_health");
407  start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
408  start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
409  start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
410  start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
411  start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
412  start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
413  start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
414 }
415 
416 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
417 {
418  // don't clear player score
419  return true;
420 }
421 
422 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
423 {
425  return false;
426 
427  entity definition = M_ARGV(0, entity);
428 
429  if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
430  {
431  return false;
432  }
433  return (autocvar_g_pickup_items <= 0); // only allow items if explicitly enabled
434 }
435 
437 {
438  StartItem(this, ITEM_ExtraLife);
439 }
440 
441 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
442 {
443  if (MUTATOR_RETURNVALUE) return false;
444  if (!autocvar_g_powerups) return false;
445  if (!autocvar_g_lms_extra_lives) return false;
446 
447  entity ent = M_ARGV(0, entity);
448 
449  // Can't use .itemdef here
450  if (ent.classname != "item_health_mega") return false;
451 
452  entity e = spawn();
454 
455  e.nextthink = time + 0.1;
456  e.spawnflags = ent.spawnflags;
457  e.noalign = ent.noalign;
458  setorigin(e, ent.origin);
459 
460  return true;
461 }
462 
463 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
464 {
465  if(MUTATOR_RETURNVALUE) return false;
466 
467  entity item = M_ARGV(0, entity);
468  entity toucher = M_ARGV(1, entity);
469 
470  if(item.itemdef == ITEM_ExtraLife)
471  {
472  Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
474  return MUT_ITEMTOUCH_PICKUP;
475  }
476 
477  return MUT_ITEMTOUCH_CONTINUE;
478 }
479 
481 {
483  if (INGAME(it) && it.lms_spectate_warning < 2)
484  ++M_ARGV(0, int); // activerealplayers
485  ++M_ARGV(1, int); // realplayers
486  });
487 
488  return true;
489 }
490 
491 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
492 {
493  entity player = M_ARGV(0, entity);
494 
495  if(warmup_stage || time < game_starttime || player.lms_spectate_warning)
496  {
497  // for the forfeit message...
498  player.lms_spectate_warning = 2;
499  }
500  else
501  {
502  if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
503  {
504  player.lms_spectate_warning = 1;
505  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");
506  }
507  return MUT_SPECCMD_RETURN;
508  }
509  return MUT_SPECCMD_CONTINUE;
510 }
511 
513 {
514  M_ARGV(0, float) = WinningCondition_LMS();
515  return true;
516 }
517 
518 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
519 {
520  if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
521  M_ARGV(0, string) = autocvar_g_lms_weaponarena;
522 }
523 
524 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
525 {
526  if(game_stopped)
527  if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
528  return true; // allow writing to this field in intermission as it is needed for newly joining players
529 }
530 
532 {
533  lms_lowest_lives = 999;
534 }
#define INGAME(it)
Definition: sv_rules.qh:20
MUTATOR_HOOKFUNCTION(lms, reset_map_global)
Definition: sv_lms.qc:120
float DEAD_DYING
Definition: progsdefs.qc:275
const int RESPAWN_FORCE
Definition: client.qh:330
float start_ammo_rockets
Definition: world.qh:87
int autocvar_g_lms_extra_lives
Definition: sv_lms.qc:9
#define INGAME_STATUS_SET(it, s)
Definition: sv_rules.qh:17
const int CBC_ORDER_EXCLUSIVE
Definition: base.qh:9
TRANSMUTE(Player, this)
void lms_Initialize()
Definition: sv_lms.qc:531
void ClientConnect(entity this)
ClientConnect
Definition: client.qc:1096
float start_ammo_fuel
Definition: world.qh:90
float warmup_start_ammo_shells
Definition: world.qh:104
#define INGAME_STATUS_JOINED
Definition: sv_rules.qh:13
void ClearWinners()
Definition: world.qc:1422
void SetWinners(.float field, float value)
Definition: world.qc:1407
entity() spawn
ClientState CS(Client this)
Definition: state.qh:47
#define FOREACH_CLIENT(cond, body)
Definition: utils.qh:49
void lms_extralife(entity this)
Definition: sv_lms.qc:436
#define GameRules_scoring_add(client, fld, value)
Definition: sv_rules.qh:78
void lms_RemovePlayer(entity player)
Definition: sv_lms.qc:256
bool warmup_stage
Definition: main.qh:103
int start_items
Definition: world.qh:84
float start_ammo_shells
Definition: world.qh:85
int autocvar_g_powerups
Definition: sv_powerups.qh:7
void CheckRules_World()
Definition: world.qc:1593
int killcount
Definition: client.qh:317
void PlayerPreThink(entity this)
Definition: client.qc:2402
float warmup_start_ammo_plasma
Definition: world.qh:108
ClientDisconnect(this)
const int WINNING_YES
Definition: world.qh:136
#define IS_REAL_CLIENT(v)
Definition: utils.qh:17
RES_HEALTH
Definition: ent_cs.qc:126
entity WinningConditionHelper_winner
the winning player, or NULL if none
Definition: scores.qh:113
bool autocvar_g_lms_rot
Definition: sv_lms.qc:14
float warmup_start_ammo_cells
Definition: world.qh:107
const int FRAGS_PLAYER
Definition: constants.qh:3
void SetResource(entity e, Resource res_type, float amount)
Sets the current amount of resource the given entity will have.
Definition: cl_resources.qc:26
float warmup_start_ammo_fuel
Definition: world.qh:109
float WinningConditionHelper_secondscore
second highest score
Definition: scores.qh:109
void StartItem(entity this, GameItem def)
Definition: items.qc:1148
float start_ammo_cells
Definition: world.qh:88
#define NULL
Definition: post.qh:17
float jointime
Definition: client.qh:64
const int WINNING_NEVER
Definition: world.qh:137
#define MUTATOR_RETURNVALUE
Definition: base.qh:303
#define M_ARGV(x, type)
Definition: events.qh:17
#define IS_DEAD(s)
Definition: utils.qh:26
#define INGAME_STATUS_CLEAR(it)
Definition: sv_rules.qh:18
float warmup_start_armorvalue
Definition: world.qh:111
int LMS_NewPlayerLives()
Definition: sv_lms.qc:17
float DEAD_RESPAWNING
Definition: progsdefs.qc:278
bool campaign_bots_may_start
campaign mode: bots shall spawn but wait for the player to spawn before they do anything in other gam...
Definition: campaign.qh:27
bool autocvar_g_lms_regenerate
Definition: sv_lms.qc:13
int lms_lowest_lives
Definition: sv_lms.qh:35
float GetResource(entity e, Resource res_type)
Returns the current amount of resource the given entity has.
Definition: cl_resources.qc:10
float start_health
Definition: world.qh:98
const int FRAGS_PLAYER_OUT_OF_GAME
Definition: constants.qh:5
bool autocvar_g_lms_items
Definition: sv_lms.qc:12
#define LOG_TRACE(...)
Definition: log.qh:81
float warmup_start_ammo_rockets
Definition: world.qh:106
bool autocvar_g_lms_join_anytime
Definition: sv_lms.qc:10
bool sv_ready_restart_after_countdown
Definition: world.qh:119
const int WINNING_NO
Definition: world.qh:135
int WinningCondition_LMS()
Definition: sv_lms.qc:39
const int FRAGS_SPECTATOR
Definition: constants.qh:4
float start_ammo_nails
Definition: world.qh:86
#define IS_BOT_CLIENT(v)
want: (IS_CLIENT(v) && !IS_REAL_CLIENT(v))
Definition: utils.qh:15
float warmup_start_health
Definition: world.qh:110
setorigin(ent, v)
#define setthink(e, f)
string autocvar_g_lms_weaponarena
Definition: sv_lms.qh:12
int autocvar_g_lms_last_join
Definition: sv_lms.qc:11
int autocvar_g_pickup_items
Definition: items.qh:11
PutClientInServer(this)
bool autocvar_g_campaign
Definition: campaign.qh:6
if(IS_DEAD(this))
Definition: impulse.qc:92
float WinningConditionHelper_topscore
highest score
Definition: scores.qh:108
float time
Definition: csprogsdefs.qc:16
float start_armorvalue
Definition: world.qh:99
#define INGAME_JOINING(it)
Definition: sv_rules.qh:22
float start_ammo_plasma
Definition: world.qh:89
bool lms_AddPlayer(entity player)
Definition: sv_lms.qc:160
float winning
Definition: world.qh:134
#define IS_PLAYER(v)
Definition: utils.qh:9
const int RESPAWN_SILENT
Definition: client.qh:331
#define INGAME_STATUS_JOINING
Definition: sv_rules.qh:12
void WinningConditionHelper(entity this)
Sets the following results for the current scores entities.
Definition: scores.qc:415
float warmup_start_ammo_nails
Definition: world.qh:105