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