1 #include "gamemode_invasion.qh"
2 #ifndef GAMEMODE_INVASION_H
3 #define GAMEMODE_INVASION_H
5 #define autocvar_g_invasion_point_limit cvar("g_invasion_point_limit")
6 int autocvar_g_invasion_teams;
7 bool autocvar_g_invasion_team_spawns;
9 void invasion_Initialize();
11 REGISTER_MUTATOR(inv, false)
15 if (time > 1) // game loads at time 1
16 error("This is a game type and it cannot be added at runtime.");
18 invasion_Initialize();
20 cvar_settemp("g_monsters", "1");
22 SetLimits(autocvar_g_invasion_point_limit, autocvar_leadlimit_override, autocvar_timelimit_override, -1);
23 if (autocvar_g_invasion_teams >= 2)
26 if (autocvar_g_invasion_team_spawns)
27 have_team_spawns = -1; // request team spawns
31 MUTATOR_ONROLLBACK_OR_REMOVE
33 // we actually cannot roll back invasion_Initialize here
34 // BUT: we don't need to! If this gets called, adding always
40 LOG_INFO("This is a game type and it cannot be removed at runtime.");
56 float inv_monsters_perteam[17];
58 float inv_monsterskill;
60 const float ST_INV_KILLS = 1;
65 #include <common/monsters/spawn.qh>
66 #include <common/monsters/sv_monsters.qh>
68 #include <server/teamplay.qh>
71 float autocvar_g_invasion_round_timelimit;
72 float autocvar_g_invasion_spawnpoint_spawn_delay;
73 float autocvar_g_invasion_warmup;
74 int autocvar_g_invasion_monster_count;
75 bool autocvar_g_invasion_zombies_only;
76 float autocvar_g_invasion_spawn_delay;
78 spawnfunc(invasion_spawnpoint)
80 if(!g_invasion) { remove(this); return; }
82 this.classname = "invasion_spawnpoint";
84 if(autocvar_g_invasion_zombies_only) // precache only if it hasn't been already
86 Monster mon = get_monsterinfo(this.monsterid);
91 float invasion_PickMonster(float supermonster_count)
93 if(autocvar_g_invasion_zombies_only)
94 return MON_ZOMBIE.monsterid;
99 RandomSelection_Init();
101 for(i = MON_FIRST; i <= MON_LAST; ++i)
103 mon = get_monsterinfo(i);
104 if((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM) || ((mon.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
105 continue; // flying/swimming monsters not yet supported
107 RandomSelection_Add(NULL, i, string_null, 1, 1);
110 return RandomSelection_chosen_float;
113 entity invasion_PickSpawn()
117 RandomSelection_Init();
119 for(e = NULL;(e = find(e, classname, "invasion_spawnpoint")); )
121 RandomSelection_Add(e, 0, string_null, 1, ((time >= e.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
122 e.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
125 return RandomSelection_chosen_ent;
128 void invasion_SpawnChosenMonster(float mon)
130 entity spawn_point, monster;
132 spawn_point = invasion_PickSpawn();
134 if(spawn_point == NULL)
136 LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations\n");
138 setsize(e, (get_monsterinfo(mon)).mins, (get_monsterinfo(mon)).maxs);
140 if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
141 monster = spawnmonster("", mon, NULL, NULL, e.origin, false, false, 2);
144 setthink(e, SUB_Remove);
145 e.nextthink = time + 0.1;
148 monster = spawnmonster("", ((spawn_point.monsterid) ? spawn_point.monsterid : mon), spawn_point, spawn_point, spawn_point.origin, false, false, 2);
150 if(spawn_point) monster.target2 = spawn_point.target2;
151 monster.spawnshieldtime = time;
152 if(spawn_point && spawn_point.target_range) monster.target_range = spawn_point.target_range;
155 if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
156 monster.team = spawn_point.team;
159 RandomSelection_Init();
160 if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_Add(NULL, NUM_TEAM_1, string_null, 1, 1);
161 if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_Add(NULL, NUM_TEAM_2, string_null, 1, 1);
162 if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_Add(NULL, NUM_TEAM_3, string_null, 1, 1); }
163 if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_Add(NULL, NUM_TEAM_4, string_null, 1, 1); }
165 monster.team = RandomSelection_chosen_float;
170 monster_setupcolors(monster);
174 WaypointSprite_UpdateTeamRadar(monster.sprite, RADARICON_DANGER, ((monster.team) ? Team_ColorRGB(monster.team) : '1 0 0'));
176 monster.sprite.team = 0;
177 monster.sprite.SendFlags |= 1;
181 monster.monster_attack = false; // it's the player's job to kill all the monsters
183 if(inv_roundcnt >= inv_maxrounds)
184 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
187 void invasion_SpawnMonsters(float supermonster_count)
189 float chosen_monster = invasion_PickMonster(supermonster_count);
191 invasion_SpawnChosenMonster(chosen_monster);
194 float Invasion_CheckWinner()
196 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
198 FOREACH_ENTITY_FLAGS(flags, FL_MONSTER, LAMBDA(Monster_Remove(it)));
200 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
201 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
202 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
206 float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
208 FOREACH_ENTITY_FLAGS(flags, FL_MONSTER, LAMBDA(
211 if((get_monsterinfo(it.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;
226 if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
228 if(time >= inv_lastcheck)
230 invasion_SpawnMonsters(supermonster_count);
231 inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
237 if(inv_numspawned < 1)
238 return 0; // nothing has spawned yet
242 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
245 else if(inv_numkilled < inv_maxspawned)
248 entity winner = NULL;
249 float winning_score = 0, winner_team = 0;
254 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
256 if(winner_team) { winner_team = 0; }
257 else { winner_team = NUM_TEAM_2; }
259 if(winner_team) { winner_team = 0; }
260 else { winner_team = NUM_TEAM_3; }
262 if(winner_team) { winner_team = 0; }
263 else { winner_team = NUM_TEAM_4; }
267 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
268 float cs = PlayerScore_Add(it, SP_KILLS, 0);
269 if(cs > winning_score)
277 FOREACH_ENTITY_FLAGS(flags, FL_MONSTER, LAMBDA(Monster_Remove(it)));
283 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
284 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
289 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
290 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
293 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
298 bool Invasion_CheckPlayers()
303 void Invasion_RoundStart()
306 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
307 it.player_blocked = false;
311 if(inv_roundcnt < inv_maxrounds)
312 inv_roundcnt += 1; // a limiter to stop crazy counts
314 inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
320 inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
324 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
325 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
326 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
327 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
328 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
332 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
334 entity frag_target = M_ARGV(0, entity);
335 entity frag_attacker = M_ARGV(1, entity);
337 if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
341 if(teamplay) { inv_monsters_perteam[frag_target.team] -= 1; }
343 if(IS_PLAYER(frag_attacker))
344 if(SAME_TEAM(frag_attacker, frag_target)) // in non-teamplay modes, same team = same player, so this works
345 PlayerScore_Add(frag_attacker, SP_KILLS, -1);
348 PlayerScore_Add(frag_attacker, SP_KILLS, +1);
350 TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
355 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
357 entity mon = M_ARGV(0, entity);
359 if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
362 if(!(mon.spawnflags & MONSTERFLAG_RESPAWNED))
368 mon.monster_skill = inv_monsterskill;
370 if((get_monsterinfo(mon.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
371 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
373 mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
376 MUTATOR_HOOKFUNCTION(inv, OnEntityPreSpawn)
378 entity ent = M_ARGV(0, entity);
380 if(startsWith(ent.classname, "monster_"))
381 if(!(ent.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;
391 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
393 // no regeneration in invasion
397 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
399 entity player = M_ARGV(0, entity);
401 player.bot_attack = false;
404 MUTATOR_HOOKFUNCTION(inv, PlayerDamage_Calculate)
406 entity frag_attacker = M_ARGV(1, entity);
407 entity frag_target = M_ARGV(2, entity);
408 float frag_damage = M_ARGV(4, float);
409 vector frag_force = M_ARGV(6, vector);
411 if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
414 frag_force = '0 0 0';
416 M_ARGV(4, float) = frag_damage;
417 M_ARGV(6, vector) = frag_force;
421 MUTATOR_HOOKFUNCTION(inv, SV_ParseClientCommand)
423 if(MUTATOR_RETURNVALUE) // command was already handled?
426 entity player = M_ARGV(0, entity);
427 string cmd_name = M_ARGV(1, string);
429 if(cmd_name == "debuginvasion")
431 sprint(player, strcat("inv_maxspawned = ", ftos(inv_maxspawned), "\n"));
432 sprint(player, strcat("inv_numspawned = ", ftos(inv_numspawned), "\n"));
433 sprint(player, strcat("inv_numkilled = ", ftos(inv_numkilled), "\n"));
434 sprint(player, strcat("inv_roundcnt = ", ftos(inv_roundcnt), "\n"));
435 sprint(player, strcat("monsters_total = ", ftos(monsters_total), "\n"));
436 sprint(player, strcat("monsters_killed = ", ftos(monsters_killed), "\n"));
437 sprint(player, strcat("inv_monsterskill = ", ftos(inv_monsterskill), "\n"));
443 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
445 entity targ = M_ARGV(1, entity);
447 if(!IS_MONSTER(targ))
451 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
454 start_armorvalue = 200;
457 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
459 entity frag_target = M_ARGV(1, entity);
461 if(IS_MONSTER(frag_target))
462 return MUT_ACCADD_INVALID;
463 return MUT_ACCADD_INDIFFERENT;
466 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
468 // monster spawning disabled during an invasion
469 M_ARGV(1, string) = "You cannot spawn monsters during an invasion!";
473 MUTATOR_HOOKFUNCTION(inv, GetTeamCount, CBC_ORDER_EXCLUSIVE)
475 M_ARGV(0, float) = invasion_teams;
478 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
480 M_ARGV(0, string) = "This command does not work during an invasion!";
484 void invasion_ScoreRules(float inv_teams)
486 if(inv_teams) { CheckAllowedTeams(NULL); }
487 ScoreRules_basics(inv_teams, 0, 0, false);
488 if(inv_teams) ScoreInfo_SetLabel_TeamScore(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
489 ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
490 ScoreRules_basics_end();
493 void invasion_DelayedInit(entity this) // Do this check with a delay so we can wait for teams to be set up.
495 if(autocvar_g_invasion_teams)
496 invasion_teams = bound(2, autocvar_g_invasion_teams, 4);
500 independent_players = 1; // to disable extra useless scores
502 invasion_ScoreRules(invasion_teams);
504 independent_players = 0;
506 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
507 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
510 inv_maxrounds = 15; // 15?
513 void invasion_Initialize()
515 if(autocvar_g_invasion_zombies_only) {
516 Monster mon = MON_ZOMBIE;
517 mon.mr_precache(mon);
522 for(i = MON_FIRST; i <= MON_LAST; ++i)
524 mon = get_monsterinfo(i);
525 if((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM))
526 continue; // flying/swimming monsters not yet supported
528 mon.mr_precache(mon);
532 InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);