Advertisement
DarkRevenant

Untitled

May 17th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.46 KB | None | 0 0
  1. package data.scripts.plugins;
  2.  
  3. import com.fs.starfarer.api.Global;
  4. import com.fs.starfarer.api.combat.CombatEngineAPI;
  5. import com.fs.starfarer.api.combat.EveryFrameCombatPlugin;
  6. import com.fs.starfarer.api.combat.ShipAPI;
  7. import com.fs.starfarer.api.combat.ShipAPI.HullSize;
  8. import com.fs.starfarer.api.input.InputEventAPI;
  9. import com.fs.starfarer.api.util.IntervalUtil;
  10.  
  11. import java.awt.Color;
  12. import java.util.*;
  13.  
  14. import data.scripts.util.AnamorphicFlare;
  15. import org.dark.shaders.distortion.DistortionShader;
  16. import org.dark.shaders.distortion.RippleDistortion;
  17. import org.dark.shaders.light.LightShader;
  18. import org.dark.shaders.light.StandardLight;
  19.  
  20. import org.lwjgl.util.vector.Vector2f;
  21. import org.lazywizard.lazylib.CollisionUtils;
  22. import org.lazywizard.lazylib.combat.entities.AnchoredEntity;
  23.  
  24. public class ShipDestructionEffects implements EveryFrameCombatPlugin {
  25.  
  26.     private CombatEngineAPI engine;
  27.     private static final Set<ShipAPI> deadShips = new LinkedHashSet<>(); // Faster for what we're doing
  28.     private static final List<ExplodingShip> explodingShips = new ArrayList<>();
  29.  
  30.     private static final Map<HullSize, Float> explosionLength = new HashMap<>();
  31.  
  32.     static {
  33.         explosionLength.put(HullSize.FIGHTER, 0.5f);
  34.         explosionLength.put(HullSize.FRIGATE, 2.5f);
  35.         explosionLength.put(HullSize.DESTROYER, 5f);
  36.         explosionLength.put(HullSize.DEFAULT, 5f);
  37.         explosionLength.put(HullSize.CRUISER, 7.5f);
  38.         explosionLength.put(HullSize.CAPITAL_SHIP, 10f);
  39.     }
  40.  
  41.     private static final Map<HullSize, Float> explosionSizeMod = new HashMap<>();
  42.  
  43.     static {
  44.         explosionSizeMod.put(HullSize.FIGHTER, 0.5f);
  45.         explosionSizeMod.put(HullSize.FRIGATE, 0.75f);
  46.         explosionSizeMod.put(HullSize.DESTROYER, 1f);
  47.         explosionSizeMod.put(HullSize.DEFAULT, 1f);
  48.         explosionSizeMod.put(HullSize.CRUISER, 1.25f);
  49.         explosionSizeMod.put(HullSize.CAPITAL_SHIP, 1.5f);
  50.     }
  51.  
  52.     private static final Map<HullSize, Float> flareBrightness = new HashMap<>();
  53.  
  54.     static {
  55.         flareBrightness.put(HullSize.FIGHTER, 10f);
  56.         flareBrightness.put(HullSize.FRIGATE, 25f);
  57.         flareBrightness.put(HullSize.DESTROYER, 50f);
  58.         flareBrightness.put(HullSize.DEFAULT, 50f);
  59.         flareBrightness.put(HullSize.CRUISER, 100f);
  60.         flareBrightness.put(HullSize.CAPITAL_SHIP, 250f);
  61.     }
  62.  
  63.     private static final Map<HullSize, Float> flareThickness = new HashMap<>();
  64.  
  65.     static {
  66.         flareThickness.put(HullSize.FIGHTER, 0.33f);
  67.         flareThickness.put(HullSize.FRIGATE, 0.10f);
  68.         flareThickness.put(HullSize.DESTROYER, 0.06f);
  69.         flareThickness.put(HullSize.DEFAULT, 0.06f);
  70.         flareThickness.put(HullSize.CRUISER, 0.04f);
  71.         flareThickness.put(HullSize.CAPITAL_SHIP, 0.01f);
  72.     }
  73.  
  74.     private static final Map<HullSize, Float> lightDuration = new HashMap<>();
  75.  
  76.     static {
  77.         lightDuration.put(HullSize.FIGHTER, 0f);
  78.         lightDuration.put(HullSize.FRIGATE, 0.5f);
  79.         lightDuration.put(HullSize.DESTROYER, 1f);
  80.         lightDuration.put(HullSize.DEFAULT, 1f);
  81.         lightDuration.put(HullSize.CRUISER, 1.5f);
  82.         lightDuration.put(HullSize.CAPITAL_SHIP, 2f);
  83.     }
  84.  
  85.     private static final Map<HullSize, Float> lightIntensity = new HashMap<>();
  86.  
  87.     static {
  88.         lightIntensity.put(HullSize.FIGHTER, 0.5f);
  89.         lightIntensity.put(HullSize.FRIGATE, 1f);
  90.         lightIntensity.put(HullSize.DESTROYER, 1.5f);
  91.         lightIntensity.put(HullSize.DEFAULT, 1.5f);
  92.         lightIntensity.put(HullSize.CRUISER, 1.75f);
  93.         lightIntensity.put(HullSize.CAPITAL_SHIP, 2f);
  94.     }
  95.  
  96.     private static final Map<HullSize, Float> rippleLength = new HashMap<>();
  97.  
  98.     static {
  99.         rippleLength.put(HullSize.FIGHTER, 0.5f);
  100.         rippleLength.put(HullSize.FRIGATE, 0.75f);
  101.         rippleLength.put(HullSize.DESTROYER, 1f);
  102.         rippleLength.put(HullSize.DEFAULT, 1f);
  103.         rippleLength.put(HullSize.CRUISER, 1.25f);
  104.         rippleLength.put(HullSize.CAPITAL_SHIP, 1.5f);
  105.     }
  106.  
  107.     @Override
  108.     public void init(CombatEngineAPI engine) {
  109.     }
  110.  
  111.     private final IntervalUtil interval = new IntervalUtil(0.1f, 0.1f);
  112.  
  113.     @Override
  114.     public void advance(float amount, List<InputEventAPI> events) {
  115.         if (engine != Global.getCombatEngine()) {
  116.             deadShips.clear();
  117.             for (ExplodingShip exploder : explodingShips) {
  118.                 exploder.flamePoints.clear();
  119.             }
  120.             explodingShips.clear();
  121.             this.engine = Global.getCombatEngine();
  122.             return;
  123.         }
  124.  
  125.         if (engine.isPaused()) {
  126.             return;
  127.         }
  128.  
  129.         interval.advance(amount);
  130.  
  131.         List<ShipAPI> ships = engine.getShips();
  132.  
  133.         // We run through all the ships and check for newly-destroyed ships, adding them to the graphics loop
  134.         for (ShipAPI ship : ships) {
  135.             if (ship == null) {
  136.                 continue;
  137.             }
  138.  
  139.             if (ship.isHulk() == true) {
  140.                 if (!deadShips.contains(ship)) {
  141.                     deadShips.add(ship);
  142.  
  143.                     if (ship.getHullSize() != ShipAPI.HullSize.FIGHTER && !ship.isDrone()) {
  144.                         float intensity = flareBrightness.get(ship.getHullSize()) / 10f;
  145.                         float intensity2 = flareThickness.get(ship.getHullSize()) / 3f;
  146.                         AnamorphicFlare.createFlare(ship, new Vector2f(ship.getLocation()), engine, intensity, intensity2, 0f, 15f, 1f, new Color(255, 180, 150), new Color(255, 255, 255));
  147.                     }
  148.                    
  149.                     if (!ship.isFighter()) {
  150.                         RippleDistortion ripple = new RippleDistortion();
  151.                         ripple.setSize(ship.getCollisionRadius() * 4.5f);
  152.                         ripple.setIntensity(ship.getCollisionRadius() * lightIntensity.get(ship.getHullSize()));
  153.                         ripple.setLocation(ship.getLocation());
  154.                         ripple.setFrameRate(60f / rippleLength.get(ship.getHullSize()));
  155.                         ripple.fadeInSize(rippleLength.get(ship.getHullSize()) * 1.5f);
  156.                         ripple.fadeOutIntensity(rippleLength.get(ship.getHullSize()));
  157.                         ripple.setSize(ship.getCollisionRadius() * 1.5f);
  158.                         DistortionShader.addDistortion(ripple);
  159.                     }
  160.  
  161.                     StandardLight light = new StandardLight();
  162.                     light.setSize(ship.getCollisionRadius() * explosionSizeMod.get(ship.getHullSize()) * 4f);
  163.                     light.setIntensity(lightIntensity.get(ship.getHullSize()));
  164.                     light.setColor(1f, 0.7f, 0.6f);
  165.                     light.setLocation(ship.getLocation());
  166.                     light.setLifetime(lightDuration.get(ship.getHullSize()));
  167.                     light.setAutoFadeOutTime(0.5f + lightDuration.get(ship.getHullSize()));
  168.                     LightShader.addLight(light);
  169.  
  170.                     if (!ship.isShuttlePod() && !ship.isDrone()) {
  171.                         int count = (int) (ship.getCollisionRadius() * explosionSizeMod.get(ship.getHullSize()) / 4f);
  172.                         float length = explosionLength.get(ship.getHullSize()) * ((float) Math.random() * 0.5f + 0.75f);
  173.  
  174.                         ExplodingShip exploder = new ExplodingShip(ship, count, length / (float) count);
  175.                         explodingShips.add(exploder);
  176.                     }
  177.                 }
  178.             }
  179.         }
  180.  
  181.         // If a ship is vaporized, we add some extra effects
  182.         Iterator<ShipAPI> iter = deadShips.iterator();
  183.         while (iter.hasNext()) {
  184.             ShipAPI ship = iter.next();
  185.  
  186.             if (ship != null && !ships.contains(ship)) {
  187.                 //Vector2f velocity = new Vector2f(ship.getVelocity());
  188.                 Vector2f velocity = new Vector2f();
  189.  
  190.                 engine.addHitParticle(ship.getLocation(), velocity, ship.getCollisionRadius() * 15f, 0.75f, ship.getCollisionRadius() / 15f, new Color(255, 255, 255, 255));
  191.                 engine.addSmoothParticle(ship.getLocation(), velocity, ship.getCollisionRadius() * 10f, 0.25f, ship.getCollisionRadius() / 10f, new Color(255, 255, 255, 50));
  192.  
  193.                 float sizeMod = explosionSizeMod.get(ship.getHullSize());
  194.                 int particles = (int) (sizeMod * ship.getCollisionRadius() / 2f * ((float) Math.random() * 0.5f + 0.75f));
  195.  
  196.                 for (int i = 0; i < particles; i++) {
  197.                     Vector2f point = new Vector2f(ship.getLocation());
  198.                     Vector2f vel = new Vector2f(velocity);
  199.                     vel.x += ((float) Math.random() + (float) Math.random() + (float) Math.random()) * 100f / 3f - 50f;
  200.                     vel.y += ((float) Math.random() + (float) Math.random() + (float) Math.random()) * 100f / 3f - 50f;
  201.                     point.x += ship.getCollisionRadius() * ((float) Math.random() * 1f - 0.5f);
  202.                     point.y += ship.getCollisionRadius() * ((float) Math.random() * 1f - 0.5f);
  203.  
  204.                     Color color = new Color((int) ((float) Math.random() * 35f + 220f), (int) ((float) Math.random() * 180f + 40f), (int) ((float) Math.random() * 100f));
  205.                     Color color2 = new Color((int) ((float) Math.random() * 35f + 220f), (int) ((float) Math.random() * 140f + 40f), (int) ((float) Math.random() * 40f));
  206.  
  207.                     engine.addHitParticle(point, vel, (float) Math.random() * 5f + 5f, 1f, (ship.getCollisionRadius() / 5f) * ((float) Math.random() * 1.5f + 0.25f), color);
  208.                     engine.addHitParticle(point, vel, (float) Math.random() * 15f + 30f, 0.2f, (ship.getCollisionRadius() / 5f) * ((float) Math.random() * 1.5f + 0.25f), color2);
  209.                 }
  210.  
  211.                 int fire = (int) (sizeMod * ship.getCollisionRadius() / 25f * ((float) Math.random() * 0.5f + 0.75f));
  212.  
  213.                 for (int i = 0; i < fire; i++) {
  214.                     Vector2f point = new Vector2f(ship.getLocation());
  215.                     Vector2f vel = new Vector2f(velocity);
  216.                     vel.x += (float) Math.random() * 20f - 10f;
  217.                     vel.y += (float) Math.random() * 20f - 10f;
  218.                     point.x += ship.getCollisionRadius() * ((float) Math.random() * 1.5f - 0.75f);
  219.                     point.y += ship.getCollisionRadius() * ((float) Math.random() * 1.5f - 0.75f);
  220.  
  221.                     Color color = new Color((int) ((float) Math.random() * 35f + 220f), (int) ((float) Math.random() * 140f + 40f), (int) ((float) Math.random() * 40f));
  222.                     float size = ((float) Math.random() * 30f + 100f) * sizeMod;
  223.  
  224.                     engine.spawnExplosion(point, vel, color, size, (ship.getCollisionRadius() / 25f) * ((float) Math.random() * 0.5f + 0.75f));
  225.                 }
  226.  
  227.                 /*int debris = (int) ((Float)(explosionSizeMod.get(ship.getHullSize())) * ship.getCollisionRadius() / 5f * ((float)Math.random() * 0.5f + 0.75f));
  228.                    
  229.                  for (int i = 0; i < debris; i++)
  230.                  {
  231.                  Vector2f point = new Vector2f(ship.getLocation());
  232.                  Vector2f vel = new Vector2f(ship.getVelocity());
  233.                  vel.x += (float)Math.random() * 20f - 10f;
  234.                  vel.y += (float)Math.random() * 20f - 10f;
  235.                  point.x += ship.getCollisionRadius() * ((float)Math.random() * 1.5f - 0.75f);
  236.                  point.y += ship.getCollisionRadius() * ((float)Math.random() * 1.5f - 0.75f);
  237.                  vel.x += (point.x - ship.getLocation().x) * (float)Math.random();
  238.                  vel.y += (point.y - ship.getLocation().y) * (float)Math.random();
  239.                        
  240.                  CombatEntityAPI chunk = engine.spawnProjectile(engine.getPlayerShip(), null, "debris", point, (float)Math.random() * 360f, vel);
  241.                  //chunk.setFacing((float)Math.atan2(vel.y, vel.x) * 180f / (float)Math.PI);
  242.                  }*/
  243.                 iter.remove();
  244.             }
  245.         }
  246.  
  247.         // Now for the juicy bit...
  248.         Iterator<ExplodingShip> iter2 = explodingShips.iterator();
  249.         while (iter2.hasNext()) {
  250.             ExplodingShip exploder = iter2.next();
  251.  
  252.             if (exploder.ship == null) {
  253.                 exploder.flamePoints.clear();
  254.                 iter2.remove();
  255.                 continue;
  256.             }
  257.  
  258.             // If the ship is vaporized, it gets removed from this graphics loop
  259.             if (!engine.getShips().contains(exploder.ship)) {
  260.                 exploder.flamePoints.clear();
  261.                 iter2.remove();
  262.                 continue;
  263.             }
  264.  
  265.             // Draw fire contrails from our burning wrecks
  266.             Iterator<FlamePoint> iter3 = exploder.flamePoints.iterator();
  267.             while (iter3.hasNext()) {
  268.                 FlamePoint flamePoint = iter3.next();
  269.  
  270.                 // Check if the flame contrail has expired yet
  271.                 if (!flamePoint.tick(amount)) {
  272.                     // Don't want to generate too many flames...
  273.                     if (interval.intervalElapsed()) {
  274.                         Vector2f point = new Vector2f(flamePoint.anchor.getLocation());
  275.                         Vector2f vel = new Vector2f();
  276.                         Vector2f vel2 = new Vector2f();
  277.  
  278.                         Color color = new Color((int) ((float) Math.random() * 35f + 220f), (int) ((float) Math.random() * 140f + 40f), (int) ((float) Math.random() * 40f));
  279.                         Color color2 = new Color((int) ((float) Math.random() * 40f + 20f), (int) ((float) Math.random() * 20f + 20f), 20, (int) ((float) Math.random() * 30f + 30f));
  280.  
  281.                         float sizeMod = explosionSizeMod.get(exploder.ship.getHullSize());
  282.                         float size = ((float) Math.random() * 25f) * sizeMod * flamePoint.scale;
  283.                         float size2 = ((float) Math.random() * 10f + 40f) * sizeMod * flamePoint.scale;
  284.                         float size3 = ((float) Math.random() * 40f + 80f) * sizeMod * flamePoint.scale;
  285.                         float size4 = ((float) Math.random() * 15f + 5f) * flamePoint.scale;
  286.  
  287.                         vel.x += (float) Math.random() * 40f - 20f;
  288.                         vel.y += (float) Math.random() * 40f - 20f;
  289.                         vel2.x += (float) Math.random() * 20f - 10f;
  290.                         vel2.y += (float) Math.random() * 20f - 10f;
  291.  
  292.                         // Make fewer flames if the wreck is not moving as quickly
  293.                         if ((float) Math.random() <= (flamePoint.anchor.getVelocity().length() + 50f) / 75f) {
  294.                             engine.addHitParticle(point, flamePoint.anchor.getVelocity(), size4, 0.65f, (float) Math.random() * 0.15f + 0.3f, new Color(255, 225, 175));
  295.                             if ((float) Math.random() >= 0.5f) {
  296.                                 engine.spawnExplosion(point, vel, color, size, (float) Math.random() * 1f + 0.5f);
  297.                                 engine.addSmokeParticle(point, vel2, size2, (float) Math.random() * 0.025f + 0.1f, (float) Math.random() * 4f + 4f, color2);
  298.                             } else {
  299.                                 engine.addHitParticle(point, vel, size3, (float) Math.random() * 0.05f + 0.05f, (float) Math.random() * 0.5f + 0.25f, color);
  300.                             }
  301.                         }
  302.                     }
  303.                 } else {
  304.                     iter3.remove();
  305.                 }
  306.             }
  307.  
  308.             // If the ship is an inert wreck, we flag it for removal from the loop
  309.             if (exploder.flamePoints.isEmpty() && exploder.count <= 0) {
  310.                 iter2.remove();
  311.                 continue;
  312.             }
  313.  
  314.             if (!exploder.tick(amount)) {
  315.                 continue;
  316.             }
  317.  
  318.             if (exploder.count <= 0) {
  319.                 continue;
  320.             }
  321.  
  322.             ShipAPI ship = exploder.ship;
  323.  
  324.             // And now we make it explode nicely...
  325.             while (true) {
  326.                 Vector2f point = new Vector2f(ship.getLocation());
  327.                 point.x += ship.getCollisionRadius() * (((float) Math.random() * 2f) - 1);
  328.                 point.y += ship.getCollisionRadius() * (((float) Math.random() * 2f) - 1);
  329.  
  330.                 if (CollisionUtils.isPointWithinBounds(point, ship)) {
  331.                     Vector2f vel = new Vector2f(ship.getVelocity());
  332.                     Vector2f vel2 = new Vector2f(ship.getVelocity());
  333.                     float rand = (float) Math.random() * 0.5f + 0.5f;
  334.                     float sizeMod = explosionSizeMod.get(ship.getHullSize());
  335.                     float size = (30f + (float) Math.random() * 30f) * sizeMod;
  336.                     Color color = new Color((int) ((float) Math.random() * 35f + 220f), (int) ((float) Math.random() * 140f + 40f), (int) ((float) Math.random() * 40f));
  337.  
  338.                     vel.x *= rand;
  339.                     vel.y *= rand;
  340.                     vel2.x *= rand * 0.5f;
  341.                     vel2.y *= rand * 0.5f;
  342.  
  343.                     engine.spawnExplosion(point, vel, color, size, size / 25f);
  344.                     engine.addSmokeParticle(point, vel2, size, 0.25f, size / 10f, new Color(50, 40, 30, 90));
  345.                     //engine.applyDamage(ship, point, size, DamageType.HIGH_EXPLOSIVE, 0f, true, false, null);
  346.                     if ((float) Math.random() >= 0.75) {
  347.                         Global.getSoundPlayer().playSound("explosion_from_damage", 0.95f + (float) Math.random() * 0.05f, sizeMod / 2f, point, vel);
  348.                         engine.addHitParticle(point, new Vector2f(ship.getVelocity()), size * 3f, 1f, size / 60f, new Color(255, 255, 255, 255));
  349.  
  350.                         float length = explosionLength.get(ship.getHullSize()) * ((float) Math.random() * 5f + 2.5f);
  351.                         exploder.flamePoints.add(new FlamePoint(new AnchoredEntity(ship, point), length, (float) Math.random() * 1.3f + 0.35f));
  352.                     } else {
  353.                         engine.addHitParticle(point, new Vector2f(ship.getVelocity()), size, 0.5f, size / 60f, new Color(255, 255, 255, 255));
  354.                     }
  355.                     break;
  356.                 }
  357.             }
  358.  
  359.             exploder.count -= 1;
  360.         }
  361.     }
  362.  
  363.     public static final class ExplodingShip {
  364.  
  365.         public ShipAPI ship;
  366.         public int count;
  367.         public float interval;
  368.  
  369.         public float ticker;
  370.  
  371.         public List<FlamePoint> flamePoints;
  372.  
  373.         public ExplodingShip(ShipAPI ship, int count, float interval) {
  374.             this.ship = ship;
  375.             this.count = count;
  376.             this.interval = interval;
  377.             this.flamePoints = new ArrayList<>();
  378.             ticker = 0f;
  379.         }
  380.  
  381.         public boolean tick(float amount) {
  382.             ticker += amount;
  383.             if (ticker >= interval) {
  384.                 ticker -= interval;
  385.                 return true;
  386.             } else {
  387.                 return false;
  388.             }
  389.         }
  390.     }
  391.  
  392.     public static final class FlamePoint {
  393.  
  394.         public AnchoredEntity anchor;
  395.         public float time;
  396.         public float scale;
  397.  
  398.         public float ticker;
  399.  
  400.         public FlamePoint(AnchoredEntity anchor, float time, float scale) {
  401.             this.anchor = anchor;
  402.             this.time = time;
  403.             this.scale = scale;
  404.             ticker = 0f;
  405.         }
  406.  
  407.         public boolean tick(float amount) {
  408.             ticker += amount;
  409.             if (ticker >= time) {
  410.                 ticker -= time;
  411.                 return true;
  412.             } else {
  413.                 return false;
  414.             }
  415.         }
  416.     }
  417.  
  418. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement