001package com.fs.starfarer.api.impl.campaign.procgen.themes;
002
003import java.util.ArrayList;
004import java.util.List;
005import java.util.Random;
006
007import com.fs.starfarer.api.Global;
008import com.fs.starfarer.api.campaign.CampaignEventListener.FleetDespawnReason;
009import com.fs.starfarer.api.campaign.CampaignFleetAPI;
010import com.fs.starfarer.api.campaign.LocationAPI;
011import com.fs.starfarer.api.campaign.SectorEntityToken;
012import com.fs.starfarer.api.campaign.StarSystemAPI;
013import com.fs.starfarer.api.impl.campaign.enc.EncounterManager;
014import com.fs.starfarer.api.impl.campaign.enc.EncounterPoint;
015import com.fs.starfarer.api.impl.campaign.enc.EncounterPointProvider;
016import com.fs.starfarer.api.impl.campaign.fleets.FleetFactoryV3;
017import com.fs.starfarer.api.impl.campaign.fleets.FleetParamsV3;
018import com.fs.starfarer.api.impl.campaign.fleets.SourceBasedFleetManager;
019import com.fs.starfarer.api.impl.campaign.ids.Factions;
020import com.fs.starfarer.api.impl.campaign.ids.FleetTypes;
021
022public class RemnantStationFleetManager extends SourceBasedFleetManager {
023
024        public class RemnantSystemEPGenerator implements EncounterPointProvider {
025                public List<EncounterPoint> generateEncounterPoints(LocationAPI where) {
026                        if (!where.isHyperspace()) return null;
027                        if (totalLost > 0 && source != null) {
028                                String id = "ep_" + source.getId();
029                                EncounterPoint ep = new EncounterPoint(id, where, source.getLocationInHyperspace(), EncounterManager.EP_TYPE_OUTSIDE_SYSTEM);
030                                ep.custom = this;
031                                List<EncounterPoint> result = new ArrayList<EncounterPoint>();
032                                result.add(ep);
033                                return result;//source.getContainingLocation().getName()
034                        }
035                        return null;
036                }
037        }
038        
039        protected int minPts;
040        protected int maxPts;
041        protected int totalLost;
042        protected transient RemnantSystemEPGenerator epGen;
043
044        public RemnantStationFleetManager(SectorEntityToken source, float thresholdLY, int minFleets, int maxFleets, float respawnDelay, 
045                                                                          int minPts, int maxPts) {
046                super(source, thresholdLY, minFleets, maxFleets, respawnDelay);
047                this.minPts = minPts;
048                this.maxPts = maxPts;
049        }
050        
051        protected Object readResolve() {
052                return this;
053        }
054        
055        protected transient boolean addedListener = false;
056        @Override
057        public void advance(float amount) {
058                if (!addedListener) {
059                        //totalLost = 1;
060                        /* best code ever -dgb
061                        if (Global.getSector().getPlayerPerson() != null && 
062                                        Global.getSector().getPlayerPerson().getNameString().equals("Dave Salvage") &&
063                                        Global.getSector().getClock().getDay() == 15 &&
064                                                        Global.getSector().getClock().getMonth() == 12 && 
065                                                        Global.getSector().getClock().getCycle() == 206) {
066                                totalLost = 0;
067                        }*/
068                        // global listener needs to be not this class since SourceBasedFleetManager
069                        // adds it to all fleets as their event listener
070                        // and so you'd get reportFleetDespawnedToListener() called multiple times
071                        // from global listeners, and from fleet ones
072                        epGen = new RemnantSystemEPGenerator();
073                        Global.getSector().getListenerManager().addListener(epGen, true);
074                        addedListener = true;
075                }
076                super.advance(amount);
077        }
078        
079
080        @Override
081        protected CampaignFleetAPI spawnFleet() {
082                if (source == null) return null;
083                
084                Random random = new Random();
085                
086                int combatPoints = minPts + random.nextInt(maxPts - minPts + 1);
087                
088                int bonus = totalLost * 4;
089                if (bonus > maxPts) bonus = maxPts;
090                
091                combatPoints += bonus;
092                
093                String type = FleetTypes.PATROL_SMALL;
094                if (combatPoints > 8) type = FleetTypes.PATROL_MEDIUM;
095                if (combatPoints > 16) type = FleetTypes.PATROL_LARGE;
096                
097                combatPoints *= 8f;
098                
099                FleetParamsV3 params = new FleetParamsV3(
100                                source.getMarket(),
101                                source.getLocationInHyperspace(),
102                                Factions.REMNANTS,
103                                1f,
104                                type,
105                                combatPoints, // combatPts
106                                0f, // freighterPts 
107                                0f, // tankerPts
108                                0f, // transportPts
109                                0f, // linerPts
110                                0f, // utilityPts
111                                0f // qualityMod
112                );
113                //params.officerNumberBonus = 10;
114                params.random = random;
115                
116                CampaignFleetAPI fleet = FleetFactoryV3.createFleet(params);
117                if (fleet == null) return null;;
118                
119                
120                LocationAPI location = source.getContainingLocation();
121                location.addEntity(fleet);
122                
123                RemnantSeededFleetManager.initRemnantFleetProperties(random, fleet, false);
124                
125                fleet.setLocation(source.getLocation().x, source.getLocation().y);
126                fleet.setFacing(random.nextFloat() * 360f);
127                
128                fleet.addScript(new RemnantAssignmentAI(fleet, (StarSystemAPI) source.getContainingLocation(), source));
129                fleet.getMemoryWithoutUpdate().set("$sourceId", source.getId());
130                
131                return fleet;
132        }
133
134        
135        @Override
136        public void reportFleetDespawnedToListener(CampaignFleetAPI fleet, FleetDespawnReason reason, Object param) {
137                super.reportFleetDespawnedToListener(fleet, reason, param);
138                if (reason == FleetDespawnReason.DESTROYED_BY_BATTLE) {
139                        String sid = fleet.getMemoryWithoutUpdate().getString("$sourceId");
140                        if (sid != null && source != null && sid.equals(source.getId())) {
141                        //if (sid != null && sid.equals(source.getId())) {
142                                totalLost++;
143                        }
144                }
145        }
146
147        public int getTotalLost() {
148                return totalLost;
149        }
150
151        
152}
153
154
155
156