001package com.fs.starfarer.api.impl.campaign.skills;
002
003import java.util.HashSet;
004import java.util.List;
005import java.util.Set;
006
007import com.fs.starfarer.api.Global;
008import com.fs.starfarer.api.characters.PersonAPI;
009import com.fs.starfarer.api.combat.BaseEveryFrameCombatPlugin;
010import com.fs.starfarer.api.combat.BattleObjectiveAPI;
011import com.fs.starfarer.api.combat.CombatEngineAPI;
012import com.fs.starfarer.api.combat.CombatFleetManagerAPI;
013import com.fs.starfarer.api.combat.DeployedFleetMemberAPI;
014import com.fs.starfarer.api.combat.ShipAPI;
015import com.fs.starfarer.api.combat.ViewportAPI;
016import com.fs.starfarer.api.impl.campaign.ids.BattleObjectives;
017import com.fs.starfarer.api.impl.campaign.ids.Stats;
018import com.fs.starfarer.api.input.InputEventAPI;
019
020public class CommRelayScript extends BaseEveryFrameCombatPlugin {
021        public static final float RATE_BONUS_PER_COMM_RELAY = 25f;
022        public static final Object KEY_STATUS = new Object();
023        public static final String BONUS_ID = "comm_relay_script_bonus";
024        
025        
026        private CombatEngineAPI engine;
027        public void init(CombatEngineAPI engine) {
028                this.engine = engine;
029        }
030        
031        private ShipAPI prevPlayerShip = null;
032        private int skipFrames = 0;
033        private Set<CombatFleetManagerAPI> needsCleanup = new HashSet<CombatFleetManagerAPI>();
034        public void advance(float amount, List<InputEventAPI> events) {
035                if (engine == null) return;
036                if (engine.isPaused()) return;
037                
038                // if the player changed flagships:
039                // skip a few frames to make sure the status ends up on top of the status list
040                ShipAPI playerShip = engine.getPlayerShip();
041                if (playerShip != prevPlayerShip) {
042                        prevPlayerShip = playerShip;
043                        skipFrames = 20;
044                }
045                
046                if (skipFrames > 0) {
047                        skipFrames--;
048                        return;
049                }
050                
051                updateForSide(engine.getFleetManager(0));
052                updateForSide(engine.getFleetManager(1));
053        }
054        
055        
056        private void updateForSide(CombatFleetManagerAPI manager) {
057
058                PersonAPI commander = manager.getFleetCommander();
059                if (commander == null) {
060                        manager.getTaskManager(false).getCPRateModifier().unmodify(BONUS_ID);
061                        return;
062                }
063                
064                
065                float total = 0f;
066                
067                float modifier = commander.getStats().getDynamic().getValue(Stats.COMMAND_POINT_RATE_COMMANDER);
068                boolean relaysOnly = modifier == 1f;
069                List<DeployedFleetMemberAPI> deployed = manager.getDeployedCopyDFM();
070                for (DeployedFleetMemberAPI member : deployed) {
071                        if (member.isFighterWing()) continue;
072                        float curr = member.getShip().getMutableStats().getDynamic().getValue(Stats.COMMAND_POINT_RATE_FLAT, 0f);
073                        total += curr;
074                }
075                
076                if (total > 0) relaysOnly = false;
077                
078                
079                int numRelays = 0;
080                for (BattleObjectiveAPI obj : engine.getObjectives()) {
081                        if (obj.getOwner() == manager.getOwner() && BattleObjectives.COMM_RELAY.equals(obj.getType())) {
082                                total += RATE_BONUS_PER_COMM_RELAY / 100f;
083                                numRelays++;
084                        }
085                }
086                
087                
088                manager.getTaskManager(false).getCPRateModifier().modifyFlat(BONUS_ID, total);
089                
090                
091                modifier += manager.getTaskManager(false).getCPRateModifier().getModifiedValue();
092                modifier -= 1f;
093                
094                if (manager.getOwner() == 0) {
095                        // use mult instead of modifier since mult includes skill bonus and modifier is only relays
096                        float withMult = manager.getTaskManager(false).getCPRateMult();
097                        String icon = Global.getSettings().getSpriteName("ui", "icon_tactical_coordinated_maneuvers");
098                        String title = "command network";
099                        //String data = "+" + (int)(modifier * 100f) + "% cp recovery rate";
100                        
101                        String data = "+" + (int)Math.round((withMult - 1f) * 100f) + "% cp recovery rate";
102                        boolean debuff = false;
103                        if (withMult < 1f) {
104                                data = "" + (int)Math.round((withMult - 1f) * 100f) + "% cp recovery rate";
105                                debuff = true;
106                        }
107                        
108//                      if (relaysOnly) {
109//                              title = "comm relay";
110//                              if (numRelays > 1) {
111//                                      title += "s";
112//                                      title += " (" + numRelays + ")";
113//                              }
114//                      }
115                        
116                        if (withMult != 1) {
117                                engine.maintainStatusForPlayerShip(KEY_STATUS, icon,
118                                                        title, 
119                                                        data, debuff);
120                        }
121                }
122                
123        }
124        
125        protected void cleanUpIfNeeded(CombatFleetManagerAPI manager) {
126                if (needsCleanup.contains(manager)) {
127                        needsCleanup.remove(manager);
128                        List<DeployedFleetMemberAPI> deployed = manager.getDeployedCopyDFM();
129                        for (DeployedFleetMemberAPI member : deployed) {
130                                if (member.isFighterWing()) continue;
131                                if (member.getShip() == null) continue;
132                                member.getShip().getMutableStats().getMaxSpeed().unmodify(BONUS_ID);
133                        }
134                }
135        }
136        
137
138        
139
140        public void renderInUICoords(ViewportAPI viewport) {
141        }
142
143        public void renderInWorldCoords(ViewportAPI viewport) {
144        }
145
146
147        public boolean hasInputPriority() {
148                // TODO Auto-generated method stub
149                return false;
150        }
151
152}