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