001package com.fs.starfarer.api.impl.campaign;
002
003import java.util.LinkedHashMap;
004import java.util.Map;
005
006import com.fs.starfarer.api.Global;
007import com.fs.starfarer.api.campaign.CampaignFleetAPI;
008import com.fs.starfarer.api.campaign.PlanetAPI;
009import com.fs.starfarer.api.campaign.econ.MarketConditionAPI;
010import com.fs.starfarer.api.combat.MutableStat;
011import com.fs.starfarer.api.combat.MutableStat.StatMod;
012import com.fs.starfarer.api.fleet.MutableFleetStatsAPI;
013import com.fs.starfarer.api.impl.campaign.ids.Commodities;
014import com.fs.starfarer.api.impl.campaign.ids.Conditions;
015import com.fs.starfarer.api.impl.campaign.ids.Stats;
016import com.fs.starfarer.api.impl.campaign.procgen.ConditionGenDataSpec;
017import com.fs.starfarer.api.impl.campaign.procgen.themes.DerelictThemeGenerator;
018import com.fs.starfarer.api.plugins.SurveyPlugin;
019import com.fs.starfarer.api.util.Misc;
020
021public class SurveyPluginImpl implements SurveyPlugin {
022        
023        public static int FLAT_SUPPLIES = 10;
024        
025        public static int BASE_MACHINERY = 10;
026        public static int BASE_CREW = 50;
027        public static int BASE_SUPPLIES = 10;
028        public static int MIN_SUPPLIES_OR_MACHINERY = 5;
029        
030        public static float MIN_PLANET_RADIUS = 70;
031        public static float MAX_PLANET_RADIUS = 250;
032        public static float MULT_AT_MAX_PLANET_RADIUS = 5;
033        
034        
035        private CampaignFleetAPI fleet;
036        private PlanetAPI planet;
037        
038        private MutableStat costMult = new MutableStat(1f);
039        private MutableStat xpMult = new MutableStat(1f);
040
041        public void init(CampaignFleetAPI fleet, PlanetAPI planet) {
042                this.fleet = fleet;
043                this.planet = planet;
044                
045                float hazard = getHazardMultiplier();
046                if (hazard != 1f) {
047                        costMult.modifyMult("planet_hazard", hazard, "Hazard rating");
048                }
049                float size = getSizeMultiplier();
050                if (size != 1f) {
051                        costMult.modifyMult("planet_size", size, "Planet size");
052                }
053                
054                xpMult.applyMods(costMult);
055                
056                if (fleet != null) {
057                        MutableFleetStatsAPI stats = fleet.getStats();
058                        MutableStat stat = stats.getDynamic().getStat(Stats.SURVEY_COST_MULT);
059                        for (StatMod mod : stat.getMultMods().values()) {
060                                costMult.modifyMult(mod.source, mod.value, mod.desc);
061                        }
062                }
063                
064        }
065        
066        protected float getHazardMultiplier() {
067                float hazard = planet.getMarket().getHazardValue();
068                return hazard;
069        }
070        
071        protected float getSizeMultiplier() {
072                float radius = planet.getRadius();
073                float range = MAX_PLANET_RADIUS - MIN_PLANET_RADIUS;
074                if (range <= 0) return 1f;
075                if (radius < MIN_PLANET_RADIUS) radius = MIN_PLANET_RADIUS;
076                if (radius > MAX_PLANET_RADIUS) radius = MAX_PLANET_RADIUS;
077                
078                float mult = 1f + ((radius - MIN_PLANET_RADIUS) / range) * (MULT_AT_MAX_PLANET_RADIUS - 1f);
079                
080                mult = (int)(mult * 20) / 20f;
081                
082                return mult;
083        }
084        
085
086        public Map<String, Integer> getRequired() {
087                Map<String, Integer> result = new LinkedHashMap<String, Integer>();
088                
089                float mult = getCostMult().getModifiedValue();
090                
091                int machinery = (int)Math.round(BASE_MACHINERY * mult);
092                machinery -= (int) Misc.getFleetwideTotalMod(fleet, Stats.getSurveyCostReductionId(Commodities.HEAVY_MACHINERY), 0);
093                machinery = Math.round(machinery / 10f) * 10;
094                if (machinery < MIN_SUPPLIES_OR_MACHINERY) machinery = MIN_SUPPLIES_OR_MACHINERY;
095                
096                result.put(Commodities.CREW, (int)Math.round((int)(BASE_CREW * mult) / 10f) * 10);
097                result.put(Commodities.HEAVY_MACHINERY, machinery);
098                
099                return result;
100        }
101        
102        public Map<String, Integer> getConsumed() {
103                Map<String, Integer> result = new LinkedHashMap<String, Integer>();
104                
105                float mult = getCostMult().getModifiedValue();
106                int supplies = (int)Math.round(BASE_SUPPLIES * mult);
107                supplies += FLAT_SUPPLIES;
108                supplies = Math.round((int) supplies / 10f) * 10;
109                supplies -= (int) Misc.getFleetwideTotalMod(fleet, Stats.getSurveyCostReductionId(Commodities.SUPPLIES), 0);
110                if (supplies < MIN_SUPPLIES_OR_MACHINERY) supplies = MIN_SUPPLIES_OR_MACHINERY;
111                result.put(Commodities.SUPPLIES, supplies);
112                
113                return result;
114        }
115        
116        public MutableStat getCostMult() {
117                return costMult;
118        }
119
120        public long getXP() {
121                float xp = 0;
122                
123                if (planet.getMarket() != null) {
124                        for (MarketConditionAPI mc : planet.getMarket().getConditions()) {
125                                xp += getBaseXPForCondition(mc.getId());
126                        }
127                }
128                xp *= getXPMult().getModifiedValue();
129                
130                return (long) xp;
131        }
132        
133        public long getBaseXPForCondition(String conditionId) {
134                long xp = 0;
135                
136                float base = Global.getSettings().getFloat("baseSurveyXP");
137                ConditionGenDataSpec data = (ConditionGenDataSpec) Global.getSettings().getSpec(ConditionGenDataSpec.class, conditionId, true);
138                if (data != null) {
139                        xp += base * data.getXpMult();
140                }
141                return xp;
142        }
143        
144        public MutableStat getXPMult() {
145                return xpMult;
146        }
147        
148        
149        public String getImageCategory() {
150                return "illustrations";
151        }
152
153        public String getImageKey() {
154                return "survey";
155        }
156
157        public String getSurveyDataType(PlanetAPI planet) {
158                if (planet.getMarket() == null) return null;
159                
160                int count = 0;
161                float value = 0;
162                for (MarketConditionAPI mc : planet.getMarket().getConditions()) {
163                        if (DerelictThemeGenerator.interestingConditionsWithRuins.contains(mc.getId())) {
164                                count++;
165                        }
166                        if (mc.getGenSpec() != null) {
167                                //value += mc.getGenSpec().getXpMult();
168                                value += mc.getGenSpec().getRank();
169                        }
170                }
171                
172                if (planet.getMarket().hasCondition(Conditions.HABITABLE)) {
173                        value += 4f;
174                }
175                
176                float hazard = planet.getMarket().getHazardValue();
177                value -= (hazard - 1f) * 2f;
178                
179                if (value <= 5) return Commodities.SURVEY_DATA_1;
180                if (value <= 8) return Commodities.SURVEY_DATA_2;
181                if (value <= 11 && count <= 1) return Commodities.SURVEY_DATA_3;
182                if (value <= 14 && count <= 2) return Commodities.SURVEY_DATA_4;
183                return Commodities.SURVEY_DATA_5;
184                
185//              if (value <= 10 && count <= 0) return Commodities.SURVEY_DATA_1;
186//              if (value <= 20 && count <= 0) return Commodities.SURVEY_DATA_2;
187//              if (value <= 30 && count <= 1) return Commodities.SURVEY_DATA_3;
188//              if (value <= 40 && count <= 2) return Commodities.SURVEY_DATA_4;
189//              return Commodities.SURVEY_DATA_5;
190
191//              too few class V with below approach
192//              if (count <= 0) return Commodities.SURVEY_DATA_1;
193//              if (count <= 1) return Commodities.SURVEY_DATA_2;
194//              if (count <= 2) return Commodities.SURVEY_DATA_3;
195//              if (count <= 3) return Commodities.SURVEY_DATA_4;
196//              return Commodities.SURVEY_DATA_5;
197        }
198
199        
200        public Map<String, Integer> getOutpostConsumed() {
201                Map<String, Integer> result = new LinkedHashMap<String, Integer>();
202                
203                result.put(Commodities.CREW, 1000);
204                result.put(Commodities.HEAVY_MACHINERY, 100);
205                result.put(Commodities.SUPPLIES, 200);
206                
207                return result;
208        }
209        
210}
211
212
213