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;
23 spawnfunc(invasion_wave)
25 if(!g_invasion) { delete(this); return; }
27 IL_PUSH(g_invasion_waves, this);
30 spawnfunc(invasion_spawnpoint)
32 if(!g_invasion) { delete(this); return; }
34 this.classname = "invasion_spawnpoint";
35 IL_PUSH(g_invasion_spawns, this);
38 Monster invasion_PickMonster(int supermonster_count)
40 RandomSelection_Init();
42 FOREACH(Monsters, it != MON_Null,
44 if((it.spawnflags & MON_FLAG_HIDDEN) || (it.spawnflags & MONSTER_TYPE_PASSIVE) || (it.spawnflags & MONSTER_TYPE_FLY) || (it.spawnflags & MONSTER_TYPE_SWIM) ||
45 (it.spawnflags & MONSTER_SIZE_QUAKE) || ((it.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
47 if(autocvar_g_invasion_zombies_only && !(it.spawnflags & MONSTER_TYPE_UNDEAD))
49 RandomSelection_AddEnt(it, 1, 1);
52 return RandomSelection_chosen_ent;
55 entity invasion_PickSpawn()
57 RandomSelection_Init();
59 IL_EACH(g_invasion_spawns, true,
61 RandomSelection_AddEnt(it, 1, ((time < it.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
62 it.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
65 return RandomSelection_chosen_ent;
68 entity invasion_GetWaveEntity(int wavenum)
70 IL_EACH(g_invasion_waves, it.cnt == wavenum,
72 return it; // found one
75 // if no specific one is found, find the last existing wave ent
77 IL_EACH(g_invasion_waves, it.cnt <= wavenum,
79 if(!best || it.cnt > best.cnt)
86 void invasion_SpawnChosenMonster(Monster mon)
89 entity spawn_point = invasion_PickSpawn();
90 entity wave_ent = invasion_GetWaveEntity(inv_roundcnt);
93 if(wave_ent && wave_ent.spawnmob && wave_ent.spawnmob != "")
95 RandomSelection_Init();
96 FOREACH_WORD(wave_ent.spawnmob, true,
98 RandomSelection_AddString(it, 1, 1);
101 tospawn = RandomSelection_chosen_string;
104 if(spawn_point == NULL)
106 LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations");
108 setsize(e, mon.mins, mon.maxs);
110 if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
111 monster = spawnmonster(e, tospawn, mon.monsterid, NULL, NULL, e.origin, false, false, 2);
118 else // if spawnmob field falls through (unset), fallback to mon (relying on spawnmonster for that behaviour)
119 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);
124 monster.spawnshieldtime = time;
128 if(spawn_point.target_range)
129 monster.target_range = spawn_point.target_range;
130 monster.target2 = spawn_point.target2;
135 if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
136 monster.team = spawn_point.team;
139 RandomSelection_Init();
140 if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_AddFloat(NUM_TEAM_1, 1, 1);
141 if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_AddFloat(NUM_TEAM_2, 1, 1);
142 if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_AddFloat(NUM_TEAM_3, 1, 1); }
143 if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_AddFloat(NUM_TEAM_4, 1, 1); }
145 monster.team = RandomSelection_chosen_float;
148 monster_setupcolors(monster);
152 WaypointSprite_UpdateTeamRadar(monster.sprite, RADARICON_DANGER, ((monster.team) ? Team_ColorRGB(monster.team) : '1 0 0'));
154 monster.sprite.team = 0;
155 monster.sprite.SendFlags |= 1;
159 monster.monster_attack = false; // it's the player's job to kill all the monsters
161 if(inv_roundcnt >= inv_maxrounds)
162 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
165 void invasion_SpawnMonsters(int supermonster_count)
167 Monster chosen_monster = invasion_PickMonster(supermonster_count);
169 invasion_SpawnChosenMonster(chosen_monster);
172 bool Invasion_CheckWinner()
174 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
176 IL_EACH(g_monsters, true,
180 IL_CLEAR(g_monsters);
182 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
183 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
184 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
188 float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
190 IL_EACH(g_monsters, it.health > 0,
192 if((get_monsterinfo(it.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
193 ++supermonster_count;
194 ++total_alive_monsters;
199 case NUM_TEAM_1: ++red_alive; break;
200 case NUM_TEAM_2: ++blue_alive; break;
201 case NUM_TEAM_3: ++yellow_alive; break;
202 case NUM_TEAM_4: ++pink_alive; break;
206 if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
208 if(time >= inv_lastcheck)
210 invasion_SpawnMonsters(supermonster_count);
211 inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
217 if(inv_numspawned < 1)
218 return 0; // nothing has spawned yet
222 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
225 else if(inv_numkilled < inv_maxspawned)
228 entity winner = NULL;
229 float winning_score = 0, winner_team = 0;
234 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
236 if(winner_team) { winner_team = 0; }
237 else { winner_team = NUM_TEAM_2; }
239 if(winner_team) { winner_team = 0; }
240 else { winner_team = NUM_TEAM_3; }
242 if(winner_team) { winner_team = 0; }
243 else { winner_team = NUM_TEAM_4; }
247 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
248 float cs = PlayerScore_Add(it, SP_KILLS, 0);
249 if(cs > winning_score)
257 IL_EACH(g_monsters, true,
261 IL_CLEAR(g_monsters);
267 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
268 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
273 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
274 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
277 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
282 bool Invasion_CheckPlayers()
287 void Invasion_RoundStart()
290 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
291 it.player_blocked = false;
295 if(inv_roundcnt < inv_maxrounds)
296 inv_roundcnt += 1; // a limiter to stop crazy counts
298 inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
304 inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
308 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
309 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
310 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
311 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
312 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
316 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
318 entity frag_target = M_ARGV(0, entity);
319 entity frag_attacker = M_ARGV(1, entity);
321 if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
325 if(teamplay) { inv_monsters_perteam[frag_target.team] -= 1; }
327 if(IS_PLAYER(frag_attacker))
328 if(SAME_TEAM(frag_attacker, frag_target)) // in non-teamplay modes, same team = same player, so this works
329 PlayerScore_Add(frag_attacker, SP_KILLS, -1);
332 PlayerScore_Add(frag_attacker, SP_KILLS, +1);
334 TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
339 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
341 entity mon = M_ARGV(0, entity);
343 if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
346 if(!(mon.spawnflags & MONSTERFLAG_RESPAWNED))
352 mon.monster_skill = inv_monsterskill;
354 if((get_monsterinfo(mon.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
355 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
357 mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
360 MUTATOR_HOOKFUNCTION(inv, OnEntityPreSpawn)
362 entity ent = M_ARGV(0, entity);
364 // TODO: allow these as "rogues" or something
365 if(startsWith(ent.classname, "monster_"))
366 if(!(ent.spawnflags & MONSTERFLAG_SPAWNED))
370 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
372 monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
373 monsters_killed = inv_numkilled;
376 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
378 // no regeneration in invasion
382 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
384 entity player = M_ARGV(0, entity);
386 if(player.bot_attack)
387 IL_REMOVE(g_bot_targets, player);
388 player.bot_attack = false;
391 MUTATOR_HOOKFUNCTION(inv, Damage_Calculate)
393 entity frag_attacker = M_ARGV(1, entity);
394 entity frag_target = M_ARGV(2, entity);
395 float frag_damage = M_ARGV(4, float);
396 vector frag_force = M_ARGV(6, vector);
398 if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
401 frag_force = '0 0 0';
403 M_ARGV(4, float) = frag_damage;
404 M_ARGV(6, vector) = frag_force;
408 MUTATOR_HOOKFUNCTION(inv, SV_ParseClientCommand)
410 if(MUTATOR_RETURNVALUE) // command was already handled?
413 entity player = M_ARGV(0, entity);
414 string cmd_name = M_ARGV(1, string);
416 if(cmd_name == "debuginvasion")
418 sprint(player, strcat("inv_maxspawned = ", ftos(inv_maxspawned), "\n"));
419 sprint(player, strcat("inv_numspawned = ", ftos(inv_numspawned), "\n"));
420 sprint(player, strcat("inv_numkilled = ", ftos(inv_numkilled), "\n"));
421 sprint(player, strcat("inv_roundcnt = ", ftos(inv_roundcnt), "\n"));
422 sprint(player, strcat("monsters_total = ", ftos(monsters_total), "\n"));
423 sprint(player, strcat("monsters_killed = ", ftos(monsters_killed), "\n"));
424 sprint(player, strcat("inv_monsterskill = ", ftos(inv_monsterskill), "\n"));
430 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
432 entity targ = M_ARGV(1, entity);
434 if(!IS_MONSTER(targ))
438 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
441 start_armorvalue = 200;
444 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
446 entity frag_target = M_ARGV(1, entity);
448 if(IS_MONSTER(frag_target))
449 return MUT_ACCADD_INVALID;
450 return MUT_ACCADD_INDIFFERENT;
453 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
455 // monster spawning disabled during an invasion
456 M_ARGV(1, string) = "You cannot spawn monsters during an invasion!";
460 MUTATOR_HOOKFUNCTION(inv, CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
462 M_ARGV(0, float) = invasion_teams;
465 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
467 M_ARGV(0, string) = "This command does not work during an invasion!";
471 void invasion_ScoreRules(int inv_teams)
473 if(inv_teams) { CheckAllowedTeams(NULL); }
474 ScoreRules_basics(inv_teams, 0, 0, false);
475 if(inv_teams) ScoreInfo_SetLabel_TeamScore(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
476 ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
477 ScoreRules_basics_end();
480 void invasion_DelayedInit(entity this) // Do this check with a delay so we can wait for teams to be set up.
482 if(autocvar_g_invasion_teams)
484 invasion_teams = bound(2, autocvar_g_invasion_teams, 4);
486 if(invasion_teams >= 1) teams |= BIT(0);
487 if(invasion_teams >= 2) teams |= BIT(1);
488 if(invasion_teams >= 3) teams |= BIT(2);
489 if(invasion_teams >= 4) teams |= BIT(3);
491 invasion_teams = teams; // now set it?
496 independent_players = 1; // to disable extra useless scores
498 invasion_ScoreRules(invasion_teams);
500 independent_players = 0;
502 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
503 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
506 inv_maxrounds = 15; // 15?
509 void invasion_Initialize()
511 InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);