RacesOfValheim/JotunnModStub/ConfigSync.cs
kanym 6f439f37c3 feat: Free Bat Flight, the old bat flight as a choice
The old bat flight is back as Free Flight: vanilla debug fly, up to 50 m/s, no stamina and no weight limit, Jump rises and Left Ctrl sinks. Each player picks it with Aswid - Bat Form / Free Bat Flight (local, never synced). The synced Bat Flight Rule (PlayerChoice, AlwaysStaminaFlight, AlwaysFreeFlight) lets the host or server decide for everyone. The mode is read when the form goes up, so a change counts from the next transform.

Debug fly makes vanilla treat the player as absent: no monster sees, hears or wakes for them, and hits are dropped. The six patches removed in v1.6.1 are back in AswidBatForm, for bats only and with no switch, so a free bat is fast but not invisible or immortal. Turning flight off now clears both flags, so death and reset end either flight. The compendium shows the Free Flight line when it is the active mode.

Asked for by MrBiffty, who builds tall with the bat.
2026-10-29 19:33:30 +01:00

106 lines
4.9 KiB
C#

using System;
using System.Linq;
using System.Reflection;
using BepInEx.Configuration;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Marks every gameplay config entry as admin-only so Jotunn syncs it from the server to the
/// clients (and locks it for non-admins in Configuration Manager).
///
/// Jotunn only recognises ITS OWN ConfigurationManagerAttributes (global namespace, in
/// Jotunn.dll) as a tag - it does an `is` check, not the field-name duck typing Configuration
/// Manager does - so our internal look-alike tag is not enough. This runs once after every
/// Bind() and adds Jotunn's tag beside whatever tags the entry already has, rather than
/// touching each of the ~350 Bind() calls.
///
/// Left local, because they are per-player preferences and syncing them would only take the
/// choice away from the player: keybinds, and the purely cosmetic entries (eye/claw colours,
/// glow, tint, rotation and offset of models, the VFX toggle, the stamina-bar toggle, debug
/// logging). Anything that changes a number the game computes with stays synced.
/// </summary>
internal static class ConfigSync
{
// Whole sections that are cosmetic or per-player.
private static readonly string[] LocalSections =
{
"Alfar - Look",
"Aswid - Claws",
"Aswid - Eyes",
"Fenrir - Appearance",
"Skeleton - Visuals",
"Sverm - Dorsal Spines",
"Sverm - Emberform",
"Trol - Appearance",
"War Vulture - Model",
"War Vulture - Interface",
};
// Cosmetic entries scattered through otherwise gameplay sections.
private static readonly string[] LocalKeyParts =
{
"marker light", "tint", "colour", "color", "glow", "swarm scale", "log rounds", "debug",
// The Aswid's seasick camera: motion makes some players ill, so a server cannot force it.
"camera sway",
// Each player picks their own True Form: a player model mod (VRM and the like) breaks
// under the troll costume, so a server cannot be allowed to force the costume on.
"legacy true form",
// The player's own pick of bat flight; the synced Bat Flight Rule can override it.
"free bat flight",
};
public static void Apply(ConfigFile config)
{
FieldInfo descField = typeof(ConfigEntryBase)
.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
.FirstOrDefault(f => f.FieldType == typeof(ConfigDescription));
if (descField == null)
{
Debug.LogWarning("[RacesOfValheim] Config sync skipped: ConfigEntryBase has no ConfigDescription field.");
return;
}
int synced = 0, local = 0;
foreach (ConfigDefinition def in config.Keys.ToList())
{
ConfigEntryBase entry = config[def];
if (!ShouldSync(entry)) { local++; continue; }
ConfigDescription old = entry.Description;
object[] tags = (old.Tags ?? new object[0]).Concat(new object[]
{
new global::ConfigurationManagerAttributes { IsAdminOnly = true }
}).ToArray();
descField.SetValue(entry, new ConfigDescription(old.Description, old.AcceptableValues, tags));
synced++;
}
Debug.Log($"[RacesOfValheim] Config sync: {synced} entries admin-synced, {local} left local.");
}
private static bool ShouldSync(ConfigEntryBase entry)
{
Type type = entry.SettingType;
if (type == typeof(KeyboardShortcut) || type == typeof(KeyCode)) return false;
string section = entry.Definition.Section ?? "";
string key = (entry.Definition.Key ?? "").ToLowerInvariant();
if (section.EndsWith("Keybinds", StringComparison.OrdinalIgnoreCase)) return false;
if (LocalSections.Contains(section, StringComparer.OrdinalIgnoreCase)) return false;
if (LocalKeyParts.Any(key.Contains)) return false;
// Model pose nudges (rotation/offset/yaw) are cosmetic, but "Attack Reach Offset" is a
// real hit-detection distance and must stay in step with the server.
if ((key.Contains("rotation") || key.Contains("offset") || key.Contains("yaw"))
&& !key.Contains("reach")) return false;
// Decorative note lines (CustomDrawer/ReadOnly) carry no value worth sending.
ConfigurationManagerAttributes ours = (entry.Description?.Tags ?? new object[0]).OfType<ConfigurationManagerAttributes>().FirstOrDefault();
if (ours != null && (ours.CustomDrawer != null || ours.ReadOnly == true)) return false;
return true;
}
}
}