-
Notifications
You must be signed in to change notification settings - Fork 3
/
BuildInSelector.java
243 lines (225 loc) · 9.92 KB
/
BuildInSelector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package progsmod.data.campaign.rulecmd.ui.plugins;
import java.util.BitSet;
import java.util.List;
import com.fs.starfarer.api.Global;
import com.fs.starfarer.api.campaign.InteractionDialogAPI;
import com.fs.starfarer.api.campaign.SectorEntityToken;
import com.fs.starfarer.api.campaign.CampaignUIAPI.CoreUITradeMode;
import com.fs.starfarer.api.combat.ShipAPI;
import com.fs.starfarer.api.combat.ShipVariantAPI;
import com.fs.starfarer.api.fleet.FleetMemberAPI;
import com.fs.starfarer.api.loading.HullModSpecAPI;
import com.fs.starfarer.api.ui.CustomPanelAPI;
import com.fs.starfarer.api.ui.TooltipMakerAPI;
import com.fs.starfarer.api.util.Misc;
import progsmod.data.campaign.rulecmd.delegates.BuildInSModDelegate;
import progsmod.data.campaign.rulecmd.ui.Button;
import progsmod.data.campaign.rulecmd.ui.HullModButton;
import progsmod.data.campaign.rulecmd.ui.LabelWithVariables;
import progsmod.data.campaign.rulecmd.ui.PanelCreator.PanelCreatorData;
import progsmod.data.campaign.rulecmd.util.TempShipMaker;
import util.SModUtils;
public class BuildInSelector extends Selector<HullModButton> {
// 1 variable: XP
private LabelWithVariables<Integer> xpLabel;
// 2 variables: # selected, max allowed
private LabelWithVariables<Integer> countLabel;
private int firstIndexToBeCounted;
private Button showRecentButton;
private FleetMemberAPI fleetMember;
private ShipVariantAPI checkerVariant;
private ShipVariantAPI originalVariant;
private BuildInSModDelegate delegate;
private TooltipMakerAPI tooltipMaker;
private CustomPanelAPI panel;
public void init(
BuildInSModDelegate delegate,
PanelCreatorData<List<HullModButton>> data,
LabelWithVariables<Integer> xpLabel,
LabelWithVariables<Integer> countLabel,
Button showRecentButton,
FleetMemberAPI fleetMember,
ShipVariantAPI variant,
int firstIndexToBeCounted) {
super.init(data.created);
this.delegate = delegate;
this.panel = data.panel;
this.tooltipMaker = data.tooltipMaker;
this.xpLabel = xpLabel;
this.countLabel = countLabel;
this.showRecentButton = showRecentButton;
this.originalVariant = variant;
checkerVariant = variant.clone();
this.fleetMember = fleetMember;
this.firstIndexToBeCounted = firstIndexToBeCounted;
updateItems();
}
public void addNewItems(List<HullModButton> items) {
this.items.addAll(items);
updateItems();
}
private void updateItems() {
// Disable all of the unapplicable entries
BitSet unapplicable = disableUnapplicable();
int xp = xpLabel.getVar(0);
int count = countLabel.getVar(0);
int maxCount = countLabel.getVar(1);
for (int i = 0; i < items.size(); i++) {
HullModButton button = items.get(i);
if (unapplicable.get(i)) {
continue;
}
// Costs too much
if (button.data.cost > xp) {
disable(i, button.getDefaultDescription(), true);
}
// Already at limit
else if (count >= maxCount && i >= firstIndexToBeCounted) {
disable(i, button.getDefaultDescription(), false);
}
// No reason to be disabled; enable it
else {
enable(i, button.getDefaultDescription());
}
}
}
/** Disable an item, but only if it isn't already selected. */
private void disable(int index, String reason, boolean highlight) {
HullModButton item = items.get(index);
// Ignore already selected items
if (item.isSelected()) {
return;
}
item.disable(reason, highlight);
}
/** Enable an item. */
private void enable(int index, String reason) {
items.get(index).enable(reason);
}
/** Returns a set of indices of buttons that were disabled by this
* function. */
private BitSet disableUnapplicable() {
boolean checkedEntriesChanged = true;
BitSet disabledIndices = new BitSet();
InteractionDialogAPI dialog = Global.getSector().getCampaignUI().getCurrentInteractionDialog();
SectorEntityToken interactionTarget = dialog == null ? null : dialog.getInteractionTarget();
while (checkedEntriesChanged) {
ShipAPI checkerShip = TempShipMaker.makeShip(checkerVariant, fleetMember);
// Since hull mods may have dependencies, some checked entries may
// need to be unchecked.
// Since dependencies can be chained, we need to do this in a loop.
// (# of loops is bounded by # of checked entries as well as
// longest hull mod dependency chain)
checkedEntriesChanged = false;
for (int i = firstIndexToBeCounted; i < items.size(); i++) {
HullModButton button = items.get(i);
HullModSpecAPI hullMod = Global.getSettings().getHullModSpec(button.data.id);
boolean shouldDisable = false;
String disableText = null;
if (!hullMod.getEffect().isApplicableToShip(checkerShip)) {
String reason = hullMod.getEffect().getUnapplicableReason(checkerShip);
// Can build in any number of logistics hull mods
// Don't use s-mods in the check ship as we want to be able to tell when incompatibilities arise
// via forcible removing of non s-mods
if (reason != null && !reason.startsWith("Maximum of 2 non-built-in \"Logistics\"")) {
disableText = SModUtils.shortenText(reason, button.description);
shouldDisable = true;
}
}
else if (interactionTarget != null && interactionTarget.getMarket() != null) {
CoreUITradeMode tradeMode = CoreUITradeMode.valueOf(interactionTarget.getMemory().getString("$tradeMode"));
if (!hullMod.getEffect().canBeAddedOrRemovedNow(checkerShip, interactionTarget.getMarket(), tradeMode)) {
String reason = hullMod.getEffect().getCanNotBeInstalledNowReason(checkerShip, interactionTarget.getMarket(), tradeMode);
shouldDisable = true;
disableText = SModUtils.shortenText(
reason,
button.description);
}
}
if (hullMod.hasTag("no_build_in") && !SModUtils.Constants.IGNORE_NO_BUILD_IN) {
shouldDisable = true;
disableText = hullMod.getDisplayName() + " can't be built in";
}
if (shouldDisable) {
if (button.isSelected()) {
forceDeselect(i);
checkedEntriesChanged = true;
}
if (disableText == null) {
disableText = "Can't build in (no reason given, default message)";
}
disable(i, disableText, true);
disabledIndices.set(i);
}
}
}
return disabledIndices;
}
@Override
public void advance(float amount) {
// Check if the show all button has been pressed
if (showRecentButton.isSelected()) {
showRecentButton.deselect();
showRecentButton.disable();
delegate.showRecentPressed(panel, tooltipMaker);
}
}
@Override
protected void onSelected(int index) {
xpLabel.changeVar(0, xpLabel.getVar(0) - items.get(index).data.cost);
if (index >= firstIndexToBeCounted) {
countLabel.changeVar(0, countLabel.getVar(0) + 1);
}
String hullModId = items.get(index).data.id;
checkerVariant.addMod(hullModId);
if (testForDesync()) {
forceDeselect(index);
Global.getSector().getCampaignUI().getMessageDisplay().addMessage("Can't build in due to custom hull mod incompatibility", Misc.getNegativeHighlightColor());
}
else {
updateItems();
}
}
/** Tests if a ship made from checkerVariant still has all the hull mods that checkerVariant does.
* If not, this likely means that a mod forcibly removed some other hull mod(s) due to mod incompatibilities,
* so adding this hull mod would not be safe. */
private boolean testForDesync() {
ShipVariantAPI cloneVariant = checkerVariant.clone();
TempShipMaker.makeShip(cloneVariant, fleetMember);
if (cloneVariant.hasHullMod("ML_incompatibleHullmodWarning")) {
return true;
}
for (String id : checkerVariant.getHullMods()) {
if (!cloneVariant.hasHullMod(id)) {
return true;
}
}
return false;
}
@Override
protected void onDeselected(int index) {
xpLabel.changeVar(0, xpLabel.getVar(0) + items.get(index).data.cost);
if (index >= firstIndexToBeCounted) {
countLabel.changeVar(0, countLabel.getVar(0) - 1);
}
String hullModId = items.get(index).data.id;
// Don't remove hull mods that were already on the ship
if (!originalVariant.hasHullMod(hullModId)) {
checkerVariant.removeMod(hullModId);
}
updateItems();
}
@Override
protected void forceDeselect(int index) {
super.forceDeselect(index);
xpLabel.changeVar(0, xpLabel.getVar(0) + items.get(index).data.cost);
if (index >= firstIndexToBeCounted) {
countLabel.changeVar(0, countLabel.getVar(0) - 1);
}
String hullModId = items.get(index).data.id;
// Don't remove hull mods that were already on the ship
if (!originalVariant.hasHullMod(hullModId)) {
checkerVariant.removeMod(hullModId);
}
}
}