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