]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_assault.qc
Merge branch 'sev/luma_update' into 'master'
[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(trigger.assault_sprite)
112         {
113                 WaypointSprite_Disown(trigger.assault_sprite, waypointsprite_deadlifetime);
114                 if(trigger.classname == "func_assault_destructible")
115                         trigger.sprite = world; // 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         entity objective;
144         for(objective = world; (objective = find(objective, targetname, this.target)); )
145         {
146                 if(objective.classname == "target_objective")
147                 {
148                         if(this.enemy == world)
149                                 this.enemy = objective;
150                         else
151                                 objerror("more than one objective as target - fix the map!");
152                         break;
153                 }
154         }
155
156         if(this.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; // TODO: just unsetting it?!
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_this(entity this)
236 {
237         assault_roundstart_use(this, NULL, NULL);
238 }
239 void assault_roundstart_use_self()
240 {
241     SELFPARAM();
242     assault_roundstart_use(this, NULL, NULL);
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, WITHSELF(it, vehicles_exit(VHEF_RELEASE)));
271
272     FOREACH_ENTITY_FLAGS(vehicle_flags, VHF_ISVEHICLE, LAMBDA(
273         vehicles_clearreturn(it);
274         WITHSELF(it, vehicles_spawn());
275     ));
276
277         // up round counter
278         self.winning = self.winning + 1;
279
280         // swap attacker/defender roles
281         if(assault_attacker_team == NUM_TEAM_1)
282                 assault_attacker_team = NUM_TEAM_2;
283         else
284                 assault_attacker_team = NUM_TEAM_1;
285
286         FOREACH_ENTITY(IS_NOT_A_CLIENT(it), LAMBDA(
287                 if (it.team_saved == NUM_TEAM_1) it.team_saved = NUM_TEAM_2;
288                 else if (it.team_saved == NUM_TEAM_2) it.team_saved = NUM_TEAM_1;
289         ));
290
291         // reset the level with a countdown
292         cvar_set("timelimit", ftos(ceil(time - game_starttime) / 60));
293         ReadyRestart_force(); // sets game_starttime
294 }
295
296 // Assault winning condition: If the attackers triggered a round end (by fulfilling all objectives)
297 // they win. Otherwise the defending team wins once the timelimit passes.
298 int WinningCondition_Assault()
299 {
300     SELFPARAM();
301         WinningConditionHelper(); // set worldstatus
302
303         int status = WINNING_NO;
304         // as the timelimit has not yet passed just assume the defending team will win
305         if(assault_attacker_team == NUM_TEAM_1)
306         {
307                 SetWinners(team, NUM_TEAM_2);
308         }
309         else
310         {
311                 SetWinners(team, NUM_TEAM_1);
312         }
313
314         entity ent;
315         ent = find(world, classname, "target_assault_roundend");
316         if(ent)
317         {
318                 if(ent.winning) // round end has been triggered by attacking team
319                 {
320                         bprint("ASSAULT: round completed...\n");
321                         SetWinners(team, assault_attacker_team);
322
323                         TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 666 - TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 0));
324
325                         if(ent.cnt == 1 || autocvar_g_campaign) // this was the second round
326                         {
327                                 status = WINNING_YES;
328                         }
329                         else
330                         {
331                                 WITHSELF(ent, assault_new_round());
332                         }
333                 }
334         }
335
336         return status;
337 }
338
339 // spawnfuncs
340 spawnfunc(info_player_attacker)
341 {
342         if (!g_assault) { remove(this); return; }
343
344         this.team = NUM_TEAM_1; // red, gets swapped every round
345         spawnfunc_info_player_deathmatch(this);
346 }
347
348 spawnfunc(info_player_defender)
349 {
350         if (!g_assault) { remove(this); return; }
351
352         this.team = NUM_TEAM_2; // blue, gets swapped every round
353         spawnfunc_info_player_deathmatch(this);
354 }
355
356 spawnfunc(target_objective)
357 {
358         if (!g_assault) { remove(this); return; }
359
360         this.classname = "target_objective";
361         this.use = assault_objective_use;
362         this.reset = assault_objective_reset;
363         this.reset(this);
364         this.spawn_evalfunc = target_objective_spawn_evalfunc;
365 }
366
367 spawnfunc(target_objective_decrease)
368 {
369         if (!g_assault) { remove(this); return; }
370
371         this.classname = "target_objective_decrease";
372
373         if(!this.dmg)
374                 this.dmg = 101;
375
376         this.use = assault_objective_decrease_use;
377         this.health = ASSAULT_VALUE_INACTIVE;
378         this.max_health = ASSAULT_VALUE_INACTIVE;
379         this.enemy = world;
380
381         InitializeEntity(this, target_objective_decrease_findtarget, INITPRIO_FINDTARGET);
382 }
383
384 // destructible walls that can be used to trigger target_objective_decrease
385 spawnfunc(func_breakable);
386 spawnfunc(func_assault_destructible)
387 {
388         if (!g_assault) { remove(this); return; }
389
390         this.spawnflags = 3;
391         this.classname = "func_assault_destructible";
392
393         if(assault_attacker_team == NUM_TEAM_1)
394                 this.team = NUM_TEAM_2;
395         else
396                 this.team = NUM_TEAM_1;
397
398         spawnfunc_func_breakable(this);
399 }
400
401 spawnfunc(func_assault_wall)
402 {
403         if (!g_assault) { remove(this); return; }
404
405         this.classname = "func_assault_wall";
406         this.mdl = this.model;
407         _setmodel(this, this.mdl);
408         this.solid = SOLID_BSP;
409         this.think = assault_wall_think;
410         this.nextthink = time;
411         InitializeEntity(this, assault_setenemytoobjective, INITPRIO_FINDTARGET);
412 }
413
414 spawnfunc(target_assault_roundend)
415 {
416         if (!g_assault) { remove(this); return; }
417
418         this.winning = 0; // round not yet won by attackers
419         this.classname = "target_assault_roundend";
420         this.use = target_assault_roundend_use;
421         this.cnt = 0; // first round
422         this.reset = target_assault_roundend_reset;
423 }
424
425 spawnfunc(target_assault_roundstart)
426 {
427         if (!g_assault) { remove(this); return; }
428
429         assault_attacker_team = NUM_TEAM_1;
430         this.classname = "target_assault_roundstart";
431         this.use = assault_roundstart_use;
432         this.reset2 = assault_roundstart_use_self;
433         InitializeEntity(this, assault_roundstart_use_this, INITPRIO_FINDTARGET);
434 }
435
436 // legacy bot code
437 void havocbot_goalrating_ast_targets(entity this, float ratingscale)
438 {
439         entity ad, best, wp, tod;
440         float radius, found, bestvalue;
441         vector p;
442
443         ad = findchain(classname, "func_assault_destructible");
444
445         for (; ad; ad = ad.chain)
446         {
447                 if (ad.target == "")
448                         continue;
449
450                 if (!ad.bot_attack)
451                         continue;
452
453                 found = false;
454                 for(tod = world; (tod = find(tod, targetname, ad.target)); )
455                 {
456                         if(tod.classname == "target_objective_decrease")
457                         {
458                                 if(tod.enemy.health > 0 && tod.enemy.health < ASSAULT_VALUE_INACTIVE)
459                                 {
460                                 //      dprint(etos(ad),"\n");
461                                         found = true;
462                                         break;
463                                 }
464                         }
465                 }
466
467                 if(!found)
468                 {
469                 ///     dprint("target not found\n");
470                         continue;
471                 }
472                 /// dprint("target #", etos(ad), " found\n");
473
474
475                 p = 0.5 * (ad.absmin + ad.absmax);
476         //      dprint(vtos(ad.origin), " ", vtos(ad.absmin), " ", vtos(ad.absmax),"\n");
477         //      te_knightspike(p);
478         //      te_lightning2(world, '0 0 0', p);
479
480                 // Find and rate waypoints around it
481                 found = false;
482                 best = world;
483                 bestvalue = 99999999999;
484                 for(radius=0; radius<1500 && !found; radius+=500)
485                 {
486                         for(wp=findradius(p, radius); wp; wp=wp.chain)
487                         {
488                                 if(!(wp.wpflags & WAYPOINTFLAG_GENERATED))
489                                 if(wp.classname=="waypoint")
490                                 if(checkpvs(wp.origin, ad))
491                                 {
492                                         found = true;
493                                         if(wp.cnt<bestvalue)
494                                         {
495                                                 best = wp;
496                                                 bestvalue = wp.cnt;
497                                         }
498                                 }
499                         }
500                 }
501
502                 if(best)
503                 {
504                 ///     dprint("waypoints around target were found\n");
505                 //      te_lightning2(world, '0 0 0', best.origin);
506                 //      te_knightspike(best.origin);
507
508                         navigation_routerating(this, best, ratingscale, 4000);
509                         best.cnt += 1;
510
511                         this.havocbot_attack_time = 0;
512
513                         if(checkpvs(this.view_ofs,ad))
514                         if(checkpvs(this.view_ofs,best))
515                         {
516                         //      dprint("increasing attack time for this target\n");
517                                 this.havocbot_attack_time = time + 2;
518                         }
519                 }
520         }
521 }
522
523 void havocbot_role_ast_offense(entity this)
524 {
525         if(IS_DEAD(this))
526         {
527                 this.havocbot_attack_time = 0;
528                 havocbot_ast_reset_role(this);
529                 return;
530         }
531
532         // Set the role timeout if necessary
533         if (!this.havocbot_role_timeout)
534                 this.havocbot_role_timeout = time + 120;
535
536         if (time > this.havocbot_role_timeout)
537         {
538                 havocbot_ast_reset_role(this);
539                 return;
540         }
541
542         if(this.havocbot_attack_time>time)
543                 return;
544
545         if (this.bot_strategytime < time)
546         {
547                 navigation_goalrating_start(this);
548                 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 650);
549                 havocbot_goalrating_ast_targets(this, 20000);
550                 havocbot_goalrating_items(this, 15000, this.origin, 10000);
551                 navigation_goalrating_end(this);
552
553                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
554         }
555 }
556
557 void havocbot_role_ast_defense(entity this)
558 {
559         if(IS_DEAD(this))
560         {
561                 this.havocbot_attack_time = 0;
562                 havocbot_ast_reset_role(this);
563                 return;
564         }
565
566         // Set the role timeout if necessary
567         if (!this.havocbot_role_timeout)
568                 this.havocbot_role_timeout = time + 120;
569
570         if (time > this.havocbot_role_timeout)
571         {
572                 havocbot_ast_reset_role(this);
573                 return;
574         }
575
576         if(this.havocbot_attack_time>time)
577                 return;
578
579         if (this.bot_strategytime < time)
580         {
581                 navigation_goalrating_start(this);
582                 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 3000);
583                 havocbot_goalrating_ast_targets(this, 20000);
584                 havocbot_goalrating_items(this, 15000, this.origin, 10000);
585                 navigation_goalrating_end(this);
586
587                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
588         }
589 }
590
591 void havocbot_role_ast_setrole(entity this, float role)
592 {
593         switch(role)
594         {
595                 case HAVOCBOT_AST_ROLE_DEFENSE:
596                         this.havocbot_role = havocbot_role_ast_defense;
597                         this.havocbot_role_flags = HAVOCBOT_AST_ROLE_DEFENSE;
598                         this.havocbot_role_timeout = 0;
599                         break;
600                 case HAVOCBOT_AST_ROLE_OFFENSE:
601                         this.havocbot_role = havocbot_role_ast_offense;
602                         this.havocbot_role_flags = HAVOCBOT_AST_ROLE_OFFENSE;
603                         this.havocbot_role_timeout = 0;
604                         break;
605         }
606 }
607
608 void havocbot_ast_reset_role(entity this)
609 {
610         if(IS_DEAD(this))
611                 return;
612
613         if(this.team == assault_attacker_team)
614                 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_OFFENSE);
615         else
616                 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_DEFENSE);
617 }
618
619 // mutator hooks
620 MUTATOR_HOOKFUNCTION(as, PlayerSpawn)
621 {SELFPARAM();
622         if(self.team == assault_attacker_team)
623                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_ASSAULT_ATTACKING);
624         else
625                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_ASSAULT_DEFENDING);
626
627         return false;
628 }
629
630 MUTATOR_HOOKFUNCTION(as, TurretSpawn)
631 {SELFPARAM();
632         if(!self.team || self.team == MAX_SHOT_DISTANCE)
633                 self.team = 5; // this gets reversed when match starts?
634
635         return false;
636 }
637
638 MUTATOR_HOOKFUNCTION(as, VehicleSpawn)
639 {SELFPARAM();
640         self.nextthink = time + 0.5;
641
642         return false;
643 }
644
645 MUTATOR_HOOKFUNCTION(as, HavocBot_ChooseRole)
646 {SELFPARAM();
647         havocbot_ast_reset_role(self);
648         return true;
649 }
650
651 MUTATOR_HOOKFUNCTION(as, PlayHitsound)
652 {
653         return (frag_victim.classname == "func_assault_destructible");
654 }
655
656 MUTATOR_HOOKFUNCTION(as, GetTeamCount)
657 {
658         // assault always has 2 teams
659         c1 = c2 = 0;
660         return true;
661 }
662
663 MUTATOR_HOOKFUNCTION(as, CheckRules_World)
664 {
665         ret_float = WinningCondition_Assault();
666         return true;
667 }
668
669 MUTATOR_HOOKFUNCTION(as, ReadLevelCvars)
670 {
671         // no assault warmups
672         warmup_stage = 0;
673         return false;
674 }
675
676 MUTATOR_HOOKFUNCTION(as, OnEntityPreSpawn)
677 {
678     SELFPARAM();
679         switch(self.classname)
680         {
681                 case "info_player_team1":
682                 case "info_player_team2":
683                 case "info_player_team3":
684                 case "info_player_team4":
685                         return true;
686         }
687
688         return false;
689 }
690
691 // scoreboard setup
692 void assault_ScoreRules()
693 {
694         ScoreRules_basics(2, SFL_SORT_PRIO_SECONDARY, SFL_SORT_PRIO_SECONDARY, true);
695         ScoreInfo_SetLabel_TeamScore(  ST_ASSAULT_OBJECTIVES,    "objectives",      SFL_SORT_PRIO_PRIMARY);
696         ScoreInfo_SetLabel_PlayerScore(SP_ASSAULT_OBJECTIVES,    "objectives",      SFL_SORT_PRIO_PRIMARY);
697         ScoreRules_basics_end();
698 }
699
700 #endif