Starsector API
Loading...
Searching...
No Matches
FGBlockadeAction.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.intel.group;
2
3import java.awt.Color;
4import java.util.ArrayList;
5import java.util.Collections;
6import java.util.Comparator;
7import java.util.List;
8
9import org.lwjgl.util.vector.Vector2f;
10
11import com.fs.starfarer.api.Global;
12import com.fs.starfarer.api.campaign.CampaignFleetAPI;
13import com.fs.starfarer.api.campaign.JumpPointAPI;
14import com.fs.starfarer.api.campaign.SectorEntityToken;
15import com.fs.starfarer.api.campaign.StarSystemAPI;
16import com.fs.starfarer.api.campaign.ai.CampaignFleetAIAPI.ActionType;
17import com.fs.starfarer.api.campaign.econ.MarketAPI;
18import com.fs.starfarer.api.impl.campaign.MilitaryResponseScript;
19import com.fs.starfarer.api.impl.campaign.MilitaryResponseScript.MilitaryResponseParams;
20import com.fs.starfarer.api.impl.campaign.command.WarSimScript;
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.Factions;
24import com.fs.starfarer.api.impl.campaign.ids.MemFlags;
25import com.fs.starfarer.api.impl.campaign.ids.Tags;
26import com.fs.starfarer.api.impl.campaign.intel.group.GenericRaidFGI.GenericPayloadAction;
27import com.fs.starfarer.api.util.IntervalUtil;
28import com.fs.starfarer.api.util.Misc;
29
30public class FGBlockadeAction extends FGDurationAction implements GenericPayloadAction {
31
32 public static class FGBlockadeParams {
33 public StarSystemAPI where;
34 public MarketAPI specificMarket = null;
35 public boolean doNotGetSidetracked = true;
36 //public float duration = 365f;
37 public float accessibilityPenalty = 0.6f;
38
39 public String patrolText = null;
40 public String targetFaction = Factions.PLAYER;
41 }
42
43
44 protected IntervalUtil interval = new IntervalUtil(0.1f, 0.3f);
45
46 protected FGBlockadeParams params;
47 protected List<SectorEntityToken> blockadePoints = new ArrayList<SectorEntityToken>();
48 protected SectorEntityToken primary;
49
50 protected boolean computedInitial = false;
51 protected float origDuration = 1f;
52 protected List<MilitaryResponseScript> scripts = new ArrayList<MilitaryResponseScript>();
53
54 public FGBlockadeAction(FGBlockadeParams params, float duration) {
55 super(duration);
56 origDuration = duration;
57 this.params = params;
58
59 interval.forceIntervalElapsed();
60 }
61
62 public Object readResolve() {
63 return this;
64 }
65
66
67 public void computeInitial() {
68 computedInitial = true;
69
70 blockadePoints = new ArrayList<SectorEntityToken>(params.where.getEntities(JumpPointAPI.class));
71
72 final Vector2f center = new Vector2f();
73
74 Collections.sort(blockadePoints, new Comparator<SectorEntityToken>() {
75 public int compare(SectorEntityToken o1, SectorEntityToken o2) {
76 float d1 = Misc.getDistance(center, o1.getLocation());
77 float d2 = Misc.getDistance(center, o2.getLocation());
78 return (int) Math.signum(d1 - d2);
79 }
80 });
81
82 if (blockadePoints.size() == 1) {
83 primary = blockadePoints.get(0);
84 } else if (blockadePoints.size() >= 3) {
85 primary = blockadePoints.get(1);
86 } else if (blockadePoints.size() == 2) {
87 float d0 = Misc.getDistance(center, blockadePoints.get(0).getLocation());
88 float d1 = Misc.getDistance(center, blockadePoints.get(1).getLocation());
89 if (d0 > 3000) {
90 primary = blockadePoints.get(0);
91 } else {
92 primary = blockadePoints.get(1);
93 }
94 }
95
96 // otherwise, WasSimScript adds extra MilitaryResponseScripts for objectives and
97 // attacking fleets go there almost to the exclusion of other targets
98 for (SectorEntityToken objective : params.where.getEntitiesWithTag(Tags.OBJECTIVE)) {
99 WarSimScript.setNoFightingForObjective(objective, intel.getFaction(), 1000f);
100 }
101
102 }
103
104
105 @Override
106 public void addRouteSegment(RouteData route) {
107 RouteSegment segment = new RouteSegment(getDurDays(), params.where.getCenter());
108 route.addSegment(segment);
109 }
110
111 @Override
112 public void notifyFleetsSpawnedMidSegment(RouteSegment segment) {
113 super.notifyFleetsSpawnedMidSegment(segment);
114 }
115
116 @Override
117 public void notifySegmentFinished(RouteSegment segment) {
118 super.notifySegmentFinished(segment);
119
120 //autoresolve();
121 }
122
123 @Override
124 public void setActionFinished(boolean finished) {
125 if (finished && !this.finished) {
126 List<CampaignFleetAPI> fleets = intel.getFleets();
127 for (CampaignFleetAPI fleet : fleets) {
128 Misc.setFlagWithReason(fleet.getMemoryWithoutUpdate(), MemFlags.FLEET_BUSY, fleet.getId(), true, -1f);
129 }
130
131 if (scripts != null) {
133 s.forceDone();
134 }
135 scripts.clear();
136 }
137
138 for (SectorEntityToken objective : params.where.getEntitiesWithTag(Tags.OBJECTIVE)) {
139 WarSimScript.removeNoFightingTimeoutForObjective(objective, intel.getFaction());
140 }
141 }
142 super.setActionFinished(finished);
143 }
144
145
146 @Override
147 public void directFleets(float amount) {
148 super.directFleets(amount);
149 if (isActionFinished()) return;
150
151 List<CampaignFleetAPI> fleets = intel.getFleets();
152 if (fleets.isEmpty()) {
153 setActionFinished(true);
154 return;
155 }
156
157 if (!computedInitial) {
160 }
161
162 if (primary == null) {
163 setActionFinished(true);
164 return;
165 }
166
167 float days = Global.getSector().getClock().convertToDays(amount);
168
169 interval.advance(days);
170 if (!interval.intervalElapsed()) return;
171
172 // doing this would cause the fleets to:
173 // 1) not hassle the player
174 // 2) not attack nearby enemies unless they bump into them
175
176// for (CampaignFleetAPI fleet : fleets) {
177// if (params.doNotGetSidetracked) {
178// boolean battleNear = false;
179// for (CampaignFleetAPI other : fleets) {
180// if (other == fleet || other.getBattle() == null) continue;
181// if (other.getContainingLocation() != fleet.getContainingLocation());
182// float dist = Misc.getDistance(fleet, other);
183// if (dist < 1000) {
184// CampaignFleetAIAPI ai = fleet.getAI();
185// if (ai != null && ai.wantsToJoin(other.getBattle(), other.getBattle().isPlayerInvolved())) {
186// battleNear = true;
187// break;
188// }
189// }
190// }
191// if (!battleNear) {
192// fleet.getMemoryWithoutUpdate().set(MemFlags.MEMORY_KEY_FLEET_DO_NOT_GET_SIDETRACKED, true, 0.4f);
193// }
194// }
195// }
196 }
197
198
199 protected void orderFleetMovements() {
200 if (blockadePoints.isEmpty() || primary == null) {
201 return;
202 }
203
204 float fPrimary = 1f;
205 float fNonPrimary = 1f / blockadePoints.size();
206
207 for (CampaignFleetAPI fleet : intel.getFleets()) {
208 fleet.getMemoryWithoutUpdate().unset(MemFlags.FLEET_BUSY);
209 }
210
211
212 for (SectorEntityToken target : blockadePoints) {
213 float rf = fNonPrimary;
214 if (target == primary) rf = fPrimary;
215 MilitaryResponseParams aggroParams = new MilitaryResponseParams(ActionType.HOSTILE,
216 "blockade_" + target.getId(),
218 target,
219 rf,
220 getDurDays());
221 aggroParams.travelText = null;
222 aggroParams.actionText = params.patrolText;
223
224 MilitaryResponseScript script = new MilitaryResponseScript(aggroParams);
225 params.where.addScript(script);
226 scripts.add(script);
227 }
228 }
229
230 public FGBlockadeParams getParams() {
231 return params;
232 }
233
234
235
236 public float getSuccessFraction() {
237 float f = getElapsed() / Math.max(1f, origDuration);
238 if (f < 0f) f = 0f;
239 if (f > 1f) f = 1f;
240 return f;
241 }
242
244 return Global.getSector().getFaction(params.targetFaction).getBaseUIColor();
245 }
246
247 public StarSystemAPI getWhere() {
248 return params.where;
249 }
250
251 public List<SectorEntityToken> getBlockadePoints() {
252 return blockadePoints;
253 }
254
255 public SectorEntityToken getPrimary() {
256 return primary;
257 }
258
259
260
261}
262
263
264
265
266
267
268
269
static SectorAPI getSector()
Definition Global.java:59