1 #include "sv_invasion.qh"
3 #include <common/monsters/sv_spawn.qh>
4 #include <common/monsters/sv_spawner.qh>
5 #include <common/monsters/sv_monsters.qh>
7 #include <server/teamplay.qh>
9 IntrusiveList g_invasion_roundends;
10 IntrusiveList g_invasion_waves;
11 IntrusiveList g_invasion_spawns;
12 STATIC_INIT(g_invasion)
14 g_invasion_roundends = IL_NEW();
15 g_invasion_waves = IL_NEW();
16 g_invasion_spawns = IL_NEW();
19 float autocvar_g_invasion_round_timelimit;
20 float autocvar_g_invasion_spawnpoint_spawn_delay;
21 float autocvar_g_invasion_warmup;
22 int autocvar_g_invasion_monster_count;
23 bool autocvar_g_invasion_zombies_only;
24 float autocvar_g_invasion_spawn_delay;
29 bool inv_warning_shown; // spammy
31 void target_invasion_roundend_use(entity this, entity actor, entity trigger)
33 if(!IS_PLAYER(actor)) { return; }
35 actor.inv_endreached = true;
39 // let's not count bots
40 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
45 if(plnum < ceil(realplnum * min(1, this.count))) // 70% of players
51 spawnfunc(target_invasion_roundend)
53 if(!g_invasion) { delete(this); return; }
55 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)
57 if(!this.count) { this.count = 0.7; } // require at least 70% of the players to reach the end before triggering victory
59 this.use = target_invasion_roundend_use;
61 IL_PUSH(g_invasion_roundends, this);
64 spawnfunc(invasion_wave)
66 if(!g_invasion) { delete(this); return; }
68 IL_PUSH(g_invasion_waves, this);
71 spawnfunc(invasion_spawnpoint)
73 if(!g_invasion) { delete(this); return; }
75 this.classname = "invasion_spawnpoint";
76 IL_PUSH(g_invasion_spawns, this);
81 // Invasion stage mode winning condition: If the attackers triggered a round end (by fulfilling all objectives)
83 int WinningCondition_Invasion()
85 WinningConditionHelper(NULL); // set worldstatus
87 int status = WINNING_NO;
89 if(autocvar_g_invasion_type == INV_TYPE_STAGE)
91 SetWinners(inv_endreached, true);
94 IL_EACH(g_invasion_roundends, true,
99 bprint("Invasion: round completed.\n");
100 // winners already set (TODO: teamplay support)
102 status = WINNING_YES;
108 status = WINNING_YES; // just end it? TODO: should warn mapper!
110 else if(autocvar_g_invasion_type == INV_TYPE_HUNT)
114 int found = 0; // NOTE: this ends the round if no monsters are placed
115 IL_EACH(g_monsters, !(it.spawnflags & MONSTERFLAG_RESPAWNED),
122 FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it),
126 status = WINNING_YES;
133 Monster invasion_PickMonster(int supermonster_count)
135 RandomSelection_Init();
137 FOREACH(Monsters, it != MON_Null,
139 if((it.spawnflags & MON_FLAG_HIDDEN) || (it.spawnflags & MONSTER_TYPE_PASSIVE) || (it.spawnflags & MONSTER_TYPE_FLY) || (it.spawnflags & MONSTER_TYPE_SWIM) ||
140 (it.spawnflags & MONSTER_SIZE_QUAKE) || ((it.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
142 if(autocvar_g_invasion_zombies_only && !(it.spawnflags & MONSTER_TYPE_UNDEAD))
144 RandomSelection_AddEnt(it, 1, 1);
147 return RandomSelection_chosen_ent;
150 entity invasion_PickSpawn()
152 RandomSelection_Init();
154 IL_EACH(g_invasion_spawns, true,
156 RandomSelection_AddEnt(it, 1, ((time < it.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
157 it.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
160 return RandomSelection_chosen_ent;
163 entity invasion_GetWaveEntity(int wavenum)
165 IL_EACH(g_invasion_waves, it.cnt == wavenum,
167 return it; // found one
170 // if no specific one is found, find the last existing wave ent
172 IL_EACH(g_invasion_waves, it.cnt <= wavenum,
174 if(!best || it.cnt > best.cnt)
181 void invasion_SpawnChosenMonster(Monster mon)
184 entity spawn_point = invasion_PickSpawn();
185 entity wave_ent = invasion_GetWaveEntity(inv_roundcnt);
188 if(wave_ent && wave_ent.spawnmob && wave_ent.spawnmob != "")
190 RandomSelection_Init();
191 FOREACH_WORD(wave_ent.spawnmob, true,
193 RandomSelection_AddString(it, 1, 1);
196 tospawn = RandomSelection_chosen_string;
199 if(spawn_point == NULL)
201 if(!inv_warning_shown)
203 inv_warning_shown = true;
204 LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations");
207 setsize(e, mon.m_mins, mon.m_maxs);
209 if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
210 monster = spawnmonster(e, tospawn, mon, NULL, NULL, e.origin, false, false, 2);
217 else // if spawnmob field falls through (unset), fallback to mon (relying on spawnmonster for that behaviour)
218 monster = spawnmonster(spawn(), ((spawn_point.spawnmob && spawn_point.spawnmob != "") ? spawn_point.spawnmob : tospawn), mon, spawn_point, spawn_point, spawn_point.origin, false, false, 2);
223 monster.spawnshieldtime = time;
227 if(spawn_point.target_range)
228 monster.target_range = spawn_point.target_range;
229 monster.target2 = spawn_point.target2;
234 if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
235 monster.team = spawn_point.team;
238 RandomSelection_Init();
239 if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_AddFloat(NUM_TEAM_1, 1, 1);
240 if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_AddFloat(NUM_TEAM_2, 1, 1);
241 if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_AddFloat(NUM_TEAM_3, 1, 1); }
242 if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_AddFloat(NUM_TEAM_4, 1, 1); }
244 monster.team = RandomSelection_chosen_float;
247 monster_setupcolors(monster);
251 WaypointSprite_UpdateTeamRadar(monster.sprite, RADARICON_DANGER, ((monster.team) ? Team_ColorRGB(monster.team) : '1 0 0'));
253 monster.sprite.team = 0;
254 monster.sprite.SendFlags |= 1;
258 if(monster.monster_attack)
259 IL_REMOVE(g_monster_targets, monster);
260 monster.monster_attack = false; // it's the player's job to kill all the monsters
262 if(inv_roundcnt >= inv_maxrounds)
263 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
266 void invasion_SpawnMonsters(int supermonster_count)
268 Monster chosen_monster = invasion_PickMonster(supermonster_count);
270 invasion_SpawnChosenMonster(chosen_monster);
273 bool Invasion_CheckWinner()
275 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
277 IL_EACH(g_monsters, true,
281 IL_CLEAR(g_monsters);
283 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
284 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
285 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
289 float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
291 IL_EACH(g_monsters, GetResource(it, RES_HEALTH) > 0,
293 if(it.monsterdef.spawnflags & MON_FLAG_SUPERMONSTER)
294 ++supermonster_count;
295 ++total_alive_monsters;
300 case NUM_TEAM_1: ++red_alive; break;
301 case NUM_TEAM_2: ++blue_alive; break;
302 case NUM_TEAM_3: ++yellow_alive; break;
303 case NUM_TEAM_4: ++pink_alive; break;
307 if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
309 if(time >= inv_lastcheck)
311 invasion_SpawnMonsters(supermonster_count);
312 inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
318 if(inv_numspawned < 1)
319 return 0; // nothing has spawned yet
323 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
326 else if(inv_numkilled < inv_maxspawned)
329 entity winner = NULL;
330 float winning_score = 0, winner_team = 0;
335 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
338 if(winner_team) { winner_team = 0; }
339 else { winner_team = NUM_TEAM_2; }
343 if(winner_team) { winner_team = 0; }
344 else { winner_team = NUM_TEAM_3; }
348 if(winner_team) { winner_team = 0; }
349 else { winner_team = NUM_TEAM_4; }
354 FOREACH_CLIENT(IS_PLAYER(it), {
355 float cs = GameRules_scoring_add(it, KILLS, 0);
356 if(cs > winning_score)
364 IL_EACH(g_monsters, true,
368 IL_CLEAR(g_monsters);
374 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
375 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
380 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
381 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
384 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
389 bool Invasion_CheckPlayers()
394 void Invasion_RoundStart()
397 FOREACH_CLIENT(IS_PLAYER(it), {
398 it.player_blocked = false;
402 if(inv_roundcnt < inv_maxrounds)
403 inv_roundcnt += 1; // a limiter to stop crazy counts
405 inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
411 inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
415 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
416 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
417 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
418 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
419 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
423 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
425 entity frag_target = M_ARGV(0, entity);
426 entity frag_attacker = M_ARGV(1, entity);
428 if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
430 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
435 if(teamplay) { inv_monsters_perteam[frag_target.team] -= 1; }
437 if(IS_PLAYER(frag_attacker))
439 if(SAME_TEAM(frag_attacker, frag_target)) // in non-teamplay modes, same team = same player, so this works
440 GameRules_scoring_add(frag_attacker, KILLS, -1);
443 GameRules_scoring_add(frag_attacker, KILLS, +1);
445 TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
451 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
453 entity mon = M_ARGV(0, entity);
454 mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
456 if(autocvar_g_invasion_type == INV_TYPE_HUNT)
457 return false; // allowed
459 if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
462 if(!(mon.spawnflags & MONSTERFLAG_RESPAWNED))
468 mon.monster_skill = inv_monsterskill;
470 if(mon.monsterdef.spawnflags & MON_FLAG_SUPERMONSTER)
471 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
474 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
476 if(autocvar_g_invasion_type != INV_TYPE_ROUND)
477 return; // uses map spawned monsters
479 monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
480 monsters_killed = inv_numkilled;
483 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
485 // no regeneration in invasion, regardless of the game type
489 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
491 entity player = M_ARGV(0, entity);
493 if(player.bot_attack)
494 IL_REMOVE(g_bot_targets, player);
495 player.bot_attack = false;
498 MUTATOR_HOOKFUNCTION(inv, Damage_Calculate)
500 entity frag_attacker = M_ARGV(1, entity);
501 entity frag_target = M_ARGV(2, entity);
502 float frag_damage = M_ARGV(4, float);
503 vector frag_force = M_ARGV(6, vector);
505 if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
508 frag_force = '0 0 0';
510 M_ARGV(4, float) = frag_damage;
511 M_ARGV(6, vector) = frag_force;
515 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
517 entity targ = M_ARGV(1, entity);
519 if(!IS_MONSTER(targ))
523 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
525 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
528 start_armorvalue = 200;
532 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
534 entity frag_target = M_ARGV(1, entity);
536 if(IS_MONSTER(frag_target))
537 return MUT_ACCADD_INVALID;
538 return MUT_ACCADD_INDIFFERENT;
541 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
543 // monster spawning disabled during an invasion
544 M_ARGV(1, string) = "You cannot spawn monsters during an invasion!";
548 MUTATOR_HOOKFUNCTION(inv, CheckRules_World)
550 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
553 M_ARGV(0, float) = WinningCondition_Invasion();
557 MUTATOR_HOOKFUNCTION(inv, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
559 M_ARGV(0, float) = invasion_teams;
563 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
565 M_ARGV(0, string) = "This command does not work during an invasion!";
569 void invasion_ScoreRules(int inv_teams)
571 GameRules_score_enabled(false);
572 GameRules_scoring(inv_teams, 0, 0, {
574 field_team(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
576 field(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
580 void invasion_DelayedInit(entity this) // Do this check with a delay so we can wait for teams to be set up.
582 if(autocvar_g_invasion_type == INV_TYPE_HUNT || autocvar_g_invasion_type == INV_TYPE_STAGE)
583 cvar_set("fraglimit", "0");
585 if(autocvar_g_invasion_teams)
587 invasion_teams = BITS(bound(2, autocvar_g_invasion_teams, 4));
592 independent_players = 1; // to disable extra useless scores
594 invasion_ScoreRules(invasion_teams);
596 independent_players = 0;
598 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
600 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
601 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
604 inv_maxrounds = 15; // 15?
608 void invasion_Initialize()
610 InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);