]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_invasion.qc
Kill the ret_string global
[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(world, i, string_null, 1, 1);
108         }
109
110         return RandomSelection_chosen_float;
111 }
112
113 entity invasion_PickSpawn()
114 {
115         entity e;
116
117         RandomSelection_Init();
118
119         for(e = world;(e = find(e, classname, "invasion_spawnpoint")); )
120         {
121                 RandomSelection_Add(e, 0, string_null, 1, ((time >= e.spawnshieldtime) ? 0.2 : 1)); // give recently used spawnpoints a very low rating
122                 e.spawnshieldtime = time + autocvar_g_invasion_spawnpoint_spawn_delay;
123         }
124
125         return RandomSelection_chosen_ent;
126 }
127
128 void invasion_SpawnChosenMonster(float mon)
129 {
130         entity spawn_point, monster;
131
132         spawn_point = invasion_PickSpawn();
133
134         if(spawn_point == world)
135         {
136                 LOG_TRACE("Warning: couldn't find any invasion_spawnpoint spawnpoints, attempting to spawn monsters in random locations\n");
137                 entity e = spawn();
138                 setsize(e, (get_monsterinfo(mon)).mins, (get_monsterinfo(mon)).maxs);
139
140                 if(MoveToRandomMapLocation(e, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
141                         monster = spawnmonster("", mon, world, world, e.origin, false, false, 2);
142                 else return;
143
144                 setthink(e, SUB_Remove);
145                 e.nextthink = time + 0.1;
146         }
147         else
148                 monster = spawnmonster("", ((spawn_point.monsterid) ? spawn_point.monsterid : mon), spawn_point, spawn_point, spawn_point.origin, false, false, 2);
149
150         if(spawn_point) monster.target2 = spawn_point.target2;
151         monster.spawnshieldtime = time;
152         if(spawn_point && spawn_point.target_range) monster.target_range = spawn_point.target_range;
153
154         if(teamplay)
155         if(spawn_point && spawn_point.team && inv_monsters_perteam[spawn_point.team] > 0)
156                 monster.team = spawn_point.team;
157         else
158         {
159                 RandomSelection_Init();
160                 if(inv_monsters_perteam[NUM_TEAM_1] > 0) RandomSelection_Add(world, NUM_TEAM_1, string_null, 1, 1);
161                 if(inv_monsters_perteam[NUM_TEAM_2] > 0) RandomSelection_Add(world, NUM_TEAM_2, string_null, 1, 1);
162                 if(invasion_teams >= 3) if(inv_monsters_perteam[NUM_TEAM_3] > 0) { RandomSelection_Add(world, NUM_TEAM_3, string_null, 1, 1); }
163                 if(invasion_teams >= 4) if(inv_monsters_perteam[NUM_TEAM_4] > 0) { RandomSelection_Add(world, NUM_TEAM_4, string_null, 1, 1); }
164
165                 monster.team = RandomSelection_chosen_float;
166         }
167
168         if(teamplay)
169         {
170                 monster_setupcolors(monster);
171
172                 if(monster.sprite)
173                 {
174                         WaypointSprite_UpdateTeamRadar(monster.sprite, RADARICON_DANGER, ((monster.team) ? Team_ColorRGB(monster.team) : '1 0 0'));
175
176                         monster.sprite.team = 0;
177                         monster.sprite.SendFlags |= 1;
178                 }
179         }
180
181         monster.monster_attack = false; // it's the player's job to kill all the monsters
182
183         if(inv_roundcnt >= inv_maxrounds)
184                 monster.spawnflags |= MONSTERFLAG_MINIBOSS; // last round spawns minibosses
185 }
186
187 void invasion_SpawnMonsters(float supermonster_count)
188 {
189         float chosen_monster = invasion_PickMonster(supermonster_count);
190
191         invasion_SpawnChosenMonster(chosen_monster);
192 }
193
194 float Invasion_CheckWinner()
195 {
196         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
197         {
198                 FOREACH_ENTITY_FLAGS(flags, FL_MONSTER, LAMBDA(Monster_Remove(it)));
199
200                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_OVER);
201                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_OVER);
202                 round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
203                 return 1;
204         }
205
206         float total_alive_monsters = 0, supermonster_count = 0, red_alive = 0, blue_alive = 0, yellow_alive = 0, pink_alive = 0;
207
208         FOREACH_ENTITY_FLAGS(flags, FL_MONSTER, LAMBDA(
209                 if(it.health > 0)
210                 {
211                         if((get_monsterinfo(it.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
212                                 ++supermonster_count;
213                         ++total_alive_monsters;
214
215                         if(teamplay)
216                         switch(it.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
226         if((total_alive_monsters + inv_numkilled) < inv_maxspawned && inv_maxcurrent < inv_maxspawned)
227         {
228                 if(time >= inv_lastcheck)
229                 {
230                         invasion_SpawnMonsters(supermonster_count);
231                         inv_lastcheck = time + autocvar_g_invasion_spawn_delay;
232                 }
233
234                 return 0;
235         }
236
237         if(inv_numspawned < 1)
238                 return 0; // nothing has spawned yet
239
240         if(teamplay)
241         {
242                 if(((red_alive > 0) + (blue_alive > 0) + (yellow_alive > 0) + (pink_alive > 0)) > 1)
243                         return 0;
244         }
245         else if(inv_numkilled < inv_maxspawned)
246                 return 0;
247
248         entity winner = world;
249         float winning_score = 0, winner_team = 0;
250
251
252         if(teamplay)
253         {
254                 if(red_alive > 0) { winner_team = NUM_TEAM_1; }
255                 if(blue_alive > 0)
256                 if(winner_team) { winner_team = 0; }
257                 else { winner_team = NUM_TEAM_2; }
258                 if(yellow_alive > 0)
259                 if(winner_team) { winner_team = 0; }
260                 else { winner_team = NUM_TEAM_3; }
261                 if(pink_alive > 0)
262                 if(winner_team) { winner_team = 0; }
263                 else { winner_team = NUM_TEAM_4; }
264         }
265         else
266         {
267                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
268                         float cs = PlayerScore_Add(it, SP_KILLS, 0);
269                         if(cs > winning_score)
270                         {
271                                 winning_score = cs;
272                                 winner = it;
273                         }
274                 ));
275         }
276
277         FOREACH_ENTITY_FLAGS(flags, FL_MONSTER, LAMBDA(Monster_Remove(it)));
278
279         if(teamplay)
280         {
281                 if(winner_team)
282                 {
283                         Send_Notification(NOTIF_ALL, world, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
284                         Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
285                 }
286         }
287         else if(winner)
288         {
289                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_PLAYER_WIN, winner.netname);
290                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_PLAYER_WIN, winner.netname);
291         }
292
293         round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
294
295         return 1;
296 }
297
298 bool Invasion_CheckPlayers()
299 {
300         return true;
301 }
302
303 void Invasion_RoundStart()
304 {
305         int numplayers = 0;
306         FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
307                 it.player_blocked = false;
308                 ++numplayers;
309         ));
310
311         if(inv_roundcnt < inv_maxrounds)
312                 inv_roundcnt += 1; // a limiter to stop crazy counts
313
314         inv_monsterskill = inv_roundcnt + max(1, numplayers * 0.3);
315
316         inv_maxcurrent = 0;
317         inv_numspawned = 0;
318         inv_numkilled = 0;
319
320         inv_maxspawned = rint(max(autocvar_g_invasion_monster_count, autocvar_g_invasion_monster_count * (inv_roundcnt * 0.5)));
321
322         if(teamplay)
323         {
324                 DistributeEvenly_Init(inv_maxspawned, invasion_teams);
325                 inv_monsters_perteam[NUM_TEAM_1] = DistributeEvenly_Get(1);
326                 inv_monsters_perteam[NUM_TEAM_2] = DistributeEvenly_Get(1);
327                 if(invasion_teams >= 3) inv_monsters_perteam[NUM_TEAM_3] = DistributeEvenly_Get(1);
328                 if(invasion_teams >= 4) inv_monsters_perteam[NUM_TEAM_4] = DistributeEvenly_Get(1);
329         }
330 }
331
332 MUTATOR_HOOKFUNCTION(inv, MonsterDies)
333 {
334         entity frag_target = M_ARGV(0, entity);
335         entity frag_attacker = M_ARGV(1, entity);
336
337         if(!(frag_target.spawnflags & MONSTERFLAG_RESPAWNED))
338         {
339                 inv_numkilled += 1;
340                 inv_maxcurrent -= 1;
341                 if(teamplay) { inv_monsters_perteam[frag_target.team] -= 1; }
342
343                 if(IS_PLAYER(frag_attacker))
344                 if(SAME_TEAM(frag_attacker, frag_target)) // in non-teamplay modes, same team = same player, so this works
345                         PlayerScore_Add(frag_attacker, SP_KILLS, -1);
346                 else
347                 {
348                         PlayerScore_Add(frag_attacker, SP_KILLS, +1);
349                         if(teamplay)
350                                 TeamScore_AddToTeam(frag_attacker.team, ST_INV_KILLS, +1);
351                 }
352         }
353
354         return false;
355 }
356
357 MUTATOR_HOOKFUNCTION(inv, MonsterSpawn)
358 {
359         entity mon = M_ARGV(0, entity);
360
361         if(!(mon.spawnflags & MONSTERFLAG_SPAWNED))
362                 return true;
363
364         if(!(mon.spawnflags & MONSTERFLAG_RESPAWNED))
365         {
366                 inv_numspawned += 1;
367                 inv_maxcurrent += 1;
368         }
369
370         mon.monster_skill = inv_monsterskill;
371
372         if((get_monsterinfo(mon.monsterid)).spawnflags & MON_FLAG_SUPERMONSTER)
373                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_INVASION_SUPERMONSTER, mon.monster_name);
374
375         mon.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_BOTCLIP | DPCONTENTS_MONSTERCLIP;
376
377         return false;
378 }
379
380 MUTATOR_HOOKFUNCTION(inv, OnEntityPreSpawn)
381 {
382         entity ent = M_ARGV(0, entity);
383
384         if(startsWith(ent.classname, "monster_"))
385         if(!(ent.spawnflags & MONSTERFLAG_SPAWNED))
386                 return true;
387
388         return false;
389 }
390
391 MUTATOR_HOOKFUNCTION(inv, SV_StartFrame)
392 {
393         monsters_total = inv_maxspawned; // TODO: make sure numspawned never exceeds maxspawned
394         monsters_killed = inv_numkilled;
395
396         return false;
397 }
398
399 MUTATOR_HOOKFUNCTION(inv, PlayerRegen)
400 {
401         // no regeneration in invasion
402         return true;
403 }
404
405 MUTATOR_HOOKFUNCTION(inv, PlayerSpawn)
406 {
407         entity player = M_ARGV(0, entity);
408
409         player.bot_attack = false;
410         return false;
411 }
412
413 MUTATOR_HOOKFUNCTION(inv, PlayerDamage_Calculate)
414 {
415         entity frag_attacker = M_ARGV(1, entity);
416         entity frag_target = M_ARGV(2, entity);
417         float frag_damage = M_ARGV(4, float);
418         vector frag_force = M_ARGV(6, vector);
419
420         if(IS_PLAYER(frag_attacker) && IS_PLAYER(frag_target) && frag_attacker != frag_target)
421         {
422                 frag_damage = 0;
423                 frag_force = '0 0 0';
424
425                 M_ARGV(4, float) = frag_damage;
426                 M_ARGV(6, vector) = frag_force;
427         }
428
429         return false;
430 }
431
432 MUTATOR_HOOKFUNCTION(inv, SV_ParseClientCommand)
433 {
434         if(MUTATOR_RETURNVALUE) // command was already handled?
435                 return false;
436
437         entity player = M_ARGV(0, entity);
438         string cmd_name = M_ARGV(1, string);
439
440         if(cmd_name == "debuginvasion")
441         {
442                 sprint(player, strcat("inv_maxspawned = ", ftos(inv_maxspawned), "\n"));
443                 sprint(player, strcat("inv_numspawned = ", ftos(inv_numspawned), "\n"));
444                 sprint(player, strcat("inv_numkilled = ", ftos(inv_numkilled), "\n"));
445                 sprint(player, strcat("inv_roundcnt = ", ftos(inv_roundcnt), "\n"));
446                 sprint(player, strcat("monsters_total = ", ftos(monsters_total), "\n"));
447                 sprint(player, strcat("monsters_killed = ", ftos(monsters_killed), "\n"));
448                 sprint(player, strcat("inv_monsterskill = ", ftos(inv_monsterskill), "\n"));
449
450                 return true;
451         }
452
453         return false;
454 }
455
456 MUTATOR_HOOKFUNCTION(inv, BotShouldAttack)
457 {
458         entity targ = M_ARGV(1, entity);
459
460         if(!IS_MONSTER(targ))
461                 return true;
462
463         return false;
464 }
465
466 MUTATOR_HOOKFUNCTION(inv, SetStartItems)
467 {
468         start_health = 200;
469         start_armorvalue = 200;
470         return false;
471 }
472
473 MUTATOR_HOOKFUNCTION(inv, AccuracyTargetValid)
474 {
475         entity frag_target = M_ARGV(1, entity);
476
477         if(IS_MONSTER(frag_target))
478                 return MUT_ACCADD_INVALID;
479         return MUT_ACCADD_INDIFFERENT;
480 }
481
482 MUTATOR_HOOKFUNCTION(inv, AllowMobSpawning)
483 {
484         // monster spawning disabled during an invasion
485         M_ARGV(1, string) = "You cannot spawn monsters during an invasion!";
486         return true;
487 }
488
489 MUTATOR_HOOKFUNCTION(inv, GetTeamCount, CBC_ORDER_EXCLUSIVE)
490 {
491         M_ARGV(0, float) = invasion_teams;
492 }
493
494 MUTATOR_HOOKFUNCTION(inv, AllowMobButcher)
495 {
496         M_ARGV(0, string) = "This command does not work during an invasion!";
497         return true;
498 }
499
500 void invasion_ScoreRules(float inv_teams)
501 {
502         if(inv_teams) { CheckAllowedTeams(world); }
503         ScoreRules_basics(inv_teams, 0, 0, false);
504         if(inv_teams) ScoreInfo_SetLabel_TeamScore(ST_INV_KILLS, "frags", SFL_SORT_PRIO_PRIMARY);
505         ScoreInfo_SetLabel_PlayerScore(SP_KILLS, "frags", ((inv_teams) ? SFL_SORT_PRIO_SECONDARY : SFL_SORT_PRIO_PRIMARY));
506         ScoreRules_basics_end();
507 }
508
509 void invasion_DelayedInit(entity this) // Do this check with a delay so we can wait for teams to be set up.
510 {
511         if(autocvar_g_invasion_teams)
512                 invasion_teams = bound(2, autocvar_g_invasion_teams, 4);
513         else
514                 invasion_teams = 0;
515
516         independent_players = 1; // to disable extra useless scores
517
518         invasion_ScoreRules(invasion_teams);
519
520         independent_players = 0;
521
522         round_handler_Spawn(Invasion_CheckPlayers, Invasion_CheckWinner, Invasion_RoundStart);
523         round_handler_Init(5, autocvar_g_invasion_warmup, autocvar_g_invasion_round_timelimit);
524
525         inv_roundcnt = 0;
526         inv_maxrounds = 15; // 15?
527 }
528
529 void invasion_Initialize()
530 {
531         if(autocvar_g_invasion_zombies_only) {
532                 Monster mon = MON_ZOMBIE;
533                 mon.mr_precache(mon);
534         } else
535         {
536                 float i;
537                 entity mon;
538                 for(i = MON_FIRST; i <= MON_LAST; ++i)
539                 {
540                         mon = get_monsterinfo(i);
541                         if((mon.spawnflags & MONSTER_TYPE_FLY) || (mon.spawnflags & MONSTER_TYPE_SWIM))
542                                 continue; // flying/swimming monsters not yet supported
543
544                         mon.mr_precache(mon);
545                 }
546         }
547
548         InitializeEntity(world, invasion_DelayedInit, INITPRIO_GAMETYPE);
549 }
550
551 #endif