]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_invasion.qc
GetTeamCount --> CheckAllowedTeams
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator / gamemode_invasion.qc
1 #include "gamemode_invasion.qh"
2
3 #include <common/monsters/sv_spawn.qh>
4 #include <common/monsters/sv_monsters.qh>
5
6 #include <server/teamplay.qh>
7
8 IntrusiveList g_invasion_spawns;
9 STATIC_INIT(g_invasion_spawns) { g_invasion_spawns = IL_NEW(); }
10
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;
17
18 spawnfunc(invasion_spawnpoint)
19 {
20         if(!g_invasion) { delete(this); return; }
21
22         this.classname = "invasion_spawnpoint";
23         IL_PUSH(g_invasion_spawns, this);
24
25         if(autocvar_g_invasion_zombies_only) // precache only if it hasn't been already
26         if(this.monsterid) {
27                 Monster mon = get_monsterinfo(this.monsterid);
28                 mon.mr_precache(mon);
29         }
30 }
31
32 int invasion_PickMonster(int supermonster_count)
33 {
34         if(autocvar_g_invasion_zombies_only)
35                 return MON_ZOMBIE.monsterid;
36
37         RandomSelection_Init();
38
39         FOREACH(Monsters, it != MON_Null,
40         {
41                 if((it.spawnflags & MONSTER_TYPE_FLY) || (it.spawnflags & MONSTER_TYPE_SWIM) || (it.spawnflags & MONSTER_SIZE_QUAKE) || ((it.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
42                         continue;
43         RandomSelection_Add(NULL, it.monsterid, string_null, 1, 1);
44         });
45
46         return RandomSelection_chosen_float;
47 }
48
49 entity invasion_PickSpawn()
50 {
51         RandomSelection_Init();
52
53         IL_EACH(g_invasion_spawns, true,
54         {
55                 RandomSelection_Add(it, 0, string_null, 1, ((time >= it.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
56                 it.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
57         });
58
59         return RandomSelection_chosen_ent;
60 }
61
62 void invasion_SpawnChosenMonster(float mon)
63 {
64         entity spawn_point, monster;
65
66         spawn_point = invasion_PickSpawn();
67
68         if(spawn_point == NULL)
69         {
70                 LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations");
71                 entity e = spawn();
72                 setsize(e, (get_monsterinfo(mon)).mins, (get_monsterinfo(mon)).maxs);
73
74                 if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
75                         monster = spawnmonster("", mon, NULL, NULL, e.origin, false, false, 2);
76                 else return;
77
78                 setthink(e, SUB_Remove);
79                 e.nextthink = time + 0.1;
80         }
81         else
82                 monster = spawnmonster("", ((spawn_point.monsterid) ? spawn_point.monsterid : mon), spawn_point, spawn_point, spawn_point.origin, false, false, 2);
83
84         if(spawn_point) monster.target2 = spawn_point.target2;
85         monster.spawnshieldtime = time;
86         if(spawn_point && spawn_point.target_range) monster.target_range = spawn_point.target_range;
87
88         if(teamplay)
89         if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
90                 monster.team = spawn_point.team;
91         else
92         {
93                 RandomSelection_Init();
94                 if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_Add(NULL, NUM_TEAM_1, string_null, 1, 1);
95                 if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_Add(NULL, NUM_TEAM_2, string_null, 1, 1);
96                 if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_Add(NULL, NUM_TEAM_3, string_null, 1, 1); }
97                 if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_Add(NULL, NUM_TEAM_4, string_null, 1, 1); }
98
99                 monster.team = RandomSelection_chosen_float;
100         }
101
102         if(teamplay)
103         {
104                 monster_setupcolors(monster);
105
106                 if(monster.sprite)
107                 {
108                         WaypointSprite_UpdateTeamRadar(monster.sprite, RADARICON_DANGER, ((monster.team) ? Team_ColorRGB(monster.team) : '1 0 0'));
109
110                         monster.sprite.team = 0;
111                         monster.sprite.SendFlags |= 1;
112                 }
113         }
114
115         monster.monster_attack = false; // it's the player's job to kill all the monsters
116
117         if(inv_roundcnt >= inv_maxrounds)
118                 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
119 }
120
121 void invasion_SpawnMonsters(int supermonster_count)
122 {
123         int chosen_monster = invasion_PickMonster(supermonster_count);
124
125         invasion_SpawnChosenMonster(chosen_monster);
126 }
127
128 bool Invasion_CheckWinner()
129 {
130         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
131         {
132                 IL_EACH(g_monsters, true,
133                 {
134                         Monster_Remove(it);
135                 });
136                 IL_CLEAR(g_monsters);
137
138                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
139                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
140                 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
141                 return 1;
142         }
143
144         float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
145
146         IL_EACH(g_monsters, it.health > 0,
147         {
148                 if((get_monsterinfo(it.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
149                         ++supermonster_count;
150                 ++total_alive_monsters;
151
152                 if(teamplay)
153                 switch(it.team)
154                 {
155                         case NUM_TEAM_1: ++red_alive; break;
156                         case NUM_TEAM_2: ++blue_alive; break;
157                         case NUM_TEAM_3: ++yellow_alive; break;
158                         case NUM_TEAM_4: ++pink_alive; break;
159                 }
160         });
161
162         if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
163         {
164                 if(time >= inv_lastcheck)
165                 {
166                         invasion_SpawnMonsters(supermonster_count);
167                         inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
168                 }
169
170                 return 0;
171         }
172
173         if(inv_numspawned < 1)
174                 return 0; // nothing has spawned yet
175
176         if(teamplay)
177         {
178                 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
179                         return 0;
180         }
181         else if(inv_numkilled < inv_maxspawned)
182                 return 0;
183
184         entity winner = NULL;
185         float winning_score = 0, winner_team = 0;
186
187
188         if(teamplay)
189         {
190                 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
191                 if(blue_alive > 0)
192                 if(winner_team) { winner_team = 0; }
193                 else { winner_team = NUM_TEAM_2; }
194                 if(yellow_alive > 0)
195                 if(winner_team) { winner_team = 0; }
196                 else { winner_team = NUM_TEAM_3; }
197                 if(pink_alive > 0)
198                 if(winner_team) { winner_team = 0; }
199                 else { winner_team = NUM_TEAM_4; }
200         }
201         else
202         {
203                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
204                         float cs = PlayerScore_Add(it, SP_KILLS, 0);
205                         if(cs > winning_score)
206                         {
207                                 winning_score = cs;
208                                 winner = it;
209                         }
210                 ));
211         }
212
213         IL_EACH(g_monsters, true,
214         {
215                 Monster_Remove(it);
216         });
217         IL_CLEAR(g_monsters);
218
219         if(teamplay)
220         {
221                 if(winner_team)
222                 {
223                         Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
224                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
225                 }
226         }
227         else if(winner)
228         {
229                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
230                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
231         }
232
233         round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
234
235         return 1;
236 }
237
238 bool Invasion_CheckPlayers()
239 {
240         return true;
241 }
242
243 void Invasion_RoundStart()
244 {
245         int numplayers = 0;
246         FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
247                 it.player_blocked = false;
248                 ++numplayers;
249         ));
250
251         if(inv_roundcnt < inv_maxrounds)
252                 inv_roundcnt += 1; // a limiter to stop crazy counts
253
254         inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
255
256         inv_maxcurrent = 0;
257         inv_numspawned = 0;
258         inv_numkilled = 0;
259
260         inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
261
262         if(teamplay)
263         {
264                 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
265                 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
266                 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
267                 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
268                 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
269         }
270 }
271
272 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
273 {
274         entity frag_target = M_ARGV(0, entity);
275         entity frag_attacker = M_ARGV(1, entity);
276
277         if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
278         {
279                 inv_numkilled += 1;
280                 inv_maxcurrent -= 1;
281                 if(teamplay) { inv_monsters_perteam[frag_target.team] -= 1; }
282
283                 if(IS_PLAYER(frag_attacker))
284                 if(SAME_TEAM(frag_attacker, frag_target)) // in non-teamplay modes, same team = same player, so this works
285                         PlayerScore_Add(frag_attacker, SP_KILLS, -1);
286                 else
287                 {
288                         PlayerScore_Add(frag_attacker, SP_KILLS, +1);
289                         if(teamplay)
290                                 TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
291                 }
292         }
293 }
294
295 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
296 {
297         entity mon = M_ARGV(0, entity);
298
299         if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
300                 return true;
301
302         if(!(mon.spawnflags & MONSTERFLAG_RESPAWNED))
303         {
304                 inv_numspawned += 1;
305                 inv_maxcurrent += 1;
306         }
307
308         mon.monster_skill = inv_monsterskill;
309
310         if((get_monsterinfo(mon.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
311                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
312
313         mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
314 }
315
316 MUTATOR_HOOKFUNCTION(inv, OnEntityPreSpawn)
317 {
318         entity ent = M_ARGV(0, entity);
319
320         if(startsWith(ent.classname, "monster_"))
321         if(!(ent.spawnflags & MONSTERFLAG_SPAWNED))
322                 return true;
323 }
324
325 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
326 {
327         monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
328         monsters_killed = inv_numkilled;
329 }
330
331 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
332 {
333         // no regeneration in invasion
334         return true;
335 }
336
337 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
338 {
339         entity player = M_ARGV(0, entity);
340
341         if(player.bot_attack)
342                 IL_REMOVE(g_bot_targets, player);
343         player.bot_attack = false;
344 }
345
346 MUTATOR_HOOKFUNCTION(inv, PlayerDamage_Calculate)
347 {
348         entity frag_attacker = M_ARGV(1, entity);
349         entity frag_target = M_ARGV(2, entity);
350         float frag_damage = M_ARGV(4, float);
351         vector frag_force = M_ARGV(6, vector);
352
353         if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
354         {
355                 frag_damage = 0;
356                 frag_force = '0 0 0';
357
358                 M_ARGV(4, float) = frag_damage;
359                 M_ARGV(6, vector) = frag_force;
360         }
361 }
362
363 MUTATOR_HOOKFUNCTION(inv, SV_ParseClientCommand)
364 {
365         if(MUTATOR_RETURNVALUE) // command was already handled?
366                 return;
367
368         entity player = M_ARGV(0, entity);
369         string cmd_name = M_ARGV(1, string);
370
371         if(cmd_name == "debuginvasion")
372         {
373                 sprint(player, strcat("inv_maxspawned = ", ftos(inv_maxspawned), "\n"));
374                 sprint(player, strcat("inv_numspawned = ", ftos(inv_numspawned), "\n"));
375                 sprint(player, strcat("inv_numkilled = ", ftos(inv_numkilled), "\n"));
376                 sprint(player, strcat("inv_roundcnt = ", ftos(inv_roundcnt), "\n"));
377                 sprint(player, strcat("monsters_total = ", ftos(monsters_total), "\n"));
378                 sprint(player, strcat("monsters_killed = ", ftos(monsters_killed), "\n"));
379                 sprint(player, strcat("inv_monsterskill = ", ftos(inv_monsterskill), "\n"));
380
381                 return true;
382         }
383 }
384
385 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
386 {
387         entity targ = M_ARGV(1, entity);
388
389         if(!IS_MONSTER(targ))
390                 return true;
391 }
392
393 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
394 {
395         start_health = 200;
396         start_armorvalue = 200;
397 }
398
399 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
400 {
401         entity frag_target = M_ARGV(1, entity);
402
403         if(IS_MONSTER(frag_target))
404                 return MUT_ACCADD_INVALID;
405         return MUT_ACCADD_INDIFFERENT;
406 }
407
408 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
409 {
410         // monster spawning disabled during an invasion
411         M_ARGV(1, string) = "You cannot spawn monsters during an invasion!";
412         return true;
413 }
414
415 MUTATOR_HOOKFUNCTION(inv, CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
416 {
417         M_ARGV(0, float) = invasion_teams;
418 }
419
420 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
421 {
422         M_ARGV(0, string) = "This command does not work during an invasion!";
423         return true;
424 }
425
426 void invasion_ScoreRules(int inv_teams)
427 {
428         if(inv_teams) { CheckAllowedTeams(NULL); }
429         ScoreRules_basics(inv_teams, 0, 0, false);
430         if(inv_teams) ScoreInfo_SetLabel_TeamScore(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
431         ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
432         ScoreRules_basics_end();
433 }
434
435 void invasion_DelayedInit(entity this) // Do this check with a delay so we can wait for teams to be set up.
436 {
437         if(autocvar_g_invasion_teams)
438         {
439                 invasion_teams = bound(2, autocvar_g_invasion_teams, 4);
440                 int teams = 0;
441                 if(invasion_teams >= 1) teams |= BIT(0);
442                 if(invasion_teams >= 2) teams |= BIT(1);
443                 if(invasion_teams >= 3) teams |= BIT(2);
444                 if(invasion_teams >= 4) teams |= BIT(3);
445
446                 invasion_teams = teams; // now set it?
447         }
448         else
449                 invasion_teams = 0;
450
451         independent_players = 1; // to disable extra useless scores
452
453         invasion_ScoreRules(invasion_teams);
454
455         independent_players = 0;
456
457         round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
458         round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
459
460         inv_roundcnt = 0;
461         inv_maxrounds = 15; // 15?
462 }
463
464 void invasion_Initialize()
465 {
466         if(autocvar_g_invasion_zombies_only) {
467                 Monster mon = MON_ZOMBIE;
468                 mon.mr_precache(mon);
469         } else
470         {
471                 float i;
472                 entity mon;
473                 for(i = MON_FIRST; i <= MON_LAST; ++i)
474                 {
475                         mon = get_monsterinfo(i);
476                         if((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM))
477                                 continue; // flying/swimming monsters not yet supported
478
479                         mon.mr_precache(mon);
480                 }
481         }
482
483         InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);
484 }