Starsector API
Loading...
Searching...
No Matches
DistressResponse.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.rulecmd.salvage;
2
3import java.util.List;
4import java.util.Map;
5
6import org.lwjgl.util.vector.Vector2f;
7
8import com.fs.starfarer.api.Global;
9import com.fs.starfarer.api.campaign.CampaignFleetAPI;
10import com.fs.starfarer.api.campaign.CargoAPI;
11import com.fs.starfarer.api.campaign.FactionAPI;
12import com.fs.starfarer.api.campaign.InteractionDialogAPI;
13import com.fs.starfarer.api.campaign.OptionPanelAPI;
14import com.fs.starfarer.api.campaign.RepLevel;
15import com.fs.starfarer.api.campaign.SectorEntityToken;
16import com.fs.starfarer.api.campaign.TextPanelAPI;
17import com.fs.starfarer.api.campaign.SectorEntityToken.VisibilityLevel;
18import com.fs.starfarer.api.campaign.econ.CommoditySpecAPI;
19import com.fs.starfarer.api.campaign.rules.MemoryAPI;
20import com.fs.starfarer.api.characters.AbilityPlugin;
21import com.fs.starfarer.api.characters.PersonAPI;
22import com.fs.starfarer.api.fleet.FleetMemberAPI;
23import com.fs.starfarer.api.impl.campaign.abilities.DistressCallAbility;
24import com.fs.starfarer.api.impl.campaign.ids.Abilities;
25import com.fs.starfarer.api.impl.campaign.ids.Commodities;
26import com.fs.starfarer.api.impl.campaign.ids.Entities;
27import com.fs.starfarer.api.impl.campaign.ids.Strings;
28import com.fs.starfarer.api.impl.campaign.rulecmd.AddRemoveCommodity;
29import com.fs.starfarer.api.impl.campaign.rulecmd.BaseCommandPlugin;
30import com.fs.starfarer.api.util.Misc;
31import com.fs.starfarer.api.util.Misc.Token;
32
37public class DistressResponse extends BaseCommandPlugin {
38
39 //public static float REP_NORMAL = -0.05f;
40 public static float REP_PER_USE = -0.02f;
41 public static float REP_SCAM = -0.15f;
42
43 protected CampaignFleetAPI playerFleet;
44 protected CampaignFleetAPI fleet;
45 protected SectorEntityToken entity;
46 protected FactionAPI playerFaction;
47 protected FactionAPI entityFaction;
48 protected TextPanelAPI text;
49 protected OptionPanelAPI options;
50 protected CargoAPI playerCargo;
51 protected MemoryAPI memory;
52 protected InteractionDialogAPI dialog;
53 protected Map<String, MemoryAPI> memoryMap;
54 protected PersonAPI person;
55 protected FactionAPI faction;
56
57 public boolean execute(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) {
58
59 this.dialog = dialog;
60 this.memoryMap = memoryMap;
61
62 String command = params.get(0).getString(memoryMap);
63 if (command == null) return false;
64
66
67 entity = dialog.getInteractionTarget();
68 text = dialog.getTextPanel();
69 options = dialog.getOptionPanel();
70
71 playerFleet = Global.getSector().getPlayerFleet();
72 playerCargo = playerFleet.getCargo();
73
74 playerFaction = Global.getSector().getPlayerFaction();
75 entityFaction = entity.getFaction();
76
77 person = dialog.getInteractionTarget().getActivePerson();
78 faction = person.getFaction();
79
80 fleet = (CampaignFleetAPI) dialog.getInteractionTarget();
81
82 if (command.equals("playerNeedsHelp")) {
83 return playerNeedsHelp();
84 } else if (command.equals("didNotNeedHelp")) {
86 }
87 else if (command.equals("acceptHelp")) {
88 acceptAid();
89 }
90 else if (command.equals("neverMind")) {
91 neverMind();
92 }
93 else if (command.equals("isCargoPodsScam")) {
94 return isCargoPodsScam();
95 }
96 else if (command.equals("cargoPodsScam")) {
98 }
99 else if (command.equals("unrespond")) {
100 unrespond();
101 }
102 else if (command.equals("init")) {
103 init();
104 }
105 else if (command.equals("pay")) {
106 pay();
107 }
108 else if (command.equals("thank")) {
109 thank();
110 }
111
112 return true;
113 }
114
115 protected void init() {
116 person.getMemoryWithoutUpdate().set("$distressUsesLastCycle", getNumUses(), 0);
117
118 int fuel = getNeededFuel();
119 int supplies = getNeededSupplies();
120 int maxFuel = getMaxFuel();
121 int maxSupplies = getMaxSupplies();
122
123 boolean adequate = fuel <= maxFuel * 1.5f && supplies <= maxSupplies * 1.5f;
124 person.getMemoryWithoutUpdate().set("$distressHelpAdequate", adequate, 0);
125
126
127 int distressPayment = getPayment();
128 person.getMemoryWithoutUpdate().set("$distressPayment", Misc.getWithDGS(distressPayment), 0);
129 person.getMemoryWithoutUpdate().set("$distressPaymentC", Misc.getWithDGS(distressPayment) + Strings.C, 0);
130 person.getMemoryWithoutUpdate().set("$distressCanAfford", distressPayment <= playerCargo.getCredits().get(), 0);
131 }
132
133 protected int getPayment() {
134 int fuel = getNeededFuel();
135 int supplies = getNeededSupplies();
136 int maxFuel = getMaxFuel();
137 int maxSupplies = getMaxSupplies();
138 if (fuel > maxFuel) fuel = maxFuel;
139 if (supplies > maxSupplies) supplies = maxSupplies;
140
141 CommoditySpecAPI fuelComm = Global.getSettings().getCommoditySpec(Commodities.FUEL);
142 CommoditySpecAPI suppliesComm = Global.getSettings().getCommoditySpec(Commodities.FUEL);
143
144 float distressPayment = fuel * fuelComm.getBasePrice() + supplies * suppliesComm.getBasePrice();
145 distressPayment = (float) Math.ceil(distressPayment / 10000f) * 10000f;
146 distressPayment *= 2f;
147
148 return (int) distressPayment;
149 }
150
151 protected int getNumUses() {
152 AbilityPlugin plugin = playerFleet.getAbility(Abilities.DISTRESS_CALL);
153 if (plugin != null) {
154 int uses = ((DistressCallAbility) plugin).getNumUsesInLastPeriod();
155 return uses;
156 }
157 return 0;
158 }
159
160 protected boolean playerNeedsHelp() {
161 return getNeededFuel() > 0 || getNeededSupplies() > 0;
162 }
163
164 protected void cargoPodsScam() {
165 Misc.adjustRep(REP_SCAM, RepLevel.INHOSPITABLE, fleet.getFaction().getId(),
166 REP_SCAM, RepLevel.INHOSPITABLE, person, text);
167
168 unrespond();
169 }
170
171
172 protected boolean isCargoPodsScam () {
173 for (SectorEntityToken entity : fleet.getContainingLocation().getAllEntities()) {
174 if (Entities.CARGO_PODS.equals(entity.getCustomEntityType())) {
175 VisibilityLevel level = entity.getVisibilityLevelTo(fleet);
176 if (level == VisibilityLevel.COMPOSITION_DETAILS ||
177 level == VisibilityLevel.COMPOSITION_AND_FACTION_DETAILS) {
178 if (entity.getCargo().getFuel() >= 10 || entity.getCargo().getSupplies() >= 10) {
179 return true;
180 }
181 }
182 }
183 }
184 return false;
185 }
186
187 protected int getNeededFuel() {
188 float returnDistLY = Misc.getDistanceLY(new Vector2f(), playerFleet.getLocationInHyperspace());
189 int fuel = (int) (returnDistLY * Math.max(1, playerFleet.getLogistics().getFuelCostPerLightYear()));
190 fuel *= 0.75f;
191 if (fuel < 10) fuel = 10;
192 fuel -= playerCargo.getFuel();
193 if (fuel < 0) fuel = 0;
194
195 return fuel;
196 }
197
198 protected int getNeededSupplies() {
199 //int supplies = (int) (playerCargo.getMaxCapacity() * 0.25f);
200 float supplies = 0f;
201 for (FleetMemberAPI member : playerFleet.getFleetData().getMembersListCopy()) {
202 supplies += member.getStats().getSuppliesPerMonth().getModifiedValue();
203 }
204 //supplies *= 2;
205
206 if (supplies < 5) supplies = 5;
207 supplies -= playerCargo.getSupplies();
208 if (supplies < 0) supplies = 0;
209
210 return (int) supplies;
211 }
212
213 protected float getRepPenalty() {
214 float uses = getNumUses();
215 return REP_PER_USE * (uses + 1);
216 }
217
218 protected void didNotNeedHelp() {
219 float penalty = getRepPenalty();
220 Misc.adjustRep(penalty, RepLevel.INHOSPITABLE, fleet.getFaction().getId(),
221 penalty * 2f, RepLevel.INHOSPITABLE, person, text);
222
223 unrespond();
224 }
225
226 protected void neverMind() {
227 float penalty = getRepPenalty();
228 Misc.adjustRep(penalty * 0.5f, RepLevel.INHOSPITABLE, fleet.getFaction().getId(),
229 penalty, RepLevel.INHOSPITABLE, person, text);
230 }
231
232 protected int getMaxFuel() {
233 return (int) Math.max(10, (fleet.getCargo().getMaxFuel() * 0.25f));
234 }
235 protected int getMaxSupplies() {
236 return (int) Math.max(10, (fleet.getCargo().getMaxCapacity() * 0.1f));
237 }
238
239 protected void pay() {
240 //int credits = getPayment();
241 int credits = (int) person.getMemoryWithoutUpdate().getFloat("$distressPayment");
242 playerCargo.getCredits().subtract(credits);
244
245 float uses = getNumUses();
246 if (uses <= 4) {
247 Misc.adjustRep(0.01f, RepLevel.WELCOMING, fleet.getFaction().getId(),
248 0.02f, RepLevel.WELCOMING, person, text);
249 }
250 }
251
252 protected void thank() {
253 float penalty = getRepPenalty();
254 Misc.adjustRep(penalty, RepLevel.INHOSPITABLE, fleet.getFaction().getId(),
255 penalty * 2f, RepLevel.INHOSPITABLE, person, text);
256 }
257
258 protected void acceptAid() {
259 int fuel = getNeededFuel();
260 int supplies = getNeededSupplies();
261
262 int maxFuel = getMaxFuel();
263 int maxSupplies = getMaxSupplies();
264
265 if (fuel > maxFuel) fuel = maxFuel;
266 if (supplies > maxSupplies) supplies = maxSupplies;
267
268// float penalty = getRepPenalty();
269// Misc.adjustRep(penalty, RepLevel.INHOSPITABLE, fleet.getFaction().getId(),
270// penalty * 2f, RepLevel.INHOSPITABLE, person, text);
271
272 if (fuel > 0) {
273 playerCargo.addFuel(fuel);
274 AddRemoveCommodity.addCommodityGainText(Commodities.FUEL, fuel, text);
275 }
276 if (supplies > 0) {
277 playerCargo.addSupplies(supplies);
278 AddRemoveCommodity.addCommodityGainText(Commodities.SUPPLIES, supplies, text);
279 }
280
281 unrespond();
282 }
283
284 protected void unrespond() {
285 Misc.clearTarget(fleet, true);
286 Misc.makeUnimportant(fleet, "distressResponse");
287 fleet.getMemoryWithoutUpdate().unset("$distressResponse");
288 Misc.giveStandardReturnToSourceAssignments(fleet);
289 }
290
291
292
293}
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
static SettingsAPI getSettings()
Definition Global.java:51
static SectorAPI getSector()
Definition Global.java:59
static void addCommodityGainText(String commodityId, int quantity, TextPanelAPI text)
static void addCreditsLossText(int credits, TextPanelAPI text)
static MemoryAPI getEntityMemory(Map< String, MemoryAPI > memoryMap)
boolean execute(String ruleId, InteractionDialogAPI dialog, List< Token > params, Map< String, MemoryAPI > memoryMap)
CommoditySpecAPI getCommoditySpec(String commodityId)