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