-
Notifications
You must be signed in to change notification settings - Fork 32
/
LegacyResourceGenerator.cs
198 lines (166 loc) · 7.66 KB
/
LegacyResourceGenerator.cs
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using Kethane.GeodesicGrid;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using UnityEngine;
namespace Kethane.Generators
{
internal class LegacyResourceGenerator : IResourceGenerator
{
private GeneratorConfiguration config;
public LegacyResourceGenerator(ConfigNode node)
{
config = new GeneratorConfiguration(node);
}
public IBodyResources Load(CelestialBody body, ConfigNode node)
{
return new BodyDeposits(config.ForBody(body), node);
}
private class GeneratorConfiguration
{
public float MinRadius { get; private set; }
public float MaxRadius { get; private set; }
public double MinQuantity { get; private set; }
public double MaxQuantity { get; private set; }
public int MinVertices { get; private set; }
public int MaxVertices { get; private set; }
public float RadiusVariance { get; private set; }
public int DepositCount { get; private set; }
public int NumberOfTries { get; private set; }
private Dictionary<string, GeneratorConfiguration> bodies = new Dictionary<string, GeneratorConfiguration>();
public GeneratorConfiguration(ConfigNode node)
{
load(node);
foreach (var bodyNode in node.GetNodes("Body"))
{
var body = (GeneratorConfiguration)this.MemberwiseClone();
body.load(bodyNode);
bodies[bodyNode.GetValue("name")] = body;
}
}
public GeneratorConfiguration ForBody(CelestialBody body)
{
return bodies.ContainsKey(body.name) ? bodies[body.name] : this;
}
private void load(ConfigNode node)
{
MinRadius = Misc.Parse(node.GetValue("MinRadius"), MinRadius);
MaxRadius = Misc.Parse(node.GetValue("MaxRadius"), MaxRadius);
MinQuantity = Misc.Parse(node.GetValue("MinQuantity"), MinQuantity);
MaxQuantity = Misc.Parse(node.GetValue("MaxQuantity"), MaxQuantity);
MinVertices = Misc.Parse(node.GetValue("MinVertices"), MinVertices);
MaxVertices = Misc.Parse(node.GetValue("MaxVertices"), MaxVertices);
RadiusVariance = Misc.Parse(node.GetValue("RadiusVariance"), RadiusVariance);
DepositCount = Misc.Parse(node.GetValue("DepositCount"), DepositCount);
NumberOfTries = Misc.Parse(node.GetValue("NumberOfTries"), NumberOfTries);
}
}
private class BodyDeposits : IBodyResources
{
private readonly List<Deposit> deposits;
private readonly int seed;
private static System.Random seedGenerator = new System.Random();
public double MaxQuantity { get; private set; }
public BodyDeposits(GeneratorConfiguration resource, ConfigNode node)
{
if (node == null) { node = new ConfigNode(); }
this.deposits = new List<Deposit>();
this.seed = Misc.Parse(node.GetValue("Seed"), seedGenerator.Next());
var random = new System.Random(seed);
for (int i = 0; i < resource.DepositCount; i++)
{
float R = random.Range(resource.MinRadius, resource.MaxRadius);
for (int j = 0; j < resource.NumberOfTries; j++)
{
Vector2 Pos = new Vector2(random.Range(R, 360 - R), random.Range(R, 180 - R));
var deposit = Deposit.Generate(Pos, R, random, resource);
if (!deposits.Any(d => d.Shape.Vertices.Any(v => deposit.Shape.PointInPolygon(new Vector2(v.x, v.y)))) && !deposit.Shape.Vertices.Any(v => deposits.Any(d => d.Shape.PointInPolygon(new Vector2(v.x, v.y)))))
{
deposits.Add(deposit);
break;
}
}
}
var depositValues = node.GetValues("Deposit");
for (int i = 0; i < Math.Min(deposits.Count, depositValues.Length); i++)
{
deposits[i].Quantity = Misc.Parse(depositValues[i], deposits[i].InitialQuantity);
}
MaxQuantity = resource.MaxQuantity;
}
public ICellResource GetResource(Cell cell)
{
var pos = cell.Position;
var lat = (float)(Math.Atan2(pos.y, Math.Sqrt(pos.x * pos.x + pos.z * pos.z)) * 180 / Math.PI);
var lon = (float)(Math.Atan2(pos.z, pos.x) * 180 / Math.PI);
var x = lon + 180f;
var y = 90f - lat;
return deposits.FirstOrDefault(d => d.Shape.PointInPolygon(new Vector2(x, y)));
}
public ConfigNode Save()
{
var node = new ConfigNode();
node.AddValue("Seed", seed);
foreach (var deposit in deposits)
{
node.AddValue("Deposit", deposit.Quantity);
}
return node;
}
}
private class Deposit : ICellResource
{
public Polygon Shape;
public double Quantity { get; set; }
public double InitialQuantity { get; set; }
public Deposit(Polygon shape, double quantity, double initialQuantity)
{
Shape = shape;
Quantity = quantity;
InitialQuantity = initialQuantity;
}
public static Deposit Generate(Vector2 Pos, float radius, System.Random random, GeneratorConfiguration resource)
{
var initialQuantity = random.Range(resource.MinQuantity, resource.MaxQuantity);
var vertices = new List<Vector2>();
int vertexCount = random.Next(resource.MinVertices, resource.MaxVertices);
for (int i = 0; i < vertexCount; i++)
{
float randomRadius = random.Range(resource.RadiusVariance * radius, radius);
float angle = 2.0f * (float)Math.PI * ((float)i / (float)vertexCount);
float x = Pos.x + randomRadius * (float)Math.Cos(angle);
float z = Pos.y - randomRadius * (float)Math.Sin(angle);
vertices.Add(new Vector2(x, z));
}
var Shape = new Polygon(vertices.ToArray());
return new Deposit(Shape, initialQuantity, initialQuantity);
}
}
private class Polygon
{
private Vector2[] _vertices;
public Polygon(Vector2[] vertices)
{
_vertices = vertices.ToArray();
}
public ReadOnlyCollection<Vector2> Vertices
{
get { return new ReadOnlyCollection<Vector2>(_vertices); }
}
public bool PointInPolygon(Vector2 p)
{
bool isInside = false;
for (int i = 0, j = _vertices.Length - 1; i < _vertices.Length; j = i++)
{
if (((_vertices[i].y > p.y) != (_vertices[j].y > p.y)) &&
(p.x < (_vertices[j].x - _vertices[i].x) * (p.y - _vertices[i].y) / (_vertices[j].y - _vertices[i].y) + _vertices[i].x))
{
isInside = !isInside;
}
}
return isInside;
}
}
}
}