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