-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathArmor.java
703 lines (616 loc) · 23 KB
/
Armor.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/*
* Copyright (c) 2009 Jay Lawson (jaylawson39 at yahoo.com). All rights reserved.
* Copyright (C) 2013-2025 The MegaMek Team. All Rights Reserved.
*
* This file is part of MekHQ.
*
* MekHQ is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License (GPL),
* version 3 or (at your option) any later version,
* as published by the Free Software Foundation.
*
* MekHQ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* A copy of the GPL should have been included with this project;
* if not, see <https://www.gnu.org/licenses/>.
*
* NOTICE: The MegaMek organization is a non-profit group of volunteers
* creating free software for the BattleTech community.
*
* MechWarrior, BattleMech, `Mech and AeroTech are registered trademarks
* of The Topps Company, Inc. All Rights Reserved.
*
* Catalyst Game Labs and the Catalyst Game Labs logo are trademarks of
* InMediaRes Productions, LLC.
*/
package mekhq.campaign.parts;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Objects;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import megamek.common.Dropship;
import megamek.common.Entity;
import megamek.common.EquipmentType;
import megamek.common.IArmorState;
import megamek.common.ITechnology;
import megamek.common.Tank;
import megamek.common.TargetRoll;
import megamek.common.TechAdvancement;
import megamek.common.annotations.Nullable;
import megamek.common.equipment.ArmorType;
import megamek.logging.MMLogger;
import mekhq.MekHQ;
import mekhq.campaign.Campaign;
import mekhq.campaign.finances.Money;
import mekhq.campaign.parts.enums.PartRepairType;
import mekhq.campaign.personnel.SkillType;
import mekhq.campaign.work.IAcquisitionWork;
import mekhq.campaign.work.WorkTime;
import mekhq.utilities.MHQXMLUtility;
import mekhq.utilities.ReportingUtilities;
/**
* @author Jay Lawson (jaylawson39 at yahoo.com)
*/
public class Armor extends Part implements IAcquisitionWork {
private static final MMLogger logger = MMLogger.create(Armor.class);
protected int type;
protected int amount;
protected int amountNeeded;
protected int location;
private boolean rear;
protected boolean clan;
public Armor() {
this(0, 0, 0, -1, false, false, null);
}
public Armor(int tonnage, int t, int points, int loc, boolean r, boolean clan, Campaign c) {
// Amount is used for armor quantity, not tonnage
super(tonnage, c);
this.type = t;
this.amount = points;
this.location = loc;
this.rear = r;
this.clan = clan;
this.name = "Armor";
if (type > -1) {
this.name += " (" + ArmorType.of(type, clan).getName() + ')';
}
}
@Override
public Armor clone() {
Armor clone = new Armor(0, type, amount, -1, false, clan, campaign);
clone.copyBaseData(this);
return clone;
}
@Override
public double getTonnage() {
return amount / getArmorPointsPerTon();
}
@Override
public Money getActualValue() {
return adjustCostsForCampaignOptions(Money.of(getTonnage() * ArmorType.of(type, clan).getCost()));
}
public double getTonnageNeeded() {
double armorPerTon = ArmorType.of(type, isClanTechBase()).getPointsPerTon();
if (type == EquipmentType.T_ARMOR_HARDENED) {
armorPerTon = 8.0;
}
return amountNeeded / armorPerTon;
}
public Money getValueNeeded() {
return adjustCostsForCampaignOptions(Money.of(getTonnageNeeded() * ArmorType.of(type, clan).getCost()));
}
@Override
public Money getStickerPrice() {
// always in 5-ton increments
return Money.of(5 * ArmorType.of(type, clan).getCost());
}
@Override
public Money getBuyCost() {
return getActualValue();
}
@Override
public String getDesc() {
if (isSalvaging()) {
return super.getDesc();
}
StringBuilder toReturn = new StringBuilder();
toReturn.append("<html><b>Replace ")
.append(getName());
if (!getCampaign().getCampaignOptions().isDestroyByMargin()) {
toReturn.append(" - ")
.append(ReportingUtilities.messageSurroundedBySpanWithColor(
SkillType.getExperienceLevelColor(getSkillMin()),
SkillType.getExperienceLevelName(getSkillMin()) + "+"));
}
toReturn.append("</b><br/>")
.append(getDetails())
.append("<br/>");
if (getSkillMin() <= SkillType.EXP_ELITE) {
toReturn.append(getTimeLeft())
.append(" minutes")
.append(null != getTech() ? " (scheduled)" : "")
.append(" <b>TN:</b> ")
.append(getAllMods(null).getValue() > -1 ? "+" : "")
.append(getAllMods(null).getValueAsString());
if (getMode() != WorkTime.NORMAL) {
toReturn.append(" <i>")
.append(getCurrentModeName())
.append( "</i>");
}
}
toReturn.append("</html>");
return toReturn.toString();
}
@Override
public String getDetails() {
return getDetails(true);
}
@Override
public String getDetails(boolean includeRepairDetails) {
StringBuilder toReturn = new StringBuilder();
if (null != unit) {
if (isSalvaging()) {
toReturn.append(unit.getEntity().getLocationName(location))
.append(rear ? " (Rear)" : "")
.append(", ")
.append(amount)
.append(amount == 1 ? " point" : " points");
} else {
toReturn.append(unit.getEntity().getLocationName(location))
.append(rear ? " (Rear)" : "")
.append(", ")
.append(amountNeeded)
.append(amountNeeded == 1 ? " point" : " points")
.append("<br/>");
int amountAvailable = getAmountAvailable();
if(amountAvailable == 0) {
toReturn.append(ReportingUtilities.messageSurroundedBySpanWithColor(
MekHQ.getMHQOptions().getFontColorNegativeHexColor(), "None in stock"));
} else if (amountAvailable < amountNeeded) {
toReturn.append(ReportingUtilities.spanOpeningWithCustomColor(
MekHQ.getMHQOptions().getFontColorNegativeHexColor()))
.append("Only ")
.append(amountAvailable)
.append(" in stock")
.append(ReportingUtilities.CLOSING_SPAN_TAG);
} else {
toReturn.append(ReportingUtilities.spanOpeningWithCustomColor(
MekHQ.getMHQOptions().getFontColorPositiveHexColor()))
.append(amountAvailable)
.append(" in stock")
.append(ReportingUtilities.CLOSING_SPAN_TAG);
}
PartInventory inventories = campaign.getPartInventory(getNewPart());
String orderTransitString = inventories.getTransitOrderedDetails();
if (!orderTransitString.isEmpty()) {
toReturn.append(ReportingUtilities.spanOpeningWithCustomColor(
MekHQ.getMHQOptions().getFontColorWarningHexColor()))
.append(" (")
.append(orderTransitString)
.append(")")
.append(ReportingUtilities.CLOSING_SPAN_TAG);
}
}
} else {
toReturn.append(amount)
.append(" points");
}
return toReturn.toString();
}
public int getType() {
return type;
}
public int getAmount() {
return amount;
}
public void addAmount(final int amount) {
this.amount += amount;
}
public int getAmountNeeded() {
return amountNeeded;
}
public int getTotalAmount() {
return amount + amountNeeded;
}
@Override
public int getLocation() {
return location;
}
@Override
public String getLocationName() {
return unit != null ? unit.getEntity().getLocationName(location) : null;
}
public boolean isRearMounted() {
return rear;
}
public void setAmount(int amount) {
this.amount = amount;
}
public void setAmountNeeded(int needed) {
this.amountNeeded = needed;
}
public boolean isSameType(Armor armor) {
if (getType() == EquipmentType.T_ARMOR_STANDARD
&& armor.getType() == EquipmentType.T_ARMOR_STANDARD) {
// standard armor is compatible between clan and IS
return true;
}
return getType() == armor.getType() && isClanTechBase() == armor.isClanTechBase();
}
@Override
public boolean isSamePartType(Part part) {
return (getClass() == part.getClass())
&& Objects.equals(getRefitUnit(), part.getRefitUnit())
&& isSameType((Armor) part);
}
@Override
public boolean isSameStatus(Part part) {
return this.getDaysToArrival() == part.getDaysToArrival();
}
@Override
public TechAdvancement getTechAdvancement() {
return ArmorType.of(type, clan).getTechAdvancement();
}
public double getArmorWeight(int points) {
// from megamek.common.Entity.getArmorWeight()
// this roundabout method is actually necessary to avoid rounding
// weirdness. Yeah, it's dumb.
double armorPerTon = ArmorType.of(getType(), isClan()).getPointsPerTon();
if (getType() == EquipmentType.T_ARMOR_HARDENED) {
armorPerTon = 8.0;
}
double armorWeight = points / armorPerTon;
armorWeight = Math.ceil(armorWeight * 2.0) / 2.0;
return armorWeight;
}
@Override
public void writeToXML(final PrintWriter pw, int indent) {
indent = writeToXMLBegin(pw, indent);
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "amount", amount);
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "type", type);
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "location", location);
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "rear", rear);
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "amountNeeded", amountNeeded);
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "clan", clan);
writeToXMLEnd(pw, indent);
}
@Override
protected void loadFieldsFromXmlNode(Node wn) {
NodeList nl = wn.getChildNodes();
for (int x = 0; x < nl.getLength(); x++) {
Node wn2 = nl.item(x);
try {
if (wn2.getNodeName().equalsIgnoreCase("amount")) {
amount = Integer.parseInt(wn2.getTextContent());
} else if (wn2.getNodeName().equalsIgnoreCase("type")) {
type = Integer.parseInt(wn2.getTextContent());
} else if (wn2.getNodeName().equalsIgnoreCase("location")) {
location = Integer.parseInt(wn2.getTextContent());
} else if (wn2.getNodeName().equalsIgnoreCase("amountNeeded")) {
amountNeeded = Integer.parseInt(wn2.getTextContent());
} else if (wn2.getNodeName().equalsIgnoreCase("rear")) {
rear = wn2.getTextContent().equalsIgnoreCase("true");
} else if (wn2.getNodeName().equalsIgnoreCase("clan")) {
clan = wn2.getTextContent().equalsIgnoreCase("true");
}
} catch (Exception e) {
logger.error("", e);
}
}
}
@Override
public void fix() {
if (unit.getEntity().isCapitalScale()) {
amountNeeded *= 10;
}
int amountFound = Math.min(getAmountAvailable(), amountNeeded);
int fixAmount = Math.min(amount +
// Make sure that we handle the capital scale conversion when setting the fix
// amount
(unit.getEntity().isCapitalScale() ? (amountFound / 10) : amountFound),
unit.getEntity().getOArmor(location, rear));
unit.getEntity().setArmor(fixAmount, location, rear);
changeAmountAvailable(-1 * amountFound);
updateConditionFromEntity(false);
skillMin = SkillType.EXP_GREEN;
shorthandedMod = 0;
}
@Override
public String find(int transitDays) {
Part newPart = getNewPart();
newPart.setBrandNew(true);
newPart.setDaysToArrival(transitDays);
if (campaign.getQuartermaster().buyPart(newPart, transitDays)) {
return "<font color='" + MekHQ.getMHQOptions().getFontColorPositiveHexColor()
+ "'><b> part found</b>.</font> It will be delivered in " + transitDays + " days.";
} else {
return "<font color='" + MekHQ.getMHQOptions().getFontColorNegativeHexColor()
+ "'><b> You cannot afford this part. Transaction cancelled</b>.</font>";
}
}
@Override
public Object getNewEquipment() {
return getNewPart();
}
@Override
public String failToFind() {
return "<font color='" + MekHQ.getMHQOptions().getFontColorNegativeHexColor()
+ "'><b> part not found</b>.</font>";
}
@Override
public MissingPart getMissingPart() {
// no such thing
return null;
}
@Override
public IAcquisitionWork getAcquisitionWork() {
return new Armor(0, type, (int) Math.round(5 * getArmorPointsPerTon()), -1, false, clan, campaign);
}
@Override
public void remove(boolean salvage) {
unit.getEntity().setArmor(IArmorState.ARMOR_DESTROYED, location, rear);
if (salvage) {
// Account for capital-scale units when warehouse armor is stored at standard
// scale.
if (unit.getEntity().isCapitalScale()) {
amount *= 10;
}
changeAmountAvailable(amount);
}
updateConditionFromEntity(false);
}
public int getBaseTimeFor(Entity entity) {
if (null != entity) {
if (entity instanceof Tank) {
return 3;
} else if (entity instanceof Dropship) {
return 15;
} else if (entity.isCapitalScale()) {
return 120;
}
}
// Meks, protomeks, battle armor, and normal aerospace
return 5;
}
@Override
public void updateConditionFromEntity(boolean checkForDestruction) {
if (isReservedForRefit()) {
return;
}
if (null == unit) {
return;
}
amount = unit.getEntity().getArmorForReal(location, rear);
if (amount < 0) {
amount = 0;
}
amountNeeded = unit.getEntity().getOArmor(location, rear) - amount;
}
@Override
public int getBaseTime() {
Entity entity = unit != null ? unit.getEntity() : null;
if (isSalvaging()) {
return getBaseTimeFor(entity) * amount;
}
return getBaseTimeFor(entity) * Math.min(amountNeeded, getAmountAvailable());
}
@Override
public int getDifficulty() {
return -2;
}
@Override
public boolean isSalvaging() {
return super.isSalvaging() && amount > 0;
}
@Override
public boolean needsFixing() {
return amountNeeded > 0;
}
@Override
public void updateConditionFromPart() {
if (null != unit) {
int armor = Math.min(amount, unit.getEntity().getOArmor(location, rear));
if (armor == 0) {
armor = IArmorState.ARMOR_DESTROYED;
}
unit.getEntity().setArmor(armor, location, rear);
}
}
@Override
public @Nullable String checkFixable() {
if (isSalvaging()) {
return null;
}
if (getAmountAvailable() == 0) {
return "No spare armor available";
}
if (isMountedOnDestroyedLocation()) {
return unit.getEntity().getLocationName(location) + " is destroyed.";
}
return null;
}
@Override
public boolean isMountedOnDestroyedLocation() {
return null != unit && unit.isLocationDestroyed(location);
}
@Override
public boolean onBadHipOrShoulder() {
return null != unit && unit.hasBadHipOrShoulder(location);
}
@Override
public String getAcquisitionDesc() {
String toReturn = "<html><font";
toReturn += ">";
toReturn += "<b>" + getAcquisitionDisplayName() + "</b> " + getAcquisitionBonus() + "<br/>";
toReturn += getAcquisitionExtraDesc() + "<br/>";
PartInventory inventories = campaign.getPartInventory(getAcquisitionPart());
toReturn += inventories.getTransitOrderedDetails() + "<br/>";
toReturn += adjustCostsForCampaignOptions(getStickerPrice()).toAmountAndSymbolString() + "<br/>";
toReturn += "</font></html>";
return toReturn;
}
@Override
public String getAcquisitionDisplayName() {
return getName();
}
@Override
public String getAcquisitionExtraDesc() {
return ((int) Math.round(getArmorPointsPerTon())) * 5 + " points (5 tons)";
}
@Override
public String getAcquisitionName() {
return getName();
}
@Override
public String getAcquisitionBonus() {
String bonus = getAllAcquisitionMods().getValueAsString();
if (getAllAcquisitionMods().getValue() > -1) {
bonus = '+' + bonus;
}
return '(' + bonus + ')';
}
@Override
public Part getAcquisitionPart() {
return getNewPart();
}
@Override
public TargetRoll getAllAcquisitionMods() {
TargetRoll target = new TargetRoll();
// Faction and Tech mod
if (isClanTechBase() && campaign.getCampaignOptions().getClanAcquisitionPenalty() > 0) {
target.addModifier(campaign.getCampaignOptions().getClanAcquisitionPenalty(), "clan-tech");
} else if (campaign.getCampaignOptions().getIsAcquisitionPenalty() > 0) {
target.addModifier(campaign.getCampaignOptions().getIsAcquisitionPenalty(), "Inner Sphere tech");
}
// availability mod
int avail = getAvailability();
int availabilityMod = Availability.getAvailabilityModifier(avail);
target.addModifier(availabilityMod, "availability (" + ITechnology.getRatingName(avail) + ')');
return target;
}
@Override
public int getSellableQuantity() {
return amount;
}
public double getArmorPointsPerTon() {
return ArmorType.of(type, clan).getPointsPerTon();
}
public Part getNewPart() {
return new Armor(0, type, (int) Math.round(5 * getArmorPointsPerTon()), -1, false, clan, campaign);
}
public boolean isEnoughSpareArmorAvailable() {
return getAmountAvailable() >= amountNeeded;
}
public int getAmountAvailable() {
Armor a = (Armor) campaign.getWarehouse().findSparePart(part -> (part instanceof Armor)
&& part.isPresent()
&& !part.isReservedForRefit()
&& isSameType((Armor) part));
return (a == null) ? 0 : a.getAmount();
}
public void changeAmountAvailable(int amount) {
Armor a = (Armor) campaign.getWarehouse().findSparePart(part -> (part instanceof Armor)
&& part.isPresent()
&& Objects.equals(getRefitUnit(), part.getRefitUnit())
&& isSameType((Armor) part));
if (null != a) {
a.setAmount(a.getAmount() + amount);
if (a.getAmount() <= 0) {
campaign.getWarehouse().removePart(a);
}
} else if (amount > 0) {
campaign.getQuartermaster()
.addPart(new Armor(getUnitTonnage(), type, amount, -1, false, isClanTechBase(), campaign), 0);
}
}
@Override
public String fail(int rating) {
skillMin = ++rating;
timeSpent = 0;
shorthandedMod = 0;
// if we are impossible to fix now, we should scrap this amount of armor
// from spares and start over
String scrap = "";
if (skillMin > SkillType.EXP_ELITE) {
scrap = " Armor supplies lost!";
if (isSalvaging()) {
remove(false);
} else {
skillMin = SkillType.EXP_GREEN;
changeAmountAvailable(
-1 * Math.min((unit.getEntity().isCapitalScale() ? (amountNeeded * 10) : amountNeeded),
getAmountAvailable()));
}
}
return " <font color='" + MekHQ.getMHQOptions().getFontColorNegativeHexColor() + "'><b> failed." + scrap
+ "</b></font>";
}
@Override
public String scrap() {
remove(false);
skillMin = SkillType.EXP_GREEN;
return ArmorType.of(type, clan).getName() + " armor scrapped.";
}
@Override
public boolean isInSupply() {
return amountNeeded <= getAmountAvailable();
}
@Override
public String getQuantityName(int quan) {
double totalTon = quan * getTonnage();
String report = DecimalFormat.getInstance().format(totalTon) + " tons of " + getName();
if (totalTon == 1.0) {
report = DecimalFormat.getInstance().format(totalTon) + " ton of " + getName();
}
return report;
}
@Override
public String getArrivalReport() {
double totalTon = quantity * getTonnage();
String report = getQuantityName(quantity);
if (totalTon == 1.0) {
report += " has arrived";
} else {
report += " have arrived";
}
return report;
}
@Override
public void doMaintenanceDamage(int d) {
int current = unit.getEntity().getArmor(location, rear);
if (d >= current) {
unit.getEntity().setArmor(IArmorState.ARMOR_DESTROYED, location, rear);
} else {
unit.getEntity().setArmor(current - d, location, rear);
}
updateConditionFromEntity(false);
}
@Override
public boolean isPriceAdjustedForAmount() {
return true;
}
public void changeType(int ty, boolean cl) {
this.type = ty;
this.clan = cl;
this.name = "Armor";
if (type > -1) {
this.name += " (" + ArmorType.of(type, clan).getName() + ')';
}
}
@Override
public PartRepairType getMRMSOptionType() {
return PartRepairType.ARMOUR;
}
@Override
public boolean isIntroducedBy(int year, boolean clan, int techFaction) {
return getIntroductionDate(clan, techFaction) <= year;
}
@Override
public boolean isExtinctIn(int year, boolean clan, int techFaction) {
return isExtinct(year, clan, techFaction);
}
}