Advertisement
Originem

TrailManagerVersionOriginem

Jan 1st, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.52 KB | None | 0 0
  1. //Improved By Originem, Original By Nicke535, handles the trails for various projectiles in the mod
  2. package data.scripts.plugins;
  3.  
  4. import com.fs.starfarer.api.Global;
  5. import com.fs.starfarer.api.combat.BaseEveryFrameCombatPlugin;
  6. import com.fs.starfarer.api.combat.CombatEngineAPI;
  7. import com.fs.starfarer.api.combat.DamagingProjectileAPI;
  8. import com.fs.starfarer.api.graphics.SpriteAPI;
  9. import com.fs.starfarer.api.input.InputEventAPI;
  10. import com.fs.starfarer.api.loading.ProjectileSpawnType;
  11. import com.fs.starfarer.api.util.IntervalUtil;
  12. import org.lazywizard.lazylib.MathUtils;
  13. import org.lazywizard.lazylib.VectorUtils;
  14. import org.lwjgl.util.vector.Vector2f;
  15.  
  16. import java.awt.*;
  17. import java.util.*;
  18. import java.util.List;
  19. import java.util.Map.Entry;
  20.  
  21. import static org.lwjgl.opengl.GL11.*;
  22.  
  23. public class trails_OptimizedProjectileTrailHandlerPlugin extends BaseEveryFrameCombatPlugin {
  24.     /**
  25.      * To specify trail's features.
  26.      */
  27.     private static class TrailData {
  28.         /**
  29.          * sprites must be under TSW_fx
  30.          */
  31.         String spriteID = "trail_fuzzy";
  32.         float durationIn = 0f;
  33.         float durationMain = 0f;
  34.         float durationOut = 0f;
  35.         float startSize = 10f;
  36.         float endSize = 10f;
  37.         Color startColor = Color.white;
  38.         Color endColor = Color.white;
  39.         float opacity = 1f;
  40.         int blendSrc = GL_SRC_ALPHA;
  41.         int blendDest = GL_ONE;
  42.         float loopLength = -1f;
  43.         float scrollSpeed = 0f;
  44.         float spawnOffset = 0f;
  45.         /**
  46.          * NEW: compensates for lateral movement of a projectile. Should generally be 0f in most cases, due to some oddities
  47.          * in behaviour with direction-changing scripts, but can be helpful for aligning certain projectiles
  48.          */
  49.         float lateralCompensationMult = 1f;
  50.         /**
  51.          * NEW: whether a shot's trail loses opacity as the projectile fades out. Should generally be true, but may need to
  52.          * be set to false on some scripted weapons. Has no real effect on flak rounds or missiles, and should thus be set
  53.          * false for those
  54.          */
  55.         boolean fadeOutFadeTrail = true;
  56.         /**
  57.          * NEW: whether a shot should have its direction adjusted to face the same way as its velocity vector, thus
  58.          * helping with trail alignment for projectiles without using lateral compensation. DOES NOT WORK FOR
  59.          * PROJECTILES SPAWNED WITH BALLISTIC_AS_BEAM AS SPAWNTYPE, and should not be used on missiles
  60.          */
  61.         boolean projectileAngleAdjustment = false;
  62.  
  63.         TrailData() {
  64.  
  65.         }
  66.  
  67.         /**
  68.          * Save the data to the TRAIL_DATAS in this plugin.
  69.          *
  70.          * @param projectileID projectile's ID
  71.          */
  72.         private void save(String projectileID) {
  73.             TRAIL_DATAS.put(projectileID, this);
  74.         }
  75.     }
  76.  
  77.     //Auto-generate data by maps above,based on TRAIL_SPRITES
  78.     private static final Map<String, TrailData> TRAIL_DATAS = new HashMap<>();
  79.  
  80.     static {
  81.         TrailData data;
  82.  
  83.         data = new TrailData();
  84.         data.spriteID = "trail_fuzzy";
  85.         data.durationIn = 0f;
  86.         data.durationMain = 0f;
  87.         data.durationOut = 0.3f;
  88.         data.startSize = 10f;
  89.         data.endSize = 5f;
  90.         data.startColor = new Color(255, 160, 30);
  91.         data.endColor = new Color(204, 185, 155);
  92.         data.opacity = 0.8f;
  93.         data.blendSrc = GL_SRC_ALPHA;
  94.         data.blendDest = GL_ONE;
  95.         data.loopLength = 300f;
  96.         data.scrollSpeed = 500f;
  97.         data.spawnOffset = 0f;
  98.         data.lateralCompensationMult = 1f;
  99.         data.fadeOutFadeTrail = true;
  100.         data.projectileAngleAdjustment = false;
  101.         data.save("hellbore_shot");
  102.  
  103.         data = new TrailData();
  104.         data.spriteID = "trail_zappy";
  105.         data.durationIn = 0f;
  106.         data.durationMain = 0f;
  107.         data.durationOut = 0.8f;
  108.         data.startSize = 7f;
  109.         data.endSize = 3f;
  110.         data.startColor = new Color(120, 120, 255);
  111.         data.endColor = new Color(100, 100, 25);
  112.         data.opacity = 0.8f;
  113.         data.blendSrc = GL_SRC_ALPHA;
  114.         data.blendDest = GL_ONE;
  115.         data.loopLength = -1f;
  116.         data.scrollSpeed = 0f;
  117.         data.spawnOffset = 30f;
  118.         data.lateralCompensationMult = 1f;
  119.         data.fadeOutFadeTrail = true;
  120.         data.projectileAngleAdjustment = false;
  121.         data.save("gauss_shot");
  122.  
  123.         data = new TrailData();
  124.         data.spriteID = "trail_zappy";
  125.         data.durationIn = 0f;
  126.         data.durationMain = 0f;
  127.         data.durationOut = 0.4f;
  128.         data.startSize = 12f;
  129.         data.endSize = 5f;
  130.         data.startColor = new Color(150, 100, 255);
  131.         data.endColor = new Color(150, 100, 255);
  132.         data.opacity = 0.6f;
  133.         data.blendSrc = GL_SRC_ALPHA;
  134.         data.blendDest = GL_ONE;
  135.         data.loopLength = -1f;
  136.         data.scrollSpeed = 0f;
  137.         data.spawnOffset = 2f;
  138.         data.lateralCompensationMult = 1f;
  139.         data.fadeOutFadeTrail = true;
  140.         data.projectileAngleAdjustment = false;
  141.         data.save("amblaster_shot");
  142.  
  143.         data = new TrailData();
  144.         data.spriteID = "trail_fuzzy";
  145.         data.durationIn = 0f;
  146.         data.durationMain = 0.1f;
  147.         data.durationOut = 0.5f;
  148.         data.startSize = 7f;
  149.         data.endSize = 1f;
  150.         data.startColor = new Color(255, 255, 215);
  151.         data.endColor = new Color(255, 255, 245);
  152.         data.opacity = 0.5f;
  153.         data.blendSrc = GL_SRC_ALPHA;
  154.         data.blendDest = GL_ONE_MINUS_SRC_ALPHA;
  155.         data.loopLength = -1f;
  156.         data.scrollSpeed = 0f;
  157.         data.spawnOffset = 20f;
  158.         data.lateralCompensationMult = 1f;
  159.         data.fadeOutFadeTrail = true;
  160.         data.projectileAngleAdjustment = false;
  161.         data.save("hveldriver_shot");
  162.  
  163.         data = new TrailData();
  164.         data.spriteID = "trail_zappy";
  165.         data.durationIn = 0.05f;
  166.         data.durationMain = 0.05f;
  167.         data.durationOut = 0.2f;
  168.         data.startSize = 8f;
  169.         data.endSize = 25f;
  170.         data.startColor = new Color(255, 170, 60);
  171.         data.endColor = new Color(60, 30, 20);
  172.         data.opacity = 0.4f;
  173.         data.blendSrc = GL_SRC_ALPHA;
  174.         data.blendDest = GL_ONE;
  175.         data.loopLength = 400f;
  176.         data.scrollSpeed = 1000f;
  177.         data.spawnOffset = 30f;
  178.         data.lateralCompensationMult = 1f;
  179.         data.fadeOutFadeTrail = true;
  180.         data.projectileAngleAdjustment = false;
  181.         data.save("heavymauler_shot");
  182.  
  183.         data = new TrailData();
  184.         data.spriteID = "trail_zappy";
  185.         data.durationIn = 0f;
  186.         data.durationMain = 0f;
  187.         data.durationOut = 0.2f;
  188.         data.startSize = 16f;
  189.         data.endSize = 8f;
  190.         data.startColor = new Color(95, 75, 255);
  191.         data.endColor = new Color(125, 95, 255);
  192.         data.opacity = 0.6f;
  193.         data.blendSrc = GL_SRC_ALPHA;
  194.         data.blendDest = GL_ONE;
  195.         data.loopLength = 200f;
  196.         data.scrollSpeed = 1000f;
  197.         data.spawnOffset = 0f;
  198.         data.lateralCompensationMult = 1f;
  199.         data.fadeOutFadeTrail = true;
  200.         data.projectileAngleAdjustment = false;
  201.         data.save("mjolnir_shot");
  202.     }
  203.  
  204.     // Map to record trail ids
  205.     private Map<DamagingProjectileAPI, TrailIDC> projectileTrailIDCs = new WeakHashMap<>();
  206.     // Timer to clean projectiles
  207.     private IntervalUtil trailDataCleanerTimer = new IntervalUtil(3f, 3f);
  208.  
  209.     @Override
  210.     public void init(CombatEngineAPI engine) {
  211.         //Reinitialize the lists
  212.         projectileTrailIDCs.clear();
  213.     }
  214.  
  215.     @Override
  216.     public void advance(float amount, List<InputEventAPI> events) {
  217.         if (Global.getCombatEngine() == null || Global.getCombatEngine().isPaused()) {
  218.             return;
  219.         }
  220.         CombatEngineAPI engine = Global.getCombatEngine();
  221.  
  222.         //clean the projectile references per 3 seconds
  223.         trailDataCleanerTimer.advance(amount);
  224.         if (trailDataCleanerTimer.intervalElapsed()) {
  225.             Iterator<Entry<DamagingProjectileAPI, TrailIDC>> iterator = projectileTrailIDCs.entrySet().iterator();
  226.             while (iterator.hasNext()) {
  227.                 Entry<DamagingProjectileAPI, TrailIDC> entry = iterator.next();
  228.                 DamagingProjectileAPI projectile = entry.getKey();
  229.                 if (projectile == null || !engine.isEntityInPlay(projectile)) iterator.remove();
  230.             }
  231.         }
  232.  
  233.         //Runs once on each projectile that matches one of the IDs specified in our maps
  234.         for (DamagingProjectileAPI proj : engine.getProjectiles()) {
  235.             //Ignore already-collided projectiles, and projectiles that don't match our IDs
  236.             if (proj.getProjectileSpecId() == null || proj.didDamage()) {
  237.                 continue;
  238.             }
  239.  
  240.             if (!TRAIL_DATAS.keySet().contains(proj.getProjectileSpecId())) {
  241.                 continue;
  242.             }
  243.  
  244.             //-------------------------------------------For visual effects---------------------------------------------
  245.             String specID = proj.getProjectileSpecId();
  246.             TrailData trailData = TRAIL_DATAS.get(specID);
  247.             SpriteAPI spriteToUse = Global.getSettings().getSprite("trails_fx", trailData.spriteID);
  248.             Vector2f projVel = new Vector2f(proj.getVelocity());
  249.  
  250.             //If we use angle adjustment, do that here
  251.             if (trailData.projectileAngleAdjustment && projVel.length() > 0.1f && !proj.getSpawnType().equals(ProjectileSpawnType.BALLISTIC_AS_BEAM)) {
  252.                 proj.setFacing(VectorUtils.getAngle(new Vector2f(0f, 0f), projVel));
  253.             }
  254.  
  255.  
  256.             //Fix for some first-frame error shenanigans
  257.             if (projVel.length() < 0.1f && proj.getSource() != null) {
  258.                 projVel = new Vector2f(proj.getSource().getVelocity());
  259.             }
  260.  
  261.             //Gets a custom "offset" position, so we can slightly alter the spawn location to account for "natural fade-in", and add that to our spawn position
  262.             Vector2f offsetPoint = new Vector2f((float) Math.cos(Math.toRadians(proj.getFacing())) * trailData.spawnOffset, (float) Math.sin(Math.toRadians(proj.getFacing())) * trailData.spawnOffset);
  263.             Vector2f spawnPosition = new Vector2f(offsetPoint.x + proj.getLocation().x, offsetPoint.y + proj.getLocation().y);
  264.  
  265.             //Sideway offset velocity, for projectiles that use it
  266.             Vector2f projBodyVel = VectorUtils.rotate(projVel, -proj.getFacing());
  267.             Vector2f projLateralBodyVel = new Vector2f(0f, projBodyVel.getY());
  268.             Vector2f sidewayVel = (Vector2f) VectorUtils.rotate(projLateralBodyVel, proj.getFacing()).scale(trailData.lateralCompensationMult);
  269.  
  270.             //Opacity adjustment for fade-out, if the projectile uses it
  271.             float opacityMult = 1f;
  272.             if (trailData.fadeOutFadeTrail && proj.isFading()) {
  273.                 opacityMult = proj.getDamageAmount() / proj.getBaseDamageAmount();
  274.             }
  275.  
  276.             TrailIDC trailIDC = projectileTrailIDCs.get(proj);
  277.  
  278.             if (specID.contains("gauss_shot")) {
  279.                 if (trailIDC == null) {
  280.                     trailIDC = new TrailIDC(true, true, false);
  281.                     projectileTrailIDCs.put(proj, trailIDC);
  282.                 }
  283.                 MagicTrailPlugin.AddTrailMemberAdvanced(proj, trailIDC.mainId, spriteToUse, spawnPosition, 0f, MathUtils.getRandomNumberInRange(0f, 105f), proj.getFacing() - 180f,
  284.                         0f, MathUtils.getRandomNumberInRange(-330f, 330f), trailData.startSize, trailData.endSize, trailData.startColor, new Color(180, 190, 255),
  285.                         0.4f * opacityMult, trailData.durationIn, 0.2f, trailData.durationOut + 0.2f, trailData.blendSrc,
  286.                         trailData.blendDest, trailData.loopLength, trailData.scrollSpeed, sidewayVel, null);
  287.  
  288.                 MagicTrailPlugin.AddTrailMemberAdvanced(proj, trailIDC.dualCoreId, spriteToUse, spawnPosition, 0f, MathUtils.getRandomNumberInRange(0f, 105f), proj.getFacing() - 180f,
  289.                         0f, MathUtils.getRandomNumberInRange(-330f, 330f), trailData.startSize, trailData.endSize, trailData.startColor, new Color(180, 190, 255),
  290.                         0.4f * opacityMult, trailData.durationIn, 0.2f, trailData.durationOut + 0.2f, trailData.blendSrc,
  291.                         trailData.blendDest, trailData.loopLength, trailData.scrollSpeed, sidewayVel, null);
  292.  
  293.                 MagicTrailPlugin.AddTrailMemberAdvanced(proj, trailIDC.equityId, Global.getSettings().getSprite("trails_fx", "trail_smooth"), spawnPosition, 0f, 0f, proj.getFacing() - 180f,
  294.                         0f, 0f, 60f, 30f, trailData.startColor, trailData.startColor,
  295.                         0.5f * opacityMult, 0f, 0.05f, 0.15f, trailData.blendSrc,
  296.                         trailData.blendDest, trailData.loopLength, 0f, sidewayVel, null);
  297.             } else {
  298.                 if (trailIDC == null) {
  299.                     trailIDC = new TrailIDC();
  300.                     projectileTrailIDCs.put(proj, trailIDC);
  301.                 }
  302.                 //Then, actually spawn a trail
  303.                 MagicTrailPlugin.AddTrailMemberAdvanced(proj, trailIDC.mainId, spriteToUse, spawnPosition, 0f, 0f, proj.getFacing() - 180f,
  304.                         0f, 0f, trailData.startSize, trailData.endSize, trailData.startColor, trailData.endColor,
  305.                         trailData.opacity * opacityMult, trailData.durationIn, trailData.durationMain, trailData.durationOut, trailData.blendSrc,
  306.                         trailData.blendDest, trailData.loopLength, trailData.scrollSpeed, sidewayVel, null);
  307.  
  308.  
  309.                 //adding secondary trail for hellbore
  310.                 if (specID.contains("hellbore_shot")) {
  311.                     //If we haven't already started a second trail for this projectile, set the extra id for it
  312.                     if (!trailIDC.isInit) {
  313.                         trailIDC.setExtraIDs(true, false, false);
  314.                     }
  315.                     MagicTrailPlugin.AddTrailMemberAdvanced(proj, trailIDC.dualCoreId, Global.getSettings().getSprite("trails_fx", "trail_contrail"), spawnPosition, 0f, 0f, proj.getFacing() - 180f,
  316.                             0f, 0f, 25f, 35f, new Color(100, 90, 80), new Color(100, 90, 80),
  317.                             0.4f * opacityMult, trailData.durationIn, 0f, 0.8f, trailData.blendSrc,
  318.                             trailData.blendDest, trailData.loopLength, 800f, sidewayVel, null);
  319.                 }
  320.  
  321.  
  322.                 if (specID.contains("mjolnir_shot")) {
  323.                     if (!trailIDC.isInit) {
  324.                         trailIDC.setExtraIDs(true, false, false);
  325.                     }
  326.                     MagicTrailPlugin.AddTrailMemberAdvanced(proj, trailIDC.dualCoreId, spriteToUse, spawnPosition, 0f, 0f, proj.getFacing() - 180f,
  327.                             0f, 0f, 5f, 5f, new Color(200, 200, 200), new Color(200, 200, 200),
  328.                             0.3f * opacityMult, trailData.durationIn, trailData.durationMain, 0.3f, trailData.blendSrc,
  329.                             trailData.blendDest, trailData.loopLength, trailData.scrollSpeed, sidewayVel, null);
  330.  
  331.                 }
  332.  
  333.                 if (specID.contains("amblaster_shot")) {
  334.                     if (!trailIDC.isInit) {
  335.                         trailIDC.setExtraIDs(true, true, false);
  336.                     }
  337.                     MagicTrailPlugin.AddTrailMemberAdvanced(proj, trailIDC.dualCoreId, spriteToUse, spawnPosition, MathUtils.getRandomNumberInRange(0f, 200f), MathUtils.getRandomNumberInRange(0f, 500f), proj.getFacing() - 180f,
  338.                             MathUtils.getRandomNumberInRange(-200f, 200f), MathUtils.getRandomNumberInRange(-500f, 500f), trailData.startSize, trailData.endSize, trailData.startColor, trailData.endColor,
  339.                             trailData.opacity * opacityMult, trailData.durationIn, trailData.durationMain, 0.2f, trailData.blendSrc,
  340.                             trailData.blendDest, trailData.loopLength, trailData.scrollSpeed, sidewayVel, null);
  341.  
  342.                     MagicTrailPlugin.AddTrailMemberAdvanced(proj, trailIDC.equityId, spriteToUse, spawnPosition, MathUtils.getRandomNumberInRange(0f, 200f), MathUtils.getRandomNumberInRange(0f, 500f), proj.getFacing() - 180f,
  343.                             MathUtils.getRandomNumberInRange(-200f, 200f), MathUtils.getRandomNumberInRange(-500f, 500f), trailData.startSize, trailData.endSize, trailData.startColor, trailData.endColor,
  344.                             trailData.opacity * opacityMult, trailData.durationIn, trailData.durationMain, 0.2f, trailData.blendSrc,
  345.                             trailData.blendDest, trailData.loopLength, trailData.scrollSpeed, sidewayVel, null);
  346.  
  347.                 }
  348.  
  349.                 if (specID.contains("heavymauler_shot")) {
  350.                     //If we haven't already started a second trail for this projectile, set the extra id for it
  351.                     if (!trailIDC.isInit) {
  352.                         trailIDC.setExtraIDs(true, false, false);
  353.                     }
  354.                     MagicTrailPlugin.AddTrailMemberAdvanced(proj, trailIDC.dualCoreId, Global.getSettings().getSprite("trails_fx", "trail_smooth"), spawnPosition, 0f, 0f, proj.getFacing() - 180f,
  355.                             0f, 0f, 60f, 60f, new Color(255, 140, 60), new Color(255, 140, 60),
  356.                             0.3f * opacityMult, 0f, 0f, 0.15f, trailData.blendSrc,
  357.                             trailData.blendDest, trailData.loopLength, 0f, sidewayVel, null);
  358.                 }
  359.             }
  360.         }
  361.     }
  362.  
  363.     //A class which deal with the MagicTrailIDs
  364.     private class TrailIDC {
  365.         //A map for known projectiles and their IDs: should be cleared in init
  366.         float mainId;
  367.         //Used when doing dual-core sprites
  368.         float dualCoreId;
  369.         //Used for the Equity
  370.         float equityId;
  371.         //Used for glow gauss
  372.         float glowId;
  373.         boolean isInit = false;
  374.  
  375.         public TrailIDC(boolean initDualCore, boolean initEquityId, boolean initGlowId) {
  376.             this.mainId = MagicTrailPlugin.getUniqueID();
  377.             setExtraIDs(initDualCore, initEquityId, initGlowId);
  378.         }
  379.  
  380.         public TrailIDC() {
  381.             this.mainId = MagicTrailPlugin.getUniqueID();
  382.         }
  383.  
  384.         public void setExtraIDs(boolean initDualCore, boolean initEquityId, boolean initGlowId) {
  385.             if (initDualCore) dualCoreId = MagicTrailPlugin.getUniqueID();
  386.             if (initEquityId) equityId = MagicTrailPlugin.getUniqueID();
  387.             if (initGlowId) glowId = MagicTrailPlugin.getUniqueID();
  388.             this.isInit = true;
  389.         }
  390.     }
  391. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement