-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathUnit.java
4728 lines (4383 loc) · 186 KB
/
Unit.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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Unit.java
*
* Copyright (C) 2016 MegaMek team
* Copyright (c) 2009 Jay Lawson <jaylawson39 at yahoo.com>. 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 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with MekHQ. If not, see <http://www.gnu.org/licenses/>.
*/
package mekhq.campaign.unit;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import mekhq.campaign.finances.Money;
import mekhq.campaign.log.ServiceLogger;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import megamek.common.ASFBay;
import megamek.common.Aero;
import megamek.common.AmmoType;
import megamek.common.BattleArmor;
import megamek.common.BattleArmorBay;
import megamek.common.Bay;
import megamek.common.BayType;
import megamek.common.CargoBay;
import megamek.common.Compute;
import megamek.common.ConvFighter;
import megamek.common.CriticalSlot;
import megamek.common.Dropship;
import megamek.common.Engine;
import megamek.common.Entity;
import megamek.common.EntityMovementMode;
import megamek.common.EntityWeightClass;
import megamek.common.EquipmentType;
import megamek.common.FighterSquadron;
import megamek.common.HeavyVehicleBay;
import megamek.common.IArmorState;
import megamek.common.ILocationExposureStatus;
import megamek.common.IPlayer;
import megamek.common.ITechnology;
import megamek.common.Infantry;
import megamek.common.InfantryBay;
import megamek.common.InsulatedCargoBay;
import megamek.common.Jumpship;
import megamek.common.LAMPilot;
import megamek.common.LandAirMech;
import megamek.common.LightVehicleBay;
import megamek.common.LiquidCargoBay;
import megamek.common.LivestockCargoBay;
import megamek.common.LocationFullException;
import megamek.common.Mech;
import megamek.common.MechBay;
import megamek.common.MiscType;
import megamek.common.Mounted;
import megamek.common.PillionSeatCargoBay;
import megamek.common.Player;
import megamek.common.Protomech;
import megamek.common.ProtomechBay;
import megamek.common.QuadMech;
import megamek.common.QuadVee;
import megamek.common.RefrigeratedCargoBay;
import megamek.common.SimpleTechLevel;
import megamek.common.SmallCraft;
import megamek.common.SmallCraftBay;
import megamek.common.SpaceStation;
import megamek.common.StandardSeatCargoBay;
import megamek.common.SupportTank;
import megamek.common.Tank;
import megamek.common.TargetRoll;
import megamek.common.TechConstants;
import megamek.common.VTOL;
import megamek.common.Warship;
import megamek.common.WeaponType;
import megamek.common.annotations.Nullable;
import megamek.common.logging.LogLevel;
import megamek.common.options.IOption;
import megamek.common.options.IOptionGroup;
import megamek.common.options.OptionsConstants;
import megamek.common.options.PilotOptions;
import megamek.common.weapons.InfantryAttack;
import megamek.common.weapons.bayweapons.BayWeapon;
import megamek.common.weapons.infantry.InfantryWeapon;
import mekhq.MekHQ;
import mekhq.MekHqXmlSerializable;
import mekhq.MekHqXmlUtil;
import mekhq.Utilities;
import mekhq.Version;
import mekhq.campaign.Campaign;
import mekhq.campaign.event.PersonCrewAssignmentEvent;
import mekhq.campaign.event.PersonTechAssignmentEvent;
import mekhq.campaign.event.UnitArrivedEvent;
import mekhq.campaign.parts.AeroHeatSink;
import mekhq.campaign.parts.AeroLifeSupport;
import mekhq.campaign.parts.AeroSensor;
import mekhq.campaign.parts.Armor;
import mekhq.campaign.parts.Avionics;
import mekhq.campaign.parts.BaArmor;
import mekhq.campaign.parts.BattleArmorSuit;
import mekhq.campaign.parts.BayDoor;
import mekhq.campaign.parts.Cubicle;
import mekhq.campaign.parts.DropshipDockingCollar;
import mekhq.campaign.parts.EnginePart;
import mekhq.campaign.parts.FireControlSystem;
import mekhq.campaign.parts.InfantryArmorPart;
import mekhq.campaign.parts.InfantryMotiveType;
import mekhq.campaign.parts.LandingGear;
import mekhq.campaign.parts.MekActuator;
import mekhq.campaign.parts.MekCockpit;
import mekhq.campaign.parts.MekGyro;
import mekhq.campaign.parts.MekLifeSupport;
import mekhq.campaign.parts.MekLocation;
import mekhq.campaign.parts.MekSensor;
import mekhq.campaign.parts.MissingAeroHeatSink;
import mekhq.campaign.parts.MissingAeroLifeSupport;
import mekhq.campaign.parts.MissingAeroSensor;
import mekhq.campaign.parts.MissingAvionics;
import mekhq.campaign.parts.MissingBattleArmorSuit;
import mekhq.campaign.parts.MissingBayDoor;
import mekhq.campaign.parts.MissingCubicle;
import mekhq.campaign.parts.MissingDropshipDockingCollar;
import mekhq.campaign.parts.MissingEnginePart;
import mekhq.campaign.parts.MissingFireControlSystem;
import mekhq.campaign.parts.MissingLandingGear;
import mekhq.campaign.parts.MissingMekActuator;
import mekhq.campaign.parts.MissingMekCockpit;
import mekhq.campaign.parts.MissingMekGyro;
import mekhq.campaign.parts.MissingMekLifeSupport;
import mekhq.campaign.parts.MissingMekLocation;
import mekhq.campaign.parts.MissingMekSensor;
import mekhq.campaign.parts.MissingPart;
import mekhq.campaign.parts.MissingProtomekArmActuator;
import mekhq.campaign.parts.MissingProtomekJumpJet;
import mekhq.campaign.parts.MissingProtomekLegActuator;
import mekhq.campaign.parts.MissingProtomekLocation;
import mekhq.campaign.parts.MissingProtomekSensor;
import mekhq.campaign.parts.MissingQuadVeeGear;
import mekhq.campaign.parts.MissingRotor;
import mekhq.campaign.parts.MissingSpacecraftEngine;
import mekhq.campaign.parts.MissingThrusters;
import mekhq.campaign.parts.MissingTurret;
import mekhq.campaign.parts.MissingVeeSensor;
import mekhq.campaign.parts.MissingVeeStabiliser;
import mekhq.campaign.parts.MotiveSystem;
import mekhq.campaign.parts.Part;
import mekhq.campaign.parts.PodSpace;
import mekhq.campaign.parts.ProtomekArmActuator;
import mekhq.campaign.parts.ProtomekArmor;
import mekhq.campaign.parts.ProtomekJumpJet;
import mekhq.campaign.parts.ProtomekLegActuator;
import mekhq.campaign.parts.ProtomekLocation;
import mekhq.campaign.parts.ProtomekSensor;
import mekhq.campaign.parts.QuadVeeGear;
import mekhq.campaign.parts.Refit;
import mekhq.campaign.parts.Rotor;
import mekhq.campaign.parts.SpacecraftEngine;
import mekhq.campaign.parts.StructuralIntegrity;
import mekhq.campaign.parts.TankLocation;
import mekhq.campaign.parts.Thrusters;
import mekhq.campaign.parts.TransportBayPart;
import mekhq.campaign.parts.Turret;
import mekhq.campaign.parts.TurretLock;
import mekhq.campaign.parts.VeeSensor;
import mekhq.campaign.parts.VeeStabiliser;
import mekhq.campaign.parts.equipment.AmmoBin;
import mekhq.campaign.parts.equipment.BattleArmorAmmoBin;
import mekhq.campaign.parts.equipment.BattleArmorEquipmentPart;
import mekhq.campaign.parts.equipment.EquipmentPart;
import mekhq.campaign.parts.equipment.HeatSink;
import mekhq.campaign.parts.equipment.InfantryWeaponPart;
import mekhq.campaign.parts.equipment.JumpJet;
import mekhq.campaign.parts.equipment.LargeCraftAmmoBin;
import mekhq.campaign.parts.equipment.MASC;
import mekhq.campaign.parts.equipment.MissingAmmoBin;
import mekhq.campaign.parts.equipment.MissingBattleArmorEquipmentPart;
import mekhq.campaign.parts.equipment.MissingEquipmentPart;
import mekhq.campaign.parts.equipment.MissingHeatSink;
import mekhq.campaign.parts.equipment.MissingJumpJet;
import mekhq.campaign.personnel.Person;
import mekhq.campaign.personnel.PersonnelOptions;
import mekhq.campaign.personnel.SkillType;
import mekhq.campaign.work.IAcquisitionWork;
import mekhq.campaign.work.IPartWork;
/**
* This is a wrapper class for entity, so that we can add some functionality to
* it
*
* @author Jay Lawson <jaylawson39 at yahoo.com>
*/
public class Unit implements MekHqXmlSerializable, ITechnology {
public static final int SITE_FIELD = 0;
public static final int SITE_MOBILE_BASE = 1;
public static final int SITE_BAY = 2;
public static final int SITE_FACILITY = 3;
public static final int SITE_FACTORY = 4;
public static final int SITE_N = 5;
// To be used for transport and cargo reports
public static final int ETYPE_MOTHBALLED = -9876;
public static final int TECH_WORK_DAY = 480;
protected Entity entity;
private int site;
private boolean salvaged;
private UUID id;
private int oldId;
private String fluffName;
//assignments
private int forceId;
protected int scenarioId;
private ArrayList<UUID> drivers;
private ArrayList<UUID> gunners;
private ArrayList<UUID> vesselCrew;
//this is the id of the tech officer in a superheavy tripod
private UUID techOfficer;
private UUID navigator;
//this is the id of the tech assigned for maintenance if any
private UUID tech;
//mothballing variables - if mothball time is not zero then mothballing/activating is in progress
private int mothballTime;
private boolean mothballed;
private int daysSinceMaintenance;
private int daysActivelyMaintained;
private int astechDaysMaintained;
//old ids for reverse compatibility
private ArrayList<Integer> oldDrivers;
private ArrayList<Integer> oldGunners;
private ArrayList<Integer> oldVesselCrew;
private Integer oldNavigator;
private Campaign campaign;
private ArrayList<Part> parts;
private String lastMaintenanceReport;
private ArrayList<PodSpace> podSpace;
private Refit refit;
//a made-up person to handle repairs on Large Craft
private Person engineer;
//for backwards compatibility with 0.1.8, but otherwise is no longer used
@SuppressWarnings("unused")
private int pilotId = -1;
private String history;
//for delivery
protected int daysToArrival;
private MothballInfo mothballInfo;
public Unit() {
this(null, null);
}
public Unit(Entity en, Campaign c) {
this.entity = en;
if (entity != null) {
entity.setCamoCategory(null);
entity.setCamoFileName(null);
}
this.site = SITE_BAY;
this.salvaged = false;
this.campaign = c;
this.parts = new ArrayList<>();
this.podSpace = new ArrayList<>();
this.drivers = new ArrayList<>();
this.gunners = new ArrayList<>();
this.vesselCrew = new ArrayList<>();
this.navigator = null;
this.tech = null;
this.mothballTime = 0;
this.mothballed = false;
this.oldDrivers = new ArrayList<>();
this.oldGunners = new ArrayList<>();
this.oldVesselCrew = new ArrayList<>();
this.oldNavigator = -1;
scenarioId = -1;
this.refit = null;
this.engineer = null;
this.history = "";
this.daysSinceMaintenance = 0;
this.daysActivelyMaintained = 0;
this.astechDaysMaintained = 0;
this.lastMaintenanceReport = null;
this.fluffName = "";
reCalc();
}
public static String getDamageStateName(int i) {
switch(i) {
case Entity.DMG_NONE:
return "Undamaged";
case Entity.DMG_LIGHT:
return "Light Damage";
case Entity.DMG_MODERATE:
return "Moderate Damage";
case Entity.DMG_HEAVY:
return "Heavy Damage";
case Entity.DMG_CRIPPLED:
return "Crippled";
default:
return "Unknown";
}
}
public Campaign getCampaign() {
return campaign;
}
public void setCampaign(Campaign c) {
campaign = c;
}
/**
* A convenience function to tell whether the unit can be acted upon
* e.g. assigned pilots, techs, repaired, etc.
* @return
*/
public boolean isAvailable() {
return isAvailable(false);
}
/**
* A convenience function to tell whether the unit can be acted upon
* e.g. assigned pilots, techs, repaired, etc.
* @return
*/
public boolean isAvailable(boolean ignoreRefit) {
if (ignoreRefit) {
return isPresent() && !isDeployed() && !isMothballing() && !isMothballed();
}
return isPresent() && !isDeployed() && !isRefitting() && !isMothballing() && !isMothballed();
}
public String getStatus() {
if(isMothballing()) {
if(isMothballed()) {
return "Activating (" + getMothballTime() + "m)";
} else {
return "Mothballing (" + getMothballTime() + "m)";
}
}
if(isMothballed()) {
return "Mothballed";
}
if(isDeployed()) {
return "Deployed";
}
if(!isPresent()) {
return "In transit (" + getDaysToArrival() + " days)";
}
if(isRefitting()) {
return "Refitting";
}
if(!isRepairable()) {
return "Salvage";
}
else if(!isFunctional()) {
return "Inoperable";
}
else {
return getDamageStateName(getDamageState());
}
}
public void reCalc() {
// Do nothing.
}
public void setEntity(Entity en) {
//if there is already an entity, then make sure this
//one gets some of the same things set
if(null != this.entity) {
en.setId(this.entity.getId());
en.duplicateMarker = this.entity.duplicateMarker;
}
this.entity = en;
}
public Entity getEntity() {
return entity;
}
public UUID getId() {
return id;
}
public void setId(UUID i) {
this.id = i;
}
public int getSite() {
return site;
}
public void setSite(int i) {
this.site = i;
}
public boolean isSalvage() {
return salvaged;
}
public void setSalvage(boolean b) {
this.salvaged = b;
}
public String getHistory() {
return history;
}
public void setHistory(String s) {
this.history = s;
}
public static boolean isFunctional(Entity en) {
if (en instanceof Mech) {
// center torso bad?? head bad?
if (en.isLocationBad(Mech.LOC_CT)
|| en.isLocationBad(Mech.LOC_HEAD)) {
return false;
}
// engine destruction?
//cockpit hits
int engineHits = 0;
int cockpitHits = 0;
for (int i = 0; i < en.locations(); i++) {
engineHits += en.getHitCriticals(CriticalSlot.TYPE_SYSTEM,
Mech.SYSTEM_ENGINE, i);
cockpitHits += en.getHitCriticals(CriticalSlot.TYPE_SYSTEM,
Mech.SYSTEM_COCKPIT, i);
}
if (engineHits > 2) {
return false;
}
if(cockpitHits > 0) {
return false;
}
}
if (en instanceof Tank) {
for (int i = 0; i < en.locations(); i++) {
if(i == Tank.LOC_TURRET || i == Tank.LOC_TURRET_2) {
continue;
}
if (en.isLocationBad(i)) {
return false;
}
}
if(en instanceof VTOL) {
if(en.getWalkMP() <= 0) {
return false;
}
}
}
if(en instanceof Aero) {
if(en.getWalkMP() <= 0 && !(en instanceof Jumpship)) {
return false;
}
return ((Aero) en).getSI() > 0;
}
return true;
}
public boolean isFunctional() {
return isFunctional(entity);
}
public static boolean isRepairable(Entity en) {
if (en instanceof Mech) {
// you can repair anything so long as one point of CT is left
if (en.getInternal(Mech.LOC_CT) <= 0) {
return false;
}
}
if (en instanceof Tank) {
// can't repair a tank with a destroyed location
for (int i = 0; i < en.locations(); i++) {
if(i == Tank.LOC_TURRET || i == Tank.LOC_TURRET_2 || i == Tank.LOC_BODY) {
continue;
}
if (en.getInternal(i) <= 0) {
return false;
}
}
}
if(en instanceof Aero) {
return ((Aero) en).getSI() > 0;
}
return true;
}
public boolean isRepairable() {
return isRepairable(entity);
}
/**
* Determines if this unit can be serviced.
*
* @return <code>true</code> if the unit has parts that are salvageable or in
* need of repair.
*/
public boolean isServiceable() {
if (isSalvage() || !isRepairable()) {
return hasSalvageableParts();
} else {
return hasPartsNeedingFixing();
}
}
/**
* Is the given location on the entity destroyed?
*
* @param loc
* - an <code>int</code> for the location
* @return <code>true</code> if the location is destroyed
*/
public boolean isLocationDestroyed(int loc) {
if (loc > entity.locations() || loc < 0) {
return false;
}
/*boolean blownOff = entity.isLocationBlownOff(loc);
entity.setLocationBlownOff(loc, false);
boolean isDestroyed = entity.isLocationBad(loc);
entity.setLocationBlownOff(loc, blownOff);
return isDestroyed;
*/
return entity.isLocationTrulyDestroyed(loc);
}
public boolean isLocationBreached(int loc) {
return entity.getLocationStatus(loc) == ILocationExposureStatus.BREACHED;
}
public boolean hasBadHipOrShoulder(int loc) {
return entity instanceof Mech
&& (entity.getDamagedCriticals(CriticalSlot.TYPE_SYSTEM, Mech.ACTUATOR_HIP, loc) > 0
|| entity.getDamagedCriticals(CriticalSlot.TYPE_SYSTEM, Mech.ACTUATOR_SHOULDER, loc) > 0);
}
/**
* Run a diagnostic on this unit
* TODO: This is being called in the PersonnelTableModel after changes to the personnel
* attached to a unit, but I am not sure it needs to be. I don't think any parts check
* attached personnel. I think it could b removed, but I am going to leave it for the
* moment because I have made so many other changes in this version.
*/
public void runDiagnostic(boolean checkForDestruction) {
//need to set up an array of part ids to avoid concurrent modification
//problems because some updateCondition methods will remove the part and put
//in a new one
ArrayList<Part> tempParts = new ArrayList<>();
tempParts.addAll(parts);
for(Part part : tempParts) {
part.updateConditionFromEntity(checkForDestruction);
}
}
private boolean isPartAvailableForRepairs(IPartWork partWork, boolean onlyNotBeingWorkedOn) {
return (!onlyNotBeingWorkedOn || (onlyNotBeingWorkedOn && !partWork.isBeingWorkedOn()));
}
public ArrayList<IPartWork> getPartsNeedingFixing() {
return getPartsNeedingFixing(false);
}
/**
* Determines if this unit has parts in need of repair.
*
* @return <code>true</code> if the unit has parts that are in need of repair.
*/
public boolean hasPartsNeedingFixing() {
boolean onlyNotBeingWorkedOn = false;
for (Part part : parts) {
if (part.needsFixing() && isPartAvailableForRepairs(part, onlyNotBeingWorkedOn)) {
return true;
}
}
for (PodSpace pod : podSpace) {
if (pod.needsFixing() && isPartAvailableForRepairs(pod, onlyNotBeingWorkedOn)) {
return true;
}
}
return false;
}
public ArrayList<IPartWork> getPartsNeedingFixing(boolean onlyNotBeingWorkedOn) {
ArrayList<IPartWork> brokenParts = new ArrayList<>();
for(Part part: parts) {
if(part.needsFixing() && isPartAvailableForRepairs(part, onlyNotBeingWorkedOn)) {
brokenParts.add(part);
}
}
for (PodSpace pod : podSpace) {
if (pod.needsFixing() && isPartAvailableForRepairs(pod, onlyNotBeingWorkedOn)) {
brokenParts.add(pod);
}
}
return brokenParts;
}
public ArrayList<IPartWork> getSalvageableParts() {
return getSalvageableParts(false);
}
/**
* Determines if this unit has parts that are salvageable.
*
* @return <code>true</code> if the unit has parts that are salvageable.
*/
public boolean hasSalvageableParts() {
boolean onlyNotBeingWorkedOn = false;
for (Part part : parts) {
if (part.isSalvaging() && isPartAvailableForRepairs(part, onlyNotBeingWorkedOn)) {
return true;
}
}
for (PodSpace pod : podSpace) {
if (pod.hasSalvageableParts() && isPartAvailableForRepairs(pod, onlyNotBeingWorkedOn)) {
return true;
}
}
return false;
}
public ArrayList<IPartWork> getSalvageableParts(boolean onlyNotBeingWorkedOn) {
ArrayList<IPartWork> salvageParts = new ArrayList<>();
for(Part part: parts) {
if(part.isSalvaging() && isPartAvailableForRepairs(part, onlyNotBeingWorkedOn)) {
salvageParts.add(part);
}
}
for (PodSpace pod : podSpace) {
if (pod.hasSalvageableParts() && isPartAvailableForRepairs(pod, onlyNotBeingWorkedOn)) {
salvageParts.add(pod);
}
}
return salvageParts;
}
public ArrayList<IAcquisitionWork> getPartsNeeded() {
ArrayList<IAcquisitionWork> missingParts = new ArrayList<>();
if(isSalvage() || !isRepairable()) {
return missingParts;
}
boolean armorFound = false;
for(Part part: parts) {
if(part instanceof MissingPart && part.needsFixing() && null == ((MissingPart)part).findReplacement(false)) {
missingParts.add((MissingPart)part);
}
//we need to check for armor as well, but this one is funny because we dont want to
//check per location really, since armor can be used anywhere. So stop after we reach
//the first Armor needing replacement
//TODO: we need to adjust for patchwork armor, which can have different armor types by location
if(!armorFound && part instanceof Armor) {
Armor a = (Armor)part;
if(a.needsFixing() && !a.isEnoughSpareArmorAvailable()) {
missingParts.add(a);
armorFound = true;
}
}
if(!armorFound && part instanceof ProtomekArmor) {
ProtomekArmor a = (ProtomekArmor)part;
if(a.needsFixing() && !a.isEnoughSpareArmorAvailable()) {
missingParts.add(a);
armorFound = true;
}
}
if(!armorFound && part instanceof BaArmor) {
BaArmor a = (BaArmor)part;
if(a.needsFixing() && !a.isEnoughSpareArmorAvailable()) {
missingParts.add(a);
armorFound = true;
}
}
if(part instanceof AmmoBin && !((AmmoBin)part).isEnoughSpareAmmoAvailable()) {
missingParts.add((AmmoBin)part);
}
}
return missingParts;
}
public Money getValueOfAllMissingParts() {
Money value = Money.zero();
for(Part part : parts) {
if(part instanceof MissingAmmoBin) {
AmmoBin newBin = (AmmoBin) ((MissingAmmoBin)part).getNewEquipment();
value = value.plus(newBin.getValueNeeded());
}
if(part instanceof MissingPart) {
Part newPart = (Part)((MissingPart)part).getNewEquipment();
newPart.setBrandNew(!getCampaign().getCampaignOptions().useBLCSaleValue());
value = value.plus(newPart.getActualValue());
}
else if(part instanceof AmmoBin) {
value = value.plus(((AmmoBin)part).getValueNeeded());
}
else if(part instanceof Armor) {
value = value.plus(((Armor)part).getValueNeeded());
}
}
return value;
}
public void removePart(Part part) {
parts.remove(part);
}
/**
* @param m
* - A Mounted class to find crits for
* @return the number of crits existing for this Mounted
*/
public int getCrits(Mounted m) {
// TODO: I should probably just add this method to Entity in MM
// For the above, Mounted would probably be even better than Entity
int hits = 0;
for (int loc = 0; loc < entity.locations(); loc++) {
for (int i = 0; i < entity.getNumberOfCriticals(loc); i++) {
CriticalSlot slot = entity.getCritical(loc, i);
// ignore empty & system slots
if ((slot == null)
|| (slot.getType() != CriticalSlot.TYPE_EQUIPMENT)) {
continue;
}
Mounted m1 = slot.getMount();
Mounted m2 = slot.getMount2();
if (slot.getIndex() == -1) {
if ((m.equals(m1) || m.equals(m2))
&& (slot.isHit() || slot.isDestroyed())) {
hits++;
}
} else {
if (entity.getEquipmentNum(m) == slot.getIndex()
&& (slot.isHit() || slot.isDestroyed())) {
hits++;
}
}
}
}
return hits;
}
public boolean hasPilot() {
return null != entity.getCrew();
}
public String getPilotDesc() {
if (hasPilot()) {
return entity.getCrew().getName() + " "
+ entity.getCrew().getGunnery() + "/"
+ entity.getCrew().getPiloting();
}
return "NO PILOT";
}
/**
* produce a string in HTML that can be embedded in larger reports
*/
public String getDescHTML() {
String toReturn = "<b>" + getName() + "</b><br/>";
toReturn += getPilotDesc() + "<br/>";
if (isDeployed()) {
toReturn += "DEPLOYED!<br/>";
} else {
toReturn += "Site: " + getCurrentSiteName() + "<br/>";
}
return toReturn;
}
public TargetRoll getSiteMod() {
switch (site) {
case SITE_FIELD:
return new TargetRoll(2, "in the field");
case SITE_MOBILE_BASE:
return new TargetRoll(1, "field workshop");
case SITE_BAY:
return new TargetRoll(0, "transport bay");
case SITE_FACILITY:
return new TargetRoll(-2, "maintenance facility");
case SITE_FACTORY:
return new TargetRoll(-4, "factory");
default:
return new TargetRoll(0, "unknown location");
}
}
public static String getSiteName(int loc) {
switch (loc) {
case SITE_FIELD:
return "In the Field";
case SITE_MOBILE_BASE:
return "Field Workshop";
case SITE_BAY:
return "Transport Bay";
case SITE_FACILITY:
return "Maintenance Facility";
case SITE_FACTORY:
return "Factory";
default:
return "Unknown";
}
}
public String getCurrentSiteName() {
return getSiteName(site);
}
public boolean isDeployed() {
return scenarioId != -1;
}
public void undeploy() {
scenarioId = -1;
}
// TODO: Add support for advanced medical
public String checkDeployment() {
if (!isFunctional()) {
return "unit is not functional";
}
if (isUnmanned()) {
return "unit has no pilot";
}
if(isRefitting()) {
return "unit is being refit";
}
if(entity instanceof Tank
&& getActiveCrew().size() < getFullCrewSize()) {
return "This vehicle requires a crew of " + getFullCrewSize();
}
//Taharqa: I am not going to allow BattleArmor units with unmanned suits to deploy. It is
//possible to hack this to work in MM, but it becomes a serious problem when the unit becomes
//a total loss because the unmanned suits are also treated as destroyed. I tried hacking something
//together in ResolveScenarioTracker and decided that it was not right. If someone wants to deploy
//a non-full strength BA unit, they can salvage the suits that are unmanned and then they can deploy
//it
if(entity instanceof BattleArmor) {
for(int i = BattleArmor.LOC_TROOPER_1; i <= ((BattleArmor)entity).getTroopers(); i++) {
if(entity.getInternal(i) == 0) {
return "This BattleArmor unit has empty suits. Fill them with pilots or salvage them.";
}
}
}
return null;
}
/**
* Have to make one here because the one in MegaMek only returns true if
* operable
*
* @return
*/
public boolean hasTSM() {
for (Mounted mEquip : entity.getMisc()) {
MiscType mtype = (MiscType) mEquip.getType();
if (null != mtype && mtype.hasFlag(MiscType.F_TSM)) {
return true;
}
}
return false;
}
/**
* Returns true if there is at least one missing critical slot for
* this system in the given location
*/
public boolean isSystemMissing(int system, int loc) {
for (int i = 0; i < entity.getNumberOfCriticals(loc); i++) {
CriticalSlot ccs = entity.getCritical(loc, i);
if ((ccs != null) && (ccs.getType() == CriticalSlot.TYPE_SYSTEM)
&& (ccs.getIndex() == system) && ccs.isMissing()) {
return true;
}
}
return false;
}
/**
* Number of slots doomed, missing or destroyed in all locations
* @param type
* @param index
* @return
*/
public int getHitCriticals(int type, int index) {
int hits = 0;
for (int loc = 0; loc < entity.locations(); loc++) {
hits += getHitCriticals(type, index, loc);
}
return hits;
}
/**
* Number of slots doomed, missing or destroyed in a location
*/
public int getHitCriticals(int type, int index, int loc) {
int hits = 0;
Mounted m = null;
if (type == CriticalSlot.TYPE_EQUIPMENT) {
m = entity.getEquipment(index);
}
int numberOfCriticals = entity.getNumberOfCriticals(loc);
for (int i = 0; i < numberOfCriticals; i++) {
CriticalSlot ccs = entity.getCritical(loc, i);
// Check to see if this crit mounts the supplied item
// For systems, we can compare the index, but for equipment we
// need to get the Mounted that is mounted in that index and
// compare types. Superheavies may have two Mounted in each crit
if ((ccs != null) && (ccs.getType() == type)) {
if (ccs.isDestroyed()) {
if ((type == CriticalSlot.TYPE_SYSTEM) && (ccs.getIndex() == index)) {
hits++;
} else if ((type == CriticalSlot.TYPE_EQUIPMENT)
&& (null != m) && (m.equals(ccs.getMount()) || m.equals(ccs.getMount2()))) {
hits++;
}
}
}
}
return hits;
}
public void damageSystem(int type, int equipmentNum, int hits) {
//make sure we take note of existing hits to start and as we cycle through locations
int existingHits = getHitCriticals(type, equipmentNum);
int neededHits = Math.max(0, hits - existingHits);
int usedHits = 0;
for (int loc = 0; loc < getEntity().locations(); loc++) {
if(neededHits > usedHits) {
usedHits += damageSystem(type, equipmentNum, loc, neededHits-usedHits);
}
}
}
public int damageSystem(int type, int equipmentNum, int loc, int hits) {
int nhits = 0;
for (int i = 0; i < getEntity().getNumberOfCriticals(loc); i++) {
CriticalSlot cs = getEntity().getCritical(loc, i);
// ignore empty & system slots
if ((cs == null) || (cs.getType() != type)) {
continue;
}
Mounted mounted = getEntity().getEquipment(equipmentNum);
Mounted m1 = cs.getMount();
Mounted m2 = cs.getMount2();
if (cs.getIndex() == equipmentNum || (mounted != null && (mounted.equals(m1) || mounted.equals(m2)))) {
if(nhits < hits) {
cs.setHit(true);
cs.setDestroyed(true);
cs.setRepairable(true);
cs.setMissing(false);
nhits++;
}
}
}
return nhits;
}
public void destroySystem(int type, int equipmentNum) {
for (int loc = 0; loc < getEntity().locations(); loc++) {
destroySystem(type, equipmentNum, loc);
}
}
public void destroySystem(int type, int equipmentNum, int loc) {
for (int i = 0; i < getEntity().getNumberOfCriticals(loc); i++) {
CriticalSlot cs = getEntity().getCritical(loc, i);