Starsector API
Loading...
Searching...
No Matches
MultiBlueprintItemPlugin.java
Go to the documentation of this file.
1package com.fs.starfarer.api.campaign.impl.items;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.Comparator;
6import java.util.Iterator;
7import java.util.LinkedHashSet;
8import java.util.List;
9import java.util.Set;
10
11import java.awt.Color;
12
13import com.fs.starfarer.api.Global;
14import com.fs.starfarer.api.campaign.CargoStackAPI;
15import com.fs.starfarer.api.campaign.CargoTransferHandlerAPI;
16import com.fs.starfarer.api.campaign.FactionAPI;
17import com.fs.starfarer.api.campaign.econ.MarketAPI;
18import com.fs.starfarer.api.campaign.econ.SubmarketAPI;
19import com.fs.starfarer.api.combat.ShipHullSpecAPI;
20import com.fs.starfarer.api.graphics.SpriteAPI;
21import com.fs.starfarer.api.loading.FighterWingSpecAPI;
22import com.fs.starfarer.api.loading.WeaponSpecAPI;
23import com.fs.starfarer.api.ui.TooltipMakerAPI;
24import com.fs.starfarer.api.util.Misc;
25
27
28
29 protected Set<String> tags = new LinkedHashSet<String>();
30 @Override
31 public void init(CargoStackAPI stack) {
32 super.init(stack);
33
34 String param = spec.getParams();
35 if (!param.isEmpty()) {
36 for (String tag : param.split(",")) {
37 tag = tag.trim();
38 if (tag.isEmpty()) continue;
39 tags.add(tag);
40 }
41 }
42 }
43
44 transient protected List<String> cachedFighters = null;
45 transient protected List<String> cachedShips = null;
46 transient protected List<String> cachedWeapons= null;
47
48 public List<String> getProvidedFighters() {
49 if (cachedFighters == null) {
51 }
52 return cachedFighters;
53 }
54
55 public List<String> getProvidedShips() {
56 if (cachedShips == null) {
58 }
59 return cachedShips;
60 }
61
62 public List<String> getProvidedWeapons() {
63 if (cachedWeapons == null) {
65 }
66 return cachedWeapons;
67 }
68 public List<String> getProvidedIndustries() {
69 return null;
70 }
71
72 @Override
73 public void render(float x, float y, float w, float h, float alphaMult,
74 float glowMult, SpecialItemRendererAPI renderer) {
75
76 SpriteAPI sprite = Global.getSettings().getSprite("blueprint_packages", getId(), true);
77 if (sprite.getTextureId() == 0) return; // no texture for a "holo", so no custom rendering
78
79
80 float cx = x + w/2f;
81 float cy = y + h/2f;
82
83 w = 40;
84 h = 40;
85
86 float p = 1;
87 float blX = cx - 12f - p;
88 float blY = cy - 22f - p;
89 float tlX = cx - 26f - p;
90 float tlY = cy + 19f + p;
91 float trX = cx + 20f + p;
92 float trY = cy + 24f + p;
93 float brX = cx + 34f + p;
94 float brY = cy - 9f - p;
95
96 List<String> ships = getProvidedShips();
97 List<String> weapons = getProvidedWeapons();
98 List<String> fighters = getProvidedFighters();
99 boolean known = areAllKnown(ships, weapons, fighters);
100
101 float mult = 1f;
102
103 sprite.setAlphaMult(alphaMult * mult);
104 sprite.setNormalBlend();
105 sprite.renderWithCorners(blX, blY, tlX, tlY, trX, trY, brX, brY);
106
107 if (glowMult > 0) {
108 sprite.setAlphaMult(alphaMult * glowMult * 0.5f * mult);
109 sprite.setAdditiveBlend();
110 sprite.renderWithCorners(blX, blY, tlX, tlY, trX, trY, brX, brY);
111 }
112
113 if (known) {
114 renderer.renderBGWithCorners(Color.black, blX, blY, tlX, tlY, trX, trY, brX, brY,
115 alphaMult * 0.5f, 0f, false);
116 }
117
118 renderer.renderScanlinesWithCorners(blX, blY, tlX, tlY, trX, trY, brX, brY, alphaMult, false);
119 }
120
121 @Override
122 public int getPrice(MarketAPI market, SubmarketAPI submarket) {
123 return super.getPrice(market, submarket);
124 }
125
126 @Override
127 public String getName() {
128 return super.getName();
129 }
130
131 @Override
132 public String getDesignType() {
133 return null;
134 }
135
136 @Override
137 public void createTooltip(TooltipMakerAPI tooltip, boolean expanded, CargoTransferHandlerAPI transferHandler, Object stackSource) {
138 super.createTooltip(tooltip, expanded, transferHandler, stackSource);
139
140 float pad = 3f;
141 float opad = 10f;
142 float small = 5f;
143 Color h = Misc.getHighlightColor();
144 Color g = Misc.getGrayColor();
145 Color b = Misc.getButtonTextColor();
147
148 List<String> ships = getProvidedShips();
149 List<String> weapons = getProvidedWeapons();
150 List<String> fighters = getProvidedFighters();
151
152 float maxTotal = 21;
153 int minPer = 3;
154
155 float shipWeight = 0f;
156 float weaponWeight = 0f;
157 float fighterWeight = 0f;
158
160
161 float knownWeight = 0.25f;
162 for (String id : ships) {
163 if (pf.knowsShip(id)) {
164 shipWeight += knownWeight;
165 } else {
166 shipWeight += 1f;
167 }
168 }
169 for (String id : weapons) {
170 if (pf.knowsWeapon(id)) {
171 weaponWeight += knownWeight;
172 } else {
173 weaponWeight += 1f;
174 }
175 }
176 for (String id : fighters) {
177 if (pf.knowsFighter(id)) {
178 fighterWeight += knownWeight;
179 } else {
180 fighterWeight += 1f;
181 }
182 }
183
184 float totalWeight = shipWeight + weaponWeight + fighterWeight;
185 if (totalWeight < knownWeight) totalWeight = knownWeight;
186
187 int maxShips = (int) Math.max(minPer, (shipWeight / totalWeight) * maxTotal);
188 int maxWeapons = (int) Math.max(minPer, (weaponWeight / totalWeight) * maxTotal);
189 int maxFighters = (int) Math.max(minPer, (fighterWeight / totalWeight) * maxTotal);
190
191
192 if (!ships.isEmpty()) {
193 addShipList(tooltip, "Ship hulls:", ships, maxShips, opad);
194 }
195 if (!weapons.isEmpty()) {
196 addWeaponList(tooltip, "Weapons:", weapons, maxWeapons, opad);
197 }
198 if (!fighters.isEmpty()) {
199 addFighterList(tooltip, "Fighters:", fighters, maxFighters, opad);
200 }
201
202
203
204 addCostLabel(tooltip, opad, transferHandler, stackSource);
205
207 boolean known = areAllKnown(ships, weapons, fighters);
208 if (known) {
209 tooltip.addPara("All blueprints in package already known", g, opad);
210 } else {
211 tooltip.addPara("Right-click to learn", b, opad);
212 }
213 }
214 }
215
216 protected boolean areAllKnown(List<String> ships, List<String> weapons, List<String> fighters) {
217
218 for (String hullId : ships) {
219 if (!Global.getSector().getPlayerFaction().knowsShip(hullId)) {
220 return false;
221 }
222 }
223 for (String weaponId : weapons) {
224 if (!Global.getSector().getPlayerFaction().knowsWeapon(weaponId)) {
225 return false;
226 }
227 }
228 for (String wingId : fighters) {
230 return false;
231 }
232 }
233 return true;
234 }
235
236 @Override
237 public float getTooltipWidth() {
238 return super.getTooltipWidth();
239 }
240
241 @Override
242 public boolean isTooltipExpandable() {
243 return false;
244 }
245
246 @Override
247 public boolean hasRightClickAction() {
248 return true;
249 }
250
251 @Override
253 List<String> ships = getProvidedShips();
254 List<String> weapons = getProvidedWeapons();
255 List<String> fighters = getProvidedFighters();
256 return !areAllKnown(ships, weapons, fighters);
257 }
258
259 @Override
261 List<String> ships = getProvidedShips();
262 List<String> weapons = getProvidedWeapons();
263 List<String> fighters = getProvidedFighters();
264
265 if (areAllKnown(ships, weapons, fighters)) {
267 "All blueprints in package already known");//,
268 } else {
269 Global.getSoundPlayer().playUISound("ui_acquired_blueprint", 1, 1);
271 int sCount = 0;
272 int wCount = 0;
273 int fCount = 0;
274 for (String id : ships) {
275 if (!pf.knowsShip(id)) sCount++;
276 pf.addKnownShip(id, true);
277 }
278 for (String id : weapons) {
279 if (!pf.knowsWeapon(id)) wCount++;
280 pf.addKnownWeapon(id, true);
281 }
282 for (String id : fighters) {
283 if (!pf.knowsFighter(id)) fCount++;
284 pf.addKnownFighter(id, true);
285 }
286
287 if (sCount > 0) {
289 "Acquired " + sCount + " ship blueprints", Misc.getTooltipTitleAndLightHighlightColor(),
290 "" + sCount, Misc.getHighlightColor());
291 }
292 if (wCount > 0) {
294 "Acquired " + wCount + " weapon blueprints", Misc.getTooltipTitleAndLightHighlightColor(),
295 "" + wCount, Misc.getHighlightColor());
296 }
297 if (fCount > 0) {
299 "Acquired " + fCount + " fighter blueprints", Misc.getTooltipTitleAndLightHighlightColor(),
300 "" + fCount, Misc.getHighlightColor());
301 }
302
303 }
304 }
305
306 public static List<String> getWeaponIds(Set<String> tags) {
307 List<WeaponSpecAPI> specs = Global.getSettings().getAllWeaponSpecs();
308
309 Iterator<WeaponSpecAPI> iter = specs.iterator();
310// while (iter.hasNext()) {
311// WeaponSpecAPI curr = iter.next();
312// if (curr.getAIHints().contains(AIHints.SYSTEM)) {
313// iter.remove();
314// }
315// }
316
317 if (!tags.isEmpty()) {
318 iter = specs.iterator();
319 while (iter.hasNext()) {
320 WeaponSpecAPI curr = iter.next();
321 for (String tag : tags) {
322 boolean not = tag.startsWith("!");
323 tag = not ? tag.substring(1) : tag;
324 boolean has = curr.hasTag(tag);
325 if (not == has) {
326 iter.remove();
327 break;
328 }
329 }
330 }
331 }
332
333 Collections.sort(specs, new Comparator<WeaponSpecAPI>() {
334 public int compare(WeaponSpecAPI o1, WeaponSpecAPI o2) {
335 return o2.getSize().ordinal() - o1.getSize().ordinal();
336 }
337 });
338
339 List<String> result = new ArrayList<String>();
340 for (WeaponSpecAPI spec : specs) {
341 result.add(spec.getWeaponId());
342 }
343 return result;
344 }
345
346
347 public static List<String> getShipIds(Set<String> tags) {
348 List<ShipHullSpecAPI> specs = Global.getSettings().getAllShipHullSpecs();
349
350 Iterator<ShipHullSpecAPI> iter = specs.iterator();
351
352 if (!tags.isEmpty()) {
353 iter = specs.iterator();
354 while (iter.hasNext()) {
355 ShipHullSpecAPI curr = iter.next();
356// if (tags.contains("pirate_bp") && curr.getHullId().equals("mule_d_pirates")) {
357// System.out.println(curr.getHullId());
358// }
359// if (!curr.isBaseHull()) {
360// iter.remove();
361// continue;
362// }
363
364// if (curr.hasTag("rare_bp")) {
365// System.out.println("\"" + curr.getHullId() + "\",");
366// }
367
368 for (String tag : tags) {
369 boolean not = tag.startsWith("!");
370 tag = not ? tag.substring(1) : tag;
371 boolean has = curr.hasTag(tag);
372 if (not == has) {
373 iter.remove();
374 break;
375 }
376 }
377 }
378 } else {
379 specs.clear();
380 }
381
382 Collections.sort(specs, new Comparator<ShipHullSpecAPI>() {
383 public int compare(ShipHullSpecAPI o1, ShipHullSpecAPI o2) {
384 return o2.getHullSize().ordinal() - o1.getHullSize().ordinal();
385 }
386 });
387
388 List<String> result = new ArrayList<String>();
389 for (ShipHullSpecAPI spec : specs) {
390 result.add(spec.getHullId());
391 }
392 return result;
393 }
394
395
396 public static List<String> getWingIds(Set<String> tags) {
397 List<FighterWingSpecAPI> specs = Global.getSettings().getAllFighterWingSpecs();
398
399 Iterator<FighterWingSpecAPI> iter = specs.iterator();
400
401 if (!tags.isEmpty()) {
402 iter = specs.iterator();
403 while (iter.hasNext()) {
404 FighterWingSpecAPI curr = iter.next();
405 for (String tag : tags) {
406 boolean not = tag.startsWith("!");
407 tag = not ? tag.substring(1) : tag;
408 boolean has = curr.hasTag(tag);
409 if (not == has) {
410 iter.remove();
411 break;
412 }
413 }
414 }
415 }
416
417 List<String> result = new ArrayList<String>();
418 for (FighterWingSpecAPI spec : specs) {
419 result.add(spec.getId());
420 }
421 return result;
422 }
423
424}
425
426
427
428
429
static SettingsAPI getSettings()
Definition Global.java:57
static boolean CODEX_TOOLTIP_MODE
Definition Global.java:15
static SoundPlayerAPI getSoundPlayer()
Definition Global.java:49
static SectorAPI getSector()
Definition Global.java:65
void addCostLabel(TooltipMakerAPI tooltip, float pad, CargoTransferHandlerAPI transferHandler, Object stackSource)
void addWeaponList(TooltipMakerAPI tooltip, String title, List< String > weapons, int max, float opad)
void addShipList(TooltipMakerAPI tooltip, String title, List< String > hulls, int max, float opad)
void addFighterList(TooltipMakerAPI tooltip, String title, List< String > wings, int max, float opad)
boolean areAllKnown(List< String > ships, List< String > weapons, List< String > fighters)
void render(float x, float y, float w, float h, float alphaMult, float glowMult, SpecialItemRendererAPI renderer)
void createTooltip(TooltipMakerAPI tooltip, boolean expanded, CargoTransferHandlerAPI transferHandler, Object stackSource)
static Color getButtonTextColor()
Definition Misc.java:842
static Color getGrayColor()
Definition Misc.java:826
static Color getHighlightColor()
Definition Misc.java:792
static Color getTooltipTitleAndLightHighlightColor()
Definition Misc.java:799
static Color getPositiveHighlightColor()
Definition Misc.java:822
List< WeaponSpecAPI > getAllWeaponSpecs()
List< ShipHullSpecAPI > getAllShipHullSpecs()
SpriteAPI getSprite(String filename)
List< FighterWingSpecAPI > getAllFighterWingSpecs()
SoundAPI playUISound(String id, float pitch, float volume)
boolean knowsWeapon(String weaponId)
void addKnownShip(String hullId, boolean setTimestamp)
void addKnownFighter(String wingId, boolean setTimestamp)
void addKnownWeapon(String weaponId, boolean setTimestamp)
void renderScanlinesWithCorners(float blX, float blY, float tlX, float tlY, float trX, float trY, float brX, float brY, float alphaMult, boolean additive)
void renderBGWithCorners(Color bgColor, float blX, float blY, float tlX, float tlY, float trX, float trY, float brX, float brY, float alphaMult, float glowMult, boolean additive)
void renderWithCorners(float blX, float blY, float tlX, float tlY, float trX, float trY, float brX, float brY)
LabelAPI addPara(String format, float pad, Color hl, String... highlights)