RacesOfValheim/JotunnModStub/TrolsHammer/TrolsHammerItem.cs
kanym 0d1f655598 feat: Shrink Hammer, and Alt + scroll to size pieces with both hammers
A player asked to build below vanilla size for small details. The new
Shrink Hammer, craftable by any race, builds from 0.25 to 1; Trol's Hammer
keeps 1 to 3. With either hammer, Alt + scroll in build mode changes the
size in steps of 0.05, an Alt: Size row joins the build hints, and the size
floats over the ghost when it is not the default. With the repair tool, the
aimed piece shows the size it was built at. Both hammers get icons of their
own.
2026-10-30 22:49:27 +01:00

98 lines
4.4 KiB
C#

using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Trol's Hammer: the builder's hammer, one size up.
///
/// It is a straight clone of the vanilla Hammer and it deliberately keeps the ONE field that
/// makes it a hammer at all - m_buildPieces, the PieceTable. Sharing the vanilla table rather
/// than cloning it is the whole trick behind "exactly the same things to build": every piece
/// vanilla puts in the hammer's menu is there, at the same cost and with the same drop, and so
/// is every piece another mod adds to that table later. A cloned table would have been a
/// snapshot of the build menu taken at startup and would have quietly gone stale.
///
/// Nothing about the piece prefabs is touched, so the size does not live on the item: it lives
/// on the placement, and TrolsHammerPatches is where that happens.
///
/// The recipe (1 core wood, 2 fine wood) is on a workbench rather than free-hand like the
/// vanilla hammer, because both materials already require one to exist.
/// </summary>
public static class TrolsHammerItem
{
public const string PrefabName = "TrolsHammer";
public const string DisplayName = "$rov_item_trolshammer_name";
private const string SourcePrefab = "Hammer";
private const string Label = "TrolsHammer";
/// <summary>Core wood. The item id is RoundLog - a display name that does not predict the id.</summary>
private const string CoreWood = "RoundLog";
private const string FineWood = "FineWood";
public static void Register()
{
GameObject prefab = PrefabManager.Instance.CreateClonedPrefab(PrefabName, SourcePrefab);
if (prefab == null)
{
Jotunn.Logger.LogWarning($"[{Label}] Could not clone {SourcePrefab}; {DisplayName} not registered.");
return;
}
ItemDrop drop = prefab.GetComponent<ItemDrop>();
if (drop == null || drop.m_itemData == null || drop.m_itemData.m_shared == null)
{
Jotunn.Logger.LogWarning($"[{Label}] {SourcePrefab} clone has no usable ItemDrop; {DisplayName} not registered.");
return;
}
ItemDrop.ItemData.SharedData shared = drop.m_itemData.m_shared;
shared.m_name = DisplayName;
shared.m_description = "$rov_item_trolshammer_desc";
// A blue-steel head and a troll's trophy, so it cannot pass for the vanilla hammer.
Sprite icon = HammerIcon.Load("TrolsHammer_icon.png");
if (icon != null) shared.m_icons = new[] { icon };
if (shared.m_buildPieces == null)
{
// Not fatal, but worth saying out loud: without a piece table the item is a stick.
Jotunn.Logger.LogWarning($"[{Label}] The {SourcePrefab} clone carries no build piece table; " +
$"{DisplayName} will open an empty build menu.");
}
ItemManager.Instance.AddItem(new CustomItem(prefab, fixReference: false));
ItemManager.Instance.AddRecipe(new CustomRecipe(new RecipeConfig
{
Item = PrefabName,
Amount = 1,
CraftingStation = CraftingStations.Workbench,
MinStationLevel = 1,
Requirements = new[]
{
new RequirementConfig(CoreWood, 1, 0, true),
new RequirementConfig(FineWood, 2, 0, true),
},
}));
Jotunn.Logger.LogInfo($"[{Label}] {DisplayName} registered on the vanilla hammer's piece table.");
}
/// <summary>
/// Whether an item in hand is this hammer.
///
/// m_dropPrefab is the reliable identity - it is the prefab the ItemData came from - but it
/// is null on an ItemData built before ObjectDB finished, so the display name is kept as a
/// second reading rather than as the only one.
/// </summary>
public static bool IsTrolsHammer(ItemDrop.ItemData item)
{
if (item == null) return false;
if (item.m_dropPrefab != null) return item.m_dropPrefab.name == PrefabName;
return item.m_shared != null && item.m_shared.m_name == DisplayName;
}
}
}