001package com.fs.starfarer.api.impl.campaign.intel;
002
003import java.util.List;
004import java.util.Map;
005import java.util.Set;
006
007import java.awt.Color;
008
009import org.apache.log4j.Logger;
010import org.lwjgl.util.vector.Vector2f;
011
012import com.fs.starfarer.api.Global;
013import com.fs.starfarer.api.campaign.CampaignFleetAPI;
014import com.fs.starfarer.api.campaign.CargoAPI;
015import com.fs.starfarer.api.campaign.FactionAPI;
016import com.fs.starfarer.api.campaign.InteractionDialogAPI;
017import com.fs.starfarer.api.campaign.RepLevel;
018import com.fs.starfarer.api.campaign.ReputationActionResponsePlugin.ReputationAdjustmentResult;
019import com.fs.starfarer.api.campaign.SectorEntityToken;
020import com.fs.starfarer.api.campaign.econ.MarketAPI;
021import com.fs.starfarer.api.campaign.rules.MemoryAPI;
022import com.fs.starfarer.api.impl.campaign.CoreReputationPlugin;
023import com.fs.starfarer.api.impl.campaign.CoreReputationPlugin.MissionCompletionRep;
024import com.fs.starfarer.api.impl.campaign.CoreReputationPlugin.RepActionEnvelope;
025import com.fs.starfarer.api.impl.campaign.CoreReputationPlugin.RepActions;
026import com.fs.starfarer.api.impl.campaign.CoreReputationPlugin.RepRewards;
027import com.fs.starfarer.api.impl.campaign.ids.Factions;
028import com.fs.starfarer.api.impl.campaign.ids.MemFlags;
029import com.fs.starfarer.api.impl.campaign.ids.Tags;
030import com.fs.starfarer.api.impl.campaign.rulecmd.AddRemoveCommodity;
031import com.fs.starfarer.api.impl.campaign.rulecmd.salvage.special.BreadcrumbSpecial;
032import com.fs.starfarer.api.ui.SectorMapAPI;
033import com.fs.starfarer.api.ui.TooltipMakerAPI;
034import com.fs.starfarer.api.util.Misc;
035import com.fs.starfarer.api.util.Misc.Token;
036import com.fs.starfarer.api.util.WeightedRandomPicker;
037
038public class AnalyzeEntityMissionIntel extends BaseMissionIntel {
039        public static Logger log = Global.getLogger(AnalyzeEntityMissionIntel.class);
040        
041        protected int reward;
042        protected FactionAPI faction;
043        protected MarketAPI market;
044        
045        protected SectorEntityToken entity;
046
047        
048        public AnalyzeEntityMissionIntel(SectorEntityToken entity) {
049                this.entity = entity;
050                
051                WeightedRandomPicker<MarketAPI> marketPicker = new WeightedRandomPicker<MarketAPI>();
052                for (MarketAPI market : Global.getSector().getEconomy().getMarketsCopy()) {
053                        if (market.isHidden()) continue;
054                        if (market.getFaction().isPlayerFaction()) continue;
055                        marketPicker.add(market, market.getSize());
056                }
057                
058                market = marketPicker.pick();
059                if (market == null) {
060                        endImmediately();
061                        return;
062                }
063                
064                faction = market.getFaction();
065                if (!market.getFaction().isHostileTo(Factions.INDEPENDENT) && (float) Math.random() > 0.67f) {
066                        faction = Global.getSector().getFaction(Factions.INDEPENDENT);
067                }
068                
069                setDuration(120f);
070                
071                reward = (int) Misc.getDistance(new Vector2f(), entity.getLocationInHyperspace());
072                //reward *= 1.25f;
073                reward = 20000 + (reward / 10000) * 10000;
074                if (reward < 10000) reward = 10000;
075                
076                
077                log.info("Created AnalyzeEntityMissionIntel: " + entity.getName() + ", faction: " + faction.getDisplayName());
078
079                
080                initRandomCancel();
081                setPostingLocation(market.getPrimaryEntity());
082                
083                Global.getSector().getIntelManager().queueIntel(this);
084                
085        }
086        
087        @Override
088        public void notifyPlayerAboutToOpenIntelScreen() {
089                if (isPosted() && (!entity.isAlive() || entity.hasTag(Tags.NON_CLICKABLE))) {
090                        cancel();
091                }
092        }
093
094
095
096        public SectorEntityToken getEntity() {
097                return entity;
098        }
099
100        @Override
101        protected MissionResult createAbandonedResult(boolean withPenalty) {
102                if (withPenalty) {
103                        MissionCompletionRep rep = new MissionCompletionRep(RepRewards.HIGH, RepLevel.WELCOMING,
104                                         -RepRewards.TINY, RepLevel.INHOSPITABLE);
105                        ReputationAdjustmentResult result = Global.getSector().adjustPlayerReputation(
106                                new RepActionEnvelope(RepActions.MISSION_FAILURE, rep,
107                                                                          null, null, true, false),
108                                                                          faction.getId());
109                        return new MissionResult(0, result);
110                }
111                return new MissionResult();
112        }
113
114        @Override
115        protected MissionResult createTimeRanOutFailedResult() {
116                return createAbandonedResult(true);
117        }
118
119        @Override
120        public void missionAccepted() {
121                entity.getMemoryWithoutUpdate().set("$aem_target", true, getDuration());
122                entity.getMemoryWithoutUpdate().set("$aem_eventRef", this, getDuration());
123                Misc.setFlagWithReason(entity.getMemoryWithoutUpdate(), MemFlags.ENTITY_MISSION_IMPORTANT,
124                                                       "aem", true, getDuration());     
125        }
126        
127        
128        @Override
129        public void endMission() {
130                entity.getMemoryWithoutUpdate().unset("$aem_target");
131                entity.getMemoryWithoutUpdate().unset("$aem_eventRef");
132                Misc.setFlagWithReason(entity.getMemoryWithoutUpdate(), MemFlags.ENTITY_MISSION_IMPORTANT,
133                                                          "aem", false, 0f);
134                endAfterDelay();
135        }
136
137        @Override
138        public void advanceMission(float amount) {
139        }
140
141        @Override
142        public boolean callEvent(String ruleId, final InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) {
143                String action = params.get(0).getString(memoryMap);
144                
145                CampaignFleetAPI playerFleet = Global.getSector().getPlayerFleet();
146                CargoAPI cargo = playerFleet.getCargo();
147                
148                if (action.equals("runPackage")) {
149                        AddRemoveCommodity.addCreditsGainText(reward, dialog.getTextPanel());
150                        cargo.getCredits().add(reward);
151                        
152                        MissionCompletionRep rep = new MissionCompletionRep(RepRewards.HIGH, RepLevel.WELCOMING,
153                                         -RepRewards.TINY, RepLevel.INHOSPITABLE);
154
155                        ReputationAdjustmentResult result = Global.getSector().adjustPlayerReputation(
156                                        new RepActionEnvelope(RepActions.MISSION_SUCCESS, rep,
157                                                                                  null, dialog.getTextPanel(), true, false), 
158                                                                                  faction.getId());
159                        setMissionResult(new MissionResult(reward, result));
160                        setMissionState(MissionState.COMPLETED);
161                        endMission();
162                }
163                
164                return true;
165        }
166        
167        
168        protected void addBulletPoints(TooltipMakerAPI info, ListInfoMode mode) {
169                
170                Color h = Misc.getHighlightColor();
171                Color g = Misc.getGrayColor();
172                float pad = 3f;
173                float opad = 10f;
174                
175                float initPad = pad;
176                if (mode == ListInfoMode.IN_DESC) initPad = opad;
177                
178                Color tc = getBulletColorForMode(mode);
179                
180                bullet(info);
181                boolean isUpdate = getListInfoParam() != null;
182                
183                if (isUpdate) {
184                        // 3 possible updates: de-posted/expired, failed, completed
185                        if (isFailed() || isCancelled()) {
186                                return;
187                        } else if (isCompleted()) {
188                                if (missionResult.payment > 0) {
189                                        info.addPara("%s received", initPad, tc, h, Misc.getDGSCredits(missionResult.payment));
190                                }
191                                CoreReputationPlugin.addAdjustmentMessage(missionResult.rep1.delta, faction, null, 
192                                                                                                                  null, null, info, tc, isUpdate, 0f);
193                        }
194                } else {
195                        // either in small description, or in tooltip/intel list
196                        if (missionResult != null) {
197                                if (missionResult.payment > 0) {
198                                        info.addPara("%s received", initPad, tc, h, Misc.getDGSCredits(missionResult.payment));
199                                        initPad = 0f;
200                                }
201                                
202                                if (missionResult.rep1 != null) {
203                                        CoreReputationPlugin.addAdjustmentMessage(missionResult.rep1.delta, faction, null, 
204                                                                                                          null, null, info, tc, isUpdate, initPad);
205                                        initPad = 0f;
206                                }
207                        } else {
208                                if (mode != ListInfoMode.IN_DESC) {
209                                        info.addPara("Faction: " + faction.getDisplayName(), initPad, tc,
210                                                                                                 faction.getBaseUIColor(),
211                                                                                                 faction.getDisplayName());
212                                        initPad = 0f;
213                                }
214                                
215                                info.addPara("%s reward", initPad, tc, h, Misc.getDGSCredits(reward));
216                                addDays(info, "to complete", duration - elapsedDays, tc, 0f);
217                        }
218                }
219                
220                unindent(info);
221        }
222        
223        @Override
224        public void createIntelInfo(TooltipMakerAPI info, ListInfoMode mode) {
225                Color c = getTitleColor(mode);
226                info.addPara(getName(), c, 0f);
227                
228                addBulletPoints(info, mode);
229        }
230        
231        public String getSortString() {
232                return super.getSortString();
233                //return "Analyze";
234        }
235        
236        public String getName() {
237                String name = "";
238//              if (entity.getCustomEntitySpec() != null) {
239//                      name = entity.getCustomEntitySpec().getNameInText();
240//              } else {
241                        name = entity.getName(); // we want caps on every word since this is a title, so no getNameInText()
242//              }
243                
244                return "Analyze " + name + getPostfixForState();
245        }
246        
247
248        @Override
249        public FactionAPI getFactionForUIColors() {
250                return faction;
251        }
252
253        public String getSmallDescriptionTitle() {
254                return getName();
255        }
256        
257
258        @Override
259        public void createSmallDescription(TooltipMakerAPI info, float width, float height) {
260                Color h = Misc.getHighlightColor();
261                Color g = Misc.getGrayColor();
262                //Color c = getTitleColor(mode);
263                Color tc = Misc.getTextColor();
264                float pad = 3f;
265                float opad = 10f;
266
267                info.addImage(faction.getLogo(), width, 128, opad);
268
269                String name = "";
270                String shortName = "";
271                String isOrAre = "is";
272                String aOrAn = "a";
273                if (entity.getCustomEntitySpec() != null) {
274                        name = entity.getCustomEntitySpec().getNameInText();
275                        shortName = entity.getCustomEntitySpec().getShortName();
276                        isOrAre = entity.getCustomEntitySpec().getIsOrAre();
277                        aOrAn = entity.getCustomEntitySpec().getAOrAn();
278                } else {
279                        name = entity.getName();
280                        shortName = entity.getName();
281                }
282                
283                String authorities = "authorities";
284                if (!faction.getId().equals(market.getFactionId())) {
285                        authorities = "concerns";
286                }
287                info.addPara("%s " + authorities + " " + market.getOnOrAt() + " " + market.getName() + 
288                                " have posted a reward for running a custom sensor package on " + aOrAn + " " + name + ".",
289                                opad, faction.getBaseUIColor(), Misc.ucFirst(faction.getPersonNamePrefix()));
290                
291                String loc = BreadcrumbSpecial.getLocatedString(entity, true);
292                info.addPara("The " + shortName + " " + isOrAre + " " + loc + ".", opad);
293                
294                if (isPosted() || isAccepted()) {
295                        addBulletPoints(info, ListInfoMode.IN_DESC);
296                        
297                        addGenericMissionState(info);
298                        
299                        addAcceptOrAbandonButton(info, width, "Accept", "Abandon");
300                } else {
301                        addGenericMissionState(info);
302                        
303                        addBulletPoints(info, ListInfoMode.IN_DESC);
304                }
305
306        }
307        
308        public String getIcon() {
309                return Global.getSettings().getSpriteName("campaignMissions", "analyze_entity");
310        }
311        
312        public Set<String> getIntelTags(SectorMapAPI map) {
313                Set<String> tags = super.getIntelTags(map);
314                tags.add(Tags.INTEL_EXPLORATION);
315                tags.add(Tags.INTEL_MISSIONS);
316                tags.add(faction.getId());
317                return tags;
318        }
319
320        @Override
321        public SectorEntityToken getMapLocation(SectorMapAPI map) {
322                if (entity != null && entity.isDiscoverable() && entity.getStarSystem() != null) {
323                        return entity.getStarSystem().getCenter();
324                }
325                return entity;
326        }
327
328        
329        
330}