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