001package com.fs.starfarer.api.impl.campaign.enc;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import com.fs.starfarer.api.EveryFrameScript;
007import com.fs.starfarer.api.Global;
008import com.fs.starfarer.api.campaign.CampaignFleetAPI;
009import com.fs.starfarer.api.campaign.JumpPointAPI;
010import com.fs.starfarer.api.campaign.JumpPointAPI.JumpDestination;
011import com.fs.starfarer.api.campaign.NascentGravityWellAPI;
012import com.fs.starfarer.api.campaign.SectorEntityToken;
013import com.fs.starfarer.api.campaign.StarSystemAPI;
014import com.fs.starfarer.api.util.IntervalUtil;
015import com.fs.starfarer.api.util.Misc;
016
017public class AbyssalLocationDespawner implements EveryFrameScript {
018
019        protected IntervalUtil interval = new IntervalUtil(1f, 2f); 
020        protected StarSystemAPI system;
021        //protected float elapsed;
022
023        public AbyssalLocationDespawner(StarSystemAPI system) {
024                this.system = system;
025        }
026
027        public void advance(float amount) {
028                if (system == null) return;
029                
030                
031                interval.advance(amount);
032                if (interval.intervalElapsed()) {
033                        
034                        // this part is now handled by StrandedGiveTJScript
035//                      if (system.isCurrentLocation()) {
036//                              elapsed += interval.getIntervalDuration();
037//                              CampaignFleetAPI pf = Global.getSector().getPlayerFleet();
038//                              if (!pf.hasAbility(Abilities.TRANSVERSE_JUMP) && elapsed > 60f &&
039//                                              !Global.getSector().getCampaignUI().isShowingDialog() && 
040//                                              !Global.getSector().getCampaignUI().isShowingMenu()) {
041//                                      Misc.showRuleDialog(pf, "StrandedInDeepSpace");
042//                              }
043//                              return;
044//                      } else {
045//                              elapsed = 0f;
046//                      }
047                        
048                        if (system.getDaysSinceLastPlayerVisit() < 10f) {
049                                return;
050                        }
051                        
052                        CampaignFleetAPI pf = Global.getSector().getPlayerFleet();
053                        if (pf == null) {
054                                return;
055                        }
056                        float distLY = Misc.getDistanceLY(pf.getLocationInHyperspace(), system.getLocation());
057                        if (distLY < 5) {
058                                return;
059                        }
060                        
061                        Global.getSector().removeStarSystemNextFrame(system);
062                        
063                        
064                        List<SectorEntityToken> remove = new ArrayList<SectorEntityToken>();
065                        for (SectorEntityToken curr : Global.getSector().getHyperspace().getJumpPoints()) {
066                                JumpPointAPI jp = (JumpPointAPI) curr;
067                                if (jp.getDestinations().isEmpty()) continue;
068                                JumpDestination dest = jp.getDestinations().get(0);
069                                if (dest.getDestination() == null) continue;
070                                if (system == dest.getDestination().getStarSystem()) {
071                                        remove.add(curr);
072                                }
073                        }
074                        
075                        for (NascentGravityWellAPI curr : Global.getSector().getHyperspace().getGravityWells()) {
076                                if (curr.getTarget() == null) continue;
077                                if (system == curr.getTarget().getStarSystem()) {
078                                        remove.add(curr);
079                                }
080                        }
081                        
082                        remove.add(system.getHyperspaceAnchor());
083                        
084                        for (SectorEntityToken curr : remove) {
085                                Global.getSector().getHyperspace().removeEntity(curr);
086                        }
087                        
088                        system = null;
089                }
090        }
091        
092        public boolean isDone() {
093                return system == null;
094        }
095
096        public boolean runWhilePaused() {
097                return false;
098        }       
099}
100
101
102