001package com.fs.starfarer.api.impl.campaign.rulecmd.salvage;
002
003import java.util.ArrayList;
004import java.util.List;
005import java.util.Map;
006import java.util.Random;
007
008import com.fs.starfarer.api.Global;
009import com.fs.starfarer.api.campaign.CampaignFleetAPI;
010import com.fs.starfarer.api.campaign.CargoAPI;
011import com.fs.starfarer.api.campaign.CargoStackAPI;
012import com.fs.starfarer.api.campaign.FactionAPI;
013import com.fs.starfarer.api.campaign.InteractionDialogAPI;
014import com.fs.starfarer.api.campaign.OptionPanelAPI;
015import com.fs.starfarer.api.campaign.SectorEntityToken;
016import com.fs.starfarer.api.campaign.TextPanelAPI;
017import com.fs.starfarer.api.campaign.econ.CommoditySpecAPI;
018import com.fs.starfarer.api.campaign.rules.MemoryAPI;
019import com.fs.starfarer.api.characters.PersonAPI;
020import com.fs.starfarer.api.fleet.FleetMemberAPI;
021import com.fs.starfarer.api.impl.campaign.ids.Commodities;
022import com.fs.starfarer.api.impl.campaign.rulecmd.BaseCommandPlugin;
023import com.fs.starfarer.api.util.Misc;
024import com.fs.starfarer.api.util.Misc.Token;
025
026/**
027 */
028public class DemandCargo extends BaseCommandPlugin {
029        
030        public static class DemandData {
031                public CargoAPI cargo;//Global.getFactory().createCargo(true);
032                public List<FleetMemberAPI> ships = new ArrayList<FleetMemberAPI>();
033                public int credits = 0;
034                public boolean hasAnythingWorthwhile = true;
035        }
036        
037        
038        
039        protected CampaignFleetAPI playerFleet;
040        protected CampaignFleetAPI otherFleet;
041        protected SectorEntityToken entity;
042        protected FactionAPI playerFaction;
043        protected FactionAPI entityFaction;
044        protected TextPanelAPI text;
045        protected OptionPanelAPI options;
046        protected CargoAPI playerCargo;
047        protected MemoryAPI memory;
048        protected InteractionDialogAPI dialog;
049        protected Map<String, MemoryAPI> memoryMap;
050        protected PersonAPI person;
051        protected FactionAPI faction;
052
053        protected boolean buysAICores;
054        protected float valueMult;
055        protected float repMult;
056        
057        protected DemandData data = new DemandData();
058        
059        public DemandCargo() {
060                super();
061        }
062
063        public boolean execute(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) {
064                
065//              Pirate interaction flow:
066//                      They hail you OR you open the comm link and "perhaps we can avoid fighting"
067//                      <some greeting/setup text>
068//                      -> Continue
069//                      <negotiating text, list of stuff>
070//                      -> Cut the comm link, OR
071//                      -> Order the transfer
072//                      <lost X> text
073//                      -> Continue?
074//                      <ending text, allow you to disengage>
075//                      -> Cut the comm link
076                
077                this.dialog = dialog;
078                this.memoryMap = memoryMap;
079                
080                String command = params.get(0).getString(memoryMap);
081                if (command == null) return false;
082                
083                memory = getEntityMemory(memoryMap);
084                
085                entity = dialog.getInteractionTarget();
086                text = dialog.getTextPanel();
087                options = dialog.getOptionPanel();
088                
089                otherFleet = (CampaignFleetAPI) entity;
090                
091                playerFleet = Global.getSector().getPlayerFleet();
092                playerCargo = playerFleet.getCargo();
093                
094                playerFaction = Global.getSector().getPlayerFaction();
095                entityFaction = entity.getFaction();
096                
097                person = dialog.getInteractionTarget().getActivePerson();
098                if (person != null) {
099                        faction = person.getFaction();
100                } else {
101                        faction = entityFaction;
102                }
103                
104                String key = "$DemandCargo_temp";
105                MemoryAPI mem = otherFleet.getMemoryWithoutUpdate();
106                if (mem.contains(key)) {
107                        data = (DemandData) mem.get(key);
108                } else {
109                        data = new DemandData();
110                        data.cargo = Global.getFactory().createCargo(true);;
111                        mem.set(key, data, 0f);
112                }
113                
114                if (command.equals("selectCargo")) {
115                        selectCores();
116                } else if (command.equals("playerHasValuableCargo")) {
117                        //return playerHasCores();
118                }
119                
120                return true;
121        }
122        
123        protected Random getRandom() {
124                String key = "$DemandCargo_random";
125                MemoryAPI mem = otherFleet.getMemoryWithoutUpdate();
126                Random random = null;
127                if (mem.contains(key)) {
128                        random = (Random) mem.get(key);
129                } else {
130                        //random = new Random(Misc.getSalvageSeed(otherFleet));
131                        long seed = Misc.getSalvageSeed(otherFleet);
132                        seed /= 321L;
133                        seed *= (Global.getSector().getClock().getMonth() + 10);
134                        random = new Random(seed);
135                }
136                mem.set(key, random, 30f);
137                
138                return random;
139        }
140
141        protected void computeAndPrintDemands() {
142                int fleetValue = 0;
143                
144                for (FleetMemberAPI member : playerFleet.getFleetData().getMembersListCopy()) {
145                        fleetValue += member.getHullSpec().getBaseValue();
146                }
147                
148                for (CargoStackAPI stack : playerCargo.getStacksCopy()) {
149                        fleetValue += stack.getBaseValuePerUnit() * stack.getSize();
150                }
151                
152                float demandFraction = 0.2f;
153                
154                float rel = otherFleet.getFaction().getRelToPlayer().getRel();
155                
156                if (rel > 0) {
157                        demandFraction *= (0.2f + 0.8f * rel);
158                }
159                
160                int demandValue = (int) (fleetValue * demandFraction);
161                
162                Random random = getRandom();
163                
164                
165                
166        }
167        
168        
169
170        protected void selectCores() {
171                CargoAPI copy = Global.getFactory().createCargo(false);
172                //copy.addAll(cargo);
173                for (CargoStackAPI stack : playerCargo.getStacksCopy()) {
174                        CommoditySpecAPI spec = stack.getResourceIfResource();
175                        if (spec != null && spec.getDemandClass().equals(Commodities.AI_CORES)) {
176                                copy.addFromStack(stack);
177                        }
178                }
179                copy.sort();
180                
181//              final float width = 310f;
182//              dialog.showCargoPickerDialog("Select AI cores to turn in", "Confirm", "Cancel", true, width, copy, new CargoPickerListener() {
183//                      public void pickedCargo(CargoAPI cargo) {
184//                              cargo.sort();
185//                              for (CargoStackAPI stack : cargo.getStacksCopy()) {
186//                                      playerCargo.removeItems(stack.getType(), stack.getData(), stack.getSize());
187//                                      if (stack.isCommodityStack()) { // should be always, but just in case
188//                                              AddRemoveCommodity.addCommodityLossText(stack.getCommodityId(), (int) stack.getSize(), text);
189//                                      }
190//                              }
191//                              
192//                              float bounty = computeCoreCreditValue(cargo);
193//                              float repChange = computeCoreReputationValue(cargo);
194//
195//                              if (bounty > 0) {
196//                                      playerCargo.getCredits().add(bounty);
197//                                      AddRemoveCommodity.addCreditsGainText((int)bounty, text);
198//                              }
199//                              
200//                              if (repChange >= 1f) {
201//                                      CustomRepImpact impact = new CustomRepImpact();
202//                                      impact.delta = repChange * 0.01f;
203//                                      Global.getSector().adjustPlayerReputation(
204//                                                      new RepActionEnvelope(RepActions.CUSTOM, impact,
205//                                                                                                null, text, true), 
206//                                                                                                faction.getId());
207//                                      
208//                                      impact.delta *= 0.25f;
209//                                      if (impact.delta >= 0.01f) {
210//                                              Global.getSector().adjustPlayerReputation(
211//                                                              new RepActionEnvelope(RepActions.CUSTOM, impact,
212//                                                                                                        null, text, true), 
213//                                                                                                        person);
214//                                      }
215//                              }
216//                              
217//                              FireBest.fire(null, dialog, memoryMap, "AICoresTurnedIn");
218//                      }
219//                      public void cancelledCargoSelection() {
220//                      }
221//                      public void recreateTextPanel(TooltipMakerAPI panel, CargoAPI cargo, CargoStackAPI pickedUp, boolean pickedUpFromSource, CargoAPI combined) {
222//                      
223//                              float bounty = computeCoreCreditValue(combined);
224//                              float repChange = computeCoreReputationValue(combined);
225//                              
226//                              float pad = 3f;
227//                              float small = 5f;
228//                              float opad = 10f;
229//
230//                              panel.setParaFontOrbitron();
231//                              panel.addPara(Misc.ucFirst(faction.getDisplayName()), faction.getBaseUIColor(), 1f);
232//                              //panel.addPara(faction.getDisplayNameLong(), faction.getBaseUIColor(), opad);
233//                              //panel.addPara(faction.getDisplayName() + " (" + entity.getMarket().getName() + ")", faction.getBaseUIColor(), opad);
234//                              panel.setParaFontDefault();
235//                              
236//                              panel.addImage(faction.getLogo(), width * 1f, pad);
237//                              
238//                              
239//                              //panel.setParaFontColor(Misc.getGrayColor());
240//                              //panel.setParaSmallInsignia();
241//                              //panel.setParaInsigniaLarge();
242//                              panel.addPara("Compared to dealing with other factions, turning AI cores in to " + 
243//                                              faction.getDisplayNameLongWithArticle() + " " +
244//                                              "will result in:", opad);
245//                              panel.beginGridFlipped(width, 1, 40f, 10f);
246//                              //panel.beginGrid(150f, 1);
247//                              panel.addToGrid(0, 0, "Bounty value", "" + (int)(valueMult * 100f) + "%");
248//                              panel.addToGrid(0, 1, "Reputation gain", "" + (int)(repMult * 100f) + "%");
249//                              panel.addGrid(pad);
250//                              
251//                              panel.addPara("If you turn in the selected AI cores, you will receive a %s bounty " +
252//                                              "and your standing with " + faction.getDisplayNameWithArticle() + " will improve by %s points.",
253//                                              opad * 1f, Misc.getHighlightColor(),
254//                                              Misc.getWithDGS(bounty) + Strings.C,
255//                                              "" + (int) repChange);
256//                              
257//                              
258//                              //panel.addPara("Bounty: %s", opad, Misc.getHighlightColor(), Misc.getWithDGS(bounty) + Strings.C);
259//                              //panel.addPara("Reputation: %s", pad, Misc.getHighlightColor(), "+12");
260//                      }
261//              });
262        }
263
264
265
266        
267        
268}
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283