Skip to content

Commit

Permalink
Merge pull request #17 from sirskunkalot/feature/guard-plan-totem
Browse files Browse the repository at this point in the history
Feature/guard plan totem
  • Loading branch information
MathiasDecrock authored Oct 14, 2021
2 parents 7ba2699 + 184dffc commit 54b2308
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 92 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Version 0.7.0
* Plan Totems will now replace broken pieces in range with plans
* Added enable/disable to individual Plan Totems (both building & replacing with plans are controlled by this)

# Version 0.6.14
* Fix stuck square tool outline

Expand Down
12 changes: 6 additions & 6 deletions PlanBuild/ModCompat/PatcherValheimRaft.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ private PatcherValheimRaft()
{
}

[HarmonyPatch(typeof(PlanPiece), "CalculateSupported")]
[HarmonyPatch(typeof(PlanPiece), nameof(PlanPiece.CalculateSupported))]
[HarmonyPrefix]
private static bool PlanPiece_CalculateSupported_Prefix(PlanPiece __instance, ref bool __result)
{
Expand All @@ -24,18 +24,18 @@ private static bool PlanPiece_CalculateSupported_Prefix(PlanPiece __instance, re
return true;
}

[HarmonyPatch(typeof(PlanPiece), "OnPiecePlaced")]
[HarmonyPatch(typeof(PlanPiece), nameof(PlanPiece.OnPieceReplaced))]
[HarmonyPrefix]
private static void PlanPiece_OnPiecePlaced_Postfix(PlanPiece __instance, GameObject actualPiece)
private static void PlanPiece_OnPieceReplaced_Postfix(GameObject originatingPiece, GameObject placedPiece)
{
MoveableBaseRootComponent moveableBaseRoot = __instance.GetComponentInParent<MoveableBaseRootComponent>();
MoveableBaseRootComponent moveableBaseRoot = originatingPiece.GetComponentInParent<MoveableBaseRootComponent>();
if (moveableBaseRoot)
{
moveableBaseRoot.AddNewPiece(actualPiece.GetComponent<Piece>());
moveableBaseRoot.AddNewPiece(placedPiece.GetComponent<Piece>());
}
}

[HarmonyPatch(typeof(BlueprintComponent), "OnPiecePlaced")]
[HarmonyPatch(typeof(BlueprintComponent), nameof(BlueprintComponent.OnPiecePlaced))]
[HarmonyPrefix]
private static void BlueprintManager_OnPiecePlaced_Postfix(GameObject placedPiece)
{
Expand Down
10 changes: 10 additions & 0 deletions PlanBuild/Plans/PlanDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ internal bool FindByPieceName(string m_name, out List<Piece> originalPieces)
return NamePiecePrefabMapping.TryGetValue(m_name, out originalPieces);
}

internal bool FindPlanByPrefabName(string name, out PlanPiecePrefab planPiecePrefab)
{
int index = name.IndexOf("(Clone)");
if (index != -1)
{
name = name.Substring(0, index);
}
return PlanPiecePrefabs.TryGetValue(name, out planPiecePrefab);
}

public bool CanCreatePlan(Piece piece)
{
return piece.m_enabled
Expand Down
25 changes: 25 additions & 0 deletions PlanBuild/Plans/PlanManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,31 @@ internal void Init()
On.Player.HaveRequirements_Piece_RequirementMode += OnHaveRequirements;
On.Player.SetupPlacementGhost += OnSetupPlacementGhost;
On.WearNTear.Highlight += OnHighlight;
On.WearNTear.Destroy += OnDestroy;
}

private void OnDestroy(On.WearNTear.orig_Destroy orig, WearNTear wearNTear)
{
//Check if actually destoyed, not removed by middle clicking with Hammer
if (wearNTear.m_nview && wearNTear.m_nview.IsOwner()
&& wearNTear.GetHealthPercentage() <= 0f
&& PlanDB.Instance.FindPlanByPrefabName(wearNTear.name, out PlanPiecePrefab planPrefab))
{
foreach (PlanTotem planTotem in PlanTotem.m_allPlanTotems)
{
if (!planTotem.GetEnabled())
{
continue;
}
UnityEngine.GameObject gameObject = wearNTear.gameObject;
if (planTotem.InRange(gameObject))
{
planTotem.Replace(gameObject, planPrefab);
break;
}
}
}
orig(wearNTear);
}

private void CreatePlanTable()
Expand Down
55 changes: 22 additions & 33 deletions PlanBuild/Plans/PlanPiece.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using PlanBuild.Utils;
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using static Piece;
Expand All @@ -23,8 +22,7 @@ public class PlanPiece : MonoBehaviour, Interactable, Hoverable
private Piece m_piece;
private ZNetView m_nView;
internal WearNTear m_wearNTear;

public string m_hoverText = "";

public Piece originalPiece;

//GUI
Expand Down Expand Up @@ -66,8 +64,8 @@ public void Awake()
m_nView.Register<bool>("Refund", RPC_Refund);
m_nView.Register<string, int>("AddResource", RPC_AddResource);
m_nView.Register<long>("SpawnPieceAndDestroy", RPC_SpawnPieceAndDestroy);
UpdateHoverText();
}


private void OnDestroyed()
{
Expand All @@ -93,7 +91,7 @@ private void RPC_Refund(long sender, bool all)

private bool hasSupport = false;

private bool CalculateSupported()
internal bool CalculateSupported()
{
return m_nView.GetZDO().GetFloat("support") >= m_minSupport;
}
Expand Down Expand Up @@ -241,14 +239,13 @@ public string GetHoverName()
private float m_lastUseTime = -9999f;
private readonly float m_holdRepeatInterval = 1f;
private float m_minSupport = 0f;
public float m_maxSupport;
internal float m_maxSupport;

public string GetHoverText()
{
if (Time.time - m_lastLookedTime > 0.2f)
{
m_lastLookedTime = Time.time;
// Debug.Log("Setting up piece info");
SetupPieceInfo(originalPiece);
}
Hud.instance.m_buildHud.SetActive(true);
Expand Down Expand Up @@ -411,7 +408,6 @@ public bool Interact(Humanoid user, bool hold, bool alt)
{
m_nView.InvokeRPC("AddResource", resourceName, 1);
user.GetInventory().RemoveItem(resourceName, 1);
UpdateHoverText();
return true;
}
}
Expand Down Expand Up @@ -479,7 +475,6 @@ public bool AddAllMaterials(Inventory inventory)
{
m_nView.InvokeRPC("AddResource", resourceName, amountToAdd);
inventory.RemoveItem(resourceName, amountToAdd);
UpdateHoverText();
added = true;
reqFinished = remaining == amountToAdd;
}
Expand Down Expand Up @@ -536,7 +531,6 @@ public bool UseItem(Humanoid user, ItemDrop.ItemData item)
{
m_nView.InvokeRPC("AddResource", resourceName, 1);
PlayerRemoveResource(user, resourceName, 1);
UpdateHoverText();
return true;
}
}
Expand Down Expand Up @@ -614,24 +608,24 @@ private int GetResourceCount(string resource)
return m_nView.GetZDO().GetInt(zdoPlanResource + "_" + resource);
}

public void UpdateHoverText()
{
StringBuilder builder = new StringBuilder();
foreach (Requirement requirement in originalPiece.m_resources)
{
builder.Append(requirement.m_resItem.m_itemData.m_shared.m_name + ": " + GetResourceCount(requirement.m_resItem.m_itemData.m_shared.m_name) + "/" + requirement.m_amount + "\n");
}
m_hoverText = builder.ToString();
}

private void RPC_SpawnPieceAndDestroy(long sender, long creatorID)
{
if (!m_nView.IsOwner())
{
return;
}
GameObject actualPiece = Object.Instantiate(originalPiece.gameObject, gameObject.transform.position, gameObject.transform.rotation);
OnPiecePlaced(actualPiece);
GameObject actualPiece = SpawnPiece(gameObject, creatorID, transform.position, transform.rotation, originalPiece.gameObject, m_nView.GetZDO().GetString(zdoAdditionalInfo));
#if DEBUG
Jotunn.Logger.LogDebug("Plan spawn actual piece: " + actualPiece + " -> Destroying self");
#endif
BlueprintManager.Instance.PlanPieceRemovedFromBlueprint(this);
ZNetScene.instance.Destroy(this.gameObject);
}

internal static GameObject SpawnPiece(GameObject originatingObject, long creatorID, Vector3 position, Quaternion rotation, GameObject prefab, string textReceiverInput)
{
GameObject actualPiece = Object.Instantiate(prefab, position, rotation);
OnPieceReplaced(originatingObject, actualPiece);
// Register special effects
if (creatorID == Player.m_localPlayer?.GetPlayerID())
{
Expand All @@ -653,28 +647,23 @@ private void RPC_SpawnPieceAndDestroy(long sender, long creatorID)
// Count up player builds
Game.instance.GetPlayerProfile().m_playerStats.m_builds++;
}
WearNTear wearntear = gameObject.GetComponent<WearNTear>();
WearNTear wearntear = actualPiece.GetComponent<WearNTear>();
if (wearntear)
{
wearntear.OnPlaced();
}
TextReceiver textReceiver = gameObject.GetComponent<TextReceiver>();
TextReceiver textReceiver = actualPiece.GetComponent<TextReceiver>();
if (textReceiver != null)
{
textReceiver.SetText(m_nView.GetZDO().GetString(zdoAdditionalInfo));
textReceiver.SetText(textReceiverInput);
}

actualPiece.GetComponent<Piece>().SetCreator(creatorID);

#if DEBUG
Jotunn.Logger.LogDebug("Plan spawn actual piece: " + actualPiece + " -> Destroying self");
#endif
BlueprintManager.Instance.PlanPieceRemovedFromBlueprint(this);
ZNetScene.instance.Destroy(this.gameObject);
return actualPiece;
}

internal virtual void OnPiecePlaced(GameObject actualPiece)
internal static void OnPieceReplaced(GameObject originatingPiece, GameObject placedPiece)
{

}

[HarmonyPatch(typeof(WearNTear), "Damage")]
Expand Down
Loading

0 comments on commit 54b2308

Please sign in to comment.