001package com.fs.starfarer.api.impl.campaign.intel.bases;
002
003import java.awt.Color;
004import java.util.Set;
005
006import com.fs.starfarer.api.Global;
007import com.fs.starfarer.api.campaign.FactionAPI;
008import com.fs.starfarer.api.campaign.SectorEntityToken;
009import com.fs.starfarer.api.campaign.StarSystemAPI;
010import com.fs.starfarer.api.campaign.econ.MarketAPI;
011import com.fs.starfarer.api.campaign.econ.MarketConditionAPI;
012import com.fs.starfarer.api.campaign.econ.MarketConditionPlugin;
013import com.fs.starfarer.api.impl.campaign.DebugFlags;
014import com.fs.starfarer.api.impl.campaign.ids.Conditions;
015import com.fs.starfarer.api.impl.campaign.ids.Factions;
016import com.fs.starfarer.api.impl.campaign.ids.Tags;
017import com.fs.starfarer.api.impl.campaign.intel.BaseIntelPlugin;
018import com.fs.starfarer.api.impl.campaign.intel.events.PiracyRespiteScript;
019import com.fs.starfarer.api.ui.Alignment;
020import com.fs.starfarer.api.ui.SectorMapAPI;
021import com.fs.starfarer.api.ui.TooltipMakerAPI;
022import com.fs.starfarer.api.util.Misc;
023
024public class PirateActivityIntel extends BaseIntelPlugin {
025
026        //protected MarketAPI market;
027        protected StarSystemAPI system;
028        protected PirateBaseIntel source;
029        
030        public PirateActivityIntel(StarSystemAPI system, PirateBaseIntel source) {
031                this.system = system;
032                this.source = source;
033                
034                
035                boolean hasPlayerMarkets = false;
036                for (MarketAPI curr : Global.getSector().getEconomy().getMarkets(system)) {
037                        hasPlayerMarkets |= curr.isPlayerOwned();
038                }
039                if (!hasPlayerMarkets) {
040                        setPostingLocation(system.getCenter());
041                }
042                
043                Global.getSector().addScript(this);
044                
045                
046                //Global.getSector().getIntelManager().queueIntel(this);
047                if (!Misc.getMarketsInLocation(system, Factions.PLAYER).isEmpty()) {
048                        Global.getSector().getIntelManager().addIntel(this);
049                } else {
050                        Global.getSector().getIntelManager().queueIntel(this);
051                }
052        }
053        
054        @Override
055        public boolean canMakeVisibleToPlayer(boolean playerInRelayRange) {
056                if (DebugFlags.SEND_UPDATES_WHEN_NO_COMM && source.isPlayerVisible()) return true;
057                return super.canMakeVisibleToPlayer(playerInRelayRange);
058        }
059
060
061        public PirateBaseIntel getSource() {
062                return source;
063        }
064
065        @Override
066        protected void notifyEnded() {
067                super.notifyEnded();
068                Global.getSector().removeScript(this);
069        }
070
071
072        @Override
073        protected void notifyEnding() {
074                super.notifyEnding();
075                
076                for (MarketAPI curr : source.getAffectedMarkets(system)) {
077                        if (curr.hasCondition(Conditions.PIRATE_ACTIVITY)) {
078                                curr.removeCondition(Conditions.PIRATE_ACTIVITY);
079                        }
080                }
081        }
082
083        @Override
084        protected void advanceImpl(float amount) {
085                super.advanceImpl(amount);
086                
087                if (source.isEnding() || source.getTarget() != system) {
088                        endAfterDelay();
089                        if (DebugFlags.SEND_UPDATES_WHEN_NO_COMM || Global.getSector().getIntelManager().isPlayerInRangeOfCommRelay()) {
090                                sendUpdateIfPlayerHasIntel(new Object(), false);
091                        }
092                        return;
093                }
094                
095                for (MarketAPI curr : source.getAffectedMarkets(system)) {
096                        if (curr.isPlayerOwned() && PiracyRespiteScript.get() != null) continue;
097                        
098                        if (!curr.hasCondition(Conditions.PIRATE_ACTIVITY)) {
099                                curr.addCondition(Conditions.PIRATE_ACTIVITY, source);
100                        }
101                }
102        }
103
104        protected void addBulletPoints(TooltipMakerAPI info, ListInfoMode mode) {
105                
106                Color h = Misc.getHighlightColor();
107                Color g = Misc.getGrayColor();
108                float pad = 3f;
109                float opad = 10f;
110                
111                float initPad = pad;
112                if (mode == ListInfoMode.IN_DESC) initPad = opad;
113                
114                Color tc = getBulletColorForMode(mode);
115                
116                bullet(info);
117                boolean isUpdate = getListInfoParam() != null;
118                
119//              info.addPara("Danger level: " + danger, initPad, tc, dangerColor, danger);
120//              initPad = 0f;
121                
122                unindent(info);
123        }
124        
125        @Override
126        public void createIntelInfo(TooltipMakerAPI info, ListInfoMode mode) {
127                Color c = getTitleColor(mode);
128                info.addPara(getName(), c, 0f);
129                addBulletPoints(info, mode);
130        }
131        
132        @Override
133        public void createSmallDescription(TooltipMakerAPI info, float width, float height) {
134                Color h = Misc.getHighlightColor();
135                Color g = Misc.getGrayColor();
136                Color tc = Misc.getTextColor();
137                float pad = 3f;
138                float opad = 10f;
139                
140                info.addImage(getFactionForUIColors().getLogo(), width, 128, opad);
141                
142                info.addPara("Pirates have been targeting colonies and shipping " +
143                                "in the " + system.getNameWithLowercaseType() + ".", opad);
144                
145                if (source.isPlayerVisible()) {
146                        //info.addPara("The pirates are based out of the " + source.getSystem().getNameWithLowercaseType() + ".", opad);
147                        info.addPara("The pirates are based out of " +
148                                        source.getMarket().getName() + " in the " + source.getSystem().getNameWithLowercaseType() + ".", opad);
149                } else {
150                        float distLY = Misc.getDistanceLY(system.getLocation(), source.getSystem().getLocation());
151                        if (distLY < 10) {
152                                info.addPara("The location of the pirate base is unknown, but it's likely to be somewhere nearby.", opad);
153                        } else {
154                                info.addPara("The location of the pirate base is unknown, but there are indications that it's quite distant.", opad);
155                        }
156                }
157                
158                info.addSectionHeading("Colonies affected", getFactionForUIColors().getBaseUIColor(),
159                                                          getFactionForUIColors().getDarkUIColor(), Alignment.MID, opad);
160                
161                //info.addPara("Colonies affected:", opad);
162                
163                MarketConditionAPI condition = null;
164                float initPad = opad;
165                for (MarketAPI curr : source.getAffectedMarkets(system)) {
166                        if (condition == null) {
167                                condition = curr.getCondition(Conditions.PIRATE_ACTIVITY);
168                        }
169                        
170                        addMarketToList(info, curr, initPad, tc);
171                        initPad = 0f;
172                }
173                
174                if (condition != null) {
175                        MarketConditionPlugin plugin = condition.getPlugin();
176//                      String text = condition.getSpec().getDesc();
177//                      Map<String, String> tokens = plugin.getTokenReplacements();
178//                      if (tokens != null) {
179//                              for (String token : tokens.keySet()) {
180//                                      String value = tokens.get(token);
181//                                      text = text.replaceAll("(?s)\\" + token, value);
182//                              }
183//                      }
184//                      if (!text.isEmpty()) {
185//                              info.addPara(text, opad);
186//                      }
187                        
188                        ((PirateActivity)plugin).createTooltipAfterDescription(info, true);
189                }
190                
191        }
192        
193        
194        public StarSystemAPI getSystem() {
195                return system;
196        }
197
198        @Override
199        public String getIcon() {
200                return Global.getSettings().getSpriteName("intel", "pirate_activity");
201        }
202        
203        @Override
204        public Set<String> getIntelTags(SectorMapAPI map) {
205                Set<String> tags = super.getIntelTags(map);
206                tags.add(Factions.PIRATES);
207                
208                if (!Misc.getMarketsInLocation(system, Factions.PLAYER).isEmpty()) {
209                        tags.add(Tags.INTEL_COLONIES);
210                }
211                
212                return tags;
213        }
214        
215        public String getSortString() {
216                String base = Misc.ucFirst(getFactionForUIColors().getPersonNamePrefix());
217                return base + " C"; // so it goes after "Pirate Base"
218        }
219        
220        public String getName() {
221                String base = "Pirate Activity";
222                if (isEnding()) {
223                        return base + " - Over";
224                }
225                return base + " - " + system.getBaseName();
226        }
227        
228        @Override
229        public FactionAPI getFactionForUIColors() {
230                return source.getFactionForUIColors();
231        }
232
233        public String getSmallDescriptionTitle() {
234                return getName();
235        }
236
237        @Override
238        public SectorEntityToken getMapLocation(SectorMapAPI map) {
239                return system.getCenter();
240        }
241
242        
243        @Override
244        public String getCommMessageSound() {
245                return getSoundMinorMessage();
246        }
247        
248        
249}
250
251
252
253
254
255
256