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