Starsector API
Loading...
Searching...
No Matches
DisposableAggroAssignmentAI.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.fleets;
2
3import java.util.Random;
4
5import org.lwjgl.util.vector.Vector2f;
6
7import com.fs.starfarer.api.EveryFrameScript;
8import com.fs.starfarer.api.Global;
9import com.fs.starfarer.api.campaign.CampaignFleetAPI;
10import com.fs.starfarer.api.campaign.FleetAssignment;
11import com.fs.starfarer.api.campaign.LocationAPI;
12import com.fs.starfarer.api.campaign.PlanetAPI;
13import com.fs.starfarer.api.campaign.SectorEntityToken;
14import com.fs.starfarer.api.campaign.StarSystemAPI;
15import com.fs.starfarer.api.campaign.econ.MarketAPI;
16import com.fs.starfarer.api.util.Misc;
17import com.fs.starfarer.api.util.WeightedRandomPicker;
18
20
24
25
27 DisposableFleetManager manager, float probStayInHyper) {
28 this.fleet = fleet;
29 this.system = system;
30 this.manager = manager;
31
32 giveInitialAssignments(probStayInHyper);
33 }
34
35 protected void giveInitialAssignments(float probStayInHyper) {
36 boolean playerInSameLocation = fleet.getContainingLocation() == Global.getSector().getCurrentLocation();
37
38 SectorEntityToken target = null;
39 if (!playerInSameLocation && !fleet.isInHyperspace()) { // in system, player is in hyper
40 target = pickEntityToGuard(new Random(), system, fleet);
41 if (target != null) {
42 Vector2f loc = Misc.getPointAtRadius(target.getLocation(), target.getRadius() + 100f);
43 fleet.setLocation(loc.x, loc.y);
44 } else {
45 Vector2f loc = Misc.getPointAtRadius(new Vector2f(), 5000f);
46 fleet.setLocation(loc.x, loc.y);
47 }
48 } else if (fleet.isInHyperspace()) { // fleet in hyper; don't care about player being there or not
50 if (from != null && system != null) {
52 float arc = 90f;
53 angle = angle - arc / 2f + arc * (float) Math.random();
54 //float dist = Global.getSettings().getMaxSensorRangeHyper() + 500f + 1000f * (float) Math.random();
55 float dist = Global.getSettings().getMaxSensorRangeHyper() + 1000f;
56 Vector2f loc = Misc.getUnitVectorAtDegreeAngle(angle);
57 loc.scale(dist);
58 Vector2f.add(loc, system.getLocation(), loc);
59
61
62 fleet.setLocation(loc.x, loc.y);
63 } else {
65 fleet.setLocation(loc.x, loc.y);
66 }
67 } else { // player in same location, and in-system
68 target = pickEntityToGuard(new Random(), system, fleet);
69 Vector2f loc = new Vector2f(5000, 0);
70 if (target != null) {
71 Vector2f from = target.getLocation();
74 } else { // no jump points case; ???
76 Misc.getPointAtRadius(new Vector2f(0, 0), 8000),
78 }
79 fleet.setLocation(loc.x, loc.y);
80 }
81 pickNext(target, probStayInHyper);
82 }
83
84 protected void pickNext(SectorEntityToken target, float probStayInHyper) {
85 if (fleet.isInHyperspace()) {
86 Vector2f dest = Misc.getPointAtRadius(system.getLocation(), 1000);
88 SectorEntityToken token = hyper.createToken(dest.x, dest.y);
91 }
92
93 if (fleet.isInHyperspace() && (float) Math.random() < probStayInHyper) {
96 } else {
97 if (target == null) target = pickEntityToGuard(new Random(), system, fleet);
98 if (target != null) {
99 float speed = Misc.getSpeedForBurnLevel(8);
100 float dist = Misc.getDistance(fleet.getLocation(), target.getLocation());
101 float seconds = dist / speed;
102 float days = seconds / Global.getSector().getClock().getSecondsPerDay();
103 days += 30f + 10f * (float) Math.random();
106 return;
107 } else {
108 float days = 5f + 5f * (float) Math.random();
111 }
112 }
113 }
114
115 public void advance(float amount) {
116 if (fleet.getCurrentAssignment() == null) {
117 pickNext(null, 0f);
118 }
119 }
120
121 public boolean isDone() {
122 return false;
123 }
124
125 public boolean runWhilePaused() {
126 return false;
127 }
128
131
132 for (SectorEntityToken entity : system.getJumpPoints()) {
133 float mult = getWeightMultForPatrols(system, fleet, entity);
134 picker.add(entity, 5f * mult);
135 }
136
137 // gas giants are exits from hyperspace, so hang around there too
138 for (PlanetAPI planet : system.getPlanets()) {
139 if (planet.isGasGiant()) {
140 float mult = getWeightMultForPatrols(system, fleet, planet);
141 picker.add(planet, 5f * mult);
142 }
143 }
144
146 if (market.getFaction() != fleet.getFaction()) continue;
147
148 float mult = getWeightMultForPatrols(system, fleet, market.getPrimaryEntity());
149 picker.add(market.getPrimaryEntity(), 5f * mult);
150 }
151
152 return picker.pick();
153 }
154
156 float count = countNearbyPatrols(system, fleet, entity);
157 return 1f / (count * 10f + 1f);
158 }
159
161 float count = 0;
162 for (CampaignFleetAPI other : system.getFleets()) {
163 float dist = Misc.getDistance(other, entity);
164 if (dist > 3000) continue;
165
166 if (!Misc.isPatrol(other)) continue;
167
168 if (fleet.isHostileTo(other)) {
169 count++;
170 }
171 }
172 return count;
173 }
174
175}
176
177
178
179
180
181
182
183
184
185
static SettingsAPI getSettings()
Definition Global.java:57
static SectorAPI getSector()
Definition Global.java:65
static float countNearbyPatrols(StarSystemAPI system, CampaignFleetAPI fleet, SectorEntityToken entity)
static SectorEntityToken pickEntityToGuard(Random random, StarSystemAPI system, CampaignFleetAPI fleet)
DisposableAggroAssignmentAI(CampaignFleetAPI fleet, StarSystemAPI system, DisposableFleetManager manager, float probStayInHyper)
static float getWeightMultForPatrols(StarSystemAPI system, CampaignFleetAPI fleet, SectorEntityToken entity)
String getActionOutsideText(StarSystemAPI system, CampaignFleetAPI fleet)
String getActionInsideText(StarSystemAPI system, CampaignFleetAPI fleet)
String getTravelText(StarSystemAPI system, CampaignFleetAPI fleet)
static boolean isPatrol(CampaignFleetAPI fleet)
Definition Misc.java:4761
static Vector2f getUnitVectorAtDegreeAngle(float degrees)
Definition Misc.java:1196
static Vector2f pickLocationNotNearPlayer(LocationAPI where, Vector2f from, float minDist)
Definition Misc.java:2210
static Vector2f pickHyperLocationNotNearPlayer(Vector2f from, float minDist)
Definition Misc.java:2193
static float getDistance(SectorEntityToken from, SectorEntityToken to)
Definition Misc.java:599
static SectorEntityToken getSourceEntity(CampaignFleetAPI fleet)
Definition Misc.java:2169
static Vector2f getPointAtRadius(Vector2f from, float r)
Definition Misc.java:697
static float getSpeedForBurnLevel(float burnLevel)
Definition Misc.java:1671
static float getAngleInDegrees(Vector2f v)
Definition Misc.java:1126
void addAssignment(FleetAssignment assignment, SectorEntityToken target, float maxDurationInDays)
boolean isHostileTo(SectorEntityToken other)
FleetAssignmentDataAPI getCurrentAssignment()
List< SectorEntityToken > getJumpPoints()
SectorEntityToken createToken(float x, float y)
List< CampaignFleetAPI > getFleets()
List< MarketAPI > getMarkets(LocationAPI loc)