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