001package com.fs.starfarer.api.impl.campaign.econ;
002
003import java.util.LinkedHashMap;
004import java.util.Map;
005
006import com.fs.starfarer.api.Global;
007import com.fs.starfarer.api.campaign.econ.Industry;
008import com.fs.starfarer.api.campaign.econ.MarketAPI;
009import com.fs.starfarer.api.campaign.econ.MarketImmigrationModifier;
010import com.fs.starfarer.api.impl.campaign.econ.impl.ConstructionQueue.ConstructionQueueItem;
011import com.fs.starfarer.api.impl.campaign.ids.Conditions;
012import com.fs.starfarer.api.impl.campaign.ids.Factions;
013import com.fs.starfarer.api.impl.campaign.ids.Industries;
014import com.fs.starfarer.api.impl.campaign.ids.People;
015import com.fs.starfarer.api.impl.campaign.intel.events.LuddicChurchHostileActivityFactor;
016import com.fs.starfarer.api.impl.campaign.population.PopulationComposition;
017import com.fs.starfarer.api.loading.IndustrySpecAPI;
018import com.fs.starfarer.api.ui.TooltipMakerAPI;
019import com.fs.starfarer.api.util.Misc;
020
021public class LuddicMajority extends BaseMarketConditionPlugin implements MarketImmigrationModifier {
022
023        public static float STABILITY = 1f;
024        public static float IMMIGRATION_BASE = 5f;
025        
026        public static float PRODUCTION_BASE_RURAL = 1f;
027        public static Map<String, Integer> PRODUCTION_OVERRIDES = new LinkedHashMap<String, Integer>();
028        // makes explaining the effect more complicated; don't do it
029        static {
030                //PRODUCTION_OVERRIDES.put(Industries.FARMING, 2);
031        }
032        
033        public static int BONUS_MULT_DEFEATED_EXPEDITION = 2;
034
035        
036        @Deprecated
037        public static String [] luddicFactions = new String [] {
038                "knights_of_ludd",
039                "luddic_church",
040                "luddic_path",
041        };
042        
043        
044        public void apply(String id) {
045                if (!matchesBonusConditions(market)) {
046                        unapply(id);
047                        return;
048                }
049                
050                market.addTransientImmigrationModifier(this);
051                
052                int stability = (int) Math.round(STABILITY * getEffectMult());
053                if (stability != 0) {
054                        market.getStability().modifyFlat(id, stability, "Luddic majority");
055                }
056                
057                float mult = getEffectMult();
058                for (Industry ind : market.getIndustries()) {
059                        if (ind.getSpec().hasTag(Industries.TAG_RURAL) || PRODUCTION_OVERRIDES.containsKey(ind.getId())) {
060                                int production = (int) Math.round(PRODUCTION_BASE_RURAL * mult);
061                                if (PRODUCTION_OVERRIDES.containsKey(ind.getId())) {
062                                        production = (int) Math.round(PRODUCTION_OVERRIDES.get(ind.getId()) * mult);
063                                }
064                                if (production != 0) {
065                                        ind.getSupplyBonusFromOther().modifyFlat(id, production, "Luddic majority");
066                                }
067                        }
068                }
069        }
070
071        public void unapply(String id) {
072                market.removeTransientImmigrationModifier(this);
073                
074                market.getStability().unmodify(id);
075                
076                for (Industry ind : market.getIndustries()) {
077                        if (ind.getSpec().hasTag(Industries.TAG_RURAL) || PRODUCTION_OVERRIDES.containsKey(ind.getId())) {
078                                ind.getSupplyBonusFromOther().unmodifyFlat(id);
079                        }
080                }
081        }
082        
083        public void modifyIncoming(MarketAPI market, PopulationComposition incoming) {
084                float bonus = getImmigrationBonus(true);
085                if (bonus > 0) {
086                        incoming.add(Factions.LUDDIC_CHURCH, bonus);
087                        incoming.getWeight().modifyFlat(getModId(), bonus, "Luddic immigration (Luddic majority)");
088                }
089        }
090        
091        public float getImmigrationBonus(boolean withEffectMult) {
092                float bonus = IMMIGRATION_BASE * market.getSize();
093                if (withEffectMult) bonus *= getEffectMult();
094                return bonus;
095        }
096        
097        public float getEffectMult() {
098                if (market.isPlayerOwned() && 
099                                LuddicChurchHostileActivityFactor.isDefeatedExpedition()) {
100                        return BONUS_MULT_DEFEATED_EXPEDITION;
101                }
102                return 1f;
103                
104        }
105        
106        protected void createTooltipAfterDescription(TooltipMakerAPI tooltip, boolean expanded) {
107                super.createTooltipAfterDescription(tooltip, expanded);
108                
109                String name = market.getName();
110                float opad = 10f;
111                
112                tooltip.addPara("A majority of the population of " + name + " are Luddic faithful. "
113                                + "This may result in a substantial boost "
114                                + "to stability and productivity.", opad);
115                
116                tooltip.addPara("For colonies outside the core, it may also result in increased population growth, "
117                                + "from Luddic immigrants seeking to escape the sometimes oppressive influence of the Luddic Church.", opad);
118
119                tooltip.addPara("%s stability", 
120                                opad, Misc.getHighlightColor(),
121                                "+" + (int)STABILITY);
122                
123                tooltip.addPara("%s production for Farming, Light Industry, and similar", 
124                                opad, Misc.getHighlightColor(),
125                                "+" + (int)PRODUCTION_BASE_RURAL);
126
127                if (market.isPlayerOwned()) {
128                        tooltip.addPara("%s population growth",
129                                        opad, Misc.getHighlightColor(), 
130                                        "+" + (int) getImmigrationBonus(false));
131                }
132
133                if (!Global.CODEX_TOOLTIP_MODE) {
134                        addConditions(tooltip, market, opad);
135                }
136        }
137        
138        public static void addConditions(TooltipMakerAPI tooltip, MarketAPI market, float opad) {
139                boolean madeDeal = LuddicChurchHostileActivityFactor.isMadeDeal() && market.isPlayerOwned();
140                boolean freePort = market.isFreePort();
141                freePort = false;
142                boolean habitable = market.hasCondition(Conditions.HABITABLE);
143                
144                boolean hasRural = false;
145                boolean hasIndustrial = false;
146                boolean hasMilitary = false;
147                String heavy = null;
148                String military = null;
149                String rural = null;
150                
151                for (Industry ind : market.getIndustries()) {
152                        if (ind.getSpec().hasTag(Industries.TAG_INDUSTRIAL)) {
153                                if (heavy == null) heavy = ind.getCurrentName();
154                                hasIndustrial = true;
155                        }
156                        if (ind.getSpec().hasTag(Industries.TAG_MILITARY) || ind.getSpec().hasTag(Industries.TAG_COMMAND)) {
157                                if (military == null) military = ind.getCurrentName();
158                                hasMilitary = true;
159                        }
160                        
161                        if (ind.getSpec().hasTag(Industries.TAG_RURAL)) {
162                                if (rural == null) rural = ind.getCurrentName();
163                                hasRural = true;
164                        }
165                }
166                
167                if (market.getConstructionQueue() != null) {
168                        for (ConstructionQueueItem item : market.getConstructionQueue().getItems()) {
169                                IndustrySpecAPI spec = Global.getSettings().getIndustrySpec(item.id);
170                                if (spec != null) {
171                                        if (spec.hasTag(Industries.TAG_INDUSTRIAL)) {
172                                                if (heavy == null) heavy = spec.getName();
173                                                hasIndustrial = true;
174                                        }
175                                        if (spec.hasTag(Industries.TAG_MILITARY) || spec.hasTag(Industries.TAG_COMMAND)) {
176                                                if (military == null) military = spec.getName();
177                                                hasMilitary = true;
178                                        }
179                                        
180                                        if (spec.hasTag(Industries.TAG_RURAL)) {
181                                                if (rural == null) rural = spec.getName();
182                                                hasRural = true;
183                                        }
184                                }
185                                break;
186                        }
187                }
188                
189                boolean matches = matchesBonusConditions(market);
190                
191                if (!matches) {
192                        if (market.isPlayerOwned()) {
193                                tooltip.addPara("The following factors result in these bonuses being negated, and, "
194                                                + "unless addressed, will result in the \"Luddic Majority\" condition "
195                                                + "being removed if the colony increases in size:", opad,
196                                                Misc.getNegativeHighlightColor(), "negated", "removed");
197                        } else {
198                                tooltip.addPara("The following factors result in these bonuses being negated:", opad,
199                                                Misc.getNegativeHighlightColor(), "negated", "removed");
200                        }
201                        //opad = 5f;
202                        tooltip.setBulletedListMode("    - ");
203                        if (market.getAdmin() != null && market.getAdmin().getId().equals(People.DARDAN_KATO)) {
204                                tooltip.addPara("Dardan Kato's \"policies\"", opad);
205                                opad = 0f;
206                        }
207                        if (madeDeal) {
208                                tooltip.addPara("Deal made with Luddic Church to curtail immigration", opad);
209                                opad = 0f;
210                        }
211                        if (freePort) {
212                                tooltip.addPara("The colony is a free port", opad);
213                                opad = 0f;
214                        }
215                        if (!habitable) {
216                                tooltip.addPara("The colony is not habitable", opad);
217                                opad = 0f;
218                        }
219                        if (!hasRural) {
220                                tooltip.addPara("The colony has no suitable employment for the faithful, such as farming or light industry", opad);
221                                opad = 0f;
222                        }
223                        if (hasIndustrial) {
224                                tooltip.addPara("The colony has heavy industrial facilities (" + heavy + ")", opad);
225                                opad = 0f;
226                        }
227                        if (hasMilitary) {
228                                tooltip.addPara("The colony has military facilities (" + military + ")", opad);
229                                opad = 0f;
230                        }
231                        tooltip.setBulletedListMode(null);
232                } else {
233                        if (market.isPlayerOwned() && LuddicChurchHostileActivityFactor.isDefeatedExpedition()) {
234                                tooltip.addPara("The bonus is doubled due to the faithful " + market.getOnOrAt() + " " + 
235                                                        market.getName() + " feeling securely out from under the direct "
236                                                + "influence of the Luddic Church.", opad, Misc.getPositiveHighlightColor(), "doubled");
237                        }
238                }
239                
240                tooltip.setBulletedListMode("    - ");
241                tooltip.setBulletedListMode(null);
242        }
243        
244
245        public static boolean matchesBonusConditions(MarketAPI market) {
246                if (market.isPlayerOwned() && LuddicChurchHostileActivityFactor.isMadeDeal()) return false;
247                
248                // feels a bit too restrictive
249                //if (market.isFreePort()) return false;
250                
251                if (!market.hasCondition(Conditions.HABITABLE)) return false;
252                
253                if (market.getAdmin() != null && market.getAdmin().getId().equals(People.DARDAN_KATO)) {
254                        // he's that dumb about it
255                        return false;
256                }
257                
258                boolean hasRural = false;
259                for (Industry ind : market.getIndustries()) {
260                        if (ind.getSpec().hasTag(Industries.TAG_INDUSTRIAL)) return false;
261                        if (ind.getSpec().hasTag(Industries.TAG_MILITARY)) return false;
262                        if (ind.getSpec().hasTag(Industries.TAG_COMMAND)) return false;
263                        
264                        hasRural |= ind.getSpec().hasTag(Industries.TAG_RURAL);
265                }
266                
267                if (market.getConstructionQueue() != null) {
268                        for (ConstructionQueueItem item : market.getConstructionQueue().getItems()) {
269                                IndustrySpecAPI spec = Global.getSettings().getIndustrySpec(item.id);
270                                if (spec != null) {
271                                        if (spec.hasTag(Industries.TAG_INDUSTRIAL)) return false;
272                                        if (spec.hasTag(Industries.TAG_MILITARY) || spec.hasTag(Industries.TAG_COMMAND)) return false;
273                                }
274                                break;
275                        }
276                }
277                
278                return hasRural;
279        }
280
281        public String getIconName() {
282                if (!matchesBonusConditions(market)) {// && market.isPlayerOwned()) {
283                        return Global.getSettings().getSpriteName("events", "luddic_majority_unhappy");
284                }
285                return super.getIconName();
286        }
287}
288
289
290
291
292
293
294
295