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