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;
18 spawnfunc(invasion_spawnpoint)
20 if(!g_invasion) { delete(this); return; }
22 this.classname = "invasion_spawnpoint";
23 IL_PUSH(g_invasion_spawns, this);
25 if(autocvar_g_invasion_zombies_only) // precache only if it hasn't been already
27 Monster mon = get_monsterinfo(this.monsterid);
32 int invasion_PickMonster(int supermonster_count)
34 if(autocvar_g_invasion_zombies_only)
35 return MON_ZOMBIE.monsterid;
37 RandomSelection_Init();
39 FOREACH(Monsters, it != MON_Null,
41 if((it.spawnflags & MONSTER_TYPE_FLY) || (it.spawnflags & MONSTER_TYPE_SWIM) || (it.spawnflags & MONSTER_SIZE_QUAKE) || ((it.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
43 RandomSelection_Add(NULL, it.monsterid, string_null, 1, 1);
46 return RandomSelection_chosen_float;
49 entity invasion_PickSpawn()
51 RandomSelection_Init();
53 IL_EACH(g_invasion_spawns, true,
55 RandomSelection_Add(it, 0, string_null, 1, ((time >= it.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
56 it.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
59 return RandomSelection_chosen_ent;
62 void invasion_SpawnChosenMonster(float mon)
64 entity spawn_point, monster;
66 spawn_point = invasion_PickSpawn();
68 if(spawn_point == NULL)
70 LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations");
72 setsize(e, (get_monsterinfo(mon)).mins, (get_monsterinfo(mon)).maxs);
74 if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
75 monster = spawnmonster("", mon, NULL, NULL, e.origin, false, false, 2);
78 setthink(e, SUB_Remove);
79 e.nextthink = time + 0.1;
82 monster = spawnmonster("", ((spawn_point.monsterid) ? spawn_point.monsterid : mon), spawn_point, spawn_point, spawn_point.origin, false, false, 2);
84 if(spawn_point) monster.target2 = spawn_point.target2;
85 monster.spawnshieldtime = time;
86 if(spawn_point && spawn_point.target_range) monster.target_range = spawn_point.target_range;
89 if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
90 monster.team = spawn_point.team;
93 RandomSelection_Init();
94 if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_Add(NULL, NUM_TEAM_1, string_null, 1, 1);
95 if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_Add(NULL, NUM_TEAM_2, string_null, 1, 1);
96 if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_Add(NULL, NUM_TEAM_3, string_null, 1, 1); }
97 if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_Add(NULL, NUM_TEAM_4, string_null, 1, 1); }
99 monster.team = RandomSelection_chosen_float;
104 monster_setupcolors(monster);
108 WaypointSprite_UpdateTeamRadar(monster.sprite, RADARICON_DANGER, ((monster.team) ? Team_ColorRGB(monster.team) : '1 0 0'));
110 monster.sprite.team = 0;
111 monster.sprite.SendFlags |= 1;
115 monster.monster_attack = false; // it's the player's job to kill all the monsters
117 if(inv_roundcnt >= inv_maxrounds)
118 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
121 void invasion_SpawnMonsters(int supermonster_count)
123 int chosen_monster = invasion_PickMonster(supermonster_count);
125 invasion_SpawnChosenMonster(chosen_monster);
128 bool Invasion_CheckWinner()
130 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
132 IL_EACH(g_monsters, true,
136 IL_CLEAR(g_monsters);
138 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
139 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
140 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
144 float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
146 IL_EACH(g_monsters, it.health > 0,
148 if((get_monsterinfo(it.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
149 ++supermonster_count;
150 ++total_alive_monsters;
155 case NUM_TEAM_1: ++red_alive; break;
156 case NUM_TEAM_2: ++blue_alive; break;
157 case NUM_TEAM_3: ++yellow_alive; break;
158 case NUM_TEAM_4: ++pink_alive; break;
162 if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
164 if(time >= inv_lastcheck)
166 invasion_SpawnMonsters(supermonster_count);
167 inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
173 if(inv_numspawned < 1)
174 return 0; // nothing has spawned yet
178 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
181 else if(inv_numkilled < inv_maxspawned)
184 entity winner = NULL;
185 float winning_score = 0, winner_team = 0;
190 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
192 if(winner_team) { winner_team = 0; }
193 else { winner_team = NUM_TEAM_2; }
195 if(winner_team) { winner_team = 0; }
196 else { winner_team = NUM_TEAM_3; }
198 if(winner_team) { winner_team = 0; }
199 else { winner_team = NUM_TEAM_4; }
203 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
204 float cs = PlayerScore_Add(it, SP_KILLS, 0);
205 if(cs > winning_score)
213 IL_EACH(g_monsters, true,
217 IL_CLEAR(g_monsters);
223 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
224 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
229 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
230 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
233 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
238 bool Invasion_CheckPlayers()
243 void Invasion_RoundStart()
246 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
247 it.player_blocked = false;
251 if(inv_roundcnt < inv_maxrounds)
252 inv_roundcnt += 1; // a limiter to stop crazy counts
254 inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
260 inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
264 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
265 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
266 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
267 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
268 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
272 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
274 entity frag_target = M_ARGV(0, entity);
275 entity frag_attacker = M_ARGV(1, entity);
277 if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
281 if(teamplay) { inv_monsters_perteam[frag_target.team] -= 1; }
283 if(IS_PLAYER(frag_attacker))
284 if(SAME_TEAM(frag_attacker, frag_target)) // in non-teamplay modes, same team = same player, so this works
285 PlayerScore_Add(frag_attacker, SP_KILLS, -1);
288 PlayerScore_Add(frag_attacker, SP_KILLS, +1);
290 TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
295 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
297 entity mon = M_ARGV(0, entity);
299 if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
302 if(!(mon.spawnflags & MONSTERFLAG_RESPAWNED))
308 mon.monster_skill = inv_monsterskill;
310 if((get_monsterinfo(mon.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
311 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
313 mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
316 MUTATOR_HOOKFUNCTION(inv, OnEntityPreSpawn)
318 entity ent = M_ARGV(0, entity);
320 if(startsWith(ent.classname, "monster_"))
321 if(!(ent.spawnflags & MONSTERFLAG_SPAWNED))
325 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
327 monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
328 monsters_killed = inv_numkilled;
331 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
333 // no regeneration in invasion
337 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
339 entity player = M_ARGV(0, entity);
341 if(player.bot_attack)
342 IL_REMOVE(g_bot_targets, player);
343 player.bot_attack = false;
346 MUTATOR_HOOKFUNCTION(inv, PlayerDamage_Calculate)
348 entity frag_attacker = M_ARGV(1, entity);
349 entity frag_target = M_ARGV(2, entity);
350 float frag_damage = M_ARGV(4, float);
351 vector frag_force = M_ARGV(6, vector);
353 if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
356 frag_force = '0 0 0';
358 M_ARGV(4, float) = frag_damage;
359 M_ARGV(6, vector) = frag_force;
363 MUTATOR_HOOKFUNCTION(inv, SV_ParseClientCommand)
365 if(MUTATOR_RETURNVALUE) // command was already handled?
368 entity player = M_ARGV(0, entity);
369 string cmd_name = M_ARGV(1, string);
371 if(cmd_name == "debuginvasion")
373 sprint(player, strcat("inv_maxspawned = ", ftos(inv_maxspawned), "\n"));
374 sprint(player, strcat("inv_numspawned = ", ftos(inv_numspawned), "\n"));
375 sprint(player, strcat("inv_numkilled = ", ftos(inv_numkilled), "\n"));
376 sprint(player, strcat("inv_roundcnt = ", ftos(inv_roundcnt), "\n"));
377 sprint(player, strcat("monsters_total = ", ftos(monsters_total), "\n"));
378 sprint(player, strcat("monsters_killed = ", ftos(monsters_killed), "\n"));
379 sprint(player, strcat("inv_monsterskill = ", ftos(inv_monsterskill), "\n"));
385 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
387 entity targ = M_ARGV(1, entity);
389 if(!IS_MONSTER(targ))
393 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
396 start_armorvalue = 200;
399 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
401 entity frag_target = M_ARGV(1, entity);
403 if(IS_MONSTER(frag_target))
404 return MUT_ACCADD_INVALID;
405 return MUT_ACCADD_INDIFFERENT;
408 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
410 // monster spawning disabled during an invasion
411 M_ARGV(1, string) = "You cannot spawn monsters during an invasion!";
415 MUTATOR_HOOKFUNCTION(inv, CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
417 M_ARGV(0, float) = invasion_teams;
420 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
422 M_ARGV(0, string) = "This command does not work during an invasion!";
426 void invasion_ScoreRules(int inv_teams)
428 if(inv_teams) { CheckAllowedTeams(NULL); }
429 ScoreRules_basics(inv_teams, 0, 0, false);
430 if(inv_teams) ScoreInfo_SetLabel_TeamScore(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
431 ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
432 ScoreRules_basics_end();
435 void invasion_DelayedInit(entity this) // Do this check with a delay so we can wait for teams to be set up.
437 if(autocvar_g_invasion_teams)
439 invasion_teams = bound(2, autocvar_g_invasion_teams, 4);
441 if(invasion_teams >= 1) teams |= BIT(0);
442 if(invasion_teams >= 2) teams |= BIT(1);
443 if(invasion_teams >= 3) teams |= BIT(2);
444 if(invasion_teams >= 4) teams |= BIT(3);
446 invasion_teams = teams; // now set it?
451 independent_players = 1; // to disable extra useless scores
453 invasion_ScoreRules(invasion_teams);
455 independent_players = 0;
457 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
458 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
461 inv_maxrounds = 15; // 15?
464 void invasion_Initialize()
466 if(autocvar_g_invasion_zombies_only) {
467 Monster mon = MON_ZOMBIE;
468 mon.mr_precache(mon);
473 for(i = MON_FIRST; i <= MON_LAST; ++i)
475 mon = get_monsterinfo(i);
476 if((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM))
477 continue; // flying/swimming monsters not yet supported
479 mon.mr_precache(mon);
483 InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);