001package com.fs.starfarer.api.impl.campaign.intel.bar.events;
002
003import java.awt.Color;
004import java.util.ArrayList;
005import java.util.List;
006
007import com.fs.starfarer.api.Global;
008import com.fs.starfarer.api.campaign.CargoAPI;
009import com.fs.starfarer.api.campaign.CargoAPI.CargoItemType;
010import com.fs.starfarer.api.campaign.PersonImportance;
011import com.fs.starfarer.api.campaign.TextPanelAPI;
012import com.fs.starfarer.api.campaign.econ.CommoditySpecAPI;
013import com.fs.starfarer.api.campaign.econ.Industry;
014import com.fs.starfarer.api.campaign.econ.MarketAPI;
015import com.fs.starfarer.api.characters.PersonAPI;
016import com.fs.starfarer.api.impl.campaign.CoreReputationPlugin.CustomRepImpact;
017import com.fs.starfarer.api.impl.campaign.CoreReputationPlugin.RepActionEnvelope;
018import com.fs.starfarer.api.impl.campaign.CoreReputationPlugin.RepActions;
019import com.fs.starfarer.api.impl.campaign.econ.impl.BaseIndustry;
020import com.fs.starfarer.api.impl.campaign.ids.Commodities;
021import com.fs.starfarer.api.impl.campaign.ids.Factions;
022import com.fs.starfarer.api.impl.campaign.ids.Industries;
023import com.fs.starfarer.api.impl.campaign.ids.Ranks;
024import com.fs.starfarer.api.impl.campaign.ids.Tags;
025import com.fs.starfarer.api.impl.campaign.intel.contacts.ContactIntel;
026import com.fs.starfarer.api.impl.campaign.rulecmd.AddRemoveCommodity;
027import com.fs.starfarer.api.util.Misc;
028import com.fs.starfarer.api.util.WeightedRandomPicker;
029
030public class QuartermasterCargoSwapBarEvent extends BaseGetCommodityBarEvent {
031        
032        public QuartermasterCargoSwapBarEvent() {
033                super();
034        }
035        
036        public boolean shouldShowAtMarket(MarketAPI market) {
037                if (!super.shouldShowAtMarket(market)) return false;
038                regen(market);
039                if (commodity == null) return false;
040                
041                if (!market.getFactionId().equals(Factions.HEGEMONY)) {
042                        return false;
043                }
044                boolean hasMilitaryBase = false;
045                for (Industry ind : market.getIndustries()) {
046                        if (ind.getSpec().hasTag(Industries.TAG_MILITARY) || ind.getSpec().hasTag(Industries.TAG_COMMAND)) {
047                                hasMilitaryBase = true;
048                                break;
049                        }
050                }
051                if (!hasMilitaryBase) return false;
052                
053                
054                return true;
055        }
056
057        protected int playerGiveQuantity = 0;
058        protected String playerGiveCommodity = null;
059        @Override
060        protected String getCommodityId() {
061                String[] possible = new String[] {
062                                Commodities.HAND_WEAPONS,
063                                Commodities.HEAVY_MACHINERY,
064                                Commodities.SUPPLIES,
065                                Commodities.FUEL,
066                                Commodities.RARE_METALS,
067                                Commodities.METALS,
068                };
069                
070                List <String> playerHas = new ArrayList<String>(); 
071                List <String> playerNotHas = new ArrayList<String>(); 
072                
073                CargoAPI cargo = Global.getSector().getPlayerFleet().getCargo();
074
075                
076                float value = getValue();
077                for (String c : possible) {
078                        int q = (int) cargo.getQuantity(CargoItemType.RESOURCES, c);
079                        CommoditySpecAPI spec = Global.getSettings().getCommoditySpec(c);
080                        int num = (int) (value / spec.getBasePrice());
081                        if (q >= num) {
082                                playerHas.add(c);
083                        } else if (q < num) {
084                                playerNotHas.add(c);
085                        }
086                }
087                
088                if (playerHas.isEmpty() || playerNotHas.isEmpty()) return null;
089                
090                WeightedRandomPicker<String> take = new WeightedRandomPicker<String>(random);
091                take.addAll(playerNotHas);
092                
093                WeightedRandomPicker<String> give = new WeightedRandomPicker<String>(random);
094                give.addAll(playerHas);
095                
096                String pick = take.pick();
097                playerGiveCommodity = give.pick();
098                
099                CommoditySpecAPI spec = Global.getSettings().getCommoditySpec(playerGiveCommodity);
100                int num = (int) (value / spec.getBasePrice());
101                playerGiveQuantity = (int) (num * 0.33f);
102                
103                if (playerGiveQuantity <= 0) return null;
104                
105                return pick;
106        }
107        
108        @Override
109        protected PersonAPI createPerson() {
110                for (PersonAPI person : market.getPeopleCopy()) {
111                        if (Ranks.POST_SUPPLY_OFFICER.equals(person.getPostId())) {
112                                adjustPerson(person);
113                                return person;
114                        }
115                }
116
117                PersonAPI person = Global.getSector().getFaction(getPersonFaction()).createRandomPerson(random);
118                person.setRankId(getPersonRank());
119                person.setPostId(getPersonRank());
120                adjustPerson(person);
121                return person;
122        }
123        
124        @Override
125        protected void adjustPerson(PersonAPI person) {
126                super.adjustPerson(person);
127                person.setImportanceAndVoice(PersonImportance.MEDIUM, random);
128                person.addTag(Tags.CONTACT_MILITARY);
129        }
130
131        @Override
132        protected String getPersonFaction() {
133                return Factions.HEGEMONY;
134        }
135        
136        @Override
137        protected String getPersonRank() {
138                return Ranks.SPACE_COMMANDER;
139        }
140        
141        @Override
142        protected String getPersonPost() {
143                return Ranks.POST_SUPPLY_OFFICER;
144        }
145        
146        protected float getValue() {
147                float value = (1000 + 100 * random.nextInt(6)) * BaseIndustry.getSizeMult(market.getSize());
148                return value;
149        }
150        
151        @Override
152        protected int computeQuantity() {
153                String c = commodity;
154                CommoditySpecAPI spec = Global.getSettings().getCommoditySpec(c);
155                int num = (int) (getValue() / spec.getBasePrice());
156                return num;
157        }
158        
159        @Override
160        protected float getPriceMult() {
161                return 0;
162        }
163        
164        @Override
165        protected String getPrompt() {
166                return "None other than the station's quartermaster, who looks rather sullen, is sitting at the bar.";
167        }
168        
169        @Override
170        protected String getOptionText() {
171                return "Approach the quartermaster and offer to buy " + getHimOrHer() + " a drink";
172        }
173        
174        @Override
175        protected String getMainText() {
176                CommoditySpecAPI take = Global.getSettings().getCommoditySpec(commodity);
177                CommoditySpecAPI give = Global.getSettings().getCommoditySpec(playerGiveCommodity);
178                
179                CargoAPI cargo = Global.getSector().getPlayerFleet().getCargo();
180                int qty = (int) cargo.getQuantity(CargoItemType.RESOURCES, give.getId());
181                String units = "units";
182                if (qty == 1) units = "unit";
183                
184                return "In venerated tradition going back thousands of cycles, the quartermaster " +
185                                "vents to you about how " + getHisOrHer() + " provision request got mixed up and " +
186                                getHeOrShe() + " was shipped %s units of " + take.getLowerCaseName() + " instead of %s units of " +
187                                give.getLowerCaseName() + ". \"A mining drone with half its Ludd-damned " +
188                                "computer fried on rads would do a better job than the idiots in Fleet Supply,\" " +
189                                getHeOrShe() + " growls, knocking back the rest of " + getHisOrHer() + " drink and " +
190                                "slamming the glass down.\n\n" +
191                                "You have %s " + units + " of " + give.getLowerCaseName() + " on board.";
192        }
193        
194        @Override
195        protected String [] getMainTextTokens() {
196                CommoditySpecAPI give = Global.getSettings().getCommoditySpec(playerGiveCommodity);
197                CargoAPI cargo = Global.getSector().getPlayerFleet().getCargo();
198                int qty = (int) cargo.getQuantity(CargoItemType.RESOURCES, give.getId());
199                return new String [] { Misc.getWithDGS(quantity),  Misc.getWithDGS(playerGiveQuantity) ,
200                                Misc.getWithDGS(qty)};
201        }
202        @Override
203        protected Color [] getMainTextColors() {
204                return new Color [] { Misc.getHighlightColor(), Misc.getHighlightColor(), Misc.getHighlightColor()};
205        }
206        
207        @Override
208        protected String getConfirmText() {
209                CommoditySpecAPI take = Global.getSettings().getCommoditySpec(commodity);
210                CommoditySpecAPI give = Global.getSettings().getCommoditySpec(playerGiveCommodity);
211                return "Offer to swap " + playerGiveQuantity + " " + give.getLowerCaseName() + " " +
212                                "for " + quantity + " " + take.getLowerCaseName() + ", as a favor from you to " + getHimOrHer();
213        }
214        
215        @Override
216        protected String getCancelText() {
217                return "Do nothing but commiserate with the quartermaster as you finish your drink.";
218        }
219
220        @Override
221        protected String getAcceptText() {
222                CommoditySpecAPI take = Global.getSettings().getCommoditySpec(commodity);
223                CommoditySpecAPI give = Global.getSettings().getCommoditySpec(playerGiveCommodity);
224                return "You exchange comms with the quartermaster and the very next day " + getHeOrShe() + 
225                           " smoothes things along with the port authorities and arranges for the quickest cargo " +
226                           "transfer you've ever seen, trading your %s " + give.getLowerCaseName() + " for " +
227                           "%s " + take.getLowerCaseName() + ". Afterward, the quartermaster " +
228                           "sends you a personal thank-you note.";
229        }
230        
231        @Override
232        protected String [] getAcceptTextTokens() {
233                return new String [] { Misc.getWithDGS(playerGiveQuantity), Misc.getWithDGS(quantity) };
234        }
235        @Override
236        protected Color [] getAcceptTextColors() {
237                return new Color [] { Misc.getHighlightColor(), Misc.getHighlightColor() };
238        }
239
240        @Override
241        protected void doExtraConfirmActions() {
242                CargoAPI cargo = Global.getSector().getPlayerFleet().getCargo();
243                cargo.removeCommodity(playerGiveCommodity, playerGiveQuantity);
244                
245                TextPanelAPI text = dialog.getTextPanel();
246                AddRemoveCommodity.addCommodityLossText(playerGiveCommodity, playerGiveQuantity, text);
247                
248                CustomRepImpact impact = new CustomRepImpact();
249                impact.delta = 0.1f;
250                
251                Global.getSector().adjustPlayerReputation(
252                                new RepActionEnvelope(RepActions.CUSTOM, 
253                                                impact, null, text, true, true),
254                                                person);
255                
256                impact = new CustomRepImpact();
257                impact.delta = 0.03f;
258                Global.getSector().adjustPlayerReputation(
259                                new RepActionEnvelope(RepActions.CUSTOM, 
260                                                impact, null, text, true, true),
261                                                person.getFaction().getId());
262                
263                ContactIntel.addPotentialContact(person, market, text);
264        }
265        
266        
267        @Override
268        protected String getDeclineText() {
269                return "You drink to the bureaucrats in Fleet Supply \"mistaking the airlock for " +
270                                "the head-hatch\", empty your glass, and make your exit.";
271        }
272
273
274        
275}
276
277
278