Skip to content

Commit

Permalink
2.5.15: miners prioritize copper, silver, and tin over rocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jpw1991 committed Apr 5, 2023
1 parent 2adf2e5 commit 5738473
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
2 changes: 1 addition & 1 deletion ChebsNecromancy/BasePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class BasePlugin : BaseUnityPlugin
{
public const string PluginGuid = "com.chebgonaz.ChebsNecromancy";
public const string PluginName = "ChebsNecromancy";
public const string PluginVersion = "2.5.14";
public const string PluginVersion = "2.5.15";
private const string ConfigFileName = PluginGuid + ".cfg";
private static readonly string ConfigFileFullPath = Path.Combine(Paths.ConfigPath, ConfigFileName);

Expand Down
55 changes: 26 additions & 29 deletions ChebsNecromancy/Minions/AI/MinerAI.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Random = UnityEngine.Random;

namespace ChebsNecromancy.Minions.AI
{
Expand All @@ -16,7 +18,6 @@ internal class MinerAI : MonoBehaviour

private bool _inContact;
private float _lerpedValue;
//private float _timeFollowingObject;

private void Awake()
{
Expand All @@ -34,16 +35,29 @@ public void LookForMineableObjects()
// All rocks are in the static_solid layer and have a Destructible component with type Default.
// We can just match names as the rock names are pretty unique
var layerMask = 1 << LayerMask.NameToLayer("static_solid") | 1 << LayerMask.NameToLayer("Default_small");
var closest = UndeadMinion.FindClosest<Transform>(transform,
// get all nearby rocks/ores
var nearby = UndeadMinion.FindNearby<Transform>(transform,
SkeletonMinerMinion.LookRadius.Value,
layerMask,
Hittable,
false);

// assign a priority to the rocks/ores -> eg. copper takes precedence over simple rocks
var priorities = nearby
.Select(c => (c, c.name.Contains("_Tin")
|| c.name.Contains("silver")
|| c.name.Contains("copper")
? 1
: 2));

// Order the list of tuples by priority first, then by distance
var orderedPriorities = priorities.OrderBy(t => t.Item2)
.ThenBy(t => Vector3.Distance(transform.position, t.Item1.position));

// Get the first item from the ordered list
var closest = orderedPriorities.FirstOrDefault().ToTuple()?.Item1;
if (closest != null)
{
_monsterAI.SetFollowTarget(closest.gameObject);
//_timeFollowingObject = Time.time;
}
}

Expand All @@ -54,20 +68,11 @@ private void Update()
{
if (Vector3.Distance(transform.position, followTarget.transform.position) < 5f)
{
//var t = Mathf.PingPong(Time.time, 2f);
//_lerpedValue = Mathf.Lerp(1f, -1f, t);

transform.LookAt(followTarget.transform.position);// + Vector3.down * _lerpedValue);

}
//followTarget.GetComponent<Destructible>().Damage(new);

TryAttack();
}
// else
// {
// _timeFollowingObject = 0f;
// }

if (Time.time > nextCheck)
{
nextCheck = Time.time + SkeletonMinerMinion.UpdateDelay.Value
Expand All @@ -88,9 +93,6 @@ private void TryAttack()
{
var followTarget = _monsterAI.GetFollowTarget();
if (followTarget != null && _inContact)
//&& (_inContact // in contact with rock (physically touching it)
//|| _timeFollowingObject > 20f // or can't reach it after prolonged period (large rock fragments)
//))
{
_monsterAI.DoAttack(null, false);

Expand All @@ -100,7 +102,6 @@ private void TryAttack()
var hitData = new HitData();
hitData.m_damage.m_pickaxe = 500;
destructible.m_nview.InvokeRPC("Damage", hitData);
//destructible.Damage(hitData);
return;
}

Expand All @@ -111,7 +112,7 @@ private void TryAttack()
for (int i = 0; i < mineRock5.m_hitAreas.Count; i++)
{
var hitArea = mineRock5.m_hitAreas[i];
if (hitArea.m_health > 0f)// && !hitArea.m_supported)
if (hitArea.m_health > 0f)
{
var hitData = new HitData();
hitData.m_damage.m_damage = hitArea.m_health;
Expand All @@ -120,8 +121,6 @@ private void TryAttack()
mineRock5.DamageArea(i, hitData);
}
}
//mineRock5.Damage(hitData);
//mineRock5.m_nview.Destroy();
return;
}

Expand All @@ -131,17 +130,16 @@ private void TryAttack()
// destroy all fragments
for (int i = 0; i < mineRock.m_hitAreas.Length; i++)
{
var collider = mineRock.m_hitAreas[i];
if (collider.TryGetComponent(out HitArea hitArea) && hitArea.m_health > 0f)
var col = mineRock.m_hitAreas[i];
if (col.TryGetComponent(out HitArea hitArea) && hitArea.m_health > 0f)
{
var hitData = new HitData();
hitData.m_damage.m_damage = hitArea.m_health;
hitData.m_point = collider.bounds.center;
hitData.m_point = col.bounds.center;
hitData.m_toolTier = 100;
mineRock5.DamageArea(i, hitData);
}
}
//mineRock.Damage(hitData);
}
}
}
Expand Down Expand Up @@ -173,10 +171,9 @@ private bool Hittable(GameObject go)
return parent != null && rocksListName.Contains(parent.name);
}) != null
|| (destructible != null
//&& (destructible.gameObject.layer == LayerMask.NameToLayer("static_solid") || destructible.gameObject.layer == LayerMask.NameToLayer("Default_small"))
&& destructible.m_destructibleType == DestructibleType.Default
&& destructible.GetComponent<Container>() == null // don't attack containers
&& destructible.GetComponent<Pickable>() == null // don't attack useful bushes
&& destructible.m_destructibleType == DestructibleType.Default
&& destructible.GetComponent<Container>() == null // don't attack containers
&& destructible.GetComponent<Pickable>() == null // don't attack useful bushes
)
|| go.GetComponentInParent<MineRock5>() != null
|| go.GetComponentInParent<MineRock>() != null;
Expand Down
2 changes: 1 addition & 1 deletion ChebsNecromancy/Package/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ChebsNecromancy",
"description": "Cheb's Necromancy adds Necromancy to Valheim via craftable wands and structures. Minions will follow you, guard your base, and perform menial tasks.",
"version_number": "2.5.14",
"version_number": "2.5.15",
"website_url": "https://github.com/jpw1991/chebs-necromancy",
"dependencies": [
"ValheimModding-Jotunn-2.11.2"
Expand Down

0 comments on commit 5738473

Please sign in to comment.