]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_assault.qc
Register score fields
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator / gamemode_assault.qc
1 #include "gamemode_assault.qh"
2 #ifndef GAMEMODE_ASSAULT_H
3 #define GAMEMODE_ASSAULT_H
4
5 void assault_ScoreRules();
6 void ActivateTeamplay();
7
8 REGISTER_MUTATOR(as, false)
9 {
10         ActivateTeamplay();
11         have_team_spawns = -1; // request team spawns
12
13         MUTATOR_ONADD
14         {
15                 if (time > 1) // game loads at time 1
16                         error("This is a game type and it cannot be added at runtime.");
17                 assault_ScoreRules();
18         }
19
20         MUTATOR_ONROLLBACK_OR_REMOVE
21         {
22                 // we actually cannot roll back assault_Initialize here
23                 // BUT: we don't need to! If this gets called, adding always
24                 // succeeds.
25         }
26
27         MUTATOR_ONREMOVE
28         {
29                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
30                 return -1;
31         }
32
33         return 0;
34 }
35
36 // sprites
37 .entity assault_decreaser;
38 .entity assault_sprite;
39
40 // legacy bot defs
41 const int HAVOCBOT_AST_ROLE_NONE = 0;
42 const int HAVOCBOT_AST_ROLE_DEFENSE = 2;
43 const int HAVOCBOT_AST_ROLE_OFFENSE = 4;
44
45 .int havocbot_role_flags;
46 .float havocbot_attack_time;
47
48 .void(entity this) havocbot_role;
49 .void(entity this) havocbot_previous_role;
50
51 void(entity this) havocbot_role_ast_defense;
52 void(entity this) havocbot_role_ast_offense;
53 .entity havocbot_ast_target;
54
55 void(entity bot) havocbot_ast_reset_role;
56
57 void(entity this, float ratingscale, vector org, float sradius) havocbot_goalrating_items;
58 void(entity this, float ratingscale, vector org, float sradius) havocbot_goalrating_enemyplayers;
59
60 // scoreboard stuff
61 const float ST_ASSAULT_OBJECTIVES = 1;
62
63 // predefined spawnfuncs
64 void target_objective_decrease_activate();
65 #endif
66
67 #ifdef IMPLEMENTATION
68 .entity sprite;
69
70 // random functions
71 void assault_objective_use()
72 {SELFPARAM();
73         // activate objective
74         self.health = 100;
75         //print("^2Activated objective ", self.targetname, "=", etos(self), "\n");
76         //print("Activator is ", activator.classname, "\n");
77
78         for (entity e = world; (e = find(e, target, this.targetname)); )
79         {
80                 if (e.classname == "target_objective_decrease")
81                 {
82                         WITHSELF(e, target_objective_decrease_activate());
83                 }
84         }
85
86         setself(this);
87 }
88
89 vector target_objective_spawn_evalfunc(entity player, entity spot, vector current)
90 {SELFPARAM();
91         if(self.health < 0 || self.health >= ASSAULT_VALUE_INACTIVE)
92                 return '-1 0 0';
93         return current;
94 }
95
96 // reset this objective. Used when spawning an objective
97 // and when a new round starts
98 void assault_objective_reset(entity this)
99 {
100         this.health = ASSAULT_VALUE_INACTIVE;
101 }
102
103 // decrease the health of targeted objectives
104 void assault_objective_decrease_use()
105 {SELFPARAM();
106         if(activator.team != assault_attacker_team)
107         {
108                 // wrong team triggered decrease
109                 return;
110         }
111
112         if(other.assault_sprite)
113         {
114                 WaypointSprite_Disown(other.assault_sprite, waypointsprite_deadlifetime);
115                 if(other.classname == "func_assault_destructible")
116                         other.sprite = world;
117         }
118         else
119                 return; // already activated! cannot activate again!
120
121         if(self.enemy.health < ASSAULT_VALUE_INACTIVE)
122         {
123                 if(self.enemy.health - self.dmg > 0.5)
124                 {
125                         PlayerTeamScore_Add(activator, SP_SCORE, ST_SCORE, self.dmg);
126                         self.enemy.health = self.enemy.health - self.dmg;
127                 }
128                 else
129                 {
130                         PlayerTeamScore_Add(activator, SP_SCORE, ST_SCORE, self.enemy.health);
131                         PlayerTeamScore_Add(activator, SP_ASSAULT_OBJECTIVES, ST_ASSAULT_OBJECTIVES, 1);
132                         self.enemy.health = -1;
133
134                         entity oldactivator;
135
136                         setself(this.enemy);
137                         if(self.message)
138                                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(centerprint(it, self.message)));
139
140                         oldactivator = activator;
141                         activator = this;
142                         SUB_UseTargets();
143                         activator = oldactivator;
144                         setself(this);
145                 }
146         }
147 }
148
149 void assault_setenemytoobjective(entity this)
150 {
151         entity objective;
152         for(objective = world; (objective = find(objective, targetname, self.target)); )
153         {
154                 if(objective.classname == "target_objective")
155                 {
156                         if(self.enemy == world)
157                                 self.enemy = objective;
158                         else
159                                 objerror("more than one objective as target - fix the map!");
160                         break;
161                 }
162         }
163
164         if(self.enemy == world)
165                 objerror("no objective as target - fix the map!");
166 }
167
168 float assault_decreaser_sprite_visible(entity e)
169 {SELFPARAM();
170         entity decreaser;
171
172         decreaser = self.assault_decreaser;
173
174         if(decreaser.enemy.health >= ASSAULT_VALUE_INACTIVE)
175                 return false;
176
177         return true;
178 }
179
180 void target_objective_decrease_activate()
181 {SELFPARAM();
182         entity ent, spr;
183         self.owner = world;
184         for(ent = world; (ent = find(ent, target, self.targetname)); )
185         {
186                 if(ent.assault_sprite != world)
187                 {
188                         WaypointSprite_Disown(ent.assault_sprite, waypointsprite_deadlifetime);
189                         if(ent.classname == "func_assault_destructible")
190                                 ent.sprite = world;
191                 }
192
193                 spr = WaypointSprite_SpawnFixed(WP_Assault, 0.5 * (ent.absmin + ent.absmax), ent, assault_sprite, RADARICON_OBJECTIVE);
194                 spr.assault_decreaser = self;
195                 spr.waypointsprite_visible_for_player = assault_decreaser_sprite_visible;
196                 spr.classname = "sprite_waypoint";
197                 WaypointSprite_UpdateRule(spr, assault_attacker_team, SPRITERULE_TEAMPLAY);
198                 if(ent.classname == "func_assault_destructible")
199                 {
200                         WaypointSprite_UpdateSprites(spr, WP_AssaultDefend, WP_AssaultDestroy, WP_AssaultDestroy);
201                         WaypointSprite_UpdateMaxHealth(spr, ent.max_health);
202                         WaypointSprite_UpdateHealth(spr, ent.health);
203                         ent.sprite = spr;
204                 }
205                 else
206                         WaypointSprite_UpdateSprites(spr, WP_AssaultDefend, WP_AssaultPush, WP_AssaultPush);
207         }
208 }
209
210 void target_objective_decrease_findtarget(entity this)
211 {
212         assault_setenemytoobjective(this);
213 }
214
215 void target_assault_roundend_reset(entity this)
216 {
217         //print("round end reset\n");
218         ++this.cnt; // up round counter
219         this.winning = false; // up round
220 }
221
222 void target_assault_roundend_use()
223 {SELFPARAM();
224         self.winning = 1; // round has been won by attackers
225 }
226
227 void assault_roundstart_use(entity this)
228 {
229         activator = self;
230         SUB_UseTargets();
231
232         //(Re)spawn all turrets
233         FOREACH_ENTITY_CLASS("turret_main", true, LAMBDA(
234                 // Swap turret teams
235                 if(it.team == NUM_TEAM_1)
236                         it.team = NUM_TEAM_2;
237                 else
238                         it.team = NUM_TEAM_1;
239
240                 // Dubbles as teamchange
241                 WITHSELF(it, turret_respawn());
242         ));
243 }
244 void assault_roundstart_use_self()
245 {
246     SELFPARAM();
247     assault_roundstart_use(this);
248 }
249
250 void assault_wall_think()
251 {SELFPARAM();
252         if(self.enemy.health < 0)
253         {
254                 self.model = "";
255                 self.solid = SOLID_NOT;
256         }
257         else
258         {
259                 self.model = self.mdl;
260                 self.solid = SOLID_BSP;
261         }
262
263         self.nextthink = time + 0.2;
264 }
265
266 // trigger new round
267 // reset objectives, toggle spawnpoints, reset triggers, ...
268 void vehicles_clearreturn(entity veh);
269 void vehicles_spawn();
270 void assault_new_round()
271 {SELFPARAM();
272         //bprint("ASSAULT: new round\n");
273
274         // Eject players from vehicles
275     FOREACH_CLIENT(IS_PLAYER(it) && it.vehicle, WITHSELF(it, vehicles_exit(VHEF_RELEASE)));
276
277     FOREACH_ENTITY_FLAGS(vehicle_flags, VHF_ISVEHICLE, LAMBDA(
278         setself(it);
279         vehicles_clearreturn(self);
280         vehicles_spawn();
281     ));
282
283     setself(this);
284
285         // up round counter
286         self.winning = self.winning + 1;
287
288         // swap attacker/defender roles
289         if(assault_attacker_team == NUM_TEAM_1)
290                 assault_attacker_team = NUM_TEAM_2;
291         else
292                 assault_attacker_team = NUM_TEAM_1;
293
294         FOREACH_ENTITY(IS_NOT_A_CLIENT(it), LAMBDA(
295                 if (it.team_saved == NUM_TEAM_1) it.team_saved = NUM_TEAM_2;
296                 else if (it.team_saved == NUM_TEAM_2) it.team_saved = NUM_TEAM_1;
297         ));
298
299         // reset the level with a countdown
300         cvar_set("timelimit", ftos(ceil(time - game_starttime) / 60));
301         ReadyRestart_force(); // sets game_starttime
302 }
303
304 // Assault winning condition: If the attackers triggered a round end (by fulfilling all objectives)
305 // they win. Otherwise the defending team wins once the timelimit passes.
306 int WinningCondition_Assault()
307 {
308     SELFPARAM();
309         WinningConditionHelper(); // set worldstatus
310
311         int status = WINNING_NO;
312         // as the timelimit has not yet passed just assume the defending team will win
313         if(assault_attacker_team == NUM_TEAM_1)
314         {
315                 SetWinners(team, NUM_TEAM_2);
316         }
317         else
318         {
319                 SetWinners(team, NUM_TEAM_1);
320         }
321
322         entity ent;
323         ent = find(world, classname, "target_assault_roundend");
324         if(ent)
325         {
326                 if(ent.winning) // round end has been triggered by attacking team
327                 {
328                         bprint("ASSAULT: round completed...\n");
329                         SetWinners(team, assault_attacker_team);
330
331                         TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 666 - TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 0));
332
333                         if(ent.cnt == 1 || autocvar_g_campaign) // this was the second round
334                         {
335                                 status = WINNING_YES;
336                         }
337                         else
338                         {
339                                 WITHSELF(ent, assault_new_round());
340                         }
341                 }
342         }
343
344         return status;
345 }
346
347 // spawnfuncs
348 spawnfunc(info_player_attacker)
349 {
350         if (!g_assault) { remove(self); return; }
351
352         self.team = NUM_TEAM_1; // red, gets swapped every round
353         spawnfunc_info_player_deathmatch(this);
354 }
355
356 spawnfunc(info_player_defender)
357 {
358         if (!g_assault) { remove(self); return; }
359
360         self.team = NUM_TEAM_2; // blue, gets swapped every round
361         spawnfunc_info_player_deathmatch(this);
362 }
363
364 spawnfunc(target_objective)
365 {
366         if (!g_assault) { remove(this); return; }
367
368         this.classname = "target_objective";
369         this.use = assault_objective_use;
370         this.reset = assault_objective_reset;
371         this.reset(this);
372         this.spawn_evalfunc = target_objective_spawn_evalfunc;
373 }
374
375 spawnfunc(target_objective_decrease)
376 {
377         if (!g_assault) { remove(self); return; }
378
379         self.classname = "target_objective_decrease";
380
381         if(!self.dmg)
382                 self.dmg = 101;
383
384         self.use = assault_objective_decrease_use;
385         self.health = ASSAULT_VALUE_INACTIVE;
386         self.max_health = ASSAULT_VALUE_INACTIVE;
387         self.enemy = world;
388
389         InitializeEntity(self, target_objective_decrease_findtarget, INITPRIO_FINDTARGET);
390 }
391
392 // destructible walls that can be used to trigger target_objective_decrease
393 spawnfunc(func_breakable);
394 spawnfunc(func_assault_destructible)
395 {
396         if (!g_assault) { remove(self); return; }
397
398         self.spawnflags = 3;
399         self.classname = "func_assault_destructible";
400
401         if(assault_attacker_team == NUM_TEAM_1)
402                 self.team = NUM_TEAM_2;
403         else
404                 self.team = NUM_TEAM_1;
405
406         spawnfunc_func_breakable(this);
407 }
408
409 spawnfunc(func_assault_wall)
410 {
411         if (!g_assault) { remove(self); return; }
412
413         self.classname = "func_assault_wall";
414         self.mdl = self.model;
415         _setmodel(self, self.mdl);
416         self.solid = SOLID_BSP;
417         self.think = assault_wall_think;
418         self.nextthink = time;
419         InitializeEntity(self, assault_setenemytoobjective, INITPRIO_FINDTARGET);
420 }
421
422 spawnfunc(target_assault_roundend)
423 {
424         if (!g_assault) { remove(self); return; }
425
426         self.winning = 0; // round not yet won by attackers
427         self.classname = "target_assault_roundend";
428         self.use = target_assault_roundend_use;
429         self.cnt = 0; // first round
430         self.reset = target_assault_roundend_reset;
431 }
432
433 spawnfunc(target_assault_roundstart)
434 {
435         if (!g_assault) { remove(self); return; }
436
437         assault_attacker_team = NUM_TEAM_1;
438         self.classname = "target_assault_roundstart";
439         self.use = assault_roundstart_use_self;
440         self.reset2 = assault_roundstart_use_self;
441         InitializeEntity(self, assault_roundstart_use, INITPRIO_FINDTARGET);
442 }
443
444 // legacy bot code
445 void havocbot_goalrating_ast_targets(entity this, float ratingscale)
446 {
447         entity ad, best, wp, tod;
448         float radius, found, bestvalue;
449         vector p;
450
451         ad = findchain(classname, "func_assault_destructible");
452
453         for (; ad; ad = ad.chain)
454         {
455                 if (ad.target == "")
456                         continue;
457
458                 if (!ad.bot_attack)
459                         continue;
460
461                 found = false;
462                 for(tod = world; (tod = find(tod, targetname, ad.target)); )
463                 {
464                         if(tod.classname == "target_objective_decrease")
465                         {
466                                 if(tod.enemy.health > 0 && tod.enemy.health < ASSAULT_VALUE_INACTIVE)
467                                 {
468                                 //      dprint(etos(ad),"\n");
469                                         found = true;
470                                         break;
471                                 }
472                         }
473                 }
474
475                 if(!found)
476                 {
477                 ///     dprint("target not found\n");
478                         continue;
479                 }
480                 /// dprint("target #", etos(ad), " found\n");
481
482
483                 p = 0.5 * (ad.absmin + ad.absmax);
484         //      dprint(vtos(ad.origin), " ", vtos(ad.absmin), " ", vtos(ad.absmax),"\n");
485         //      te_knightspike(p);
486         //      te_lightning2(world, '0 0 0', p);
487
488                 // Find and rate waypoints around it
489                 found = false;
490                 best = world;
491                 bestvalue = 99999999999;
492                 for(radius=0; radius<1500 && !found; radius+=500)
493                 {
494                         for(wp=findradius(p, radius); wp; wp=wp.chain)
495                         {
496                                 if(!(wp.wpflags & WAYPOINTFLAG_GENERATED))
497                                 if(wp.classname=="waypoint")
498                                 if(checkpvs(wp.origin, ad))
499                                 {
500                                         found = true;
501                                         if(wp.cnt<bestvalue)
502                                         {
503                                                 best = wp;
504                                                 bestvalue = wp.cnt;
505                                         }
506                                 }
507                         }
508                 }
509
510                 if(best)
511                 {
512                 ///     dprint("waypoints around target were found\n");
513                 //      te_lightning2(world, '0 0 0', best.origin);
514                 //      te_knightspike(best.origin);
515
516                         navigation_routerating(this, best, ratingscale, 4000);
517                         best.cnt += 1;
518
519                         this.havocbot_attack_time = 0;
520
521                         if(checkpvs(this.view_ofs,ad))
522                         if(checkpvs(this.view_ofs,best))
523                         {
524                         //      dprint("increasing attack time for this target\n");
525                                 this.havocbot_attack_time = time + 2;
526                         }
527                 }
528         }
529 }
530
531 void havocbot_role_ast_offense(entity this)
532 {
533         if(IS_DEAD(this))
534         {
535                 this.havocbot_attack_time = 0;
536                 havocbot_ast_reset_role(this);
537                 return;
538         }
539
540         // Set the role timeout if necessary
541         if (!this.havocbot_role_timeout)
542                 this.havocbot_role_timeout = time + 120;
543
544         if (time > this.havocbot_role_timeout)
545         {
546                 havocbot_ast_reset_role(this);
547                 return;
548         }
549
550         if(this.havocbot_attack_time>time)
551                 return;
552
553         if (this.bot_strategytime < time)
554         {
555                 navigation_goalrating_start(this);
556                 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 650);
557                 havocbot_goalrating_ast_targets(this, 20000);
558                 havocbot_goalrating_items(this, 15000, this.origin, 10000);
559                 navigation_goalrating_end(this);
560
561                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
562         }
563 }
564
565 void havocbot_role_ast_defense(entity this)
566 {
567         if(IS_DEAD(this))
568         {
569                 this.havocbot_attack_time = 0;
570                 havocbot_ast_reset_role(this);
571                 return;
572         }
573
574         // Set the role timeout if necessary
575         if (!this.havocbot_role_timeout)
576                 this.havocbot_role_timeout = time + 120;
577
578         if (time > this.havocbot_role_timeout)
579         {
580                 havocbot_ast_reset_role(this);
581                 return;
582         }
583
584         if(this.havocbot_attack_time>time)
585                 return;
586
587         if (this.bot_strategytime < time)
588         {
589                 navigation_goalrating_start(this);
590                 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 3000);
591                 havocbot_goalrating_ast_targets(this, 20000);
592                 havocbot_goalrating_items(this, 15000, this.origin, 10000);
593                 navigation_goalrating_end(this);
594
595                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
596         }
597 }
598
599 void havocbot_role_ast_setrole(entity this, float role)
600 {
601         switch(role)
602         {
603                 case HAVOCBOT_AST_ROLE_DEFENSE:
604                         this.havocbot_role = havocbot_role_ast_defense;
605                         this.havocbot_role_flags = HAVOCBOT_AST_ROLE_DEFENSE;
606                         this.havocbot_role_timeout = 0;
607                         break;
608                 case HAVOCBOT_AST_ROLE_OFFENSE:
609                         this.havocbot_role = havocbot_role_ast_offense;
610                         this.havocbot_role_flags = HAVOCBOT_AST_ROLE_OFFENSE;
611                         this.havocbot_role_timeout = 0;
612                         break;
613         }
614 }
615
616 void havocbot_ast_reset_role(entity this)
617 {
618         if(IS_DEAD(this))
619                 return;
620
621         if(this.team == assault_attacker_team)
622                 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_OFFENSE);
623         else
624                 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_DEFENSE);
625 }
626
627 // mutator hooks
628 MUTATOR_HOOKFUNCTION(as, PlayerSpawn)
629 {SELFPARAM();
630         if(self.team == assault_attacker_team)
631                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_ASSAULT_ATTACKING);
632         else
633                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_ASSAULT_DEFENDING);
634
635         return false;
636 }
637
638 MUTATOR_HOOKFUNCTION(as, TurretSpawn)
639 {SELFPARAM();
640         if(!self.team || self.team == MAX_SHOT_DISTANCE)
641                 self.team = 5; // this gets reversed when match starts?
642
643         return false;
644 }
645
646 MUTATOR_HOOKFUNCTION(as, VehicleSpawn)
647 {SELFPARAM();
648         self.nextthink = time + 0.5;
649
650         return false;
651 }
652
653 MUTATOR_HOOKFUNCTION(as, HavocBot_ChooseRole)
654 {SELFPARAM();
655         havocbot_ast_reset_role(self);
656         return true;
657 }
658
659 MUTATOR_HOOKFUNCTION(as, PlayHitsound)
660 {
661         return (frag_victim.classname == "func_assault_destructible");
662 }
663
664 MUTATOR_HOOKFUNCTION(as, GetTeamCount)
665 {
666         // assault always has 2 teams
667         c1 = c2 = 0;
668         return true;
669 }
670
671 MUTATOR_HOOKFUNCTION(as, CheckRules_World)
672 {
673         ret_float = WinningCondition_Assault();
674         return true;
675 }
676
677 MUTATOR_HOOKFUNCTION(as, ReadLevelCvars)
678 {
679         // no assault warmups
680         warmup_stage = 0;
681         return false;
682 }
683
684 MUTATOR_HOOKFUNCTION(as, OnEntityPreSpawn)
685 {
686     SELFPARAM();
687         switch(self.classname)
688         {
689                 case "info_player_team1":
690                 case "info_player_team2":
691                 case "info_player_team3":
692                 case "info_player_team4":
693                         return true;
694         }
695
696         return false;
697 }
698
699 // scoreboard setup
700 void assault_ScoreRules()
701 {
702         ScoreRules_basics(2, SFL_SORT_PRIO_SECONDARY, SFL_SORT_PRIO_SECONDARY, true);
703         ScoreInfo_SetLabel_TeamScore(  ST_ASSAULT_OBJECTIVES,    "objectives",      SFL_SORT_PRIO_PRIMARY);
704         ScoreInfo_SetLabel_PlayerScore(SP_ASSAULT_OBJECTIVES,    "objectives",      SFL_SORT_PRIO_PRIMARY);
705         ScoreRules_basics_end();
706 }
707
708 #endif