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