Merge branch 'fix-grafvaettir-drone': drones mine and work instead of wandering

This commit is contained in:
kanym 2026-10-29 13:23:43 +01:00
commit a3d4f4ca02
4 changed files with 54 additions and 17 deletions

View file

@ -301,7 +301,11 @@ namespace RacesOfValheim
else if (Time.time >= m_nextFightScan)
{
m_nextFightScan = Time.time + FightScanInterval;
if (BaseAI.FindClosestEnemy(m_character, transform.position, FightRange) != null)
// FindClosestEnemy takes its first match at ANY distance (it only compares against
// maxDistance once it holds one), so with a deer anywhere in the loaded zones it never
// returned null and vanilla kept the tick: the drone idled about instead of working.
Character enemy = BaseAI.FindClosestEnemy(m_character, transform.position, FightRange);
if (enemy != null && Vector3.Distance(enemy.transform.position, transform.position) <= FightRange)
{
m_fightUntil = Time.time + FightLinger;
}

View file

@ -273,9 +273,13 @@ namespace RacesOfValheim
}
if (target == null || !target.gameObject.activeInHierarchy) target = FindDeposit();
if (target == null) { ai.StopMoving(); return true; }
Vector3 point = Vector3.zero;
Collider area = target != null ? NearestArea(target, out point) : null;
if (area == null) { target = null; ai.StopMoving(); return true; }
Vector3 point = target.transform.position;
// Measured to the closest point of the rock, not to its root: a copper deposit's root
// sits at its base inside the mesh, so a drone aiming there stopped on top of the rock
// and never came within reach.
float distance = Vector3.Distance(transform.position, point);
if (distance > 3.5f)
@ -287,7 +291,7 @@ namespace RacesOfValheim
ai.StopMoving();
if (Time.time < nextStrike) return true;
nextStrike = Time.time + 1f;
Strike(target);
Strike(target, area, point);
return true;
}
@ -324,7 +328,31 @@ namespace RacesOfValheim
return best;
}
private void Strike(Component deposit)
/// <summary>
/// The live hit area of the deposit closest to the drone, and the closest point on it.
/// MineRock and MineRock5 only take a hit on one of their own areas; a MineRock with an area
/// root (tin) keeps its base model collider apart, and a hit on that one is thrown away.
/// Broken areas are switched off, so only active colliders count.
/// </summary>
private Collider NearestArea(Component deposit, out Vector3 point)
{
point = Vector3.zero;
MineRock rock = deposit as MineRock;
GameObject root = rock != null && rock.m_areaRoot != null ? rock.m_areaRoot : deposit.gameObject;
Collider best = null;
float bestDistance = float.MaxValue;
foreach (Collider collider in root.GetComponentsInChildren<Collider>())
{
if (collider == null || !collider.enabled) continue;
Vector3 closest = collider.bounds.ClosestPoint(transform.position);
float distance = Vector3.Distance(transform.position, closest);
if (distance < bestDistance) { best = collider; bestDistance = distance; point = closest; }
}
return best;
}
private void Strike(Component deposit, Collider area, Vector3 point)
{
IDestructible destructible = deposit as IDestructible;
if (destructible == null) return;
@ -334,16 +362,12 @@ namespace RacesOfValheim
hit.m_toolTier = 100;
hit.m_dodgeable = false;
hit.m_blockable = false;
hit.m_point = deposit.transform.position;
hit.m_dir = (deposit.transform.position - transform.position).normalized;
// MineRock5 picks the piece of the rock to hurt from the collider that was hit.
Collider collider = deposit.GetComponentInChildren<Collider>();
if (collider != null)
{
hit.m_hitCollider = collider;
hit.m_point = collider.bounds.center;
}
hit.m_hitCollider = area;
hit.m_point = point;
hit.m_dir = (area.bounds.center - transform.position).normalized;
// Vanilla MineRock (tin) reads the attacker's ZDOID for its hit effects without a null
// check: a hit with no attacker throws before the area can break.
hit.SetAttacker(GetComponent<Character>());
destructible.Damage(hit);
}

View file

@ -89,8 +89,13 @@ namespace RacesOfValheim
DroneZapDamage = config.Bind(drone, "Zap Damage", 1f, new ConfigDescription("Lightning damage of a drone's defensive zap.", new AcceptableValueRange<float>(0f, 100f)));
DroneZapInterval = config.Bind(drone, "Zap Interval", 2f, new ConfigDescription("Seconds between a drone's zaps.", new AcceptableValueRange<float>(0.2f, 30f)));
MinerDeposits = config.Bind(drone, "Mining Targets", "MineRock_Copper,MineRock_Tin,MineRock_Iron,silvervein,rock3_silver,giant_brain",
// Copper deposits are rock4_copper (and rock4_copper_frac once hit), crypt scrap is mudpile;
// the first default missed both, so a config still holding it is moved to the new one.
const string oldTargets = "MineRock_Copper,MineRock_Tin,MineRock_Iron,silvervein,rock3_silver,giant_brain";
const string targets = "rock4_copper,MineRock_Copper,MineRock_Tin,MineRock_Iron,mudpile,silvervein,rock3_silver,giant_brain";
MinerDeposits = config.Bind(drone, "Mining Targets", targets,
new ConfigDescription("Comma separated parts of prefab names the mining drones dig (case does not matter). Soft tissue comes from giant_brain."));
if (MinerDeposits.Value == oldTargets) MinerDeposits.Value = targets;
MinerDamage = config.Bind(drone, "Mining Damage", 40f, new ConfigDescription("Pickaxe damage each mining drone does per second to a deposit.", new AcceptableValueRange<float>(1f, 1000f)));
WildPickables = config.Bind(drone, "Wild Plants Harvested", "Pickable_Mushroom,Pickable_Dandelion,Pickable_Thistle,RaspberryBush,BlueberryBush,CloudberryBush",

View file

@ -270,7 +270,11 @@ namespace RacesOfValheim
else if (Time.time >= m_nextFightScan)
{
m_nextFightScan = Time.time + FightScanInterval;
if (BaseAI.FindClosestEnemy(m_character, transform.position, FightRange) != null)
// FindClosestEnemy takes its first match at ANY distance (it only compares against
// maxDistance once it holds one), so with a deer anywhere in the loaded zones it never
// returned null and vanilla kept the tick: the worker idled about instead of working.
Character enemy = BaseAI.FindClosestEnemy(m_character, transform.position, FightRange);
if (enemy != null && Vector3.Distance(enemy.transform.position, transform.position) <= FightRange)
{
m_fightUntil = Time.time + FightLinger;
}