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 <server/g_damage.qh>
7 #include <server/g_world.qh>
8 #include <server/spawnpoints.qh>
11 #define AS_ROUND_DELAY 5
13 IntrusiveList g_assault_destructibles;
14 IntrusiveList g_assault_objectivedecreasers;
15 IntrusiveList g_assault_objectives;
16 STATIC_INIT(g_assault)
18 g_assault_destructibles = IL_NEW();
19 g_assault_objectivedecreasers = IL_NEW();
20 g_assault_objectives = IL_NEW();
24 void assault_objective_use(entity this, entity actor, entity trigger)
27 SetResourceExplicit(this, RES_HEALTH, 100);
28 //print("^2Activated objective ", this.targetname, "=", etos(this), "\n");
29 //print("Activator is ", actor.classname, "\n");
31 IL_EACH(g_assault_objectivedecreasers, it.target == this.targetname,
33 target_objective_decrease_activate(it);
37 vector target_objective_spawn_evalfunc(entity this, entity player, entity spot, vector current)
39 float hlth = GetResource(this, RES_HEALTH);
40 if (hlth < 0 || hlth >= ASSAULT_VALUE_INACTIVE)
45 // reset this objective. Used when spawning an objective
46 // and when a new round starts
47 void assault_objective_reset(entity this)
49 SetResourceExplicit(this, RES_HEALTH, ASSAULT_VALUE_INACTIVE);
52 // decrease the health of targeted objectives
53 void assault_objective_decrease_use(entity this, entity actor, entity trigger)
55 if(actor.team != assault_attacker_team)
57 // wrong team triggered decrease
61 if(trigger.assault_sprite)
63 WaypointSprite_Disown(trigger.assault_sprite, waypointsprite_deadlifetime);
64 if(trigger.classname == "func_assault_destructible")
65 trigger.sprite = NULL; // TODO: just unsetting it?!
68 return; // already activated! cannot activate again!
70 float hlth = GetResource(this.enemy, RES_HEALTH);
71 if (hlth < ASSAULT_VALUE_INACTIVE)
73 if (hlth - this.dmg > 0.5)
75 GameRules_scoring_add_team(actor, SCORE, this.dmg);
76 TakeResource(this.enemy, RES_HEALTH, this.dmg);
80 GameRules_scoring_add_team(actor, SCORE, hlth);
81 GameRules_scoring_add_team(actor, ASSAULT_OBJECTIVES, 1);
82 SetResourceExplicit(this.enemy, RES_HEALTH, -1);
84 if(this.enemy.message)
85 FOREACH_CLIENT(IS_PLAYER(it), { centerprint(it, this.enemy.message); });
87 SUB_UseTargets(this.enemy, this, trigger);
92 void assault_setenemytoobjective(entity this)
94 IL_EACH(g_assault_objectives, it.targetname == this.target,
96 if(this.enemy == NULL)
99 objerror(this, "more than one objective as target - fix the map!");
103 if(this.enemy == NULL)
104 objerror(this, "no objective as target - fix the map!");
107 bool assault_decreaser_sprite_visible(entity this, entity player, entity view)
109 if(GetResource(this.assault_decreaser.enemy, RES_HEALTH) >= ASSAULT_VALUE_INACTIVE)
115 void target_objective_decrease_activate(entity this)
119 FOREACH_ENTITY_STRING(target, this.targetname,
121 if(it.assault_sprite != NULL)
123 WaypointSprite_Disown(it.assault_sprite, waypointsprite_deadlifetime);
124 if(it.classname == "func_assault_destructible")
125 it.sprite = NULL; // TODO: just unsetting it?!
128 spr = WaypointSprite_SpawnFixed(WP_AssaultDefend, 0.5 * (it.absmin + it.absmax), it, assault_sprite, RADARICON_OBJECTIVE);
129 spr.assault_decreaser = this;
130 spr.waypointsprite_visible_for_player = assault_decreaser_sprite_visible;
131 spr.classname = "sprite_waypoint";
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 this.classname = "target_objective";
315 IL_PUSH(g_assault_objectives, this);
316 this.use = assault_objective_use;
317 this.reset = assault_objective_reset;
319 this.spawn_evalfunc = target_objective_spawn_evalfunc;
322 spawnfunc(target_objective_decrease)
324 if (!g_assault) { delete(this); return; }
326 this.classname = "target_objective_decrease";
327 IL_PUSH(g_assault_objectivedecreasers, this);
332 this.use = assault_objective_decrease_use;
333 SetResourceExplicit(this, RES_HEALTH, ASSAULT_VALUE_INACTIVE);
334 this.max_health = ASSAULT_VALUE_INACTIVE;
337 InitializeEntity(this, target_objective_decrease_findtarget, INITPRIO_FINDTARGET);
340 // destructible walls that can be used to trigger target_objective_decrease
341 bool destructible_heal(entity targ, entity inflictor, float amount, float limit)
343 float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
344 float hlth = GetResource(targ, RES_HEALTH);
345 if (hlth <= 0 || hlth >= true_limit)
348 GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
351 WaypointSprite_UpdateHealth(targ.sprite, GetResource(targ, RES_HEALTH));
353 func_breakable_colormod(targ);
357 spawnfunc(func_assault_destructible)
359 if (!g_assault) { delete(this); return; }
362 this.classname = "func_assault_destructible";
363 this.event_heal = destructible_heal;
364 IL_PUSH(g_assault_destructibles, this);
366 if(assault_attacker_team == NUM_TEAM_1)
367 this.team = NUM_TEAM_2;
369 this.team = NUM_TEAM_1;
371 spawnfunc_func_breakable(this);
374 spawnfunc(func_assault_wall)
376 if (!g_assault) { delete(this); return; }
378 this.classname = "func_assault_wall";
379 this.mdl = this.model;
380 _setmodel(this, this.mdl);
381 this.solid = SOLID_BSP;
382 setthink(this, assault_wall_think);
383 this.nextthink = time;
384 InitializeEntity(this, assault_setenemytoobjective, INITPRIO_FINDTARGET);
387 spawnfunc(target_assault_roundend)
389 if (!g_assault) { delete(this); return; }
391 this.winning = 0; // round not yet won by attackers
392 this.classname = "target_assault_roundend";
393 this.use = target_assault_roundend_use;
394 this.cnt = 0; // first round
395 this.reset = target_assault_roundend_reset;
398 spawnfunc(target_assault_roundstart)
400 if (!g_assault) { delete(this); return; }
402 assault_attacker_team = NUM_TEAM_1;
403 this.classname = "target_assault_roundstart";
404 this.use = assault_roundstart_use;
405 this.reset2 = assault_roundstart_use_this;
406 InitializeEntity(this, assault_roundstart_use_this, INITPRIO_FINDTARGET);
410 void havocbot_goalrating_ast_targets(entity this, float ratingscale)
412 IL_EACH(g_assault_destructibles, it.bot_attack,
419 IL_EACH(g_assault_objectivedecreasers, it.targetname == destr.target,
421 float hlth = GetResource(it.enemy, RES_HEALTH);
422 if (hlth > 0 && hlth < ASSAULT_VALUE_INACTIVE)
432 vector p = 0.5 * (it.absmin + it.absmax);
434 // Find and rate waypoints around it
437 float bestvalue = FLOAT_MAX;
439 for (float radius = 500; radius <= 1500 && !found; radius += 500)
441 FOREACH_ENTITY_RADIUS(p, radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
443 if(checkpvs(it.origin, des))
446 if(it.cnt < bestvalue)
457 /// dprint("waypoints around target were found\n");
458 // te_lightning2(NULL, '0 0 0', best.origin);
459 // te_knightspike(best.origin);
461 navigation_routerating(this, best, ratingscale, 4000);
464 this.havocbot_attack_time = 0;
466 if(checkpvs(this.origin + this.view_ofs, it))
467 if(checkpvs(this.origin + this.view_ofs, best))
469 // dprint("increasing attack time for this target\n");
470 this.havocbot_attack_time = time + 2;
476 void havocbot_role_ast_offense(entity this)
480 this.havocbot_attack_time = 0;
481 havocbot_ast_reset_role(this);
485 // Set the role timeout if necessary
486 if (!this.havocbot_role_timeout)
487 this.havocbot_role_timeout = time + 120;
489 if (time > this.havocbot_role_timeout)
491 havocbot_ast_reset_role(this);
495 if(this.havocbot_attack_time>time)
498 if (navigation_goalrating_timeout(this))
501 navigation_goalrating_start(this);
502 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 650);
503 havocbot_goalrating_ast_targets(this, 20000);
504 havocbot_goalrating_items(this, 30000, this.origin, 10000);
505 navigation_goalrating_end(this);
507 navigation_goalrating_timeout_set(this);
511 void havocbot_role_ast_defense(entity this)
515 this.havocbot_attack_time = 0;
516 havocbot_ast_reset_role(this);
520 // Set the role timeout if necessary
521 if (!this.havocbot_role_timeout)
522 this.havocbot_role_timeout = time + 120;
524 if (time > this.havocbot_role_timeout)
526 havocbot_ast_reset_role(this);
530 if(this.havocbot_attack_time>time)
533 if (navigation_goalrating_timeout(this))
536 navigation_goalrating_start(this);
537 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 3000);
538 havocbot_goalrating_ast_targets(this, 20000);
539 havocbot_goalrating_items(this, 30000, this.origin, 10000);
540 navigation_goalrating_end(this);
542 navigation_goalrating_timeout_set(this);
546 void havocbot_role_ast_setrole(entity this, float role)
550 case HAVOCBOT_AST_ROLE_DEFENSE:
551 this.havocbot_role = havocbot_role_ast_defense;
552 this.havocbot_role_timeout = 0;
554 case HAVOCBOT_AST_ROLE_OFFENSE:
555 this.havocbot_role = havocbot_role_ast_offense;
556 this.havocbot_role_timeout = 0;
561 void havocbot_ast_reset_role(entity this)
566 if(this.team == assault_attacker_team)
567 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_OFFENSE);
569 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_DEFENSE);
573 MUTATOR_HOOKFUNCTION(as, PlayerSpawn)
575 entity player = M_ARGV(0, entity);
577 if(player.team == assault_attacker_team)
578 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_ATTACKING);
580 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_DEFENDING);
583 MUTATOR_HOOKFUNCTION(as, TurretSpawn)
585 entity turret = M_ARGV(0, entity);
587 if(!turret.team || turret.team == FLOAT_MAX)
588 turret.team = assault_attacker_team; // this gets reversed when match starts (assault_roundstart_use)
591 MUTATOR_HOOKFUNCTION(as, VehicleInit)
593 entity veh = M_ARGV(0, entity);
595 veh.nextthink = time + 0.5;
598 MUTATOR_HOOKFUNCTION(as, HavocBot_ChooseRole)
600 entity bot = M_ARGV(0, entity);
602 havocbot_ast_reset_role(bot);
606 MUTATOR_HOOKFUNCTION(as, PlayHitsound)
608 entity frag_victim = M_ARGV(0, entity);
610 return (frag_victim.classname == "func_assault_destructible");
613 MUTATOR_HOOKFUNCTION(as, TeamBalance_CheckAllowedTeams)
615 // assault always has 2 teams
616 M_ARGV(0, float) = BIT(0) | BIT(1);
620 MUTATOR_HOOKFUNCTION(as, CheckRules_World)
622 M_ARGV(0, float) = WinningCondition_Assault();
626 MUTATOR_HOOKFUNCTION(as, ReadLevelCvars)
630 sv_ready_restart_after_countdown = 0;
633 MUTATOR_HOOKFUNCTION(as, OnEntityPreSpawn)
635 entity ent = M_ARGV(0, entity);
637 switch(ent.classname)
639 case "info_player_team1":
640 case "info_player_team2":
641 case "info_player_team3":
642 case "info_player_team4":
647 MUTATOR_HOOKFUNCTION(as, ReadyRestart_Deny)
649 // readyrestart not supported (yet)