001package com.fs.starfarer.api.impl.campaign.missions.hub;
002
003import java.util.Map;
004import java.util.Random;
005
006import com.fs.starfarer.api.Global;
007import com.fs.starfarer.api.campaign.InteractionDialogAPI;
008import com.fs.starfarer.api.campaign.econ.MarketAPI;
009import com.fs.starfarer.api.campaign.rules.MemKeys;
010import com.fs.starfarer.api.campaign.rules.MemoryAPI;
011import com.fs.starfarer.api.impl.campaign.intel.bar.events.BaseBarEvent;
012import com.fs.starfarer.api.impl.campaign.rulecmd.FireBest;
013import com.fs.starfarer.api.loading.BarEventSpec;
014import com.fs.starfarer.api.util.Misc;
015
016public class HubMissionBarEventWrapper extends BaseBarEvent {
017
018        protected long seed;
019        //protected PersonAPI person;
020        
021        protected transient BarEventSpec spec = null;
022        protected String specId = null;
023        
024        protected transient Random genRandom;
025        protected transient HubMissionWithBarEvent mission;
026        
027        public HubMissionBarEventWrapper(String specId) {
028                this.specId = specId;
029                seed = Misc.genRandomSeed();
030                readResolve();
031        }
032        
033        protected Object readResolve() {
034                spec = Global.getSettings().getBarEventSpec(specId);
035                return this;
036        }
037        
038        public String getBarEventId() {
039                return specId;
040        }
041        
042        
043        @Override
044        public boolean shouldShowAtMarket(MarketAPI market) {
045                //return super.shouldShowAtMarket(market);
046//              if (spec.getId().equals("extr")) {
047//                      System.out.println("wfwefwe");
048//              }
049                if (shownAt != null && shownAt != market) return false;
050                
051                abortMission();
052                
053                //if (mission == null) {
054                        genRandom = new Random(seed + market.getId().hashCode() * 181783497276652981L);
055                        
056                        if (genRandom.nextFloat() > spec.getProb()) return false;
057                        
058                        mission = spec.createMission();
059                        mission.setMissionId(specId);
060                        mission.setGenRandom(genRandom);
061                //}
062                
063                return mission.shouldShowAtMarket(market);
064        }
065        
066
067        public String getSpecId() {
068                return specId;
069        }
070
071        public HubMission getMission() {
072                return mission;
073        }
074
075        public void abortMission() {
076                if (mission != null) {
077                        mission.abort();
078                        mission = null;
079                }
080        }
081        
082        @Override
083        public void addPromptAndOption(InteractionDialogAPI dialog, Map<String, MemoryAPI> memoryMap) {
084//              abortMission();
085                
086                MarketAPI market = dialog.getInteractionTarget().getMarket();
087//              genRandom = new Random(seed + market.getId().hashCode());
088//              
089//              mission = spec.createMission();
090//              mission.setMissionId(specId);
091//              mission.setGenRandom(genRandom);
092                mission.createAndAbortIfFailed(market, true);
093                //mission.setGenRandom(null);
094                if (mission.isMissionCreationAborted()) {
095                        mission = null;
096                        return;
097                }
098                
099                MemoryAPI prev = memoryMap.get(MemKeys.LOCAL);
100                if (mission.getPerson() != null) {
101                        memoryMap.put(MemKeys.ENTITY, prev);
102                        memoryMap.put(MemKeys.LOCAL, mission.getPerson().getMemoryWithoutUpdate());
103                        memoryMap.put(MemKeys.PERSON_FACTION, mission.getPerson().getFaction().getMemory());
104                }
105                mission.updateInteractionData(dialog, memoryMap);
106                
107                FireBest.fire(null, dialog, memoryMap, mission.getTriggerPrefix() + "_blurbBar true");          
108                FireBest.fire(null, dialog, memoryMap, mission.getTriggerPrefix() + "_optionBar true");
109                
110                memoryMap.put(MemKeys.LOCAL, prev);
111                memoryMap.remove(MemKeys.ENTITY);
112                memoryMap.remove(MemKeys.PERSON_FACTION);
113                
114                
115                //BaseMissionHub.getCreatedMissionsList(mission.getPerson(), market).add((BaseHubMission) mission);
116        }
117        
118        
119        
120}
121
122
123
124
125
126
127
128