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