001package com.fs.starfarer.api.impl.campaign.intel.events;
002
003import com.fs.starfarer.api.EveryFrameScript;
004import com.fs.starfarer.api.Global;
005import com.fs.starfarer.api.campaign.StarSystemAPI;
006import com.fs.starfarer.api.campaign.comm.IntelInfoPlugin;
007import com.fs.starfarer.api.campaign.econ.MarketAPI;
008import com.fs.starfarer.api.impl.campaign.ids.Factions;
009import com.fs.starfarer.api.impl.campaign.ids.Industries;
010import com.fs.starfarer.api.impl.campaign.intel.SystemBountyIntel;
011import com.fs.starfarer.api.util.IntervalUtil;
012import com.fs.starfarer.api.util.Misc;
013
014public class CommerceBountyManager implements EveryFrameScript {
015
016        protected IntervalUtil tracker = new IntervalUtil(0.5f, 1.5f);
017        
018        public boolean isDone() {
019                return false;
020        }
021
022        public boolean runWhilePaused() {
023                return false;
024        }
025
026        public void advance(float amount) {
027                float days = Global.getSector().getClock().convertToDays(amount);
028                tracker.advance(days);
029                if (tracker.intervalElapsed()) {
030//                      HostileActivityEventIntel ha = HostileActivityEventIntel.get();
031//                      if (ha == null) return;
032                        
033                        //boolean haLevelSufficient = ha.isStageActive(Stage.HA_1);
034                        boolean haLevelSufficient = true;
035                        
036                        for (StarSystemAPI system : Misc.getPlayerSystems(false)) {
037                                SystemBountyIntel bounty = getCommerceBounty(system);
038                                if (bounty != null && bounty.isEnding()) continue;
039                                
040                                boolean hasFunctionalCommerce = doesStarSystemHavePlayerCommerceIndustry(system, true);
041                                boolean hasCommerce = doesStarSystemHavePlayerCommerceIndustry(system, false);
042                        
043                                if (bounty != null && (!hasCommerce || !haLevelSufficient)) {
044                                        bounty.endAfterDelay();
045                                        continue;
046                                }
047                                
048                                MarketAPI market = getPlayerCommerceMarket(system);
049                                if (market == null) continue;
050                                
051                                if (bounty == null && hasFunctionalCommerce && haLevelSufficient) {
052                                        float reward = Global.getSettings().getFloat("baseCommerceSystemBounty");
053                                        new SystemBountyIntel(market, (int) reward, true);
054                                } 
055                        }
056                }
057        }
058        
059        public static SystemBountyIntel getCommerceBounty(StarSystemAPI system) {
060                for (IntelInfoPlugin curr : Global.getSector().getIntelManager().getIntel(SystemBountyIntel.class)) {
061                        SystemBountyIntel intel = (SystemBountyIntel) curr;
062                        if (intel.getLocation() == system && intel.isCommerceMode()) return intel;
063                }
064                return null;
065        }
066        public static boolean doesStarSystemHavePlayerCommerceIndustry(StarSystemAPI system, boolean requireFunctional) {
067                for (MarketAPI market : Misc.getMarketsInLocation(system, Factions.PLAYER)) {
068                        if (requireFunctional && market.hasFunctionalIndustry(Industries.COMMERCE)) {
069                                return true;
070                        }
071                        if (!requireFunctional && market.hasIndustry(Industries.COMMERCE)) {
072                                return true;
073                        }
074                }
075                return false;
076        }
077        public static MarketAPI getPlayerCommerceMarket(StarSystemAPI system) {
078                MarketAPI best = null;
079                for (MarketAPI market : Misc.getMarketsInLocation(system, Factions.PLAYER)) {
080                        if (market.hasIndustry(Industries.COMMERCE)) {
081                                if (best == null || best.getSize() < market.getSize()) {
082                                        best = market;
083                                }
084                        }
085                }
086                return best;
087        }
088
089}
090
091
092
093