#include "gamemode_invasion.qh"
-#ifndef GAMEMODE_INVASION_H
-#define GAMEMODE_INVASION_H
-#define autocvar_g_invasion_point_limit cvar("g_invasion_point_limit")
-int autocvar_g_invasion_teams;
-bool autocvar_g_invasion_team_spawns;
-bool g_invasion;
-void invasion_Initialize();
+#include <common/monsters/sv_spawn.qh>
+#include <common/monsters/sv_monsters.qh>
-REGISTER_MUTATOR(inv, false)
-{
- MUTATOR_ONADD
- {
- if (time > 1) // game loads at time 1
- error("This is a game type and it cannot be added at runtime.");
- g_invasion = true;
- invasion_Initialize();
+#include <server/teamplay.qh>
- cvar_settemp("g_monsters", "1");
+IntrusiveList g_invasion_roundends;
+STATIC_INIT(g_invasion_roundends) { g_invasion_roundends = IL_NEW(); }
- SetLimits(autocvar_g_invasion_point_limit, autocvar_leadlimit_override, autocvar_timelimit_override, -1);
- if (autocvar_g_invasion_teams >= 2)
- {
- ActivateTeamplay();
- if (autocvar_g_invasion_team_spawns)
- have_team_spawns = -1; // request team spawns
- }
- }
+IntrusiveList g_invasion_waves;
+STATIC_INIT(g_invasion_waves) { g_invasion_waves = IL_NEW(); }
- MUTATOR_ONROLLBACK_OR_REMOVE
- {
- // we actually cannot roll back invasion_Initialize here
- // BUT: we don't need to! If this gets called, adding always
- // succeeds.
- }
+IntrusiveList g_invasion_spawns;
+STATIC_INIT(g_invasion_spawns) { g_invasion_spawns = IL_NEW(); }
- MUTATOR_ONREMOVE
- {
- LOG_INFO("This is a game type and it cannot be removed at runtime.");
- return -1;
- }
+float autocvar_g_invasion_round_timelimit;
+float autocvar_g_invasion_spawnpoint_spawn_delay;
+float autocvar_g_invasion_warmup;
+int autocvar_g_invasion_monster_count;
+bool autocvar_g_invasion_zombies_only;
+float autocvar_g_invasion_spawn_delay;
- return 0;
-}
+bool victent_present;
+.bool inv_endreached;
-float inv_numspawned;
-float inv_maxspawned;
-float inv_roundcnt;
-float inv_maxrounds;
-float inv_numkilled;
-float inv_lastcheck;
-float inv_maxcurrent;
+bool inv_warning_shown; // spammy
-float invasion_teams;
-float inv_monsters_perteam[17];
+.string spawnmob;
-float inv_monsterskill;
+void target_invasion_roundend_use(entity this, entity actor, entity trigger)
+{
+ if(!IS_PLAYER(actor)) { return; }
-const float ST_INV_KILLS = 1;
-#endif
+ actor.inv_endreached = true;
-#ifdef IMPLEMENTATION
+ int plnum = 0;
+ int realplnum = 0;
+ // let's not count bots
+ FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
+ ++realplnum;
+ if(it.inv_endreached)
+ ++plnum;
+ });
+ if(plnum < ceil(realplnum * min(1, this.count))) // 70% of players
+ return;
-#include <common/monsters/spawn.qh>
-#include <common/monsters/sv_monsters.qh>
+ this.winning = true;
+}
-#include <server/teamplay.qh>
+spawnfunc(target_invasion_roundend)
+{
+ if(!g_invasion) { delete(this); return; }
+ 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)
-float autocvar_g_invasion_round_timelimit;
-float autocvar_g_invasion_spawnpoint_spawn_delay;
-float autocvar_g_invasion_warmup;
-int autocvar_g_invasion_monster_count;
-bool autocvar_g_invasion_zombies_only;
-float autocvar_g_invasion_spawn_delay;
+ if(!this.count) { this.count = 0.7; } // require at least 70% of the players to reach the end before triggering victory
-spawnfunc(invasion_spawnpoint)
+ this.use = target_invasion_roundend_use;
+
+ IL_PUSH(g_invasion_roundends, this);
+}
+
+spawnfunc(invasion_wave)
{
if(!g_invasion) { delete(this); return; }
- this.classname = "invasion_spawnpoint";
+ IL_PUSH(g_invasion_waves, this);
+}
- if(autocvar_g_invasion_zombies_only) // precache only if it hasn't been already
- if(this.monsterid) {
- Monster mon = get_monsterinfo(this.monsterid);
- mon.mr_precache(mon);
- }
+spawnfunc(invasion_spawnpoint)
+{
+ if(!g_invasion) { delete(this); return; }
+
+ IL_PUSH(g_invasion_spawns, this);
}
-float invasion_PickMonster(float supermonster_count)
+void ClearWinners();
+
+// Invasion stage mode winning condition: If the attackers triggered a round end (by fulfilling all objectives)
+// they win.
+int WinningCondition_Invasion()
{
- if(autocvar_g_invasion_zombies_only)
- return MON_ZOMBIE.monsterid;
+ WinningConditionHelper(NULL); // set worldstatus
- float i;
- entity mon;
+ int status = WINNING_NO;
- RandomSelection_Init();
+ if(autocvar_g_invasion_type == INV_TYPE_STAGE)
+ {
+ SetWinners(inv_endreached, true);
+
+ int found = 0;
+ IL_EACH(g_invasion_roundends, true,
+ {
+ ++found;
+ if(it.winning)
+ {
+ bprint("Invasion: round completed.\n");
+ // winners already set (TODO: teamplay support)
+
+ status = WINNING_YES;
+ break;
+ }
+ });
- for(i = MON_FIRST; i <= MON_LAST; ++i)
+ if(!found)
+ status = WINNING_YES; // just end it? TODO: should warn mapper!
+ }
+ else if(autocvar_g_invasion_type == INV_TYPE_HUNT)
{
- mon = get_monsterinfo(i);
- if((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM) || ((mon.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
- continue; // flying/swimming monsters not yet supported
+ ClearWinners();
+
+ int found = 0; // NOTE: this ends the round if no monsters are placed
+ IL_EACH(g_monsters, !(it.spawnflags & MONSTERFLAG_RESPAWNED),
+ {
+ ++found;
+ });
- RandomSelection_Add(NULL, i, string_null, 1, 1);
+ if(found <= 0)
+ {
+ FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it),
+ {
+ it.winning = true;
+ });
+ status = WINNING_YES;
+ }
}
- return RandomSelection_chosen_float;
+ return status;
+}
+
+Monster invasion_PickMonster(int supermonster_count)
+{
+ RandomSelection_Init();
+
+ FOREACH(Monsters, it != MON_Null,
+ {
+ if((it.spawnflags & MON_FLAG_HIDDEN) || (it.spawnflags & MONSTER_TYPE_PASSIVE) || (it.spawnflags & MONSTER_TYPE_FLY) || (it.spawnflags & MONSTER_TYPE_SWIM) ||
+ (it.spawnflags & MONSTER_SIZE_QUAKE) || ((it.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
+ continue;
+ if(autocvar_g_invasion_zombies_only && !(it.spawnflags & MONSTER_TYPE_UNDEAD))
+ continue;
+ RandomSelection_AddEnt(it, 1, 1);
+ });
+
+ return RandomSelection_chosen_ent;
}
entity invasion_PickSpawn()
{
RandomSelection_Init();
- FOREACH_ENTITY_CLASS("invasion_spawnpoint", true,
+ IL_EACH(g_invasion_spawns, true,
{
- RandomSelection_Add(it, 0, string_null, 1, ((time >= it.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
+ RandomSelection_AddEnt(it, 1, ((time < it.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
it.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
});
return RandomSelection_chosen_ent;
}
-void invasion_SpawnChosenMonster(float mon)
+entity invasion_GetWaveEntity(int wavenum)
{
- entity spawn_point, monster;
+ IL_EACH(g_invasion_waves, it.cnt == wavenum,
+ {
+ return it; // found one
+ });
- spawn_point = invasion_PickSpawn();
+ // if no specific one is found, find the last existing wave ent
+ entity best = NULL;
+ IL_EACH(g_invasion_waves, it.cnt <= wavenum,
+ {
+ if(!best || it.cnt > best.cnt)
+ best = it;
+ });
+
+ return best;
+}
+
+void invasion_SpawnChosenMonster(Monster mon)
+{
+ entity monster;
+ entity spawn_point = invasion_PickSpawn();
+ entity wave_ent = invasion_GetWaveEntity(inv_roundcnt);
+
+ string tospawn = "";
+ if(wave_ent && wave_ent.spawnmob && wave_ent.spawnmob != "")
+ {
+ RandomSelection_Init();
+ FOREACH_WORD(wave_ent.spawnmob, true,
+ {
+ RandomSelection_AddString(it, 1, 1);
+ });
+
+ tospawn = RandomSelection_chosen_string;
+ }
if(spawn_point == NULL)
{
- LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations");
+ if(!inv_warning_shown)
+ {
+ inv_warning_shown = true;
+ LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations");
+ }
entity e = spawn();
- setsize(e, (get_monsterinfo(mon)).mins, (get_monsterinfo(mon)).maxs);
+ setsize(e, mon.m_mins, mon.m_maxs);
if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
- monster = spawnmonster("", mon, NULL, NULL, e.origin, false, false, 2);
- else return;
-
- setthink(e, SUB_Remove);
- e.nextthink = time + 0.1;
+ monster = spawnmonster(e, tospawn, mon.monsterid, NULL, NULL, e.origin, false, false, 2);
+ else
+ {
+ delete(e);
+ return;
+ }
}
- else
- monster = spawnmonster("", ((spawn_point.monsterid) ? spawn_point.monsterid : mon), spawn_point, spawn_point, spawn_point.origin, false, false, 2);
+ else // if spawnmob field falls through (unset), fallback to mon (relying on spawnmonster for that behaviour)
+ monster = spawnmonster(spawn(), ((spawn_point.spawnmob && spawn_point.spawnmob != "") ? spawn_point.spawnmob : tospawn), mon.monsterid, spawn_point, spawn_point, spawn_point.origin, false, false, 2);
+
+ if(!monster)
+ return;
- if(spawn_point) monster.target2 = spawn_point.target2;
monster.spawnshieldtime = time;
- if(spawn_point && spawn_point.target_range) monster.target_range = spawn_point.target_range;
- if(teamplay)
- if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
- monster.team = spawn_point.team;
- else
+ if(spawn_point)
{
- RandomSelection_Init();
- if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_Add(NULL, NUM_TEAM_1, string_null, 1, 1);
- if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_Add(NULL, NUM_TEAM_2, string_null, 1, 1);
- if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_Add(NULL, NUM_TEAM_3, string_null, 1, 1); }
- if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_Add(NULL, NUM_TEAM_4, string_null, 1, 1); }
-
- monster.team = RandomSelection_chosen_float;
+ if(spawn_point.target_range)
+ monster.target_range = spawn_point.target_range;
+ monster.target2 = spawn_point.target2;
}
if(teamplay)
{
+ if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
+ monster.team = spawn_point.team;
+ else
+ {
+ RandomSelection_Init();
+ if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_AddFloat(NUM_TEAM_1, 1, 1);
+ if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_AddFloat(NUM_TEAM_2, 1, 1);
+ if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_AddFloat(NUM_TEAM_3, 1, 1); }
+ if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_AddFloat(NUM_TEAM_4, 1, 1); }
+
+ monster.team = RandomSelection_chosen_float;
+ }
+
monster_setupcolors(monster);
if(monster.sprite)
}
}
+ if(monster.monster_attack)
+ IL_REMOVE(g_monster_targets, monster);
monster.monster_attack = false; // it's the player's job to kill all the monsters
if(inv_roundcnt >= inv_maxrounds)
monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
}
-void invasion_SpawnMonsters(float supermonster_count)
+void invasion_SpawnMonsters(int supermonster_count)
{
- float chosen_monster = invasion_PickMonster(supermonster_count);
+ Monster chosen_monster = invasion_PickMonster(supermonster_count);
invasion_SpawnChosenMonster(chosen_monster);
}
-float Invasion_CheckWinner()
+bool Invasion_CheckWinner()
{
if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
{
}
else
{
- FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
- float cs = PlayerScore_Add(it, SP_KILLS, 0);
+ FOREACH_CLIENT(IS_PLAYER(it), {
+ float cs = GameRules_scoring_add(it, KILLS, 0);
if(cs > winning_score)
{
winning_score = cs;
winner = it;
}
- ));
+ });
}
IL_EACH(g_monsters, true,
void Invasion_RoundStart()
{
int numplayers = 0;
- FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
+ FOREACH_CLIENT(IS_PLAYER(it), {
it.player_blocked = false;
++numplayers;
- ));
+ });
if(inv_roundcnt < inv_maxrounds)
inv_roundcnt += 1; // a limiter to stop crazy counts
if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
{
- inv_numkilled += 1;
- inv_maxcurrent -= 1;
+ if(autocvar_g_invasion_type == INV_TYPE_ROUND)
+ {
+ inv_numkilled += 1;
+ inv_maxcurrent -= 1;
+ }
if(teamplay) { inv_monsters_perteam[frag_target.team] -= 1; }
if(IS_PLAYER(frag_attacker))
if(SAME_TEAM(frag_attacker, frag_target)) // in non-teamplay modes, same team = same player, so this works
- PlayerScore_Add(frag_attacker, SP_KILLS, -1);
+ GameRules_scoring_add(frag_attacker, KILLS, -1);
else
{
- PlayerScore_Add(frag_attacker, SP_KILLS, +1);
+ GameRules_scoring_add(frag_attacker, KILLS, +1);
if(teamplay)
TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
}
MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
{
entity mon = M_ARGV(0, entity);
+ mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
+
+ if(autocvar_g_invasion_type == INV_TYPE_HUNT)
+ return false; // allowed
if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
return true;
if((get_monsterinfo(mon.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
-
- mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
-}
-
-MUTATOR_HOOKFUNCTION(inv, OnEntityPreSpawn)
-{
- entity ent = M_ARGV(0, entity);
-
- if(startsWith(ent.classname, "monster_"))
- if(!(ent.spawnflags & MONSTERFLAG_SPAWNED))
- return true;
}
MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
{
+ if(autocvar_g_invasion_type != INV_TYPE_ROUND)
+ return; // uses map spawned monsters
+
monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
monsters_killed = inv_numkilled;
}
MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
{
- // no regeneration in invasion
+ // no regeneration in invasion, regardless of the game type
return true;
}
{
entity player = M_ARGV(0, entity);
+ if(player.bot_attack)
+ IL_REMOVE(g_bot_targets, player);
player.bot_attack = false;
}
-MUTATOR_HOOKFUNCTION(inv, PlayerDamage_Calculate)
+MUTATOR_HOOKFUNCTION(inv, Damage_Calculate)
{
entity frag_attacker = M_ARGV(1, entity);
entity frag_target = M_ARGV(2, entity);
}
}
-MUTATOR_HOOKFUNCTION(inv, SV_ParseClientCommand)
-{
- if(MUTATOR_RETURNVALUE) // command was already handled?
- return;
-
- entity player = M_ARGV(0, entity);
- string cmd_name = M_ARGV(1, string);
-
- if(cmd_name == "debuginvasion")
- {
- sprint(player, strcat("inv_maxspawned = ", ftos(inv_maxspawned), "\n"));
- sprint(player, strcat("inv_numspawned = ", ftos(inv_numspawned), "\n"));
- sprint(player, strcat("inv_numkilled = ", ftos(inv_numkilled), "\n"));
- sprint(player, strcat("inv_roundcnt = ", ftos(inv_roundcnt), "\n"));
- sprint(player, strcat("monsters_total = ", ftos(monsters_total), "\n"));
- sprint(player, strcat("monsters_killed = ", ftos(monsters_killed), "\n"));
- sprint(player, strcat("inv_monsterskill = ", ftos(inv_monsterskill), "\n"));
-
- return true;
- }
-}
-
MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
{
entity targ = M_ARGV(1, entity);
MUTATOR_HOOKFUNCTION(inv, SetStartItems)
{
- start_health = 200;
- start_armorvalue = 200;
+ if(autocvar_g_invasion_type == INV_TYPE_ROUND)
+ {
+ start_health = 200;
+ start_armorvalue = 200;
+ }
}
MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
return true;
}
-MUTATOR_HOOKFUNCTION(inv, GetTeamCount, CBC_ORDER_EXCLUSIVE)
+MUTATOR_HOOKFUNCTION(inv, CheckRules_World)
+{
+ if(autocvar_g_invasion_type == INV_TYPE_ROUND)
+ return false;
+
+ M_ARGV(0, float) = WinningCondition_Invasion();
+ return true;
+}
+
+MUTATOR_HOOKFUNCTION(inv, CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
{
M_ARGV(0, float) = invasion_teams;
}
void invasion_ScoreRules(int inv_teams)
{
if(inv_teams) { CheckAllowedTeams(NULL); }
- ScoreRules_basics(inv_teams, 0, 0, false);
- if(inv_teams) ScoreInfo_SetLabel_TeamScore(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
- ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
- ScoreRules_basics_end();
+ GameRules_score_enabled(false);
+ GameRules_scoring(inv_teams, 0, 0, {
+ if (inv_teams) {
+ field_team(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
+ }
+ field(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
+ });
}
void invasion_DelayedInit(entity this) // Do this check with a delay so we can wait for teams to be set up.
{
+ if(autocvar_g_invasion_type == INV_TYPE_HUNT || autocvar_g_invasion_type == INV_TYPE_STAGE)
+ cvar_set("fraglimit", "0");
+
if(autocvar_g_invasion_teams)
{
- invasion_teams = bound(2, autocvar_g_invasion_teams, 4);
- int teams = 0;
- if(invasion_teams >= 1) teams |= BIT(0);
- if(invasion_teams >= 2) teams |= BIT(1);
- if(invasion_teams >= 3) teams |= BIT(2);
- if(invasion_teams >= 4) teams |= BIT(3);
-
- invasion_teams = teams; // now set it?
+ invasion_teams = BITS(bound(2, autocvar_g_invasion_teams, 4));
}
else
invasion_teams = 0;
independent_players = 0;
- round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
- round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
+ if(autocvar_g_invasion_type == INV_TYPE_ROUND)
+ {
+ round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
+ round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
- inv_roundcnt = 0;
- inv_maxrounds = 15; // 15?
+ inv_roundcnt = 0;
+ inv_maxrounds = 15; // 15?
+ }
}
void invasion_Initialize()
{
- if(autocvar_g_invasion_zombies_only) {
- Monster mon = MON_ZOMBIE;
- mon.mr_precache(mon);
- } else
- {
- float i;
- entity mon;
- for(i = MON_FIRST; i <= MON_LAST; ++i)
- {
- mon = get_monsterinfo(i);
- if((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM))
- continue; // flying/swimming monsters not yet supported
-
- mon.mr_precache(mon);
- }
- }
-
InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);
}
-
-#endif