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