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