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