237 lines
12 KiB
C#
237 lines
12 KiB
C#
using HarmonyLib;
|
|
using Jotunn.Configs;
|
|
using Jotunn.Entities;
|
|
using Jotunn.Managers;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
/// <summary>
|
|
/// Tier 2 of the Trenta's item chain: plant it, wait, and the tree that comes up fertilises what
|
|
/// surrounds it, stings what approaches it, and fills a chest with honey. Three functions that do
|
|
/// not depend on each other, on one piece. Design doc: Trenta_Composter.txt section 11.
|
|
///
|
|
/// ONE piece, not two. The doc called for a sapling carrying a vanilla Plant whose m_grownPrefabs
|
|
/// pointed at a separate grown prefab; this grows in place instead - same crate, same ZNetView,
|
|
/// with the young model swapped for the grown one when the clock runs out. Three risks went away
|
|
/// with that decision and nothing was lost:
|
|
/// - no second prefab to register outside a piece table, and no question about whether Jotunn
|
|
/// has put it in ZNetScene by the time a Plant tries to instantiate it;
|
|
/// - no dependency on Beech_Sapling actually carrying a Plant, which was the one link in the
|
|
/// tier-1 design nobody had opened yet;
|
|
/// - the honey chest and the Fertilizer trough survive the transition, because there is no
|
|
/// transition - nothing is destroyed and re-spawned, so nothing has to be carried over.
|
|
/// The growth timer is the same shape as the Composter's: one timestamp on ZNet's world clock, so
|
|
/// it advances with the zone unloaded and across a restart.
|
|
///
|
|
/// The layout is forced by a single fact worth stating plainly: there is one ZNetView per
|
|
/// instantiated prefab, and Container keeps its inventory in the ZDO. So there can be exactly one
|
|
/// real chest, and it is the honey one. The Fertilizer goes in through a Switch on the plant
|
|
/// itself - E on the crate opens the honey, E on the tree feeds it - which is the same division
|
|
/// of labour the vanilla fermenter uses between its root component and its child switches.
|
|
/// </summary>
|
|
public static class TrentaHeartwoodGrove
|
|
{
|
|
public static void Register()
|
|
{
|
|
RegisterSeed();
|
|
RegisterPiece();
|
|
|
|
PieceDensityLimit.Register(TrentaConfig.GrovePieceName,
|
|
PieceDensityLimit.RadiusForArea(TrentaConfig.GroveDensityAreaKm2), TrentaConfig.GroveMaxInRange,
|
|
"This country already answers to {0} of these. Plant it somewhere else.");
|
|
}
|
|
|
|
// ---------------------------------------------------------------- the seed
|
|
|
|
private static void RegisterSeed()
|
|
{
|
|
GameObject prefab = PrefabManager.Instance.CreateClonedPrefab(TrentaConfig.GroveSeedName, "AncientSeed");
|
|
if (prefab == null)
|
|
{
|
|
Jotunn.Logger.LogWarning("[Trenta] Could not clone AncientSeed; Heartwood Seed not registered.");
|
|
return;
|
|
}
|
|
|
|
ItemDrop drop = prefab.GetComponent<ItemDrop>();
|
|
if (drop == null) return;
|
|
|
|
ItemDrop.ItemData.SharedData shared = drop.m_itemData.m_shared;
|
|
shared.m_name = TrentaConfig.GroveSeedDisplayName;
|
|
shared.m_description = "A seed fed on a whole barrel of rot. Plant it and go away for a while.";
|
|
shared.m_itemType = ItemDrop.ItemData.ItemType.Material;
|
|
shared.m_weight = 1f;
|
|
shared.m_maxStackSize = 10;
|
|
|
|
ItemManager.Instance.AddItem(new CustomItem(prefab, fixReference: false));
|
|
|
|
// One grove costs exactly one full Composter batch - 100 units of flesh and forty
|
|
// minutes. That is the whole balance argument, and it is why the cost is expressed in
|
|
// Fertilizer rather than in some second currency.
|
|
ItemManager.Instance.AddRecipe(new CustomRecipe(new RecipeConfig
|
|
{
|
|
Item = TrentaConfig.GroveSeedName,
|
|
Amount = 1,
|
|
CraftingStation = "piece_preptable",
|
|
Requirements = new[]
|
|
{
|
|
new RequirementConfig(TrentaConfig.FertilizerName, TrentaConfig.GroveSeedFertilizerCost),
|
|
new RequirementConfig(TrentaConfig.RotSeedName, 1),
|
|
new RequirementConfig("YggdrasilWood", 10),
|
|
}
|
|
}));
|
|
}
|
|
|
|
// ---------------------------------------------------------------- the piece
|
|
|
|
private static void RegisterPiece()
|
|
{
|
|
// A wooden chest is the base because it arrives with everything a honey store needs
|
|
// already wired: ZNetView, Piece, WearNTear, colliders, and a Container whose lid
|
|
// objects and effect lists are assigned. Building one from scratch would mean assigning
|
|
// all of that by hand and finding out in game which field Container.Awake dereferences
|
|
// without checking.
|
|
GameObject prefab = PrefabManager.Instance.CreateClonedPrefab(TrentaConfig.GrovePieceName, "piece_chest_wood");
|
|
if (prefab == null)
|
|
{
|
|
Jotunn.Logger.LogWarning("[Trenta] Could not clone piece_chest_wood; Heartwood Grove not registered.");
|
|
return;
|
|
}
|
|
|
|
Container container = prefab.GetComponent<Container>();
|
|
if (container == null)
|
|
{
|
|
Jotunn.Logger.LogWarning("[Trenta] piece_chest_wood clone has no Container; Heartwood Grove not registered.");
|
|
return;
|
|
}
|
|
|
|
container.m_name = TrentaConfig.GroveDisplayName;
|
|
|
|
GroveBehaviour grove = prefab.AddComponent<GroveBehaviour>();
|
|
grove.m_container = container;
|
|
grove.m_youngModel = BuildModel(prefab.transform, "GroveYoung", TrentaConfig.GroveSaplingSourcePrefab, TrentaConfig.GroveModelScale);
|
|
grove.m_grownModel = BuildModel(prefab.transform, "GroveGrown", TrentaConfig.GroveModelPrefab, TrentaConfig.GroveModelScale);
|
|
|
|
// The plant is the second interaction point. It carries its own collider so the hover
|
|
// raycast resolves to the Switch on it rather than walking up to the Container on the
|
|
// root - which is how the crate keeps meaning "open the honey" while the tree means
|
|
// "feed me". Sized generously: it has to be clickable at both growth stages.
|
|
grove.m_switch = BuildTroughSwitch(grove.m_grownModel, grove.m_youngModel);
|
|
|
|
Piece piece = prefab.GetComponent<Piece>();
|
|
if (piece != null)
|
|
{
|
|
piece.m_craftingStation = null;
|
|
piece.m_resources = new Piece.Requirement[0];
|
|
piece.m_name = TrentaConfig.GroveDisplayName;
|
|
}
|
|
|
|
PieceConfig config = new PieceConfig
|
|
{
|
|
Name = TrentaConfig.GroveDisplayName,
|
|
Description = "A crate of rot with something growing out of it. Give it time.",
|
|
PieceTable = PieceTables.Hammer,
|
|
Category = PieceCategories.Misc,
|
|
Requirements = new[]
|
|
{
|
|
new RequirementConfig(TrentaConfig.GroveSeedName, 1, 0, true),
|
|
}
|
|
};
|
|
|
|
PieceManager.Instance.AddPiece(new CustomPiece(prefab, fixReference: false, config));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Borrows the visible half of a world prefab and nothing else.
|
|
///
|
|
/// The same discipline the VFX borrowing follows, and here it matters more: the Yggdrasil
|
|
/// shoot is a TreeBase, so taken whole it would bring a choppable trunk, a log drop and a
|
|
/// tree collider along with the mesh - a grove that can be felled for wood instead of being
|
|
/// destroyed as a piece. Only Renderers and their transforms survive; every component that
|
|
/// is not a Renderer, and every collider, is stripped.
|
|
/// </summary>
|
|
private static GameObject BuildModel(Transform root, string name, string sourcePrefabName, float scale)
|
|
{
|
|
GameObject holder = new GameObject(name);
|
|
holder.transform.SetParent(root, false);
|
|
holder.transform.localPosition = Vector3.zero;
|
|
|
|
GameObject source = PrefabManager.Cache.GetPrefab<GameObject>(sourcePrefabName);
|
|
if (source == null)
|
|
{
|
|
Jotunn.Logger.LogWarning($"[Trenta] Heartwood Grove: could not find model prefab '{sourcePrefabName}'.");
|
|
return holder;
|
|
}
|
|
|
|
GameObject copy = Object.Instantiate(source, holder.transform);
|
|
copy.name = "model";
|
|
copy.transform.localPosition = Vector3.zero;
|
|
copy.transform.localScale = Vector3.one * scale;
|
|
|
|
foreach (MonoBehaviour behaviour in copy.GetComponentsInChildren<MonoBehaviour>(true))
|
|
{
|
|
if (behaviour != null) Object.DestroyImmediate(behaviour);
|
|
}
|
|
foreach (Collider collider in copy.GetComponentsInChildren<Collider>(true))
|
|
{
|
|
if (collider != null) Object.DestroyImmediate(collider);
|
|
}
|
|
foreach (ZNetView nview in copy.GetComponentsInChildren<ZNetView>(true))
|
|
{
|
|
if (nview != null) Object.DestroyImmediate(nview);
|
|
}
|
|
|
|
return holder;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The Fertilizer input: a Switch with a collider of its own, hung off the grown model but
|
|
/// parented so it stays live at both stages (the models get toggled, this does not).
|
|
/// </summary>
|
|
private static Switch BuildTroughSwitch(GameObject grownModel, GameObject youngModel)
|
|
{
|
|
// Parented to the models' shared parent - the piece root - precisely so GroveBehaviour
|
|
// can switch the two models on and off without taking the interaction point with them.
|
|
Transform parent = grownModel != null ? grownModel.transform.parent : youngModel.transform.parent;
|
|
|
|
GameObject holder = new GameObject("GroveTrough");
|
|
holder.transform.SetParent(parent, false);
|
|
holder.transform.localPosition = new Vector3(0f, 1.2f, 0f);
|
|
|
|
// Two decisions here that both come down to "will the hover raycast find this at all",
|
|
// which is the one thing about this piece that cannot be settled by reading code:
|
|
// - the layer is copied from the piece root rather than left on Default, because
|
|
// Player.FindHoverObject raycasts against a mask and a brand new GameObject starts
|
|
// on whatever layer 0 happens to be;
|
|
// - it is NOT a trigger. Physics.queriesHitTriggers is a global whose value here is
|
|
// not ours, so a trigger collider might simply be invisible to that raycast. Solid
|
|
// also happens to be right: it is a tree trunk, and walking through it would be odd.
|
|
holder.layer = parent.gameObject.layer;
|
|
|
|
BoxCollider collider = holder.AddComponent<BoxCollider>();
|
|
collider.size = new Vector3(1.2f, 2.4f, 1.2f);
|
|
collider.isTrigger = false;
|
|
|
|
Switch trough = holder.AddComponent<Switch>();
|
|
trough.m_name = TrentaConfig.GroveDisplayName;
|
|
return trough;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Same reason as the Composter's: a piece registered after the vanilla learning pass never
|
|
/// reaches the hammer on its own. The Heartwood Seed requirement is the real gate.
|
|
/// </summary>
|
|
public static void EnsureKnown(Player player)
|
|
{
|
|
if (player == null) return;
|
|
|
|
GameObject prefab = PrefabManager.Instance.GetPrefab(TrentaConfig.GrovePieceName);
|
|
Piece piece = prefab != null ? prefab.GetComponent<Piece>() : null;
|
|
if (piece == null) return;
|
|
|
|
Traverse traverse = Traverse.Create(player);
|
|
traverse.Method("AddKnownPiece", piece).GetValue();
|
|
traverse.Method("UpdateAvailablePiecesList").GetValue();
|
|
}
|
|
}
|
|
}
|