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 "../../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;
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()
196 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
198 FOR_EACH_MONSTER(head)
199 Monster_Remove(head);
201 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_OVER);
202 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_OVER);
203 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
207 float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
209 FOR_EACH_MONSTER(head) if(head.health > 0)
211 if((get_monsterinfo(head.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
212 ++supermonster_count;
213 ++total_alive_monsters;
218 case NUM_TEAM_1: ++red_alive; break;
219 case NUM_TEAM_2: ++blue_alive; break;
220 case NUM_TEAM_3: ++yellow_alive; break;
221 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; }
265 FOR_EACH_PLAYER(head)
267 float cs = PlayerScore_Add(head, SP_KILLS, 0);
268 if(cs > winning_score)
275 FOR_EACH_MONSTER(head)
276 Monster_Remove(head);
282 Send_Notification(NOTIF_ALL, world, MSG_CENTER, APP_TEAM_NUM_4(winner_team, CENTER_ROUND_TEAM_WIN_));
283 Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_NUM_4(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 float Invasion_CheckPlayers()
302 void Invasion_RoundStart()
305 float numplayers = 0;
308 e.player_blocked = 0;
312 if(inv_roundcnt < inv_maxrounds)
313 inv_roundcnt += 1; // a limiter to stop crazy counts
315 inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
321 inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
325 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
326 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
327 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
328 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
329 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
333 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
335 if(!(self.spawnflags & MONSTERFLAG_RESPAWNED))
339 if(teamplay) { inv_monsters_perteam[self.team] -= 1; }
341 if(IS_PLAYER(frag_attacker))
342 if(SAME_TEAM(frag_attacker, self)) // in non-teamplay modes, same team = same player, so this works
343 PlayerScore_Add(frag_attacker, SP_KILLS, -1);
346 PlayerScore_Add(frag_attacker, SP_KILLS, +1);
348 TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
355 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
357 if(!(self.spawnflags & MONSTERFLAG_SPAWNED))
360 if(!(self.spawnflags & MONSTERFLAG_RESPAWNED))
366 self.monster_skill = inv_monsterskill;
368 if((get_monsterinfo(self.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
369 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, self.monster_name);
371 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
376 MUTATOR_HOOKFUNCTION(inv, OnEntityPreSpawn)
378 if(startsWith(self.classname, "monster_"))
379 if(!(self.spawnflags & MONSTERFLAG_SPAWNED))
385 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
387 monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
388 monsters_killed = inv_numkilled;
393 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
395 // no regeneration in invasion
399 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
401 self.bot_attack = false;
405 MUTATOR_HOOKFUNCTION(inv, PlayerDamage_Calculate)
407 if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
410 frag_force = '0 0 0';
416 MUTATOR_HOOKFUNCTION(inv, SV_ParseClientCommand)
418 if(MUTATOR_RETURNVALUE) // command was already handled?
421 if(cmd_name == "debuginvasion")
423 sprint(self, strcat("inv_maxspawned = ", ftos(inv_maxspawned), "\n"));
424 sprint(self, strcat("inv_numspawned = ", ftos(inv_numspawned), "\n"));
425 sprint(self, strcat("inv_numkilled = ", ftos(inv_numkilled), "\n"));
426 sprint(self, strcat("inv_roundcnt = ", ftos(inv_roundcnt), "\n"));
427 sprint(self, strcat("monsters_total = ", ftos(monsters_total), "\n"));
428 sprint(self, strcat("monsters_killed = ", ftos(monsters_killed), "\n"));
429 sprint(self, strcat("inv_monsterskill = ", ftos(inv_monsterskill), "\n"));
437 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
439 if(!IS_MONSTER(checkentity))
445 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
448 start_armorvalue = 200;
452 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
454 if(IS_MONSTER(frag_target))
455 return MUT_ACCADD_INVALID;
456 return MUT_ACCADD_INDIFFERENT;
459 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
461 // monster spawning disabled during an invasion
465 MUTATOR_HOOKFUNCTION(inv, GetTeamCount, CBC_ORDER_EXCLUSIVE)
467 ret_float = invasion_teams;
471 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
473 ret_string = "This command does not work during an invasion!";
477 void invasion_ScoreRules(float inv_teams)
479 if(inv_teams) { CheckAllowedTeams(world); }
480 ScoreRules_basics(inv_teams, 0, 0, false);
481 if(inv_teams) ScoreInfo_SetLabel_TeamScore(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
482 ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
483 ScoreRules_basics_end();
486 void invasion_DelayedInit() // Do this check with a delay so we can wait for teams to be set up.
488 if(autocvar_g_invasion_teams)
489 invasion_teams = bound(2, autocvar_g_invasion_teams, 4);
493 independent_players = 1; // to disable extra useless scores
495 invasion_ScoreRules(invasion_teams);
497 independent_players = 0;
499 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
500 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
503 inv_maxrounds = 15; // 15?
506 void invasion_Initialize()
508 if(autocvar_g_invasion_zombies_only) {
509 Monster mon = MON_ZOMBIE;
510 mon.mr_precache(mon);
515 for(i = MON_FIRST; i <= MON_LAST; ++i)
517 mon = get_monsterinfo(i);
518 if((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM))
519 continue; // flying/swimming monsters not yet supported
521 mon.mr_precache(mon);
525 InitializeEntity(world, invasion_DelayedInit, INITPRIO_GAMETYPE);