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