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