1 #include "gamemode_invasion.qh"
5 #include "../../common/monsters/spawn.qh"
6 #include "../../common/monsters/sv_monsters.qh"
8 #include "../teamplay.qh"
12 float autocvar_g_invasion_round_timelimit;
13 int autocvar_g_invasion_teams;
14 bool autocvar_g_invasion_team_spawns;
15 float autocvar_g_invasion_spawnpoint_spawn_delay;
16 #define autocvar_g_invasion_point_limit cvar("g_invasion_point_limit")
17 float autocvar_g_invasion_warmup;
18 int autocvar_g_invasion_monster_count;
19 bool autocvar_g_invasion_zombies_only;
20 float autocvar_g_invasion_spawn_delay;
22 spawnfunc(invasion_spawnpoint)
24 if(!g_invasion) { remove(self); return; }
26 self.classname = "invasion_spawnpoint";
28 if(autocvar_g_invasion_zombies_only) // precache only if it hasn't been already
30 Monster mon = get_monsterinfo(self.monsterid);
35 float invasion_PickMonster(float supermonster_count)
37 if(autocvar_g_invasion_zombies_only)
38 return MON_ZOMBIE.monsterid;
43 RandomSelection_Init();
45 for(i = MON_FIRST; i <= MON_LAST; ++i)
47 mon = get_monsterinfo(i);
48 if((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM) || ((mon.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
49 continue; // flying/swimming monsters not yet supported
51 RandomSelection_Add(world, i, string_null, 1, 1);
54 return RandomSelection_chosen_float;
57 entity invasion_PickSpawn()
61 RandomSelection_Init();
63 for(e = world;(e = find(e, classname, "invasion_spawnpoint")); )
65 RandomSelection_Add(e, 0, string_null, 1, ((time >= e.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
66 e.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
69 return RandomSelection_chosen_ent;
72 void invasion_SpawnChosenMonster(float mon)
74 entity spawn_point, monster;
76 spawn_point = invasion_PickSpawn();
78 if(spawn_point == world)
80 LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations\n");
82 setsize(e, (get_monsterinfo(mon)).mins, (get_monsterinfo(mon)).maxs);
84 if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
85 monster = spawnmonster("", mon, world, world, e.origin, false, false, 2);
89 e.nextthink = time + 0.1;
92 monster = spawnmonster("", ((spawn_point.monsterid) ? spawn_point.monsterid : mon), spawn_point, spawn_point, spawn_point.origin, false, false, 2);
94 if(spawn_point) monster.target2 = spawn_point.target2;
95 monster.spawnshieldtime = time;
96 if(spawn_point && spawn_point.target_range) monster.target_range = spawn_point.target_range;
99 if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
100 monster.team = spawn_point.team;
103 RandomSelection_Init();
104 if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_Add(world, NUM_TEAM_1, string_null, 1, 1);
105 if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_Add(world, NUM_TEAM_2, string_null, 1, 1);
106 if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_Add(world, NUM_TEAM_3, string_null, 1, 1); }
107 if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_Add(world, NUM_TEAM_4, string_null, 1, 1); }
109 monster.team = RandomSelection_chosen_float;
114 monster_setupcolors(monster);
118 WaypointSprite_UpdateTeamRadar(monster.sprite, RADARICON_DANGER, ((monster.team) ? Team_ColorRGB(monster.team) : '1 0 0'));
120 monster.sprite.team = 0;
121 monster.sprite.SendFlags |= 1;
125 monster.monster_attack = false; // it's the player's job to kill all the monsters
127 if(inv_roundcnt >= inv_maxrounds)
128 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
131 void invasion_SpawnMonsters(float supermonster_count)
133 float chosen_monster = invasion_PickMonster(supermonster_count);
135 invasion_SpawnChosenMonster(chosen_monster);
138 float Invasion_CheckWinner()
141 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
143 FOR_EACH_MONSTER(head)
144 Monster_Remove(head);
146 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_OVER);
147 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_OVER);
148 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
152 float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
154 FOR_EACH_MONSTER(head) if(head.health > 0)
156 if((get_monsterinfo(head.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
157 ++supermonster_count;
158 ++total_alive_monsters;
163 case NUM_TEAM_1: ++red_alive; break;
164 case NUM_TEAM_2: ++blue_alive; break;
165 case NUM_TEAM_3: ++yellow_alive; break;
166 case NUM_TEAM_4: ++pink_alive; break;
170 if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
172 if(time >= inv_lastcheck)
174 invasion_SpawnMonsters(supermonster_count);
175 inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
181 if(inv_numspawned < 1)
182 return 0; // nothing has spawned yet
186 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
189 else if(inv_numkilled < inv_maxspawned)
192 entity winner = world;
193 float winning_score = 0, winner_team = 0;
198 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
200 if(winner_team) { winner_team = 0; }
201 else { winner_team = NUM_TEAM_2; }
203 if(winner_team) { winner_team = 0; }
204 else { winner_team = NUM_TEAM_3; }
206 if(winner_team) { winner_team = 0; }
207 else { winner_team = NUM_TEAM_4; }
210 FOR_EACH_PLAYER(head)
212 float cs = PlayerScore_Add(head, SP_KILLS, 0);
213 if(cs > winning_score)
220 FOR_EACH_MONSTER(head)
221 Monster_Remove(head);
227 Send_Notification(NOTIF_ALL, world, MSG_CENTER, APP_TEAM_NUM_4(winner_team, CENTER_ROUND_TEAM_WIN_));
228 Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_NUM_4(winner_team, INFO_ROUND_TEAM_WIN_));
233 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
234 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
237 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
242 float Invasion_CheckPlayers()
247 void Invasion_RoundStart()
250 float numplayers = 0;
253 e.player_blocked = 0;
257 if(inv_roundcnt < inv_maxrounds)
258 inv_roundcnt += 1; // a limiter to stop crazy counts
260 inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
266 inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
270 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
271 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
272 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
273 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
274 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
278 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
280 if(!(self.spawnflags & MONSTERFLAG_RESPAWNED))
284 if(teamplay) { inv_monsters_perteam[self.team] -= 1; }
286 if(IS_PLAYER(frag_attacker))
287 if(SAME_TEAM(frag_attacker, self)) // in non-teamplay modes, same team = same player, so this works
288 PlayerScore_Add(frag_attacker, SP_KILLS, -1);
291 PlayerScore_Add(frag_attacker, SP_KILLS, +1);
293 TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
300 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
302 if(!(self.spawnflags & MONSTERFLAG_SPAWNED))
305 if(!(self.spawnflags & MONSTERFLAG_RESPAWNED))
311 self.monster_skill = inv_monsterskill;
313 if((get_monsterinfo(self.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
314 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, self.monster_name);
316 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
321 MUTATOR_HOOKFUNCTION(inv, OnEntityPreSpawn)
323 if(startsWith(self.classname, "monster_"))
324 if(!(self.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;
338 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
340 // no regeneration in invasion
344 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
346 self.bot_attack = false;
350 MUTATOR_HOOKFUNCTION(inv, PlayerDamage_Calculate)
352 if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
355 frag_force = '0 0 0';
361 MUTATOR_HOOKFUNCTION(inv, SV_ParseClientCommand)
363 if(MUTATOR_RETURNVALUE) // command was already handled?
366 if(cmd_name == "debuginvasion")
368 sprint(self, strcat("inv_maxspawned = ", ftos(inv_maxspawned), "\n"));
369 sprint(self, strcat("inv_numspawned = ", ftos(inv_numspawned), "\n"));
370 sprint(self, strcat("inv_numkilled = ", ftos(inv_numkilled), "\n"));
371 sprint(self, strcat("inv_roundcnt = ", ftos(inv_roundcnt), "\n"));
372 sprint(self, strcat("monsters_total = ", ftos(monsters_total), "\n"));
373 sprint(self, strcat("monsters_killed = ", ftos(monsters_killed), "\n"));
374 sprint(self, strcat("inv_monsterskill = ", ftos(inv_monsterskill), "\n"));
382 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
384 if(!IS_MONSTER(checkentity))
390 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
393 start_armorvalue = 200;
397 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
399 if(IS_MONSTER(frag_target))
400 return MUT_ACCADD_INVALID;
401 return MUT_ACCADD_INDIFFERENT;
404 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
406 // monster spawning disabled during an invasion
410 MUTATOR_HOOKFUNCTION(inv, GetTeamCount, CBC_ORDER_EXCLUSIVE)
412 ret_float = invasion_teams;
416 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
418 ret_string = "This command does not work during an invasion!";
422 void invasion_ScoreRules(float inv_teams)
424 if(inv_teams) { CheckAllowedTeams(world); }
425 ScoreRules_basics(inv_teams, 0, 0, false);
426 if(inv_teams) ScoreInfo_SetLabel_TeamScore(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
427 ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
428 ScoreRules_basics_end();
431 void invasion_DelayedInit() // Do this check with a delay so we can wait for teams to be set up.
433 if(autocvar_g_invasion_teams)
434 invasion_teams = bound(2, autocvar_g_invasion_teams, 4);
438 independent_players = 1; // to disable extra useless scores
440 invasion_ScoreRules(invasion_teams);
442 independent_players = 0;
444 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
445 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
448 inv_maxrounds = 15; // 15?
451 void invasion_Initialize()
453 if(autocvar_g_invasion_zombies_only) {
454 Monster mon = MON_ZOMBIE;
455 mon.mr_precache(mon);
460 for(i = MON_FIRST; i <= MON_LAST; ++i)
462 mon = get_monsterinfo(i);
463 if((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM))
464 continue; // flying/swimming monsters not yet supported
466 mon.mr_precache(mon);
470 InitializeEntity(world, invasion_DelayedInit, INITPRIO_GAMETYPE);
473 REGISTER_MUTATOR(inv, IS_GAMETYPE(INVASION))
475 SetLimits(autocvar_g_invasion_point_limit, -1, -1, -1);
476 if(autocvar_g_invasion_teams >= 2)
479 if(autocvar_g_invasion_team_spawns)
480 have_team_spawns = -1; // request team spawns
485 if(time > 1) // game loads at time 1
486 error("This is a game type and it cannot be added at runtime.");
488 invasion_Initialize();
490 cvar_settemp("g_monsters", "1");
493 MUTATOR_ONROLLBACK_OR_REMOVE
495 // we actually cannot roll back invasion_Initialize here
496 // BUT: we don't need to! If this gets called, adding always
502 LOG_INFO("This is a game type and it cannot be removed at runtime.");