]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/assault/sv_assault.qc
Further cleanup of 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 <server/g_damage.qh>
7 #include <server/g_world.qh>
8 #include <server/spawnpoints.qh>
9
10 .entity sprite;
11 #define AS_ROUND_DELAY 5
12
13 IntrusiveList g_assault_destructibles;
14 IntrusiveList g_assault_objectivedecreasers;
15 IntrusiveList g_assault_objectives;
16 STATIC_INIT(g_assault)
17 {
18         g_assault_destructibles = IL_NEW();
19         g_assault_objectivedecreasers = IL_NEW();
20         g_assault_objectives = IL_NEW();
21 }
22
23 // random functions
24 void assault_objective_use(entity this, entity actor, entity trigger)
25 {
26         // activate objective
27         SetResourceExplicit(this, RES_HEALTH, 100);
28         //print("^2Activated objective ", this.targetname, "=", etos(this), "\n");
29         //print("Activator is ", actor.classname, "\n");
30
31         IL_EACH(g_assault_objectivedecreasers, it.target == this.targetname,
32         {
33                 target_objective_decrease_activate(it);
34         });
35 }
36
37 vector target_objective_spawn_evalfunc(entity this, entity player, entity spot, vector current)
38 {
39         float hlth = GetResource(this, RES_HEALTH);
40         if (hlth < 0 || hlth >= ASSAULT_VALUE_INACTIVE)
41                 return '-1 0 0';
42         return current;
43 }
44
45 // reset this objective. Used when spawning an objective
46 // and when a new round starts
47 void assault_objective_reset(entity this)
48 {
49         SetResourceExplicit(this, RES_HEALTH, ASSAULT_VALUE_INACTIVE);
50 }
51
52 // decrease the health of targeted objectives
53 void assault_objective_decrease_use(entity this, entity actor, entity trigger)
54 {
55         if(actor.team != assault_attacker_team)
56         {
57                 // wrong team triggered decrease
58                 return;
59         }
60
61         if(trigger.assault_sprite)
62         {
63                 WaypointSprite_Disown(trigger.assault_sprite, waypointsprite_deadlifetime);
64                 if(trigger.classname == "func_assault_destructible")
65                         trigger.sprite = NULL; // TODO: just unsetting it?!
66         }
67         else
68                 return; // already activated! cannot activate again!
69
70         float hlth = GetResource(this.enemy, RES_HEALTH);
71         if (hlth < ASSAULT_VALUE_INACTIVE)
72         {
73                 if (hlth - this.dmg > 0.5)
74                 {
75                         GameRules_scoring_add_team(actor, SCORE, this.dmg);
76                         TakeResource(this.enemy, RES_HEALTH, this.dmg);
77                 }
78                 else
79                 {
80                         GameRules_scoring_add_team(actor, SCORE, hlth);
81                         GameRules_scoring_add_team(actor, ASSAULT_OBJECTIVES, 1);
82                         SetResourceExplicit(this.enemy, RES_HEALTH, -1);
83
84                         if(this.enemy.message)
85                                 FOREACH_CLIENT(IS_PLAYER(it), { centerprint(it, this.enemy.message); });
86
87                         SUB_UseTargets(this.enemy, this, trigger);
88                 }
89         }
90 }
91
92 void assault_setenemytoobjective(entity this)
93 {
94         IL_EACH(g_assault_objectives, it.targetname == this.target,
95         {
96                 if(this.enemy == NULL)
97                         this.enemy = it;
98                 else
99                         objerror(this, "more than one objective as target - fix the map!");
100                 break;
101         });
102
103         if(this.enemy == NULL)
104                 objerror(this, "no objective as target - fix the map!");
105 }
106
107 bool assault_decreaser_sprite_visible(entity this, entity player, entity view)
108 {
109         if(GetResource(this.assault_decreaser.enemy, RES_HEALTH) >= ASSAULT_VALUE_INACTIVE)
110                 return false;
111
112         return true;
113 }
114
115 void target_objective_decrease_activate(entity this)
116 {
117         entity spr;
118         this.owner = NULL;
119         FOREACH_ENTITY_STRING(target, this.targetname,
120         {
121                 if(it.assault_sprite != NULL)
122                 {
123                         WaypointSprite_Disown(it.assault_sprite, waypointsprite_deadlifetime);
124                         if(it.classname == "func_assault_destructible")
125                                 it.sprite = NULL; // TODO: just unsetting it?!
126                 }
127
128                 spr = WaypointSprite_SpawnFixed(WP_AssaultDefend, 0.5 * (it.absmin + it.absmax), it, assault_sprite, RADARICON_OBJECTIVE);
129                 spr.assault_decreaser = this;
130                 spr.waypointsprite_visible_for_player = assault_decreaser_sprite_visible;
131                 spr.classname = "sprite_waypoint";
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         this.classname = "target_objective";
315         IL_PUSH(g_assault_objectives, this);
316         this.use = assault_objective_use;
317         this.reset = assault_objective_reset;
318         this.reset(this);
319         this.spawn_evalfunc = target_objective_spawn_evalfunc;
320 }
321
322 spawnfunc(target_objective_decrease)
323 {
324         if (!g_assault) { delete(this); return; }
325
326         this.classname = "target_objective_decrease";
327         IL_PUSH(g_assault_objectivedecreasers, this);
328
329         if(!this.dmg)
330                 this.dmg = 101;
331
332         this.use = assault_objective_decrease_use;
333         SetResourceExplicit(this, RES_HEALTH, ASSAULT_VALUE_INACTIVE);
334         this.max_health = ASSAULT_VALUE_INACTIVE;
335         this.enemy = NULL;
336
337         InitializeEntity(this, target_objective_decrease_findtarget, INITPRIO_FINDTARGET);
338 }
339
340 // destructible walls that can be used to trigger target_objective_decrease
341 bool destructible_heal(entity targ, entity inflictor, float amount, float limit)
342 {
343         float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
344         float hlth = GetResource(targ, RES_HEALTH);
345         if (hlth <= 0 || hlth >= true_limit)
346                 return false;
347
348         GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
349         if(targ.sprite)
350         {
351                 WaypointSprite_UpdateHealth(targ.sprite, GetResource(targ, RES_HEALTH));
352         }
353         func_breakable_colormod(targ);
354         return true;
355 }
356
357 spawnfunc(func_assault_destructible)
358 {
359         if (!g_assault) { delete(this); return; }
360
361         this.spawnflags = 3;
362         this.classname = "func_assault_destructible";
363         this.event_heal = destructible_heal;
364         IL_PUSH(g_assault_destructibles, this);
365
366         if(assault_attacker_team == NUM_TEAM_1)
367                 this.team = NUM_TEAM_2;
368         else
369                 this.team = NUM_TEAM_1;
370
371         spawnfunc_func_breakable(this);
372 }
373
374 spawnfunc(func_assault_wall)
375 {
376         if (!g_assault) { delete(this); return; }
377
378         this.classname = "func_assault_wall";
379         this.mdl = this.model;
380         _setmodel(this, this.mdl);
381         this.solid = SOLID_BSP;
382         setthink(this, assault_wall_think);
383         this.nextthink = time;
384         InitializeEntity(this, assault_setenemytoobjective, INITPRIO_FINDTARGET);
385 }
386
387 spawnfunc(target_assault_roundend)
388 {
389         if (!g_assault) { delete(this); return; }
390
391         this.winning = 0; // round not yet won by attackers
392         this.classname = "target_assault_roundend";
393         this.use = target_assault_roundend_use;
394         this.cnt = 0; // first round
395         this.reset = target_assault_roundend_reset;
396 }
397
398 spawnfunc(target_assault_roundstart)
399 {
400         if (!g_assault) { delete(this); return; }
401
402         assault_attacker_team = NUM_TEAM_1;
403         this.classname = "target_assault_roundstart";
404         this.use = assault_roundstart_use;
405         this.reset2 = assault_roundstart_use_this;
406         InitializeEntity(this, assault_roundstart_use_this, INITPRIO_FINDTARGET);
407 }
408
409 // legacy bot code
410 void havocbot_goalrating_ast_targets(entity this, float ratingscale)
411 {
412         IL_EACH(g_assault_destructibles, it.bot_attack,
413         {
414                 if (it.target == "")
415                         continue;
416
417                 bool found = false;
418                 entity destr = it;
419                 IL_EACH(g_assault_objectivedecreasers, it.targetname == destr.target,
420                 {
421                         float hlth = GetResource(it.enemy, RES_HEALTH);
422                         if (hlth > 0 && hlth < ASSAULT_VALUE_INACTIVE)
423                         {
424                                 found = true;
425                                 break;
426                         }
427                 });
428
429                 if(!found)
430                         continue;
431
432                 vector p = 0.5 * (it.absmin + it.absmax);
433
434                 // Find and rate waypoints around it
435                 found = false;
436                 entity best = NULL;
437                 float bestvalue = FLOAT_MAX;
438                 entity des = it;
439                 for (float radius = 500; radius <= 1500 && !found; radius += 500)
440                 {
441                         FOREACH_ENTITY_RADIUS(p, radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
442                         {
443                                 if(checkpvs(it.origin, des))
444                                 {
445                                         found = true;
446                                         if(it.cnt < bestvalue)
447                                         {
448                                                 best = it;
449                                                 bestvalue = it.cnt;
450                                         }
451                                 }
452                         });
453                 }
454
455                 if(best)
456                 {
457                 ///     dprint("waypoints around target were found\n");
458                 //      te_lightning2(NULL, '0 0 0', best.origin);
459                 //      te_knightspike(best.origin);
460
461                         navigation_routerating(this, best, ratingscale, 4000);
462                         best.cnt += 1;
463
464                         this.havocbot_attack_time = 0;
465
466                         if(checkpvs(this.origin + this.view_ofs, it))
467                         if(checkpvs(this.origin + this.view_ofs, best))
468                         {
469                         //      dprint("increasing attack time for this target\n");
470                                 this.havocbot_attack_time = time + 2;
471                         }
472                 }
473         });
474 }
475
476 void havocbot_role_ast_offense(entity this)
477 {
478         if(IS_DEAD(this))
479         {
480                 this.havocbot_attack_time = 0;
481                 havocbot_ast_reset_role(this);
482                 return;
483         }
484
485         // Set the role timeout if necessary
486         if (!this.havocbot_role_timeout)
487                 this.havocbot_role_timeout = time + 120;
488
489         if (time > this.havocbot_role_timeout)
490         {
491                 havocbot_ast_reset_role(this);
492                 return;
493         }
494
495         if(this.havocbot_attack_time>time)
496                 return;
497
498         if (navigation_goalrating_timeout(this))
499         {
500                 // role: offense
501                 navigation_goalrating_start(this);
502                 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 650);
503                 havocbot_goalrating_ast_targets(this, 20000);
504                 havocbot_goalrating_items(this, 30000, this.origin, 10000);
505                 navigation_goalrating_end(this);
506
507                 navigation_goalrating_timeout_set(this);
508         }
509 }
510
511 void havocbot_role_ast_defense(entity this)
512 {
513         if(IS_DEAD(this))
514         {
515                 this.havocbot_attack_time = 0;
516                 havocbot_ast_reset_role(this);
517                 return;
518         }
519
520         // Set the role timeout if necessary
521         if (!this.havocbot_role_timeout)
522                 this.havocbot_role_timeout = time + 120;
523
524         if (time > this.havocbot_role_timeout)
525         {
526                 havocbot_ast_reset_role(this);
527                 return;
528         }
529
530         if(this.havocbot_attack_time>time)
531                 return;
532
533         if (navigation_goalrating_timeout(this))
534         {
535                 // role: defense
536                 navigation_goalrating_start(this);
537                 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 3000);
538                 havocbot_goalrating_ast_targets(this, 20000);
539                 havocbot_goalrating_items(this, 30000, this.origin, 10000);
540                 navigation_goalrating_end(this);
541
542                 navigation_goalrating_timeout_set(this);
543         }
544 }
545
546 void havocbot_role_ast_setrole(entity this, float role)
547 {
548         switch(role)
549         {
550                 case HAVOCBOT_AST_ROLE_DEFENSE:
551                         this.havocbot_role = havocbot_role_ast_defense;
552                         this.havocbot_role_timeout = 0;
553                         break;
554                 case HAVOCBOT_AST_ROLE_OFFENSE:
555                         this.havocbot_role = havocbot_role_ast_offense;
556                         this.havocbot_role_timeout = 0;
557                         break;
558         }
559 }
560
561 void havocbot_ast_reset_role(entity this)
562 {
563         if(IS_DEAD(this))
564                 return;
565
566         if(this.team == assault_attacker_team)
567                 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_OFFENSE);
568         else
569                 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_DEFENSE);
570 }
571
572 // mutator hooks
573 MUTATOR_HOOKFUNCTION(as, PlayerSpawn)
574 {
575         entity player = M_ARGV(0, entity);
576
577         if(player.team == assault_attacker_team)
578                 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_ATTACKING);
579         else
580                 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_DEFENDING);
581 }
582
583 MUTATOR_HOOKFUNCTION(as, TurretSpawn)
584 {
585         entity turret = M_ARGV(0, entity);
586
587         if(!turret.team || turret.team == FLOAT_MAX)
588                 turret.team = assault_attacker_team; // this gets reversed when match starts (assault_roundstart_use)
589 }
590
591 MUTATOR_HOOKFUNCTION(as, VehicleInit)
592 {
593         entity veh = M_ARGV(0, entity);
594
595         veh.nextthink = time + 0.5;
596 }
597
598 MUTATOR_HOOKFUNCTION(as, HavocBot_ChooseRole)
599 {
600         entity bot = M_ARGV(0, entity);
601
602         havocbot_ast_reset_role(bot);
603         return true;
604 }
605
606 MUTATOR_HOOKFUNCTION(as, PlayHitsound)
607 {
608         entity frag_victim = M_ARGV(0, entity);
609
610         return (frag_victim.classname == "func_assault_destructible");
611 }
612
613 MUTATOR_HOOKFUNCTION(as, TeamBalance_CheckAllowedTeams)
614 {
615         // assault always has 2 teams
616         M_ARGV(0, float) = BIT(0) | BIT(1);
617         return true;
618 }
619
620 MUTATOR_HOOKFUNCTION(as, CheckRules_World)
621 {
622         M_ARGV(0, float) = WinningCondition_Assault();
623         return true;
624 }
625
626 MUTATOR_HOOKFUNCTION(as, ReadLevelCvars)
627 {
628         // incompatible
629         warmup_stage = 0;
630         sv_ready_restart_after_countdown = 0;
631 }
632
633 MUTATOR_HOOKFUNCTION(as, OnEntityPreSpawn)
634 {
635         entity ent = M_ARGV(0, entity);
636
637         switch(ent.classname)
638         {
639                 case "info_player_team1":
640                 case "info_player_team2":
641                 case "info_player_team3":
642                 case "info_player_team4":
643                         return true;
644         }
645 }
646
647 MUTATOR_HOOKFUNCTION(as, ReadyRestart_Deny)
648 {
649         // readyrestart not supported (yet)
650         return true;
651 }