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