Skip to content

Commit

Permalink
Add the ability to swap out instances of static classes
Browse files Browse the repository at this point in the history
  • Loading branch information
sixlettervariables committed Dec 16, 2020
1 parent 04d950d commit acb530c
Show file tree
Hide file tree
Showing 34 changed files with 360 additions and 286 deletions.
3 changes: 2 additions & 1 deletion MekHQ/src/mekhq/campaign/Campaign.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
import mekhq.campaign.universe.DefaultPlanetSelector;
import mekhq.campaign.universe.Era;
import mekhq.campaign.universe.Faction;
import mekhq.campaign.universe.Factions;
import mekhq.campaign.universe.IUnitGenerator;
import mekhq.campaign.universe.News;
import mekhq.campaign.universe.NewsItem;
Expand Down Expand Up @@ -3898,7 +3899,7 @@ public void setGMMode(boolean b) {
}

public Faction getFaction() {
return Faction.getFaction(factionCode);
return Factions.getInstance().getFaction(factionCode);
}

public String getFactionName() {
Expand Down
3 changes: 2 additions & 1 deletion MekHQ/src/mekhq/campaign/againstTheBot/AtBConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import mekhq.campaign.personnel.SkillType;
import mekhq.campaign.rating.IUnitRating;
import mekhq.campaign.universe.Faction;
import mekhq.campaign.universe.Factions;

/**
* @author Neoancient
Expand Down Expand Up @@ -291,7 +292,7 @@ public static int decodeWeightStr(String s, int i) {

public static String getParentFactionType(String factionCode) {
String org = AtBConfiguration.ORG_IS;
Faction faction = Faction.getFaction(factionCode);
Faction faction = Factions.getInstance().getFaction(factionCode);

if (faction.isComStar()) {
org = AtBConfiguration.ORG_CS;
Expand Down
5 changes: 3 additions & 2 deletions MekHQ/src/mekhq/campaign/finances/CurrencyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import mekhq.campaign.mission.AtBContract;
import mekhq.campaign.mission.Contract;
import mekhq.campaign.universe.Faction;
import mekhq.campaign.universe.Factions;
import mekhq.campaign.universe.PlanetarySystem;

import org.joda.money.CurrencyUnitDataProvider;
Expand Down Expand Up @@ -170,7 +171,7 @@ synchronized Currency getDefaultCurrency() {
for (Contract contract : this.campaign.getActiveContracts()) {
if (contract instanceof AtBContract) {
Currency currency = possibleCurrencies.getOrDefault(
Faction.getFaction(((AtBContract)contract).getEmployerCode()).getCurrencyCode(),
Factions.getInstance().getFaction(((AtBContract)contract).getEmployerCode()).getCurrencyCode(),
null);

if (currency != null) {
Expand Down Expand Up @@ -253,7 +254,7 @@ protected void registerCurrencies() {

// Adjust the currency start and end dates if needed by the
// start/end dates of the factions that use it
for (Faction faction : Faction.getFactions()) {
for (Faction faction : Factions.getInstance().getFactions()) {
if (faction.getCurrencyCode().equals(code)) {
if (faction.getStartYear() < startYear) {
startYear = faction.getStartYear();
Expand Down
29 changes: 15 additions & 14 deletions MekHQ/src/mekhq/campaign/market/ContractMarket.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import mekhq.campaign.personnel.SkillType;
import mekhq.campaign.rating.IUnitRating;
import mekhq.campaign.universe.Faction;
import mekhq.campaign.universe.Factions;
import mekhq.campaign.universe.PlanetarySystem;
import mekhq.campaign.universe.RandomFactionGenerator;
import mekhq.campaign.universe.Systems;
Expand Down Expand Up @@ -356,7 +357,7 @@ private void checkForSubcontracts(Campaign campaign, AtBContract contract, int u
contract.setEmployerCode(employer, campaign.getGameYear());
contract.setMissionType(findAtBMissionType(unitRatingMod,
RandomFactionGenerator.getInstance().getFactionHints()
.isISMajorPower(Faction.getFaction(contract.getEmployerCode()))));
.isISMajorPower(Factions.getInstance().getFaction(contract.getEmployerCode()))));

if (contract.getMissionType() == AtBContract.MT_PIRATEHUNTING) {
contract.setEnemyCode("PIR");
Expand All @@ -374,9 +375,9 @@ private void checkForSubcontracts(Campaign campaign, AtBContract contract, int u
* (ComStar, Mercs not under contract) are more likely to have garrison-type
* contracts and less likely to have battle-type contracts unless at war.
*/
if (RandomFactionGenerator.getInstance().getFactionHints().isNeutral(Faction.getFaction(employer)) &&
!RandomFactionGenerator.getInstance().getFactionHints().isAtWarWith(Faction.getFaction(employer),
Faction.getFaction(contract.getEnemyCode()), campaign.getLocalDate())) {
if (RandomFactionGenerator.getInstance().getFactionHints().isNeutral(Factions.getInstance().getFaction(employer)) &&
!RandomFactionGenerator.getInstance().getFactionHints().isAtWarWith(Factions.getInstance().getFaction(employer),
Factions.getInstance().getFaction(contract.getEnemyCode()), campaign.getLocalDate())) {
if (contract.getMissionType() == AtBContract.MT_PLANETARYASSAULT) {
contract.setMissionType(AtBContract.MT_GARRISONDUTY);
} else if (contract.getMissionType() == AtBContract.MT_RELIEFDUTY) {
Expand Down Expand Up @@ -430,9 +431,9 @@ private void checkForSubcontracts(Campaign campaign, AtBContract contract, int u
contract.calculateContract(campaign);

//Ralgith had a version of this then the PR got added. Commenting this Out.
/* contract.setName(Faction.getFaction(employer).getShortName() + "-" + String.format("%1$tY%1$tm", contract.getStartDate())
/* contract.setName(Factions.getInstance().getFaction(employer).getShortName() + "-" + String.format("%1$tY%1$tm", contract.getStartDate())
+ "-" + AtBContract.missionTypeNames[contract.getMissionType()]
+ "-" + Faction.getFaction(contract.getEnemyCode()).getShortName()
+ "-" + Factions.getInstance().getFaction(contract.getEnemyCode()).getShortName()
+ "-" + contract.getLength());*/

return contract;
Expand All @@ -444,7 +445,7 @@ protected AtBContract generateAtBSubcontract(Campaign campaign,
contract.setEmployerCode(parent.getEmployerCode(), campaign.getGameYear());
contract.setMissionType(findAtBMissionType(unitRatingMod,
RandomFactionGenerator.getInstance().getFactionHints()
.isISMajorPower(Faction.getFaction(contract.getEmployerCode()))));
.isISMajorPower(Factions.getInstance().getFaction(contract.getEmployerCode()))));

if (contract.getMissionType() == AtBContract.MT_PIRATEHUNTING)
contract.setEnemyCode("PIR");
Expand Down Expand Up @@ -612,13 +613,13 @@ public void setAllyRating(AtBContract contract, boolean isAttacker, int year) {
if (contract.getMissionType() == AtBContract.MT_PLANETARYASSAULT) {
mod += 1;
}
if (Faction.getFaction(contract.getEmployerCode()).isClan() && !isAttacker) {
if (Factions.getInstance().getFaction(contract.getEmployerCode()).isClan() && !isAttacker) {
//facing front-line units
mod += 1;
}
contract.setAllySkill(getSkillRating(Compute.d6(2) + mod));
if (year > 2950 && year < 3039 &&
!Faction.getFaction(contract.getEmployerCode()).isClan()) {
!Factions.getInstance().getFaction(contract.getEmployerCode()).isClan()) {
mod -= 1;
}
contract.setAllyQuality(getQualityRating(Compute.d6(2) + mod));
Expand All @@ -639,12 +640,12 @@ public void setEnemyRating(AtBContract contract, boolean isAttacker, int year) {
if (AtBContract.isMinorPower(contract.getEmployerCode())) {
mod -= 1;
}
if (Faction.getFaction(contract.getEmployerCode()).isClan()) {
if (Factions.getInstance().getFaction(contract.getEmployerCode()).isClan()) {
mod += isAttacker?2:4;
}
contract.setEnemySkill(getSkillRating(Compute.d6(2) + mod));
if (year > 2950 && year < 3039 &&
!Faction.getFaction(contract.getEnemyCode()).isClan()) {
!Factions.getInstance().getFaction(contract.getEnemyCode()).isClan()) {
mod -= 1;
}
contract.setEnemyQuality(getQualityRating(Compute.d6(2) + mod));
Expand Down Expand Up @@ -718,8 +719,8 @@ protected void setAtBContractClauses(AtBContract contract, int unitRatingMod, Ca
mods.mods[Compute.randomInt(4)] -= 1;
}

if (Faction.getFaction(contract.getEnemyCode()).isClan() &&
!Faction.getFaction(contract.getEmployerCode()).isClan()) {
if (Factions.getInstance().getFaction(contract.getEnemyCode()).isClan() &&
!Factions.getInstance().getFaction(contract.getEmployerCode()).isClan()) {
for (int i = 0; i < 4; i++)
if (i == CLAUSE_SALVAGE) mods.mods[i] -= 2;
else mods.mods[i] += 1;
Expand All @@ -740,7 +741,7 @@ protected void setAtBContractClauses(AtBContract contract, int unitRatingMod, Ca
}

if (RandomFactionGenerator.getInstance().getFactionHints()
.isISMajorPower(Faction.getFaction(contract.getEmployerCode()))) {
.isISMajorPower(Factions.getInstance().getFaction(contract.getEmployerCode()))) {
mods.mods[CLAUSE_SALVAGE] += -1;
mods.mods[CLAUSE_TRANSPORT] += 1;
}
Expand Down
3 changes: 2 additions & 1 deletion MekHQ/src/mekhq/campaign/market/UnitMarket.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import mekhq.campaign.mission.Mission;
import mekhq.campaign.rating.IUnitRating;
import mekhq.campaign.universe.Faction;
import mekhq.campaign.universe.Factions;
import mekhq.campaign.universe.IUnitGenerator;
import mekhq.campaign.universe.RandomFactionGenerator;
import mekhq.campaign.universe.UnitGeneratorParameters;
Expand Down Expand Up @@ -136,7 +137,7 @@ public void generateUnitOffers(Campaign campaign) {
if (campaign.getUnitRatingMod() >= IUnitRating.DRAGOON_B) {
Set<Faction> factions = campaign.getCurrentSystem().getFactionSet(campaign.getLocalDate());
String faction = Utilities.getRandomItem(factions).getShortName();
if (campaign.getFaction().isClan() || !Faction.getFaction(faction).isClan()) {
if (campaign.getFaction().isClan() || !Factions.getInstance().getFaction(faction).isClan()) {
addOffers(campaign, Compute.d6() - 3, UnitMarketType.FACTORY, UnitType.MEK,
faction, IUnitRating.DRAGOON_A, 6);
addOffers(campaign, Compute.d6() - 2, UnitMarketType.FACTORY, UnitType.TANK,
Expand Down
19 changes: 10 additions & 9 deletions MekHQ/src/mekhq/campaign/mission/AtBContract.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import mekhq.campaign.rating.IUnitRating;
import mekhq.campaign.unit.Unit;
import mekhq.campaign.universe.Faction;
import mekhq.campaign.universe.Factions;
import mekhq.campaign.universe.RandomFactionGenerator;

/**
Expand Down Expand Up @@ -340,7 +341,7 @@ public static int getEffectiveNumUnits(Campaign campaign) {
}

public static boolean isMinorPower(String fName) {
Faction faction = Faction.getFaction(fName);
Faction faction = Factions.getInstance().getFaction(fName);
if (null != faction) {
return !RandomFactionGenerator.getInstance().getFactionHints().isISMajorPower(faction) &&
!faction.isClan();
Expand Down Expand Up @@ -397,7 +398,7 @@ public void calculatePaymentMultiplier(Campaign campaign) {
break;
}

Faction employer = Faction.getFaction(employerCode);
Faction employer = Factions.getInstance().getFaction(employerCode);
if ((null != employer)
&& (RandomFactionGenerator.getInstance().getFactionHints().isISMajorPower(employer)
|| employer.isClan())) {
Expand Down Expand Up @@ -494,7 +495,7 @@ public void checkMorale(LocalDate today, int dragoonRating) {
isMinorPower(enemyCode) ||
enemyCode.equals("MERC")) {
mod -= 1;
} else if (Faction.getFaction(enemyCode).isClan()) {
} else if (Factions.getInstance().getFaction(enemyCode).isClan()) {
mod += 2;
}

Expand Down Expand Up @@ -1021,8 +1022,8 @@ public int findBigBattleType() {
public boolean contractExtended (Campaign campaign) {
if ((getMissionType() != MT_PIRATEHUNTING) && (getMissionType() != MT_RIOTDUTY)) {
String warName = RandomFactionGenerator.getInstance()
.getFactionHints().getCurrentWar(Faction.getFaction(getEmployerCode()),
Faction.getFaction(getEnemyCode()), campaign.getLocalDate());
.getFactionHints().getCurrentWar(Factions.getInstance().getFaction(getEmployerCode()),
Factions.getInstance().getFaction(getEnemyCode()), campaign.getLocalDate());
if (null != warName) {
int extension = 0;
int roll = Compute.d6();
Expand Down Expand Up @@ -1313,17 +1314,17 @@ public void setEmployerCode(String code, int year) {
public String getEmployerName(int year) {
if (mercSubcontract) {
return "Mercenary (" +
Faction.getFaction(employerCode).getFullName(year) + ")";
Factions.getInstance().getFaction(employerCode).getFullName(year) + ")";
}
return Faction.getFaction(employerCode).getFullName(year);
return Factions.getInstance().getFaction(employerCode).getFullName(year);
}

public String getEnemyCode() {
return enemyCode;
}

public String getEnemyName(int year) {
return Faction.getFaction(enemyCode).getFullName(year);
return Factions.getInstance().getFaction(enemyCode).getFullName(year);
}

public void setEnemyCode(String enemyCode) {
Expand Down Expand Up @@ -1566,7 +1567,7 @@ public AtBContract(Contract c, Campaign campaign) {
missionType = MT_PLANETARYASSAULT;
}
}
Faction f = Faction.getFactionFromFullNameAndYear(c.getEmployer(), campaign.getGameYear());
Faction f = Factions.getInstance().getFactionFromFullNameAndYear(c.getEmployer(), campaign.getGameYear());
if (null == f) {
employerCode = "IND";
} else {
Expand Down
13 changes: 7 additions & 6 deletions MekHQ/src/mekhq/campaign/mission/AtBDynamicScenarioFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import mekhq.campaign.unit.Unit;
import mekhq.campaign.universe.Era;
import mekhq.campaign.universe.Faction;
import mekhq.campaign.universe.Factions;
import mekhq.campaign.universe.Faction.Tag;
import mekhq.campaign.universe.IUnitGenerator;
import mekhq.campaign.universe.Planet;
Expand Down Expand Up @@ -1164,7 +1165,7 @@ public static List<Entity> generateBAForNova(AtBScenario scenario, List<Entity>
// if yes, then pick the fastest mech and load it up, adding the generated BA to the transport relationships.

// non-clan forces and units that aren't stars don't become novas
if (!Faction.getFaction(factionCode).isClan() && (starUnits.size() != 5)) {
if (!Factions.getInstance().getFaction(factionCode).isClan() && (starUnits.size() != 5)) {
return transportedUnits;
}

Expand Down Expand Up @@ -1290,7 +1291,7 @@ public static Entity createEntityWithCrew(String factionCode, int skill, Campaig
en.setOwner(campaign.getPlayer());
en.setGame(campaign.getGame());

Faction faction = Faction.getFaction(factionCode);
Faction faction = Factions.getInstance().getFaction(factionCode);

RandomNameGenerator rng = RandomNameGenerator.getInstance();
rng.setChosenFaction(faction.getNameGenerator());
Expand Down Expand Up @@ -1446,7 +1447,7 @@ private static List<Integer> generateUnitTypes(int unitTypeCode, int unitCount,
int actualUnitType = unitTypeCode;

if (unitTypeCode == ScenarioForceTemplate.SPECIAL_UNIT_TYPE_ATB_MIX) {
Faction faction = Faction.getFaction(factionCode);
Faction faction = Factions.getInstance().getFaction(factionCode);

// "AtB Mix" will skip vehicles if the "use vehicles" checkbox is turned off
// or if the faction is clan and "clan opfors use vehicles" is turned off
Expand Down Expand Up @@ -1544,7 +1545,7 @@ private static List<Integer> generateClanUnitTypes(int unitCount, int forceQuali
* @return Unit weight string.
*/
private static String generateUnitWeights(List<Integer> unitTypes, String faction, int weightClass, int maxWeight, int minWeight, Campaign campaign) {
Faction genFaction = Faction.getFaction(faction);
Faction genFaction = Factions.getInstance().getFaction(faction);
String factionWeightString = AtBConfiguration.ORG_IS;
if (genFaction.isClan() || faction.equals("MH")) {
factionWeightString = AtBConfiguration.ORG_CLAN;
Expand Down Expand Up @@ -2219,7 +2220,7 @@ private static void setScenarioRerolls(AtBDynamicScenario scenario, Campaign cam
* @return "Lance" size.
*/
public static int getLanceSize(String factionCode) {
Faction faction = Faction.getFaction(factionCode);
Faction faction = Factions.getInstance().getFaction(factionCode);
if (faction != null) {
// clans and marian hegemony use a fundamental unit size of 5.
if (faction.isClan() || factionCode.equals("MH")) {
Expand Down Expand Up @@ -2344,7 +2345,7 @@ private static String getPlanetOwnerFaction(AtBContract contract, LocalDate curr
List<String> planetFactions = contract.getSystem().getFactions(currentDate);
if (planetFactions != null && !planetFactions.isEmpty()) {
factionCode = planetFactions.get(0);
Faction ownerFaction = Faction.getFaction(factionCode);
Faction ownerFaction = Factions.getInstance().getFaction(factionCode);

if (ownerFaction.is(Tag.ABANDONED)) {
factionCode = "MERC";
Expand Down
4 changes: 2 additions & 2 deletions MekHQ/src/mekhq/campaign/mission/AtBScenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
import mekhq.campaign.personnel.SkillType;
import mekhq.campaign.rating.IUnitRating;
import mekhq.campaign.unit.Unit;
import mekhq.campaign.universe.Faction;
import mekhq.campaign.universe.Factions;
import mekhq.campaign.universe.Planet;
import mekhq.campaign.universe.PlanetarySystem;
import mekhq.campaign.universe.Systems;
Expand Down Expand Up @@ -1038,7 +1038,7 @@ protected void addLance(List<Entity> list, String faction,
*/
private void addLance(List<Entity> list, String faction, int skill, int quality, int weightClass,
int maxWeight, Campaign campaign, int arrivalTurn) {
if (Faction.getFaction(faction).isClan()) {
if (Factions.getInstance().getFaction(faction).isClan()) {
addStar(list, faction, skill, quality, weightClass, maxWeight, campaign, arrivalTurn);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import mekhq.campaign.personnel.SkillType;
import mekhq.campaign.rating.IUnitRating;
import mekhq.campaign.unit.Unit;
import mekhq.campaign.universe.Faction;
import mekhq.campaign.universe.Factions;

public class AtBScenarioModifierApplicator {

Expand Down Expand Up @@ -191,7 +191,7 @@ public static void adjustSkill(AtBDynamicScenario scenario, Campaign campaign,
rsg.setMethod(RandomSkillsGenerator.M_TAHARQA);
rsg.setLevel(adjustedSkill);

if (Faction.getFaction(scenario.getContract(campaign).getEnemyCode()).isClan()) {
if (Factions.getInstance().getFaction(scenario.getContract(campaign).getEnemyCode()).isClan()) {
rsg.setType(RandomSkillsGenerator.T_CLAN);
}

Expand Down
5 changes: 3 additions & 2 deletions MekHQ/src/mekhq/campaign/personnel/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import mekhq.campaign.unit.Unit;
import mekhq.campaign.work.IPartWork;
import mekhq.campaign.universe.Faction;
import mekhq.campaign.universe.Factions;
import mekhq.campaign.universe.Planet;

/**
Expand Down Expand Up @@ -343,7 +344,7 @@ public Person(String givenName, String surname, String honorific, Campaign campa
secondaryDesignator = ROMDesignation.NONE;
commander = false;
dependent = false;
originFaction = Faction.getFaction(factionCode);
originFaction = Factions.getInstance().getFaction(factionCode);
originPlanet = null;
clan = originFaction.isClan();
phenotype = Phenotype.NONE;
Expand Down Expand Up @@ -1928,7 +1929,7 @@ public static Person generateInstanceFromXML(Node wn, Campaign c, Version versio
} else if (wn2.getNodeName().equalsIgnoreCase("dependent")) {
retVal.dependent = Boolean.parseBoolean(wn2.getTextContent().trim());
} else if (wn2.getNodeName().equalsIgnoreCase("faction")) {
retVal.originFaction = Faction.getFaction(wn2.getTextContent().trim());
retVal.originFaction = Factions.getInstance().getFaction(wn2.getTextContent().trim());
} else if (wn2.getNodeName().equalsIgnoreCase("planetId")) {
String systemId = wn2.getAttributes().getNamedItem("systemId").getTextContent().trim();
String planetId = wn2.getTextContent().trim();
Expand Down
Loading

0 comments on commit acb530c

Please sign in to comment.