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 <server/bot/api.qh>
8 #include <server/world.qh>
9 #include <server/teamplay.qh>
11 IntrusiveList g_invasion_roundends;
12 IntrusiveList g_invasion_waves;
13 IntrusiveList g_invasion_spawns;
14 STATIC_INIT(g_invasion)
16 g_invasion_roundends = IL_NEW();
17 g_invasion_waves = IL_NEW();
18 g_invasion_spawns = IL_NEW();
21 float autocvar_g_invasion_round_timelimit;
22 float autocvar_g_invasion_spawnpoint_spawn_delay;
23 float autocvar_g_invasion_warmup;
24 int autocvar_g_invasion_monster_count;
25 bool autocvar_g_invasion_zombies_only;
26 float autocvar_g_invasion_spawn_delay;
31 bool inv_warning_shown; // spammy
33 void target_invasion_roundend_use(entity this, entity actor, entity trigger)
35 if(!IS_PLAYER(actor)) { return; }
37 actor.inv_endreached = true;
41 // let's not count bots
42 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
47 if(plnum < ceil(realplnum * min(1, this.count))) // 70% of players
53 spawnfunc(target_invasion_roundend)
55 if(!g_invasion) { delete(this); return; }
57 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)
59 if(!this.count) { this.count = 0.7; } // require at least 70% of the players to reach the end before triggering victory
61 this.use = target_invasion_roundend_use;
63 IL_PUSH(g_invasion_roundends, this);
66 spawnfunc(invasion_wave)
68 if(!g_invasion) { delete(this); return; }
70 IL_PUSH(g_invasion_waves, this);
73 spawnfunc(invasion_spawnpoint)
75 if(!g_invasion) { delete(this); return; }
77 IL_PUSH(g_invasion_spawns, this);
82 // Invasion stage mode winning condition: If the attackers triggered a round end (by fulfilling all objectives)
84 int WinningCondition_Invasion()
86 WinningConditionHelper(NULL); // set worldstatus
88 int status = WINNING_NO;
90 if(autocvar_g_invasion_type == INV_TYPE_STAGE)
92 SetWinners(inv_endreached, true);
95 IL_EACH(g_invasion_roundends, true,
100 bprint("Invasion: round completed.\n");
101 // winners already set
103 status = WINNING_YES;
109 status = WINNING_YES; // just end it? TODO: should warn mapper!
111 else if(autocvar_g_invasion_type == INV_TYPE_HUNT)
115 int found = 0; // NOTE: this ends the round if no monsters are placed
116 IL_EACH(g_monsters, !(it.spawnflags & MONSTERFLAG_RESPAWNED),
123 FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it),
127 status = WINNING_YES;
134 Monster invasion_PickMonster(int supermonster_count)
136 RandomSelection_Init();
138 FOREACH(Monsters, it != MON_Null,
140 if((it.spawnflags & MON_FLAG_HIDDEN) || (it.spawnflags & MONSTER_TYPE_PASSIVE) || (it.spawnflags & MONSTER_TYPE_FLY) || (it.spawnflags & MONSTER_TYPE_SWIM)
141 || (it.spawnflags & MONSTER_SIZE_QUAKE) || ((it.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
143 if(autocvar_g_invasion_zombies_only && !(it.spawnflags & MONSTER_TYPE_UNDEAD))
145 RandomSelection_AddEnt(it, 1, 1);
148 return RandomSelection_chosen_ent;
151 entity invasion_PickSpawn()
153 RandomSelection_Init();
155 IL_EACH(g_invasion_spawns, true,
157 RandomSelection_AddEnt(it, 1, ((time < it.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
158 it.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
161 return RandomSelection_chosen_ent;
164 entity invasion_GetWaveEntity(int wavenum)
166 IL_EACH(g_invasion_waves, it.cnt == wavenum,
168 return it; // found one
171 // if no specific one is found, find the last existing wave ent
173 IL_EACH(g_invasion_waves, it.cnt <= wavenum,
175 if(!best || it.cnt > best.cnt)
182 void invasion_SpawnChosenMonster(Monster mon)
185 entity spawn_point = invasion_PickSpawn();
186 entity wave_ent = invasion_GetWaveEntity(inv_roundcnt);
189 if(wave_ent && wave_ent.spawnmob && wave_ent.spawnmob != "")
191 RandomSelection_Init();
192 FOREACH_WORD(wave_ent.spawnmob, true,
194 RandomSelection_AddString(it, 1, 1);
197 tospawn = RandomSelection_chosen_string;
200 if(spawn_point == NULL)
202 if(!inv_warning_shown)
204 inv_warning_shown = true;
205 LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations");
208 setsize(e, mon.m_mins, mon.m_maxs);
210 if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
212 monster = spawnmonster(e, tospawn, mon, NULL, NULL, e.origin, false, false, 2);
213 monster.angles_x = monster.angles_z = 0;
221 else // if spawnmob field falls through (unset), fallback to mon (relying on spawnmonster for that behaviour)
222 monster = spawnmonster(spawn(), ((spawn_point.spawnmob && spawn_point.spawnmob != "") ? spawn_point.spawnmob : tospawn), mon, spawn_point, spawn_point, spawn_point.origin, false, false, 2);
227 monster.spawnshieldtime = time;
231 if(spawn_point.target_range)
232 monster.target_range = spawn_point.target_range;
233 monster.target2 = spawn_point.target2;
236 if(monster.monster_attack)
237 IL_REMOVE(g_monster_targets, monster);
238 monster.monster_attack = false; // it's the player's job to kill all the monsters
240 if(inv_roundcnt >= inv_maxrounds)
241 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
244 void invasion_SpawnMonsters(int supermonster_count)
246 Monster chosen_monster = invasion_PickMonster(supermonster_count);
248 invasion_SpawnChosenMonster(chosen_monster);
251 bool Invasion_CheckWinner()
253 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
255 IL_EACH(g_monsters, true,
259 IL_CLEAR(g_monsters);
261 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
262 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
263 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
267 float total_alive_monsters = 0, supermonster_count = 0;
269 IL_EACH(g_monsters, GetResource(it, RES_HEALTH) > 0,
271 if(it.monsterdef.spawnflags & MON_FLAG_SUPERMONSTER)
272 ++supermonster_count;
273 ++total_alive_monsters;
276 if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
278 if(time >= inv_lastcheck)
280 invasion_SpawnMonsters(supermonster_count);
281 inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
287 if(inv_numspawned < 1)
288 return 0; // nothing has spawned yet
290 if(inv_numkilled < inv_maxspawned)
293 entity winner = NULL;
294 float winning_score = 0;
296 FOREACH_CLIENT(IS_PLAYER(it), {
297 float cs = GameRules_scoring_add(it, KILLS, 0);
298 if(cs > winning_score)
305 IL_EACH(g_monsters, true,
309 IL_CLEAR(g_monsters);
313 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
314 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
317 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
322 bool Invasion_CheckPlayers()
327 void Invasion_RoundStart()
330 FOREACH_CLIENT(IS_PLAYER(it), {
331 it.player_blocked = false;
335 if(inv_roundcnt < inv_maxrounds)
336 inv_roundcnt += 1; // a limiter to stop crazy counts
338 inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
344 inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
347 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
349 entity frag_target = M_ARGV(0, entity);
350 entity frag_attacker = M_ARGV(1, entity);
352 if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
354 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
360 if(IS_PLAYER(frag_attacker))
362 if(SAME_TEAM(frag_attacker, frag_target))
363 GameRules_scoring_add(frag_attacker, KILLS, -1);
365 GameRules_scoring_add(frag_attacker, KILLS, +1);
370 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
372 entity mon = M_ARGV(0, entity);
373 mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
375 if(autocvar_g_invasion_type == INV_TYPE_HUNT)
376 return false; // allowed
378 if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
381 if(!(mon.spawnflags & MONSTERFLAG_RESPAWNED))
387 mon.monster_skill = inv_monsterskill;
389 if(mon.monsterdef.spawnflags & MON_FLAG_SUPERMONSTER)
390 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
393 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
395 if(autocvar_g_invasion_type != INV_TYPE_ROUND)
396 return; // uses map spawned monsters
398 monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
399 monsters_killed = inv_numkilled;
402 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
404 // no regeneration in invasion, regardless of the game type
408 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
410 entity player = M_ARGV(0, entity);
412 if(player.bot_attack)
413 IL_REMOVE(g_bot_targets, player);
414 player.bot_attack = false;
417 MUTATOR_HOOKFUNCTION(inv, Damage_Calculate)
419 entity frag_attacker = M_ARGV(1, entity);
420 entity frag_target = M_ARGV(2, entity);
421 float frag_damage = M_ARGV(4, float);
422 vector frag_force = M_ARGV(6, vector);
424 if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
427 frag_force = '0 0 0';
429 M_ARGV(4, float) = frag_damage;
430 M_ARGV(6, vector) = frag_force;
434 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
436 entity targ = M_ARGV(1, entity);
438 if(!IS_MONSTER(targ))
442 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
444 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
447 start_armorvalue = 200;
451 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
453 entity frag_target = M_ARGV(1, entity);
455 if(IS_MONSTER(frag_target))
456 return MUT_ACCADD_INVALID;
457 return MUT_ACCADD_INDIFFERENT;
460 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
462 // monster spawning disabled during an invasion
463 M_ARGV(1, string) = "You cannot spawn monsters during an invasion!";
467 MUTATOR_HOOKFUNCTION(inv, CheckRules_World)
469 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
472 M_ARGV(0, float) = WinningCondition_Invasion();
476 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
478 M_ARGV(0, string) = "This command does not work during an invasion!";
482 void invasion_ScoreRules()
484 GameRules_score_enabled(false);
485 GameRules_scoring(0, 0, 0, {
486 field(SP_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
490 void invasion_DelayedInit(entity this)
492 if(autocvar_g_invasion_type == INV_TYPE_HUNT || autocvar_g_invasion_type == INV_TYPE_STAGE)
493 cvar_set("fraglimit", "0");
495 independent_players = 1; // to disable extra useless scores
497 invasion_ScoreRules();
499 independent_players = 0;
501 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
503 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
504 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
507 inv_maxrounds = 15; // 15?
511 void invasion_Initialize()
513 InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);