001package com.fs.starfarer.api.impl.campaign;
002
003import java.awt.Color;
004
005import org.lwjgl.util.vector.Vector2f;
006
007import com.fs.starfarer.api.EveryFrameScript;
008import com.fs.starfarer.api.Global;
009import com.fs.starfarer.api.campaign.LocationAPI;
010import com.fs.starfarer.api.campaign.SectorEntityToken;
011import com.fs.starfarer.api.campaign.StarSystemAPI;
012import com.fs.starfarer.api.impl.campaign.ExplosionEntityPlugin.ExplosionFleetDamage;
013import com.fs.starfarer.api.impl.campaign.ExplosionEntityPlugin.ExplosionParams;
014import com.fs.starfarer.api.impl.campaign.ids.Entities;
015import com.fs.starfarer.api.impl.campaign.ids.Factions;
016import com.fs.starfarer.api.impl.campaign.ids.Tags;
017import com.fs.starfarer.api.util.IntervalUtil;
018import com.fs.starfarer.api.util.Misc;
019
020public class GateExplosionScript implements EveryFrameScript {
021
022        public static class SystemCutOffRemoverScript implements EveryFrameScript {
023                public StarSystemAPI system;
024                public IntervalUtil interval = new IntervalUtil(0.5f, 1.5f);
025                public boolean done;
026                public float elapsed = 0f;
027                
028                public SystemCutOffRemoverScript(StarSystemAPI system) {
029                        super();
030                        this.system = system;
031                }
032                public boolean isDone() {
033                        return done;
034                }
035                public boolean runWhilePaused() {
036                        return false;
037                }
038
039                public void advance(float amount) {
040                        if (done) return;
041                        
042                        float days = Global.getSector().getClock().convertToDays(amount);
043                        elapsed += days;
044                        interval.advance(days);
045                        if (interval.intervalElapsed() && elapsed > 10f) { // make sure gate's already exploded
046                                boolean allJPUsable = true;
047                                boolean anyJPUsable = false;
048                                for (SectorEntityToken jp : system.getJumpPoints()) {
049                                        allJPUsable &= !jp.getMemoryWithoutUpdate().getBoolean(JumpPointInteractionDialogPluginImpl.UNSTABLE_KEY);
050                                        anyJPUsable |= !jp.getMemoryWithoutUpdate().getBoolean(JumpPointInteractionDialogPluginImpl.UNSTABLE_KEY);
051                                }
052                                //if (allJPUsable) {
053                                if (anyJPUsable) {
054                                        system.removeTag(Tags.SYSTEM_CUT_OFF_FROM_HYPER);
055                                        done = true;
056                                }
057                        }
058                }
059                
060        }
061        
062        public static float UNSTABLE_DAYS_MIN = 200;
063        public static float UNSTABLE_DAYS_MAX = 400;
064        
065        protected boolean done = false;
066        protected boolean playedWindup = false;
067        protected SectorEntityToken explosion = null;
068        protected float delay = 0.5f;
069        protected float delay2 = 1f;
070        
071        protected SectorEntityToken gate;
072        
073        
074        public GateExplosionScript(SectorEntityToken gate) {
075                this.gate = gate;
076                //GateEntityPlugin plugin = (GateEntityPlugin) gate.getCustomPlugin();
077                
078                // do this immediately so player can't establish a colony between when the gate explosion begins
079                // and when it ends
080                StarSystemAPI system = gate.getStarSystem();
081                if (system != null) {
082                        system.addTag(Tags.SYSTEM_CUT_OFF_FROM_HYPER);
083                        system.addScript(new SystemCutOffRemoverScript(system));
084                }
085                
086                delay = 1.2f; // plus approximately 2 seconds from how long plugin.jitter() takes to build up
087                
088        }
089
090
091        public void advance(float amount) {
092                if (done) return;
093                
094                if (!playedWindup) {
095                        if (gate.isInCurrentLocation()) {
096                                Global.getSoundPlayer().playSound("gate_explosion_windup", 1f, 1f, gate.getLocation(), Misc.ZERO);
097                        }
098                        playedWindup = true;
099                }
100                
101                
102                GateEntityPlugin plugin = (GateEntityPlugin) gate.getCustomPlugin();
103                plugin.jitter();
104                
105                if (plugin.getJitterLevel() > 0.9f) {
106                        delay -= amount;
107                }
108                if (delay <= 0 && explosion == null) {
109                        //Misc.fadeAndExpire(gate);
110
111                        LocationAPI cl = gate.getContainingLocation();
112                        Vector2f loc = gate.getLocation();
113                        Vector2f vel = gate.getVelocity();
114                        
115                        float size = gate.getRadius() + 2000f;
116                        Color color = new Color(255, 165, 100);
117                        color = new Color(100, 255, 165);
118                        color = new Color(150, 255, 200);
119                        color = new Color(100, 200, 150, 255);
120                        color = new Color(255, 255, 100, 255);
121                        color = new Color(100, 255, 150, 255);
122                        //color = new Color(255, 155, 255);
123                        //ExplosionParams params = new ExplosionParams(color, cl, loc, size, 1f);
124                        ExplosionParams params = new ExplosionParams(color, cl, loc, size, 2f);
125                        params.damage = ExplosionFleetDamage.HIGH;
126                        
127                        explosion = cl.addCustomEntity(Misc.genUID(), "Gate Explosion", 
128                                                                                        Entities.EXPLOSION, Factions.NEUTRAL, params);
129                        explosion.setLocation(loc.x, loc.y);
130                }
131                
132                if (explosion != null) {
133                        delay2 -= amount;
134                        if (!explosion.isAlive() || delay2 <= 0) {
135                                done = true;
136                                
137                                StarSystemAPI system = gate.getStarSystem();
138                                if (system != null) {
139                                        for (SectorEntityToken jp : system.getJumpPoints()) {
140                                                float days = UNSTABLE_DAYS_MIN + (UNSTABLE_DAYS_MAX - UNSTABLE_DAYS_MIN) * (float) Math.random();
141                                                jp.getMemoryWithoutUpdate().set(JumpPointInteractionDialogPluginImpl.UNSTABLE_KEY, true, days);
142                                        }
143                                }
144                        }
145                }
146        }
147        
148        
149        
150        
151        public boolean isDone() {
152                return done;
153        }
154
155        public boolean runWhilePaused() {
156                return false;
157        }
158        
159}
160
161
162
163