JScratch
Loading...
Searching...
No Matches
DndCharacterAPI.java
Go to the documentation of this file.
1package com.burrbox.dndiablo;
2
3import java.util.Arrays;
4import java.util.Random;
5
10public class DndCharacterAPI {
11
15 public static DndCharacterStats createLevel1Character(String name, DNDClass dndClass,
16 int str, int dex, int con,
17 int intel, int wis, int cha) {
19
20 // 1. Assign Basic Info
21 stats.dndclass = dndClass;
22 stats.level = 1;
23 stats.proficiency = 2; // Level 1-4 is always +2
24
25 // 2. Set Ability Scores
26 stats.strengthScore = str;
27 stats.dexterityScore = dex;
28 stats.constitutionScore = con;
29 stats.intelligenceScore = intel;
30 stats.wisdomScore = wis;
31 stats.charismaScore = cha;
32
33 // 3. Calculate Modifiers and Derived Stats
35
36 // 4. Initial HP (Max Hit Die + Constitution Modifier)
37 stats.maxHp = dndClass.hitDie + stats.constitutionModifier;
38 stats.hp = stats.maxHp;
39
40 // 5. Default Armor Class (10 + Dex) - Assuming no armor equipped yet
41 stats.armourClass = 10 + stats.dexterityModifier;
42
43 // 6. Map Saving Throw Proficiencies from Class to Boolean Flags
44 applyClassProficiencies(stats, dndClass);
45
46 return stats;
47 }
48
53 public static void refreshCalculatedStats(DndCharacterStats stats) {
54 stats.strengthModifier = calculateModifier(stats.strengthScore);
55 stats.dexterityModifier = calculateModifier(stats.dexterityScore);
56 stats.constitutionModifier = calculateModifier(stats.constitutionScore);
57 stats.intelligenceModifier = calculateModifier(stats.intelligenceScore);
58 stats.wisdomModifier = calculateModifier(stats.wisdomScore);
59 stats.charismaModifier = calculateModifier(stats.charismaScore);
60
61 // Standard D&D Passive Senses
62 stats.passivePerception = 10 + stats.wisdomModifier;
63 stats.passiveInsight = 10 + stats.wisdomModifier;
64 stats.passiveInvestigation = 10 + stats.intelligenceModifier;
65
66 // Initiative is usually just Dex modifier
67 stats.initiative = stats.dexterityModifier;
68 }
69
73 private static int calculateModifier(int score) {
74 return (int) Math.floor((score - 10) / 2.0);
75 }
76
77 private static void applyClassProficiencies(DndCharacterStats stats, DNDClass dndClass) {
78 // Reset all
79 stats.profStrengthSave = false;
80 stats.profDexteritySave = false;
81 stats.profConstitutionSave = false;
82 stats.profIntelligenceSave = false;
83 stats.profWisdomSave = false;
84 stats.profCharismaSave = false;
85
86 // Apply based on class list
87 if (dndClass.savingThrowProficiencies.contains("Strength")) stats.profStrengthSave = true;
88 if (dndClass.savingThrowProficiencies.contains("Dexterity")) stats.profDexteritySave = true;
89 if (dndClass.savingThrowProficiencies.contains("Constitution")) stats.profConstitutionSave = true;
90 if (dndClass.savingThrowProficiencies.contains("Intelligence")) stats.profIntelligenceSave = true;
91 if (dndClass.savingThrowProficiencies.contains("Wisdom")) stats.profWisdomSave = true;
92 if (dndClass.savingThrowProficiencies.contains("Charisma")) stats.profCharismaSave = true;
93 }
94 private static final Random dice = new Random(2026); //set seed
95
100 public static void levelUp(DndCharacterStats stats) {
101 stats.level++;
102
103 // 1. Proficiency Bonus Scaling (Official D&D 5e Table)
104 if (stats.level >= 17) stats.proficiency = 6;
105 else if (stats.level >= 13) stats.proficiency = 5;
106 else if (stats.level >= 9) stats.proficiency = 4;
107 else if (stats.level >= 5) stats.proficiency = 3;
108 else stats.proficiency = 2;
109
110 // 2. HP Increase: Roll Hit Die + Constitution Modifier (Minimum 1)
111 int hpGain = dice.nextInt(stats.dndclass.hitDie) + 1 + stats.constitutionModifier;
112 if (hpGain < 1) hpGain = 1; // You always gain at least 1 HP
113
114 stats.maxHp += hpGain;
115 stats.hp += hpGain; // Heal by the amount gained
116
118 }
119
120 // --- Dynamic Getters for Rolls ---
121
127 return s.strengthModifier + (s.profStrengthSave ? s.proficiency : 0);
128 }
129
131 return s.dexterityModifier + (s.profDexteritySave ? s.proficiency : 0);
132 }
133
135 return s.constitutionModifier + (s.profConstitutionSave ? s.proficiency : 0);
136 }
137
139 return s.intelligenceModifier + (s.profIntelligenceSave ? s.proficiency : 0);
140 }
141
143 return s.wisdomModifier + (s.profWisdomSave ? s.proficiency : 0);
144 }
145
147 return s.charismaModifier + (s.profCharismaSave ? s.proficiency : 0);
148 }
149
155 public static int[] getStandardArray() {
156 return new int[]{15, 14, 13, 12, 10, 8};
157 }
158
164 public static int[] rollAbilityScores() {
165 int[] scores = new int[6];
166 for (int i = 0; i < 6; i++) {
167 scores[i] = rollSingleAbilityScore();
168 }
169 Arrays.sort(scores); // Optional: sorts them low to high for easier assignment
170 return scores;
171 }
172
173 private static int rollSingleAbilityScore() {
174 int[] rolls = new int[4];
175 int sum = 0;
176 int lowest = 7; // Higher than any possible d6 roll
177
178 for (int i = 0; i < 4; i++) {
179 rolls[i] = dice.nextInt(6) + 1;
180 sum += rolls[i];
181 if (rolls[i] < lowest) {
182 lowest = rolls[i];
183 }
184 }
185 return sum - lowest; // Drop the lowest die
186 }
187
193 public static int[] getPointBuyBaseline() {
194 return new int[]{13, 13, 13, 12, 12, 12};
195 }
196
197 }
List< String > savingThrowProficiencies
Definition DNDClass.java:49
static int getStrengthSaveBonus(DndCharacterStats s)
static int getIntelligenceSaveBonus(DndCharacterStats s)
static void refreshCalculatedStats(DndCharacterStats stats)
static void levelUp(DndCharacterStats stats)
static void applyClassProficiencies(DndCharacterStats stats, DNDClass dndClass)
static int getCharismaSaveBonus(DndCharacterStats s)
static int getWisdomSaveBonus(DndCharacterStats s)
static int getDexteritySaveBonus(DndCharacterStats s)
static int getConstitutionSaveBonus(DndCharacterStats s)
static DndCharacterStats createLevel1Character(String name, DNDClass dndClass, int str, int dex, int con, int intel, int wis, int cha)