Skip to content
Open
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
3 changes: 2 additions & 1 deletion GuideMarker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ private static void PaintMeridians(PlatformSystem platformSystem)
}
}

// This version doesn't seem to ever be called so I didn't bother to update it with the new system used in MinorMeridianPainter.cs
private static void PaintMinorMeridians(PlatformSystem platformSystem)
{
var planetRadius = platformSystem.planet.radius;
Expand All @@ -208,7 +209,7 @@ private static void PaintMinorMeridians(PlatformSystem platformSystem)
if (PluginConfig.LatitudeOutOfBounds(lat))
continue;

for (var meridianOffset = -180; meridianOffset < 180; meridianOffset += interval)
for (var meridianOffset = -180; meridianOffset < 180; meridianOffset += Mathf.RoundToInt(interval))
{
var lonOffsetMin = -1;

Expand Down
12 changes: 6 additions & 6 deletions LatLon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Bulldozer
public float Lat => Precision == 1 ? _lat : _lat / (float) Precision;

public float Long => Precision == 1 ? _lng : _lng / (float) Precision;
public static LatLon Empty => new (-1000, -1000, 1);
public static LatLon Empty => new (0, 0, 0);

private static readonly Dictionary<int, Dictionary<int, LatLon>> PoolLatToLonToInstance = new();
public readonly int Precision;
Expand Down Expand Up @@ -44,8 +44,8 @@ public static LatLon FromCoords(double lat, double lon, int precisionMultiple =
throw new InvalidDataException("Invalid precision multiple " + precisionMultiple);
}

int newLat = lat < 0 ? Mathf.CeilToInt((float)lat * precisionMultiple) : Mathf.FloorToInt((float)lat * precisionMultiple);
int newLon = lon < 0 ? Mathf.CeilToInt((float)lon * precisionMultiple) : Mathf.FloorToInt((float)lon * precisionMultiple);
int newLat = Mathf.RoundToInt((float)lat * precisionMultiple);
int newLon = Mathf.RoundToInt((float)lon * precisionMultiple);
if (!PoolLatToLonToInstance.TryGetValue(newLat, out var lonLookup))
{
lonLookup = new Dictionary<int, LatLon>();
Expand All @@ -62,7 +62,7 @@ public static LatLon FromCoords(double lat, double lon, int precisionMultiple =

public bool Equals(LatLon other)
{
return _lat == other._lat && _lng == other._lng;
return _lat == other._lat && _lng == other._lng && Precision == other.Precision;
}

public override bool Equals(object obj)
Expand Down Expand Up @@ -90,15 +90,15 @@ public override int GetHashCode()

public bool IsEmpty()
{
return Precision == 0 || _lat == Empty._lat && _lng == Empty._lng;
return Precision == 0;
}

public override string ToString()
{
return $"{Lat},{Long}";
}

public LatLon Offset(int latOffset, int lonOffset)
public LatLon Offset(float latOffset, float lonOffset)
{
return FromCoords(Lat + latOffset, Long + lonOffset, Precision);
}
Expand Down
27 changes: 14 additions & 13 deletions PluginConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Collections;
using System.ComponentModel;
using BepInEx.Configuration;
Expand Down Expand Up @@ -52,8 +53,8 @@ public static class PluginConfig
public static ConfigEntry<int> factoryTeardownRunTimePerFrame;
public static ConfigEntry<OperationMode> soilPileConsumption;
public static ConfigEntry<OperationMode> foundationConsumption;
public static ConfigEntry<int> maxLatitude;
public static ConfigEntry<int> minLatitude;
public static ConfigEntry<float> maxLatitude;
public static ConfigEntry<float> minLatitude;

// used to make UI checkbox values persistent
public static ConfigEntry<bool> addGuideLines;
Expand All @@ -64,7 +65,7 @@ public static class PluginConfig
public static ConfigEntry<bool> addGuideLinesMeridian;
public static ConfigEntry<bool> addGuideLinesTropic;
public static ConfigEntry<bool> addGuideLinesPoles;
public static ConfigEntry<int> minorMeridianInterval;
public static ConfigEntry<float> minorMeridianInterval;

public static ConfigEntry<int> guideLinesEquatorColor;
public static ConfigEntry<int> guideLinesTropicColor;
Expand Down Expand Up @@ -94,13 +95,13 @@ public static void InitConfig(ConfigFile configFile)
{
PluginConfigFile = configFile;

minLatitude = configFile.Bind("Control", "Min Latitude", -90,
minLatitude = configFile.Bind("Control", "Min Latitude", -90f,
new ConfigDescription("Minimum latitude to work on. Set equal to Max Latitude to apply to entire planet",
new AcceptableValueRange<int>(-90, 90)));
new AcceptableValueRange<float>(-90f, 90f)));

maxLatitude = configFile.Bind("Control", "Max Latitude", 90,
maxLatitude = configFile.Bind("Control", "Max Latitude", 90f,
new ConfigDescription("Max latitude to work on, set min and max to the same value to apply to entire planet",
new AcceptableValueRange<int>(-90, 90)));
new AcceptableValueRange<float>(-90f, 90f)));
minLatitude.SettingChanged += OnMinLatitudeChange;
maxLatitude.SettingChanged += OnMaxLatitudeChange;

Expand Down Expand Up @@ -141,10 +142,10 @@ public static void InitConfig(ConfigFile configFile)
"Enable/disable of the tropic guidelines individually. No effect if AddGuideLines is disabled");
addGuideLinesPoles = configFile.Bind("Decoration", "AddGuideLinesPoles", false,
"Enable/disable painting polar areas. No effect if AddGuideLines is disabled. Poles are considered first 2 tropics");
minorMeridianInterval = configFile.Bind("Decoration", "MinorMeridianInterval", 0,
minorMeridianInterval = configFile.Bind("Decoration", "MinorMeridianInterval", 0f,
new ConfigDescription(
"Paint meridians starting at 0 and incrementing by this value. E.g., a value of 10 would add a meridian line every 10 degrees 18, 36 total. 0 to disable",
new AcceptableValueRange<int>(0, 89), "meridians"));
new AcceptableValueRange<float>(0f, 120f), "meridians"));

guideLinesEquatorColor = configFile.Bind("CustomColors", "Equator Color", 7,
new ConfigDescription("Index of color in palette to paint equator. Default is green", new AcceptableValueRange<int>(0, 31), "color"));
Expand Down Expand Up @@ -196,17 +197,17 @@ public static void InitConfig(ConfigFile configFile)

private static void OnMaxLatitudeChange(object sender, EventArgs e)
{
if (sender is ConfigEntry<int> entry && minLatitude.Value > entry.Value)
if (sender is ConfigEntry<float> entry && minLatitude.Value > entry.Value)
{
minLatitude.Value = maxLatitude.Value;
}
}

private static void OnMinLatitudeChange(object sender, EventArgs e)
{
if (sender is ConfigEntry<int> entry && maxLatitude.Value < entry.Value)
if (sender is ConfigEntry<float> entry && maxLatitude.Value < entry.Value)
{
maxLatitude.Value = entry.Value;
maxLatitude.Value = minLatitude.Value;
}
}

Expand Down Expand Up @@ -300,7 +301,7 @@ public static string GetLatRangeString()
maxLatDir = "° ";
}

return $"{minlat}{latDir} - {maxlat}{maxLatDir}";
return $"{minlat.ToString("F2", CultureInfo.CurrentCulture)}{latDir} - {maxlat.ToString("F2", CultureInfo.CurrentCulture)}{maxLatDir}";
}
}
}
46 changes: 46 additions & 0 deletions PluginConfigWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ private static void DrawSetting(ConfigEntryBase configEntry)
descriptionAdded = DrawRangeField(configEntry);
}

if (!descriptionAdded && configEntry.SettingType == typeof(float))
{
descriptionAdded = DrawFloatRangeField(configEntry);
}

if (!descriptionAdded)
{
//something went wrong, default to text field
Expand Down Expand Up @@ -283,6 +288,47 @@ private static bool DrawRangeField(ConfigEntryBase configEntry)
return true;
}

private static bool DrawFloatRangeField(ConfigEntryBase configEntry)
{
if (configEntry.Description.AcceptableValues is not AcceptableValueRange<float> acceptableValueRange)
{
return false;
}

GUILayout.BeginHorizontal();
AcceptableValueRange<float> acceptableValues = acceptableValueRange;
var converted = (float)configEntry.BoxedValue;
var leftValue = acceptableValues.MinValue;
var rightValue = acceptableValues.MaxValue;

var result = GUILayout.HorizontalSlider(converted, leftValue, rightValue, GUILayout.MinWidth(200));
if (Math.Abs(result - converted) > Mathf.Abs(rightValue - leftValue) / 1000)
{
var newValue = Convert.ChangeType(Mathf.Round(result / 0.36f) * 0.36f, configEntry.SettingType, CultureInfo.InvariantCulture);
configEntry.BoxedValue = newValue;
}

var strVal = ((float)configEntry.BoxedValue).ToString("F2", CultureInfo.CurrentCulture);
var strResult = GUILayout.TextField(strVal, GUILayout.Width(50));

GUILayout.EndHorizontal();
if (strResult != strVal)
{
try
{
var resultVal = float.Parse(strResult, NumberStyles.Any, CultureInfo.CurrentCulture);
var clampedResultVal = Mathf.Clamp(resultVal, leftValue, rightValue);
configEntry.BoxedValue = clampedResultVal;
}
catch (FormatException)
{
// Ignore user typing in bad data
}
}

return true;
}

public static Color GetColorByIndex(int index)
{
if (index < 0 || index > 31)
Expand Down
40 changes: 20 additions & 20 deletions ReformIndexInfoProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ namespace Bulldozer
public class ReformIndexInfoProvider
{
private const int LatitudesPerPass = 10;
public const int latLonPrecision = 1000;
private readonly Dictionary<int, LatLon> _llLookup = new();
private readonly LatLon[] _llModLookup = new LatLon[GameMain.localPlanet.data.modData.Length * 2];
private readonly HashSet<LatLon> _tropicsLatitudes = new();
private readonly LatLon[] _equatorLatitudes = { LatLon.Empty, LatLon.Empty };
private readonly HashSet<LatLon> _meridians = new();
public PlatformSystem platformSystem;
private bool _lookupsCreated;
private float _latLookupWorkItemIndex = -89.9f;
private float _latLookupWorkItemIndex;
private int _initUpdateCounter;
private int _planetId;
private int prevLength = -1;
Expand All @@ -27,6 +28,7 @@ public ReformIndexInfoProvider(PlatformSystem platformSystem)
{
this.platformSystem = platformSystem;
_planetId = platformSystem.planet.id;
_latLookupWorkItemIndex = -90f + 90f / platformSystem?.latitudeCount ?? 500f;
}

public int PlanetId => _planetId;
Expand All @@ -37,13 +39,14 @@ private void SetInitValues(PlatformSystem newPlatformSystem, int planetId)
_llLookup.Clear();
Array.Clear(_llModLookup, 0, _llLookup.Count);
_tropicsLatitudes.Clear();
prevLength = -1;
_equatorLatitudes[0] = LatLon.Empty;
_equatorLatitudes[1] = LatLon.Empty;
_meridians.Clear();
_lookupsCreated = false;
_planetId = planetId;
platformSystem = newPlatformSystem;
_latLookupWorkItemIndex = -89.9f;
_latLookupWorkItemIndex = -90f + 90f / platformSystem?.latitudeCount ?? 500f;
_initUpdateCounter = 0;
}

Expand Down Expand Up @@ -124,7 +127,6 @@ public void DoInitWork(PlanetData planetData)
var planetRawData = platformSystem.planet.data;
var start = DateTime.Now;
var maxRuntimeMS = GetMaxRuntimeMS();
var latLonPrecision = planetData.realRadius < 250f ? 10 : 10;
var latitudeCount = platformSystem.latitudeCount;
var latDegIncrement = (90 * 2.0f) / latitudeCount;

Expand All @@ -137,10 +139,15 @@ public void DoInitWork(PlanetData planetData)
continue;
prevStart = startNdx;
var longitudeCounts = endNdxExclusive - startNdx;
if (longitudeCounts != prevLength)
if (longitudeCounts != prevLength && prevLength >= 0)
{
// got ourselves a new tropic here
_tropicsLatitudes.Add(LatLon.FromCoords(_latLookupWorkItemIndex, 0, latLonPrecision));
// mark the north side of the tropic in the southern hemisphere and the south side in the northern hemisphere
// it might be better to switch these or add a setting to choose
if (_latLookupWorkItemIndex < 0)
_tropicsLatitudes.Add(LatLon.FromCoords(_latLookupWorkItemIndex, 0, latLonPrecision));
else
_tropicsLatitudes.Add(LatLon.FromCoords(_latLookupWorkItemIndex - latDegIncrement, 0, latLonPrecision));
}

var latCoord = LatLon.FromCoords(_latLookupWorkItemIndex, 0, latLonPrecision);
Expand Down Expand Up @@ -173,15 +180,15 @@ public void DoInitWork(PlanetData planetData)
{
var numLongitudes = (endNdxExclusive - startNdx);
// longitudes above this index are in going west from 0
var maxPositiveLongitudeIndex = (endNdxExclusive + startNdx) / 2;
var maxPositiveLongitudeIndex = (endNdxExclusive + startNdx) / 2 - 1;
var negCounts = numLongitudes / 2;
var degPerIndex = 180f / negCounts;
var longMod = numLongitudes / 4;
for (var desired = startNdx; desired < endNdxExclusive; desired++)
{
if (desired > maxPositiveLongitudeIndex)
{
var longDegrees = degPerIndex * (desired - maxPositiveLongitudeIndex);
var longDegrees = degPerIndex * (desired - maxPositiveLongitudeIndex - 0.5f);
_llLookup[desired] = LatLon.FromCoords(_latLookupWorkItemIndex, -longDegrees, latLonPrecision);

var pos = GeoUtil.LatLonToPosition(_latLookupWorkItemIndex, -longDegrees, platformSystem.planet.realRadius);
Expand All @@ -190,7 +197,7 @@ public void DoInitWork(PlanetData planetData)
}
else
{
var longDegrees = degPerIndex * (desired - startNdx);
var longDegrees = degPerIndex * (desired - startNdx + 0.5f);
_llLookup[desired] = LatLon.FromCoords(_latLookupWorkItemIndex, longDegrees, latLonPrecision);
var pos = GeoUtil.LatLonToPosition(_latLookupWorkItemIndex, longDegrees, platformSystem.planet.realRadius);
var currentDataIndex = planetRawData.QueryIndex(pos);
Expand All @@ -216,7 +223,7 @@ public void DoInitWork(PlanetData planetData)
}
}

if (_latLookupWorkItemIndex > 89)
if (_latLookupWorkItemIndex >= 90)
{
_lookupsCreated = true;
}
Expand Down Expand Up @@ -249,18 +256,11 @@ public int InitPercentComplete()

public (int offsetStartIndex, int offsetEndIndex) GetReformIndexesForLatitude(float latDegrees)
{
double latInRads = Mathf.Deg2Rad * latDegrees;
var latIndex = (float)(latInRads / (Mathf.PI * 2)) * platformSystem.segment;
var scaledLat = Mathf.Round(latIndex * 10f);
var absScaledLat = Mathf.Abs(scaledLat);
var scaledLatFloat = scaledLat >= 0.0 ? absScaledLat : -absScaledLat;
var latitudeSeg = scaledLatFloat / 10f;

var scaledLatSeg = latitudeSeg > 0.0 ? Mathf.CeilToInt(latitudeSeg * 5f) : Mathf.FloorToInt(latitudeSeg * 5f);
var latCountsHalf = platformSystem.latitudeCount / 2;
var y = scaledLatSeg > 0 ? scaledLatSeg - 1 : latCountsHalf - scaledLatSeg - 1;
var startIndex = platformSystem.reformOffsets[y];
var unscaledCount = PlatformSystem.DetermineLongitudeSegmentCount(Mathf.FloorToInt(Mathf.Abs(latitudeSeg)), platformSystem.segment);
var scaledLat = Mathf.FloorToInt(Mathf.Abs(latDegrees) * platformSystem.latitudeCount / 180f);
var latIndex = latDegrees >= 0 ? scaledLat : scaledLat + latCountsHalf;
var startIndex = platformSystem.reformOffsets[latIndex];
var unscaledCount = PlatformSystem.DetermineLongitudeSegmentCount(scaledLat / 5, platformSystem.segment);

var endIndex = startIndex + unscaledCount * 5;
return (startIndex, endIndex);
Expand Down
Loading