Starsector API
Loading...
Searching...
No Matches
SystemBountyManager.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.intel;
2
3import java.util.HashSet;
4import java.util.Set;
5
6import com.fs.starfarer.api.EveryFrameScript;
7import com.fs.starfarer.api.Global;
8import com.fs.starfarer.api.campaign.econ.MarketAPI;
9import com.fs.starfarer.api.impl.campaign.ids.Conditions;
10import com.fs.starfarer.api.impl.campaign.ids.Factions;
11import com.fs.starfarer.api.impl.campaign.ids.MemFlags;
12import com.fs.starfarer.api.util.ListMap;
13import com.fs.starfarer.api.util.WeightedRandomPicker;
14
16
17 public static final String KEY = "$core_systemBountyManager";
18
20 Object test = Global.getSector().getMemoryWithoutUpdate().get(KEY);
21 return (SystemBountyManager) test;
22 }
23
25 super();
26 Global.getSector().getMemoryWithoutUpdate().set(KEY, this);
27 }
28
29 @Override
30 protected int getMinConcurrent() {
31 return Global.getSettings().getInt("minSystemBounties");
32 }
33 @Override
34 protected int getMaxConcurrent() {
35 return Global.getSettings().getInt("maxSystemBounties");
36 }
37
38 @Override
40 //if ((float) Math.random() < 0.1f) return null;
41
42 MarketAPI market = pickMarket();
43 if (market == null) return null;
44
45 SystemBountyIntel intel = new SystemBountyIntel(market);
46 return intel;
47 }
48
49
50 public boolean isActive(MarketAPI market) {
51 for (EveryFrameScript s : getActive()) {
52 if (market == ((SystemBountyIntel)s).getMarket()) return true;
53 }
54 return false;
55 }
56
57 public SystemBountyIntel getActive(MarketAPI market) {
58 for (EveryFrameScript s : getActive()) {
60 if (intel.isDone()) continue;
61
62 if (market == intel.getMarket()) return intel;
63 }
64 return null;
65 }
66
67 public void addOrResetBounty(MarketAPI market) {
68 if (market != null) {
70 if (active != null) {
71 active.reset();
72 } else {
73 addActive(new SystemBountyIntel(market));
74 }
75 }
76 }
77
78 protected MarketAPI pickMarket() {
79 Set<MarketAPI> already = new HashSet<MarketAPI>();
80 for (EveryFrameScript s : getActive()) {
81 already.add(((SystemBountyIntel)s).getMarket());
82 }
83
84 ListMap<MarketAPI> locationIdToMarket = new ListMap<MarketAPI>();
85 for (MarketAPI market : Global.getSector().getEconomy().getMarketsCopy()) {
86 if (market.isHidden()) continue;
87 locationIdToMarket.add(market.getContainingLocation().getId(), market);
88 }
89
90 WeightedRandomPicker<MarketAPI> pickerWithPirateActivity = new WeightedRandomPicker<MarketAPI>();
91 WeightedRandomPicker<MarketAPI> picker = new WeightedRandomPicker<MarketAPI>();
92 OUTER: for (MarketAPI market : Global.getSector().getEconomy().getMarketsCopy()) {
93 if (market.isHidden()) continue;
94 if (market.getSize() <= 4) continue;
95 if (market.isPlayerOwned()) continue;
96 if (already.contains(market)) continue;
97
98 if (market.getFaction().getCustom().optBoolean(Factions.CUSTOM_POSTS_NO_BOUNTIES)) {
99 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
static SettingsAPI getSettings()
Definition Global.java:51
static SectorAPI getSector()
Definition Global.java:59