]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/clanarena/sv_clanarena.qc
Merge branch 'drjaska/ca-less-stalemates' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / clanarena / sv_clanarena.qc
1 #include "sv_clanarena.qh"
2
3 float autocvar_g_ca_damage2score = 100;
4 bool autocvar_g_ca_spectate_enemies;
5 bool autocvar_g_ca_prevent_stalemate;
6
7 float autocvar_g_ca_start_health = 200;
8 float autocvar_g_ca_start_armor = 200;
9 float autocvar_g_ca_start_ammo_shells = 60;
10 float autocvar_g_ca_start_ammo_nails = 320;
11 float autocvar_g_ca_start_ammo_rockets = 160;
12 float autocvar_g_ca_start_ammo_cells = 180;
13 float autocvar_g_ca_start_ammo_plasma = 180;
14 float autocvar_g_ca_start_ammo_fuel = 0;
15
16 .float ca_damage_counter;
17
18 void CA_count_alive_players()
19 {
20         total_players = 0;
21         for (int i = 1; i <= NUM_TEAMS; ++i)
22         {
23                 Team_SetNumberOfAlivePlayers(Team_GetTeamFromIndex(i), 0);
24         }
25         FOREACH_CLIENT(IS_PLAYER(it) && Entity_HasValidTeam(it),
26         {
27                 ++total_players;
28                 if (IS_DEAD(it))
29                 {
30                         continue;
31                 }
32                 entity team_ = Entity_GetTeam(it);
33                 int num_alive = Team_GetNumberOfAlivePlayers(team_);
34                 ++num_alive;
35                 Team_SetNumberOfAlivePlayers(team_, num_alive);
36         });
37         FOREACH_CLIENT(IS_REAL_CLIENT(it),
38         {
39                 STAT(REDALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(1));
40                 STAT(BLUEALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(2));
41                 STAT(YELLOWALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(3));
42                 STAT(PINKALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(4));
43         });
44 }
45
46 void nades_Clear(entity player);
47
48 int CA_PreventStalemate()
49 {
50         //LOG_INFO("PreventStalemate running");
51         int winnerTeam = 0;
52         int secondTeam = 0;
53
54         for(int i = 1; i <= AVAILABLE_TEAMS; i++)
55         {
56                 if(!winnerTeam || Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) > Team_GetNumberOfAlivePlayers(Team_GetTeam(winnerTeam)))
57                 {
58                         secondTeam = winnerTeam;
59                         winnerTeam = Team_IndexToTeam(i);
60                 }
61                 else
62                 {
63                         if(!secondTeam || Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) > Team_GetNumberOfAlivePlayers(Team_GetTeam(secondTeam)))
64                                 secondTeam = Team_IndexToTeam(i);
65                 }
66         }
67
68         if(Team_GetNumberOfAlivePlayers(Team_GetTeam(winnerTeam)) != Team_GetNumberOfAlivePlayers(Team_GetTeam(secondTeam)))
69         {
70                 LOG_INFOF("Stalemate broken by alive players. Best team: %s%s (%d)^7 - Trailing team: %s%s (%d)",
71                         Team_ColorCode(winnerTeam), Team_ColorName(winnerTeam), Team_GetNumberOfAlivePlayers(Team_GetTeam(winnerTeam)),
72                         Team_ColorCode(secondTeam), Team_ColorName(secondTeam), Team_GetNumberOfAlivePlayers(Team_GetTeam(secondTeam)));
73                 return winnerTeam;
74         }
75
76         // Equality. Let's check which team has more health now
77         //LOG_INFO("Equality. Checking health now.");
78         winnerTeam = 0;
79         secondTeam = 0;
80         int winnerTeamHealth = 0;
81         int secondTeamHealth = 0;
82         int teamIndex, teamHealth;
83
84         for(int i = 1; i <= AVAILABLE_TEAMS; i++)
85         {
86                 teamIndex = i;
87                 teamHealth = 0;
88
89                 // Add up health for the players in this team
90                 FOREACH_CLIENT(IS_PLAYER(it) && Entity_HasValidTeam(it) && it.team == Team_IndexToTeam(teamIndex),
91                 {
92                         if (IS_DEAD(it))
93                                 continue;
94                         teamHealth += GetResource(it, RES_HEALTH) + GetResource(it, RES_ARMOR);
95                 });
96
97                 // Set the winner teams
98                 if(!winnerTeam || teamHealth > winnerTeamHealth)
99                 {
100                         secondTeam = winnerTeam;
101                         secondTeamHealth = winnerTeamHealth;
102                         winnerTeam = Team_IndexToTeam(i);
103                         winnerTeamHealth = teamHealth;
104                 }
105                 else
106                 {
107                         if(!secondTeam || teamHealth > secondTeamHealth)
108                         {
109                                 secondTeam = Team_IndexToTeam(i);
110                                 secondTeamHealth = teamHealth;
111                         }
112                 }
113         }
114
115         if(winnerTeamHealth != secondTeamHealth)
116         {
117                 LOG_INFOF("Stalemate broken by team health. Best team: %s%s (%d)^7 - Trailing team: %s%s (%d)",
118                         Team_ColorCode(winnerTeam), Team_ColorName(winnerTeam), winnerTeamHealth,
119                         Team_ColorCode(secondTeam), Team_ColorName(secondTeam), secondTeamHealth);
120                 return winnerTeam;
121         }
122         else
123                 return -2; // Equality. Can't avoid the stalemate.
124 }
125
126 float CA_CheckWinner()
127 {
128         int winner_team = 0;
129
130         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
131         {
132                 if(autocvar_g_ca_prevent_stalemate)
133                         winner_team = CA_PreventStalemate();
134                 else
135                         winner_team = -2;
136         }
137
138         CA_count_alive_players();
139         if (!winner_team)
140                 winner_team = Team_GetWinnerAliveTeam();
141         if (!winner_team)
142                 return 0;
143
144         if(winner_team > 0)
145         {
146                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
147                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
148                 TeamScore_AddToTeam(winner_team, ST_CA_ROUNDS, +1);
149         }
150         else if(winner_team == -1)
151         {
152                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
153                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
154         }
155         else if(winner_team == -2)
156         {
157                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
158                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
159         }
160
161         allowed_to_spawn = false;
162         game_stopped = true;
163         round_handler_Init(5, autocvar_g_ca_warmup, autocvar_g_ca_round_timelimit);
164
165         FOREACH_CLIENT(IS_PLAYER(it), { nades_Clear(it); });
166
167         return 1;
168 }
169
170 void CA_RoundStart()
171 {
172         allowed_to_spawn = boolean(warmup_stage);
173 }
174
175 bool CA_CheckTeams()
176 {
177         static int prev_missing_teams_mask;
178         allowed_to_spawn = true;
179         CA_count_alive_players();
180         if (Team_GetNumberOfAliveTeams() == NumTeams(ca_teams))
181         {
182                 if(prev_missing_teams_mask > 0)
183                         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
184                 prev_missing_teams_mask = -1;
185                 return true;
186         }
187         if(total_players == 0)
188         {
189                 if(prev_missing_teams_mask > 0)
190                         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
191                 prev_missing_teams_mask = -1;
192                 return false;
193         }
194         int missing_teams_mask = 0;
195         for (int i = 1; i <= NUM_TEAMS; ++i)
196         {
197                 if ((ca_teams & Team_IndexToBit(i)) &&
198                         (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) == 0))
199                 {
200                         missing_teams_mask |= Team_IndexToBit(i);
201                 }
202         }
203         if(prev_missing_teams_mask != missing_teams_mask)
204         {
205                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
206                 prev_missing_teams_mask = missing_teams_mask;
207         }
208         return false;
209 }
210
211 bool ca_isEliminated(entity e)
212 {
213         if(INGAME_JOINED(e) && (IS_DEAD(e) || e.frags == FRAGS_PLAYER_OUT_OF_GAME))
214                 return true;
215         if(INGAME_JOINING(e))
216                 return true;
217         return false;
218 }
219
220 /** Returns next available player to spectate if g_ca_spectate_enemies == 0 */
221 entity CA_SpectateNext(entity player, entity start)
222 {
223         if (SAME_TEAM(start, player)) return start;
224         // continue from current player
225         for (entity e = start; (e = find(e, classname, STR_PLAYER)); )
226         {
227                 if (SAME_TEAM(player, e)) return e;
228         }
229         // restart from the beginning
230         for (entity e = NULL; (e = find(e, classname, STR_PLAYER)); )
231         {
232                 if (SAME_TEAM(player, e)) return e;
233         }
234         return start;
235 }
236
237
238 MUTATOR_HOOKFUNCTION(ca, PlayerSpawn)
239 {
240         entity player = M_ARGV(0, entity);
241
242         INGAME_STATUS_SET(player, INGAME_STATUS_JOINED);
243         if (time <= game_starttime) // reset on game restart, not on round start
244                 player.ca_damage_counter = 0;
245         if (!warmup_stage)
246                 eliminatedPlayers.SendFlags |= 1;
247 }
248
249 MUTATOR_HOOKFUNCTION(ca, ForbidSpawn)
250 {
251         entity player = M_ARGV(0, entity);
252
253         // spectators / observers that weren't playing can join; they are
254         // immediately forced to observe in the PutClientInServer hook
255         // this way they are put in a team and can play in the next round
256         if (!allowed_to_spawn && INGAME(player))
257                 return true;
258         return false;
259 }
260
261 MUTATOR_HOOKFUNCTION(ca, PutClientInServer)
262 {
263         entity player = M_ARGV(0, entity);
264
265         if (!allowed_to_spawn && IS_PLAYER(player)) // this is true even when player is trying to join
266         {
267                 TRANSMUTE(Observer, player);
268                 if (CS(player).jointime != time && !INGAME(player)) // not when connecting
269                 {
270                         INGAME_STATUS_SET(player, INGAME_STATUS_JOINING);
271                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_JOIN_LATE);
272                 }
273         }
274 }
275
276 MUTATOR_HOOKFUNCTION(ca, reset_map_players)
277 {
278         FOREACH_CLIENT(true, {
279                 CS(it).killcount = 0;
280                 if (INGAME(it) || IS_BOT_CLIENT(it))
281                 {
282                         TRANSMUTE(Player, it);
283                         INGAME_STATUS_SET(it, INGAME_STATUS_JOINED);
284                         PutClientInServer(it);
285                 }
286         });
287         return true;
288 }
289
290 MUTATOR_HOOKFUNCTION(ca, reset_map_global)
291 {
292         allowed_to_spawn = true;
293         return true;
294 }
295
296 MUTATOR_HOOKFUNCTION(ca, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
297 {
298         M_ARGV(0, float) = ca_teams;
299         return true;
300 }
301
302 entity ca_LastPlayerForTeam(entity this)
303 {
304         entity last_pl = NULL;
305         FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
306                 if (!IS_DEAD(it) && SAME_TEAM(this, it))
307                 {
308                         if (!last_pl)
309                                 last_pl = it;
310                         else
311                                 return NULL;
312                 }
313         });
314         return last_pl;
315 }
316
317 void ca_LastPlayerForTeam_Notify(entity this)
318 {
319         if (!warmup_stage && round_handler_IsActive() && round_handler_IsRoundStarted())
320         {
321                 entity pl = ca_LastPlayerForTeam(this);
322                 if (pl)
323                         Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
324         }
325 }
326
327 MUTATOR_HOOKFUNCTION(ca, PlayerDies)
328 {
329         entity frag_target = M_ARGV(2, entity);
330
331         ca_LastPlayerForTeam_Notify(frag_target);
332         if (!allowed_to_spawn)
333         {
334                 frag_target.respawn_flags = RESPAWN_SILENT;
335                 // prevent unwanted sudden rejoin as spectator and movement of spectator camera
336                 frag_target.respawn_time = time + 2;
337         }
338         frag_target.respawn_flags |= RESPAWN_FORCE;
339         if (!warmup_stage)
340                 eliminatedPlayers.SendFlags |= 1;
341         return true;
342 }
343
344
345 MUTATOR_HOOKFUNCTION(ca, ClientDisconnect)
346 {
347         entity player = M_ARGV(0, entity);
348
349         if (IS_PLAYER(player) && !IS_DEAD(player))
350                 ca_LastPlayerForTeam_Notify(player);
351         return true;
352 }
353
354 MUTATOR_HOOKFUNCTION(ca, MakePlayerObserver)
355 {
356         entity player = M_ARGV(0, entity);
357
358         bool is_forced = M_ARGV(1, bool);
359         if (is_forced && INGAME(player))
360                 INGAME_STATUS_CLEAR(player);
361
362         if (IS_PLAYER(player) && !IS_DEAD(player))
363                 ca_LastPlayerForTeam_Notify(player);
364         if (player.killindicator_teamchange == -2) // player wants to spectate
365         {
366                 entcs_update_players(player);
367                 INGAME_STATUS_CLEAR(player);
368         }
369         if (INGAME(player))
370                 player.frags = FRAGS_PLAYER_OUT_OF_GAME;
371         if (!warmup_stage)
372                 eliminatedPlayers.SendFlags |= 1;
373         if (!INGAME(player))
374                 return false;  // allow team reset
375         return true;  // prevent team reset
376 }
377
378 MUTATOR_HOOKFUNCTION(ca, ForbidThrowCurrentWeapon)
379 {
380         return true;
381 }
382
383 MUTATOR_HOOKFUNCTION(ca, GiveFragsForKill, CBC_ORDER_FIRST)
384 {
385         M_ARGV(2, float) = 0; // score will be given to the winner team when the round ends
386         return true;
387 }
388
389 MUTATOR_HOOKFUNCTION(ca, SetStartItems)
390 {
391         start_items       &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
392         if(!cvar("g_use_ammunition"))
393                 start_items |= IT_UNLIMITED_AMMO;
394
395         start_health       = warmup_start_health       = autocvar_g_ca_start_health;
396         start_armorvalue   = warmup_start_armorvalue   = autocvar_g_ca_start_armor;
397         start_ammo_shells  = warmup_start_ammo_shells  = autocvar_g_ca_start_ammo_shells;
398         start_ammo_nails   = warmup_start_ammo_nails   = autocvar_g_ca_start_ammo_nails;
399         start_ammo_rockets = warmup_start_ammo_rockets = autocvar_g_ca_start_ammo_rockets;
400         start_ammo_cells   = warmup_start_ammo_cells   = autocvar_g_ca_start_ammo_cells;
401         start_ammo_plasma  = warmup_start_ammo_plasma  = autocvar_g_ca_start_ammo_plasma;
402         start_ammo_fuel    = warmup_start_ammo_fuel    = autocvar_g_ca_start_ammo_fuel;
403 }
404
405 MUTATOR_HOOKFUNCTION(ca, Damage_Calculate)
406 {
407         entity frag_attacker = M_ARGV(1, entity);
408         entity frag_target = M_ARGV(2, entity);
409         float frag_deathtype = M_ARGV(3, float);
410         float frag_damage = M_ARGV(4, float);
411         float frag_mirrordamage = M_ARGV(5, float);
412
413         if (IS_PLAYER(frag_target))
414         if (!IS_DEAD(frag_target))
415         if (frag_target == frag_attacker || SAME_TEAM(frag_target, frag_attacker) || frag_deathtype == DEATH_FALL.m_id)
416                 frag_damage = 0;
417
418         frag_mirrordamage = 0;
419
420         M_ARGV(4, float) = frag_damage;
421         M_ARGV(5, float) = frag_mirrordamage;
422 }
423
424 MUTATOR_HOOKFUNCTION(ca, FilterItem)
425 {
426         entity item = M_ARGV(0, entity);
427
428         if (autocvar_g_powerups <= 0)
429         if (item.itemdef.instanceOfPowerup)
430                 return true;
431
432         if (autocvar_g_pickup_items <= 0)
433                 return true;
434 }
435
436 MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor)
437 {
438         if (time < game_starttime || (round_handler_IsActive() && !round_handler_IsRoundStarted()))
439                 return;
440
441         entity frag_attacker = M_ARGV(1, entity);
442         entity frag_target = M_ARGV(2, entity);
443         float frag_deathtype = M_ARGV(6, float);
444         float frag_damage = M_ARGV(7, float);
445         float damage_take = bound(0, M_ARGV(4, float), GetResource(frag_target, RES_HEALTH));
446         float damage_save = bound(0, M_ARGV(5, float), GetResource(frag_target, RES_ARMOR));
447
448         float excess = max(0, frag_damage - damage_take - damage_save);
449
450         if (autocvar_g_ca_damage2score <= 0 || frag_damage - excess == 0) return;
451
452         entity scorer = NULL;
453         float scorer_damage = 0;
454
455         if (IS_PLAYER(frag_attacker))
456         {
457                 if (DIFF_TEAM(frag_target, frag_attacker))
458                         scorer_damage = frag_damage - excess;
459                 else // friendly fire
460                         scorer_damage = -(frag_damage - excess);
461
462                 scorer = frag_attacker;
463         }
464         else
465         {
466                 //handle (environmental hazard) suiciding, check first if player has a registered attacker who most likely pushed them there to avoid punishing pushed players as pushers are already rewarded
467                 //deathtypes:
468                 //kill = suicide, drown = drown in water/liquid, hurttrigger = out of the map void or hurt triggers inside maps like electric sparks
469                 //camp = campcheck, lava = lava, slime = slime
470                 //team change / rebalance suicides are currently not included
471                 if (frag_deathtype == DEATH_KILL.m_id ||
472                         frag_deathtype == DEATH_DROWN.m_id ||
473                         frag_deathtype == DEATH_HURTTRIGGER.m_id ||
474                         frag_deathtype == DEATH_CAMP.m_id ||
475                         frag_deathtype == DEATH_LAVA.m_id ||
476                         frag_deathtype == DEATH_SLIME.m_id ||
477                         frag_deathtype == DEATH_SWAMP.m_id)
478                 {
479                         scorer_damage = -(frag_damage - excess);
480                         scorer = frag_target;
481                 }
482         }
483
484         if (scorer)
485                 GameRules_scoring_add_float2int(scorer, SCORE, scorer_damage, ca_damage_counter, autocvar_g_ca_damage2score);
486 }
487
488 MUTATOR_HOOKFUNCTION(ca, CalculateRespawnTime)
489 {
490         // no respawn calculations needed, player is forced to spectate anyway
491         return true;
492 }
493
494 MUTATOR_HOOKFUNCTION(ca, PlayerRegen)
495 {
496         // no regeneration in CA
497         return true;
498 }
499
500 MUTATOR_HOOKFUNCTION(ca, Scores_CountFragsRemaining)
501 {
502         // announce remaining frags
503         return true;
504 }
505
506 MUTATOR_HOOKFUNCTION(ca, SpectateSet)
507 {
508         entity client = M_ARGV(0, entity);
509         entity targ = M_ARGV(1, entity);
510
511         if (!autocvar_g_ca_spectate_enemies && INGAME(client))
512         if (DIFF_TEAM(targ, client))
513                 return true;
514 }
515
516 MUTATOR_HOOKFUNCTION(ca, SpectateNext)
517 {
518         entity client = M_ARGV(0, entity);
519
520         if (!autocvar_g_ca_spectate_enemies && INGAME(client)
521                 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
522         {
523                 entity targ = M_ARGV(1, entity);
524                 M_ARGV(1, entity) = CA_SpectateNext(client, targ);
525                 return true;
526         }
527 }
528
529 MUTATOR_HOOKFUNCTION(ca, SpectatePrev)
530 {
531         entity client = M_ARGV(0, entity);
532         entity targ = M_ARGV(1, entity);
533         entity first = M_ARGV(2, entity);
534
535         if (!autocvar_g_ca_spectate_enemies && INGAME(client)
536                 && Team_GetNumberOfAlivePlayers(Entity_GetTeam(client)))
537         {
538                 do { targ = targ.chain; }
539                 while(targ && DIFF_TEAM(targ, client));
540
541                 if (!targ)
542                 {
543                         for (targ = first; targ && DIFF_TEAM(targ, client); targ = targ.chain);
544
545                         if (targ == client.enemy)
546                                 return MUT_SPECPREV_RETURN;
547                 }
548         }
549         else
550                 return MUT_SPECPREV_CONTINUE;
551
552         M_ARGV(1, entity) = targ;
553
554         return MUT_SPECPREV_FOUND;
555 }
556
557 MUTATOR_HOOKFUNCTION(ca, Bot_FixCount, CBC_ORDER_EXCLUSIVE)
558 {
559         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
560                 if (IS_PLAYER(it) || INGAME_JOINED(it))
561                         ++M_ARGV(0, int);
562                 ++M_ARGV(1, int);
563         });
564         return true;
565 }
566
567 MUTATOR_HOOKFUNCTION(ca, ClientCommand_Spectate)
568 {
569         entity player = M_ARGV(0, entity);
570
571         if (INGAME(player))
572         {
573                 // they're going to spec, we can do other checks
574                 if (autocvar_sv_spectate && (IS_SPEC(player) || IS_OBSERVER(player)))
575                         Send_Notification(NOTIF_ONE_ONLY, player, MSG_INFO, INFO_CA_LEAVE);
576                 return MUT_SPECCMD_FORCE;
577         }
578
579         return MUT_SPECCMD_CONTINUE;
580 }
581
582 MUTATOR_HOOKFUNCTION(ca, HideTeamNagger)
583 {
584         return true; // doesn't work well with the whole spectator as player thing
585 }
586
587 MUTATOR_HOOKFUNCTION(ca, SetWeaponArena)
588 {
589         if (M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
590                 M_ARGV(0, string) = autocvar_g_ca_weaponarena;
591 }
592
593 MUTATOR_HOOKFUNCTION(ca, SV_ParseServerCommand)
594 {
595         string cmd_name = M_ARGV(0, string);
596         if (cmd_name == "shuffleteams")
597                 shuffleteams_on_reset_map = !allowed_to_spawn;
598         return false;
599 }