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 IntrusiveList g_invasion_roundends;
8 IntrusiveList g_invasion_waves;
9 IntrusiveList g_invasion_spawns;
10 STATIC_INIT(g_invasion)
12 g_invasion_roundends = IL_NEW();
13 g_invasion_waves = IL_NEW();
14 g_invasion_spawns = IL_NEW();
17 float autocvar_g_invasion_round_timelimit;
18 float autocvar_g_invasion_spawnpoint_spawn_delay;
19 float autocvar_g_invasion_warmup;
20 int autocvar_g_invasion_monster_count;
21 bool autocvar_g_invasion_zombies_only;
22 float autocvar_g_invasion_spawn_delay;
27 bool inv_warning_shown; // spammy
29 void target_invasion_roundend_use(entity this, entity actor, entity trigger)
31 if(!IS_PLAYER(actor)) { return; }
33 actor.inv_endreached = true;
37 // let's not count bots
38 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
43 if(plnum < ceil(realplnum * min(1, this.count))) // 70% of players
49 spawnfunc(target_invasion_roundend)
51 if(!g_invasion) { delete(this); return; }
53 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)
55 if(!this.count) { this.count = 0.7; } // require at least 70% of the players to reach the end before triggering victory
57 this.use = target_invasion_roundend_use;
59 IL_PUSH(g_invasion_roundends, this);
62 spawnfunc(invasion_wave)
64 if(!g_invasion) { delete(this); return; }
66 IL_PUSH(g_invasion_waves, this);
69 spawnfunc(invasion_spawnpoint)
71 if(!g_invasion) { delete(this); return; }
73 this.classname = "invasion_spawnpoint";
74 IL_PUSH(g_invasion_spawns, this);
79 // Invasion stage mode winning condition: If the attackers triggered a round end (by fulfilling all objectives)
81 int WinningCondition_Invasion()
83 WinningConditionHelper(NULL); // set worldstatus
85 int status = WINNING_NO;
87 if(autocvar_g_invasion_type == INV_TYPE_STAGE)
89 SetWinners(inv_endreached, true);
92 IL_EACH(g_invasion_roundends, true,
97 bprint("Invasion: round completed.\n");
98 // winners already set
100 status = WINNING_YES;
106 status = WINNING_YES; // just end it? TODO: should warn mapper!
108 else if(autocvar_g_invasion_type == INV_TYPE_HUNT)
112 int found = 0; // NOTE: this ends the round if no monsters are placed
113 IL_EACH(g_monsters, !(it.spawnflags & MONSTERFLAG_RESPAWNED),
120 FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it),
124 status = WINNING_YES;
131 Monster invasion_PickMonster(int supermonster_count)
133 RandomSelection_Init();
135 FOREACH(Monsters, it != MON_Null,
137 if((it.spawnflags & MON_FLAG_HIDDEN) || (it.spawnflags & MONSTER_TYPE_PASSIVE) || (it.spawnflags & MONSTER_TYPE_FLY) || (it.spawnflags & MONSTER_TYPE_SWIM)
138 || (it.spawnflags & MONSTER_SIZE_QUAKE) || ((it.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
140 if(autocvar_g_invasion_zombies_only && !(it.spawnflags & MONSTER_TYPE_UNDEAD))
142 RandomSelection_AddEnt(it, 1, 1);
145 return RandomSelection_chosen_ent;
148 entity invasion_PickSpawn()
150 RandomSelection_Init();
152 IL_EACH(g_invasion_spawns, true,
154 RandomSelection_AddEnt(it, 1, ((time < it.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
155 it.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
158 return RandomSelection_chosen_ent;
161 entity invasion_GetWaveEntity(int wavenum)
163 IL_EACH(g_invasion_waves, it.cnt == wavenum,
165 return it; // found one
168 // if no specific one is found, find the last existing wave ent
170 IL_EACH(g_invasion_waves, it.cnt <= wavenum,
172 if(!best || it.cnt > best.cnt)
179 void invasion_SpawnChosenMonster(Monster mon)
182 entity spawn_point = invasion_PickSpawn();
183 entity wave_ent = invasion_GetWaveEntity(inv_roundcnt);
186 if(wave_ent && wave_ent.spawnmob && wave_ent.spawnmob != "")
188 RandomSelection_Init();
189 FOREACH_WORD(wave_ent.spawnmob, true,
191 RandomSelection_AddString(it, 1, 1);
194 tospawn = RandomSelection_chosen_string;
197 if(spawn_point == NULL)
199 if(!inv_warning_shown)
201 inv_warning_shown = true;
202 LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations");
205 setsize(e, mon.m_mins, mon.m_maxs);
207 if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
209 monster = spawnmonster(e, tospawn, mon.monsterid, NULL, NULL, e.origin, false, false, 2);
210 monster.angles_x = monster.angles_z = 0;
218 else // if spawnmob field falls through (unset), fallback to mon (relying on spawnmonster for that behaviour)
219 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);
224 monster.spawnshieldtime = time;
228 if(spawn_point.target_range)
229 monster.target_range = spawn_point.target_range;
230 monster.target2 = spawn_point.target2;
233 if(monster.monster_attack)
234 IL_REMOVE(g_monster_targets, monster);
235 monster.monster_attack = false; // it's the player's job to kill all the monsters
237 if(inv_roundcnt >= inv_maxrounds)
238 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
241 void invasion_SpawnMonsters(int supermonster_count)
243 Monster chosen_monster = invasion_PickMonster(supermonster_count);
245 invasion_SpawnChosenMonster(chosen_monster);
248 bool Invasion_CheckWinner()
250 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
252 IL_EACH(g_monsters, true,
256 IL_CLEAR(g_monsters);
258 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
259 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
260 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
264 float total_alive_monsters = 0, supermonster_count = 0;
266 IL_EACH(g_monsters, GetResource(it, RES_HEALTH) > 0,
268 if((get_monsterinfo(it.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
269 ++supermonster_count;
270 ++total_alive_monsters;
273 if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
275 if(time >= inv_lastcheck)
277 invasion_SpawnMonsters(supermonster_count);
278 inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
284 if(inv_numspawned < 1)
285 return 0; // nothing has spawned yet
287 if(inv_numkilled < inv_maxspawned)
290 entity winner = NULL;
291 float winning_score = 0;
293 FOREACH_CLIENT(IS_PLAYER(it), {
294 float cs = GameRules_scoring_add(it, KILLS, 0);
295 if(cs > winning_score)
302 IL_EACH(g_monsters, true,
306 IL_CLEAR(g_monsters);
310 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
311 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
314 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
319 bool Invasion_CheckPlayers()
324 void Invasion_RoundStart()
327 FOREACH_CLIENT(IS_PLAYER(it), {
328 it.player_blocked = false;
332 if(inv_roundcnt < inv_maxrounds)
333 inv_roundcnt += 1; // a limiter to stop crazy counts
335 inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
341 inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
344 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
346 entity frag_target = M_ARGV(0, entity);
347 entity frag_attacker = M_ARGV(1, entity);
349 if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
351 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
357 if(IS_PLAYER(frag_attacker))
359 if(SAME_TEAM(frag_attacker, frag_target))
360 GameRules_scoring_add(frag_attacker, KILLS, -1);
362 GameRules_scoring_add(frag_attacker, KILLS, +1);
367 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
369 entity mon = M_ARGV(0, entity);
370 mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
372 if(autocvar_g_invasion_type == INV_TYPE_HUNT)
373 return false; // allowed
375 if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
378 if(!(mon.spawnflags & MONSTERFLAG_RESPAWNED))
384 mon.monster_skill = inv_monsterskill;
386 if((get_monsterinfo(mon.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
387 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
390 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
392 if(autocvar_g_invasion_type != INV_TYPE_ROUND)
393 return; // uses map spawned monsters
395 monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
396 monsters_killed = inv_numkilled;
399 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
401 // no regeneration in invasion, regardless of the game type
405 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
407 entity player = M_ARGV(0, entity);
409 if(player.bot_attack)
410 IL_REMOVE(g_bot_targets, player);
411 player.bot_attack = false;
414 MUTATOR_HOOKFUNCTION(inv, Damage_Calculate)
416 entity frag_attacker = M_ARGV(1, entity);
417 entity frag_target = M_ARGV(2, entity);
418 float frag_damage = M_ARGV(4, float);
419 vector frag_force = M_ARGV(6, vector);
421 if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
424 frag_force = '0 0 0';
426 M_ARGV(4, float) = frag_damage;
427 M_ARGV(6, vector) = frag_force;
431 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
433 entity targ = M_ARGV(1, entity);
435 if(!IS_MONSTER(targ))
439 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
441 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
444 start_armorvalue = 200;
448 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
450 entity frag_target = M_ARGV(1, entity);
452 if(IS_MONSTER(frag_target))
453 return MUT_ACCADD_INVALID;
454 return MUT_ACCADD_INDIFFERENT;
457 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
459 // monster spawning disabled during an invasion
460 M_ARGV(1, string) = "You cannot spawn monsters during an invasion!";
464 MUTATOR_HOOKFUNCTION(inv, CheckRules_World)
466 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
469 M_ARGV(0, float) = WinningCondition_Invasion();
473 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
475 M_ARGV(0, string) = "This command does not work during an invasion!";
479 void invasion_ScoreRules()
481 GameRules_score_enabled(false);
482 GameRules_scoring(0, 0, 0, {
483 field(SP_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
487 void invasion_DelayedInit(entity this)
489 if(autocvar_g_invasion_type == INV_TYPE_HUNT || autocvar_g_invasion_type == INV_TYPE_STAGE)
490 cvar_set("fraglimit", "0");
492 independent_players = 1; // to disable extra useless scores
494 invasion_ScoreRules();
496 independent_players = 0;
498 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
500 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
501 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
504 inv_maxrounds = 15; // 15?
508 void invasion_Initialize()
510 InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);