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