Wagons on the rails, buildings on the slope, an autopilot nothing stops

- The train's loco and wagons stand on their front and back axles, so they pitch with the track up and down hills
  instead of standing level and hanging off one end; wheels sit on the rail tops, gauge matched to the models.
- A piece set down on the soil leans with the ground under its footprint (up to 22 degrees) and sinks until no
  corner hangs in the air; foundations stay level.
- On autopilot a machine touches nothing: it rides on the soil and the base deck and drives straight through
  buildings, rocks, rails, people and machines. Wedged anyway, it hops a few metres on; it no longer gives up on
  the way to unload.
- -demoauto puts a building in the way to the yard, sets one down on the steepest clear slope and reports any tip-over.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NiemandKG
2026-11-10 19:54:01 +06:00
parent 6b2a9931b9
commit 0db89bf7cb
5 changed files with 186 additions and 31 deletions

View File

@ -271,8 +271,44 @@ namespace GrassCutter
return;
}
at = _point;
if (GameDefs.IsFoundation(id)) at += _up * Raise;
rot = Quaternion.LookRotation(Quaternion.AngleAxis(_yaw, _up) * Heading(_up), _up);
// a foundation stays level (it is a floor to build on); everything else sits on the slope it stands on
if (GameDefs.IsFoundation(id)) at += _up * Raise;
else if (!_on) Lean(GameDefs.Piece(id), ref at, ref rot);
}
/// <summary>The most a piece leans with the ground under it.</summary>
const float MaxLean = 22f;
/// <summary>
/// Tilts a piece standing on the soil to the ground under its footprint (the plane through its four corners),
/// then lowers it until no corner hangs in the air.
/// </summary>
internal static void Lean(PieceDef d, ref Vector3 at, ref Quaternion rot)
{
if (d == null || Planet.Altitude(at) > 0.3f) return;
var up = Planet.Up(at);
float hx = Mathf.Max(0.5f, d.sizeX * 0.5f), hz = Mathf.Max(0.5f, d.sizeZ * 0.5f);
var r = rot * Vector3.right * hx;
var f = rot * Vector3.forward * hz;
var fr = Planet.Surface(at + r + f);
var fl = Planet.Surface(at - r + f);
var bl = Planet.Surface(at - r - f);
var br = Planet.Surface(at + r - f);
var n = Vector3.Cross(fr + fl - bl - br, fr + br - fl - bl);
if (n.sqrMagnitude < 1e-6f) return;
n.Normalize();
if (Vector3.Dot(n, up) < 0f) n = -n;
n = Vector3.RotateTowards(up, n, MaxLean * Mathf.Deg2Rad, 0f).normalized;
var mid = (fr + fl + bl + br) * 0.25f;
var c = Planet.Surface(at);
var p = mid + Vector3.ProjectOnPlane(c - mid, n);
// sink to the lowest of the corners and the middle, a little more than that so no gap shows
float drop = 0f;
foreach (var s in new[] { fr, fl, bl, br, c }) drop = Mathf.Min(drop, Vector3.Dot(s - p, n));
at = p + n * (Mathf.Max(drop, -0.6f) - 0.04f);
var fwd = Vector3.ProjectOnPlane(rot * Vector3.forward, n);
if (fwd.sqrMagnitude > 1e-6f) rot = Quaternion.LookRotation(fwd.normalized, n);
}
// ───────────────────────── pieces ─────────────────────────