Starsector API
Loading...
Searching...
No Matches
Cryorevival.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.econ.impl;
2
3import java.util.Map;
4
5import java.awt.Color;
6
7import org.lwjgl.util.vector.Vector2f;
8
9import com.fs.starfarer.api.Global;
10import com.fs.starfarer.api.campaign.SectorEntityToken;
11import com.fs.starfarer.api.campaign.comm.IntelInfoPlugin;
12import com.fs.starfarer.api.campaign.econ.CommoditySpecAPI;
13import com.fs.starfarer.api.campaign.econ.MarketAPI;
14import com.fs.starfarer.api.campaign.econ.MarketImmigrationModifier;
15import com.fs.starfarer.api.campaign.listeners.ColonyOtherFactorsListener;
16import com.fs.starfarer.api.impl.PlanetSearchData.PlanetFilter;
17import com.fs.starfarer.api.impl.campaign.ids.Commodities;
18import com.fs.starfarer.api.impl.campaign.ids.Factions;
19import com.fs.starfarer.api.impl.campaign.ids.Industries;
20import com.fs.starfarer.api.impl.campaign.intel.misc.CryosleeperIntel;
21import com.fs.starfarer.api.impl.campaign.population.PopulationComposition;
22import com.fs.starfarer.api.loading.IndustrySpecAPI;
23import com.fs.starfarer.api.ui.TooltipMakerAPI;
24import com.fs.starfarer.api.util.Misc;
25import com.fs.starfarer.api.util.Pair;
26
27public class Cryorevival extends BaseIndustry implements MarketImmigrationModifier {
28
29 public static float ALPHA_CORE_BONUS = 1f;
30 public static float IMPROVE_BONUS = 1f;
31
32 public static float MIN_BONUS_MULT = 0.1f;
33 public static float MAX_BONUS_DIST_LY = 10f;
34
35 public static float MAX_BONUS_WHEN_UNMET_DEMAND = 0.5f;
36
37 public void apply() {
38 super.apply(true);
39
40 //int size = market.getSize();
41 //demand(Commodities.HEAVY_MACHINERY, size);
43 }
44
45
46 @Override
47 public void unapply() {
48 super.unapply();
49 }
50
51 protected boolean hasPostDemandSection(boolean hasDemand, IndustryTooltipMode mode) {
52 return mode != IndustryTooltipMode.NORMAL || isFunctional();
53 }
54
55 @Override
56 protected void addPostDemandSection(TooltipMakerAPI tooltip, boolean hasDemand, IndustryTooltipMode mode) {
57 if (mode != IndustryTooltipMode.NORMAL || isFunctional()) {
58 Color h = Misc.getHighlightColor();
59 float opad = 10f;
60
61 float bonus = getImmigrationBonus();
62 float max = getMaxImmigrationBonus();
63
65 float demandMult = getDemandMetPopulationMult();
66
68 if (p != null) {
69 String dStr = "" + Misc.getRoundedValueMaxOneAfterDecimal(p.two);
70 String lights = "light-years";
71 if (dStr.equals("1")) lights = "light-year";
72 tooltip.addPara("Distance to nearest cryosleeper: %s " + lights + ", growth bonus multiplier: %s.",
73 opad, h,
75 "" + (int)Math.round(distMult * 100f) + "%");
76 }
77 if (mode != IndustryTooltipMode.NORMAL) {
78 tooltip.addPara("If any demand is unmet, " +
79 "the maximum growth bonus is reduced by %s.",
80 opad, h,
81 "" + (int)Math.round(MAX_BONUS_WHEN_UNMET_DEMAND * 100f) + "%");
82 } else {
83 tooltip.addPara("%s growth bonus multiplier based on met demand. If any demand is unmet, " +
84 "the maximum bonus is reduced by %s.",
85 opad, h,
86 "" + (int)Math.round(demandMult * 100f) + "%",
87 "" + (int)Math.round(MAX_BONUS_WHEN_UNMET_DEMAND * 100f) + "%");
88 }
89
90
91 tooltip.addPara("Population growth: %s (max for colony size: %s)", opad, h, "+" + Math.round(bonus), "+" + Math.round(max));
92 }
93 }
94
95
96 @Override
97 public boolean isAvailableToBuild() {
98 //if (Global.getSettings().isDevMode()) return true;
99
101
102// StarSystemAPI system = market.getStarSystem();
103// if (system == null) return false;
104// for (SectorEntityToken entity : system.getEntitiesWithTag(Tags.CRYOSLEEPER)) {
105// if (entity.getMemoryWithoutUpdate().contains("$usable")) {
106// return true;
107// }
108// }
109// return false;
110 }
111
112
113 @Override
114 public boolean showWhenUnavailable() {
115 return false;
116 }
117
118
119 @Override
120 public String getUnavailableReason() {
121 return "Requires in-system cryosleeper"; // unused since not shown when unavailable
122 }
123
125 if (isFunctional()) {
126 incoming.add(Factions.SLEEPER, getImmigrationBonus() * 2f);
128
129 if (Commodities.ALPHA_CORE.equals(getAICoreId())) {
131 "Alpha core (" + getNameForModifier() + ")");
132 }
133 if (isImproved()) {
136 }
137 }
138 }
139
140
141 @Override
142 protected void applyAlphaCoreModifiers() {
143 }
144
145 @Override
146 protected void applyNoAICoreModifiers() {
147 }
148
149 @Override
153
154 protected void addAlphaCoreDescription(TooltipMakerAPI tooltip, AICoreDescriptionMode mode) {
155 float opad = 10f;
156 Color highlight = Misc.getHighlightColor();
157
158 String pre = "Alpha-level AI core currently assigned. ";
159 if (mode == AICoreDescriptionMode.MANAGE_CORE_DIALOG_LIST || mode == AICoreDescriptionMode.INDUSTRY_TOOLTIP) {
160 pre = "Alpha-level AI core. ";
161 }
163 String str = "+" + (int)Math.round(a);
164
165 if (mode == AICoreDescriptionMode.INDUSTRY_TOOLTIP) {
167 TooltipMakerAPI text = tooltip.beginImageWithText(coreSpec.getIconName(), 48);
168 text.addPara(pre + "Reduces upkeep cost by %s. Reduces demand by %s unit. " +
169 "%s population growth.", 0f, highlight,
170 "" + (int)((1f - UPKEEP_MULT) * 100f) + "%", "" + DEMAND_REDUCTION,
171 str);
172 tooltip.addImageWithText(opad);
173 return;
174 }
175
176 tooltip.addPara(pre + "Reduces upkeep cost by %s. Reduces demand by %s unit. " +
177 "%s population growth.", opad, highlight,
178 "" + (int)((1f - UPKEEP_MULT) * 100f) + "%", "" + DEMAND_REDUCTION,
179 str);
180
181 }
182
183
184 @Override
185 public boolean canImprove() {
186 return true;
187 }
188
189 public void addImproveDesc(TooltipMakerAPI info, ImprovementDescriptionMode mode) {
190 float opad = 10f;
191 Color highlight = Misc.getHighlightColor();
192
194 String str = "" + (int)Math.round(a);
195
196 if (mode == ImprovementDescriptionMode.INDUSTRY_TOOLTIP) {
197 info.addPara("Population growth increased by %s.", 0f, highlight,str);
198 } else {
199 info.addPara("Increases population growth by %s.", 0f, highlight,str);
200 }
201
202 info.addSpacer(opad);
203 super.addImproveDesc(info, mode);
204 }
205
206
207 public static Pair<SectorEntityToken, Float> getNearestCryosleeper(Vector2f locInHyper, boolean usable) {
208 SectorEntityToken nearest = null;
209 float minDist = Float.MAX_VALUE;
210
211 //for (SectorEntityToken entity : Global.getSector().getCustomEntitiesWithTag(Tags.CRYOSLEEPER)) {
213 CryosleeperIntel cryo = (CryosleeperIntel) intel;
214 SectorEntityToken entity = cryo.getEntity();
215 if (!usable || entity.getMemoryWithoutUpdate().contains("$usable")) {
216 float dist = Misc.getDistanceLY(locInHyper, entity.getLocationInHyperspace());
217 if (dist > MAX_BONUS_DIST_LY && Math.round(dist * 10f) <= MAX_BONUS_DIST_LY * 10f) {
218 dist = MAX_BONUS_DIST_LY;
219 }
220 if (dist < minDist) {
221 minDist = dist;
222 nearest = entity;
223 }
224 }
225 }
226
227 if (nearest == null) return null;
228
229 return new Pair<SectorEntityToken, Float>(nearest, minDist);
230 }
231
232
233 public static float getDistancePopulationMult(Vector2f locInHyper) {
235 if (p == null) return 0f;
236 if (p.two > MAX_BONUS_DIST_LY) return 0f;
237
238
239 float f = 1f - p.two / MAX_BONUS_DIST_LY;
240 if (f < 0f) f = 0f;
241 if (f > 1f) f = 1f;
242
243 float mult = MIN_BONUS_MULT + (1f - MIN_BONUS_MULT) * f;
244
245 return mult;
246 }
247
248 protected float getDemandMetPopulationMult() {
251 float def = deficit.two;
252 if (def > demand) def = demand;
253
254 float mult = 1f;
255 if (def > 0 && demand > 0) {
256 mult = (demand - def) / demand;
258 }
259 return mult;
260 }
261
265
266 protected float getMaxImmigrationBonus() {
267 return getSizeMult() * 10f;
268 }
269
270
271 public static class CryosleeperFactor implements ColonyOtherFactorsListener, PlanetFilter {
272 public boolean isActiveFactorFor(SectorEntityToken entity) {
273 return getNearestCryosleeper(entity.getLocationInHyperspace(), true) != null;
274 }
275
276 public void printOtherFactors(TooltipMakerAPI text, SectorEntityToken entity) {
277 float distMult = getDistancePopulationMult(entity.getLocationInHyperspace());
278
280 if (p != null) {
281 Color h = Misc.getHighlightColor();
282 float opad = 10f;
283
284 String dStr = "" + Misc.getRoundedValueMaxOneAfterDecimal(p.two);
285 String lights = "light-years";
286 if (dStr.equals("1")) lights = "light-year";
287
288 if (p.two > MAX_BONUS_DIST_LY) {
289 text.addPara("The nearest cryosleeper is located in the " +
290 p.one.getContainingLocation().getNameWithLowercaseType() + ", %s " + lights + " away. The maximum " +
291 "range at which sleepers can be safely brought over for revival is %s light-years.",
292 opad, h,
294 "" + (int)MAX_BONUS_DIST_LY);
295 } else {
296 text.addPara("The nearest cryosleeper is located in the " +
297 p.one.getContainingLocation().getNameWithLowercaseType() + ", %s " + lights + " away, allowing " +
298 "a Cryorevival Facility built here to operate at %s effectiveness.",
299 opad, h,
301 "" + (int)Math.round(distMult * 100f) + "%");
302 }
303 }
304 }
305
306 public String getOtherFactorId() {
307 return "cryosleeper";
308 }
309
310 public String getOtherFactorButtonText() {
311 return "Domain-era Cryosleeper within range";
312 }
313
314 @Override
315 public boolean accept(SectorEntityToken entity, Map<String, String> params) {
316 if (!params.containsKey(getOtherFactorId())) return true;
317
318 if (entity.getMarket() == null) return false;
319
320 Pair<SectorEntityToken, Float> p = Cryorevival.getNearestCryosleeper(entity.getLocationInHyperspace(), false);
321 if (p == null || p.two > Cryorevival.MAX_BONUS_DIST_LY) return false;
322 return true;
323 }
324
325 @Override
326 public boolean shouldShow() {
327 return Global.getSector().getIntelManager().getIntelCount(CryosleeperIntel.class, true) > 0;
328 }
329 @Override
330 public void createTooltip(TooltipMakerAPI info, float width, String param) {
331 float opad = 10f;
332 Color h = Misc.getHighlightColor();
333 IndustrySpecAPI spec = Global.getSettings().getIndustrySpec(Industries.CRYOREVIVAL);
334 info.addTitle("Cryosleeper");
335 info.addPara("Only show planets within %s light-years of a Domain-era Cryosleeper. Colonies "
336 + "within range can build a %s and benefit from hugely increased population growth.",
337 opad, h, "" + (int)Math.round(Cryorevival.MAX_BONUS_DIST_LY), spec.getName());
338 }
339 }
340
341}
342
343
344
static SettingsAPI getSettings()
Definition Global.java:57
static SectorAPI getSector()
Definition Global.java:65
void modifyFlat(String source, float value)
Map< String, MutableCommodityQuantity > demand
Pair< String, Integer > getMaxDeficit(String ... commodityIds)
boolean hasPostDemandSection(boolean hasDemand, IndustryTooltipMode mode)
void addPostDemandSection(TooltipMakerAPI tooltip, boolean hasDemand, IndustryTooltipMode mode)
void addAlphaCoreDescription(TooltipMakerAPI tooltip, AICoreDescriptionMode mode)
void modifyIncoming(MarketAPI market, PopulationComposition incoming)
static float getDistancePopulationMult(Vector2f locInHyper)
void addImproveDesc(TooltipMakerAPI info, ImprovementDescriptionMode mode)
static Pair< SectorEntityToken, Float > getNearestCryosleeper(Vector2f locInHyper, boolean usable)
static float getDistanceLY(SectorEntityToken from, SectorEntityToken to)
Definition Misc.java:602
static Color getHighlightColor()
Definition Misc.java:792
static String getRoundedValueMaxOneAfterDecimal(float value)
Definition Misc.java:673
CommoditySpecAPI getCommoditySpec(String commodityId)
LabelAPI addPara(String format, float pad, Color hl, String... highlights)
UIComponentAPI addSpacer(float height)
TooltipMakerAPI beginImageWithText(String spriteName, float imageHeight)
UIPanelAPI addImageWithText(float pad)