001package com.fs.starfarer.api.impl.campaign;
002
003import com.fs.starfarer.api.Global;
004import com.fs.starfarer.api.campaign.InteractionDialogAPI;
005import com.fs.starfarer.api.campaign.SectorEntityToken;
006import com.fs.starfarer.api.campaign.TextPanelAPI;
007import com.fs.starfarer.api.campaign.comm.IntelInfoPlugin;
008import com.fs.starfarer.api.campaign.econ.MarketAPI;
009import com.fs.starfarer.api.impl.campaign.econ.CommRelayCondition;
010import com.fs.starfarer.api.impl.campaign.ids.Conditions;
011import com.fs.starfarer.api.impl.campaign.ids.MemFlags;
012import com.fs.starfarer.api.impl.campaign.ids.Tags;
013import com.fs.starfarer.api.impl.campaign.intel.BaseIntelPlugin;
014import com.fs.starfarer.api.impl.campaign.intel.misc.CommSnifferIntel;
015import com.fs.starfarer.api.ui.TooltipMakerAPI;
016import com.fs.starfarer.api.util.Misc;
017
018public class CommRelayEntityPlugin extends BaseCampaignObjectivePlugin {
019
020        public static interface CommSnifferReadableIntel {
021                boolean canMakeVisibleToCommSniffer(boolean playerInRelayRange, SectorEntityToken relay);
022        }
023        
024        
025        public void init(SectorEntityToken entity, Object pluginParams) {
026                super.init(entity, pluginParams);
027                readResolve();
028        }
029        
030        Object readResolve() {
031                return this;
032        }
033        
034        public void advance(float amount) {
035                if (entity.getContainingLocation() == null || entity.isInHyperspace()) return;
036                
037                if (entity.getMemoryWithoutUpdate().getBoolean(MemFlags.OBJECTIVE_NON_FUNCTIONAL)) return;
038                
039                // everything else is handled by the relay condition - it picks what relay to use and when to remove itself
040                for (MarketAPI market : Misc.getMarketsInLocation(entity.getContainingLocation())) {
041                        CommRelayCondition mc = CommRelayCondition.get(market);
042                        if (mc == null) {
043                                market.addCondition(Conditions.COMM_RELAY);
044                                mc = CommRelayCondition.get(market);
045                        }
046                        if (mc != null) {
047                                mc.getRelays().add(entity);
048                        }
049                }
050                
051                checkIntelFromCommSniffer();
052        }
053
054        
055
056        protected boolean isMakeshift() {
057                return entity.hasTag(Tags.MAKESHIFT);
058        }
059        
060        
061        public void printNonFunctionalAndHackDescription(TextPanelAPI text) {
062                if (entity.getMemoryWithoutUpdate().getBoolean(MemFlags.OBJECTIVE_NON_FUNCTIONAL)) {
063                        text.addPara("This one, however, is not connected to the Sector-wide network and is not emitting the hyperwave radiation typically indicative of relay operation. The cause of its lack of function is unknown.");
064                }
065                if (isHacked()) {
066                        text.addPara("You have a comm sniffer running on this relay.");
067                }
068        }
069        
070        
071        public void printEffect(TooltipMakerAPI text, float pad) {
072//              int bonus = Math.abs(Math.round(
073//                                      CommRelayCondition.NO_RELAY_PENALTY - CommRelayCondition.COMM_RELAY_BONUS));
074//              if (isMakeshift()) {
075//                      bonus = Math.abs(Math.round(
076//                                      CommRelayCondition.NO_RELAY_PENALTY - CommRelayCondition.MAKESHIFT_COMM_RELAY_BONUS));
077//              }
078                int bonus = Math.abs(Math.round(
079                                CommRelayCondition.COMM_RELAY_BONUS));
080                if (isMakeshift()) {
081                        bonus = Math.abs(Math.round(
082                                        CommRelayCondition.MAKESHIFT_COMM_RELAY_BONUS));
083                }
084                text.addPara(BaseIntelPlugin.INDENT + "%s stability for same-faction colonies in system",
085                                pad, Misc.getHighlightColor(), "+" + bonus);
086        }
087        
088        public void addHackStatusToTooltip(TooltipMakerAPI text, float pad) {
089//              int bonus = Math.abs(Math.round(
090//                              CommRelayCondition.NO_RELAY_PENALTY - CommRelayCondition.COMM_RELAY_BONUS));
091//              if (isMakeshift()) {
092//                      bonus = Math.abs(Math.round(
093//                              CommRelayCondition.NO_RELAY_PENALTY - CommRelayCondition.MAKESHIFT_COMM_RELAY_BONUS));
094//              }
095                int bonus = Math.abs(Math.round(
096                                CommRelayCondition.COMM_RELAY_BONUS));
097                if (isMakeshift()) {
098                        bonus = Math.abs(Math.round(
099                                        CommRelayCondition.MAKESHIFT_COMM_RELAY_BONUS));
100                }
101                        
102                if (isHacked()) {
103                        text.addPara("%s stability for in-system colonies",
104                                         pad, Misc.getHighlightColor(), "+" + bonus);
105                        text.addPara("Comm sniffer installed", Misc.getTextColor(), pad);
106                } else {
107                        text.addPara("%s stability for same-faction colonies in-system",
108                                         pad, Misc.getHighlightColor(), "+" + bonus);
109
110                }
111        }
112
113        @Override
114        public void setHacked(boolean hacked) {
115                if (hacked) {
116                        setHacked(hacked, -1f);
117                        boolean found = CommSnifferIntel.getExistingSnifferIntelForRelay(entity) != null;
118                        if (!found) {
119                                CommSnifferIntel intel = new CommSnifferIntel(entity);
120                                InteractionDialogAPI dialog = Global.getSector().getCampaignUI().getCurrentInteractionDialog();
121                                if (dialog != null) {
122                                        Global.getSector().getIntelManager().addIntelToTextPanel(intel, dialog.getTextPanel());
123                                }
124                        }
125                } else {
126                        setHacked(hacked, -1f);
127                }
128        }
129
130        
131        
132        private void checkIntelFromCommSniffer() {
133                if (!isHacked()) return;
134                
135                boolean playerInRelayRange = Global.getSector().getIntelManager().isPlayerInRangeOfCommRelay();
136                for (IntelInfoPlugin intel : Global.getSector().getIntelManager().getCommQueue()) {
137                        if (intel instanceof CommSnifferReadableIntel) {
138                                CommSnifferReadableIntel csi = (CommSnifferReadableIntel) intel;
139                                if (csi.canMakeVisibleToCommSniffer(playerInRelayRange, entity)) {
140                                        intel.setForceAddNextFrame(true);
141                                }
142                        }
143                }
144        }
145        
146}
147
148
149