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