forked from mattsemar/dsp-bulldozer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridExplorer.cs
More file actions
154 lines (135 loc) · 6.31 KB
/
GridExplorer.cs
File metadata and controls
154 lines (135 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System.Collections.Generic;
using System.Diagnostics;
using Bulldozer.SelectiveDecoration;
using UnityEngine;
using static Bulldozer.Log;
namespace Bulldozer
{
public class SnapArgs
{
public int[] indices = new int[100];
public Vector3[] points = new Vector3[100];
}
public class GridExplorer
{
public delegate void PostComputeReformAction(SnapArgs snapArgs, Vector3 center, float radius, int reformSize, int neededSoilPile, bool timeExpired = false,
float lastLat = 0, float lastLon = 0);
public static void IterateReform(BuildTool_Reform reformTool, PostComputeReformAction postComputeFn, int maxExecutionMs, float startLat = -89.9f,
float startLon = -179.9f)
{
var checkedReformIndices = new HashSet<int>();
var checkedDataPos = new HashSet<int>();
var platformSystem = reformTool.planet.factory.platformSystem;
if (platformSystem.IsAllReformed())
{
return;
}
int cursorPointCount;
var radius = 0.990946f * 10;
var stopwatch = new Stopwatch();
stopwatch.Start();
for (var lat = startLat; lat < 90; lat += 0.25f)
{
if (PluginConfig.LatitudeOutOfBounds(lat))
continue;
for (var lon = -179.9f; lon < 180; lon += 0.25f)
{
var position = GeoUtil.LatLonToPosition(lat, 0, reformTool.planet.radius);
position.Normalize();
var reformIndexForPosition = platformSystem.GetReformIndexForPosition(position);
if (reformIndexForPosition < 0)
continue;
if (checkedReformIndices.Contains(reformIndexForPosition))
{
continue;
}
checkedReformIndices.Add(reformIndexForPosition);
if (platformSystem.IsTerrainReformed(platformSystem.GetReformType(reformIndexForPosition)))
{
continue;
}
var snapArgs = new SnapArgs();
cursorPointCount = reformTool.planet.aux.ReformSnap(position, 10, 1, 1, snapArgs.points, snapArgs.indices, platformSystem,
out var center);
var queryIndex = reformTool.planet.data.QueryIndex(center);
if (checkedDataPos.Contains(queryIndex))
{
continue;
}
checkedDataPos.Add(queryIndex);
foreach (var cursorPoint in snapArgs.points)
{
checkedDataPos.Add(reformTool.planet.data.QueryIndex(cursorPoint));
}
var flattenTerrainReform = reformTool.factory.ComputeFlattenTerrainReform(snapArgs.points, center, radius, cursorPointCount);
postComputeFn?.Invoke(snapArgs, center, radius, 10, flattenTerrainReform);
if (stopwatch.ElapsedMilliseconds > maxExecutionMs)
{
LogAndPopupMessage($"cancel after running ${stopwatch.ElapsedMilliseconds} lat={lat} / lon={lon}");
stopwatch.Stop();
// signal that we did not finish this task
if (postComputeFn != null)
postComputeFn(snapArgs, center, radius, 10, 0, true, lat, lon);
break;
}
}
}
}
public static int GetNeededFoundation(PlatformSystem platformSystem, ReformIndexInfoProvider reformIndexInfoProvider)
{
var planetPainter = SelectiveDecorationBuilder.Build(reformIndexInfoProvider);
var foundationNeeded = 0;
for (var index = 0; index < platformSystem.maxReformCount; ++index)
{
if (PluginConfig.IsLatConstrained())
{
var latLon = reformIndexInfoProvider.GetForIndex(index);
if (!latLon.IsEmpty() && PluginConfig.LatitudeOutOfBounds(latLon.Lat))
{
continue;
}
}
if (PluginConfig.guideLinesOnly.Value)
{
var latLon = reformIndexInfoProvider.GetForIndex(index);
if (!latLon.IsEmpty() && planetPainter.DecoratorForLocation(latLon).IsNone())
{
continue;
}
}
foundationNeeded += platformSystem.IsTerrainReformed(platformSystem.GetReformType(index)) ? 0 : 1;
}
return foundationNeeded;
}
public static (int foundation, int soilPile) CountNeededResources(PlatformSystem platformSystem, ReformIndexInfoProvider indexInfoProvider)
{
logger.LogInfo($"player current soil pile {GameMain.mainPlayer.sandCount}");
var platformSystemPlanet = platformSystem?.planet;
if (platformSystemPlanet == null)
{
logger.LogWarning("null platform system passed in");
return (int.MaxValue, int.MaxValue);
}
platformSystem.EnsureReformData();
var neededFoundation = 0;
var neededSoil = 0;
if (PluginConfig.foundationConsumption.Value != OperationMode.FullCheat)
{
neededFoundation = GetNeededFoundation(platformSystem, indexInfoProvider);
}
if (PluginConfig.soilPileConsumption.Value != OperationMode.FullCheat && !PluginConfig.guideLinesOnly.Value)
{
_soilNeeded = 0;
IterateReform(GameMain.mainPlayer.controller.actionBuild.reformTool, SumReform, 1000 * 5);
neededSoil = _soilNeeded;
}
return (neededFoundation, neededSoil);
}
private static int _soilNeeded;
public static void SumReform(SnapArgs snapArgs, Vector3 center, float radius, int reformSize, int neededSoilPile, bool timeExpired = false,
float lastLat = 0, float lastLon = 0)
{
_soilNeeded += neededSoilPile;
}
}
}