1 #include "gamemode_invasion.qh"
3 #include <common/monsters/sv_spawn.qh>
4 #include <common/monsters/sv_monsters.qh>
6 #include <server/teamplay.qh>
8 IntrusiveList g_invasion_spawns;
9 STATIC_INIT(g_invasion_spawns) { g_invasion_spawns = IL_NEW(); }
11 float autocvar_g_invasion_round_timelimit;
12 float autocvar_g_invasion_spawnpoint_spawn_delay;
13 float autocvar_g_invasion_warmup;
14 int autocvar_g_invasion_monster_count;
15 bool autocvar_g_invasion_zombies_only;
16 float autocvar_g_invasion_spawn_delay;
20 spawnfunc(invasion_spawnpoint)
22 if(!g_invasion) { delete(this); return; }
24 this.classname = "invasion_spawnpoint";
25 IL_PUSH(g_invasion_spawns, this);
28 Monster invasion_PickMonster(int supermonster_count)
30 RandomSelection_Init();
32 FOREACH(Monsters, it != MON_Null,
34 if((it.spawnflags & MONSTER_TYPE_PASSIVE) || (it.spawnflags & MONSTER_TYPE_FLY) || (it.spawnflags & MONSTER_TYPE_SWIM) || (it.spawnflags & MONSTER_SIZE_QUAKE) || ((it.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
36 if(autocvar_g_invasion_zombies_only && !(it.spawnflags & MONSTER_TYPE_UNDEAD))
38 RandomSelection_AddEnt(it, 1, 1);
41 return RandomSelection_chosen_ent;
44 entity invasion_PickSpawn()
46 RandomSelection_Init();
48 IL_EACH(g_invasion_spawns, true,
50 RandomSelection_AddEnt(it, 1, ((time >= it.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
51 it.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
54 return RandomSelection_chosen_ent;
57 void invasion_SpawnChosenMonster(Monster mon)
59 entity spawn_point, monster;
61 spawn_point = invasion_PickSpawn();
63 if(spawn_point == NULL)
65 LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations");
67 setsize(e, mon.mins, mon.maxs);
69 if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
70 monster = spawnmonster(e, "", mon.monsterid, NULL, NULL, e.origin, false, false, 2);
77 else // if spawnmob field falls through (unset), fallback to mon (relying on spawnmonster for that behaviour)
78 monster = spawnmonster(spawn(), spawn_point.spawnmob, mon.monsterid, spawn_point, spawn_point, spawn_point.origin, false, false, 2);
83 if(spawn_point) monster.target2 = spawn_point.target2;
84 monster.spawnshieldtime = time;
85 if(spawn_point && spawn_point.target_range) monster.target_range = spawn_point.target_range;
88 if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
89 monster.team = spawn_point.team;
92 RandomSelection_Init();
93 if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_AddFloat(NUM_TEAM_1, 1, 1);
94 if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_AddFloat(NUM_TEAM_2, 1, 1);
95 if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_AddFloat(NUM_TEAM_3, 1, 1); }
96 if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_AddFloat(NUM_TEAM_4, 1, 1); }
98 monster.team = RandomSelection_chosen_float;
103 monster_setupcolors(monster);
107 WaypointSprite_UpdateTeamRadar(monster.sprite, RADARICON_DANGER, ((monster.team) ? Team_ColorRGB(monster.team) : '1 0 0'));
109 monster.sprite.team = 0;
110 monster.sprite.SendFlags |= 1;
114 monster.monster_attack = false; // it's the player's job to kill all the monsters
116 if(inv_roundcnt >= inv_maxrounds)
117 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
120 void invasion_SpawnMonsters(int supermonster_count)
122 Monster chosen_monster = invasion_PickMonster(supermonster_count);
124 invasion_SpawnChosenMonster(chosen_monster);
127 bool Invasion_CheckWinner()
129 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
131 IL_EACH(g_monsters, true,
135 IL_CLEAR(g_monsters);
137 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
138 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
139 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
143 float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
145 IL_EACH(g_monsters, it.health > 0,
147 if((get_monsterinfo(it.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
148 ++supermonster_count;
149 ++total_alive_monsters;
154 case NUM_TEAM_1: ++red_alive; break;
155 case NUM_TEAM_2: ++blue_alive; break;
156 case NUM_TEAM_3: ++yellow_alive; break;
157 case NUM_TEAM_4: ++pink_alive; break;
161 if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
163 if(time >= inv_lastcheck)
165 invasion_SpawnMonsters(supermonster_count);
166 inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
172 if(inv_numspawned < 1)
173 return 0; // nothing has spawned yet
177 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
180 else if(inv_numkilled < inv_maxspawned)
183 entity winner = NULL;
184 float winning_score = 0, winner_team = 0;
189 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
191 if(winner_team) { winner_team = 0; }
192 else { winner_team = NUM_TEAM_2; }
194 if(winner_team) { winner_team = 0; }
195 else { winner_team = NUM_TEAM_3; }
197 if(winner_team) { winner_team = 0; }
198 else { winner_team = NUM_TEAM_4; }
202 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
203 float cs = PlayerScore_Add(it, SP_KILLS, 0);
204 if(cs > winning_score)
212 IL_EACH(g_monsters, true,
216 IL_CLEAR(g_monsters);
222 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
223 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
228 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
229 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
232 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
237 bool Invasion_CheckPlayers()
242 void Invasion_RoundStart()
245 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
246 it.player_blocked = false;
250 if(inv_roundcnt < inv_maxrounds)
251 inv_roundcnt += 1; // a limiter to stop crazy counts
253 inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
259 inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
263 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
264 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
265 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
266 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
267 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
271 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
273 entity frag_target = M_ARGV(0, entity);
274 entity frag_attacker = M_ARGV(1, entity);
276 if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
280 if(teamplay) { inv_monsters_perteam[frag_target.team] -= 1; }
282 if(IS_PLAYER(frag_attacker))
283 if(SAME_TEAM(frag_attacker, frag_target)) // in non-teamplay modes, same team = same player, so this works
284 PlayerScore_Add(frag_attacker, SP_KILLS, -1);
287 PlayerScore_Add(frag_attacker, SP_KILLS, +1);
289 TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
294 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
296 entity mon = M_ARGV(0, entity);
298 if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
301 if(!(mon.spawnflags & MONSTERFLAG_RESPAWNED))
307 mon.monster_skill = inv_monsterskill;
309 if((get_monsterinfo(mon.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
310 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
312 mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
315 MUTATOR_HOOKFUNCTION(inv, OnEntityPreSpawn)
317 entity ent = M_ARGV(0, entity);
319 if(startsWith(ent.classname, "monster_"))
320 if(!(ent.spawnflags & MONSTERFLAG_SPAWNED))
324 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
326 monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
327 monsters_killed = inv_numkilled;
330 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
332 // no regeneration in invasion
336 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
338 entity player = M_ARGV(0, entity);
340 if(player.bot_attack)
341 IL_REMOVE(g_bot_targets, player);
342 player.bot_attack = false;
345 MUTATOR_HOOKFUNCTION(inv, Damage_Calculate)
347 entity frag_attacker = M_ARGV(1, entity);
348 entity frag_target = M_ARGV(2, entity);
349 float frag_damage = M_ARGV(4, float);
350 vector frag_force = M_ARGV(6, vector);
352 if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
355 frag_force = '0 0 0';
357 M_ARGV(4, float) = frag_damage;
358 M_ARGV(6, vector) = frag_force;
362 MUTATOR_HOOKFUNCTION(inv, SV_ParseClientCommand)
364 if(MUTATOR_RETURNVALUE) // command was already handled?
367 entity player = M_ARGV(0, entity);
368 string cmd_name = M_ARGV(1, string);
370 if(cmd_name == "debuginvasion")
372 sprint(player, strcat("inv_maxspawned = ", ftos(inv_maxspawned), "\n"));
373 sprint(player, strcat("inv_numspawned = ", ftos(inv_numspawned), "\n"));
374 sprint(player, strcat("inv_numkilled = ", ftos(inv_numkilled), "\n"));
375 sprint(player, strcat("inv_roundcnt = ", ftos(inv_roundcnt), "\n"));
376 sprint(player, strcat("monsters_total = ", ftos(monsters_total), "\n"));
377 sprint(player, strcat("monsters_killed = ", ftos(monsters_killed), "\n"));
378 sprint(player, strcat("inv_monsterskill = ", ftos(inv_monsterskill), "\n"));
384 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
386 entity targ = M_ARGV(1, entity);
388 if(!IS_MONSTER(targ))
392 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
395 start_armorvalue = 200;
398 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
400 entity frag_target = M_ARGV(1, entity);
402 if(IS_MONSTER(frag_target))
403 return MUT_ACCADD_INVALID;
404 return MUT_ACCADD_INDIFFERENT;
407 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
409 // monster spawning disabled during an invasion
410 M_ARGV(1, string) = "You cannot spawn monsters during an invasion!";
414 MUTATOR_HOOKFUNCTION(inv, CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
416 M_ARGV(0, float) = invasion_teams;
419 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
421 M_ARGV(0, string) = "This command does not work during an invasion!";
425 void invasion_ScoreRules(int inv_teams)
427 if(inv_teams) { CheckAllowedTeams(NULL); }
428 ScoreRules_basics(inv_teams, 0, 0, false);
429 if(inv_teams) ScoreInfo_SetLabel_TeamScore(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
430 ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
431 ScoreRules_basics_end();
434 void invasion_DelayedInit(entity this) // Do this check with a delay so we can wait for teams to be set up.
436 if(autocvar_g_invasion_teams)
438 invasion_teams = bound(2, autocvar_g_invasion_teams, 4);
440 if(invasion_teams >= 1) teams |= BIT(0);
441 if(invasion_teams >= 2) teams |= BIT(1);
442 if(invasion_teams >= 3) teams |= BIT(2);
443 if(invasion_teams >= 4) teams |= BIT(3);
445 invasion_teams = teams; // now set it?
450 independent_players = 1; // to disable extra useless scores
452 invasion_ScoreRules(invasion_teams);
454 independent_players = 0;
456 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
457 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
460 inv_maxrounds = 15; // 15?
463 void invasion_Initialize()
465 InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);