]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator/gamemode_assault.qc
Merge branch 'master' into TimePath/stats
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator / gamemode_assault.qc
1 #ifndef GAMEMODE_ASSAULT_H
2 #define GAMEMODE_ASSAULT_H
3
4 void assault_ScoreRules();
5
6 REGISTER_MUTATOR(as, false)
7 {
8         ActivateTeamplay();
9         have_team_spawns = -1; // request team spawns
10
11         MUTATOR_ONADD
12         {
13                 if (time > 1) // game loads at time 1
14                         error("This is a game type and it cannot be added at runtime.");
15                 assault_ScoreRules();
16         }
17
18         MUTATOR_ONROLLBACK_OR_REMOVE
19         {
20                 // we actually cannot roll back assault_Initialize here
21                 // BUT: we don't need to! If this gets called, adding always
22                 // succeeds.
23         }
24
25         MUTATOR_ONREMOVE
26         {
27                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
28                 return -1;
29         }
30
31         return 0;
32 }
33
34 // sprites
35 .entity assault_decreaser;
36 .entity assault_sprite;
37
38 // legacy bot defs
39 const int HAVOCBOT_AST_ROLE_NONE = 0;
40 const int HAVOCBOT_AST_ROLE_DEFENSE = 2;
41 const int HAVOCBOT_AST_ROLE_OFFENSE = 4;
42
43 .int havocbot_role_flags;
44 .float havocbot_attack_time;
45
46 .void() havocbot_role;
47 .void() havocbot_previous_role;
48
49 void() havocbot_role_ast_defense;
50 void() havocbot_role_ast_offense;
51 .entity havocbot_ast_target;
52
53 void(entity bot) havocbot_ast_reset_role;
54
55 void(float ratingscale, vector org, float sradius) havocbot_goalrating_items;
56 void(float ratingscale, vector org, float sradius) havocbot_goalrating_enemyplayers;
57
58 // scoreboard stuff
59 const float ST_ASSAULT_OBJECTIVES = 1;
60 const float SP_ASSAULT_OBJECTIVES = 4;
61
62 // predefined spawnfuncs
63 void target_objective_decrease_activate();
64 #endif
65
66 #ifdef IMPLEMENTATION
67 .entity sprite;
68
69 // random functions
70 void assault_objective_use()
71 {SELFPARAM();
72         // activate objective
73         self.health = 100;
74         //print("^2Activated objective ", self.targetname, "=", etos(self), "\n");
75         //print("Activator is ", activator.classname, "\n");
76
77         for (entity e = world; (e = find(e, target, this.targetname)); )
78         {
79                 if (e.classname == "target_objective_decrease")
80                 {
81                         WITH(entity, self, e, target_objective_decrease_activate());
82                 }
83         }
84
85         setself(this);
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()
104 {SELFPARAM();
105         if(activator.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(activator, SP_SCORE, ST_SCORE, self.dmg);
125                         self.enemy.health = self.enemy.health - self.dmg;
126                 }
127                 else
128                 {
129                         PlayerTeamScore_Add(activator, SP_SCORE, ST_SCORE, self.enemy.health);
130                         PlayerTeamScore_Add(activator, SP_ASSAULT_OBJECTIVES, ST_ASSAULT_OBJECTIVES, 1);
131                         self.enemy.health = -1;
132
133                         entity oldactivator, head;
134
135                         setself(this.enemy);
136                         if(self.message)
137                         FOR_EACH_PLAYER(head)
138                                 centerprint(head, 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()
150 {SELFPARAM();
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()
211 {
212         assault_setenemytoobjective();
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()
228 {SELFPARAM();
229         activator = self;
230         SUB_UseTargets();
231
232         //(Re)spawn all turrets
233         for(entity ent = NULL; (ent = find(ent, classname, "turret_main")); ) {
234                 // Swap turret teams
235                 if(ent.team == NUM_TEAM_1)
236                         ent.team = NUM_TEAM_2;
237                 else
238                         ent.team = NUM_TEAM_1;
239
240                 // Dubbles as teamchange
241                 WITH(entity, self, ent, turret_respawn());
242         }
243 }
244
245 void assault_wall_think()
246 {SELFPARAM();
247         if(self.enemy.health < 0)
248         {
249                 self.model = "";
250                 self.solid = SOLID_NOT;
251         }
252         else
253         {
254                 self.model = self.mdl;
255                 self.solid = SOLID_BSP;
256         }
257
258         self.nextthink = time + 0.2;
259 }
260
261 // trigger new round
262 // reset objectives, toggle spawnpoints, reset triggers, ...
263 void vehicles_clearreturn(entity veh);
264 void vehicles_spawn();
265 void assault_new_round()
266 {SELFPARAM();
267         //bprint("ASSAULT: new round\n");
268
269         // Eject players from vehicles
270         entity e;
271     FOR_EACH_PLAYER(e)
272     {
273         if(e.vehicle)
274         {
275                 WITH(entity, self, e, vehicles_exit(VHEF_RELEASE));
276         }
277     }
278
279     for (entity e_ = findchainflags(vehicle_flags, VHF_ISVEHICLE); e_; e_ = e_.chain)
280     {
281         setself(e_);
282         vehicles_clearreturn(self);
283         vehicles_spawn();
284     }
285
286     setself(this);
287
288         // up round counter
289         self.winning = self.winning + 1;
290
291         // swap attacker/defender roles
292         if(assault_attacker_team == NUM_TEAM_1)
293                 assault_attacker_team = NUM_TEAM_2;
294         else
295                 assault_attacker_team = NUM_TEAM_1;
296
297         FOREACH_ENTITY(clienttype(it) == CLIENTTYPE_NOTACLIENT, LAMBDA(
298                 if (it.team_saved == NUM_TEAM_1) it.team_saved = NUM_TEAM_2;
299                 else if (it.team_saved == NUM_TEAM_2) it.team_saved = NUM_TEAM_1;
300         ));
301
302         // reset the level with a countdown
303         cvar_set("timelimit", ftos(ceil(time - game_starttime) / 60));
304         ReadyRestart_force(); // sets game_starttime
305 }
306
307 // spawnfuncs
308 spawnfunc(info_player_attacker)
309 {
310         if (!g_assault) { remove(self); return; }
311
312         self.team = NUM_TEAM_1; // red, gets swapped every round
313         spawnfunc_info_player_deathmatch(this);
314 }
315
316 spawnfunc(info_player_defender)
317 {
318         if (!g_assault) { remove(self); return; }
319
320         self.team = NUM_TEAM_2; // blue, gets swapped every round
321         spawnfunc_info_player_deathmatch(this);
322 }
323
324 spawnfunc(target_objective)
325 {
326         if (!g_assault) { remove(this); return; }
327
328         this.classname = "target_objective";
329         this.use = assault_objective_use;
330         this.reset = assault_objective_reset;
331         this.reset(this);
332         this.spawn_evalfunc = target_objective_spawn_evalfunc;
333 }
334
335 spawnfunc(target_objective_decrease)
336 {
337         if (!g_assault) { remove(self); return; }
338
339         self.classname = "target_objective_decrease";
340
341         if(!self.dmg)
342                 self.dmg = 101;
343
344         self.use = assault_objective_decrease_use;
345         self.health = ASSAULT_VALUE_INACTIVE;
346         self.max_health = ASSAULT_VALUE_INACTIVE;
347         self.enemy = world;
348
349         InitializeEntity(self, target_objective_decrease_findtarget, INITPRIO_FINDTARGET);
350 }
351
352 // destructible walls that can be used to trigger target_objective_decrease
353 spawnfunc(func_breakable);
354 spawnfunc(func_assault_destructible)
355 {
356         if (!g_assault) { remove(self); return; }
357
358         self.spawnflags = 3;
359         self.classname = "func_assault_destructible";
360
361         if(assault_attacker_team == NUM_TEAM_1)
362                 self.team = NUM_TEAM_2;
363         else
364                 self.team = NUM_TEAM_1;
365
366         spawnfunc_func_breakable(this);
367 }
368
369 spawnfunc(func_assault_wall)
370 {
371         if (!g_assault) { remove(self); return; }
372
373         self.classname = "func_assault_wall";
374         self.mdl = self.model;
375         _setmodel(self, self.mdl);
376         self.solid = SOLID_BSP;
377         self.think = assault_wall_think;
378         self.nextthink = time;
379         InitializeEntity(self, assault_setenemytoobjective, INITPRIO_FINDTARGET);
380 }
381
382 spawnfunc(target_assault_roundend)
383 {
384         if (!g_assault) { remove(self); return; }
385
386         self.winning = 0; // round not yet won by attackers
387         self.classname = "target_assault_roundend";
388         self.use = target_assault_roundend_use;
389         self.cnt = 0; // first round
390         self.reset = target_assault_roundend_reset;
391 }
392
393 spawnfunc(target_assault_roundstart)
394 {
395         if (!g_assault) { remove(self); return; }
396
397         assault_attacker_team = NUM_TEAM_1;
398         self.classname = "target_assault_roundstart";
399         self.use = assault_roundstart_use;
400         self.reset2 = assault_roundstart_use;
401         InitializeEntity(self, assault_roundstart_use, INITPRIO_FINDTARGET);
402 }
403
404 // legacy bot code
405 void havocbot_goalrating_ast_targets(float ratingscale)
406 {SELFPARAM();
407         entity ad, best, wp, tod;
408         float radius, found, bestvalue;
409         vector p;
410
411         ad = findchain(classname, "func_assault_destructible");
412
413         for (; ad; ad = ad.chain)
414         {
415                 if (ad.target == "")
416                         continue;
417
418                 if (!ad.bot_attack)
419                         continue;
420
421                 found = false;
422                 for(tod = world; (tod = find(tod, targetname, ad.target)); )
423                 {
424                         if(tod.classname == "target_objective_decrease")
425                         {
426                                 if(tod.enemy.health > 0 && tod.enemy.health < ASSAULT_VALUE_INACTIVE)
427                                 {
428                                 //      dprint(etos(ad),"\n");
429                                         found = true;
430                                         break;
431                                 }
432                         }
433                 }
434
435                 if(!found)
436                 {
437                 ///     dprint("target not found\n");
438                         continue;
439                 }
440                 /// dprint("target #", etos(ad), " found\n");
441
442
443                 p = 0.5 * (ad.absmin + ad.absmax);
444         //      dprint(vtos(ad.origin), " ", vtos(ad.absmin), " ", vtos(ad.absmax),"\n");
445         //      te_knightspike(p);
446         //      te_lightning2(world, '0 0 0', p);
447
448                 // Find and rate waypoints around it
449                 found = false;
450                 best = world;
451                 bestvalue = 99999999999;
452                 for(radius=0; radius<1500 && !found; radius+=500)
453                 {
454                         for(wp=findradius(p, radius); wp; wp=wp.chain)
455                         {
456                                 if(!(wp.wpflags & WAYPOINTFLAG_GENERATED))
457                                 if(wp.classname=="waypoint")
458                                 if(checkpvs(wp.origin, ad))
459                                 {
460                                         found = true;
461                                         if(wp.cnt<bestvalue)
462                                         {
463                                                 best = wp;
464                                                 bestvalue = wp.cnt;
465                                         }
466                                 }
467                         }
468                 }
469
470                 if(best)
471                 {
472                 ///     dprint("waypoints around target were found\n");
473                 //      te_lightning2(world, '0 0 0', best.origin);
474                 //      te_knightspike(best.origin);
475
476                         navigation_routerating(best, ratingscale, 4000);
477                         best.cnt += 1;
478
479                         self.havocbot_attack_time = 0;
480
481                         if(checkpvs(self.view_ofs,ad))
482                         if(checkpvs(self.view_ofs,best))
483                         {
484                         //      dprint("increasing attack time for this target\n");
485                                 self.havocbot_attack_time = time + 2;
486                         }
487                 }
488         }
489 }
490
491 void havocbot_role_ast_offense()
492 {SELFPARAM();
493         if(self.deadflag != DEAD_NO)
494         {
495                 self.havocbot_attack_time = 0;
496                 havocbot_ast_reset_role(self);
497                 return;
498         }
499
500         // Set the role timeout if necessary
501         if (!self.havocbot_role_timeout)
502                 self.havocbot_role_timeout = time + 120;
503
504         if (time > self.havocbot_role_timeout)
505         {
506                 havocbot_ast_reset_role(self);
507                 return;
508         }
509
510         if(self.havocbot_attack_time>time)
511                 return;
512
513         if (self.bot_strategytime < time)
514         {
515                 navigation_goalrating_start();
516                 havocbot_goalrating_enemyplayers(20000, self.origin, 650);
517                 havocbot_goalrating_ast_targets(20000);
518                 havocbot_goalrating_items(15000, self.origin, 10000);
519                 navigation_goalrating_end();
520
521                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
522         }
523 }
524
525 void havocbot_role_ast_defense()
526 {SELFPARAM();
527         if(self.deadflag != DEAD_NO)
528         {
529                 self.havocbot_attack_time = 0;
530                 havocbot_ast_reset_role(self);
531                 return;
532         }
533
534         // Set the role timeout if necessary
535         if (!self.havocbot_role_timeout)
536                 self.havocbot_role_timeout = time + 120;
537
538         if (time > self.havocbot_role_timeout)
539         {
540                 havocbot_ast_reset_role(self);
541                 return;
542         }
543
544         if(self.havocbot_attack_time>time)
545                 return;
546
547         if (self.bot_strategytime < time)
548         {
549                 navigation_goalrating_start();
550                 havocbot_goalrating_enemyplayers(20000, self.origin, 3000);
551                 havocbot_goalrating_ast_targets(20000);
552                 havocbot_goalrating_items(15000, self.origin, 10000);
553                 navigation_goalrating_end();
554
555                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
556         }
557 }
558
559 void havocbot_role_ast_setrole(entity bot, float role)
560 {
561         switch(role)
562         {
563                 case HAVOCBOT_AST_ROLE_DEFENSE:
564                         bot.havocbot_role = havocbot_role_ast_defense;
565                         bot.havocbot_role_flags = HAVOCBOT_AST_ROLE_DEFENSE;
566                         bot.havocbot_role_timeout = 0;
567                         break;
568                 case HAVOCBOT_AST_ROLE_OFFENSE:
569                         bot.havocbot_role = havocbot_role_ast_offense;
570                         bot.havocbot_role_flags = HAVOCBOT_AST_ROLE_OFFENSE;
571                         bot.havocbot_role_timeout = 0;
572                         break;
573         }
574 }
575
576 void havocbot_ast_reset_role(entity bot)
577 {SELFPARAM();
578         if(self.deadflag != DEAD_NO)
579                 return;
580
581         if(bot.team == assault_attacker_team)
582                 havocbot_role_ast_setrole(bot, HAVOCBOT_AST_ROLE_OFFENSE);
583         else
584                 havocbot_role_ast_setrole(bot, HAVOCBOT_AST_ROLE_DEFENSE);
585 }
586
587 // mutator hooks
588 MUTATOR_HOOKFUNCTION(as, PlayerSpawn)
589 {SELFPARAM();
590         if(self.team == assault_attacker_team)
591                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_ASSAULT_ATTACKING);
592         else
593                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_ASSAULT_DEFENDING);
594
595         return false;
596 }
597
598 MUTATOR_HOOKFUNCTION(as, TurretSpawn)
599 {SELFPARAM();
600         if(!self.team || self.team == MAX_SHOT_DISTANCE)
601                 self.team = 5; // this gets reversed when match starts?
602
603         return false;
604 }
605
606 MUTATOR_HOOKFUNCTION(as, VehicleSpawn)
607 {SELFPARAM();
608         self.nextthink = time + 0.5;
609
610         return false;
611 }
612
613 MUTATOR_HOOKFUNCTION(as, HavocBot_ChooseRole)
614 {SELFPARAM();
615         havocbot_ast_reset_role(self);
616         return true;
617 }
618
619 MUTATOR_HOOKFUNCTION(as, PlayHitsound)
620 {
621         return (frag_victim.classname == "func_assault_destructible");
622 }
623
624 MUTATOR_HOOKFUNCTION(as, GetTeamCount)
625 {
626         // assault always has 2 teams
627         c1 = c2 = 0;
628         return true;
629 }
630
631 MUTATOR_HOOKFUNCTION(as, CheckRules_World)
632 {
633         ret_float = WinningCondition_Assault();
634         return true;
635 }
636
637 MUTATOR_HOOKFUNCTION(as, ReadLevelCvars)
638 {
639         // no assault warmups
640         warmup_stage = 0;
641         return false;
642 }
643
644 MUTATOR_HOOKFUNCTION(as, OnEntityPreSpawn)
645 {
646         switch(self.classname)
647         {
648                 case "info_player_team1":
649                 case "info_player_team2":
650                 case "info_player_team3":
651                 case "info_player_team4":
652                         return true;
653         }
654
655         return false;
656 }
657
658 // scoreboard setup
659 void assault_ScoreRules()
660 {
661         ScoreRules_basics(2, SFL_SORT_PRIO_SECONDARY, SFL_SORT_PRIO_SECONDARY, true);
662         ScoreInfo_SetLabel_TeamScore(  ST_ASSAULT_OBJECTIVES,    "objectives",      SFL_SORT_PRIO_PRIMARY);
663         ScoreInfo_SetLabel_PlayerScore(SP_ASSAULT_OBJECTIVES,    "objectives",      SFL_SORT_PRIO_PRIMARY);
664         ScoreRules_basics_end();
665 }
666
667 #endif