Sump/Grafts/GraftTable.cs

Static lookup tables for the graft system. Maps each BodyPlan to a GraftPlanEntry (part name, sockets, passive stat modifiers, build function) and each TwistedAbility to a graft component, plus the per-plan mesh-building functions that assemble the part out of balls, boxes and limbs.

using System;
using System.Collections.Generic;

// The grafts as data. A body plan gives the part (how it is built, where it may sit) and a
// passive change to how the lizard moves; a twisted ability gives the component that goes on a
// key. Adding a body plan or an ability is one more entry in the tables at the bottom.

/// <summary>
/// What one graft's body plan does to the lizard just by being there. Factors multiply (1 changes
/// nothing), the rest add up. <see cref="Patchwork"/> combines every active graft's numbers and
/// applies the totals; nothing else writes them.
/// </summary>
public sealed class GraftPassive
{
	public static readonly GraftPassive None = new();

	/// <summary>Gravity multiplier: below 1 is floatier jumps and slower falls.</summary>
	public float Gravity = 1f;
	/// <summary>Jump speed multiplier.</summary>
	public float Jump = 1f;
	/// <summary>Movement speed multiplier, always.</summary>
	public float Speed = 1f;
	/// <summary>Movement speed multiplier while on a wall or ceiling.</summary>
	public float WallSpeed = 1f;
	/// <summary>Movement speed multiplier reached after running straight for a while.</summary>
	public float StraightSpeed = 1f;
	/// <summary>Added to the phobia's resistance (0 to 1): shrugging things off.</summary>
	public float Resistance;
	/// <summary>Calm given back per second while the lizard keeps still and nothing frightens it.</summary>
	public float CalmWhileStill;
	/// <summary>0 to 1: how much of a hard fall is taken off just before landing.</summary>
	public float Cushion;
	/// <summary>Multiplier on how far the feet and hands find a surface to hold: better grip.</summary>
	public float Grip = 1f;
	/// <summary>Seconds between scouting pings that show where unhealed misfits are; 0 for none.</summary>
	public float PingEvery;
}

/// <summary>One place a plan's part may sit: a bone, an offset from it in the lizard's frame, and which look the part takes there.</summary>
public sealed class GraftSpot
{
	public readonly string Bone;
	public readonly Vector3 Offset;
	public readonly int Style;

	public GraftSpot( string bone, Vector3 offset, int style = 0 )
	{
		Bone = bone;
		Offset = offset;
		Style = style;
	}
}

/// <summary>Everything a body plan gives a graft.</summary>
public sealed class GraftPlanEntry
{
	/// <summary>What the part is called ("breathing bladder").</summary>
	public string PartName = "lump";
	/// <summary>Sockets in the order they are used: the first graft of this plan takes the first, the second the next.</summary>
	public GraftSpot[] Spots = { new GraftSpot( "spine_1", new Vector3( 0f, 0f, 5f ) ) };
	/// <summary>Builds the part's bits on a new graft (read <see cref="Graft.Style"/> for which look).</summary>
	public Action<Graft> Build;
	public GraftPassive Passive = GraftPassive.None;
	/// <summary>The passive in a few words, for the HUD.</summary>
	public string PassiveLine = "";
	/// <summary>What the lizard thinks as it grows in. {0} is the misfit's name.</summary>
	public string VoiceLine = "{0} is part of me now. We have agreed not to discuss which part.";
}

/// <summary>Everything a twisted ability gives a graft.</summary>
public sealed class GraftAbilityEntry
{
	/// <summary>Adds the ability's component to the graft's object.</summary>
	public Func<GameObject, GraftAbility> Create;
}

public static class GraftTable
{
	public static GraftPlanEntry Plan( BodyPlan plan ) => Plans.TryGetValue( plan, out var entry ) ? entry : Unknown;

	public static GraftAbility CreateAbility( TwistedAbility ability, GameObject on )
	{
		if ( !Abilities.TryGetValue( ability, out var entry ) || entry.Create is null ) return null;
		try { return entry.Create( on ); }
		catch ( Exception e ) { Log.Warning( $"Graft: no ability for {ability}: {e.Message}" ); return null; }
	}

	/// <summary>A body plan nobody has written a graft for yet still grows something.</summary>
	static readonly GraftPlanEntry Unknown = new()
	{
		Build = g => g.Ball( "Lump", Graft.BitKind.Bladder, Vector3.Zero, new Vector3( 4f ), g.Primary ),
	};

	// ---- Body plan -> part, sockets, passive ------------------------------------------------------

	public static readonly Dictionary<BodyPlan, GraftPlanEntry> Plans = new()
	{
		[BodyPlan.GasSac] = new GraftPlanEntry
		{
			PartName = "breathing bladder",
			Spots = new[]
			{
				new GraftSpot( "spine_2", new Vector3( -2f, 0f, 6f ) ),
				new GraftSpot( "spine_0", new Vector3( -1f, 0f, 6f ) ),
				new GraftSpot( "spine_1", new Vector3( 0f, 6f, 4f ) ),
				new GraftSpot( "spine_1", new Vector3( 0f, -6f, 4f ) ),
			},
			Build = BuildBladder,
			Passive = new GraftPassive { Gravity = 0.72f, Jump = 1.1f },
			PassiveLine = "floatier jumps, slower falls",
			VoiceLine = "{0}'s breath is in my back now. I come down slower. I am not falling alone. I do squeak a bit.",
		},

		[BodyPlan.CrystalLattice] = new GraftPlanEntry
		{
			PartName = "crystal ridge",
			Spots = new[]
			{
				new GraftSpot( "spine_1", new Vector3( 0f, 0f, 5f ), 0 ),   // a ridge down the spine
				new GraftSpot( "arm_lower_l", Vector3.Zero, 1 ),              // a crystal forearm
				new GraftSpot( "arm_lower_r", Vector3.Zero, 1 ),
				new GraftSpot( "tail_1", new Vector3( 0f, 0f, 3f ), 0 ),
			},
			Build = BuildCrystal,
			Passive = new GraftPassive { Gravity = 1.12f, Speed = 0.95f, Resistance = 0.15f },
			PassiveLine = "heavier, but shrugs off shoves and frights",
			VoiceLine = "{0}'s shards are in my bones. I weigh more. Let them push.",
		},

		[BodyPlan.Swarm] = new GraftPlanEntry
		{
			PartName = "halo of motes",
			Spots = new[]
			{
				new GraftSpot( "head", new Vector3( 0f, 0f, 7f ), 0 ),
				new GraftSpot( "neck_0", new Vector3( 0f, 0f, 4f ), 1 ),
				new GraftSpot( "head", new Vector3( 0f, 0f, 12f ), 1 ),
			},
			Build = BuildHalo,
			Passive = new GraftPassive { PingEvery = 14f },
			PassiveLine = "scouts out the ones still hiding",
			VoiceLine = "{0} circles my head and goes looking. I can feel where the others are hiding. Three of it are in my ear.",
		},

		[BodyPlan.Jelly] = new GraftPlanEntry
		{
			PartName = "trailing skirt",
			Spots = new[]
			{
				new GraftSpot( "tail_2", new Vector3( 0f, 0f, -1f ), 0 ),      // a skirt round the tail
				new GraftSpot( "arm_upper_r", new Vector3( 0f, -2f, 0f ), 1 ), // a trailing arm
				new GraftSpot( "pelvis", new Vector3( -2f, 0f, -4f ), 0 ),
				new GraftSpot( "arm_upper_l", new Vector3( 0f, 2f, 0f ), 1 ),
			},
			Build = BuildSkirt,
			Passive = new GraftPassive { Cushion = 0.6f, Grip = 1.3f },
			PassiveLine = "softer landings, better grip",
			VoiceLine = "{0} hangs off me, soft. Landing does not hurt now. The rock holds on to me. That makes two things on me that wobble.",
		},

		[BodyPlan.Insectoid] = new GraftPlanEntry
		{
			PartName = "extra legs",
			Spots = new[]
			{
				new GraftSpot( "spine_1", new Vector3( 0f, 0f, -2f ), 0 ),  // a pair of small legs on the flanks
				new GraftSpot( "head", new Vector3( 3f, 0f, 3f ), 1 ),      // antennae
				new GraftSpot( "pelvis", new Vector3( 2f, 0f, -1f ), 0 ),
				new GraftSpot( "spine_2", new Vector3( 0f, 0f, -2f ), 0 ),
			},
			Build = BuildLegs,
			Passive = new GraftPassive { WallSpeed = 1.3f },
			PassiveLine = "faster on walls and ceilings",
			VoiceLine = "{0}'s legs find holds mine never did. The walls are roads now.",
		},

		[BodyPlan.Fungus] = new GraftPlanEntry
		{
			PartName = "budding caps",
			Spots = new[]
			{
				new GraftSpot( "spine_2", new Vector3( 0f, 0f, 2f ), 0 ),   // caps on both shoulders
				new GraftSpot( "tail_3", new Vector3( 0f, 0f, 2f ), 1 ),    // a row down the tail
				new GraftSpot( "spine_0", new Vector3( 0f, 0f, 3f ), 0 ),
				new GraftSpot( "tail_5", new Vector3( 0f, 0f, 2f ), 1 ),
			},
			Build = BuildCaps,
			Passive = new GraftPassive { CalmWhileStill = 0.06f },
			PassiveLine = "calm comes back faster while still",
			VoiceLine = "{0} is budding on my shoulders. When I stop, it is quiet. It was never quiet before. I smell faintly of soup.",
		},

		[BodyPlan.Machine] = new GraftPlanEntry
		{
			PartName = "bolted-on lamp",
			Spots = new[]
			{
				new GraftSpot( "spine_2", new Vector3( 3f, 6f, 3f ), 0 ),   // a lamp on the shoulder
				new GraftSpot( "pelvis", new Vector3( -2f, 0f, 5f ), 1 ),   // an antenna with a blinking light
				new GraftSpot( "spine_2", new Vector3( 3f, -6f, 3f ), 0 ),
				new GraftSpot( "tail_1", new Vector3( 0f, 0f, 3f ), 1 ),
			},
			Build = BuildLamp,
			Passive = new GraftPassive { StraightSpeed = 1.25f },
			PassiveLine = "builds speed in a straight line",
			VoiceLine = "{0} is bolted to me, humming. It only knows one direction. So: forward.",
		},
	};

	// ---- Twisted ability -> the component on a key ------------------------------------------------

	public static readonly Dictionary<TwistedAbility, GraftAbilityEntry> Abilities = new()
	{
		[TwistedAbility.BodyDoor] = new GraftAbilityEntry { Create = go => go.Components.Create<BodyDoorGraft>() },
		[TwistedAbility.OnlyWhileWatched] = new GraftAbilityEntry { Create = go => go.Components.Create<UnwatchedGraft>() },
		[TwistedAbility.BulletMagnet] = new GraftAbilityEntry { Create = go => go.Components.Create<MagnetGraft>() },
		[TwistedAbility.PriceTag] = new GraftAbilityEntry { Create = go => go.Components.Create<PriceTagGraft>() },
		[TwistedAbility.ToyStillness] = new GraftAbilityEntry { Create = go => go.Components.Create<ToyGraft>() },
	};

	// ---- The parts --------------------------------------------------------------------------------
	// Built at the socket, in the lizard's frame: +X ahead, +Y left, +Z up, inches. The lizard's
	// back is about ten inches wide, so these are small.

	/// <summary>GasSac: a pale bladder on the back that swells and sags, on a darker collar, with thin tendrils trailing behind.</summary>
	static void BuildBladder( Graft g )
	{
		var pale = Color.Lerp( g.Primary, Color.White, 0.45f );
		g.Ball( "Collar", Graft.BitKind.Static, new Vector3( 0f, 0f, -1f ), new Vector3( 4.5f, 4.5f, 1.6f ), g.Secondary );
		g.Ball( "Bladder", Graft.BitKind.Bladder, new Vector3( 0f, 0f, 4f ), new Vector3( 6.5f, 5.5f, 5.5f ), pale );
		g.Ball( "Nodule", Graft.BitKind.Bladder, new Vector3( 3f, 2.5f, 8.5f ), new Vector3( 1.8f ), g.Primary );
		for ( int i = 0; i < 3; i++ )
			g.Limb( "Tendril " + i, Graft.BitKind.Tendril, new Vector3( -4f, (i - 1) * 3f, 0f ), g.Rand( 7f, 11f ), 0.8f,
				i == 1 ? g.Primary : g.Secondary, 65f, 180f + (i - 1) * 22f );
	}

	/// <summary>CrystalLattice: a ridge of angular shards down the spine, or a cluster of them growing out of a forearm.</summary>
	static void BuildCrystal( Graft g )
	{
		bool forearm = g.Style == 1;
		int count = forearm ? 4 : 5;
		for ( int i = 0; i < count; i++ )
		{
			float length = forearm ? (i == 0 ? 13f : g.Rand( 6f, 10f )) : g.Rand( 6f, 12f ) * (i == 1 ? 1.3f : 1f);
			float thick = g.Rand( 1.8f, 3f );
			// The ridge runs fore and aft; the forearm cluster bunches round the bone.
			var at = forearm
				? new Vector3( g.Rand( -2f, 2f ), g.Rand( -2f, 2f ), g.Rand( -3f, 1f ) )
				: new Vector3( 8f - i * 4.5f, g.Rand( -1f, 1f ), length * 0.3f );
			var bit = g.Box( "Shard " + i, Graft.BitKind.Shard, at, new Vector3( thick, thick * g.Rand( 0.6f, 1f ), length ),
				i % 3 == 2 ? g.Secondary : g.Primary );
			var lean = forearm
				? new Angles( g.Rand( 100f, 150f ), i * 90f + g.Rand( -25f, 25f ), g.Rand( 0f, 90f ) )   // pointing down and out
				: new Angles( g.Rand( -28f, -8f ), g.Rand( -12f, 12f ), g.Rand( 0f, 90f ) );            // swept back
			g.Turn( bit, lean.ToRotation() );
			bit.A = g.Rand( 6f, 18f ) * (i % 2 == 0 ? 1f : -1f); // degrees a second about its own length
		}
	}

	/// <summary>Swarm: a ring of motes orbiting the head. The second one is wider and slower.</summary>
	static void BuildHalo( Graft g )
	{
		bool wide = g.Style == 1;
		for ( int i = 0; i < 9; i++ )
		{
			float radius = g.Rand( 0.8f, 1.5f );
			var bit = g.Ball( "Mote " + i, Graft.BitKind.Mote, Vector3.Zero, new Vector3( radius ), i % 3 == 0 ? g.Secondary : g.Primary );
			bit.Length = wide ? g.Rand( 11f, 15f ) : g.Rand( 7f, 10f );
			bit.A = g.Rand( 1.2f, 2.4f ) * (wide ? -0.7f : 1f);
			bit.B = g.Rand( 0.9f, 2f );
		}
	}

	/// <summary>Jelly: a small soft dome with a rippling skirt, or a few limp arms trailing from the shoulder.</summary>
	static void BuildSkirt( Graft g )
	{
		var pale = Color.Lerp( g.Primary, Color.White, 0.3f );
		if ( g.Style == 1 )
		{
			g.Ball( "Bulb", Graft.BitKind.Bladder, Vector3.Zero, new Vector3( 3f, 3f, 2.4f ), pale );
			for ( int i = 0; i < 3; i++ )
				g.Limb( "Arm " + i, Graft.BitKind.Tendril, new Vector3( -1f, (i - 1) * 1.5f, -1f ), g.Rand( 11f, 16f ), 1.1f,
					i == 1 ? g.Secondary : g.Primary, 80f, 180f + (i - 1) * 30f );
			return;
		}

		g.Ball( "Dome", Graft.BitKind.Bladder, new Vector3( 0f, 0f, 1f ), new Vector3( 5f, 5f, 3.2f ), pale );
		for ( int i = 0; i < 7; i++ )
		{
			float angle = i * (360f / 7f);
			var out_ = Rotation.FromYaw( angle ).Forward;
			var bit = g.Ball( "Skirt " + i, Graft.BitKind.Ripple, new Vector3( out_.x * 4.6f, out_.y * 4.6f, -2.5f ), new Vector3( 1.7f, 1.7f, 3.6f ),
				i % 2 == 0 ? g.Primary : g.Secondary );
			bit.B = angle;
		}
	}

	/// <summary>Insectoid: an extra pair of small stepping legs on the flanks, or a pair of twitching antennae.</summary>
	static void BuildLegs( Graft g )
	{
		if ( g.Style == 1 )
		{
			g.Limb( "Antenna L", Graft.BitKind.Antenna, new Vector3( 0f, 2f, 0f ), 12f, 0.7f, g.Secondary, -55f, 24f );
			g.Ball( "Root L", Graft.BitKind.Static, new Vector3( 0f, 2f, 0f ), new Vector3( 1.2f ), g.Primary );
			g.Limb( "Antenna R", Graft.BitKind.Antenna, new Vector3( 0f, -2f, 0f ), 12f, 0.7f, g.Secondary, -55f, -24f );
			g.Ball( "Root R", Graft.BitKind.Static, new Vector3( 0f, -2f, 0f ), new Vector3( 1.2f ), g.Primary );
			return;
		}

		for ( int i = 0; i < 2; i++ )
		{
			float side = i == 0 ? 1f : -1f;
			g.Ball( "Hip " + i, Graft.BitKind.Static, new Vector3( 0f, side * 5f, 0f ), new Vector3( 2f ), g.Primary );
			var leg = g.Limb( "Leg " + i, Graft.BitKind.Leg, new Vector3( 0f, side * 5.5f, 0f ), 14f, 1.3f, g.Secondary, 48f, side * 78f );
			leg.Index = i; // opposite sides step in turn
		}
	}

	/// <summary>Fungus: caps budding on both shoulders, or a row of them down the tail, with spores drifting off.</summary>
	static void BuildCaps( Graft g )
	{
		var stalk = Color.Lerp( g.Secondary, Color.White, 0.35f );
		bool tail = g.Style == 1;
		int count = tail ? 3 : 2;
		for ( int i = 0; i < count; i++ )
		{
			float side = i == 0 ? 1f : -1f;
			var root = tail ? new Vector3( 3f - i * 5f, 0f, 0f ) : new Vector3( g.Rand( -1f, 1f ), side * 6f, 0f );
			float height = g.Rand( 3f, 5.5f ), size = g.Rand( 2.6f, 4f );
			g.Limb( "Stalk " + i, Graft.BitKind.Static, root, height, 1.2f, stalk, -90f + g.Rand( -14f, 14f ), tail ? 0f : side * 90f );
			g.Ball( "Cap " + i, Graft.BitKind.Cap, g.LimbEnd, new Vector3( size, size, size * 0.45f ), i == 1 ? g.Secondary : g.Primary );
			g.Ball( "Bud " + i, Graft.BitKind.Cap, root + new Vector3( 1.5f, side * 1.5f, 1f ), new Vector3( size * 0.4f, size * 0.4f, size * 0.25f ), g.Primary );
		}
		for ( int i = 0; i < 3; i++ )
			g.Ball( "Spore " + i, Graft.BitKind.Spore, new Vector3( g.Rand( -4f, 4f ), g.Rand( -6f, 6f ), 5f ), new Vector3( 0.6f ), Color.Lerp( g.Secondary, Color.White, 0.5f ) );
	}

	/// <summary>Machine: a lamp on a bolted bracket (it really lights the way), or a thin mast with a blinking bead.</summary>
	static void BuildLamp( Graft g )
	{
		var metal = Color.Lerp( g.Primary, new Color( 0.45f, 0.46f, 0.48f ), 0.5f );
		var dark = new Color( 0.12f, 0.12f, 0.14f );
		var bright = Color.Lerp( g.Secondary, Color.White, 0.5f );

		if ( g.Style == 1 )
		{
			g.Box( "Base", Graft.BitKind.Static, Vector3.Zero, new Vector3( 4f, 4f, 1.6f ), metal );
			g.Limb( "Mast", Graft.BitKind.Antenna, new Vector3( 0f, 0f, 0.8f ), 15f, 0.6f, dark, -78f, 180f );
			var bead = g.Ball( "Bead", Graft.BitKind.Lamp, g.LimbEnd, new Vector3( 1.1f ), bright );
			bead.A = 1.2f;
			return;
		}

		var plate = g.Box( "Bracket", Graft.BitKind.Static, Vector3.Zero, new Vector3( 5.5f, 4.5f, 2.5f ), metal );
		g.Turn( plate, new Angles( 4f, 12f, -6f ).ToRotation() ); // bolted on crooked
		g.Ball( "Bolt A", Graft.BitKind.Static, new Vector3( -2f, 1.5f, 1.4f ), new Vector3( 0.7f ), dark );
		g.Ball( "Bolt B", Graft.BitKind.Static, new Vector3( -2f, -1.5f, 1.4f ), new Vector3( 0.7f ), dark );
		g.Limb( "Neck", Graft.BitKind.Static, new Vector3( 1f, 0f, 1f ), 4f, 1f, dark, -40f, 0f );
		g.Ball( "Lamp", Graft.BitKind.Lamp, g.LimbEnd, new Vector3( 2.2f ), bright );
		g.Glow( g.LimbEnd + Vector3.Forward * 3f, bright, 220f );
	}
}