]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/invasion/sv_invasion.qc
Don't create 2 more sound entities to play the very same sound file
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / invasion / sv_invasion.qc
1 #include "sv_invasion.qh"
2
3 #include <common/monsters/sv_spawn.qh>
4 #include <common/monsters/sv_spawner.qh>
5 #include <common/monsters/sv_monsters.qh>
6
7 #include <server/teamplay.qh>
8
9 float autocvar_g_invasion_round_timelimit;
10 float autocvar_g_invasion_spawnpoint_spawn_delay;
11 float autocvar_g_invasion_warmup;
12 int autocvar_g_invasion_monster_count;
13 bool autocvar_g_invasion_zombies_only;
14 float autocvar_g_invasion_spawn_delay;
15
16 bool victent_present;
17 .bool inv_endreached;
18
19 bool inv_warning_shown; // spammy
20
21 void target_invasion_roundend_use(entity this, entity actor, entity trigger)
22 {
23         if(!IS_PLAYER(actor)) { return; }
24
25         actor.inv_endreached = true;
26
27         int plnum = 0;
28         int realplnum = 0;
29         // let's not count bots
30         FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
31                 ++realplnum;
32                 if(it.inv_endreached)
33                         ++plnum;
34         });
35         if(plnum < ceil(realplnum * min(1, this.count))) // 70% of players
36                 return;
37
38         this.winning = true;
39 }
40
41 spawnfunc(target_invasion_roundend)
42 {
43         if(!g_invasion) { delete(this); return; }
44
45         victent_present = true; // a victory entity is present, we don't need to rely on monster count TODO: merge this with the intrusive list (can check empty)
46
47         if(!this.count) { this.count = 0.7; } // require at least 70% of the players to reach the end before triggering victory
48
49         this.use = target_invasion_roundend_use;
50
51         IL_PUSH(g_invasion_roundends, this);
52 }
53
54 spawnfunc(invasion_wave)
55 {
56         if(!g_invasion) { delete(this); return; }
57
58         IL_PUSH(g_invasion_waves, this);
59 }
60
61 spawnfunc(invasion_spawnpoint)
62 {
63         if(!g_invasion) { delete(this); return; }
64
65         this.classname = "invasion_spawnpoint";
66         IL_PUSH(g_invasion_spawns, this);
67 }
68
69 void ClearWinners();
70
71 // Invasion stage mode winning condition: If the attackers triggered a round end (by fulfilling all objectives)
72 // they win.
73 int WinningCondition_Invasion()
74 {
75         WinningConditionHelper(NULL); // set worldstatus
76
77         int status = WINNING_NO;
78
79         if(autocvar_g_invasion_type == INV_TYPE_STAGE)
80         {
81                 SetWinners(inv_endreached, true);
82
83                 int found = 0;
84                 IL_EACH(g_invasion_roundends, true,
85                 {
86                         ++found;
87                         if(it.winning)
88                         {
89                                 bprint("Invasion: round completed.\n");
90                                 // winners already set (TODO: teamplay support)
91
92                                 status = WINNING_YES;
93                                 break;
94                         }
95                 });
96
97                 if(!found)
98                         status = WINNING_YES; // just end it? TODO: should warn mapper!
99         }
100         else if(autocvar_g_invasion_type == INV_TYPE_HUNT)
101         {
102                 ClearWinners();
103
104                 int found = 0; // NOTE: this ends the round if no monsters are placed
105                 IL_EACH(g_monsters, !(it.spawnflags & MONSTERFLAG_RESPAWNED),
106                 {
107                         ++found;
108                 });
109
110                 if(found <= 0)
111                 {
112                         FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it),
113                         {
114                                 it.winning = true;
115                         });
116                         status = WINNING_YES;
117                 }
118         }
119
120         return status;
121 }
122
123 Monster invasion_PickMonster(int supermonster_count)
124 {
125         RandomSelection_Init();
126
127         FOREACH(Monsters, it != MON_Null,
128         {
129                 if((it.spawnflags & MON_FLAG_HIDDEN) || (it.spawnflags & MONSTER_TYPE_PASSIVE) || (it.spawnflags & MONSTER_TYPE_FLY) || (it.spawnflags & MONSTER_TYPE_SWIM) ||
130                         (it.spawnflags & MONSTER_SIZE_QUAKE) || ((it.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
131                         continue;
132                 if(autocvar_g_invasion_zombies_only && !(it.spawnflags & MONSTER_TYPE_UNDEAD))
133                         continue;
134         RandomSelection_AddEnt(it, 1, 1);
135         });
136
137         return RandomSelection_chosen_ent;
138 }
139
140 entity invasion_PickSpawn()
141 {
142         RandomSelection_Init();
143
144         IL_EACH(g_invasion_spawns, true,
145         {
146                 RandomSelection_AddEnt(it, 1, ((time < it.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
147                 it.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
148         });
149
150         return RandomSelection_chosen_ent;
151 }
152
153 entity invasion_GetWaveEntity(int wavenum)
154 {
155         IL_EACH(g_invasion_waves, it.cnt == wavenum,
156         {
157                 return it; // found one
158         });
159
160         // if no specific one is found, find the last existing wave ent
161         entity best = NULL;
162         IL_EACH(g_invasion_waves, it.cnt <= wavenum,
163         {
164                 if(!best || it.cnt > best.cnt)
165                         best = it;
166         });
167
168         return best;
169 }
170
171 void invasion_SpawnChosenMonster(Monster mon)
172 {
173         entity monster;
174         entity spawn_point = invasion_PickSpawn();
175         entity wave_ent = invasion_GetWaveEntity(inv_roundcnt);
176
177         string tospawn = "";
178         if(wave_ent && wave_ent.spawnmob && wave_ent.spawnmob != "")
179         {
180                 RandomSelection_Init();
181                 FOREACH_WORD(wave_ent.spawnmob, true,
182                 {
183                         RandomSelection_AddString(it, 1, 1);
184                 });
185
186                 tospawn = RandomSelection_chosen_string;
187         }
188
189         if(spawn_point == NULL)
190         {
191                 if(!inv_warning_shown)
192                 {
193                         inv_warning_shown = true;
194                         LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations");
195                 }
196                 entity e = spawn();
197                 setsize(e, mon.m_mins, mon.m_maxs);
198
199                 if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
200                         monster = spawnmonster(e, tospawn, mon.monsterid, NULL, NULL, e.origin, false, false, 2);
201                 else
202                 {
203                         delete(e);
204                         return;
205                 }
206         }
207         else // if spawnmob field falls through (unset), fallback to mon (relying on spawnmonster for that behaviour)
208                 monster = spawnmonster(spawn(), ((spawn_point.spawnmob && spawn_point.spawnmob != "") ? spawn_point.spawnmob : tospawn), mon.monsterid, spawn_point, spawn_point, spawn_point.origin, false, false, 2);
209
210         if(!monster)
211                 return;
212
213         monster.spawnshieldtime = time;
214
215         if(spawn_point)
216         {
217                 if(spawn_point.target_range)
218                         monster.target_range = spawn_point.target_range;
219                 monster.target2 = spawn_point.target2;
220         }
221
222         if(teamplay)
223         {
224                 if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
225                         monster.team = spawn_point.team;
226                 else
227                 {
228                         RandomSelection_Init();
229                         if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_AddFloat(NUM_TEAM_1, 1, 1);
230                         if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_AddFloat(NUM_TEAM_2, 1, 1);
231                         if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_AddFloat(NUM_TEAM_3, 1, 1); }
232                         if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_AddFloat(NUM_TEAM_4, 1, 1); }
233
234                         monster.team = RandomSelection_chosen_float;
235                 }
236
237                 monster_setupcolors(monster);
238
239                 if(monster.sprite)
240                 {
241                         WaypointSprite_UpdateTeamRadar(monster.sprite, RADARICON_DANGER, ((monster.team) ? Team_ColorRGB(monster.team) : '1 0 0'));
242
243                         monster.sprite.team = 0;
244                         monster.sprite.SendFlags |= 1;
245                 }
246         }
247
248         if(monster.monster_attack)
249                 IL_REMOVE(g_monster_targets, monster);
250         monster.monster_attack = false; // it's the player's job to kill all the monsters
251
252         if(inv_roundcnt >= inv_maxrounds)
253                 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
254 }
255
256 void invasion_SpawnMonsters(int supermonster_count)
257 {
258         Monster chosen_monster = invasion_PickMonster(supermonster_count);
259
260         invasion_SpawnChosenMonster(chosen_monster);
261 }
262
263 bool Invasion_CheckWinner()
264 {
265         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
266         {
267                 IL_EACH(g_monsters, true,
268                 {
269                         Monster_Remove(it);
270                 });
271                 IL_CLEAR(g_monsters);
272
273                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
274                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
275                 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
276                 return 1;
277         }
278
279         float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
280
281         IL_EACH(g_monsters, GetResource(it, RES_HEALTH) > 0,
282         {
283                 if((get_monsterinfo(it.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
284                         ++supermonster_count;
285                 ++total_alive_monsters;
286
287                 if(teamplay)
288                 switch(it.team)
289                 {
290                         case NUM_TEAM_1: ++red_alive; break;
291                         case NUM_TEAM_2: ++blue_alive; break;
292                         case NUM_TEAM_3: ++yellow_alive; break;
293                         case NUM_TEAM_4: ++pink_alive; break;
294                 }
295         });
296
297         if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
298         {
299                 if(time >= inv_lastcheck)
300                 {
301                         invasion_SpawnMonsters(supermonster_count);
302                         inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
303                 }
304
305                 return 0;
306         }
307
308         if(inv_numspawned < 1)
309                 return 0; // nothing has spawned yet
310
311         if(teamplay)
312         {
313                 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
314                         return 0;
315         }
316         else if(inv_numkilled < inv_maxspawned)
317                 return 0;
318
319         entity winner = NULL;
320         float winning_score = 0, winner_team = 0;
321
322
323         if(teamplay)
324         {
325                 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
326                 if(blue_alive > 0)
327                 {
328                         if(winner_team) { winner_team = 0; }
329                         else { winner_team = NUM_TEAM_2; }
330                 }
331                 if(yellow_alive > 0)
332                 {
333                         if(winner_team) { winner_team = 0; }
334                         else { winner_team = NUM_TEAM_3; }
335                 }
336                 if(pink_alive > 0)
337                 {
338                         if(winner_team) { winner_team = 0; }
339                         else { winner_team = NUM_TEAM_4; }
340                 }
341         }
342         else
343         {
344                 FOREACH_CLIENT(IS_PLAYER(it), {
345                         float cs = GameRules_scoring_add(it, KILLS, 0);
346                         if(cs > winning_score)
347                         {
348                                 winning_score = cs;
349                                 winner = it;
350                         }
351                 });
352         }
353
354         IL_EACH(g_monsters, true,
355         {
356                 Monster_Remove(it);
357         });
358         IL_CLEAR(g_monsters);
359
360         if(teamplay)
361         {
362                 if(winner_team)
363                 {
364                         Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
365                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
366                 }
367         }
368         else if(winner)
369         {
370                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
371                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
372         }
373
374         round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
375
376         return 1;
377 }
378
379 bool Invasion_CheckPlayers()
380 {
381         return true;
382 }
383
384 void Invasion_RoundStart()
385 {
386         int numplayers = 0;
387         FOREACH_CLIENT(IS_PLAYER(it), {
388                 it.player_blocked = false;
389                 ++numplayers;
390         });
391
392         if(inv_roundcnt < inv_maxrounds)
393                 inv_roundcnt += 1; // a limiter to stop crazy counts
394
395         inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
396
397         inv_maxcurrent = 0;
398         inv_numspawned = 0;
399         inv_numkilled = 0;
400
401         inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
402
403         if(teamplay)
404         {
405                 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
406                 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
407                 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
408                 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
409                 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
410         }
411 }
412
413 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
414 {
415         entity frag_target = M_ARGV(0, entity);
416         entity frag_attacker = M_ARGV(1, entity);
417
418         if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
419         {
420                 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
421                 {
422                         inv_numkilled += 1;
423                         inv_maxcurrent -= 1;
424                 }
425                 if(teamplay) { inv_monsters_perteam[frag_target.team] -= 1; }
426
427                 if(IS_PLAYER(frag_attacker))
428                 {
429                         if(SAME_TEAM(frag_attacker, frag_target)) // in non-teamplay modes, same team = same player, so this works
430                                 GameRules_scoring_add(frag_attacker, KILLS, -1);
431                         else
432                         {
433                                 GameRules_scoring_add(frag_attacker, KILLS, +1);
434                                 if(teamplay)
435                                         TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
436                         }
437                 }
438         }
439 }
440
441 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
442 {
443         entity mon = M_ARGV(0, entity);
444         mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
445
446         if(autocvar_g_invasion_type == INV_TYPE_HUNT)
447                 return false; // allowed
448
449         if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
450                 return true;
451
452         if(!(mon.spawnflags & MONSTERFLAG_RESPAWNED))
453         {
454                 inv_numspawned += 1;
455                 inv_maxcurrent += 1;
456         }
457
458         mon.monster_skill = inv_monsterskill;
459
460         if((get_monsterinfo(mon.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
461                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
462 }
463
464 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
465 {
466         if(autocvar_g_invasion_type != INV_TYPE_ROUND)
467                 return; // uses map spawned monsters
468
469         monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
470         monsters_killed = inv_numkilled;
471 }
472
473 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
474 {
475         // no regeneration in invasion, regardless of the game type
476         return true;
477 }
478
479 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
480 {
481         entity player = M_ARGV(0, entity);
482
483         if(player.bot_attack)
484                 IL_REMOVE(g_bot_targets, player);
485         player.bot_attack = false;
486 }
487
488 MUTATOR_HOOKFUNCTION(inv, Damage_Calculate)
489 {
490         entity frag_attacker = M_ARGV(1, entity);
491         entity frag_target = M_ARGV(2, entity);
492         float frag_damage = M_ARGV(4, float);
493         vector frag_force = M_ARGV(6, vector);
494
495         if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
496         {
497                 frag_damage = 0;
498                 frag_force = '0 0 0';
499
500                 M_ARGV(4, float) = frag_damage;
501                 M_ARGV(6, vector) = frag_force;
502         }
503 }
504
505 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
506 {
507         entity targ = M_ARGV(1, entity);
508
509         if(!IS_MONSTER(targ))
510                 return true;
511 }
512
513 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
514 {
515         if(autocvar_g_invasion_type == INV_TYPE_ROUND)
516         {
517                 start_health = 200;
518                 start_armorvalue = 200;
519         }
520 }
521
522 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
523 {
524         entity frag_target = M_ARGV(1, entity);
525
526         if(IS_MONSTER(frag_target))
527                 return MUT_ACCADD_INVALID;
528         return MUT_ACCADD_INDIFFERENT;
529 }
530
531 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
532 {
533         // monster spawning disabled during an invasion
534         M_ARGV(1, string) = "You cannot spawn monsters during an invasion!";
535         return true;
536 }
537
538 MUTATOR_HOOKFUNCTION(inv, CheckRules_World)
539 {
540         if(autocvar_g_invasion_type == INV_TYPE_ROUND)
541                 return false;
542
543         M_ARGV(0, float) = WinningCondition_Invasion();
544         return true;
545 }
546
547 MUTATOR_HOOKFUNCTION(inv, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
548 {
549         M_ARGV(0, float) = invasion_teams;
550         return true;
551 }
552
553 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
554 {
555         M_ARGV(0, string) = "This command does not work during an invasion!";
556         return true;
557 }
558
559 void invasion_ScoreRules(int inv_teams)
560 {
561         GameRules_score_enabled(false);
562         GameRules_scoring(inv_teams, 0, 0, {
563             if (inv_teams) {
564             field_team(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
565             }
566             field(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
567         });
568 }
569
570 void invasion_DelayedInit(entity this) // Do this check with a delay so we can wait for teams to be set up.
571 {
572         if(autocvar_g_invasion_type == INV_TYPE_HUNT || autocvar_g_invasion_type == INV_TYPE_STAGE)
573                 cvar_set("fraglimit", "0");
574
575         if(autocvar_g_invasion_teams)
576         {
577                 invasion_teams = BITS(bound(2, autocvar_g_invasion_teams, 4));
578         }
579         else
580                 invasion_teams = 0;
581
582         independent_players = 1; // to disable extra useless scores
583
584         invasion_ScoreRules(invasion_teams);
585
586         independent_players = 0;
587
588         if(autocvar_g_invasion_type == INV_TYPE_ROUND)
589         {
590                 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
591                 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
592
593                 inv_roundcnt = 0;
594                 inv_maxrounds = 15; // 15?
595         }
596 }
597
598 void invasion_Initialize()
599 {
600         InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);
601 }