Starsector API
Loading...
Searching...
No Matches
ShippingDisruption.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.econ;
2
3import java.awt.Color;
4import java.util.ArrayList;
5import java.util.List;
6import java.util.Map;
7
8import com.fs.starfarer.api.Global;
9import com.fs.starfarer.api.campaign.econ.CommodityOnMarketAPI;
10import com.fs.starfarer.api.campaign.econ.MarketAPI;
11import com.fs.starfarer.api.campaign.econ.MarketConditionAPI;
12import com.fs.starfarer.api.combat.MutableStatWithTempMods;
13import com.fs.starfarer.api.combat.MutableStat.StatMod;
14import com.fs.starfarer.api.impl.campaign.ids.Conditions;
15import com.fs.starfarer.api.ui.TooltipMakerAPI;
16import com.fs.starfarer.api.util.CountingMap;
17import com.fs.starfarer.api.util.Misc;
18
20
21 public static String COMMODITY_LOSS_PREFIX = "sh_loss";
22 public static float ACCESS_LOSS_DURATION = 90f;
23
24 public static float ACCESS_PER_UNITS_LOST = 0.1f; // accessibility penalty per marketSize units lost
25
26
27 public static float getPenaltyForShippingLost(float marketSize, float unitsLost) {
28 float result = Math.round(unitsLost / marketSize * ACCESS_PER_UNITS_LOST * 100f) / 100f;
29 if (result == 0) result = 0.01f;
30 return result;
31 }
32
33 public static ShippingDisruption getDisruption(MarketAPI market) {
34 MarketConditionAPI mc = market.getCondition(Conditions.SHIPPING_DISRUPTION);
35 if (mc == null) {
36 String id = market.addCondition(Conditions.SHIPPING_DISRUPTION);
37 mc = market.getSpecificCondition(id);
38 //mc.setFromEvent(true);
39 }
40 return (ShippingDisruption) mc.getPlugin();
41 }
42
43 protected MutableStatWithTempMods shippingLost = new MutableStatWithTempMods(0f);
44
45 protected float disruptionTimeLeft = 0f;
47 }
48
54 public void addShippingLost(float units) {
55 shippingLost.addTemporaryModFlat(ACCESS_LOSS_DURATION, getModId() + Misc.genUID(), units);
57 }
58
59 protected void updatePenaltyValue() {
61 apply(getModId());
62 }
63
64 public float getDisruptionTimeLeft() {
65 return disruptionTimeLeft;
66 }
67
69 this.disruptionTimeLeft = disruptionTimeLeft;
70 }
71
72 public void notifyDisrupted(float duration) {
73 disruptionTimeLeft = Math.max(disruptionTimeLeft, duration);
74 }
75 public void apply(String id) {
76 float penalty = getPenaltyForShippingLost(market.getSize(), shippingLost.getModifiedValue());
77 String name = "Trade fleets lost";
78 market.getAccessibilityMod().modifyFlat(id, -penalty, name);
79
80 if (market.isPlayerOwned()) {
81 for (CommodityOnMarketAPI com : market.getCommoditiesCopy()) {
82 List<String> unmodify = new ArrayList<String>();
83 if (com.getMaxSupply() >= com.getAvailable() + 1) {
84 for (String key : com.getAvailableStat().getFlatMods().keySet()) {
85 StatMod mod = com.getAvailableStat().getFlatStatMod(key);
86 int val = (int)Math.round(Math.abs(mod.value));
87 if (key.startsWith(COMMODITY_LOSS_PREFIX) && val != 0) {
88 unmodify.add(mod.source);
89 }
90 }
91 }
92 for (String modId : unmodify) {
93 com.getAvailableStat().unmodifyFlat(modId);
94 }
95 }
96 }
97 }
98
99 public void unapply(String id) {
100 market.getAccessibilityMod().unmodifyFlat(id);
101 }
102
103
104 @Override
105 public void advance(float amount) {
106 float days = Global.getSector().getClock().convertToDays(amount);
107 shippingLost.advance(days);
109
110 disruptionTimeLeft -= days;
111 if (disruptionTimeLeft <= 0) {
113 if (shippingLost.isUnmodified()) {
114 market.removeSpecificCondition(condition.getIdForPluginModifications());
115 }
116 }
117 }
118
119 public Map<String, String> getTokenReplacements() {
120 return super.getTokenReplacements();
121 //return event.getTokenReplacements();
122 }
123
124 @Override
125 public String[] getHighlights() {
126 return super.getHighlights();
127 //return event.getHighlights("report_td");
128 }
129
130 @Override
131 public boolean isTransient() {
132 return false;
133 }
134
135 @Override
136 public void createTooltip(TooltipMakerAPI tooltip, boolean expanded) {
137 tooltip.addTitle("Shipping Disruption");
138
139 int loss = shippingLost.getModifiedInt();
140
141 int penalty = (int)Math.round(getPenaltyForShippingLost(market.getSize(), shippingLost.getModifiedValue()) * 100f);
142
143 Color h = Misc.getHighlightColor();
144 Color n = Misc.getNegativeHighlightColor();
145
146 float pad = 3f;
147 float small = 5f;
148 float opad = 10f;
149
150 //tooltip.addPara("Loss: %s, Accesibility penalty: %s", pad, h, "" + loss, "" + penalty);
151 if (penalty > 0) {
152 tooltip.addPara("Trade fleets launched from " + market.getName() + " have suffered losses, " +
153 "resulting in a temporary accessibility penalty of %s.", opad, h, "" + penalty + "%");
154 }
155
156 CountingMap<CommodityOnMarketAPI> losses = new CountingMap<CommodityOnMarketAPI>();
157
158 for (CommodityOnMarketAPI com : market.getCommoditiesCopy()) {
159 for (String key : com.getAvailableStat().getFlatMods().keySet()) {
160 StatMod mod = com.getAvailableStat().getFlatStatMod(key);
161
162 int val = (int)Math.round(Math.abs(mod.value));
163 if (key.startsWith(COMMODITY_LOSS_PREFIX) && val != 0) {
164 losses.add(com, val);
165 }
166 }
167 }
168
169 if (!losses.isEmpty()) {
170 tooltip.addPara("The local availability of some commodities has been reduced by trade fleet losses. " +
171 "Provided no further losses occur, commodity availability should return to normal levels within at most three months.", opad);
172 tooltip.beginGridFlipped(400, 1, 30, opad);
173 int j = 0;
174 for (CommodityOnMarketAPI com : losses.keySet()) {
175 tooltip.addToGrid(0, j++, com.getCommodity().getName(),
176 "-" + losses.getCount(com), h);
177 }
178 tooltip.addGrid(pad);
179 }
180
181 //tooltip.addPara(com.getCommodity().getName() + ": %s", pad, h, "" + (int) mod.value);
182 }
183
184 @Override
185 public float getTooltipWidth() {
186 return super.getTooltipWidth();
187 }
188
189 @Override
190 public boolean hasCustomTooltip() {
191 return true;
192 }
193
194 @Override
195 public boolean isTooltipExpandable() {
196 return super.isTooltipExpandable();
197 }
198
199
200}
201
202
203
204
205
static SectorAPI getSector()
Definition Global.java:59
static ShippingDisruption getDisruption(MarketAPI market)
void createTooltip(TooltipMakerAPI tooltip, boolean expanded)
static float getPenaltyForShippingLost(float marketSize, float unitsLost)