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