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

210: One Person will be Artillery Trained when hiring minimum complement for an artillery-armed unit #2887

Merged
merged 2 commits into from
Sep 29, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
*/
public abstract class AbstractSkillGenerator {

private RandomSkillPreferences rskillPrefs = new RandomSkillPreferences();
private RandomSkillPreferences rskillPrefs;

protected AbstractSkillGenerator(final RandomSkillPreferences randomSkillPreferences) {
this.rskillPrefs = randomSkillPreferences;
}

/**
* Gets the {@link RandomSkillPreferences}.
Expand Down Expand Up @@ -163,6 +167,17 @@ protected void generateDefaultSkills(Person person, PersonnelRole primaryRole, i
}
}

public void generateArtillerySkill(final Person person) {
generateArtillerySkill(person, getPhenotypeBonus(person));
}

protected void generateArtillerySkill(final Person person, final int bonus) {
final int experienceLevel = Utilities.generateExpLevel(rskillPrefs.getArtilleryBonus());
if (experienceLevel > SkillType.EXP_ULTRA_GREEN) {
addSkill(person, SkillType.S_ARTILLERY, experienceLevel, rskillPrefs.randomizeSkill(), bonus);
}
}

protected static void addSkill(Person person, String skillName, int level, int bonus) {
person.addSkill(skillName, new Skill(skillName, level, bonus));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public Person generate(Campaign campaign, PersonnelRole primaryRole, PersonnelRo
generateBirthday(campaign, person, expLvl, person.isClanner()
&& person.getPhenotype() != Phenotype.NONE);

AbstractSkillGenerator skillGenerator = new DefaultSkillGenerator();
skillGenerator.setSkillPreferences(getSkillPreferences());
AbstractSkillGenerator skillGenerator = new DefaultSkillGenerator(getSkillPreferences());
skillGenerator.generateSkills(person, expLvl);

AbstractSpecialAbilityGenerator specialAbilityGenerator = new DefaultSpecialAbilityGenerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
import mekhq.campaign.personnel.enums.PersonnelRole;

public class DefaultSkillGenerator extends AbstractSkillGenerator {
//region Constructors
public DefaultSkillGenerator(final RandomSkillPreferences randomSkillPreferences) {
super(randomSkillPreferences);
}
//endregion Constructors

@Override
public void generateSkills(Person person, int expLvl) {
Expand Down Expand Up @@ -74,10 +79,7 @@ public void generateSkills(Person person, int expLvl) {
if (getCampaignOptions(person).useArtillery()
&& (primaryRole.isMechWarrior() || primaryRole.isVehicleGunner() || primaryRole.isSoldier())
&& Utilities.rollProbability(rskillPrefs.getArtilleryProb())) {
int artyLvl = Utilities.generateExpLevel(rskillPrefs.getArtilleryBonus());
if (artyLvl > SkillType.EXP_ULTRA_GREEN) {
addSkill(person, SkillType.S_ARTILLERY, artyLvl, rskillPrefs.randomizeSkill(), bonus);
}
generateArtillerySkill(person, bonus);
}

// roll random secondary skill
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@
package mekhq.campaign.unit.actions;

import megamek.common.*;
import mekhq.Utilities;
import mekhq.campaign.Campaign;
import mekhq.campaign.personnel.Person;
import mekhq.campaign.personnel.SkillType;
import mekhq.campaign.personnel.enums.PersonnelRole;
import mekhq.campaign.personnel.generator.DefaultSkillGenerator;
import mekhq.campaign.unit.Unit;

import java.util.List;

/**
* Hires a full complement of personnel for a unit.
*/
Expand Down Expand Up @@ -144,8 +149,19 @@ public void execute(Campaign campaign, Unit unit) {
unit.setTechOfficer(p);
}

// Ensure we generate at least one person with the artillery skill if using that skill and
// the unit has an artillery weapon
if (campaign.getCampaignOptions().useArtillery() && (unit.getEntity() != null)
&& unit.getEntity().getWeaponList().stream()
.anyMatch(weapon -> (weapon.getType() instanceof WeaponType)
&& (((WeaponType) weapon.getType()).getDamage() == WeaponType.DAMAGE_ARTILLERY))) {
final List<Person> gunners = unit.getGunners();
if (!gunners.isEmpty() && gunners.stream().noneMatch(person -> person.getSkills().hasSkill(SkillType.S_ARTILLERY))) {
NickAragua marked this conversation as resolved.
Show resolved Hide resolved
new DefaultSkillGenerator(campaign.getRandomSkillPreferences()).generateArtillerySkill(Utilities.getRandomItem(gunners));
}
}

unit.resetPilotAndEntity();
unit.runDiagnostic(false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static org.mockito.Mockito.*;

import mekhq.campaign.CampaignOptions;
import mekhq.campaign.personnel.enums.PersonnelRole;
import org.junit.Test;

Expand All @@ -38,6 +39,9 @@ public class HirePersonnelUnitActionTest {
@Test
public void fullUnitTakesNoAction() {
Campaign mockCampaign = mock(Campaign.class);
CampaignOptions mockOptions = mock(CampaignOptions.class);
doReturn(mockOptions).when(mockCampaign).getCampaignOptions();
doReturn(false).when(mockOptions).useArtillery();
Entity mockEntity = mock(Mech.class);
Unit unit = spy(new Unit(mockEntity, mockCampaign));

Expand All @@ -59,6 +63,9 @@ public void fullUnitTakesNoAction() {
@Test
public void actionPassesAlongGMSetting() {
Campaign mockCampaign = mock(Campaign.class);
CampaignOptions mockOptions = mock(CampaignOptions.class);
doReturn(mockOptions).when(mockCampaign).getCampaignOptions();
doReturn(false).when(mockOptions).useArtillery();
Entity mockEntity = mock(Mech.class);
Unit unit = spy(new Unit(mockEntity, mockCampaign));

Expand Down Expand Up @@ -87,6 +94,9 @@ public void actionPassesAlongGMSetting() {
@Test
public void mechNeedingDriverAddsDriver() {
Campaign mockCampaign = mock(Campaign.class);
CampaignOptions mockOptions = mock(CampaignOptions.class);
doReturn(mockOptions).when(mockCampaign).getCampaignOptions();
doReturn(false).when(mockOptions).useArtillery();
Entity mockEntity = mock(Mech.class);
Unit unit = spy(new Unit(mockEntity, mockCampaign));

Expand Down Expand Up @@ -143,6 +153,9 @@ public void mechDoesntAddDriverIfRecruitFails() {
@Test
public void lamNeedingDriverAddsDriver() {
Campaign mockCampaign = mock(Campaign.class);
CampaignOptions mockOptions = mock(CampaignOptions.class);
doReturn(mockOptions).when(mockCampaign).getCampaignOptions();
doReturn(false).when(mockOptions).useArtillery();
Entity mockEntity = mock(LandAirMech.class);
Unit unit = spy(new Unit(mockEntity, mockCampaign));

Expand Down Expand Up @@ -171,6 +184,9 @@ public void lamNeedingDriverAddsDriver() {
@Test
public void mechNeedingGunnerAddsGunner() {
Campaign mockCampaign = mock(Campaign.class);
CampaignOptions mockOptions = mock(CampaignOptions.class);
doReturn(mockOptions).when(mockCampaign).getCampaignOptions();
doReturn(false).when(mockOptions).useArtillery();
Entity mockEntity = mock(Mech.class);
Unit unit = spy(new Unit(mockEntity, mockCampaign));

Expand Down Expand Up @@ -198,6 +214,9 @@ public void mechNeedingGunnerAddsGunner() {
@Test
public void tankNeedingGunnerAddsGunner() {
Campaign mockCampaign = mock(Campaign.class);
CampaignOptions mockOptions = mock(CampaignOptions.class);
doReturn(mockOptions).when(mockCampaign).getCampaignOptions();
doReturn(false).when(mockOptions).useArtillery();
Entity mockEntity = mock(Tank.class);
Unit unit = spy(new Unit(mockEntity, mockCampaign));

Expand Down Expand Up @@ -225,6 +244,9 @@ public void tankNeedingGunnerAddsGunner() {
@Test
public void spaceShipNeedingGunnerAddsGunner() {
Campaign mockCampaign = mock(Campaign.class);
CampaignOptions mockOptions = mock(CampaignOptions.class);
doReturn(mockOptions).when(mockCampaign).getCampaignOptions();
doReturn(false).when(mockOptions).useArtillery();
Entity mockEntity = mock(Jumpship.class);
Unit unit = spy(new Unit(mockEntity, mockCampaign));

Expand Down Expand Up @@ -252,6 +274,9 @@ public void spaceShipNeedingGunnerAddsGunner() {
@Test
public void smallCraftNeedingGunnerAddsGunner() {
Campaign mockCampaign = mock(Campaign.class);
CampaignOptions mockOptions = mock(CampaignOptions.class);
doReturn(mockOptions).when(mockCampaign).getCampaignOptions();
doReturn(false).when(mockOptions).useArtillery();
Entity mockEntity = mock(SmallCraft.class);
Unit unit = spy(new Unit(mockEntity, mockCampaign));

Expand Down Expand Up @@ -279,6 +304,9 @@ public void smallCraftNeedingGunnerAddsGunner() {
@Test
public void spaceShipNeedingCrewAddsCrew() {
Campaign mockCampaign = mock(Campaign.class);
CampaignOptions mockOptions = mock(CampaignOptions.class);
doReturn(mockOptions).when(mockCampaign).getCampaignOptions();
doReturn(false).when(mockOptions).useArtillery();
Entity mockEntity = mock(Jumpship.class);
when(mockEntity.isSupportVehicle()).thenReturn(false);
Unit unit = spy(new Unit(mockEntity, mockCampaign));
Expand Down Expand Up @@ -307,6 +335,9 @@ public void spaceShipNeedingCrewAddsCrew() {
@Test
public void supportVehicleNeedingCrewAddsCrew() {
Campaign mockCampaign = mock(Campaign.class);
CampaignOptions mockOptions = mock(CampaignOptions.class);
doReturn(mockOptions).when(mockCampaign).getCampaignOptions();
doReturn(false).when(mockOptions).useArtillery();
Entity mockEntity = mock(SupportTank.class);
when(mockEntity.isSupportVehicle()).thenReturn(true);
Unit unit = spy(new Unit(mockEntity, mockCampaign));
Expand Down Expand Up @@ -335,6 +366,9 @@ public void supportVehicleNeedingCrewAddsCrew() {
@Test
public void spaceShipNeedingNavigatorAddsNavigator() {
Campaign mockCampaign = mock(Campaign.class);
CampaignOptions mockOptions = mock(CampaignOptions.class);
doReturn(mockOptions).when(mockCampaign).getCampaignOptions();
doReturn(false).when(mockOptions).useArtillery();
Entity mockEntity = mock(Jumpship.class);
when(mockEntity.isSupportVehicle()).thenReturn(false);
Unit unit = spy(new Unit(mockEntity, mockCampaign));
Expand Down Expand Up @@ -363,6 +397,9 @@ public void spaceShipNeedingNavigatorAddsNavigator() {
@Test
public void mechNeedingTechOfficerAddsTechOfficer() {
Campaign mockCampaign = mock(Campaign.class);
CampaignOptions mockOptions = mock(CampaignOptions.class);
doReturn(mockOptions).when(mockCampaign).getCampaignOptions();
doReturn(false).when(mockOptions).useArtillery();
Entity mockEntity = mock(Mech.class);
Unit unit = spy(new Unit(mockEntity, mockCampaign));

Expand Down Expand Up @@ -390,6 +427,9 @@ public void mechNeedingTechOfficerAddsTechOfficer() {
@Test
public void tankNeedingTechOfficerAddsTechOfficer() {
Campaign mockCampaign = mock(Campaign.class);
CampaignOptions mockOptions = mock(CampaignOptions.class);
doReturn(mockOptions).when(mockCampaign).getCampaignOptions();
doReturn(false).when(mockOptions).useArtillery();
Entity mockEntity = mock(Tank.class);
Unit unit = spy(new Unit(mockEntity, mockCampaign));

Expand Down