Merge branch 'localisation' into localisation-turkish

This commit is contained in:
kanym 2026-10-17 19:33:40 +02:00
commit 3a6c88c3d1
2 changed files with 215 additions and 4 deletions

View file

@ -48,6 +48,12 @@ namespace RacesOfValheim
// drives the creature's own sleep through it, and not every creature has one.
private Animator sleepChecked;
private bool hasSleeping;
private int sleepState;
private bool wasSitting;
// Read off ulv_animator with scripts/dump_controller.py: the entry state "check sleeping" goes
// to Sleep only at start-up, and Movement has no way back into it. "Base Layer.Sleep" is the
// real path; the short name stays as a fallback for another creature built the same way.
private static readonly string[] SleepStateNames = { "Base Layer.Sleep", "Sleep" };
private static readonly System.Collections.Generic.HashSet<string> sleepLogged = new System.Collections.Generic.HashSet<string>();
/// <summary>Copies the movement parameters, and says whether the form counts as grounded.</summary>
@ -66,8 +72,15 @@ namespace RacesOfValheim
// sneaking in a form slid across the ground in the idle pose. Crouched, the player moves
// at sneaking speed, and the creature's controller picks its walk for a speed that low:
// the sneak a player asked for, from animations the creature already has.
try { form.SetFloat("forward_speed", grounded ? playerAnim.GetFloat("forward_speed") : 0f); } catch {}
try { form.SetFloat("sideway_speed", grounded ? playerAnim.GetFloat("sideway_speed") : 0f); } catch {}
//
// Sitting reads as still too. The seated player's own Animator keeps a forward_speed that
// is not zero, and passed through it kept the Ulv in its Walk clip - on the spot, and out
// of reach of its Sleep state, which only leaves from idle. Seen in the log: sitting,
// sleeping=true, clip 'Walk'.
bool sitting = IsSitting(player);
bool still = !grounded || sitting;
try { form.SetFloat("forward_speed", still ? 0f : playerAnim.GetFloat("forward_speed")); } catch {}
try { form.SetFloat("sideway_speed", still ? 0f : playerAnim.GetFloat("sideway_speed")); } catch {}
// Sitting puts the creature to sleep, on the "sleeping" bool its own MonsterAI uses - the
// Ulv curls up. IsSitting reads the animator's state, which is replicated, so it is right
@ -80,18 +93,111 @@ namespace RacesOfValheim
{
if (p.type == AnimatorControllerParameterType.Bool && p.name == "sleeping") { hasSleeping = true; break; }
}
// The state's name is not readable at runtime, only testable - but it is readable offline,
// see SleepStateNames.
sleepState = 0;
string found = "none";
if (hasSleeping)
{
foreach (string candidate in SleepStateNames)
{
int hash = Animator.StringToHash(candidate);
if (!form.HasState(0, hash)) continue;
sleepState = hash;
found = candidate;
break;
}
}
string controller = form.runtimeAnimatorController != null ? form.runtimeAnimatorController.name : form.name;
if (sleepLogged.Add(controller))
{
Jotunn.Logger.LogInfo($"[FenrirForm] '{controller}' {(hasSleeping ? "has" : "has no")} sleeping animation; " +
(hasSleeping ? "sitting puts it to sleep." : "sitting keeps it idle."));
(hasSleeping ? $"sitting puts it to sleep (state '{found}')." : "sitting keeps it idle."));
}
}
if (hasSleeping) form.SetBool("sleeping", IsSitting(player));
if (hasSleeping)
{
form.SetBool("sleeping", sitting);
// The bool alone never put the Ulv to sleep. A vanilla Ulv is BORN asleep: its
// controller enters Sleep from the start and only knows the way out (Sleep, then
// 'Sleep to Idle' once the bool drops). Nothing leads from Idle back into Sleep, so
// the state is entered by hand when the player sits; standing up drops the bool and
// the controller's own wake-up plays.
if (sitting && !wasSitting && sleepState != 0) form.CrossFadeInFixedTime(sleepState, 0.4f, 0);
}
wasSitting = sitting;
PlaceOnChair(form, player);
return grounded;
}
// How far in front of the seat a creature on a chair settles, in metres.
private const float ChairForward = 0.5f;
private static readonly int ChairGroundMask = LayerMask.GetMask("Default", "static_solid", "terrain", "piece", "vehicle");
private static readonly RaycastHit[] chairHits = new RaycastHit[16];
private Transform placedRoot;
/// <summary>
/// On a chair the player sits on the seat itself, and a creature there stood inside the chair,
/// at seat height. It goes half a metre in front of the seat instead, standing on whatever is
/// under that point. Moves the form's own root (the child of the player), never the player.
/// The sit key is left alone: the player is on the ground already.
/// </summary>
private void PlaceOnChair(Animator form, Player player)
{
Transform root = RootUnder(form.transform, player != null ? player.transform : null);
if (root == null) return;
bool onChair = player.IsSitting();
if (!onChair)
{
if (placedRoot == root && root.localPosition != Vector3.zero) root.localPosition = Vector3.zero;
return;
}
placedRoot = root;
Transform body = player.transform;
Piece seat = SeatUnder(body.position);
Vector3 target = body.position + body.forward * ChairForward;
float groundY = target.y;
int count = Physics.RaycastNonAlloc(target + Vector3.up * 1.5f, Vector3.down, chairHits, 4f, ChairGroundMask);
float best = float.MaxValue;
for (int i = 0; i < count; i++)
{
Collider c = chairHits[i].collider;
if (c == null || c.transform.IsChildOf(body)) continue;
if (seat != null && c.GetComponentInParent<Piece>() == seat) continue;
if (chairHits[i].distance < best) { best = chairHits[i].distance; groundY = chairHits[i].point.y; }
}
root.localPosition = body.InverseTransformPoint(new Vector3(target.x, groundY, target.z));
}
/// <summary>The chair or bench the player sits on: the first piece straight under them.</summary>
private static Piece SeatUnder(Vector3 position)
{
int count = Physics.RaycastNonAlloc(position + Vector3.up * 0.3f, Vector3.down, chairHits, 1.5f, ChairGroundMask);
Piece seat = null;
float best = float.MaxValue;
for (int i = 0; i < count; i++)
{
Piece piece = chairHits[i].collider != null ? chairHits[i].collider.GetComponentInParent<Piece>() : null;
if (piece != null && chairHits[i].distance < best) { best = chairHits[i].distance; seat = piece; }
}
return seat;
}
private static Transform RootUnder(Transform t, Transform player)
{
if (player == null) return null;
while (t != null && t.parent != player) t = t.parent;
return t;
}
/// <summary>
/// The sit key, or a chair. The sit key is an EMOTE, not the "sitting" animator tag that
/// Humanoid.IsSitting asks about - that tag belongs to chairs and benches - so IsSitting alone