001package com.fs.starfarer.api.impl.campaign.intel; 002 003import java.util.HashSet; 004import java.util.Set; 005 006import com.fs.starfarer.api.EveryFrameScript; 007import com.fs.starfarer.api.Global; 008import com.fs.starfarer.api.campaign.econ.MarketAPI; 009import com.fs.starfarer.api.impl.campaign.ids.Conditions; 010import com.fs.starfarer.api.impl.campaign.ids.Factions; 011import com.fs.starfarer.api.impl.campaign.ids.MemFlags; 012import com.fs.starfarer.api.util.ListMap; 013import com.fs.starfarer.api.util.WeightedRandomPicker; 014 015public class SystemBountyManager extends BaseEventManager { 016 017 public static final String KEY = "$core_systemBountyManager"; 018 019 public static SystemBountyManager getInstance() { 020 Object test = Global.getSector().getMemoryWithoutUpdate().get(KEY); 021 return (SystemBountyManager) test; 022 } 023 024 public SystemBountyManager() { 025 super(); 026 Global.getSector().getMemoryWithoutUpdate().set(KEY, this); 027 } 028 029 @Override 030 protected int getMinConcurrent() { 031 return Global.getSettings().getInt("minSystemBounties"); 032 } 033 @Override 034 protected int getMaxConcurrent() { 035 return Global.getSettings().getInt("maxSystemBounties"); 036 } 037 038 @Override 039 protected EveryFrameScript createEvent() { 040 //if ((float) Math.random() < 0.1f) return null; 041 042 MarketAPI market = pickMarket(); 043 if (market == null) return null; 044 045 SystemBountyIntel intel = new SystemBountyIntel(market); 046 return intel; 047 } 048 049 050 public boolean isActive(MarketAPI market) { 051 for (EveryFrameScript s : getActive()) { 052 if (market == ((SystemBountyIntel)s).getMarket()) return true; 053 } 054 return false; 055 } 056 057 public SystemBountyIntel getActive(MarketAPI market) { 058 for (EveryFrameScript s : getActive()) { 059 SystemBountyIntel intel = (SystemBountyIntel) s; 060 if (intel.isDone()) continue; 061 062 if (market == intel.getMarket()) return intel; 063 } 064 return null; 065 } 066 067 public void addOrResetBounty(MarketAPI market) { 068 if (market != null) { 069 SystemBountyIntel active = getActive(market); 070 if (active != null) { 071 active.reset(); 072 } else { 073 addActive(new SystemBountyIntel(market)); 074 } 075 } 076 } 077 078 protected MarketAPI pickMarket() { 079 Set<MarketAPI> already = new HashSet<MarketAPI>(); 080 for (EveryFrameScript s : getActive()) { 081 already.add(((SystemBountyIntel)s).getMarket()); 082 } 083 084 ListMap<MarketAPI> locationIdToMarket = new ListMap<MarketAPI>(); 085 for (MarketAPI market : Global.getSector().getEconomy().getMarketsCopy()) { 086 if (market.isHidden()) continue; 087 locationIdToMarket.add(market.getContainingLocation().getId(), market); 088 } 089 090 WeightedRandomPicker<MarketAPI> pickerWithPirateActivity = new WeightedRandomPicker<MarketAPI>(); 091 WeightedRandomPicker<MarketAPI> picker = new WeightedRandomPicker<MarketAPI>(); 092 OUTER: for (MarketAPI market : Global.getSector().getEconomy().getMarketsCopy()) { 093 if (market.isHidden()) continue; 094 if (market.getSize() <= 4) continue; 095 if (market.isPlayerOwned()) continue; 096 if (already.contains(market)) continue; 097 098 if (market.getFaction().getCustom().optBoolean(Factions.CUSTOM_POSTS_NO_BOUNTIES)) { 099 continue; 100 } 101 102 if (Global.getSector().isInNewGameAdvance() && market.getId().equals("jangala")) { 103 continue; // want a fresh one to be auto-started there on game start 104 } 105 106 for (MarketAPI other : locationIdToMarket.getList(market.getContainingLocation().getId())) { 107 if (market.getFaction() == other.getFaction() && already.contains(other)) { 108 continue OUTER; 109 } 110 } 111 112 float w = market.getSize() * 0.25f; 113 for (MarketAPI other : locationIdToMarket.getList(market.getContainingLocation().getId())) { 114 if (market.getFaction().isHostileTo(other.getFaction())) { 115// w += other.getSize() * 25f; 116// if (other.getMemoryWithoutUpdate().getBoolean(MemFlags.MARKET_MILITARY) || 117// other.getMemoryWithoutUpdate().getBoolean(MemFlags.MARKET_PATROL)) { 118// w += other.getSize() * 25f; 119// } 120 w += 10f; 121 if (other.getMemoryWithoutUpdate().getBoolean(MemFlags.MARKET_MILITARY) || 122 other.getMemoryWithoutUpdate().getBoolean(MemFlags.MARKET_PATROL)) { 123 w += 10f; 124 } 125 if (market.getMemoryWithoutUpdate().getBoolean(MemFlags.MARKET_MILITARY)) { 126 w += 10f; 127 } else if (market.getMemoryWithoutUpdate().getBoolean(MemFlags.MARKET_PATROL)) { 128 w += 5f; 129 } 130 } 131 } 132 133 if (market.hasCondition(Conditions.PIRATE_ACTIVITY)) { 134 w += 10f; 135 pickerWithPirateActivity.add(market, w); 136 } else { 137 picker.add(market, w); 138 } 139 } 140 141 //picker.print("Bounty weights: "); 142 143 MarketAPI market = pickerWithPirateActivity.pick(); 144 float w = pickerWithPirateActivity.getWeight(market); 145 if (market == null) { 146 market = picker.pick(); 147 w = picker.getWeight(market); 148 } 149 150 float probMult = 1f / (getOngoing() + 1f); 151 152 if ((float) Math.random() > w * 0.01f * probMult) { 153 market = null; 154 } 155 156 return market; 157 158 } 159 160} 161 162 163 164 165 166 167 168