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