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