1 #include "sv_assault.qh"
3 #include <common/mapobjects/func/breakable.qh>
4 #include <server/g_damage.qh>
5 #include <server/spawnpoints.qh>
8 #define AS_ROUND_DELAY 5
10 IntrusiveList g_assault_destructibles;
11 IntrusiveList g_assault_objectivedecreasers;
12 IntrusiveList g_assault_objectives;
13 STATIC_INIT(g_assault)
15 g_assault_destructibles = IL_NEW();
16 g_assault_objectivedecreasers = IL_NEW();
17 g_assault_objectives = IL_NEW();
21 void assault_objective_use(entity this, entity actor, entity trigger)
24 SetResourceExplicit(this, RES_HEALTH, 100);
25 //print("^2Activated objective ", this.targetname, "=", etos(this), "\n");
26 //print("Activator is ", actor.classname, "\n");
28 IL_EACH(g_assault_objectivedecreasers, it.target == this.targetname,
30 target_objective_decrease_activate(it);
34 vector target_objective_spawn_evalfunc(entity this, entity player, entity spot, vector current)
36 float hlth = GetResource(this, RES_HEALTH);
37 if (hlth < 0 || hlth >= ASSAULT_VALUE_INACTIVE)
42 // reset this objective. Used when spawning an objective
43 // and when a new round starts
44 void assault_objective_reset(entity this)
46 SetResourceExplicit(this, RES_HEALTH, ASSAULT_VALUE_INACTIVE);
49 // decrease the health of targeted objectives
50 void assault_objective_decrease_use(entity this, entity actor, entity trigger)
52 if(actor.team != assault_attacker_team)
54 // wrong team triggered decrease
58 if(trigger.assault_sprite)
60 WaypointSprite_Disown(trigger.assault_sprite, waypointsprite_deadlifetime);
61 if(trigger.classname == "func_assault_destructible")
62 trigger.sprite = NULL; // TODO: just unsetting it?!
65 return; // already activated! cannot activate again!
67 float hlth = GetResource(this.enemy, RES_HEALTH);
68 if (hlth < ASSAULT_VALUE_INACTIVE)
70 if (hlth - this.dmg > 0.5)
72 GameRules_scoring_add_team(actor, SCORE, this.dmg);
73 TakeResource(this.enemy, RES_HEALTH, this.dmg);
77 GameRules_scoring_add_team(actor, SCORE, hlth);
78 GameRules_scoring_add_team(actor, ASSAULT_OBJECTIVES, 1);
79 SetResourceExplicit(this.enemy, RES_HEALTH, -1);
81 if(this.enemy.message)
82 FOREACH_CLIENT(IS_PLAYER(it), { centerprint(it, this.enemy.message); });
84 SUB_UseTargets(this.enemy, this, trigger);
89 void assault_setenemytoobjective(entity this)
91 IL_EACH(g_assault_objectives, it.targetname == this.target,
93 if(this.enemy == NULL)
96 objerror(this, "more than one objective as target - fix the map!");
100 if(this.enemy == NULL)
101 objerror(this, "no objective as target - fix the map!");
104 bool assault_decreaser_sprite_visible(entity this, entity player, entity view)
106 if(GetResource(this.assault_decreaser.enemy, RES_HEALTH) >= ASSAULT_VALUE_INACTIVE)
112 void target_objective_decrease_activate(entity this)
116 FOREACH_ENTITY_STRING(target, this.targetname,
118 if(it.assault_sprite != NULL)
120 WaypointSprite_Disown(it.assault_sprite, waypointsprite_deadlifetime);
121 if(it.classname == "func_assault_destructible")
122 it.sprite = NULL; // TODO: just unsetting it?!
125 spr = WaypointSprite_SpawnFixed(WP_AssaultDefend, 0.5 * (it.absmin + it.absmax), it, assault_sprite, RADARICON_OBJECTIVE);
126 spr.assault_decreaser = this;
127 spr.waypointsprite_visible_for_player = assault_decreaser_sprite_visible;
128 spr.classname = "sprite_waypoint";
129 WaypointSprite_UpdateRule(spr, assault_attacker_team, SPRITERULE_TEAMPLAY);
130 if(it.classname == "func_assault_destructible")
132 WaypointSprite_UpdateSprites(spr, WP_AssaultDefend, WP_AssaultDestroy, WP_AssaultDestroy);
133 WaypointSprite_UpdateMaxHealth(spr, it.max_health);
134 WaypointSprite_UpdateHealth(spr, GetResource(it, RES_HEALTH));
138 WaypointSprite_UpdateSprites(spr, WP_AssaultDefend, WP_AssaultPush, WP_AssaultPush);
142 void target_objective_decrease_findtarget(entity this)
144 assault_setenemytoobjective(this);
147 void target_assault_roundend_reset(entity this)
149 //print("round end reset\n");
150 ++this.cnt; // up round counter
151 this.winning = false; // up round
154 void target_assault_roundend_use(entity this, entity actor, entity trigger)
156 this.winning = 1; // round has been won by attackers
159 void assault_roundstart_use(entity this, entity actor, entity trigger)
161 SUB_UseTargets(this, this, trigger);
163 //(Re)spawn all turrets
164 IL_EACH(g_turrets, true,
167 if(it.team == NUM_TEAM_1)
168 it.team = NUM_TEAM_2;
170 it.team = NUM_TEAM_1;
172 // Doubles as teamchange
176 void assault_roundstart_use_this(entity this)
178 assault_roundstart_use(this, NULL, NULL);
181 void assault_wall_think(entity this)
183 if(GetResource(this.enemy, RES_HEALTH) < 0)
186 this.solid = SOLID_NOT;
190 this.model = this.mdl;
191 this.solid = SOLID_BSP;
194 this.nextthink = time + 0.2;
198 // reset objectives, toggle spawnpoints, reset triggers, ...
199 void assault_new_round(entity this)
201 //bprint("ASSAULT: new round\n");
204 this.winning = this.winning + 1;
206 // swap attacker/defender roles
207 if(assault_attacker_team == NUM_TEAM_1)
208 assault_attacker_team = NUM_TEAM_2;
210 assault_attacker_team = NUM_TEAM_1;
212 IL_EACH(g_saved_team, !IS_CLIENT(it),
214 if(it.team_saved == NUM_TEAM_1)
215 it.team_saved = NUM_TEAM_2;
216 else if(it.team_saved == NUM_TEAM_2)
217 it.team_saved = NUM_TEAM_1;
220 // reset the level with a countdown
221 cvar_set("timelimit", ftos(ceil(time - AS_ROUND_DELAY - game_starttime) / 60));
222 ReadyRestart_force(); // sets game_starttime
227 void as_round_think()
229 game_stopped = false;
230 assault_new_round(as_round.ent_winning);
235 // Assault winning condition: If the attackers triggered a round end (by fulfilling all objectives)
236 // they win. Otherwise the defending team wins once the timelimit passes.
237 int WinningCondition_Assault()
242 WinningConditionHelper(NULL); // set worldstatus
244 int status = WINNING_NO;
245 // as the timelimit has not yet passed just assume the defending team will win
246 if(assault_attacker_team == NUM_TEAM_1)
248 SetWinners(team, NUM_TEAM_2);
252 SetWinners(team, NUM_TEAM_1);
256 ent = find(NULL, classname, "target_assault_roundend");
259 if(ent.winning) // round end has been triggered by attacking team
261 bprint("Assault: round completed.\n");
262 SetWinners(team, assault_attacker_team);
264 TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 666 - TeamScore_AddToTeam(assault_attacker_team, ST_ASSAULT_OBJECTIVES, 0));
266 if(ent.cnt == 1 || autocvar_g_campaign) // this was the second round
268 status = WINNING_YES;
272 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ASSAULT_OBJ_DESTROYED, ceil(time - game_starttime));
273 as_round = new(as_round);
274 as_round.think = as_round_think;
275 as_round.ent_winning = ent;
276 as_round.nextthink = time + AS_ROUND_DELAY;
279 // make sure timelimit isn't hit while the game is blocked
280 if(autocvar_timelimit > 0)
281 if(time + AS_ROUND_DELAY >= game_starttime + autocvar_timelimit * 60)
282 cvar_set("timelimit", ftos(autocvar_timelimit + AS_ROUND_DELAY / 60));
291 spawnfunc(info_player_attacker)
293 if (!g_assault) { delete(this); return; }
295 this.team = NUM_TEAM_1; // red, gets swapped every round
296 spawnfunc_info_player_deathmatch(this);
299 spawnfunc(info_player_defender)
301 if (!g_assault) { delete(this); return; }
303 this.team = NUM_TEAM_2; // blue, gets swapped every round
304 spawnfunc_info_player_deathmatch(this);
307 spawnfunc(target_objective)
309 if (!g_assault) { delete(this); return; }
311 this.classname = "target_objective";
312 IL_PUSH(g_assault_objectives, this);
313 this.use = assault_objective_use;
314 this.reset = assault_objective_reset;
316 this.spawn_evalfunc = target_objective_spawn_evalfunc;
319 spawnfunc(target_objective_decrease)
321 if (!g_assault) { delete(this); return; }
323 this.classname = "target_objective_decrease";
324 IL_PUSH(g_assault_objectivedecreasers, this);
329 this.use = assault_objective_decrease_use;
330 SetResourceExplicit(this, RES_HEALTH, ASSAULT_VALUE_INACTIVE);
331 this.max_health = ASSAULT_VALUE_INACTIVE;
334 InitializeEntity(this, target_objective_decrease_findtarget, INITPRIO_FINDTARGET);
337 // destructible walls that can be used to trigger target_objective_decrease
338 bool destructible_heal(entity targ, entity inflictor, float amount, float limit)
340 float true_limit = ((limit != RES_LIMIT_NONE) ? limit : targ.max_health);
341 float hlth = GetResource(targ, RES_HEALTH);
342 if (hlth <= 0 || hlth >= true_limit)
345 GiveResourceWithLimit(targ, RES_HEALTH, amount, true_limit);
348 WaypointSprite_UpdateHealth(targ.sprite, GetResource(targ, RES_HEALTH));
350 func_breakable_colormod(targ);
354 spawnfunc(func_assault_destructible)
356 if (!g_assault) { delete(this); return; }
359 this.classname = "func_assault_destructible";
360 this.event_heal = destructible_heal;
361 IL_PUSH(g_assault_destructibles, this);
363 if(assault_attacker_team == NUM_TEAM_1)
364 this.team = NUM_TEAM_2;
366 this.team = NUM_TEAM_1;
368 spawnfunc_func_breakable(this);
371 spawnfunc(func_assault_wall)
373 if (!g_assault) { delete(this); return; }
375 this.classname = "func_assault_wall";
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.classname = "target_assault_roundend";
390 this.use = target_assault_roundend_use;
391 this.cnt = 0; // first round
392 this.reset = target_assault_roundend_reset;
395 spawnfunc(target_assault_roundstart)
397 if (!g_assault) { delete(this); return; }
399 assault_attacker_team = NUM_TEAM_1;
400 this.classname = "target_assault_roundstart";
401 this.use = assault_roundstart_use;
402 this.reset2 = assault_roundstart_use_this;
403 InitializeEntity(this, assault_roundstart_use_this, INITPRIO_FINDTARGET);
407 void havocbot_goalrating_ast_targets(entity this, float ratingscale)
409 IL_EACH(g_assault_destructibles, it.bot_attack,
416 IL_EACH(g_assault_objectivedecreasers, it.targetname == destr.target,
418 float hlth = GetResource(it.enemy, RES_HEALTH);
419 if (hlth > 0 && hlth < ASSAULT_VALUE_INACTIVE)
429 vector p = 0.5 * (it.absmin + it.absmax);
431 // Find and rate waypoints around it
434 float bestvalue = FLOAT_MAX;
436 for (float radius = 500; radius <= 1500 && !found; radius += 500)
438 FOREACH_ENTITY_RADIUS(p, radius, it.classname == "waypoint" && !(it.wpflags & WAYPOINTFLAG_GENERATED),
440 if(checkpvs(it.origin, des))
443 if(it.cnt < bestvalue)
454 /// dprint("waypoints around target were found\n");
455 // te_lightning2(NULL, '0 0 0', best.origin);
456 // te_knightspike(best.origin);
458 navigation_routerating(this, best, ratingscale, 4000);
461 this.havocbot_attack_time = 0;
463 if(checkpvs(this.origin + this.view_ofs, it))
464 if(checkpvs(this.origin + this.view_ofs, best))
466 // dprint("increasing attack time for this target\n");
467 this.havocbot_attack_time = time + 2;
473 void havocbot_role_ast_offense(entity this)
477 this.havocbot_attack_time = 0;
478 havocbot_ast_reset_role(this);
482 // Set the role timeout if necessary
483 if (!this.havocbot_role_timeout)
484 this.havocbot_role_timeout = time + 120;
486 if (time > this.havocbot_role_timeout)
488 havocbot_ast_reset_role(this);
492 if(this.havocbot_attack_time>time)
495 if (navigation_goalrating_timeout(this))
498 navigation_goalrating_start(this);
499 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 650);
500 havocbot_goalrating_ast_targets(this, 20000);
501 havocbot_goalrating_items(this, 30000, this.origin, 10000);
502 navigation_goalrating_end(this);
504 navigation_goalrating_timeout_set(this);
508 void havocbot_role_ast_defense(entity this)
512 this.havocbot_attack_time = 0;
513 havocbot_ast_reset_role(this);
517 // Set the role timeout if necessary
518 if (!this.havocbot_role_timeout)
519 this.havocbot_role_timeout = time + 120;
521 if (time > this.havocbot_role_timeout)
523 havocbot_ast_reset_role(this);
527 if(this.havocbot_attack_time>time)
530 if (navigation_goalrating_timeout(this))
533 navigation_goalrating_start(this);
534 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 3000);
535 havocbot_goalrating_ast_targets(this, 20000);
536 havocbot_goalrating_items(this, 30000, this.origin, 10000);
537 navigation_goalrating_end(this);
539 navigation_goalrating_timeout_set(this);
543 void havocbot_role_ast_setrole(entity this, float role)
547 case HAVOCBOT_AST_ROLE_DEFENSE:
548 this.havocbot_role = havocbot_role_ast_defense;
549 this.havocbot_role_timeout = 0;
551 case HAVOCBOT_AST_ROLE_OFFENSE:
552 this.havocbot_role = havocbot_role_ast_offense;
553 this.havocbot_role_timeout = 0;
558 void havocbot_ast_reset_role(entity this)
563 if(this.team == assault_attacker_team)
564 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_OFFENSE);
566 havocbot_role_ast_setrole(this, HAVOCBOT_AST_ROLE_DEFENSE);
570 MUTATOR_HOOKFUNCTION(as, PlayerSpawn)
572 entity player = M_ARGV(0, entity);
574 if(player.team == assault_attacker_team)
575 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_ATTACKING);
577 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ASSAULT_DEFENDING);
580 MUTATOR_HOOKFUNCTION(as, TurretSpawn)
582 entity turret = M_ARGV(0, entity);
584 if(!turret.team || turret.team == FLOAT_MAX)
585 turret.team = assault_attacker_team; // this gets reversed when match starts (assault_roundstart_use)
588 MUTATOR_HOOKFUNCTION(as, VehicleInit)
590 entity veh = M_ARGV(0, entity);
592 veh.nextthink = time + 0.5;
595 MUTATOR_HOOKFUNCTION(as, HavocBot_ChooseRole)
597 entity bot = M_ARGV(0, entity);
599 havocbot_ast_reset_role(bot);
603 MUTATOR_HOOKFUNCTION(as, PlayHitsound)
605 entity frag_victim = M_ARGV(0, entity);
607 return (frag_victim.classname == "func_assault_destructible");
610 MUTATOR_HOOKFUNCTION(as, TeamBalance_CheckAllowedTeams)
612 // assault always has 2 teams
613 M_ARGV(0, float) = BIT(0) | BIT(1);
617 MUTATOR_HOOKFUNCTION(as, CheckRules_World)
619 M_ARGV(0, float) = WinningCondition_Assault();
623 MUTATOR_HOOKFUNCTION(as, ReadLevelCvars)
627 sv_ready_restart_after_countdown = 0;
630 MUTATOR_HOOKFUNCTION(as, OnEntityPreSpawn)
632 entity ent = M_ARGV(0, entity);
634 switch(ent.classname)
636 case "info_player_team1":
637 case "info_player_team2":
638 case "info_player_team3":
639 case "info_player_team4":
644 MUTATOR_HOOKFUNCTION(as, ReadyRestart_Deny)
646 // readyrestart not supported (yet)