001package com.fs.starfarer.api.impl.campaign.intel;
002
003import java.util.ArrayList;
004
005import com.fs.starfarer.api.EveryFrameScript;
006import com.fs.starfarer.api.Global;
007import com.fs.starfarer.api.campaign.SectorEntityToken;
008import com.fs.starfarer.api.campaign.StarSystemAPI;
009import com.fs.starfarer.api.impl.campaign.ids.Tags;
010import com.fs.starfarer.api.impl.campaign.intel.GenericMissionManager.GenericMissionCreator;
011import com.fs.starfarer.api.util.Misc;
012import com.fs.starfarer.api.util.WeightedRandomPicker;
013
014public class AnalyzeEntityIntelCreator implements GenericMissionCreator {
015
016        public EveryFrameScript createMissionIntel() {
017                SectorEntityToken entity = pickEntity();
018                if (entity == null) return null;
019                return new AnalyzeEntityMissionIntel(entity);
020        }
021        
022        public float getMissionFrequencyWeight() {
023                return 15f;
024        }
025        
026        
027        protected transient WeightedRandomPicker<SectorEntityToken> entityPicker = null;
028        
029        protected void initPicker() {
030                entityPicker = new WeightedRandomPicker<SectorEntityToken>();
031                for (StarSystemAPI system : Global.getSector().getStarSystems()) {
032                        if (system.hasTag(Tags.THEME_DERELICT_MOTHERSHIP)) continue;
033                        if (system.hasTag(Tags.THEME_DERELICT_CRYOSLEEPER)) continue;
034                        
035                        if (system.hasTag(Tags.THEME_DERELICT_PROBES) || 
036                                        system.hasTag(Tags.THEME_RUINS) ||
037                                        system.hasTag(Tags.THEME_REMNANT_DESTROYED)) {
038                                
039                                float w = 1f;
040                                if (system.hasTag(Tags.THEME_DERELICT_PROBES)) {
041                                        w = 3f;
042                                        if (Global.getSector().isInNewGameAdvance()) {
043                                                w = 5f;
044                                        }
045                                }
046                                for (SectorEntityToken entity : system.getEntitiesWithTag(Tags.SALVAGEABLE)) {
047                                        // skip derelict ships etc that will expire
048                                        if (entity.hasTag(Tags.EXPIRES)) continue;
049                                        if (Misc.isImportantForReason(entity.getMemoryWithoutUpdate(), "aem")) continue;
050                                        if (entity.hasTag(Tags.NOT_RANDOM_MISSION_TARGET)) continue;
051                                        if (entity.getMemoryWithoutUpdate() != null && entity.getMemoryWithoutUpdate().getBoolean("$ttWeaponsCache")) continue;
052                                        if (entity.getCircularOrbitRadius() > 10000f) continue;
053                                        if (entity.getContainingLocation() != null && entity.getContainingLocation().hasTag(Tags.THEME_HIDDEN)) continue;
054                                        entityPicker.add(entity, w);
055                                }
056                                
057                        }
058                }
059        }
060        
061        protected void prunePicker() {
062                for (SectorEntityToken item : new ArrayList<SectorEntityToken>(entityPicker.getItems())) {
063                        if (!item.isAlive()) {
064                                entityPicker.remove(item);
065                        }
066                }
067        }
068
069        protected SectorEntityToken pickEntity() {
070                if (entityPicker == null) {
071                        initPicker();
072                }
073                prunePicker();
074                
075                SectorEntityToken entity = entityPicker.pick();
076                
077                for (EveryFrameScript s : GenericMissionManager.getInstance().getActive()) {
078                        if (s instanceof AnalyzeEntityMissionIntel) {
079                                AnalyzeEntityMissionIntel intel = (AnalyzeEntityMissionIntel) s;
080                                if (entity == intel.getEntity()) {
081                                        return null;
082                                }
083                        }
084                }
085                
086                return entity;
087        }
088        
089
090}
091
092
093