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(spawn(), "", mon.m_id, NULL, NULL, e.origin, false, false, 2);
83 setthink(e, SUB_Remove);
84 e.nextthink = time + 0.1;
86 else // if spawnmob field falls through (unset), fallback to mon (relying on spawnmonster for that behaviour)
87 monster = spawnmonster(spawn(), spawn_point.spawnmob, mon.m_id, spawn_point, spawn_point, spawn_point.origin, false, false, 2);
89 if(spawn_point) monster.target2 = spawn_point.target2;
90 monster.spawnshieldtime = time;
91 if(spawn_point && spawn_point.target_range) monster.target_range = spawn_point.target_range;
94 if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
95 monster.team = spawn_point.team;
98 RandomSelection_Init();
99 if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_AddFloat(NUM_TEAM_1, 1, 1);
100 if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_AddFloat(NUM_TEAM_2, 1, 1);
101 if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_AddFloat(NUM_TEAM_3, 1, 1); }
102 if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_AddFloat(NUM_TEAM_4, 1, 1); }
104 monster.team = RandomSelection_chosen_float;
109 monster_setupcolors(monster);
113 WaypointSprite_UpdateTeamRadar(monster.sprite, RADARICON_DANGER, ((monster.team) ? Team_ColorRGB(monster.team) : '1 0 0'));
115 monster.sprite.team = 0;
116 monster.sprite.SendFlags |= 1;
120 monster.monster_attack = false; // it's the player's job to kill all the monsters
122 if(inv_roundcnt >= inv_maxrounds)
123 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
126 void invasion_SpawnMonsters(int supermonster_count)
128 Monster chosen_monster = invasion_PickMonster(supermonster_count);
130 invasion_SpawnChosenMonster(chosen_monster);
133 bool Invasion_CheckWinner()
135 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
137 IL_EACH(g_monsters, true,
141 IL_CLEAR(g_monsters);
143 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
144 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
145 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
149 float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
151 IL_EACH(g_monsters, it.health > 0,
153 if((get_monsterinfo(it.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
154 ++supermonster_count;
155 ++total_alive_monsters;
160 case NUM_TEAM_1: ++red_alive; break;
161 case NUM_TEAM_2: ++blue_alive; break;
162 case NUM_TEAM_3: ++yellow_alive; break;
163 case NUM_TEAM_4: ++pink_alive; break;
167 if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
169 if(time >= inv_lastcheck)
171 invasion_SpawnMonsters(supermonster_count);
172 inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
178 if(inv_numspawned < 1)
179 return 0; // nothing has spawned yet
183 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
186 else if(inv_numkilled < inv_maxspawned)
189 entity winner = NULL;
190 float winning_score = 0, winner_team = 0;
195 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
197 if(winner_team) { winner_team = 0; }
198 else { winner_team = NUM_TEAM_2; }
200 if(winner_team) { winner_team = 0; }
201 else { winner_team = NUM_TEAM_3; }
203 if(winner_team) { winner_team = 0; }
204 else { winner_team = NUM_TEAM_4; }
208 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
209 float cs = PlayerScore_Add(it, SP_KILLS, 0);
210 if(cs > winning_score)
218 IL_EACH(g_monsters, true,
222 IL_CLEAR(g_monsters);
228 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
229 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
234 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
235 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
238 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
243 bool Invasion_CheckPlayers()
248 void Invasion_RoundStart()
251 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
252 it.player_blocked = false;
256 if(inv_roundcnt < inv_maxrounds)
257 inv_roundcnt += 1; // a limiter to stop crazy counts
259 inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
265 inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
269 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
270 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
271 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
272 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
273 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
277 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
279 entity frag_target = M_ARGV(0, entity);
280 entity frag_attacker = M_ARGV(1, entity);
282 if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
286 if(teamplay) { inv_monsters_perteam[frag_target.team] -= 1; }
288 if(IS_PLAYER(frag_attacker))
289 if(SAME_TEAM(frag_attacker, frag_target)) // in non-teamplay modes, same team = same player, so this works
290 PlayerScore_Add(frag_attacker, SP_KILLS, -1);
293 PlayerScore_Add(frag_attacker, SP_KILLS, +1);
295 TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
300 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
302 entity mon = M_ARGV(0, entity);
304 if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
307 if(!(mon.spawnflags & MONSTERFLAG_RESPAWNED))
313 mon.monster_skill = inv_monsterskill;
315 if((get_monsterinfo(mon.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
316 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
318 mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
321 MUTATOR_HOOKFUNCTION(inv, OnEntityPreSpawn)
323 entity ent = M_ARGV(0, entity);
325 if(startsWith(ent.classname, "monster_"))
326 if(!(ent.spawnflags & MONSTERFLAG_SPAWNED))
330 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
332 monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
333 monsters_killed = inv_numkilled;
336 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
338 // no regeneration in invasion
342 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
344 entity player = M_ARGV(0, entity);
346 if(player.bot_attack)
347 IL_REMOVE(g_bot_targets, player);
348 player.bot_attack = false;
351 MUTATOR_HOOKFUNCTION(inv, PlayerDamage_Calculate)
353 entity frag_attacker = M_ARGV(1, entity);
354 entity frag_target = M_ARGV(2, entity);
355 float frag_damage = M_ARGV(4, float);
356 vector frag_force = M_ARGV(6, vector);
358 if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
361 frag_force = '0 0 0';
363 M_ARGV(4, float) = frag_damage;
364 M_ARGV(6, vector) = frag_force;
368 MUTATOR_HOOKFUNCTION(inv, SV_ParseClientCommand)
370 if(MUTATOR_RETURNVALUE) // command was already handled?
373 entity player = M_ARGV(0, entity);
374 string cmd_name = M_ARGV(1, string);
376 if(cmd_name == "debuginvasion")
378 sprint(player, strcat("inv_maxspawned = ", ftos(inv_maxspawned), "\n"));
379 sprint(player, strcat("inv_numspawned = ", ftos(inv_numspawned), "\n"));
380 sprint(player, strcat("inv_numkilled = ", ftos(inv_numkilled), "\n"));
381 sprint(player, strcat("inv_roundcnt = ", ftos(inv_roundcnt), "\n"));
382 sprint(player, strcat("monsters_total = ", ftos(monsters_total), "\n"));
383 sprint(player, strcat("monsters_killed = ", ftos(monsters_killed), "\n"));
384 sprint(player, strcat("inv_monsterskill = ", ftos(inv_monsterskill), "\n"));
390 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
392 entity targ = M_ARGV(1, entity);
394 if(!IS_MONSTER(targ))
398 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
401 start_armorvalue = 200;
404 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
406 entity frag_target = M_ARGV(1, entity);
408 if(IS_MONSTER(frag_target))
409 return MUT_ACCADD_INVALID;
410 return MUT_ACCADD_INDIFFERENT;
413 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
415 // monster spawning disabled during an invasion
416 M_ARGV(1, string) = "You cannot spawn monsters during an invasion!";
420 MUTATOR_HOOKFUNCTION(inv, CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
422 M_ARGV(0, float) = invasion_teams;
425 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
427 M_ARGV(0, string) = "This command does not work during an invasion!";
431 void invasion_ScoreRules(int inv_teams)
433 if(inv_teams) { CheckAllowedTeams(NULL); }
434 ScoreRules_basics(inv_teams, 0, 0, false);
435 if(inv_teams) ScoreInfo_SetLabel_TeamScore(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
436 ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
437 ScoreRules_basics_end();
440 void invasion_DelayedInit(entity this) // Do this check with a delay so we can wait for teams to be set up.
442 if(autocvar_g_invasion_teams)
444 invasion_teams = bound(2, autocvar_g_invasion_teams, 4);
446 if(invasion_teams >= 1) teams |= BIT(0);
447 if(invasion_teams >= 2) teams |= BIT(1);
448 if(invasion_teams >= 3) teams |= BIT(2);
449 if(invasion_teams >= 4) teams |= BIT(3);
451 invasion_teams = teams; // now set it?
456 independent_players = 1; // to disable extra useless scores
458 invasion_ScoreRules(invasion_teams);
460 independent_players = 0;
462 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
463 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
466 inv_maxrounds = 15; // 15?
469 void invasion_Initialize()
471 if(autocvar_g_invasion_zombies_only) {
472 Monster mon = MON_ZOMBIE;
473 mon.mr_precache(mon);
478 for(i = MON_FIRST; i <= MON_LAST; ++i)
480 mon = get_monsterinfo(i);
481 if((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM))
482 continue; // flying/swimming monsters not yet supported
484 mon.mr_precache(mon);
488 InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);