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