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