5 #include <common/monsters/sv_spawn.qh>
6 #include <common/monsters/sv_spawner.qh>
7 #include <common/monsters/sv_monsters.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 this.classname = "invasion_spawnpoint";
78 IL_PUSH(g_invasion_spawns, this);
83 // Invasion stage mode winning condition: If the attackers triggered a round end (by fulfilling all objectives)
85 int WinningCondition_Invasion()
87 WinningConditionHelper(NULL); // set worldstatus
89 int status = WINNING_NO;
91 if(autocvar_g_invasion_type == INV_TYPE_STAGE)
93 SetWinners(inv_endreached, true);
96 IL_EACH(g_invasion_roundends, true,
101 bprint("Invasion: round completed.\n");
102 // winners already set (TODO: teamplay support)
104 status = WINNING_YES;
110 status = WINNING_YES; // just end it? TODO: should warn mapper!
112 else if(autocvar_g_invasion_type == INV_TYPE_HUNT)
116 int found = 0; // NOTE: this ends the round if no monsters are placed
117 IL_EACH(g_monsters, !(it.spawnflags & MONSTERFLAG_RESPAWNED),
124 FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it),
128 status = WINNING_YES;
135 Monster invasion_PickMonster(int supermonster_count)
137 RandomSelection_Init();
139 FOREACH(Monsters, it != MON_Null,
141 if((it.spawnflags & MON_FLAG_HIDDEN) || (it.spawnflags & MONSTER_TYPE_PASSIVE) || (it.spawnflags & MONSTER_TYPE_FLY) || (it.spawnflags & MONSTER_TYPE_SWIM) ||
142 (it.spawnflags & MONSTER_SIZE_QUAKE) || ((it.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
144 if(autocvar_g_invasion_zombies_only && !(it.spawnflags & MONSTER_TYPE_UNDEAD))
146 RandomSelection_AddEnt(it, 1, 1);
149 return RandomSelection_chosen_ent;
152 entity invasion_PickSpawn()
154 RandomSelection_Init();
156 IL_EACH(g_invasion_spawns, true,
158 RandomSelection_AddEnt(it, 1, ((time < it.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
159 it.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
162 return RandomSelection_chosen_ent;
165 entity invasion_GetWaveEntity(int wavenum)
167 IL_EACH(g_invasion_waves, it.cnt == wavenum,
169 return it; // found one
172 // if no specific one is found, find the last existing wave ent
174 IL_EACH(g_invasion_waves, it.cnt <= wavenum,
176 if(!best || it.cnt > best.cnt)
183 void invasion_SpawnChosenMonster(Monster mon)
186 entity spawn_point = invasion_PickSpawn();
187 entity wave_ent = invasion_GetWaveEntity(inv_roundcnt);
190 if(wave_ent && wave_ent.spawnmob && wave_ent.spawnmob != "")
192 RandomSelection_Init();
193 FOREACH_WORD(wave_ent.spawnmob, true,
195 RandomSelection_AddString(it, 1, 1);
198 tospawn = RandomSelection_chosen_string;
201 if(spawn_point == NULL)
203 if(!inv_warning_shown)
205 inv_warning_shown = true;
206 LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations");
209 setsize(e, mon.m_mins, mon.m_maxs);
211 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.monsterid, NULL, NULL, e.origin, false, false, 2);
219 else // if spawnmob field falls through (unset), fallback to mon (relying on spawnmonster for that behaviour)
220 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);
225 monster.spawnshieldtime = time;
229 if(spawn_point.target_range)
230 monster.target_range = spawn_point.target_range;
231 monster.target2 = spawn_point.target2;
236 if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
237 monster.team = spawn_point.team;
240 RandomSelection_Init();
241 if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_AddFloat(NUM_TEAM_1, 1, 1);
242 if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_AddFloat(NUM_TEAM_2, 1, 1);
243 if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_AddFloat(NUM_TEAM_3, 1, 1); }
244 if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_AddFloat(NUM_TEAM_4, 1, 1); }
246 monster.team = RandomSelection_chosen_float;
249 monster_setupcolors(monster);
253 WaypointSprite_UpdateTeamRadar(monster.sprite, RADARICON_DANGER, ((monster.team) ? Team_ColorRGB(monster.team) : '1 0 0'));
255 monster.sprite.team = 0;
256 monster.sprite.SendFlags |= 1;
260 if(monster.monster_attack)
261 IL_REMOVE(g_monster_targets, monster);
262 monster.monster_attack = false; // it's the player's job to kill all the monsters
264 if(inv_roundcnt >= inv_maxrounds)
265 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
268 void invasion_SpawnMonsters(int supermonster_count)
270 Monster chosen_monster = invasion_PickMonster(supermonster_count);
272 invasion_SpawnChosenMonster(chosen_monster);
275 bool Invasion_CheckWinner()
277 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
279 IL_EACH(g_monsters, true,
283 IL_CLEAR(g_monsters);
285 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
286 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
287 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
291 float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
293 IL_EACH(g_monsters, GetResourceAmount(it, RESOURCE_HEALTH) > 0,
295 if((get_monsterinfo(it.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
296 ++supermonster_count;
297 ++total_alive_monsters;
302 case NUM_TEAM_1: ++red_alive; break;
303 case NUM_TEAM_2: ++blue_alive; break;
304 case NUM_TEAM_3: ++yellow_alive; break;
305 case NUM_TEAM_4: ++pink_alive; break;
309 if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
311 if(time >= inv_lastcheck)
313 invasion_SpawnMonsters(supermonster_count);
314 inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
320 if(inv_numspawned < 1)
321 return 0; // nothing has spawned yet
325 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
328 else if(inv_numkilled < inv_maxspawned)
331 entity winner = NULL;
332 float winning_score = 0, winner_team = 0;
337 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
339 if(winner_team) { winner_team = 0; }
340 else { winner_team = NUM_TEAM_2; }
342 if(winner_team) { winner_team = 0; }
343 else { winner_team = NUM_TEAM_3; }
345 if(winner_team) { winner_team = 0; }
346 else { winner_team = NUM_TEAM_4; }
350 FOREACH_CLIENT(IS_PLAYER(it), {
351 float cs = GameRules_scoring_add(it, KILLS, 0);
352 if(cs > winning_score)
360 IL_EACH(g_monsters, true,
364 IL_CLEAR(g_monsters);
370 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
371 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
376 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
377 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
380 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
385 bool Invasion_CheckPlayers()
390 void Invasion_RoundStart()
393 FOREACH_CLIENT(IS_PLAYER(it), {
394 it.player_blocked = false;
398 if(inv_roundcnt < inv_maxrounds)
399 inv_roundcnt += 1; // a limiter to stop crazy counts
401 inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
407 inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
411 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
412 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
413 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
414 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
415 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
419 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
421 entity frag_target = M_ARGV(0, entity);
422 entity frag_attacker = M_ARGV(1, entity);
424 if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
426 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
431 if(teamplay) { inv_monsters_perteam[frag_target.team] -= 1; }
433 if(IS_PLAYER(frag_attacker))
434 if(SAME_TEAM(frag_attacker, frag_target)) // in non-teamplay modes, same team = same player, so this works
435 GameRules_scoring_add(frag_attacker, KILLS, -1);
438 GameRules_scoring_add(frag_attacker, KILLS, +1);
440 TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
445 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
447 entity mon = M_ARGV(0, entity);
448 mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
450 if(autocvar_g_invasion_type == INV_TYPE_HUNT)
451 return false; // allowed
453 if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
456 if(!(mon.spawnflags & MONSTERFLAG_RESPAWNED))
462 mon.monster_skill = inv_monsterskill;
464 if((get_monsterinfo(mon.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
465 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
468 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
470 if(autocvar_g_invasion_type != INV_TYPE_ROUND)
471 return; // uses map spawned monsters
473 monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
474 monsters_killed = inv_numkilled;
477 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
479 // no regeneration in invasion, regardless of the game type
483 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
485 entity player = M_ARGV(0, entity);
487 if(player.bot_attack)
488 IL_REMOVE(g_bot_targets, player);
489 player.bot_attack = false;
492 MUTATOR_HOOKFUNCTION(inv, Damage_Calculate)
494 entity frag_attacker = M_ARGV(1, entity);
495 entity frag_target = M_ARGV(2, entity);
496 float frag_damage = M_ARGV(4, float);
497 vector frag_force = M_ARGV(6, vector);
499 if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
502 frag_force = '0 0 0';
504 M_ARGV(4, float) = frag_damage;
505 M_ARGV(6, vector) = frag_force;
509 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
511 entity targ = M_ARGV(1, entity);
513 if(!IS_MONSTER(targ))
517 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
519 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
522 start_armorvalue = 200;
526 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
528 entity frag_target = M_ARGV(1, entity);
530 if(IS_MONSTER(frag_target))
531 return MUT_ACCADD_INVALID;
532 return MUT_ACCADD_INDIFFERENT;
535 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
537 // monster spawning disabled during an invasion
538 M_ARGV(1, string) = "You cannot spawn monsters during an invasion!";
542 MUTATOR_HOOKFUNCTION(inv, CheckRules_World)
544 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
547 M_ARGV(0, float) = WinningCondition_Invasion();
551 MUTATOR_HOOKFUNCTION(inv, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
553 M_ARGV(0, float) = invasion_teams;
557 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
559 M_ARGV(0, string) = "This command does not work during an invasion!";
563 void invasion_ScoreRules(int inv_teams)
565 //if(inv_teams) { CheckAllowedTeams(NULL); } // Another bug?
566 GameRules_score_enabled(false);
567 GameRules_scoring(inv_teams, 0, 0, {
569 field_team(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
571 field(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
575 void invasion_DelayedInit(entity this) // Do this check with a delay so we can wait for teams to be set up.
577 if(autocvar_g_invasion_type == INV_TYPE_HUNT || autocvar_g_invasion_type == INV_TYPE_STAGE)
578 cvar_set("fraglimit", "0");
580 if(autocvar_g_invasion_teams)
582 invasion_teams = BITS(bound(2, autocvar_g_invasion_teams, 4));
587 independent_players = 1; // to disable extra useless scores
589 invasion_ScoreRules(invasion_teams);
591 independent_players = 0;
593 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
595 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
596 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
599 inv_maxrounds = 15; // 15?
603 void invasion_Initialize()
605 InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);