1 #include "sv_invasion.qh"
3 #include <common/mapobjects/triggers.qh>
4 #include <common/monsters/sv_spawn.qh>
5 #include <common/monsters/sv_spawner.qh>
6 #include <common/monsters/sv_monsters.qh>
7 #include <common/mutators/mutator/status_effects/_mod.qh>
9 #include <server/bot/api.qh>
10 #include <server/world.qh>
11 #include <server/teamplay.qh>
13 float autocvar_g_invasion_round_timelimit;
14 float autocvar_g_invasion_spawnpoint_spawn_delay;
15 float autocvar_g_invasion_warmup;
16 int autocvar_g_invasion_monster_count;
17 bool autocvar_g_invasion_zombies_only;
18 float autocvar_g_invasion_spawn_delay;
23 bool inv_warning_shown; // spammy
25 void target_invasion_roundend_use(entity this, entity actor, entity trigger)
27 if(!IS_PLAYER(actor)) { return; }
29 actor.inv_endreached = true;
33 // let's not count bots
34 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
39 if(plnum < ceil(realplnum * min(1, this.count))) // 70% of players
45 spawnfunc(target_invasion_roundend)
47 if(!g_invasion) { delete(this); return; }
49 victent_present = true; // a victory entity is present, we don't need to rely on monster count TODO: merge this with the intrusive list (can check empty)
51 if(!this.count) { this.count = 0.7; } // require at least 70% of the players to reach the end before triggering victory
53 this.use = target_invasion_roundend_use;
55 IL_PUSH(g_invasion_roundends, this);
58 spawnfunc(invasion_wave)
60 if(!g_invasion) { delete(this); return; }
62 IL_PUSH(g_invasion_waves, this);
65 spawnfunc(invasion_spawnpoint)
67 if(!g_invasion) { delete(this); return; }
69 IL_PUSH(g_invasion_spawns, this);
74 // Invasion stage mode winning condition: If the attackers triggered a round end (by fulfilling all objectives)
76 int WinningCondition_Invasion()
78 WinningConditionHelper(NULL); // set worldstatus
80 int status = WINNING_NO;
82 if(autocvar_g_invasion_type == INV_TYPE_STAGE)
84 SetWinners(inv_endreached, true);
87 IL_EACH(g_invasion_roundends, true,
92 bprint("Invasion: round completed.\n");
93 // winners already set (TODO: teamplay support)
101 status = WINNING_YES; // just end it? TODO: should warn mapper!
103 else if(autocvar_g_invasion_type == INV_TYPE_HUNT)
107 int found = 0; // NOTE: this ends the round if no monsters are placed
108 IL_EACH(g_monsters, !(it.spawnflags & MONSTERFLAG_RESPAWNED),
115 FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it),
119 status = WINNING_YES;
126 Monster invasion_PickMonster(int supermonster_count)
128 RandomSelection_Init();
130 FOREACH(Monsters, it != MON_Null,
132 if((it.spawnflags & MON_FLAG_HIDDEN) || (it.spawnflags & MONSTER_TYPE_PASSIVE) || (it.spawnflags & MONSTER_TYPE_FLY) || (it.spawnflags & MONSTER_TYPE_SWIM) ||
133 (it.spawnflags & MONSTER_SIZE_QUAKE) || ((it.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
135 if(autocvar_g_invasion_zombies_only && !(it.spawnflags & MONSTER_TYPE_UNDEAD))
137 RandomSelection_AddEnt(it, 1, 1);
140 return RandomSelection_chosen_ent;
143 entity invasion_PickSpawn()
145 RandomSelection_Init();
147 IL_EACH(g_invasion_spawns, true,
149 RandomSelection_AddEnt(it, 1, ((time < it.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
150 it.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
153 return RandomSelection_chosen_ent;
156 entity invasion_GetWaveEntity(int wavenum)
158 IL_EACH(g_invasion_waves, it.cnt == wavenum,
160 return it; // found one
163 // if no specific one is found, find the last existing wave ent
165 IL_EACH(g_invasion_waves, it.cnt <= wavenum,
167 if(!best || it.cnt > best.cnt)
174 void invasion_SpawnChosenMonster(Monster mon)
177 entity spawn_point = invasion_PickSpawn();
178 entity wave_ent = invasion_GetWaveEntity(inv_roundcnt);
181 if(wave_ent && wave_ent.spawnmob && wave_ent.spawnmob != "")
183 RandomSelection_Init();
184 FOREACH_WORD(wave_ent.spawnmob, true,
186 RandomSelection_AddString(it, 1, 1);
189 tospawn = RandomSelection_chosen_string;
192 if(spawn_point == NULL)
194 if(!inv_warning_shown)
196 inv_warning_shown = true;
197 LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations");
200 setsize(e, mon.m_mins, mon.m_maxs);
202 if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
203 monster = spawnmonster(e, tospawn, mon, NULL, NULL, e.origin, false, false, 2);
210 else // if spawnmob field falls through (unset), fallback to mon (relying on spawnmonster for that behaviour)
211 monster = spawnmonster(spawn(), ((spawn_point.spawnmob && spawn_point.spawnmob != "") ? spawn_point.spawnmob : tospawn), mon, spawn_point, spawn_point, spawn_point.origin, false, false, 2);
216 StatusEffects_remove(STATUSEFFECT_SpawnShield, monster, STATUSEFFECT_REMOVE_NORMAL);
220 if(spawn_point.target_range)
221 monster.target_range = spawn_point.target_range;
222 monster.target2 = spawn_point.target2;
227 if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
228 monster.team = spawn_point.team;
231 RandomSelection_Init();
232 if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_AddFloat(NUM_TEAM_1, 1, 1);
233 if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_AddFloat(NUM_TEAM_2, 1, 1);
234 if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_AddFloat(NUM_TEAM_3, 1, 1); }
235 if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_AddFloat(NUM_TEAM_4, 1, 1); }
237 monster.team = RandomSelection_chosen_float;
240 monster_setupcolors(monster);
244 WaypointSprite_UpdateTeamRadar(monster.sprite, RADARICON_DANGER, ((monster.team) ? Team_ColorRGB(monster.team) : '1 0 0'));
246 monster.sprite.team = 0;
247 monster.sprite.SendFlags |= 1;
251 if(monster.monster_attack)
252 IL_REMOVE(g_monster_targets, monster);
253 monster.monster_attack = false; // it's the player's job to kill all the monsters
255 if(inv_roundcnt >= inv_maxrounds)
256 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
259 void invasion_SpawnMonsters(int supermonster_count)
261 Monster chosen_monster = invasion_PickMonster(supermonster_count);
263 invasion_SpawnChosenMonster(chosen_monster);
266 bool Invasion_CheckWinner()
268 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
270 IL_EACH(g_monsters, true,
274 IL_CLEAR(g_monsters);
276 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
277 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
278 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
282 float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
284 IL_EACH(g_monsters, GetResource(it, RES_HEALTH) > 0,
286 if(it.monsterdef.spawnflags & MON_FLAG_SUPERMONSTER)
287 ++supermonster_count;
288 ++total_alive_monsters;
293 case NUM_TEAM_1: ++red_alive; break;
294 case NUM_TEAM_2: ++blue_alive; break;
295 case NUM_TEAM_3: ++yellow_alive; break;
296 case NUM_TEAM_4: ++pink_alive; break;
300 if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
302 if(time >= inv_lastcheck)
304 invasion_SpawnMonsters(supermonster_count);
305 inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
311 if(inv_numspawned < 1)
312 return 0; // nothing has spawned yet
316 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
319 else if(inv_numkilled < inv_maxspawned)
322 entity winner = NULL;
323 float winning_score = 0, winner_team = 0;
328 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
331 if(winner_team) { winner_team = 0; }
332 else { winner_team = NUM_TEAM_2; }
336 if(winner_team) { winner_team = 0; }
337 else { winner_team = NUM_TEAM_3; }
341 if(winner_team) { winner_team = 0; }
342 else { winner_team = NUM_TEAM_4; }
347 FOREACH_CLIENT(IS_PLAYER(it), {
348 float cs = GameRules_scoring_add(it, KILLS, 0);
349 if(cs > winning_score)
357 IL_EACH(g_monsters, true,
361 IL_CLEAR(g_monsters);
367 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
368 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
373 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
374 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
377 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
382 bool Invasion_CheckPlayers()
387 void Invasion_RoundStart()
390 FOREACH_CLIENT(IS_PLAYER(it), {
391 it.player_blocked = false;
395 if(inv_roundcnt < inv_maxrounds)
396 inv_roundcnt += 1; // a limiter to stop crazy counts
398 inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
404 inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
408 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
409 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
410 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
411 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
412 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
416 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
418 entity frag_target = M_ARGV(0, entity);
419 entity frag_attacker = M_ARGV(1, entity);
421 if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
423 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
428 if(teamplay) { inv_monsters_perteam[frag_target.team] -= 1; }
430 if(IS_PLAYER(frag_attacker))
432 if(SAME_TEAM(frag_attacker, frag_target)) // in non-teamplay modes, same team = same player, so this works
433 GameRules_scoring_add(frag_attacker, KILLS, -1);
436 GameRules_scoring_add(frag_attacker, KILLS, +1);
438 TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
444 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
446 entity mon = M_ARGV(0, entity);
447 mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
449 if(autocvar_g_invasion_type == INV_TYPE_HUNT)
450 return false; // allowed
452 if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
455 if(!(mon.spawnflags & MONSTERFLAG_RESPAWNED))
461 mon.monster_skill = inv_monsterskill;
463 if(mon.monsterdef.spawnflags & MON_FLAG_SUPERMONSTER)
464 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
467 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
469 if(autocvar_g_invasion_type != INV_TYPE_ROUND)
470 return; // uses map spawned monsters
472 monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
473 monsters_killed = inv_numkilled;
476 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
478 // no regeneration in invasion, regardless of the game type
482 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
484 entity player = M_ARGV(0, entity);
486 if(player.bot_attack)
487 IL_REMOVE(g_bot_targets, player);
488 player.bot_attack = false;
491 MUTATOR_HOOKFUNCTION(inv, Damage_Calculate)
493 entity frag_attacker = M_ARGV(1, entity);
494 entity frag_target = M_ARGV(2, entity);
495 float frag_damage = M_ARGV(4, float);
496 vector frag_force = M_ARGV(6, vector);
498 if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
501 frag_force = '0 0 0';
503 M_ARGV(4, float) = frag_damage;
504 M_ARGV(6, vector) = frag_force;
508 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
510 entity targ = M_ARGV(1, entity);
512 if(!IS_MONSTER(targ))
516 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
518 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
521 start_armorvalue = 200;
525 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
527 entity frag_target = M_ARGV(1, entity);
529 if(IS_MONSTER(frag_target))
530 return MUT_ACCADD_INVALID;
531 return MUT_ACCADD_INDIFFERENT;
534 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
536 // monster spawning disabled during an invasion
537 M_ARGV(1, string) = "You cannot spawn monsters during an invasion!";
541 MUTATOR_HOOKFUNCTION(inv, CheckRules_World)
543 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
546 M_ARGV(0, float) = WinningCondition_Invasion();
550 MUTATOR_HOOKFUNCTION(inv, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
552 M_ARGV(0, float) = invasion_teams;
556 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
558 M_ARGV(0, string) = "This command does not work during an invasion!";
562 void invasion_ScoreRules(int inv_teams)
564 GameRules_score_enabled(false);
565 GameRules_scoring(inv_teams, 0, 0, {
567 field_team(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
569 field(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
573 void invasion_DelayedInit(entity this) // Do this check with a delay so we can wait for teams to be set up.
575 if(autocvar_g_invasion_type == INV_TYPE_HUNT || autocvar_g_invasion_type == INV_TYPE_STAGE)
576 cvar_set("fraglimit", "0");
578 if(autocvar_g_invasion_teams)
580 invasion_teams = BITS(bound(2, autocvar_g_invasion_teams, 4));
585 independent_players = 1; // to disable extra useless scores
587 invasion_ScoreRules(invasion_teams);
589 independent_players = 0;
591 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
593 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
594 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
597 inv_maxrounds = 15; // 15?
601 void invasion_Initialize()
603 InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);