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