Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions JotunnLib/Configs/LocationConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,23 @@ public class LocationConfig
[Obsolete("This property is unused by Valheim.")]
public float ChanceToSpawn { get; set; } = 10f;

private float? _exteriorRadius;

/// <summary>
/// Radius of the location. Terrain delta is calculated within this circle.
/// If left unset, falls back to the prefab's <see cref="Location"/> component value when one exists.
/// Defaults to 10f when no value has been set and no component is present.
/// </summary>
public float ExteriorRadius { get; set; } = 10f;
public float ExteriorRadius
{
get => _exteriorRadius ?? 10f;
set => _exteriorRadius = value;
}

/// <summary>
/// True when the caller explicitly assigned a value to <see cref="ExteriorRadius"/>.
/// </summary>
internal bool HasExteriorRadius => _exteriorRadius.HasValue;

/// <summary>
/// Attempt to place in the central zone first
Expand Down Expand Up @@ -128,10 +141,22 @@ public class LocationConfig
/// </summary>
public bool HasInterior { get; set; }

private float? _interiorRadius;

/// <summary>
/// Radius of the interior attached to the location.
/// If left unset, falls back to the prefab's <see cref="Location"/> component value when one exists.
/// </summary>
public float InteriorRadius
{
get => _interiorRadius ?? 0f;
set => _interiorRadius = value;
}

/// <summary>
/// Radius of the interior attached to the location
/// True when the caller explicitly assigned a value to <see cref="InteriorRadius"/>.
/// </summary>
public float InteriorRadius { get; set; }
internal bool HasInteriorRadius => _interiorRadius.HasValue;

/// <summary>
/// Environment string used by the interior
Expand All @@ -158,10 +183,22 @@ public class LocationConfig
/// </summary>
public bool IconAlways { get; set; }

private bool? _clearArea;

/// <summary>
/// Enable to forbid Vegetation from spawning inside the circle defined by <see cref="ExteriorRadius"/>.
/// If left unset, falls back to the prefab's <see cref="Location"/> component value when one exists.
/// </summary>
public bool ClearArea
{
get => _clearArea ?? false;
set => _clearArea = value;
}

/// <summary>
/// Enable to forbid Vegetation from spawning inside the circle defined by <see cref="ExteriorRadius"/>
/// True when the caller explicitly assigned a value to <see cref="ClearArea"/>.
/// </summary>
public bool ClearArea { get; set; }
internal bool HasClearArea => _clearArea.HasValue;

/// <summary>
/// Create a new <see cref="LocationConfig"/>
Expand Down
23 changes: 23 additions & 0 deletions JotunnLib/Entities/CustomLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace Jotunn.Entities
/// </summary>
public class CustomLocation : CustomEntity
{
private readonly LocationConfig _locationConfig;

/// <summary>
/// The exterior prefab for this custom location.
/// </summary>
Expand Down Expand Up @@ -87,6 +89,7 @@ public CustomLocation(GameObject exteriorPrefab, GameObject interiorPrefab, bool
{
Prefab = exteriorPrefab;
Name = exteriorPrefab.name;
_locationConfig = locationConfig;

if (exteriorPrefab.TryGetComponent<Location>(out var location))
{
Expand All @@ -107,6 +110,8 @@ public CustomLocation(GameObject exteriorPrefab, GameObject interiorPrefab, bool
ZoneLocation.m_prefab = new SoftReference<GameObject>(AssetManager.Instance.AddAsset(exteriorPrefab));
ZoneLocation.m_prefabName = exteriorPrefab.name;

SyncZoneLocationFromComponent(Location, locationConfig);

FixReference = fixReference;
}

Expand All @@ -124,6 +129,7 @@ public CustomLocation(SoftReference<GameObject> softReferencePrefab, bool fixRef
return;
}

_locationConfig = locationConfig;
var parent = ZoneManager.Instance.LocationContainer.transform;
AssetManager.Instance.ResolveMocksOnLoad(softReferencePrefab, parent, OnLocationResolve);
Name = softReferencePrefab.Name;
Expand All @@ -136,12 +142,29 @@ public CustomLocation(SoftReference<GameObject> softReferencePrefab, bool fixRef

private void OnLocationResolve(GameObject gameObject)
{
if (gameObject.TryGetComponent<Location>(out var location))
{
SyncZoneLocationFromComponent(location, _locationConfig);
}

if (gameObject.TryGetComponent<ZoneSystem.ZoneLocation>(out var zoneLocation))
{
ZoneManager.Instance.PrepareLocation(zoneLocation, SourceMod);
}
}

private void SyncZoneLocationFromComponent(Location location, LocationConfig locationConfig)
{
if (location == null || ZoneLocation == null)
{
return;
}

if (!locationConfig.HasExteriorRadius) ZoneLocation.m_exteriorRadius = location.m_exteriorRadius;
if (!locationConfig.HasInteriorRadius) ZoneLocation.m_interiorRadius = location.m_interiorRadius;
if (!locationConfig.HasClearArea) ZoneLocation.m_clearArea = location.m_clearArea;
}

/// <summary>
/// Helper method to determine if a location prefab with a given name is a custom location created with Jötunn.
/// </summary>
Expand Down
Loading