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
15 void assault_objective_use(entity this, entity actor, entity trigger)
18 SetResourceExplicit(this, RES_HEALTH, 100);
19 //print("^2Activated objective ", this.targetname, "=", etos(this), "\n");
20 //print("Activator is ", actor.classname, "\n");
22 IL_EACH(g_assault_objectivedecreasers, it.target == this.targetname,
24 target_objective_decrease_activate(it);
28 vector target_objective_spawn_evalfunc(entity this, entity player, entity spot, vector current)
30 float hlth = GetResource(this, RES_HEALTH);
31 if (hlth < 0 || hlth >= ASSAULT_VALUE_INACTIVE)
36 // reset this objective. Used when spawning an objective
37 // and when a new round starts
38 void assault_objective_reset(entity this)
40 SetResourceExplicit(this, RES_HEALTH, ASSAULT_VALUE_INACTIVE);
43 // decrease the health of targeted objectives
44 void assault_objective_decrease_use(entity this, entity actor, entity trigger)
46 if(actor.team != assault_attacker_team)
48 // wrong team triggered decrease
52 if(trigger.assault_sprite)
54 WaypointSprite_Disown(trigger.assault_sprite, waypointsprite_deadlifetime);
55 if(trigger.classname == "func_assault_destructible")
56 trigger.sprite = NULL; // TODO: just unsetting it?!
59 return; // already activated! cannot activate again!
61 float hlth = GetResource(this.enemy, RES_HEALTH);
62 if (hlth < ASSAULT_VALUE_INACTIVE)
64 if (hlth - this.dmg > 0.5)
66 GameRules_scoring_add_team(actor, SCORE, this.dmg);
67 TakeResource(this.enemy, RES_HEALTH, this.dmg);
71 GameRules_scoring_add_team(actor, SCORE, hlth);
72 GameRules_scoring_add_team(actor, ASSAULT_OBJECTIVES, 1);
73 SetResourceExplicit(this.enemy, RES_HEALTH, -1);
75 if(this.enemy.message)
76 FOREACH_CLIENT(IS_PLAYER(it), { centerprint(it, this.enemy.message); });
78 SUB_UseTargets(this.enemy, this, trigger);
83 void assault_setenemytoobjective(entity this)
85 IL_EACH(g_assault_objectives, it.targetname == this.target,
87 if(this.enemy == NULL)
90 objerror(this, "more than one objective as target - fix the map!");
94 if(this.enemy == NULL)
95 objerror(this, "no objective as target - fix the map!");
98 bool assault_decreaser_sprite_visible(entity this, entity player, entity view)
100 if(GetResource(this.assault_decreaser.enemy, RES_HEALTH) >= ASSAULT_VALUE_INACTIVE)
106 void target_objective_decrease_activate(entity this)
110 FOREACH_ENTITY_STRING(target, this.targetname,
112 if(it.assault_sprite != NULL)
114 WaypointSprite_Disown(it.assault_sprite, waypointsprite_deadlifetime);
115 if(it.classname == "func_assault_destructible")
116 it.sprite = NULL; // TODO: just unsetting it?!
119 spr = WaypointSprite_SpawnFixed(WP_AssaultDefend, 0.5 * (it.absmin + it.absmax), it, assault_sprite, RADARICON_OBJECTIVE);
120 spr.assault_decreaser = this;
121 spr.waypointsprite_visible_for_player = assault_decreaser_sprite_visible;
122 WaypointSprite_UpdateRule(spr, assault_attacker_team, SPRITERULE_TEAMPLAY);
123 if(it.classname == "func_assault_destructible")
125 WaypointSprite_UpdateSprites(spr, WP_AssaultDefend, WP_AssaultDestroy, WP_AssaultDestroy);
126 WaypointSprite_UpdateMaxHealth(spr, it.max_health);
127 WaypointSprite_UpdateHealth(spr, GetResource(it, RES_HEALTH));
131 WaypointSprite_UpdateSprites(spr, WP_AssaultDefend, WP_AssaultPush, WP_AssaultPush);
135 void target_objective_decrease_findtarget(entity this)
137 assault_setenemytoobjective(this);
140 void target_assault_roundend_reset(entity this)
142 //print("round end reset\n");
143 ++this.cnt; // up round counter
144 this.winning = false; // up round
147 void target_assault_roundend_use(entity this, entity actor, entity trigger)
149 this.winning = 1; // round has been won by attackers
152 void assault_roundstart_use(entity this, entity actor, entity trigger)
154 SUB_UseTargets(this, this, trigger);
156 //(Re)spawn all turrets
157 IL_EACH(g_turrets, true,
160 if(it.team == NUM_TEAM_1)
161 it.team = NUM_TEAM_2;
163 it.team = NUM_TEAM_1;
165 // Doubles as teamchange
169 void assault_roundstart_use_this(entity this)
171 assault_roundstart_use(this, NULL, NULL);
174 void assault_wall_think(entity this)
176 if(GetResource(this.enemy, RES_HEALTH) < 0)
179 this.solid = SOLID_NOT;
183 this.model = this.mdl;
184 this.solid = SOLID_BSP;
187 this.nextthink = time + 0.2;
191 // reset objectives, toggle spawnpoints, reset triggers, ...
192 void assault_new_round(entity this)
195 this.winning = this.winning + 1;
197 // swap attacker/defender roles
198 if(assault_attacker_team == NUM_TEAM_1)
199 assault_attacker_team = NUM_TEAM_2;
201 assault_attacker_team = NUM_TEAM_1;
203 IL_EACH(g_saved_team, !IS_CLIENT(it),
205 if(it.team_saved == NUM_TEAM_1)
206 it.team_saved = NUM_TEAM_2;
207 else if(it.team_saved == NUM_TEAM_2)
208 it.team_saved = NUM_TEAM_1;
211 // reset the level with a countdown
212 cvar_set("timelimit", ftos(ceil(time - AS_ROUND_DELAY - game_starttime) / 60));
213 bprint("Starting second round...\n");
214 ReadyRestart_force(true); // sets game_starttime
219 void as_round_think()
221 game_stopped = false;
222 assault_new_round(as_round.ent_winning);
227 // Assault winning condition: If the attackers triggered a round end (by fulfilling all objectives)
228 // they win. Otherwise the defending team wins once the timelimit passes.
229 int WinningCondition_Assault()
234 WinningConditionHelper(NULL); // set worldstatus
236 int status = WINNING_NO;
237 // as the timelimit has not yet passed just assume the defending team will win
238 if(assault_attacker_team == NUM_TEAM_1)
240 SetWinners(team, NUM_TEAM_2);
244 SetWinners(team, NUM_TEAM_1);
248 ent = find(NULL, classname, "target_assault_roundend");
251 if(ent.winning) // round end has been triggered by attacking team
253 bprint(Team_ColoredFullName(assault_attacker_team), " destroyed the objective in ", process_time(2, ceil(time - game_starttime)), "\n");
254 SetWinners(team, assault_attacker_team);
256 TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 666 - TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 0));
258 if(ent.cnt == 1 || autocvar_g_campaign) // this was the second round
260 status = WINNING_YES;
264 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ASSAULT_OBJ_DESTROYED, ceil(time - game_starttime));
265 as_round = new(as_round);
266 as_round.think = as_round_think;
267 as_round.ent_winning = ent;
268 as_round.nextthink = time + AS_ROUND_DELAY;
271 // make sure timelimit isn't hit while the game is blocked
272 if(autocvar_timelimit > 0)
273 if(time + AS_ROUND_DELAY >= game_starttime + autocvar_timelimit * 60)
274 cvar_set("timelimit", ftos(autocvar_timelimit + AS_ROUND_DELAY / 60));
283 spawnfunc(info_player_attacker)
285 if (!g_assault) { delete(this); return; }
287 this.team = NUM_TEAM_1; // red, gets swapped every round
288 spawnfunc_info_player_deathmatch(this);
291 spawnfunc(info_player_defender)
293 if (!g_assault) { delete(this); return; }
295 this.team = NUM_TEAM_2; // blue, gets swapped every round
296 spawnfunc_info_player_deathmatch(this);
299 spawnfunc(target_objective)
301 if (!g_assault) { delete(this); return; }
303 IL_PUSH(g_assault_objectives, this);
304 this.use = assault_objective_use;
305 this.reset = assault_objective_reset;
307 this.spawn_evalfunc = target_objective_spawn_evalfunc;
310 spawnfunc(target_objective_decrease)
312 if (!g_assault) { delete(this); return; }
314 IL_PUSH(g_assault_objectivedecreasers, this);
319 this.use = assault_objective_decrease_use;
320 SetResourceExplicit(this, RES_HEALTH, ASSAULT_VALUE_INACTIVE);
321 this.max_health = ASSAULT_VALUE_INACTIVE;
324 InitializeEntity(this, target_objective_decrease_findtarget, INITPRIO_FINDTARGET);
327 // destructible walls that can be used to trigger target_objective_decrease
328 bool destructible_heal(entity targ, entity inflictor, float amount, float limit)
330 float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
331 float hlth = GetResource(targ, RES_HEALTH);
332 if (hlth <= 0 || hlth >= true_limit)
335 GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
338 WaypointSprite_UpdateHealth(targ.sprite, GetResource(targ, RES_HEALTH));
340 func_breakable_colormod(targ);
344 spawnfunc(func_assault_destructible)
346 if (!g_assault) { delete(this); return; }
349 this.event_heal = destructible_heal;
350 IL_PUSH(g_assault_destructibles, this);
352 if(assault_attacker_team == NUM_TEAM_1)
353 this.team = NUM_TEAM_2;
355 this.team = NUM_TEAM_1;
357 func_breakable_setup(this);
360 spawnfunc(func_assault_wall)
362 if (!g_assault) { delete(this); return; }
364 this.mdl = this.model;
365 _setmodel(this, this.mdl);
366 this.solid = SOLID_BSP;
367 setthink(this, assault_wall_think);
368 this.nextthink = time;
369 InitializeEntity(this, assault_setenemytoobjective, INITPRIO_FINDTARGET);
372 spawnfunc(target_assault_roundend)
374 if (!g_assault) { delete(this); return; }
376 this.winning = 0; // round not yet won by attackers
377 this.use = target_assault_roundend_use;
378 this.cnt = 0; // first round
379 this.reset = target_assault_roundend_reset;
382 spawnfunc(target_assault_roundstart)
384 if (!g_assault) { delete(this); return; }
386 assault_attacker_team = NUM_TEAM_1;
387 this.use = assault_roundstart_use;
388 this.reset2 = assault_roundstart_use_this;
389 InitializeEntity(this, assault_roundstart_use_this, INITPRIO_FINDTARGET);
393 void havocbot_goalrating_ast_targets(entity this, float ratingscale)
395 IL_EACH(g_assault_destructibles, it.bot_attack,
402 IL_EACH(g_assault_objectivedecreasers, it.targetname == destr.target,
404 float hlth = GetResource(it.enemy, RES_HEALTH);
405 if (hlth > 0 && hlth < ASSAULT_VALUE_INACTIVE)
415 vector p = 0.5 * (it.absmin + it.absmax);
417 // Find and rate waypoints around it
420 float bestvalue = FLOAT_MAX;
422 for (float radius = 500; radius <= 1500 && !found; radius += 500)
424 FOREACH_ENTITY_RADIUS(p, radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
426 if(checkpvs(it.origin, des))
429 if(it.cnt < bestvalue)
440 /// dprint("waypoints around target were found\n");
441 // te_lightning2(NULL, '0 0 0', best.origin);
442 // te_knightspike(best.origin);
444 navigation_routerating(this, best, ratingscale, 4000);
447 this.havocbot_attack_time = 0;
449 if(checkpvs(this.origin + this.view_ofs, it))
450 if(checkpvs(this.origin + this.view_ofs, best))
452 // dprint("increasing attack time for this target\n");
453 this.havocbot_attack_time = time + 2;
459 void havocbot_role_ast_offense(entity this)
463 this.havocbot_attack_time = 0;
464 havocbot_ast_reset_role(this);
468 // Set the role timeout if necessary
469 if (!this.havocbot_role_timeout)
470 this.havocbot_role_timeout = time + 120;
472 if (time > this.havocbot_role_timeout)
474 havocbot_ast_reset_role(this);
478 if(this.havocbot_attack_time>time)
481 if (navigation_goalrating_timeout(this))
484 navigation_goalrating_start(this);
485 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 650);
486 havocbot_goalrating_ast_targets(this, 20000);
487 havocbot_goalrating_items(this, 30000, this.origin, 10000);
488 navigation_goalrating_end(this);
490 navigation_goalrating_timeout_set(this);
494 void havocbot_role_ast_defense(entity this)
498 this.havocbot_attack_time = 0;
499 havocbot_ast_reset_role(this);
503 // Set the role timeout if necessary
504 if (!this.havocbot_role_timeout)
505 this.havocbot_role_timeout = time + 120;
507 if (time > this.havocbot_role_timeout)
509 havocbot_ast_reset_role(this);
513 if(this.havocbot_attack_time>time)
516 if (navigation_goalrating_timeout(this))
519 navigation_goalrating_start(this);
520 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 3000);
521 havocbot_goalrating_ast_targets(this, 20000);
522 havocbot_goalrating_items(this, 30000, this.origin, 10000);
523 navigation_goalrating_end(this);
525 navigation_goalrating_timeout_set(this);
529 void havocbot_role_ast_setrole(entity this, float role)
533 case HAVOCBOT_AST_ROLE_DEFENSE:
534 this.havocbot_role = havocbot_role_ast_defense;
535 this.havocbot_role_timeout = 0;
537 case HAVOCBOT_AST_ROLE_OFFENSE:
538 this.havocbot_role = havocbot_role_ast_offense;
539 this.havocbot_role_timeout = 0;
544 void havocbot_ast_reset_role(entity this)
549 if(this.team == assault_attacker_team)
550 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_OFFENSE);
552 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_DEFENSE);
556 MUTATOR_HOOKFUNCTION(as, PlayerSpawn)
558 entity player = M_ARGV(0, entity);
560 if(player.team == assault_attacker_team)
561 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_ATTACKING);
563 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_DEFENDING);
566 MUTATOR_HOOKFUNCTION(as, TurretSpawn)
568 entity turret = M_ARGV(0, entity);
570 if(!turret.team || turret.team == FLOAT_MAX)
571 turret.team = assault_attacker_team; // this gets reversed when match starts (assault_roundstart_use)
574 MUTATOR_HOOKFUNCTION(as, VehicleInit)
576 entity veh = M_ARGV(0, entity);
578 veh.nextthink = time + 0.5;
581 MUTATOR_HOOKFUNCTION(as, HavocBot_ChooseRole)
583 entity bot = M_ARGV(0, entity);
585 havocbot_ast_reset_role(bot);
589 MUTATOR_HOOKFUNCTION(as, PlayHitsound)
591 entity frag_victim = M_ARGV(0, entity);
593 return (frag_victim.classname == "func_assault_destructible");
596 MUTATOR_HOOKFUNCTION(as, TeamBalance_CheckAllowedTeams)
598 // assault always has 2 teams
599 M_ARGV(0, float) = BIT(0) | BIT(1);
603 MUTATOR_HOOKFUNCTION(as, CheckRules_World)
605 M_ARGV(0, float) = WinningCondition_Assault();
609 MUTATOR_HOOKFUNCTION(as, ReadLevelCvars)
613 sv_ready_restart_after_countdown = 0;
616 MUTATOR_HOOKFUNCTION(as, OnEntityPreSpawn)
618 entity ent = M_ARGV(0, entity);
620 switch(ent.classname)
622 case "info_player_team1":
623 case "info_player_team2":
624 case "info_player_team3":
625 case "info_player_team4":
630 MUTATOR_HOOKFUNCTION(as, ReadyRestart_Deny)
632 // readyrestart not supported (yet)