]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_invasion.qc
Merge branch 'master' into Mario/invasion_types
[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 // Invasion stage mode winning condition: If the attackers triggered a round end (by fulfilling all objectives)
80 // they win.
81 int WinningCondition_Invasion()
82 {
83         WinningConditionHelper(NULL); // set worldstatus
84
85         int status = WINNING_NO;
86
87         if(autocvar_g_invasion_type == INV_TYPE_STAGE)
88         {
89                 SetWinners(inv_endreached, true);
90
91                 int found = 0;
92                 IL_EACH(g_invasion_roundends, true,
93                 {
94                         ++found;
95                         if(it.winning)
96                         {
97                                 bprint("Invasion: round completed.\n");
98                                 // winners already set (TODO: teamplay support)
99
100                                 status = WINNING_YES;
101                                 break;
102                         }
103                 });
104
105                 if(!found)
106                         status = WINNING_YES; // just end it? TODO: should warn mapper!
107         }
108         else if(autocvar_g_invasion_type == INV_TYPE_HUNT)
109         {
110                 ClearWinners();
111
112                 int found = 0; // NOTE: this ends the round if no monsters are placed
113                 IL_EACH(g_monsters, !(it.spawnflags & MONSTERFLAG_RESPAWNED),
114                 {
115                         ++found;
116                 });
117
118                 if(found <= 0)
119                 {
120                         FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it),
121                         {
122                                 it.winning = true;
123                         });
124                         status = WINNING_YES;
125                 }
126         }
127
128         return status;
129 }
130
131 Monster invasion_PickMonster(int supermonster_count)
132 {
133         RandomSelection_Init();
134
135         FOREACH(Monsters, it != MON_Null,
136         {
137                 if((it.spawnflags & MON_FLAG_HIDDEN) || (it.spawnflags & MONSTER_TYPE_PASSIVE) || (it.spawnflags & MONSTER_TYPE_FLY) || (it.spawnflags & MONSTER_TYPE_SWIM) ||
138                         (it.spawnflags & MONSTER_SIZE_QUAKE) || ((it.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
139                         continue;
140                 if(autocvar_g_invasion_zombies_only && !(it.spawnflags & MONSTER_TYPE_UNDEAD))
141                         continue;
142         RandomSelection_AddEnt(it, 1, 1);
143         });
144
145         return RandomSelection_chosen_ent;
146 }
147
148 entity invasion_PickSpawn()
149 {
150         RandomSelection_Init();
151
152         IL_EACH(g_invasion_spawns, true,
153         {
154                 RandomSelection_AddEnt(it, 1, ((time < it.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
155                 it.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
156         });
157
158         return RandomSelection_chosen_ent;
159 }
160
161 entity invasion_GetWaveEntity(int wavenum)
162 {
163         IL_EACH(g_invasion_waves, it.cnt == wavenum,
164         {
165                 return it; // found one
166         });
167
168         // if no specific one is found, find the last existing wave ent
169         entity best = NULL;
170         IL_EACH(g_invasion_waves, it.cnt <= wavenum,
171         {
172                 if(!best || it.cnt > best.cnt)
173                         best = it;
174         });
175
176         return best;
177 }
178
179 void invasion_SpawnChosenMonster(Monster mon)
180 {
181         entity monster;
182         entity spawn_point = invasion_PickSpawn();
183         entity wave_ent = invasion_GetWaveEntity(inv_roundcnt);
184
185         string tospawn = "";
186         if(wave_ent && wave_ent.spawnmob && wave_ent.spawnmob != "")
187         {
188                 RandomSelection_Init();
189                 FOREACH_WORD(wave_ent.spawnmob, true,
190                 {
191                         RandomSelection_AddString(it, 1, 1);
192                 });
193
194                 tospawn = RandomSelection_chosen_string;
195         }
196
197         if(spawn_point == NULL)
198         {
199                 if(!inv_warning_shown)
200                 {
201                         inv_warning_shown = true;
202                         LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations");
203                 }
204                 entity e = spawn();
205                 setsize(e, mon.m_mins, mon.m_maxs);
206
207                 if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
208                         monster = spawnmonster(e, tospawn, mon.monsterid, NULL, NULL, e.origin, false, false, 2);
209                 else
210                 {
211                         delete(e);
212                         return;
213                 }
214         }
215         else // if spawnmob field falls through (unset), fallback to mon (relying on spawnmonster for that behaviour)
216                 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);
217
218         if(!monster)
219                 return;
220
221         monster.spawnshieldtime = time;
222
223         if(spawn_point)
224         {
225                 if(spawn_point.target_range)
226                         monster.target_range = spawn_point.target_range;
227                 monster.target2 = spawn_point.target2;
228         }
229
230         if(teamplay)
231         {
232                 if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
233                         monster.team = spawn_point.team;
234                 else
235                 {
236                         RandomSelection_Init();
237                         if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_AddFloat(NUM_TEAM_1, 1, 1);
238                         if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_AddFloat(NUM_TEAM_2, 1, 1);
239                         if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_AddFloat(NUM_TEAM_3, 1, 1); }
240                         if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_AddFloat(NUM_TEAM_4, 1, 1); }
241
242                         monster.team = RandomSelection_chosen_float;
243                 }
244
245                 monster_setupcolors(monster);
246
247                 if(monster.sprite)
248                 {
249                         WaypointSprite_UpdateTeamRadar(monster.sprite, RADARICON_DANGER, ((monster.team) ? Team_ColorRGB(monster.team) : '1 0 0'));
250
251                         monster.sprite.team = 0;
252                         monster.sprite.SendFlags |= 1;
253                 }
254         }
255
256         if(monster.monster_attack)
257                 IL_REMOVE(g_monster_targets, monster);
258         monster.monster_attack = false; // it's the player's job to kill all the monsters
259
260         if(inv_roundcnt >= inv_maxrounds)
261                 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
262 }
263
264 void invasion_SpawnMonsters(int supermonster_count)
265 {
266         Monster chosen_monster = invasion_PickMonster(supermonster_count);
267
268         invasion_SpawnChosenMonster(chosen_monster);
269 }
270
271 bool Invasion_CheckWinner()
272 {
273         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
274         {
275                 IL_EACH(g_monsters, true,
276                 {
277                         Monster_Remove(it);
278                 });
279                 IL_CLEAR(g_monsters);
280
281                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
282                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
283                 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
284                 return 1;
285         }
286
287         float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
288
289         IL_EACH(g_monsters, it.health > 0,
290         {
291                 if((get_monsterinfo(it.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
292                         ++supermonster_count;
293                 ++total_alive_monsters;
294
295                 if(teamplay)
296                 switch(it.team)
297                 {
298                         case NUM_TEAM_1: ++red_alive; break;
299                         case NUM_TEAM_2: ++blue_alive; break;
300                         case NUM_TEAM_3: ++yellow_alive; break;
301                         case NUM_TEAM_4: ++pink_alive; break;
302                 }
303         });
304
305         if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
306         {
307                 if(time >= inv_lastcheck)
308                 {
309                         invasion_SpawnMonsters(supermonster_count);
310                         inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
311                 }
312
313                 return 0;
314         }
315
316         if(inv_numspawned < 1)
317                 return 0; // nothing has spawned yet
318
319         if(teamplay)
320         {
321                 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
322                         return 0;
323         }
324         else if(inv_numkilled < inv_maxspawned)
325                 return 0;
326
327         entity winner = NULL;
328         float winning_score = 0, winner_team = 0;
329
330
331         if(teamplay)
332         {
333                 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
334                 if(blue_alive > 0)
335                 if(winner_team) { winner_team = 0; }
336                 else { winner_team = NUM_TEAM_2; }
337                 if(yellow_alive > 0)
338                 if(winner_team) { winner_team = 0; }
339                 else { winner_team = NUM_TEAM_3; }
340                 if(pink_alive > 0)
341                 if(winner_team) { winner_team = 0; }
342                 else { winner_team = NUM_TEAM_4; }
343         }
344         else
345         {
346                 FOREACH_CLIENT(IS_PLAYER(it), {
347                         float cs = PlayerScore_Add(it, SP_KILLS, 0);
348                         if(cs > winning_score)
349                         {
350                                 winning_score = cs;
351                                 winner = it;
352                         }
353                 });
354         }
355
356         IL_EACH(g_monsters, true,
357         {
358                 Monster_Remove(it);
359         });
360         IL_CLEAR(g_monsters);
361
362         if(teamplay)
363         {
364                 if(winner_team)
365                 {
366                         Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
367                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
368                 }
369         }
370         else if(winner)
371         {
372                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
373                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
374         }
375
376         round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
377
378         return 1;
379 }
380
381 bool Invasion_CheckPlayers()
382 {
383         return true;
384 }
385
386 void Invasion_RoundStart()
387 {
388         int numplayers = 0;
389         FOREACH_CLIENT(IS_PLAYER(it), {
390                 it.player_blocked = false;
391                 ++numplayers;
392         });
393
394         if(inv_roundcnt < inv_maxrounds)
395                 inv_roundcnt += 1; // a limiter to stop crazy counts
396
397         inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
398
399         inv_maxcurrent = 0;
400         inv_numspawned = 0;
401         inv_numkilled = 0;
402
403         inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
404
405         if(teamplay)
406         {
407                 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
408                 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
409                 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
410                 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
411                 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
412         }
413 }
414
415 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
416 {
417         entity frag_target = M_ARGV(0, entity);
418         entity frag_attacker = M_ARGV(1, entity);
419
420         if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
421         {
422                 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
423                 {
424                         inv_numkilled += 1;
425                         inv_maxcurrent -= 1;
426                 }
427                 if(teamplay) { inv_monsters_perteam[frag_target.team] -= 1; }
428
429                 if(IS_PLAYER(frag_attacker))
430                 if(SAME_TEAM(frag_attacker, frag_target)) // in non-teamplay modes, same team = same player, so this works
431                         PlayerScore_Add(frag_attacker, SP_KILLS, -1);
432                 else
433                 {
434                         PlayerScore_Add(frag_attacker, SP_KILLS, +1);
435                         if(teamplay)
436                                 TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
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, CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
548 {
549         M_ARGV(0, float) = invasion_teams;
550 }
551
552 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
553 {
554         M_ARGV(0, string) = "This command does not work during an invasion!";
555         return true;
556 }
557
558 void invasion_ScoreRules(int inv_teams)
559 {
560         if(inv_teams) { CheckAllowedTeams(NULL); }
561         ScoreRules_basics(inv_teams, 0, 0, false);
562         if(inv_teams) ScoreInfo_SetLabel_TeamScore(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
563         ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
564         ScoreRules_basics_end();
565 }
566
567 void invasion_DelayedInit(entity this) // Do this check with a delay so we can wait for teams to be set up.
568 {
569         if(autocvar_g_invasion_type == INV_TYPE_HUNT || autocvar_g_invasion_type == INV_TYPE_STAGE)
570                 cvar_set("fraglimit", "0");
571
572         if(autocvar_g_invasion_teams)
573         {
574                 invasion_teams = bound(2, autocvar_g_invasion_teams, 4);
575                 int teams = 0;
576                 if(invasion_teams >= 1) teams |= BIT(0);
577                 if(invasion_teams >= 2) teams |= BIT(1);
578                 if(invasion_teams >= 3) teams |= BIT(2);
579                 if(invasion_teams >= 4) teams |= BIT(3);
580
581                 invasion_teams = teams; // now set it?
582         }
583         else
584                 invasion_teams = 0;
585
586         independent_players = 1; // to disable extra useless scores
587
588         invasion_ScoreRules(invasion_teams);
589
590         independent_players = 0;
591
592         if(autocvar_g_invasion_type == INV_TYPE_ROUND)
593         {
594                 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
595                 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
596
597                 inv_roundcnt = 0;
598                 inv_maxrounds = 15; // 15?
599         }
600 }
601
602 void invasion_Initialize()
603 {
604         InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);
605 }