]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_assault.qc
Fix the use of self, activator and other globals in .use
[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 player, entity spot, vector current)
89 {SELFPARAM();
90         if(self.health < 0 || self.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(other.assault_sprite)
112         {
113                 WaypointSprite_Disown(other.assault_sprite, waypointsprite_deadlifetime);
114                 if(other.classname == "func_assault_destructible")
115                         other.sprite = world;
116         }
117         else
118                 return; // already activated! cannot activate again!
119
120         if(self.enemy.health < ASSAULT_VALUE_INACTIVE)
121         {
122                 if(self.enemy.health - self.dmg > 0.5)
123                 {
124                         PlayerTeamScore_Add(actor, SP_SCORE, ST_SCORE, self.dmg);
125                         self.enemy.health = self.enemy.health - self.dmg;
126                 }
127                 else
128                 {
129                         PlayerTeamScore_Add(actor, SP_SCORE, ST_SCORE, self.enemy.health);
130                         PlayerTeamScore_Add(actor, SP_ASSAULT_OBJECTIVES, ST_ASSAULT_OBJECTIVES, 1);
131                         self.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()
142 {SELFPARAM();
143         entity objective;
144         for(objective = world; (objective = find(objective, targetname, self.target)); )
145         {
146                 if(objective.classname == "target_objective")
147                 {
148                         if(self.enemy == world)
149                                 self.enemy = objective;
150                         else
151                                 objerror("more than one objective as target - fix the map!");
152                         break;
153                 }
154         }
155
156         if(self.enemy == world)
157                 objerror("no objective as target - fix the map!");
158 }
159
160 float assault_decreaser_sprite_visible(entity e)
161 {SELFPARAM();
162         entity decreaser;
163
164         decreaser = self.assault_decreaser;
165
166         if(decreaser.enemy.health >= ASSAULT_VALUE_INACTIVE)
167                 return false;
168
169         return true;
170 }
171
172 void target_objective_decrease_activate(entity this)
173 {
174         entity ent, spr;
175         this.owner = world;
176         for(ent = world; (ent = find(ent, target, this.targetname)); )
177         {
178                 if(ent.assault_sprite != world)
179                 {
180                         WaypointSprite_Disown(ent.assault_sprite, waypointsprite_deadlifetime);
181                         if(ent.classname == "func_assault_destructible")
182                                 ent.sprite = world;
183                 }
184
185                 spr = WaypointSprite_SpawnFixed(WP_Assault, 0.5 * (ent.absmin + ent.absmax), ent, assault_sprite, RADARICON_OBJECTIVE);
186                 spr.assault_decreaser = this;
187                 spr.waypointsprite_visible_for_player = assault_decreaser_sprite_visible;
188                 spr.classname = "sprite_waypoint";
189                 WaypointSprite_UpdateRule(spr, assault_attacker_team, SPRITERULE_TEAMPLAY);
190                 if(ent.classname == "func_assault_destructible")
191                 {
192                         WaypointSprite_UpdateSprites(spr, WP_AssaultDefend, WP_AssaultDestroy, WP_AssaultDestroy);
193                         WaypointSprite_UpdateMaxHealth(spr, ent.max_health);
194                         WaypointSprite_UpdateHealth(spr, ent.health);
195                         ent.sprite = spr;
196                 }
197                 else
198                         WaypointSprite_UpdateSprites(spr, WP_AssaultDefend, WP_AssaultPush, WP_AssaultPush);
199         }
200 }
201
202 void target_objective_decrease_findtarget()
203 {
204         assault_setenemytoobjective();
205 }
206
207 void target_assault_roundend_reset(entity this)
208 {
209         //print("round end reset\n");
210         ++this.cnt; // up round counter
211         this.winning = false; // up round
212 }
213
214 void target_assault_roundend_use(entity this, entity actor, entity trigger)
215 {
216         this.winning = 1; // round has been won by attackers
217 }
218
219 void assault_roundstart_use(entity this, entity actor, entity trigger)
220 {
221         SUB_UseTargets(this, this, trigger);
222
223         //(Re)spawn all turrets
224         FOREACH_ENTITY_CLASS("turret_main", true, LAMBDA(
225                 // Swap turret teams
226                 if(it.team == NUM_TEAM_1)
227                         it.team = NUM_TEAM_2;
228                 else
229                         it.team = NUM_TEAM_1;
230
231                 // Dubbles as teamchange
232                 WITHSELF(it, turret_respawn());
233         ));
234 }
235
236 void assault_wall_think()
237 {SELFPARAM();
238         if(self.enemy.health < 0)
239         {
240                 self.model = "";
241                 self.solid = SOLID_NOT;
242         }
243         else
244         {
245                 self.model = self.mdl;
246                 self.solid = SOLID_BSP;
247         }
248
249         self.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();
256 void assault_new_round()
257 {SELFPARAM();
258         //bprint("ASSAULT: new round\n");
259
260         // Eject players from vehicles
261     FOREACH_CLIENT(IS_PLAYER(it) && it.vehicle, WITHSELF(it, vehicles_exit(VHEF_RELEASE)));
262
263     FOREACH_ENTITY_FLAGS(vehicle_flags, VHF_ISVEHICLE, LAMBDA(
264         setself(it);
265         vehicles_clearreturn(self);
266         vehicles_spawn();
267     ));
268
269     setself(this);
270
271         // up round counter
272         self.winning = self.winning + 1;
273
274         // swap attacker/defender roles
275         if(assault_attacker_team == NUM_TEAM_1)
276                 assault_attacker_team = NUM_TEAM_2;
277         else
278                 assault_attacker_team = NUM_TEAM_1;
279
280         FOREACH_ENTITY(IS_NOT_A_CLIENT(it), LAMBDA(
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     SELFPARAM();
295         WinningConditionHelper(); // 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(world, 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                                 WITHSELF(ent, assault_new_round());
326                         }
327                 }
328         }
329
330         return status;
331 }
332
333 // spawnfuncs
334 spawnfunc(info_player_attacker)
335 {
336         if (!g_assault) { remove(self); return; }
337
338         self.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) { remove(self); return; }
345
346         self.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) { remove(this); return; }
353
354         this.classname = "target_objective";
355         this.use1 = 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) { remove(self); return; }
364
365         self.classname = "target_objective_decrease";
366
367         if(!self.dmg)
368                 self.dmg = 101;
369
370         self.use1 = assault_objective_decrease_use;
371         self.health = ASSAULT_VALUE_INACTIVE;
372         self.max_health = ASSAULT_VALUE_INACTIVE;
373         self.enemy = world;
374
375         InitializeEntity(self, 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) { remove(self); return; }
383
384         self.spawnflags = 3;
385         self.classname = "func_assault_destructible";
386
387         if(assault_attacker_team == NUM_TEAM_1)
388                 self.team = NUM_TEAM_2;
389         else
390                 self.team = NUM_TEAM_1;
391
392         spawnfunc_func_breakable(this);
393 }
394
395 spawnfunc(func_assault_wall)
396 {
397         if (!g_assault) { remove(self); return; }
398
399         self.classname = "func_assault_wall";
400         self.mdl = self.model;
401         _setmodel(self, self.mdl);
402         self.solid = SOLID_BSP;
403         self.think = assault_wall_think;
404         self.nextthink = time;
405         InitializeEntity(self, assault_setenemytoobjective, INITPRIO_FINDTARGET);
406 }
407
408 spawnfunc(target_assault_roundend)
409 {
410         if (!g_assault) { remove(self); return; }
411
412         self.winning = 0; // round not yet won by attackers
413         self.classname = "target_assault_roundend";
414         self.use1 = target_assault_roundend_use;
415         self.cnt = 0; // first round
416         self.reset = target_assault_roundend_reset;
417 }
418
419 spawnfunc(target_assault_roundstart)
420 {
421         if (!g_assault) { remove(self); return; }
422
423         assault_attacker_team = NUM_TEAM_1;
424         self.classname = "target_assault_roundstart";
425         self.use1 = assault_roundstart_use;
426         self.reset2 = adaptor_think2use;
427         InitializeEntity(self, adaptor_think2use, INITPRIO_FINDTARGET);
428 }
429
430 // legacy bot code
431 void havocbot_goalrating_ast_targets(entity this, float ratingscale)
432 {
433         entity ad, best, wp, tod;
434         float radius, found, bestvalue;
435         vector p;
436
437         ad = findchain(classname, "func_assault_destructible");
438
439         for (; ad; ad = ad.chain)
440         {
441                 if (ad.target == "")
442                         continue;
443
444                 if (!ad.bot_attack)
445                         continue;
446
447                 found = false;
448                 for(tod = world; (tod = find(tod, targetname, ad.target)); )
449                 {
450                         if(tod.classname == "target_objective_decrease")
451                         {
452                                 if(tod.enemy.health > 0 && tod.enemy.health < ASSAULT_VALUE_INACTIVE)
453                                 {
454                                 //      dprint(etos(ad),"\n");
455                                         found = true;
456                                         break;
457                                 }
458                         }
459                 }
460
461                 if(!found)
462                 {
463                 ///     dprint("target not found\n");
464                         continue;
465                 }
466                 /// dprint("target #", etos(ad), " found\n");
467
468
469                 p = 0.5 * (ad.absmin + ad.absmax);
470         //      dprint(vtos(ad.origin), " ", vtos(ad.absmin), " ", vtos(ad.absmax),"\n");
471         //      te_knightspike(p);
472         //      te_lightning2(world, '0 0 0', p);
473
474                 // Find and rate waypoints around it
475                 found = false;
476                 best = world;
477                 bestvalue = 99999999999;
478                 for(radius=0; radius<1500 && !found; radius+=500)
479                 {
480                         for(wp=findradius(p, radius); wp; wp=wp.chain)
481                         {
482                                 if(!(wp.wpflags & WAYPOINTFLAG_GENERATED))
483                                 if(wp.classname=="waypoint")
484                                 if(checkpvs(wp.origin, ad))
485                                 {
486                                         found = true;
487                                         if(wp.cnt<bestvalue)
488                                         {
489                                                 best = wp;
490                                                 bestvalue = wp.cnt;
491                                         }
492                                 }
493                         }
494                 }
495
496                 if(best)
497                 {
498                 ///     dprint("waypoints around target were found\n");
499                 //      te_lightning2(world, '0 0 0', best.origin);
500                 //      te_knightspike(best.origin);
501
502                         navigation_routerating(this, best, ratingscale, 4000);
503                         best.cnt += 1;
504
505                         this.havocbot_attack_time = 0;
506
507                         if(checkpvs(this.view_ofs,ad))
508                         if(checkpvs(this.view_ofs,best))
509                         {
510                         //      dprint("increasing attack time for this target\n");
511                                 this.havocbot_attack_time = time + 2;
512                         }
513                 }
514         }
515 }
516
517 void havocbot_role_ast_offense(entity this)
518 {
519         if(IS_DEAD(this))
520         {
521                 this.havocbot_attack_time = 0;
522                 havocbot_ast_reset_role(this);
523                 return;
524         }
525
526         // Set the role timeout if necessary
527         if (!this.havocbot_role_timeout)
528                 this.havocbot_role_timeout = time + 120;
529
530         if (time > this.havocbot_role_timeout)
531         {
532                 havocbot_ast_reset_role(this);
533                 return;
534         }
535
536         if(this.havocbot_attack_time>time)
537                 return;
538
539         if (this.bot_strategytime < time)
540         {
541                 navigation_goalrating_start(this);
542                 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 650);
543                 havocbot_goalrating_ast_targets(this, 20000);
544                 havocbot_goalrating_items(this, 15000, this.origin, 10000);
545                 navigation_goalrating_end(this);
546
547                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
548         }
549 }
550
551 void havocbot_role_ast_defense(entity this)
552 {
553         if(IS_DEAD(this))
554         {
555                 this.havocbot_attack_time = 0;
556                 havocbot_ast_reset_role(this);
557                 return;
558         }
559
560         // Set the role timeout if necessary
561         if (!this.havocbot_role_timeout)
562                 this.havocbot_role_timeout = time + 120;
563
564         if (time > this.havocbot_role_timeout)
565         {
566                 havocbot_ast_reset_role(this);
567                 return;
568         }
569
570         if(this.havocbot_attack_time>time)
571                 return;
572
573         if (this.bot_strategytime < time)
574         {
575                 navigation_goalrating_start(this);
576                 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 3000);
577                 havocbot_goalrating_ast_targets(this, 20000);
578                 havocbot_goalrating_items(this, 15000, this.origin, 10000);
579                 navigation_goalrating_end(this);
580
581                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
582         }
583 }
584
585 void havocbot_role_ast_setrole(entity this, float role)
586 {
587         switch(role)
588         {
589                 case HAVOCBOT_AST_ROLE_DEFENSE:
590                         this.havocbot_role = havocbot_role_ast_defense;
591                         this.havocbot_role_flags = HAVOCBOT_AST_ROLE_DEFENSE;
592                         this.havocbot_role_timeout = 0;
593                         break;
594                 case HAVOCBOT_AST_ROLE_OFFENSE:
595                         this.havocbot_role = havocbot_role_ast_offense;
596                         this.havocbot_role_flags = HAVOCBOT_AST_ROLE_OFFENSE;
597                         this.havocbot_role_timeout = 0;
598                         break;
599         }
600 }
601
602 void havocbot_ast_reset_role(entity this)
603 {
604         if(IS_DEAD(this))
605                 return;
606
607         if(this.team == assault_attacker_team)
608                 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_OFFENSE);
609         else
610                 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_DEFENSE);
611 }
612
613 // mutator hooks
614 MUTATOR_HOOKFUNCTION(as, PlayerSpawn)
615 {SELFPARAM();
616         if(self.team == assault_attacker_team)
617                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_ASSAULT_ATTACKING);
618         else
619                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_ASSAULT_DEFENDING);
620
621         return false;
622 }
623
624 MUTATOR_HOOKFUNCTION(as, TurretSpawn)
625 {SELFPARAM();
626         if(!self.team || self.team == MAX_SHOT_DISTANCE)
627                 self.team = 5; // this gets reversed when match starts?
628
629         return false;
630 }
631
632 MUTATOR_HOOKFUNCTION(as, VehicleSpawn)
633 {SELFPARAM();
634         self.nextthink = time + 0.5;
635
636         return false;
637 }
638
639 MUTATOR_HOOKFUNCTION(as, HavocBot_ChooseRole)
640 {SELFPARAM();
641         havocbot_ast_reset_role(self);
642         return true;
643 }
644
645 MUTATOR_HOOKFUNCTION(as, PlayHitsound)
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         ret_float = WinningCondition_Assault();
660         return true;
661 }
662
663 MUTATOR_HOOKFUNCTION(as, ReadLevelCvars)
664 {
665         // no assault warmups
666         warmup_stage = 0;
667         return false;
668 }
669
670 MUTATOR_HOOKFUNCTION(as, OnEntityPreSpawn)
671 {
672     SELFPARAM();
673         switch(self.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         return false;
683 }
684
685 // scoreboard setup
686 void assault_ScoreRules()
687 {
688         ScoreRules_basics(2, SFL_SORT_PRIO_SECONDARY, SFL_SORT_PRIO_SECONDARY, true);
689         ScoreInfo_SetLabel_TeamScore(  ST_ASSAULT_OBJECTIVES,    "objectives",      SFL_SORT_PRIO_PRIMARY);
690         ScoreInfo_SetLabel_PlayerScore(SP_ASSAULT_OBJECTIVES,    "objectives",      SFL_SORT_PRIO_PRIMARY);
691         ScoreRules_basics_end();
692 }
693
694 #endif