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 spr.classname = "sprite_waypoint";
133 WaypointSprite_UpdateRule(spr, assault_attacker_team, SPRITERULE_TEAMPLAY);
134 if(it.classname == "func_assault_destructible")
136 WaypointSprite_UpdateSprites(spr, WP_AssaultDefend, WP_AssaultDestroy, WP_AssaultDestroy);
137 WaypointSprite_UpdateMaxHealth(spr, it.max_health);
138 WaypointSprite_UpdateHealth(spr, GetResource(it, RES_HEALTH));
142 WaypointSprite_UpdateSprites(spr, WP_AssaultDefend, WP_AssaultPush, WP_AssaultPush);
146 void target_objective_decrease_findtarget(entity this)
148 assault_setenemytoobjective(this);
151 void target_assault_roundend_reset(entity this)
153 //print("round end reset\n");
154 ++this.cnt; // up round counter
155 this.winning = false; // up round
158 void target_assault_roundend_use(entity this, entity actor, entity trigger)
160 this.winning = 1; // round has been won by attackers
163 void assault_roundstart_use(entity this, entity actor, entity trigger)
165 SUB_UseTargets(this, this, trigger);
167 //(Re)spawn all turrets
168 IL_EACH(g_turrets, true,
171 if(it.team == NUM_TEAM_1)
172 it.team = NUM_TEAM_2;
174 it.team = NUM_TEAM_1;
176 // Doubles as teamchange
180 void assault_roundstart_use_this(entity this)
182 assault_roundstart_use(this, NULL, NULL);
185 void assault_wall_think(entity this)
187 if(GetResource(this.enemy, RES_HEALTH) < 0)
190 this.solid = SOLID_NOT;
194 this.model = this.mdl;
195 this.solid = SOLID_BSP;
198 this.nextthink = time + 0.2;
202 // reset objectives, toggle spawnpoints, reset triggers, ...
203 void assault_new_round(entity this)
205 //bprint("ASSAULT: new round\n");
208 this.winning = this.winning + 1;
210 // swap attacker/defender roles
211 if(assault_attacker_team == NUM_TEAM_1)
212 assault_attacker_team = NUM_TEAM_2;
214 assault_attacker_team = NUM_TEAM_1;
216 IL_EACH(g_saved_team, !IS_CLIENT(it),
218 if(it.team_saved == NUM_TEAM_1)
219 it.team_saved = NUM_TEAM_2;
220 else if(it.team_saved == NUM_TEAM_2)
221 it.team_saved = NUM_TEAM_1;
224 // reset the level with a countdown
225 cvar_set("timelimit", ftos(ceil(time - AS_ROUND_DELAY - game_starttime) / 60));
226 ReadyRestart_force(); // sets game_starttime
231 void as_round_think()
233 game_stopped = false;
234 assault_new_round(as_round.ent_winning);
239 // Assault winning condition: If the attackers triggered a round end (by fulfilling all objectives)
240 // they win. Otherwise the defending team wins once the timelimit passes.
241 int WinningCondition_Assault()
246 WinningConditionHelper(NULL); // set worldstatus
248 int status = WINNING_NO;
249 // as the timelimit has not yet passed just assume the defending team will win
250 if(assault_attacker_team == NUM_TEAM_1)
252 SetWinners(team, NUM_TEAM_2);
256 SetWinners(team, NUM_TEAM_1);
260 ent = find(NULL, classname, "target_assault_roundend");
263 if(ent.winning) // round end has been triggered by attacking team
265 bprint("Assault: round completed.\n");
266 SetWinners(team, assault_attacker_team);
268 TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 666 - TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 0));
270 if(ent.cnt == 1 || autocvar_g_campaign) // this was the second round
272 status = WINNING_YES;
276 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ASSAULT_OBJ_DESTROYED, ceil(time - game_starttime));
277 as_round = new(as_round);
278 as_round.think = as_round_think;
279 as_round.ent_winning = ent;
280 as_round.nextthink = time + AS_ROUND_DELAY;
283 // make sure timelimit isn't hit while the game is blocked
284 if(autocvar_timelimit > 0)
285 if(time + AS_ROUND_DELAY >= game_starttime + autocvar_timelimit * 60)
286 cvar_set("timelimit", ftos(autocvar_timelimit + AS_ROUND_DELAY / 60));
295 spawnfunc(info_player_attacker)
297 if (!g_assault) { delete(this); return; }
299 this.team = NUM_TEAM_1; // red, gets swapped every round
300 spawnfunc_info_player_deathmatch(this);
303 spawnfunc(info_player_defender)
305 if (!g_assault) { delete(this); return; }
307 this.team = NUM_TEAM_2; // blue, gets swapped every round
308 spawnfunc_info_player_deathmatch(this);
311 spawnfunc(target_objective)
313 if (!g_assault) { delete(this); return; }
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 IL_PUSH(g_assault_objectivedecreasers, this);
331 this.use = assault_objective_decrease_use;
332 SetResourceExplicit(this, RES_HEALTH, ASSAULT_VALUE_INACTIVE);
333 this.max_health = ASSAULT_VALUE_INACTIVE;
336 InitializeEntity(this, target_objective_decrease_findtarget, INITPRIO_FINDTARGET);
339 // destructible walls that can be used to trigger target_objective_decrease
340 bool destructible_heal(entity targ, entity inflictor, float amount, float limit)
342 float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
343 float hlth = GetResource(targ, RES_HEALTH);
344 if (hlth <= 0 || hlth >= true_limit)
347 GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
350 WaypointSprite_UpdateHealth(targ.sprite, GetResource(targ, RES_HEALTH));
352 func_breakable_colormod(targ);
356 spawnfunc(func_assault_destructible)
358 if (!g_assault) { delete(this); return; }
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.mdl = this.model;
377 _setmodel(this, this.mdl);
378 this.solid = SOLID_BSP;
379 setthink(this, assault_wall_think);
380 this.nextthink = time;
381 InitializeEntity(this, assault_setenemytoobjective, INITPRIO_FINDTARGET);
384 spawnfunc(target_assault_roundend)
386 if (!g_assault) { delete(this); return; }
388 this.winning = 0; // round not yet won by attackers
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.use = assault_roundstart_use;
400 this.reset2 = assault_roundstart_use_this;
401 InitializeEntity(this, assault_roundstart_use_this, INITPRIO_FINDTARGET);
405 void havocbot_goalrating_ast_targets(entity this, float ratingscale)
407 IL_EACH(g_assault_destructibles, it.bot_attack,
414 IL_EACH(g_assault_objectivedecreasers, it.targetname == destr.target,
416 float hlth = GetResource(it.enemy, RES_HEALTH);
417 if (hlth > 0 && hlth < ASSAULT_VALUE_INACTIVE)
427 vector p = 0.5 * (it.absmin + it.absmax);
429 // Find and rate waypoints around it
432 float bestvalue = FLOAT_MAX;
434 for (float radius = 500; radius <= 1500 && !found; radius += 500)
436 FOREACH_ENTITY_RADIUS(p, radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
438 if(checkpvs(it.origin, des))
441 if(it.cnt < bestvalue)
452 /// dprint("waypoints around target were found\n");
453 // te_lightning2(NULL, '0 0 0', best.origin);
454 // te_knightspike(best.origin);
456 navigation_routerating(this, best, ratingscale, 4000);
459 this.havocbot_attack_time = 0;
461 if(checkpvs(this.origin + this.view_ofs, it))
462 if(checkpvs(this.origin + this.view_ofs, best))
464 // dprint("increasing attack time for this target\n");
465 this.havocbot_attack_time = time + 2;
471 void havocbot_role_ast_offense(entity this)
475 this.havocbot_attack_time = 0;
476 havocbot_ast_reset_role(this);
480 // Set the role timeout if necessary
481 if (!this.havocbot_role_timeout)
482 this.havocbot_role_timeout = time + 120;
484 if (time > this.havocbot_role_timeout)
486 havocbot_ast_reset_role(this);
490 if(this.havocbot_attack_time>time)
493 if (navigation_goalrating_timeout(this))
496 navigation_goalrating_start(this);
497 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 650);
498 havocbot_goalrating_ast_targets(this, 20000);
499 havocbot_goalrating_items(this, 30000, this.origin, 10000);
500 navigation_goalrating_end(this);
502 navigation_goalrating_timeout_set(this);
506 void havocbot_role_ast_defense(entity this)
510 this.havocbot_attack_time = 0;
511 havocbot_ast_reset_role(this);
515 // Set the role timeout if necessary
516 if (!this.havocbot_role_timeout)
517 this.havocbot_role_timeout = time + 120;
519 if (time > this.havocbot_role_timeout)
521 havocbot_ast_reset_role(this);
525 if(this.havocbot_attack_time>time)
528 if (navigation_goalrating_timeout(this))
531 navigation_goalrating_start(this);
532 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 3000);
533 havocbot_goalrating_ast_targets(this, 20000);
534 havocbot_goalrating_items(this, 30000, this.origin, 10000);
535 navigation_goalrating_end(this);
537 navigation_goalrating_timeout_set(this);
541 void havocbot_role_ast_setrole(entity this, float role)
545 case HAVOCBOT_AST_ROLE_DEFENSE:
546 this.havocbot_role = havocbot_role_ast_defense;
547 this.havocbot_role_timeout = 0;
549 case HAVOCBOT_AST_ROLE_OFFENSE:
550 this.havocbot_role = havocbot_role_ast_offense;
551 this.havocbot_role_timeout = 0;
556 void havocbot_ast_reset_role(entity this)
561 if(this.team == assault_attacker_team)
562 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_OFFENSE);
564 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_DEFENSE);
568 MUTATOR_HOOKFUNCTION(as, PlayerSpawn)
570 entity player = M_ARGV(0, entity);
572 if(player.team == assault_attacker_team)
573 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_ATTACKING);
575 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_DEFENDING);
578 MUTATOR_HOOKFUNCTION(as, TurretSpawn)
580 entity turret = M_ARGV(0, entity);
582 if(!turret.team || turret.team == FLOAT_MAX)
583 turret.team = assault_attacker_team; // this gets reversed when match starts (assault_roundstart_use)
586 MUTATOR_HOOKFUNCTION(as, VehicleInit)
588 entity veh = M_ARGV(0, entity);
590 veh.nextthink = time + 0.5;
593 MUTATOR_HOOKFUNCTION(as, HavocBot_ChooseRole)
595 entity bot = M_ARGV(0, entity);
597 havocbot_ast_reset_role(bot);
601 MUTATOR_HOOKFUNCTION(as, PlayHitsound)
603 entity frag_victim = M_ARGV(0, entity);
605 return (frag_victim.classname == "func_assault_destructible");
608 MUTATOR_HOOKFUNCTION(as, TeamBalance_CheckAllowedTeams)
610 // assault always has 2 teams
611 M_ARGV(0, float) = BIT(0) | BIT(1);
615 MUTATOR_HOOKFUNCTION(as, CheckRules_World)
617 M_ARGV(0, float) = WinningCondition_Assault();
621 MUTATOR_HOOKFUNCTION(as, ReadLevelCvars)
625 sv_ready_restart_after_countdown = 0;
628 MUTATOR_HOOKFUNCTION(as, OnEntityPreSpawn)
630 entity ent = M_ARGV(0, entity);
632 switch(ent.classname)
634 case "info_player_team1":
635 case "info_player_team2":
636 case "info_player_team3":
637 case "info_player_team4":
642 MUTATOR_HOOKFUNCTION(as, ReadyRestart_Deny)
644 // readyrestart not supported (yet)