Starsector API
Loading...
Searching...
No Matches
NebulaEditor.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.procgen;
2
3import org.lwjgl.util.vector.Vector2f;
4
5import com.fs.starfarer.api.impl.campaign.terrain.BaseTiledTerrain;
6import com.fs.starfarer.api.util.Misc;
7
8public class NebulaEditor {
9
10 protected BaseTiledTerrain plugin;
11 protected int [][] tiles;
12 protected int [][] orig;
13 protected float ts;
14 protected float width, height;
15 protected float cx, cy;
16 protected int w, h;
17 private float [][] noise;
18
19
20 public NebulaEditor(BaseTiledTerrain plugin) {
21 this.plugin = plugin;
22 tiles = plugin.getTiles();
23 ts = plugin.getTileSize();
24
25 width = tiles.length * ts;
26 height = tiles[0].length * ts;
27
28 cx = plugin.getEntity().getLocation().x;
29 cy = plugin.getEntity().getLocation().y;
30
31 w = tiles.length;
32 h = tiles[0].length;
33
35
36 regenNoise();
37 }
38
39 public int[][] getTilesCopy() {
40 int [][] copy = new int[w][h];
41 for (int i = 0; i < w; i++) {
42 for (int j = 0; j < h; j++) {
43 copy[i][j] = tiles[i][j];
44 }
45 }
46 return copy;
47 }
48
49 public int[][] getTiles() {
50 return tiles;
51 }
52
53 public int[][] getOrigTiles() {
54 return orig;
55 }
56
57 public float getTileSize() {
58 return ts;
59 }
60
61 public void regenNoise() {
62 float spikes = 1f;
63 noise = Misc.initNoise(StarSystemGenerator.random, w, h, spikes);
64 Misc.genFractalNoise(StarSystemGenerator.random, noise, 0, 0, w - 1, h - 1, 1, spikes);
65 Misc.normalizeNoise(noise);
66
67// // bug in noise generation? last line doesn't seem to have high enough values
68 // never mind, noise has to be power of two-sized
69// for (int i = 0; i < w; i++) {
70// noise[i][h - 1] = noise[i][h - 2];
71// }
72 }
73
74// public void noisePrune(float fractionKeep) {
75// if (noise == null) regenNoise();
76// int count = 0;
77// for (int i = 0; i < w; i++) {
78// for (int j = 0; j < h; j++) {
79// if (noise[i][j] < 1f - fractionKeep) {
80// tiles[i][j] = -1;
81// count++;
82// }
83// }
84// }
85// //System.out.println("Pruned " + (int)((count * 100f) / (float) (w * h)) + "% with keep=" + fractionKeep);
86// }
87
88 public void noisePrune(float fractionKeep) {
89 if (noise == null) regenNoise();
90 float [] counts = new float[100];
91 for (int i = 0; i < w; i++) {
92 for (int j = 0; j < h; j++) {
93 float f = noise[i][j];
94 int index = (int) (f * 100f);
95 if (index < 0) index = 0;
96 if (index > 99) index = 99;
97 counts[index]++;
98 }
99 }
100
101 float total = w * h;
102 float keep = fractionKeep * total;
103 float threshold = 0f;
104 float totalKept = 0f;
105 for (int i = 0; i < 100; i++) {
106 totalKept += counts[i];
107 if (totalKept >= keep) {
108 threshold = (float) i / 100f;
109 break;
110 }
111 }
112
113 int count = 0;
114 for (int i = 0; i < w; i++) {
115 for (int j = 0; j < h; j++) {
116 if (noise[i][j] > threshold) {
117 tiles[i][j] = -1;
118 count++;
119 }
120 }
121 }
122 //System.out.println("Pruned " + (int)((count * 100f) / (float) (w * h)) + "% with keep=" + fractionKeep);
123 }
124
125
126 public void clearArc(float x, float y, float innerRadius, float outerRadius, float startAngle, float endAngle) {
127 clearArc(x, y, innerRadius, outerRadius, startAngle, endAngle, 0f);
128 }
129
130 public void clearArc(float x, float y, float innerRadius, float outerRadius, float startAngle, float endAngle, float noiseThresholdToClear) {
131 clearArc(x, y, innerRadius, outerRadius, startAngle, endAngle, 1f, noiseThresholdToClear);
132 }
133
134 public void clearArc(float x, float y, float innerRadius, float outerRadius, float startAngle, float endAngle, float endRadiusMult, float noiseThresholdToClear) {
135 float circumference = (float) Math.PI * 2f * outerRadius;
136 float degreesPerIteration = 360f / (circumference / (ts * 0.5f));
137
138 for (float angle = startAngle; angle < endAngle; angle += degreesPerIteration) {
139 Vector2f dir = Misc.getUnitVectorAtDegreeAngle(angle);
140 float distMult = 1f;
141 if (endAngle > startAngle) {
142 float p = (angle - startAngle) / (endAngle - startAngle);
143 distMult = 1f + (endRadiusMult - 1f) * p;
144 }
145
146 //for (float dist = innerRadius; dist <= outerRadius; dist += ts * 0.5f) {
147 for (float dist = innerRadius * distMult; dist <= innerRadius * distMult + (outerRadius - innerRadius); dist += ts * 0.5f) {
148 Vector2f curr = new Vector2f(dir);
149 //curr.scale(dist * distMult);
150 curr.scale(dist);
151 curr.x += x;
152 curr.y += y;
153 setTileAt(curr.x, curr.y, -1, noiseThresholdToClear, setToOrigInsteadOfClear);
154 }
155 }
156 }
157
158 protected boolean setToOrigInsteadOfClear = false;
159 public boolean isSetToOrigInsteadOfClear() {
161 }
163 this.setToOrigInsteadOfClear = setToOrigInsteadOfClear;
164 }
165
166 public void setTileAt(float x, float y, int value) {
167 setTileAt(x, y, value, 0f);
168 }
169
170 public void setTileAt(float x, float y, int value, float noiseThresholdToClear) {
171 setTileAt(x, y, value, noiseThresholdToClear, false);
172 }
173 public void setTileAt(float x, float y, int value, float noiseThresholdToClear, boolean setToOrigTile) {
174 int cellX = (int) ((width / 2f + x - cx) / ts);
175 int cellY = (int) ((height / 2f + y - cy) / ts);
176
177// if (cellX < 0) cellX = 0;
178// if (cellY < 0) cellY = 0;
179// if (cellX > tiles.length - 1) cellX = tiles.length - 1;
180// if (cellY > tiles[0].length - 1) cellY = tiles[0].length - 1;
181
182 if (cellX < 0) return;
183 if (cellY < 0) return;
184 if (cellX > tiles.length - 1) return;
185 if (cellY > tiles[0].length - 1) return;
186// if (cellX < 0) cellX = 0;
187// if (cellY < 0) cellY = 0;
188// if (cellX > tiles.length - 1) cellX = tiles.length - 1;
189// if (cellY > tiles[0].length - 1) cellY = tiles[0].length - 1;
190
191 if (setToOrigTile) {
192 value = orig[cellX][cellY];
193 }
194 if (noiseThresholdToClear <= 0 || noise[cellX][cellY] > noiseThresholdToClear) {
195 tiles[cellX][cellY] = value;
196 }
197 //tiles[cellX][tiles[0].length - 1] = -1;
198 }
199
200
201
202}
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
void clearArc(float x, float y, float innerRadius, float outerRadius, float startAngle, float endAngle, float endRadiusMult, float noiseThresholdToClear)
void setTileAt(float x, float y, int value, float noiseThresholdToClear)
void setTileAt(float x, float y, int value, float noiseThresholdToClear, boolean setToOrigTile)
void setSetToOrigInsteadOfClear(boolean setToOrigInsteadOfClear)
void clearArc(float x, float y, float innerRadius, float outerRadius, float startAngle, float endAngle, float noiseThresholdToClear)
void clearArc(float x, float y, float innerRadius, float outerRadius, float startAngle, float endAngle)