Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added initiativeMaxBonus to Campaign #5260

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions MekHQ/src/mekhq/campaign/Campaign.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ public class Campaign implements ITechManager {
private int crimePirateModifier;
private LocalDate dateOfLastCrime;
private int initiativeBonus;
private int initiativeMaxBonus;
private final CampaignSummary campaignSummary;
private final Quartermaster quartermaster;
private StoryArc storyArc;
Expand Down Expand Up @@ -308,6 +309,7 @@ public Campaign() {
crimePirateModifier = 0;
dateOfLastCrime = null;
initiativeBonus = 0;
initiativeMaxBonus = 1;
setRankSystemDirect(Ranks.getRankSystemFromCode(Ranks.DEFAULT_SYSTEM_CODE));
forces = new Force(name);
forceIds.put(0, forces);
Expand Down Expand Up @@ -4467,19 +4469,42 @@ private void processReputationChanges() {
}
}

public int getInitiativeBonus(){
public int getInitiativeBonus() {
return initiativeBonus;
}
public void setInitiativeBonus(int bonus){

public void setInitiativeBonus(int bonus) {
initiativeBonus = bonus;
}

public void initiativeBonusIncrement(boolean change){
if(change){
public void applyInitiativeBonus(int bonus) {
if (bonus > initiativeMaxBonus) {
initiativeMaxBonus = bonus;
}
if ((bonus + initiativeBonus) > initiativeMaxBonus) {
initiativeBonus = initiativeMaxBonus;
} else {
initiativeBonus += bonus;
}
}

public void initiativeBonusIncrement(boolean change) {
if (change) {
setInitiativeBonus(++initiativeBonus);
}else{
setInitiativeBonus(--initiativeBonus);
} else {
setInitiativeBonus(--initiativeBonus);
}
if (initiativeBonus > initiativeMaxBonus) {
initiativeBonus = initiativeMaxBonus;
}
}

public int getInitiativeMaxBonus() {
return initiativeMaxBonus;
}

public void setInitiativeMaxBonus(int bonus) {
initiativeMaxBonus = bonus;
}

/**
Expand Down Expand Up @@ -5506,7 +5531,8 @@ public void writeToXML(final PrintWriter pw) {
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "lastMissionId", lastMissionId);
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "lastScenarioId", lastScenarioId);
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "calendar", getLocalDate());
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "InitiativeBonus", initiativeBonus);
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "initiativeBonus", initiativeBonus);
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "initiativeMaxBonus", initiativeMaxBonus);
MHQXMLUtility.writeSimpleXMLOpenTag(pw, indent++, "nameGen");
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "faction", RandomNameGenerator.getInstance().getChosenFaction());
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "percentFemale", RandomGenderGenerator.getPercentFemale());
Expand Down
2 changes: 2 additions & 0 deletions MekHQ/src/mekhq/campaign/io/CampaignXmlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,8 @@
retVal.setCrimeRating(Integer.parseInt(wn.getTextContent()));
} else if (xn.equalsIgnoreCase("initiativeBonus")) {
retVal.setInitiativeBonus(Integer.parseInt(wn.getTextContent()));
} else if (xn.equalsIgnoreCase("initiativeMaxBonus")) {

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note

Invoking
Campaign.setFactionCode
should be avoided because it has been deprecated.
retVal.setInitiativeMaxBonus(Integer.parseInt(wn.getTextContent()));
} else if (xn.equalsIgnoreCase("crimePirateModifier")) {
retVal.setCrimePirateModifier(Integer.parseInt(wn.getTextContent()));
} else if (xn.equalsIgnoreCase("dateOfLastCrime")) {
Expand Down
25 changes: 25 additions & 0 deletions MekHQ/unittests/mekhq/campaign/CampaignTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package mekhq.campaign;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
Expand Down Expand Up @@ -182,4 +183,28 @@ void testTransportShips() {
// Ensure it was removed
assertTrue(campaign.getTransportShips().isEmpty());
}

@Test
void testIniative() {
Campaign campaign = new Campaign();

campaign.applyInitiativeBonus(6);
// should increase bonus to 6 and max to 6
assertTrue(campaign.getInitiativeBonus() == 6);
assertTrue(campaign.getInitiativeMaxBonus() == 6);
// Should not be able to increment over max of 6
campaign.initiativeBonusIncrement(true);
assertFalse(campaign.getInitiativeBonus() == 7);
campaign.applyInitiativeBonus(2);
assertEquals(campaign.getInitiativeBonus(), 6);
// But should be able to decrease below max
campaign.initiativeBonusIncrement(false);
assertEquals(campaign.getInitiativeBonus(), 5);
// After setting lower Max Bonus any appied bonus thats less then max should set
// bonus to max
campaign.setInitiativeMaxBonus(3);
campaign.applyInitiativeBonus(2);
assertEquals(campaign.getInitiativeBonus(), 3);

}
}