368 lines
16 KiB
C#
368 lines
16 KiB
C#
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using HarmonyLib;
|
|
using Jotunn.Configs;
|
|
using Jotunn.Entities;
|
|
using Jotunn.Managers;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
/// <summary>
|
|
/// The Bone Ship, first piece: the small one, a clone of the Karve. See Skeleton_BoneShip.txt, sections
|
|
/// 2, 10 and 11 - every number and node name below was read off the game, not remembered.
|
|
///
|
|
/// WHAT THIS STEP DOES
|
|
/// - A new buildable ship, BoneKarve, cloned from "Karve" so the vanilla one is left alone.
|
|
/// - A hull that is rotten from the first day: WearNTear is pointed at the prefab's own worn hull as
|
|
/// its "new" one, so full health shows the worn model, and a light tint goes on top. The game's
|
|
/// SetHealthVisual guards the equal case (it only turns a visual off when it differs from the new
|
|
/// one), so m_new == m_worn is a supported shape, not a trick. The fresh hull is switched off in
|
|
/// the prefab or both would show at once.
|
|
/// - It goes where the sails are not: the wind-independent push (SailK) and a sail that never
|
|
/// unfurls, in BoneShipSailForcePatch and BoneShipSailPatch below.
|
|
///
|
|
/// WHAT IS NOT HERE YET: the torn sail texture, the bones, the rowers, the figurehead, the altar, the
|
|
/// roster and the archers. Each is its own step. The archer's mount already exists (ShipMount.cs).
|
|
/// </summary>
|
|
public static class BoneShip
|
|
{
|
|
// Every vanilla ship but the Raft has a bone version (the design, 2026-10-05). The names are what
|
|
// ShipMountTest's post table and the rest of the Bone Ship know them by.
|
|
public const string KarveName = "BoneKarve";
|
|
public const string LongshipName = "BoneLongship";
|
|
public const string AshlandsName = "BoneAshlands";
|
|
|
|
/// <summary>The hover name of the altar on the deck, where bone fragments are offered.</summary>
|
|
public const string AltarName = "$rov_piece_boneship_altar_name";
|
|
|
|
/// <summary>
|
|
/// Places in the roster, melee and archers, by ship (Skeleton_BoneShip.txt 5.7). The small ship is
|
|
/// the pile's idea at half size, the Longship a pile's 4+4, and the Ashlands ship - the biggest hull,
|
|
/// three times the Karve's mass - 6+6 (decided). Constants, because the answer is a balance call.
|
|
/// The rowers' seats follow: one per place, half on each side.
|
|
/// </summary>
|
|
private const int KarveMelee = 2, KarveRanged = 2;
|
|
private const int LongshipMelee = 4, LongshipRanged = 4;
|
|
private const int AshlandsMelee = 6, AshlandsRanged = 6;
|
|
|
|
public static int MeleeCapOf(Component ship) =>
|
|
IsKarve(ship) ? KarveMelee : IsAshlands(ship) ? AshlandsMelee : LongshipMelee;
|
|
|
|
public static int RangedCapOf(Component ship) =>
|
|
IsKarve(ship) ? KarveRanged : IsAshlands(ship) ? AshlandsRanged : LongshipRanged;
|
|
|
|
private static bool IsAshlands(Component ship)
|
|
{
|
|
return ship != null && ship.name.StartsWith(AshlandsName);
|
|
}
|
|
|
|
private static bool IsKarve(Component ship)
|
|
{
|
|
return ship != null && ship.name.StartsWith(KarveName);
|
|
}
|
|
|
|
public static bool IsKarveShip(Component ship) => IsKarve(ship);
|
|
|
|
/// <summary>Half the length of the hull, read from the OnboardTrigger every ship carries.</summary>
|
|
public static float HalfLengthOf(Component ship)
|
|
{
|
|
Transform trigger = ship != null ? ship.transform.Find("OnboardTrigger") : null;
|
|
BoxCollider box = trigger != null ? trigger.GetComponent<BoxCollider>() : null;
|
|
return box != null ? Mathf.Abs(box.size.z * trigger.localScale.z) * 0.5f : 0f;
|
|
}
|
|
|
|
/// <summary>Our prefab, the vanilla one it is cloned from, and the localisation key stem.</summary>
|
|
private struct Spec
|
|
{
|
|
public string Name;
|
|
public string Source;
|
|
public string KeyStem;
|
|
public Spec(string name, string source, string keyStem) { Name = name; Source = source; KeyStem = keyStem; }
|
|
}
|
|
|
|
private static readonly Spec[] Specs =
|
|
{
|
|
new Spec(KarveName, "Karve", "karve"),
|
|
new Spec(LongshipName, "VikingShip", "longship"),
|
|
new Spec(AshlandsName, "VikingShip_Ashlands", "ashlands"),
|
|
};
|
|
|
|
private static string DisplayKey(Spec s) => "$rov_piece_boneship_" + s.KeyStem + "_name";
|
|
private static string DescriptionKey(Spec s) => "$rov_piece_boneship_" + s.KeyStem + "_desc";
|
|
|
|
/// <summary>
|
|
/// What the oarsmen push with, as a share of what a perfect wind would. Wind direction and strength
|
|
/// no longer matter, so this is the whole sail. The vanilla factor is angle * lerp(0.25, 1, wind):
|
|
/// 1.0 is the best a real wind can ever do, and a good breeze on a good heading is about 0.7. Measured
|
|
/// on a Karve at Full: 0.46 gave 4.5 m/s. 0.75 is the proposal; to be tuned with an F8 at cruise.
|
|
/// </summary>
|
|
public const float SailK = 0.75f;
|
|
|
|
/// <summary>The key part of every bone ship's recipe: the swamp's Wishbone. One per ship.</summary>
|
|
private const string WishbonePrefab = "Wishbone";
|
|
|
|
/// <summary>
|
|
/// What each costs. The Wishbone is the decided part; the rest are PLACEHOLDERS that grow with the
|
|
/// hull, and the Ashlands one asks for the fine wood a late-game ship would. The workbench requirement
|
|
/// comes from the vanilla ship each one is cloned from. Open to every race: nothing here, or on the
|
|
/// pieces, asks who is building.
|
|
/// </summary>
|
|
private static RequirementConfig[] CostOf(string name)
|
|
{
|
|
switch (name)
|
|
{
|
|
case KarveName:
|
|
return new[]
|
|
{
|
|
new RequirementConfig(WishbonePrefab, 1, 0, true),
|
|
new RequirementConfig("Wood", 30, 0, true),
|
|
new RequirementConfig("BoneFragments", 60, 0, true),
|
|
new RequirementConfig("Resin", 10, 0, true),
|
|
};
|
|
|
|
case LongshipName:
|
|
return new[]
|
|
{
|
|
new RequirementConfig(WishbonePrefab, 1, 0, true),
|
|
new RequirementConfig("Wood", 60, 0, true),
|
|
new RequirementConfig("BoneFragments", 120, 0, true),
|
|
new RequirementConfig("Resin", 20, 0, true),
|
|
};
|
|
|
|
default: // the Ashlands one
|
|
return new[]
|
|
{
|
|
new RequirementConfig(WishbonePrefab, 1, 0, true),
|
|
new RequirementConfig("FineWood", 60, 0, true),
|
|
new RequirementConfig("BoneFragments", 200, 0, true),
|
|
new RequirementConfig("Resin", 30, 0, true),
|
|
};
|
|
}
|
|
}
|
|
|
|
/// <summary>Multiplied into the worn hull's colour: a touch darker and greener on top of the worn one.</summary>
|
|
private static readonly Color RotTint = new Color(0.80f, 0.82f, 0.72f, 1f);
|
|
|
|
public static bool IsBoneShip(Ship ship)
|
|
{
|
|
return ship != null && ship.GetComponent<BoneShipMarker>() != null;
|
|
}
|
|
|
|
public static void Register()
|
|
{
|
|
BoneShipArcher.Register();
|
|
|
|
foreach (Spec spec in Specs)
|
|
{
|
|
RegisterShip(spec);
|
|
}
|
|
}
|
|
|
|
private static void RegisterShip(Spec spec)
|
|
{
|
|
GameObject prefab = PrefabManager.Instance.CreateClonedPrefab(spec.Name, spec.Source);
|
|
if (prefab == null)
|
|
{
|
|
Jotunn.Logger.LogWarning($"[BoneShip] Could not clone '{spec.Source}'; {spec.Name} is not registered.");
|
|
return;
|
|
}
|
|
|
|
prefab.AddComponent<BoneShipMarker>();
|
|
RotHull(prefab, spec.Name);
|
|
BoneShipSail.Apply(prefab, spec.Name);
|
|
|
|
// The roster (the altar's bookkeeping and the troops) and the fittings (bones and the altar
|
|
// itself, built when the ship is in the world). The roster registers its RPC in Awake.
|
|
prefab.AddComponent<BoneShipRoster>();
|
|
prefab.AddComponent<BoneShipFittings>();
|
|
prefab.AddComponent<BoneShipRowers>(); // the oarsmen; how many show is the roster's count
|
|
|
|
Piece piece = prefab.GetComponent<Piece>();
|
|
if (piece == null)
|
|
{
|
|
Jotunn.Logger.LogWarning($"[BoneShip] The cloned '{spec.Source}' has no Piece; {spec.Name} is not registered.");
|
|
return;
|
|
}
|
|
|
|
piece.m_name = DisplayKey(spec);
|
|
piece.m_description = DescriptionKey(spec);
|
|
Sprite icon = BorrowIcon("TrophySkeleton");
|
|
if (icon != null) piece.m_icon = icon;
|
|
|
|
PieceManager.Instance.AddPiece(new CustomPiece(prefab, fixReference: false, new PieceConfig
|
|
{
|
|
Name = DisplayKey(spec),
|
|
Description = DescriptionKey(spec),
|
|
PieceTable = PieceTables.Hammer,
|
|
Category = PieceCategories.Misc,
|
|
CraftingStation = CraftingStations.Workbench,
|
|
Icon = icon,
|
|
Requirements = CostOf(spec.Name),
|
|
}));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Worn hull from full health. Every vanilla ship keeps its three hull visuals where WearNTear can
|
|
/// name them - m_new, m_worn, m_broken - whatever the node path (Karve: ship/visual_new,
|
|
/// Longship and Ashlands: ship/visual/hull_new), so the clone's own fields are read instead of a
|
|
/// path per ship. The fresh one is on in the prefab and has to be switched off, or both would show.
|
|
/// </summary>
|
|
private static void RotHull(GameObject prefab, string name)
|
|
{
|
|
WearNTear wear = prefab.GetComponent<WearNTear>();
|
|
if (wear == null || wear.m_new == null || wear.m_worn == null)
|
|
{
|
|
Jotunn.Logger.LogWarning($"[BoneShip] {name}: WearNTear has no new/worn hull to swap; the hull stays as it is.");
|
|
return;
|
|
}
|
|
|
|
GameObject fresh = wear.m_new;
|
|
GameObject worn = wear.m_worn;
|
|
if (fresh == worn) return;
|
|
|
|
wear.m_new = worn;
|
|
fresh.SetActive(false);
|
|
|
|
Tint(worn, RotTint);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Darkens the Piece-shader materials of a hull. COPIES them, so the vanilla ship that shares the
|
|
/// asset keeps its colour; the water mask and anything without a plain colour are left alone.
|
|
/// </summary>
|
|
private static void Tint(GameObject root, Color tint)
|
|
{
|
|
Dictionary<Material, Material> copies = new Dictionary<Material, Material>();
|
|
|
|
foreach (Renderer renderer in root.GetComponentsInChildren<Renderer>(true))
|
|
{
|
|
if (renderer == null || renderer is ParticleSystemRenderer) continue;
|
|
|
|
Material[] shared = renderer.sharedMaterials;
|
|
bool changed = false;
|
|
|
|
for (int i = 0; i < shared.Length; i++)
|
|
{
|
|
Material source = shared[i];
|
|
if (source == null || source.shader == null) continue;
|
|
if (source.shader.name != "Custom/Piece" || !source.HasProperty("_Color")) continue;
|
|
|
|
Material copy;
|
|
if (!copies.TryGetValue(source, out copy))
|
|
{
|
|
copy = new Material(source) { name = source.name + "_bone" };
|
|
copy.SetColor("_Color", source.GetColor("_Color") * tint);
|
|
copies[source] = copy;
|
|
}
|
|
|
|
shared[i] = copy;
|
|
changed = true;
|
|
}
|
|
|
|
if (changed) renderer.sharedMaterials = shared;
|
|
}
|
|
}
|
|
|
|
public static void EnsureKnown(Player player)
|
|
{
|
|
if (player == null) return;
|
|
|
|
Traverse traverse = Traverse.Create(player);
|
|
bool any = false;
|
|
|
|
foreach (Spec spec in Specs)
|
|
{
|
|
GameObject prefab = PrefabManager.Instance.GetPrefab(spec.Name);
|
|
Piece piece = prefab != null ? prefab.GetComponent<Piece>() : null;
|
|
if (piece == null) continue;
|
|
|
|
traverse.Method("AddKnownPiece", piece).GetValue();
|
|
any = true;
|
|
}
|
|
|
|
if (any) traverse.Method("UpdateAvailablePiecesList").GetValue();
|
|
}
|
|
|
|
private static Sprite BorrowIcon(string prefabName)
|
|
{
|
|
ItemDrop drop = PrefabManager.Cache.GetPrefab<ItemDrop>(prefabName);
|
|
Sprite[] icons = drop != null && drop.m_itemData != null && drop.m_itemData.m_shared != null
|
|
? drop.m_itemData.m_shared.m_icons
|
|
: null;
|
|
if (icons != null && icons.Length > 0 && icons[0] != null) return icons[0];
|
|
|
|
Jotunn.Logger.LogWarning($"[BoneShip] Could not borrow the icon from '{prefabName}'.");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>Marks a ship as one of ours. No logic: the patches below and the archers look for it.</summary>
|
|
public class BoneShipMarker : MonoBehaviour
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// The oarsmen's push, in place of the wind's.
|
|
///
|
|
/// Ship.GetSailForce is, read off the IL: normalize(windDir + forward) * angleFactor * lerp(0.25, 1,
|
|
/// windIntensity) * m_sailForceFactor * sailSize, smoothed into m_sailForce over a second. The angle
|
|
/// factor is zero when the wind comes from ahead - that is what stops a vanilla ship dead. For a bone
|
|
/// ship the target is simply forward * m_sailForceFactor * sailSize * SailK, smoothed the same way with
|
|
/// the same two private fields, so the ship accelerates like a sailing one and the vanilla factor of
|
|
/// each hull (Karve 0.03, VikingShip 0.05, Ashlands 0.085) still scales the push.
|
|
///
|
|
/// sailSize is 0 below Half, 0.5 at Half and 1 at Full, so Slow and Back are untouched: they were
|
|
/// never the wind's.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(Ship), "GetSailForce")]
|
|
public static class BoneShipSailForcePatch
|
|
{
|
|
private static readonly FieldInfo SailForce = AccessTools.Field(typeof(Ship), "m_sailForce");
|
|
private static readonly FieldInfo WindChangeVelocity = AccessTools.Field(typeof(Ship), "m_windChangeVelocity");
|
|
|
|
public static bool Prefix(Ship __instance, float sailSize, ref Vector3 __result)
|
|
{
|
|
if (!BoneShip.IsBoneShip(__instance)) return true;
|
|
if (SailForce == null || WindChangeVelocity == null) return true; // changed under us: fall back to vanilla
|
|
|
|
Vector3 current = (Vector3)SailForce.GetValue(__instance);
|
|
Vector3 velocity = (Vector3)WindChangeVelocity.GetValue(__instance);
|
|
|
|
Vector3 target = __instance.transform.forward * (__instance.m_sailForceFactor * sailSize * BoneShip.SailK);
|
|
current = Vector3.SmoothDamp(current, target, ref velocity, 1f, 99f);
|
|
|
|
SailForce.SetValue(__instance, current);
|
|
WindChangeVelocity.SetValue(__instance, velocity);
|
|
|
|
__result = current;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The sail never unfurls and the mast never turns to the wind: there is no sailing to show. Half and
|
|
/// Full would otherwise raise a sail that is meant to hang in rags. UpdateSail is what runs
|
|
/// UpdateSailSize (the furl animation) and then turns the mast, so skipping it skips both.
|
|
/// What the sail looks like left alone is for the game to show: the prefab's own pose.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(Ship), "UpdateSail")]
|
|
public static class BoneShipSailPatch
|
|
{
|
|
public static bool Prefix(Ship __instance)
|
|
{
|
|
return !BoneShip.IsBoneShip(__instance);
|
|
}
|
|
}
|
|
|
|
/// <summary>Makes the piece buildable without having to craft its parts first, like the bone pile.</summary>
|
|
[HarmonyPatch(typeof(Player), "OnSpawned")]
|
|
public static class BoneShipKnownPatch
|
|
{
|
|
public static void Postfix(Player __instance)
|
|
{
|
|
if (__instance != Player.m_localPlayer) return;
|
|
BoneShip.EnsureKnown(__instance);
|
|
}
|
|
}
|
|
}
|