Advertisement
DarkRevenant

Untitled

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