Starsector API
Loading...
Searching...
No Matches
BaseTerrain.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.terrain;
2
3import java.util.EnumSet;
4import java.util.List;
5
6import java.awt.Color;
7
8import org.lwjgl.util.vector.Vector2f;
9
10import com.fs.starfarer.api.Global;
11import com.fs.starfarer.api.campaign.CampaignEngineLayers;
12import com.fs.starfarer.api.campaign.CampaignFleetAPI;
13import com.fs.starfarer.api.campaign.CampaignTerrainPlugin;
14import com.fs.starfarer.api.campaign.SectorEntityToken;
15import com.fs.starfarer.api.campaign.rules.MemoryAPI;
16import com.fs.starfarer.api.combat.ViewportAPI;
17import com.fs.starfarer.api.loading.TerrainSpecAPI;
18import com.fs.starfarer.api.ui.Alignment;
19import com.fs.starfarer.api.ui.TooltipMakerAPI;
20import com.fs.starfarer.api.util.Misc;
21
22public class BaseTerrain implements CampaignTerrainPlugin {
23
24 public static final float EXTRA_SOUND_RADIUS = 100f;
25
27 protected String terrainId;
28 protected String name = "Unknown";
29
30 public void init(String terrainId, SectorEntityToken entity, Object param) {
31 this.terrainId = terrainId;
32 this.entity = entity;
33 }
34
35 public String getIconSpriteName() {
36 return null;
37 }
38
40 return null;
41 }
42
44 return entity;
45 }
46
47 @Override
49 this.entity = entity;
50 }
51
52 public String getTerrainId() {
53 return terrainId;
54 }
55
56 @Override
57 public void setTerrainId(String id) {
58 terrainId = id;
59 }
60
61 protected boolean shouldCheckFleetsToApplyEffect() {
62 return true;
63 }
64
65 public void advance(float amount) {
66 if (amount <= 0) {
67 return; // happens during game load
68 }
69 List<CampaignFleetAPI> fleets = entity.getContainingLocation().getFleets();
70
71 // if (entity.isInCurrentLocation()) return;
72 // if (entity.isInCurrentLocation() &&
73 // entity.getContainingLocation().isHyperspace()) {
74 // System.out.println(entity.getContainingLocation().getTerrainCopy().size());
75 // System.out.println(entity.getContainingLocation().getFleets().size());
76 // }
77// if (entity.isPlayerFleet()) {
78// System.out.println("23fwefe");
79// }
81 float renderRange = getRenderRange();
82 // renderRange *= renderRange;
83 float days = Global.getSector().getClock().convertToDays(amount);
84 for (CampaignFleetAPI fleet : fleets) {
85 if (fleet.isStationMode()) {
86 continue;
87 }
88
89 float dist = Misc.getDistance(fleet.getLocation(), entity.getLocation());
90 if (dist > renderRange) {
91 continue;
92 }
93
94 String cat = getEffectCategory();
95 String key = "$terrain_" + cat;
96
97 MemoryAPI mem = fleet.getMemoryWithoutUpdate();
98 if (cat != null && !stacksWithSelf() && mem.contains(key)) {
99 continue;
100 }
101 // if (entity.isInCurrentLocation()) continue;
102 if (containsEntity(fleet)) {
103 applyEffect(fleet, days);
104 if (cat != null) {
105 mem.set(key, true, 0);
106 }
107 }
108 }
109 }
110
112 if (fleet != null && entity.isInCurrentLocation()) {
113 if (containsPoint(fleet.getLocation(), fleet.getRadius() + getExtraSoundRadius())) {
114 float prox = getProximitySoundFactor();
115 // System.out.println(getTerrainId() + " prox: " + prox);
116 float volumeMult = prox;
117 float suppressionMult = prox;
118 // suppressionMult = 1f;
119 // System.out.println(suppressionMult);
120 if (volumeMult > 0) {
121 if (shouldPlayLoopOne()) {
122 String soundId = getSpec().getLoopOne();
123 if (soundId != null) {
125 .suppressMusic(spec.getMusicSuppression() * suppressionMult);
126 Global.getSoundPlayer().playLoop(soundId, fleet, getLoopOnePitch(),
127 getLoopOneVolume() * volumeMult, fleet.getLocation(), Misc.ZERO);
128 }
129 }
130 if (shouldPlayLoopTwo()) {
131 String soundId = getSpec().getLoopTwo();
132 if (soundId != null) {
134 .suppressMusic(spec.getMusicSuppression() * suppressionMult);
135 Global.getSoundPlayer().playLoop(soundId, fleet, getLoopTwoPitch(),
136 getLoopTwoVolume() * volumeMult, fleet.getLocation(), Misc.ZERO);
137 }
138 }
139 if (shouldPlayLoopThree()) {
140 String soundId = getSpec().getLoopThree();
141 if (soundId != null) {
143 .suppressMusic(spec.getMusicSuppression() * suppressionMult);
145 getLoopThreeVolume() * volumeMult, fleet.getLocation(), Misc.ZERO);
146 }
147 }
148 if (shouldPlayLoopFour()) {
149 String soundId = getSpec().getLoopFour();
150 if (soundId != null) {
152 .suppressMusic(spec.getMusicSuppression() * suppressionMult);
154 getLoopFourVolume() * volumeMult, fleet.getLocation(), Misc.ZERO);
155 }
156 }
157 }
158 }
159 }
160 }
161
162 protected float getExtraSoundRadius() {
163 return EXTRA_SOUND_RADIUS;
164 }
165
166 public String getEffectCategory() {
167 throw new RuntimeException("Override BaseTerrain.getEffectCategory()");
168 }
169
170 public boolean containsEntity(SectorEntityToken other) {
171 return containsPoint(other.getLocation(), other.getRadius()) && !isPreventedFromAffecting(other);
172 }
173
174 public boolean containsPoint(Vector2f point, float radius) {
175 return false;
176 }
177
178 public boolean stacksWithSelf() {
179 return false;
180 }
181
182 public void applyEffect(SectorEntityToken entity, float days) {
183
184 }
185
186 public float getProximitySoundFactor() {
187 return 1f;
188 }
189
190 public String getModId() {
191 return terrainId + "_stat_mod";
192 }
193
194 public EnumSet<CampaignEngineLayers> getActiveLayers() {
195 throw new RuntimeException(
196 "Override BaseTerrain.getActiveLayers() to return the CampaignEngineLayers the terrain should render in.");
197 }
198
199 public float getRenderRange() {
200 throw new RuntimeException(
201 "Override BaseTerrain.getRenderRange() to return the maximum distance to render this terrain at (should exceed visible radius).");
202 }
203
204 public void render(CampaignEngineLayers layer, ViewportAPI viewport) {
205
206 }
207
208 public void renderOnMap(float factor, float alphaMult) {
209
210 }
211
212 public void renderOnMapAbove(float factor, float alphaMult) {
213
214 }
215
216 public boolean hasTooltip() {
217 return false;
218 }
219
220 // public void createTooltip(TooltipMakerAPI tooltip, boolean expanded) {
221 // }
222
223 protected void createFirstSection(TooltipMakerAPI tooltip, boolean expanded) {
224 }
225
226 protected void createTravelSection(TooltipMakerAPI tooltip, boolean expanded, float firstPad) {
227 }
228
229 protected void createCombatSection(TooltipMakerAPI tooltip, boolean expanded) {
230 }
231
232 protected boolean shouldPlayLoopOne() {
233 return getSpec().getLoopOne() != null;
234 }
235
236 protected boolean shouldPlayLoopTwo() {
237 return getSpec().getLoopTwo() != null;
238 }
239
240 protected boolean shouldPlayLoopThree() {
241 return getSpec().getLoopThree() != null;
242 }
243
244 protected boolean shouldPlayLoopFour() {
245 return getSpec().getLoopFour() != null;
246 }
247
248 protected float getLoopOnePitch() {
249 return 1f;
250 }
251
252 protected float getLoopOneVolume() {
253 return 1f;
254 }
255
256 protected float getLoopTwoPitch() {
257 return 1f;
258 }
259
260 protected float getLoopTwoVolume() {
261 return 1f;
262 }
263
264 protected float getLoopThreePitch() {
265 return 1f;
266 }
267
268 protected float getLoopThreeVolume() {
269 return 1f;
270 }
271
272 protected float getLoopFourPitch() {
273 return 1f;
274 }
275
276 protected float getLoopFourVolume() {
277 return 1f;
278 }
279
280 public void createTooltip(TooltipMakerAPI tooltip, boolean expanded) {
281 float pad = 10f;
282 float small = 5f;
283
284 createFirstSection(tooltip, expanded);
285
286 float nextPad = pad;
287 if (expanded) {
288 tooltip.addSectionHeading("Travel", Alignment.MID, pad);
289 nextPad = small;
290 }
291
292 createTravelSection(tooltip, expanded, nextPad);
293
294 if (expanded) {
295 tooltip.addSectionHeading("Combat", Alignment.MID, pad);
296 createCombatSection(tooltip, expanded);
297 }
298 }
299
300 public boolean isTooltipExpandable() {
301 return true;
302 }
303
304 public float getTooltipWidth() {
305 return 350f;
306 }
307
308 public String getTerrainName() {
309 return name;
310 }
311
312 public String getNameAOrAn() {
313 return "a";
314 }
315
316 public void setTerrainName(String name) {
317 this.name = name;
318 }
319
320 public Color getNameColor() {
321 return Global.getSettings().getColor("buttonText");
322 }
323
324 public boolean hasAIFlag(Object flag) {
325 return false;
326 }
327
328 public boolean hasAIFlag(Object flag, CampaignFleetAPI fleet) {
329 return hasAIFlag(flag);
330 }
331
332 public float getMaxEffectRadius(Vector2f locFrom) {
333 return 0f;
334 }
335
336 public float getMinEffectRadius(Vector2f locFrom) {
337 return 0f;
338 }
339
340 public float getOptimalEffectRadius(Vector2f locFrom) {
341 return 0f;
342 }
343
344 public boolean hasMapIcon() {
345 return true;
346 }
347
348 private transient TerrainSpecAPI spec = null;
349
351 if (spec != null)
352 return spec;
354 return spec;
355 }
356
357 public boolean canPlayerHoldStationIn() {
358 return true;
359 }
360
361 public void renderOnRadar(Vector2f radarCenter, float factor, float alphaMult) {
362
363 }
364
365 public String getNameForTooltip() {
366 return getTerrainName();
367 }
368
369 // protected boolean doNotLowerCaseName = false;
370 // public String getTerrainNameLowerCase() {
371 // if (doNotLowerCaseName) return getTerrainName();
372 // return getTerrainName().toLowerCase();
373 // }
374
375
376 public static String TERRAIN_LOCK_KEY = "$terrain_mutex_key";
377
379 if (other.getMemoryWithoutUpdate() == null) return false;
380 String id = entity.getId();
381 String key = TERRAIN_LOCK_KEY;
382 String lockId = other.getMemoryWithoutUpdate().getString(key);
383 return lockId != null && !lockId.equals(id);
384 }
388 protected void preventOtherTerrainFromAffecting(SectorEntityToken other, float dur) {
389 String id = entity.getId();
391 }
392
393}
394
395
396
static SettingsAPI getSettings()
Definition Global.java:57
static SoundPlayerAPI getSoundPlayer()
Definition Global.java:49
static SectorAPI getSector()
Definition Global.java:65
boolean containsPoint(Vector2f point, float radius)
void applyEffect(SectorEntityToken entity, float days)
void createTravelSection(TooltipMakerAPI tooltip, boolean expanded, float firstPad)
boolean hasAIFlag(Object flag, CampaignFleetAPI fleet)
void preventOtherTerrainFromAffecting(SectorEntityToken other)
EnumSet< CampaignEngineLayers > getActiveLayers()
void createCombatSection(TooltipMakerAPI tooltip, boolean expanded)
void renderOnMap(float factor, float alphaMult)
void preventOtherTerrainFromAffecting(SectorEntityToken other, float dur)
void createTooltip(TooltipMakerAPI tooltip, boolean expanded)
void init(String terrainId, SectorEntityToken entity, Object param)
boolean isPreventedFromAffecting(SectorEntityToken other)
void renderOnRadar(Vector2f radarCenter, float factor, float alphaMult)
void renderOnMapAbove(float factor, float alphaMult)
void render(CampaignEngineLayers layer, ViewportAPI viewport)
void createFirstSection(TooltipMakerAPI tooltip, boolean expanded)
static final Vector2f ZERO
Definition Misc.java:249
static float getDistance(SectorEntityToken from, SectorEntityToken to)
Definition Misc.java:599
TerrainSpecAPI getTerrainSpec(String terrainId)
void playLoop(String id, Object playingEntity, float pitch, float volume, Vector2f loc, Vector2f vel)
List< CampaignFleetAPI > getFleets()
void set(String key, Object value)
LabelAPI addSectionHeading(String str, Alignment align, float pad)