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