Starsector API
Loading...
Searching...
No Matches
HIActionStage.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.intel.inspection;
2
3import java.awt.Color;
4import java.util.ArrayList;
5import java.util.List;
6
7import com.fs.starfarer.api.Global;
8import com.fs.starfarer.api.campaign.CampaignFleetAPI;
9import com.fs.starfarer.api.campaign.CargoAPI;
10import com.fs.starfarer.api.campaign.SectorEntityToken;
11import com.fs.starfarer.api.campaign.ai.CampaignFleetAIAPI.ActionType;
12import com.fs.starfarer.api.campaign.econ.CommoditySpecAPI;
13import com.fs.starfarer.api.campaign.econ.Industry;
14import com.fs.starfarer.api.campaign.econ.MarketAPI;
15import com.fs.starfarer.api.characters.PersonAPI;
16import com.fs.starfarer.api.impl.campaign.MilitaryResponseScript;
17import com.fs.starfarer.api.impl.campaign.MilitaryResponseScript.MilitaryResponseParams;
18import com.fs.starfarer.api.impl.campaign.command.WarSimScript;
19import com.fs.starfarer.api.impl.campaign.econ.impl.OrbitalStation;
20import com.fs.starfarer.api.impl.campaign.fleets.RouteManager;
21import com.fs.starfarer.api.impl.campaign.fleets.RouteManager.RouteData;
22import com.fs.starfarer.api.impl.campaign.fleets.RouteManager.RouteSegment;
23import com.fs.starfarer.api.impl.campaign.ids.MemFlags;
24import com.fs.starfarer.api.impl.campaign.intel.inspection.HegemonyInspectionIntel.AntiInspectionOrders;
25import com.fs.starfarer.api.impl.campaign.intel.inspection.HegemonyInspectionIntel.HegemonyInspectionOutcome;
26import com.fs.starfarer.api.impl.campaign.intel.raid.ActionStage;
27import com.fs.starfarer.api.impl.campaign.intel.raid.RaidIntel.RaidStageStatus;
28import com.fs.starfarer.api.impl.campaign.procgen.themes.BaseAssignmentAI.FleetActionDelegate;
29import com.fs.starfarer.api.impl.campaign.rulecmd.salvage.MarketCMD;
30import com.fs.starfarer.api.ui.TooltipMakerAPI;
31import com.fs.starfarer.api.util.Misc;
32
33public class HIActionStage extends ActionStage implements FleetActionDelegate {
34
35 public static float REP_PENALTY_HID_STUFF = -0.2f;
36 public static float REP_PENALTY_NORMAL = -0.1f;
37
38 protected MarketAPI target;
39 protected boolean playerTargeted = false;
40 protected List<MilitaryResponseScript> scripts = new ArrayList<MilitaryResponseScript>();
41 protected boolean gaveOrders = true; // will be set to false in updateRoutes()
42 protected float untilAutoresolve = 0f;
43
45 super(raid);
46 this.target = target;
47 playerTargeted = target.isPlayerOwned(); // I mean, it's player-targeted by nature, but still
50 if (intel.getOrders() == AntiInspectionOrders.RESIST) {
51 //untilAutoresolve = 30f;
52 untilAutoresolve = 15f + 5f * (float) Math.random();
53 }
54 }
55
56
57
58 @Override
59 public void advance(float amount) {
60 super.advance(amount);
61
62 float days = Misc.getDays(amount);
63 untilAutoresolve -= days;
64
65 if (!gaveOrders) {
66 gaveOrders = true;
67
69
70 // getMaxDays() is always 1 here
71 // scripts get removed anyway so we don't care about when they expire naturally
72 // just make sure they're around for long enough
73 float duration = 100f;
74
75 MilitaryResponseParams params = new MilitaryResponseParams(ActionType.HOSTILE,
76 "HI_" + target.getId(),
77 intel.getFaction(),
78 target.getPrimaryEntity(),
79 1f,
80 duration);
82 target.getContainingLocation().addScript(script);
83 scripts.add(script);
84
85 MilitaryResponseParams defParams = new MilitaryResponseParams(ActionType.HOSTILE,
86 "defHI_" + target.getId(),
87 target.getFaction(),
88 target.getPrimaryEntity(),
89 1f,
90 duration);
91 MilitaryResponseScript defScript = new MilitaryResponseScript(defParams);
92 target.getContainingLocation().addScript(defScript);
93 scripts.add(defScript);
94 }
95 }
96
97 protected void removeMilScripts() {
98 if (scripts != null) {
100 s.forceDone();
101 }
102 }
103 }
104
105 @Override
106 protected void updateStatus() {
107// if (true) {
108// status = RaidStageStatus.SUCCESS;
109// return;
110// }
111
112 abortIfNeededBasedOnFP(true);
113 if (status != RaidStageStatus.ONGOING) return;
114
115 boolean inSpawnRange = RouteManager.isPlayerInSpawnRange(target.getPrimaryEntity());
116 if (!inSpawnRange && untilAutoresolve <= 0){
117 autoresolve();
118 return;
119 }
120
121 if (!target.isInEconomy() || !target.isPlayerOwned()) {
122 status = RaidStageStatus.FAILURE;
124 giveReturnOrdersToStragglers(getRoutes());
125 return;
126 }
127
128 }
129
130 public String getRaidActionText(CampaignFleetAPI fleet, MarketAPI market) {
131 return "performing inspection of " + market.getName();
132 }
133
134 public String getRaidApproachText(CampaignFleetAPI fleet, MarketAPI market) {
135 return "moving to inspect " + market.getName();
136 }
137
138 public void performRaid(CampaignFleetAPI fleet, MarketAPI market) {
140
141 if (market == null) {
142 market = target;
143 }
144
146
147 status = RaidStageStatus.SUCCESS;
148 boolean hostile = market.getFaction().isHostileTo(intel.getFaction());
149 AntiInspectionOrders orders = intel.getOrders();
150
151 if (hostile || orders == AntiInspectionOrders.RESIST) {
152 //RecentUnrest.get(target).add(3, Misc.ucFirst(intel.getFaction().getPersonNamePrefix()) + " inspection");
153 //float str = HegemonyInspectionIntel.DEFAULT_INSPECTION_GROUND_STRENGTH;
154 float str = intel.getAssembleStage().getOrigSpawnFP() * 3f;
155 if (fleet != null) str = MarketCMD.getRaidStr(fleet);
156
157 float re = MarketCMD.getRaidEffectiveness(market, str);
158 MarketCMD.applyRaidStabiltyPenalty(market,
159 Misc.ucFirst(intel.getFaction().getPersonNamePrefix()) + " inspection", re);
160 Misc.setFlagWithReason(market.getMemoryWithoutUpdate(), MemFlags.RECENTLY_RAIDED,
161 intel.getFaction().getId(), true, 30f);
162 Misc.setRaidedTimestamp(market);
164 } else if (orders == AntiInspectionOrders.BRIBE) {
165 intel.setOutcome(HegemonyInspectionOutcome.BRIBED);
166 } else if (orders == AntiInspectionOrders.COMPLY) {
168 }
169
170// if (fleet != null) {
171// fleet.getMemoryWithoutUpdate().unset(MemFlags.MEMORY_KEY_RAIDER);
172// }
173
174 //if (intel.getOutcome() != null) {
175// if (intel.getOutcome() != null && status == RaidStageStatus.SUCCESS) {
176// intel.sendOutcomeUpdate();
177// }
178 if (intel.getOutcome() != null) {
179 if (status == RaidStageStatus.SUCCESS) {
180 intel.sendOutcomeUpdate();
181 } else {
183 giveReturnOrdersToStragglers(getRoutes());
184 }
185 }
186 }
187
188 protected List<String> coresRemoved = new ArrayList<String>();
189 protected void removeCoresAndApplyResult(CampaignFleetAPI fleet) {
191 AntiInspectionOrders orders = intel.getOrders();
192
193 boolean resist = orders == AntiInspectionOrders.RESIST;
194 List<String> found = removeCores(fleet, resist);
195
196 if (coresRemoved == null) coresRemoved = new ArrayList<String>();
197 coresRemoved.clear();
198 coresRemoved.addAll(found);
199 List<String> expected = intel.getExpectedCores();
200
201 int valFound = 0;
202 int valExpected = 0;
203
204 for (String id : found) {
205 CommoditySpecAPI spec = Global.getSettings().getCommoditySpec(id);
206 valFound += spec.getBasePrice();
207
208 if (fleet != null) {
209 fleet.getCargo().addCommodity(id, 1);
210 }
211 }
212 for (String id : expected) {
213 CommoditySpecAPI spec = Global.getSettings().getCommoditySpec(id);
214 valExpected += spec.getBasePrice();
215 }
216
217 if (valExpected < 30000) {
218 valExpected = 30000;
219 }
220
221 //resist = false;
222 if (!resist && valExpected > valFound * 1.25f) {
223 intel.setOutcome(HegemonyInspectionOutcome.FOUND_EVIDENCE_NO_CORES);
224 for (Industry curr : target.getIndustries()) {
225 curr.setDisrupted((intel.getRandom().nextFloat() * 45f) + 15f);
226 }
228 } else {
229 intel.setOutcome(HegemonyInspectionOutcome.CONFISCATE_CORES);
231 }
232 }
233
234 public List<String> getCoresRemoved() {
235 return coresRemoved;
236 }
237
238 protected List<String> removeCores(CampaignFleetAPI inspector, boolean resist) {
239
241 //float str = HegemonyInspectionIntel.DEFAULT_INSPECTION_GROUND_STRENGTH;
242 //float str = intel.getAssembleStage().getOrigSpawnFP() * Misc.FP_TO_GROUND_RAID_STR_APPROX_MULT;
243 float str = intel.getRaidFPAdjusted() / intel.getNumFleets() * Misc.FP_TO_GROUND_RAID_STR_APPROX_MULT;
244 if (inspector != null) str = MarketCMD.getRaidStr(inspector);
245
246 //str = 100000f;
247
248 float re = MarketCMD.getRaidEffectiveness(target, str);
249
250 List<String> result = new ArrayList<String>();
251 for (Industry curr : target.getIndustries()) {
252 String id = curr.getAICoreId();
253 if (id != null) {
254 if (resist && intel.getRandom().nextFloat() > re) continue;
255 result.add(id);
256 curr.setAICoreId(null);
257 }
258 }
259 PersonAPI admin = target.getAdmin();
260 if (admin.isAICore()) {
261 if (!resist || intel.getRandom().nextFloat() < re) {
262 result.add(admin.getAICoreId());
263 target.setAdmin(null);
264 }
265 }
266 target.reapplyIndustries();
267
268 List<String> missing = new ArrayList<String>(intel.getExpectedCores());
269 for (String id : result) {
270 missing.remove(id);
271 }
272
273 CargoAPI cargo = Misc.getStorageCargo(target);
274 if (cargo != null) {
275 for (String id : new ArrayList<String>(missing)) {
276 float qty = cargo.getCommodityQuantity(id);
277 if (qty >= 1) {
278 if (resist && intel.getRandom().nextFloat() > re) continue;
279 cargo.removeCommodity(id, 1);
280 missing.remove(id);
281 result.add(id);
282 }
283 }
284 }
285
286 cargo = Misc.getLocalResourcesCargo(target);
287 if (cargo != null) {
288 for (String id : new ArrayList<String>(missing)) {
289 float qty = cargo.getCommodityQuantity(id);
290 if (qty >= 1) {
291 if (resist && intel.getRandom().nextFloat() > re) continue;
292 cargo.removeCommodity(id, 1);
293 missing.remove(id);
294 result.add(id);
295 }
296 }
297 }
298
299
300 return result;
301 }
302
303 protected void autoresolve() {
304 float str = WarSimScript.getFactionStrength(intel.getFaction(), target.getStarSystem());
305
306 float enemyStr = WarSimScript.getEnemyStrength(intel.getFaction(), target.getStarSystem(), true);
307
308
309 boolean hostile = target.getFaction().isHostileTo(intel.getFaction());
310
311 //AntiInspectionOrders orders = ((HegemonyInspectionIntel) intel).getOrders();
312
313 //if (hostile || )
314 float defensiveStr = enemyStr + WarSimScript.getStationStrength(target.getFaction(),
315 target.getStarSystem(), target.getPrimaryEntity());
316 if (hostile && defensiveStr >= str) {
317 status = RaidStageStatus.FAILURE;
319 giveReturnOrdersToStragglers(getRoutes());
320 return;
321 }
322
323 //status = RaidStageStatus.FAILURE;
324 if (hostile) {
325 Industry station = Misc.getStationIndustry(target);
326 if (station != null) {
327 OrbitalStation.disrupt(station);
328 }
329 }
330
331 performRaid(null, target);
332
333 //removeMilScripts();
334 }
335
336 protected void updateRoutes() {
337 resetRoutes();
338
339 boolean hostile = target.getFaction().isHostileTo(intel.getFaction());
340
341 AntiInspectionOrders orders = ((HegemonyInspectionIntel)intel).getOrders();
342
343 if (!hostile && orders == AntiInspectionOrders.RESIST) {
344 ((HegemonyInspectionIntel)intel).makeHostileAndSendUpdate();
345 } else {
346 ((HegemonyInspectionIntel)intel).sendInSystemUpdate();
347 }
348
349 gaveOrders = false;
350 ((HegemonyInspectionIntel)intel).setEnteredSystem(true);
351
352 //FactionAPI faction = intel.getFaction();
353
354 List<RouteData> routes = RouteManager.getInstance().getRoutesForSource(intel.getRouteSourceId());
355
356 for (RouteData route : routes) {
357 if (target.getStarSystem() != null) { // so that fleet may spawn NOT at the target
358 route.addSegment(new RouteSegment(Math.min(5f, untilAutoresolve), target.getStarSystem().getCenter()));
359 }
360 route.addSegment(new RouteSegment(1000f, target.getPrimaryEntity()));
361 }
362 }
363
364
365 public void showStageInfo(TooltipMakerAPI info) {
366 int curr = intel.getCurrentStage();
367 int index = intel.getStageIndex(this);
368
369 Color h = Misc.getHighlightColor();
370 Color g = Misc.getGrayColor();
371 Color tc = Misc.getTextColor();
372 float pad = 3f;
373 float opad = 10f;
374
376 AntiInspectionOrders orders = intel.getOrders();
377 boolean resist = orders == AntiInspectionOrders.RESIST;
378
379 if (status == RaidStageStatus.FAILURE) {
380 if (intel.getOutcome() == HegemonyInspectionOutcome.COLONY_NO_LONGER_EXISTS) {
381 info.addPara("The inspection has been aborted.", opad);
382 } else {
383 info.addPara("The inspection task force has been defeated by the defenders of " +
384 target.getName() + ". The inspection is now over.", opad);
385 }
386 } else if (status == RaidStageStatus.SUCCESS) {
387 CargoAPI cores = Global.getFactory().createCargo(true);
388 for (String id : coresRemoved) {
389 cores.addCommodity(id, 1);
390 }
391 cores.sort();
392
393 switch (intel.getOutcome()) {
394 case BRIBED:
395 info.addPara("The funds you've allocated have been used to resolve the inspection to the " +
396 "satisfaction of all parties.", opad);
397 break;
398 case CONFISCATE_CORES:
399 if (!cores.isEmpty()) {
400 info.addPara("The inspectors have confiscated the following AI cores:", opad);
401 info.showCargo(cores, 10, true, opad);
402 } else {
403 if (resist) {
404 info.addPara("The inspectors have not been able to confiscate any AI cores.", opad);
405 } else {
406 info.addPara("The inspectors have not found any AI cores.", opad);
407 }
408 }
409 break;
410 case FOUND_EVIDENCE_NO_CORES:
411 if (!cores.isEmpty()) {
412 info.addPara("The inspectors have confiscated the following AI cores:", opad);
413 info.showCargo(cores, 10, true, opad);
414 } else {
415 info.addPara("The inspectors have not found any AI cores.", opad);
416 }
417 info.addPara("There was ample evidence of AI core use, spurring the inspectors to great zeal " +
418 "in trying to find them. Local operations have been significantly disrupted.", opad);
419 break;
420 }
421 } else if (curr == index) {
422 info.addPara("The inspection of " + target.getName() + " is currently under way.", opad);
423
424 }
425 }
426
427 public boolean canRaid(CampaignFleetAPI fleet, MarketAPI market) {
429 if (intel.getOutcome() != null) return false;
430 return market == target;
431 }
432
433 public String getRaidPrepText(CampaignFleetAPI fleet, SectorEntityToken from) {
434 return "orbiting " + from.getName();
435 }
436
437 public String getRaidInSystemText(CampaignFleetAPI fleet) {
438 return "traveling";
439 }
440
441 public String getRaidDefaultText(CampaignFleetAPI fleet) {
442 return "traveling";
443 }
444
445 @Override
446 public boolean isPlayerTargeted() {
447 return playerTargeted;
448 }
449}
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
static SettingsAPI getSettings()
Definition Global.java:51
static FactoryAPI getFactory()
Definition Global.java:35
String getRaidApproachText(CampaignFleetAPI fleet, MarketAPI market)
boolean canRaid(CampaignFleetAPI fleet, MarketAPI market)
List< String > removeCores(CampaignFleetAPI inspector, boolean resist)
HIActionStage(HegemonyInspectionIntel raid, MarketAPI target)
void performRaid(CampaignFleetAPI fleet, MarketAPI market)
String getRaidPrepText(CampaignFleetAPI fleet, SectorEntityToken from)
String getRaidActionText(CampaignFleetAPI fleet, MarketAPI market)
CargoAPI createCargo(boolean unlimitedStacks)
CommoditySpecAPI getCommoditySpec(String commodityId)