1 #include "sv_assault.qh"
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>
12 #define AS_ROUND_DELAY 5
14 IntrusiveList g_assault_destructibles;
15 IntrusiveList g_assault_objectivedecreasers;
16 IntrusiveList g_assault_objectives;
17 STATIC_INIT(g_assault)
19 g_assault_destructibles = IL_NEW();
20 g_assault_objectivedecreasers = IL_NEW();
21 g_assault_objectives = IL_NEW();
25 void assault_objective_use(entity this, entity actor, entity trigger)
28 SetResourceExplicit(this, RES_HEALTH, 100);
29 //print("^2Activated objective ", this.targetname, "=", etos(this), "\n");
30 //print("Activator is ", actor.classname, "\n");
32 IL_EACH(g_assault_objectivedecreasers, it.target == this.targetname,
34 target_objective_decrease_activate(it);
38 vector target_objective_spawn_evalfunc(entity this, entity player, entity spot, vector current)
40 float hlth = GetResource(this, RES_HEALTH);
41 if (hlth < 0 || hlth >= ASSAULT_VALUE_INACTIVE)
46 // reset this objective. Used when spawning an objective
47 // and when a new round starts
48 void assault_objective_reset(entity this)
50 SetResourceExplicit(this, RES_HEALTH, ASSAULT_VALUE_INACTIVE);
53 // decrease the health of targeted objectives
54 void assault_objective_decrease_use(entity this, entity actor, entity trigger)
56 if(actor.team != assault_attacker_team)
58 // wrong team triggered decrease
62 if(trigger.assault_sprite)
64 WaypointSprite_Disown(trigger.assault_sprite, waypointsprite_deadlifetime);
65 if(trigger.classname == "func_assault_destructible")
66 trigger.sprite = NULL; // TODO: just unsetting it?!
69 return; // already activated! cannot activate again!
71 float hlth = GetResource(this.enemy, RES_HEALTH);
72 if (hlth < ASSAULT_VALUE_INACTIVE)
74 if (hlth - this.dmg > 0.5)
76 GameRules_scoring_add_team(actor, SCORE, this.dmg);
77 TakeResource(this.enemy, RES_HEALTH, this.dmg);
81 GameRules_scoring_add_team(actor, SCORE, hlth);
82 GameRules_scoring_add_team(actor, ASSAULT_OBJECTIVES, 1);
83 SetResourceExplicit(this.enemy, RES_HEALTH, -1);
85 if(this.enemy.message)
86 FOREACH_CLIENT(IS_PLAYER(it), { centerprint(it, this.enemy.message); });
88 SUB_UseTargets(this.enemy, this, trigger);
93 void assault_setenemytoobjective(entity this)
95 IL_EACH(g_assault_objectives, it.targetname == this.target,
97 if(this.enemy == NULL)
100 objerror(this, "more than one objective as target - fix the map!");
104 if(this.enemy == NULL)
105 objerror(this, "no objective as target - fix the map!");
108 bool assault_decreaser_sprite_visible(entity this, entity player, entity view)
110 if(GetResource(this.assault_decreaser.enemy, RES_HEALTH) >= ASSAULT_VALUE_INACTIVE)
116 void target_objective_decrease_activate(entity this)
120 FOREACH_ENTITY_STRING(target, this.targetname,
122 if(it.assault_sprite != NULL)
124 WaypointSprite_Disown(it.assault_sprite, waypointsprite_deadlifetime);
125 if(it.classname == "func_assault_destructible")
126 it.sprite = NULL; // TODO: just unsetting it?!
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")
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));
141 WaypointSprite_UpdateSprites(spr, WP_AssaultDefend, WP_AssaultPush, WP_AssaultPush);
145 void target_objective_decrease_findtarget(entity this)
147 assault_setenemytoobjective(this);
150 void target_assault_roundend_reset(entity this)
152 //print("round end reset\n");
153 ++this.cnt; // up round counter
154 this.winning = false; // up round
157 void target_assault_roundend_use(entity this, entity actor, entity trigger)
159 this.winning = 1; // round has been won by attackers
162 void assault_roundstart_use(entity this, entity actor, entity trigger)
164 SUB_UseTargets(this, this, trigger);
166 //(Re)spawn all turrets
167 IL_EACH(g_turrets, true,
170 if(it.team == NUM_TEAM_1)
171 it.team = NUM_TEAM_2;
173 it.team = NUM_TEAM_1;
175 // Doubles as teamchange
179 void assault_roundstart_use_this(entity this)
181 assault_roundstart_use(this, NULL, NULL);
184 void assault_wall_think(entity this)
186 if(GetResource(this.enemy, RES_HEALTH) < 0)
189 this.solid = SOLID_NOT;
193 this.model = this.mdl;
194 this.solid = SOLID_BSP;
197 this.nextthink = time + 0.2;
201 // reset objectives, toggle spawnpoints, reset triggers, ...
202 void assault_new_round(entity this)
204 //bprint("ASSAULT: new round\n");
207 this.winning = this.winning + 1;
209 // swap attacker/defender roles
210 if(assault_attacker_team == NUM_TEAM_1)
211 assault_attacker_team = NUM_TEAM_2;
213 assault_attacker_team = NUM_TEAM_1;
215 IL_EACH(g_saved_team, !IS_CLIENT(it),
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;
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
230 void as_round_think()
232 game_stopped = false;
233 assault_new_round(as_round.ent_winning);
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()
245 WinningConditionHelper(NULL); // set worldstatus
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)
251 SetWinners(team, NUM_TEAM_2);
255 SetWinners(team, NUM_TEAM_1);
259 ent = find(NULL, classname, "target_assault_roundend");
262 if(ent.winning) // round end has been triggered by attacking team
264 bprint("Assault: round completed.\n");
265 SetWinners(team, assault_attacker_team);
267 TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 666 - TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 0));
269 if(ent.cnt == 1 || autocvar_g_campaign) // this was the second round
271 status = WINNING_YES;
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;
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));
294 spawnfunc(info_player_attacker)
296 if (!g_assault) { delete(this); return; }
298 this.team = NUM_TEAM_1; // red, gets swapped every round
299 spawnfunc_info_player_deathmatch(this);
302 spawnfunc(info_player_defender)
304 if (!g_assault) { delete(this); return; }
306 this.team = NUM_TEAM_2; // blue, gets swapped every round
307 spawnfunc_info_player_deathmatch(this);
310 spawnfunc(target_objective)
312 if (!g_assault) { delete(this); return; }
314 IL_PUSH(g_assault_objectives, this);
315 this.use = assault_objective_use;
316 this.reset = assault_objective_reset;
318 this.spawn_evalfunc = target_objective_spawn_evalfunc;
321 spawnfunc(target_objective_decrease)
323 if (!g_assault) { delete(this); return; }
325 IL_PUSH(g_assault_objectivedecreasers, this);
330 this.use = assault_objective_decrease_use;
331 SetResourceExplicit(this, RES_HEALTH, ASSAULT_VALUE_INACTIVE);
332 this.max_health = ASSAULT_VALUE_INACTIVE;
335 InitializeEntity(this, target_objective_decrease_findtarget, INITPRIO_FINDTARGET);
338 // destructible walls that can be used to trigger target_objective_decrease
339 bool destructible_heal(entity targ, entity inflictor, float amount, float limit)
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)
346 GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
349 WaypointSprite_UpdateHealth(targ.sprite, GetResource(targ, RES_HEALTH));
351 func_breakable_colormod(targ);
355 spawnfunc(func_assault_destructible)
357 if (!g_assault) { delete(this); return; }
360 this.event_heal = destructible_heal;
361 IL_PUSH(g_assault_destructibles, this);
363 if(assault_attacker_team == NUM_TEAM_1)
364 this.team = NUM_TEAM_2;
366 this.team = NUM_TEAM_1;
368 func_breakable_setup(this);
371 spawnfunc(func_assault_wall)
373 if (!g_assault) { delete(this); return; }
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);
383 spawnfunc(target_assault_roundend)
385 if (!g_assault) { delete(this); return; }
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;
393 spawnfunc(target_assault_roundstart)
395 if (!g_assault) { delete(this); return; }
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);
404 void havocbot_goalrating_ast_targets(entity this, float ratingscale)
406 IL_EACH(g_assault_destructibles, it.bot_attack,
413 IL_EACH(g_assault_objectivedecreasers, it.targetname == destr.target,
415 float hlth = GetResource(it.enemy, RES_HEALTH);
416 if (hlth > 0 && hlth < ASSAULT_VALUE_INACTIVE)
426 vector p = 0.5 * (it.absmin + it.absmax);
428 // Find and rate waypoints around it
431 float bestvalue = FLOAT_MAX;
433 for (float radius = 500; radius <= 1500 && !found; radius += 500)
435 FOREACH_ENTITY_RADIUS(p, radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
437 if(checkpvs(it.origin, des))
440 if(it.cnt < bestvalue)
451 /// dprint("waypoints around target were found\n");
452 // te_lightning2(NULL, '0 0 0', best.origin);
453 // te_knightspike(best.origin);
455 navigation_routerating(this, best, ratingscale, 4000);
458 this.havocbot_attack_time = 0;
460 if(checkpvs(this.origin + this.view_ofs, it))
461 if(checkpvs(this.origin + this.view_ofs, best))
463 // dprint("increasing attack time for this target\n");
464 this.havocbot_attack_time = time + 2;
470 void havocbot_role_ast_offense(entity this)
474 this.havocbot_attack_time = 0;
475 havocbot_ast_reset_role(this);
479 // Set the role timeout if necessary
480 if (!this.havocbot_role_timeout)
481 this.havocbot_role_timeout = time + 120;
483 if (time > this.havocbot_role_timeout)
485 havocbot_ast_reset_role(this);
489 if(this.havocbot_attack_time>time)
492 if (navigation_goalrating_timeout(this))
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);
501 navigation_goalrating_timeout_set(this);
505 void havocbot_role_ast_defense(entity this)
509 this.havocbot_attack_time = 0;
510 havocbot_ast_reset_role(this);
514 // Set the role timeout if necessary
515 if (!this.havocbot_role_timeout)
516 this.havocbot_role_timeout = time + 120;
518 if (time > this.havocbot_role_timeout)
520 havocbot_ast_reset_role(this);
524 if(this.havocbot_attack_time>time)
527 if (navigation_goalrating_timeout(this))
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);
536 navigation_goalrating_timeout_set(this);
540 void havocbot_role_ast_setrole(entity this, float role)
544 case HAVOCBOT_AST_ROLE_DEFENSE:
545 this.havocbot_role = havocbot_role_ast_defense;
546 this.havocbot_role_timeout = 0;
548 case HAVOCBOT_AST_ROLE_OFFENSE:
549 this.havocbot_role = havocbot_role_ast_offense;
550 this.havocbot_role_timeout = 0;
555 void havocbot_ast_reset_role(entity this)
560 if(this.team == assault_attacker_team)
561 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_OFFENSE);
563 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_DEFENSE);
567 MUTATOR_HOOKFUNCTION(as, PlayerSpawn)
569 entity player = M_ARGV(0, entity);
571 if(player.team == assault_attacker_team)
572 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_ATTACKING);
574 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_DEFENDING);
577 MUTATOR_HOOKFUNCTION(as, TurretSpawn)
579 entity turret = M_ARGV(0, entity);
581 if(!turret.team || turret.team == FLOAT_MAX)
582 turret.team = assault_attacker_team; // this gets reversed when match starts (assault_roundstart_use)
585 MUTATOR_HOOKFUNCTION(as, VehicleInit)
587 entity veh = M_ARGV(0, entity);
589 veh.nextthink = time + 0.5;
592 MUTATOR_HOOKFUNCTION(as, HavocBot_ChooseRole)
594 entity bot = M_ARGV(0, entity);
596 havocbot_ast_reset_role(bot);
600 MUTATOR_HOOKFUNCTION(as, PlayHitsound)
602 entity frag_victim = M_ARGV(0, entity);
604 return (frag_victim.classname == "func_assault_destructible");
607 MUTATOR_HOOKFUNCTION(as, TeamBalance_CheckAllowedTeams)
609 // assault always has 2 teams
610 M_ARGV(0, float) = BIT(0) | BIT(1);
614 MUTATOR_HOOKFUNCTION(as, CheckRules_World)
616 M_ARGV(0, float) = WinningCondition_Assault();
620 MUTATOR_HOOKFUNCTION(as, ReadLevelCvars)
624 sv_ready_restart_after_countdown = 0;
627 MUTATOR_HOOKFUNCTION(as, OnEntityPreSpawn)
629 entity ent = M_ARGV(0, entity);
631 switch(ent.classname)
633 case "info_player_team1":
634 case "info_player_team2":
635 case "info_player_team3":
636 case "info_player_team4":
641 MUTATOR_HOOKFUNCTION(as, ReadyRestart_Deny)
643 // readyrestart not supported (yet)