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