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_spawns;
9 STATIC_INIT(g_invasion_spawns) { g_invasion_spawns = IL_NEW(); }
11 float autocvar_g_invasion_round_timelimit;
12 float autocvar_g_invasion_spawnpoint_spawn_delay;
13 float autocvar_g_invasion_warmup;
14 int autocvar_g_invasion_monster_count;
15 bool autocvar_g_invasion_zombies_only;
16 float autocvar_g_invasion_spawn_delay;
20 spawnfunc(invasion_spawnpoint)
22 if(!g_invasion) { delete(this); return; }
24 this.classname = "invasion_spawnpoint";
25 IL_PUSH(g_invasion_spawns, this);
27 if(autocvar_g_invasion_zombies_only) // precache only if it hasn't been already
30 FOREACH(Monsters, it.netname == this.spawnmob,
37 Monster invasion_PickMonster(int supermonster_count)
39 if(autocvar_g_invasion_zombies_only)
42 RandomSelection_Init();
44 FOREACH(Monsters, it != MON_Null,
46 if((it.spawnflags & MONSTER_TYPE_PASSIVE) || (it.spawnflags & MONSTER_TYPE_FLY) || (it.spawnflags & MONSTER_TYPE_SWIM) || (it.spawnflags & MONSTER_SIZE_QUAKE) || ((it.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
48 RandomSelection_AddEnt(it, 1, 1);
51 return RandomSelection_chosen_ent;
54 entity invasion_PickSpawn()
56 RandomSelection_Init();
58 IL_EACH(g_invasion_spawns, true,
60 RandomSelection_AddEnt(it, 1, ((time >= it.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
61 it.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
64 return RandomSelection_chosen_ent;
67 void invasion_SpawnChosenMonster(Monster mon)
69 entity spawn_point, monster;
71 spawn_point = invasion_PickSpawn();
73 if(spawn_point == NULL)
75 LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations");
77 setsize(e, mon.mins, mon.maxs);
79 if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
80 monster = spawnmonster(e, "", mon.m_id, NULL, NULL, e.origin, false, false, 2);
83 else // if spawnmob field falls through (unset), fallback to mon (relying on spawnmonster for that behaviour)
84 monster = spawnmonster(spawn(), spawn_point.spawnmob, mon.m_id, spawn_point, spawn_point, spawn_point.origin, false, false, 2);
86 if(spawn_point) monster.target2 = spawn_point.target2;
87 monster.spawnshieldtime = time;
88 if(spawn_point && spawn_point.target_range) monster.target_range = spawn_point.target_range;
91 if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
92 monster.team = spawn_point.team;
95 RandomSelection_Init();
96 if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_AddFloat(NUM_TEAM_1, 1, 1);
97 if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_AddFloat(NUM_TEAM_2, 1, 1);
98 if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_AddFloat(NUM_TEAM_3, 1, 1); }
99 if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_AddFloat(NUM_TEAM_4, 1, 1); }
101 monster.team = RandomSelection_chosen_float;
106 monster_setupcolors(monster);
110 WaypointSprite_UpdateTeamRadar(monster.sprite, RADARICON_DANGER, ((monster.team) ? Team_ColorRGB(monster.team) : '1 0 0'));
112 monster.sprite.team = 0;
113 monster.sprite.SendFlags |= 1;
117 monster.monster_attack = false; // it's the player's job to kill all the monsters
119 if(inv_roundcnt >= inv_maxrounds)
120 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
123 void invasion_SpawnMonsters(int supermonster_count)
125 Monster chosen_monster = invasion_PickMonster(supermonster_count);
127 invasion_SpawnChosenMonster(chosen_monster);
130 bool Invasion_CheckWinner()
132 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
134 IL_EACH(g_monsters, true,
138 IL_CLEAR(g_monsters);
140 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
141 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
142 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
146 float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
148 IL_EACH(g_monsters, it.health > 0,
150 if((get_monsterinfo(it.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
151 ++supermonster_count;
152 ++total_alive_monsters;
157 case NUM_TEAM_1: ++red_alive; break;
158 case NUM_TEAM_2: ++blue_alive; break;
159 case NUM_TEAM_3: ++yellow_alive; break;
160 case NUM_TEAM_4: ++pink_alive; break;
164 if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
166 if(time >= inv_lastcheck)
168 invasion_SpawnMonsters(supermonster_count);
169 inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
175 if(inv_numspawned < 1)
176 return 0; // nothing has spawned yet
180 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
183 else if(inv_numkilled < inv_maxspawned)
186 entity winner = NULL;
187 float winning_score = 0, winner_team = 0;
192 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
194 if(winner_team) { winner_team = 0; }
195 else { winner_team = NUM_TEAM_2; }
197 if(winner_team) { winner_team = 0; }
198 else { winner_team = NUM_TEAM_3; }
200 if(winner_team) { winner_team = 0; }
201 else { winner_team = NUM_TEAM_4; }
205 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
206 float cs = PlayerScore_Add(it, SP_KILLS, 0);
207 if(cs > winning_score)
215 IL_EACH(g_monsters, true,
219 IL_CLEAR(g_monsters);
225 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
226 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
231 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
232 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
235 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
240 bool Invasion_CheckPlayers()
245 void Invasion_RoundStart()
248 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
249 it.player_blocked = false;
253 if(inv_roundcnt < inv_maxrounds)
254 inv_roundcnt += 1; // a limiter to stop crazy counts
256 inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
262 inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
266 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
267 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
268 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
269 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
270 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
274 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
276 entity frag_target = M_ARGV(0, entity);
277 entity frag_attacker = M_ARGV(1, entity);
279 if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
283 if(teamplay) { inv_monsters_perteam[frag_target.team] -= 1; }
285 if(IS_PLAYER(frag_attacker))
286 if(SAME_TEAM(frag_attacker, frag_target)) // in non-teamplay modes, same team = same player, so this works
287 PlayerScore_Add(frag_attacker, SP_KILLS, -1);
290 PlayerScore_Add(frag_attacker, SP_KILLS, +1);
292 TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
297 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
299 entity mon = M_ARGV(0, entity);
301 if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
304 if(!(mon.spawnflags & MONSTERFLAG_RESPAWNED))
310 mon.monster_skill = inv_monsterskill;
312 if((get_monsterinfo(mon.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
313 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
315 mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
318 MUTATOR_HOOKFUNCTION(inv, OnEntityPreSpawn)
320 entity ent = M_ARGV(0, entity);
322 if(startsWith(ent.classname, "monster_"))
323 if(!(ent.spawnflags & MONSTERFLAG_SPAWNED))
327 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
329 monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
330 monsters_killed = inv_numkilled;
333 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
335 // no regeneration in invasion
339 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
341 entity player = M_ARGV(0, entity);
343 if(player.bot_attack)
344 IL_REMOVE(g_bot_targets, player);
345 player.bot_attack = false;
348 MUTATOR_HOOKFUNCTION(inv, PlayerDamage_Calculate)
350 entity frag_attacker = M_ARGV(1, entity);
351 entity frag_target = M_ARGV(2, entity);
352 float frag_damage = M_ARGV(4, float);
353 vector frag_force = M_ARGV(6, vector);
355 if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
358 frag_force = '0 0 0';
360 M_ARGV(4, float) = frag_damage;
361 M_ARGV(6, vector) = frag_force;
365 MUTATOR_HOOKFUNCTION(inv, SV_ParseClientCommand)
367 if(MUTATOR_RETURNVALUE) // command was already handled?
370 entity player = M_ARGV(0, entity);
371 string cmd_name = M_ARGV(1, string);
373 if(cmd_name == "debuginvasion")
375 sprint(player, strcat("inv_maxspawned = ", ftos(inv_maxspawned), "\n"));
376 sprint(player, strcat("inv_numspawned = ", ftos(inv_numspawned), "\n"));
377 sprint(player, strcat("inv_numkilled = ", ftos(inv_numkilled), "\n"));
378 sprint(player, strcat("inv_roundcnt = ", ftos(inv_roundcnt), "\n"));
379 sprint(player, strcat("monsters_total = ", ftos(monsters_total), "\n"));
380 sprint(player, strcat("monsters_killed = ", ftos(monsters_killed), "\n"));
381 sprint(player, strcat("inv_monsterskill = ", ftos(inv_monsterskill), "\n"));
387 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
389 entity targ = M_ARGV(1, entity);
391 if(!IS_MONSTER(targ))
395 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
398 start_armorvalue = 200;
401 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
403 entity frag_target = M_ARGV(1, entity);
405 if(IS_MONSTER(frag_target))
406 return MUT_ACCADD_INVALID;
407 return MUT_ACCADD_INDIFFERENT;
410 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
412 // monster spawning disabled during an invasion
413 M_ARGV(1, string) = "You cannot spawn monsters during an invasion!";
417 MUTATOR_HOOKFUNCTION(inv, CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
419 M_ARGV(0, float) = invasion_teams;
422 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
424 M_ARGV(0, string) = "This command does not work during an invasion!";
428 void invasion_ScoreRules(int inv_teams)
430 if(inv_teams) { CheckAllowedTeams(NULL); }
431 ScoreRules_basics(inv_teams, 0, 0, false);
432 if(inv_teams) ScoreInfo_SetLabel_TeamScore(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
433 ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
434 ScoreRules_basics_end();
437 void invasion_DelayedInit(entity this) // Do this check with a delay so we can wait for teams to be set up.
439 if(autocvar_g_invasion_teams)
441 invasion_teams = bound(2, autocvar_g_invasion_teams, 4);
443 if(invasion_teams >= 1) teams |= BIT(0);
444 if(invasion_teams >= 2) teams |= BIT(1);
445 if(invasion_teams >= 3) teams |= BIT(2);
446 if(invasion_teams >= 4) teams |= BIT(3);
448 invasion_teams = teams; // now set it?
453 independent_players = 1; // to disable extra useless scores
455 invasion_ScoreRules(invasion_teams);
457 independent_players = 0;
459 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
460 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
463 inv_maxrounds = 15; // 15?
466 void invasion_Initialize()
468 if(autocvar_g_invasion_zombies_only) {
469 Monster mon = MON_ZOMBIE;
470 mon.mr_precache(mon);
475 for(i = MON_FIRST; i <= MON_LAST; ++i)
477 mon = get_monsterinfo(i);
478 if((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM))
479 continue; // flying/swimming monsters not yet supported
481 mon.mr_precache(mon);
485 InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);