]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/invasion/sv_invasion.qc
Merge branch 'master' into Mario/monsters
[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 #include <server/bot/api.qh>
8 #include <server/world.qh>
9 #include <server/teamplay.qh>
10
11 IntrusiveList g_invasion_roundends;
12 IntrusiveList g_invasion_waves;
13 IntrusiveList g_invasion_spawns;
14 STATIC_INIT(g_invasion)
15 {
16         g_invasion_roundends = IL_NEW();
17         g_invasion_waves = IL_NEW();
18         g_invasion_spawns = IL_NEW();
19 }
20
21 float autocvar_g_invasion_round_timelimit;
22 float autocvar_g_invasion_spawnpoint_spawn_delay;
23 float autocvar_g_invasion_warmup;
24 int autocvar_g_invasion_monster_count;
25 bool autocvar_g_invasion_zombies_only;
26 float autocvar_g_invasion_spawn_delay;
27
28 bool victent_present;
29 .bool inv_endreached;
30
31 bool inv_warning_shown; // spammy
32
33 void target_invasion_roundend_use(entity this, entity actor, entity trigger)
34 {
35         if(!IS_PLAYER(actor)) { return; }
36
37         actor.inv_endreached = true;
38
39         int plnum = 0;
40         int realplnum = 0;
41         // let's not count bots
42         FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
43                 ++realplnum;
44                 if(it.inv_endreached)
45                         ++plnum;
46         });
47         if(plnum < ceil(realplnum * min(1, this.count))) // 70% of players
48                 return;
49
50         this.winning = true;
51 }
52
53 spawnfunc(target_invasion_roundend)
54 {
55         if(!g_invasion) { delete(this); return; }
56
57         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)
58
59         if(!this.count) { this.count = 0.7; } // require at least 70% of the players to reach the end before triggering victory
60
61         this.use = target_invasion_roundend_use;
62
63         IL_PUSH(g_invasion_roundends, this);
64 }
65
66 spawnfunc(invasion_wave)
67 {
68         if(!g_invasion) { delete(this); return; }
69
70         IL_PUSH(g_invasion_waves, this);
71 }
72
73 spawnfunc(invasion_spawnpoint)
74 {
75         if(!g_invasion) { delete(this); return; }
76
77         IL_PUSH(g_invasion_spawns, this);
78 }
79
80 void ClearWinners();
81
82 // Invasion stage mode winning condition: If the attackers triggered a round end (by fulfilling all objectives)
83 // they win.
84 int WinningCondition_Invasion()
85 {
86         WinningConditionHelper(NULL); // set worldstatus
87
88         int status = WINNING_NO;
89
90         if(autocvar_g_invasion_type == INV_TYPE_STAGE)
91         {
92                 SetWinners(inv_endreached, true);
93
94                 int found = 0;
95                 IL_EACH(g_invasion_roundends, true,
96                 {
97                         ++found;
98                         if(it.winning)
99                         {
100                                 bprint("Invasion: round completed.\n");
101                                 // winners already set
102
103                                 status = WINNING_YES;
104                                 break;
105                         }
106                 });
107
108                 if(!found)
109                         status = WINNING_YES; // just end it? TODO: should warn mapper!
110         }
111         else if(autocvar_g_invasion_type == INV_TYPE_HUNT)
112         {
113                 ClearWinners();
114
115                 int found = 0; // NOTE: this ends the round if no monsters are placed
116                 IL_EACH(g_monsters, !(it.spawnflags & MONSTERFLAG_RESPAWNED),
117                 {
118                         ++found;
119                 });
120
121                 if(found <= 0)
122                 {
123                         FOREACH_CLIENT(IS_PLAYER(it) && !IS_DEAD(it),
124                         {
125                                 it.winning = true;
126                         });
127                         status = WINNING_YES;
128                 }
129         }
130
131         return status;
132 }
133
134 Monster invasion_PickMonster(int supermonster_count)
135 {
136         RandomSelection_Init();
137
138         FOREACH(Monsters, it != MON_Null,
139         {
140                 if((it.spawnflags & MON_FLAG_HIDDEN) || (it.spawnflags & MONSTER_TYPE_PASSIVE) || (it.spawnflags & MONSTER_TYPE_FLY) || (it.spawnflags & MONSTER_TYPE_SWIM)
141                         || (it.spawnflags & MONSTER_SIZE_QUAKE) || ((it.spawnflags & MON_FLAG_SUPERMONSTER) && supermonster_count >= 1))
142                         continue;
143                 if(autocvar_g_invasion_zombies_only && !(it.spawnflags & MONSTER_TYPE_UNDEAD))
144                         continue;
145         RandomSelection_AddEnt(it, 1, 1);
146         });
147
148         return RandomSelection_chosen_ent;
149 }
150
151 entity invasion_PickSpawn()
152 {
153         RandomSelection_Init();
154
155         IL_EACH(g_invasion_spawns, true,
156         {
157                 RandomSelection_AddEnt(it, 1, ((time < it.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
158                 it.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
159         });
160
161         return RandomSelection_chosen_ent;
162 }
163
164 entity invasion_GetWaveEntity(int wavenum)
165 {
166         IL_EACH(g_invasion_waves, it.cnt == wavenum,
167         {
168                 return it; // found one
169         });
170
171         // if no specific one is found, find the last existing wave ent
172         entity best = NULL;
173         IL_EACH(g_invasion_waves, it.cnt <= wavenum,
174         {
175                 if(!best || it.cnt > best.cnt)
176                         best = it;
177         });
178
179         return best;
180 }
181
182 void invasion_SpawnChosenMonster(Monster mon)
183 {
184         entity monster;
185         entity spawn_point = invasion_PickSpawn();
186         entity wave_ent = invasion_GetWaveEntity(inv_roundcnt);
187
188         string tospawn = "";
189         if(wave_ent && wave_ent.spawnmob && wave_ent.spawnmob != "")
190         {
191                 RandomSelection_Init();
192                 FOREACH_WORD(wave_ent.spawnmob, true,
193                 {
194                         RandomSelection_AddString(it, 1, 1);
195                 });
196
197                 tospawn = RandomSelection_chosen_string;
198         }
199
200         if(spawn_point == NULL)
201         {
202                 if(!inv_warning_shown)
203                 {
204                         inv_warning_shown = true;
205                         LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations");
206                 }
207                 entity e = spawn();
208                 setsize(e, mon.m_mins, mon.m_maxs);
209
210                 if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
211                 {
212                         monster = spawnmonster(e, tospawn, mon, NULL, NULL, e.origin, false, false, 2);
213                         monster.angles_x = monster.angles_z = 0;
214                 }
215                 else
216                 {
217                         delete(e);
218                         return;
219                 }
220         }
221         else // if spawnmob field falls through (unset), fallback to mon (relying on spawnmonster for that behaviour)
222                 monster = spawnmonster(spawn(), ((spawn_point.spawnmob && spawn_point.spawnmob != "") ? spawn_point.spawnmob : tospawn), mon, spawn_point, spawn_point, spawn_point.origin, false, false, 2);
223
224         if(!monster)
225                 return;
226
227         monster.spawnshieldtime = time;
228
229         if(spawn_point)
230         {
231                 if(spawn_point.target_range)
232                         monster.target_range = spawn_point.target_range;
233                 monster.target2 = spawn_point.target2;
234         }
235
236         if(monster.monster_attack)
237                 IL_REMOVE(g_monster_targets, monster);
238         monster.monster_attack = false; // it's the player's job to kill all the monsters
239
240         if(inv_roundcnt >= inv_maxrounds)
241                 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
242 }
243
244 void invasion_SpawnMonsters(int supermonster_count)
245 {
246         Monster chosen_monster = invasion_PickMonster(supermonster_count);
247
248         invasion_SpawnChosenMonster(chosen_monster);
249 }
250
251 bool Invasion_CheckWinner()
252 {
253         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
254         {
255                 IL_EACH(g_monsters, true,
256                 {
257                         Monster_Remove(it);
258                 });
259                 IL_CLEAR(g_monsters);
260
261                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
262                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
263                 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
264                 return 1;
265         }
266
267         float total_alive_monsters = 0, supermonster_count = 0;
268
269         IL_EACH(g_monsters, GetResource(it, RES_HEALTH) > 0,
270         {
271                 if(it.monsterdef.spawnflags & MON_FLAG_SUPERMONSTER)
272                         ++supermonster_count;
273                 ++total_alive_monsters;
274         });
275
276         if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
277         {
278                 if(time >= inv_lastcheck)
279                 {
280                         invasion_SpawnMonsters(supermonster_count);
281                         inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
282                 }
283
284                 return 0;
285         }
286
287         if(inv_numspawned < 1)
288                 return 0; // nothing has spawned yet
289
290         if(inv_numkilled < inv_maxspawned)
291                 return 0;
292
293         entity winner = NULL;
294         float winning_score = 0;
295
296         FOREACH_CLIENT(IS_PLAYER(it), {
297                 float cs = GameRules_scoring_add(it, KILLS, 0);
298                 if(cs > winning_score)
299                 {
300                         winning_score = cs;
301                         winner = it;
302                 }
303         });
304
305         IL_EACH(g_monsters, true,
306         {
307                 Monster_Remove(it);
308         });
309         IL_CLEAR(g_monsters);
310
311         if(winner)
312         {
313                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
314                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
315         }
316
317         round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
318
319         return 1;
320 }
321
322 bool Invasion_CheckPlayers()
323 {
324         return true;
325 }
326
327 void Invasion_RoundStart()
328 {
329         int numplayers = 0;
330         FOREACH_CLIENT(IS_PLAYER(it), {
331                 it.player_blocked = false;
332                 ++numplayers;
333         });
334
335         if(inv_roundcnt < inv_maxrounds)
336                 inv_roundcnt += 1; // a limiter to stop crazy counts
337
338         inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
339
340         inv_maxcurrent = 0;
341         inv_numspawned = 0;
342         inv_numkilled = 0;
343
344         inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
345 }
346
347 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
348 {
349         entity frag_target = M_ARGV(0, entity);
350         entity frag_attacker = M_ARGV(1, entity);
351
352         if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
353         {
354                 if(autocvar_g_invasion_type == INV_TYPE_ROUND)
355                 {
356                         inv_numkilled += 1;
357                         inv_maxcurrent -= 1;
358                 }
359
360                 if(IS_PLAYER(frag_attacker))
361                 {
362                         if(SAME_TEAM(frag_attacker, frag_target))
363                                 GameRules_scoring_add(frag_attacker, KILLS, -1);
364                         else
365                                 GameRules_scoring_add(frag_attacker, KILLS, +1);
366                 }
367         }
368 }
369
370 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
371 {
372         entity mon = M_ARGV(0, entity);
373         mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
374
375         if(autocvar_g_invasion_type == INV_TYPE_HUNT)
376                 return false; // allowed
377
378         if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
379                 return true;
380
381         if(!(mon.spawnflags & MONSTERFLAG_RESPAWNED))
382         {
383                 inv_numspawned += 1;
384                 inv_maxcurrent += 1;
385         }
386
387         mon.monster_skill = inv_monsterskill;
388
389         if(mon.monsterdef.spawnflags & MON_FLAG_SUPERMONSTER)
390                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
391 }
392
393 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
394 {
395         if(autocvar_g_invasion_type != INV_TYPE_ROUND)
396                 return; // uses map spawned monsters
397
398         monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
399         monsters_killed = inv_numkilled;
400 }
401
402 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
403 {
404         // no regeneration in invasion, regardless of the game type
405         return true;
406 }
407
408 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
409 {
410         entity player = M_ARGV(0, entity);
411
412         if(player.bot_attack)
413                 IL_REMOVE(g_bot_targets, player);
414         player.bot_attack = false;
415 }
416
417 MUTATOR_HOOKFUNCTION(inv, Damage_Calculate)
418 {
419         entity frag_attacker = M_ARGV(1, entity);
420         entity frag_target = M_ARGV(2, entity);
421         float frag_damage = M_ARGV(4, float);
422         vector frag_force = M_ARGV(6, vector);
423
424         if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
425         {
426                 frag_damage = 0;
427                 frag_force = '0 0 0';
428
429                 M_ARGV(4, float) = frag_damage;
430                 M_ARGV(6, vector) = frag_force;
431         }
432 }
433
434 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
435 {
436         entity targ = M_ARGV(1, entity);
437
438         if(!IS_MONSTER(targ))
439                 return true;
440 }
441
442 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
443 {
444         if(autocvar_g_invasion_type == INV_TYPE_ROUND)
445         {
446                 start_health = 200;
447                 start_armorvalue = 200;
448         }
449 }
450
451 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
452 {
453         entity frag_target = M_ARGV(1, entity);
454
455         if(IS_MONSTER(frag_target))
456                 return MUT_ACCADD_INVALID;
457         return MUT_ACCADD_INDIFFERENT;
458 }
459
460 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
461 {
462         // monster spawning disabled during an invasion
463         M_ARGV(1, string) = "You cannot spawn monsters during an invasion!";
464         return true;
465 }
466
467 MUTATOR_HOOKFUNCTION(inv, CheckRules_World)
468 {
469         if(autocvar_g_invasion_type == INV_TYPE_ROUND)
470                 return false;
471
472         M_ARGV(0, float) = WinningCondition_Invasion();
473         return true;
474 }
475
476 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
477 {
478         M_ARGV(0, string) = "This command does not work during an invasion!";
479         return true;
480 }
481
482 void invasion_ScoreRules()
483 {
484         GameRules_score_enabled(false);
485         GameRules_scoring(0, 0, 0, {
486             field(SP_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
487         });
488 }
489
490 void invasion_DelayedInit(entity this)
491 {
492         if(autocvar_g_invasion_type == INV_TYPE_HUNT || autocvar_g_invasion_type == INV_TYPE_STAGE)
493                 cvar_set("fraglimit", "0");
494
495         independent_players = 1; // to disable extra useless scores
496
497         invasion_ScoreRules();
498
499         independent_players = 0;
500
501         if(autocvar_g_invasion_type == INV_TYPE_ROUND)
502         {
503                 round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
504                 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
505
506                 inv_roundcnt = 0;
507                 inv_maxrounds = 15; // 15?
508         }
509 }
510
511 void invasion_Initialize()
512 {
513         InitializeEntity(NULL, invasion_DelayedInit, INITPRIO_GAMETYPE);
514 }