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>
8 #include <server/bot/api.qh>
9 #include <server/world.qh>
10 #include <server/teamplay.qh>
12 IntrusiveList g_invasion_roundends;
13 IntrusiveList g_invasion_waves;
14 IntrusiveList g_invasion_spawns;
15 STATIC_INIT(g_invasion)
17 g_invasion_roundends = IL_NEW();
18 g_invasion_waves = IL_NEW();
19 g_invasion_spawns = IL_NEW();
22 float autocvar_g_invasion_round_timelimit;
23 float autocvar_g_invasion_spawnpoint_spawn_delay;
24 float autocvar_g_invasion_warmup;
25 int autocvar_g_invasion_monster_count;
26 bool autocvar_g_invasion_zombies_only;
27 float autocvar_g_invasion_spawn_delay;
32 bool inv_warning_shown; // spammy
34 void target_invasion_roundend_use(entity this, entity actor, entity trigger)
36 if(!IS_PLAYER(actor)) { return; }
38 actor.inv_endreached = true;
42 // let's not count bots
43 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
48 if(plnum < ceil(realplnum * min(1, this.count))) // 70% of players
54 spawnfunc(target_invasion_roundend)
56 if(!g_invasion) { delete(this); return; }
58 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)
60 if(!this.count) { this.count = 0.7; } // require at least 70% of the players to reach the end before triggering victory
62 this.use = target_invasion_roundend_use;
64 IL_PUSH(g_invasion_roundends, this);
67 spawnfunc(invasion_wave)
69 if(!g_invasion) { delete(this); return; }
71 IL_PUSH(g_invasion_waves, this);
74 spawnfunc(invasion_spawnpoint)
76 if(!g_invasion) { delete(this); return; }
78 this.classname = "invasion_spawnpoint";
79 IL_PUSH(g_invasion_spawns, this);
84 // Invasion stage mode winning condition: If the attackers triggered a round end (by fulfilling all objectives)
86 int WinningCondition_Invasion()
88 WinningConditionHelper(NULL); // set worldstatus
90 int status = WINNING_NO;
92 if(autocvar_g_invasion_type == INV_TYPE_STAGE)
94 SetWinners(inv_endreached, true);
97 IL_EACH(g_invasion_roundends, true,
102 bprint("Invasion: round completed.\n");
103 // winners already set (TODO: teamplay support)
105 status = WINNING_YES;
111 status = WINNING_YES; // just end it? TODO: should warn mapper!
113 else if(autocvar_g_invasion_type == INV_TYPE_HUNT)
117 int found = 0; // NOTE: this ends the round if no monsters are placed
118 IL_EACH(g_monsters, !(it.spawnflags & MONSTERFLAG_RESPAWNED),
125 FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it),
129 status = WINNING_YES;
136 Monster invasion_PickMonster(int supermonster_count)
138 RandomSelection_Init();
140 FOREACH(Monsters, it != MON_Null,
142 if((it.spawnflags & MON_FLAG_HIDDEN) || (it.spawnflags & MONSTER_TYPE_PASSIVE) || (it.spawnflags & MONSTER_TYPE_FLY) || (it.spawnflags & MONSTER_TYPE_SWIM) ||
143 (it.spawnflags & MONSTER_SIZE_QUAKE) || ((it.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
145 if(autocvar_g_invasion_zombies_only && !(it.spawnflags & MONSTER_TYPE_UNDEAD))
147 RandomSelection_AddEnt(it, 1, 1);
150 return RandomSelection_chosen_ent;
153 entity invasion_PickSpawn()
155 RandomSelection_Init();
157 IL_EACH(g_invasion_spawns, true,
159 RandomSelection_AddEnt(it, 1, ((time < it.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
160 it.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
163 return RandomSelection_chosen_ent;
166 entity invasion_GetWaveEntity(int wavenum)
168 IL_EACH(g_invasion_waves, it.cnt == wavenum,
170 return it; // found one
173 // if no specific one is found, find the last existing wave ent
175 IL_EACH(g_invasion_waves, it.cnt <= wavenum,
177 if(!best || it.cnt > best.cnt)
184 void invasion_SpawnChosenMonster(Monster mon)
187 entity spawn_point = invasion_PickSpawn();
188 entity wave_ent = invasion_GetWaveEntity(inv_roundcnt);
191 if(wave_ent && wave_ent.spawnmob && wave_ent.spawnmob != "")
193 RandomSelection_Init();
194 FOREACH_WORD(wave_ent.spawnmob, true,
196 RandomSelection_AddString(it, 1, 1);
199 tospawn = RandomSelection_chosen_string;
202 if(spawn_point == NULL)
204 if(!inv_warning_shown)
206 inv_warning_shown = true;
207 LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations");
210 setsize(e, mon.m_mins, mon.m_maxs);
212 if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
213 monster = spawnmonster(e, tospawn, mon, NULL, NULL, e.origin, false, false, 2);
220 else // if spawnmob field falls through (unset), fallback to mon (relying on spawnmonster for that behaviour)
221 monster = spawnmonster(spawn(), ((spawn_point.spawnmob && spawn_point.spawnmob != "") ? spawn_point.spawnmob : tospawn), mon, spawn_point, spawn_point, spawn_point.origin, false, false, 2);
226 monster.spawnshieldtime = time;
230 if(spawn_point.target_range)
231 monster.target_range = spawn_point.target_range;
232 monster.target2 = spawn_point.target2;
237 if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
238 monster.team = spawn_point.team;
241 RandomSelection_Init();
242 if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_AddFloat(NUM_TEAM_1, 1, 1);
243 if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_AddFloat(NUM_TEAM_2, 1, 1);
244 if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_AddFloat(NUM_TEAM_3, 1, 1); }
245 if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_AddFloat(NUM_TEAM_4, 1, 1); }
247 monster.team = RandomSelection_chosen_float;
250 monster_setupcolors(monster);
254 WaypointSprite_UpdateTeamRadar(monster.sprite, RADARICON_DANGER, ((monster.team) ? Team_ColorRGB(monster.team) : '1 0 0'));
256 monster.sprite.team = 0;
257 monster.sprite.SendFlags |= 1;
261 if(monster.monster_attack)
262 IL_REMOVE(g_monster_targets, monster);
263 monster.monster_attack = false; // it's the player's job to kill all the monsters
265 if(inv_roundcnt >= inv_maxrounds)
266 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
269 void invasion_SpawnMonsters(int supermonster_count)
271 Monster chosen_monster = invasion_PickMonster(supermonster_count);
273 invasion_SpawnChosenMonster(chosen_monster);
276 bool Invasion_CheckWinner()
278 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
280 IL_EACH(g_monsters, true,
284 IL_CLEAR(g_monsters);
286 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
287 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
288 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
292 float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
294 IL_EACH(g_monsters, GetResource(it, RES_HEALTH) > 0,
296 if(it.monsterdef.spawnflags & MON_FLAG_SUPERMONSTER)
297 ++supermonster_count;
298 ++total_alive_monsters;
303 case NUM_TEAM_1: ++red_alive; break;
304 case NUM_TEAM_2: ++blue_alive; break;
305 case NUM_TEAM_3: ++yellow_alive; break;
306 case NUM_TEAM_4: ++pink_alive; break;
310 if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
312 if(time >= inv_lastcheck)
314 invasion_SpawnMonsters(supermonster_count);
315 inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
321 if(inv_numspawned < 1)
322 return 0; // nothing has spawned yet
326 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
329 else if(inv_numkilled < inv_maxspawned)
332 entity winner = NULL;
333 float winning_score = 0, winner_team = 0;
338 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
341 if(winner_team) { winner_team = 0; }
342 else { winner_team = NUM_TEAM_2; }
346 if(winner_team) { winner_team = 0; }
347 else { winner_team = NUM_TEAM_3; }
351 if(winner_team) { winner_team = 0; }
352 else { winner_team = NUM_TEAM_4; }
357 FOREACH_CLIENT(IS_PLAYER(it), {
358 float cs = GameRules_scoring_add(it, KILLS, 0);
359 if(cs > winning_score)
367 IL_EACH(g_monsters, true,
371 IL_CLEAR(g_monsters);
377 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
378 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
383 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
384 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
387 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
392 bool Invasion_CheckPlayers()
397 void Invasion_RoundStart()
400 FOREACH_CLIENT(IS_PLAYER(it), {
401 it.player_blocked = false;
405 if(inv_roundcnt < inv_maxrounds)
406 inv_roundcnt += 1; // a limiter to stop crazy counts
408 inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
414 inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
418 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
419 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
420 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
421 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
422 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
426 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
428 entity frag_target = M_ARGV(0, entity);
429 entity frag_attacker = M_ARGV(1, entity);
431 if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
433 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
438 if(teamplay) { inv_monsters_perteam[frag_target.team] -= 1; }
440 if(IS_PLAYER(frag_attacker))
442 if(SAME_TEAM(frag_attacker, frag_target)) // in non-teamplay modes, same team = same player, so this works
443 GameRules_scoring_add(frag_attacker, KILLS, -1);
446 GameRules_scoring_add(frag_attacker, KILLS, +1);
448 TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
454 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
456 entity mon = M_ARGV(0, entity);
457 mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
459 if(autocvar_g_invasion_type == INV_TYPE_HUNT)
460 return false; // allowed
462 if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
465 if(!(mon.spawnflags & MONSTERFLAG_RESPAWNED))
471 mon.monster_skill = inv_monsterskill;
473 if(mon.monsterdef.spawnflags & MON_FLAG_SUPERMONSTER)
474 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
477 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
479 if(autocvar_g_invasion_type != INV_TYPE_ROUND)
480 return; // uses map spawned monsters
482 monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
483 monsters_killed = inv_numkilled;
486 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
488 // no regeneration in invasion, regardless of the game type
492 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
494 entity player = M_ARGV(0, entity);
496 if(player.bot_attack)
497 IL_REMOVE(g_bot_targets, player);
498 player.bot_attack = false;
501 MUTATOR_HOOKFUNCTION(inv, Damage_Calculate)
503 entity frag_attacker = M_ARGV(1, entity);
504 entity frag_target = M_ARGV(2, entity);
505 float frag_damage = M_ARGV(4, float);
506 vector frag_force = M_ARGV(6, vector);
508 if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
511 frag_force = '0 0 0';
513 M_ARGV(4, float) = frag_damage;
514 M_ARGV(6, vector) = frag_force;
518 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
520 entity targ = M_ARGV(1, entity);
522 if(!IS_MONSTER(targ))
526 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
528 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
531 start_armorvalue = 200;
535 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
537 entity frag_target = M_ARGV(1, entity);
539 if(IS_MONSTER(frag_target))
540 return MUT_ACCADD_INVALID;
541 return MUT_ACCADD_INDIFFERENT;
544 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
546 // monster spawning disabled during an invasion
547 M_ARGV(1, string) = "You cannot spawn monsters during an invasion!";
551 MUTATOR_HOOKFUNCTION(inv, CheckRules_World)
553 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
556 M_ARGV(0, float) = WinningCondition_Invasion();
560 MUTATOR_HOOKFUNCTION(inv, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
562 M_ARGV(0, float) = invasion_teams;
566 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
568 M_ARGV(0, string) = "This command does not work during an invasion!";
572 void invasion_ScoreRules(int inv_teams)
574 GameRules_score_enabled(false);
575 GameRules_scoring(inv_teams, 0, 0, {
577 field_team(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
579 field(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
583 void invasion_DelayedInit(entity this) // Do this check with a delay so we can wait for teams to be set up.
585 if(autocvar_g_invasion_type == INV_TYPE_HUNT || autocvar_g_invasion_type == INV_TYPE_STAGE)
586 cvar_set("fraglimit", "0");
588 if(autocvar_g_invasion_teams)
590 invasion_teams = BITS(bound(2, autocvar_g_invasion_teams, 4));
595 independent_players = 1; // to disable extra useless scores
597 invasion_ScoreRules(invasion_teams);
599 independent_players = 0;
601 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
603 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
604 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
607 inv_maxrounds = 15; // 15?
611 void invasion_Initialize()
613 InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);