]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/invasion/sv_invasion.qc
Merge branch 'master' into terencehill/less_entities
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / invasion / sv_invasion.qc
1 #include "sv_invasion.qh"
2
3 #include <common/mapobjects/triggers.qh>
4 #include <common/monsters/sv_spawn.qh>
5 #include <common/monsters/sv_spawner.qh>
6 #include <common/monsters/sv_monsters.qh>
7
8 #include <server/bot/api.qh>
9 #include <server/world.qh>
10 #include <server/teamplay.qh>
11
12 float autocvar_g_invasion_round_timelimit;
13 float autocvar_g_invasion_spawnpoint_spawn_delay;
14 float autocvar_g_invasion_warmup;
15 int autocvar_g_invasion_monster_count;
16 bool autocvar_g_invasion_zombies_only;
17 float autocvar_g_invasion_spawn_delay;
18
19 bool victent_present;
20 .bool inv_endreached;
21
22 bool inv_warning_shown; // spammy
23
24 void target_invasion_roundend_use(entity this, entity actor, entity trigger)
25 {
26         if(!IS_PLAYER(actor)) { return; }
27
28         actor.inv_endreached = true;
29
30         int plnum = 0;
31         int realplnum = 0;
32         // let's not count bots
33         FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
34                 ++realplnum;
35                 if(it.inv_endreached)
36                         ++plnum;
37         });
38         if(plnum < ceil(realplnum * min(1, this.count))) // 70% of players
39                 return;
40
41         this.winning = true;
42 }
43
44 spawnfunc(target_invasion_roundend)
45 {
46         if(!g_invasion) { delete(this); return; }
47
48         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)
49
50         if(!this.count) { this.count = 0.7; } // require at least 70% of the players to reach the end before triggering victory
51
52         this.use = target_invasion_roundend_use;
53
54         IL_PUSH(g_invasion_roundends, this);
55 }
56
57 spawnfunc(invasion_wave)
58 {
59         if(!g_invasion) { delete(this); return; }
60
61         IL_PUSH(g_invasion_waves, this);
62 }
63
64 spawnfunc(invasion_spawnpoint)
65 {
66         if(!g_invasion) { delete(this); return; }
67
68         IL_PUSH(g_invasion_spawns, this);
69 }
70
71 void ClearWinners();
72
73 // Invasion stage mode winning condition: If the attackers triggered a round end (by fulfilling all objectives)
74 // they win.
75 int WinningCondition_Invasion()
76 {
77         WinningConditionHelper(NULL); // set worldstatus
78
79         int status = WINNING_NO;
80
81         if(autocvar_g_invasion_type == INV_TYPE_STAGE)
82         {
83                 SetWinners(inv_endreached, true);
84
85                 int found = 0;
86                 IL_EACH(g_invasion_roundends, true,
87                 {
88                         ++found;
89                         if(it.winning)
90                         {
91                                 bprint("Invasion: round completed.\n");
92                                 // winners already set (TODO: teamplay support)
93
94                                 status = WINNING_YES;
95                                 break;
96                         }
97                 });
98
99                 if(!found)
100                         status = WINNING_YES; // just end it? TODO: should warn mapper!
101         }
102         else if(autocvar_g_invasion_type == INV_TYPE_HUNT)
103         {
104                 ClearWinners();
105
106                 int found = 0; // NOTE: this ends the round if no monsters are placed
107                 IL_EACH(g_monsters, !(it.spawnflags & MONSTERFLAG_RESPAWNED),
108                 {
109                         ++found;
110                 });
111
112                 if(found <= 0)
113                 {
114                         FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it),
115                         {
116                                 it.winning = true;
117                         });
118                         status = WINNING_YES;
119                 }
120         }
121
122         return status;
123 }
124
125 Monster invasion_PickMonster(int supermonster_count)
126 {
127         RandomSelection_Init();
128
129         FOREACH(Monsters, it != MON_Null,
130         {
131                 if((it.spawnflags & MON_FLAG_HIDDEN) || (it.spawnflags & MONSTER_TYPE_PASSIVE) || (it.spawnflags & MONSTER_TYPE_FLY) || (it.spawnflags & MONSTER_TYPE_SWIM) ||
132                         (it.spawnflags & MONSTER_SIZE_QUAKE) || ((it.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
133                         continue;
134                 if(autocvar_g_invasion_zombies_only && !(it.spawnflags & MONSTER_TYPE_UNDEAD))
135                         continue;
136         RandomSelection_AddEnt(it, 1, 1);
137         });
138
139         return RandomSelection_chosen_ent;
140 }
141
142 entity invasion_PickSpawn()
143 {
144         RandomSelection_Init();
145
146         IL_EACH(g_invasion_spawns, true,
147         {
148                 RandomSelection_AddEnt(it, 1, ((time < it.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
149                 it.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
150         });
151
152         return RandomSelection_chosen_ent;
153 }
154
155 entity invasion_GetWaveEntity(int wavenum)
156 {
157         IL_EACH(g_invasion_waves, it.cnt == wavenum,
158         {
159                 return it; // found one
160         });
161
162         // if no specific one is found, find the last existing wave ent
163         entity best = NULL;
164         IL_EACH(g_invasion_waves, it.cnt <= wavenum,
165         {
166                 if(!best || it.cnt > best.cnt)
167                         best = it;
168         });
169
170         return best;
171 }
172
173 void invasion_SpawnChosenMonster(Monster mon)
174 {
175         entity monster;
176         entity spawn_point = invasion_PickSpawn();
177         entity wave_ent = invasion_GetWaveEntity(inv_roundcnt);
178
179         string tospawn = "";
180         if(wave_ent && wave_ent.spawnmob && wave_ent.spawnmob != "")
181         {
182                 RandomSelection_Init();
183                 FOREACH_WORD(wave_ent.spawnmob, true,
184                 {
185                         RandomSelection_AddString(it, 1, 1);
186                 });
187
188                 tospawn = RandomSelection_chosen_string;
189         }
190
191         if(spawn_point == NULL)
192         {
193                 if(!inv_warning_shown)
194                 {
195                         inv_warning_shown = true;
196                         LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations");
197                 }
198                 entity e = spawn();
199                 setsize(e, mon.m_mins, mon.m_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, 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, 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         if(monster.monster_attack)
251                 IL_REMOVE(g_monster_targets, monster);
252         monster.monster_attack = false; // it's the player's job to kill all the monsters
253
254         if(inv_roundcnt >= inv_maxrounds)
255                 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
256 }
257
258 void invasion_SpawnMonsters(int supermonster_count)
259 {
260         Monster chosen_monster = invasion_PickMonster(supermonster_count);
261
262         invasion_SpawnChosenMonster(chosen_monster);
263 }
264
265 bool Invasion_CheckWinner()
266 {
267         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
268         {
269                 IL_EACH(g_monsters, true,
270                 {
271                         Monster_Remove(it);
272                 });
273                 IL_CLEAR(g_monsters);
274
275                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
276                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
277                 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
278                 return 1;
279         }
280
281         float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
282
283         IL_EACH(g_monsters, GetResource(it, RES_HEALTH) > 0,
284         {
285                 if(it.monsterdef.spawnflags & MON_FLAG_SUPERMONSTER)
286                         ++supermonster_count;
287                 ++total_alive_monsters;
288
289                 if(teamplay)
290                 switch(it.team)
291                 {
292                         case NUM_TEAM_1: ++red_alive; break;
293                         case NUM_TEAM_2: ++blue_alive; break;
294                         case NUM_TEAM_3: ++yellow_alive; break;
295                         case NUM_TEAM_4: ++pink_alive; break;
296                 }
297         });
298
299         if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
300         {
301                 if(time >= inv_lastcheck)
302                 {
303                         invasion_SpawnMonsters(supermonster_count);
304                         inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
305                 }
306
307                 return 0;
308         }
309
310         if(inv_numspawned < 1)
311                 return 0; // nothing has spawned yet
312
313         if(teamplay)
314         {
315                 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
316                         return 0;
317         }
318         else if(inv_numkilled < inv_maxspawned)
319                 return 0;
320
321         entity winner = NULL;
322         float winning_score = 0, winner_team = 0;
323
324
325         if(teamplay)
326         {
327                 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
328                 if(blue_alive > 0)
329                 {
330                         if(winner_team) { winner_team = 0; }
331                         else { winner_team = NUM_TEAM_2; }
332                 }
333                 if(yellow_alive > 0)
334                 {
335                         if(winner_team) { winner_team = 0; }
336                         else { winner_team = NUM_TEAM_3; }
337                 }
338                 if(pink_alive > 0)
339                 {
340                         if(winner_team) { winner_team = 0; }
341                         else { winner_team = NUM_TEAM_4; }
342                 }
343         }
344         else
345         {
346                 FOREACH_CLIENT(IS_PLAYER(it), {
347                         float cs = GameRules_scoring_add(it, 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                 {
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
443 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
444 {
445         entity mon = M_ARGV(0, entity);
446         mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
447
448         if(autocvar_g_invasion_type == INV_TYPE_HUNT)
449                 return false; // allowed
450
451         if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
452                 return true;
453
454         if(!(mon.spawnflags & MONSTERFLAG_RESPAWNED))
455         {
456                 inv_numspawned += 1;
457                 inv_maxcurrent += 1;
458         }
459
460         mon.monster_skill = inv_monsterskill;
461
462         if(mon.monsterdef.spawnflags & MON_FLAG_SUPERMONSTER)
463                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
464 }
465
466 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
467 {
468         if(autocvar_g_invasion_type != INV_TYPE_ROUND)
469                 return; // uses map spawned monsters
470
471         monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
472         monsters_killed = inv_numkilled;
473 }
474
475 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
476 {
477         // no regeneration in invasion, regardless of the game type
478         return true;
479 }
480
481 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
482 {
483         entity player = M_ARGV(0, entity);
484
485         if(player.bot_attack)
486                 IL_REMOVE(g_bot_targets, player);
487         player.bot_attack = false;
488 }
489
490 MUTATOR_HOOKFUNCTION(inv, Damage_Calculate)
491 {
492         entity frag_attacker = M_ARGV(1, entity);
493         entity frag_target = M_ARGV(2, entity);
494         float frag_damage = M_ARGV(4, float);
495         vector frag_force = M_ARGV(6, vector);
496
497         if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
498         {
499                 frag_damage = 0;
500                 frag_force = '0 0 0';
501
502                 M_ARGV(4, float) = frag_damage;
503                 M_ARGV(6, vector) = frag_force;
504         }
505 }
506
507 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
508 {
509         entity targ = M_ARGV(1, entity);
510
511         if(!IS_MONSTER(targ))
512                 return true;
513 }
514
515 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
516 {
517         if(autocvar_g_invasion_type == INV_TYPE_ROUND)
518         {
519                 start_health = 200;
520                 start_armorvalue = 200;
521         }
522 }
523
524 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
525 {
526         entity frag_target = M_ARGV(1, entity);
527
528         if(IS_MONSTER(frag_target))
529                 return MUT_ACCADD_INVALID;
530         return MUT_ACCADD_INDIFFERENT;
531 }
532
533 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
534 {
535         // monster spawning disabled during an invasion
536         M_ARGV(1, string) = "You cannot spawn monsters during an invasion!";
537         return true;
538 }
539
540 MUTATOR_HOOKFUNCTION(inv, CheckRules_World)
541 {
542         if(autocvar_g_invasion_type == INV_TYPE_ROUND)
543                 return false;
544
545         M_ARGV(0, float) = WinningCondition_Invasion();
546         return true;
547 }
548
549 MUTATOR_HOOKFUNCTION(inv, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
550 {
551         M_ARGV(0, float) = invasion_teams;
552         return true;
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         GameRules_score_enabled(false);
564         GameRules_scoring(inv_teams, 0, 0, {
565             if (inv_teams) {
566             field_team(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
567             }
568             field(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
569         });
570 }
571
572 void invasion_DelayedInit(entity this) // Do this check with a delay so we can wait for teams to be set up.
573 {
574         if(autocvar_g_invasion_type == INV_TYPE_HUNT || autocvar_g_invasion_type == INV_TYPE_STAGE)
575                 cvar_set("fraglimit", "0");
576
577         if(autocvar_g_invasion_teams)
578         {
579                 invasion_teams = BITS(bound(2, autocvar_g_invasion_teams, 4));
580         }
581         else
582                 invasion_teams = 0;
583
584         independent_players = 1; // to disable extra useless scores
585
586         invasion_ScoreRules(invasion_teams);
587
588         independent_players = 0;
589
590         if(autocvar_g_invasion_type == INV_TYPE_ROUND)
591         {
592                 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
593                 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
594
595                 inv_roundcnt = 0;
596                 inv_maxrounds = 15; // 15?
597         }
598 }
599
600 void invasion_Initialize()
601 {
602         InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);
603 }