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