]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/lms/sv_lms.qc
Merge branch 'master' into terencehill/lms_updates
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / lms / sv_lms.qc
1 #include "sv_lms.qh"
2
3 #include <common/mutators/mutator/instagib/items.qh>
4 #include <server/campaign.qh>
5 #include <server/command/_mod.qh>
6 #include <server/world.qh>
7 #include <server/items/items.qh>
8
9 int autocvar_g_lms_extra_lives;
10 bool autocvar_g_lms_join_anytime;
11 int autocvar_g_lms_last_join;
12 bool autocvar_g_lms_regenerate;
13 int autocvar_g_lms_leader_wp_lives;
14 float autocvar_g_lms_leader_wp_max_relative;
15 float autocvar_g_lms_leader_wp_time;
16 float autocvar_g_lms_leader_wp_time_repeat;
17 float autocvar_g_lms_dynamic_respawn_delay;
18 float autocvar_g_lms_dynamic_respawn_delay_base;
19 float autocvar_g_lms_dynamic_respawn_delay_increase;
20 bool autocvar_g_lms_dynamic_vampire;
21 float autocvar_g_lms_dynamic_vampire_factor_base;
22 float autocvar_g_lms_dynamic_vampire_factor_increase;
23 float autocvar_g_lms_dynamic_vampire_factor_max;
24 int autocvar_g_lms_dynamic_vampire_min_lives_diff;
25
26 .float lms_wp_time;
27
28 // main functions
29 int LMS_NewPlayerLives()
30 {
31         int fl = floor(autocvar_fraglimit);
32         if(fl == 0 || fl > 999)
33                 fl = 999;
34
35         // first player has left the game for dying too much? Nobody else can get in.
36         if(lms_lowest_lives < 1)
37                 return 0;
38
39         if(!autocvar_g_lms_join_anytime)
40                 if(lms_lowest_lives < fl - max(0, floor(autocvar_g_lms_last_join)))
41                         return 0;
42
43         return bound(1, lms_lowest_lives, fl);
44 }
45
46 void ClearWinners();
47
48 // LMS winning condition: game terminates if and only if there's at most one
49 // one player who's living lives. Top two scores being equal cancels the time
50 // limit.
51 int WinningCondition_LMS()
52 {
53         entity first_player = NULL;
54         int totalplayers = 0;
55         FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
56                 if (!totalplayers)
57                         first_player = it;
58                 ++totalplayers;
59         });
60
61         if (totalplayers)
62         {
63                 if (totalplayers > 1)
64                 {
65                         // two or more active players - continue with the game
66
67                         if (autocvar_g_campaign)
68                         {
69                                 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
70                                         float pl_lives = GameRules_scoring_add(it, LMS_LIVES, 0);
71                                         if (!pl_lives)
72                                                 return WINNING_YES; // human player lost, game over
73                                         break;
74                                 });
75                         }
76                 }
77                 else
78                 {
79                         // exactly one player?
80
81                         ClearWinners();
82                         SetWinners(winning, 0); // NOTE: exactly one player is still "player", so this works out
83
84                         if (LMS_NewPlayerLives())
85                         {
86                                 // game still running (that is, nobody got removed from the game by a frag yet)? then continue
87                                 return WINNING_NO;
88                         }
89                         else
90                         {
91                                 // a winner!
92                                 // and assign him his first place
93                                 GameRules_scoring_add(first_player, LMS_RANK, 1);
94                                 if(warmup_stage)
95                                         return WINNING_NO;
96                                 else
97                                         return WINNING_YES;
98                         }
99                 }
100         }
101         else
102         {
103                 // nobody is playing at all...
104                 if (LMS_NewPlayerLives())
105                 {
106                         // wait for players...
107                 }
108                 else
109                 {
110                         // SNAFU (maybe a draw game?)
111                         ClearWinners();
112                         LOG_TRACE("No players, ending game.");
113                         return WINNING_YES;
114                 }
115         }
116
117         // When we get here, we have at least two players who are actually LIVING,
118         // now check if the top two players have equal score.
119         WinningConditionHelper(NULL);
120
121         ClearWinners();
122         if(WinningConditionHelper_winner)
123                 WinningConditionHelper_winner.winning = true;
124         if(WinningConditionHelper_topscore == WinningConditionHelper_secondscore)
125                 return WINNING_NEVER;
126
127         // Top two have different scores? Way to go for our beloved TIMELIMIT!
128         return WINNING_NO;
129 }
130
131 // mutator hooks
132 MUTATOR_HOOKFUNCTION(lms, reset_map_global)
133 {
134         lms_lowest_lives = 999;
135 }
136
137 MUTATOR_HOOKFUNCTION(lms, reset_map_players)
138 {
139         FOREACH_CLIENT(true, {
140                 TRANSMUTE(Player, it);
141                 it.frags = FRAGS_PLAYER;
142                 GameRules_scoring_add(it, LMS_LIVES, LMS_NewPlayerLives());
143                 PutClientInServer(it);
144                 if (it.waypointsprite_attachedforcarrier)
145                         WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
146         });
147 }
148
149 // FIXME add support for sv_ready_restart_after_countdown
150 // that is find a way to respawn/reset players IN GAME without setting lives to 0
151 MUTATOR_HOOKFUNCTION(lms, ReadLevelCvars)
152 {
153         // incompatible
154         sv_ready_restart_after_countdown = 0;
155 }
156
157 MUTATOR_HOOKFUNCTION(lms, PutClientInServer)
158 {
159         entity player = M_ARGV(0, entity);
160
161         if(player.frags == FRAGS_SPECTATOR)
162                 TRANSMUTE(Observer, player);
163         else
164         {
165                 float tl = GameRules_scoring_add(player, LMS_LIVES, 0);
166                 if(tl < lms_lowest_lives)
167                         lms_lowest_lives = tl;
168                 if(tl <= 0)
169                         TRANSMUTE(Observer, player);
170                 if(warmup_stage)
171                         GameRules_scoring_add(player, LMS_RANK, -GameRules_scoring_add(player, LMS_RANK, 0));
172         }
173 }
174
175 MUTATOR_HOOKFUNCTION(lms, CalculateRespawnTime)
176 {
177         entity player = M_ARGV(0, entity);
178         player.respawn_flags |= RESPAWN_FORCE;
179
180         if (autocvar_g_lms_dynamic_respawn_delay <= 0)
181                 return false;
182
183         int pl_lives = GameRules_scoring_add(player, LMS_LIVES, 0);
184         int max_lives = 0;
185         int pl_cnt = 0;
186         FOREACH_CLIENT(it != player && IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
187                 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
188                 if (lives > max_lives)
189                         max_lives = lives;
190                 pl_cnt++;
191         });
192
193         // min delay with only 2 players
194         if (pl_cnt == 1) // player wasn't counted
195                 max_lives = 0;
196
197         player.respawn_time = time + autocvar_g_lms_dynamic_respawn_delay_base +
198                 autocvar_g_lms_dynamic_respawn_delay_increase * max(0, max_lives - pl_lives);
199         return true;
200 }
201
202 MUTATOR_HOOKFUNCTION(lms, ForbidSpawn)
203 {
204         entity player = M_ARGV(0, entity);
205
206         if(warmup_stage)
207                 return false;
208         if(player.frags == FRAGS_SPECTATOR || GameRules_scoring_add(player, LMS_LIVES, 0) <= 0)
209         {
210                 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_LMS_NOLIVES);
211                 return true;
212         }
213         return false;
214 }
215
216 void lms_RemovePlayer(entity player)
217 {
218         static int quitters = 0;
219         float player_rank = GameRules_scoring_add(player, LMS_RANK, 0);
220         if (!player_rank)
221         {
222                 if (player.lms_spectate_warning < 2)
223                 {
224                         player.frags = FRAGS_PLAYER_OUT_OF_GAME;
225                         int pl_cnt = 0;
226                         FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
227                                 pl_cnt++;
228                         });
229                         GameRules_scoring_add(player, LMS_RANK, pl_cnt + 1);
230                 }
231                 else
232                 {
233                         FOREACH_CLIENT(true, {
234                                 if (it.frags == FRAGS_PLAYER_OUT_OF_GAME)
235                                 {
236                                         float it_rank = GameRules_scoring_add(it, LMS_RANK, 0);
237                                         if (it_rank > player_rank && it_rank <= 256)
238                                                 GameRules_scoring_add(it, LMS_RANK, -1);
239                                 }
240                                 else if (it.frags != FRAGS_SPECTATOR)
241                                 {
242                                         float tl = GameRules_scoring_add(it, LMS_LIVES, 0);
243                                         if(tl < lms_lowest_lives)
244                                                 lms_lowest_lives = tl;
245                                 }
246                         });
247                         GameRules_scoring_add(player, LMS_RANK, 665 - quitters); // different from 666
248                         if(!warmup_stage)
249                         {
250                                 GameRules_scoring_add(player, LMS_LIVES, -GameRules_scoring_add(player, LMS_LIVES, 0));
251                                 ++quitters;
252                         }
253                         player.frags = FRAGS_PLAYER_OUT_OF_GAME;
254                         TRANSMUTE(Observer, player);
255                 }
256         }
257
258         if (CS(player).killcount != FRAGS_SPECTATOR && player.lms_spectate_warning < 3)
259         {
260                 if (GameRules_scoring_add(player, LMS_RANK, 0) > 0 && player.lms_spectate_warning < 2)
261                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_NOLIVES, player.netname);
262                 else
263                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_LMS_FORFEIT, player.netname);
264         }
265 }
266
267 MUTATOR_HOOKFUNCTION(lms, ClientDisconnect)
268 {
269         entity player = M_ARGV(0, entity);
270
271         // no further message other than the disconnect message
272         player.lms_spectate_warning = 3;
273
274         lms_RemovePlayer(player);
275 }
276
277 MUTATOR_HOOKFUNCTION(lms, MakePlayerObserver)
278 {
279         entity player = M_ARGV(0, entity);
280
281         if (!IS_PLAYER(player))
282                 return true;
283
284         lms_RemovePlayer(player);
285         return true;  // prevent team reset
286 }
287
288 MUTATOR_HOOKFUNCTION(lms, ClientConnect)
289 {
290         entity player = M_ARGV(0, entity);
291
292         if(GameRules_scoring_add(player, LMS_LIVES, LMS_NewPlayerLives()) <= 0)
293         {
294                 GameRules_scoring_add(player, LMS_RANK, 666); // mark as forced spectator for the hud code
295                 player.frags = FRAGS_SPECTATOR;
296         }
297 }
298
299 // FIXME LMS doesn't allow clients to spectate due to its particular implementation
300 MUTATOR_HOOKFUNCTION(lms, AutoJoinOnConnection)
301 {
302         if(autocvar_g_campaign)
303                 return false;
304         return true;
305 }
306
307 MUTATOR_HOOKFUNCTION(lms, PlayerPreThink)
308 {
309         entity player = M_ARGV(0, entity);
310
311         if(player.deadflag == DEAD_DYING)
312                 player.deadflag = DEAD_RESPAWNING;
313 }
314
315 MUTATOR_HOOKFUNCTION(lms, PlayerRegen)
316 {
317         if(autocvar_g_lms_regenerate)
318                 return false;
319         return true;
320 }
321
322 MUTATOR_HOOKFUNCTION(lms, ForbidThrowCurrentWeapon)
323 {
324         // forbode!
325         return true;
326 }
327
328 MUTATOR_HOOKFUNCTION(lms, Damage_Calculate)
329 {
330         if (!autocvar_g_lms_dynamic_vampire)
331                 return;
332
333         entity frag_attacker = M_ARGV(1, entity);
334         entity frag_target = M_ARGV(2, entity);
335         float frag_damage = M_ARGV(4, float);
336
337         if (IS_PLAYER(frag_attacker) && !IS_DEAD(frag_target) && frag_attacker != frag_target)
338         {
339                 float vampire_factor = 0;
340
341                 int frag_attacker_lives = GameRules_scoring_add(frag_attacker, LMS_LIVES, 0);
342                 int frag_target_lives = GameRules_scoring_add(frag_target, LMS_LIVES, 0);
343                 int diff = frag_target_lives - frag_attacker_lives - autocvar_g_lms_dynamic_vampire_min_lives_diff;
344
345                 if (diff >= 0)
346                         vampire_factor = autocvar_g_lms_dynamic_vampire_factor_base + diff * autocvar_g_lms_dynamic_vampire_factor_increase;
347                 if (vampire_factor > 0)
348                 {
349                         vampire_factor = min(vampire_factor, autocvar_g_lms_dynamic_vampire_factor_max);
350                         SetResourceExplicit(frag_attacker, RES_HEALTH,
351                                 min(GetResource(frag_attacker, RES_HEALTH) + frag_damage * vampire_factor, start_health));
352                 }
353         }
354 }
355
356 bool lms_waypointsprite_visible_for_player(entity this, entity player, entity view) // runs on waypoints which are attached to ballcarriers, updates once per frame
357 {
358         if(view.lms_wp_time)
359                 if(IS_SPEC(player))
360                         return false; // we don't want spectators of leaders to see the attached waypoint on the top of their screen
361
362         float leader_time = autocvar_g_lms_leader_wp_time;
363         float leader_repeat_time = leader_time + autocvar_g_lms_leader_wp_time_repeat;
364         float wp_time = this.owner.lms_wp_time;
365         if (wp_time && (time - wp_time) % leader_repeat_time > leader_time)
366                 return false;
367
368         return true;
369 }
370
371 void lms_UpdateWaypoints()
372 {
373         int max_lives = 0;
374         int pl_cnt = 0;
375         FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
376                 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
377                 if (lives > max_lives)
378                         max_lives = lives;
379                 pl_cnt++;
380         });
381
382         int second_max_lives = 0;
383         int pl_cnt_with_max_lives = 0;
384         FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
385                 int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
386                 if (lives == max_lives)
387                         pl_cnt_with_max_lives++;
388                 else if (lives > second_max_lives)
389                         second_max_lives = lives;
390         });
391
392         int lives_diff = autocvar_g_lms_leader_wp_lives;
393         if (max_lives - second_max_lives >= lives_diff && pl_cnt_with_max_lives <= pl_cnt * autocvar_g_lms_leader_wp_max_relative)
394                 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
395                         int lives = GameRules_scoring_add(it, LMS_LIVES, 0);
396                         if (lives == max_lives)
397                         {
398                                 if (!it.waypointsprite_attachedforcarrier)
399                                 {
400                                         WaypointSprite_AttachCarrier(WP_LmsLeader, it, RADARICON_FLAGCARRIER);
401                                         it.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = lms_waypointsprite_visible_for_player;
402                                         WaypointSprite_UpdateRule(it.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
403                                         vector pl_color = colormapPaletteColor(it.clientcolors & 0x0F, false);
404                                         WaypointSprite_UpdateTeamRadar(it.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, pl_color);
405                                         WaypointSprite_Ping(it.waypointsprite_attachedforcarrier);
406                                 }
407                                 if (!it.lms_wp_time)
408                                         it.lms_wp_time = time;
409                         }
410                         else
411                         {
412                                 if (it.waypointsprite_attachedforcarrier)
413                                         WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
414                                 it.lms_wp_time = 0;
415                         }
416                 });
417         else
418                 FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
419                         if (it.waypointsprite_attachedforcarrier)
420                                 WaypointSprite_Kill(it.waypointsprite_attachedforcarrier);
421                         it.lms_wp_time = 0;
422                 });
423 }
424
425 MUTATOR_HOOKFUNCTION(lms, PlayerDied)
426 {
427         if (!warmup_stage && autocvar_g_lms_leader_wp_lives > 0)
428                 lms_UpdateWaypoints();
429 }
430
431 MUTATOR_HOOKFUNCTION(lms, GiveFragsForKill)
432 {
433         entity frag_target = M_ARGV(1, entity);
434
435         if (!warmup_stage)
436         {
437                 // remove a life
438                 int tl = GameRules_scoring_add(frag_target, LMS_LIVES, -1);
439                 if(tl < lms_lowest_lives)
440                         lms_lowest_lives = tl;
441                 if(tl <= 0)
442                 {
443                         int pl_cnt = 0;
444                         FOREACH_CLIENT(IS_PLAYER(it) && it.frags != FRAGS_PLAYER_OUT_OF_GAME, {
445                                 pl_cnt++;
446                         });
447                         frag_target.frags = FRAGS_PLAYER_OUT_OF_GAME;
448                         GameRules_scoring_add(frag_target, LMS_RANK, pl_cnt);
449                 }
450         }
451         M_ARGV(2, float) = 0; // frag score
452
453         return true;
454 }
455
456 MUTATOR_HOOKFUNCTION(lms, SetStartItems)
457 {
458         start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
459         start_health       = warmup_start_health       = cvar("g_lms_start_health");
460         start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
461         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
462         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
463         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
464         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
465         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
466         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
467 }
468
469 MUTATOR_HOOKFUNCTION(lms, ForbidPlayerScore_Clear)
470 {
471         // don't clear player score
472         return true;
473 }
474
475 MUTATOR_HOOKFUNCTION(lms, FilterItemDefinition)
476 {
477         entity definition = M_ARGV(0, entity);
478
479         if (autocvar_g_lms_extra_lives && definition == ITEM_ExtraLife)
480         {
481                 return false;
482         }
483         return true;
484 }
485
486 void lms_extralife(entity this)
487 {
488         StartItem(this, ITEM_ExtraLife);
489 }
490
491 MUTATOR_HOOKFUNCTION(lms, OnEntityPreSpawn)
492 {
493         if (MUTATOR_RETURNVALUE) return false;
494         if (!autocvar_g_powerups) return false;
495         if (!autocvar_g_lms_extra_lives) return false;
496
497         entity ent = M_ARGV(0, entity);
498
499         // Can't use .itemdef here
500         if (ent.classname != "item_health_mega") return false;
501
502         entity e = spawn();
503         setthink(e, lms_extralife);
504
505         e.nextthink = time + 0.1;
506         e.spawnflags = ent.spawnflags;
507         e.noalign = ent.noalign;
508         setorigin(e, ent.origin);
509
510         return true;
511 }
512
513 MUTATOR_HOOKFUNCTION(lms, ItemTouch)
514 {
515         if(MUTATOR_RETURNVALUE) return false;
516
517         entity item = M_ARGV(0, entity);
518         entity toucher = M_ARGV(1, entity);
519
520         if(item.itemdef == ITEM_ExtraLife)
521         {
522                 Send_Notification(NOTIF_ONE, toucher, MSG_CENTER, CENTER_EXTRALIVES, autocvar_g_lms_extra_lives);
523                 GameRules_scoring_add(toucher, LMS_LIVES, autocvar_g_lms_extra_lives);
524                 return MUT_ITEMTOUCH_PICKUP;
525         }
526
527         return MUT_ITEMTOUCH_CONTINUE;
528 }
529
530 MUTATOR_HOOKFUNCTION(lms, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
531 {
532         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
533                 ++M_ARGV(0, int); // activerealplayers
534                 ++M_ARGV(1, int); // realplayers
535         });
536
537         return true;
538 }
539
540 MUTATOR_HOOKFUNCTION(lms, ClientCommand_Spectate)
541 {
542         entity player = M_ARGV(0, entity);
543
544         if(warmup_stage || player.lms_spectate_warning)
545         {
546                 // for the forfeit message...
547                 player.lms_spectate_warning = 2;
548         }
549         else
550         {
551                 if(player.frags != FRAGS_SPECTATOR && player.frags != FRAGS_PLAYER_OUT_OF_GAME)
552                 {
553                         player.lms_spectate_warning = 1;
554                         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");
555                 }
556                 return MUT_SPECCMD_RETURN;
557         }
558         return MUT_SPECCMD_CONTINUE;
559 }
560
561 MUTATOR_HOOKFUNCTION(lms, CheckRules_World)
562 {
563         M_ARGV(0, float) = WinningCondition_LMS();
564         return true;
565 }
566
567 MUTATOR_HOOKFUNCTION(lms, SetWeaponArena)
568 {
569         if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
570                 M_ARGV(0, string) = autocvar_g_lms_weaponarena;
571 }
572
573 MUTATOR_HOOKFUNCTION(lms, GetPlayerStatus)
574 {
575         return true;
576 }
577
578 MUTATOR_HOOKFUNCTION(lms, AddPlayerScore)
579 {
580         if(game_stopped)
581         if(M_ARGV(0, entity) == SP_LMS_RANK) // score field
582                 return true; // allow writing to this field in intermission as it is needed for newly joining players
583 }
584
585 void lms_Initialize()
586 {
587         lms_lowest_lives = 999;
588 }