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