1package com.fs.starfarer.api.impl.campaign.submarkets;
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.Comparator;
6import java.util.HashMap;
7import java.util.HashSet;
12import com.fs.starfarer.api.Global;
13import com.fs.starfarer.api.campaign.CargoAPI;
14import com.fs.starfarer.api.campaign.CargoStackAPI;
15import com.fs.starfarer.api.campaign.CoreUIAPI;
16import com.fs.starfarer.api.campaign.PlayerMarketTransaction;
17import com.fs.starfarer.api.campaign.econ.CommodityOnMarketAPI;
18import com.fs.starfarer.api.campaign.econ.CommoditySpecAPI;
19import com.fs.starfarer.api.campaign.econ.MonthlyReport;
20import com.fs.starfarer.api.campaign.econ.SubmarketAPI;
21import com.fs.starfarer.api.campaign.econ.EconomyAPI.EconomyUpdateListener;
22import com.fs.starfarer.api.campaign.econ.MonthlyReport.FDNode;
23import com.fs.starfarer.api.campaign.listeners.EconomyTickListener;
24import com.fs.starfarer.api.combat.MutableStat;
25import com.fs.starfarer.api.combat.MutableStat.StatMod;
26import com.fs.starfarer.api.impl.campaign.econ.impl.BaseIndustry;
27import com.fs.starfarer.api.impl.campaign.ids.Commodities;
28import com.fs.starfarer.api.impl.campaign.ids.Strings;
29import com.fs.starfarer.api.impl.campaign.ids.Submarkets;
30import com.fs.starfarer.api.impl.campaign.shared.SharedData;
31import com.fs.starfarer.api.ui.Alignment;
32import com.fs.starfarer.api.ui.TooltipMakerAPI;
33import com.fs.starfarer.api.util.Misc;
49 protected Map<String, MutableStat>
stockpilingBonus =
new HashMap<String, MutableStat>();
76 super.advance(amount);
88 if (
market.isIllegal(com)) {
89 if (com.getCommodityMarketData().getMarketShareData(
market).isSourceIsIllegal()) {
99 if (stack.getCommodityId() ==
null)
return true;
100 if (stack.getResourceIfResource().hasTag(Commodities.TAG_NON_ECONOMIC))
return true;
107 return "Can only store resources";
114 int demand = com.getMaxDemand();
116 int shippingGlobal = com.getCommodityMarketData().getMaxShipping(com.getMarket(),
false);
119 int available = com.getAvailable();
121 StatMod mod = com.getAvailableStat().getFlatStatMod(modId);
123 available -= (int) mod.value;
124 if (available < 0) available = 0;
127 int production = com.getMaxSupply();
128 production = Math.min(production, available);
131 demand = com.getMaxDemand();
132 export = (int) Math.min(production, shippingGlobal);
134 int extra = available - Math.max(export, demand);
135 if (extra < 0) extra = 0;
137 int deficit = demand - available;
140 int imports = available - production;
141 if (imports < 0) imports = 0;
145 float unit = com.getCommodity().getEconUnit();
152 String cid = com.getId();
164 if (limit < 0) limit = 0;
205 CommodityOnMarketAPI com =
market.getCommodityData(commodityId);
235 return !
market.isPlayerOwned();
263 taken.addAll(transaction.getBought());
264 left.addAll(transaction.getSold());
266 CargoAPI copy =
taken.createCopy();
268 left.removeAll(copy);
289 stat =
new MutableStat(0);
300 List<CommodityOnMarketAPI> all =
new ArrayList<CommodityOnMarketAPI>(
market.getAllCommodities());
302 float totalCost = 0f;
306 for (CommodityOnMarketAPI com : all) {
307 int curr = (int)
cargo.getCommodityQuantity(com.getId());
308 if (curr <= 0)
continue;
311 units = Math.min(units,
cargo.getCommodityQuantity(com.getId()));
312 units -=
taken.getCommodityQuantity(com.getId());
315 totalCost += units * per;
318 return (
int) totalCost;
324 int result = (int) Math.round((spec.getBasePrice() * mult));
325 if (result < 1) result = 1;
333 String modId = Submarkets.LOCAL_RESOURCES;
335 StatMod mod = com.getAvailableStat().getFlatMods().get(modId);
336 float modAlready = 0;
337 if (mod !=
null) modAlready = mod.value;
339 int demand = com.getMaxDemand();
340 int available = (int) Math.round(com.getAvailable() - modAlready);
342 if (demand > available) {
343 float deficitDrawBaseAmount = BaseIndustry.getSizeMult(demand) - BaseIndustry.getSizeMult(available);
344 deficitDrawBaseAmount *= com.getCommodity().getEconUnit();
345 return deficitDrawBaseAmount;
350 protected boolean doShortageCountering(CommodityOnMarketAPI com,
float amount,
boolean withShortageCountering) {
354 com.getAvailableStat().unmodifyFlat(modId);
356 int demand = com.getMaxDemand();
357 int available = com.getAvailable();
363 if (withShortageCountering && demand > available) {
365 int deficit = demand - available;
366 if (deficit != deficit)
return false;
368 float deficitDrawBaseAmount = BaseIndustry.getSizeMult(demand) - BaseIndustry.getSizeMult(available);
369 deficitDrawBaseAmount *= com.getCommodity().getEconUnit();
373 float drawAmount = deficitDrawBaseAmount * days / 30f;
374 float curr =
cargo.getCommodityQuantity(com.getId());
375 if (curr > 0 && deficitDrawBaseAmount > 0) {
376 int daysLeft = (int) (curr / deficitDrawBaseAmount * 30f);
377 String daysStr =
"days";
382 com.getAvailableStat().modifyFlat(modId, deficit,
383 "Local resource stockpiles (" + daysLeft +
" " + daysStr +
" left)");
385 float free =
left.getCommodityQuantity(com.getId());
386 free = Math.min(drawAmount, free);
387 left.removeCommodity(com.getId(), free);
388 if (drawAmount > 0) {
389 cargo.removeCommodity(com.getId(), drawAmount);
393 if (
market.isPlayerOwned() && drawAmount > 0) {
394 MonthlyReport report = SharedData.getData().getCurrentReport();
395 FDNode node = report.getCounterShortageNode(
market);
397 CargoAPI tooltipCargo = (CargoAPI) node.custom2;
398 float addToTooltipCargo = drawAmount;
399 float q = tooltipCargo.getCommodityQuantity(com.getId()) + addToTooltipCargo;
401 addToTooltipCargo = 1f;
403 tooltipCargo.addCommodity(com.getId(), addToTooltipCargo);
408 FDNode comNode = report.getNode(node, com.getId());
410 CommoditySpecAPI spec = com.getCommodity();
411 comNode.icon = spec.getIconName();
412 comNode.upkeep += unitPrice * drawAmount;
413 comNode.custom = com;
415 if (comNode.custom2 ==
null) {
416 comNode.custom2 = 0f;
418 comNode.custom2 = (Float)comNode.custom2 + drawAmount;
420 float qty = Math.max(1, (Float) comNode.custom2);
421 qty = (float) Math.ceil(qty);
422 comNode.name = spec.getName() +
" " + Strings.X + Misc.getWithDGS(qty);
423 comNode.tooltipCreator = report.getMonthlyReportTooltip();
446 if (iterIndex != lastIterInMonth)
return;
448 if (
market.isPlayerOwned()) {
449 CargoAPI copy =
taken.createCopy();
451 left.removeAll(copy);
453 MonthlyReport report = SharedData.getData().getCurrentReport();
456 for (CargoStackAPI stack :
taken.getStacksCopy()) {
457 if (!stack.isCommodityStack())
continue;
459 FDNode node = report.getRestockingNode(
market);
460 CargoAPI tooltipCargo = (CargoAPI) node.custom2;
462 float addToTooltipCargo = stack.getSize();
463 String cid = stack.getCommodityId();
464 float q = tooltipCargo.getCommodityQuantity(cid) + addToTooltipCargo;
466 addToTooltipCargo = 1f;
468 tooltipCargo.addCommodity(cid, addToTooltipCargo);
473 FDNode comNode = report.getNode(node, cid);
475 CommoditySpecAPI spec = stack.getResourceIfResource();
476 comNode.icon = spec.getIconName();
477 comNode.upkeep += unitPrice * addToTooltipCargo;
478 comNode.custom =
market.getCommodityData(cid);
480 if (comNode.custom2 ==
null) {
481 comNode.custom2 = 0f;
483 comNode.custom2 = (Float)comNode.custom2 + addToTooltipCargo;
485 float qty = Math.max(1, (Float) comNode.custom2);
486 qty = (float) Math.ceil(qty);
487 comNode.name = spec.getName() +
" " + Strings.X + Misc.getWithDGS(qty);
488 comNode.tooltipCreator = report.getMonthlyReportTooltip();
506 return "End of month";
514 Set<String> seen =
new HashSet<String>();
516 if (!stack.isCommodityStack())
continue;
518 String cid = stack.getCommodityId();
519 if (seen.contains(cid))
continue;
522 CommodityOnMarketAPI com =
market.getCommodityData(cid);
525 int post = (int)
cargo.getCommodityQuantity(cid);
527 int units = pre - post;
529 units -=
left.getCommodityQuantity(cid);
533 total += price * units;
537 return Misc.getDGSCredits(total);
544 return "0" + Strings.C;
559 List<CommodityOnMarketAPI> all =
new ArrayList<CommodityOnMarketAPI>(
market.getAllCommodities());
561 Collections.sort(all,
new Comparator<CommodityOnMarketAPI>() {
562 public int compare(CommodityOnMarketAPI o1, CommodityOnMarketAPI o2) {
565 return limit2 - limit1;
571 tooltip.beginGridFlipped(400f, 1, 70f, opad);
573 for (CommodityOnMarketAPI com : all) {
574 if (com.isNonEcon())
continue;
575 if (com.getCommodity().isMeta())
continue;
580 if (limit <= 0)
continue;
582 tooltip.addToGrid(0, j++,
583 com.getCommodity().getName(),
584 Misc.getWithDGS(limit));
588 tooltip.addPara(
"A portion of the resources produced by the colony will be made available here. " +
589 "These resources can be extracted from the colony's economy for a cost equal to %s of their base value. " +
590 "This cost will be deducted at the end of the month.", opad,
593 tooltip.addPara(
"These resources can also be used to counter temporary shortages, for a " +
594 "cost equal to %s of their base value. If additional resources are placed here, they " +
595 "will be used as well, at no cost.", opad,
599 tooltip.addSectionHeading(
"Stockpiled per month",
market.getFaction().getBaseUIColor(),
market.getFaction().getDarkUIColor(), Alignment.MID, opad);
601 tooltip.addGrid(opad);
603 tooltip.addPara(
"Stockpiles are limited to %s the monthly rate.", opad,
606 tooltip.addPara(
"No stockpiling.", opad);
static SettingsAPI getSettings()
static FactoryAPI getFactory()
static SectorAPI getSector()
float sinceLastCargoUpdate
void addAndRemoveStockpiledResources(float amount, boolean withShortageCountering, boolean withDecreaseToLimit, boolean withCargoUpdate)
void updateCargoPrePlayerInteraction()
float getStockpilingAddRateMult(CommodityOnMarketAPI com)
void reportEconomyMonthEnd()
boolean showInFleetScreen()
boolean showInCargoScreen()
static float getDeficitMonthlyCommodityUnits(CommodityOnMarketAPI com)
int getStockpileLimit(CommodityOnMarketAPI com)
transient CargoAPI preTransactionCargoCopy
void reportEconomyTick(int iterIndex)
void reportPlayerMarketTransaction(PlayerMarketTransaction transaction)
boolean doShortageCountering(CommodityOnMarketAPI com, float amount, boolean withShortageCountering)
boolean isTooltipExpandable()
void commodityUpdated(String commodityId)
void advance(float amount)
boolean isEnabled(CoreUIAPI ui)
Map< String, MutableStat > stockpilingBonus
MutableStat getStockpilingBonus(String cid)
static float STOCKPILE_MAX_MONTHS
static float STOCKPILE_COST_MULT
String getTotalTextOverride()
void init(SubmarketAPI submarket)
String getTotalValueOverride()
boolean isEconomyListenerExpired()
String getTariffTextOverride()
int getEstimatedShortageCounteringCostPerMonth()
static float STOCKPILE_MULT_EXCESS
static float STOCKPILE_MULT_IMPORTS
static float STOCKPILE_MULT_PRODUCTION
void createTooltipAfterDescription(TooltipMakerAPI tooltip, boolean expanded)
static float STOCKPILE_SHORTAGE_COST_MULT
static int getStockpilingUnitPrice(CommoditySpecAPI spec, boolean forShortageCountering)
LocalResourcesSubmarketPlugin()
boolean isParticipatesInEconomy()
String getIllegalTransferText(CargoStackAPI stack, TransferAction action)
String getTariffValueOverride()
boolean isIllegalOnSubmarket(CargoStackAPI stack, TransferAction action)
boolean shouldHaveCommodity(CommodityOnMarketAPI com)
CargoAPI createCargo(boolean unlimitedStacks)
float getFloat(String key)