001package com.fs.starfarer.api.impl.combat.threat;
002
003import org.lwjgl.util.vector.Vector2f;
004
005import com.fs.starfarer.api.Global;
006import com.fs.starfarer.api.combat.CombatEngineAPI;
007import com.fs.starfarer.api.combat.CombatFleetManagerAPI;
008import com.fs.starfarer.api.combat.ShipAPI;
009import com.fs.starfarer.api.combat.ShipCommand;
010import com.fs.starfarer.api.combat.ShipSystemAIScript;
011import com.fs.starfarer.api.combat.ShipSystemAPI;
012import com.fs.starfarer.api.combat.ShipwideAIFlags;
013import com.fs.starfarer.api.util.IntervalUtil;
014
015
016public class ConstructionSwarmSystemAI implements ShipSystemAIScript {
017
018        public static float REQUIRED_DP_FOR_NORMAL_USE = 35;
019        
020        protected ShipAPI ship;
021        protected CombatEngineAPI engine;
022        protected ShipwideAIFlags flags;
023        protected ShipSystemAPI system;
024        //protected ConstructionSwarmSystemScript script;
025        
026        protected IntervalUtil tracker = new IntervalUtil(0.5f, 1f);
027        
028        protected float keepUsingFor = 0f;
029        protected float timeSpentAtHighFragmentLevel = 0f;
030        
031        public void init(ShipAPI ship, ShipSystemAPI system, ShipwideAIFlags flags, CombatEngineAPI engine) {
032                this.ship = ship;
033                this.flags = flags;
034                this.engine = engine;
035                this.system = system;
036                
037                // can't actual use this here this way due to class load order dependency making the game crash on startup
038                //script = (ConstructionSwarmSystemScript)system.getScript();
039        }
040        
041        public void advance(float amount, Vector2f missileDangerDir, Vector2f collisionDangerDir, ShipAPI target) {
042                tracker.advance(amount);
043                
044                keepUsingFor -= amount;
045                
046                if (tracker.intervalElapsed()) {
047                        if (system.getCooldownRemaining() > 0) return;
048                        if (system.isOutOfAmmo()) return;
049                        if (system.isActive()) return;
050                        
051                        
052                        ConstructionSwarmSystemScript script = (ConstructionSwarmSystemScript)system.getScript();
053                        if (!script.isUsable(system, ship)) {
054                                return;
055                        }
056                        
057                        CombatFleetManagerAPI manager = Global.getCombatEngine().getFleetManager(ship.getOriginalOwner());
058                        int dpLeft = 0;
059                        if (manager != null) {
060                                dpLeft = manager.getMaxStrength() - manager.getCurrStrength();
061                        }
062                        
063                        float cr = ship.getCurrentCR();
064                        float softFlux = ship.getFluxLevel();
065                        float hardFlux = ship.getHardFluxLevel();
066                        
067                        if (cr < 0.2f + hardFlux * 0.2f) {
068                                return;
069                        }
070
071                        RoilingSwarmEffect swarm = RoilingSwarmEffect.getSwarmFor(ship);
072                        if (swarm == null) return;
073
074                        int active = swarm.getNumActiveMembers();
075                        int max = swarm.getNumMembersToMaintain();
076
077                        if (active >= max * 0.9f) {
078                                timeSpentAtHighFragmentLevel += tracker.getIntervalDuration();
079                        } else {
080                                timeSpentAtHighFragmentLevel = 0f;
081                        }
082                        
083                
084                        
085                        if ((active >= max || timeSpentAtHighFragmentLevel >= 10f) && dpLeft >= REQUIRED_DP_FOR_NORMAL_USE) {
086                                keepUsingFor = 3f + (float) Math.random() * 2f;
087                        }
088                        
089                        if (keepUsingFor <= 0f && (hardFlux > 0.5f || softFlux > 0.9f)) {
090                                keepUsingFor = 0.5f;
091                        }
092
093                        if (keepUsingFor > 0f) {
094                                ship.giveCommand(ShipCommand.USE_SYSTEM, null, 0);
095                        }
096                }
097        }
098
099}
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121