1 #include "gamemode_invasion.qh"
3 #include <common/monsters/sv_spawn.qh>
4 #include <common/monsters/sv_monsters.qh>
6 #include <server/teamplay.qh>
8 IntrusiveList g_invasion_waves;
9 STATIC_INIT(g_invasion_waves) { g_invasion_waves = IL_NEW(); }
11 IntrusiveList g_invasion_spawns;
12 STATIC_INIT(g_invasion_spawns) { g_invasion_spawns = IL_NEW(); }
14 float autocvar_g_invasion_round_timelimit;
15 float autocvar_g_invasion_spawnpoint_spawn_delay;
16 float autocvar_g_invasion_warmup;
17 int autocvar_g_invasion_monster_count;
18 bool autocvar_g_invasion_zombies_only;
19 float autocvar_g_invasion_spawn_delay;
21 bool inv_warning_shown; // spammy
25 spawnfunc(invasion_wave)
27 if(!g_invasion) { delete(this); return; }
29 IL_PUSH(g_invasion_waves, this);
32 spawnfunc(invasion_spawnpoint)
34 if(!g_invasion) { delete(this); return; }
36 this.classname = "invasion_spawnpoint";
37 IL_PUSH(g_invasion_spawns, this);
40 Monster invasion_PickMonster(int supermonster_count)
42 RandomSelection_Init();
44 FOREACH(Monsters, it != MON_Null,
46 if((it.spawnflags & MON_FLAG_HIDDEN) || (it.spawnflags & MONSTER_TYPE_PASSIVE) || (it.spawnflags & MONSTER_TYPE_FLY) || (it.spawnflags & MONSTER_TYPE_SWIM) ||
47 (it.spawnflags & MONSTER_SIZE_QUAKE) || ((it.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
49 if(autocvar_g_invasion_zombies_only && !(it.spawnflags & MONSTER_TYPE_UNDEAD))
51 RandomSelection_AddEnt(it, 1, 1);
54 return RandomSelection_chosen_ent;
57 entity invasion_PickSpawn()
59 RandomSelection_Init();
61 IL_EACH(g_invasion_spawns, true,
63 RandomSelection_AddEnt(it, 1, ((time < it.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
64 it.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
67 return RandomSelection_chosen_ent;
70 entity invasion_GetWaveEntity(int wavenum)
72 IL_EACH(g_invasion_waves, it.cnt == wavenum,
74 return it; // found one
77 // if no specific one is found, find the last existing wave ent
79 IL_EACH(g_invasion_waves, it.cnt <= wavenum,
81 if(!best || it.cnt > best.cnt)
88 void invasion_SpawnChosenMonster(Monster mon)
91 entity spawn_point = invasion_PickSpawn();
92 entity wave_ent = invasion_GetWaveEntity(inv_roundcnt);
95 if(wave_ent && wave_ent.spawnmob && wave_ent.spawnmob != "")
97 RandomSelection_Init();
98 FOREACH_WORD(wave_ent.spawnmob, true,
100 RandomSelection_AddString(it, 1, 1);
103 tospawn = RandomSelection_chosen_string;
106 if(spawn_point == NULL)
108 if(!inv_warning_shown)
110 inv_warning_shown = true;
111 LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations");
114 setsize(e, mon.m_mins, mon.m_maxs);
116 if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
117 monster = spawnmonster(e, tospawn, mon.monsterid, NULL, NULL, e.origin, false, false, 2);
124 else // if spawnmob field falls through (unset), fallback to mon (relying on spawnmonster for that behaviour)
125 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);
130 monster.spawnshieldtime = time;
134 if(spawn_point.target_range)
135 monster.target_range = spawn_point.target_range;
136 monster.target2 = spawn_point.target2;
141 if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
142 monster.team = spawn_point.team;
145 RandomSelection_Init();
146 if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_AddFloat(NUM_TEAM_1, 1, 1);
147 if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_AddFloat(NUM_TEAM_2, 1, 1);
148 if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_AddFloat(NUM_TEAM_3, 1, 1); }
149 if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_AddFloat(NUM_TEAM_4, 1, 1); }
151 monster.team = RandomSelection_chosen_float;
154 monster_setupcolors(monster);
158 WaypointSprite_UpdateTeamRadar(monster.sprite, RADARICON_DANGER, ((monster.team) ? Team_ColorRGB(monster.team) : '1 0 0'));
160 monster.sprite.team = 0;
161 monster.sprite.SendFlags |= 1;
165 if(monster.monster_attack)
166 IL_REMOVE(g_monster_targets, monster);
167 monster.monster_attack = false; // it's the player's job to kill all the monsters
169 if(inv_roundcnt >= inv_maxrounds)
170 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
173 void invasion_SpawnMonsters(int supermonster_count)
175 Monster chosen_monster = invasion_PickMonster(supermonster_count);
177 invasion_SpawnChosenMonster(chosen_monster);
180 bool Invasion_CheckWinner()
182 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
184 IL_EACH(g_monsters, true,
188 IL_CLEAR(g_monsters);
190 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
191 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
192 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
196 float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
198 IL_EACH(g_monsters, it.health > 0,
200 if((get_monsterinfo(it.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
201 ++supermonster_count;
202 ++total_alive_monsters;
207 case NUM_TEAM_1: ++red_alive; break;
208 case NUM_TEAM_2: ++blue_alive; break;
209 case NUM_TEAM_3: ++yellow_alive; break;
210 case NUM_TEAM_4: ++pink_alive; break;
214 if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
216 if(time >= inv_lastcheck)
218 invasion_SpawnMonsters(supermonster_count);
219 inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
225 if(inv_numspawned < 1)
226 return 0; // nothing has spawned yet
230 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
233 else if(inv_numkilled < inv_maxspawned)
236 entity winner = NULL;
237 float winning_score = 0, winner_team = 0;
242 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
244 if(winner_team) { winner_team = 0; }
245 else { winner_team = NUM_TEAM_2; }
247 if(winner_team) { winner_team = 0; }
248 else { winner_team = NUM_TEAM_3; }
250 if(winner_team) { winner_team = 0; }
251 else { winner_team = NUM_TEAM_4; }
255 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
256 float cs = PlayerScore_Add(it, SP_KILLS, 0);
257 if(cs > winning_score)
265 IL_EACH(g_monsters, true,
269 IL_CLEAR(g_monsters);
275 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
276 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
281 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
282 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
285 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
290 bool Invasion_CheckPlayers()
295 void Invasion_RoundStart()
298 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
299 it.player_blocked = false;
303 if(inv_roundcnt < inv_maxrounds)
304 inv_roundcnt += 1; // a limiter to stop crazy counts
306 inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
312 inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
316 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
317 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
318 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
319 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
320 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
324 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
326 entity frag_target = M_ARGV(0, entity);
327 entity frag_attacker = M_ARGV(1, entity);
329 if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
333 if(teamplay) { inv_monsters_perteam[frag_target.team] -= 1; }
335 if(IS_PLAYER(frag_attacker))
336 if(SAME_TEAM(frag_attacker, frag_target)) // in non-teamplay modes, same team = same player, so this works
337 PlayerScore_Add(frag_attacker, SP_KILLS, -1);
340 PlayerScore_Add(frag_attacker, SP_KILLS, +1);
342 TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
347 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
349 entity mon = M_ARGV(0, entity);
351 if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
354 if(!(mon.spawnflags & MONSTERFLAG_RESPAWNED))
360 mon.monster_skill = inv_monsterskill;
362 if((get_monsterinfo(mon.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
363 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
365 mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
368 MUTATOR_HOOKFUNCTION(inv, OnEntityPreSpawn)
370 entity ent = M_ARGV(0, entity);
372 // TODO: allow these as "rogues" or something
373 if(startsWith(ent.classname, "monster_"))
374 if(!(ent.spawnflags & MONSTERFLAG_SPAWNED))
378 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
380 monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
381 monsters_killed = inv_numkilled;
384 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
386 // no regeneration in invasion
390 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
392 entity player = M_ARGV(0, entity);
394 if(player.bot_attack)
395 IL_REMOVE(g_bot_targets, player);
396 player.bot_attack = false;
399 MUTATOR_HOOKFUNCTION(inv, Damage_Calculate)
401 entity frag_attacker = M_ARGV(1, entity);
402 entity frag_target = M_ARGV(2, entity);
403 float frag_damage = M_ARGV(4, float);
404 vector frag_force = M_ARGV(6, vector);
406 if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
409 frag_force = '0 0 0';
411 M_ARGV(4, float) = frag_damage;
412 M_ARGV(6, vector) = frag_force;
416 MUTATOR_HOOKFUNCTION(inv, SV_ParseClientCommand)
418 if(MUTATOR_RETURNVALUE) // command was already handled?
421 entity player = M_ARGV(0, entity);
422 string cmd_name = M_ARGV(1, string);
424 if(cmd_name == "debuginvasion")
426 sprint(player, strcat("inv_maxspawned = ", ftos(inv_maxspawned), "\n"));
427 sprint(player, strcat("inv_numspawned = ", ftos(inv_numspawned), "\n"));
428 sprint(player, strcat("inv_numkilled = ", ftos(inv_numkilled), "\n"));
429 sprint(player, strcat("inv_roundcnt = ", ftos(inv_roundcnt), "\n"));
430 sprint(player, strcat("monsters_total = ", ftos(monsters_total), "\n"));
431 sprint(player, strcat("monsters_killed = ", ftos(monsters_killed), "\n"));
432 sprint(player, strcat("inv_monsterskill = ", ftos(inv_monsterskill), "\n"));
438 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
440 entity targ = M_ARGV(1, entity);
442 if(!IS_MONSTER(targ))
446 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
449 start_armorvalue = 200;
452 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
454 entity frag_target = M_ARGV(1, entity);
456 if(IS_MONSTER(frag_target))
457 return MUT_ACCADD_INVALID;
458 return MUT_ACCADD_INDIFFERENT;
461 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
463 // monster spawning disabled during an invasion
464 M_ARGV(1, string) = "You cannot spawn monsters during an invasion!";
468 MUTATOR_HOOKFUNCTION(inv, CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
470 M_ARGV(0, float) = invasion_teams;
473 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
475 M_ARGV(0, string) = "This command does not work during an invasion!";
479 void invasion_ScoreRules(int inv_teams)
481 if(inv_teams) { CheckAllowedTeams(NULL); }
482 ScoreRules_basics(inv_teams, 0, 0, false);
483 if(inv_teams) ScoreInfo_SetLabel_TeamScore(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
484 ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
485 ScoreRules_basics_end();
488 void invasion_DelayedInit(entity this) // Do this check with a delay so we can wait for teams to be set up.
490 if(autocvar_g_invasion_teams)
492 invasion_teams = bound(2, autocvar_g_invasion_teams, 4);
494 if(invasion_teams >= 1) teams |= BIT(0);
495 if(invasion_teams >= 2) teams |= BIT(1);
496 if(invasion_teams >= 3) teams |= BIT(2);
497 if(invasion_teams >= 4) teams |= BIT(3);
499 invasion_teams = teams; // now set it?
504 independent_players = 1; // to disable extra useless scores
506 invasion_ScoreRules(invasion_teams);
508 independent_players = 0;
510 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
511 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
514 inv_maxrounds = 15; // 15?
517 void invasion_Initialize()
519 InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);