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