Files
NiemandKG 36e4bb2a10 Round 6 fixes: held tools, headlamp glare, ladder openings, level horizon, roof hulls, rain hits, idle motion, belt packs, meadow variety
- Held tools sit lower and further right; the hammer faces forward and strikes about the camera's right
- Headlamp leaves held items, treats and vending machines alone (rendering layer NoLamp); other lights keep lighting them
- Hay grounded on the floor in world space (it floated)
- Deck fence opens at each ladder, rail colliders split around it; ladders moved to 2 m off the edge middle, clear of the intakes; climbing goes over a rail only if one is in the way
- Crew stand to the soil's up (Planet.Level): flat round the base, the ball's up past it, so the horizon stays level on a small planet
- Convex roof hulls for the crew cabin, garage, pitched roof and windmill tower; cabin box lowered to the eaves
- Rain and acid rain hits differ by surface: soil, grass, wood, metal
- Idle motion: dishes pan, sways sway, sensors turn, eyes blink, windmill turns in the breeze, reels and parked rotors tick over
- Belt: take tools off at the supply wall; hold E at a vending machine for a pack of 3 on the belt, LMB has one
- New meadow pieces: boulders, slab stones, crystals, mushrooms, stump, bulb plants
- -demofix checks for all of the above; loc rows in all 11 tables

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 22:52:05 +06:00

297 lines
14 KiB
C#

using System.Threading.Tasks;
using UnityEngine;
namespace GrassCutter
{
/// <summary>
/// Herbaria-7 is a real ball. Its centre sits straight under the base, so the base keeps its plain coordinates on the
/// north pole and "up" anywhere is simply away from the centre. The soil is a pure function of direction: hills and
/// craters on the ball, a flat clearing around the base, the same on every peer. Nothing walks on a collider: the crew,
/// machines and bundles all ask <see cref="Altitude"/>.
/// </summary>
public static class Planet
{
/// <summary>How big the ball is: the whole planet, or a half or a third of it (<see cref="Difficulties"/>).</summary>
public static float Radius { get; private set; } = GameDefs.PlanetRadius;
/// <summary>The ball against the whole planet.</summary>
public static float Scale => Radius / GameDefs.PlanetRadius;
public const float G = 9.81f;
/// <summary>Straight under the base, whatever the size: the base keeps its coordinates on the pole.</summary>
public static Vector3 Center { get; private set; } = new Vector3(0f, GameDefs.WaterY - GameDefs.PlanetRadius, 0f);
/// <summary>The ball has changed size: whatever was laid out on it lays itself out again.</summary>
public static event System.Action Rescaled;
/// <summary>Below this the base's flat maps (mow mask, deck grid) stop meaning anything: the far side shares their x, z.</summary>
public static float NorthCapY => -300f * Scale;
// the clearing around the base, then the blend out into open country (metres from the pole)
const float ClearA = 150f, ClearB = 420f;
const float CraterCell = 460f;
// the same around each of the eleven far stations, at the height the land has there
const float StationA = 45f, StationB = 140f;
const int FarStations = 11;
static readonly Vector3[] s_stations = new Vector3[FarStations];
static readonly float[] s_level = new float[FarStations];
static float s_stationReach = Mathf.Cos(StationB / GameDefs.PlanetRadius);
static Planet() => Levels();
static void Levels()
{
for (int i = 0; i < FarStations; i++)
{
s_stations[i] = HexGrid.Centre(i + 1);
s_level[i] = Terrain(s_stations[i]);
}
s_stationReach = Mathf.Cos(StationB / Radius);
}
/// <summary>
/// The ball at another size: the soil is worked out afresh (the same hills, in metres, on a smaller ball), the sectors
/// are cut again from the same lattice, and everything laid out on the ball hears <see cref="Rescaled"/>.
/// </summary>
public static void Rescale(float radius)
{
if (Mathf.Approximately(radius, Radius)) return;
Radius = radius;
Center = new Vector3(0f, GameDefs.WaterY - radius, 0f);
Levels();
HexGrid.Forget();
if (s_heights) Object.Destroy(s_heights);
s_heights = null;
Bake();
Rescaled?.Invoke();
}
/// <summary>Up at the camera, and the turn that brings the base's frame there: the sky, sun and particles live in it.</summary>
public static Vector3 LocalUp { get; private set; } = Vector3.up;
public static Quaternion Frame { get; private set; } = Quaternion.identity;
public static Vector3 Up(Vector3 p) => (p - Center).normalized;
/// <summary>
/// Which way the soil itself faces: straight up on the flat the base stands on, the ball's own up out past it, blended
/// across the ring where the flat bends down onto the ball. Standing to this keeps the horizon level: on a small
/// ball the flat's far side leans tens of degrees off the ball's up, and a head stood to the ball saw it tilted.
/// </summary>
public static Vector3 Level(Vector3 p)
{
var up = Up(p);
float s = Mathf.Sqrt(Mathf.Max(0f, 2f - 2f * up.y)) * Radius;
if (s >= ClearB) return up;
if (s <= ClearA) return Vector3.up;
return Vector3.Slerp(Vector3.up, up, Mathf.SmoothStep(0f, 1f, (s - ClearA) / (ClearB - ClearA))).normalized;
}
public static Vector3 OnSurface(Vector3 dir) => Center + dir * (Radius + HeightAt(dir));
public static Vector3 Surface(Vector3 p) => OnSurface(Up(p));
public static bool InNorthCap(Vector3 p) => p.y > NorthCapY;
/// <summary>Metres above the soil straight below (negative: under it).</summary>
public static float Altitude(Vector3 p)
{
var d = p - Center;
float r = d.magnitude;
return r - Radius - HeightAt(d / r);
}
/// <summary>Where a ray first meets the soil. There is no collider to hit, so it walks.</summary>
public static bool Cast(Vector3 from, Vector3 dir, float max, out Vector3 hit)
{
const float Step = 0.2f;
float clear = 0f;
for (float t = Step; t <= max + Step; t += Step)
{
if (Altitude(from + dir * t) >= 0f) { clear = t; continue; }
float hi = t;
for (int i = 0; i < 6; i++)
{
float mid = (clear + hi) * 0.5f;
if (Altitude(from + dir * mid) < 0f) hi = mid; else clear = mid;
}
hit = from + dir * hi;
return hi <= max;
}
hit = default;
return false;
}
/// <summary>Direction of a spot x, z metres from the base along the ground.</summary>
public static Vector3 FromBase(float x, float z)
{
float s = Mathf.Sqrt(x * x + z * z);
if (s < 1e-3f) return Vector3.up;
float a = s / Radius, k = Mathf.Sin(a) / s;
return new Vector3(x * k, Mathf.Cos(a), z * k);
}
public static Vector3 FromLatLon(float lat, float lon)
{
float a = lat * Mathf.Deg2Rad, b = lon * Mathf.Deg2Rad;
return new Vector3(Mathf.Cos(a) * Mathf.Sin(b), Mathf.Sin(a), Mathf.Cos(a) * Mathf.Cos(b));
}
public static void SetFrame(Vector3 camera)
{
LocalUp = Up(camera);
// carried along rather than recomputed: the sky never spins when you walk over the south pole
Frame = Quaternion.FromToRotation(Frame * Vector3.up, LocalUp) * Frame;
Physics.gravity = -LocalUp * G; // only particles use it: bodies fall toward the centre on their own
}
// ───────────────────────── soil ─────────────────────────
/// <summary>Height of the soil above the bare ball along a direction.</summary>
public static float HeightAt(Vector3 dir)
{
float s = Mathf.Sqrt(Mathf.Max(0f, 2f - 2f * dir.y)) * Radius;
if (s <= ClearA) return Radius / dir.y - Radius; // the plane the base stands on
float open = StationGround(dir, Terrain(dir));
if (s >= ClearB) return open;
float k = Mathf.SmoothStep(0f, 1f, (s - ClearA) / (ClearB - ClearA));
return Mathf.Lerp(Radius / dir.y - Radius, open, k);
}
static float StationGround(Vector3 dir, float open)
{
for (int i = 0; i < FarStations; i++)
{
float dot = Vector3.Dot(dir, s_stations[i]);
if (dot < s_stationReach) continue;
float gap = Mathf.Sqrt(Mathf.Max(0f, 2f - 2f * dot)) * Radius;
float flat = s_level[i] + Radius / dot - Radius;
return gap <= StationA ? flat : Mathf.Lerp(flat, open, Mathf.SmoothStep(0f, 1f, (gap - StationA) / (StationB - StationA)));
}
return open;
}
static float Terrain(Vector3 dir)
{
float x = dir.x * Radius, y = dir.y * Radius, z = dir.z * Radius;
return (Noise(x * 0.0011f + 31.7f, y * 0.0011f + 12.1f, z * 0.0011f + 5.3f) - 0.5f) * 70f
+ (Noise(x * 0.0037f + 3.3f, y * 0.0037f + 7.7f, z * 0.0037f + 1.1f) - 0.5f) * 26f
+ (Noise(x * 0.012f + 9.1f, y * 0.012f + 1.9f, z * 0.012f + 4.2f) - 0.5f) * 6f
+ Craters(x, y, z);
}
/// <summary>A few bowls with raised rims, one possible per 460 m cell, none near the base.</summary>
static float Craters(float x, float y, float z)
{
int cx = Mathf.FloorToInt(x / CraterCell), cy = Mathf.FloorToInt(y / CraterCell), cz = Mathf.FloorToInt(z / CraterCell);
float h = 0f;
for (int k = -1; k <= 1; k++)
for (int j = -1; j <= 1; j++)
for (int i = -1; i <= 1; i++)
{
int ix = cx + i, iy = cy + j, iz = cz + k;
if (Hash(ix, iy, iz) > 0.42f) continue;
float px = (ix + Hash(iz, ix, iy)) * CraterCell, py = (iy + Hash(iy, iz, ix)) * CraterCell, pz = (iz + Hash(ix + 7, iy, iz)) * CraterCell;
float len = Mathf.Sqrt(px * px + py * py + pz * pz);
if (len < 1f) continue;
float s = Radius / len;
px *= s; py *= s; pz *= s;
// the middle of the crater must lie well away from the base
float bx = px, by = py - Radius, bz = pz;
if (bx * bx + by * by + bz * bz < 620f * 620f) continue;
float size = Hash(iy + 3, iz, ix);
float r = Mathf.Lerp(35f, 120f, size * size);
float dx = x - px, dy = y - py, dz = z - pz;
float d2 = dx * dx + dy * dy + dz * dz;
if (d2 > r * r * 2.6f) continue;
float t = Mathf.Sqrt(d2) / r;
if (t < 1f) h -= r * 0.2f * (1f - t * t);
float rim = (t - 1f) / 0.22f;
h += r * 0.06f * Mathf.Exp(-rim * rim);
}
return h;
}
static float Hash(int x, int y, int z)
{
uint h = (uint)x * 374761393u + (uint)y * 668265263u + (uint)z * 2246822519u;
h = (h ^ (h >> 13)) * 1274126177u;
h ^= h >> 16;
return h / 4294967296f;
}
static float Noise(float x, float y, float z)
{
int ix = Mathf.FloorToInt(x), iy = Mathf.FloorToInt(y), iz = Mathf.FloorToInt(z);
float fx = x - ix, fy = y - iy, fz = z - iz;
fx = fx * fx * (3f - 2f * fx); fy = fy * fy * (3f - 2f * fy); fz = fz * fz * (3f - 2f * fz);
float a = Mathf.LerpUnclamped(Hash(ix, iy, iz), Hash(ix + 1, iy, iz), fx);
float b = Mathf.LerpUnclamped(Hash(ix, iy + 1, iz), Hash(ix + 1, iy + 1, iz), fx);
float c = Mathf.LerpUnclamped(Hash(ix, iy, iz + 1), Hash(ix + 1, iy, iz + 1), fx);
float d = Mathf.LerpUnclamped(Hash(ix, iy + 1, iz + 1), Hash(ix + 1, iy + 1, iz + 1), fx);
return Mathf.LerpUnclamped(Mathf.LerpUnclamped(a, b, fy), Mathf.LerpUnclamped(c, d, fy), fz);
}
// ───────────────────────── the GPU's copy ─────────────────────────
const int HeightRes = 1024, NoiseRes = 64;
static readonly int HeightsId = Shader.PropertyToID("_GcHeights"), HeightResId = Shader.PropertyToID("_GcHeightRes");
static readonly int PlanetId = Shader.PropertyToID("_GcPlanet"), NoiseId = Shader.PropertyToID("_GcNoise3");
static Texture2DArray s_heights;
static Texture3D s_noise;
/// <summary>Publish the soil to the shaders: six cube faces of heights (Planet.hlsl reads them) and a tiling 3D noise.</summary>
public static void Bake()
{
if (!s_heights)
{
s_heights = new Texture2DArray(HeightRes, HeightRes, 6, TextureFormat.RFloat, false, true)
{
name = "PlanetHeights", wrapMode = TextureWrapMode.Clamp, filterMode = FilterMode.Bilinear,
};
for (int f = 0; f < 6; f++)
{
var data = new float[HeightRes * HeightRes];
int face = f;
Parallel.For(0, HeightRes, j =>
{
for (int i = 0; i < HeightRes; i++)
data[j * HeightRes + i] = HeightAt(FaceDir(face, i / (HeightRes - 1f) * 2f - 1f, j / (HeightRes - 1f) * 2f - 1f));
});
s_heights.SetPixelData(data, 0, f);
}
s_heights.Apply(false, true);
var rng = new System.Random(4177);
var px = new Color32[NoiseRes * NoiseRes * NoiseRes];
var bytes = new byte[4];
for (int i = 0; i < px.Length; i++)
{
rng.NextBytes(bytes);
px[i] = new Color32(bytes[0], bytes[1], bytes[2], bytes[3]);
}
s_noise = new Texture3D(NoiseRes, NoiseRes, NoiseRes, TextureFormat.RGBA32, true)
{
name = "PlanetNoise", wrapMode = TextureWrapMode.Repeat, filterMode = FilterMode.Bilinear,
};
s_noise.SetPixels32(px);
s_noise.Apply(true, true);
}
Shader.SetGlobalTexture(HeightsId, s_heights);
Shader.SetGlobalTexture(NoiseId, s_noise);
Shader.SetGlobalFloat(HeightResId, HeightRes);
Shader.SetGlobalVector(PlanetId, new Vector4(Center.x, Center.y, Center.z, Radius));
}
/// <summary>Same face layout as GcHeight in Planet.hlsl. u, v run -1..1 and both faces of an edge share its texels.</summary>
static Vector3 FaceDir(int f, float u, float v)
{
Vector3 d;
switch (f)
{
case 0: d = new Vector3(1f, v, u); break;
case 1: d = new Vector3(-1f, v, u); break;
case 2: d = new Vector3(u, 1f, v); break;
case 3: d = new Vector3(u, -1f, v); break;
case 4: d = new Vector3(u, v, 1f); break;
default: d = new Vector3(u, v, -1f); break;
}
return d.normalized;
}
}
}