Starsector API
Loading...
Searching...
No Matches
CustomsInspectionGenerateResult.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.rulecmd;
2
3import java.awt.Color;
4import java.util.ArrayList;
5import java.util.Collections;
6import java.util.List;
7import java.util.Map;
8
9import com.fs.starfarer.api.Global;
10import com.fs.starfarer.api.campaign.CampaignFleetAPI;
11import com.fs.starfarer.api.campaign.CargoAPI;
12import com.fs.starfarer.api.campaign.CargoStackAPI;
13import com.fs.starfarer.api.campaign.FactionAPI;
14import com.fs.starfarer.api.campaign.InteractionDialogAPI;
15import com.fs.starfarer.api.campaign.TextPanelAPI;
16import com.fs.starfarer.api.campaign.rules.MemKeys;
17import com.fs.starfarer.api.campaign.rules.MemoryAPI;
18import com.fs.starfarer.api.fleet.FleetMemberAPI;
19import com.fs.starfarer.api.impl.campaign.events.InvestigationEvent;
20import com.fs.starfarer.api.util.Misc;
21import com.fs.starfarer.api.util.Misc.Token;
22
24
25 public static enum CargoInspectionResultType {
26 TOLL,
27 TOLL_AND_FINE,
28 }
29
30 public static class CargoInspectionResult {
31 private CargoInspectionResultType type;
32 private float tollAmount;
33 private CargoAPI legalFound, illegalFound;
34
35 public float getTollAmount() {
36 return tollAmount;
37 }
38 public void setTollAmount(float tollAmount) {
39 this.tollAmount = tollAmount;
40 }
41 public CargoInspectionResultType getType() {
42 return type;
43 }
44 public void setType(CargoInspectionResultType type) {
45 this.type = type;
46 }
47 public CargoAPI getLegalFound() {
48 return legalFound;
49 }
50 public void setLegalFound(CargoAPI legalFound) {
51 this.legalFound = legalFound;
52 }
53 public CargoAPI getIllegalFound() {
54 return illegalFound;
55 }
56 public void setIllegalFound(CargoAPI illegalFound) {
57 this.illegalFound = illegalFound;
58 }
59 }
60
61
62 public boolean execute(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) {
63 if (dialog == null) return false;
64 if (!(dialog.getInteractionTarget() instanceof CampaignFleetAPI)) return false;
65
66 CampaignFleetAPI playerFleet = Global.getSector().getPlayerFleet();
67 CampaignFleetAPI other = (CampaignFleetAPI) dialog.getInteractionTarget();
68
69 FactionAPI faction = other.getFaction();
70
71 CargoInspectionResult result = new CargoInspectionResult();
72 float totalLegal = 0;
73 float totalIllegal = 0;
74 float totalIllegalFound = 0;
75 CargoAPI legalFound = Global.getFactory().createCargo(true);
76 CargoAPI illegalFound = Global.getFactory().createCargo(true);
77 CargoAPI all = Global.getFactory().createCargo(true);
78
79 for (CargoStackAPI stack : playerFleet.getCargo().getStacksCopy()) {
80 boolean legal = !faction.isIllegal(stack);
81 if (legal) {
82 totalLegal += stack.getSize();
83 } else {
84 totalIllegal += stack.getSize();
85 }
86 all.addFromStack(stack);
87 }
88 float guiltMult = InvestigationEvent.getPlayerRepGuiltMult(faction);
89 //totalIllegal *= guiltMult;
90 float capacity = playerFleet.getCargo().getMaxCapacity();
91
92 float shieldedFraction = Misc.getShieldedCargoFraction(playerFleet);
93 float unshieldedFraction = 1f - shieldedFraction;
94
95 float shieldedMult = (0.5f + 0.5f * unshieldedFraction);
96
97 if (totalLegal + totalIllegal > 0) {
98 List<CargoStackAPI> stacks = all.getStacksCopy();
99 Collections.shuffle(stacks);
100 float illegalSoFar = 0;
101 for (CargoStackAPI stack : stacks) {
102 if (stack.getSize() <= 0) continue;
103 boolean legal = !faction.isIllegal(stack);
104 illegalSoFar += stack.getSize();
105 float chanceToFind = illegalSoFar / (totalLegal + totalIllegal);
106 chanceToFind *= guiltMult;
107 chanceToFind *= shieldedMult;
108 if (legal) {
109 legalFound.addFromStack(stack);
110 } else if ((float) Math.random() < chanceToFind) {
111 float qty = stack.getSize();
112 qty = qty * (0.33f + (float) Math.random() * 0.67f);
113 qty *= shieldedMult;
114 qty = Math.round(qty);
115 if (qty < 1) qty = 1;
116 illegalFound.addItems(stack.getType(), stack.getData(), qty);
117 }
118 }
119 }
120
121 result.setLegalFound(legalFound);
122 result.setIllegalFound(illegalFound);
123 if (illegalFound.isEmpty()) {
124 result.setType(CargoInspectionResultType.TOLL);
125 } else {
126 result.setType(CargoInspectionResultType.TOLL_AND_FINE);
127 }
128
129 //float shipTollAmount = playerFleet.getFleetPoints() * 50f;
130 float shipTollAmount = 0f; //playerFleet.getFleetPoints() * 50f;
131 for (FleetMemberAPI member : playerFleet.getFleetData().getMembersListCopy()) {
132 shipTollAmount += member.getBaseSellValue() * 0.125f * faction.getTollFraction();
133 }
134 shipTollAmount = (int)shipTollAmount;
135
136 float tollFraction = faction.getTollFraction();
137 float fineFraction = faction.getFineFraction();
138
139 float toll = 0;
140 float fine = 0;
141 for (CargoStackAPI stack : legalFound.getStacksCopy()) {
142 toll += stack.getSize() * stack.getBaseValuePerUnit() * tollFraction * shieldedMult;
143 }
144 for (CargoStackAPI stack : illegalFound.getStacksCopy()) {
145 fine += stack.getSize() * stack.getBaseValuePerUnit() * fineFraction;
146 totalIllegalFound += stack.getSize();
147 }
148
149 float totalTollAndFine = shipTollAmount + toll + fine;
150
151 toll = (int)toll;
152 fine = (int)fine;
153
154 //totalTollAndFine *= 70f;
155
156 result.setTollAmount(totalTollAndFine);
157
158 MemoryAPI memory = memoryMap.get(MemKeys.LOCAL);
159 memory.set("$tollAmount", "" + (int)result.getTollAmount(), 0);
160 memory.set("$inspectionResultType", result.getType().name(), 0);
161 memory.set("$playerCanAffordPayment", playerFleet.getCargo().getCredits().get() >= result.getTollAmount(), 0);
162 memory.set("$cargoInspectionResult", result, 0);
163
164
165 TextPanelAPI text = dialog.getTextPanel();
166
167 text.setFontVictor();
168 text.setFontSmallInsignia();
169
170 Color hl = Misc.getHighlightColor();
171 Color red = Misc.getNegativeHighlightColor();
172 text.addParagraph("-----------------------------------------------------------------------------");
173
174 text.addParagraph("Fleet size toll: " + (int) shipTollAmount);
175 text.highlightInLastPara(hl, "" + (int) shipTollAmount);
176 text.addParagraph("Cargo toll: " + (int) toll);
177 text.highlightInLastPara(hl, "" + (int) toll);
178
179 if (!illegalFound.isEmpty()) {
180 text.addParagraph("Contraband found!", red);
181 String para = "";
182 List<String> highlights = new ArrayList<String>();
183 for (CargoStackAPI stack : illegalFound.getStacksCopy()) {
184 para += stack.getDisplayName() + " x " + (int)stack.getSize() + "\n";
185 highlights.add("" + (int)stack.getSize());
186 }
187 para = para.substring(0, para.length() - 1);
188 text.addParagraph(para);
189 text.highlightInLastPara(hl, highlights.toArray(new String [0]));
190
191 text.addParagraph("Fine: " + (int) fine);
192 text.highlightInLastPara(hl, "" + (int) fine);
193 }
194
195 text.addParagraph("Total: " + (int) totalTollAndFine + " credits");
196 text.highlightInLastPara(hl, "" + (int) totalTollAndFine);
197
198 text.addParagraph("-----------------------------------------------------------------------------");
199
200 text.setFontInsignia();
201
202// float repLoss = totalIllegalFound / 10f * totalIllegalFound / capacity;
203// repLoss = Math.round(repLoss);
204// if (repLoss > 5) repLoss = 5f;
205// if (repLoss == 0 && totalIllegalFound > 0) repLoss = 1f;
206// if (repLoss > 0) {
207// RepActionEnvelope envelope = new RepActionEnvelope(RepActions.CUSTOMS_CAUGHT_SMUGGLING, repLoss, text);
208// Global.getSector().adjustPlayerReputation(envelope, faction.getId());
209// }
210
211
212 return true;
213 }
214
215}
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
static FactoryAPI getFactory()
Definition Global.java:35
static SectorAPI getSector()
Definition Global.java:59
boolean execute(String ruleId, InteractionDialogAPI dialog, List< Token > params, Map< String, MemoryAPI > memoryMap)
CargoAPI createCargo(boolean unlimitedStacks)