Every message the mod puts in the middle or corner of the screen now reads its text through ModText.Get, and messages.json holds 191 texts in English and Italian. Values computed in code become $1, $2 in the text, so a translator can put them wherever the sentence needs them. Branching messages get one key per branch. A few messages travel through a field before they are shown: thralls' refusals and the building limits now hold a token or key there. Boss and form names inside messages use tokens too, the bosses the game's own, so "The Elder" arrives translated. ModText.Get now localizes each argument before inserting it. A token argument left for a later pass ran into the text after it: "$1's power" became the unknown token "enemy_eikthyr's". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
301 lines
12 KiB
C#
301 lines
12 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
/// <summary>
|
|
/// The Blood Refinery's logic. Modelled on <see cref="Smelter"/> - same one-second tick, same
|
|
/// owner-only accounting with everything in the ZDO so it keeps working in multiplayer and
|
|
/// keeps ticking while the zone is unloaded - but with two numeric tanks instead of an ore
|
|
/// queue and a single fuel item.
|
|
///
|
|
/// Raw flesh is destroyed on the spot and becomes blood; bone is destroyed on the spot and
|
|
/// becomes bone dust. When both tanks can pay, the machine refines a Blood Essence and spits
|
|
/// it out of the vanilla output point. It never deals damage - see AswidBloodRefinery for the
|
|
/// strip pass that guarantees that.
|
|
/// </summary>
|
|
public class BloodRefineryBehaviour : MonoBehaviour
|
|
{
|
|
// Wired up from the vanilla Smelter before it is destroyed, which is what keeps the
|
|
// animation and the VFX identical to the eitr refinery's.
|
|
public Switch m_addMeatSwitch;
|
|
public Switch m_addBoneSwitch;
|
|
public Transform m_outputPoint;
|
|
public GameObject m_enabledObject;
|
|
public GameObject m_disabledObject;
|
|
public GameObject m_haveMeatObject;
|
|
public GameObject m_haveBoneObject;
|
|
public GameObject m_noMeatObject;
|
|
public Animator[] m_animators;
|
|
public EffectList m_meatAddedEffects;
|
|
public EffectList m_boneAddedEffects;
|
|
public EffectList m_produceEffects;
|
|
public SmokeSpawner m_smokeSpawner;
|
|
|
|
// Balance. Ten units of blood is roughly one cooked lox steak's worth.
|
|
public float m_maxBlood = 50f;
|
|
public float m_maxBone = 20f;
|
|
public float m_bloodPerEssence = 5f;
|
|
public float m_bonePerEssence = 2f;
|
|
public float m_secPerEssence = 20f;
|
|
|
|
private const string KeyBlood = "RoV_blood";
|
|
private const string KeyBone = "RoV_bone";
|
|
private const string KeyTimer = "RoV_refineTimer";
|
|
private const string KeyAccumulator = "RoV_accTime";
|
|
private const string KeyStartTime = "RoV_startTime";
|
|
|
|
private ZNetView m_nview;
|
|
private bool m_blockedSmoke;
|
|
|
|
private void Awake()
|
|
{
|
|
m_nview = GetComponent<ZNetView>();
|
|
if (m_nview == null) m_nview = GetComponentInParent<ZNetView>();
|
|
if (m_nview == null || m_nview.GetZDO() == null) return;
|
|
|
|
if (m_addMeatSwitch != null)
|
|
{
|
|
m_addMeatSwitch.m_onUse = OnAddMeat;
|
|
m_addMeatSwitch.m_onHover = HoverMeat;
|
|
}
|
|
if (m_addBoneSwitch != null)
|
|
{
|
|
m_addBoneSwitch.m_onUse = OnAddBone;
|
|
m_addBoneSwitch.m_onHover = HoverBone;
|
|
}
|
|
|
|
m_nview.Register<string>("RoV_AddMeat", RPC_AddMeat);
|
|
m_nview.Register<string>("RoV_AddBone", RPC_AddBone);
|
|
|
|
WearNTear wear = GetComponent<WearNTear>();
|
|
if (wear == null) wear = GetComponentInParent<WearNTear>();
|
|
if (wear != null) wear.m_onDestroyed = (Action)Delegate.Combine(wear.m_onDestroyed, new Action(OnDestroyed));
|
|
|
|
InvokeRepeating("UpdateRefinery", 1f, 1f);
|
|
}
|
|
|
|
// ---------------------------------------------------------------- input
|
|
|
|
private bool OnAddMeat(Switch caller, Humanoid user, ItemDrop.ItemData item)
|
|
{
|
|
return TryAdd(user, item, isMeat: true) || item != null;
|
|
}
|
|
|
|
private bool OnAddBone(Switch caller, Humanoid user, ItemDrop.ItemData item)
|
|
{
|
|
return TryAdd(user, item, isMeat: false) || item != null;
|
|
}
|
|
|
|
// Both switches treat an item in hand as handled whatever TryAdd says: false makes
|
|
// Humanoid.UseItem cover our own message with the generic "$msg_cantuseon".
|
|
private bool TryAdd(Humanoid user, ItemDrop.ItemData item, bool isMeat)
|
|
{
|
|
if (user == null) return false;
|
|
Inventory inventory = user.GetInventory();
|
|
if (inventory == null) return false;
|
|
|
|
if (item == null) item = FindUsable(inventory, isMeat);
|
|
if (item == null)
|
|
{
|
|
user.Message(MessageHud.MessageType.Center, (isMeat ? ModText.Get("rov_msg_aswid_no_raw_flesh_render") : ModText.Get("rov_msg_aswid_no_bone_grind")));
|
|
return false;
|
|
}
|
|
|
|
string prefabName = item.m_dropPrefab != null ? item.m_dropPrefab.name : null;
|
|
int value = isMeat ? AswidBloodRefinery.BloodValue(prefabName) : AswidBloodRefinery.BoneValue(prefabName);
|
|
if (value <= 0)
|
|
{
|
|
user.Message(MessageHud.MessageType.Center, "$msg_wontwork");
|
|
return false;
|
|
}
|
|
|
|
float stored = isMeat ? GetBlood() : GetBone();
|
|
float capacity = isMeat ? m_maxBlood : m_maxBone;
|
|
if (stored >= capacity)
|
|
{
|
|
user.Message(MessageHud.MessageType.Center, "$msg_itsfull");
|
|
return false;
|
|
}
|
|
|
|
user.Message(MessageHud.MessageType.Center, "$msg_added " + item.m_shared.m_name);
|
|
inventory.RemoveItem(item, 1);
|
|
m_nview.InvokeRPC(isMeat ? "RoV_AddMeat" : "RoV_AddBone", prefabName);
|
|
return true;
|
|
}
|
|
|
|
private ItemDrop.ItemData FindUsable(Inventory inventory, bool isMeat)
|
|
{
|
|
foreach (ItemDrop.ItemData candidate in inventory.GetAllItems())
|
|
{
|
|
if (candidate == null || candidate.m_dropPrefab == null) continue;
|
|
string prefabName = candidate.m_dropPrefab.name;
|
|
int value = isMeat ? AswidBloodRefinery.BloodValue(prefabName) : AswidBloodRefinery.BoneValue(prefabName);
|
|
if (value > 0) return candidate;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private void RPC_AddMeat(long sender, string prefabName)
|
|
{
|
|
if (!m_nview.IsOwner()) return;
|
|
|
|
int value = AswidBloodRefinery.BloodValue(prefabName);
|
|
if (value <= 0) return;
|
|
|
|
SetBlood(Mathf.Min(m_maxBlood, GetBlood() + value));
|
|
if (m_meatAddedEffects != null) m_meatAddedEffects.Create(transform.position, transform.rotation);
|
|
}
|
|
|
|
private void RPC_AddBone(long sender, string prefabName)
|
|
{
|
|
if (!m_nview.IsOwner()) return;
|
|
|
|
int value = AswidBloodRefinery.BoneValue(prefabName);
|
|
if (value <= 0) return;
|
|
|
|
SetBone(Mathf.Min(m_maxBone, GetBone() + value));
|
|
if (m_boneAddedEffects != null) m_boneAddedEffects.Create(transform.position, transform.rotation, transform);
|
|
}
|
|
|
|
// ---------------------------------------------------------------- the loop
|
|
|
|
private void UpdateRefinery()
|
|
{
|
|
if (m_nview == null || !m_nview.IsValid()) return;
|
|
|
|
m_blockedSmoke = m_smokeSpawner != null && m_smokeSpawner.IsBlocked();
|
|
UpdateState();
|
|
|
|
if (!m_nview.IsOwner()) return;
|
|
|
|
float accumulator = m_nview.GetZDO().GetFloat(KeyAccumulator) + (float)GetDeltaTime();
|
|
if (accumulator > 3600f) accumulator = 3600f; // one hour of catch-up, as vanilla
|
|
|
|
while (accumulator >= 1f)
|
|
{
|
|
accumulator -= 1f;
|
|
if (!IsActive()) continue;
|
|
|
|
float timer = m_nview.GetZDO().GetFloat(KeyTimer) + 1f;
|
|
if (timer >= m_secPerEssence)
|
|
{
|
|
timer = 0f;
|
|
SetBlood(GetBlood() - m_bloodPerEssence);
|
|
SetBone(GetBone() - m_bonePerEssence);
|
|
Produce();
|
|
}
|
|
m_nview.GetZDO().Set(KeyTimer, timer);
|
|
}
|
|
|
|
m_nview.GetZDO().Set(KeyAccumulator, accumulator);
|
|
}
|
|
|
|
private void Produce()
|
|
{
|
|
GameObject prefab = ObjectDB.instance != null
|
|
? ObjectDB.instance.GetItemPrefab(AswidBloodRefinery.EssenceName)
|
|
: null;
|
|
if (prefab == null || m_outputPoint == null) return;
|
|
|
|
if (m_produceEffects != null) m_produceEffects.Create(transform.position, transform.rotation);
|
|
|
|
GameObject spawned = Instantiate(prefab, m_outputPoint.position, m_outputPoint.rotation);
|
|
ItemDrop drop = spawned.GetComponent<ItemDrop>();
|
|
if (drop != null)
|
|
{
|
|
drop.m_itemData.m_stack = 1;
|
|
ItemDrop.OnCreateNew(drop);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bones survive the machine, blood does not, so a demolished refinery gives the bone back
|
|
/// and keeps the blood. The hover text says so before anyone swings a hammer at it.
|
|
/// </summary>
|
|
private void OnDestroyed()
|
|
{
|
|
if (m_nview == null || !m_nview.IsOwner()) return;
|
|
|
|
int bones = Mathf.FloorToInt(GetBone());
|
|
SetBone(0f);
|
|
SetBlood(0f);
|
|
if (bones <= 0) return;
|
|
|
|
GameObject prefab = ObjectDB.instance != null ? ObjectDB.instance.GetItemPrefab("BoneFragments") : null;
|
|
if (prefab == null) return;
|
|
|
|
for (int i = 0; i < bones; i++)
|
|
{
|
|
Vector3 position = transform.position + Vector3.up * 0.5f + UnityEngine.Random.insideUnitSphere * 0.3f;
|
|
Quaternion rotation = Quaternion.Euler(0f, UnityEngine.Random.Range(0, 360), 0f);
|
|
ItemDrop.OnCreateNew(Instantiate(prefab, position, rotation));
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------- state and display
|
|
|
|
public bool IsActive()
|
|
{
|
|
if (m_blockedSmoke) return false;
|
|
return GetBlood() >= m_bloodPerEssence && GetBone() >= m_bonePerEssence;
|
|
}
|
|
|
|
private void UpdateState()
|
|
{
|
|
bool active = IsActive();
|
|
|
|
if (m_enabledObject != null) m_enabledObject.SetActive(active);
|
|
if (m_disabledObject != null) m_disabledObject.SetActive(!active);
|
|
if (m_haveBoneObject != null) m_haveBoneObject.SetActive(GetBone() > 0f);
|
|
if (m_haveMeatObject != null) m_haveMeatObject.SetActive(GetBlood() > 0f);
|
|
if (m_noMeatObject != null) m_noMeatObject.SetActive(GetBlood() <= 0f);
|
|
|
|
if (m_animators == null) return;
|
|
foreach (Animator animator in m_animators)
|
|
{
|
|
if (animator == null || !animator.gameObject.activeInHierarchy) continue;
|
|
animator.SetBool("active", active);
|
|
animator.SetFloat("activef", active ? 1f : 0f);
|
|
}
|
|
}
|
|
|
|
private string HoverMeat()
|
|
{
|
|
return Localization.instance.Localize(
|
|
$"{AswidBloodRefinery.PieceDisplayName} (blood {Mathf.Floor(GetBlood())}/{m_maxBlood})\n" +
|
|
"[<color=yellow><b>$KEY_Use</b></color>] Render raw flesh");
|
|
}
|
|
|
|
private string HoverBone()
|
|
{
|
|
return Localization.instance.Localize(
|
|
$"{AswidBloodRefinery.PieceDisplayName} (bone {Mathf.Floor(GetBone())}/{m_maxBone})\n" +
|
|
"[<color=yellow><b>$KEY_Use</b></color>] Grind bone");
|
|
}
|
|
|
|
// ---------------------------------------------------------------- ZDO accessors
|
|
|
|
private double GetDeltaTime()
|
|
{
|
|
DateTime now = ZNet.instance.GetTime();
|
|
DateTime last = new DateTime(m_nview.GetZDO().GetLong(KeyStartTime, now.Ticks));
|
|
m_nview.GetZDO().Set(KeyStartTime, now.Ticks);
|
|
return (now - last).TotalSeconds;
|
|
}
|
|
|
|
private float GetBlood() => m_nview != null && m_nview.IsValid() ? m_nview.GetZDO().GetFloat(KeyBlood) : 0f;
|
|
|
|
private float GetBone() => m_nview != null && m_nview.IsValid() ? m_nview.GetZDO().GetFloat(KeyBone) : 0f;
|
|
|
|
private void SetBlood(float value)
|
|
{
|
|
if (m_nview != null && m_nview.IsValid()) m_nview.GetZDO().Set(KeyBlood, Mathf.Max(0f, value));
|
|
}
|
|
|
|
private void SetBone(float value)
|
|
{
|
|
if (m_nview != null && m_nview.IsValid()) m_nview.GetZDO().Set(KeyBone, Mathf.Max(0f, value));
|
|
}
|
|
}
|
|
}
|