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