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