1 #include "sv_assault.qh"
3 #include <server/command/vote.qh>
4 #include <common/mapobjects/func/breakable.qh>
5 #include <server/g_damage.qh>
6 #include <server/spawnpoints.qh>
9 #define AS_ROUND_DELAY 5
11 IntrusiveList g_assault_destructibles;
12 IntrusiveList g_assault_objectivedecreasers;
13 IntrusiveList g_assault_objectives;
14 STATIC_INIT(g_assault)
16 g_assault_destructibles = IL_NEW();
17 g_assault_objectivedecreasers = IL_NEW();
18 g_assault_objectives = IL_NEW();
22 void assault_objective_use(entity this, entity actor, entity trigger)
25 SetResourceExplicit(this, RES_HEALTH, 100);
26 //print("^2Activated objective ", this.targetname, "=", etos(this), "\n");
27 //print("Activator is ", actor.classname, "\n");
29 IL_EACH(g_assault_objectivedecreasers, it.target == this.targetname,
31 target_objective_decrease_activate(it);
35 vector target_objective_spawn_evalfunc(entity this, entity player, entity spot, vector current)
37 float hlth = GetResource(this, RES_HEALTH);
38 if (hlth < 0 || hlth >= ASSAULT_VALUE_INACTIVE)
43 // reset this objective. Used when spawning an objective
44 // and when a new round starts
45 void assault_objective_reset(entity this)
47 SetResourceExplicit(this, RES_HEALTH, ASSAULT_VALUE_INACTIVE);
50 // decrease the health of targeted objectives
51 void assault_objective_decrease_use(entity this, entity actor, entity trigger)
53 if(actor.team != assault_attacker_team)
55 // wrong team triggered decrease
59 if(trigger.assault_sprite)
61 WaypointSprite_Disown(trigger.assault_sprite, waypointsprite_deadlifetime);
62 if(trigger.classname == "func_assault_destructible")
63 trigger.sprite = NULL; // TODO: just unsetting it?!
66 return; // already activated! cannot activate again!
68 float hlth = GetResource(this.enemy, RES_HEALTH);
69 if (hlth < ASSAULT_VALUE_INACTIVE)
71 if (hlth - this.dmg > 0.5)
73 GameRules_scoring_add_team(actor, SCORE, this.dmg);
74 TakeResource(this.enemy, RES_HEALTH, this.dmg);
78 GameRules_scoring_add_team(actor, SCORE, hlth);
79 GameRules_scoring_add_team(actor, ASSAULT_OBJECTIVES, 1);
80 SetResourceExplicit(this.enemy, RES_HEALTH, -1);
82 if(this.enemy.message)
83 FOREACH_CLIENT(IS_PLAYER(it), { centerprint(it, this.enemy.message); });
85 SUB_UseTargets(this.enemy, this, trigger);
90 void assault_setenemytoobjective(entity this)
92 IL_EACH(g_assault_objectives, it.targetname == this.target,
94 if(this.enemy == NULL)
97 objerror(this, "more than one objective as target - fix the map!");
101 if(this.enemy == NULL)
102 objerror(this, "no objective as target - fix the map!");
105 bool assault_decreaser_sprite_visible(entity this, entity player, entity view)
107 if(GetResource(this.assault_decreaser.enemy, RES_HEALTH) >= ASSAULT_VALUE_INACTIVE)
113 void target_objective_decrease_activate(entity this)
117 FOREACH_ENTITY_STRING(target, this.targetname,
119 if(it.assault_sprite != NULL)
121 WaypointSprite_Disown(it.assault_sprite, waypointsprite_deadlifetime);
122 if(it.classname == "func_assault_destructible")
123 it.sprite = NULL; // TODO: just unsetting it?!
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")
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));
139 WaypointSprite_UpdateSprites(spr, WP_AssaultDefend, WP_AssaultPush, WP_AssaultPush);
143 void target_objective_decrease_findtarget(entity this)
145 assault_setenemytoobjective(this);
148 void target_assault_roundend_reset(entity this)
150 //print("round end reset\n");
151 ++this.cnt; // up round counter
152 this.winning = false; // up round
155 void target_assault_roundend_use(entity this, entity actor, entity trigger)
157 this.winning = 1; // round has been won by attackers
160 void assault_roundstart_use(entity this, entity actor, entity trigger)
162 SUB_UseTargets(this, this, trigger);
164 //(Re)spawn all turrets
165 IL_EACH(g_turrets, true,
168 if(it.team == NUM_TEAM_1)
169 it.team = NUM_TEAM_2;
171 it.team = NUM_TEAM_1;
173 // Doubles as teamchange
177 void assault_roundstart_use_this(entity this)
179 assault_roundstart_use(this, NULL, NULL);
182 void assault_wall_think(entity this)
184 if(GetResource(this.enemy, RES_HEALTH) < 0)
187 this.solid = SOLID_NOT;
191 this.model = this.mdl;
192 this.solid = SOLID_BSP;
195 this.nextthink = time + 0.2;
199 // reset objectives, toggle spawnpoints, reset triggers, ...
200 void assault_new_round(entity this)
202 //bprint("ASSAULT: new round\n");
205 this.winning = this.winning + 1;
207 // swap attacker/defender roles
208 if(assault_attacker_team == NUM_TEAM_1)
209 assault_attacker_team = NUM_TEAM_2;
211 assault_attacker_team = NUM_TEAM_1;
213 IL_EACH(g_saved_team, !IS_CLIENT(it),
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;
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
228 void as_round_think()
230 game_stopped = false;
231 assault_new_round(as_round.ent_winning);
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()
243 WinningConditionHelper(NULL); // set worldstatus
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)
249 SetWinners(team, NUM_TEAM_2);
253 SetWinners(team, NUM_TEAM_1);
257 ent = find(NULL, classname, "target_assault_roundend");
260 if(ent.winning) // round end has been triggered by attacking team
262 bprint("Assault: round completed.\n");
263 SetWinners(team, assault_attacker_team);
265 TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 666 - TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 0));
267 if(ent.cnt == 1 || autocvar_g_campaign) // this was the second round
269 status = WINNING_YES;
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;
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));
292 spawnfunc(info_player_attacker)
294 if (!g_assault) { delete(this); return; }
296 this.team = NUM_TEAM_1; // red, gets swapped every round
297 spawnfunc_info_player_deathmatch(this);
300 spawnfunc(info_player_defender)
302 if (!g_assault) { delete(this); return; }
304 this.team = NUM_TEAM_2; // blue, gets swapped every round
305 spawnfunc_info_player_deathmatch(this);
308 spawnfunc(target_objective)
310 if (!g_assault) { delete(this); return; }
312 this.classname = "target_objective";
313 IL_PUSH(g_assault_objectives, this);
314 this.use = assault_objective_use;
315 this.reset = assault_objective_reset;
317 this.spawn_evalfunc = target_objective_spawn_evalfunc;
320 spawnfunc(target_objective_decrease)
322 if (!g_assault) { delete(this); return; }
324 this.classname = "target_objective_decrease";
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.classname = "func_assault_destructible";
361 this.event_heal = destructible_heal;
362 IL_PUSH(g_assault_destructibles, this);
364 if(assault_attacker_team == NUM_TEAM_1)
365 this.team = NUM_TEAM_2;
367 this.team = NUM_TEAM_1;
369 spawnfunc_func_breakable(this);
372 spawnfunc(func_assault_wall)
374 if (!g_assault) { delete(this); return; }
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);
385 spawnfunc(target_assault_roundend)
387 if (!g_assault) { delete(this); return; }
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;
396 spawnfunc(target_assault_roundstart)
398 if (!g_assault) { delete(this); return; }
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);
408 void havocbot_goalrating_ast_targets(entity this, float ratingscale)
410 IL_EACH(g_assault_destructibles, it.bot_attack,
417 IL_EACH(g_assault_objectivedecreasers, it.targetname == destr.target,
419 float hlth = GetResource(it.enemy, RES_HEALTH);
420 if (hlth > 0 && hlth < ASSAULT_VALUE_INACTIVE)
430 vector p = 0.5 * (it.absmin + it.absmax);
432 // Find and rate waypoints around it
435 float bestvalue = FLOAT_MAX;
437 for (float radius = 500; radius <= 1500 && !found; radius += 500)
439 FOREACH_ENTITY_RADIUS(p, radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
441 if(checkpvs(it.origin, des))
444 if(it.cnt < bestvalue)
455 /// dprint("waypoints around target were found\n");
456 // te_lightning2(NULL, '0 0 0', best.origin);
457 // te_knightspike(best.origin);
459 navigation_routerating(this, best, ratingscale, 4000);
462 this.havocbot_attack_time = 0;
464 if(checkpvs(this.origin + this.view_ofs, it))
465 if(checkpvs(this.origin + this.view_ofs, best))
467 // dprint("increasing attack time for this target\n");
468 this.havocbot_attack_time = time + 2;
474 void havocbot_role_ast_offense(entity this)
478 this.havocbot_attack_time = 0;
479 havocbot_ast_reset_role(this);
483 // Set the role timeout if necessary
484 if (!this.havocbot_role_timeout)
485 this.havocbot_role_timeout = time + 120;
487 if (time > this.havocbot_role_timeout)
489 havocbot_ast_reset_role(this);
493 if(this.havocbot_attack_time>time)
496 if (navigation_goalrating_timeout(this))
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);
505 navigation_goalrating_timeout_set(this);
509 void havocbot_role_ast_defense(entity this)
513 this.havocbot_attack_time = 0;
514 havocbot_ast_reset_role(this);
518 // Set the role timeout if necessary
519 if (!this.havocbot_role_timeout)
520 this.havocbot_role_timeout = time + 120;
522 if (time > this.havocbot_role_timeout)
524 havocbot_ast_reset_role(this);
528 if(this.havocbot_attack_time>time)
531 if (navigation_goalrating_timeout(this))
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);
540 navigation_goalrating_timeout_set(this);
544 void havocbot_role_ast_setrole(entity this, float role)
548 case HAVOCBOT_AST_ROLE_DEFENSE:
549 this.havocbot_role = havocbot_role_ast_defense;
550 this.havocbot_role_timeout = 0;
552 case HAVOCBOT_AST_ROLE_OFFENSE:
553 this.havocbot_role = havocbot_role_ast_offense;
554 this.havocbot_role_timeout = 0;
559 void havocbot_ast_reset_role(entity this)
564 if(this.team == assault_attacker_team)
565 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_OFFENSE);
567 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_DEFENSE);
571 MUTATOR_HOOKFUNCTION(as, PlayerSpawn)
573 entity player = M_ARGV(0, entity);
575 if(player.team == assault_attacker_team)
576 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_ATTACKING);
578 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_DEFENDING);
581 MUTATOR_HOOKFUNCTION(as, TurretSpawn)
583 entity turret = M_ARGV(0, entity);
585 if(!turret.team || turret.team == FLOAT_MAX)
586 turret.team = assault_attacker_team; // this gets reversed when match starts (assault_roundstart_use)
589 MUTATOR_HOOKFUNCTION(as, VehicleInit)
591 entity veh = M_ARGV(0, entity);
593 veh.nextthink = time + 0.5;
596 MUTATOR_HOOKFUNCTION(as, HavocBot_ChooseRole)
598 entity bot = M_ARGV(0, entity);
600 havocbot_ast_reset_role(bot);
604 MUTATOR_HOOKFUNCTION(as, PlayHitsound)
606 entity frag_victim = M_ARGV(0, entity);
608 return (frag_victim.classname == "func_assault_destructible");
611 MUTATOR_HOOKFUNCTION(as, TeamBalance_CheckAllowedTeams)
613 // assault always has 2 teams
614 M_ARGV(0, float) = BIT(0) | BIT(1);
618 MUTATOR_HOOKFUNCTION(as, CheckRules_World)
620 M_ARGV(0, float) = WinningCondition_Assault();
624 MUTATOR_HOOKFUNCTION(as, ReadLevelCvars)
628 sv_ready_restart_after_countdown = 0;
631 MUTATOR_HOOKFUNCTION(as, OnEntityPreSpawn)
633 entity ent = M_ARGV(0, entity);
635 switch(ent.classname)
637 case "info_player_team1":
638 case "info_player_team2":
639 case "info_player_team3":
640 case "info_player_team4":
645 MUTATOR_HOOKFUNCTION(as, ReadyRestart_Deny)
647 // readyrestart not supported (yet)