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