1 #ifndef GAMEMODE_INVASION_H
2 #define GAMEMODE_INVASION_H
13 float inv_monsters_perteam[17];
15 float inv_monsterskill;
17 const float ST_INV_KILLS = 1;
22 #include "../../../common/monsters/spawn.qh"
23 #include "../../../common/monsters/sv_monsters.qh"
25 #include "../../teamplay.qh"
29 float autocvar_g_invasion_round_timelimit;
30 int autocvar_g_invasion_teams;
31 bool autocvar_g_invasion_team_spawns;
32 float autocvar_g_invasion_spawnpoint_spawn_delay;
33 #define autocvar_g_invasion_point_limit cvar("g_invasion_point_limit")
34 float autocvar_g_invasion_warmup;
35 int autocvar_g_invasion_monster_count;
36 bool autocvar_g_invasion_zombies_only;
37 float autocvar_g_invasion_spawn_delay;
39 spawnfunc(invasion_spawnpoint)
41 if(!g_invasion) { remove(self); return; }
43 self.classname = "invasion_spawnpoint";
45 if(autocvar_g_invasion_zombies_only) // precache only if it hasn't been already
47 Monster mon = get_monsterinfo(self.monsterid);
52 float invasion_PickMonster(float supermonster_count)
54 if(autocvar_g_invasion_zombies_only)
55 return MON_ZOMBIE.monsterid;
60 RandomSelection_Init();
62 for(i = MON_FIRST; i <= MON_LAST; ++i)
64 mon = get_monsterinfo(i);
65 if((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM) || ((mon.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
66 continue; // flying/swimming monsters not yet supported
68 RandomSelection_Add(world, i, string_null, 1, 1);
71 return RandomSelection_chosen_float;
74 entity invasion_PickSpawn()
78 RandomSelection_Init();
80 for(e = world;(e = find(e, classname, "invasion_spawnpoint")); )
82 RandomSelection_Add(e, 0, string_null, 1, ((time >= e.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
83 e.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
86 return RandomSelection_chosen_ent;
89 void invasion_SpawnChosenMonster(float mon)
91 entity spawn_point, monster;
93 spawn_point = invasion_PickSpawn();
95 if(spawn_point == world)
97 LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations\n");
99 setsize(e, (get_monsterinfo(mon)).mins, (get_monsterinfo(mon)).maxs);
101 if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
102 monster = spawnmonster("", mon, world, world, e.origin, false, false, 2);
105 e.think = SUB_Remove;
106 e.nextthink = time + 0.1;
109 monster = spawnmonster("", ((spawn_point.monsterid) ? spawn_point.monsterid : mon), spawn_point, spawn_point, spawn_point.origin, false, false, 2);
111 if(spawn_point) monster.target2 = spawn_point.target2;
112 monster.spawnshieldtime = time;
113 if(spawn_point && spawn_point.target_range) monster.target_range = spawn_point.target_range;
116 if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
117 monster.team = spawn_point.team;
120 RandomSelection_Init();
121 if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_Add(world, NUM_TEAM_1, string_null, 1, 1);
122 if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_Add(world, NUM_TEAM_2, string_null, 1, 1);
123 if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_Add(world, NUM_TEAM_3, string_null, 1, 1); }
124 if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_Add(world, NUM_TEAM_4, string_null, 1, 1); }
126 monster.team = RandomSelection_chosen_float;
131 monster_setupcolors(monster);
135 WaypointSprite_UpdateTeamRadar(monster.sprite, RADARICON_DANGER, ((monster.team) ? Team_ColorRGB(monster.team) : '1 0 0'));
137 monster.sprite.team = 0;
138 monster.sprite.SendFlags |= 1;
142 monster.monster_attack = false; // it's the player's job to kill all the monsters
144 if(inv_roundcnt >= inv_maxrounds)
145 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
148 void invasion_SpawnMonsters(float supermonster_count)
150 float chosen_monster = invasion_PickMonster(supermonster_count);
152 invasion_SpawnChosenMonster(chosen_monster);
155 float Invasion_CheckWinner()
158 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
160 FOR_EACH_MONSTER(head)
161 Monster_Remove(head);
163 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_OVER);
164 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_OVER);
165 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
169 float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
171 FOR_EACH_MONSTER(head) if(head.health > 0)
173 if((get_monsterinfo(head.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
174 ++supermonster_count;
175 ++total_alive_monsters;
180 case NUM_TEAM_1: ++red_alive; break;
181 case NUM_TEAM_2: ++blue_alive; break;
182 case NUM_TEAM_3: ++yellow_alive; break;
183 case NUM_TEAM_4: ++pink_alive; break;
187 if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
189 if(time >= inv_lastcheck)
191 invasion_SpawnMonsters(supermonster_count);
192 inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
198 if(inv_numspawned < 1)
199 return 0; // nothing has spawned yet
203 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
206 else if(inv_numkilled < inv_maxspawned)
209 entity winner = world;
210 float winning_score = 0, winner_team = 0;
215 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
217 if(winner_team) { winner_team = 0; }
218 else { winner_team = NUM_TEAM_2; }
220 if(winner_team) { winner_team = 0; }
221 else { winner_team = NUM_TEAM_3; }
223 if(winner_team) { winner_team = 0; }
224 else { winner_team = NUM_TEAM_4; }
227 FOR_EACH_PLAYER(head)
229 float cs = PlayerScore_Add(head, SP_KILLS, 0);
230 if(cs > winning_score)
237 FOR_EACH_MONSTER(head)
238 Monster_Remove(head);
244 Send_Notification(NOTIF_ALL, world, MSG_CENTER, APP_TEAM_NUM_4(winner_team, CENTER_ROUND_TEAM_WIN_));
245 Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_NUM_4(winner_team, INFO_ROUND_TEAM_WIN_));
250 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
251 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
254 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
259 float Invasion_CheckPlayers()
264 void Invasion_RoundStart()
267 float numplayers = 0;
270 e.player_blocked = 0;
274 if(inv_roundcnt < inv_maxrounds)
275 inv_roundcnt += 1; // a limiter to stop crazy counts
277 inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
283 inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
287 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
288 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
289 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
290 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
291 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
295 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
297 if(!(self.spawnflags & MONSTERFLAG_RESPAWNED))
301 if(teamplay) { inv_monsters_perteam[self.team] -= 1; }
303 if(IS_PLAYER(frag_attacker))
304 if(SAME_TEAM(frag_attacker, self)) // in non-teamplay modes, same team = same player, so this works
305 PlayerScore_Add(frag_attacker, SP_KILLS, -1);
308 PlayerScore_Add(frag_attacker, SP_KILLS, +1);
310 TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
317 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
319 if(!(self.spawnflags & MONSTERFLAG_SPAWNED))
322 if(!(self.spawnflags & MONSTERFLAG_RESPAWNED))
328 self.monster_skill = inv_monsterskill;
330 if((get_monsterinfo(self.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
331 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, self.monster_name);
333 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
338 MUTATOR_HOOKFUNCTION(inv, OnEntityPreSpawn)
340 if(startsWith(self.classname, "monster_"))
341 if(!(self.spawnflags & MONSTERFLAG_SPAWNED))
347 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
349 monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
350 monsters_killed = inv_numkilled;
355 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
357 // no regeneration in invasion
361 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
363 self.bot_attack = false;
367 MUTATOR_HOOKFUNCTION(inv, PlayerDamage_Calculate)
369 if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
372 frag_force = '0 0 0';
378 MUTATOR_HOOKFUNCTION(inv, SV_ParseClientCommand)
380 if(MUTATOR_RETURNVALUE) // command was already handled?
383 if(cmd_name == "debuginvasion")
385 sprint(self, strcat("inv_maxspawned = ", ftos(inv_maxspawned), "\n"));
386 sprint(self, strcat("inv_numspawned = ", ftos(inv_numspawned), "\n"));
387 sprint(self, strcat("inv_numkilled = ", ftos(inv_numkilled), "\n"));
388 sprint(self, strcat("inv_roundcnt = ", ftos(inv_roundcnt), "\n"));
389 sprint(self, strcat("monsters_total = ", ftos(monsters_total), "\n"));
390 sprint(self, strcat("monsters_killed = ", ftos(monsters_killed), "\n"));
391 sprint(self, strcat("inv_monsterskill = ", ftos(inv_monsterskill), "\n"));
399 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
401 if(!IS_MONSTER(checkentity))
407 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
410 start_armorvalue = 200;
414 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
416 if(IS_MONSTER(frag_target))
417 return MUT_ACCADD_INVALID;
418 return MUT_ACCADD_INDIFFERENT;
421 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
423 // monster spawning disabled during an invasion
427 MUTATOR_HOOKFUNCTION(inv, GetTeamCount, CBC_ORDER_EXCLUSIVE)
429 ret_float = invasion_teams;
433 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
435 ret_string = "This command does not work during an invasion!";
439 void invasion_ScoreRules(float inv_teams)
441 if(inv_teams) { CheckAllowedTeams(world); }
442 ScoreRules_basics(inv_teams, 0, 0, false);
443 if(inv_teams) ScoreInfo_SetLabel_TeamScore(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
444 ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
445 ScoreRules_basics_end();
448 void invasion_DelayedInit() // Do this check with a delay so we can wait for teams to be set up.
450 if(autocvar_g_invasion_teams)
451 invasion_teams = bound(2, autocvar_g_invasion_teams, 4);
455 independent_players = 1; // to disable extra useless scores
457 invasion_ScoreRules(invasion_teams);
459 independent_players = 0;
461 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
462 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
465 inv_maxrounds = 15; // 15?
468 void invasion_Initialize()
470 if(autocvar_g_invasion_zombies_only) {
471 Monster mon = MON_ZOMBIE;
472 mon.mr_precache(mon);
477 for(i = MON_FIRST; i <= MON_LAST; ++i)
479 mon = get_monsterinfo(i);
480 if((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM))
481 continue; // flying/swimming monsters not yet supported
483 mon.mr_precache(mon);
487 InitializeEntity(world, invasion_DelayedInit, INITPRIO_GAMETYPE);
490 REGISTER_MUTATOR(inv, IS_GAMETYPE(INVASION))
492 SetLimits(autocvar_g_invasion_point_limit, -1, -1, -1);
493 if(autocvar_g_invasion_teams >= 2)
496 if(autocvar_g_invasion_team_spawns)
497 have_team_spawns = -1; // request team spawns
502 if(time > 1) // game loads at time 1
503 error("This is a game type and it cannot be added at runtime.");
505 invasion_Initialize();
507 cvar_settemp("g_monsters", "1");
510 MUTATOR_ONROLLBACK_OR_REMOVE
512 // we actually cannot roll back invasion_Initialize here
513 // BUT: we don't need to! If this gets called, adding always
519 LOG_INFO("This is a game type and it cannot be removed at runtime.");