001package com.fs.starfarer.api.impl.campaign.rulecmd.salvage; 002 003import java.util.List; 004import java.util.Map; 005 006import com.fs.starfarer.api.Global; 007import com.fs.starfarer.api.campaign.CampaignFleetAPI; 008import com.fs.starfarer.api.campaign.CargoAPI; 009import com.fs.starfarer.api.campaign.CargoPickerListener; 010import com.fs.starfarer.api.campaign.CargoStackAPI; 011import com.fs.starfarer.api.campaign.FactionAPI; 012import com.fs.starfarer.api.campaign.InteractionDialogAPI; 013import com.fs.starfarer.api.campaign.OptionPanelAPI; 014import com.fs.starfarer.api.campaign.SectorEntityToken; 015import com.fs.starfarer.api.campaign.TextPanelAPI; 016import com.fs.starfarer.api.campaign.rules.MemoryAPI; 017import com.fs.starfarer.api.characters.PersonAPI; 018import com.fs.starfarer.api.impl.campaign.CoreReputationPlugin.CustomRepImpact; 019import com.fs.starfarer.api.impl.campaign.CoreReputationPlugin.RepActionEnvelope; 020import com.fs.starfarer.api.impl.campaign.CoreReputationPlugin.RepActions; 021import com.fs.starfarer.api.impl.campaign.ids.Strings; 022import com.fs.starfarer.api.impl.campaign.ids.Tags; 023import com.fs.starfarer.api.impl.campaign.rulecmd.AddRemoveCommodity; 024import com.fs.starfarer.api.impl.campaign.rulecmd.BaseCommandPlugin; 025import com.fs.starfarer.api.impl.campaign.rulecmd.FireBest; 026import com.fs.starfarer.api.ui.TooltipMakerAPI; 027import com.fs.starfarer.api.util.Misc; 028import com.fs.starfarer.api.util.Misc.Token; 029 030/** 031 * NotifyEvent $eventHandle <params> 032 * 033 */ 034public class ZGRTurnIn extends BaseCommandPlugin { 035 036 public static float VALUE_MULT = 3f; 037 public static float REP_MULT = 0.2f; 038 039 protected CampaignFleetAPI playerFleet; 040 protected SectorEntityToken entity; 041 protected FactionAPI playerFaction; 042 protected FactionAPI entityFaction; 043 protected TextPanelAPI text; 044 protected OptionPanelAPI options; 045 protected CargoAPI playerCargo; 046 protected MemoryAPI memory; 047 protected InteractionDialogAPI dialog; 048 protected Map<String, MemoryAPI> memoryMap; 049 protected PersonAPI person; 050 protected FactionAPI faction; 051 052 public boolean execute(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) { 053 054 this.dialog = dialog; 055 this.memoryMap = memoryMap; 056 057 String command = params.get(0).getString(memoryMap); 058 if (command == null) return false; 059 060 memory = getEntityMemory(memoryMap); 061 062 entity = dialog.getInteractionTarget(); 063 text = dialog.getTextPanel(); 064 options = dialog.getOptionPanel(); 065 066 playerFleet = Global.getSector().getPlayerFleet(); 067 playerCargo = playerFleet.getCargo(); 068 069 playerFaction = Global.getSector().getPlayerFaction(); 070 entityFaction = entity.getFaction(); 071 072 person = dialog.getInteractionTarget().getActivePerson(); 073 faction = person.getFaction(); 074 075 if (command.equals("selectSellableItems")) { 076 selectSellableItems(); 077 } else if (command.equals("playerHasSellableItems")) { 078 return playerHasSellableItems(); 079 } 080 081 return true; 082 } 083 084 085 protected void selectSellableItems() { 086 CargoAPI copy = getSellableItems(); 087 088 final float width = 310f; 089 dialog.showCargoPickerDialog("Select items to turn in", "Confirm", "Cancel", true, width, copy, new CargoPickerListener() { 090 public void pickedCargo(CargoAPI cargo) { 091 if (cargo.isEmpty()) { 092 cancelledCargoSelection(); 093 return; 094 } 095 096 cargo.sort(); 097 098 float bountyThreat = 0; 099 float bountyMonster = 0; 100 MemoryAPI mem = Global.getSector().getPlayerMemoryWithoutUpdate(); 101 102 for (CargoStackAPI stack : cargo.getStacksCopy()) { 103 playerCargo.removeItems(stack.getType(), stack.getData(), stack.getSize()); 104 int num = (int) stack.getSize(); 105 AddRemoveCommodity.addStackLossText(stack, text); 106 if (isThreatStack(stack)) { 107 bountyThreat += num * stack.getBaseValuePerUnit() * VALUE_MULT; 108 } else if (isMonsterStack(stack)) { 109 bountyMonster += num * stack.getBaseValuePerUnit() * VALUE_MULT; 110 } 111 } 112 113 float repChange = computeReputationValue(cargo); 114 115 int bounty = (int) (bountyThreat + bountyMonster); 116 if (bounty > 0) { 117 playerCargo.getCredits().add(bounty); 118 AddRemoveCommodity.addCreditsGainText((int)bounty, text); 119 120 String soldTotalKey = "$itemValueSoldToZGRThreat"; 121 int curr = mem.getInt(soldTotalKey); 122 curr += bountyThreat; 123 mem.set(soldTotalKey, curr); 124 125 soldTotalKey = "$itemValueSoldToZGRMonster"; 126 curr = mem.getInt(soldTotalKey); 127 curr += bountyMonster; 128 mem.set(soldTotalKey, curr); 129 130 soldTotalKey = "$itemValueSoldToZGRTotal"; 131 curr = mem.getInt(soldTotalKey); 132 curr += bounty; 133 mem.set(soldTotalKey, curr); 134 135 } 136 137 if (repChange >= 1f) { 138 CustomRepImpact impact = new CustomRepImpact(); 139 impact.delta = repChange * 0.01f; 140 Global.getSector().adjustPlayerReputation( 141 new RepActionEnvelope(RepActions.CUSTOM, impact, 142 null, text, true), 143 faction.getId()); 144 145 impact.delta *= 0.25f; 146 if (impact.delta >= 0.01f) { 147 Global.getSector().adjustPlayerReputation( 148 new RepActionEnvelope(RepActions.CUSTOM, impact, 149 null, text, true), 150 person); 151 } 152 } 153 154 mem.set("$itemValueSoldToZGRJustNowThreat", (int)bountyThreat, 0); 155 mem.set("$itemValueSoldToZGRJustNowMonster", (int)bountyMonster, 0); 156 mem.set("$itemValueSoldToZGRJustNowTotal", (int)bounty, 0); 157 FireBest.fire(null, dialog, memoryMap, "ZGRItemsTurnedIn"); 158 } 159 public void cancelledCargoSelection() { 160 } 161 public void recreateTextPanel(TooltipMakerAPI panel, CargoAPI cargo, CargoStackAPI pickedUp, boolean pickedUpFromSource, CargoAPI combined) { 162 163 float bounty = 0f; 164 for (CargoStackAPI stack : combined.getStacksCopy()) { 165 int num = (int) stack.getSize(); 166 bounty += num * stack.getBaseValuePerUnit() * VALUE_MULT; 167 } 168 169 float repChange = computeReputationValue(combined); 170 171 float pad = 3f; 172 float small = 5f; 173 float opad = 10f; 174 175 panel.setParaFontOrbitron(); 176 panel.addPara(Misc.ucFirst(faction.getDisplayName()), faction.getBaseUIColor(), 1f); 177 //panel.addTitle(Misc.ucFirst(faction.getDisplayName()), faction.getBaseUIColor()); 178 //panel.addPara(faction.getDisplayNameLong(), faction.getBaseUIColor(), opad); 179 //panel.addPara(faction.getDisplayName() + " (" + entity.getMarket().getName() + ")", faction.getBaseUIColor(), opad); 180 panel.setParaFontDefault(); 181 182 panel.addImage(faction.getLogo(), width * 1f, 3f); 183 184 panel.addPara("If you turn in the selected items, you will receive a %s bounty " + 185 "and your standing with " + faction.getDisplayNameWithArticle() + " will improve by %s points.", 186 opad * 1f, Misc.getHighlightColor(), 187 Misc.getWithDGS(bounty) + Strings.C, 188 "" + (int) repChange); 189 } 190 }); 191 } 192 193 protected float computeReputationValue(CargoAPI cargo) { 194 float rep = 0; 195 for (CargoStackAPI stack : cargo.getStacksCopy()) { 196 rep += getBaseRepValue(stack) * stack.getSize(); 197 } 198 rep *= REP_MULT; 199 return rep; 200 } 201 202 public static float getBaseRepValue(CargoStackAPI stack) { 203 if (stack.isWeaponStack()) { 204 switch (stack.getWeaponSpecIfWeapon().getSize()) { 205 case LARGE: return 3f; 206 case MEDIUM: return 2f; 207 case SMALL: return 1f; 208 } 209 } 210 if (stack.isSpecialStack()) { 211 return 5f; 212 } 213 return 1f; 214 } 215 216 217 protected boolean playerHasSellableItems() { 218 return !getSellableItems().isEmpty(); 219 } 220 221 public static boolean isThreatStack(CargoStackAPI stack) { 222 boolean match = false; 223 match |= stack.isWeaponStack() && stack.getWeaponSpecIfWeapon().hasTag(Tags.THREAT); 224 match |= stack.isSpecialStack() && stack.getSpecialItemSpecIfSpecial().hasTag(Tags.THREAT); 225 return match; 226 } 227 public static boolean isMonsterStack(CargoStackAPI stack) { 228 boolean match = false; 229 match |= stack.isWeaponStack() && stack.getWeaponSpecIfWeapon().hasTag(Tags.MONSTER); 230 match |= stack.isSpecialStack() && stack.getSpecialItemSpecIfSpecial().hasTag(Tags.MONSTER); 231 return match; 232 } 233 234 protected CargoAPI getSellableItems() { 235 CargoAPI copy = Global.getFactory().createCargo(false); 236 for (CargoStackAPI stack : playerCargo.getStacksCopy()) { 237 boolean match = isThreatStack(stack) || isMonsterStack(stack); 238 if (match) { 239 copy.addFromStack(stack); 240 } 241 } 242 copy.sort(); 243 return copy; 244 } 245 246 247} 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262