1 #ifndef GAMEMODE_INVASION_H
2 #define GAMEMODE_INVASION_H
4 #define autocvar_g_invasion_point_limit cvar("g_invasion_point_limit")
5 int autocvar_g_invasion_teams;
6 bool autocvar_g_invasion_team_spawns;
8 void invasion_Initialize();
10 REGISTER_MUTATOR(inv, false)
14 if (time > 1) // game loads at time 1
15 error("This is a game type and it cannot be added at runtime.");
17 invasion_Initialize();
19 cvar_settemp("g_monsters", "1");
21 SetLimits(autocvar_g_invasion_point_limit, -1, -1, -1);
22 if (autocvar_g_invasion_teams >= 2)
25 if (autocvar_g_invasion_team_spawns)
26 have_team_spawns = -1; // request team spawns
30 MUTATOR_ONROLLBACK_OR_REMOVE
32 // we actually cannot roll back invasion_Initialize here
33 // BUT: we don't need to! If this gets called, adding always
39 LOG_INFO("This is a game type and it cannot be removed at runtime.");
55 float inv_monsters_perteam[17];
57 float inv_monsterskill;
59 const float ST_INV_KILLS = 1;
64 #include <common/monsters/spawn.qh>
65 #include <common/monsters/sv_monsters.qh>
67 #include <server/teamplay.qh>
70 float autocvar_g_invasion_round_timelimit;
71 float autocvar_g_invasion_spawnpoint_spawn_delay;
72 float autocvar_g_invasion_warmup;
73 int autocvar_g_invasion_monster_count;
74 bool autocvar_g_invasion_zombies_only;
75 float autocvar_g_invasion_spawn_delay;
77 spawnfunc(invasion_spawnpoint)
79 if(!g_invasion) { remove(self); return; }
81 self.classname = "invasion_spawnpoint";
83 if(autocvar_g_invasion_zombies_only) // precache only if it hasn't been already
85 Monster mon = get_monsterinfo(self.monsterid);
90 float invasion_PickMonster(float supermonster_count)
92 if(autocvar_g_invasion_zombies_only)
93 return MON_ZOMBIE.monsterid;
98 RandomSelection_Init();
100 for(i = MON_FIRST; i <= MON_LAST; ++i)
102 mon = get_monsterinfo(i);
103 if((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM) || ((mon.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
104 continue; // flying/swimming monsters not yet supported
106 RandomSelection_Add(world, i, string_null, 1, 1);
109 return RandomSelection_chosen_float;
112 entity invasion_PickSpawn()
116 RandomSelection_Init();
118 for(e = world;(e = find(e, classname, "invasion_spawnpoint")); )
120 RandomSelection_Add(e, 0, string_null, 1, ((time >= e.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
121 e.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
124 return RandomSelection_chosen_ent;
127 void invasion_SpawnChosenMonster(float mon)
129 entity spawn_point, monster;
131 spawn_point = invasion_PickSpawn();
133 if(spawn_point == world)
135 LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations\n");
137 setsize(e, (get_monsterinfo(mon)).mins, (get_monsterinfo(mon)).maxs);
139 if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
140 monster = spawnmonster("", mon, world, world, e.origin, false, false, 2);
143 e.think = SUB_Remove_self;
144 e.nextthink = time + 0.1;
147 monster = spawnmonster("", ((spawn_point.monsterid) ? spawn_point.monsterid : mon), spawn_point, spawn_point, spawn_point.origin, false, false, 2);
149 if(spawn_point) monster.target2 = spawn_point.target2;
150 monster.spawnshieldtime = time;
151 if(spawn_point && spawn_point.target_range) monster.target_range = spawn_point.target_range;
154 if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
155 monster.team = spawn_point.team;
158 RandomSelection_Init();
159 if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_Add(world, NUM_TEAM_1, string_null, 1, 1);
160 if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_Add(world, NUM_TEAM_2, string_null, 1, 1);
161 if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_Add(world, NUM_TEAM_3, string_null, 1, 1); }
162 if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_Add(world, NUM_TEAM_4, string_null, 1, 1); }
164 monster.team = RandomSelection_chosen_float;
169 monster_setupcolors(monster);
173 WaypointSprite_UpdateTeamRadar(monster.sprite, RADARICON_DANGER, ((monster.team) ? Team_ColorRGB(monster.team) : '1 0 0'));
175 monster.sprite.team = 0;
176 monster.sprite.SendFlags |= 1;
180 monster.monster_attack = false; // it's the player's job to kill all the monsters
182 if(inv_roundcnt >= inv_maxrounds)
183 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
186 void invasion_SpawnMonsters(float supermonster_count)
188 float chosen_monster = invasion_PickMonster(supermonster_count);
190 invasion_SpawnChosenMonster(chosen_monster);
193 float Invasion_CheckWinner()
195 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
197 FOREACH_ENTITY_FLAGS(flags, FL_MONSTER, LAMBDA(Monster_Remove(it)));
199 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_OVER);
200 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_OVER);
201 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
205 float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
207 FOREACH_ENTITY_FLAGS(flags, FL_MONSTER, LAMBDA(
210 if((get_monsterinfo(it.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
211 ++supermonster_count;
212 ++total_alive_monsters;
217 case NUM_TEAM_1: ++red_alive; break;
218 case NUM_TEAM_2: ++blue_alive; break;
219 case NUM_TEAM_3: ++yellow_alive; break;
220 case NUM_TEAM_4: ++pink_alive; break;
225 if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
227 if(time >= inv_lastcheck)
229 invasion_SpawnMonsters(supermonster_count);
230 inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
236 if(inv_numspawned < 1)
237 return 0; // nothing has spawned yet
241 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
244 else if(inv_numkilled < inv_maxspawned)
247 entity winner = world;
248 float winning_score = 0, winner_team = 0;
253 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
255 if(winner_team) { winner_team = 0; }
256 else { winner_team = NUM_TEAM_2; }
258 if(winner_team) { winner_team = 0; }
259 else { winner_team = NUM_TEAM_3; }
261 if(winner_team) { winner_team = 0; }
262 else { winner_team = NUM_TEAM_4; }
266 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
267 float cs = PlayerScore_Add(it, SP_KILLS, 0);
268 if(cs > winning_score)
276 FOREACH_ENTITY_FLAGS(flags, FL_MONSTER, LAMBDA(Monster_Remove(it)));
282 Send_Notification(NOTIF_ALL, world, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
283 Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
288 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
289 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
292 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
297 bool Invasion_CheckPlayers()
302 void Invasion_RoundStart()
305 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
306 it.player_blocked = false;
310 if(inv_roundcnt < inv_maxrounds)
311 inv_roundcnt += 1; // a limiter to stop crazy counts
313 inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
319 inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
323 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
324 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
325 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
326 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
327 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
331 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
333 if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
337 if(teamplay) { inv_monsters_perteam[frag_target.team] -= 1; }
339 if(IS_PLAYER(frag_attacker))
340 if(SAME_TEAM(frag_attacker, frag_target)) // in non-teamplay modes, same team = same player, so this works
341 PlayerScore_Add(frag_attacker, SP_KILLS, -1);
344 PlayerScore_Add(frag_attacker, SP_KILLS, +1);
346 TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
353 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
355 if(!(self.spawnflags & MONSTERFLAG_SPAWNED))
358 if(!(self.spawnflags & MONSTERFLAG_RESPAWNED))
364 self.monster_skill = inv_monsterskill;
366 if((get_monsterinfo(self.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
367 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, self.monster_name);
369 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
374 MUTATOR_HOOKFUNCTION(inv, OnEntityPreSpawn)
376 if(startsWith(self.classname, "monster_"))
377 if(!(self.spawnflags & MONSTERFLAG_SPAWNED))
383 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
385 monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
386 monsters_killed = inv_numkilled;
391 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
393 // no regeneration in invasion
397 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
399 self.bot_attack = false;
403 MUTATOR_HOOKFUNCTION(inv, PlayerDamage_Calculate)
405 if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
408 frag_force = '0 0 0';
414 MUTATOR_HOOKFUNCTION(inv, SV_ParseClientCommand)
416 if(MUTATOR_RETURNVALUE) // command was already handled?
419 if(cmd_name == "debuginvasion")
421 sprint(self, strcat("inv_maxspawned = ", ftos(inv_maxspawned), "\n"));
422 sprint(self, strcat("inv_numspawned = ", ftos(inv_numspawned), "\n"));
423 sprint(self, strcat("inv_numkilled = ", ftos(inv_numkilled), "\n"));
424 sprint(self, strcat("inv_roundcnt = ", ftos(inv_roundcnt), "\n"));
425 sprint(self, strcat("monsters_total = ", ftos(monsters_total), "\n"));
426 sprint(self, strcat("monsters_killed = ", ftos(monsters_killed), "\n"));
427 sprint(self, strcat("inv_monsterskill = ", ftos(inv_monsterskill), "\n"));
435 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
437 if(!IS_MONSTER(checkentity))
443 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
446 start_armorvalue = 200;
450 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
452 if(IS_MONSTER(frag_target))
453 return MUT_ACCADD_INVALID;
454 return MUT_ACCADD_INDIFFERENT;
457 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
459 // monster spawning disabled during an invasion
463 MUTATOR_HOOKFUNCTION(inv, GetTeamCount, CBC_ORDER_EXCLUSIVE)
465 ret_float = invasion_teams;
469 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
471 ret_string = "This command does not work during an invasion!";
475 void invasion_ScoreRules(float inv_teams)
477 if(inv_teams) { CheckAllowedTeams(world); }
478 ScoreRules_basics(inv_teams, 0, 0, false);
479 if(inv_teams) ScoreInfo_SetLabel_TeamScore(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
480 ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
481 ScoreRules_basics_end();
484 void invasion_DelayedInit() // Do this check with a delay so we can wait for teams to be set up.
486 if(autocvar_g_invasion_teams)
487 invasion_teams = bound(2, autocvar_g_invasion_teams, 4);
491 independent_players = 1; // to disable extra useless scores
493 invasion_ScoreRules(invasion_teams);
495 independent_players = 0;
497 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
498 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
501 inv_maxrounds = 15; // 15?
504 void invasion_Initialize()
506 if(autocvar_g_invasion_zombies_only) {
507 Monster mon = MON_ZOMBIE;
508 mon.mr_precache(mon);
513 for(i = MON_FIRST; i <= MON_LAST; ++i)
515 mon = get_monsterinfo(i);
516 if((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM))
517 continue; // flying/swimming monsters not yet supported
519 mon.mr_precache(mon);
523 InitializeEntity(world, invasion_DelayedInit, INITPRIO_GAMETYPE);