1 #include "sv_assault.qh"
3 #include <common/mapobjects/func/breakable.qh>
4 #include <server/spawnpoints.qh>
7 #define AS_ROUND_DELAY 5
9 IntrusiveList g_assault_destructibles;
10 IntrusiveList g_assault_objectivedecreasers;
11 IntrusiveList g_assault_objectives;
12 STATIC_INIT(g_assault)
14 g_assault_destructibles = IL_NEW();
15 g_assault_objectivedecreasers = IL_NEW();
16 g_assault_objectives = IL_NEW();
20 void assault_objective_use(entity this, entity actor, entity trigger)
23 SetResourceExplicit(this, RES_HEALTH, 100);
24 //print("^2Activated objective ", this.targetname, "=", etos(this), "\n");
25 //print("Activator is ", actor.classname, "\n");
27 IL_EACH(g_assault_objectivedecreasers, it.target == this.targetname,
29 target_objective_decrease_activate(it);
33 vector target_objective_spawn_evalfunc(entity this, entity player, entity spot, vector current)
35 float hlth = GetResource(this, RES_HEALTH);
36 if (hlth < 0 || hlth >= ASSAULT_VALUE_INACTIVE)
41 // reset this objective. Used when spawning an objective
42 // and when a new round starts
43 void assault_objective_reset(entity this)
45 SetResourceExplicit(this, RES_HEALTH, ASSAULT_VALUE_INACTIVE);
48 // decrease the health of targeted objectives
49 void assault_objective_decrease_use(entity this, entity actor, entity trigger)
51 if(actor.team != assault_attacker_team)
53 // wrong team triggered decrease
57 if(trigger.assault_sprite)
59 WaypointSprite_Disown(trigger.assault_sprite, waypointsprite_deadlifetime);
60 if(trigger.classname == "func_assault_destructible")
61 trigger.sprite = NULL; // TODO: just unsetting it?!
64 return; // already activated! cannot activate again!
66 float hlth = GetResource(this.enemy, RES_HEALTH);
67 if (hlth < ASSAULT_VALUE_INACTIVE)
69 if (hlth - this.dmg > 0.5)
71 GameRules_scoring_add_team(actor, SCORE, this.dmg);
72 TakeResource(this.enemy, RES_HEALTH, this.dmg);
76 GameRules_scoring_add_team(actor, SCORE, hlth);
77 GameRules_scoring_add_team(actor, ASSAULT_OBJECTIVES, 1);
78 SetResourceExplicit(this.enemy, RES_HEALTH, -1);
80 if(this.enemy.message)
81 FOREACH_CLIENT(IS_PLAYER(it), { centerprint(it, this.enemy.message); });
83 SUB_UseTargets(this.enemy, this, trigger);
88 void assault_setenemytoobjective(entity this)
90 IL_EACH(g_assault_objectives, it.targetname == this.target,
92 if(this.enemy == NULL)
95 objerror(this, "more than one objective as target - fix the map!");
99 if(this.enemy == NULL)
100 objerror(this, "no objective as target - fix the map!");
103 bool assault_decreaser_sprite_visible(entity this, entity player, entity view)
105 if(GetResource(this.assault_decreaser.enemy, RES_HEALTH) >= ASSAULT_VALUE_INACTIVE)
111 void target_objective_decrease_activate(entity this)
115 FOREACH_ENTITY_STRING(target, this.targetname,
117 if(it.assault_sprite != NULL)
119 WaypointSprite_Disown(it.assault_sprite, waypointsprite_deadlifetime);
120 if(it.classname == "func_assault_destructible")
121 it.sprite = NULL; // TODO: just unsetting it?!
124 spr = WaypointSprite_SpawnFixed(WP_AssaultDefend, 0.5 * (it.absmin + it.absmax), it, assault_sprite, RADARICON_OBJECTIVE);
125 spr.assault_decreaser = this;
126 spr.waypointsprite_visible_for_player = assault_decreaser_sprite_visible;
127 spr.classname = "sprite_waypoint";
128 WaypointSprite_UpdateRule(spr, assault_attacker_team, SPRITERULE_TEAMPLAY);
129 if(it.classname == "func_assault_destructible")
131 WaypointSprite_UpdateSprites(spr, WP_AssaultDefend, WP_AssaultDestroy, WP_AssaultDestroy);
132 WaypointSprite_UpdateMaxHealth(spr, it.max_health);
133 WaypointSprite_UpdateHealth(spr, GetResource(it, RES_HEALTH));
137 WaypointSprite_UpdateSprites(spr, WP_AssaultDefend, WP_AssaultPush, WP_AssaultPush);
141 void target_objective_decrease_findtarget(entity this)
143 assault_setenemytoobjective(this);
146 void target_assault_roundend_reset(entity this)
148 //print("round end reset\n");
149 ++this.cnt; // up round counter
150 this.winning = false; // up round
153 void target_assault_roundend_use(entity this, entity actor, entity trigger)
155 this.winning = 1; // round has been won by attackers
158 void assault_roundstart_use(entity this, entity actor, entity trigger)
160 SUB_UseTargets(this, this, trigger);
162 //(Re)spawn all turrets
163 IL_EACH(g_turrets, true,
166 if(it.team == NUM_TEAM_1)
167 it.team = NUM_TEAM_2;
169 it.team = NUM_TEAM_1;
171 // Doubles as teamchange
175 void assault_roundstart_use_this(entity this)
177 assault_roundstart_use(this, NULL, NULL);
180 void assault_wall_think(entity this)
182 if(GetResource(this.enemy, RES_HEALTH) < 0)
185 this.solid = SOLID_NOT;
189 this.model = this.mdl;
190 this.solid = SOLID_BSP;
193 this.nextthink = time + 0.2;
197 // reset objectives, toggle spawnpoints, reset triggers, ...
198 void assault_new_round(entity this)
200 //bprint("ASSAULT: new round\n");
203 this.winning = this.winning + 1;
205 // swap attacker/defender roles
206 if(assault_attacker_team == NUM_TEAM_1)
207 assault_attacker_team = NUM_TEAM_2;
209 assault_attacker_team = NUM_TEAM_1;
211 IL_EACH(g_saved_team, !IS_CLIENT(it),
213 if(it.team_saved == NUM_TEAM_1)
214 it.team_saved = NUM_TEAM_2;
215 else if(it.team_saved == NUM_TEAM_2)
216 it.team_saved = NUM_TEAM_1;
219 // reset the level with a countdown
220 cvar_set("timelimit", ftos(ceil(time - AS_ROUND_DELAY - game_starttime) / 60));
221 ReadyRestart_force(); // sets game_starttime
226 void as_round_think()
228 game_stopped = false;
229 assault_new_round(as_round.ent_winning);
234 // Assault winning condition: If the attackers triggered a round end (by fulfilling all objectives)
235 // they win. Otherwise the defending team wins once the timelimit passes.
236 int WinningCondition_Assault()
241 WinningConditionHelper(NULL); // set worldstatus
243 int status = WINNING_NO;
244 // as the timelimit has not yet passed just assume the defending team will win
245 if(assault_attacker_team == NUM_TEAM_1)
247 SetWinners(team, NUM_TEAM_2);
251 SetWinners(team, NUM_TEAM_1);
255 ent = find(NULL, classname, "target_assault_roundend");
258 if(ent.winning) // round end has been triggered by attacking team
260 bprint("Assault: round completed.\n");
261 SetWinners(team, assault_attacker_team);
263 TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 666 - TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 0));
265 if(ent.cnt == 1 || autocvar_g_campaign) // this was the second round
267 status = WINNING_YES;
271 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ASSAULT_OBJ_DESTROYED, ceil(time - game_starttime));
272 as_round = new(as_round);
273 as_round.think = as_round_think;
274 as_round.ent_winning = ent;
275 as_round.nextthink = time + AS_ROUND_DELAY;
278 // make sure timelimit isn't hit while the game is blocked
279 if(autocvar_timelimit > 0)
280 if(time + AS_ROUND_DELAY >= game_starttime + autocvar_timelimit * 60)
281 cvar_set("timelimit", ftos(autocvar_timelimit + AS_ROUND_DELAY / 60));
290 spawnfunc(info_player_attacker)
292 if (!g_assault) { delete(this); return; }
294 this.team = NUM_TEAM_1; // red, gets swapped every round
295 spawnfunc_info_player_deathmatch(this);
298 spawnfunc(info_player_defender)
300 if (!g_assault) { delete(this); return; }
302 this.team = NUM_TEAM_2; // blue, gets swapped every round
303 spawnfunc_info_player_deathmatch(this);
306 spawnfunc(target_objective)
308 if (!g_assault) { delete(this); return; }
310 this.classname = "target_objective";
311 IL_PUSH(g_assault_objectives, this);
312 this.use = assault_objective_use;
313 this.reset = assault_objective_reset;
315 this.spawn_evalfunc = target_objective_spawn_evalfunc;
318 spawnfunc(target_objective_decrease)
320 if (!g_assault) { delete(this); return; }
322 this.classname = "target_objective_decrease";
323 IL_PUSH(g_assault_objectivedecreasers, this);
328 this.use = assault_objective_decrease_use;
329 SetResourceExplicit(this, RES_HEALTH, ASSAULT_VALUE_INACTIVE);
330 this.max_health = ASSAULT_VALUE_INACTIVE;
333 InitializeEntity(this, target_objective_decrease_findtarget, INITPRIO_FINDTARGET);
336 // destructible walls that can be used to trigger target_objective_decrease
337 bool destructible_heal(entity targ, entity inflictor, float amount, float limit)
339 float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
340 float hlth = GetResource(targ, RES_HEALTH);
341 if (hlth <= 0 || hlth >= true_limit)
344 GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
347 WaypointSprite_UpdateHealth(targ.sprite, GetResource(targ, RES_HEALTH));
349 func_breakable_colormod(targ);
353 spawnfunc(func_assault_destructible)
355 if (!g_assault) { delete(this); return; }
358 this.classname = "func_assault_destructible";
359 this.event_heal = destructible_heal;
360 IL_PUSH(g_assault_destructibles, this);
362 if(assault_attacker_team == NUM_TEAM_1)
363 this.team = NUM_TEAM_2;
365 this.team = NUM_TEAM_1;
367 spawnfunc_func_breakable(this);
370 spawnfunc(func_assault_wall)
372 if (!g_assault) { delete(this); return; }
374 this.classname = "func_assault_wall";
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.classname = "target_assault_roundend";
389 this.use = target_assault_roundend_use;
390 this.cnt = 0; // first round
391 this.reset = target_assault_roundend_reset;
394 spawnfunc(target_assault_roundstart)
396 if (!g_assault) { delete(this); return; }
398 assault_attacker_team = NUM_TEAM_1;
399 this.classname = "target_assault_roundstart";
400 this.use = assault_roundstart_use;
401 this.reset2 = assault_roundstart_use_this;
402 InitializeEntity(this, assault_roundstart_use_this, INITPRIO_FINDTARGET);
406 void havocbot_goalrating_ast_targets(entity this, float ratingscale)
408 IL_EACH(g_assault_destructibles, it.bot_attack,
415 IL_EACH(g_assault_objectivedecreasers, it.targetname == destr.target,
417 float hlth = GetResource(it.enemy, RES_HEALTH);
418 if (hlth > 0 && hlth < ASSAULT_VALUE_INACTIVE)
428 vector p = 0.5 * (it.absmin + it.absmax);
430 // Find and rate waypoints around it
433 float bestvalue = FLOAT_MAX;
435 for (float radius = 500; radius <= 1500 && !found; radius += 500)
437 FOREACH_ENTITY_RADIUS(p, radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
439 if(checkpvs(it.origin, des))
442 if(it.cnt < bestvalue)
453 /// dprint("waypoints around target were found\n");
454 // te_lightning2(NULL, '0 0 0', best.origin);
455 // te_knightspike(best.origin);
457 navigation_routerating(this, best, ratingscale, 4000);
460 this.havocbot_attack_time = 0;
462 if(checkpvs(this.origin + this.view_ofs, it))
463 if(checkpvs(this.origin + this.view_ofs, best))
465 // dprint("increasing attack time for this target\n");
466 this.havocbot_attack_time = time + 2;
472 void havocbot_role_ast_offense(entity this)
476 this.havocbot_attack_time = 0;
477 havocbot_ast_reset_role(this);
481 // Set the role timeout if necessary
482 if (!this.havocbot_role_timeout)
483 this.havocbot_role_timeout = time + 120;
485 if (time > this.havocbot_role_timeout)
487 havocbot_ast_reset_role(this);
491 if(this.havocbot_attack_time>time)
494 if (navigation_goalrating_timeout(this))
497 navigation_goalrating_start(this);
498 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 650);
499 havocbot_goalrating_ast_targets(this, 20000);
500 havocbot_goalrating_items(this, 30000, this.origin, 10000);
501 navigation_goalrating_end(this);
503 navigation_goalrating_timeout_set(this);
507 void havocbot_role_ast_defense(entity this)
511 this.havocbot_attack_time = 0;
512 havocbot_ast_reset_role(this);
516 // Set the role timeout if necessary
517 if (!this.havocbot_role_timeout)
518 this.havocbot_role_timeout = time + 120;
520 if (time > this.havocbot_role_timeout)
522 havocbot_ast_reset_role(this);
526 if(this.havocbot_attack_time>time)
529 if (navigation_goalrating_timeout(this))
532 navigation_goalrating_start(this);
533 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 3000);
534 havocbot_goalrating_ast_targets(this, 20000);
535 havocbot_goalrating_items(this, 30000, this.origin, 10000);
536 navigation_goalrating_end(this);
538 navigation_goalrating_timeout_set(this);
542 void havocbot_role_ast_setrole(entity this, float role)
546 case HAVOCBOT_AST_ROLE_DEFENSE:
547 this.havocbot_role = havocbot_role_ast_defense;
548 this.havocbot_role_timeout = 0;
550 case HAVOCBOT_AST_ROLE_OFFENSE:
551 this.havocbot_role = havocbot_role_ast_offense;
552 this.havocbot_role_timeout = 0;
557 void havocbot_ast_reset_role(entity this)
562 if(this.team == assault_attacker_team)
563 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_OFFENSE);
565 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_DEFENSE);
569 MUTATOR_HOOKFUNCTION(as, PlayerSpawn)
571 entity player = M_ARGV(0, entity);
573 if(player.team == assault_attacker_team)
574 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_ATTACKING);
576 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_DEFENDING);
579 MUTATOR_HOOKFUNCTION(as, TurretSpawn)
581 entity turret = M_ARGV(0, entity);
583 if(!turret.team || turret.team == FLOAT_MAX)
584 turret.team = assault_attacker_team; // this gets reversed when match starts (assault_roundstart_use)
587 MUTATOR_HOOKFUNCTION(as, VehicleInit)
589 entity veh = M_ARGV(0, entity);
591 veh.nextthink = time + 0.5;
594 MUTATOR_HOOKFUNCTION(as, HavocBot_ChooseRole)
596 entity bot = M_ARGV(0, entity);
598 havocbot_ast_reset_role(bot);
602 MUTATOR_HOOKFUNCTION(as, PlayHitsound)
604 entity frag_victim = M_ARGV(0, entity);
606 return (frag_victim.classname == "func_assault_destructible");
609 MUTATOR_HOOKFUNCTION(as, TeamBalance_CheckAllowedTeams)
611 // assault always has 2 teams
612 M_ARGV(0, float) = BIT(0) | BIT(1);
616 MUTATOR_HOOKFUNCTION(as, CheckRules_World)
618 M_ARGV(0, float) = WinningCondition_Assault();
622 MUTATOR_HOOKFUNCTION(as, ReadLevelCvars)
626 sv_ready_restart_after_countdown = 0;
629 MUTATOR_HOOKFUNCTION(as, OnEntityPreSpawn)
631 entity ent = M_ARGV(0, entity);
633 switch(ent.classname)
635 case "info_player_team1":
636 case "info_player_team2":
637 case "info_player_team3":
638 case "info_player_team4":
643 MUTATOR_HOOKFUNCTION(as, ReadyRestart_Deny)
645 // readyrestart not supported (yet)