Part of the rampage mode's crowd system. Spawns escalating waves of responders (police, riot squads, army, generals with a band) on streets a few hundred units from the lizard, arranges them into ranks around a moving squad centre, and marches or holds them until they break and charge.
using System;
using System.Collections.Generic;
// What the town sends as the rampage gets worse: police in a line, riot squads, the army, men in
// black, and finally a parade of generals with a marching band. They arrive as stand-ins in
// formation a few streets away and are people like anybody else from then on.
public sealed partial class RampagePopulace
{
/// <summary>A formation: a moving middle and a heading, with everybody keeping a place relative to it.</summary>
sealed class Squad
{
public Vector3 Centre, Heading;
public float Speed, Until, NextSound;
public string Sound;
/// <summary>Marches straight on instead of stopping to make a stand.</summary>
public bool Parade;
/// <summary>Drawn up in front of the lizard, waiting for it.</summary>
public bool Holding;
/// <summary>The formation is over: every man for himself.</summary>
public bool Broken;
}
readonly List<Squad> _squads = new();
int _pendingTier = -1;
float _nextWave;
/// <summary>A line makes its stand this far from the lizard, and breaks (and goes for it) when it comes this close.</summary>
const float HoldDistance = 620f, BreakDistance = 320f;
/// <summary>Where a place in a formation is just now.</summary>
static Vector3 SlotPoint( Squad squad, Vector2 slot )
{
var left = new Vector3( -squad.Heading.y, squad.Heading.x, 0f );
return squad.Centre + squad.Heading * slot.x + left * slot.y;
}
// The tier went up in the middle of somebody being hit: the new arrivals wait for the next frame.
void OnTierUp( int tier ) => _pendingTier = Math.Max( _pendingTier, tier );
void UpdateResponse( Vector3 lizard, float now, float dt )
{
if ( _pendingTier >= 0 )
{
// A new tier: everything it unlocks turns up at once.
int tier = _pendingTier;
_pendingTier = -1;
foreach ( var response in HumanRoles.Responses )
if ( response.Tier == tier || (tier > 5 && response.Tier == 5) ) Send( response, lizard );
_nextWave = now + RespondEvery;
}
else if ( Rampage.Tier >= 1 && now >= _nextWave )
{
// And between tiers, more of whatever the town has, mostly the latest.
_nextWave = now + RespondEvery * Game.Random.Float( 0.8f, 1.25f );
HumanRoles.Response pick = null;
int seen = 0;
foreach ( var response in HumanRoles.Responses )
{
if ( response.Tier > Rampage.Tier ) continue;
if ( response.Tier < Rampage.Tier - 1 && Game.Random.Float( 0f, 1f ) < 0.7f ) continue;
seen++;
if ( Game.Random.Next( seen ) == 0 ) pick = response;
}
if ( pick is not null ) Send( pick, lizard );
}
foreach ( var squad in _squads )
{
if ( squad.Broken ) continue;
if ( now >= squad.Until ) { squad.Broken = true; continue; }
var to = (lizard - squad.Centre).WithZ( 0f );
float distance = to.Length;
var toward = distance > 1f ? to / distance : squad.Heading;
if ( squad.Parade )
{
// Marches on, through whatever is happening, and comes round again for another go.
squad.Centre += squad.Heading * squad.Speed * dt;
if ( distance > 2600f && Vector3.Dot( squad.Heading, toward ) < 0f ) squad.Heading = toward;
}
else if ( !squad.Holding )
{
squad.Heading = toward;
squad.Centre += toward * squad.Speed * dt;
if ( distance < HoldDistance ) squad.Holding = true;
}
else
{
// Drawn up and waiting, like skittles. If it comes close they break ranks and go for it.
squad.Heading = Vector3.Lerp( squad.Heading, toward, MathF.Min( dt * 1.5f, 1f ) ).Normal;
if ( distance < BreakDistance ) squad.Broken = true;
else if ( distance > HoldDistance * 2.2f ) squad.Holding = false;
}
squad.Centre = PopulaceMap.Contain( squad.Centre );
if ( squad.Sound is not null && now >= squad.NextSound && distance < 2600f )
{
squad.NextSound = now + (squad.Parade ? 6f : Game.Random.Float( 4f, 7f ));
PopulaceFx.Sound( squad.Sound, squad.Centre );
}
}
}
/// <summary>One wave arrives: a few streets from the lizard, drawn up in ranks facing it (or as a gaggle).</summary>
void Send( HumanRoles.Response response, Vector3 lizard )
{
if ( !_built || Responders + response.Count > MaxResponders || _free.Count < response.Count ) return;
// A street some way off, so they are seen coming.
float angle = Game.Random.Float( 0f, MathF.PI * 2f );
var spot = PopulaceMap.Contain( lizard + new Vector3( MathF.Cos( angle ), MathF.Sin( angle ), 0f ) * ArriveDistance );
for ( int tries = 0; tries < 30; tries++ )
{
var street = PopulaceMap.RandomStreet();
float distance = FlatDistance( street, lizard );
if ( distance < ArriveDistance * 0.7f || distance > ArriveDistance * 1.5f || !PopulaceMap.CanWalk( street ) ) continue;
spot = street;
break;
}
var heading = (lizard - spot).WithZ( 0f );
heading = heading.LengthSquared > 1f ? heading.Normal : Vector3.Forward;
float yaw = MathF.Atan2( heading.y, heading.x );
Squad squad = null;
int squadIndex = -1;
if ( response.PerRank > 0 )
{
squad = new Squad
{
Centre = spot,
Heading = heading,
Speed = WalkSpeed * (response.Parade ? 0.6f : 0.95f),
Parade = response.Parade,
Sound = response.Sound,
Until = Time.Now + (response.Parade ? 120f : 70f),
NextSound = Time.Now + 1f,
};
squadIndex = _squads.Count;
_squads.Add( squad );
}
for ( int k = 0; k < response.Count && _free.Count > 0; k++ )
{
int rank = response.PerRank > 0 ? k / response.PerRank : k;
var role = response.Ranks[Math.Min( rank, response.Ranks.Length - 1 )];
var slot = Vector2.Zero;
Vector3 at;
if ( squad is not null )
{
int file = k % response.PerRank;
slot = new Vector2( -rank * 72f, (file - (response.PerRank - 1) * 0.5f) * 46f );
at = SlotPoint( squad, slot );
}
else at = spot + new Vector3( Game.Random.Float( -110f, 110f ), Game.Random.Float( -110f, 110f ), 0f );
int i = _free.Pop();
AddAgent( i, role, at, yaw );
ref var a = ref _agents[i];
a.Stray = true; // no block to go home to: off duty when left behind
a.Squad = squadIndex;
a.Slot = slot;
WakeAgent( i );
Responders++;
}
// Heard from the lizard's side of town, wherever they have actually turned up.
if ( response.Sound is not null )
PopulaceFx.Sound( response.Sound, lizard - heading * 500f );
}
}