001package com.fs.starfarer.api.impl.campaign.rulecmd; 002 003import java.awt.Color; 004import java.util.ArrayList; 005import java.util.Collections; 006import java.util.List; 007import java.util.Map; 008 009import com.fs.starfarer.api.Global; 010import com.fs.starfarer.api.campaign.CampaignFleetAPI; 011import com.fs.starfarer.api.campaign.CargoAPI; 012import com.fs.starfarer.api.campaign.CargoStackAPI; 013import com.fs.starfarer.api.campaign.FactionAPI; 014import com.fs.starfarer.api.campaign.InteractionDialogAPI; 015import com.fs.starfarer.api.campaign.TextPanelAPI; 016import com.fs.starfarer.api.campaign.rules.MemKeys; 017import com.fs.starfarer.api.campaign.rules.MemoryAPI; 018import com.fs.starfarer.api.fleet.FleetMemberAPI; 019import com.fs.starfarer.api.impl.campaign.events.InvestigationEvent; 020import com.fs.starfarer.api.util.Misc; 021import com.fs.starfarer.api.util.Misc.Token; 022 023public class CustomsInspectionGenerateResult extends BaseCommandPlugin { 024 025 public static enum CargoInspectionResultType { 026 TOLL, 027 TOLL_AND_FINE, 028 } 029 030 public static class CargoInspectionResult { 031 private CargoInspectionResultType type; 032 private float tollAmount; 033 private CargoAPI legalFound, illegalFound; 034 035 public float getTollAmount() { 036 return tollAmount; 037 } 038 public void setTollAmount(float tollAmount) { 039 this.tollAmount = tollAmount; 040 } 041 public CargoInspectionResultType getType() { 042 return type; 043 } 044 public void setType(CargoInspectionResultType type) { 045 this.type = type; 046 } 047 public CargoAPI getLegalFound() { 048 return legalFound; 049 } 050 public void setLegalFound(CargoAPI legalFound) { 051 this.legalFound = legalFound; 052 } 053 public CargoAPI getIllegalFound() { 054 return illegalFound; 055 } 056 public void setIllegalFound(CargoAPI illegalFound) { 057 this.illegalFound = illegalFound; 058 } 059 } 060 061 062 public boolean execute(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) { 063 if (dialog == null) return false; 064 if (!(dialog.getInteractionTarget() instanceof CampaignFleetAPI)) return false; 065 066 CampaignFleetAPI playerFleet = Global.getSector().getPlayerFleet(); 067 CampaignFleetAPI other = (CampaignFleetAPI) dialog.getInteractionTarget(); 068 069 FactionAPI faction = other.getFaction(); 070 071 CargoInspectionResult result = new CargoInspectionResult(); 072 float totalLegal = 0; 073 float totalIllegal = 0; 074 float totalIllegalFound = 0; 075 CargoAPI legalFound = Global.getFactory().createCargo(true); 076 CargoAPI illegalFound = Global.getFactory().createCargo(true); 077 CargoAPI all = Global.getFactory().createCargo(true); 078 079 for (CargoStackAPI stack : playerFleet.getCargo().getStacksCopy()) { 080 boolean legal = !faction.isIllegal(stack); 081 if (legal) { 082 totalLegal += stack.getSize(); 083 } else { 084 totalIllegal += stack.getSize(); 085 } 086 all.addFromStack(stack); 087 } 088 float guiltMult = InvestigationEvent.getPlayerRepGuiltMult(faction); 089 //totalIllegal *= guiltMult; 090 float capacity = playerFleet.getCargo().getMaxCapacity(); 091 092 float shieldedFraction = Misc.getShieldedCargoFraction(playerFleet); 093 float unshieldedFraction = 1f - shieldedFraction; 094 095 float shieldedMult = (0.5f + 0.5f * unshieldedFraction); 096 097 if (totalLegal + totalIllegal > 0) { 098 List<CargoStackAPI> stacks = all.getStacksCopy(); 099 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