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