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
21 protected StarSystemAPI system;
22 protected CampaignFleetAPI fleet;
24
25
26 public DisposableAggroAssignmentAI(CampaignFleetAPI fleet, StarSystemAPI system,
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
49 SectorEntityToken from = Misc.getSourceEntity(fleet);
50 if (from != null && system != null) {
51 float angle = Misc.getAngleInDegrees(system.getLocation(), from.getLocationInHyperspace());
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
60 loc = Misc.pickHyperLocationNotNearPlayer(new Vector2f(loc), Global.getSettings().getMaxSensorRangeHyper() + 500f);
61
62 fleet.setLocation(loc.x, loc.y);
63 } else {
64 Vector2f loc = Misc.pickHyperLocationNotNearPlayer(system.getLocation(), Global.getSettings().getMaxSensorRangeHyper() + 500f);
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();
72 loc = Misc.pickLocationNotNearPlayer(system, from,
74 } else { // no jump points case; ???
75 loc = Misc.pickLocationNotNearPlayer(system,
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);
87 LocationAPI hyper = Global.getSector().getHyperspace();
88 SectorEntityToken token = hyper.createToken(dest.x, dest.y);
89 fleet.addAssignment(FleetAssignment.GO_TO_LOCATION, token, 1000,
91 }
92
93 if (fleet.isInHyperspace() && (float) Math.random() < probStayInHyper) {
94 fleet.addAssignment(FleetAssignment.RAID_SYSTEM, system.getHyperspaceAnchor(), 10000,
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();
104 fleet.addAssignment(FleetAssignment.PATROL_SYSTEM, target, days,
106 return;
107 } else {
108 float days = 5f + 5f * (float) Math.random();
109 fleet.addAssignment(FleetAssignment.PATROL_SYSTEM, null, days,
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
129 public static SectorEntityToken pickEntityToGuard(Random random, StarSystemAPI system, CampaignFleetAPI fleet) {
130 WeightedRandomPicker<SectorEntityToken> picker = new WeightedRandomPicker<SectorEntityToken>(random);
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
145 for (MarketAPI market : Global.getSector().getEconomy().getMarkets(system)) {
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
155 public static float getWeightMultForPatrols(StarSystemAPI system, CampaignFleetAPI fleet, SectorEntityToken entity) {
156 float count = countNearbyPatrols(system, fleet, entity);
157 return 1f / (count * 10f + 1f);
158 }
159
160 public static float countNearbyPatrols(StarSystemAPI system, CampaignFleetAPI fleet, SectorEntityToken entity) {
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:51
static SectorAPI getSector()
Definition Global.java:59
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)