Starsector API
Loading...
Searching...
No Matches
RecentUnrest.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.econ;
2
3import com.fs.starfarer.api.campaign.econ.MarketAPI;
4import com.fs.starfarer.api.campaign.econ.MarketConditionAPI;
5import com.fs.starfarer.api.impl.campaign.ids.Conditions;
6import com.fs.starfarer.api.impl.campaign.intel.BaseIntelPlugin;
7import com.fs.starfarer.api.ui.TooltipMakerAPI;
8import com.fs.starfarer.api.util.Misc;
9import com.fs.starfarer.api.util.TimeoutTracker;
10
12
13 public static RecentUnrest get(MarketAPI market) {
14 return get(market, true);
15 }
16 public static RecentUnrest get(MarketAPI market, boolean addIfNeeded) {
17 MarketConditionAPI mc = market.getCondition(Conditions.RECENT_UNREST);
18 if (mc == null && !addIfNeeded) return null;
19
20 if (mc == null) {
21 String id = market.addCondition(Conditions.RECENT_UNREST);
22 mc = market.getSpecificCondition(id);
23 }
24 return (RecentUnrest) mc.getPlugin();
25 }
26
27 public static int getPenalty(MarketAPI market) {
28 RecentUnrest ru = get(market, false);
29 if (ru == null) return 0;
30 return ru.getPenalty();
31 }
32
33 public static float DECREASE_DAYS = 90f;
34
35 protected int penalty;
36 protected float untilDecrease = DECREASE_DAYS;
37 protected TimeoutTracker<String> reasons = new TimeoutTracker<String>();
38
39 public RecentUnrest() {
40 }
41
42 public int getPenalty() {
43 return penalty;
44 }
45
46 public void apply(String id) {
47 market.getStability().modifyFlat(id, -1 * penalty, "Recent unrest");
48 }
49
50 public void unapply(String id) {
51 market.getStability().unmodify(id);
52 }
53
54
55 public void add(int stability, String reason) {
56 penalty += stability;
57 float dur = reasons.getRemaining(reason) + stability * DECREASE_DAYS;
58 reasons.add(reason, dur);
59 }
60
61 public void counter(int points, String reason) {
62 points = Math.min(points, penalty);
63 penalty -= points;
64 if (penalty < 0) penalty = 0;
65 float dur = reasons.getRemaining(reason) + points * DECREASE_DAYS;
66 reasons.add(reason, dur);
67 }
68
69 @Override
70 public void advance(float amount) {
71 super.advance(amount);
72
73 float days = Misc.getDays(amount);
74 //days *= 100000f;
75 reasons.advance(days);
76
77 if (penalty > 0) {
78 untilDecrease -= days;
79 if (untilDecrease <= 0) {
80 penalty--;
81 if (penalty < 0) penalty = 0;
83 }
84 }
85 if (penalty <= 0) {
86 market.removeSpecificCondition(condition.getIdForPluginModifications());
87 }
88 }
89
90 @Override
91 public void createTooltip(TooltipMakerAPI tooltip, boolean expanded) {
92 super.createTooltip(tooltip, expanded);
93 }
94
95
96 @Override
97 protected void createTooltipAfterDescription(TooltipMakerAPI tooltip, boolean expanded) {
98 super.createTooltipAfterDescription(tooltip, expanded);
99
100 float pad = 3f;
101 float opad = 10f;
102
103 tooltip.addPara("%s stability. Goes down by one point every three months.",
104 opad, Misc.getHighlightColor(),
105 "-" + (int)penalty);
106
107 if (!reasons.getItems().isEmpty()) {
108 tooltip.addPara("Recent contributing factors:", opad);
109
110 float initPad = pad;
111 for (String reason : reasons.getItems()) {
112 tooltip.addPara(BaseIntelPlugin.BULLET + reason, initPad);
113 initPad = 0f;
114 }
115 }
116
117 }
118
119 @Override
120 public boolean isTransient() {
121 return false;
122 }
123
124 public void setPenalty(int penalty) {
125 this.penalty = penalty;
126 }
127
128
129}
void createTooltipAfterDescription(TooltipMakerAPI tooltip, boolean expanded)
void createTooltip(TooltipMakerAPI tooltip, boolean expanded)