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