Skip to content

Commit 1d80362

Browse files
committed
dded Randomize Traits Option For Character Creation
- Introduced a new option to randomize traits during character creation, including Connections, Reputation, Wealth, and Unlucky scores. - Updated `CampaignOptionsDialog.properties` with labels and tooltips for the new setting. - Extended `RandomSkillPreferences` to support `randomizeTraits` with default settings and XML integration. - Modified personnel generators to calculate and assign random trait values when the option is enabled. - Added methods to the `Person` class for modifying traits with proper clamping to ensure values remain within defined limits. - Updated `AdvancementTab` to include a checkbox for enabling or disabling the new randomization feature.
1 parent 60fec35 commit 1d80362

File tree

7 files changed

+130
-15
lines changed

7 files changed

+130
-15
lines changed

Diff for: MekHQ/resources/mekhq/resources/CampaignOptionsDialog.properties

+11
Original file line numberDiff line numberDiff line change
@@ -1796,6 +1796,17 @@ lblExtraRandomness.tooltip=If checked, an additional 1d6 will be rolled per skil
17961796
<br>\
17971797
<br><b>Warning:</b> Due to the way experience levels are calculated, enabling this option will\
17981798
\ more frequently have characters created with slightly lower than normal experience levels.
1799+
lblRandomizeTraits.text=Randomize Traits \u26A0 \uD83C\uDF1F
1800+
lblRandomizeTraits.tooltip=If checked, a newly created character's Connections, Wealth, Reputation, and Unlucky scores\
1801+
\ are randomized.\
1802+
<br>\
1803+
<br>For <b>Connections</b> a d6 is rolled, on a roll of 6 the character's Connections score becomes 1.\
1804+
<br>\
1805+
<br>For <b>Wealth</b> and <b>Reputation</b> a d6 is rolled (for each), on a roll of 6 the character's score becomes 1.\
1806+
\ While a roll of 1 causes their score to become -1.\
1807+
<br>\
1808+
<br>For <b>Unlucky</b> a d20 is rolled, on a roll of 1 the character's score becomes 1. A high Unlucky score is a bad\
1809+
\ thing.
17991810
lblPhenotypesPanel.text=Clan Trueborn Percentages
18001811
lblMekWarrior.text=MekWarrior
18011812
lblMekWarrior.tooltip=What percentage of Clan MekWarriors should have a Trueborn phenotype?

Diff for: MekHQ/src/mekhq/campaign/RandomSkillPreferences.java

+13
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class RandomSkillPreferences {
5252
private int overallRecruitBonus;
5353
Map<PersonnelRole, Integer> recruitmentBonuses;
5454
private boolean randomizeSkill;
55+
private boolean randomizeTraits;
5556
private boolean useClanBonuses;
5657
private int antiMekProb;
5758
private int[] specialAbilityBonus;
@@ -68,6 +69,7 @@ public RandomSkillPreferences() {
6869
overallRecruitBonus = 0;
6970
recruitmentBonuses = new HashMap<>();
7071
randomizeSkill = true;
72+
randomizeTraits = false;
7173
useClanBonuses = true;
7274
antiMekProb = 10;
7375
combatSmallArmsBonus = -3;
@@ -173,6 +175,14 @@ public boolean randomizeSkill() {
173175
return randomizeSkill;
174176
}
175177

178+
public boolean isRandomizeTraits() {
179+
return randomizeTraits;
180+
}
181+
182+
public void setRandomizeTraits(boolean randomizeTraits) {
183+
this.randomizeTraits = randomizeTraits;
184+
}
185+
176186
public void setUseClanBonuses(boolean b) {
177187
this.useClanBonuses = b;
178188
}
@@ -269,6 +279,7 @@ public void writeToXML(final PrintWriter pw, int indent) {
269279
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "commandSkillsModifier", commandSkillsModifier);
270280
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "roleplaySkillsModifier", roleplaySkillsModifier);
271281
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "randomizeSkill", randomizeSkill);
282+
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "randomizeTraits", randomizeTraits);
272283
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "useClanBonuses", useClanBonuses);
273284
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "antiMekProb", antiMekProb);
274285
MHQXMLUtility.writeSimpleXMLTag(pw, indent, "combatSmallArmsBonus", combatSmallArmsBonus);
@@ -303,6 +314,8 @@ public static RandomSkillPreferences generateRandomSkillPreferencesFromXml(Node
303314
retVal.overallRecruitBonus = Integer.parseInt(wn2.getTextContent().trim());
304315
} else if (wn2.getNodeName().equalsIgnoreCase("randomizeSkill")) {
305316
retVal.randomizeSkill = wn2.getTextContent().equalsIgnoreCase("true");
317+
} else if (wn2.getNodeName().equalsIgnoreCase("randomizeTraits")) {
318+
retVal.randomizeTraits = wn2.getTextContent().equalsIgnoreCase("true");
306319
} else if (wn2.getNodeName().equalsIgnoreCase("useClanBonuses")) {
307320
retVal.useClanBonuses = wn2.getTextContent().equalsIgnoreCase("true");
308321
} else if (wn2.getNodeName().equalsIgnoreCase("antiMekProb")) {

Diff for: MekHQ/src/mekhq/campaign/personnel/Person.java

+48-4
Original file line numberDiff line numberDiff line change
@@ -4916,31 +4916,75 @@ public int getConnections() {
49164916
}
49174917

49184918
public void setConnections(final int connections) {
4919-
this.connections = connections;
4919+
this.connections = clamp(connections, MINIMUM_CONNECTIONS, MAXIMUM_CONNECTIONS);
4920+
}
4921+
4922+
/**
4923+
* Adjusts the person's Connections score by the specified amount.
4924+
*
4925+
* <p>The change in connections can be positive or negative, depending on the provided delta value.</p>
4926+
*
4927+
* @param delta The amount by which to adjust the number of connections. A positive value increases the connections,
4928+
* while a negative value decreases them.
4929+
*/
4930+
public void changeConnections(final int delta) {
4931+
int newValue = connections + delta;
4932+
connections = clamp(newValue, MINIMUM_CONNECTIONS, MAXIMUM_CONNECTIONS);
49204933
}
49214934

49224935
public int getWealth() {
49234936
return wealth;
49244937
}
49254938

49264939
public void setWealth(final int wealth) {
4927-
this.wealth = wealth;
4940+
this.wealth = clamp(wealth, MINIMUM_REPUTATION, MAXIMUM_REPUTATION);
4941+
}
4942+
4943+
/**
4944+
* Adjusts the person's wealth by the specified amount.
4945+
*
4946+
* <p>The change in wealth can be positive or negative, depending on the provided delta value.</p>
4947+
*
4948+
* @param delta The amount by which to adjust the wealth. A positive value increases the wealth, while a negative
4949+
* value decreases it.
4950+
*/
4951+
public void changeWealth(final int delta) {
4952+
int newValue = wealth + delta;
4953+
wealth = clamp(newValue, MINIMUM_WEALTH, MAXIMUM_WEALTH);
49284954
}
49294955

49304956
public int getReputation() {
49314957
return reputation;
49324958
}
49334959

49344960
public void setReputation(final int reputation) {
4935-
this.reputation = reputation;
4961+
this.reputation = clamp(reputation, MINIMUM_REPUTATION, MAXIMUM_REPUTATION);
4962+
}
4963+
4964+
/**
4965+
* Adjusts the person's reputation by the specified amount.
4966+
*
4967+
* <p>The change in reputation can be positive or negative, depending on the provided delta value.</p>
4968+
*
4969+
* @param delta The amount by which to adjust the reputation. A positive value increases the reputation, while a
4970+
* negative value decreases it.
4971+
*/
4972+
public void changeReputation(final int delta) {
4973+
int newValue = reputation + delta;
4974+
reputation = clamp(newValue, MINIMUM_REPUTATION, MAXIMUM_REPUTATION);
49364975
}
49374976

49384977
public int getUnlucky() {
49394978
return unlucky;
49404979
}
49414980

49424981
public void setUnlucky(final int unlucky) {
4943-
this.unlucky = unlucky;
4982+
this.unlucky = clamp(unlucky, MINIMUM_UNLUCKY, MAXIMUM_UNLUCKY);
4983+
}
4984+
4985+
public void changeUnlucky(final int delta) {
4986+
int newValue = unlucky + delta;
4987+
unlucky = clamp(newValue, MINIMUM_UNLUCKY, MAXIMUM_UNLUCKY);
49444988
}
49454989

49464990
/**

Diff for: MekHQ/src/mekhq/campaign/personnel/generator/AbstractSkillGenerator.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
import mekhq.campaign.Campaign;
3636
import mekhq.campaign.RandomSkillPreferences;
3737
import mekhq.campaign.personnel.Person;
38-
import mekhq.campaign.personnel.skills.Skill;
39-
import mekhq.campaign.personnel.skills.SkillType;
4038
import mekhq.campaign.personnel.enums.PersonnelRole;
4139
import mekhq.campaign.personnel.skills.Skill;
4240
import mekhq.campaign.personnel.skills.SkillType;
@@ -78,6 +76,8 @@ public void setSkillPreferences(RandomSkillPreferences skillPreferences) {
7876
*/
7977
public abstract void generateSkills(Campaign campaign, Person person, int expLvl);
8078

79+
public abstract void generateTraits(Person person);
80+
8181
/**
8282
* Generates the default skills for a {@link Person} based on their primary role.
8383
*
@@ -88,7 +88,7 @@ public void setSkillPreferences(RandomSkillPreferences skillPreferences) {
8888
* @param rollModifier A roll modifier to apply to any randomizations.
8989
*/
9090
protected void generateDefaultSkills(Person person, PersonnelRole primaryRole, int expLvl, int bonus,
91-
int rollModifier) {
91+
int rollModifier) {
9292
switch (primaryRole) {
9393
case MEKWARRIOR:
9494
addSkill(person, SkillType.S_PILOT_MEK, expLvl, rskillPrefs.randomizeSkill(), bonus, rollModifier);
@@ -209,12 +209,12 @@ public static void addSkill(Person person, String skillName, int level, int bonu
209209
}
210210

211211
protected static void addSkill(Person person, String skillName, int experienceLevel, boolean randomizeLevel,
212-
int bonus) {
212+
int bonus) {
213213
addSkill(person, skillName, experienceLevel, randomizeLevel, bonus, 0);
214214
}
215215

216216
protected static void addSkill(Person person, String skillName, int experienceLevel, boolean randomizeLevel,
217-
int bonus, int rollMod) {
217+
int bonus, int rollMod) {
218218
if (randomizeLevel) {
219219
person.addSkill(skillName, Skill.randomizeLevel(skillName, experienceLevel, bonus, rollMod));
220220
} else {

Diff for: MekHQ/src/mekhq/campaign/personnel/generator/DefaultPersonnelGenerator.java

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public Person generate(Campaign campaign, PersonnelRole primaryRole, PersonnelRo
103103

104104
AbstractSkillGenerator skillGenerator = new DefaultSkillGenerator(getSkillPreferences());
105105
skillGenerator.generateSkills(campaign, person, expLvl);
106+
skillGenerator.generateTraits(person);
106107

107108
// Limit skills by age for children and adolescents
108109
int age = person.getAge(campaign.getLocalDate());

Diff for: MekHQ/src/mekhq/campaign/personnel/generator/DefaultSkillGenerator.java

+42-3
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@
2727
*/
2828
package mekhq.campaign.personnel.generator;
2929

30+
import static megamek.common.Compute.d6;
31+
import static megamek.common.Compute.randomInt;
3032
import static mekhq.campaign.personnel.skills.SkillDeprecationTool.DEPRECATED_SKILLS;
3133
import static mekhq.campaign.personnel.skills.enums.SkillSubType.SUPPORT_COMMAND;
3234

3335
import java.util.ArrayList;
3436
import java.util.List;
3537

36-
import megamek.common.Compute;
3738
import mekhq.Utilities;
3839
import mekhq.campaign.Campaign;
3940
import mekhq.campaign.CampaignOptions;
4041
import mekhq.campaign.RandomSkillPreferences;
4142
import mekhq.campaign.personnel.Person;
42-
import mekhq.campaign.personnel.skills.SkillType;
4343
import mekhq.campaign.personnel.enums.PersonnelRole;
4444
import mekhq.campaign.personnel.skills.SkillType;
4545

@@ -147,9 +147,48 @@ public void generateSkills(final Campaign campaign, final Person person, final i
147147
}
148148
}
149149

150-
String selSkill = possibleSkills.get(Compute.randomInt(possibleSkills.size()));
150+
String selSkill = possibleSkills.get(randomInt(possibleSkills.size()));
151151
int secondLvl = Utilities.generateExpLevel(rskillPrefs.getSecondSkillBonus());
152152
addSkill(person, selSkill, secondLvl, rskillPrefs.randomizeSkill(), 0);
153153
}
154154
}
155+
156+
/**
157+
* Generates traits for the specified person based on random or pre-determined criteria.
158+
*
159+
* <p>When randomization is enabled, this method calculates and assigns specific traits such as connections,
160+
* reputation, wealth, and bad luck using random rolls. Each trait has its own set of rules for adjustment.</p>
161+
*
162+
* @param person The person whose traits will be updated. Traits are adjusted based on random rolls when
163+
* randomization is enabled.
164+
*/
165+
@Override
166+
public void generateTraits(Person person) {
167+
if (!getSkillPreferences().isRandomizeTraits()) {
168+
return;
169+
}
170+
171+
// Connections
172+
if (d6() == 6) {
173+
person.changeConnections(1);
174+
}
175+
176+
// Reputation
177+
int roll = d6();
178+
if (roll == 6 || roll == 1) {
179+
person.changeReputation(roll == 6 ? 1 : -1);
180+
}
181+
182+
// Wealth
183+
roll = d6();
184+
if (roll == 6 || roll == 1) {
185+
person.changeWealth(roll == 6 ? 1 : -1);
186+
}
187+
188+
// Unlucky
189+
roll = randomInt(20);
190+
if (roll == 0) {
191+
person.changeUnlucky(1);
192+
}
193+
}
155194
}

Diff for: MekHQ/src/mekhq/gui/campaignOptions/contents/AdvancementTab.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import mekhq.campaign.Campaign;
4444
import mekhq.campaign.CampaignOptions;
4545
import mekhq.campaign.RandomSkillPreferences;
46-
import mekhq.campaign.personnel.skills.SkillType;
4746
import mekhq.campaign.personnel.enums.PersonnelRole;
4847
import mekhq.campaign.personnel.enums.Phenotype;
4948
import mekhq.campaign.personnel.skills.SkillType;
@@ -114,6 +113,7 @@ public class AdvancementTab {
114113

115114
//start Skill Randomization Tab
116115
private JCheckBox chkExtraRandomness;
116+
private JCheckBox chkRandomizeTraits;
117117

118118
private JPanel pnlPhenotype;
119119
private JLabel[] phenotypeLabels;
@@ -520,6 +520,7 @@ private JPanel createAdministratorsPanel() {
520520
*/
521521
private void initializeSkillRandomizationTab() {
522522
chkExtraRandomness = new JCheckBox();
523+
chkRandomizeTraits = new JCheckBox();
523524

524525
pnlPhenotype = new JPanel();
525526
phenotypeLabels = new JLabel[] {}; // This will be initialized properly later
@@ -604,6 +605,7 @@ public JPanel skillRandomizationTab() {
604605

605606
// Contents
606607
chkExtraRandomness = new CampaignOptionsCheckBox("ExtraRandomness");
608+
chkRandomizeTraits = new CampaignOptionsCheckBox("RandomizeTraits");
607609

608610
pnlPhenotype = createPhenotypePanel();
609611
pnlRandomAbilities = createAbilityPanel();
@@ -621,6 +623,9 @@ public JPanel skillRandomizationTab() {
621623
layout.gridwidth = 1;
622624
panel.add(chkExtraRandomness, layout);
623625

626+
layout.gridy++;
627+
panel.add(chkRandomizeTraits, layout);
628+
624629
layout.gridx = 0;
625630
layout.gridy++;
626631
panel.add(pnlPhenotype, layout);
@@ -1100,7 +1105,7 @@ public void loadValuesFromCampaignOptions() {
11001105
* {@code null}, values are loaded from the current skill preferences.
11011106
*/
11021107
public void loadValuesFromCampaignOptions(@Nullable CampaignOptions presetCampaignOptions,
1103-
@Nullable RandomSkillPreferences presetRandomSkillPreferences) {
1108+
@Nullable RandomSkillPreferences presetRandomSkillPreferences) {
11041109
CampaignOptions options = presetCampaignOptions;
11051110
if (presetCampaignOptions == null) {
11061111
options = this.campaignOptions;
@@ -1132,6 +1137,7 @@ public void loadValuesFromCampaignOptions(@Nullable CampaignOptions presetCampai
11321137

11331138
//start Skill Randomization Tab
11341139
chkExtraRandomness.setSelected(skillPreferences.randomizeSkill());
1140+
chkRandomizeTraits.setSelected(skillPreferences.isRandomizeTraits());
11351141
final int[] phenotypeProbabilities = options.getPhenotypeProbabilities();
11361142
for (int i = 0; i < phenotypeSpinners.length; i++) {
11371143
phenotypeSpinners[i].setValue(phenotypeProbabilities[i]);
@@ -1184,7 +1190,7 @@ public void loadValuesFromCampaignOptions(@Nullable CampaignOptions presetCampai
11841190
* {@code null}, values are applied to the current skill preferences.
11851191
*/
11861192
public void applyCampaignOptionsToCampaign(@Nullable CampaignOptions presetCampaignOptions,
1187-
@Nullable RandomSkillPreferences presetRandomSkillPreferences) {
1193+
@Nullable RandomSkillPreferences presetRandomSkillPreferences) {
11881194
CampaignOptions options = presetCampaignOptions;
11891195
if (presetCampaignOptions == null) {
11901196
options = this.campaignOptions;
@@ -1216,6 +1222,7 @@ public void applyCampaignOptionsToCampaign(@Nullable CampaignOptions presetCampa
12161222

12171223
//start Skill Randomization Tab
12181224
skillPreferences.setRandomizeSkill(chkExtraRandomness.isSelected());
1225+
skillPreferences.setRandomizeTraits(chkRandomizeTraits.isSelected());
12191226
for (int i = 0; i < phenotypeSpinners.length; i++) {
12201227
options.setPhenotypeProbability(i, (int) phenotypeSpinners[i].getValue());
12211228
}

0 commit comments

Comments
 (0)