Starsector API
Loading...
Searching...
No Matches
RangeBlockerUtil.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.terrain;
2
3import com.fs.starfarer.api.campaign.PlanetAPI;
4import com.fs.starfarer.api.campaign.SectorEntityToken;
5import com.fs.starfarer.api.util.Misc;
6
7public class RangeBlockerUtil {
8
9 private int resolution;
10 private float maxRange;
11// private LocationAPI location;
12// private SectorEntityToken entity;
13// private SectorEntityToken exclude;
14
15 private float degreesPerUnit;
16 private float [] limits;
17 private float [] curr;
18 //private float [] alphas;
19
20 private boolean wasUpdated = false;
21 private boolean isAnythingShortened = false;
22
23 public RangeBlockerUtil(int resolution, float maxRange) {
24 this.resolution = resolution;
25 this.maxRange = maxRange;
26
27 //degreesPerUnit = (float) Math.floor(360f / (float) resolution);
28 degreesPerUnit = 360f / (float) resolution;
29
30 limits = new float [resolution];
31 curr = new float [resolution];
32 //alphas = new float [resolution];
33 }
34
35
36 public boolean wasEverUpdated() {
37 return wasUpdated;
38 }
39
40
41 public void updateAndSync(SectorEntityToken entity, SectorEntityToken exclude, float diffMult) {
42 updateLimits(entity, exclude, diffMult);
43 sync();
44// for (int i = 0; i < resolution; i++) {
45// alphas[i] = 1f;
46// }
47 }
48
49 public void sync() {
50 for (int i = 0; i < resolution; i++) {
51 curr[i] = limits[i];
52 }
53 }
54
55 public float getShortenAmountAt(float angle) {
56 return maxRange - getCurrMaxAt(angle);
57 }
58
59 public float getCurrMaxAt(float angle) {
60 angle = Misc.normalizeAngle(angle);
61
62 float index = angle / 360f * resolution;
63 int i1 = (int) Math.floor(index);
64 int i2 = (int) Math.ceil(index);
65 while (i1 >= resolution) i1 -= resolution;
66 while (i2 >= resolution) i2 -= resolution;
67
68 float v1 = curr[i1];
69 float v2 = curr[i2];
70
71 return v1 + (v2 - v1) * (index - (int) index);
72
73 //int index = getIndexForAngle(angle);
74 //return curr[index];
75 }
76
77 public float getAlphaAt(float angle) {
78 return 1f;
79// int index = getIndexForAngle(angle);
80// return alphas[index];
81 }
82
83 public void advance(float amount, float minApproachSpeed, float diffMult) {
84 for (int i = 0; i < resolution; i++) {
85 curr[i] = Misc.approach(curr[i], limits[i], minApproachSpeed, diffMult, amount);
86// if (curr[i] > limits[i] + 100f) {
87// alphas[i] = Misc.approach(alphas[i], 0f, 1f, 1f, amount);
88// } else {
89// alphas[i] = Misc.approach(alphas[i], 1f, 1f, 1f, amount);
90// }
91 }
92 }
93
94 public void updateLimits(SectorEntityToken entity, SectorEntityToken exclude, float diffMult) {
95 //if (true) return;
96
97 for (int i = 0; i < resolution; i++) {
98 limits[i] = maxRange;
99 }
100
101 isAnythingShortened = false;
102
103 for (PlanetAPI planet : entity.getContainingLocation().getPlanets()) {
104 if (planet == entity || planet == exclude) continue;
105 float dist = Misc.getDistance(entity.getLocation(), planet.getLocation());
106 if (dist > maxRange) continue;
107
108 float graceRadius = 100f;
109 graceRadius = 0f;
110 graceRadius = planet.getRadius() + 100f;
111 float span = Misc.computeAngleSpan(planet.getRadius() + graceRadius, dist);
112
113 float angle = Misc.getAngleInDegrees(entity.getLocation(), planet.getLocation());
114
115 float offsetSize = maxRange * 0.2f;
116
117 float spanOffset = span * 0.4f * 1f / diffMult;
118 spanOffset = 0f;
119 for (float f = angle - span/2f - spanOffset; f <= angle + span/2f - spanOffset; f += degreesPerUnit) {
120 float offset = Math.abs(f - angle) / (span / 2f);
121 if (offset > 1) offset = 1;
122 offset = (1f - (float) Math.cos(offset * 3.1416f / 2f));
123 //offset = (float) Math.sqrt(offset);
124// offset += 1f;
125 offset *= offset;
126// offset *= offset;
127// offset -= 1f;
128 //offset *= planet.getRadius() + graceRadius;
129 offset *= offsetSize;
130
131
132 int index = getIndexForAngle(f);
133 limits[index] = Math.min(dist - (planet.getRadius()) * 0.5f + offset, limits[index]);
134 isAnythingShortened = true;
135 }
136 }
137
138 wasUpdated = true;
139 }
140
141 public void block(float angle, float arc, float limit) {
142 //float radius = Misc.computeAngleRadius(angle, limit);
143 float offsetSize = Math.max(limit, maxRange * 0.1f);
144
145 for (float f = angle - arc/2f; f <= angle + arc/2f; f += degreesPerUnit) {
146
147 float offset = Math.abs(f - angle) / (arc / 2f);
148 if (offset > 1) offset = 1;
149 offset = (1f - (float) Math.cos(offset * 3.1416f / 2f));
150 offset += 1f;
151 offset *= offset;
152 offset -= 1f;
153 offset *= offsetSize;
154 //offset = 0f;
155 offset = Math.abs(offset);
156
157 int index = getIndexForAngle(f);
158 limits[index] = Math.min(limit + offset, limits[index]);
159 isAnythingShortened = true;
160 }
161 }
162
163 public int getIndexForAngle(float angle) {
164 angle = Misc.normalizeAngle(angle);
165
166 int index = (int)Math.round(angle / 360f * (float) resolution);
167 if (index < 0) index = 0;
168 //if (index > resolution - 1) index = resolution - 1;
169 while (index >= resolution) index -= resolution;
170
171 return index;
172 }
173
174 public boolean isAnythingShortened() {
175 return isAnythingShortened;
176 }
177
178
179 public float getMaxRange() {
180 return maxRange;
181 }
182
183
184}
185
186
187
188
189
190
191
192
193
194
195
void advance(float amount, float minApproachSpeed, float diffMult)
void updateLimits(SectorEntityToken entity, SectorEntityToken exclude, float diffMult)
void updateAndSync(SectorEntityToken entity, SectorEntityToken exclude, float diffMult)