Starsector API
Loading...
Searching...
No Matches
FactionHostilityManager.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.intel;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Map;
7import java.util.Random;
8
9import com.fs.starfarer.api.EveryFrameScript;
10import com.fs.starfarer.api.Global;
11import com.fs.starfarer.api.campaign.FactionAPI;
12import com.fs.starfarer.api.campaign.RepLevel;
13import com.fs.starfarer.api.impl.campaign.ids.Factions;
14import com.fs.starfarer.api.util.Misc;
15import com.fs.starfarer.api.util.Pair;
16import com.fs.starfarer.api.util.TimeoutTracker;
17import com.fs.starfarer.api.util.WeightedRandomPicker;
18
20
21 public static String getConflictId(FactionAPI a, FactionAPI b) {
22 String id1 = a.getId();
23 String id2 = b.getId();
24 if (id1.compareTo(id2) < 0) {
25 return id1 + "_" + id2;
26 }
27 return id2 + "_" + id1;
28 }
29
30 public static final String KEY = "$core_factionHostilityManager";
31
32 public static final float TIMEOUT_AFTER_ENDING = 30f;
33 public static final float CHECK_DAYS = 10f;
34 public static final float CHECK_PROB = 0.5f;
35
37 Object test = Global.getSector().getMemoryWithoutUpdate().get(KEY);
38 return (FactionHostilityManager) test;
39 }
40
42 Global.getSector().getMemoryWithoutUpdate().set(KEY, this);
43 }
44
45 public boolean areHostilitiesOngoing(FactionAPI a, FactionAPI b) {
47 return intel != null && !intel.isEnding();
48 }
49
50 public FactionHostilityIntel getHostilties(FactionAPI a, FactionAPI b) {
51 String id = getConflictId(a, b);
52 for (EveryFrameScript s : getActive()) {
54 if (intel.getId().equals(id)) {
55 return intel;
56 }
57 }
58 return null;
59 }
60
61 public List<FactionHostilityIntel> getHostilitiesInvolving(FactionAPI faction) {
62 List<FactionHostilityIntel> result = new ArrayList<FactionHostilityIntel>();
63 for (EveryFrameScript s : getActive()) {
65 if (intel.getOne() == faction || intel.getTwo() == faction) {
66 result.add(intel);
67 }
68 }
69 return result;
70 }
71
72
73 @Override
74 protected int getMinConcurrent() {
75 List<FactionAPI> factions = getEligibleFactions(true);
76
77 float mult = Global.getSettings().getFloat("minFactionHostilitiesMult");
78 return (int) Math.ceil(factions.size() * mult);
79 }
80
81 @Override
82 protected int getMaxConcurrent() {
83 List<FactionAPI> factions = getEligibleFactions(true);
84
85 float mult = Global.getSettings().getFloat("maxFactionHostilitiesMult");
86 return (int) Math.ceil(factions.size() * mult);
87 }
88
89 protected List<FactionAPI> getEligibleFactions(boolean checkNumMarkets) {
90 List<FactionAPI> factions = Global.getSector().getAllFactions();
91 List<FactionAPI> result = new ArrayList<FactionAPI>();
92 for (int i = 0; i < factions.size(); i++) {
93 FactionAPI faction = factions.get(i);
94 if (!faction.getCustom().optBoolean(Factions.CUSTOM_ENGAGES_IN_HOSTILITIES)) {
95 continue;
96 }
97
98 if (faction.isPlayerFaction()) continue;
99
100 if (checkNumMarkets) {
101 int count = Misc.getFactionMarkets(faction).size();
102 if (count <= 0) continue;
103 }
104
105 result.add(faction);
106 }
107
108 return result;
109 }
110
111
112 @Override
113 protected float getBaseInterval() {
114 return CHECK_DAYS;
115 }
116
117 public void startHostilities(String a, String b) {
118 startHostilities(Global.getSector().getFaction(a), Global.getSector().getFaction(b));
119
120 }
121 public void startHostilities(FactionAPI a, FactionAPI b) {
122 if (areHostilitiesOngoing(a, b)) return;
123
125 addActive(intel);
126 }
127
128 protected Random random = new Random();
129
130 @Override
132 if (random.nextFloat() < CHECK_PROB) return null;
133
134 Pair<FactionAPI, FactionAPI> pick = pickFactions();
135 if (pick == null) return null;
136
137 FactionHostilityIntel intel = new FactionHostilityIntel(pick.one, pick.two);
138
139 return intel;
140 }
141
142 public void notifyRecentlyEnded(String id) {
144 }
145
146 public TimeoutTracker<String> getRecentlyEnded() {
147 return recentlyEnded;
148 }
149
150 protected TimeoutTracker<String> recentlyEnded = new TimeoutTracker<String>();
151 @Override
152 public void advance(float amount) {
153 //amount *= 100f;
154 super.advance(amount);
155
156 float days = Global.getSector().getClock().convertToDays(amount);
157 recentlyEnded.advance(days);
158 }
159
160 protected Pair<FactionAPI, FactionAPI> pickFactions() {
161 List<FactionAPI> factions = new ArrayList<FactionAPI>();
162 Map<FactionAPI, Integer> marketCounts = new HashMap<FactionAPI, Integer>();
163
164 for (FactionAPI faction : getEligibleFactions(false)) {
165 int count = Misc.getFactionMarkets(faction).size();
166 if (count <= 0) continue;
167 marketCounts.put(faction, count);
168
169 factions.add(faction);
170 }
171
172 WeightedRandomPicker<FactionAPI> picker = new WeightedRandomPicker<FactionAPI>(random);
173
174 boolean weighCommissionedMore = false;
175 FactionAPI commission = Misc.getCommissionFaction();
176 if (commission != null && getHostilitiesInvolving(commission).isEmpty()) {
177 weighCommissionedMore = true;
178 }
179
180 for (FactionAPI faction : factions) {
181 float w = 1f;
182 if (commission == faction && weighCommissionedMore) {
183 w = factions.size();
184 }
185 picker.add(faction, w);
186 }
187
188 FactionAPI one = picker.pick();
189 if (one == null) return null;
190
191 picker.clear();
192 for (FactionAPI faction : factions) {
193 if (faction == one) continue;
194 if (faction.isHostileTo(one)) continue;
195 if (faction.isAtWorst(one, RepLevel.COOPERATIVE)) continue;
196
197 String id = getConflictId(one, faction);
198 if (recentlyEnded.contains(id)) continue;
199
200 float w = marketCounts.get(faction);
201 picker.add(faction, w);
202 }
203
204 FactionAPI two = picker.pick();
205 if (two == null) return null;
206
207 return new Pair<FactionAPI, FactionAPI>(one, two);
208
209 }
210
211}
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
static SettingsAPI getSettings()
Definition Global.java:51
static SectorAPI getSector()
Definition Global.java:59
List< FactionHostilityIntel > getHostilitiesInvolving(FactionAPI faction)
FactionHostilityIntel getHostilties(FactionAPI a, FactionAPI b)