Starsector API
Loading...
Searching...
No Matches
PriceUpdate.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.events;
2
3import com.fs.starfarer.api.Global;
4import com.fs.starfarer.api.campaign.econ.CommodityOnMarketAPI;
5import com.fs.starfarer.api.campaign.econ.MarketAPI;
6import com.fs.starfarer.api.campaign.events.CampaignEventPlugin.PriceUpdatePlugin;
7import com.fs.starfarer.api.impl.campaign.shared.CommodityStatTracker;
8import com.fs.starfarer.api.util.Misc;
9
10public class PriceUpdate implements PriceUpdatePlugin {
11
12 private String marketId;
13 private String commodityId;
14 private float supplyPrice;
15 private float demandPrice;
16 private PriceType type;
17 private long timestamp;
18 private float demand;//, available;
20 //this.commodity = commodity;
21 marketId = commodity.getMarket().getId();
22 commodityId = commodity.getId();
23 supplyPrice = Math.round(commodity.getMarket().getSupplyPrice(commodity.getId(), 1, true));
24 demandPrice = Math.round(commodity.getMarket().getDemandPrice(commodity.getId(), 1, true));
25 //priceMult = commodity.getPlayerPriceMult().getModifiedValue();
26
27// if (commodityId.equals("food")) {
28// System.out.println("dsfsdfsdf");
29// }
30 updateType();
31 timestamp = Global.getSector().getClock().getTimestamp();
32
33// float f = Global.getSettings().getFloat("economyAverageFractionOfStockpilePlayerCanBuy");
34// available = commodity.getAverageStockpileAfterDemand() * f;
35 demand = commodity.getDemand().getDemandValue();
36 }
37
38 public void updatePrices(float stockpileMult) {
40 float stockpile = com.getStockpile();
41 float after = stockpile * stockpileMult;
42
43 float diff = (after - stockpile) * com.getUtilityOnMarket();
44
45 supplyPrice = Math.round(com.getMarket().getSupplyPriceAssumingExistingTransaction(
46 com.getId(), 1, diff, true));
47 demandPrice = Math.round(com.getMarket().getDemandPriceAssumingExistingTransaction(
48 com.getId(), 1, diff, true));
49
50 }
51
52
54 return getRoundedPriceForDisplay(0, 0);
55 }
56
63 public int getRoundedPriceForDisplay(float priceFlat, float pricePercent) {
64 float testSupplyPrice = supplyPrice;
65 float testDemandPrice = demandPrice;
66 //getCommodity().getMarket().getDemandPrice(getCommodity().getId(), 1, true)
67 // if params are non-0, then the curr price may already include a player-facing modifier - so, discount it
68 // this won't work for multiple sources modifying the same price, though
69 // never mind that - only call this method when the bonus you want to ignore is not already applied.
70 if (priceFlat != 0 || pricePercent != 0) {
72 testSupplyPrice = Math.round(commodity.getMarket().getSupplyPrice(commodity.getId(), 1, true) * (1f + pricePercent / 100f) + priceFlat);
73 testDemandPrice = Math.round(commodity.getMarket().getDemandPrice(commodity.getId(), 1, true) * (1f + pricePercent / 100f) + priceFlat);
74 }
75 if (true) {
76 // for now, cheat and grab real prices from market
78// if (commodity.getId().equals(Commodities.HAND_WEAPONS) && commodity.getMarket().getId().equals("ogma")) {
79// System.out.println("q23sdfsdf");
80// }
81 testSupplyPrice = Math.round(commodity.getMarket().getSupplyPrice(commodity.getId(), 1, true));
82 testDemandPrice = Math.round(commodity.getMarket().getDemandPrice(commodity.getId(), 1, true));
83
84 supplyPrice = testSupplyPrice;
85 demandPrice = testDemandPrice;
86 updateType();
87 }
88
89 int amt = 0;
90 switch (getType()) {
91 case NORMAL:
92 amt = (int) (testSupplyPrice + testDemandPrice) / 2;
93 break;
94 case CHEAP:
95 amt = (int) testSupplyPrice;
96 break;
97 case EXPENSIVE:
98 amt = (int) testDemandPrice;
99 break;
100 }
101 amt = (int) Misc.getRounded(amt);
102 return amt;
103 }
104
105
106
107 public float getDemand() {
108 return demand;
109 }
110
111 public float getAvailable() {
112 //float f = Global.getSettings().getFloat("economyAverageFractionOfStockpilePlayerCanBuy");
114 //float available = com.getMaxPlayerFacingStockpile(f, com.getAverageStockpileAfterDemand());
115 float available = com.getStockpile();
116 return available;
117 }
118
119 public void updateType() {
120 updateType(0f, 0f);
121 }
122
129 public void updateType(float priceFlat, float pricePercent) {
131 //CommodityStatTracker stats = SharedData.getData().getActivityTracker().getCommodityTracker();
133 float testSupplyPrice = supplyPrice;
134 float testDemandPrice = demandPrice;
135
136 // if params are non-0, then the curr price may already include a player-facing modifier - so, discount it
137 // this won't work for multiple sources modifying the same price, though
138 // never mind - only call this method when the bonus you want to ignore is not already applied.
139 if (priceFlat != 0 || pricePercent != 0) {
140 testSupplyPrice = Math.round(commodity.getMarket().getSupplyPrice(commodity.getId(), 1, true) * (1f + pricePercent / 100f) + priceFlat);
141 testDemandPrice = Math.round(commodity.getMarket().getDemandPrice(commodity.getId(), 1, true) * (1f + pricePercent / 100f) + priceFlat);
142 }
143
144 if (stats.isSupplyPriceSignificant(commodity, Misc.getRounded(testSupplyPrice))) {
145 type = PriceType.CHEAP;
146 } else if (stats.isDemandPriceSignificant(commodity, Misc.getRounded(testDemandPrice))) {
147 type = PriceType.EXPENSIVE;
148 } else {
149 type = PriceType.NORMAL;
150 }
151 }
152
153 public long getTimestamp() {
154 return timestamp;
155 }
156
161 public boolean isSignificant() {
162 return isSignificant(0f, 0f);
163 }
164
170 public boolean isSignificant(float priceFlat, float pricePercent) {
171 updateType(priceFlat, pricePercent);
173 if ((getType() == PriceType.CHEAP)
174 && commodity.getStockpile() < 100) return false;
175
176 if ((getType() == PriceType.NORMAL)
177 && commodity.getStockpile() + commodity.getDemand().getDemandValue() < 100) return false;
178
179 if (getType() == PriceType.EXPENSIVE && commodity.getDemand().getDemandValue() < 100) return false;
180
181 return true;
182 }
183
185 return Global.getSector().getEconomy().getMarket(marketId);
186 }
188 MarketAPI market = getMarket();
189 if (market == null) return null;
190 return market.getCommodityData(commodityId);
191 }
192 public float getSupplyPrice() {
193 return supplyPrice;
194 }
195 public float getDemandPrice() {
196 return demandPrice;
197 }
198 public PriceType getType() {
199 return type;
200 }
201}
static SectorAPI getSector()
Definition Global.java:65
int getRoundedPriceForDisplay(float priceFlat, float pricePercent)
void updateType(float priceFlat, float pricePercent)
boolean isSignificant(float priceFlat, float pricePercent)
static float getRounded(float in)
Definition Misc.java:639
float getDemandPriceAssumingExistingTransaction(String commodityId, double quantity, double existingTransactionUtility, boolean isPlayerPrice)
float getSupplyPriceAssumingExistingTransaction(String commodityId, double quantity, double existingTransactionUtility, boolean isPlayerPrice)
float getSupplyPrice(String commodityId, double quantity, boolean isPlayerPrice)
CommodityOnMarketAPI getCommodityData(String commodityId)
float getDemandPrice(String commodityId, double quantity, boolean isPlayerPrice)