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