]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_assault.qc
Fix race not starting (game ends) when the qualifying session ends (everyone readied...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator / gamemode_assault.qc
1 #include "gamemode_assault.qh"
2 #ifndef GAMEMODE_ASSAULT_H
3 #define GAMEMODE_ASSAULT_H
4
5 void assault_ScoreRules();
6
7 REGISTER_MUTATOR(as, false)
8 {
9         ActivateTeamplay();
10         have_team_spawns = -1; // request team spawns
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                 assault_ScoreRules();
17         }
18
19         MUTATOR_ONROLLBACK_OR_REMOVE
20         {
21                 // we actually cannot roll back assault_Initialize here
22                 // BUT: we don't need to! If this gets called, adding always
23                 // succeeds.
24         }
25
26         MUTATOR_ONREMOVE
27         {
28                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
29                 return -1;
30         }
31
32         return 0;
33 }
34
35 // sprites
36 .entity assault_decreaser;
37 .entity assault_sprite;
38
39 // legacy bot defs
40 const int HAVOCBOT_AST_ROLE_NONE = 0;
41 const int HAVOCBOT_AST_ROLE_DEFENSE = 2;
42 const int HAVOCBOT_AST_ROLE_OFFENSE = 4;
43
44 .int havocbot_role_flags;
45 .float havocbot_attack_time;
46
47 .void() havocbot_role;
48 .void() havocbot_previous_role;
49
50 void() havocbot_role_ast_defense;
51 void() havocbot_role_ast_offense;
52 .entity havocbot_ast_target;
53
54 void(entity bot) havocbot_ast_reset_role;
55
56 void(float ratingscale, vector org, float sradius) havocbot_goalrating_items;
57 void(float ratingscale, vector org, float sradius) havocbot_goalrating_enemyplayers;
58
59 // scoreboard stuff
60 const float ST_ASSAULT_OBJECTIVES = 1;
61 const float SP_ASSAULT_OBJECTIVES = 4;
62
63 // predefined spawnfuncs
64 void target_objective_decrease_activate();
65 #endif
66
67 #ifdef IMPLEMENTATION
68 .entity sprite;
69
70 // random functions
71 void assault_objective_use()
72 {SELFPARAM();
73         // activate objective
74         self.health = 100;
75         //print("^2Activated objective ", self.targetname, "=", etos(self), "\n");
76         //print("Activator is ", activator.classname, "\n");
77
78         for (entity e = world; (e = find(e, target, this.targetname)); )
79         {
80                 if (e.classname == "target_objective_decrease")
81                 {
82                         WITH(entity, self, e, target_objective_decrease_activate());
83                 }
84         }
85
86         setself(this);
87 }
88
89 vector target_objective_spawn_evalfunc(entity player, entity spot, vector current)
90 {SELFPARAM();
91         if(self.health < 0 || self.health >= ASSAULT_VALUE_INACTIVE)
92                 return '-1 0 0';
93         return current;
94 }
95
96 // reset this objective. Used when spawning an objective
97 // and when a new round starts
98 void assault_objective_reset(entity this)
99 {
100         this.health = ASSAULT_VALUE_INACTIVE;
101 }
102
103 // decrease the health of targeted objectives
104 void assault_objective_decrease_use()
105 {SELFPARAM();
106         if(activator.team != assault_attacker_team)
107         {
108                 // wrong team triggered decrease
109                 return;
110         }
111
112         if(other.assault_sprite)
113         {
114                 WaypointSprite_Disown(other.assault_sprite, waypointsprite_deadlifetime);
115                 if(other.classname == "func_assault_destructible")
116                         other.sprite = world;
117         }
118         else
119                 return; // already activated! cannot activate again!
120
121         if(self.enemy.health < ASSAULT_VALUE_INACTIVE)
122         {
123                 if(self.enemy.health - self.dmg > 0.5)
124                 {
125                         PlayerTeamScore_Add(activator, SP_SCORE, ST_SCORE, self.dmg);
126                         self.enemy.health = self.enemy.health - self.dmg;
127                 }
128                 else
129                 {
130                         PlayerTeamScore_Add(activator, SP_SCORE, ST_SCORE, self.enemy.health);
131                         PlayerTeamScore_Add(activator, SP_ASSAULT_OBJECTIVES, ST_ASSAULT_OBJECTIVES, 1);
132                         self.enemy.health = -1;
133
134                         entity oldactivator;
135
136                         setself(this.enemy);
137                         if(self.message)
138                                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(centerprint(it, self.message)));
139
140                         oldactivator = activator;
141                         activator = this;
142                         SUB_UseTargets();
143                         activator = oldactivator;
144                         setself(this);
145                 }
146         }
147 }
148
149 void assault_setenemytoobjective()
150 {SELFPARAM();
151         entity objective;
152         for(objective = world; (objective = find(objective, targetname, self.target)); )
153         {
154                 if(objective.classname == "target_objective")
155                 {
156                         if(self.enemy == world)
157                                 self.enemy = objective;
158                         else
159                                 objerror("more than one objective as target - fix the map!");
160                         break;
161                 }
162         }
163
164         if(self.enemy == world)
165                 objerror("no objective as target - fix the map!");
166 }
167
168 float assault_decreaser_sprite_visible(entity e)
169 {SELFPARAM();
170         entity decreaser;
171
172         decreaser = self.assault_decreaser;
173
174         if(decreaser.enemy.health >= ASSAULT_VALUE_INACTIVE)
175                 return false;
176
177         return true;
178 }
179
180 void target_objective_decrease_activate()
181 {SELFPARAM();
182         entity ent, spr;
183         self.owner = world;
184         for(ent = world; (ent = find(ent, target, self.targetname)); )
185         {
186                 if(ent.assault_sprite != world)
187                 {
188                         WaypointSprite_Disown(ent.assault_sprite, waypointsprite_deadlifetime);
189                         if(ent.classname == "func_assault_destructible")
190                                 ent.sprite = world;
191                 }
192
193                 spr = WaypointSprite_SpawnFixed(WP_Assault, 0.5 * (ent.absmin + ent.absmax), ent, assault_sprite, RADARICON_OBJECTIVE);
194                 spr.assault_decreaser = self;
195                 spr.waypointsprite_visible_for_player = assault_decreaser_sprite_visible;
196                 spr.classname = "sprite_waypoint";
197                 WaypointSprite_UpdateRule(spr, assault_attacker_team, SPRITERULE_TEAMPLAY);
198                 if(ent.classname == "func_assault_destructible")
199                 {
200                         WaypointSprite_UpdateSprites(spr, WP_AssaultDefend, WP_AssaultDestroy, WP_AssaultDestroy);
201                         WaypointSprite_UpdateMaxHealth(spr, ent.max_health);
202                         WaypointSprite_UpdateHealth(spr, ent.health);
203                         ent.sprite = spr;
204                 }
205                 else
206                         WaypointSprite_UpdateSprites(spr, WP_AssaultDefend, WP_AssaultPush, WP_AssaultPush);
207         }
208 }
209
210 void target_objective_decrease_findtarget()
211 {
212         assault_setenemytoobjective();
213 }
214
215 void target_assault_roundend_reset(entity this)
216 {
217         //print("round end reset\n");
218         ++this.cnt; // up round counter
219         this.winning = false; // up round
220 }
221
222 void target_assault_roundend_use()
223 {SELFPARAM();
224         self.winning = 1; // round has been won by attackers
225 }
226
227 void assault_roundstart_use()
228 {SELFPARAM();
229         activator = self;
230         SUB_UseTargets();
231
232         //(Re)spawn all turrets
233         FOREACH_ENTITY_CLASS("turret_main", true, LAMBDA(
234                 // Swap turret teams
235                 if(it.team == NUM_TEAM_1)
236                         it.team = NUM_TEAM_2;
237                 else
238                         it.team = NUM_TEAM_1;
239
240                 // Dubbles as teamchange
241                 WITH(entity, self, it, turret_respawn());
242         ));
243 }
244
245 void assault_wall_think()
246 {SELFPARAM();
247         if(self.enemy.health < 0)
248         {
249                 self.model = "";
250                 self.solid = SOLID_NOT;
251         }
252         else
253         {
254                 self.model = self.mdl;
255                 self.solid = SOLID_BSP;
256         }
257
258         self.nextthink = time + 0.2;
259 }
260
261 // trigger new round
262 // reset objectives, toggle spawnpoints, reset triggers, ...
263 void vehicles_clearreturn(entity veh);
264 void vehicles_spawn();
265 void assault_new_round()
266 {SELFPARAM();
267         //bprint("ASSAULT: new round\n");
268
269         // Eject players from vehicles
270     FOREACH_CLIENT(IS_PLAYER(it) && it.vehicle, LAMBDA(WITH(entity, self, it, vehicles_exit(VHEF_RELEASE))));
271
272     FOREACH_ENTITY_FLAGS(vehicle_flags, VHF_ISVEHICLE, LAMBDA(
273         setself(it);
274         vehicles_clearreturn(self);
275         vehicles_spawn();
276     ));
277
278     setself(this);
279
280         // up round counter
281         self.winning = self.winning + 1;
282
283         // swap attacker/defender roles
284         if(assault_attacker_team == NUM_TEAM_1)
285                 assault_attacker_team = NUM_TEAM_2;
286         else
287                 assault_attacker_team = NUM_TEAM_1;
288
289         FOREACH_ENTITY(IS_NOT_A_CLIENT(it), LAMBDA(
290                 if (it.team_saved == NUM_TEAM_1) it.team_saved = NUM_TEAM_2;
291                 else if (it.team_saved == NUM_TEAM_2) it.team_saved = NUM_TEAM_1;
292         ));
293
294         // reset the level with a countdown
295         cvar_set("timelimit", ftos(ceil(time - game_starttime) / 60));
296         ReadyRestart_force(); // sets game_starttime
297 }
298
299 // Assault winning condition: If the attackers triggered a round end (by fulfilling all objectives)
300 // they win. Otherwise the defending team wins once the timelimit passes.
301 int WinningCondition_Assault()
302 {
303         WinningConditionHelper(); // set worldstatus
304
305         int status = WINNING_NO;
306         // as the timelimit has not yet passed just assume the defending team will win
307         if(assault_attacker_team == NUM_TEAM_1)
308         {
309                 SetWinners(team, NUM_TEAM_2);
310         }
311         else
312         {
313                 SetWinners(team, NUM_TEAM_1);
314         }
315
316         entity ent;
317         ent = find(world, classname, "target_assault_roundend");
318         if(ent)
319         {
320                 if(ent.winning) // round end has been triggered by attacking team
321                 {
322                         bprint("ASSAULT: round completed...\n");
323                         SetWinners(team, assault_attacker_team);
324
325                         TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 666 - TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 0));
326
327                         if(ent.cnt == 1 || autocvar_g_campaign) // this was the second round
328                         {
329                                 status = WINNING_YES;
330                         }
331                         else
332                         {
333                                 WITH(entity, self, ent, assault_new_round());
334                         }
335                 }
336         }
337
338         return status;
339 }
340
341 // spawnfuncs
342 spawnfunc(info_player_attacker)
343 {
344         if (!g_assault) { remove(self); return; }
345
346         self.team = NUM_TEAM_1; // red, gets swapped every round
347         spawnfunc_info_player_deathmatch(this);
348 }
349
350 spawnfunc(info_player_defender)
351 {
352         if (!g_assault) { remove(self); return; }
353
354         self.team = NUM_TEAM_2; // blue, gets swapped every round
355         spawnfunc_info_player_deathmatch(this);
356 }
357
358 spawnfunc(target_objective)
359 {
360         if (!g_assault) { remove(this); return; }
361
362         this.classname = "target_objective";
363         this.use = assault_objective_use;
364         this.reset = assault_objective_reset;
365         this.reset(this);
366         this.spawn_evalfunc = target_objective_spawn_evalfunc;
367 }
368
369 spawnfunc(target_objective_decrease)
370 {
371         if (!g_assault) { remove(self); return; }
372
373         self.classname = "target_objective_decrease";
374
375         if(!self.dmg)
376                 self.dmg = 101;
377
378         self.use = assault_objective_decrease_use;
379         self.health = ASSAULT_VALUE_INACTIVE;
380         self.max_health = ASSAULT_VALUE_INACTIVE;
381         self.enemy = world;
382
383         InitializeEntity(self, target_objective_decrease_findtarget, INITPRIO_FINDTARGET);
384 }
385
386 // destructible walls that can be used to trigger target_objective_decrease
387 spawnfunc(func_breakable);
388 spawnfunc(func_assault_destructible)
389 {
390         if (!g_assault) { remove(self); return; }
391
392         self.spawnflags = 3;
393         self.classname = "func_assault_destructible";
394
395         if(assault_attacker_team == NUM_TEAM_1)
396                 self.team = NUM_TEAM_2;
397         else
398                 self.team = NUM_TEAM_1;
399
400         spawnfunc_func_breakable(this);
401 }
402
403 spawnfunc(func_assault_wall)
404 {
405         if (!g_assault) { remove(self); return; }
406
407         self.classname = "func_assault_wall";
408         self.mdl = self.model;
409         _setmodel(self, self.mdl);
410         self.solid = SOLID_BSP;
411         self.think = assault_wall_think;
412         self.nextthink = time;
413         InitializeEntity(self, assault_setenemytoobjective, INITPRIO_FINDTARGET);
414 }
415
416 spawnfunc(target_assault_roundend)
417 {
418         if (!g_assault) { remove(self); return; }
419
420         self.winning = 0; // round not yet won by attackers
421         self.classname = "target_assault_roundend";
422         self.use = target_assault_roundend_use;
423         self.cnt = 0; // first round
424         self.reset = target_assault_roundend_reset;
425 }
426
427 spawnfunc(target_assault_roundstart)
428 {
429         if (!g_assault) { remove(self); return; }
430
431         assault_attacker_team = NUM_TEAM_1;
432         self.classname = "target_assault_roundstart";
433         self.use = assault_roundstart_use;
434         self.reset2 = assault_roundstart_use;
435         InitializeEntity(self, assault_roundstart_use, INITPRIO_FINDTARGET);
436 }
437
438 // legacy bot code
439 void havocbot_goalrating_ast_targets(float ratingscale)
440 {SELFPARAM();
441         entity ad, best, wp, tod;
442         float radius, found, bestvalue;
443         vector p;
444
445         ad = findchain(classname, "func_assault_destructible");
446
447         for (; ad; ad = ad.chain)
448         {
449                 if (ad.target == "")
450                         continue;
451
452                 if (!ad.bot_attack)
453                         continue;
454
455                 found = false;
456                 for(tod = world; (tod = find(tod, targetname, ad.target)); )
457                 {
458                         if(tod.classname == "target_objective_decrease")
459                         {
460                                 if(tod.enemy.health > 0 && tod.enemy.health < ASSAULT_VALUE_INACTIVE)
461                                 {
462                                 //      dprint(etos(ad),"\n");
463                                         found = true;
464                                         break;
465                                 }
466                         }
467                 }
468
469                 if(!found)
470                 {
471                 ///     dprint("target not found\n");
472                         continue;
473                 }
474                 /// dprint("target #", etos(ad), " found\n");
475
476
477                 p = 0.5 * (ad.absmin + ad.absmax);
478         //      dprint(vtos(ad.origin), " ", vtos(ad.absmin), " ", vtos(ad.absmax),"\n");
479         //      te_knightspike(p);
480         //      te_lightning2(world, '0 0 0', p);
481
482                 // Find and rate waypoints around it
483                 found = false;
484                 best = world;
485                 bestvalue = 99999999999;
486                 for(radius=0; radius<1500 && !found; radius+=500)
487                 {
488                         for(wp=findradius(p, radius); wp; wp=wp.chain)
489                         {
490                                 if(!(wp.wpflags & WAYPOINTFLAG_GENERATED))
491                                 if(wp.classname=="waypoint")
492                                 if(checkpvs(wp.origin, ad))
493                                 {
494                                         found = true;
495                                         if(wp.cnt<bestvalue)
496                                         {
497                                                 best = wp;
498                                                 bestvalue = wp.cnt;
499                                         }
500                                 }
501                         }
502                 }
503
504                 if(best)
505                 {
506                 ///     dprint("waypoints around target were found\n");
507                 //      te_lightning2(world, '0 0 0', best.origin);
508                 //      te_knightspike(best.origin);
509
510                         navigation_routerating(best, ratingscale, 4000);
511                         best.cnt += 1;
512
513                         self.havocbot_attack_time = 0;
514
515                         if(checkpvs(self.view_ofs,ad))
516                         if(checkpvs(self.view_ofs,best))
517                         {
518                         //      dprint("increasing attack time for this target\n");
519                                 self.havocbot_attack_time = time + 2;
520                         }
521                 }
522         }
523 }
524
525 void havocbot_role_ast_offense()
526 {SELFPARAM();
527         if(IS_DEAD(self))
528         {
529                 self.havocbot_attack_time = 0;
530                 havocbot_ast_reset_role(self);
531                 return;
532         }
533
534         // Set the role timeout if necessary
535         if (!self.havocbot_role_timeout)
536                 self.havocbot_role_timeout = time + 120;
537
538         if (time > self.havocbot_role_timeout)
539         {
540                 havocbot_ast_reset_role(self);
541                 return;
542         }
543
544         if(self.havocbot_attack_time>time)
545                 return;
546
547         if (self.bot_strategytime < time)
548         {
549                 navigation_goalrating_start();
550                 havocbot_goalrating_enemyplayers(20000, self.origin, 650);
551                 havocbot_goalrating_ast_targets(20000);
552                 havocbot_goalrating_items(15000, self.origin, 10000);
553                 navigation_goalrating_end();
554
555                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
556         }
557 }
558
559 void havocbot_role_ast_defense()
560 {SELFPARAM();
561         if(IS_DEAD(self))
562         {
563                 self.havocbot_attack_time = 0;
564                 havocbot_ast_reset_role(self);
565                 return;
566         }
567
568         // Set the role timeout if necessary
569         if (!self.havocbot_role_timeout)
570                 self.havocbot_role_timeout = time + 120;
571
572         if (time > self.havocbot_role_timeout)
573         {
574                 havocbot_ast_reset_role(self);
575                 return;
576         }
577
578         if(self.havocbot_attack_time>time)
579                 return;
580
581         if (self.bot_strategytime < time)
582         {
583                 navigation_goalrating_start();
584                 havocbot_goalrating_enemyplayers(20000, self.origin, 3000);
585                 havocbot_goalrating_ast_targets(20000);
586                 havocbot_goalrating_items(15000, self.origin, 10000);
587                 navigation_goalrating_end();
588
589                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
590         }
591 }
592
593 void havocbot_role_ast_setrole(entity bot, float role)
594 {
595         switch(role)
596         {
597                 case HAVOCBOT_AST_ROLE_DEFENSE:
598                         bot.havocbot_role = havocbot_role_ast_defense;
599                         bot.havocbot_role_flags = HAVOCBOT_AST_ROLE_DEFENSE;
600                         bot.havocbot_role_timeout = 0;
601                         break;
602                 case HAVOCBOT_AST_ROLE_OFFENSE:
603                         bot.havocbot_role = havocbot_role_ast_offense;
604                         bot.havocbot_role_flags = HAVOCBOT_AST_ROLE_OFFENSE;
605                         bot.havocbot_role_timeout = 0;
606                         break;
607         }
608 }
609
610 void havocbot_ast_reset_role(entity bot)
611 {SELFPARAM();
612         if(IS_DEAD(self))
613                 return;
614
615         if(bot.team == assault_attacker_team)
616                 havocbot_role_ast_setrole(bot, HAVOCBOT_AST_ROLE_OFFENSE);
617         else
618                 havocbot_role_ast_setrole(bot, HAVOCBOT_AST_ROLE_DEFENSE);
619 }
620
621 // mutator hooks
622 MUTATOR_HOOKFUNCTION(as, PlayerSpawn)
623 {SELFPARAM();
624         if(self.team == assault_attacker_team)
625                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_ASSAULT_ATTACKING);
626         else
627                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_ASSAULT_DEFENDING);
628
629         return false;
630 }
631
632 MUTATOR_HOOKFUNCTION(as, TurretSpawn)
633 {SELFPARAM();
634         if(!self.team || self.team == MAX_SHOT_DISTANCE)
635                 self.team = 5; // this gets reversed when match starts?
636
637         return false;
638 }
639
640 MUTATOR_HOOKFUNCTION(as, VehicleSpawn)
641 {SELFPARAM();
642         self.nextthink = time + 0.5;
643
644         return false;
645 }
646
647 MUTATOR_HOOKFUNCTION(as, HavocBot_ChooseRole)
648 {SELFPARAM();
649         havocbot_ast_reset_role(self);
650         return true;
651 }
652
653 MUTATOR_HOOKFUNCTION(as, PlayHitsound)
654 {
655         return (frag_victim.classname == "func_assault_destructible");
656 }
657
658 MUTATOR_HOOKFUNCTION(as, GetTeamCount)
659 {
660         // assault always has 2 teams
661         c1 = c2 = 0;
662         return true;
663 }
664
665 MUTATOR_HOOKFUNCTION(as, CheckRules_World)
666 {
667         ret_float = WinningCondition_Assault();
668         return true;
669 }
670
671 MUTATOR_HOOKFUNCTION(as, ReadLevelCvars)
672 {
673         // no assault warmups
674         warmup_stage = 0;
675         return false;
676 }
677
678 MUTATOR_HOOKFUNCTION(as, OnEntityPreSpawn)
679 {
680         switch(self.classname)
681         {
682                 case "info_player_team1":
683                 case "info_player_team2":
684                 case "info_player_team3":
685                 case "info_player_team4":
686                         return true;
687         }
688
689         return false;
690 }
691
692 // scoreboard setup
693 void assault_ScoreRules()
694 {
695         ScoreRules_basics(2, SFL_SORT_PRIO_SECONDARY, SFL_SORT_PRIO_SECONDARY, true);
696         ScoreInfo_SetLabel_TeamScore(  ST_ASSAULT_OBJECTIVES,    "objectives",      SFL_SORT_PRIO_PRIMARY);
697         ScoreInfo_SetLabel_PlayerScore(SP_ASSAULT_OBJECTIVES,    "objectives",      SFL_SORT_PRIO_PRIMARY);
698         ScoreRules_basics_end();
699 }
700
701 #endif