]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/invasion/sv_invasion.qc
Fix monsters spawning with random pitch when randomly placed in invasion
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / invasion / sv_invasion.qc
1 #include "sv_invasion.qh"
2
3 #include <common/monsters/sv_spawn.qh>
4 #include <common/monsters/sv_spawner.qh>
5 #include <common/monsters/sv_monsters.qh>
6
7 #include <server/teamplay.qh>
8
9 IntrusiveList g_invasion_roundends;
10 IntrusiveList g_invasion_waves;
11 IntrusiveList g_invasion_spawns;
12 STATIC_INIT(g_invasion)
13 {
14         g_invasion_roundends = IL_NEW();
15         g_invasion_waves = IL_NEW();
16         g_invasion_spawns = IL_NEW();
17 }
18
19 float autocvar_g_invasion_round_timelimit;
20 float autocvar_g_invasion_spawnpoint_spawn_delay;
21 float autocvar_g_invasion_warmup;
22 int autocvar_g_invasion_monster_count;
23 bool autocvar_g_invasion_zombies_only;
24 float autocvar_g_invasion_spawn_delay;
25
26 bool victent_present;
27 .bool inv_endreached;
28
29 bool inv_warning_shown; // spammy
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                 {
211                         monster = spawnmonster(e, tospawn, mon.monsterid, NULL, NULL, e.origin, false, false, 2);
212                         monster.angles_x = monster.angles_z = 0;
213                 }
214                 else
215                 {
216                         delete(e);
217                         return;
218                 }
219         }
220         else // if spawnmob field falls through (unset), fallback to mon (relying on spawnmonster for that behaviour)
221                 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);
222
223         if(!monster)
224                 return;
225
226         monster.spawnshieldtime = time;
227
228         if(spawn_point)
229         {
230                 if(spawn_point.target_range)
231                         monster.target_range = spawn_point.target_range;
232                 monster.target2 = spawn_point.target2;
233         }
234
235         if(teamplay)
236         {
237                 if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
238                         monster.team = spawn_point.team;
239                 else
240                 {
241                         RandomSelection_Init();
242                         if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_AddFloat(NUM_TEAM_1, 1, 1);
243                         if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_AddFloat(NUM_TEAM_2, 1, 1);
244                         if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_AddFloat(NUM_TEAM_3, 1, 1); }
245                         if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_AddFloat(NUM_TEAM_4, 1, 1); }
246
247                         monster.team = RandomSelection_chosen_float;
248                 }
249
250                 monster_setupcolors(monster);
251
252                 if(monster.sprite)
253                 {
254                         WaypointSprite_UpdateTeamRadar(monster.sprite, RADARICON_DANGER, ((monster.team) ? Team_ColorRGB(monster.team) : '1 0 0'));
255
256                         monster.sprite.team = 0;
257                         monster.sprite.SendFlags |= 1;
258                 }
259         }
260
261         if(monster.monster_attack)
262                 IL_REMOVE(g_monster_targets, monster);
263         monster.monster_attack = false; // it's the player's job to kill all the monsters
264
265         if(inv_roundcnt >= inv_maxrounds)
266                 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
267 }
268
269 void invasion_SpawnMonsters(int supermonster_count)
270 {
271         Monster chosen_monster = invasion_PickMonster(supermonster_count);
272
273         invasion_SpawnChosenMonster(chosen_monster);
274 }
275
276 bool Invasion_CheckWinner()
277 {
278         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
279         {
280                 IL_EACH(g_monsters, true,
281                 {
282                         Monster_Remove(it);
283                 });
284                 IL_CLEAR(g_monsters);
285
286                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
287                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
288                 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
289                 return 1;
290         }
291
292         float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
293
294         IL_EACH(g_monsters, GetResourceAmount(it, RESOURCE_HEALTH) > 0,
295         {
296                 if((get_monsterinfo(it.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
297                         ++supermonster_count;
298                 ++total_alive_monsters;
299
300                 if(teamplay)
301                 switch(it.team)
302                 {
303                         case NUM_TEAM_1: ++red_alive; break;
304                         case NUM_TEAM_2: ++blue_alive; break;
305                         case NUM_TEAM_3: ++yellow_alive; break;
306                         case NUM_TEAM_4: ++pink_alive; break;
307                 }
308         });
309
310         if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
311         {
312                 if(time >= inv_lastcheck)
313                 {
314                         invasion_SpawnMonsters(supermonster_count);
315                         inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
316                 }
317
318                 return 0;
319         }
320
321         if(inv_numspawned < 1)
322                 return 0; // nothing has spawned yet
323
324         if(teamplay)
325         {
326                 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
327                         return 0;
328         }
329         else if(inv_numkilled < inv_maxspawned)
330                 return 0;
331
332         entity winner = NULL;
333         float winning_score = 0, winner_team = 0;
334
335
336         if(teamplay)
337         {
338                 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
339                 if(blue_alive > 0)
340                 if(winner_team) { winner_team = 0; }
341                 else { winner_team = NUM_TEAM_2; }
342                 if(yellow_alive > 0)
343                 if(winner_team) { winner_team = 0; }
344                 else { winner_team = NUM_TEAM_3; }
345                 if(pink_alive > 0)
346                 if(winner_team) { winner_team = 0; }
347                 else { winner_team = NUM_TEAM_4; }
348         }
349         else
350         {
351                 FOREACH_CLIENT(IS_PLAYER(it), {
352                         float cs = GameRules_scoring_add(it, KILLS, 0);
353                         if(cs > winning_score)
354                         {
355                                 winning_score = cs;
356                                 winner = it;
357                         }
358                 });
359         }
360
361         IL_EACH(g_monsters, true,
362         {
363                 Monster_Remove(it);
364         });
365         IL_CLEAR(g_monsters);
366
367         if(teamplay)
368         {
369                 if(winner_team)
370                 {
371                         Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
372                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
373                 }
374         }
375         else if(winner)
376         {
377                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
378                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
379         }
380
381         round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
382
383         return 1;
384 }
385
386 bool Invasion_CheckPlayers()
387 {
388         return true;
389 }
390
391 void Invasion_RoundStart()
392 {
393         int numplayers = 0;
394         FOREACH_CLIENT(IS_PLAYER(it), {
395                 it.player_blocked = false;
396                 ++numplayers;
397         });
398
399         if(inv_roundcnt < inv_maxrounds)
400                 inv_roundcnt += 1; // a limiter to stop crazy counts
401
402         inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
403
404         inv_maxcurrent = 0;
405         inv_numspawned = 0;
406         inv_numkilled = 0;
407
408         inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
409
410         if(teamplay)
411         {
412                 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
413                 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
414                 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
415                 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
416                 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
417         }
418 }
419
420 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
421 {
422         entity frag_target = M_ARGV(0, entity);
423         entity frag_attacker = M_ARGV(1, entity);
424
425         if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
426         {
427                 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
428                 {
429                         inv_numkilled += 1;
430                         inv_maxcurrent -= 1;
431                 }
432                 if(teamplay) { inv_monsters_perteam[frag_target.team] -= 1; }
433
434                 if(IS_PLAYER(frag_attacker))
435                 if(SAME_TEAM(frag_attacker, frag_target)) // in non-teamplay modes, same team = same player, so this works
436                         GameRules_scoring_add(frag_attacker, KILLS, -1);
437                 else
438                 {
439                         GameRules_scoring_add(frag_attacker, KILLS, +1);
440                         if(teamplay)
441                                 TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
442                 }
443         }
444 }
445
446 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
447 {
448         entity mon = M_ARGV(0, entity);
449         mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
450
451         if(autocvar_g_invasion_type == INV_TYPE_HUNT)
452                 return false; // allowed
453
454         if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
455                 return true;
456
457         if(!(mon.spawnflags & MONSTERFLAG_RESPAWNED))
458         {
459                 inv_numspawned += 1;
460                 inv_maxcurrent += 1;
461         }
462
463         mon.monster_skill = inv_monsterskill;
464
465         if((get_monsterinfo(mon.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
466                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
467 }
468
469 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
470 {
471         if(autocvar_g_invasion_type != INV_TYPE_ROUND)
472                 return; // uses map spawned monsters
473
474         monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
475         monsters_killed = inv_numkilled;
476 }
477
478 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
479 {
480         // no regeneration in invasion, regardless of the game type
481         return true;
482 }
483
484 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
485 {
486         entity player = M_ARGV(0, entity);
487
488         if(player.bot_attack)
489                 IL_REMOVE(g_bot_targets, player);
490         player.bot_attack = false;
491 }
492
493 MUTATOR_HOOKFUNCTION(inv, Damage_Calculate)
494 {
495         entity frag_attacker = M_ARGV(1, entity);
496         entity frag_target = M_ARGV(2, entity);
497         float frag_damage = M_ARGV(4, float);
498         vector frag_force = M_ARGV(6, vector);
499
500         if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
501         {
502                 frag_damage = 0;
503                 frag_force = '0 0 0';
504
505                 M_ARGV(4, float) = frag_damage;
506                 M_ARGV(6, vector) = frag_force;
507         }
508 }
509
510 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
511 {
512         entity targ = M_ARGV(1, entity);
513
514         if(!IS_MONSTER(targ))
515                 return true;
516 }
517
518 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
519 {
520         if(autocvar_g_invasion_type == INV_TYPE_ROUND)
521         {
522                 start_health = 200;
523                 start_armorvalue = 200;
524         }
525 }
526
527 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
528 {
529         entity frag_target = M_ARGV(1, entity);
530
531         if(IS_MONSTER(frag_target))
532                 return MUT_ACCADD_INVALID;
533         return MUT_ACCADD_INDIFFERENT;
534 }
535
536 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
537 {
538         // monster spawning disabled during an invasion
539         M_ARGV(1, string) = "You cannot spawn monsters during an invasion!";
540         return true;
541 }
542
543 MUTATOR_HOOKFUNCTION(inv, CheckRules_World)
544 {
545         if(autocvar_g_invasion_type == INV_TYPE_ROUND)
546                 return false;
547
548         M_ARGV(0, float) = WinningCondition_Invasion();
549         return true;
550 }
551
552 MUTATOR_HOOKFUNCTION(inv, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
553 {
554         M_ARGV(0, float) = invasion_teams;
555         return true;
556 }
557
558 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
559 {
560         M_ARGV(0, string) = "This command does not work during an invasion!";
561         return true;
562 }
563
564 void invasion_ScoreRules(int inv_teams)
565 {
566         GameRules_score_enabled(false);
567         GameRules_scoring(inv_teams, 0, 0, {
568             if (inv_teams) {
569             field_team(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
570             }
571             field(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
572         });
573 }
574
575 void invasion_DelayedInit(entity this) // Do this check with a delay so we can wait for teams to be set up.
576 {
577         if(autocvar_g_invasion_type == INV_TYPE_HUNT || autocvar_g_invasion_type == INV_TYPE_STAGE)
578                 cvar_set("fraglimit", "0");
579
580         if(autocvar_g_invasion_teams)
581         {
582                 invasion_teams = BITS(bound(2, autocvar_g_invasion_teams, 4));
583         }
584         else
585                 invasion_teams = 0;
586
587         independent_players = 1; // to disable extra useless scores
588
589         invasion_ScoreRules(invasion_teams);
590
591         independent_players = 0;
592
593         if(autocvar_g_invasion_type == INV_TYPE_ROUND)
594         {
595                 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
596                 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
597
598                 inv_roundcnt = 0;
599                 inv_maxrounds = 15; // 15?
600         }
601 }
602
603 void invasion_Initialize()
604 {
605         InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);
606 }