Sump/Flashbacks/FarmFlashback.cs

One flashback scenario for the Sump game: a farm stealth level built from Arena primitives (cornfields, crash pod, farmhouse, barn with storm cellar, pickup, dogs) with a farmer boss and farmhand watchers. It runs a sight/suspicion meter, a hunt state with shotgun knockdowns, corn cover and dog barking, and ends when the player collects three pod pieces and reaches the barn cellar. The Adopted contact variant rebuilds it as a dusk moving day with a truck timer and a bedroom window goal.

🐞 In StartHunt the farmer says "And it's got my {object}!" while the placeholder used elsewhere in this file is {thing}, so the keepsake name is not substituted and the line prints the literal placeholder.
using System;
using System.Collections.Generic;

/// <summary>
/// FARM HORROR: a stealth game. Night, a crashed pod smoking in a cornfield, a farmhouse, a
/// barn, a pickup. The farmer hunts with a flashlight and a shotgun; his hands and dogs spread
/// out when he shouts. Stay out of the beam and out of sight (the corn hides you unless you run
/// in it), find the three scattered pieces of your pod, and get down the barn's storm cellar.
/// Seen: a warning shout, then shots. A hit knocks you down and the hands come running to grab.
///
/// For a misfit that was adopted by a child and forgotten, the same farm is the child's home by
/// day turning to dusk: the family is packing the house into a moving truck, the three things
/// to find are what the child gave you, and the goal is her bedroom window before the truck goes.
/// Nobody shoots; seen, they chase you off.
/// </summary>
public sealed class FarmFlashback : Flashback
{
	// ---- Tuning ---------------------------------------------------------------------------------
	const float HalfX = 2200f, HalfY = 1700f;
	/// <summary>How much of a watcher's reach is left when you are in the corn and not running.</summary>
	const float CornCover = 0.14f;
	/// <summary>Faster than this in the corn and the stalks give you away.</summary>
	const float CornRustleSpeed = 235f;
	/// <summary>Seconds the farmer must see you to go from calm to shooting.</summary>
	const float BossNoticeSeconds = 1.5f;
	const float HandNoticeSeconds = 2.4f;
	const float CalmSeconds = 8f;
	const float WarnAt = 0.45f;
	const float ShotRange = 1500f, ShotEvery = 2.3f, KnockdownSeconds = 1.8f;
	const float HuntGiveUpSeconds = 10f, HandsChaseSeconds = 16f, FamilyChaseSeconds = 12f;
	const float SniffRange = 230f, DogSpeed = 150f, DogBarkSeconds = 5f, DogRestSeconds = 11f;
	const float TakeRadius = 55f, GoalRadius = 90f;
	/// <summary>Seconds until the moving truck leaves (the adopted one's memory only).</summary>
	const float TruckLeavesAfter = 270f;

	bool Adopted => Profile.Contact == FirstContact.AdoptedThenForgotten;
	public override string GenreTitle => Adopted ? "FARM HORROR: MOVING DAY" : "FARM HORROR";

	// ---- The place ------------------------------------------------------------------------------
	readonly struct Plot
	{
		public readonly float X0, X1, Y0, Y1;
		public Plot( float x0, float x1, float y0, float y1 ) { X0 = x0; X1 = x1; Y0 = y0; Y1 = y1; }
		public bool Has( Vector3 p ) => p.x >= X0 && p.x <= X1 && p.y >= Y0 && p.y <= Y1;
	}

	static readonly Plot[] Fields =
	{
		new( -1950f, -900f, -750f, 750f ),    // where the pod came down
		new( -650f, 350f, -1500f, -780f ),
		new( -650f, 350f, 720f, 1450f ),
	};

	static readonly Vector3 CrashSite = new( -1420f, 0f, 0f );
	static readonly Vector3 Porch = new( 1050f, 760f, 0f );
	static readonly Vector3 PorchLight = new( 1050f, 700f, 0f );
	static readonly Vector3 Cellar = new( 1135f, -950f, 0f );
	static readonly Vector3 Window = new( 735f, 1040f, 0f );
	static readonly Vector3 TruckBack = new( 870f, 560f, 0f );
	static readonly Vector3 HayBale = new( 880f, -620f, 0f );

	// Where a piece (or the clung object) may lie.
	static readonly Vector3[] Spots =
	{
		new( -150f, 1080f, 0f ),   // deep in the north field
		new( 260f, 400f, 0f ),     // by the pickup
		new( 770f, 640f, 0f ),     // under the porch light
		new( 560f, -330f, 0f ),    // by the hay in the yard
		new( -150f, -1120f, 0f ),  // deep in the south field
		new( -1120f, 500f, 0f ),   // under the scarecrow
		new( 1430f, 200f, 0f ),    // by the well, east
	};

	static readonly Vector3[] YardPoints =
	{
		new( -620f, 0f, 0f ), new( -320f, 520f, 0f ), new( -320f, -520f, 0f ), new( 300f, -40f, 0f ),
		new( 700f, -420f, 0f ), new( 700f, 380f, 0f ), new( 1150f, -480f, 0f ), new( 80f, -680f, 0f ), new( 80f, 640f, 0f ),
	};

	// ---- State ----------------------------------------------------------------------------------
	sealed class Piece
	{
		public GameObject Object;
		public Vector3 At;
		public bool Taken;
	}

	sealed class Dog
	{
		public GameObject Object;
		public Vector3 Home, Target;
		public float BarkUntil, RestUntil, NextBark, NextRoam;
		public bool Barking;
		public NavAgent Nav;   // its way round the barn and the house
	}

	readonly List<Piece> _pieces = new();
	readonly List<Watcher> _hands = new();
	readonly List<Dog> _dogs = new();
	readonly List<GameObject> _smoke = new();
	Watcher _boss;
	GameObject _keepsake, _truck;
	Vector3 _keepsakeAt;
	PointLight _crashGlow, _muzzle;
	SpotLight _torch;

	float _suspicion, _unseen = 100f, _still, _nextShot, _downUntil, _handsChaseUntil, _muzzleUntil, _nextSpread, _revealedUntil;
	bool _warned, _hunting, _escaped, _keepsakeTaken, _snored, _magnetLine, _allFoundSaid;
	Vector3 _lastSeen;
	int _taken;

	string BossName => Adopted ? "DAD" : "FARMER";
	string HandName => Adopted ? "MOVER" : "FARMHAND";

	// ---- Building -------------------------------------------------------------------------------

	public override void Build()
	{
		var a = Arena;
		int year = Adopted ? Rng.Next( 1979, 1996 ) : Rng.Next( 1951, 1989 );
		string[] counties = { "Harlan County, Kansas", "outside Sioux Rapids, Iowa", "Gage County, Nebraska", "Route 9, Indiana", "Tull's Crossing, Ohio", "the Dunmore place, Missouri" };
		Place = Adopted ? $"{Pick( counties )}. Moving day, {year}" : $"{Pick( counties )}. A night in {year}";

		var earth = Adopted ? new Color( 0.36f, 0.3f, 0.2f ) : new Color( 0.1f, 0.09f, 0.08f );
		a.Ground( HalfX, HalfY, earth );
		a.Slab( "Far Fields", -3200f, 3200f, -3200f, 3200f, -70f, -0.5f, earth, false, null, false );   // out to the sky
		a.Surround( HalfX - 50f, HalfY - 50f, 700f, Adopted ? new Color( 0.5f, 0.68f, 0.9f ) : new Color( 0.015f, 0.02f, 0.045f ) );
		if ( Adopted ) a.Air( new Color( 0.5f, 0.5f, 0.46f ), new Color( 0.62f, 0.68f, 0.74f ), 1800f, 9000f, new Color( 1f, 0.95f, 0.85f ), new Angles( 50f, 30f, 0f ) );
		else a.Air( new Color( 0.045f, 0.055f, 0.1f ), new Color( 0.008f, 0.01f, 0.02f ), 350f, 4200f, new Color( 0.16f, 0.2f, 0.32f ), new Angles( 48f, 215f, 0f ) );

		BuildCorn();
		BuildCrash();
		BuildHouse();
		BuildBarn();
		BuildYard();
		BuildPeople();
		BuildPieces();

		Start = a.P( CrashSite.x + 70f, CrashSite.y + 50f, 2f );
		StartYaw = a.Yaw( 0f );

		var rules = Rules;
		rules.BoundsRadius = 3600f;
		rules.PinLoseSeconds = 5f;
		rules.PinLoseLine = Adopted ? "They put you in a box and drove you to the edge of town." : "They rolled you in a tarp. It was not a clean tarp.";
		rules.Won = () => _escaped;
		if ( Adopted )
		{
			rules.TimeLimit = TruckLeavesAfter;
			rules.TimeLabel = "THE TRUCK LEAVES IN";
			rules.TimeUpLine = "The truck pulled out. She did not look back.";
		}
	}

	/// <summary>Rows of tall stalks. They are not solid and do not block the camera: what hides you is being among them.</summary>
	void BuildCorn()
	{
		var corn = Arena.Group( "Corn" );
		var green = Adopted ? new Color( 0.45f, 0.55f, 0.18f ) : new Color( 0.1f, 0.17f, 0.07f );
		foreach ( var field in Fields )
		{
			for ( float x = field.X0 + 30f; x < field.X1; x += 72f )
			{
				for ( float y = field.Y0 + 25f; y < field.Y1; y += 58f )
				{
					var at = new Vector3( x + R( -9f, 9f ), y + R( -12f, 12f ), 0f );
					if ( (at - CrashSite).Length < 190f ) continue;   // the pod flattened these
					float height = R( 84f, 112f );
					float shade = R( 0.8f, 1.15f );
					Arena.Block( "Stalk", at + Vector3.Up * (height * 0.5f), new Vector3( R( 7f, 11f ), R( 7f, 11f ), height ),
						new Color( green.r * shade, green.g * shade, green.b * shade ), false, corn, false );
				}
			}
		}

		// A scarecrow, to be mistaken for somebody.
		var post = new Vector3( -1120f, 430f, 0f );
		Arena.Block( "Scarecrow Post", post + Vector3.Up * 40f, new Vector3( 5f, 5f, 80f ), new Color( 0.3f, 0.22f, 0.14f ), false, corn, false );
		Arena.Block( "Scarecrow Arms", post + Vector3.Up * 60f, new Vector3( 5f, 62f, 5f ), new Color( 0.3f, 0.22f, 0.14f ), false, corn, false );
		Arena.Block( "Scarecrow Coat", post + Vector3.Up * 50f, new Vector3( 12f, 24f, 30f ), new Color( 0.35f, 0.2f, 0.16f ), false, corn, false );
		Arena.Ball( "Scarecrow Head", post + Vector3.Up * 76f, new Vector3( 9f ), new Color( 0.75f, 0.68f, 0.5f ), corn );
		Arena.Block( "Scarecrow Hat", post + Vector3.Up * 86f, new Vector3( 26f, 26f, 2f ), new Color( 0.55f, 0.45f, 0.2f ), false, corn, false );
	}

	/// <summary>Where it starts: the pod, smoking in a flattened circle of corn. (For the adopted one, the hen house she kept you in.)</summary>
	void BuildCrash()
	{
		var a = Arena;
		var site = a.Group( "Crash Site" );
		if ( Adopted )
		{
			a.Slab( "Hen House", CrashSite.x - 60f, CrashSite.x + 20f, -45f, 45f, 0f, 64f, new Color( 0.62f, 0.3f, 0.22f ), true, site );
			a.Slab( "Hen House Roof", CrashSite.x - 70f, CrashSite.x + 30f, -55f, 55f, 64f, 72f, new Color( 0.3f, 0.3f, 0.32f ), false, site );
			a.Block( "Shoebox Bed", CrashSite + new Vector3( 50f, 40f, 6f ), new Vector3( 26f, 16f, 10f ), new Color( 0.8f, 0.7f, 0.5f ), false, site, false );
			a.Block( "Saucer", CrashSite + new Vector3( 60f, 10f, 1.5f ), new Vector3( 12f, 12f, 2f ), Color.White, false, site, false );
			return;
		}

		var hull = Color.Lerp( Profile.Primary, new Color( 0.2f, 0.2f, 0.22f ), 0.6f );
		a.Ball( "Pod", CrashSite + Vector3.Up * 12f, new Vector3( 78f, 62f, 46f ), hull, site, true );
		a.Ball( "Pod Hatch", CrashSite + new Vector3( 40f, 22f, 40f ), new Vector3( 22f ), Color.Lerp( Profile.Secondary, Color.White, 0.3f ), site );
		for ( int i = 0; i < 7; i++ )
		{
			float angle = R( 0f, MathF.PI * 2f ), far = R( 90f, 170f );
			a.Block( "Pod Shard", CrashSite + new Vector3( MathF.Cos( angle ) * far, MathF.Sin( angle ) * far, R( 2f, 7f ) ),
				new Vector3( R( 10f, 30f ), R( 6f, 16f ), R( 3f, 12f ) ), hull, false, site, false );
		}
		a.Slab( "Scorch", CrashSite.x - 190f, CrashSite.x + 120f, -110f, 110f, 0f, 1f, new Color( 0.03f, 0.03f, 0.03f ), false, site, false );
		_crashGlow = a.Lamp( "Pod Fire", CrashSite + new Vector3( -20f, 0f, 50f ), new Color( 1f, 0.45f, 0.15f ), 520f, 0.9f, site );

		for ( int i = 0; i < 6; i++ )
			_smoke.Add( a.Ball( "Smoke", CrashSite + Vector3.Up * 60f, new Vector3( 30f ), new Color( 0.09f, 0.09f, 0.1f ), site ) );
	}

	void BuildHouse()
	{
		var a = Arena;
		var house = a.Group( "Farmhouse" );
		var wall = Adopted ? new Color( 0.92f, 0.9f, 0.82f ) : new Color( 0.4f, 0.4f, 0.42f );
		var roof = new Color( 0.2f, 0.16f, 0.15f );
		a.Slab( "House", 790f, 1310f, 830f, 1270f, 0f, 250f, wall, true, house );
		a.Slab( "House Roof", 760f, 1340f, 800f, 1300f, 250f, 276f, roof, true, house );
		a.Slab( "House Roof Top", 860f, 1240f, 900f, 1200f, 276f, 330f, roof, false, house );
		a.Slab( "Chimney", 1200f, 1240f, 1100f, 1140f, 276f, 390f, new Color( 0.45f, 0.25f, 0.2f ), false, house );
		a.Slab( "Porch", 850f, 1250f, 740f, 830f, 0f, 2f, new Color( 0.4f, 0.3f, 0.2f ), false, house, false );
		a.Slab( "Front Door", 1020f, 1080f, 826f, 830f, 0f, 96f, new Color( 0.3f, 0.18f, 0.12f ), false, house, false );

		var lit = new Color( 1f, 0.85f, 0.45f );
		a.Slab( "Window", 880f, 950f, 826f, 830f, 100f, 160f, lit, false, house, false );
		a.Slab( "Window", 1150f, 1220f, 826f, 830f, 100f, 160f, Adopted ? lit : new Color( 0.1f, 0.1f, 0.14f ), false, house, false );
		a.Lamp( "Porch Light", new Vector3( PorchLight.x, 790f, 150f ), lit, 560f, Adopted ? 0.4f : 1.3f, house );

		// Her bedroom window, round the side: low, and (in her memory) still lit.
		a.Slab( "Bedroom Window", 786f, 790f, 1000f, 1080f, 36f, 120f, Adopted ? new Color( 1f, 0.8f, 0.85f ) : new Color( 0.1f, 0.1f, 0.14f ), false, house, false );
		if ( Adopted ) a.Lamp( "Bedroom Glow", new Vector3( Window.x, Window.y, 80f ), new Color( 1f, 0.75f, 0.8f ), 300f, 1f, house );
	}

	void BuildBarn()
	{
		var a = Arena;
		var barn = a.Group( "Barn" );
		var red = Adopted ? new Color( 0.62f, 0.14f, 0.1f ) : new Color( 0.2f, 0.06f, 0.05f );
		a.Slab( "Barn", 1200f, 1800f, -1190f, -710f, 0f, 380f, red, true, barn );
		a.Slab( "Barn Roof", 1170f, 1830f, -1220f, -680f, 380f, 410f, new Color( 0.16f, 0.16f, 0.18f ), true, barn );
		a.Slab( "Barn Roof Top", 1300f, 1700f, -1110f, -790f, 410f, 480f, new Color( 0.16f, 0.16f, 0.18f ), false, barn );
		a.Slab( "Barn Door", 1420f, 1580f, -710f, -706f, 0f, 200f, new Color( 0.8f, 0.78f, 0.72f ), false, barn, false );

		// The storm cellar: two slanting doors in the ground against the barn's west wall.
		a.Slab( "Cellar Frame", 1070f, 1200f, -1020f, -880f, 0f, 6f, new Color( 0.25f, 0.25f, 0.25f ), false, barn, false );
		a.Slab( "Cellar Door", 1080f, 1195f, -1010f, -953f, 6f, 12f, new Color( 0.3f, 0.36f, 0.28f ), false, barn, false );
		a.Slab( "Cellar Door", 1080f, 1195f, -947f, -890f, 6f, 12f, new Color( 0.3f, 0.36f, 0.28f ), false, barn, false );
		if ( !Adopted ) a.Lamp( "Cellar Lantern", new Vector3( Cellar.x, Cellar.y, 70f ), new Color( 0.5f, 1f, 0.6f ), 260f, 0.7f, barn );
	}

	void BuildYard()
	{
		var a = Arena;
		var yard = a.Group( "Yard" );

		// The pickup.
		var paint = new Color( 0.2f, 0.4f, 0.5f );
		a.Slab( "Pickup Bed", 60f, 250f, 262f, 338f, 16f, 46f, paint, true, yard );
		a.Slab( "Pickup Cab", 140f, 205f, 264f, 336f, 46f, 80f, paint, true, yard );
		foreach ( var wheel in new[] { new Vector3( 90f, 262f, 16f ), new Vector3( 90f, 338f, 16f ), new Vector3( 220f, 262f, 16f ), new Vector3( 220f, 338f, 16f ) } )
			a.Ball( "Wheel", wheel, new Vector3( 16f, 7f, 16f ), new Color( 0.05f, 0.05f, 0.05f ), yard );

		// A well to the east.
		a.Slab( "Well", 1400f, 1460f, 240f, 300f, 0f, 34f, new Color( 0.4f, 0.4f, 0.42f ), true, yard );
		a.Slab( "Well Roof", 1390f, 1470f, 230f, 310f, 84f, 92f, new Color( 0.3f, 0.2f, 0.14f ), false, yard );

		// Hay to hide behind, scattered differently for every misfit.
		var hay = new Color( 0.72f, 0.6f, 0.28f ) * (Adopted ? 1f : 0.45f);
		int bales = Rng.Next( 6, 10 );
		for ( int i = 0; i < bales; i++ )
		{
			var at = new Vector3( R( -750f, 1300f ), R( -620f, 620f ), 0f );
			if ( TooNearSomething( at ) ) continue;
			bool lengthways = Rng.Next( 2 ) == 0;
			a.Block( "Hay Bale", at + Vector3.Up * 24f, lengthways ? new Vector3( 78f, 46f, 48f ) : new Vector3( 46f, 78f, 48f ), hay.WithAlpha( 1f ), true, yard );
		}

		// The bale the farmhand is asleep on.
		a.Block( "Hay Bale (occupied)", HayBale + Vector3.Up * 22f, new Vector3( 120f, 96f, 44f ), hay.WithAlpha( 1f ), true, yard );

		if ( Adopted )
		{
			// The moving truck, backed up to the porch, and the house coming out of the door in boxes.
			_truck = a.Group( "Moving Truck", yard );
			a.Slab( "Truck Box", 900f, 1200f, 470f, 630f, 22f, 200f, new Color( 0.92f, 0.92f, 0.9f ), true, _truck );
			a.Slab( "Truck Cab", 1200f, 1290f, 480f, 620f, 22f, 150f, new Color( 0.85f, 0.3f, 0.2f ), true, _truck );
			a.Slab( "Truck Ramp", 840f, 900f, 500f, 600f, 0f, 6f, new Color( 0.5f, 0.5f, 0.52f ), false, _truck, false );
			for ( int i = 0; i < 9; i++ )
			{
				var at = new Vector3( R( 620f, 860f ), R( 440f, 760f ), 0f );
				float size = R( 22f, 40f );
				a.Block( "Packing Box", at + Vector3.Up * (size * 0.5f), new Vector3( size, size * R( 0.8f, 1.3f ), size ), new Color( 0.68f, 0.52f, 0.32f ), true, yard );
			}
		}
	}

	bool TooNearSomething( Vector3 at )
	{
		if ( (at - Porch).Length < 320f || (at - Cellar).Length < 260f || (at - HayBale).Length < 200f ) return true;
		if ( at.x > 0f && at.x < 310f && at.y > 200f && at.y < 400f ) return true;       // the pickup
		if ( Adopted && at.x > 560f && at.x < 1340f && at.y > 380f ) return true;        // the truck and its boxes
		foreach ( var spot in Spots )
			if ( (at - spot).Length < 110f ) return true;
		return false;
	}

	void BuildPeople()
	{
		var a = Arena;

		// The one with the light (or, by day, the one who "is not chasing it, he is just shooing it").
		var bossLook = Adopted
			? new HumanLook { Shirt = new Color( 0.3f, 0.5f, 0.35f ), Trousers = new Color( 0.5f, 0.45f, 0.35f ), WalkSpeed = 72f, RunSpeed = 150f }
			: new HumanLook { Shirt = new Color( 0.6f, 0.15f, 0.12f ), Trousers = new Color( 0.2f, 0.28f, 0.45f ), Hat = new Color( 0.35f, 0.25f, 0.15f ), WideHat = true, WalkSpeed = 74f, RunSpeed = 135f };
		_boss = new Watcher
		{
			Name = BossName,
			Person = a.Human( Adopted ? "Dad" : "Farmer", Porch, -90f, bossLook ),
			Post = a.P( Porch ), PostYaw = a.Yaw( -90f ),
			Route = World( Shuffled( YardPoints, 5 ) ),
			Range = Adopted ? 750f : 950f, HalfAngle = Adopted ? 40f : 22f, NearRange = 120f, Pause = 2.5f,
		};

		if ( !Adopted )
		{
			var holder = new GameObject( _boss.Person.GameObject, true, "Flashlight" );
			holder.Flags |= GameObjectFlags.NotSaved;
			holder.LocalPosition = new Vector3( 16f, 0f, 48f );
			holder.LocalRotation = Rotation.From( 9f, 0f, 0f );   // a little downward
			holder.Tags.Add( PlayerPhobia.BrightTag );
			_torch = holder.Components.Create<SpotLight>();
			_torch.LightColor = new Color( 3.2f, 2.9f, 2.3f );
			_torch.ConeOuter = 23f;
			_torch.ConeInner = 13f;
			_torch.Radius = 1150f;
			_torch.Shadows = true;
			var shaft = holder.Components.Create<LightShaft>();
			shaft.MaxLength = 800f;
			shaft.Sprites = 26;
			shaft.StartOffset = 14f;

			_muzzle = a.Lamp( "Muzzle Flash", Porch + Vector3.Up * 50f, new Color( 1f, 0.8f, 0.4f ), 900f, 6f );
			_muzzle.GameObject.Enabled = false;
		}

		// The hands (or the family and the movers).
		var handLook = Adopted
			? new HumanLook { Shirt = new Color( 0.45f, 0.47f, 0.5f ), Trousers = new Color( 0.3f, 0.32f, 0.36f ), Hat = new Color( 0.75f, 0.2f, 0.15f ), WalkSpeed = 78f, RunSpeed = 150f }
			: new HumanLook { Shirt = new Color( 0.62f, 0.52f, 0.35f ), Trousers = new Color( 0.22f, 0.26f, 0.38f ), Hat = new Color( 0.78f, 0.66f, 0.3f ), WideHat = true, WalkSpeed = 80f, RunSpeed = 150f };

		if ( Adopted )
		{
			// Two movers carrying the house to the truck, and Mum fetching what is left in the yard.
			for ( int i = 0; i < 2; i++ )
			{
				var door = Porch + new Vector3( -40f + i * 80f, 20f, 0f );
				var mover = AddHand( HandName, door, -90f, handLook, new[] { TruckBack + new Vector3( i * 40f, 0f, 0f ), door }, 3f + i );
				Arena.Part( mover.Person.GameObject, "Carried Box", false, new Vector3( 20f, 0f, 40f ), new Vector3( 22f, 26f, 20f ), new Color( 0.68f, 0.52f, 0.32f ) );
			}
			var mumLook = new HumanLook { Shirt = new Color( 0.95f, 0.8f, 0.3f ), Trousers = new Color( 0.3f, 0.34f, 0.5f ), WalkSpeed = 70f, RunSpeed = 145f };
			var mum = AddHand( "MUM", new Vector3( 700f, 300f, 0f ), 180f, mumLook, Shuffled( YardPoints, 4 ), 4f );
			mum.Range = 650f;
		}
		else
		{
			int hands = 2 + Rng.Next( 0, 3 );
			Vector3[][] rounds =
			{
				new[] { new Vector3( -800f, -600f, 0f ), new Vector3( -800f, 600f, 0f ) },                              // along the crash field's edge
				new[] { new Vector3( 450f, -680f, 0f ), new Vector3( 1050f, -620f, 0f ), new Vector3( 1000f, -200f, 0f ) }, // barn side
				new[] { new Vector3( 450f, 640f, 0f ), new Vector3( -700f, 640f, 0f ) },                                // north field's edge
				new[] { new Vector3( 1500f, 60f, 0f ), new Vector3( 600f, 60f, 0f ) },                                  // across the yard
			};
			for ( int i = 0; i < hands; i++ )
			{
				var round = rounds[i % rounds.Length];
				var route = new Vector3[round.Length];
				for ( int n = 0; n < round.Length; n++ ) route[n] = round[n] + new Vector3( R( -60f, 60f ), R( -60f, 60f ), 0f );
				AddHand( HandName, round[0], 0f, handLook, route, R( 1.5f, 4f ) );
			}
		}

		// Dogs. They cannot be hidden from: they smell you. But they only bark.
		int dogs = Adopted ? 1 : 1 + Rng.Next( 0, 2 );
		for ( int i = 0; i < dogs; i++ ) AddDog( new Vector3( R( 200f, 900f ), R( -300f, 300f ), 0f ) );

		// The Fat Guy: a hand asleep on a hay bale by the barn, through all of it.
		a.FatGuyCameo( HayBale + Vector3.Up * 44f, 180f, true, Adopted ? new Color( 0.45f, 0.47f, 0.5f ) : null );
	}

	Watcher AddHand( string name, Vector3 at, float yaw, HumanLook look, Vector3[] route, float pause )
	{
		var hand = new Watcher
		{
			Name = name,
			Person = Arena.Human( name, at, yaw, look ),
			Post = Arena.P( at ), PostYaw = Arena.Yaw( yaw ),
			Route = World( route ),
			Range = 480f, HalfAngle = 40f, NearRange = 95f, Pause = pause,
		};
		_hands.Add( hand );
		return hand;
	}

	/// <summary>Arena-frame points as world points.</summary>
	Vector3[] World( Vector3[] points )
	{
		var world = new Vector3[points.Length];
		for ( int i = 0; i < points.Length; i++ ) world[i] = Arena.P( points[i] );
		return world;
	}

	/// <summary>A few of some points, in a seeded order (arena frame).</summary>
	Vector3[] Shuffled( Vector3[] from, int count )
	{
		var pool = new List<Vector3>( from );
		var route = new Vector3[Math.Min( count, pool.Count )];
		for ( int i = 0; i < route.Length; i++ )
		{
			int pick = Rng.Next( pool.Count );
			route[i] = pool[pick];
			pool.RemoveAt( pick );
		}
		return route;
	}

	void AddDog( Vector3 at )
	{
		var a = Arena;
		var go = a.Holder( "Dog", at );
		var coat = Rng.Next( 2 ) == 0 ? new Color( 0.45f, 0.3f, 0.18f ) : new Color( 0.75f, 0.7f, 0.6f );
		a.Part( go, "Body", false, new Vector3( 0f, 0f, 16f ), new Vector3( 30f, 10f, 11f ), coat );
		a.Part( go, "Head", false, new Vector3( 19f, 0f, 22f ), new Vector3( 11f, 9f, 9f ), coat );
		a.Part( go, "Nose", false, new Vector3( 27f, 0f, 20f ), new Vector3( 6f, 5f, 5f ), new Color( 0.1f, 0.08f, 0.08f ) );
		a.Part( go, "Tail", false, new Vector3( -18f, 0f, 22f ), new Vector3( 9f, 3f, 3f ), coat );
		foreach ( var leg in new[] { new Vector3( 11f, 4f, 5f ), new Vector3( 11f, -4f, 5f ), new Vector3( -11f, 4f, 5f ), new Vector3( -11f, -4f, 5f ) } )
			a.Part( go, "Leg", false, leg, new Vector3( 3.5f, 3.5f, 11f ), coat );
		_dogs.Add( new Dog { Object = go, Home = a.P( at ), Target = a.P( at ) } );
	}

	void BuildPieces()
	{
		// Three of the spots get a piece, one more gets the thing the misfit will never put down.
		var pool = new List<Vector3>( Spots );
		var color = Adopted ? new Color( 1f, 0.6f, 0.75f ) : Color.Lerp( Profile.Secondary, new Color( 0.4f, 0.9f, 1f ), 0.5f );
		string[] gifts = { "Her Drawing Of You", "The Saucer", "The Collar She Made" };
		for ( int i = 0; i < 3; i++ )
		{
			int pick = Rng.Next( pool.Count );
			var at = pool[pick] + new Vector3( R( -40f, 40f ), R( -40f, 40f ), 0f );
			pool.RemoveAt( pick );
			_pieces.Add( new Piece { At = at, Object = Arena.Pickup( Adopted ? gifts[i] : "Pod Piece", at, color ) } );
		}

		_keepsakeAt = pool[Rng.Next( pool.Count )] + new Vector3( R( -30f, 30f ), R( -30f, 30f ), 0f );
		_keepsake = Arena.Keepsake( _keepsakeAt );
	}

	// ---- Playing --------------------------------------------------------------------------------

	public override void Begin()
	{
		Arena.Crowd.Grace( 2f );
		if ( Adopted )
		{
			Say( "MUM", "Katie, in the truck, sweetheart. No, it is not coming. We talked about this." );
			Say( "DAD", "She has not asked about it in a month. It will go back to the woods. The goldfish did." );
		}
		else
		{
			Say( "FARMER", "Something came down in the east forty. Took out half the corn and the good outhouse. Earl, get the dogs." );
			Say( "FARMHAND", "Think it's one of them weather balloons? The last one owed me money." );
		}
	}

	protected override void Tick( float dt )
	{
		var a = Arena;
		var player = a.Player;
		float now = Time.Now;
		var local = a.PlayerLocal;
		float speed = a.PlayerSpeed;

		if ( Adopted ) TurnToDusk();
		else Smoulder( now );

		// How easy you are to see, right now.
		_still = speed < 14f ? _still + dt : 0f;
		bool inCorn = InCorn( local );
		bool hidden = inCorn && speed < CornRustleSpeed && now > _revealedUntil;
		float visibility = a.Body.Visibility;
		if ( _still > 0.7f ) visibility *= a.Body.StillVisibility;
		if ( hidden ) visibility *= CornCover;
		if ( !Adopted && (local - PorchLight).WithZ( 0f ).Length < 300f ) visibility *= 1.6f;
		a.Visibility = visibility;

		// Up again after a hit.
		if ( _downUntil > 0f && now >= _downUntil ) { _downUntil = 0f; player.Unpin(); }

		// Everybody looks.
		_boss.Update( a, dt );
		bool bossSees = _boss.Sees;
		bool handSees = false;
		foreach ( var hand in _hands )
		{
			hand.Update( a, dt );
			handSees |= hand.Sees;
		}
		bool barking = UpdateDogs( dt, now, player.WorldPosition, hidden );
		bool seen = bossSees || handSees;
		if ( seen || barking ) { _lastSeen = player.WorldPosition; _unseen = 0f; }
		else _unseen += dt;

		Suspect( dt, now, bossSees, handSees, barking );
		if ( _hunting ) Hunt( dt, now, seen );

		// Chasers give up once the hunt is over and nobody has hold of you.
		if ( !_hunting && now > _handsChaseUntil && !a.Crowd.Held )
		{
			foreach ( var hand in _hands ) if ( hand.Chasing ) hand.StandDown();
			if ( _boss.Chasing ) _boss.StandDown();
		}

		if ( _muzzle.IsValid() && _muzzle.GameObject.Enabled && now > _muzzleUntil ) _muzzle.GameObject.Enabled = false;

		Collect( local );
		Cameo( local );
		Goal( local );
		Rules.Meter = _hunting ? 1f : _suspicion;
		Rules.MeterLabel = _hunting ? (Adopted ? "THEY SEE YOU" : "HUNTED") : "SEEN";
	}

	bool InCorn( Vector3 local )
	{
		if ( (local - CrashSite).WithZ( 0f ).Length < 170f ) return false;   // flattened
		foreach ( var field in Fields )
			if ( field.Has( local ) ) return true;
		return false;
	}

	/// <summary>The meter between calm and hunted: fills while they can see you, drains while they cannot.</summary>
	void Suspect( float dt, float now, bool bossSees, bool handSees, bool barking )
	{
		if ( _hunting ) { _suspicion = 1f; return; }

		float rise = 0f;
		if ( bossSees ) rise = 1f / BossNoticeSeconds;
		else if ( handSees ) rise = 1f / HandNoticeSeconds;
		if ( barking ) rise += 0.2f;
		_suspicion = rise > 0f ? MathF.Min( _suspicion + rise * dt, 1f ) : MathF.Max( _suspicion - dt / CalmSeconds, 0f );

		if ( _suspicion < 0.15f ) _warned = false;
		if ( _suspicion >= WarnAt && !_warned )
		{
			// The warning shout: everyone spreads out toward where you were.
			_warned = true;
			Sump.Sfx( Adopted ? "farm_call" : "farm_shout", _boss.Position );
			if ( Adopted ) Say( Rng.Next( 2 ) == 0 ? "MUM" : "DAD", Pick( new[] { "Did you see something by the corn?", "Is that... Dale, is that it? Do not let Katie see.", "Shoo! Go on! Get! Go and be nature!" } ) );
			else Say( "FARMER", Pick( new[] { "Who's out there? I got a gun and a bad knee!", "Somethin's movin' in my corn. Spread out!", "Duke! Whatcha got, boy? Earl, go round the far side!" } ) );
			SpreadOut( now );
		}

		if ( _suspicion >= 1f ) StartHunt( now );
	}

	void SpreadOut( float now )
	{
		_nextSpread = now + 5f;
		_boss.Investigate( _lastSeen );
		foreach ( var hand in _hands )
			if ( !hand.Chasing ) hand.Investigate( _lastSeen + new Vector3( Game.Random.Float( -320f, 320f ), Game.Random.Float( -320f, 320f ), 0f ) );
		foreach ( var dog in _dogs ) dog.Target = _lastSeen + new Vector3( Game.Random.Float( -200f, 200f ), Game.Random.Float( -200f, 200f ), 0f );
	}

	void StartHunt( float now )
	{
		_hunting = true;
		_unseen = 0f;
		_nextShot = now + 1.1f;
		Sump.Sfx( Adopted ? "farm_scream" : "farm_alarm", _boss.Position );

		if ( Adopted )
		{
			Say( "MUM", Pick( new[] { "It's HERE. Dale! It followed her home again!", "Get it away from the truck! She will see it!" } ) );
			Say( "DAD", "Grab it. Box it. We drop it at the county line, same as the trampoline, and that is the END of it." );
			_boss.Chase();
			foreach ( var hand in _hands ) hand.Chase();
			_handsChaseUntil = now + FamilyChaseSeconds;
		}
		else
		{
			Say( "FARMER", Pick( new[] { "THERE! By God, what IS that? It's got no trousers on!", "It ain't a coyote. Earl, it AIN'T a coyote!", "It's the thing off the radio! Don't let it reach the barn!" } ) );
			if ( _keepsakeTaken ) Say( "FARMER", "And it's got my {object}!" );
		}
	}

	/// <summary>Hunted: the farmer stops and fires while he has a line on you; everyone else closes on where you were last seen.</summary>
	void Hunt( float dt, float now, bool seen )
	{
		var a = Arena;
		var player = a.Player;

		if ( _unseen > HuntGiveUpSeconds && !a.Crowd.Held )
		{
			_hunting = false;
			_suspicion = 0.3f;
			_boss.Aim = null;
			if ( Adopted ) Say( "DAD", "Gone. Good. Finish the boxes, we are leaving at six." );
			else Say( "FARMER", Pick( new[] { "Lost it. It's in the corn somewhere. Keep them lights on.", "Gone to ground. It'll come out. Things always come out. I learned that off a prune." } ) );
			return;
		}

		if ( Adopted )
		{
			// The family keeps after you for as long as they keep seeing you.
			if ( seen ) _handsChaseUntil = now + FamilyChaseSeconds;
			return;
		}

		if ( now >= _nextSpread ) SpreadOut( now );

		var target = player.WorldPosition + Vector3.Up * 12f;
		bool line = _boss.Alive && _boss.Person.State == CrowdState.Standing
			&& (target - _boss.Eye).Length < ShotRange && a.ClearLine( _boss.Eye, target );
		_boss.Aim = line ? player.WorldPosition : null;
		if ( !line ) { _nextShot = MathF.Max( _nextShot, now + 0.8f ); return; }

		if ( now < _nextShot || _downUntil > 0f ) return;
		_nextShot = now + ShotEvery * Game.Random.Float( 0.85f, 1.25f );
		Shoot( now, target );
	}

	void Shoot( float now, Vector3 target )
	{
		var a = Arena;
		var player = a.Player;
		var muzzle = _boss.Eye + _boss.Forward * 34f;
		float distance = (target - muzzle).Length;

		Sump.Sfx( "farm_shotgun", muzzle );
		if ( _muzzle.IsValid() )
		{
			_muzzle.GameObject.WorldPosition = muzzle;
			_muzzle.GameObject.Enabled = true;
			_muzzleUntil = now + 0.09f;
		}
		player.Kick( Vector3.Random * 12f + Vector3.Down * 6f );

		// Harder to hit far off, in the corn, or running flat out.
		float chance = 0.62f - 0.45f * Math.Clamp( (distance - 300f) / 1100f, 0f, 1f );
		if ( InCorn( a.PlayerLocal ) ) chance *= 0.45f;
		if ( a.PlayerSpeed > 250f ) chance *= 0.7f;
		if ( Profile.Ability == TwistedAbility.BulletMagnet ) chance = MathF.Min( chance * 1.25f, 0.85f );   // it starts here

		if ( Game.Random.Float( 0f, 1f ) > chance )
		{
			Sump.Sfx( "farm_shot_miss", target );
			if ( Game.Random.Float( 0f, 1f ) < 0.4f ) Say( "FARMER", Pick( new[] { "Hold still, damn you!", "Missed. It's quick. Or I'm drunk. Could be both.", "Light it up! I can't see it!" } ), 3f );
			return;
		}

		// Hit: down in the dirt, and the hands come running to get hold of you.
		player.Pin( player.WorldPosition );
		player.Kick( Vector3.Down * 34f );
		_downUntil = now + KnockdownSeconds;
		_handsChaseUntil = now + HandsChaseSeconds;
		Sump.Sfx( "farm_hit", player.WorldPosition );
		Say( "FARMER", Pick( new[] { "Got it! Get the tarp, boys, GET IT!", "It's down! Don't touch it bare-handed! You don't know where it's been! Space, mostly!", "Winged it! Go, go, before it gets up!" } ) );
		foreach ( var hand in _hands ) hand.Chase();

		if ( Profile.Ability == TwistedAbility.BulletMagnet && !_magnetLine )
		{
			_magnetLine = true;
			Say( Who, "It curved. I felt it curve to find me. Everything has, ever since.", 6f );
		}
	}

	/// <summary>Dogs trot about the yard. One that smells you comes over and barks, which gives you away even in the corn.</summary>
	bool UpdateDogs( float dt, float now, Vector3 player, bool hidden )
	{
		bool any = false;
		// A mushroom that is not moving does not smell like anything worth barking at.
		float nose = SniffRange * (_still > 0.7f && Arena.Body.StillVisibility < 0.2f ? 0.25f : 1f);

		foreach ( var dog in _dogs )
		{
			if ( !dog.Object.IsValid() ) continue;
			var at = dog.Object.WorldPosition;
			float toPlayer = (player - at).WithZ( 0f ).Length;

			if ( !dog.Barking && now > dog.RestUntil && toPlayer < nose )
			{
				dog.Barking = true;
				dog.BarkUntil = now + DogBarkSeconds;
			}

			if ( dog.Barking )
			{
				any = true;
				_revealedUntil = now + 2.5f;
				dog.Target = toPlayer > 80f ? player : at;
				if ( now >= dog.NextBark )
				{
					dog.NextBark = now + Game.Random.Float( 0.55f, 0.95f );
					Sump.Sfx( "farm_dog_bark", at );
				}
				if ( now > dog.BarkUntil || toPlayer > nose * 2.5f )
				{
					// Bored (or outrun): off it goes for a while.
					dog.Barking = false;
					dog.RestUntil = now + DogRestSeconds;
					dog.Target = dog.Home;
				}
			}
			else if ( now >= dog.NextRoam )
			{
				dog.NextRoam = now + Game.Random.Float( 4f, 9f );
				dog.Target = Arena.P( Game.Random.Float( -700f, 1400f ), Game.Random.Float( -650f, 650f ) );
			}

			var goal = (dog.Target - at).WithZ( 0f );
			if ( goal.Length < 12f ) continue;
			dog.Nav ??= Arena.Nav?.Agent();
			var to = ((dog.Nav?.Steer( at.WithZ( Arena.FloorZ ), dog.Target ) ?? dog.Target) - at).WithZ( 0f );
			if ( to.Length < 1f ) continue;   // no way any nearer: it waits
			var step = to.Normal * MathF.Min( DogSpeed * (dog.Barking ? 1.3f : 0.7f) * dt, goal.Length );
			dog.Object.WorldPosition = (at + step).WithZ( Arena.FloorZ + MathF.Abs( MathF.Sin( now * 13f ) ) * 2f );
			dog.Object.WorldRotation = Rotation.Slerp( dog.Object.WorldRotation, Rotation.LookAt( to.Normal, Vector3.Up ), Math.Clamp( dt * 8f, 0f, 1f ) );
		}
		return any;
	}

	void Collect( Vector3 local )
	{
		var a = Arena;
		foreach ( var piece in _pieces )
		{
			if ( piece.Taken || (local - piece.At).WithZ( 0f ).Length > TakeRadius ) continue;
			piece.Taken = true;
			_taken++;
			if ( piece.Object.IsValid() ) piece.Object.Destroy();
			Sump.Sfx( "farm_pickup", a.Player.WorldPosition );
			Rules.Reach( "piece" + _taken, a.Player.WorldPosition, a.Player.Look.yaw, _taken );

			if ( Adopted ) Say( Who, _taken switch { 1 => "She drew this. That is me. I am the one with the sun over it.", 2 => "She filled this every morning. Then some mornings. Then not.", _ => "She made it too small. I wore it anyway." }, 5.5f );
			else Say( Who, _taken switch { 1 => "A piece of the hull. Still warm.", 2 => "The beacon. Cracked. It was calling home. Nothing answered.", _ => "The last of it. There is no ship now. Only somewhere to hide." }, 5.5f );
		}

		if ( !_keepsakeTaken && (local - _keepsakeAt).WithZ( 0f ).Length < TakeRadius )
		{
			_keepsakeTaken = true;
			if ( _keepsake.IsValid() ) _keepsake.Destroy();
			_suspicion = MathF.Min( _suspicion, 0.2f );
			Sump.Sfx( "flashback_keepsake", a.Player.WorldPosition );
			Say( Who, Adopted
				? "{thing}. It was hers. She left it in the yard and did not come back for it. Neither of us was packed."
				: "{thing}. It was just lying there in the dirt, and it was not afraid of me. I have not put it down since.", 7f );
		}

		if ( _taken >= _pieces.Count && !_allFoundSaid )
		{
			_allFoundSaid = true;
			Say( Who, Adopted ? "Her window. The one she used to open for me." : "The barn. There is a door in the ground. Under the ground they do not look." );
		}

		Rules.Objective = _taken < _pieces.Count
			? (Adopted ? $"Find what she gave you ({_taken}/{_pieces.Count}). Keep out of the family's sight; the corn hides you if you do not run"
				: $"Find the pieces of your pod ({_taken}/{_pieces.Count}). Stay out of the light; the corn hides you if you do not run")
			: (Adopted ? "Get to her bedroom window, round the side of the house" : "Get to the storm cellar by the barn");
	}

	/// <summary>The Fat Guy sleeps through everything.</summary>
	void Cameo( Vector3 local )
	{
		if ( _snored || (local - HayBale).WithZ( 0f ).Length > 260f ) return;
		_snored = true;
		Sump.Sfx( "fatguy_snore", Arena.P( HayBale ) );
		Say( HandName, "Zzzzz... (a vast man in a pink tracksuit is asleep on the hay. He will not wake up for this. The hay will never be the same.)", 6f );
	}

	void Goal( Vector3 local )
	{
		if ( _escaped || _taken < _pieces.Count ) return;
		var goal = Adopted ? Window : Cellar;
		if ( (local - goal).WithZ( 0f ).Length > GoalRadius || Arena.Crowd.Pinned ) return;
		_escaped = true;
	}

	public override void Won()
	{
		Sump.Sfx( Adopted ? "farm_truck_leaves" : "farm_cellar_door", Arena.Player.WorldPosition );
		if ( Adopted )
		{
			Say( "MUM", "Katie? Did you leave anything behind, sweetie?", 5f );
			Say( "KATIE", "...No.", 5f );
		}
		else Say( "FARMER", "It's gone under the barn. Leave it. Board it up. We never saw nothin'. And nobody tells Marlene about the outhouse.", 6f );
	}

	public override void Rewind( FlashbackCheckpoint to )
	{
		_hunting = false;
		_warned = false;
		_suspicion = 0f;
		_unseen = 100f;
		_downUntil = 0f;
		_handsChaseUntil = 0f;
		_revealedUntil = 0f;
		_boss.Reset();
		foreach ( var hand in _hands ) hand.Reset();
		foreach ( var dog in _dogs )
		{
			if ( !dog.Object.IsValid() ) continue;
			dog.Object.WorldPosition = dog.Home;
			dog.Target = dog.Home;
			dog.Barking = false;
			dog.RestUntil = Time.Now + 6f;
		}
	}

	// ---- Mood -----------------------------------------------------------------------------------

	/// <summary>The pod's fire flickers and its smoke climbs.</summary>
	void Smoulder( float now )
	{
		if ( _crashGlow.IsValid() )
			_crashGlow.LightColor = (new Color( 1f, 0.45f, 0.15f ) * (0.7f + 0.3f * MathF.Sin( now * 9f ) * MathF.Sin( now * 2.3f ))).WithAlpha( 1f );

		for ( int i = 0; i < _smoke.Count; i++ )
		{
			if ( !_smoke[i].IsValid() ) continue;
			float t = (now * 0.12f + i / (float)_smoke.Count) % 1f;
			_smoke[i].WorldPosition = Arena.P( CrashSite.x - 20f + MathF.Sin( i * 2.1f + t * 3f ) * 30f * t, CrashSite.y + t * 60f, 50f + t * 420f );
			_smoke[i].WorldScale = new Vector3( 0.8f + t * 2.6f );
		}
	}

	/// <summary>The adopted one's afternoon runs out: the light goes long and orange as the truck fills.</summary>
	void TurnToDusk()
	{
		var a = Arena;
		float t = Rules.TimeLimit > 0f ? Math.Clamp( Rules.Elapsed / Rules.TimeLimit, 0f, 1f ) : 0f;
		float dusk = t * t;
		a.TintSky( Color.Lerp( new Color( 0.5f, 0.68f, 0.9f ), new Color( 0.75f, 0.32f, 0.16f ), dusk ) );
		if ( a.Ambient.IsValid() ) a.Ambient.Color = Color.Lerp( new Color( 0.5f, 0.5f, 0.46f ), new Color( 0.22f, 0.14f, 0.16f ), dusk );
		if ( a.Sun.IsValid() )
		{
			a.Sun.LightColor = Color.Lerp( new Color( 1f, 0.95f, 0.85f ), new Color( 0.9f, 0.4f, 0.15f ), dusk );
			a.Sun.GameObject.WorldRotation = new Angles( 50f - 40f * t, 30f, 0f ).ToRotation();
		}
	}
}