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.awt.Color;
4
5import org.lwjgl.util.vector.Vector2f;
6
7import com.fs.starfarer.api.Global;
8import com.fs.starfarer.api.campaign.SectorEntityToken;
9import com.fs.starfarer.api.campaign.econ.CommoditySpecAPI;
10import com.fs.starfarer.api.campaign.econ.MarketAPI;
11import com.fs.starfarer.api.campaign.econ.MarketImmigrationModifier;
12import com.fs.starfarer.api.campaign.listeners.ColonyOtherFactorsListener;
13import com.fs.starfarer.api.impl.campaign.ids.Commodities;
14import com.fs.starfarer.api.impl.campaign.ids.Factions;
15import com.fs.starfarer.api.impl.campaign.ids.Tags;
16import com.fs.starfarer.api.impl.campaign.population.PopulationComposition;
17import com.fs.starfarer.api.ui.TooltipMakerAPI;
18import com.fs.starfarer.api.util.Misc;
19import com.fs.starfarer.api.util.Pair;
20
21public class Cryorevival extends BaseIndustry implements MarketImmigrationModifier {
22
23 public static float ALPHA_CORE_BONUS = 1f;
24 public static float IMPROVE_BONUS = 1f;
25
26 public static float MIN_BONUS_MULT = 0.1f;
27 public static float MAX_BONUS_DIST_LY = 10f;
28
29 public static float MAX_BONUS_WHEN_UNMET_DEMAND = 0.5f;
30
31 public void apply() {
32 super.apply(true);
33
34 //int size = market.getSize();
35 //demand(Commodities.HEAVY_MACHINERY, size);
36 demand(Commodities.ORGANICS, 10);
37 }
38
39
40 @Override
41 public void unapply() {
42 super.unapply();
43 }
44
45 protected boolean hasPostDemandSection(boolean hasDemand, IndustryTooltipMode mode) {
46 return mode != IndustryTooltipMode.NORMAL || isFunctional();
47 }
48
49 @Override
50 protected void addPostDemandSection(TooltipMakerAPI tooltip, boolean hasDemand, IndustryTooltipMode mode) {
51 if (mode != IndustryTooltipMode.NORMAL || isFunctional()) {
52 Color h = Misc.getHighlightColor();
53 float opad = 10f;
54
55 float bonus = getImmigrationBonus();
56 float max = getMaxImmigrationBonus();
57
58 float distMult = getDistancePopulationMult(market.getLocationInHyperspace());
59 float demandMult = getDemandMetPopulationMult();
60
61 Pair<SectorEntityToken, Float> p = getNearestCryosleeper(market.getLocationInHyperspace(), true);
62 if (p != null) {
63 String dStr = "" + Misc.getRoundedValueMaxOneAfterDecimal(p.two);
64 String lights = "light-years";
65 if (dStr.equals("1")) lights = "light-year";
66 tooltip.addPara("Distance to nearest cryosleeper: %s " + lights + ", growth bonus multiplier: %s.",
67 opad, h,
68 "" + Misc.getRoundedValueMaxOneAfterDecimal(p.two),
69 "" + (int)Math.round(distMult * 100f) + "%");
70 }
71 if (mode != IndustryTooltipMode.NORMAL) {
72 tooltip.addPara("If any demand is unmet, " +
73 "the maximum growth bonus is reduced by %s.",
74 opad, h,
75 "" + (int)Math.round(MAX_BONUS_WHEN_UNMET_DEMAND * 100f) + "%");
76 } else {
77 tooltip.addPara("%s growth bonus multiplier based on met demand. If any demand is unmet, " +
78 "the maximum bonus is reduced by %s.",
79 opad, h,
80 "" + (int)Math.round(demandMult * 100f) + "%",
81 "" + (int)Math.round(MAX_BONUS_WHEN_UNMET_DEMAND * 100f) + "%");
82 }
83
84
85 tooltip.addPara("Population growth: %s (max for colony size: %s)", opad, h, "+" + Math.round(bonus), "+" + Math.round(max));
86 }
87 }
88
89
90 @Override
91 public boolean isAvailableToBuild() {
92 //if (Global.getSettings().isDevMode()) return true;
93
94 return getDistancePopulationMult(market.getLocationInHyperspace()) > 0;
95
96// StarSystemAPI system = market.getStarSystem();
97// if (system == null) return false;
98// for (SectorEntityToken entity : system.getEntitiesWithTag(Tags.CRYOSLEEPER)) {
99// if (entity.getMemoryWithoutUpdate().contains("$usable")) {
100// return true;
101// }
102// }
103// return false;
104 }
105
106
107 @Override
108 public boolean showWhenUnavailable() {
109 return false;
110 }
111
112
113 @Override
114 public String getUnavailableReason() {
115 return "Requires in-system cryosleeper"; // unused since not shown when unavailable
116 }
117
118 public void modifyIncoming(MarketAPI market, PopulationComposition incoming) {
119 if (isFunctional()) {
120 incoming.add(Factions.SLEEPER, getImmigrationBonus() * 2f);
121 incoming.getWeight().modifyFlat(getModId(), getImmigrationBonus(), getNameForModifier());
122
123 if (Commodities.ALPHA_CORE.equals(getAICoreId())) {
124 incoming.getWeight().modifyFlat(getModId(1), (int)(getImmigrationBonus() * ALPHA_CORE_BONUS),
125 "Alpha core (" + getNameForModifier() + ")");
126 }
127 if (isImproved()) {
128 incoming.getWeight().modifyFlat(getModId(2), (int)(getImmigrationBonus() * IMPROVE_BONUS),
130 }
131 }
132 }
133
134
135 @Override
136 protected void applyAlphaCoreModifiers() {
137 }
138
139 @Override
140 protected void applyNoAICoreModifiers() {
141 }
142
143 @Override
145 demandReduction.modifyFlat(getModId(0), DEMAND_REDUCTION, "Alpha core");
146 }
147
148 protected void addAlphaCoreDescription(TooltipMakerAPI tooltip, AICoreDescriptionMode mode) {
149 float opad = 10f;
150 Color highlight = Misc.getHighlightColor();
151
152 String pre = "Alpha-level AI core currently assigned. ";
153 if (mode == AICoreDescriptionMode.MANAGE_CORE_DIALOG_LIST || mode == AICoreDescriptionMode.INDUSTRY_TOOLTIP) {
154 pre = "Alpha-level AI core. ";
155 }
157 String str = "+" + (int)Math.round(a);
158
159 if (mode == AICoreDescriptionMode.INDUSTRY_TOOLTIP) {
160 CommoditySpecAPI coreSpec = Global.getSettings().getCommoditySpec(aiCoreId);
161 TooltipMakerAPI text = tooltip.beginImageWithText(coreSpec.getIconName(), 48);
162 text.addPara(pre + "Reduces upkeep cost by %s. Reduces demand by %s unit. " +
163 "%s population growth.", 0f, highlight,
164 "" + (int)((1f - UPKEEP_MULT) * 100f) + "%", "" + DEMAND_REDUCTION,
165 str);
166 tooltip.addImageWithText(opad);
167 return;
168 }
169
170 tooltip.addPara(pre + "Reduces upkeep cost by %s. Reduces demand by %s unit. " +
171 "%s population growth.", opad, highlight,
172 "" + (int)((1f - UPKEEP_MULT) * 100f) + "%", "" + DEMAND_REDUCTION,
173 str);
174
175 }
176
177
178 @Override
179 public boolean canImprove() {
180 return true;
181 }
182
183 public void addImproveDesc(TooltipMakerAPI info, ImprovementDescriptionMode mode) {
184 float opad = 10f;
185 Color highlight = Misc.getHighlightColor();
186
188 String str = "" + (int)Math.round(a);
189
190 if (mode == ImprovementDescriptionMode.INDUSTRY_TOOLTIP) {
191 info.addPara("Population growth increased by %s.", 0f, highlight,str);
192 } else {
193 info.addPara("Increases population growth by %s.", 0f, highlight,str);
194 }
195
196 info.addSpacer(opad);
197 super.addImproveDesc(info, mode);
198 }
199
200
201 public static Pair<SectorEntityToken, Float> getNearestCryosleeper(Vector2f locInHyper, boolean usable) {
202 SectorEntityToken nearest = null;
203 float minDist = Float.MAX_VALUE;
204
205 for (SectorEntityToken entity : Global.getSector().getCustomEntitiesWithTag(Tags.CRYOSLEEPER)) {
206 if (!usable || entity.getMemoryWithoutUpdate().contains("$usable")) {
207 float dist = Misc.getDistanceLY(locInHyper, entity.getLocationInHyperspace());
208 if (dist > MAX_BONUS_DIST_LY && Math.round(dist * 10f) <= MAX_BONUS_DIST_LY * 10f) {
209 dist = MAX_BONUS_DIST_LY;
210 }
211 if (dist < minDist) {
212 minDist = dist;
213 nearest = entity;
214 }
215 }
216 }
217
218 if (nearest == null) return null;
219
220 return new Pair<SectorEntityToken, Float>(nearest, minDist);
221 }
222
223
224 public static float getDistancePopulationMult(Vector2f locInHyper) {
225 Pair<SectorEntityToken, Float> p = getNearestCryosleeper(locInHyper, true);
226 if (p == null) return 0f;
227 if (p.two > MAX_BONUS_DIST_LY) return 0f;
228
229
230 float f = 1f - p.two / MAX_BONUS_DIST_LY;
231 if (f < 0f) f = 0f;
232 if (f > 1f) f = 1f;
233
234 float mult = MIN_BONUS_MULT + (1f - MIN_BONUS_MULT) * f;
235
236 return mult;
237 }
238
239 protected float getDemandMetPopulationMult() {
240 Pair<String, Integer> deficit = getMaxDeficit(Commodities.ORGANICS);
241 float demand = getDemand(Commodities.ORGANICS).getQuantity().getModifiedValue();
242 float def = deficit.two;
243 if (def > demand) def = demand;
244
245 float mult = 1f;
246 if (def > 0 && demand > 0) {
247 mult = (demand - def) / demand;
249 }
250 return mult;
251 }
252
253 protected float getImmigrationBonus() {
255 }
256
257 protected float getMaxImmigrationBonus() {
258 return getSizeMult() * 10f;
259 }
260
261
262 public static class CryosleeperFactor implements ColonyOtherFactorsListener {
263 public boolean isActiveFactorFor(SectorEntityToken entity) {
264 return getNearestCryosleeper(entity.getLocationInHyperspace(), true) != null;
265 }
266
267 public void printOtherFactors(TooltipMakerAPI text, SectorEntityToken entity) {
268 float distMult = getDistancePopulationMult(entity.getLocationInHyperspace());
269
270 Pair<SectorEntityToken, Float> p = getNearestCryosleeper(entity.getLocationInHyperspace(), true);
271 if (p != null) {
272 Color h = Misc.getHighlightColor();
273 float opad = 10f;
274
275 String dStr = "" + Misc.getRoundedValueMaxOneAfterDecimal(p.two);
276 String lights = "light-years";
277 if (dStr.equals("1")) lights = "light-year";
278
279 if (p.two > MAX_BONUS_DIST_LY) {
280 text.addPara("The nearest cryosleeper is located in the " +
281 p.one.getContainingLocation().getNameWithLowercaseType() + ", %s " + lights + " away. The maximum " +
282 "range at which sleepers can be safely brought over for revival is %s light-years.",
283 opad, h,
284 "" + Misc.getRoundedValueMaxOneAfterDecimal(p.two),
285 "" + (int)MAX_BONUS_DIST_LY);
286 } else {
287 text.addPara("The nearest cryosleeper is located in the " +
288 p.one.getContainingLocation().getNameWithLowercaseType() + ", %s " + lights + " away, allowing " +
289 "a Cryorevival Facility built here to operate at %s effectiveness.",
290 opad, h,
291 "" + Misc.getRoundedValueMaxOneAfterDecimal(p.two),
292 "" + (int)Math.round(distMult * 100f) + "%");
293 }
294 }
295 }
296 }
297
298}
299
300
301
static SettingsAPI getSettings()
Definition Global.java:51
static SectorAPI getSector()
Definition Global.java:59
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)
CommoditySpecAPI getCommoditySpec(String commodityId)