Advertisement
DarkRevenant

Untitled

Apr 14th, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.02 KB | None | 0 0
  1. /*Original scripting framework by Debido
  2.  Ship revival code originally ship boarding code by Xenoargh
  3.  Designed for use with the BGE mod by Tecrys
  4.  */
  5. package bge.data.scripts.plugins;
  6.  
  7. import com.fs.starfarer.api.Global;
  8. import com.fs.starfarer.api.campaign.FleetDataAPI;
  9. import com.fs.starfarer.api.combat.CombatEngineAPI;
  10. import com.fs.starfarer.api.combat.DamageType;
  11. import com.fs.starfarer.api.combat.EveryFrameCombatPlugin;
  12. import com.fs.starfarer.api.combat.ShipAPI;
  13. import com.fs.starfarer.api.fleet.FleetMemberAPI;
  14. import com.fs.starfarer.api.fleet.FleetMemberType;
  15. import com.fs.starfarer.api.mission.FleetSide;
  16. import com.fs.starfarer.api.util.IntervalUtil;
  17. import java.awt.Color;
  18. import java.util.*;
  19. import org.lazywizard.lazylib.MathUtils;
  20. import org.lwjgl.util.vector.Vector2f;
  21.  
  22. public class BGENecroMorphosisMonitor implements EveryFrameCombatPlugin {
  23.  
  24.     // Stores the currently monitored juiceTokens
  25.     private final List<ShipRequestData> revivalList = new ArrayList();
  26.     private final List<ShipAPI> deadShips = new ArrayList();
  27.     private static final float REVIVE_CHANCE = 0.90f;
  28.     private final Random myRandom = new Random();
  29.     private CombatEngineAPI engine; //required
  30.     private static final float INTERVAL_FRACTION = 5f;
  31.     private final IntervalUtil tracker = new IntervalUtil(INTERVAL_FRACTION, INTERVAL_FRACTION);
  32.  
  33.     @Override
  34.     public void advance(float amount, List events) {
  35.  
  36.         if (engine != Global.getCombatEngine()) {
  37.             deadShips.clear();
  38.             revivalList.clear();
  39.             this.engine = Global.getCombatEngine();
  40.             return;
  41.         }
  42.  
  43.         if (engine.isPaused()) {
  44.             return;
  45.         }
  46.  
  47.         tracker.advance(amount);
  48.         if (tracker.intervalElapsed()) {
  49.             List<ShipAPI> ships = engine.getShips();
  50.  
  51.             // We run through all the ships and check for newly-destroyed ships, adding them to the revival list
  52.             for (ShipAPI ship : ships) {
  53.                 if (ship == null) {
  54.                     continue;
  55.                 }
  56.  
  57.                 if (ship.isHulk() == true) {
  58.                     if (deadShips.contains(ship) == false && ship.getVariant().getHullMods().contains("BGENecroMorphosis")) {
  59.                         deadShips.add(ship);
  60.                         revivalList.add(new ShipRequestData(ship));
  61.                     }
  62.                 }
  63.             }
  64.  
  65.             for (ShipRequestData shipData : revivalList) {
  66.                 if (shipData.isRevived || shipData.chargeCounter != 0) {
  67.                     myRandom.setSeed(System.currentTimeMillis()); //generate new seed
  68.                     float chance = myRandom.nextFloat(); //get the next float chance
  69.                     if (chance <= REVIVE_CHANCE) { //if the float chance is less than our revival chance, revive them
  70.                         shipData.isRevived = true;
  71.                         ShipAPI reviveTarget = shipData.ship;
  72.                         int owner = reviveTarget.getOwner(); //debug stuff
  73.                         //int playerOwner = Global.getCombatEngine().getPlayerShip().getOwner(); //debug stuff
  74.                         ShipAPI randShip = engine.getShips().get(0);
  75.  
  76.                         if (owner == 0 || owner == 100) {
  77.                             Vector2f loc = reviveTarget.getLocation();
  78.                             float facing = reviveTarget.getFacing();
  79.  
  80.                             String variantID = shipData.ship.getVariant().getHullVariantId();
  81.                             //Safety code here
  82.                             //ShipHullSpecAPI shipHull = reviveTarget.getHullSpec();
  83.                             //String hullName = shipHull.getHullId();
  84.                             String shipName = reviveTarget.getVariant().getDesignation();//Get the ship name to prevent duplicates
  85.                             //shipName = shipName + ship.getVariant().getHullSpec().getHullName();
  86.  
  87.                             //Give our fleet the captured ship.
  88.                             ShipAPI spawned = engine.getFleetManager(FleetSide.PLAYER).spawnShipOrWing(variantID, loc, facing);
  89.                             if (spawned == null) {
  90.                                 return;
  91.                             }
  92.                             spawned.setCurrentCR(MathUtils.getRandomNumberInRange(0.25f, 0.5f));
  93.  
  94.                             //This code just moves the captured enemy ship a long way off the map and blows it up.
  95.                             //We ~must~ blow it up; using remove() results in an un-winnable battle state, because it doesn't count as "dead".
  96.                             loc.x += 100000f;
  97.                             loc.y += 100000f;
  98.                             engine.spawnEmpArc(randShip, loc, reviveTarget, reviveTarget,
  99.                                     DamageType.ENERGY,
  100.                                     10000000000f,
  101.                                     0f, // emp
  102.                                     10000000f, // max range
  103.                                     "tachyon_lance_emp_impact",
  104.                                     30f, // thickness
  105.                                     new Color(255, 100, 0, 255),
  106.                                     new Color(255, 200, 0, 255)
  107.                             );
  108.  
  109.                             //Here we're checking to make sure we're in a Campaign mission context; if not, halt!
  110.                             if (Global.getSector().getPlayerFleet() != null) {
  111.                                 //If in campaign, we need to keep the ship we've captured after the battle, because ships spawned during the battle aren't...
  112.                                 //...being carried over to the cargo at the end of the battle.
  113.                                 //This also means that if the player loses the captured ship during the battle, they'll keep it anyway.
  114.                                 //Fixing that would probably require some list of captured ships and a post-battle state check...
  115.                                 //...or a cyclic script that would remove them if they become Hulks during the battle.
  116.                                 //For now, this suffices, even if it's not totally polished.
  117.                                 FleetDataAPI fleet = Global.getSector().getPlayerFleet().getFleetData();
  118.                                 if (fleet != null) {
  119.  
  120.                                     //boolean isPlayerShip = false;
  121.                                     boolean foundClone = false;
  122.  
  123.                                     List<FleetMemberAPI> members = fleet.getMembersListCopy();
  124.                                     for (FleetMemberAPI thisMember : members) {
  125.                                         String thisMemberName = thisMember.getShipName();
  126.  
  127.                                         if (thisMemberName == null) {
  128.                                             continue;
  129.                                         }
  130.                                         if (shipName == null) {
  131.                                             continue;
  132.                                         }
  133.                                         if (thisMemberName.contains(shipName)) {
  134.                                             foundClone = true;
  135.                                             //Sets the name back to the same name
  136.                                             thisMember.setShipName(shipName);
  137.                                         }
  138.                                     }
  139.                                     if (foundClone == false) {
  140.                                         FleetMemberAPI newShip = Global.getFactory().createFleetMember(FleetMemberType.SHIP, variantID);
  141.                                         fleet.addFleetMember(newShip);
  142.                                         newShip.setShipName(shipName);
  143.                                     }
  144.                                 }
  145.                             }
  146.                         } else {
  147.                             Vector2f loc = reviveTarget.getLocation();
  148.                             float facing = reviveTarget.getFacing();
  149.                             String variantID; //Safety code here
  150.                             //ShipHullSpecAPI shipHull = reviveTarget.getHullSpec();
  151.                             //String hullName = shipHull.getHullId();
  152.  
  153.                             variantID = shipData.ship.getVariant().getHullVariantId();
  154.  
  155.                             //Give the enemy fleet the captured ship.
  156.                             engine.getFleetManager(FleetSide.ENEMY).spawnShipOrWing(variantID, loc, facing);
  157.  
  158.                             //This code just moves the captured enemy ship a long way off the map and blows it up.
  159.                             //We ~must~ blow it up; using remove() results in an un-winnable battle state, because it doesn't count as "dead".
  160.                             loc.x += 100000f;
  161.                             loc.y += 100000f;
  162.                             engine.spawnEmpArc(randShip, loc, reviveTarget, reviveTarget,
  163.                                     DamageType.ENERGY,
  164.                                     10000000000f,
  165.                                     0f, // emp
  166.                                     100000f, // max range
  167.                                     "tachyon_lance_emp_impact",
  168.                                     30f, // thickness
  169.                                     new Color(255, 100, 0, 255),
  170.                                     new Color(255, 200, 0, 255)
  171.                             );
  172.  
  173.                         }
  174.                     } //IF REVIVAL CHANCE MORE THAN 0.9f
  175.                     else {
  176.                         shipData.chargeCounter--;
  177.                     } //ELSE
  178.                 } //IF NOT REVIVED
  179.             }
  180.         }
  181.     }
  182.  
  183.     @Override
  184.     public void init(CombatEngineAPI engine) {
  185.     }
  186.  
  187.     public final class ShipRequestData {
  188.  
  189.         public ShipAPI ship;
  190.         public boolean isRevived = false;
  191.         public int chargeCounter = 4;
  192.  
  193.         public ShipRequestData(ShipAPI reviveTarget) {
  194.             this.ship = reviveTarget;
  195.         }
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement