Starsector API
Loading...
Searching...
No Matches
GenericProbeEntityPlugin.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.entities;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import java.awt.Color;
7
8import org.lwjgl.util.vector.Vector2f;
9
10import com.fs.starfarer.api.Global;
11import com.fs.starfarer.api.Script;
12import com.fs.starfarer.api.campaign.CampaignEngineLayers;
13import com.fs.starfarer.api.campaign.SectorEntityToken;
14import com.fs.starfarer.api.campaign.SectorEntityToken.VisibilityLevel;
15import com.fs.starfarer.api.combat.ViewportAPI;
16import com.fs.starfarer.api.impl.campaign.BaseCustomEntityPlugin;
17import com.fs.starfarer.api.impl.campaign.ids.Pings;
18import com.fs.starfarer.api.impl.campaign.ids.Tags;
19import com.fs.starfarer.api.util.CampaignEngineGlowIndividualEngine;
20import com.fs.starfarer.api.util.CampaignEngineGlowUtil;
21import com.fs.starfarer.api.util.CampaignEntityMovementUtil;
22import com.fs.starfarer.api.util.Misc;
23
38public class GenericProbeEntityPlugin extends BaseCustomEntityPlugin { // implements EngineGlowControls {
39
40 public static enum ProbeActionType {
41 TRAVEL,
42 ASSUME_ORBIT,
43 EMIT_PING,
44 PERFORM_ACTION,
45 STOP,
46 WAIT,
47 }
48
49 public static class GenericProbeParams {
50 public float maxBurn = 20f;
51 public float burnAccel = 5f;
52 public float maxTurnRate = 120f;
53 public float turnAccel = 120f;
54
55 public Color fringe = new Color(255, 100, 0, 255);
56 public Color flame = new Color(255, 165, 100, 255);
57 public Color core = new Color(255, 255, 255, 255);
58 public float engineGlowShiftRate = 2f;
59 public float engineGlowLength = 100f;
60 public float engineGlowWidth = 15f;
61 public float engineGlowGlowSize = 100f;
62 public float engineGlowTexSpanMult = 0.1f;
63
64 protected List<ProbeAction> actions = new ArrayList<>();
65
66 public void travelTo(Vector2f location) {
67 ProbeAction a = new ProbeAction(ProbeActionType.TRAVEL);
68 a.location = location;
69 actions.add(a);
70 }
71 public void travelTo(SectorEntityToken target) {
72 ProbeAction a = new ProbeAction(ProbeActionType.TRAVEL);
73 a.target = target;
74 actions.add(a);
75 }
76 public void travelInDir(float dir, float duration) {
77 ProbeAction a = new ProbeAction(ProbeActionType.TRAVEL);
78 a.dir = dir;
79 a.duration = duration;
80 actions.add(a);
81 }
82
83 public void assumeOrbit(SectorEntityToken target, float radius, float duration) {
84 ProbeAction a = new ProbeAction(ProbeActionType.ASSUME_ORBIT);
85 a.target = target;
86 a.radius = radius;
87 a.duration = duration;
88 actions.add(a);
89 }
90
91 public void emitPing(String pingId) {
92 ProbeAction a = new ProbeAction(ProbeActionType.EMIT_PING);
93 a.pingId = pingId;
94 actions.add(a);
95 }
96 public void performAction(Script action) {
97 ProbeAction a = new ProbeAction(ProbeActionType.PERFORM_ACTION);
98 a.action = action;
99 actions.add(a);
100 }
101 public void wait(float duration) {
102 ProbeAction a = new ProbeAction(ProbeActionType.WAIT);
103 a.duration = duration;
104 actions.add(a);
105 }
106 public void stop(float duration) {
107 ProbeAction a = new ProbeAction(ProbeActionType.STOP);
108 a.duration = duration;
109 actions.add(a);
110 }
111 }
112
113
114 public static class ProbeAction {
115 public ProbeActionType type;
116 public SectorEntityToken target;
117 public Vector2f location;
118 public float dir;
119 public float radius;
120 public float duration;
121 public String pingId;
122 public Script action;
123 public ProbeAction(ProbeActionType type) {
124 this.type = type;
125 }
126
127 }
128
129 protected GenericProbeParams params;
132
133 public void init(SectorEntityToken entity, Object pluginParams) {
134 super.init(entity, pluginParams);
135 this.params = (GenericProbeParams) pluginParams;
136 readResolve();
137 }
138
139 Object readResolve() {
140 if (engineGlow == null) {
141 engineGlow = new CampaignEngineGlowUtil(entity, params.fringe, params.core, params.flame,
142 params.engineGlowShiftRate);
144 90f, params.engineGlowLength, params.engineGlowWidth, params.engineGlowGlowSize,
145 new Vector2f(-10f, 0f), engineGlow);
146 engine.setFlameTexSpanMult(params.engineGlowTexSpanMult);
147 engineGlow.addEngine(engine);
148 }
149
150 if (movement == null) {
151 float maxSpeed = Misc.getSpeedForBurnLevel(params.maxBurn);
152 float accel = Misc.getSpeedForBurnLevel(params.burnAccel);
153 movement = new CampaignEntityMovementUtil(entity, params.turnAccel, params.maxBurn, accel, maxSpeed);
155 }
156
157 return this;
158 }
159
161 return movement;
162 }
163
165 return engineGlow;
166 }
167
168 public void advance(float amount) {
170 engineGlow.advance(amount);
171 }
172
173 if (entity.hasTag(Tags.FADING_OUT_AND_EXPIRING) || params.actions.isEmpty()) {
176 return;
177 }
178
179 ProbeAction curr = params.actions.get(0);
180 ProbeAction next = null;
181 if (params.actions.size() > 1) next = params.actions.get(1);
182
183 if (curr.type == ProbeActionType.TRAVEL) {
184 if (curr.location == null && curr.target == null) {
185 movement.moveInDirection(curr.dir);
186 curr.duration -= amount;
187 if (curr.duration <= 0) {
188 params.actions.remove(0);
189 }
190 } else {
191 Vector2f loc = curr.location;
192 if (loc == null) loc = curr.target.getLocation();
193 float dist = Misc.getDistance(entity.getLocation(), loc);
194
195 if (next != null && next.type == ProbeActionType.ASSUME_ORBIT) {
196 float orbitAngle = Misc.getAngleInDegrees(next.target.getLocation(), entity.getLocation());
197 Vector2f away = Misc.getUnitVectorAtDegreeAngle(orbitAngle);
198 away.scale(next.target.getRadius() + next.radius * 0.8f);
199 loc = Vector2f.add(loc, away, new Vector2f());
200 }
202
203 float checkDist = 100f + entity.getRadius();
204 if (curr.target != null) checkDist += curr.target.getRadius();
205 if (next != null && next.type == ProbeActionType.ASSUME_ORBIT) {
206 checkDist -= 100f;
207 checkDist += next.radius;
208 }
209 if (dist < checkDist) {
210 params.actions.remove(0);
211 }
212 }
213
214 } else if (curr.type == ProbeActionType.ASSUME_ORBIT) {
215 if (entity.getOrbit() == null || entity.getOrbitFocus() != curr.target) {
217 float orbitAngle = Misc.getAngleInDegrees(curr.target.getLocation(), entity.getLocation());
218 float orbitRadius = Misc.getDistance(entity.getLocation(), curr.target.getLocation());
219 float orbitDays = orbitRadius / (10f + (float) Math.random() * 5f);
220 orbitDays *= 0.2f;
221 entity.setCircularOrbit(curr.target, orbitAngle, orbitRadius, orbitDays);
222 }
223 curr.duration -= amount;
224 if (curr.duration <= 0) {
225 params.actions.remove(0);
226 }
227 } else if (curr.type == ProbeActionType.EMIT_PING) {
228 VisibilityLevel level = entity.getVisibilityLevelToPlayerFleet();
229 if (level != VisibilityLevel.NONE) {
231 }
232 params.actions.remove(0);
233 } else if (curr.type == ProbeActionType.STOP || curr.type == ProbeActionType.WAIT) {
234 if (curr.type == ProbeActionType.STOP) {
235 movement.stop();
236 }
237 curr.duration -= amount;
238 if (curr.duration <= 0) {
239 params.actions.remove(0);
240 }
241 } else if (curr.type == ProbeActionType.PERFORM_ACTION) {
242 curr.action.run();
243 params.actions.remove(0);
244 }
245
246 movement.advance(amount);
247
248 }
249
250 public void render(CampaignEngineLayers layer, ViewportAPI viewport) {
251 float alphaMult = viewport.getAlphaMult();
252 alphaMult *= entity.getSensorFaderBrightness();
254 if (alphaMult <= 0f) return;
255
256 engineGlow.render(alphaMult);
257 }
258
259 public float getRenderRange() {
260 return entity.getRadius() + 1000f; // for engine glow/trails
261 }
262
263
264
265}
266
267
268
269
270
271
272
273
static SectorAPI getSector()
Definition Global.java:65
void render(CampaignEngineLayers layer, ViewportAPI viewport)
static final String FADING_OUT_AND_EXPIRING
Definition Tags.java:341
void addEngine(CampaignEngineGlowIndividualEngine engine)
static Vector2f getUnitVectorAtDegreeAngle(float degrees)
Definition Misc.java:1196
static void fadeAndExpire(SectorEntityToken entity)
Definition Misc.java:3133
static float getDistance(SectorEntityToken from, SectorEntityToken to)
Definition Misc.java:599
static float getSpeedForBurnLevel(float burnLevel)
Definition Misc.java:1671
static float getAngleInDegrees(Vector2f v)
Definition Misc.java:1126
EveryFrameScript addPing(SectorEntityToken entity, String pingType)
void setCircularOrbit(SectorEntityToken focus, float angle, float orbitRadius, float orbitDays)